mapFolding 0.3.8__py3-none-any.whl → 0.3.9__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 (46) hide show
  1. mapFolding/__init__.py +38 -0
  2. mapFolding/basecamp.py +55 -0
  3. mapFolding/beDRY.py +364 -0
  4. mapFolding/oeis.py +329 -0
  5. {someAssemblyRequired → mapFolding/someAssemblyRequired}/makeJob.py +3 -3
  6. mapFolding/someAssemblyRequired/synthesizeModuleJAX.py +29 -0
  7. {someAssemblyRequired → mapFolding/someAssemblyRequired}/synthesizeModulesNumba.py +149 -89
  8. mapFolding/syntheticModules/__init__.py +3 -0
  9. syntheticModules/numbaInitialize.py → mapFolding/syntheticModules/numba_countInitialize.py +5 -7
  10. syntheticModules/numbaParallel.py → mapFolding/syntheticModules/numba_countParallel.py +5 -4
  11. syntheticModules/numbaSequential.py → mapFolding/syntheticModules/numba_countSequential.py +5 -4
  12. mapFolding/syntheticModules/numba_doTheNeedful.py +33 -0
  13. mapFolding/theDao.py +214 -0
  14. mapFolding/theSSOT.py +269 -0
  15. mapFolding-0.3.9.dist-info/LICENSE +407 -0
  16. {mapFolding-0.3.8.dist-info → mapFolding-0.3.9.dist-info}/METADATA +9 -5
  17. mapFolding-0.3.9.dist-info/RECORD +40 -0
  18. mapFolding-0.3.9.dist-info/top_level.txt +2 -0
  19. tests/__init__.py +1 -0
  20. tests/conftest.py +224 -0
  21. tests/conftest_tmpRegistry.py +62 -0
  22. tests/conftest_uniformTests.py +53 -0
  23. tests/test_oeis.py +200 -0
  24. tests/test_other.py +258 -0
  25. tests/test_tasks.py +44 -0
  26. tests/test_types.py +5 -0
  27. benchmarks/benchmarking.py +0 -67
  28. citations/constants.py +0 -3
  29. citations/updateCitation.py +0 -354
  30. mapFolding-0.3.8.dist-info/RECORD +0 -26
  31. mapFolding-0.3.8.dist-info/top_level.txt +0 -5
  32. syntheticModules/__init__.py +0 -3
  33. {reference → mapFolding/reference}/flattened.py +0 -0
  34. {reference → mapFolding/reference}/hunterNumba.py +0 -0
  35. {reference → mapFolding/reference}/irvineJavaPort.py +0 -0
  36. {reference → mapFolding/reference}/jax.py +0 -0
  37. {reference → mapFolding/reference}/lunnan.py +0 -0
  38. {reference → mapFolding/reference}/lunnanNumpy.py +0 -0
  39. {reference → mapFolding/reference}/lunnanWhile.py +0 -0
  40. {reference → mapFolding/reference}/rotatedEntryPoint.py +0 -0
  41. {reference → mapFolding/reference}/total_countPlus1vsPlusN.py +0 -0
  42. {someAssemblyRequired → mapFolding/someAssemblyRequired}/__init__.py +0 -0
  43. {someAssemblyRequired → mapFolding/someAssemblyRequired}/getLLVMforNoReason.py +0 -0
  44. {someAssemblyRequired → mapFolding/someAssemblyRequired}/synthesizeModuleJobNumba.py +0 -0
  45. {mapFolding-0.3.8.dist-info → mapFolding-0.3.9.dist-info}/WHEEL +0 -0
  46. {mapFolding-0.3.8.dist-info → mapFolding-0.3.9.dist-info}/entry_points.txt +0 -0
