mapFolding 0.6.0__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.
- mapFolding/__init__.py +6 -104
- mapFolding/basecamp.py +12 -8
- mapFolding/beDRY.py +96 -286
- mapFolding/filesystem.py +87 -0
- mapFolding/noHomeYet.py +20 -0
- mapFolding/oeis.py +46 -39
- mapFolding/reference/flattened.py +377 -0
- mapFolding/reference/hunterNumba.py +132 -0
- mapFolding/reference/irvineJavaPort.py +120 -0
- mapFolding/reference/jax.py +208 -0
- mapFolding/reference/lunnan.py +153 -0
- mapFolding/reference/lunnanNumpy.py +123 -0
- mapFolding/reference/lunnanWhile.py +121 -0
- mapFolding/reference/rotatedEntryPoint.py +240 -0
- mapFolding/reference/total_countPlus1vsPlusN.py +211 -0
- mapFolding/someAssemblyRequired/Z0Z_workbench.py +34 -0
- mapFolding/someAssemblyRequired/__init__.py +16 -0
- mapFolding/someAssemblyRequired/getLLVMforNoReason.py +21 -0
- mapFolding/someAssemblyRequired/ingredientsNumba.py +100 -0
- mapFolding/someAssemblyRequired/synthesizeCountingFunctions.py +7 -0
- mapFolding/someAssemblyRequired/synthesizeDataConverters.py +135 -0
- mapFolding/someAssemblyRequired/synthesizeNumba.py +91 -0
- mapFolding/someAssemblyRequired/synthesizeNumbaJob.py +417 -0
- mapFolding/someAssemblyRequired/synthesizeNumbaModules.py +91 -0
- mapFolding/someAssemblyRequired/transformationTools.py +425 -0
- mapFolding/someAssemblyRequired/whatWillBe.py +311 -0
- mapFolding/syntheticModules/__init__.py +0 -0
- mapFolding/syntheticModules/dataNamespaceFlattened.py +30 -0
- mapFolding/syntheticModules/numbaCount.py +90 -0
- mapFolding/syntheticModules/numbaCountExample.py +158 -0
- mapFolding/syntheticModules/numbaCountSequential.py +110 -0
- mapFolding/syntheticModules/numbaCount_doTheNeedful.py +13 -0
- mapFolding/syntheticModules/numba_doTheNeedful.py +12 -0
- mapFolding/syntheticModules/numba_doTheNeedfulExample.py +13 -0
- mapFolding/theDao.py +203 -227
- mapFolding/theSSOT.py +255 -102
- {mapfolding-0.6.0.dist-info → mapfolding-0.7.0.dist-info}/METADATA +7 -6
- mapfolding-0.7.0.dist-info/RECORD +50 -0
- {mapfolding-0.6.0.dist-info → mapfolding-0.7.0.dist-info}/WHEEL +1 -1
- {mapfolding-0.6.0.dist-info → mapfolding-0.7.0.dist-info}/top_level.txt +1 -0
- tests/__init__.py +0 -0
- tests/conftest.py +278 -0
- tests/test_computations.py +49 -0
- tests/test_filesystem.py +52 -0
- tests/test_oeis.py +128 -0
- tests/test_other.py +84 -0
- tests/test_tasks.py +50 -0
- mapFolding/theConfiguration.py +0 -58
- mapFolding/theSSOTdatatypes.py +0 -155
- mapFolding/theWrongWay.py +0 -7
- mapfolding-0.6.0.dist-info/RECORD +0 -16
- {mapfolding-0.6.0.dist-info → mapfolding-0.7.0.dist-info}/LICENSE +0 -0
- {mapfolding-0.6.0.dist-info → mapfolding-0.7.0.dist-info}/entry_points.txt +0 -0
mapFolding/__init__.py
CHANGED
|
@@ -1,107 +1,9 @@
|
|
|
1
|
-
from
|
|
2
|
-
from
|
|
3
|
-
import importlib
|
|
1
|
+
from mapFolding.basecamp import countFolds
|
|
2
|
+
from mapFolding.oeis import clearOEIScache, getOEISids, OEIS_for_n
|
|
4
3
|
|
|
5
|
-
|
|
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
|
-
'
|
|
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
|
|
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
|
-
|
|
42
|
+
mapShape: tuple[int, ...] = validateListDimensions(listDimensions)
|
|
43
|
+
concurrencyLimit: int = setCPUlimit(CPUlimit)
|
|
44
|
+
computationStateInitialized: ComputationState = outfitCountFolds(mapShape, computationDivisions, concurrencyLimit)
|
|
41
45
|
|
|
42
|
-
dispatcher =
|
|
43
|
-
dispatcher(
|
|
46
|
+
dispatcher = getPackageDispatcher()
|
|
47
|
+
computationStateComplete: ComputationState = dispatcher(computationStateInitialized)
|
|
44
48
|
|
|
45
|
-
|
|
49
|
+
computationStateComplete.getFoldsTotal()
|
|
46
50
|
|
|
47
51
|
if pathLikeWriteFoldsTotal is not None:
|
|
48
|
-
pathFilenameFoldsTotal: Path = getPathFilenameFoldsTotal(
|
|
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,112 @@
|
|
|
1
1
|
"""A relatively stable API for oft-needed functionality."""
|
|
2
|
-
from mapFolding import (
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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)
|
|
28
53
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
|
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)
|
|
40
62
|
|
|
41
|
-
|
|
42
|
-
|
|
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
|
|
43
70
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
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)
|
|
48
74
|
|
|
49
|
-
def
|
|
50
|
-
"""
|
|
51
|
-
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.
|
|
52
77
|
|
|
53
78
|
Parameters:
|
|
54
|
-
|
|
55
|
-
|
|
79
|
+
CPUlimit: whether and how to limit the CPU usage. See notes for details.
|
|
56
80
|
Returns:
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
listPositive = [dimension for dimension in listNonNegative if dimension > 0]
|
|
61
|
-
|
|
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
|
|
70
|
-
|
|
71
|
-
return productDimensions
|
|
72
|
-
|
|
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`.
|
|
75
|
-
|
|
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.
|
|
78
|
-
|
|
79
|
-
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.
|
|
81
|
+
concurrencyLimit: The actual concurrency limit that was set
|
|
82
|
+
Raises:
|
|
83
|
+
TypeError: If CPUlimit is not of the expected types
|
|
83
84
|
|
|
84
|
-
|
|
85
|
-
|
|
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.
|
|
86
92
|
"""
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
|
94
104
|
else:
|
|
95
|
-
|
|
105
|
+
raise NotImplementedError("This function only supports the 'numba' concurrency package.")
|
|
96
106
|
|
|
97
|
-
|
|
98
|
-
return pathFilenameFoldsTotal
|
|
107
|
+
return concurrencyLimit
|
|
99
108
|
|
|
100
|
-
def getTaskDivisions(computationDivisions: int | str | None, concurrencyLimit: int,
|
|
109
|
+
def getTaskDivisions(computationDivisions: int | str | None, concurrencyLimit: int, leavesTotal: int) -> int:
|
|
101
110
|
"""
|
|
102
111
|
Determines whether to divide the computation into tasks and how many divisions.
|
|
103
112
|
|
|
@@ -131,12 +140,11 @@ def getTaskDivisions(computationDivisions: int | str | None, concurrencyLimit: i
|
|
|
131
140
|
Task divisions should not exceed total leaves or the folds will be over-counted.
|
|
132
141
|
"""
|
|
133
142
|
taskDivisions = 0
|
|
134
|
-
leavesTotal = getLeavesTotal(listDimensions)
|
|
135
143
|
if not computationDivisions:
|
|
136
144
|
pass
|
|
137
145
|
elif isinstance(computationDivisions, int):
|
|
138
146
|
taskDivisions = computationDivisions
|
|
139
|
-
elif isinstance(computationDivisions, str):
|
|
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.
|
|
140
148
|
computationDivisions = computationDivisions.lower()
|
|
141
149
|
if computationDivisions == 'maximum':
|
|
142
150
|
taskDivisions = leavesTotal
|
|
@@ -146,209 +154,11 @@ def getTaskDivisions(computationDivisions: int | str | None, concurrencyLimit: i
|
|
|
146
154
|
raise ValueError(f"I received {computationDivisions} for the parameter, `computationDivisions`, but the so-called programmer didn't implement code for that.")
|
|
147
155
|
|
|
148
156
|
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
|
|
150
|
-
|
|
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)
|
|
170
|
-
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]
|
|
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))
|
|
195
159
|
|
|
196
|
-
|
|
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'))
|
|
160
|
+
def outfitCountFolds(mapShape: tuple[int, ...], computationDivisions: int | str | None = None, concurrencyLimit: int = 1) -> ComputationState:
|
|
242
161
|
leavesTotal = getLeavesTotal(mapShape)
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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)
|
|
162
|
+
taskDivisions = getTaskDivisions(computationDivisions, concurrencyLimit, leavesTotal)
|
|
163
|
+
computationStateInitialized = ComputationState(mapShape, leavesTotal, taskDivisions)
|
|
164
|
+
return computationStateInitialized
|
mapFolding/filesystem.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Filesystem functions for mapFolding package."""
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
def saveFoldsTotal(pathFilename: str | os.PathLike[str], foldsTotal: int) -> None:
|
|
6
|
+
"""
|
|
7
|
+
Save foldsTotal with multiple fallback mechanisms.
|
|
8
|
+
|
|
9
|
+
Parameters:
|
|
10
|
+
pathFilename: Target save location
|
|
11
|
+
foldsTotal: Critical computed value to save
|
|
12
|
+
"""
|
|
13
|
+
try:
|
|
14
|
+
pathFilenameFoldsTotal = Path(pathFilename)
|
|
15
|
+
pathFilenameFoldsTotal.parent.mkdir(parents=True, exist_ok=True)
|
|
16
|
+
pathFilenameFoldsTotal.write_text(str(foldsTotal))
|
|
17
|
+
except Exception as ERRORmessage:
|
|
18
|
+
try:
|
|
19
|
+
print(f"\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal\n\n{foldsTotal=}\n\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal\n")
|
|
20
|
+
print(ERRORmessage)
|
|
21
|
+
print(f"\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal\n\n{foldsTotal=}\n\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal\n")
|
|
22
|
+
randomnessPlanB = (int(str(foldsTotal).strip()[-1]) + 1) * ['YO_']
|
|
23
|
+
filenameInfixUnique = ''.join(randomnessPlanB)
|
|
24
|
+
pathFilenamePlanB = os.path.join(os.getcwd(), 'foldsTotal' + filenameInfixUnique + '.txt')
|
|
25
|
+
writeStreamFallback = open(pathFilenamePlanB, 'w')
|
|
26
|
+
writeStreamFallback.write(str(foldsTotal))
|
|
27
|
+
writeStreamFallback.close()
|
|
28
|
+
print(str(pathFilenamePlanB))
|
|
29
|
+
except Exception:
|
|
30
|
+
print(foldsTotal)
|
|
31
|
+
return None
|
|
32
|
+
|
|
33
|
+
def getFilenameFoldsTotal(mapShape: tuple[int, ...]) -> str:
|
|
34
|
+
"""Imagine your computer has been counting folds for 9 days, and when it tries to save your newly discovered value,
|
|
35
|
+
the filename is invalid. I bet you think this function is more important after that thought experiment.
|
|
36
|
+
|
|
37
|
+
Make a standardized filename for the computed value `foldsTotal`.
|
|
38
|
+
|
|
39
|
+
The filename takes into account
|
|
40
|
+
- the dimensions of the map, aka `mapShape`, aka `listDimensions`
|
|
41
|
+
- no spaces in the filename
|
|
42
|
+
- safe filesystem characters
|
|
43
|
+
- unique extension
|
|
44
|
+
- Python-safe strings:
|
|
45
|
+
- no starting with a number
|
|
46
|
+
- no reserved words
|
|
47
|
+
- no dashes or other special characters
|
|
48
|
+
- uh, I can't remember, but I found some other frustrating limitations
|
|
49
|
+
- 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
|
|
50
|
+
|
|
51
|
+
Parameters:
|
|
52
|
+
mapShape: A sequence of integers representing the dimensions of the map.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
filenameFoldsTotal: A filename string in format 'pMxN.foldsTotal' where M,N are sorted dimensions
|
|
56
|
+
"""
|
|
57
|
+
return 'p' + 'x'.join(str(dimension) for dimension in sorted(mapShape)) + '.foldsTotal'
|
|
58
|
+
|
|
59
|
+
def getPathFilenameFoldsTotal(mapShape: tuple[int, ...], pathLikeWriteFoldsTotal: str | os.PathLike[str] | None = None) -> Path:
|
|
60
|
+
"""Get a standardized path and filename for the computed value `foldsTotal`.
|
|
61
|
+
|
|
62
|
+
If you provide a directory, the function will append a standardized filename. If you provide a filename
|
|
63
|
+
or a relative path and filename, the function will prepend the default path.
|
|
64
|
+
|
|
65
|
+
Parameters:
|
|
66
|
+
mapShape: List of dimensions for the map folding problem.
|
|
67
|
+
pathLikeWriteFoldsTotal (pathJobRootDEFAULT): Path, filename, or relative path and filename. If None, uses default path.
|
|
68
|
+
Defaults to None.
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
pathFilenameFoldsTotal: Absolute path and filename.
|
|
72
|
+
"""
|
|
73
|
+
from mapFolding.theSSOT import getPathJobRootDEFAULT
|
|
74
|
+
|
|
75
|
+
if pathLikeWriteFoldsTotal is None:
|
|
76
|
+
pathFilenameFoldsTotal = getPathJobRootDEFAULT() / getFilenameFoldsTotal(mapShape)
|
|
77
|
+
else:
|
|
78
|
+
pathLikeSherpa = Path(pathLikeWriteFoldsTotal)
|
|
79
|
+
if pathLikeSherpa.is_dir():
|
|
80
|
+
pathFilenameFoldsTotal = pathLikeSherpa / getFilenameFoldsTotal(mapShape)
|
|
81
|
+
elif pathLikeSherpa.is_file() and pathLikeSherpa.is_absolute():
|
|
82
|
+
pathFilenameFoldsTotal = pathLikeSherpa
|
|
83
|
+
else:
|
|
84
|
+
pathFilenameFoldsTotal = getPathJobRootDEFAULT() / pathLikeSherpa
|
|
85
|
+
|
|
86
|
+
pathFilenameFoldsTotal.parent.mkdir(parents=True, exist_ok=True)
|
|
87
|
+
return pathFilenameFoldsTotal
|