Skip to content

analysis_functions

phyllotaxis_analysis.analysis_functions Link

This module contains the functions used to detect permutations in sequences of divergence angles.

EqualTheorAngles Link

Bases: Exception

Raised when different multiples of canonical angles modulo 360 leads to the same value

candidate_angles Link

candidate_angles(angle, bordersDict)

find the theoretical angles that may correspond to a measured angle

:Parameters: - angle - measured angle - bordersDict - dictionary of theoretical angles and corresponding intervals

:Returns: - possible theoretical angles for angle

Source code in src/phyllotaxis_analysis/analysis_functions.py
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
def candidate_angles(angle, bordersDict):
    """ 
    find the theoretical angles that may correspond to a measured angle

    :Parameters:
    -   `angle`    -    measured angle
    -    `bordersDict`    -    dictionary of theoretical angles and corresponding intervals

    :Returns:
    -    possible theoretical angles for `angle`

    """
    candidates = list()
    for tAngle in bordersDict:
        B = bordersDict[tAngle]
        if B[0] < tAngle < B[1] and B[0] < angle < B[1]:
            candidates.append(tAngle)
        if (tAngle < B[0]) and (tAngle < B[1]):
            if (0 <= angle < B[1]) or (B[0] < angle < 360):
                candidates.append(tAngle)
        if (tAngle > B[0]) and (tAngle > B[1]):
            if (B[0] <= angle < 360) or (0 < angle < B[1]):
                candidates.append(tAngle)
    return candidates

candidate_angles_list Link

candidate_angles_list(angles, bordersDict)

calculate list of possible theoretical angles for a list of measured angle

Returns list of possible theoretical angles for a list of measured angle

:Parameters: - angles - measured angle - bordersDict - dictionary of theoretical angles and corresponding intervals

:Returns: - list of possible theoretical angles for itemList

Source code in src/phyllotaxis_analysis/analysis_functions.py
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
def candidate_angles_list(angles, bordersDict):
    """
    calculate list of possible theoretical angles for a list of measured angle

    Returns list of possible theoretical angles for a list of measured angle 

    :Parameters:
    -   `angles`    -    measured angle
    -    `bordersDict`    -    dictionary of theoretical angles and corresponding intervals

    :Returns:
    -    list of possible theoretical angles for `itemList`
    """
    candidatesList = (candidate_angles(angle, bordersDict) for angle in angles)
    return product(*candidatesList)

circular_std_dev Link

circular_std_dev(kappa)

Calculates the circular standard deviation of a von Mises distribution.

The von Mises distribution is a continuous probability distribution on the circle. It is also known as the circular normal distribution or Tikhonov distribution.

This function calculates the circular standard deviation (circularsigma) of a von Mises distribution, given its concentration parameter kappa (> 0).

Refer to scipy.stats.vonmises for more information on the von Mises distribution.

Parameters:

Name Type Description Default
kappa float

The concentration parameter of the von Mises distribution (> 0)

required

Returns:

Type Description
float

The circular standard deviation of the distribution

See Also

scipy.stats.vonmises : Probability density function, cumulative distribution function, etc. of the von Mises distribution.

References

.. [1] Gatto, A., & Jammalamadaka, S. R. (2007). Circular statistics. Handbook of statistics, 26, 1-45.

Source code in src/phyllotaxis_analysis/analysis_functions.py
 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
def circular_std_dev(kappa):
    """Calculates the circular standard deviation of a von Mises distribution.

    The von Mises distribution is a continuous probability distribution on the circle.
    It is also known as the circular normal distribution or Tikhonov distribution.

    This function calculates the circular standard deviation (circularsigma) of a
    von Mises distribution, given its concentration parameter kappa (> 0).

    Refer to `scipy.stats.vonmises` for more information on the von Mises distribution.

    Parameters
    ----------
    kappa : float
        The concentration parameter of the von Mises distribution (> 0)

    Returns
    -------
    float
        The circular standard deviation of the distribution

    See Also
    --------
    scipy.stats.vonmises : Probability density function, cumulative distribution function, etc.
    of the von Mises distribution.

    References
    ----------
    .. [1] Gatto, A., & Jammalamadaka, S. R. (2007). Circular statistics.
       Handbook of statistics, 26, 1-45.
    """
    return 180.0 / np.pi * (np.sqrt(-2 * np.log(i1(kappa) / i0(kappa))))

code_sequence Link

code_sequence(seq, permutationBlockMaxSize, canonicalAngle, bordersDict, kappa, threshold)

make list of n-admissible trees coding sequence of measured angles

:Parameters: - seq - sequence of measured angles - permutationBlockMaxSize - maximum number of organs involved in permutations - canonicalAngle - canonical divergence angle - bordersDict - dictionary of theoretical angles and the corresponding intervals - kappa - concentration parameter - threshold - a statistical threshold to prune the n-admissible trees

:Returns: - treesIndexes - a list of [n-admissible tree, index] such that each tree in the list is an n-admissible tree coding to seq[(index - 1): index]

Source code in src/phyllotaxis_analysis/analysis_functions.py
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
def code_sequence(seq, permutationBlockMaxSize, canonicalAngle, bordersDict, kappa, threshold):
    """
    make list of `n`-admissible trees coding sequence of measured angles

    :Parameters:
    -   `seq`    -    sequence of measured angles
    -   `permutationBlockMaxSize` -     maximum number of organs involved in permutations
    -    `canonicalAngle`    -    canonical divergence angle
    -   `bordersDict`    -    dictionary of theoretical angles and the corresponding intervals
    -   `kappa`    -    concentration parameter
    -    `threshold`    -    a statistical threshold to prune the `n`-admissible trees

    :Returns:
    -    `treesIndexes`    -    a list of `[n-admissible tree, index]` such that each tree in the list is an n-admissible tree coding to `seq[(index - 1): index]`
    """
    D_n, D_n_Coeff = theoretical_divergence_angles(permutationBlockMaxSize, canonicalAngle)
    D_n_coeffDict = dict((D_n[i], D_n_Coeff[i]) for i in range(3 * permutationBlockMaxSize - 2))
    #    weights = weightsFunc(theoreticalAngles)
    sequenceLength = len(seq)
    treesIndexes = list()
    index = 0
    while index <= sequenceLength - 1:
        if index == 0:
            tree = make_n_admissible_tree(seq[index:], permutationBlockMaxSize, canonicalAngle, bordersDict, threshold,
                                          D_n, D_n_Coeff, D_n_coeffDict, kappa, seqBegin=True)
        else:
            tree = make_n_admissible_tree(seq[index:], permutationBlockMaxSize, canonicalAngle, bordersDict, threshold,
                                          D_n, D_n_Coeff, D_n_coeffDict, kappa, D_n_coeffDict)
        index += tree.getMaxLevel() + 1
        treesIndexes.append([tree, index - 1])
    return treesIndexes