mapFolding/oeis.py ADDED
@@ -0,0 +1,329 @@
1
+ """Everything implementing the The Online Encyclopedia of Integer Sequences (OEIS);
2
+ _only_ things that implement _only_ the OEIS."""
3
+ from datetime import datetime, timedelta
4
+ from mapFolding import countFolds
5
+ from typing import TYPE_CHECKING, List, Callable, Dict, Final, Union, Any
6
+ import argparse
7
+ import pathlib
8
+ import random
9
+ import sys
10
+ import time
11
+ import urllib.request
12
+ import urllib.response
13
+
14
+ if TYPE_CHECKING:
15
+ from typing import TypedDict
16
+ else:
17
+ TypedDict = dict
18
+
19
+ """
20
+ Section: make `settingsOEIS`"""
21
+ class SettingsOEIS(TypedDict):
22
+ # I would prefer to load description dynamically from OEIS, but it's a pita for me
23
+ # to learn how to efficiently implement right now.
24
+ description: str
25
+ getMapShape: Callable[[int], List[int]]
26
+ valuesBenchmark: List[int]
27
+ valuesKnown: Dict[int, int]
28
+ valuesTestParallelization: List[int]
29
+ valuesTestValidation: List[int]
30
+ valueUnknown: int
31
+
32
+ settingsOEIShardcodedValues: Dict[str, Dict[str, Any]] = {
33
+ 'A001415': {
34
+ 'description': 'Number of ways of folding a 2 X n strip of stamps.',
35
+ 'getMapShape': lambda n: sorted([2, n]),
36
+ 'valuesBenchmark': [14],
37
+ 'valuesTestParallelization': [*range(3, 7)],
38
+ 'valuesTestValidation': [0, 1, random.randint(2, 9)],
39
+ },
40
+ 'A001416': {
41
+ 'description': 'Number of ways of folding a 3 X n strip of stamps.',
42
+ 'getMapShape': lambda n: sorted([3, n]),
43
+ 'valuesBenchmark': [9],
44
+ 'valuesTestParallelization': [*range(3, 5)],
45
+ 'valuesTestValidation': [0, 1, random.randint(2, 6)],
46
+ },
47
+ 'A001417': {
48
+ 'description': 'Number of ways of folding a 2 X 2 X ... X 2 n-dimensional map.',
49
+ 'getMapShape': lambda n: [2] * n,
50
+ 'valuesBenchmark': [6],
51
+ 'valuesTestParallelization': [*range(2, 4)],
52
+ 'valuesTestValidation': [0, 1, random.randint(2, 4)],
53
+ },
54
+ 'A195646': {
55
+ 'description': 'Number of ways of folding a 3 X 3 X ... X 3 n-dimensional map.',
56
+ 'getMapShape': lambda n: [3] * n,
57
+ 'valuesBenchmark': [3],
58
+ 'valuesTestParallelization': [*range(2, 3)],
59
+ 'valuesTestValidation': [0, 1, 2],
60
+ },
61
+ 'A001418': {
62
+ 'description': 'Number of ways of folding an n X n sheet of stamps.',
63
+ 'getMapShape': lambda n: [n, n],
64
+ 'valuesBenchmark': [5],
65
+ 'valuesTestParallelization': [*range(2, 4)],
66
+ # offset 1: hypothetically, if I were to load the offset from OEIS, I could use it to
67
+ # determine if a sequence is defined at n=0, which would affect, for example, the valuesTestValidation.
68
+ 'valuesTestValidation': [1, random.randint(2, 4)],
69
+ },
70
+ }
71
+
72
+ oeisIDsImplemented: Final[List[str]] = sorted([oeisID.upper().strip() for oeisID in settingsOEIShardcodedValues.keys()])
73
+ """Directly implemented OEIS IDs; standardized, e.g., 'A001415'."""
74
+
75
+ def _validateOEISid(oeisIDcandidate: str) -> str:
76
+ """
77
+ Validates an OEIS sequence ID against implemented sequences.
78
+
79
+ If the provided ID is recognized within the application's implemented
80
+ OEIS sequences, the function returns the verified ID in uppercase.
81
+ Otherwise, a KeyError is raised indicating that the sequence is not
82
+ directly supported.
83
+
84
+ Parameters:
85
+ oeisIDcandidate: The OEIS sequence identifier to validate.
86
+
87
+ Returns:
88
+ oeisID: The validated and possibly modified OEIS sequence ID, if recognized.
89
+
90
+ Raises:
91
+ KeyError: If the provided sequence ID is not directly implemented.
92
+ """
93
+ if oeisIDcandidate in oeisIDsImplemented:
94
+ return oeisIDcandidate
95
+ else:
96
+ oeisIDcleaned = str(oeisIDcandidate).upper().strip()
97
+ if oeisIDcleaned in oeisIDsImplemented:
98
+ return oeisIDcleaned
99
+ else:
100
+ raise KeyError(
101
+ f"OEIS ID {oeisIDcandidate} is not directly implemented.\n"
102
+ f"Available sequences:\n{_formatOEISsequenceInfo()}"
103
+ )
104
+
105
+ def _getFilenameOEISbFile(oeisID: str) -> str:
106
+ oeisID = _validateOEISid(oeisID)
107
+ return f"b{oeisID[1:]}.txt"
108
+
109
+ def _parseBFileOEIS(OEISbFile: str, oeisID: str) -> Dict[int, int]:
110
+ """
111
+ Parses the content of an OEIS b-file for a given sequence ID.
112
+ This function processes a multiline string representing an OEIS b-file and
113
+ creates a dictionary mapping integer indices to their corresponding sequence
114
+ values. The first line of the b-file is expected to contain a comment that
115
+ matches the given sequence ID. If it does not match, a ValueError is raised.
116
+
117
+ Parameters:
118
+ OEISbFile: A multiline string representing an OEIS b-file.
119
+ oeisID: The expected OEIS sequence identifier.
120
+ Returns:
121
+ OEISsequence: A dictionary where each key is an integer index `n` and
122
+ each value is the sequence value `a(n)` corresponding to that index.
123
+ Raises:
124
+ ValueError: If the first line of the file does not indicate the expected
125
+ sequence ID or if the content format is invalid.
126
+ """
127
+ bFileLines = OEISbFile.strip().splitlines()
128
+ # The first line has the sequence ID
129
+ if not bFileLines.pop(0).startswith(f"# {oeisID}"):
130
+ raise ValueError(f"Content does not match sequence {oeisID}")
131
+
132
+ OEISsequence = {}
133
+ for line in bFileLines:
134
+ if line.startswith('#'):
135
+ continue
136
+ n, aOFn = map(int, line.split())
137
+ OEISsequence[n] = aOFn
138
+ return OEISsequence
139
+
140
+ try:
141
+ _pathCache = pathlib.Path(__file__).parent / ".cache"
142
+ except NameError:
143
+ _pathCache = pathlib.Path.home() / ".mapFoldingCache"
144
+
145
+ def _getOEISidValues(oeisID: str) -> Dict[int, int]:
146
+ """
147
+ Retrieves the specified OEIS sequence as a dictionary mapping integer indices
148
+ to their corresponding values.
149
+ This function checks for a cached local copy of the sequence data, using it if
150
+ it has not expired. Otherwise, it fetches the sequence data from the OEIS
151
+ website and writes it to the cache. The parsed data is returned as a dictionary
152
+ mapping each index to its sequence value.
153
+
154
+ Parameters:
155
+ oeisID: The identifier of the OEIS sequence to retrieve.
156
+ Returns:
157
+ OEISsequence: A dictionary where each key is an integer index, `n`, and each
158
+ value is the corresponding "a(n)" from the OEIS entry.
159
+ Raises:
160
+ ValueError: If the cached or downloaded file format is invalid.
161
+ IOError: If there is an error reading from or writing to the local cache.
162
+ """
163
+
164
+ pathFilenameCache = _pathCache / _getFilenameOEISbFile(oeisID)
165
+ cacheDays = 7
166
+
167
+ tryCache = False
168
+ if pathFilenameCache.exists():
169
+ fileAge = datetime.now() - datetime.fromtimestamp(pathFilenameCache.stat().st_mtime)
170
+ tryCache = fileAge < timedelta(days=cacheDays)
171
+
172
+ if tryCache:
173
+ try:
174
+ OEISbFile = pathFilenameCache.read_text()
175
+ return _parseBFileOEIS(OEISbFile, oeisID)
176
+ except (ValueError, IOError):
177
+ tryCache = False
178
+
179
+ urlOEISbFile = f"https://oeis.org/{oeisID}/{_getFilenameOEISbFile(oeisID)}"
180
+ httpResponse: urllib.response.addinfourl = urllib.request.urlopen(urlOEISbFile)
181
+ OEISbFile = httpResponse.read().decode('utf-8')
182
+
183
+ if not tryCache:
184
+ pathFilenameCache.parent.mkdir(parents=True, exist_ok=True)
185
+ pathFilenameCache.write_text(OEISbFile)
186
+
187
+ return _parseBFileOEIS(OEISbFile, oeisID)
188
+
189
+ def makeSettingsOEIS() -> Dict[str, SettingsOEIS]:
190
+ """
191
+ Creates a dictionary mapping OEIS IDs to their corresponding settings.
192
+
193
+ This function initializes settings for each implemented OEIS sequence by combining
194
+ hardcoded values with dynamically retrieved OEIS sequence values.
195
+
196
+ Returns:
197
+ Dict[str, SettingsOEIS]: A dictionary where:
198
+ - Keys are OEIS sequence IDs (str)
199
+ - Values are SettingsOEIS objects containing:
200
+ - description: Text description of the sequence
201
+ - getMapShape: Function to get dimensions
202
+ - valuesBenchmark: Benchmark values
203
+ - valuesKnown: Known values from OEIS
204
+ - valuesTestValidation: Values for test validation
205
+ - valueUnknown: First unknown value in sequence
206
+
207
+ Note:
208
+ Relies on global variables:
209
+ - oeisIDsImplemented: List of implemented OEIS sequence IDs
210
+ - settingsOEIShardcodedValues: Dictionary of hardcoded settings per sequence
211
+ """
212
+ settingsTarget = {}
213
+ for oeisID in oeisIDsImplemented:
214
+ valuesKnownSherpa = _getOEISidValues(oeisID)
215
+ settingsTarget[oeisID] = SettingsOEIS(
216
+ description=settingsOEIShardcodedValues[oeisID]['description'],
217
+ getMapShape=settingsOEIShardcodedValues[oeisID]['getMapShape'],
218
+ valuesBenchmark=settingsOEIShardcodedValues[oeisID]['valuesBenchmark'],
219
+ valuesTestParallelization=settingsOEIShardcodedValues[oeisID]['valuesTestParallelization'],
220
+ valuesTestValidation=settingsOEIShardcodedValues[oeisID]['valuesTestValidation'],
221
+ valuesKnown = valuesKnownSherpa,
222
+ valueUnknown = max(valuesKnownSherpa.keys(), default=0) + 1
223
+ )
224
+ return settingsTarget
225
+
226
+ settingsOEIS: Dict[str, SettingsOEIS] = makeSettingsOEIS()
227
+ """All values and settings for `oeisIDsImplemented`."""
228
+
229
+ """
230
+ Section: private functions"""
231
+
232
+ def _formatHelpText() -> str:
233
+ """Format standardized help text for both CLI and interactive use."""
234
+ exampleOEISid = oeisIDsImplemented[0]
235
+ exampleN = settingsOEIS[exampleOEISid]['valuesTestValidation'][-1]
236
+
237
+ return (
238
+ "\nAvailable OEIS sequences:\n"
239
+ f"{_formatOEISsequenceInfo()}\n"
240
+ "\nUsage examples:\n"
241
+ " Command line:\n"
242
+ f" OEIS_for_n {exampleOEISid} {exampleN}\n"
243
+ " Python:\n"
244
+ " from mapFolding import oeisIDfor_n\n"
245
+ f" foldsTotal = oeisIDfor_n('{exampleOEISid}', {exampleN})"
246
+ )
247
+
248
+ def _formatOEISsequenceInfo() -> str:
249
+ """Format information about available OEIS sequences for display or error messages."""
250
+ return "\n".join(
251
+ f" {oeisID}: {settingsOEIS[oeisID]['description']}"
252
+ for oeisID in oeisIDsImplemented
253
+ )
254
+
255
+ """
256
+ Section: public functions"""
257
+
258
+ def oeisIDfor_n(oeisID: str, n: int) -> int:
259
+ """
260
+ Calculate a(n) of a sequence from "The On-Line Encyclopedia of Integer Sequences" (OEIS).
261
+
262
+ Parameters:
263
+ oeisID: The ID of the OEIS sequence.
264
+ n: A non-negative integer for which to calculate the sequence value.
265
+
266
+ Returns:
267
+ sequenceValue: a(n) of the OEIS sequence.
268
+
269
+ Raises:
270
+ ValueError: If n is negative.
271
+ KeyError: If the OEIS sequence ID is not directly implemented.
272
+ """
273
+ oeisID = _validateOEISid(oeisID)
274
+
275
+ if not isinstance(n, int) or n < 0:
276
+ raise ValueError("`n` must be non-negative integer.")
277
+
278
+ listDimensions = settingsOEIS[oeisID]['getMapShape'](n)
279
+
280
+ if n <= 1 or len(listDimensions) < 2:
281
+ foldsTotal = settingsOEIS[oeisID]['valuesKnown'].get(n, None)
282
+ if foldsTotal is not None:
283
+ return foldsTotal
284
+ else:
285
+ raise ArithmeticError(f"OEIS sequence {oeisID} is not defined at n={n}.")
286
+
287
+ return countFolds(listDimensions)
288
+
289
+ def OEIS_for_n() -> None:
290
+ """Command-line interface for oeisIDfor_n."""
291
+ parserCLI = argparse.ArgumentParser(
292
+ description="Calculate a(n) for an OEIS sequence.",
293
+ epilog=_formatHelpText(),
294
+ formatter_class=argparse.RawDescriptionHelpFormatter
295
+ )
296
+ parserCLI.add_argument('oeisID', help="OEIS sequence identifier")
297
+ parserCLI.add_argument('n', type=int, help="Calculate a(n) for this n")
298
+
299
+ argumentsCLI = parserCLI.parse_args()
300
+
301
+ timeStart = time.perf_counter()
302
+
303
+ try:
304
+ print(oeisIDfor_n(argumentsCLI.oeisID, argumentsCLI.n), "distinct folding patterns.")
305
+ except (KeyError, ValueError, ArithmeticError) as ERRORmessage:
306
+ print(f"Error: {ERRORmessage}", file=sys.stderr)
307
+ sys.exit(1)
308
+
309
+ timeElapsed = time.perf_counter() - timeStart
310
+ print(f"Time elapsed: {timeElapsed:.3f} seconds")
311
+
312
+ def clearOEIScache() -> None:
313
+ """Delete all cached OEIS sequence files."""
314
+ if not _pathCache.exists():
315
+ print(f"Cache directory, {_pathCache}, not found - nothing to clear.")
316
+ return
317
+ else:
318
+ for oeisID in settingsOEIS:
319
+ pathFilenameCache = _pathCache / _getFilenameOEISbFile(oeisID)
320
+ pathFilenameCache.unlink(missing_ok=True)
321
+
322
+ print(f"Cache cleared from {_pathCache}")
323
+
324
+ def getOEISids() -> None:
325
+ """Print all available OEIS sequence IDs that are directly implemented."""
326
+ print(_formatHelpText())
327
+
328
+ if __name__ == "__main__":
329
+ getOEISids()
@@ -5,16 +5,16 @@ import pathlib
5
5
  import pickle
