mapFolding 0.2.1__py3-none-any.whl → 0.2.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mapFolding/__init__.py CHANGED
@@ -1,6 +1,6 @@
1
1
  from .theSSOT import *
2
- from .beDRY import getTaskDivisions, makeConnectionGraph, outfitFoldings, setCPUlimit
3
- from .beDRY import getLeavesTotal, parseDimensions, validateListDimensions
2
+ from Z0Z_tools import defineConcurrencyLimit, intInnit, oopsieKwargsie
3
+ from .beDRY import getFilenameFoldsTotal, outfitCountFolds
4
4
  from .startHere import countFolds
5
5
  from .oeis import oeisIDfor_n, getOEISids, clearOEIScache
6
6
 
mapFolding/babbage.py CHANGED
@@ -6,7 +6,7 @@ import numba
6
6
  import numpy
7
7
 
8
8
  @numba.jit(cache=True)
9
- def _countFolds(connectionGraph: NDArray[integer[Any]], foldsTotal: NDArray[integer[Any]], mapShape: Tuple[int, ...], my: NDArray[integer[Any]], gapsWhere: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]]) -> int:
9
+ def _countFolds(connectionGraph: NDArray[integer[Any]], foldsSubTotals: NDArray[integer[Any]], gapsWhere: NDArray[integer[Any]], mapShape: Tuple[int, ...], my: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]]):
10
10
  """
11
11
  What in tarnation is this stupid module and function?
12
12
 
@@ -27,4 +27,4 @@ def _countFolds(connectionGraph: NDArray[integer[Any]], foldsTotal: NDArray[inte
27
27
  """
28
28
  # TODO learn if I really must change this jitted function to get the super jit to recompile
29
29
  # print('babbage')
30
- return countFoldsCompiled(connectionGraph, foldsTotal, my, gapsWhere, the, track)
30
+ countFoldsCompiled(connectionGraph=connectionGraph, foldsSubTotals=foldsSubTotals, gapsWhere=gapsWhere, my=my, the=the, track=track)
mapFolding/beDRY.py CHANGED
@@ -1,13 +1,17 @@
1
1
  """A relatively stable API for oft-needed functionality."""
2
- from mapFolding.importPackages import intInnit, defineConcurrencyLimit, oopsieKwargsie
2
+ from mapFolding import dtypeDefault, dtypeLarge
3
3
  from mapFolding import indexMy, indexThe, indexTrack, computationState
4
+ from mapFolding import intInnit, defineConcurrencyLimit, oopsieKwargsie
5
+ from numpy import integer
6
+ from numpy.typing import NDArray
4
7
  from typing import Any, List, Optional, Sequence, Type, Union
5
- import numpy
6
8
  import numba
7
- from numpy.typing import NDArray
8
- from numpy import integer
9
+ import numpy
9
10
  import sys
10
11
 
12
+ def getFilenameFoldsTotal(listDimensions: Sequence[int]) -> str:
13
+ return str(sorted(listDimensions)).replace(' ', '') + '.foldsTotal'
14
+
11
15
  def getLeavesTotal(listDimensions: Sequence[int]) -> int:
12
16
  """
13
17
  How many leaves are in the map.
@@ -32,7 +36,7 @@ def getLeavesTotal(listDimensions: Sequence[int]) -> int:
32
36
 
33
37
  return productDimensions
34
38
 
35
- def getTaskDivisions(computationDivisions: Optional[Union[int, str]], concurrencyLimit: int, the: NDArray[integer[Any]], CPUlimit: Optional[Union[bool, float, int]], listDimensions: Sequence[int]):
39
+ def getTaskDivisions(computationDivisions: Optional[Union[int, str]], concurrencyLimit: int, CPUlimit: Optional[Union[bool, float, int]], listDimensions: Sequence[int]):
36
40
  """
37
41
  Determines whether or how to divide the computation into tasks.
38
42
 