counter_clock_wise Link

counter_clock_wise(angles)

Change the orientation of a divergence angles sequence to counterclockwise.

This function takes a list of divergence angles and returns the same list with all the angles rotated by 360 degrees, effectively changing their orientation.

Parameters:

Name Type Description Default
angles list

List of divergence angles in degrees.

required

Returns:

Type Description
list

The input list with all the angles rotated by 360 degrees

Examples:

>>> from phyllotaxis_analysis.analysis_functions import counter_clock_wise
>>> counter_clock_wise([0, 45, 90, 135, 180, 225, 270, 315])
[180, 135, 90, 45, 0, 315, 270, 225]
Source code in src/phyllotaxis_analysis/analysis_functions.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def counter_clock_wise(angles: list) -> list:
    """Change the orientation of a divergence angles sequence to counterclockwise.

    This function takes a list of divergence angles and returns the same list with all the angles
    rotated by 360 degrees, effectively changing their orientation.

    Parameters
    ----------
    angles : list
        List of divergence angles in degrees.

    Returns
    -------
    list
        The input list with all the angles rotated by 360 degrees

    Examples
    --------
    >>> from phyllotaxis_analysis.analysis_functions import counter_clock_wise
    >>> counter_clock_wise([0, 45, 90, 135, 180, 225, 270, 315])
    [180, 135, 90, 45, 0, 315, 270, 225]
    """
    return [360 - i for i in angles]

extract_sequences Link

extract_sequences(treesIndexes, seq, permutationBlockMaxSize, canonicalAngle, probCheck=True)

extract n-admissible sequences from a list of n-admissible trees

:Parameters: - treesIndexes - a list of [n-admissible tree, index] such that each tree in the list is the n-admissible tree coding seq[(index - 1): index] - seq - sequence of measured angles - permutationBlockMaxSize - maximum number of organs involved in permutations - canonicalAngle - canonical divergence angle

:Returns: - prediction - prediction, i.e. list of theoretical angles coding seq - notExplainedList - list of not explained angles - validPermutations - list of valid permutations - invalids - list of invalidated permutations

Source code in src/phyllotaxis_analysis/analysis_functions.py
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
def extract_sequences(treesIndexes, seq, permutationBlockMaxSize, canonicalAngle, probCheck=True):
    """
    extract `n`-admissible sequences from a list of n-admissible trees

    :Parameters:
    -   `treesIndexes`    -    a list of `[n-admissible tree, index]` such that each tree in the list is the n-admissible tree coding `seq[(index - 1): index]`
    -   `seq`    -    sequence of measured angles
    -   `permutationBlockMaxSize` -     maximum number of organs involved in permutations
    -    `canonicalAngle`    -    canonical divergence angle


    :Returns:
    -   `prediction`    -    prediction, i.e. list of theoretical angles coding `seq`
    -   `notExplainedList`    -    list of not explained angles
    -   `validPermutations`    -    list of valid permutations
    -   `invalids`    -    list of invalidated permutations
    """
    theoreticalAngles, theoreticalCoeffAngles = theoretical_divergence_angles(permutationBlockMaxSize, canonicalAngle)
    D_n_coeffDict = dict(
        (theoreticalAngles[i], theoreticalCoeffAngles[i]) for i in range(3 * permutationBlockMaxSize - 2))
    index = 0
    bestSeq = []
    bestProb = 0
    for treeIndex in treesIndexes:
        bestPath = []
        seqList = []
        maxProb = None
        for pathAll in treeIndex[0].allPathsAll():
            result, inversionList = is_n_admissible2(pathAll[0], permutationBlockMaxSize, canonicalAngle, D_n_coeffDict)
            if not result:
                result, inversionList = is_n_admissible2(pathAll[0][:-1], permutationBlockMaxSize, canonicalAngle,
                                                         D_n_coeffDict)
            newInversionList = list()
            for inversion in inversionList:
                newInversion = [j + index for j in inversion]
                newInversionList.append(newInversion)
            seqList.append(pathAll)
            if maxProb == None:
                maxProb = pathAll[3]
            elif maxProb < pathAll[3]:
                maxProb = pathAll[3]
        for pathAll in seqList:
            if pathAll[3] == maxProb:
                bestPath.append(pathAll)
                break  # ???
        bestProb += maxProb
        bestSeq.append(bestPath)
        index = treeIndex[1] + 1
    ind = 0
    notExplainedList = []
    prediction = []
    inversions = []
    allProb = []
    for sList in bestSeq:
        for pathAll in sList:
            prediction.extend(pathAll[0])
            if probCheck:
                allProb.extend(pathAll[1])
            notExplainedList.extend([j[1] + ind for j in pathAll[2]])
            result, inversionList = is_n_admissible2(pathAll[0], permutationBlockMaxSize, canonicalAngle, D_n_coeffDict)
            if not result:
                result, inversionList = is_n_admissible2(pathAll[0][:-1], permutationBlockMaxSize, canonicalAngle,
                                                         D_n_coeffDict)
            newInversionList = list()
            for inversion in inversionList:
                newInversion = [j + ind for j in inversion]
                newInversionList.append(newInversion)
            inversions.extend(newInversionList)
            ind += len(pathAll[0])
    notExplainedAngles = [seq[j] for j in notExplainedList]
    D_n, D_n_com = theoretical_divergence_angles(permutationBlockMaxSize, canonicalAngle)
    code = dict((D_n[j], D_n_com[j]) for j in range(len(D_n)))
    codedSeqNow = [code[j] for j in prediction]

    ssList = []
    ends = []
    for l in treesIndexes:
        ends.append(l[1])
    if prediction[0: ends[0] + 1] != []:
        subSeqs = [prediction[0: ends[0] + 1]]
    else:
        subSeqs = []
    for i in range(len(ends) - 1):
        subSeqs.append(prediction[ends[i] + 1: ends[i + 1] + 1])
    if prediction[ends[-1] + 1: len(prediction)] != []:
        subSeqs.append(prediction[ends[-1] + 1: len(seq)])

    invalideCounter = 0
    ind = 0
    for ss in subSeqs:
        res = is_n_admissible_first_angles(ss, permutationBlockMaxSize, canonicalAngle, D_n_coeffDict)
        if res[0]:
            ssList.append([ss, res[1], []])
        elif len(ss) > 1:
            res = is_n_admissible_first_angles(ss[:-1], permutationBlockMaxSize, canonicalAngle, D_n_coeffDict)
            invalides = sub_invalidate_last(res[1], len(ss) - 1)
            for inv in invalides:
                invalideCounter += len(inv)
            ssList.append([ss, res[1], invalides])
        else:
            ssList.append([ss, res[1], []])

        ind += len(ss)
    invalideCounter += len(notExplainedList)
    invalids = invalidate_seq(codedSeqNow, notExplainedList, permutationBlockMaxSize)
    validPermutations = lists_complement_as_set(newInversionList, invalids)
    return prediction, notExplainedList, validPermutations, invalids

