mapFolding 0.5.1__py3-none-any.whl → 0.7.0__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.
Files changed (51) hide show
  1. mapFolding/__init__.py +6 -101
  2. mapFolding/basecamp.py +12 -10
  3. mapFolding/beDRY.py +96 -316
  4. mapFolding/filesystem.py +87 -0
  5. mapFolding/noHomeYet.py +20 -0
  6. mapFolding/oeis.py +39 -36
  7. mapFolding/reference/flattened.py +377 -0
  8. mapFolding/reference/hunterNumba.py +132 -0
  9. mapFolding/reference/irvineJavaPort.py +120 -0
  10. mapFolding/reference/jax.py +208 -0
  11. mapFolding/reference/lunnan.py +153 -0
  12. mapFolding/reference/lunnanNumpy.py +123 -0
  13. mapFolding/reference/lunnanWhile.py +121 -0
  14. mapFolding/reference/rotatedEntryPoint.py +240 -0
  15. mapFolding/reference/total_countPlus1vsPlusN.py +211 -0
  16. mapFolding/someAssemblyRequired/Z0Z_workbench.py +34 -0
  17. mapFolding/someAssemblyRequired/__init__.py +16 -0
  18. mapFolding/someAssemblyRequired/getLLVMforNoReason.py +21 -0
  19. mapFolding/someAssemblyRequired/ingredientsNumba.py +100 -0
  20. mapFolding/someAssemblyRequired/synthesizeCountingFunctions.py +7 -0
  21. mapFolding/someAssemblyRequired/synthesizeDataConverters.py +135 -0
  22. mapFolding/someAssemblyRequired/synthesizeNumba.py +91 -0
  23. mapFolding/someAssemblyRequired/synthesizeNumbaJob.py +417 -0
  24. mapFolding/someAssemblyRequired/synthesizeNumbaModules.py +91 -0
  25. mapFolding/someAssemblyRequired/transformationTools.py +425 -0
  26. mapFolding/someAssemblyRequired/whatWillBe.py +311 -0
  27. mapFolding/syntheticModules/__init__.py +0 -0
  28. mapFolding/syntheticModules/dataNamespaceFlattened.py +30 -0
  29. mapFolding/syntheticModules/numbaCount.py +90 -0
  30. mapFolding/syntheticModules/numbaCountExample.py +158 -0
  31. mapFolding/syntheticModules/numbaCountSequential.py +110 -0
  32. mapFolding/syntheticModules/numbaCount_doTheNeedful.py +13 -0
  33. mapFolding/syntheticModules/numba_doTheNeedful.py +12 -0
  34. mapFolding/syntheticModules/numba_doTheNeedfulExample.py +13 -0
  35. mapFolding/theDao.py +203 -227
  36. mapFolding/theSSOT.py +254 -123
  37. {mapFolding-0.5.1.dist-info → mapfolding-0.7.0.dist-info}/METADATA +10 -8
  38. mapfolding-0.7.0.dist-info/RECORD +50 -0
  39. {mapFolding-0.5.1.dist-info → mapfolding-0.7.0.dist-info}/WHEEL +1 -1
  40. {mapFolding-0.5.1.dist-info → mapfolding-0.7.0.dist-info}/top_level.txt +1 -0
  41. tests/__init__.py +0 -0
  42. tests/conftest.py +278 -0
  43. tests/test_computations.py +49 -0
  44. tests/test_filesystem.py +52 -0
  45. tests/test_oeis.py +128 -0
  46. tests/test_other.py +84 -0
  47. tests/test_tasks.py +50 -0
  48. mapFolding/theSSOTdatatypes.py +0 -156
  49. mapFolding-0.5.1.dist-info/RECORD +0 -14
  50. {mapFolding-0.5.1.dist-info → mapfolding-0.7.0.dist-info}/LICENSE +0 -0
  51. {mapFolding-0.5.1.dist-info → mapfolding-0.7.0.dist-info}/entry_points.txt +0 -0
mapFolding/__init__.py CHANGED
@@ -1,104 +1,9 @@
1
- from collections import defaultdict
2
- from types import ModuleType
3
- import importlib
1
+ from mapFolding.basecamp import countFolds
2
+ from mapFolding.oeis import clearOEIScache, getOEISids, OEIS_for_n
4
3
 
