mapFolding 0.6.0__py3-none-any.whl → 0.7.1__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 (54) hide show
  1. mapFolding/__init__.py +6 -104
  2. mapFolding/basecamp.py +12 -8
  3. mapFolding/beDRY.py +103 -286
  4. mapFolding/filesystem.py +95 -0
  5. mapFolding/noHomeYet.py +20 -0
  6. mapFolding/oeis.py +46 -39
  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 +33 -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 +357 -0
  27. mapFolding/syntheticModules/__init__.py +0 -0
  28. mapFolding/syntheticModules/dataNamespaceFlattened.py +30 -0
  29. mapFolding/syntheticModules/multiprocessingCount_doTheNeedful.py +216 -0
  30. mapFolding/syntheticModules/numbaCount.py +90 -0
  31. mapFolding/syntheticModules/numbaCountExample.py +158 -0
  32. mapFolding/syntheticModules/numbaCountSequential.py +111 -0
  33. mapFolding/syntheticModules/numbaCount_doTheNeedful.py +13 -0
  34. mapFolding/syntheticModules/numba_doTheNeedful.py +12 -0
  35. mapFolding/syntheticModules/numba_doTheNeedfulExample.py +13 -0
  36. mapFolding/theDao.py +216 -229
  37. mapFolding/theSSOT.py +269 -101
  38. {mapfolding-0.6.0.dist-info → mapfolding-0.7.1.dist-info}/METADATA +7 -6
  39. mapfolding-0.7.1.dist-info/RECORD +51 -0
  40. {mapfolding-0.6.0.dist-info → mapfolding-0.7.1.dist-info}/WHEEL +1 -1
  41. {mapfolding-0.6.0.dist-info → mapfolding-0.7.1.dist-info}/top_level.txt +1 -0
  42. tests/__init__.py +0 -0
  43. tests/conftest.py +278 -0
  44. tests/test_computations.py +53 -0
  45. tests/test_filesystem.py +52 -0
  46. tests/test_oeis.py +128 -0
  47. tests/test_other.py +84 -0
  48. tests/test_tasks.py +56 -0
  49. mapFolding/theConfiguration.py +0 -58
  50. mapFolding/theSSOTdatatypes.py +0 -155
  51. mapFolding/theWrongWay.py +0 -7
  52. mapfolding-0.6.0.dist-info/RECORD +0 -16
  53. {mapfolding-0.6.0.dist-info → mapfolding-0.7.1.dist-info}/LICENSE +0 -0
  54. {mapfolding-0.6.0.dist-info → mapfolding-0.7.1.dist-info}/entry_points.txt +0 -0
mapFolding/__init__.py CHANGED
@@ -1,107 +1,9 @@
1
- from collections import defaultdict
2
- from types import ModuleType
3
- import importlib
1
+ from mapFolding.basecamp import countFolds as 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
- 'indexMy',
45
- 'indexTrack',
46
- 'myPackageNameIs',
47
- 'pathPackage',
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
- 'additional_importsHARDCODED',
64
- 'formatFilenameModule',
65
- 'getAlgorithmDispatcher',
66
- 'getAlgorithmSource',
67
- 'getPathJobRootDEFAULT',
68
- 'getPathSyntheticModules',
69
- 'listCallablesDispatchees',
70
- 'moduleOfSyntheticModules',
71
- 'Z0Z_filenameModuleWrite',
72
- 'Z0Z_filenameWriteElseCallableTarget',
73
- 'Z0Z_getDatatypeModuleScalar',
74
- 'Z0Z_getDecoratorCallable',
75
- 'Z0Z_identifierCountFolds',
76
- 'Z0Z_setDatatypeModuleScalar',
77
- 'Z0Z_setDecoratorCallable',
78
- ])
79
-
80
- # Parameters for the prima donna
81
- _dictionaryListsImportFrom['mapFolding.theSSOT'].extend([
82
- 'ParametersNumba',
83
- 'parametersNumbaDEFAULT',
84
- 'parametersNumbaFailEarly',
85
- 'parametersNumbaMinimum',
86
- 'parametersNumbaParallelDEFAULT',
87
- 'parametersNumbaSuperJit',
88
- 'parametersNumbaSuperJitParallel',
89
- ])
90
-
91
- # Coping
92
- _dictionaryListsImportFrom['mapFolding.theSSOT'].extend([
93
- 'FREAKOUT',
94
- ])
95
-
96
- _mapSymbolToModule: dict[str, str] = {}
97
- for moduleAsStr, listSymbolsAsStr in _dictionaryListsImportFrom.items():
98
- for symbolAsStr in listSymbolsAsStr:
99
- _mapSymbolToModule[symbolAsStr] = moduleAsStr
100
-
101
- from typing import TYPE_CHECKING
102
- if TYPE_CHECKING:
103
- from basecamp import *
104
- from beDRY import *
105
- from oeis import *
106
- from theDao import *
107
- from theSSOT 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
 