invalidate_seq Link

invalidate_seq(S, notExplained, permutationBlockMaxSize)

invalidate permutations preceding a not explained angles down to a splitting point

:Parameters: - S - sequence of divergence angles - notExplained - list of not explained angles - permutationBlockMaxSize - maximum number of organs involved in permutations

:Returns: invalid - list of invalidated angles

Source code in src/phyllotaxis_analysis/analysis_functions.py
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
def invalidate_seq(S, notExplained, permutationBlockMaxSize):
    """
    invalidate permutations preceding a not explained angles down to a splitting point

    :Parameters:
    -    `S`    - sequence of divergence angles
    -   `notExplained`    -    list of not explained angles
    -    `permutationBlockMaxSize`    -    maximum number of organs involved in permutations

    :Returns:
    `invalid`    -    list of invalidated angles  
    """
    invalide = []
    if len(notExplained) == 0:
        return invalide
    notExplained.sort()
    subSeq = []
    first = -1
    for i in notExplained:
        subSeq.append(S[first + 1: i + 1])
        first = i
    if len(S[notExplained[-1] + 1:]) != 0:
        subSeq.append(S[notExplained[-1] + 1:])
    UsubSeq = []
    for s in subSeq:
        u = [sum(s[:i]) for i in range(len(s))]
        Min = min(u)
        if Min != 0:
            u = [i - Min for i in u]
        UsubSeq.append(u)
    LenUsubSeq = [len(u) for u in UsubSeq]
    LenSsubSeq = [len(s) for s in subSeq]
    counter = 0
    ind = 0
    if notExplained[-1] == len(S) - 1:
        newUSubSeq = list(UsubSeq)
    else:
        newUSubSeq = list(UsubSeq[:-1])
    for U in newUSubSeq:
        i = len(U)
        result, inversions = is_n_admissible_u(U, permutationBlockMaxSize)
        if len(subSeq[counter]) > 1:
            marked = False
            for j in range(i - 1, -1, -1):
                if U[j] == j:
                    isInInverions = False
                    for l1 in inversions:
                        if j in l1:
                            isInInverions = True
                            break
                    if not isInInverions:
                        if j + 1 != i:  # if j + 1 == i this means that there is no inversion before the not explained angle
                            invalide.append([j + ind, i + ind - 2])  # ([j + 1 + ind,i - 1 + ind])
                        marked = True
                        break
            if not marked:
                invalide.append([0 + ind, i + ind - 2])
        ind += len(subSeq[counter])
        counter += 1
    return invalide

is_n_admissible Link

is_n_admissible(seq, canonicalAngle, permutationBlockMaxSize=None)

check whether an entered sequence is an n-admissible sequence

:Parameters: - seq - a sequence of theoretical angles - canonicalAngle - canonical divergence angle - permutationBlockMaxSize - maximum number of organs involved in a permutation

:Returns: - True if the seq is n-admissible, otherwise False - permutations - list of permutations - U - order index series

Source code in src/phyllotaxis_analysis/analysis_functions.py
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
def is_n_admissible(seq, canonicalAngle, permutationBlockMaxSize=None):
    """
    check whether an entered sequence is an `n`-admissible sequence 

    :Parameters:
    -    `seq`    -    a sequence of theoretical angles
    -    `canonicalAngle`    -    canonical divergence angle
    -    `permutationBlockMaxSize`    -    maximum number of organs involved in a permutation 

    :Returns:
    -    True if the `seq` is `n`-admissible, otherwise False
    -    `permutations`    -    list of permutations
    -   `U`    -    order index series
    """
    if permutationBlockMaxSize == None:
        for n in range(1, len(seq) + 1):
            theoreticalAngles, theoreticalCoeffAngles = theoretical_divergence_angles(n, canonicalAngle)
            D_n_coeffDict = dict((theoreticalAngles[i], theoreticalCoeffAngles[i]) for i in range(3 * n - 2))
            try:
                res = is_n_admissible_first_angles(seq, n, canonicalAngle, D_n_coeffDict)
            except KeyError:
                continue
            if res[0]:
                return n, res[1:]
        return 0, [], []
    else:
        theoreticalAngles, theoreticalCoeffAngles = theoretical_divergence_angles(permutationBlockMaxSize,
                                                                                  canonicalAngle)
        D_n_coeffDict = dict(
            (theoreticalAngles[i], theoreticalCoeffAngles[i]) for i in range(3 * permutationBlockMaxSize - 2))
        res = is_n_admissible_first_angles(seq, permutationBlockMaxSize, canonicalAngle, D_n_coeffDict)
        if res[0]:
            return permutationBlockMaxSize, res[1:]
        else:
            return 0, [], []