@@ -46,15 +50,12 @@ def getTaskDivisions(computationDivisions: Optional[Union[int, str]], concurrenc
46
50
  - "cpu": limits the divisions to the number of available CPUs, i.e. `concurrencyLimit`
47
51
  concurrencyLimit:
48
52
  Maximum number of concurrent tasks allowed
49
- the:
50
- Array of settings, including `leavesTotal`
51
- CPUlimit: for error reporting
52
53
  listDimensions: for error reporting
54
+ CPUlimit: for error reporting
53
55
 
54
56
  Returns
55
57
  -------
56
- the
57
- Updated settings, including for `taskDivisions`
58
+ taskDivisions:
58
59
 
59
60
  Raises
60
61
  ------
@@ -65,26 +66,27 @@ def getTaskDivisions(computationDivisions: Optional[Union[int, str]], concurrenc
65
66
  -----
66
67
  Task divisions cannot exceed total leaves to prevent duplicate counting of folds.
67
68
  """
68
-
69
69
  if not computationDivisions:
70
- the[indexThe.taskDivisions] = 0
71
- elif isinstance(computationDivisions, int):
72
- the[indexThe.taskDivisions] = computationDivisions
70
+ return 0
71
+ else:
72
+ leavesTotal = getLeavesTotal(listDimensions)
73
+ if isinstance(computationDivisions, int):
74
+ taskDivisions = computationDivisions
73
75
  elif isinstance(computationDivisions, str):
74
76
  computationDivisions = computationDivisions.lower()
75
77
  if computationDivisions == "maximum":
76
- the[indexThe.taskDivisions] = the[indexThe.leavesTotal]
78
+ taskDivisions = leavesTotal
77
79
  elif computationDivisions == "cpu":
78
- the[indexThe.taskDivisions] = min(concurrencyLimit, the[indexThe.leavesTotal])
80
+ taskDivisions = min(concurrencyLimit, leavesTotal)
79
81
  else:
80
82
  raise ValueError(f"I received {computationDivisions} for the parameter, `computationDivisions`, but the so-called programmer didn't implement code for that.")
81
83
 
82
- if the[indexThe.taskDivisions] > the[indexThe.leavesTotal]:
83
- raise ValueError(f"Problem: `taskDivisions`, ({the[indexThe.taskDivisions]}), is greater than `leavesTotal`, ({the[indexThe.leavesTotal]}), which will cause duplicate counting of the folds.\n\nChallenge: you cannot directly set `taskDivisions` or `leavesTotal`. They are derived from parameters that may or may not still be named `computationDivisions`, `CPUlimit` , and `listDimensions` and from dubious-quality Python code.\n\nFor those parameters, I received {computationDivisions=}, {CPUlimit=}, and {listDimensions=}.\n\nPotential solutions: get a different hobby or set `computationDivisions` to a different value.")
84
+ if taskDivisions > leavesTotal:
85
+ raise ValueError(f"Problem: `taskDivisions`, ({taskDivisions}), is greater than `leavesTotal`, ({leavesTotal}), which will cause duplicate counting of the folds.\n\nChallenge: you cannot directly set `taskDivisions` or `leavesTotal`. They are derived from parameters that may or may not still be named `computationDivisions`, `CPUlimit` , and `listDimensions` and from dubious-quality Python code.\n\nFor those parameters, I received {computationDivisions=}, {CPUlimit=}, and {listDimensions=}.\n\nPotential solutions: get a different hobby or set `computationDivisions` to a different value.")
84
86
 
85
- return the
87
+ return taskDivisions
86
88
 
87
- def makeConnectionGraph(listDimensions: Sequence[int], dtype: Optional[Type] = numpy.int64) -> NDArray[integer[Any]]:
89
+ def makeConnectionGraph(listDimensions: Sequence[int], **keywordArguments: Optional[Type]) -> NDArray[integer[Any]]:
88
90
  """
89
91
  Constructs a multi-dimensional connection graph representing the connections between the leaves of a map with the given dimensions.
90
92
  Also called a Cartesian product decomposition or dimensional product mapping.
@@ -94,16 +96,17 @@ def makeConnectionGraph(listDimensions: Sequence[int], dtype: Optional[Type] = n
94
96
  Returns:
95
97
  connectionGraph: A 3D numpy array with shape of (dimensionsTotal + 1, leavesTotal + 1, leavesTotal + 1).
96
98
  """
99
+ datatype = keywordArguments.get('datatype', dtypeDefault)
97
100
  mapShape = validateListDimensions(listDimensions)
98
101
  leavesTotal = getLeavesTotal(mapShape)
99
- arrayDimensions = numpy.array(mapShape, dtype=dtype)
102
+ arrayDimensions = numpy.array(mapShape, dtype=datatype)
100
103
  dimensionsTotal = len(arrayDimensions)
101
104
 
102
105
  # Step 1: find the cumulative product of the map's dimensions
103
- cumulativeProduct = numpy.multiply.accumulate([1] + mapShape, dtype=dtype)
106
+ cumulativeProduct = numpy.multiply.accumulate([1] + mapShape, dtype=datatype)
104
107
 
105
108
  # Step 2: create a coordinate system
106
- coordinateSystem = numpy.zeros((dimensionsTotal + 1, leavesTotal + 1), dtype=dtype)
109
+ coordinateSystem = numpy.zeros((dimensionsTotal + 1, leavesTotal + 1), dtype=datatype)
107
110
 
108
111
  for dimension1ndex in range(1, dimensionsTotal + 1):
109
112
  for leaf1ndex in range(1, leavesTotal + 1):
@@ -113,7 +116,7 @@ def makeConnectionGraph(listDimensions: Sequence[int], dtype: Optional[Type] = n
113
116
  )
114
117
 
115
118
  # Step 3: create and fill the connection graph
116
- connectionGraph = numpy.zeros((dimensionsTotal + 1, leavesTotal + 1, leavesTotal + 1), dtype=dtype)
119
+ connectionGraph = numpy.zeros((dimensionsTotal + 1, leavesTotal + 1, leavesTotal + 1), dtype=datatype)
117
120
 
118
121
  for dimension1ndex in range(1, dimensionsTotal + 1):
119
122
  for activeLeaf1ndex in range(1, leavesTotal + 1):
@@ -139,7 +142,13 @@ def makeConnectionGraph(listDimensions: Sequence[int], dtype: Optional[Type] = n
139
142
 
140
143
  return connectionGraph
141
144
 
142
- def outfitFoldings(listDimensions: Sequence[int], computationDivisions: Optional[Union[int, str]] = None, CPUlimit: Optional[Union[bool, float, int]] = None, dtypeDefault: Optional[Type] = numpy.int64, dtypeLarge: Optional[Type] = numpy.int64, ) -> computationState:
145
+ def makeDataContainer(shape, datatype: Optional[Type] = None):
146
+ """Create a container, probably numpy.ndarray, with the given shape and datatype."""
147
+ if datatype is None:
148
+ datatype = dtypeDefault
149
+ return numpy.zeros(shape, dtype=datatype)
150
+
151
+ def outfitCountFolds(listDimensions: Sequence[int], computationDivisions: Optional[Union[int, str]] = None, CPUlimit: Optional[Union[bool, float, int]] = None, **keywordArguments: Optional[Type]) -> computationState:
143
152
  """
144
153
  Initializes and configures the computation state for map folding computations.
145
154
 
@@ -148,42 +157,53 @@ def outfitFoldings(listDimensions: Sequence[int], computationDivisions: Optional
148
157
  listDimensions:
149
158
  The dimensions of the map to be folded
150
159
  computationDivisions (None):
151
- Specifies how to divide the computation tasks
160
+ Specifies how to divide computations:
161
+ - None: no division of the computation into tasks; sets task divisions to 0
162
+ - int: direct set the number of task divisions; cannot exceed the map's total leaves
163
+ - "maximum": divides into `leavesTotal`-many `taskDivisions`
164
+ - "cpu": limits the divisions to the number of available CPUs, i.e. `concurrencyLimit`
152
165
  CPUlimit (None):
153
- Limits the CPU usage for computations
154
- dtypeDefault (numpy.int64):
155
- The default numpy dtype to use for arrays
156
- dtypeLarge (numpy.int64):
157
- The numpy dtype to use for larger arrays
166
+ Whether and how to limit the CPU usage. See notes for details.
158
167
 
159
168
  Returns
160
169
  -------
161
170
  computationState
162
171
  An initialized computation state containing:
163
172
  - connectionGraph: Graph representing connections in the map
164
- - foldsTotal: Array tracking total folds
173
+ - foldsSubTotals: Array tracking total folds
165
174
  - mapShape: Validated and sorted dimensions of the map
166
175
  - my: Array for internal state tracking
167
176
  - gapsWhere: Array tracking gap positions
168
- - the: Configured task divisions
177
+ - the: Static settings and metadata
169
178
  - track: Array for tracking computation progress
179
+
180
+ Limits on CPU usage `CPUlimit`:
181
+ - `False`, `None`, or `0`: No limits on CPU usage; uses all available CPUs. All other values will potentially limit CPU usage.
182
+ - `True`: Yes, limit the CPU usage; limits to 1 CPU.
183
+ - Integer `>= 1`: Limits usage to the specified number of CPUs.
184
+ - Decimal value (`float`) between 0 and 1: Fraction of total CPUs to use.
185
+ - Decimal value (`float`) between -1 and 0: Fraction of CPUs to *not* use.
186
+ - Integer `<= -1`: Subtract the absolute value from total CPUs.
170
187
  """
188
+ datatypeDefault = keywordArguments.get('datatypeDefault', dtypeDefault)
189
+ datatypeLarge = keywordArguments.get('datatypeLarge', dtypeLarge)
171
190
 
172
- the = numpy.zeros(len(indexThe), dtype=dtypeDefault)
191
+ the = makeDataContainer(len(indexThe), datatypeDefault)
173
192
 
174
193
  mapShape = tuple(sorted(validateListDimensions(listDimensions)))
175
194
  the[indexThe.leavesTotal] = getLeavesTotal(mapShape)
176
195
  the[indexThe.dimensionsTotal] = len(mapShape)
177
196
  concurrencyLimit = setCPUlimit(CPUlimit)
197
+ the[indexThe.taskDivisions] = getTaskDivisions(computationDivisions, concurrencyLimit, CPUlimit, listDimensions)
178
198
 
179
199
  stateInitialized = computationState(
180
- connectionGraph = makeConnectionGraph(mapShape, dtype=dtypeDefault),
181
- foldsTotal = numpy.zeros(the[indexThe.leavesTotal], dtype=numpy.int64),
200
+ connectionGraph = makeConnectionGraph(mapShape, datatype=datatypeDefault),
201
+ foldsSubTotals = makeDataContainer(the[indexThe.leavesTotal], datatypeLarge),
182
202
  mapShape = mapShape,
183
- my = numpy.zeros(len(indexMy), dtype=dtypeLarge),
184
- gapsWhere = numpy.zeros(int(the[indexThe.leavesTotal]) * int(the[indexThe.leavesTotal]) + 1, dtype=dtypeDefault),
185
- the = getTaskDivisions(computationDivisions, concurrencyLimit, the, CPUlimit, listDimensions),
186
- track = numpy.zeros((len(indexTrack), the[indexThe.leavesTotal] + 1), dtype=dtypeLarge)
203
+ my = makeDataContainer(len(indexMy), datatypeLarge),
204
+ gapsWhere = makeDataContainer(int(the[indexThe.leavesTotal]) * int(the[indexThe.leavesTotal]) + 1, datatypeDefault),
205
+ the = the,
206
+ track = makeDataContainer((len(indexTrack), the[indexThe.leavesTotal] + 1), datatypeLarge)
187
207
  )
188
208
 
189
209
  stateInitialized['my'][indexMy.leaf1ndex.value] = 1
mapFolding/lovelace.py CHANGED
@@ -1,145 +1,217 @@
1
- """
2
- The algorithm for counting folds.
1
+ from mapFolding import indexMy, indexThe, indexTrack
2
+ from numpy import integer
3
+ from numpy.typing import NDArray
4
+ from typing import Any
5
+ import numba
6
+ import numpy
3
7
 
4
- Starting from established data structures, the algorithm initializes some baseline values. The initialization uses a loop that is not used after the first fold is counted.
8
+ def activeGapIncrement(my: NDArray[integer[Any]]):
9
+ my[indexMy.gap1ndex.value] += 1
5
10
 
6
- After initialization, the folds are either counted sequentially or counted with inefficiently divided parallel tasks.
11
+ def activeLeafGreaterThan0Condition(my: NDArray[integer[Any]]):
12
+ return my[indexMy.leaf1ndex.value] > 0
7
13
 
8
- All three of these actions--initialization, sequential counting, and parallel counting--use nearly identical logic. Without Numba, all of the logic is in one function with exactly one additional
9
- conditional statement for initialization and exactly one additional conditional statement for parallel counting.
14
+ def activeLeafGreaterThanLeavesTotalCondition(my: NDArray[integer[Any]], the: NDArray[integer[Any]]):
15
+ return my[indexMy.leaf1ndex.value] > the[indexThe.leavesTotal.value]
10
16
 
11
- Numba's just-in-time (jit) compiler, especially super jit, is capable of radically increasing throughput and dramatically reducing the size of the compiled code, especially by ejecting unused code.
17
+ def activeLeafIsTheFirstLeafCondition(my: NDArray[integer[Any]]):
18
+ return my[indexMy.leaf1ndex.value] <= 1
12
19
 
13
- The complexity of this module is due to me allegedly applying Numba's features. Allegedly.
20
+ def activeLeafNotEqualToTaskDivisionsCondition(my: NDArray[integer[Any]], the: NDArray[integer[Any]]):
21
+ return my[indexMy.leaf1ndex.value] != the[indexThe.taskDivisions.value]
14
22
 
15
- (The flow starts with the last function.)
16
- """
17
- from mapFolding import indexMy, indexThe, indexTrack
18
- from numpy import integer
19
- from numpy.typing import NDArray
20
- from typing import Any, Tuple, Optional
21
- import numba
22
- import numpy
23
+ def allDimensionsAreUnconstrained(my: NDArray[integer[Any]], the: NDArray[integer[Any]]):
24
+ return my[indexMy.dimensionsUnconstrained.value] == the[indexThe.dimensionsTotal.value]
23
25
 
24
- @numba.jit(parallel=False, _nrt=True, boundscheck=False, error_model='numpy', fastmath=True, forceinline=True, looplift=False, no_cfunc_wrapper=True, no_cpython_wrapper=True, nogil=True, nopython=True)
25
- def ifComputationDivisions(my: NDArray[integer[Any]], the: NDArray[integer[Any]]) -> bool:
26
- if the[indexThe.taskDivisions.value] == 0:
27
- return True
28
- return my[indexMy.leaf1ndex.value] != the[indexThe.taskDivisions.value] or \
29
- (my[indexMy.leafConnectee.value] % the[indexThe.taskDivisions.value]) == my[indexMy.taskIndex.value]
26
+ def backtrack(my: NDArray[integer[Any]], track: NDArray[integer[Any]]):
27
+ my[indexMy.leaf1ndex.value] -= 1
28
+ track[indexTrack.leafBelow.value, track[indexTrack.leafAbove.value, my[indexMy.leaf1ndex.value]]] = track[indexTrack.leafBelow.value, my[indexMy.leaf1ndex.value]]
29
+ track[indexTrack.leafAbove.value, track[indexTrack.leafBelow.value, my[indexMy.leaf1ndex.value]]] = track[indexTrack.leafAbove.value, my[indexMy.leaf1ndex.value]]
30
30
 
31
- @numba.jit(parallel=False, _nrt=True, boundscheck=False, error_model='numpy', fastmath=True, forceinline=True, looplift=False, no_cfunc_wrapper=True, no_cpython_wrapper=True, nogil=True, nopython=True)
32
- def insertUnconstrainedLeaf(my: NDArray[integer[Any]], the: NDArray[integer[Any]], initializeUnconstrainedLeaf: Optional[bool]) -> bool:
33
- if initializeUnconstrainedLeaf:
34
- return my[indexMy.dimensionsUnconstrained.value] == the[indexThe.dimensionsTotal.value]
35
- else:
36
- return False
31
+ def backtrackCondition(my: NDArray[integer[Any]], track: NDArray[integer[Any]]):
32
+ return my[indexMy.leaf1ndex.value] > 0 and my[indexMy.gap1ndex.value] == track[indexTrack.gapRangeStart.value, my[indexMy.leaf1ndex.value] - 1]
37
33
 
38
- @numba.jit(parallel=False, _nrt=True, boundscheck=False, error_model='numpy', fastmath=True, forceinline=True, looplift=False, no_cfunc_wrapper=True, no_cpython_wrapper=True, nogil=True, nopython=True)
39
- def initializationConditionUnconstrainedLeaf(my: NDArray[integer[Any]], initializeUnconstrainedLeaf: Optional[bool]) -> bool:
40
- if initializeUnconstrainedLeaf is None or initializeUnconstrainedLeaf is False:
41
- return False
42
- else:
34
+ def countGaps(gapsWhere: NDArray[integer[Any]], my: NDArray[integer[Any]], track: NDArray[integer[Any]]):
35
+ gapsWhere[my[indexMy.gap1ndexCeiling.value]] = my[indexMy.leafConnectee.value]
36
+ if track[indexTrack.countDimensionsGapped.value, my[indexMy.leafConnectee.value]] == 0:
37
+ gap1ndexCeilingIncrement(my=my)
38
+ track[indexTrack.countDimensionsGapped.value, my[indexMy.leafConnectee.value]] += 1
39
+
40
+ def dimension1ndexIncrement(my: NDArray[integer[Any]]):
41
+ my[indexMy.dimension1ndex.value] += 1
42
+
43
+ def dimensionsUnconstrainedCondition(connectionGraph: NDArray[integer[Any]], my: NDArray[integer[Any]]):
44
+ return connectionGraph[my[indexMy.dimension1ndex.value], my[indexMy.leaf1ndex.value], my[indexMy.leaf1ndex.value]] == my[indexMy.leaf1ndex.value]
45
+
46
+ def dimensionsUnconstrainedIncrement(my: NDArray[integer[Any]]):
47
+ my[indexMy.dimensionsUnconstrained.value] += 1
48
+
49
+ def filterCommonGaps(gapsWhere: NDArray[integer[Any]], my: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]]):
50
+ gapsWhere[my[indexMy.gap1ndex.value]] = gapsWhere[my[indexMy.indexMiniGap.value]]
51
+ if track[indexTrack.countDimensionsGapped.value, gapsWhere[my[indexMy.indexMiniGap.value]]] == the[indexThe.dimensionsTotal.value] - my[indexMy.dimensionsUnconstrained.value]:
52
+ activeGapIncrement(my=my)
53
+ track[indexTrack.countDimensionsGapped.value, gapsWhere[my[indexMy.indexMiniGap.value]]] = 0
54
+
55
+ def findGapsInitializeVariables(my: NDArray[integer[Any]], track: NDArray[integer[Any]]):
56
+ my[indexMy.dimensionsUnconstrained.value] = 0
57
+ my[indexMy.gap1ndexCeiling.value] = track[indexTrack.gapRangeStart.value, my[indexMy.leaf1ndex.value] - 1]
58
+ my[indexMy.dimension1ndex.value] = 1
59
+
60
+ def foldsSubTotalIncrement(foldsSubTotals: NDArray[integer[Any]], my: NDArray[integer[Any]], the: NDArray[integer[Any]]):
61
+ foldsSubTotals[my[indexMy.taskIndex.value]] += the[indexThe.leavesTotal.value]
62
+
63
+ def gap1ndexCeilingIncrement(my: NDArray[integer[Any]]):
64
+ my[indexMy.gap1ndexCeiling.value] += 1
65
+
66
+ def indexMiniGapIncrement(my: NDArray[integer[Any]]):
67
+ my[indexMy.indexMiniGap.value] += 1
68
+
69
+ def indexMiniGapInitialization(my: NDArray[integer[Any]]):
70
+ my[indexMy.indexMiniGap.value] = my[indexMy.gap1ndex.value]
71
+
72
+ def insertUnconstrainedLeaf(gapsWhere: NDArray[integer[Any]], my: NDArray[integer[Any]]):
73
+ my[indexMy.indexLeaf.value] = 0
74
+ while my[indexMy.indexLeaf.value] < my[indexMy.leaf1ndex.value]:
75
+ gapsWhere[my[indexMy.gap1ndexCeiling.value]] = my[indexMy.indexLeaf.value]
76
+ my[indexMy.gap1ndexCeiling.value] += 1
77
+ my[indexMy.indexLeaf.value] += 1
78
+
79
+ def leafBelowSentinelIs1Condition(track: NDArray[integer[Any]]):
80
+ return track[indexTrack.leafBelow.value, 0] == 1
81
+
82
+ def leafConnecteeInitialization(connectionGraph: NDArray[integer[Any]], my: NDArray[integer[Any]]):
83
+ my[indexMy.leafConnectee.value] = connectionGraph[my[indexMy.dimension1ndex.value], my[indexMy.leaf1ndex.value], my[indexMy.leaf1ndex.value]]
84
+
85
+ def leafConnecteeUpdate(connectionGraph: NDArray[integer[Any]], my: NDArray[integer[Any]], track: NDArray[integer[Any]]):
86
+ my[indexMy.leafConnectee.value] = connectionGraph[my[indexMy.dimension1ndex.value], my[indexMy.leaf1ndex.value], track[indexTrack.leafBelow.value, my[indexMy.leafConnectee.value]]]
87
+
88
+ def loopingLeavesConnectedToActiveLeaf(my: NDArray[integer[Any]]):
89
+ return my[indexMy.leafConnectee.value] != my[indexMy.leaf1ndex.value]
90
+
91
+ def loopingTheDimensions(my: NDArray[integer[Any]], the: NDArray[integer[Any]]):
92
+ return my[indexMy.dimension1ndex.value] <= the[indexThe.dimensionsTotal.value]
93
+
94
+ def loopingToActiveGapCeiling(my: NDArray[integer[Any]]):
95
+ return my[indexMy.indexMiniGap.value] < my[indexMy.gap1ndexCeiling.value]
96
+
97
+ def placeLeaf(gapsWhere: NDArray[integer[Any]], my: NDArray[integer[Any]], track: NDArray[integer[Any]]):
98
+ my[indexMy.gap1ndex.value] -= 1
99
+ track[indexTrack.leafAbove.value, my[indexMy.leaf1ndex.value]] = gapsWhere[my[indexMy.gap1ndex.value]]
100
+ track[indexTrack.leafBelow.value, my[indexMy.leaf1ndex.value]] = track[indexTrack.leafBelow.value, track[indexTrack.leafAbove.value, my[indexMy.leaf1ndex.value]]]
101
+ track[indexTrack.leafBelow.value, track[indexTrack.leafAbove.value, my[indexMy.leaf1ndex.value]]] = my[indexMy.leaf1ndex.value]
102
+ track[indexTrack.leafAbove.value, track[indexTrack.leafBelow.value, my[indexMy.leaf1ndex.value]]] = my[indexMy.leaf1ndex.value]
103
+ track[indexTrack.gapRangeStart.value, my[indexMy.leaf1ndex.value]] = my[indexMy.gap1ndex.value]
104
+ my[indexMy.leaf1ndex.value] += 1
105
+
106
+ def placeLeafCondition(my: NDArray[integer[Any]]):
107
+ return my[indexMy.leaf1ndex.value] > 0
108
+
109
+ def taskIndexCondition(my: NDArray[integer[Any]], the: NDArray[integer[Any]]):
110
+ return my[indexMy.leafConnectee.value] % the[indexThe.taskDivisions.value] == my[indexMy.taskIndex.value]
111
+
112
+ def thereAreComputationDivisionsYouMightSkip(my: NDArray[integer[Any]], the: NDArray[integer[Any]]):
113
+ if activeLeafNotEqualToTaskDivisionsCondition(my=my, the=the):
114
+ return True
115
+ if taskIndexCondition(my=my, the=the):
116
+ return True
117
+ return False
118
+
119
+ def initialize(connectionGraph: NDArray[integer[Any]], gapsWhere: NDArray[integer[Any]], my: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]]):
120
+ while activeLeafGreaterThan0Condition(my=my):
121
+ if activeLeafIsTheFirstLeafCondition(my=my) or leafBelowSentinelIs1Condition(track=track):
122
+ findGapsInitializeVariables(my=my, track=track)
123
+ while loopingTheDimensions(my=my, the=the):
124
+ if dimensionsUnconstrainedCondition(connectionGraph=connectionGraph, my=my):
125
+ dimensionsUnconstrainedIncrement(my=my)
126
+ else:
127
+ leafConnecteeInitialization(connectionGraph=connectionGraph, my=my)
128
+ while loopingLeavesConnectedToActiveLeaf(my=my):
129
+ countGaps(gapsWhere=gapsWhere, my=my, track=track)
130
+ leafConnecteeUpdate(connectionGraph=connectionGraph, my=my, track=track)
131
+ dimension1ndexIncrement(my=my)
132
+ if allDimensionsAreUnconstrained(my=my, the=the):
133
+ insertUnconstrainedLeaf(gapsWhere=gapsWhere, my=my)
134
+ indexMiniGapInitialization(my=my)
135
+ while loopingToActiveGapCeiling(my=my):
136
+ filterCommonGaps(gapsWhere=gapsWhere, my=my, the=the, track=track)
137
+ indexMiniGapIncrement(my=my)
138
+ if placeLeafCondition(my=my):
139
+ placeLeaf(gapsWhere=gapsWhere, my=my, track=track)
43
140
  if my[indexMy.gap1ndex.value] > 0:
44
- return True
45
- else:
46
- return False
47
-
48
- @numba.jit(parallel=False, _nrt=True, boundscheck=False, error_model='numpy', fastmath=True, forceinline=True, looplift=False, no_cfunc_wrapper=True, no_cpython_wrapper=True, nogil=True, nopython=True)
49
- def doWhile(connectionGraph: NDArray[integer[Any]], foldsTotal: NDArray[integer[Any]], my: NDArray[integer[Any]], gapsWhere: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]], initializeUnconstrainedLeaf: Optional[bool]) -> Tuple[NDArray[integer[Any]], NDArray[integer[Any]], NDArray[integer[Any]], NDArray[integer[Any]]]:
50
- while my[indexMy.leaf1ndex.value] > 0:
51
- if my[indexMy.leaf1ndex.value] <= 1 or track[indexTrack.leafBelow.value, 0] == 1:
52
- if my[indexMy.leaf1ndex.value] > the[indexThe.leavesTotal.value]:
53
- foldsTotal[my[indexMy.taskIndex.value]] += the[indexThe.leavesTotal.value]
141
+ break
142
+
143
+ def countParallel(connectionGraph: NDArray[integer[Any]], foldsSubTotals: NDArray[integer[Any]], gapsWhere: NDArray[integer[Any]], my: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]]):
144
+ while activeLeafGreaterThan0Condition(my=my):
145
+ if activeLeafIsTheFirstLeafCondition(my=my) or leafBelowSentinelIs1Condition(track=track):
146
+ if activeLeafGreaterThanLeavesTotalCondition(my=my, the=the):
147
+ foldsSubTotalIncrement(foldsSubTotals=foldsSubTotals, my=my, the=the)
54
148
  else:
55
- my[indexMy.dimensionsUnconstrained.value] = 0
56
- my[indexMy.gap1ndexCeiling.value] = track[indexTrack.gapRangeStart.value, my[indexMy.leaf1ndex.value] - 1]
57
- my[indexMy.dimension1ndex.value] = 1
58
- while my[indexMy.dimension1ndex.value] <= the[indexThe.dimensionsTotal.value]:
59
- if connectionGraph[my[indexMy.dimension1ndex.value], my[indexMy.leaf1ndex.value], my[indexMy.leaf1ndex.value]] == my[indexMy.leaf1ndex.value]:
60
- my[indexMy.dimensionsUnconstrained.value] += 1
149
+ findGapsInitializeVariables(my=my, track=track)
150
+ while loopingTheDimensions(my=my, the=the):
151
+ if dimensionsUnconstrainedCondition(connectionGraph=connectionGraph, my=my):
152
+ dimensionsUnconstrainedIncrement(my=my)
61
153
  else:
62
- my[indexMy.leafConnectee.value] = connectionGraph[my[indexMy.dimension1ndex.value], my[indexMy.leaf1ndex.value], my[indexMy.leaf1ndex.value]]
63
- while my[indexMy.leafConnectee.value] != my[indexMy.leaf1ndex.value]:
64
- # NOTE This conditional check should only be in the parallel counting branch
65
- if ifComputationDivisions(my, the):
66
- gapsWhere[my[indexMy.gap1ndexCeiling.value]] = my[indexMy.leafConnectee.value]
67
- if track[indexTrack.countDimensionsGapped.value, my[indexMy.leafConnectee.value]] == 0:
68
- my[indexMy.gap1ndexCeiling.value] += 1
69
- track[indexTrack.countDimensionsGapped.value, my[indexMy.leafConnectee.value]] += 1
70
- my[indexMy.leafConnectee.value] = connectionGraph[my[indexMy.dimension1ndex.value], my[indexMy.leaf1ndex.value], track[indexTrack.leafBelow.value, my[indexMy.leafConnectee.value]]]
71
- my[indexMy.dimension1ndex.value] += 1
72
- # NOTE This `if` statement and `while` loop should be absent from the code that does the counting
73
- if insertUnconstrainedLeaf(my, the, initializeUnconstrainedLeaf):
74
- my[indexMy.indexLeaf.value] = 0
75
- while my[indexMy.indexLeaf.value] < my[indexMy.leaf1ndex.value]:
76
- gapsWhere[my[indexMy.gap1ndexCeiling.value]] = my[indexMy.indexLeaf.value]
77
- my[indexMy.gap1ndexCeiling.value] += 1
78
- my[indexMy.indexLeaf.value] += 1
79
- my[indexMy.indexMiniGap.value] = my[indexMy.gap1ndex.value]
80
- while my[indexMy.indexMiniGap.value] < my[indexMy.gap1ndexCeiling.value]:
81
- gapsWhere[my[indexMy.gap1ndex.value]] = gapsWhere[my[indexMy.indexMiniGap.value]]
82
- if track[indexTrack.countDimensionsGapped.value, gapsWhere[my[indexMy.indexMiniGap.value]]] == the[indexThe.dimensionsTotal.value] - my[indexMy.dimensionsUnconstrained.value]:
83
- my[indexMy.gap1ndex.value] += 1
84
- track[indexTrack.countDimensionsGapped.value, gapsWhere[my[indexMy.indexMiniGap.value]]] = 0
85
- my[indexMy.indexMiniGap.value] += 1
86
- while my[indexMy.leaf1ndex.value] > 0 and my[indexMy.gap1ndex.value] == track[indexTrack.gapRangeStart.value, my[indexMy.leaf1ndex.value] - 1]:
87
- my[indexMy.leaf1ndex.value] -= 1
88
- track[indexTrack.leafBelow.value, track[indexTrack.leafAbove.value, my[indexMy.leaf1ndex.value]]] = track[indexTrack.leafBelow.value, my[indexMy.leaf1ndex.value]]
89
- track[indexTrack.leafAbove.value, track[indexTrack.leafBelow.value, my[indexMy.leaf1ndex.value]]] = track[indexTrack.leafAbove.value, my[indexMy.leaf1ndex.value]]
90
- if my[indexMy.leaf1ndex.value] > 0:
91
- my[indexMy.gap1ndex.value] -= 1
92
- track[indexTrack.leafAbove.value, my[indexMy.leaf1ndex.value]] = gapsWhere[my[indexMy.gap1ndex.value]]
93
- track[indexTrack.leafBelow.value, my[indexMy.leaf1ndex.value]] = track[indexTrack.leafBelow.value, track[indexTrack.leafAbove.value, my[indexMy.leaf1ndex.value]]]
94
- track[indexTrack.leafBelow.value, track[indexTrack.leafAbove.value, my[indexMy.leaf1ndex.value]]] = my[indexMy.leaf1ndex.value]
95
- track[indexTrack.leafAbove.value, track[indexTrack.leafBelow.value, my[indexMy.leaf1ndex.value]]] = my[indexMy.leaf1ndex.value]
96
- track[indexTrack.gapRangeStart.value, my[indexMy.leaf1ndex.value]] = my[indexMy.gap1ndex.value]
97
- my[indexMy.leaf1ndex.value] += 1
98
- # NOTE This check and break should be absent from the code that does the counting
99
- if initializationConditionUnconstrainedLeaf(my, initializeUnconstrainedLeaf):
100
- break
101
- return foldsTotal, my, gapsWhere, track
154
+ leafConnecteeInitialization(connectionGraph=connectionGraph, my=my)
155
+ while loopingLeavesConnectedToActiveLeaf(my=my):
156
+ if thereAreComputationDivisionsYouMightSkip(my=my, the=the):
157
+ countGaps(gapsWhere=gapsWhere, my=my, track=track)
158
+ leafConnecteeUpdate(connectionGraph=connectionGraph, my=my, track=track)
159
+ dimension1ndexIncrement(my=my)
160
+ indexMiniGapInitialization(my=my)
161
+ while loopingToActiveGapCeiling(my=my):
162
+ filterCommonGaps(gapsWhere=gapsWhere, my=my, the=the, track=track)
163
+ indexMiniGapIncrement(my=my)
164
+ while backtrackCondition(my=my, track=track):
165
+ backtrack(my=my, track=track)
166
+ if placeLeafCondition(my=my):
167
+ placeLeaf(gapsWhere=gapsWhere, my=my, track=track)
168
+
169
+ def countSequential(connectionGraph: NDArray[integer[Any]], foldsSubTotals: NDArray[integer[Any]], gapsWhere: NDArray[integer[Any]], my: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]]):
170
+ while activeLeafGreaterThan0Condition(my=my):
171
+ if activeLeafIsTheFirstLeafCondition(my=my) or leafBelowSentinelIs1Condition(track=track):
172
+ if activeLeafGreaterThanLeavesTotalCondition(my=my, the=the):
173
+ foldsSubTotalIncrement(foldsSubTotals=foldsSubTotals, my=my, the=the)
174
+ else:
175
+ findGapsInitializeVariables(my=my, track=track)
176
+ while loopingTheDimensions(my=my, the=the):
177
+ if dimensionsUnconstrainedCondition(connectionGraph=connectionGraph, my=my):
178
+ dimensionsUnconstrainedIncrement(my=my)
179
+ else:
180
+ leafConnecteeInitialization(connectionGraph=connectionGraph, my=my)
181
+ while loopingLeavesConnectedToActiveLeaf(my=my):
182
+ countGaps(gapsWhere=gapsWhere, my=my, track=track)
183
+ leafConnecteeUpdate(connectionGraph=connectionGraph, my=my, track=track)
184
+ dimension1ndexIncrement(my=my)
185
+ indexMiniGapInitialization(my=my)
186
+ while loopingToActiveGapCeiling(my=my):
187
+ filterCommonGaps(gapsWhere=gapsWhere, my=my, the=the, track=track)
188
+ indexMiniGapIncrement(my=my)
189
+ while backtrackCondition(my=my, track=track):
190
+ backtrack(my=my, track=track)
191
+ if placeLeafCondition(my=my):
192
+ placeLeaf(gapsWhere=gapsWhere, my=my, track=track)
102
193
 
103
194
  @numba.jit(parallel=True, _nrt=True, boundscheck=False, error_model='numpy', fastmath=True, forceinline=True, looplift=False, no_cfunc_wrapper=True, no_cpython_wrapper=True, nogil=True, nopython=True)
104
- def doTaskIndices(connectionGraph: NDArray[integer[Any]], foldsTotal: NDArray[integer[Any]], my: NDArray[integer[Any]], gapsWhere: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]]) -> NDArray[integer[Any]]:
105
- """This is the only function with the `parallel=True` option.
106
- Make a copy of the initialized state because all task divisions can start from this baseline.
107
- Run the counting algorithm but with conditional execution of a few lines of code, so each task has an incomplete count that does not overlap with other tasks."""
108
- stateFoldsSubTotal = foldsTotal.copy()
195
+ def doTaskIndices(connectionGraph: NDArray[integer[Any]], foldsSubTotals: NDArray[integer[Any]], gapsWhere: NDArray[integer[Any]], my: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]]):
196
+
197
+ stateGapsWhere = gapsWhere.copy()
109
198
  stateMy = my.copy()
110
- statePotentialGaps = gapsWhere.copy()
111
199
  stateTrack = track.copy()
112
200
 
113
201
  for indexSherpa in numba.prange(the[indexThe.taskDivisions.value]):
114
- my = stateMy.copy()
115
- my[indexMy.taskIndex.value] = indexSherpa
116
- foldsSubTotal, _1, _2, _3 = doWhile(connectionGraph, stateFoldsSubTotal.copy(), my, statePotentialGaps.copy(), the, stateTrack.copy(), initializeUnconstrainedLeaf=False)
202
+ mySherpa = stateMy.copy()
203
+ mySherpa[indexMy.taskIndex.value] = indexSherpa
204
+ countParallel(connectionGraph=connectionGraph, foldsSubTotals=foldsSubTotals, gapsWhere=stateGapsWhere.copy(), my=mySherpa, the=the, track=stateTrack.copy())
117
205
 
118
- foldsTotal[indexSherpa] = foldsSubTotal[indexSherpa]
206
+ return foldsSubTotals
119
207
 
120
- return foldsTotal
208
+ def countFoldsCompiled(connectionGraph: NDArray[integer[Any]], foldsSubTotals: NDArray[integer[Any]], gapsWhere: NDArray[integer[Any]], my: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]]):
121
209
 
122
- @numba.jit(parallel=False, _nrt=True, boundscheck=False, error_model='numpy', fastmath=True, forceinline=True, looplift=False, no_cfunc_wrapper=True, no_cpython_wrapper=True, nogil=True, nopython=True)
123
- def countFoldsCompileBranch(connectionGraph: NDArray[integer[Any]], foldsTotal: NDArray[integer[Any]], my: NDArray[integer[Any]], gapsWhere: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]], obviousFlagForNumba: bool) -> NDArray[integer[Any]]:
124
- """Allegedly, `obviousFlagForNumba` allows Numba to compile two versions: one for parallel execution and one leaner version for sequential execution."""
125
- if obviousFlagForNumba:
126
- foldsTotal, _1, _2, _3 = doWhile(connectionGraph, foldsTotal, my, gapsWhere, the, track, initializeUnconstrainedLeaf=False)
127
- else:
128
- foldsTotal = doTaskIndices(connectionGraph, foldsTotal, my, gapsWhere, the, track)
210
+ initialize(connectionGraph=connectionGraph, gapsWhere=gapsWhere, my=my, the=the, track=track)
129
211
 
130
- return foldsTotal
131
-
132
- @numba.jit(parallel=False, _nrt=True, boundscheck=False, error_model='numpy', fastmath=True, forceinline=True, looplift=False, no_cfunc_wrapper=True, no_cpython_wrapper=True, nogil=True, nopython=True)
133
- def countFoldsCompiled(connectionGraph: NDArray[integer[Any]], foldsTotal: NDArray[integer[Any]], my: NDArray[integer[Any]], gapsWhere: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]]) -> int:
134
- # ^ Receive the data structures.
135
-
136
- # Initialize baseline values primarily to eliminate the need for the logic of `insertUnconstrainedLeaf`
137
- _0, my, gapsWhere, track = doWhile(connectionGraph, foldsTotal, my, gapsWhere, the, track, initializeUnconstrainedLeaf=True)
138
-
139
- obviousFlagForNumba = the[indexThe.taskDivisions.value] == int(False)
140
-
141
- # Call the function that will branch to sequential or parallel counting
142
- foldsTotal = countFoldsCompileBranch(connectionGraph, foldsTotal, my, gapsWhere, the, track, obviousFlagForNumba)
212
+ if the[indexThe.taskDivisions.value] > 0:
213
+ doTaskIndices(connectionGraph=connectionGraph, foldsSubTotals=foldsSubTotals, gapsWhere=gapsWhere, my=my, the=the, track=track)
214
+ else:
215
+ countSequential(connectionGraph=connectionGraph, foldsSubTotals=foldsSubTotals, gapsWhere=gapsWhere, my=my, the=the, track=track)
143
216
 
144
- # Return an `int` integer
145
- return numpy.sum(foldsTotal).item()
217
+ numba.jit_module(parallel=False, _nrt=True, boundscheck=False, error_model='numpy', fastmath=True, forceinline=True, looplift=False, no_cfunc_wrapper=True, no_cpython_wrapper=True, nogil=True, nopython=True)