mapFolding 0.3.4__tar.gz → 0.3.5__tar.gz

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 (35) hide show
  1. {mapfolding-0.3.4 → mapfolding-0.3.5}/PKG-INFO +1 -1
  2. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/mapFolding.egg-info/PKG-INFO +1 -1
  3. mapfolding-0.3.5/mapFolding/someAssemblyRequired/synthesizeModules.py +244 -0
  4. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/syntheticModules/countInitialize.py +3 -3
  5. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/syntheticModules/countParallel.py +2 -2
  6. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/syntheticModules/countSequential.py +10 -10
  7. {mapfolding-0.3.4 → mapfolding-0.3.5}/pyproject.toml +1 -1
  8. mapfolding-0.3.4/mapFolding/someAssemblyRequired/synthesizeModules.py +0 -170
  9. {mapfolding-0.3.4 → mapfolding-0.3.5}/README.md +0 -0
  10. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/benchmarks/benchmarking.py +0 -0
  11. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/citations/updateCitation.py +0 -0
  12. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/mapFolding.egg-info/SOURCES.txt +0 -0
  13. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/mapFolding.egg-info/dependency_links.txt +0 -0
  14. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/mapFolding.egg-info/entry_points.txt +0 -0
  15. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/mapFolding.egg-info/requires.txt +0 -0
  16. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/mapFolding.egg-info/top_level.txt +0 -0
  17. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/reference/flattened.py +0 -0
  18. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/reference/hunterNumba.py +0 -0
  19. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/reference/irvineJavaPort.py +0 -0
  20. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/reference/jax.py +0 -0
  21. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/reference/lunnan.py +0 -0
  22. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/reference/lunnanNumpy.py +0 -0
  23. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/reference/lunnanWhile.py +0 -0
  24. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/reference/rotatedEntryPoint.py +0 -0
  25. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/reference/total_countPlus1vsPlusN.py +0 -0
  26. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/someAssemblyRequired/__init__.py +0 -0
  27. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/someAssemblyRequired/generalizeSourceCode.py +0 -0
  28. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/someAssemblyRequired/getLLVMforNoReason.py +0 -0
  29. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/someAssemblyRequired/makeJob.py +0 -0
  30. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/someAssemblyRequired/synthesizeModuleJob.py +0 -0
  31. {mapfolding-0.3.4 → mapfolding-0.3.5}/mapFolding/syntheticModules/__init__.py +0 -0
  32. {mapfolding-0.3.4 → mapfolding-0.3.5}/setup.cfg +0 -0
  33. {mapfolding-0.3.4 → mapfolding-0.3.5}/tests/test_oeis.py +0 -0
  34. {mapfolding-0.3.4 → mapfolding-0.3.5}/tests/test_other.py +0 -0
  35. {mapfolding-0.3.4 → mapfolding-0.3.5}/tests/test_tasks.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: mapFolding
3
- Version: 0.3.4
3
+ Version: 0.3.5
4
4
  Summary: Count distinct ways to fold a map (or a strip of stamps)
5
5
  Author-email: Hunter Hogan <HunterHogan@pm.me>
6
6
  License: CC-BY-NC-4.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: mapFolding
3
- Version: 0.3.4
3
+ Version: 0.3.5
4
4
  Summary: Count distinct ways to fold a map (or a strip of stamps)
5
5
  Author-email: Hunter Hogan <HunterHogan@pm.me>
6
6
  License: CC-BY-NC-4.0