@@ -37,15 +39,17 @@ def countFolds(listDimensions: Sequence[int]
37
39
  N.B.: You probably don't want to divide the computation into tasks.
38
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.
39
41
  """
40
- stateUniversal: computationState = outfitCountFolds(listDimensions, computationDivisions=computationDivisions, CPUlimit=CPUlimit)
42
+ mapShape: tuple[int, ...] = validateListDimensions(listDimensions)
43
+ concurrencyLimit: int = setCPUlimit(CPUlimit)
44
+ computationStateInitialized: ComputationState = outfitCountFolds(mapShape, computationDivisions, concurrencyLimit)
41
45
 
42
- dispatcher = getDispatcherCallable()
43
- dispatcher(**stateUniversal)
46
+ dispatcherCallableProxy = getPackageDispatcher()
47
+ computationStateComplete: ComputationState = dispatcherCallableProxy(computationStateInitialized)
44
48
 
45
- foldsTotal = int(stateUniversal['foldGroups'][0:-1].sum() * stateUniversal['foldGroups'][-1])
49
+ computationStateComplete.getFoldsTotal()
46
50
 
47
51
  if pathLikeWriteFoldsTotal is not None:
48
- pathFilenameFoldsTotal: Path = getPathFilenameFoldsTotal(stateUniversal['mapShape'], pathLikeWriteFoldsTotal)
49
- saveFoldsTotal(pathFilenameFoldsTotal, foldsTotal)
52
+ pathFilenameFoldsTotal: Path = getPathFilenameFoldsTotal(computationStateComplete.mapShape, pathLikeWriteFoldsTotal)
53
+ saveFoldsTotal(pathFilenameFoldsTotal, computationStateComplete.foldsTotal)
50
54
 
51
- return foldsTotal
55
+ return computationStateComplete.foldsTotal
mapFolding/beDRY.py CHANGED
@@ -1,103 +1,119 @@
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
- setDatatypeLeavesTotal,
2
+ from mapFolding.theSSOT import (
3
+ Array3D,
4
+ ComputationState,
5
+ getDatatypePackage,
6
+ getNumpyDtypeDefault,
11
7
  )
12
8
  from collections.abc import Sequence
13
- from numba import get_num_threads, set_num_threads
14
- from numpy import dtype, integer, ndarray
15
- from numpy.typing import DTypeLike, NDArray
16
- from pathlib import Path
17
9
  from sys import maxsize as sysMaxsize
18
10
  from typing import Any
19
11
  from Z0Z_tools import defineConcurrencyLimit, intInnit, oopsieKwargsie
20
12
  import numpy
21
- import os
22
13
 
23
- def getFilenameFoldsTotal(mapShape: Sequence[int] | ndarray[tuple[int], dtype[integer[Any]]]) -> str:
24
- """Imagine your computer has been counting folds for 9 days, and when it tries to save your newly discovered value,
25
- the filename is invalid. I bet you think this function is more important after that thought experiment.
26
-
27
- Make a standardized filename for the computed value `foldsTotal`.
28
-
29
- The filename takes into account
30
- - the dimensions of the map, aka `mapShape`, aka `listDimensions`
31
- - no spaces in the filename
32
- - safe filesystem characters
33
- - unique extension
34
- - Python-safe strings:
35
- - no starting with a number
36
- - no reserved words
37
- - no dashes or other special characters
38
- - uh, I can't remember, but I found some other frustrating limitations
39
- - 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
40
-
41
- Parameters:
42
- mapShape: A sequence of integers representing the dimensions of the map.
43
-
44
- Returns:
45
- filenameFoldsTotal: A filename string in format 'pMxN.foldsTotal' where M,N are sorted dimensions
46
- """
47
- return 'p' + 'x'.join(str(dimension) for dimension in sorted(mapShape)) + '.foldsTotal'
48
-
49
- def getLeavesTotal(listDimensions: Sequence[int]) -> int:
50
- """
51
- How many leaves are in the map.
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)
52
53
 
