SetTools.py - Tools for working on sets

Some of the functions in this module precede the set datatype in python.

Reference

SetTools.combinations(list_of_sets)

create all combinations of a list of sets

>>> combinations([set((1,2)), set((2,3))])
[((0,), set([1, 2]), set([1, 2])), ((1,), set([2, 3]), set([2, 3]))]
>>> combinations([set((1,2)), set((2,3)), set((3,4))])
[((0,), set([1, 2]), set([1, 2])), ((1,), set([2, 3]), set([2, 3])), ((2,), set([3, 4]), set([3, 4])), ((0, 1), set([1, 2, 3]), set([2])), ((0, 2), set([1, 2, 3, 4]), set([])), ((1, 2), set([2, 3, 4]), set([3]))]
Returns

result – The resut is a list of tuples containing (set_composition, union, intersection)

Return type

list

SetTools.writeSets(outfile, list_of_sets, labels=None)

output a list of sets as a tab-separated file.

This method build a list of all items contained across all sets and outputs a matrix of 0’s and 1’s denoting set membership. The items are in the table rows and the sets are in the table columns.

Parameters
  • outfile (File) – File to write to

  • list_of_sets (list) – The list of sets to output

  • labels (list) – List of labels(column names)

SetTools.unionIntersectionMatrix(list_of_sets)

build union and intersection matrix of a list of sets.

>>> unionIntersectionMatrix([set((1,2)), set((2,3))])
array([[0, 1],
       [3, 0]])
>>> unionIntersectionMatrix([set((1,2)), set((2,3)), set((3,4))])
array([[0, 1, 0],
       [3, 0, 1],
       [4, 3, 0]])
Parameters

list_of_sets (list) – The list of sets to work with.

Returns

matrix – The matrix is a list of lists. The upper diagonal of the matrix contains the size of the union of two sets and the lower diagonal the intersection of two sets.

Return type

numpy.matrix

SetTools.getAllCombinations(*sets)

generate all combination of elements from a collection of sets.

This method is derived from a python recipe by Zoran Isailovski: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410685

>>> getAllCombinations(set((1,2)), set((2,3)), set((3,4)))
[(1, 2, 3), (1, 2, 4), (1, 3, 3), (1, 3, 4), (2, 2, 3), (2, 2, 4), (2, 3, 3), (2, 3, 4)]
SetTools.xuniqueCombinations(items, n)

Return a list of unique combinations of items in list.

>>> list(xuniqueCombinations([1, 2, 3], 1))
[[1], [2], [3]]
>>> list(xuniqueCombinations([1, 2, 3], 2))
[[1, 2], [1, 3], [2, 3]]
>>> list(xuniqueCombinations([1, 2, 3], 3))
[[1, 2, 3]]
SetTools.compareLists(list1, list2)

returns the union and the disjoint members of two lists.

Note

Deprecated Use python sets instead.

Returns

  • unique1 (set) – Elements unique in set1

  • unique2 (set) – Elements unique in set2

  • common (set) – Elements in both lists.