1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
|
#! /usr/bin/env python
##############################################################################
## DendroPy Phylogenetic Computing Library.
##
## Copyright 2010-2015 Jeet Sukumaran and Mark T. Holder.
## All rights reserved.
##
## See "LICENSE.rst" for terms and conditions of usage.
##
## If you use this work or any portion thereof in published work,
## please cite it as:
##
## Sukumaran, J. and M. T. Holder. 2010. DendroPy: a Python library
## for phylogenetic computing. Bioinformatics 26: 1569-1571.
##
##############################################################################
"""
Various data structures.
"""
import collections
import copy
import sys
import csv
###############################################################################
## OrderedSet
class OrderedSet(object):
"""
Ordered collection of unique objects with O(1) look-ups, addition, and
deletion.
"""
def __init__(self, *args):
self._item_list = []
self._item_set = set()
if len(args) > 1:
raise TypeError("OrderedSet expected at most 1 arguments, got 2")
elif len(args) == 1:
for a in args[0]:
if a not in self._item_set:
self._item_set.add(a)
self._item_list.append(a)
def __copy__(self, memo=None):
o = OrderedSet(self._item_list)
return o
def __deepcopy__(self, memo=None):
other = self.__class__()
memo[id(self)] = other
memo[id(self._item_set)] = other._item_set
memo[id(self._item_list)] = other._item_list
for item in self._item_list:
c = copy.deepcopy(item, memo)
memo[id(item)] = c
other._item_set.add(c)
other._item_list.append(c)
for k in self.__dict__:
if k in other.__dict__:
continue
other.__dict__[k] = copy.deepcopy(self.__dict__[k], memo)
memo[id(self.__dict__[k])] = other.__dict__[k]
return other
def __len__(self):
return len(self._item_list)
def __getitem__(self, index):
"""
Returns value at ``index``.
Note takes *index* of than value as key.
"""
return self._item_list[index]
def __setitem__(self, index, value):
"""
Sets value at ``index``.
Note takes *index* of than value as key.
"""
item = self._item_list[index]
self._item_set.remove(item)
self._item_set.add(value)
self._item_list[index] = value
def __delitem__(self, index):
"""
Deletes value at ``index``.
Note takes *index* of than value as key.
"""
self._item_set.remove(self._item_list[index])
del self._item_list[index]
def discard(self, key):
"""
Deletes value of ``key`` from ``self``.
No error if no value of ``key`` is not in ``self``.
"""
if key in self._item_set:
self._item_set.remove(key)
self._item_list.remove(key)
def remove(self, key):
"""
Deletes value of ``key`` from ``self``.
KeyErrorif no value of ``key`` is not in ``self``.
"""
self._item_set.remove(key)
self._item_list.remove(key)
def __iter__(self):
"""
Returns iterator over values in ``self``.
"""
return iter(self._item_list)
def next(self):
"""
Returns iterator over values in ``self``.
"""
return self.__iter__()
def __reversed__(self):
"""
Returns `OrderedSet` with values in reversed order.
"""
return OrderedSet(reversed(self._item_list))
def __add__(self, other):
"""
Returns `OrderedSet` consisting of union of values in ``self``
and ``other``.
"""
v = self._item_list + other._item_list
return OrderedSet(v)
def index(self, value):
"""
Returns index of element with value of ``value``.
"""
return self._item_list.index(value)
def __contains__(self, value):
"""
Returns |True| if ``value`` is in ``self`` or |False| otherwise.
"""
return value in self._item_set
def add(self, value):
"""
Adds a new element, ``value``, to ``self`` if ``value`` is not already in
``self``.
"""
if value not in self._item_set:
self._item_set.add(value)
self._item_list.append(value)
return value
else:
return None
def update(self, other):
"""
Updates ``self`` with values in ``other`` for each value in ``other`` that is
not already in ``self``.
"""
for i in other:
if i not in self._item_set:
self._item_set.add(i)
self._item_list.append(i)
def __str__(self):
return "[{}]".format(", ".join([str(i) for i in self._item_list]))
def __repr__(self):
return "{}([{}])".format(self.__class__.__name__,
", ".join([str(i) for i in self._item_list]))
def __hash__(self):
return id(self)
# return hash( (t for t in self._item_list) )
def __eq__(self, o):
return self._item_list == o._item_list
def __lt__(self, o):
return self._item_list < o._item_list
def pop(self, last=True):
"""
Removes and return value in ``self``.
By default, removes last value.
"""
if not self._item_set:
raise KeyError('OrderedSet is empty')
if last:
key = self._item_list[-1]
else:
key = self._item_list[0]
self.discard(key)
return key
def clear(self):
self._item_set = set()
self._item_list = []
###############################################################################
## NormalizedBitmaskDict
class NormalizedBitmaskDict(collections.OrderedDict):
"""
Keys, {K_i}, are longs. ``fill_bitmask`` must be provided before elements can be
added removed from dictionary. All keys are normalized such that the
least- significant bit is '0'. That is, if the key's least-significant bit
is '0', it is added as-is, otherwise it is complemented by XOR'ing it with
'fill_bitmask'.
"""
def least_significant_set_bit(s):
"""
Returns least-significant bit in integer 's' that is set.
"""
m = s & (s - 1)
return m ^ s
least_significant_set_bit = staticmethod(least_significant_set_bit)
# this is for the least-significant-bit-is-1 normalization convention
# def normalize(key, fill_bitmask, lowest_relevant_bit):
# if key & lowest_relevant_bit:
# return key & fill_bitmask # keep least-significant bit to 1
# else:
# return (~key) & fill_bitmask # force least-significant bit as 1
# normalize = staticmethod(normalize)
# this is for the least-significant-bit-is-0 normalization convention
def normalize(key, fill_bitmask, lowest_relevant_bit):
if key & lowest_relevant_bit:
return (~key) & fill_bitmask # force least-significant bit to 0
else:
return key & fill_bitmask # keep least-significant bit as 0
normalize = staticmethod(normalize)
def __init__(self, other=None, fill_bitmask=None):
"""
Parameters
----------
fill_bitmask : integer
A bitmask where all possible bits that can be set to 1 are set to 1.
When representing a taxon namespaces, with 8 taxa, for example,
this would be 0b11111111. Incomplete leaf-sets on trees need to
having the missing taxa bits set to 0. For example, for a tree
missing taxa 2, 3, and 5, ``fill_bitmask`` would be 0b11101001.
"""
collections.OrderedDict.__init__(self)
self.lowest_relevant_bit = NormalizedBitmaskDict.least_significant_set_bit(fill_bitmask)
self.fill_bitmask = fill_bitmask
if other is not None:
if isinstance(other, NormalizedBitmaskDict):
self.fill_bitmask = other.fill_bitmask
self.lowest_relevant_bit = other.lowest_relevant_bit
if isinstance(other, dict):
for key, val in other.items():
self[key] = val
def __deepcopy__(self, memo):
o = NormalizedBitmaskDict(fill_bitmask=self.fill_bitmask)
memo[id(self)] = o
o.fill_bitmask = self.fill_bitmask
o.lowest_relevant_bit = self.lowest_relevant_bit
for key, val in self.items():
o[key] = copy.deepcopy(val, memo)
return o
def normalize_key_and_assign_value(self, key, value):
"*Almost* like __setitem__(), but returns value of normalized key to calling code."
normalized_key = self.normalize_key(key)
dict.__setitem__(self, normalized_key, value)
return normalized_key
def normalize_key(self, key):
return NormalizedBitmaskDict.normalize(key, self.fill_bitmask, self.lowest_relevant_bit)
def __setitem__(self, key, value):
"Sets item with normalized key."
self.normalize_key_and_assign_value(key, value)
def __getitem__(self, key):
"Gets an item by its key."
key = self.normalize_key(key)
return dict.__getitem__(self, key)
def __delitem__(self, key):
"Remove item with normalized key."
key = self.normalize_key(key)
dict.__delitem__(self, key)
def __contains__(self, key):
"Returns true if has normalized key."
key = self.normalize_key(key)
return dict.__contains__(self, key)
def pop(self, key, alt_val=None):
"a.pop(k[, x]): a[k] if k in a, else x (and remove k)"
key = self.normalize_key(key)
return dict.pop(self, key)
def get(self, key, def_val=None):
"Gets an item by its key, returning default if key not present."
key = self.normalize_key(key)
return dict.get(self, key, def_val)
def setdefault(self, key, def_val=None):
"Sets the default value to return if key not present."
dict.setdefault(self, self.normalize_key(key), def_val)
def update(self, other):
"""
updates (and overwrites) key/value pairs:
k = { 'a':'A', 'b':'B', 'c':'C'}
q = { 'c':'C', 'd':'D', 'f':'F'}
k.update(q)
{'a': 'A', 'c': 'C', 'b': 'B', 'd': 'D', 'f': 'F'}
"""
for key, val in other.items():
dict.__setitem__(self, self.normalize_key(key), val)
def fromkeys(self, iterable, value=None):
"Creates a new dictionary with keys from seq and values set to value."
raise NotImplementedError
###############################################################################
# CaseInsensitiveDict
#
# From:
# https://github.com/kennethreitz/requests
#
# Copyright 2014 Kenneth Reitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
class CaseInsensitiveDict(collections.MutableMapping):
"""
A case-insensitive ``dict``-like object.
Implements all methods and operations of
``collections.MutableMapping`` as well as dict's ``copy``. Also
provides ``lower_items``.
All keys are expected to be strings. The structure remembers the
case of the last key to be set, and ``iter(instance)``,
``keys()``, ``items()``, ``iterkeys()``, and ``iteritems()``
will contain case-sensitive keys. However, querying and contains
testing is case insensitive:
cid = CaseInsensitiveDict()
cid['Accept'] = 'application/json'
cid['aCCEPT'] == 'application/json' # True
list(cid) == ['Accept'] # True
For example, ``headers['content-encoding']`` will return the
value of a ``'Content-Encoding'`` response header, regardless
of how the header name was originally stored.
If the constructor, ``.update``, or equality comparison
operations are given keys that have equal ``.lower()``s, the
behavior is undefined.
"""
def __init__(self, data=None, **kwargs):
self._store = dict()
if data is None:
data = {}
self.update(data, **kwargs)
def __setitem__(self, key, value):
# Use the lowercased key for lookups, but store the actual
# key alongside the value.
self._store[key.lower()] = (key, value)
def __getitem__(self, key):
return self._store[key.lower()][1]
def __delitem__(self, key):
del self._store[key.lower()]
def __iter__(self):
return (casedkey for casedkey, mappedvalue in self._store.values())
def __len__(self):
return len(self._store)
def lower_items(self):
"""Like iteritems(), but with all lowercase keys."""
return (
(lowerkey, keyval[1])
for (lowerkey, keyval)
in self._store.items()
)
def __eq__(self, other):
if isinstance(other, collections.Mapping):
other = CaseInsensitiveDict(other)
else:
return NotImplementedError
# Compare insensitively
return dict(self.lower_items()) == dict(other.lower_items())
# Copy is required
def copy(self):
return CaseInsensitiveDict(self._store.values())
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, dict(self.items()))
# CaseInsensitiveDict
###############################################################################
###############################################################################
## OrderedCaselessDict
class OrderedCaselessDict(dict):
"""
Inherits from dict. Maintains two sets of keys: the first the keys
belonging to dict, which actually accesses the container
items. This is always cast to lower() whenever it is called, thus
ensuring that keys are always of the same case. The second set of
keys it maintains locally in an list, thus maintaining the order
in which they were added. The second set of keys is not cast to
lower(), which means that client code can always recover the
original 'canonical' casing of the keys.
ONLY TAKES STRING KEYS!
"""
def __init__(self, other=None):
"""
__init__ creates the local set of keys, and then initializes self with
arguments, if any, by using the superclass methods, keeping
the ordered keys in sync.
"""
super(OrderedCaselessDict, self).__init__()
self._ordered_keys = []
if other is not None:
if isinstance(other, dict):
for key, val in other.items():
if key.lower() not in self:
self._ordered_keys.append(str(key))
super(OrderedCaselessDict, \
self).__setitem__(key.lower(), val)
else:
for key, val in other:
if key.lower() not in self:
self._ordered_keys.append(str(key))
super(OrderedCaselessDict, \
self).__setitem__(key.lower(), val)
def __deepcopy__(self, memo):
o = self.__class__()
memo[id(self)] = o
for key, val in self.items():
o[key] = copy.deepcopy(val, memo)
return o
def copy(self):
"Returns a shallow copy of self."
return self.__class__(self)
def iterkeys(self):
"Returns an iterator over self's ordered keys."
return iter(self._ordered_keys)
def itervalues(self):
"Returns an iterator over self's key, value pairs."
for key in self.iterkeys():
yield self[key.lower()]
def iteritems(self):
"Returns an iterator over self's values."
for key in self.iterkeys():
yield (key, self[key.lower()])
def items(self):
"Returns key, value pairs in key-order."
return [(key, self[key]) for key in self.iterkeys()]
def values(self):
"Returns list of key, value pairs."
return [v for v in self.itervalues()]
def __iter__(self):
"Returns an iterator over self's ordered keys."
return self.iterkeys()
def __getitem__(self, key):
"Gets an item using a case-insensitive key."
return super(OrderedCaselessDict, self).__getitem__(key.lower())
def __setitem__(self, key, value):
"Sets an item using a case-insensitive key,"
if key.lower() not in self:
self._ordered_keys.append(str(key))
super(OrderedCaselessDict, self).__setitem__(key.lower(), value)
def __delitem__(self, key):
"Remove item with specified key."
del(self._ordered_keys[self.index(key)])
super(OrderedCaselessDict, \
self).__delitem__(key.lower())
def __contains__(self, key):
"Returns true if has key, regardless of case."
return super(OrderedCaselessDict, self).__contains__(key.lower())
def pop(self, key, alt_val=None):
"a.pop(k[, x]): a[k] if k in a, else x (and remove k)"
if key.lower() in self:
val = self[key]
self.__delitem__(key.lower())
return val
else:
return alt_val
def popitem(self):
"a.popitem() remove and last (key, value) pair"
key = self._ordered_keys[-1]
item = (key, self[key.lower()])
self.__delitem__(key)
return item
def caseless_keys(self):
"Returns a copy of the ordered list of keys."
return [k.lower() for k in self._ordered_keys]
def index(self, key):
"""
Return the index of (caseless) key.
Raise KeyError if not found.
"""
count = 0
for k in self._ordered_keys:
if k.lower() == key.lower():
return count
count = count + 1
raise KeyError(key)
def keys(self):
"Returns a copy of the ordered list of keys."
return list(self._ordered_keys)
def clear(self):
"Deletes all items from the dictionary."
self._ordered_keys = []
super(OrderedCaselessDict, self).clear()
def has_key(self, key):
"Returns true if has key, regardless of case."
return key.lower() in self
def get(self, key, def_val=None):
"Gets an item by its key, returning default if key not present."
return super(OrderedCaselessDict, self).get(key.lower(), def_val)
def setdefault(self, key, def_val=None):
"Sets the default value to return if key not present."
return super(OrderedCaselessDict, self).setdefault(key.lower(), def_val)
def update(self, other):
"""
updates (and overwrites) key/value pairs:
k = { 'a':'A', 'b':'B', 'c':'C'}
q = { 'c':'C', 'd':'D', 'f':'F'}
k.update(q)
{'a': 'A', 'c': 'C', 'b': 'B', 'd': 'D', 'f': 'F'}
"""
for key, val in other.items():
if key.lower() not in self:
self._ordered_keys.append(str(key))
super(OrderedCaselessDict, self).__setitem__(key.lower(), val)
def fromkeys(self, iterable, value=None):
"Creates a new dictionary with keys from seq and values set to value."
ocd = OrderedCaselessDict()
for key in iterable:
if key.lower() not in self:
self[key] = value
return ocd
###############################################################################
## FrozenOrderedDict
class FrozenOrderedDict(collections.OrderedDict):
class ImmutableTypeError(TypeError):
def __init__(self, *args, **kwargs):
super(FrozenOrderedDict.ImmutableTypeError, self).__init__(*args, **kwargs)
def __init__(self, *args, **kwargs):
self._is_frozen = False
super(FrozenOrderedDict, self).__init__(*args, **kwargs)
self._is_frozen = True
def __setitem__(self, key, value):
if self._is_frozen:
raise FrozenOrderedDict.ImmutableTypeError("{} is immutable".format(self.__class__.__name__))
else:
super(FrozenOrderedDict, self).__setitem__(key, value)
def __delitem__(self, key):
if self._is_frozen:
raise FrozenOrderedDict.ImmutableTypeError("{} is immutable".format(self.__class__.__name__))
else:
super(FrozenOrderedDict, self).__delitem__(key)
def pop(self, key, alt_val=None):
if self._is_frozen:
raise FrozenOrderedDict.ImmutableTypeError("{} is immutable".format(self.__class__.__name__))
else:
super(FrozenOrderedDict, self).pop(key, alt_val)
def popitem(self):
if self._is_frozen:
raise FrozenOrderedDict.ImmutableTypeError("{} is immutable".format(self.__class__.__name__))
else:
super(FrozenOrderedDict, self).popitem()
def clear(self):
if self._is_frozen:
raise FrozenOrderedDict.ImmutableTypeError("{} is immutable".format(self.__class__.__name__))
else:
super(FrozenOrderedDict, self).clear()
def update(self, other):
if self._is_frozen:
raise FrozenOrderedDict.ImmutableTypeError("{} is immutable".format(self.__class__.__name__))
else:
super(FrozenOrderedDict, self).update(other)
def fromkeys(self, iterable, value=None):
if self._is_frozen:
raise FrozenOrderedDict.ImmutableTypeError("{} is immutable".format(self.__class__.__name__))
else:
super(FrozenOrderedDict, self).fromkeys(iterable, value)
def __deepcopy__(self, memo):
temp = FrozenOrderedDict()
temp._is_frozen = False
for k in self:
k2 = copy.deepcopy(k, memo)
memo[id(k)] = k2
v = self[k]
v2 = copy.deepcopy(v, memo)
memo[id(v)] = v2
temp[k2] = v2
temp._is_frozen = True
return temp
def __copy__(self):
temp = FrozenOrderedDict()
temp._is_frozen = False
for k in self:
temp[k] = self[k]
temp._is_frozen = True
return temp
##############################################################################
## DataTable
class DataTable(object):
@classmethod
def from_csv(cls,
src,
is_first_row_column_names=True,
is_first_column_row_names=True,
is_trim_space=True,
default_data_type=None,
column_data_types=None,
label_transform_fn=None,
**csv_reader_kwargs
):
"""
Returns table from a full-configured csv.Reader instance.
Parameters
-----------
csv_reader : a csv.Reader instance
The source of the data.
is_first_row_column_names : bool
If True, then the first row is interpreted as column names;
otherwise, treated as data row.
is_first_column_row_names : bool
If True, then the first column is interpreted as row names;
otherwise, treated as data column.
is_trim_space : bool
If True, then will strip space from both sides of all tokens before
processing. Note that only spaces will be trimmed, not tabs.
default_data_type : type or function object
Any callable that, when passed a value, returns the coerced-to-type
equivalent for the data.
column_data_types : dict
A dictionary where the key are the column names and the
corresponding value the type (see ``default_data_type`` for
description).
Returns
-------
t: a DataTable instance
Returns table from a full-configured csv.Reader instance.
"""
if isinstance(src, str):
with open(src, "r") as fsrc:
return cls._from_csv_file(
src=fsrc,
is_first_row_column_names=is_first_row_column_names,
is_first_column_row_names=is_first_column_row_names,
is_trim_space=is_trim_space,
default_data_type=default_data_type,
column_data_types=column_data_types,
label_transform_fn=label_transform_fn,
**csv_reader_kwargs)
else:
return cls._from_csv_file(
src=src,
is_first_row_column_names=is_first_row_column_names,
is_first_column_row_names=is_first_column_row_names,
is_trim_space=is_trim_space,
default_data_type=default_data_type,
column_data_types=column_data_types,
label_transform_fn=label_transform_fn,
**csv_reader_kwargs)
@classmethod
def _from_csv_file(cls,
src,
is_first_row_column_names=True,
is_first_column_row_names=True,
is_trim_space=True,
default_data_type=None,
column_data_types=None,
label_transform_fn=None,
**csv_reader_kwargs
):
ncols = None
data_table = cls()
if is_first_row_column_names:
first_data_row_offset = 1
else:
first_data_row_offset = 0
if is_first_column_row_names:
first_data_column_offset = 1
else:
first_data_column_offset = 0
if column_data_types is None:
column_data_types = {}
if label_transform_fn is None:
label_transform_fn = lambda x: x
csv_reader = csv.reader(src, **csv_reader_kwargs)
for row_idx, row in enumerate(csv_reader):
if ncols is None:
ncols = len(row)
else:
if len(row) == 1 and row[0].strip() == "" and is_trim_space: # blank row
continue
assert ncols == len(row), "{} != {}".format(ncols, len(row))
for cell_idx, cell in enumerate(row):
if is_trim_space:
cell = cell.strip(" ")
if row_idx == 0 and is_first_row_column_names:
if cell_idx == 0 and is_first_column_row_names:
continue
data_table.add_column(
column_name=label_transform_fn(cell),
data_type=column_data_types.get(cell, default_data_type))
elif cell_idx == 0 and is_first_column_row_names:
data_table.add_row(label_transform_fn(cell))
else:
if row_idx == 0 and not is_first_row_column_names:
data_table.add_column(data_type=column_data_types.get(cell, default_data_type))
if cell_idx == 0 and not is_first_column_row_names:
data_table.add_row()
effective_row_idx = row_idx - first_data_row_offset
assert effective_row_idx < len(data_table._row_names), "{}: {}".format(effective_row_idx, data_table._row_names)
effective_column_idx = cell_idx - first_data_column_offset
assert effective_column_idx < len(data_table._column_names), "{}: {}".format(effective_column_idx, data_table._column_names)
data_table[effective_row_idx, effective_column_idx] = cell
return data_table
def __init__(self):
self._row_names = []
self._row_name_set = set()
self._column_names = []
self._column_data_types = {}
self._column_name_set = set()
self._data = {}
def add_column(self, column_name=None, pos=None, data_type=None):
column_name = self._validate_new_column_name(column_name)
assert column_name not in self._column_name_set
if pos is None:
pos = len(self._column_names)
self._column_names.insert(pos, column_name)
self._column_name_set.add(column_name)
self._column_data_types[column_name] = data_type
def add_row(self, row_name=None, pos=None):
row_name = self._validate_new_row_name(row_name)
assert row_name not in self._row_name_set
if pos is None:
pos = len(self._row_names)
self._row_names.insert(pos, row_name)
self._row_name_set.add(row_name)
def __getitem__(self, key):
row_name = self._dereference_key(
key=key[0],
name_list=self._row_names,
name_set=self._row_name_set)
column_name = self._dereference_key(
key=key[1],
name_list=self._column_names,
name_set=self._column_name_set)
if row_name not in self._data:
return None
if column_name not in self._data[row_name]:
return None
return self._data[row_name][column_name]
def __setitem__(self, key, value):
row_name = self._dereference_key(
key=key[0],
name_list=self._row_names,
name_set=self._row_name_set)
column_name = self._dereference_key(
key=key[1],
name_list=self._column_names,
name_set=self._column_name_set)
if row_name not in self._data:
self._data[row_name] = {}
if self._column_data_types[column_name] is not None:
value = self._column_data_types[column_name](value)
self._data[row_name][column_name] = value
def row_name_iter(self):
for row_name in self._row_names:
yield row_name
def column_name_iter(self):
for column_name in self._column_names:
yield column_name
def row_value_iter(self, column_name):
column_name = self._dereference_key(
key=column_name,
name_list=self._column_names,
name_set=self._column_name_set)
for row_name in self._row_names:
yield self[row_name, column_name]
def column_value_iter(self, row_name):
row_name = self._dereference_key(
key=row_name,
name_list=self._row_names,
name_set=self._row_name_set)
for column_name in self._column_names:
yield self[row_name, column_name]
def write_csv(self,
out,
is_first_row_column_names=True,
is_first_column_row_names=True,
missing_data_value="NA",
**csv_writer_kwargs
):
if isinstance(out, str):
dest = open(out, "w")
else:
dest = out
if "delimiter" not in csv_writer_kwargs:
csv_writer_kwargs["delimiter"] = ","
csv_writer = csv.writer(dest, csv_writer_kwargs)
if is_first_row_column_names:
header = []
if is_first_column_row_names:
header.append("")
header.extend(self._column_names)
csv_writer.writerow(header)
for row_name in self._row_names:
row = []
if is_first_column_row_names:
row.append(row_name)
for column_name in self._column_names:
v = self[row_name, column_name]
if v is None:
v = missing_data_value
row.append(v)
csv_writer.writerow(row)
def _validate_new_column_name(self, column_name=None):
if column_name is None:
column_name = "V{}".format(len(self._column_names))
else:
column_name = str(column_name)
return column_name
def _validate_new_row_name(self, row_name=None):
if row_name is None:
row_name = "V{}".format(len(self._row_names))
else:
row_name = str(row_name)
return row_name
def _dereference_key(self, key, name_list, name_set):
if isinstance(key, int):
return name_list[key]
else:
if key not in name_set:
raise KeyError(key)
else:
return key
def num_rows(self):
return len(self._row_names)
def num_columns(self):
return len(self._column_names)
def __len__(self):
return self.num_rows()
###############################################################################
## Generic Container Interace (for reference)
class _ContainerInterface(list):
"""
The following methods can be defined to implement container objects. Containers usually are
sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other
containers as well. The first set of methods is used either to emulate a sequence or to
emulate a mapping; the difference is that for a sequence, the allowable keys should be the
integers k for which 0 <= k < N where N is the length of the sequence, or slice objects,
which define a range of items. (For backwards compatibility, the method __getslice__() (see
below) can also be defined to handle simple, but not extended slices.) It is also recommended
that mappings provide the methods keys(), values(), items(), has_key(), get(), clear(),
setdefault(), iterkeys(), itervalues(), iteritems(), pop(), popitem(), copy(), and update()
behaving similar to those for Python's standard dictionary objects. The UserDict module
provides a DictMixin class to help create those methods from a base set of __getitem__(),
__setitem__(), __delitem__(), and keys(). Mutable sequences should provide methods append(),
count(), index(), extend(), insert(), pop(), remove(), reverse() and sort(), like Python
standard list objects. Finally, sequence types should implement addition (meaning
concatenation) and multiplication (meaning repetition) by defining the methods __add__(),
__radd__(), __iadd__(), __mul__(), __rmul__() and __imul__() described below; they should not
define __coerce__() or other numerical operators. It is recommended that both mappings and
sequences implement the __contains__() method to allow efficient use of the in operator; for
mappings, in should be equivalent of has_key(); for sequences, it should search through the
values. It is further recommended that both mappings and sequences implement the __iter__()
method to allow efficient iteration through the container; for mappings, __iter__() should be
the same as iterkeys(); for sequences, it should iterate through the values.
"""
def __len__(self):
"""
Called to implement the built-in function len(). Should return the length of the
object, an integer >= 0. Also, an object that doesn't define a __nonzero__() method
and whose __len__() method returns zero is considered to be false in a Boolean
context.
"""
pass
def __getitem__(self, key):
"""
Called to implement evaluation of self[key]. For sequence types, the accepted keys
should be integers and slice objects. Note that the special interpretation of
negative indices (if the class wishes to emulate a sequence type) is up to the
__getitem__() method. If key is of an inappropriate type, TypeError may be raised; if
of a value outside the set of indices for the sequence (after any special
interpretation of negative values), IndexError should be raised. For mapping types,
if key is missing (not in the container), KeyError should be raised.
Note
for loops expect that an IndexError will be raised for illegal indices to allow proper
detection of the end of the sequence.
"""
pass
def __setitem__(self, key, value):
"""
Called to implement assignment to self[key]. Same note as for __getitem__(). This should only be implemented for mappings if the objects support changes to the values for keys, or if new keys can be added, or for sequences if elements can be replaced. The same exceptions should be raised for improper key values as for the __getitem__() method.
"""
pass
def __delitem__(self, key):
"""
Called to implement deletion of self[key]. Same note as for __getitem__(). This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper key values as for the __getitem__() method.
"""
pass
def __iter__(self):
"""
This method is called when an iterator is required for a container. This method
should return a new iterator object that can iterate over all the objects in the
container. For mappings, it should iterate over the keys of the container, and should
also be made available as the method iterkeys().
Iterator objects also need to implement this method; they are required to return
themselves. For more information on iterator objects, see Iterator Types.
"""
pass
def __reversed__(self):
"""
Called (if present) by the reversed() builtin to implement reverse iteration. It
should return a new iterator object that iterates over all the objects in the
container in reverse order.
If the __reversed__() method is not provided, the reversed() builtin will fall back
to using the sequence protocol (__len__() and __getitem__()). Objects that support
the sequence protocol should only provide __reversed__() if they can provide an
implementation that is more efficient than the one provided by reversed().
New in version 2.6.
"""
pass
def __contains__(self, item):
"""
The membership test operators (in and not in) are normally implemented as an
iteration through a sequence. However, container objects can supply the following
special method with a more efficient implementation, which also does not require the
object be a sequence.
Called to implement membership test operators. Should return true if item is in self,
false otherwise. For mapping objects, this should consider the keys of the mapping
rather than the values or the key-item pairs.
"""
pass
###############################################################################
## RecastingIterator
class RecastingIterator(object):
"""
Given an iterator I_X that returns objects of type X {x1, x2, x3,
... etc.}, and a function F(X), that takes objects of type X as an
argument and returns objects of type Y, F(X) = Y, this class will
act as an iterator that returns objects of type Y, I_Y, given an
iterator on X. The 'function' given can be a class if the class's
constructor takes a single argument of type X.
"""
def __init__(self, source_iter, casting_fn=None, filter_fn=None):
"""
``source_iter`` is an iterator. ``casting_fn`` is a function
that takes objects returned by ``source_iter`` and returns other
objects. ``filter_fn`` is what will be applied to the SOURCE object
to decide if it will be returned.
"""
self.source_iter = iter(source_iter)
self.casting_fn = casting_fn
self.filter_fn = filter_fn
def __iter__(self):
"Returns self."
return self
def next(self):
"""
Gets next item from the underlying iterator, and if
filter_fn returns True on it, applies casting_fn to it and
returns it.
"""
while True:
source_next = self.source_iter.next()
if self.filter_fn is None or self.filter_fn(source_next):
if self.casting_fn is not None:
return self.casting_fn(source_next)
else:
return source_next
###############################################################################
## ItemAttributeProviderList
class ItemAttributeProxyList(list):
"""
This list will return the attribute of its elements instead of the
elements themselves.
"""
def __init__(self, attr_name, *args):
"""
__init__ calls the list.__init__ with all unnamed args.
``attr_name`` is the name of the attribute or property that should be
returned.
"""
self.bound_attr_name = attr_name
list.__init__(self, *args)
def __getitem__(self, *args):
item = list.__getitem__(self, *args)
# assert hasattr(item, self.bound_attr_name)
return getattr(item, self.bound_attr_name)
def __iter__(self):
"""
Iterates over all elements in self, returning their `<bound_attr_name>`
attribute.
"""
for item in list.__iter__(self):
yield getattr(item, self.bound_attr_name)
def aggregate(self):
"""
Returns a shallow-copy list of the `<bound_attr_name>` attribute of self's
members.
"""
return [getattr(item, self.bound_attr_name) for item in self]
###############################################################################
## ItemSublistProxyList
class ItemSublistProxyList(list):
"""
This list will return a the elements of the bound list attribute of its own
elements instead of the element themselves.
"""
def __init__(self, attr_name, *args):
"""
__init__ calls the list.__init__ with all unnamed args.
``attr_name`` is the name of the attribute or property that should be
returned.
"""
self.bound_attr_name = attr_name
list.__init__(self, *args)
def __str__(self):
return str([str(i) for i in self])
def __getitem__(self, *args):
req_idx = int(args[0])
for idx, i in enumerate(self):
if idx == req_idx:
return i
raise IndexError("list index out of range: %d (max=%d)" % (req_idx, idx))
def __len__(self):
return len([i for i in self])
def __iter__(self):
"""
Iterates over all elements in self, returning their `<bound_attr_name>`
attribute.
"""
for item in list.__iter__(self):
for sublist_item in getattr(item, self.bound_attr_name):
yield sublist_item
def aggregate(self):
"""
Returns a shallow-copy list of the `<bound_attr_name>` attribute of self's
members.
"""
return [i for i in self]
|