mapFolding 0.3.7__py3-none-any.whl → 0.3.8__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.
@@ -1,216 +0,0 @@
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
- if astCallable.name == 'countInitialize':
86
- parametersNumba = {}
87
- else:
88
- parametersNumba = parametersNumbaDEFAULT if not parallel else ParametersNumba({**parametersNumbaDEFAULT, 'parallel': True})
89
- listKeywordsNumbaSignature = [ast.keyword(arg=parameterName, value=ast.Constant(value=parameterValue)) for parameterName, parameterValue in parametersNumba.items()]
90
-
91
- astDecoratorNumba = ast.Call(func=ast.Attribute(value=ast.Name(id='numba', ctx=ast.Load()), attr='jit', ctx=ast.Load()), args=[astArgsNumbaSignature], keywords=listKeywordsNumbaSignature)
92
-
93
- astCallable.decorator_list = [astDecoratorNumba]
94
- return astCallable
95
-
96
- class UnpackArrayAccesses(ast.NodeTransformer):
97
- """AST transformer that replaces array accesses with simpler variables."""
98
-
99
- def __init__(self, enumIndexClass: Type[EnumIndices], arrayName: str):
100
- self.enumIndexClass = enumIndexClass
101
- self.arrayName = arrayName
102
- self.substitutions = {}
103
-
104
- def extract_member_name(self, node: ast.AST) -> Optional[str]:
105
- """Recursively extract enum member name from any node in the AST."""
106
- if isinstance(node, ast.Attribute) and node.attr == 'value':
107
- innerAttribute = node.value
108
- while isinstance(innerAttribute, ast.Attribute):
109
- if (isinstance(innerAttribute.value, ast.Name) and innerAttribute.value.id == self.enumIndexClass.__name__):
110
- return innerAttribute.attr
111
- innerAttribute = innerAttribute.value
112
- return None
113
-
114
- def transform_slice_element(self, node: ast.AST) -> ast.AST:
115
- """Transform any enum references within a slice element."""
116
- if isinstance(node, ast.Subscript):
117
- if isinstance(node.slice, ast.Attribute):
118
- member_name = self.extract_member_name(node.slice)
119
- if member_name:
120
- return ast.Name(id=member_name, ctx=node.ctx)
121
- elif isinstance(node, ast.Tuple):
122
- # Handle tuple slices by transforming each element
123
- return ast.Tuple(elts=cast(List[ast.expr], [self.transform_slice_element(elt) for elt in node.elts]), ctx=node.ctx)
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
- # If node.value is not our arrayName, just return node
135
- if not (isinstance(node.value, ast.Name) and node.value.id == self.arrayName):
136
- return node
137
-
138
- # Handle scalar array access
139
- if isinstance(node.slice, ast.Attribute):
140
- memberName = self.extract_member_name(node.slice)
141
- if memberName:
142
- self.substitutions[memberName] = ('scalar', node)
143
- return ast.Name(id=memberName, ctx=ast.Load())
144
-
145
- # Handle array slice access
146
- if isinstance(node.slice, ast.Tuple) and node.slice.elts:
147
- firstElement = node.slice.elts[0]
148
- memberName = self.extract_member_name(firstElement)
149
- sliceRemainder = [self.visit(elem) for elem in node.slice.elts[1:]]
150
- if memberName:
151
- self.substitutions[memberName] = ('array', node)
152
- if len(sliceRemainder) == 0:
153
- return ast.Name(id=memberName, ctx=ast.Load())
154
- return ast.Subscript(value=ast.Name(id=memberName, ctx=ast.Load()), slice=ast.Tuple(elts=sliceRemainder, ctx=ast.Load()) if len(sliceRemainder) > 1 else sliceRemainder[0], ctx=ast.Load())
155
-
156
- # If single-element tuple, unwrap
157
- if isinstance(node.slice, ast.Tuple) and len(node.slice.elts) == 1:
158
- node.slice = node.slice.elts[0]
159
-
160
- return node
161
-
162
- def visit_FunctionDef(self, node: ast.FunctionDef) -> ast.FunctionDef:
163
- node = cast(ast.FunctionDef, self.generic_visit(node))
164
-
165
- initializations = []
166
- for name, (kind, original_node) in self.substitutions.items():
167
- if kind == 'scalar':
168
- initializations.append(ast.Assign(targets=[ast.Name(id=name, ctx=ast.Store())], value=original_node))
169
- else: # array
170
- initializations.append(
171
- ast.Assign(
172
- targets=[ast.Name(id=name, ctx=ast.Store())],
173
- value=ast.Subscript(value=ast.Name(id=self.arrayName, ctx=ast.Load()),
174
- slice=ast.Attribute(value=ast.Attribute(
175
- value=ast.Name(id=self.enumIndexClass.__name__, ctx=ast.Load()),
176
- attr=name, ctx=ast.Load()), attr='value', ctx=ast.Load()), ctx=ast.Load())))
177
-
178
- node.body = initializations + node.body
179
- return node
180
-
181
- def inlineMapFoldingNumba(**keywordArguments: Optional[str]):
182
- codeSource = inspect.getsource(algorithmSource)
183
- pathFilenameAlgorithm = pathlib.Path(inspect.getfile(algorithmSource))
184
-
185
- listPathFilenamesDestination: list[pathlib.Path] = []
186
- listCallables = [ 'countInitialize', 'countParallel', 'countSequential', ]
187
- for callableTarget in listCallables:
188
- codeParsed: ast.Module = ast.parse(codeSource, type_comments=True)
189
- codeSourceImportStatements = {statement for statement in codeParsed.body if isinstance(statement, (ast.Import, ast.ImportFrom))}
190
- dictionaryFunctions = {statement.name: statement for statement in codeParsed.body if isinstance(statement, ast.FunctionDef)}
191
- callableInlinerWorkhorse = RecursiveInliner(dictionaryFunctions)
192
- parallel = callableTarget == 'countParallel'
193
- callableInlined = callableInlinerWorkhorse.inlineFunctionBody(callableTarget)
194
- if callableInlined:
195
- ast.fix_missing_locations(callableInlined)
196
- callableDecorated = decorateCallableWithNumba(callableInlined, parallel, **keywordArguments)
197
-
198
- if callableTarget == 'countSequential':
199
- myUnpacker = UnpackArrayAccesses(indexMy, 'my')
200
- callableDecorated = cast(ast.FunctionDef, myUnpacker.visit(callableDecorated))
201
- ast.fix_missing_locations(callableDecorated)
202
-
203
- trackUnpacker = UnpackArrayAccesses(indexTrack, 'track')
204
- callableDecorated = cast(ast.FunctionDef, trackUnpacker.visit(callableDecorated))
205
- ast.fix_missing_locations(callableDecorated)
206
-
207
- moduleAST = ast.Module(body=cast(List[ast.stmt], list(codeSourceImportStatements) + [callableDecorated]), type_ignores=[])
208
- ast.fix_missing_locations(moduleAST)
209
- moduleSource = ast.unparse(moduleAST)
210
-
211
- pathFilenameDestination = pathFilenameAlgorithm.parent / "syntheticModules" / pathFilenameAlgorithm.with_stem(callableTarget).name[5:None]
212
- pathFilenameDestination.write_text(moduleSource)
213
- listPathFilenamesDestination.append(pathFilenameDestination)
214
-
215
- if __name__ == '__main__':
216
- inlineMapFoldingNumba()