is_n_admissible2 Link

is_n_admissible2(seq, n, canonicalAngle, D_n_coeffDict)

check whether an entered sequence is an n-admissible sequence.

:Parameters: - seq - a sequence of theoretical angles - n - maximum number of organs involved in permutations - canonicalAngle - canonical divergence angle - D_n_coeffDict - dictionary of theoretical angles and their coefficients

:Returns: - True if the seq is n-admissible, otherwise it returns False. Also list of permutations

Source code in src/phyllotaxis_analysis/analysis_functions.py
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
def is_n_admissible2(seq, n, canonicalAngle, D_n_coeffDict):
    """
    check whether an entered sequence is an `n`-admissible sequence.

    :Parameters:
    -    `seq`    -    a sequence of theoretical angles
    -   `n`    -    maximum number of organs involved in permutations
    -    `canonicalAngle`    -    canonical divergence angle
    -    `D_n_coeffDict`    -    dictionary of theoretical angles and their coefficients

    :Returns:
    -    True if the `seq` is `n`-admissible, otherwise it returns False. Also list of permutations
    """
    sequenceLength = len(seq)
    coeffSeq = [D_n_coeffDict[angle] for angle in seq]
    absoluteAngles = [sum(coeffSeq[:i]) for i in range(sequenceLength + 1)]
    Min = min(absoluteAngles)
    if Min != 0:
        newAbsAngles = [i - Min for i in absoluteAngles]
        absoluteAngles = newAbsAngles
    index = 0
    permutations = list()
    notExplained = list()
    while index <= sequenceLength:
        if absoluteAngles[index] != index:
            k = sequenceLength - index + 1
            maxDepth = n if n < k else k  # Attention: if maxDepth == k it means that we are in the end of string!
            for depth in range(2, maxDepth + 1):
                if is_permutation(absoluteAngles[index: index + depth], index, depth):
                    permutations.append(absoluteAngles[index: index + depth])
                    index += depth
                    break
            else:
                notExplained.append(absoluteAngles[index])
                index += 1
        else:
            index += 1
    return len(notExplained) == 0, permutations

is_n_admissible_first_angles Link

is_n_admissible_first_angles(seq, n, canonicalAngle, D_n_coeffDict)

check whether an entered sequence is an n-admissible sequence by taking into account a possible permutation at the beginning of the sequence.

:Parameters: - seq - a sequence of theoretical angles - n - maximum number of organs involved in permutations - canonicalAngle - canonical divergence angle - D_n_coeffDict - dictionary of theoretical angles and their coefficients

:Returns: - True if the seq is n-admissible, otherwise False
- permutations - list of permutations - U - order index series

Source code in src/phyllotaxis_analysis/analysis_functions.py
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
def is_n_admissible_first_angles(seq, n, canonicalAngle, D_n_coeffDict):
    """
    check whether an entered sequence is an `n`-admissible sequence by taking into account a possible permutation at the beginning of the sequence.

    :Parameters:
    -    `seq`    -    a sequence of theoretical angles
    -   `n`    -    maximum number of organs involved in permutations
    -    `canonicalAngle`    -    canonical divergence angle
    -    `D_n_coeffDict`    -    dictionary of theoretical angles and their coefficients

    :Returns:
    -    True if the `seq` is `n`-admissible, otherwise False  
    -    `permutations`    -    list of permutations
    -   `U`    -    order index series
    """
    sequenceLength = len(seq)
    coeffSeq = [D_n_coeffDict[angle] for angle in seq]
    absoluteAngles = [sum(coeffSeq[:i]) for i in range(sequenceLength + 1)]
    Min = min(absoluteAngles)
    if Min != 0:
        U = [i - Min for i in absoluteAngles]
    else:
        U = absoluteAngles
    pMap = [0 for i in range(sequenceLength + 1)]
    for i in U:
        if i > sequenceLength:
            return False, [], U
        else:
            pMap[i] += 1
    for i in pMap:
        if i != 1:
            return False, [], U
    permutations = []
    i = 0
    while i <= sequenceLength:
        if U[i] != i:
            lower = U[i]
            upper = U[i]
            j = i + 1
            while True:
                if j > sequenceLength:
                    i = sequenceLength + 1
                    break
                if U[j] < lower:
                    lower = U[j]
                if U[j] > upper:
                    upper = U[j]
                if j - i > n - 1:
                    return False, permutations, U
                if lower == i and upper == j:
                    permutations.append(U[i: j + 1])
                    i = j + 1
                    break
                j += 1
        else:
            i += 1
    return True, permutations, U

is_n_admissible_integers Link

is_n_admissible_integers(U, n)

check whether an entered sequence of integers is an order index series of any n-admissible sequence.

:Parameters: - U - a sequence of integers - n - maximum number of organs involved in permutations

:Returns: - True if the seq is n-admissible, otherwise False
- permutations - list of permutations

Source code in src/phyllotaxis_analysis/analysis_functions.py
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
def is_n_admissible_integers(U, n):
    """
    check whether an entered sequence of integers is an order index series of any `n`-admissible sequence.

    :Parameters:
    -    `U`    -    a sequence of integers
    -   `n`    -    maximum number of organs involved in permutations

    :Returns:
    -    True if the `seq` is `n`-admissible, otherwise False  
    -    `permutations`    -    list of permutations
    """
    sequenceLength = len(U)
    pMap = [0 for i in range(sequenceLength)]
    for i in U:
        if i > sequenceLength:
            return False, []
        else:
            pMap[i] += 1
    for i in pMap:
        if i != 1:
            return False, []
    permutations = []
    i = 0
    while i < sequenceLength:
        if U[i] != i:
            lower = U[i]
            upper = U[i]
            j = i + 1
            while True:
                if j > sequenceLength:
                    i = sequenceLength + 1
                    break
                if U[j] < lower:
                    lower = U[j]
                if U[j] > upper:
                    upper = U[j]
                if j - i > n - 1:
                    return False, permutations, U
                if lower == i and upper == j:
                    permutations.append(U[i: j + 1])
                    i = j + 1
                    break
                j += 1
        else:
            i += 1
    return True, permutations