6
6
 
7
7
  @overload
8
- def makeStateJob(listDimensions: Sequence[int], writeJob: Literal[True] = True
8
+ def makeStateJob(listDimensions: Sequence[int], *, writeJob: Literal[True]
9
9
  , **keywordArguments: Optional[str]) -> pathlib.Path:
10
10
  ...
11
11
 
12
12
  @overload
13
- def makeStateJob(listDimensions: Sequence[int], writeJob: Literal[False] = False
13
+ def makeStateJob(listDimensions: Sequence[int], *, writeJob: Literal[False]
14
14
  , **keywordArguments: Optional[str]) -> computationState:
15
15
  ...
16
16
 
17
- def makeStateJob(listDimensions: Sequence[int], writeJob: bool = True, **keywordArguments: Optional[str]) -> computationState | pathlib.Path:
17
+ def makeStateJob(listDimensions: Sequence[int], *, writeJob: bool = True, **keywordArguments: Optional[str]) -> computationState | pathlib.Path:
18
18
  """
19
19
  Creates a computation state job for map folding calculations and optionally saves it to disk.
20
20
 
@@ -0,0 +1,29 @@
1
+ from mapFolding import getAlgorithmSource, relativePathSyntheticModules
2
+ from mapFolding import setDatatypeModule, setDatatypeFoldsTotal, setDatatypeElephino, setDatatypeLeavesTotal
3
+ from typing import Optional
4
+ import ast
5
+ import inspect
6
+ import pathlib
7
+ import sys
8
+
9
+ def transformPythonToJAX(codePython: str):
10
+ astPython = ast.parse(codePython)
11
+
12
+ def writeJax(*, codeSource: Optional[str] = None, pathFilenameAlgorithm: Optional[pathlib.Path] = None, pathFilenameDestination: Optional[pathlib.Path] = None) -> None:
13
+ if codeSource is None and pathFilenameAlgorithm is None:
14
+ algorithmSource = getAlgorithmSource()
15
+ codeSource = inspect.getsource(algorithmSource)
16
+ transformedText = transformPythonToJAX(codeSource)
17
+ pathFilenameAlgorithm = pathlib.Path(inspect.getfile(algorithmSource))
18
+ else:
19
+ raise NotImplementedError("You haven't written this part yet.")
20
+ if pathFilenameDestination is None:
21
+ pathFilenameDestination = pathFilenameAlgorithm.parent / relativePathSyntheticModules / "countJax.py"
22
+ # pathFilenameDestination.write_text(transformedText)
23
+
24
+ if __name__ == '__main__':
25
+ setDatatypeModule('jax.numpy', sourGrapes=True)
26
+ setDatatypeFoldsTotal('int64', sourGrapes=True)
27
+ setDatatypeElephino('uint8', sourGrapes=True)
28
+ setDatatypeLeavesTotal('uint8', sourGrapes=True)
29
+ writeJax()