53
- Parameters:
54
- listDimensions: A list of integers representing dimensions.
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)
55
62
 
56
- Returns:
57
- productDimensions: The product of all positive integer dimensions.
58
- """
59
- listNonNegative = parseDimensions(listDimensions, 'listDimensions')
60
- listPositive = [dimension for dimension in listNonNegative if dimension > 0]
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
61
70
 
62
- if not listPositive:
63
- return 0
64
- else:
65
- productDimensions = 1
66
- for dimension in listPositive:
67
- if dimension > sysMaxsize // productDimensions:
68
- raise OverflowError(f"I received {dimension=} in {listDimensions=}, but the product of the dimensions exceeds the maximum size of an integer on this system.")
69
- productDimensions *= dimension
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)
70
74
 
71
- return productDimensions
75
+ def setCPUlimit(CPUlimit: Any | None) -> int:
76
+ """Sets CPU limit for concurrent operations.
72
77
 
73
- def getPathFilenameFoldsTotal(mapShape: Sequence[int] | ndarray[tuple[int], dtype[integer[Any]]], pathLikeWriteFoldsTotal: str | os.PathLike[str] | None = None) -> Path:
74
- """Get a standardized path and filename for the computed value `foldsTotal`.
78
+ If the concurrency is managed by `numba`, the maximum number of CPUs is retrieved from `numba.get_num_threads()` and not by polling the hardware. Therefore, if there are
79
+ numba environment variables limiting the number of available CPUs, that will effect this function. That _should_ be a good thing: you control the number of CPUs available
80
+ to numba. But if you're not aware of that, you might be surprised by the results.
75
81
 
76
- If you provide a directory, the function will append a standardized filename. If you provide a filename
77
- or a relative path and filename, the function will prepend the default path.
82
+ If you are designing custom modules that use numba, note that you must call `numba.set_num_threads()` (i.e., this function) before executing an `import` statement
83
+ on a Numba-jitted function. Otherwise, the `numba.set_num_threads()` call will have no effect on the imported function.
78
84
 
79
85
  Parameters:
80
- mapShape: List of dimensions for the map folding problem.
81
- pathLikeWriteFoldsTotal (pathJobRootDEFAULT): Path, filename, or relative path and filename. If None, uses default path.
82
- Defaults to None.
83
-
86
+ CPUlimit: whether and how to limit the CPU usage. See notes for details.
84
87
  Returns:
85
- pathFilenameFoldsTotal: Absolute path and filename.
88
+ concurrencyLimit: The actual concurrency limit that was set
89
+ Raises:
90
+ TypeError: If CPUlimit is not of the expected types
91
+
92
+ Limits on CPU usage `CPUlimit`:
93
+ - `False`, `None`, or `0`: No limits on CPU usage; uses all available CPUs. All other values will potentially limit CPU usage.
94
+ - `True`: Yes, limit the CPU usage; limits to 1 CPU.
95
+ - Integer `>= 1`: Limits usage to the specified number of CPUs.
96
+ - Decimal value (`float`) between 0 and 1: Fraction of total CPUs to use.
97
+ - Decimal value (`float`) between -1 and 0: Fraction of CPUs to *not* use.
98
+ - Integer `<= -1`: Subtract the absolute value from total CPUs.
86
99
  """
