digraphsTools (version $Revision: Python 3.10)
index
/home/bisdorff/Documents/GitHub/Digraph3/pyDoc/digraphsTools.py

Python3+ implementation of Digraph3 tools
 
The module provides various generic methods and tools for handling digraphs.
 
Copyright (C) 2016-2023 Raymond Bisdorff
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 
Modules
       
itertools
collections.abc

 
Functions
       
all_partial_perms(px)
Yields all permutations obtained from a list of lists.
 
Usage example:
 
>>> listOfLists = [[1,2],[3],[4,5]]
>>> [perm for perm in all_partial_perms(listOfLists)]
 [[1, 2, 3, 4, 5], [1, 2, 3, 5, 4],
  [2, 1, 3, 4, 5], [2, 1, 3, 5, 4]]
all_perms(str)
# generate all permutations from a string or a list
# From Michael Davies's recipe:
http://snippets.dzone.com/posts/show/753
computeSequenceAlignment(seqA, seqB, match=-1, mispen=1, gappen=1, skwpen=1, Comments=True, Debug=False)
Numerical Recipes 3rd Ed., Press, Teukolsky, Vetterling, Flannery
Cambridge Unievrsity Press
Chap 10.13.2 Example DNA Sequence Alignment
Digraph3 RB June 2023
https://en.wikipedia.org/wiki/Needleman%E2%80%93Wunsch_algorithm
 
*match* : match bonus (negative integer)
 
*mispen*, *gappen*, *skwpen* : mismatch, resp. gap,
resp skew penalty (positive integer)
 
Return {'aout': aout, 'bout':bout, 'summary': summary,
'sumCosts': sumCosts}
 
Example session:
 
>>> from digraphsTools import generateRandomSequence,computeSequenceAlignment
>>> seqA = generateRandomSequence(10,alphabet=['A','C','G','T'])
>>> seqB = generateRandomSequence(10,alphabet=['A','C','G','T'])
>>> alignement = computeSequenceAlignment(seqA,seqB)
 aout:    ['C', 'C', 'A', 'T', 'G', 'A', ' ', 'G', 'C', 'G', 'A']
 bout:    [' ', 'A', 'A', 'T', 'A', 'A', 'T', 'T', 'C', 'C', 'T']
 summary: [' ', '!', '=', '=', '!', '=', ' ', '!', '=', '!', '!']
 statistics: {'match': 4, 'mismatch': 5, 'gapA': 1, 'gapB': 1}
 sum of costs 3
flatten(iterable, ltypes=<class 'collections.abc.Iterable'>)
Flattens a list of lists into a flat list.
 
Main usage:
 
>>> listOfLists = [[1,2],[3],[4]]
>>> [x for x in flatten(listOfLists)]
[1, 2, 3, 4]
generateBipolarGrayCode(n)
Bipolar version of generateGrayCode.
X is a partially determined -1 vector.
generateGrayCode(n)
Knuth ACP (4) 7.2.1.1. p.6
Algorithm G
generateLooplessGrayCode(n)
Knuth ACP (4) 7.2.1.1. p.7
Algorithm L
generateRandomSequence(length=10, alphabet=['A', 'C', 'G', 'T'])
A generator for random samples of sequences
given a certain alphabet (DNA by default).
grayCode(n)
# generate the Gray code of length n by middle reflection
# RB Feb 2017
kmpMatch(haystack, needle, Comments=True, Debug=False)
Knuth Morris Pratt string matching algorithm
https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
 
Returns a list of potential starting indexes of needle ocurrencies in the haystack
 
>>> from digraphsTools import kmpMatch
>>> kmpMatch('0011001011','0101',Comments=True)
 haystack: 00110010110011001011
 needle: 0101
 needle starting index positions in haystack: [5, 15]
omax(Med, L, Debug=False)
Epistemic **disjunction** for bipolar outranking characteristics
computation: Med is the valuation domain median and L is a list of
r-valued statement characteristics.
 
With **positive** arguments, omax operates a **max**,
with **negative** arguments, a **min**.
 
The mixture of **both positive and negative** arguments results in
an **indeterminate** value.
 