5
- _dictionaryListsImportFrom: dict[str, list[str]] = defaultdict(list)
6
-
7
- def __getattr__(name: str):
8
- if name not in _mapSymbolToModule:
9
- raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
10
-
11
- try:
12
- moduleAsStr: str = _mapSymbolToModule[name]
13
- module: ModuleType = importlib.import_module(moduleAsStr)
14
- blankSymbol = getattr(module, name)
15
- except (ImportError, ModuleNotFoundError, AttributeError):
16
- raise
17
-
18
- # The need to inject into globals tells us that the symbol has not actually been imported
19
- globals()[name] = blankSymbol
20
- return blankSymbol
21
-
22
- _dictionaryListsImportFrom['mapFolding.basecamp'].extend([
23
- 'countFolds',
24
- ])
25
-
26
- _dictionaryListsImportFrom['mapFolding.beDRY'].extend([
27
- 'getFilenameFoldsTotal',
28
- 'getPathFilenameFoldsTotal',
29
- 'outfitCountFolds',
30
- 'saveFoldsTotal',
31
- ])
32
-
33
- _dictionaryListsImportFrom['mapFolding.oeis'].extend([
4
+ __all__ = [
34
5
  'clearOEIScache',
6
+ 'countFolds',
35
7
  'getOEISids',
36
- 'oeisIDfor_n',
37
- ])
38
-
39
- # fundamentals
40
- _dictionaryListsImportFrom['mapFolding.theSSOT'].extend([
41
- 'computationState',
42
- 'EnumIndices',
43
- 'getDispatcherCallable',
44
- 'getPathPackage',
45
- 'indexMy',
46
- 'indexTrack',
47
- 'myPackageNameIs',
48
- ])
49
-
50
- # Datatype management
51
- _dictionaryListsImportFrom['mapFolding.theSSOT'].extend([
52
- 'getDatatypeModule',
53
- 'hackSSOTdatatype',
54
- 'hackSSOTdtype',
55
- 'setDatatypeElephino',
56
- 'setDatatypeFoldsTotal',
57
- 'setDatatypeLeavesTotal',
58
- 'setDatatypeModule',
59
- ])
60
-
61
- # Synthesize modules
62
- _dictionaryListsImportFrom['mapFolding.theSSOT'].extend([
63
- 'formatFilenameModuleDEFAULT',
64
- 'getAlgorithmDispatcher',
65
- 'getAlgorithmSource',
66
- 'getPathJobRootDEFAULT',
67
- 'getPathSyntheticModules',
68
- 'moduleOfSyntheticModules',
69
- 'Z0Z_getDatatypeModuleScalar',
70
- 'Z0Z_getDecoratorCallable',
71
- 'Z0Z_setDatatypeModuleScalar',
72
- 'Z0Z_setDecoratorCallable',
73
- 'Z0Z_identifierCountFolds',
74
- ])
75
-
76
- # Parameters for the prima donna
77
- _dictionaryListsImportFrom['mapFolding.theSSOT'].extend([
78
- 'ParametersNumba',
79
- 'parametersNumbaDEFAULT',
80
- 'parametersNumbaFailEarly',
81
- 'parametersNumbaMinimum',
82
- 'parametersNumbaParallelDEFAULT',
83
- 'parametersNumbaSuperJit',
84
- 'parametersNumbaSuperJitParallel',
85
- ])
86
-
87
- # Coping
88
- _dictionaryListsImportFrom['mapFolding.theSSOT'].extend([
89
- 'FREAKOUT',
90
- ])
91
-
92
- _mapSymbolToModule: dict[str, str] = {}
93
- for moduleAsStr, listSymbolsAsStr in _dictionaryListsImportFrom.items():
94
- for symbolAsStr in listSymbolsAsStr:
95
- _mapSymbolToModule[symbolAsStr] = moduleAsStr
96
-
97
- from typing import TYPE_CHECKING
98
- if TYPE_CHECKING:
99
- from basecamp import *
100
- from beDRY import *
101
- from oeis import *
102
- from theDao import *
103
- from theSSOT import *
104
- from theSSOTdatatypes import *
8
+ 'OEIS_for_n',
9
+ ]
mapFolding/basecamp.py CHANGED
@@ -1,5 +1,7 @@
1
1
  from collections.abc import Sequence
2
- from mapFolding import computationState, getDispatcherCallable, getPathFilenameFoldsTotal, outfitCountFolds, saveFoldsTotal
2
+ from mapFolding.beDRY import outfitCountFolds, setCPUlimit, validateListDimensions
3
+ from mapFolding.filesystem import getPathFilenameFoldsTotal, saveFoldsTotal
4
+ from mapFolding.theSSOT import ComputationState, getPackageDispatcher
3
5
  from os import PathLike
4
6
  from pathlib import Path
5
7
 
@@ -7,7 +9,6 @@ def countFolds(listDimensions: Sequence[int]
7
9
  , pathLikeWriteFoldsTotal: str | PathLike[str] | None = None
8
10
  , computationDivisions: int | str | None = None
9
11
  , CPUlimit: int | float | bool | None = None
10
- , **keywordArguments: str | bool
11
12
  ) -> int:
12
13
  """Count the total number of possible foldings for a given map dimensions.
13
14
 
@@ -18,7 +19,6 @@ def countFolds(listDimensions: Sequence[int]
18
19
  computationDivisions (None):
19
20
  Whether and how to divide the computational work. See notes for details.
20
21
  CPUlimit (None): This is only relevant if there are `computationDivisions`: whether and how to limit the CPU usage. See notes for details.
21
- **keywordArguments: Datatype management. See `outfitCountFolds` for details.
22
22
  Returns:
23
23
  foldsTotal: Total number of distinct ways to fold a map of the given dimensions.
24
24
 
@@ -39,15 +39,17 @@ def countFolds(listDimensions: Sequence[int]
39
39
  N.B.: You probably don't want to divide the computation into tasks.
40
40
  If you want to compute a large `foldsTotal`, dividing the computation into tasks is usually a bad idea. Dividing the algorithm into tasks is inherently inefficient: efficient division into tasks means there would be no overlap in the work performed by each task. When dividing this algorithm, the amount of overlap is between 50% and 90% by all tasks: at least 50% of the work done by every task must be done by _all_ tasks. If you improve the computation time, it will only change by -10 to -50% depending on (at the very least) the ratio of the map dimensions and the number of leaves. If an undivided computation would take 10 hours on your computer, for example, the computation will still take at least 5 hours but you might reduce the time to 9 hours. Most of the time, however, you will increase the computation time. If logicalCores >= leavesTotal, it will probably be faster. If logicalCores <= 2 * leavesTotal, it will almost certainly be slower for all map dimensions.
41
41
  """
42
- stateUniversal: computationState = outfitCountFolds(listDimensions, computationDivisions=computationDivisions, CPUlimit=CPUlimit, **keywordArguments)
42
+ mapShape: tuple[int, ...] = validateListDimensions(listDimensions)
43
+ concurrencyLimit: int = setCPUlimit(CPUlimit)
44
+ computationStateInitialized: ComputationState = outfitCountFolds(mapShape, computationDivisions, concurrencyLimit)
43
45
 
44
- dispatcher = getDispatcherCallable()
45
- dispatcher(**stateUniversal)
46
+ dispatcher = getPackageDispatcher()
47
+ computationStateComplete: ComputationState = dispatcher(computationStateInitialized)
46
48
 
47
- foldsTotal = int(stateUniversal['foldGroups'][0:-1].sum() * stateUniversal['foldGroups'][-1])
49
+ computationStateComplete.getFoldsTotal()
48
50
 
49
51
  if pathLikeWriteFoldsTotal is not None:
50
- pathFilenameFoldsTotal: Path = getPathFilenameFoldsTotal(stateUniversal['mapShape'], pathLikeWriteFoldsTotal)
51
- saveFoldsTotal(pathFilenameFoldsTotal, foldsTotal)
52
+ pathFilenameFoldsTotal: Path = getPathFilenameFoldsTotal(computationStateComplete.mapShape, pathLikeWriteFoldsTotal)
53
+ saveFoldsTotal(pathFilenameFoldsTotal, computationStateComplete.foldsTotal)
52
54
 
53
- return foldsTotal
55
+ return computationStateComplete.foldsTotal
mapFolding/beDRY.py CHANGED
@@ -1,106 +1,112 @@
1
1
  """A relatively stable API for oft-needed functionality."""