@@ -0,0 +1,244 @@
1
+ from mapFolding import indexMy, indexTrack, getAlgorithmSource, ParametersNumba, parametersNumbaDEFAULT, hackSSOTdtype
2
+ from mapFolding import datatypeLargeDEFAULT, datatypeMediumDEFAULT, datatypeSmallDEFAULT, EnumIndices
3
+ import pathlib
4
+ import inspect
5
+ import numpy
6
+ import numba
7
+ from typing import Dict, Optional, List, Union, Sequence, Type, cast
8
+ import ast
9
+
10
+ algorithmSource = getAlgorithmSource()
11
+
12
+ class RecursiveInliner(ast.NodeTransformer):
13
+ def __init__(self, dictionaryFunctions: Dict[str, ast.FunctionDef]):
14
+ self.dictionaryFunctions = dictionaryFunctions
15
+ self.processed = set()
16
+
17
+ def inlineFunctionBody(self, functionName: str) -> Optional[ast.FunctionDef]:
18
+ if (functionName in self.processed):
19
+ return None
20
+
21
+ self.processed.add(functionName)
22
+ inlineDefinition = self.dictionaryFunctions[functionName]
23
+ # Recursively process the function body
24
+ for node in ast.walk(inlineDefinition):
25
+ self.visit(node)
26
+ return inlineDefinition
27
+
28
+ def visit_Call(self, node: ast.Call) -> ast.AST:
29
+ callNode = self.generic_visit(node)
30
+ if (isinstance(callNode, ast.Call) and isinstance(callNode.func, ast.Name) and callNode.func.id in self.dictionaryFunctions):
31
+ inlineDefinition = self.inlineFunctionBody(callNode.func.id)
32
+ if (inlineDefinition and inlineDefinition.body):
33
+ lastStmt = inlineDefinition.body[-1]
34
+ if (isinstance(lastStmt, ast.Return) and lastStmt.value is not None):
35
+ return self.visit(lastStmt.value)
36
+ elif (isinstance(lastStmt, ast.Expr) and lastStmt.value is not None):
37
+ return self.visit(lastStmt.value)
38
+ return ast.Constant(value=None)
39
+ return callNode
40
+
41
+ def visit_Expr(self, node: ast.Expr) -> Union[ast.AST, List[ast.AST]]:
42
+ if (isinstance(node.value, ast.Call)):
43
+ if (isinstance(node.value.func, ast.Name) and node.value.func.id in self.dictionaryFunctions):
44
+ inlineDefinition = self.inlineFunctionBody(node.value.func.id)
45
+ if (inlineDefinition):
46
+ return [self.visit(stmt) for stmt in inlineDefinition.body]
47
+ return self.generic_visit(node)
48
+
49
+ def decorateCallableWithNumba(astCallable: ast.FunctionDef, parallel: bool=False, **keywordArguments: Optional[str]) -> ast.FunctionDef:
50
+ def makeNumbaParameterSignatureElement(signatureElement: ast.arg):
51
+ if isinstance(signatureElement.annotation, ast.Subscript) and isinstance(signatureElement.annotation.slice, ast.Tuple):
52
+ annotationShape = signatureElement.annotation.slice.elts[0]
53
+ if isinstance(annotationShape, ast.Subscript) and isinstance(annotationShape.slice, ast.Tuple):
54
+ shapeAsListSlices: Sequence[ast.expr] = [ast.Slice() for axis in range(len(annotationShape.slice.elts))]
55
+ shapeAsListSlices[-1] = ast.Slice(step=ast.Constant(value=1))
56
+ shapeAST = ast.Tuple(elts=list(shapeAsListSlices), ctx=ast.Load())
57
+ else:
58
+ shapeAST = ast.Slice(step=ast.Constant(value=1))
59
+
60
+ annotationDtype = signatureElement.annotation.slice.elts[1]
61
+ if (isinstance(annotationDtype, ast.Subscript) and isinstance(annotationDtype.slice, ast.Attribute)):
62
+ datatypeAST = annotationDtype.slice.attr
63
+ else:
64
+ datatypeAST = None
65
+
66
+ ndarrayName = signatureElement.arg
67
+ Z0Z_hackyStr = hackSSOTdtype[ndarrayName]
68
+ Z0Z_hackyStr = Z0Z_hackyStr[0] + 'ata' + Z0Z_hackyStr[1:]
69
+ datatype_attr = keywordArguments.get(Z0Z_hackyStr, None) or datatypeAST or eval(Z0Z_hackyStr+'DEFAULT')
70
+
71
+ datatypeNumba = ast.Attribute(value=ast.Name(id='numba', ctx=ast.Load()), attr=datatype_attr, ctx=ast.Load())
72
+
73
+ return ast.Subscript(value=datatypeNumba, slice=shapeAST, ctx=ast.Load())
74
+
75
+ # callableSourceDecorators = [decorator for decorator in callableInlined.decorator_list]
76
+
77
+ listNumbaParameterSignature: Sequence[ast.expr] = []
78
+ for parameter in astCallable.args.args:
79
+ signatureElement = makeNumbaParameterSignatureElement(parameter)
80
+ if (signatureElement):
81
+ listNumbaParameterSignature.append(signatureElement)
82
+
83
+ astArgsNumbaSignature = ast.Tuple(elts=listNumbaParameterSignature, ctx=ast.Load())
84
+
85
+ parametersNumba = parametersNumbaDEFAULT if not parallel else ParametersNumba({**parametersNumbaDEFAULT, 'parallel': True})
86
+ listKeywordsNumbaSignature = [ast.keyword(arg=parameterName, value=ast.Constant(value=parameterValue)) for parameterName, parameterValue in parametersNumba.items()]
87
+
88
+ astDecoratorNumba = ast.Call(func=ast.Attribute(value=ast.Name(id='numba', ctx=ast.Load()), attr='jit', ctx=ast.Load()), args=[astArgsNumbaSignature], keywords=listKeywordsNumbaSignature)
89
+
90
+ astCallable.decorator_list = [astDecoratorNumba]
91
+ return astCallable
92
+
93
+ class UnpackArrayAccesses(ast.NodeTransformer):
94
+ """AST transformer that replaces array accesses with simpler variables."""
95
+
96
+ def __init__(self, enumIndexClass: Type[EnumIndices], arrayName: str):
97
+ self.enumIndexClass = enumIndexClass
98
+ self.arrayName = arrayName
99
+ self.substitutions = {}
100
+
101
+ def extract_member_name(self, node: ast.AST) -> Optional[str]:
102
+ """Recursively extract enum member name from any node in the AST."""
103
+ if isinstance(node, ast.Attribute) and node.attr == 'value':
104
+ innerAttribute = node.value
105
+ while isinstance(innerAttribute, ast.Attribute):
106
+ if (isinstance(innerAttribute.value, ast.Name) and innerAttribute.value.id == self.enumIndexClass.__name__):
107
+ return innerAttribute.attr
108
+ innerAttribute = innerAttribute.value
109
+ return None
110
+
111
+ def transform_slice_element(self, node: ast.AST) -> ast.AST:
112
+ """Transform any enum references within a slice element."""
113
+ if isinstance(node, ast.Subscript):
114
+ if isinstance(node.slice, ast.Attribute):
115
+ member_name = self.extract_member_name(node.slice)
116
+ if member_name:
117
+ return ast.Name(id=member_name, ctx=node.ctx)
118
+ elif isinstance(node, ast.Tuple):
119
+ # Handle tuple slices by transforming each element
120
+ return ast.Tuple(
121
+ elts=cast(List[ast.expr], [self.transform_slice_element(elt) for elt in node.elts]),
122
+ ctx=node.ctx
123
+ )
124
+ elif isinstance(node, ast.Attribute):
125
+ member_name = self.extract_member_name(node)
126
+ if member_name:
127
+ return ast.Name(id=member_name, ctx=ast.Load())
128
+ return node
129
+
130
+ def visit_Subscript(self, node: ast.Subscript) -> ast.AST:
131
+ # Recursively visit any nested subscripts in value or slice
132
+ node.value = self.visit(node.value)
133
+ node.slice = self.visit(node.slice)
134
+
135
+ # If node.value is not our arrayName, just return node
136
+ if not (isinstance(node.value, ast.Name) and node.value.id == self.arrayName):
137
+ return node
138
+
139
+ # Handle scalar array access
140
+ if isinstance(node.slice, ast.Attribute):
141
+ memberName = self.extract_member_name(node.slice)
142
+ if memberName:
143
+ self.substitutions[memberName] = ('scalar', node)
144
+ return ast.Name(id=memberName, ctx=ast.Load())
145
+
146
+ # Handle array slice access
147
+ if isinstance(node.slice, ast.Tuple) and node.slice.elts:
148
+ firstElement = node.slice.elts[0]
149
+ memberName = self.extract_member_name(firstElement)
150
+ sliceRemainder = [self.visit(elem) for elem in node.slice.elts[1:]]
151
+ if memberName:
152
+ self.substitutions[memberName] = ('array', node)
153
+ if len(sliceRemainder) == 0:
154
+ return ast.Name(id=memberName, ctx=ast.Load())
155
+ return ast.Subscript(
156
+ value=ast.Name(id=memberName, ctx=ast.Load()),
157
+ slice=ast.Tuple(elts=sliceRemainder, ctx=ast.Load()) if len(sliceRemainder) > 1 else sliceRemainder[0],
158
+ ctx=ast.Load()
159
+ )
160
+
161
+ # If single-element tuple, unwrap
162
+ if isinstance(node.slice, ast.Tuple) and len(node.slice.elts) == 1:
163
+ node.slice = node.slice.elts[0]
164
+
165
+ return node
166
+
167
+ def visit_FunctionDef(self, node: ast.FunctionDef) -> ast.FunctionDef:
168
+ node = cast(ast.FunctionDef, self.generic_visit(node))
169
+
170
+ initializations = []
171
+ for name, (kind, original_node) in self.substitutions.items():
172
+ if kind == 'scalar':
173
+ initializations.append(
174
+ ast.Assign(
175
+ targets=[ast.Name(id=name, ctx=ast.Store())],
176
+ value=original_node
177
+ )
178
+ )
179
+ else: # array
180
+ initializations.append(
181
+ ast.Assign(
182
+ targets=[ast.Name(id=name, ctx=ast.Store())],
183
+ value=ast.Subscript(
184
+ value=ast.Name(id=self.arrayName, ctx=ast.Load()),
185
+ slice=ast.Attribute(
186
+ value=ast.Attribute(
187
+ value=ast.Name(id=self.enumIndexClass.__name__, ctx=ast.Load()),
188
+ attr=name,
189
+ ctx=ast.Load()
190
+ ),
191
+ attr='value',
192
+ ctx=ast.Load()
193
+ ),
194
+ ctx=ast.Load()
195
+ )
196
+ )
197
+ )
198
+
199
+ node.body = initializations + node.body
200
+ return node
201
+
202
+ def getDictionaryEnumValues() -> Dict[str, int]:
203
+ dictionaryEnumValues = {}
204
+ for enumIndex in [indexMy, indexTrack]:
205
+ for memberName, memberValue in enumIndex._member_map_.items():
206
+ dictionaryEnumValues[f"{enumIndex.__name__}.{memberName}.value"] = memberValue.value
207
+ return dictionaryEnumValues
208
+
209
+ def inlineMapFoldingNumba(**keywordArguments: Optional[str]):
210
+ dictionaryEnumValues = getDictionaryEnumValues()
211
+ codeSource = inspect.getsource(algorithmSource)
212
+ pathFilenameAlgorithm = pathlib.Path(inspect.getfile(algorithmSource))
213
+
214
+ listPathFilenamesDestination: list[pathlib.Path] = []
215
+ listCallables = [ 'countInitialize', 'countParallel', 'countSequential', ]
216
+ for callableTarget in listCallables:
217
+ codeParsed: ast.Module = ast.parse(codeSource, type_comments=True)
218
+ codeSourceImportStatements = {statement for statement in codeParsed.body if isinstance(statement, (ast.Import, ast.ImportFrom))}
219
+ dictionaryFunctions = {statement.name: statement for statement in codeParsed.body if isinstance(statement, ast.FunctionDef)}
220
+ callableInlinerWorkhorse = RecursiveInliner(dictionaryFunctions)
221
+ parallel = callableTarget == 'countParallel'
222
+ callableInlined = callableInlinerWorkhorse.inlineFunctionBody(callableTarget)
223
+ if callableInlined:
224
+ ast.fix_missing_locations(callableInlined)
225
+ callableDecorated = decorateCallableWithNumba(callableInlined, parallel, **keywordArguments)
226
+
227
+ if callableTarget == 'countSequential':
228
+ myUnpacker = UnpackArrayAccesses(indexMy, 'my')
229
+ callableDecorated = cast(ast.FunctionDef, myUnpacker.visit(callableDecorated))
230
+ ast.fix_missing_locations(callableDecorated)
231
+
232
+ trackUnpacker = UnpackArrayAccesses(indexTrack, 'track')
233
+ callableDecorated = cast(ast.FunctionDef, trackUnpacker.visit(callableDecorated))
234
+ ast.fix_missing_locations(callableDecorated)
235
+
236
+ callableInlined = ast.unparse(callableDecorated)
237
+ importsRequired = "\n".join([ast.unparse(importStatement) for importStatement in codeSourceImportStatements])
238
+
239
+ pathFilenameDestination = pathFilenameAlgorithm.parent / "syntheticModules" / pathFilenameAlgorithm.with_stem(callableTarget).name
240
+ pathFilenameDestination.write_text(importsRequired + "\n" + callableInlined)
241
+ listPathFilenamesDestination.append(pathFilenameDestination)
242
+
243
+ if __name__ == '__main__':
244
+ inlineMapFoldingNumba()
@@ -1,8 +1,8 @@
1
- from numpy import integer
2
- import numba
3
1
  import numpy