Likewise to a mean, the *omax* operator is not associative.
We therefore first assemble all positive and negative terms
and operate omax on the two assembled arguments.
omin(Med, L, Debug=False)
Epistemic **conjunction** of a list L of bipolar outranking characteristics.
Med is the given valuation domain median.
 
With **positive** arguments, omin operates a **min**,
with **negative** arguments, a **max**.
 
The mixture of both **positive and negative** arguments results
in an **indeterminate** value.
 
Likewise to a mean, the *omin* operator is not associative.
We therefore first assemble separately all positive and negative terms
and operate *omin* on the two assembled arguments.
powerset(S)
Power set generator iterator.
 
Parameter S may be any object that is accepted as input by the set class constructor.
qtilingIndexList(indexList, q, Debug=False, Comments=False)
split an index list into q parts of equal length n.
When there is a rest r < q, the r first parts are put to a length of n+1.
 
The method is used for distributing balanced sublists to q multiprocessing threads.
 
Usage example::
 
    >>> from digraphsTools import qtilingIndexList
    >>> indexList = [i for i in range(10)]
    >>> indexlist
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> qtilingIndexList(indexList,4,Comments=True)
     cardinalities: [3, 3, 2, 2]
     [(0, 3), (3, 6), (6, 8), (8, 10)]
quantile(x, p)
R type=7 (default) quantile function.
 
*x* is a vector of statistical observations of length *n*.
 
*p* is an upper-closed cumulative probabilitiy.
 
Renders the quantile *q(p)*,
i.e. the observation such that the probability to be lower or equal is *p*.
ranking2preorder(R)
Transforms a ranking (list from best to worst) into
a preorder (a list of lists from worst to best)
 
Usage:
 
>>> ranking = [1,2,3,4]
>>> ranking2preorder(ranking)
 [[4],[3],[2],[1]]
scoredTuplesSort(tuples, reverse=False, InSite=True)
Sorting a list of scored tuples only on the scores with *key=itemgetter(0)*:
 
>>> L = [(1, 'c'), (2, 'd'), (1, 'a'), (2, 'b'), (3, 'e')]
>>> scoredTuplesSort(L)
>>> L
[(1, 'c'), (1, 'a'), (2, 'd'), (2, 'b'), (3, 'e')]
 
When *InSite==False*, returns the sorted tuples list.
symmetricAverage(Med, L, weights=None, Debug=False)
[Weighted] symmetric average data fusion for bipolar outranking characteristics
computation: Med is the valuation domain median and L is a list of
r-valued statement characteristics.
 
With only **positive** or only **negative** [and median] characteristic values,
the *symmetricAverage* operator  renders the [weighted] average of the characteristics values.
 
The mixture of **both positive and negative** characteristic values results in
an **indeterminate** value.
 
Likewise to a mean, the *symmetric* operator is not associative.
We therefore first assemble separately all positive, negative and null values 
and operate *ofusion* on the three assembled values.
time(...)
time() -> floating point number
 
Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them.
timefn(fn)
A decorator for automate run time measurements
from "High Performance Python" by  M Gorelick & I Ozswald
O'Reilly 2014 p.27
total_size(o, handlers={}, verbose=False)
Returns the approximate memory footprint of an object and all of its contents.
 
Automatically finds the contents of the following containers and
their subclasses:  tuple, list, deque, dict, set, frozenset, Digraph and BigDigraph.
To search other containers, add handlers to iterate over their contents:
 
    handlers = {SomeContainerClass: iter,
                OtherContainerClass: OtherContainerClass.get_elements}
 
See http://code.activestate.com/recipes/577504/

 
Data
        colorPalettes = {1: ['none', '#EA2027', '#006266', '#1B1464', '#5758BB', '#6F1E51', '#EE5A24', '#009432', '#0652DD', '#9980FA', '#833471', '#F79F1F', '#A3CB38', '#1289A7', '#D980FA', '#B53471', '#FFC312', '#C4E538', '#12CBC4', '#FDA7DF', ...], 2: ['black', 'blue', 'coral', 'gold', 'gray', 'black', 'pink', 'green', 'orange', 'skyblue', 'wheat', 'salmon'], 3: ['none', 'black', 'red', 'cyan', 'green', 'brown', 'blue', 'gold', 'orange', 'grey', 'green2']}