is_n_admissible_not_first_angles Link

is_n_admissible_not_first_angles(seq, n, canonicalAngle, D_n_coeffDict)

check whether an entered sequence is an n-admissible sequence without taking into account a possible permutation at the beginning of the sequence.

:Parameters: - seq - a sequence of theoretical angles - n - maximum number of organs involved in permutations - canonicalAngle - canonical angle - D_n_coeffDict - dictionary of theoretical angles and their coefficients

:Returns: - True if the seq is n-admissible, otherwise False
- permutations - list of permutations - U - order index series

Source code in src/phyllotaxis_analysis/analysis_functions.py
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
def is_n_admissible_not_first_angles(seq, n, canonicalAngle, D_n_coeffDict):
    """
    check whether an entered sequence is an `n`-admissible sequence without taking into account a possible permutation at the beginning of the sequence.

    :Parameters:
    -    `seq`    -    a sequence of theoretical angles
    -   `n`    -    maximum number of organs involved in permutations
    -    `canonicalAngle`    -    canonical angle
    -    `D_n_coeffDict`    -    dictionary of theoretical angles and their coefficients

    :Returns:
    -    True if the `seq` is `n`-admissible, otherwise False  
    -    `permutations`    -    list of permutations
    -   `U`    -    order index series
    """
    sequenceLength = len(seq)
    if seq[0] not in D_n_coeffDict:
        return False, [], []
    coeffSeq = [D_n_coeffDict[angle] for angle in seq]
    U = [sum(coeffSeq[:i]) for i in range(sequenceLength + 1)]
    pMap = [0 for i in range(sequenceLength + 1)]
    for i in U:
        if i > sequenceLength or i < 0:
            return False, [], U
        else:
            pMap[i] += 1
    for i in pMap:
        if i != 1:
            return False, [], U
    permutations = []
    i = 0
    while i <= sequenceLength:
        if U[i] != i:
            lower = U[i]
            upper = U[i]
            j = i + 1
            while True:
                if j > sequenceLength:
                    i = sequenceLength + 1
                    break
                if U[j] < lower:
                    lower = U[j]
                if U[j] > upper:
                    upper = U[j]
                if j - i > n - 1:
                    return False, permutations, U
                if lower == i and upper == j:
                    permutations.append(U[i: j + 1])
                    i = j + 1
                    break
                j += 1
        else:
            i += 1
    return True, permutations, U

is_n_admissible_u Link

is_n_admissible_u(seqU, n)

check whether an entered order index series corresponds to an n-admissible sequence

:Parameters: - seqU - order index series - n - an integer

:Returns: - True if the seq is n-admissible, otherwise False
- inversionList - list of permutations

Source code in src/phyllotaxis_analysis/analysis_functions.py
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
def is_n_admissible_u(seqU, n):
    """
    check whether an entered order index series corresponds to an `n`-admissible sequence

    :Parameters:
    -    `seqU`    -    order index series
    -   `n`    -    an integer

    :Returns:
    -    True if the `seq` is `n`-admissible, otherwise False  
    -    `inversionList`    -    list of permutations
    """
    sequenceLength = len(seqU) - 1
    absoluteAngles = seqU
    index = 0
    inversionList = list()
    notExplained = list()
    while index <= sequenceLength:
        if absoluteAngles[index] != index:
            k = sequenceLength - index + 1
            maxDepth = n if n < k else k  # Attention: if maxDepth == k it means that we are in the end of string!
            for depth in range(2, maxDepth + 1):
                if is_permutation(absoluteAngles[index: index + depth], index, depth):
                    inversionList.append(absoluteAngles[index: index + depth])
                    index += depth
                    break
            else:
                notExplained.append(absoluteAngles[index])
                index += 1
        else:
            index += 1
    return len(notExplained) == 0, inversionList

is_permutation Link

is_permutation(seq, minElement, n)

check whether the entered list is a permutation of successive integers.

:Parameters: - seq - a sequence of integers - n - an integer - minElement - an integer

:Returns: - True if seq is a permutation of [minElement, ...., minElement + n - 1]

Source code in src/phyllotaxis_analysis/analysis_functions.py
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
def is_permutation(seq, minElement, n):
    """
    check whether the entered list is a permutation of  successive integers.

    :Parameters:
    -   `seq` -    a sequence of integers
    -   `n`    -    an integer
    -   `minElement`    -    an integer

    :Returns:
    -    True if `seq` is a permutation of  `[minElement, ...., minElement + n - 1]`
    """
    Psi = [0 for i in range(n)]
    for i in seq:
        if minElement <= i <= minElement + n - 1:
            Psi[i - minElement] += 1
        else:
            return False
    for i in Psi:
        if i != 1:
            return False
    return True

is_sub_sequence Link

is_sub_sequence(seq1, seq2)

check whether one sequence is subsequence of the other.

:Parameters: - seq1 - a sequence of angles - seq2 - a sequence of angles

:Returns: - True if seq1 is a subsequence of seq2, otherwise returns False.

Source code in src/phyllotaxis_analysis/analysis_functions.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
def is_sub_sequence(seq1, seq2):
    """
    check whether one sequence is subsequence of the other.

    :Parameters:
    -    seq1    -    a sequence of angles
    -    seq2    -    a sequence of angles

    :Returns:
    -    True if `seq1` is a subsequence of `seq2`, otherwise returns False. 
    """
    if len(seq1) > len(seq2):
        return False
    for i, j in zip(seq1, seq2):
        if i != j:
            return False
    return True

lists_complement_as_set Link

lists_complement_as_set(LOfL1, removeList)

return relative complement, given two lists of list (that should be considered as a list).

:Parameters: - LOfL1 - a list of lists - removeList - a list of list

:Returns: complements - relative complement of removeList to LOfL1