4
- from mapFolding import indexMy, indexTrack
5
2
  from typing import Any, Tuple
3
+ from mapFolding import indexMy, indexTrack
4
+ import numba
5
+ from numpy import integer
6
6
  @numba.jit((numba.uint8[:, :, ::1], numba.uint8[::1], numba.uint8[::1], numba.uint8[:, ::1]), _nrt=True, boundscheck=False, cache=True, error_model='numpy', fastmath=True, forceinline=False, inline='never', looplift=False, no_cfunc_wrapper=True, no_cpython_wrapper=True, nopython=True, parallel=False)
7
7
  def countInitialize(connectionGraph: numpy.ndarray[Tuple[int, int, int], numpy.dtype[integer[Any]]], gapsWhere: numpy.ndarray[Tuple[int], numpy.dtype[integer[Any]]], my: numpy.ndarray[Tuple[int], numpy.dtype[integer[Any]]], track: numpy.ndarray[Tuple[int, int], numpy.dtype[integer[Any]]]):
8
8
  while my[indexMy.leaf1ndex.value] > 0:
@@ -6,15 +6,15 @@ import numpy
6
6
  @numba.jit((numba.uint8[:, :, ::1], numba.int64[::1], numba.uint8[::1], numba.uint8[::1], numba.uint8[:, ::1]), _nrt=True, boundscheck=False, cache=True, error_model='numpy', fastmath=True, forceinline=False, inline='never', looplift=False, no_cfunc_wrapper=True, no_cpython_wrapper=True, nopython=True, parallel=True)