87
- pathLikeSherpa = Path(pathLikeWriteFoldsTotal) if pathLikeWriteFoldsTotal is not None else None
88
- if not pathLikeSherpa:
89
- pathLikeSherpa = getPathJobRootDEFAULT()
90
- if pathLikeSherpa.is_dir():
91
- pathFilenameFoldsTotal = pathLikeSherpa / getFilenameFoldsTotal(mapShape)
92
- elif pathLikeSherpa.is_absolute():
93
- pathFilenameFoldsTotal = pathLikeSherpa
94
- else:
95
- pathFilenameFoldsTotal = getPathJobRootDEFAULT() / pathLikeSherpa
100
+ if not (CPUlimit is None or isinstance(CPUlimit, (bool, int, float))):
101
+ CPUlimit = oopsieKwargsie(CPUlimit)
96
102
 
97
- pathFilenameFoldsTotal.parent.mkdir(parents=True, exist_ok=True)
98
- return pathFilenameFoldsTotal
103
+ from mapFolding.theSSOT import concurrencyPackage
104
+ if concurrencyPackage == 'numba':
105
+ from numba import get_num_threads, set_num_threads
106
+ concurrencyLimit: int = defineConcurrencyLimit(CPUlimit, get_num_threads())
107
+ set_num_threads(concurrencyLimit)
108
+ concurrencyLimit = get_num_threads()
109
+ elif concurrencyPackage == 'algorithm':
110
+ # When to use multiprocessing.set_start_method https://github.com/hunterhogan/mapFolding/issues/6
111
+ concurrencyLimit: int = defineConcurrencyLimit(CPUlimit)
112
+ else:
113
+ raise NotImplementedError(f"I received {concurrencyPackage=} but I don't know what to do with that.")
114
+ return concurrencyLimit
99
115
 
100
- def getTaskDivisions(computationDivisions: int | str | None, concurrencyLimit: int, CPUlimit: bool | float | int | None, listDimensions: Sequence[int]) -> int:
116
+ def getTaskDivisions(computationDivisions: int | str | None, concurrencyLimit: int, leavesTotal: int) -> int:
101
117
  """
102
118
  Determines whether to divide the computation into tasks and how many divisions.
103
119
 
@@ -131,12 +147,11 @@ def getTaskDivisions(computationDivisions: int | str | None, concurrencyLimit: i
131
147
  Task divisions should not exceed total leaves or the folds will be over-counted.
132
148
  """
133
149
  taskDivisions = 0
134
- leavesTotal = getLeavesTotal(listDimensions)
135
150
  if not computationDivisions:
136
151
  pass
137
152
  elif isinstance(computationDivisions, int):
138
153
  taskDivisions = computationDivisions
139
- 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.
154
+ 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.
140
155
  computationDivisions = computationDivisions.lower()
141
156
  if computationDivisions == 'maximum':
142
157
  taskDivisions = leavesTotal