Source code in src/phyllotaxis_analysis/analysis_functions.py
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
def lists_complement_as_set(LOfL1, removeList):
    """
    return relative complement, given two lists of list (that should be considered as a list).

    :Parameters:
    -    `LOfL1`    - a list of lists
    -   `removeList`    -    a list of list

    :Returns:
    `complements`    -    relative complement of `removeList` to `LOfL1`  

    """
    complements = []
    for lm in LOfL1:
        flag = False
        for lr in removeList:
            if (set(lr) == set(lm)):
                flag = True
        if not flag:
            complements.append(lm)
    return complements

log_of_prob_of_angle Link

log_of_prob_of_angle(x, mu, kappa, tAngles)

calculate logarithmic value of probability of assigning a theoretical angle to a measured angle

:Parameters: - x - measured angle - mu - theoretical angle - kappa - concentration parameter - tAngles - list of theoretical angles

:Returns: - logarithmic value of probability of assigning of mu, to x

Source code in src/phyllotaxis_analysis/analysis_functions.py
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def log_of_prob_of_angle(x, mu, kappa, tAngles):
    """
    calculate logarithmic value of probability of assigning a theoretical angle to a measured angle

    :Parameters:
    -    `x`    -    measured angle
    -   `mu`    -    theoretical angle
    -   `kappa`    -    concentration parameter
    -   `tAngles`    -    list of theoretical angles

    :Returns:
    -    logarithmic value of probability of assigning of `mu`, to `x`
    """
    Sum = sum(myVMpdf(y, mu, kappa) for y in tAngles)
    return np.log(myVMpdf(x, mu, kappa)) - np.log(Sum)

make_n_admissible_tree Link

make_n_admissible_tree(seq, permutationBlockMaxSize, canonicalAngle, bordersDict, threshold, D_n, theoreticalCoeffAngles, D_n_coeffDict, kappa, seqBegin=False)

make n-admissible tree coding sequence of measured angles. It stops when it can not code an angle, i.e. at a not explained angle

:Parameters: - seq - sequence of measured angles - permutationBlockMaxSize - maximum number of organs involved in permutations - canonicalAngle - canonical divergence angle - bordersDict - dictionary of theoretical angles and the corresponding intervals - kappa - concentration parameter - threshold - a statistical threshold to prune the n-admissible trees - D_n - list of theoretical angles - theoreticalCoeffAngles - list of coefficients of theoretical angles - D_n_coeffDict - dictionary of theoretical angles and their coefficients - kappa - concentration parameter - seqBegin - indicates whether permutations at the beginning of the sequence should be taken into account

:Returns: - n-admissible tree