7
7
  def countParallel(connectionGraph: numpy.ndarray[Tuple[int, int, int], numpy.dtype[integer[Any]]], foldGroups: numpy.ndarray[Tuple[int], numpy.dtype[integer[Any]]], gapsWherePARALLEL: numpy.ndarray[Tuple[int], numpy.dtype[integer[Any]]], myPARALLEL: numpy.ndarray[Tuple[int], numpy.dtype[integer[Any]]], trackPARALLEL: numpy.ndarray[Tuple[int, int], numpy.dtype[integer[Any]]]):
8
8
  for indexSherpa in numba.prange(myPARALLEL[indexMy.taskDivisions.value]):
9
+ groupsOfFolds: int = 0
9
10
  gapsWhere = gapsWherePARALLEL.copy()
10
11
  my = myPARALLEL.copy()
11
12
  my[indexMy.taskIndex.value] = indexSherpa
12
13
  track = trackPARALLEL.copy()
13
- groupsOfFolds: int = 0
14
14
  while my[indexMy.leaf1ndex.value] > 0:
15
15
  if my[indexMy.leaf1ndex.value] <= 1 or track[indexTrack.leafBelow.value, 0] == 1:
16
16
  if my[indexMy.leaf1ndex.value] > foldGroups[-1]:
17
- groupsOfFolds = groupsOfFolds + 1
17
+ groupsOfFolds += 1
18
18
  else:
19
19
  my[indexMy.dimensionsUnconstrained.value] = my[indexMy.dimensionsTotal.value]
20
20
  my[indexMy.gap1ndexCeiling.value] = track[indexTrack.gapRangeStart.value, my[indexMy.leaf1ndex.value] - 1]
@@ -5,24 +5,24 @@ from mapFolding import indexMy, indexTrack
5
5
  import numpy
6
6
  @numba.jit((numba.uint8[:, :, ::1], numba.int64[::1], numba.uint8[::1], numba.uint8[::1], numba.uint8[:, ::1]), _nrt=True, boundscheck=False, cache=True, error_model='numpy', fastmath=True, forceinline=False, inline='never', looplift=False, no_cfunc_wrapper=True, no_cpython_wrapper=True, nopython=True, parallel=False)
7
7
  def countSequential(connectionGraph: numpy.ndarray[Tuple[int, int, int], numpy.dtype[integer[Any]]], foldGroups: numpy.ndarray[Tuple[int], numpy.dtype[integer[Any]]], gapsWhere: numpy.ndarray[Tuple[int], numpy.dtype[integer[Any]]], my: numpy.ndarray[Tuple[int], numpy.dtype[integer[Any]]], track: numpy.ndarray[Tuple[int, int], numpy.dtype[integer[Any]]]):
8
- doFindGaps = True
9
- dimensionsTotal = my[indexMy.dimensionsTotal.value]
8
+ leafBelow = track[indexTrack.leafBelow.value]
9
+ gapRangeStart = track[indexTrack.gapRangeStart.value]
10
+ countDimensionsGapped = track[indexTrack.countDimensionsGapped.value]
11
+ leafAbove = track[indexTrack.leafAbove.value]
12
+ leaf1ndex = my[indexMy.leaf1ndex.value]
10
13
  dimensionsUnconstrained = my[indexMy.dimensionsUnconstrained.value]