@@ -146,209 +161,11 @@ def getTaskDivisions(computationDivisions: int | str | None, concurrencyLimit: i
146
161
  raise ValueError(f"I received {computationDivisions} for the parameter, `computationDivisions`, but the so-called programmer didn't implement code for that.")
147
162
 
148
163
  if taskDivisions > leavesTotal:
149
- 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.")
164
+ 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.")
165
+ return int(max(0, taskDivisions))
150
166
 
151
- return taskDivisions
152
-
153
- def makeConnectionGraph(listDimensions: Sequence[int], **keywordArguments: str | None) -> ndarray[tuple[int, int, int], dtype[integer[Any]]]:
154
- """
155
- Constructs a multi-dimensional connection graph representing the connections between the leaves of a map with the given dimensions.
156
- Also called a Cartesian product decomposition or dimensional product mapping.
157
-
158
- Parameters
159
- listDimensions: A sequence of integers representing the dimensions of the map.
160
- **keywordArguments: Datatype management.
161
-
162
- Returns
163
- connectionGraph: A 3D numpy array with shape of (dimensionsTotal, leavesTotal + 1, leavesTotal + 1).
164
- """
165
- ImaSetTheDatatype = keywordArguments.get('datatype', None)
166
- if ImaSetTheDatatype:
167
- setDatatypeLeavesTotal(ImaSetTheDatatype)
168
- dtype = hackSSOTdtype('connectionGraph')
169
- mapShape = validateListDimensions(listDimensions)
167
+ def outfitCountFolds(mapShape: tuple[int, ...], computationDivisions: int | str | None = None, concurrencyLimit: int = 1) -> ComputationState:
170
168
  leavesTotal = getLeavesTotal(mapShape)
171
- arrayDimensions = numpy.array(mapShape, dtype=dtype)
172
- dimensionsTotal = len(arrayDimensions)
173
-
174
- cumulativeProduct = numpy.multiply.accumulate([1] + mapShape, dtype=dtype)
175
- coordinateSystem = numpy.zeros((dimensionsTotal, leavesTotal + 1), dtype=dtype)
176
- for indexDimension in range(dimensionsTotal):
177
- for leaf1ndex in range(1, leavesTotal + 1):
178
- coordinateSystem[indexDimension, leaf1ndex] = ( ((leaf1ndex - 1) // cumulativeProduct[indexDimension]) % arrayDimensions[indexDimension] + 1 )
179
-
180
- connectionGraph: ndarray[tuple[int, int, int], numpy.dtype[integer[Any]]] = numpy.zeros((dimensionsTotal, leavesTotal + 1, leavesTotal + 1), dtype=dtype)
181
- for indexDimension in range(dimensionsTotal):
182
- for activeLeaf1ndex in range(1, leavesTotal + 1):
183
- for connectee1ndex in range(1, activeLeaf1ndex + 1):
184
- isFirstCoord = coordinateSystem[indexDimension, connectee1ndex] == 1
185
- isLastCoord = coordinateSystem[indexDimension, connectee1ndex] == arrayDimensions[indexDimension]
186
- exceedsActive = connectee1ndex + cumulativeProduct[indexDimension] > activeLeaf1ndex
187
- isEvenParity = (coordinateSystem[indexDimension, activeLeaf1ndex] & 1) == (coordinateSystem[indexDimension, connectee1ndex] & 1)
188
-
189
- if (isEvenParity and isFirstCoord) or (not isEvenParity and (isLastCoord or exceedsActive)):
190
- connectionGraph[indexDimension, activeLeaf1ndex, connectee1ndex] = connectee1ndex
191
- elif isEvenParity and not isFirstCoord:
192
- connectionGraph[indexDimension, activeLeaf1ndex, connectee1ndex] = connectee1ndex - cumulativeProduct[indexDimension]
193
- elif not isEvenParity and not (isLastCoord or exceedsActive):
194
- connectionGraph[indexDimension, activeLeaf1ndex, connectee1ndex] = connectee1ndex + cumulativeProduct[indexDimension]
195
-
196
- return connectionGraph
197
-
198
- def makeDataContainer(shape: int | tuple[int, ...], datatype: DTypeLike | None = None) -> NDArray[integer[Any]]:
199
- """Create a zeroed-out `ndarray` with the given shape and datatype.
200
-
201
- Parameters:
202
- shape: The shape of the array. Can be an integer for 1D arrays
203
- or a tuple of integers for multi-dimensional arrays.
204
- datatype ('dtypeFoldsTotal'): The desired data type for the array.
205
- If `None`, defaults to 'dtypeFoldsTotal'. Defaults to None.
206
-
207
- Returns:
208
- dataContainer: A new array of given shape and type, filled with zeros.
209
-
210
- Notes:
211
- If a version of the algorithm were to use something other than numpy, such as JAX or CUDA, because other
212
- functions use this function, it would be much easier to change the datatype "ecosystem".
213
- """
214
- numpyDtype = datatype or hackSSOTdtype('dtypeFoldsTotal')
215
- if 'numpy' == getDatatypeModule():
216
- return numpy.zeros(shape, dtype=numpyDtype)
217
- else:
218
- raise NotImplementedError("Somebody done broke it.")
219
-
220
- def outfitCountFolds(listDimensions: Sequence[int]
221
- , computationDivisions: int | str | None = None
222
- , CPUlimit: bool | float | int | None = None
223
- ) -> 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
-
232
- Returns:
233
- stateInitialized: The initialized computation state
234
- """
235
- my = makeDataContainer(len(indexMy), hackSSOTdtype('my'))
236
-
237
- mapShape = tuple(sorted(validateListDimensions(listDimensions)))
238
- concurrencyLimit = setCPUlimit(CPUlimit)
239
- my[indexMy.taskDivisions] = getTaskDivisions(computationDivisions, concurrencyLimit, CPUlimit, mapShape)
240
-
241
- foldGroups = makeDataContainer(max(my[indexMy.taskDivisions] + 1, 2), hackSSOTdtype('foldGroups'))
242
- leavesTotal = getLeavesTotal(mapShape)
243
- foldGroups[-1] = leavesTotal
244
-
245
- my[indexMy.dimensionsTotal] = len(mapShape)
246
- my[indexMy.leaf1ndex] = 1
247
- stateInitialized = computationState(
248
- connectionGraph = makeConnectionGraph(mapShape, datatype=hackSSOTdatatype('connectionGraph')),
249
- foldGroups = foldGroups,
250
- mapShape = numpy.array(mapShape, dtype=hackSSOTdtype('mapShape')),
251
- my = my,
252
- gapsWhere = makeDataContainer(int(leavesTotal) * int(leavesTotal) + 1, hackSSOTdtype('gapsWhere')),
253
- track = makeDataContainer((len(indexTrack), leavesTotal + 1), hackSSOTdtype('track')),
254
- )
255
-
256
- return stateInitialized
257
-
258
- def parseDimensions(dimensions: Sequence[int], parameterName: str = 'listDimensions') -> list[int]:
259
- """
260
- Parse and validate the dimensions are non-negative integers.
261
-
262
- Parameters:
263
- dimensions: Sequence of integers representing dimensions.
264
- parameterName ('listDimensions'): Name of the parameter for error messages. Defaults to 'listDimensions'.
265
- Returns:
266
- listNonNegative: List of validated non-negative integers.
267
- Raises:
268
- ValueError: If any dimension is negative or if the list is empty.
269
- TypeError: If any element cannot be converted to integer (raised by `intInnit`).
270
- """
271
- listValidated: list[int] = intInnit(dimensions, parameterName)
272
- listNonNegative: list[int] = []
273
- for dimension in listValidated:
274
- if dimension < 0:
275
- raise ValueError(f"Dimension {dimension} must be non-negative")
276
- listNonNegative.append(dimension)
277
-
278
- return listNonNegative
279
-
280
- def saveFoldsTotal(pathFilename: str | os.PathLike[str], foldsTotal: int) -> None:
281
- """
282
- Save foldsTotal with multiple fallback mechanisms.
283
-
284
- Parameters:
285
- pathFilename: Target save location
286
- foldsTotal: Critical computed value to save
287
- """
288
- try:
289
- pathFilenameFoldsTotal = Path(pathFilename)
290
- pathFilenameFoldsTotal.parent.mkdir(parents=True, exist_ok=True)
291
- pathFilenameFoldsTotal.write_text(str(foldsTotal))
292
- except Exception as ERRORmessage:
293
- try:
294
- print(f"\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal\n\n{foldsTotal=}\n\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal\n")
295
- print(ERRORmessage)
296
- print(f"\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal\n\n{foldsTotal=}\n\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal\n")
297
- randomnessPlanB = (int(str(foldsTotal).strip()[-1]) + 1) * ['YO_']
298
- filenameInfixUnique = ''.join(randomnessPlanB)
299
- pathFilenamePlanB = os.path.join(os.getcwd(), 'foldsTotal' + filenameInfixUnique + '.txt')
300
- writeStreamFallback = open(pathFilenamePlanB, 'w')
301
- writeStreamFallback.write(str(foldsTotal))
302
- writeStreamFallback.close()
303
- print(str(pathFilenamePlanB))
304
- except Exception:
305
- print(foldsTotal)
306
-
307
- def setCPUlimit(CPUlimit: Any | None) -> int:
308
- """Sets CPU limit for Numba concurrent operations. Note that it can only affect Numba-jitted functions that have not yet been imported.
309
-
310
- Parameters:
311
- CPUlimit: whether and how to limit the CPU usage. See notes for details.
312
- Returns:
313
- concurrencyLimit: The actual concurrency limit that was set
314
- Raises:
315
- TypeError: If CPUlimit is not of the expected types
316
-
317
- Limits on CPU usage `CPUlimit`:
318
- - `False`, `None`, or `0`: No limits on CPU usage; uses all available CPUs. All other values will potentially limit CPU usage.
319
- - `True`: Yes, limit the CPU usage; limits to 1 CPU.
320
- - Integer `>= 1`: Limits usage to the specified number of CPUs.
321
- - Decimal value (`float`) between 0 and 1: Fraction of total CPUs to use.
322
- - Decimal value (`float`) between -1 and 0: Fraction of CPUs to *not* use.
323
- - Integer `<= -1`: Subtract the absolute value from total CPUs.
324
- """
325
- if not (CPUlimit is None or isinstance(CPUlimit, (bool, int, float))):
326
- CPUlimit = oopsieKwargsie(CPUlimit)
327
-
328
- concurrencyLimit = int(defineConcurrencyLimit(CPUlimit))
329
- set_num_threads(concurrencyLimit)
330
- concurrencyLimit: int = get_num_threads()
331
-
332
- return concurrencyLimit
333
-
334
- def validateListDimensions(listDimensions: Sequence[int]) -> list[int]:
335
- """
336
- Validates and sorts a sequence of at least two positive dimensions.
337
-
338
- Parameters:
339
- listDimensions: A sequence of integer dimensions to be validated.
340
-
341
- Returns:
342
- dimensionsValidSorted: A list, with at least two elements, of only positive integers.
343
-
344
- Raises:
345
- ValueError: If the input listDimensions is empty.
346
- NotImplementedError: If the resulting list of positive dimensions has fewer than two elements.
347
- """
348
- if not listDimensions:
349
- raise ValueError("listDimensions is a required parameter.")
350
- listNonNegative = parseDimensions(listDimensions, 'listDimensions')
351
- dimensionsValid = [dimension for dimension in listNonNegative if dimension > 0]
352
- if len(dimensionsValid) < 2:
353
- 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/.")
354
- return sorted(dimensionsValid)
169
+ taskDivisions = getTaskDivisions(computationDivisions, concurrencyLimit, leavesTotal)
170
+ computationStateInitialized = ComputationState(mapShape, leavesTotal, taskDivisions, concurrencyLimit)
171
+ return computationStateInitialized