Source code in src/phyllotaxis_analysis/analysis_functions.py
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
def make_n_admissible_tree(seq, permutationBlockMaxSize, canonicalAngle, bordersDict, threshold, D_n,
                           theoreticalCoeffAngles, D_n_coeffDict, kappa, seqBegin=False):
    """
    make `n`-admissible tree coding sequence of measured angles. It stops when it can not code an angle, i.e. at a not explained angle

    :Parameters:
    -   `seq`    -    sequence of measured angles
    -   `permutationBlockMaxSize` -     maximum number of organs involved in permutations
    -    `canonicalAngle`    -    canonical divergence angle
    -   `bordersDict`    -    dictionary of theoretical angles and the corresponding intervals
    -   `kappa`    -    concentration parameter
    -    `threshold`    -    a statistical threshold to prune the `n`-admissible trees
    -    `D_n`    -    list of theoretical angles
    -    `theoreticalCoeffAngles`    -    list of coefficients of theoretical angles 
    -    `D_n_coeffDict`    -    dictionary of theoretical angles and their coefficients
    -    `kappa`    -    concentration parameter
    -    `seqBegin` - indicates whether permutations at the beginning of the sequence should be taken into account 

    :Returns:
    -   `n`-admissible tree
    """
    sequenceLength = len(seq)
    nAdmTree = nAdmissibleTree()
    index = 0  # the index of seq
    # Initializing tree
    possible = list()
    candidates = list()
    horizon = min(sequenceLength - index, permutationBlockMaxSize)
    for i in range(0, horizon):
        possible = candidate_angles_list(seq[index: index + i + 1], bordersDict)
        if seqBegin:
            for pos in possible:
                nadmList = is_n_admissible_first_angles(pos, permutationBlockMaxSize, canonicalAngle, D_n_coeffDict)
                if nadmList[0]:
                    candidates.append([pos, nadmList[2]])
        else:
            for pos in possible:
                nadmList = is_n_admissible_not_first_angles(pos, permutationBlockMaxSize, canonicalAngle, D_n_coeffDict)
                if nadmList[0]:
                    candidates.append([pos, nadmList[2]])

    candLen = len(candidates)
    notSubseq = np.ones(candLen)
    for j in range(candLen):
        for i in range(j + 1, candLen):
            if is_sub_sequence(candidates[j][0], candidates[i][0]):
                notSubseq[i] = 0
    newCandidates = [candidates[i] for i in range(candLen) if notSubseq[i]]
    candidatesProb = list()
    for pos in newCandidates:
        for i in range(len(pos[0])):
            if prob_of_angle(seq[index + i], pos[0][i], kappa, D_n) <= threshold:
                break
        else:
            candidatesProb.append(pos)

    if candidatesProb == []:
        for affectedAngle in max_prob_angle(seq[index], kappa, D_n):
            pbAngle = prob_of_angle(seq[index], affectedAngle, kappa, D_n)
            logPbAngle = log_of_prob_of_angle(seq[index], affectedAngle, kappa, D_n)
            #            tree.addMarkedNodeProb(tree.treeRoot().id ,[ affectedAngle, index, pbAngle, logPbAngle, "null"])
            nAdmTree.addChild(parent=nAdmTree.root, value=affectedAngle, level=index, pbAngle=pbAngle,
                              logPbAngle=logPbAngle, questionMark=True, orderIndex="null")

        return nAdmTree
    else:
        for pos in candidatesProb:
            treePos = list()  # treePos is a list of affected values and their level in the tree.
            currentID = nAdmTree.root
            for i in range(len(pos[0])):
                pbAngle = prob_of_angle(seq[index + i], pos[0][i], kappa, D_n)
                logPbAngle = log_of_prob_of_angle(seq[index + i], pos[0][i], kappa, D_n)
                orderIndex = pos[1][i + 1]
                treePos.append([pos[0][i], index + i, pbAngle, logPbAngle, orderIndex])
            nAdmTree.addListValueLevelProb(nAdmTree.root, treePos)
    # Initializing tree finished
    while True:
        leavesNotLast = nAdmTree.leavesNotLast(
            sequenceLength - 1)  # The leaves whose level are less than sequenceLength - 1
        if len(leavesNotLast) == 0:
            return nAdmTree
        leavesNotLastNotQuestion = [vertex for vertex in leavesNotLast if
                                    (not nAdmTree.get_vertex_property(vertex)["questionMark"])]
        if leavesNotLastNotQuestion == []:  # all leaves whose levels are less than sequenceLength - 1 have question marks
            leaves = nAdmTree.leaves()
            if nAdmTree.leavesLast(sequenceLength - 1) == []:

                maxLevel = nAdmTree.getMaxLevel()
                for feuille in leaves:

                    if nAdmTree.get_vertex_property(feuille)["level"] != maxLevel:
                        ver = feuille
                        while nAdmTree.is_leaf(ver):
                            pid = nAdmTree.parent(ver)
                            nAdmTree.remove_vertex(ver)
                            ver = pid
                return nAdmTree
            else:
                for vertex in leavesNotLast:
                    ver = copy.deepcopy(vertex)
                    while nAdmTree.is_leaf(ver):
                        pid = nAdmTree.parent(ver)
                        nAdmTree.remove_vertex(ver)
                        ver = pid
                return nAdmTree
        for leaf in leavesNotLastNotQuestion:
            index = nAdmTree.get_vertex_property(leaf)["level"] + 1
            pid = leaf
            possible = list()
            candidates = list()
            horizon = min(sequenceLength - index, permutationBlockMaxSize)
            for i in range(0, horizon):
                possible = candidate_angles_list(seq[index: index + i + 1], bordersDict)
                if index <= permutationBlockMaxSize:
                    for pos in possible:
                        pathRoot = nAdmTree.pathToRoot(leaf)
                        newPos = list(pathRoot)
                        newPos.extend(pos)
                        if seqBegin:  # here a voir
                            nadmList = is_n_admissible_first_angles(newPos, permutationBlockMaxSize, canonicalAngle,
                                                                    D_n_coeffDict)
                        else:
                            nadmList = is_n_admissible_not_first_angles(newPos, permutationBlockMaxSize, canonicalAngle,
                                                                        D_n_coeffDict)
                        if nadmList[0]:
                            candidates.append([pos, nadmList[2][len(pathRoot):]])
                else:
                    for pos in possible:
                        newPos = list(pos)
                        newPos[0] = (newPos[0] + (
                                nAdmTree.get_vertex_property(leaf)["orderIndex"] - index) * canonicalAngle) % 360
                        nadmList = is_n_admissible_not_first_angles(newPos, permutationBlockMaxSize, canonicalAngle,
                                                                    D_n_coeffDict)
                        if nadmList[0]:
                            candidates.append([pos, [index + i for i in nadmList[2]]])
            candLen = len(candidates)
            notSubseq = np.ones(candLen)
            for j in range(candLen):
                for i in range(j + 1, candLen):
                    if is_sub_sequence(candidates[j][0], candidates[i][0]):
                        notSubseq[i] = 0
            newCandidates = [candidates[i] for i in range(candLen) if notSubseq[i]]
            candidatesProb = list()
            for pos in newCandidates:
                for i in range(len(pos[0])):
                    if prob_of_angle(seq[index + i], pos[0][i], kappa, D_n) <= threshold:
                        break
                else:
                    candidatesProb.append(pos)
            if candidatesProb == []:
                for affectedAngle in max_prob_angle(seq[index], kappa, D_n):
                    pbAngle = prob_of_angle(seq[index], affectedAngle, kappa, D_n)
                    logPbAngle = log_of_prob_of_angle(seq[index], affectedAngle, kappa, D_n)
                    nAdmTree.addChild(parent=pid, value=affectedAngle, level=index, pbAngle=pbAngle,
                                      logPbAngle=logPbAngle, questionMark=True, orderIndex="null")
            else:
                for pos in candidatesProb:
                    treePos = list()  # treePos is a list of affected values and their level in the tree.
                    for i in range(len(pos[0])):
                        pbAngle = prob_of_angle(seq[index + i], pos[0][i], kappa, D_n)
                        logPbAngle = log_of_prob_of_angle(seq[index + i], pos[0][i], kappa, D_n)
                        orderIndex = pos[1][i + 1]
                        treePos.append([pos[0][i], index + i, pbAngle, logPbAngle, orderIndex])
                    nAdmTree.addListValueLevelProb(pid, treePos)

max_prob_angle Link

max_prob_angle(x, kappa, theoreticalAngles)

calculate the list of most probable theoretical angles

:Parameters: - x - measured angle - mu - theoretical angle - theoreticalAngles - list of theoretical angles

:Returns: - list of most probable theoretical angles that can be assigned to x

Source code in src/phyllotaxis_analysis/analysis_functions.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
def max_prob_angle(x, kappa, theoreticalAngles):
    """
    calculate the list of most probable theoretical angles 

    :Parameters:
    -    `x`    -    measured angle
    -   `mu`    -    theoretical angle
    -   `theoreticalAngles`    -    list of theoretical angles

    :Returns:
    -    list of most probable theoretical angles that can be assigned to `x`   
    """
    probList = [prob_of_angle(x, tAngle, kappa, theoreticalAngles) for tAngle in theoreticalAngles]
    maxProb = max(probList)
    # Use enumerate rather than range
    maxProbAngleList = [theoreticalAngles[i] for i in range(len(probList)) if probList[i] == maxProb]
    return maxProbAngleList

myVMpdf Link

myVMpdf(x, mu, kappa)

Computes the Von Mises probability density function.

The Von Mises distribution is a continuous probability distribution over the circle. It is also known as the circular normal distribution or Tikhonov distribution.

Parameters:

Name Type Description Default
x array_like

Quantiles at which to evaluate the probability density function.