11
- gap1ndex = my[indexMy.gap1ndex.value]
14
+ dimensionsTotal = my[indexMy.dimensionsTotal.value]
12
15
  gap1ndexCeiling = my[indexMy.gap1ndexCeiling.value]
13
16
  indexDimension = my[indexMy.indexDimension.value]
14
- indexMiniGap = my[indexMy.indexMiniGap.value]
15
- leaf1ndex = my[indexMy.leaf1ndex.value]
16
17
  leafConnectee = my[indexMy.leafConnectee.value]
18
+ indexMiniGap = my[indexMy.indexMiniGap.value]
19
+ gap1ndex = my[indexMy.gap1ndex.value]
17
20
  taskIndex = my[indexMy.taskIndex.value]
18
- leafAbove = track[indexTrack.leafAbove.value]
19
- leafBelow = track[indexTrack.leafBelow.value]
20
- countDimensionsGapped = track[indexTrack.countDimensionsGapped.value]
21
- gapRangeStart = track[indexTrack.gapRangeStart.value]
22
21
  groupsOfFolds: int = 0
22
+ doFindGaps = True
23
23
  while leaf1ndex > 0:
24
24
  if (doFindGaps := (leaf1ndex <= 1 or leafBelow[0] == 1)) and leaf1ndex > foldGroups[-1]:
25
- groupsOfFolds = groupsOfFolds + 1
25
+ groupsOfFolds += 1
26
26
  elif doFindGaps:
27
27
  dimensionsUnconstrained = dimensionsTotal
28
28
  gap1ndexCeiling = gapRangeStart[leaf1ndex - 1]
@@ -48,7 +48,7 @@ readme = { file = "README.md", content-type = "text/markdown" }
48
48
  requires-python = ">=3.10,<3.14"
49
49
  scripts = { getOEISids = "mapFolding.oeis:getOEISids", clearOEIScache = "mapFolding.oeis:clearOEIScache", OEIS_for_n = "mapFolding.oeis:OEIS_for_n" }
50
50
  urls = { Homepage = "https://github.com/hunterhogan/mapFolding", Donate = "https://www.patreon.com/integrated" }
51
- version = "0.3.4"
51
+ version = "0.3.5"
52
52
 
53
53
  [tool.coverage]