2
- from mapFolding import (
3
- computationState,
4
- getDatatypeModule,
5
- getPathJobRootDEFAULT,
6
- hackSSOTdatatype,
7
- hackSSOTdtype,
8
- indexMy,
9
- indexTrack,
10
- setDatatypeElephino,
11
- setDatatypeFoldsTotal,
12
- setDatatypeLeavesTotal,
13
- setDatatypeModule,
2
+ from mapFolding.theSSOT import (
3
+ Array3D,
4
+ ComputationState,
5
+ getDatatypePackage,
6
+ getNumpyDtypeDefault,
14
7
  )
15
8
  from collections.abc import Sequence
16
- from numba import get_num_threads, set_num_threads # type: ignore
17
- from numpy import dtype, integer, ndarray
18
- from numpy.typing import DTypeLike, NDArray
19
- from pathlib import Path
20
9
  from sys import maxsize as sysMaxsize
21
10
  from typing import Any
22
11
  from Z0Z_tools import defineConcurrencyLimit, intInnit, oopsieKwargsie
23
12
  import numpy
24
- import os
25
13
 
26
- def getFilenameFoldsTotal(mapShape: Sequence[int] | ndarray[tuple[int], dtype[integer[Any]]]) -> str:
27
- """Imagine your computer has been counting folds for 70 hours, and when it tries to save your newly discovered value,
28
- the filename is invalid. I bet you think this function is more important after that thought experiment.
29
-
30
- Make a standardized filename for the computed value `foldsTotal`.
14
+ def validateListDimensions(listDimensions: Sequence[int]) -> tuple[int, ...]:
15
+ if not listDimensions:
16
+ raise ValueError("listDimensions is a required parameter.")
17
+ listValidated: list[int] = intInnit(listDimensions, 'listDimensions')
18
+ listNonNegative: list[int] = []
19
+ for dimension in listValidated:
20
+ if dimension < 0:
21
+ raise ValueError(f"Dimension {dimension} must be non-negative")
22
+ listNonNegative.append(dimension)
23
+ dimensionsValid = [dimension for dimension in listNonNegative if dimension > 0]
24
+ if len(dimensionsValid) < 2:
25
+ raise NotImplementedError(f"This function requires listDimensions, {listDimensions}, to have at least two dimensions greater than 0. You may want to look at https://oeis.org/.")
26
+ return tuple(sorted(dimensionsValid))
27
+
28
+ def getLeavesTotal(mapShape: tuple[int, ...]) -> int:
29
+ productDimensions = 1
30
+ for dimension in mapShape:
31
+ if dimension > sysMaxsize // productDimensions:
32
+ raise OverflowError(f"I received {dimension=} in {mapShape=}, but the product of the dimensions exceeds the maximum size of an integer on this system.")
33
+ productDimensions *= dimension
34
+ return productDimensions
35
+
36
+ def getNumpyDtype(datatype: type[numpy.signedinteger[Any]] | None = None) -> type[numpy.signedinteger[Any]]:
37
+ """An imperfect way to reduce code duplication."""
38
+ if 'numpy' == getDatatypePackage():
39
+ numpyDtype = datatype or getNumpyDtypeDefault()
40
+ else:
41
+ raise NotImplementedError("Somebody done broke it.")
42
+ return numpyDtype
43
+
44
+ def makeConnectionGraph(mapShape: tuple[int, ...], leavesTotal: int, datatype: type[numpy.signedinteger[Any]] | None = None) -> Array3D:
45
+ numpyDtype = getNumpyDtype(datatype)
46
+ dimensionsTotal = len(mapShape)
47
+ cumulativeProduct = numpy.multiply.accumulate([1] + list(mapShape), dtype=numpyDtype)
48
+ arrayDimensions = numpy.array(mapShape, dtype=numpyDtype)
49
+ coordinateSystem = numpy.zeros((dimensionsTotal, leavesTotal + 1), dtype=numpyDtype)
50
+ for indexDimension in range(dimensionsTotal):
51
+ for leaf1ndex in range(1, leavesTotal + 1):
52
+ coordinateSystem[indexDimension, leaf1ndex] = (((leaf1ndex - 1) // cumulativeProduct[indexDimension]) % arrayDimensions[indexDimension] + 1)
31
53
 
32
- The filename takes into account
33
- - the dimensions of the map, aka `mapShape`, aka `listDimensions`
34
- - no spaces in the filename
35
- - safe filesystem characters
36
- - unique extension
37
- - Python-safe strings:
38
- - no starting with a number
39
- - no reserved words
40
- - no dashes or other special characters
41
- - uh, I can't remember, but I found some other frustrating limitations
42
- - if 'p' is still the first character of the filename, I picked that because it was the original identifier for the map shape in Lunnan's code
54
+ connectionGraph = numpy.zeros((dimensionsTotal, leavesTotal + 1, leavesTotal + 1), dtype=numpyDtype)
55
+ for indexDimension in range(dimensionsTotal):
56
+ for activeLeaf1ndex in range(1, leavesTotal + 1):
57
+ for connectee1ndex in range(1, activeLeaf1ndex + 1):
58
+ isFirstCoord = coordinateSystem[indexDimension, connectee1ndex] == 1
59
+ isLastCoord = coordinateSystem[indexDimension, connectee1ndex] == arrayDimensions[indexDimension]
60
+ exceedsActive = connectee1ndex + cumulativeProduct[indexDimension] > activeLeaf1ndex
61
+ isEvenParity = (coordinateSystem[indexDimension, activeLeaf1ndex] & 1) == (coordinateSystem[indexDimension, connectee1ndex] & 1)
43
62
 
44
- Parameters:
45
- mapShape: A sequence of integers representing the dimensions of the map.
63
+ if (isEvenParity and isFirstCoord) or (not isEvenParity and (isLastCoord or exceedsActive)):
64
+ connectionGraph[indexDimension, activeLeaf1ndex, connectee1ndex] = connectee1ndex
65
+ elif isEvenParity and not isFirstCoord:
66
+ connectionGraph[indexDimension, activeLeaf1ndex, connectee1ndex] = connectee1ndex - cumulativeProduct[indexDimension]
67
+ elif not isEvenParity and not (isLastCoord or exceedsActive):
68
+ connectionGraph[indexDimension, activeLeaf1ndex, connectee1ndex] = connectee1ndex + cumulativeProduct[indexDimension]
69
+ return connectionGraph
46
70
 
47
- Returns:
48
- filenameFoldsTotal: A filename string in format 'pNxM.foldsTotal' where N,M are sorted dimensions
49
- """
50
- return 'p' + 'x'.join(str(dimension) for dimension in sorted(mapShape)) + '.foldsTotal'
71
+ def makeDataContainer(shape: int | tuple[int, ...], datatype: type[numpy.signedinteger[Any]] | None = None) -> numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]]:
72
+ numpyDtype = getNumpyDtype(datatype)
73
+ return numpy.zeros(shape, dtype=numpyDtype)
51
74
 