required
mu float

The location parameter of the Von Mises distribution. Represents the mean direction of the distribution.

required
kappa float

The concentration parameter of the Von Mises distribution. Controls how concentrated the distribution is around its mean.

required

Returns:

Type Description
array_like

The value of the probability density function at x. Has the same shape as input x.

References

.. [1] Von Mises, R., "Das analytische Direktionsproblem für Kreiskegel," Zeitschrift für Angewandte Mathematik und Mechanik, vol. 8, no. 5, pp. 321-329, 1928. .. [2] Mardia, K. V., J. T. Kent, and J. M. Bibby, "Multivariate Analysis," Academic Press, London, UK, pp. 45-46, 1979.

Source code in src/phyllotaxis_analysis/analysis_functions.py
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
def myVMpdf(x, mu, kappa):
    """Computes the Von Mises probability density function.

    The Von Mises distribution is a continuous probability distribution over the circle. It
    is also known as the circular normal distribution or Tikhonov distribution.

    Parameters
    ----------
    x : array_like
        Quantiles at which to evaluate the probability density function.
    mu : float
        The location parameter of the Von Mises distribution. Represents the mean
        direction of the distribution.
    kappa : float
        The concentration parameter of the Von Mises distribution. Controls how
        concentrated the distribution is around its mean.

    Returns
    -------
    array_like
        The value of the probability density function at `x`. Has the same shape as
        input `x`.

    References
    ----------
    .. [1] Von Mises, R., "Das analytische Direktionsproblem für Kreiskegel,"
       Zeitschrift für Angewandte Mathematik und Mechanik, vol. 8, no. 5, pp. 321-329,
       1928.
    .. [2] Mardia, K. V., J. T. Kent, and J. M. Bibby, "Multivariate Analysis,"
       Academic Press, London, UK, pp. 45-46, 1979.
    """
    return 1.0 / (360 * i0(kappa)) * np.exp(kappa * np.cos(np.radians(x) - np.radians(mu)) * (np.pi / 180))

prob_of_angle Link

prob_of_angle(x, mu, kappa, tAngles)

calculate probability of assigning a theoretical angle to a measured angle

Parameters:

Name Type Description Default
x array_like

measured angle

required
mu float

theoretical angle

required
kappa float

concentration parameter

required
tAngles list

list of theoretical angles

required
Source code in src/phyllotaxis_analysis/analysis_functions.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def prob_of_angle(x, mu, kappa, tAngles):
    """
    calculate probability of assigning a theoretical angle to a measured angle

    Parameters
    ----------
    x : array_like
        measured angle
    mu : float
        theoretical angle
    kappa : float
        concentration parameter
    tAngles : list
        list of theoretical angles

    :Returns:
    -    probability of assigning of `mu` to `x`
    """
    Sum = sum(myVMpdf(y, mu, kappa) for y in tAngles)
    return myVMpdf(x, mu, kappa) / Sum

sub_invalidate_last Link

sub_invalidate_last(inversions, last)

identify from a list of permutations, those that should be invalidated given an element of an invalid permutation

:Parameters: - inversions - list of permutations - last - an element of an invalid permutation

:Returns: invalidate - list of invalid permutations

Source code in src/phyllotaxis_analysis/analysis_functions.py
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
def sub_invalidate_last(inversions, last):
    """
    identify from a list of permutations, those that should be invalidated given an element of an invalid permutation

    :Parameters:
    -    `inversions`    -    list of permutations
    -    `last`    -    an element of an invalid permutation

    :Returns:
    `invalidate`    -    list of invalid permutations
    """
    if inversions == [] or last not in inversions[-1]:
        return []
    invalidate = [inversions[-1]]
    m = min(inversions[-1])
    for i in range(len(inversions) - 2, -1, -1):
        if m - 1 == max(inversions[i]):
            invalidate.append(inversions[i])
            m = min(inversions[i])
        else:
            break
    return invalidate

theoretical_divergence_angles Link

theoretical_divergence_angles(permutation_block_max_size, alpha)

Calculates theoretical divergence angles for a given permutation block maximum size and alpha value.

This function generates the coefficients for the theoretical divergence angles and then calculates the actual angles based on those coefficients and the provided alpha value. It also checks if there are any equal theoretical divergence angles and raises an error if that is the case.

Parameters:

Name Type Description Default
permutation_block_max_size int

The maximum size of the permutation block. This value should be a positive integer.

required
alpha float

The alpha value used to calculate the theoretical divergence angles. This value should be a positive number.

required

Returns:

Type Description
list of float

A list of the calculated theoretical divergence angles.

list of int

A list of the coefficients used to calculate the theoretical divergence angles.

Raises:

Type Description
EqualTheorAngles

Raised when there are two or more equal theoretical divergence angles.

Source code in src/phyllotaxis_analysis/analysis_functions.py
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
def theoretical_divergence_angles(permutation_block_max_size, alpha):
    """Calculates theoretical divergence angles for a given permutation block maximum size and alpha value.

    This function generates the coefficients for the theoretical divergence angles and then calculates the actual
    angles based on those coefficients and the provided alpha value. It also checks if there are any equal
    theoretical divergence angles and raises an error if that is the case.

    Parameters
    ----------
    permutation_block_max_size : int
        The maximum size of the permutation block. This value should be a positive integer.
    alpha : float
        The alpha value used to calculate the theoretical divergence angles.
        This value should be a positive number.

    Returns
    -------
    list of float
        A list of the calculated theoretical divergence angles.
    list of int
        A list of the coefficients used to calculate the theoretical divergence angles.

    Raises
    ------
    EqualTheorAngles
        Raised when there are two or more equal theoretical divergence angles.
    """
    D_n_coefficients = [i for i in range(1 - permutation_block_max_size, 2 * permutation_block_max_size) if i != 0]
    D_n = [(coef * alpha) % 360 for coef in D_n_coefficients]
    for i in range(3 * permutation_block_max_size - 3):
        for j in range(i + 1, 3 * permutation_block_max_size - 3):
            if D_n[i] == D_n[j]:
                raise EqualTheorAngles(D_n)
    return D_n, D_n_coefficients