54
54
  report = { exclude_lines = [
@@ -1,170 +0,0 @@
1
- from mapFolding import indexMy, indexTrack, getAlgorithmSource, ParametersNumba, parametersNumbaDEFAULT, hackSSOTdtype
2
- from mapFolding import datatypeLargeDEFAULT, datatypeMediumDEFAULT, datatypeSmallDEFAULT
3
- import pathlib
4
- import inspect
5
- import numpy
6
- import numba
7
- from typing import Dict, Optional, List, Set, Union, Sequence
8
- import ast
9
-
10
- algorithmSource = getAlgorithmSource()
11
-
12
- class RecursiveInliner(ast.NodeTransformer):
13
- def __init__(self, dictionaryFunctions: Dict[str, ast.FunctionDef]):
14
- self.dictionaryFunctions = dictionaryFunctions
15
- self.processed = set()
16
-
17
- def inlineFunctionBody(self, functionName: str) -> Optional[ast.FunctionDef]:
18
- if functionName in self.processed:
19
- return None
20
-
21
- self.processed.add(functionName)
22
- inlineDefinition = self.dictionaryFunctions[functionName]
23
- # Recursively process the function body
24
- for node in ast.walk(inlineDefinition):
25
- self.visit(node)
26
- return inlineDefinition
27
-
28
- def visit_Call(self, node: ast.Call) -> ast.AST:
29
- callNode = self.generic_visit(node)
30
- if isinstance(callNode, ast.Call) and isinstance(callNode.func, ast.Name) and callNode.func.id in self.dictionaryFunctions:
31
- inlineDefinition = self.inlineFunctionBody(callNode.func.id)
32
- if (inlineDefinition and inlineDefinition.body):
33
- lastStmt = inlineDefinition.body[-1]
34
- if isinstance(lastStmt, ast.Return) and lastStmt.value is not None:
35
- return self.visit(lastStmt.value)
36
- elif isinstance(lastStmt, ast.Expr) and lastStmt.value is not None:
37
- return self.visit(lastStmt.value)
38
- return ast.Constant(value=None)
39
- return callNode
40
-
41
- def visit_Expr(self, node: ast.Expr) -> Union[ast.AST, List[ast.AST]]:
42
- if isinstance(node.value, ast.Call):
43
- if isinstance(node.value.func, ast.Name) and node.value.func.id in self.dictionaryFunctions:
44
- inlineDefinition = self.inlineFunctionBody(node.value.func.id)
45
- if inlineDefinition:
46
- return [self.visit(stmt) for stmt in inlineDefinition.body]
47
- return self.generic_visit(node)
48
-
49
- def decorateCallableWithNumba(astCallable: ast.FunctionDef, parallel: bool=False, **keywordArguments: Optional[str]):
50
- def makeNumbaParameterSignatureElement(signatureElement: ast.arg):
51
- if isinstance(signatureElement.annotation, ast.Subscript) and isinstance(signatureElement.annotation.slice, ast.Tuple):
52
-
53
- annotationShape = signatureElement.annotation.slice.elts[0]
54
- if isinstance(annotationShape, ast.Subscript) and isinstance(annotationShape.slice, ast.Tuple):
55
- shapeAsListSlices = [ast.Slice() for axis in range(len(annotationShape.slice.elts))]
56
- shapeAsListSlices[-1] = ast.Slice(step=ast.Constant(value=1))
57
- shapeAST = ast.Tuple(elts=shapeAsListSlices, ctx=ast.Load())
58
- else:
59
- shapeAST = ast.Slice(step=ast.Constant(value=1))
60
-
61
- annotationDtype = signatureElement.annotation.slice.elts[1]
62
- if isinstance(annotationDtype, ast.Subscript) and isinstance(annotationDtype.slice, ast.Attribute):
63
- datatypeAST = annotationDtype.slice.attr
64
- else:
65
- datatypeAST = None
66
-
67
- ndarrayName = signatureElement.arg
68
- Z0Z_hackyStr = hackSSOTdtype[ndarrayName]
69
- Z0Z_hackyStr = Z0Z_hackyStr[0] + 'ata' + Z0Z_hackyStr[1:]
70
- datatype_attr = keywordArguments.get(Z0Z_hackyStr, None) or datatypeAST or eval(Z0Z_hackyStr+'DEFAULT')
71
-
72
- datatypeNumba = ast.Attribute(value=ast.Name(id='numba', ctx=ast.Load()), attr=datatype_attr, ctx=ast.Load())
73
-
74
- return ast.Subscript(value=datatypeNumba, slice=shapeAST, ctx=ast.Load())
75
-
76
- # callableSourceDecorators = [decorator for decorator in callableInlined.decorator_list]
77
-
78
- listNumbaParameterSignature: List[ast.Subscript] = []
79
- for parameter in astCallable.args.args:
80
- signatureElement = makeNumbaParameterSignatureElement(parameter)
81
- if signatureElement:
82
- listNumbaParameterSignature.append(signatureElement)
83
-
84
- astArgsNumbaSignature = ast.Tuple(elts=listNumbaParameterSignature, ctx=ast.Load())
85
-
86
- parametersNumba = parametersNumbaDEFAULT if not parallel else ParametersNumba({**parametersNumbaDEFAULT, 'parallel': True})
87
- listKeywordsNumbaSignature = [ast.keyword(arg=parameterName, value=ast.Constant(value=parameterValue)) for parameterName, parameterValue in parametersNumba.items()]
88
-
89
- astDecoratorNumba = ast.Call(func=ast.Attribute(value=ast.Name(id='numba', ctx=ast.Load()), attr='jit', ctx=ast.Load()), args=[astArgsNumbaSignature], keywords=listKeywordsNumbaSignature)
90
-
91
- astCallable.decorator_list = [astDecoratorNumba]
92
- return astCallable
93
-
94
- def getDictionaryEnumValues() -> Dict[str, int]:
95
- dictionaryEnumValues = {}
96
- for enumIndex in [indexMy, indexTrack]:
97
- for memberName, memberValue in enumIndex._member_map_.items():
98
- dictionaryEnumValues[f"{enumIndex.__name__}.{memberName}.value"] = memberValue.value
99
- return dictionaryEnumValues
100
-
101
- def unpackArrays(codeInlined: str) -> str:
102
- dictionaryReplaceScalars = {
103
- 'my[indexMy.dimensionsTotal.value]': 'dimensionsTotal',
104
- 'my[indexMy.dimensionsUnconstrained.value]': 'dimensionsUnconstrained',
105
- 'my[indexMy.gap1ndex.value]': 'gap1ndex',
106
- 'my[indexMy.gap1ndexCeiling.value]': 'gap1ndexCeiling',
107
- 'my[indexMy.indexDimension.value]': 'indexDimension',
108
- # 'my[indexMy.indexLeaf.value]': 'indexLeaf',
109
- 'my[indexMy.indexMiniGap.value]': 'indexMiniGap',
110
- 'my[indexMy.leaf1ndex.value]': 'leaf1ndex',
111
- 'my[indexMy.leafConnectee.value]': 'leafConnectee',
112
- # 'my[indexMy.taskDivisions.value]': 'taskDivisions',
113
- 'my[indexMy.taskIndex.value]': 'taskIndex',
114
- # 'foldGroups[-1]': 'leavesTotal',
115
- }
116
-
117
- dictionaryReplaceArrays = {
118
- "track[indexTrack.leafAbove.value, ": 'leafAbove[',
119
- "track[indexTrack.leafBelow.value, ": 'leafBelow[',
120
- 'track[indexTrack.countDimensionsGapped.value, ': 'countDimensionsGapped[',
121
- 'track[indexTrack.gapRangeStart.value, ': 'gapRangeStart[',
122
- }
123
-
124
- ImaIndent = " "
125
- linesInitialize = """"""
126
-
127
- for find, replace in dictionaryReplaceScalars.items():
128
- linesInitialize += f"{ImaIndent}{replace} = {find}\n"
129
- codeInlined = codeInlined.replace(find, replace)
130
-
131
- for find, replace in dictionaryReplaceArrays.items():
132
- linesInitialize += f"{ImaIndent}{replace[0:-1]} = {find[0:-2]}]\n"
133
- codeInlined = codeInlined.replace(find, replace)
134
-
135
- ourGuyOnTheInside = " doFindGaps = True\n"
136
- linesInitialize = ourGuyOnTheInside + linesInitialize
137
-
138
- codeInlined = codeInlined.replace(ourGuyOnTheInside, linesInitialize)
139
-
140
- return codeInlined
141
-
142
- def inlineMapFoldingNumba(**keywordArguments: Optional[str]):
143
- dictionaryEnumValues = getDictionaryEnumValues()
144
- codeSource = inspect.getsource(algorithmSource)
145
- pathFilenameAlgorithm = pathlib.Path(inspect.getfile(algorithmSource))
146
-
147
- listPathFilenamesDestination: list[pathlib.Path] = []
148
- listCallables = [ 'countInitialize', 'countParallel', 'countSequential', ]
149
- for callableTarget in listCallables:
150
- codeParsed: ast.Module = ast.parse(codeSource, type_comments=True)
151
- codeSourceImportStatements = {statement for statement in codeParsed.body if isinstance(statement, (ast.Import, ast.ImportFrom))}
152
- dictionaryFunctions = {statement.name: statement for statement in codeParsed.body if isinstance(statement, ast.FunctionDef)}
153
- callableInlinerWorkhorse = RecursiveInliner(dictionaryFunctions)
154
- parallel = callableTarget == 'countParallel'
155
- callableInlined = callableInlinerWorkhorse.inlineFunctionBody(callableTarget)
156
- if callableInlined:
157
- ast.fix_missing_locations(callableInlined)
158
- callableDecorated = decorateCallableWithNumba(callableInlined, parallel, **keywordArguments)
159
-
160
- importsRequired = "\n".join([ast.unparse(importStatement) for importStatement in codeSourceImportStatements])
161
- callableInlined = ast.unparse(callableDecorated)
162
- codeUnpacked = unpackArrays(callableInlined) if callableTarget == 'countSequential' else callableInlined
163
- # inlinedCode = ast.unparse(ast.Module(body=[nodeInlined], type_ignores=[]))
164
-
165
- pathFilenameDestination = pathFilenameAlgorithm.parent / "syntheticModules" / pathFilenameAlgorithm.with_stem(callableTarget).name
166
- pathFilenameDestination.write_text(importsRequired + "\n" + codeUnpacked)
167
- listPathFilenamesDestination.append(pathFilenameDestination)
168
-
169
- if __name__ == '__main__':
170
- inlineMapFoldingNumba()
File without changes
File without changes