52
- def getLeavesTotal(listDimensions: Sequence[int]) -> int:
53
- """
54
- How many leaves are in the map.
75
+ def setCPUlimit(CPUlimit: Any | None) -> int:
76
+ """Sets CPU limit for Numba concurrent operations. Note that it can only affect Numba-jitted functions that have not yet been imported.
55
77
 
56
78
  Parameters:
57
- listDimensions: A list of integers representing dimensions.
58
-
79
+ CPUlimit: whether and how to limit the CPU usage. See notes for details.
59
80
  Returns:
60
- productDimensions: The product of all positive integer dimensions.
61
- """
62
- listNonNegative = parseDimensions(listDimensions, 'listDimensions')
63
- listPositive = [dimension for dimension in listNonNegative if dimension > 0]
64
-
65
- if not listPositive:
66
- return 0
67
- else:
68
- productDimensions = 1
69
- for dimension in listPositive:
70
- if dimension > sysMaxsize // productDimensions:
71
- raise OverflowError(f"I received {dimension=} in {listDimensions=}, but the product of the dimensions exceeds the maximum size of an integer on this system.")
72
- productDimensions *= dimension
73
-
74
- return productDimensions
75
-
76
- def getPathFilenameFoldsTotal(mapShape: Sequence[int] | ndarray[tuple[int], dtype[integer[Any]]], pathLikeWriteFoldsTotal: str | os.PathLike[str] | None = None) -> Path:
77
- """Get a standardized path and filename for the computed value `foldsTotal`.
78
-
79
- If you provide a directory, the function will append a standardized filename. If you provide a filename
80
- or a relative path and filename, the function will prepend the default path.
81
-
82
- Parameters:
83
- mapShape: List of dimensions for the map folding problem.
84
- pathLikeWriteFoldsTotal (pathJobRootDEFAULT): Path, filename, or relative path and filename. If None, uses default path.
85
- Defaults to None.
81
+ concurrencyLimit: The actual concurrency limit that was set
82
+ Raises:
83
+ TypeError: If CPUlimit is not of the expected types
86
84
 
87
- Returns:
88
- pathFilenameFoldsTotal: Absolute path and filename.
85
+ Limits on CPU usage `CPUlimit`:
86
+ - `False`, `None`, or `0`: No limits on CPU usage; uses all available CPUs. All other values will potentially limit CPU usage.
87
+ - `True`: Yes, limit the CPU usage; limits to 1 CPU.
88
+ - Integer `>= 1`: Limits usage to the specified number of CPUs.
89
+ - Decimal value (`float`) between 0 and 1: Fraction of total CPUs to use.
90
+ - Decimal value (`float`) between -1 and 0: Fraction of CPUs to *not* use.
91
+ - Integer `<= -1`: Subtract the absolute value from total CPUs.
89
92
  """
90
- pathLikeSherpa = Path(pathLikeWriteFoldsTotal) if pathLikeWriteFoldsTotal is not None else None
91
- if not pathLikeSherpa:
92
- pathLikeSherpa = getPathJobRootDEFAULT()
93
- if pathLikeSherpa.is_dir():
94
- pathFilenameFoldsTotal = pathLikeSherpa / getFilenameFoldsTotal(mapShape)
95
- elif pathLikeSherpa.is_absolute():
96
- pathFilenameFoldsTotal = pathLikeSherpa
93
+ if not (CPUlimit is None or isinstance(CPUlimit, (bool, int, float))):
94
+ CPUlimit = oopsieKwargsie(CPUlimit)
95
+
96
+ concurrencyLimit: int = int(defineConcurrencyLimit(CPUlimit))
97
+ from mapFolding.theSSOT import concurrencyPackage
98
+ if concurrencyPackage == 'numba':
99
+ from numba import get_num_threads, set_num_threads
100
+ set_num_threads(concurrencyLimit)
101
+ concurrencyLimit = get_num_threads()
102
+ elif concurrencyPackage == 'algorithm':
103
+ concurrencyLimit = 1
97
104
  else:
98
- pathFilenameFoldsTotal = getPathJobRootDEFAULT() / pathLikeSherpa
105
+ raise NotImplementedError("This function only supports the 'numba' concurrency package.")
99
106
 
100
- pathFilenameFoldsTotal.parent.mkdir(parents=True, exist_ok=True)
101
- return pathFilenameFoldsTotal
107
+ return concurrencyLimit
102
108
 
103
- def getTaskDivisions(computationDivisions: int | str | None, concurrencyLimit: int, CPUlimit: bool | float | int | None, listDimensions: Sequence[int]) -> int:
109
+ def getTaskDivisions(computationDivisions: int | str | None, concurrencyLimit: int, leavesTotal: int) -> int:
104
110
  """
105
111
  Determines whether to divide the computation into tasks and how many divisions.
106
112
 
@@ -134,12 +140,11 @@ def getTaskDivisions(computationDivisions: int | str | None, concurrencyLimit: i
134
140
  Task divisions should not exceed total leaves or the folds will be over-counted.
135
141
  """
136
142
  taskDivisions = 0
137
- leavesTotal = getLeavesTotal(listDimensions)
138
143
  if not computationDivisions:
139
144
  pass
140
145
  elif isinstance(computationDivisions, int):
141
146
  taskDivisions = computationDivisions
142
- elif isinstance(computationDivisions, str): # type: ignore 'Unnecessary isinstance call; "str" is always an instance of "str", so sayeth Pylance'. Yeah, well "User is not always an instance of "correct input" so sayeth the programmer.
147
+ elif isinstance(computationDivisions, str): # type: ignore 'Unnecessary isinstance call; "str" is always an instance of "str", so sayeth Pylance'. Yeah, well "User is not always an instance of "correct input" so sayeth the programmer.
143
148
  computationDivisions = computationDivisions.lower()
144
149
  if computationDivisions == 'maximum':
145
150
  taskDivisions = leavesTotal
@@ -149,236 +154,11 @@ def getTaskDivisions(computationDivisions: int | str | None, concurrencyLimit: i
149
154
  raise ValueError(f"I received {computationDivisions} for the parameter, `computationDivisions`, but the so-called programmer didn't implement code for that.")
150
155
 
151
156
  if taskDivisions > leavesTotal:
152
- 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.")
153
-
154
- return taskDivisions
155
-
156
- def makeConnectionGraph(listDimensions: Sequence[int], **keywordArguments: str | None) -> ndarray[tuple[int, int, int], dtype[integer[Any]]]:
157
- """
158
- Constructs a multi-dimensional connection graph representing the connections between the leaves of a map with the given dimensions.
159
- Also called a Cartesian product decomposition or dimensional product mapping.
160
-
161
- Parameters
162
- listDimensions: A sequence of integers representing the dimensions of the map.
163
- **keywordArguments: Datatype management.
164
-
165
- Returns
166
- connectionGraph: A 3D numpy array with shape of (dimensionsTotal, leavesTotal + 1, leavesTotal + 1).
167
- """
168
- ImaSetTheDatatype = keywordArguments.get('datatype', None)
169
- if ImaSetTheDatatype:
170
- setDatatypeLeavesTotal(ImaSetTheDatatype)
171
- dtype = hackSSOTdtype('connectionGraph')
172
- mapShape = validateListDimensions(listDimensions)
173
- leavesTotal = getLeavesTotal(mapShape)
174
- arrayDimensions = numpy.array(mapShape, dtype=dtype)
175
- dimensionsTotal = len(arrayDimensions)
176
-
177
- cumulativeProduct = numpy.multiply.accumulate([1] + mapShape, dtype=dtype)
178
- coordinateSystem = numpy.zeros((dimensionsTotal, leavesTotal + 1), dtype=dtype)
179
- for indexDimension in range(dimensionsTotal):
180
- for leaf1ndex in range(1, leavesTotal + 1):
181
- coordinateSystem[indexDimension, leaf1ndex] = ( ((leaf1ndex - 1) // cumulativeProduct[indexDimension]) % arrayDimensions[indexDimension] + 1 )
182
-
183
- connectionGraph: ndarray[tuple[int, int, int], numpy.dtype[integer[Any]]] = numpy.zeros((dimensionsTotal, leavesTotal + 1, leavesTotal + 1), dtype=dtype)
184
- for indexDimension in range(dimensionsTotal):
185
- for activeLeaf1ndex in range(1, leavesTotal + 1):
186
- for connectee1ndex in range(1, activeLeaf1ndex + 1):
187
- isFirstCoord = coordinateSystem[indexDimension, connectee1ndex] == 1
188
- isLastCoord = coordinateSystem[indexDimension, connectee1ndex] == arrayDimensions[indexDimension]
189
- exceedsActive = connectee1ndex + cumulativeProduct[indexDimension] > activeLeaf1ndex
190
- isEvenParity = (coordinateSystem[indexDimension, activeLeaf1ndex] & 1) == (coordinateSystem[indexDimension, connectee1ndex] & 1)
191
-
192
- if (isEvenParity and isFirstCoord) or (not isEvenParity and (isLastCoord or exceedsActive)):
193
- connectionGraph[indexDimension, activeLeaf1ndex, connectee1ndex] = connectee1ndex
194
- elif isEvenParity and not isFirstCoord:
195
- connectionGraph[indexDimension, activeLeaf1ndex, connectee1ndex] = connectee1ndex - cumulativeProduct[indexDimension]
196
- elif not isEvenParity and not (isLastCoord or exceedsActive):
197
- connectionGraph[indexDimension, activeLeaf1ndex, connectee1ndex] = connectee1ndex + cumulativeProduct[indexDimension]
198
-
199
- return connectionGraph
200
-
201
- def makeDataContainer(shape: int | tuple[int, ...], datatype: DTypeLike | None = None) -> NDArray[integer[Any]]:
202
- """Create a zeroed-out `ndarray` with the given shape and datatype.
203
-
204
- Parameters:
205
- shape: The shape of the array. Can be an integer for 1D arrays
206
- or a tuple of integers for multi-dimensional arrays.
207
- datatype ('dtypeFoldsTotal'): The desired data type for the array.
208
- If `None`, defaults to 'dtypeFoldsTotal'. Defaults to None.
209
-
210
- Returns:
211
- dataContainer: A new array of given shape and type, filled with zeros.
212
-
213
- Notes:
214
- If a version of the algorithm were to use something other than numpy, such as JAX or CUDA, because other
215
- functions use this function, it would be much easier to change the datatype "ecosystem".
216
- """
217
- numpyDtype = datatype or hackSSOTdtype('dtypeFoldsTotal')
218
- if 'numpy' == getDatatypeModule():
219
- return numpy.zeros(shape, dtype=numpyDtype)
220
- else:
221
- raise NotImplementedError("Somebody done broke it.")
222
-
223
- def outfitCountFolds(listDimensions: Sequence[int], computationDivisions: int | str | None = None, CPUlimit: bool | float | int | None = None, **keywordArguments: str | bool | None) -> computationState:
224
- """
225
- Initializes and configures the computation state for map folding computations.
226
-
227
- Parameters:
228
- listDimensions: The dimensions of the map to be folded
229
- computationDivisions (None): see `getTaskDivisions`
230
- CPUlimit (None): see `setCPUlimit`
231
- **keywordArguments: Datatype management, it's complicated: see the code below.
232
-
233
- Returns:
234
- stateInitialized: The initialized computation state
235
- """
236
- # keywordArguments START
237
- kwourGrapes = keywordArguments.get('sourGrapes', None)
238
- if kwourGrapes:
239
- sourGrapes = True
240
- else:
241
- sourGrapes = False
242
-
243
- ImaSetTheDatatype = keywordArguments.get('datatypeElephino', None)
244
- if ImaSetTheDatatype:
245
- ImaSetTheDatatype = str(ImaSetTheDatatype)
246
- setDatatypeElephino(ImaSetTheDatatype, sourGrapes)
157
+ 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.")
158
+ return int(max(0, taskDivisions))
247
159
 
248
- ImaSetTheDatatype = keywordArguments.get('datatypeFoldsTotal', None)
249
- if ImaSetTheDatatype:
250
- ImaSetTheDatatype = str(ImaSetTheDatatype)
251
- setDatatypeFoldsTotal(ImaSetTheDatatype, sourGrapes)
252
-
253
- ImaSetTheDatatype = keywordArguments.get('datatypeLeavesTotal', None)
254
- if ImaSetTheDatatype:
255
- ImaSetTheDatatype = str(ImaSetTheDatatype)
256
- setDatatypeLeavesTotal(ImaSetTheDatatype, sourGrapes)
257
-
258
- # NOTE well: this might be only hypothetical because as of this writing, `makeDataContainer` only makes numpy.zeros. But it's here in case things change.
259
- ImaSetTheDatatype = keywordArguments.get('datatypeModule', None)
260
- if ImaSetTheDatatype:
261
- ImaSetTheDatatype = str(ImaSetTheDatatype)
262
- setDatatypeModule(ImaSetTheDatatype, sourGrapes)
263
- # keywordArguments END
264
-
265
- my = makeDataContainer(len(indexMy), hackSSOTdtype('my'))
266
-
267
- mapShape = tuple(sorted(validateListDimensions(listDimensions)))
268
- concurrencyLimit = setCPUlimit(CPUlimit)
269
- my[indexMy.taskDivisions] = getTaskDivisions(computationDivisions, concurrencyLimit, CPUlimit, mapShape)
270
-
271
- foldGroups = makeDataContainer(max(my[indexMy.taskDivisions] + 1, 2), hackSSOTdtype('foldGroups'))
160
+ def outfitCountFolds(mapShape: tuple[int, ...], computationDivisions: int | str | None = None, concurrencyLimit: int = 1) -> ComputationState:
272
161
  leavesTotal = getLeavesTotal(mapShape)
273
- foldGroups[-1] = leavesTotal
274
-
275
- my[indexMy.dimensionsTotal] = len(mapShape)
276
- my[indexMy.leaf1ndex] = 1
277
- stateInitialized = computationState(
278
- connectionGraph = makeConnectionGraph(mapShape, datatype=hackSSOTdatatype('connectionGraph')),
279
- foldGroups = foldGroups,
280
- mapShape = numpy.array(mapShape, dtype=hackSSOTdtype('mapShape')),
281
- my = my,
282
- gapsWhere = makeDataContainer(int(leavesTotal) * int(leavesTotal) + 1, hackSSOTdtype('gapsWhere')),
283
- track = makeDataContainer((len(indexTrack), leavesTotal + 1), hackSSOTdtype('track')),
284
- )
285
-
286
- return stateInitialized
287
-
288
- def parseDimensions(dimensions: Sequence[int], parameterName: str = 'listDimensions') -> list[int]:
289
- """
290
- Parse and validate the dimensions are non-negative integers.
291
-
292
- Parameters:
293
- dimensions: Sequence of integers representing dimensions.
294
- parameterName ('listDimensions'): Name of the parameter for error messages. Defaults to 'listDimensions'.
295
- Returns:
296
- listNonNegative: List of validated non-negative integers.
297
- Raises:
298
- ValueError: If any dimension is negative or if the list is empty.
299
- TypeError: If any element cannot be converted to integer (raised by `intInnit`).
300
- """
301
- listValidated: list[int] = intInnit(dimensions, parameterName)
302
- listNonNegative: list[int] = []
303
- for dimension in listValidated:
304
- if dimension < 0:
305
- raise ValueError(f"Dimension {dimension} must be non-negative")
306
- listNonNegative.append(dimension)
307
-
308
- return listNonNegative
309
-
310
- def saveFoldsTotal(pathFilename: str | os.PathLike[str], foldsTotal: int) -> None:
311
- """
312
- Save foldsTotal with multiple fallback mechanisms.
313
-
314
- Parameters:
315
- pathFilename: Target save location
316
- foldsTotal: Critical computed value to save
317
- """
318
- try:
319
- pathFilenameFoldsTotal = Path(pathFilename)
320
- pathFilenameFoldsTotal.parent.mkdir(parents=True, exist_ok=True)
321
- pathFilenameFoldsTotal.write_text(str(foldsTotal))
322
- except Exception as ERRORmessage:
323
- try:
324
- print(f"\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal\n\n{foldsTotal=}\n\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal\n")
325
- print(ERRORmessage)
326
- print(f"\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal\n\n{foldsTotal=}\n\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal\n")
327
- randomnessPlanB = (int(str(foldsTotal).strip()[-1]) + 1) * ['YO_']
328
- filenameInfixUnique = ''.join(randomnessPlanB)
329
- pathFilenamePlanB = os.path.join(os.getcwd(), 'foldsTotal' + filenameInfixUnique + '.txt')
330
- writeStreamFallback = open(pathFilenamePlanB, 'w')
331
- writeStreamFallback.write(str(foldsTotal))
332
- writeStreamFallback.close()
333
- print(str(pathFilenamePlanB))
334
- except Exception:
335
- print(foldsTotal)
336
-
337
- def setCPUlimit(CPUlimit: Any | None) -> int:
338
- """Sets CPU limit for Numba concurrent operations. Note that it can only affect Numba-jitted functions that have not yet been imported.
339
-
340
- Parameters:
341
- CPUlimit: whether and how to limit the CPU usage. See notes for details.
342
- Returns:
343
- concurrencyLimit: The actual concurrency limit that was set
344
- Raises:
345
- TypeError: If CPUlimit is not of the expected types
346
-
347
- Limits on CPU usage `CPUlimit`:
348
- - `False`, `None`, or `0`: No limits on CPU usage; uses all available CPUs. All other values will potentially limit CPU usage.
349
- - `True`: Yes, limit the CPU usage; limits to 1 CPU.
350
- - Integer `>= 1`: Limits usage to the specified number of CPUs.
351
- - Decimal value (`float`) between 0 and 1: Fraction of total CPUs to use.
352
- - Decimal value (`float`) between -1 and 0: Fraction of CPUs to *not* use.
353
- - Integer `<= -1`: Subtract the absolute value from total CPUs.
354
- """
355
- if not (CPUlimit is None or isinstance(CPUlimit, (bool, int, float))):
356
- CPUlimit = oopsieKwargsie(CPUlimit)
357
-
358
- concurrencyLimit = int(defineConcurrencyLimit(CPUlimit))
359
- set_num_threads(concurrencyLimit)
360
- concurrencyLimit: int = get_num_threads()
361
-
362
- return concurrencyLimit
363
-
364
- def validateListDimensions(listDimensions: Sequence[int]) -> list[int]:
365
- """
366
- Validates and sorts a sequence of at least two positive dimensions.
367
-
368
- Parameters:
369
- listDimensions: A sequence of integer dimensions to be validated.
370
-
371
- Returns:
372
- dimensionsValidSorted: A list, with at least two elements, of only positive integers.
373
-
374
- Raises:
375
- ValueError: If the input listDimensions is empty.
376
- NotImplementedError: If the resulting list of positive dimensions has fewer than two elements.
377
- """
378
- if not listDimensions:
379
- raise ValueError("listDimensions is a required parameter.")
380
- listNonNegative = parseDimensions(listDimensions, 'listDimensions')
381
- dimensionsValid = [dimension for dimension in listNonNegative if dimension > 0]
382
- if len(dimensionsValid) < 2:
383
- raise NotImplementedError(f"This function requires listDimensions, {listDimensions}, to have at least two dimensions greater than 0. You may want to look at https://oeis.org/.")
384
- return sorted(dimensionsValid)
162
+ taskDivisions = getTaskDivisions(computationDivisions, concurrencyLimit, leavesTotal)
163
+ computationStateInitialized = ComputationState(mapShape, leavesTotal, taskDivisions)
164
+ return computationStateInitialized