mapFolding 0.3.12__py3-none-any.whl → 0.4.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 +40 -38
- mapFolding/basecamp.py +50 -50
- mapFolding/beDRY.py +336 -336
- mapFolding/oeis.py +262 -262
- mapFolding/reference/flattened.py +294 -293
- mapFolding/reference/hunterNumba.py +126 -126
- mapFolding/reference/irvineJavaPort.py +99 -99
- mapFolding/reference/jax.py +153 -153
- mapFolding/reference/lunnan.py +148 -148
- mapFolding/reference/lunnanNumpy.py +115 -115
- mapFolding/reference/lunnanWhile.py +114 -114
- mapFolding/reference/rotatedEntryPoint.py +183 -183
- mapFolding/reference/total_countPlus1vsPlusN.py +203 -203
- mapFolding/someAssemblyRequired/__init__.py +2 -1
- mapFolding/someAssemblyRequired/getLLVMforNoReason.py +12 -12
- mapFolding/someAssemblyRequired/makeJob.py +48 -48
- mapFolding/someAssemblyRequired/synthesizeModuleJAX.py +17 -17
- mapFolding/someAssemblyRequired/synthesizeNumba.py +343 -633
- mapFolding/someAssemblyRequired/synthesizeNumbaGeneralized.py +371 -0
- mapFolding/someAssemblyRequired/synthesizeNumbaJob.py +150 -0
- mapFolding/someAssemblyRequired/synthesizeNumbaModules.py +75 -0
- mapFolding/syntheticModules/__init__.py +0 -0
- mapFolding/syntheticModules/numba_countInitialize.py +3 -3
- mapFolding/syntheticModules/numba_countParallel.py +3 -3
- mapFolding/syntheticModules/numba_countSequential.py +3 -3
- mapFolding/syntheticModules/numba_doTheNeedful.py +6 -6
- mapFolding/theDao.py +165 -165
- mapFolding/theSSOT.py +176 -172
- mapFolding/theSSOTnumba.py +90 -74
- mapFolding-0.4.0.dist-info/METADATA +122 -0
- mapFolding-0.4.0.dist-info/RECORD +41 -0
- tests/conftest.py +238 -128
- tests/test_oeis.py +80 -80
- tests/test_other.py +137 -224
- tests/test_tasks.py +21 -21
- tests/test_types.py +2 -2
- mapFolding/someAssemblyRequired/synthesizeNumbaHardcoding.py +0 -188
- mapFolding-0.3.12.dist-info/METADATA +0 -155
- mapFolding-0.3.12.dist-info/RECORD +0 -40
- tests/conftest_tmpRegistry.py +0 -62
- tests/conftest_uniformTests.py +0 -53
- {mapFolding-0.3.12.dist-info → mapFolding-0.4.0.dist-info}/LICENSE +0 -0
- {mapFolding-0.3.12.dist-info → mapFolding-0.4.0.dist-info}/WHEEL +0 -0
- {mapFolding-0.3.12.dist-info → mapFolding-0.4.0.dist-info}/entry_points.txt +0 -0
- {mapFolding-0.3.12.dist-info → mapFolding-0.4.0.dist-info}/top_level.txt +0 -0
|
@@ -1,637 +1,347 @@
|
|
|
1
1
|
"""I think this module is free of hardcoded values.
|
|
2
2
|
TODO: consolidate the logic in this module."""
|
|
3
|
-
from mapFolding import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
node = cast(ast.FunctionDef, self.generic_visit(node))
|
|
233
|
-
|
|
234
|
-
initializations = []
|
|
235
|
-
for name, (kind, original_node) in self.substitutions.items():
|
|
236
|
-
if kind == 'scalar':
|
|
237
|
-
initializations.append(ast.Assign(targets=[ast.Name(id=name, ctx=ast.Store())], value=original_node))
|
|
238
|
-
else: # array
|
|
239
|
-
initializations.append(
|
|
240
|
-
ast.Assign(
|
|
241
|
-
targets=[ast.Name(id=name, ctx=ast.Store())],
|
|
242
|
-
value=ast.Subscript(value=ast.Name(id=self.arrayName, ctx=ast.Load()),
|
|
243
|
-
slice=ast.Attribute(value=ast.Attribute(
|
|
244
|
-
value=ast.Name(id=self.enumIndexClass.__name__, ctx=ast.Load()),
|
|
245
|
-
attr=name, ctx=ast.Load()), attr='value', ctx=ast.Load()), ctx=ast.Load())))
|
|
246
|
-
|
|
247
|
-
node.body = initializations + node.body
|
|
248
|
-
return node
|
|
249
|
-
|
|
250
|
-
def Z0Z_UnhandledDecorators(astCallable: ast.FunctionDef) -> ast.FunctionDef:
|
|
251
|
-
# TODO: more explicit handling of decorators. I'm able to ignore this because I know `algorithmSource` doesn't have any decorators.
|
|
252
|
-
for decoratorItem in astCallable.decorator_list.copy():
|
|
253
|
-
import warnings
|
|
254
|
-
astCallable.decorator_list.remove(decoratorItem)
|
|
255
|
-
warnings.warn(f"Removed decorator {ast.unparse(decoratorItem)} from {astCallable.name}")
|
|
256
|
-
return astCallable
|
|
257
|
-
def isThisNodeNumbaJitCall(node: ast.AST) -> bool:
|
|
258
|
-
return (isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) and node.func.attr == Z0Z_getDecoratorCallable())
|
|
259
|
-
def isThisNodeJitCall(node: ast.AST) -> bool:
|
|
260
|
-
return (isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == Z0Z_getDecoratorCallable())
|
|
261
|
-
def isThisNodeNumbaJitDecorator(node: ast.AST) -> bool:
|
|
262
|
-
return isThisNodeNumbaJitCall(node) or isThisNodeJitCall(node)
|
|
263
|
-
def Z0Z_generalizeThis(FunctionDefTarget: ast.FunctionDef, parametersNumba: Optional[ParametersNumba]=None) -> Tuple[ast.FunctionDef, ParametersNumba | None]:
|
|
264
|
-
def recycleParametersNumba(decorator: ast.Call) -> Dict[str, Any]:
|
|
265
|
-
parametersNumbaExtracted: Dict[str, Any] = {}
|
|
266
|
-
for keywordItem in decorator.keywords:
|
|
267
|
-
if isinstance(keywordItem.value, ast.Constant) and keywordItem.arg is not None:
|
|
268
|
-
parametersNumbaExtracted[keywordItem.arg] = keywordItem.value.value
|
|
269
|
-
return parametersNumbaExtracted
|
|
270
|
-
|
|
271
|
-
for decorator in FunctionDefTarget.decorator_list.copy():
|
|
272
|
-
if isThisNodeNumbaJitDecorator(decorator):
|
|
273
|
-
decorator = cast(ast.Call, decorator)
|
|
274
|
-
if parametersNumba is None:
|
|
275
|
-
parametersNumbaSherpa = recycleParametersNumba(decorator)
|
|
276
|
-
if (HunterIsSureThereAreBetterWaysToDoThis := True):
|
|
277
|
-
if parametersNumbaSherpa:
|
|
278
|
-
parametersNumba = cast(ParametersNumba, parametersNumbaSherpa)
|
|
279
|
-
FunctionDefTarget.decorator_list.remove(decorator)
|
|
280
|
-
|
|
281
|
-
return FunctionDefTarget, parametersNumba
|
|
282
|
-
|
|
283
|
-
def decorateCallableWithNumba(FunctionDefTarget: ast.FunctionDef, allImports: UniversalImportTracker, parametersNumba: Optional[ParametersNumba]=None) -> Tuple[ast.FunctionDef, UniversalImportTracker]:
|
|
284
|
-
datatypeModuleDecorator = Z0Z_getDatatypeModuleScalar()
|
|
285
|
-
def make_arg4parameter(signatureElement: ast.arg):
|
|
286
|
-
if isinstance(signatureElement.annotation, ast.Subscript) and isinstance(signatureElement.annotation.slice, ast.Tuple):
|
|
287
|
-
annotationShape = signatureElement.annotation.slice.elts[0]
|
|
288
|
-
if isinstance(annotationShape, ast.Subscript) and isinstance(annotationShape.slice, ast.Tuple):
|
|
289
|
-
shapeAsListSlices: Sequence[ast.expr] = [ast.Slice() for axis in range(len(annotationShape.slice.elts))]
|
|
290
|
-
shapeAsListSlices[-1] = ast.Slice(step=ast.Constant(value=1))
|
|
291
|
-
shapeAST = ast.Tuple(elts=list(shapeAsListSlices), ctx=ast.Load())
|
|
292
|
-
else:
|
|
293
|
-
shapeAST = ast.Slice(step=ast.Constant(value=1))
|
|
294
|
-
|
|
295
|
-
annotationDtype = signatureElement.annotation.slice.elts[1]
|
|
296
|
-
if (isinstance(annotationDtype, ast.Subscript) and isinstance(annotationDtype.slice, ast.Attribute)):
|
|
297
|
-
datatypeAST = annotationDtype.slice.attr
|
|
298
|
-
else:
|
|
299
|
-
datatypeAST = None
|
|
300
|
-
|
|
301
|
-
ndarrayName = signatureElement.arg
|
|
302
|
-
Z0Z_hacky_dtype = hackSSOTdatatype(ndarrayName)
|
|
303
|
-
datatype_attr = datatypeAST or Z0Z_hacky_dtype
|
|
304
|
-
allImports.addImportFromStr(datatypeModuleDecorator, datatype_attr)
|
|
305
|
-
datatypeNumba = ast.Name(id=datatype_attr, ctx=ast.Load())
|
|
306
|
-
|
|
307
|
-
return ast.Subscript(value=datatypeNumba, slice=shapeAST, ctx=ast.Load())
|
|
308
|
-
|
|
309
|
-
list_argsDecorator: Sequence[ast.expr] = []
|
|
310
|
-
|
|
311
|
-
list_arg4signature_or_function: Sequence[ast.expr] = []
|
|
312
|
-
for parameter in FunctionDefTarget.args.args:
|
|
313
|
-
signatureElement = make_arg4parameter(parameter)
|
|
314
|
-
if signatureElement:
|
|
315
|
-
list_arg4signature_or_function.append(signatureElement)
|
|
316
|
-
|
|
317
|
-
if FunctionDefTarget.returns and isinstance(FunctionDefTarget.returns, ast.Name):
|
|
318
|
-
theReturn: ast.Name = FunctionDefTarget.returns
|
|
319
|
-
list_argsDecorator = [cast(ast.expr, ast.Call(func=ast.Name(id=theReturn.id, ctx=ast.Load())
|
|
320
|
-
, args=list_arg4signature_or_function if list_arg4signature_or_function else [] , keywords=[] ) )]
|
|
321
|
-
elif list_arg4signature_or_function:
|
|
322
|
-
list_argsDecorator = [cast(ast.expr, ast.Tuple(elts=list_arg4signature_or_function, ctx=ast.Load()))]
|
|
323
|
-
|
|
324
|
-
FunctionDefTarget, parametersNumba = Z0Z_generalizeThis(FunctionDefTarget, parametersNumba)
|
|
325
|
-
FunctionDefTarget = Z0Z_UnhandledDecorators(FunctionDefTarget)
|
|
326
|
-
if parametersNumba is None:
|
|
327
|
-
parametersNumba = parametersNumbaDEFAULT
|
|
328
|
-
listDecoratorKeywords = [ast.keyword(arg=parameterName, value=ast.Constant(value=parameterValue)) for parameterName, parameterValue in parametersNumba.items()]
|
|
329
|
-
|
|
330
|
-
decoratorModule = Z0Z_getDatatypeModuleScalar()
|
|
331
|
-
decoratorCallable = Z0Z_getDecoratorCallable()
|
|
332
|
-
allImports.addImportFromStr(decoratorModule, decoratorCallable)
|
|
333
|
-
astDecorator = ast.Call(
|
|
334
|
-
func=ast.Name(id=decoratorCallable, ctx=ast.Load())
|
|
335
|
-
, args=list_argsDecorator if list_argsDecorator else []
|
|
336
|
-
, keywords=listDecoratorKeywords)
|
|
337
|
-
|
|
338
|
-
FunctionDefTarget.decorator_list = [astDecorator]
|
|
339
|
-
return FunctionDefTarget, allImports
|
|
340
|
-
|
|
341
|
-
def makeDecoratorJobNumba(FunctionDefTarget: ast.FunctionDef, allImports: UniversalImportTracker, parametersNumba: Optional[ParametersNumba]=None) -> Tuple[ast.FunctionDef, UniversalImportTracker]:
|
|
342
|
-
decoratorCallable = Z0Z_getDecoratorCallable()
|
|
343
|
-
def convertToPlainJit(node: ast.Call) -> ast.Call:
|
|
344
|
-
node.func = ast.Name(id=decoratorCallable, ctx=ast.Load())
|
|
345
|
-
return node
|
|
346
|
-
|
|
347
|
-
FunctionDefTarget, parametersNumba = Z0Z_generalizeThis(FunctionDefTarget, parametersNumba)
|
|
348
|
-
|
|
349
|
-
FunctionDefTarget, allImports = decorateCallableWithNumba(FunctionDefTarget, allImports, parametersNumba)
|
|
350
|
-
if isThisNodeNumbaJitCall(FunctionDefTarget.decorator_list[0]):
|
|
351
|
-
FunctionDefTarget.decorator_list[0] = convertToPlainJit(cast(ast.Call, FunctionDefTarget.decorator_list[0]))
|
|
352
|
-
|
|
353
|
-
return FunctionDefTarget, allImports
|
|
354
|
-
|
|
355
|
-
def inlineOneCallable(pythonSource: str, callableTarget: str, parametersNumba: Optional[ParametersNumba]=None, unpackArrays: Optional[bool]=False) -> str:
|
|
356
|
-
astModule: ast.Module = ast.parse(pythonSource, type_comments=True)
|
|
357
|
-
allImports = UniversalImportTracker()
|
|
358
|
-
|
|
359
|
-
for statement in astModule.body:
|
|
360
|
-
if isinstance(statement, (ast.Import, ast.ImportFrom)):
|
|
361
|
-
allImports.addAst(statement)
|
|
362
|
-
|
|
363
|
-
dictionaryFunctionDef = {statement.name: statement for statement in astModule.body if isinstance(statement, ast.FunctionDef)}
|
|
364
|
-
callableInlinerWorkhorse = RecursiveInliner(dictionaryFunctionDef)
|
|
365
|
-
FunctionDefTarget = callableInlinerWorkhorse.inlineFunctionBody(callableTarget)
|
|
366
|
-
|
|
367
|
-
if not FunctionDefTarget:
|
|
368
|
-
raise FREAKOUT
|
|
369
|
-
|
|
370
|
-
ast.fix_missing_locations(FunctionDefTarget)
|
|
371
|
-
|
|
372
|
-
FunctionDefTarget, allImports = decorateCallableWithNumba(FunctionDefTarget, allImports, parametersNumba)
|
|
373
|
-
|
|
374
|
-
if unpackArrays:
|
|
375
|
-
for tupleUnpack in [(indexMy, 'my'), (indexTrack, 'track')]:
|
|
376
|
-
unpacker = UnpackArrays(*tupleUnpack)
|
|
377
|
-
FunctionDefTarget = cast(ast.FunctionDef, unpacker.visit(FunctionDefTarget))
|
|
378
|
-
ast.fix_missing_locations(FunctionDefTarget)
|
|
379
|
-
|
|
380
|
-
moduleAST = ast.Module(body=cast(List[ast.stmt], allImports.makeListAst() + [FunctionDefTarget]), type_ignores=[])
|
|
381
|
-
ast.fix_missing_locations(moduleAST)
|
|
382
|
-
moduleSource = ast.unparse(moduleAST)
|
|
383
|
-
return moduleSource
|
|
384
|
-
|
|
385
|
-
def makeDispatcherNumba(pythonSource: str, callableTarget: str, listStuffYouOughtaKnow: List[youOughtaKnow]) -> str:
|
|
386
|
-
astSource = ast.parse(pythonSource)
|
|
387
|
-
allImports = UniversalImportTracker()
|
|
388
|
-
|
|
389
|
-
for statement in astSource.body:
|
|
390
|
-
if isinstance(statement, (ast.Import, ast.ImportFrom)):
|
|
391
|
-
allImports.addAst(statement)
|
|
392
|
-
|
|
393
|
-
for stuff in listStuffYouOughtaKnow:
|
|
394
|
-
statement = stuff.astForCompetentProgrammers
|
|
395
|
-
if isinstance(statement, (ast.Import, ast.ImportFrom)):
|
|
396
|
-
allImports.addAst(statement)
|
|
397
|
-
|
|
398
|
-
FunctionDefTarget = next((node for node in astSource.body if isinstance(node, ast.FunctionDef) and node.name == callableTarget), None)
|
|
399
|
-
|
|
400
|
-
if not FunctionDefTarget:
|
|
401
|
-
raise ValueError(f"Could not find function {callableTarget} in source code")
|
|
402
|
-
|
|
403
|
-
FunctionDefTarget, allImports = decorateCallableWithNumba(FunctionDefTarget, allImports, parametersNumbaFailEarly)
|
|
404
|
-
|
|
405
|
-
astModule = ast.Module(body=cast(List[ast.stmt], allImports.makeListAst() + [FunctionDefTarget]), type_ignores=[])
|
|
406
|
-
|
|
407
|
-
ast.fix_missing_locations(astModule)
|
|
408
|
-
return ast.unparse(astModule)
|
|
409
|
-
|
|
410
|
-
def makeStrRLEcompacted(arrayTarget: numpy.ndarray, identifierName: Optional[str]=None) -> str:
|
|
411
|
-
"""Converts a NumPy array into a compressed string representation using run-length encoding (RLE).
|
|
412
|
-
|
|
413
|
-
This function takes a NumPy array and converts it into an optimized string representation by:
|
|
414
|
-
1. Compressing consecutive sequences of numbers into range objects
|
|
415
|
-
2. Minimizing repeated zeros using array multiplication syntax
|
|
416
|
-
3. Converting the result into a valid Python array initialization statement
|
|
417
|
-
|
|
418
|
-
Parameters:
|
|
419
|
-
arrayTarget (numpy.ndarray): The input NumPy array to be converted
|
|
420
|
-
identifierName (str): The variable name to use in the output string
|
|
421
|
-
|
|
422
|
-
Returns:
|
|
423
|
-
str: A string containing Python code that recreates the input array in compressed form.
|
|
424
|
-
Format: "{identifierName} = numpy.array({compressed_data}, dtype=numpy.{dtype})"
|
|
425
|
-
"""
|
|
426
|
-
|
|
427
|
-
def compressRangesNDArrayNoFlatten(arraySlice):
|
|
428
|
-
if isinstance(arraySlice, numpy.ndarray) and arraySlice.ndim > 1:
|
|
429
|
-
return [compressRangesNDArrayNoFlatten(arraySlice[index]) for index in range(arraySlice.shape[0])]
|
|
430
|
-
elif isinstance(arraySlice, numpy.ndarray) and arraySlice.ndim == 1:
|
|
431
|
-
listWithRanges = []
|
|
432
|
-
for group in more_itertools.consecutive_groups(arraySlice.tolist()):
|
|
433
|
-
ImaSerious = list(group)
|
|
434
|
-
if len(ImaSerious) <= 4:
|
|
435
|
-
listWithRanges += ImaSerious
|
|
436
|
-
else:
|
|
437
|
-
ImaRange = [range(ImaSerious[0], ImaSerious[-1] + 1)]
|
|
438
|
-
listWithRanges += ImaRange
|
|
439
|
-
return listWithRanges
|
|
440
|
-
return arraySlice
|
|
441
|
-
|
|
442
|
-
arrayAsNestedLists = compressRangesNDArrayNoFlatten(arrayTarget)
|
|
443
|
-
|
|
444
|
-
stringMinimized = python_minifier.minify(str(arrayAsNestedLists))
|
|
445
|
-
commaZeroMaximum = arrayTarget.shape[-1] - 1
|
|
446
|
-
stringMinimized = stringMinimized.replace('[0' + ',0'*commaZeroMaximum + ']', '[0]*'+str(commaZeroMaximum+1))
|
|
447
|
-
for countZeros in range(commaZeroMaximum, 2, -1):
|
|
448
|
-
stringMinimized = stringMinimized.replace(',0'*countZeros + ']', ']+[0]*'+str(countZeros))
|
|
449
|
-
|
|
450
|
-
stringMinimized = stringMinimized.replace('range', '*range')
|
|
451
|
-
|
|
452
|
-
if identifierName:
|
|
453
|
-
return f"{identifierName} = array({stringMinimized}, dtype={arrayTarget.dtype})"
|
|
454
|
-
return stringMinimized
|
|
455
|
-
|
|
456
|
-
def moveArrayTo_body(FunctionDefTarget: ast.FunctionDef, astArg: ast.arg, argData: numpy.ndarray, allImports: UniversalImportTracker) -> Tuple[ast.FunctionDef, UniversalImportTracker]:
|
|
457
|
-
arrayType = type(argData)
|
|
458
|
-
moduleConstructor = arrayType.__module__
|
|
459
|
-
constructorName = arrayType.__name__
|
|
460
|
-
# NOTE hack
|
|
461
|
-
constructorName = constructorName.replace('ndarray', 'array')
|
|
462
|
-
argData_dtype: numpy.dtype = argData.dtype
|
|
463
|
-
argData_dtypeName = argData.dtype.name
|
|
464
|
-
|
|
465
|
-
allImports.addImportFromStr(moduleConstructor, constructorName)
|
|
466
|
-
allImports.addImportFromStr(moduleConstructor, argData_dtypeName)
|
|
467
|
-
|
|
468
|
-
onlyDataRLE = makeStrRLEcompacted(argData)
|
|
469
|
-
astStatement = cast(ast.Expr, ast.parse(onlyDataRLE).body[0])
|
|
470
|
-
dataAst = astStatement.value
|
|
471
|
-
|
|
472
|
-
arrayCall = ast.Call(
|
|
473
|
-
func=ast.Name(id=constructorName, ctx=ast.Load())
|
|
474
|
-
, args=[dataAst]
|
|
475
|
-
, keywords=[ast.keyword(arg='dtype' , value=ast.Name(id=argData_dtypeName , ctx=ast.Load()) ) ] )
|
|
476
|
-
|
|
477
|
-
assignment = ast.Assign( targets=[ast.Name(id=astArg.arg, ctx=ast.Store())], value=arrayCall )
|
|
478
|
-
FunctionDefTarget.body.insert(0, assignment)
|
|
479
|
-
FunctionDefTarget.args.args.remove(astArg)
|
|
480
|
-
|
|
481
|
-
return FunctionDefTarget, allImports
|
|
482
|
-
|
|
483
|
-
def evaluateArrayIn_body(FunctionDefTarget: ast.FunctionDef, astArg: ast.arg, argData: numpy.ndarray, allImports: UniversalImportTracker) -> Tuple[ast.FunctionDef, UniversalImportTracker]:
|
|
484
|
-
arrayType = type(argData)
|
|
485
|
-
moduleConstructor = arrayType.__module__
|
|
486
|
-
constructorName = arrayType.__name__
|
|
487
|
-
# NOTE hack
|
|
488
|
-
constructorName = constructorName.replace('ndarray', 'array')
|
|
489
|
-
allImports.addImportFromStr(moduleConstructor, constructorName)
|
|
490
|
-
|
|
491
|
-
for stmt in FunctionDefTarget.body.copy():
|
|
492
|
-
if isinstance(stmt, ast.Assign):
|
|
493
|
-
if isinstance(stmt.targets[0], ast.Name) and isinstance(stmt.value, ast.Subscript):
|
|
494
|
-
astAssignee: ast.Name = stmt.targets[0]
|
|
495
|
-
argData_dtypeName = hackSSOTdatatype(astAssignee.id)
|
|
496
|
-
allImports.addImportFromStr(moduleConstructor, argData_dtypeName)
|
|
497
|
-
astSubscript: ast.Subscript = stmt.value
|
|
498
|
-
if isinstance(astSubscript.value, ast.Name) and astSubscript.value.id == astArg.arg and isinstance(astSubscript.slice, ast.Attribute):
|
|
499
|
-
indexAs_astAttribute: ast.Attribute = astSubscript.slice
|
|
500
|
-
indexAsStr = ast.unparse(indexAs_astAttribute)
|
|
501
|
-
argDataSlice = argData[eval(indexAsStr)]
|
|
502
|
-
|
|
503
|
-
onlyDataRLE = makeStrRLEcompacted(argDataSlice)
|
|
504
|
-
astStatement = cast(ast.Expr, ast.parse(onlyDataRLE).body[0])
|
|
505
|
-
dataAst = astStatement.value
|
|
506
|
-
|
|
507
|
-
arrayCall = ast.Call(
|
|
508
|
-
func=ast.Name(id=constructorName, ctx=ast.Load()) , args=[dataAst]
|
|
509
|
-
, keywords=[ast.keyword(arg='dtype', value=ast.Name(id=argData_dtypeName, ctx=ast.Load()) ) ] )
|
|
510
|
-
|
|
511
|
-
assignment = ast.Assign( targets=[astAssignee], value=arrayCall )
|
|
512
|
-
FunctionDefTarget.body.insert(0, assignment)
|
|
513
|
-
FunctionDefTarget.body.remove(stmt)
|
|
514
|
-
|
|
515
|
-
FunctionDefTarget.args.args.remove(astArg)
|
|
516
|
-
return FunctionDefTarget, allImports
|
|
517
|
-
|
|
518
|
-
def evaluate_argIn_body(FunctionDefTarget: ast.FunctionDef, astArg: ast.arg, argData: numpy.ndarray, Z0Z_listChaff: List[str], allImports: UniversalImportTracker) -> Tuple[ast.FunctionDef, UniversalImportTracker]:
|
|
519
|
-
moduleConstructor = Z0Z_getDatatypeModuleScalar()
|
|
520
|
-
for stmt in FunctionDefTarget.body.copy():
|
|
521
|
-
if isinstance(stmt, ast.Assign):
|
|
522
|
-
if isinstance(stmt.targets[0], ast.Name) and isinstance(stmt.value, ast.Subscript):
|
|
523
|
-
astAssignee: ast.Name = stmt.targets[0]
|
|
524
|
-
argData_dtypeName = hackSSOTdatatype(astAssignee.id)
|
|
525
|
-
allImports.addImportFromStr(moduleConstructor, argData_dtypeName)
|
|
526
|
-
astSubscript: ast.Subscript = stmt.value
|
|
527
|
-
if isinstance(astSubscript.value, ast.Name) and astSubscript.value.id == astArg.arg and isinstance(astSubscript.slice, ast.Attribute):
|
|
528
|
-
indexAs_astAttribute: ast.Attribute = astSubscript.slice
|
|
529
|
-
indexAsStr = ast.unparse(indexAs_astAttribute)
|
|
530
|
-
argDataSlice: int = argData[eval(indexAsStr)].item()
|
|
531
|
-
astCall = ast.Call(func=ast.Name(id=argData_dtypeName, ctx=ast.Load()) , args=[ast.Constant(value=argDataSlice)], keywords=[])
|
|
532
|
-
assignment = ast.Assign(targets=[astAssignee], value=astCall)
|
|
533
|
-
if astAssignee.id not in Z0Z_listChaff:
|
|
534
|
-
FunctionDefTarget.body.insert(0, assignment)
|
|
535
|
-
FunctionDefTarget.body.remove(stmt)
|
|
536
|
-
FunctionDefTarget.args.args.remove(astArg)
|
|
537
|
-
return FunctionDefTarget, allImports
|
|
538
|
-
|
|
539
|
-
def evaluateAnnAssignIn_body(FunctionDefTarget: ast.FunctionDef, allImports: UniversalImportTracker) -> Tuple[ast.FunctionDef, UniversalImportTracker]:
|
|
540
|
-
moduleConstructor = Z0Z_getDatatypeModuleScalar()
|
|
541
|
-
for stmt in FunctionDefTarget.body.copy():
|
|
542
|
-
if isinstance(stmt, ast.AnnAssign):
|
|
543
|
-
if isinstance(stmt.target, ast.Name) and isinstance(stmt.value, ast.Constant):
|
|
544
|
-
astAssignee: ast.Name = stmt.target
|
|
545
|
-
argData_dtypeName = hackSSOTdatatype(astAssignee.id)
|
|
546
|
-
allImports.addImportFromStr(moduleConstructor, argData_dtypeName)
|
|
547
|
-
astCall = ast.Call(func=ast.Name(id=argData_dtypeName, ctx=ast.Load()) , args=[stmt.value], keywords=[])
|
|
548
|
-
assignment = ast.Assign(targets=[astAssignee], value=astCall)
|
|
549
|
-
FunctionDefTarget.body.insert(0, assignment)
|
|
550
|
-
FunctionDefTarget.body.remove(stmt)
|
|
551
|
-
return FunctionDefTarget, allImports
|
|
552
|
-
|
|
553
|
-
def removeIdentifierFrom_body(FunctionDefTarget: ast.FunctionDef, astArg: ast.arg) -> ast.FunctionDef:
|
|
554
|
-
for stmt in FunctionDefTarget.body.copy():
|
|
555
|
-
if isinstance(stmt, ast.Assign):
|
|
556
|
-
if isinstance(stmt.targets[0], ast.Subscript) and isinstance(stmt.targets[0].value, ast.Name):
|
|
557
|
-
if stmt.targets[0].value.id == astArg.arg:
|
|
558
|
-
FunctionDefTarget.body.remove(stmt)
|
|
559
|
-
FunctionDefTarget.args.args.remove(astArg)
|
|
560
|
-
return FunctionDefTarget
|
|
561
|
-
|
|
562
|
-
def astObjectToAstConstant(FunctionDefTarget: ast.FunctionDef, object: str, value: int) -> ast.FunctionDef:
|
|
563
|
-
"""
|
|
564
|
-
Replaces nodes in astFunction matching the AST of the string `object`
|
|
565
|
-
with a constant node holding the provided value.
|
|
566
|
-
"""
|
|
567
|
-
targetExpression = ast.parse(object, mode='eval').body
|
|
568
|
-
targetDump = ast.dump(targetExpression, annotate_fields=False)
|
|
569
|
-
|
|
570
|
-
def findNode(node: ast.AST) -> bool:
|
|
571
|
-
return ast.dump(node, annotate_fields=False) == targetDump
|
|
572
|
-
|
|
573
|
-
def replaceWithConstant(node: ast.AST) -> ast.AST:
|
|
574
|
-
return ast.copy_location(ast.Constant(value=value), node)
|
|
575
|
-
|
|
576
|
-
transformer = NodeReplacer(findNode, replaceWithConstant)
|
|
577
|
-
newFunction = cast(ast.FunctionDef, transformer.visit(FunctionDefTarget))
|
|
578
|
-
ast.fix_missing_locations(newFunction)
|
|
579
|
-
return newFunction
|
|
580
|
-
|
|
581
|
-
def astNameToAstConstant(FunctionDefTarget: ast.FunctionDef, name: str, value: int) -> ast.FunctionDef:
|
|
582
|
-
def findName(node: ast.AST) -> bool:
|
|
583
|
-
return isinstance(node, ast.Name) and node.id == name
|
|
584
|
-
|
|
585
|
-
def replaceWithConstant(node: ast.AST) -> ast.AST:
|
|
586
|
-
return ast.copy_location(ast.Constant(value=value), node)
|
|
587
|
-
|
|
588
|
-
return cast(ast.FunctionDef, NodeReplacer(findName, replaceWithConstant).visit(FunctionDefTarget))
|
|
589
|
-
|
|
590
|
-
def makeLauncherJobNumba(callableTarget: str, pathFilenameFoldsTotal: pathlib.Path) -> ast.Module:
|
|
591
|
-
linesLaunch = f"""
|
|
3
|
+
from mapFolding.someAssemblyRequired.synthesizeNumbaGeneralized import *
|
|
4
|
+
|
|
5
|
+
def insertArrayIn_body(FunctionDefTarget: ast.FunctionDef, identifier: str, arrayTarget: numpy.ndarray, allImports: UniversalImportTracker, unrollSlices: Optional[int]=None) -> Tuple[ast.FunctionDef, UniversalImportTracker]:
|
|
6
|
+
arrayType = type(arrayTarget)
|
|
7
|
+
moduleConstructor = arrayType.__module__
|
|
8
|
+
constructorName = arrayType.__name__
|
|
9
|
+
# NOTE hack
|
|
10
|
+
constructorName = constructorName.replace('ndarray', 'array')
|
|
11
|
+
argData_dtype: numpy.dtype = arrayTarget.dtype
|
|
12
|
+
argData_dtypeName = arrayTarget.dtype.name
|
|
13
|
+
|
|
14
|
+
allImports.addImportFromStr(moduleConstructor, constructorName)
|
|
15
|
+
allImports.addImportFromStr(moduleConstructor, argData_dtypeName)
|
|
16
|
+
|
|
17
|
+
def insertAssign(assignee: str, arraySlice: numpy.ndarray) -> None:
|
|
18
|
+
nonlocal FunctionDefTarget
|
|
19
|
+
onlyDataRLE = makeStrRLEcompacted(arraySlice) #NOTE
|
|
20
|
+
astStatement = cast(ast.Expr, ast.parse(onlyDataRLE).body[0])
|
|
21
|
+
dataAst = astStatement.value
|
|
22
|
+
|
|
23
|
+
arrayCall = Then.make_astCall(name=constructorName, args=[dataAst], dictionaryKeywords={'dtype': ast.Name(id=argData_dtypeName, ctx=ast.Load())})
|
|
24
|
+
|
|
25
|
+
assignment = ast.Assign(targets=[ast.Name(id=assignee, ctx=ast.Store())], value=arrayCall)#NOTE
|
|
26
|
+
FunctionDefTarget.body.insert(0, assignment)
|
|
27
|
+
|
|
28
|
+
if not unrollSlices:
|
|
29
|
+
insertAssign(identifier, arrayTarget)
|
|
30
|
+
else:
|
|
31
|
+
for index, arraySlice in enumerate(arrayTarget):
|
|
32
|
+
insertAssign(f"{identifier}_{index}", arraySlice)
|
|
33
|
+
|
|
34
|
+
return FunctionDefTarget, allImports
|
|
35
|
+
|
|
36
|
+
def findAndReplaceArrayIn_body(FunctionDefTarget: ast.FunctionDef, identifier: str, arrayTarget: numpy.ndarray, allImports: UniversalImportTracker) -> Tuple[ast.FunctionDef, UniversalImportTracker]:
|
|
37
|
+
arrayType = type(arrayTarget)
|
|
38
|
+
moduleConstructor = arrayType.__module__
|
|
39
|
+
constructorName = arrayType.__name__
|
|
40
|
+
# NOTE hack
|
|
41
|
+
constructorName = constructorName.replace('ndarray', 'array')
|
|
42
|
+
allImports.addImportFromStr(moduleConstructor, constructorName)
|
|
43
|
+
|
|
44
|
+
for stmt in FunctionDefTarget.body.copy():
|
|
45
|
+
if isinstance(stmt, ast.Assign):
|
|
46
|
+
if isinstance(stmt.targets[0], ast.Name) and isinstance(stmt.value, ast.Subscript):
|
|
47
|
+
astAssignee: ast.Name = stmt.targets[0]
|
|
48
|
+
argData_dtypeName = hackSSOTdatatype(astAssignee.id)
|
|
49
|
+
allImports.addImportFromStr(moduleConstructor, argData_dtypeName)
|
|
50
|
+
astSubscript: ast.Subscript = stmt.value
|
|
51
|
+
if isinstance(astSubscript.value, ast.Name) and astSubscript.value.id == identifier and isinstance(astSubscript.slice, ast.Attribute):
|
|
52
|
+
indexAs_astAttribute: ast.Attribute = astSubscript.slice
|
|
53
|
+
indexAsStr = ast.unparse(indexAs_astAttribute)
|
|
54
|
+
argDataSlice = arrayTarget[eval(indexAsStr)]
|
|
55
|
+
|
|
56
|
+
onlyDataRLE = makeStrRLEcompacted(argDataSlice)
|
|
57
|
+
astStatement = cast(ast.Expr, ast.parse(onlyDataRLE).body[0])
|
|
58
|
+
dataAst = astStatement.value
|
|
59
|
+
|
|
60
|
+
arrayCall = Then.make_astCall(name=constructorName, args=[dataAst], dictionaryKeywords={'dtype': ast.Name(id=argData_dtypeName, ctx=ast.Load())})
|
|
61
|
+
|
|
62
|
+
assignment = ast.Assign( targets=[astAssignee], value=arrayCall )
|
|
63
|
+
FunctionDefTarget.body.insert(0, assignment)
|
|
64
|
+
FunctionDefTarget.body.remove(stmt)
|
|
65
|
+
return FunctionDefTarget, allImports
|
|
66
|
+
|
|
67
|
+
def findAndReplaceArraySubscriptIn_body(FunctionDefTarget: ast.FunctionDef, identifier: str, arrayTarget: numpy.ndarray, Z0Z_listChaff: List[str], allImports: UniversalImportTracker) -> Tuple[ast.FunctionDef, UniversalImportTracker]:
|
|
68
|
+
moduleConstructor = Z0Z_getDatatypeModuleScalar()
|
|
69
|
+
for stmt in FunctionDefTarget.body.copy():
|
|
70
|
+
if isinstance(stmt, ast.Assign):
|
|
71
|
+
if isinstance(stmt.targets[0], ast.Name) and isinstance(stmt.value, ast.Subscript):
|
|
72
|
+
astAssignee: ast.Name = stmt.targets[0]
|
|
73
|
+
argData_dtypeName = hackSSOTdatatype(astAssignee.id)
|
|
74
|
+
allImports.addImportFromStr(moduleConstructor, argData_dtypeName)
|
|
75
|
+
astSubscript: ast.Subscript = stmt.value
|
|
76
|
+
if isinstance(astSubscript.value, ast.Name) and astSubscript.value.id == identifier and isinstance(astSubscript.slice, ast.Attribute):
|
|
77
|
+
indexAs_astAttribute: ast.Attribute = astSubscript.slice
|
|
78
|
+
indexAsStr = ast.unparse(indexAs_astAttribute)
|
|
79
|
+
argDataSlice: int = arrayTarget[eval(indexAsStr)].item()
|
|
80
|
+
astCall = ast.Call(func=ast.Name(id=argData_dtypeName, ctx=ast.Load()) , args=[ast.Constant(value=argDataSlice)], keywords=[])
|
|
81
|
+
assignment = ast.Assign(targets=[astAssignee], value=astCall)
|
|
82
|
+
if astAssignee.id not in Z0Z_listChaff:
|
|
83
|
+
FunctionDefTarget.body.insert(0, assignment)
|
|
84
|
+
FunctionDefTarget.body.remove(stmt)
|
|
85
|
+
return FunctionDefTarget, allImports
|
|
86
|
+
|
|
87
|
+
def removeAssignTargetFrom_body(FunctionDefTarget: ast.FunctionDef, identifier: str) -> ast.FunctionDef:
|
|
88
|
+
# Remove assignment nodes where the target is either a Subscript referencing `identifier` or satisfies ifThis.nameIs(identifier).
|
|
89
|
+
def predicate(astNode: ast.AST) -> bool:
|
|
90
|
+
if not isinstance(astNode, ast.Assign) or not astNode.targets:
|
|
91
|
+
return False
|
|
92
|
+
targetNode = astNode.targets[0]
|
|
93
|
+
return (isinstance(targetNode, ast.Subscript) and isinstance(targetNode.value, ast.Name) and targetNode.value.id == identifier) or ifThis.nameIs(identifier)(targetNode)
|
|
94
|
+
def replacementBuilder(astNode: ast.AST) -> Optional[ast.stmt]:
|
|
95
|
+
# Returning None removes the node.
|
|
96
|
+
return None
|
|
97
|
+
FunctionDefSherpa = NodeReplacer(predicate, replacementBuilder).visit(FunctionDefTarget)
|
|
98
|
+
if not FunctionDefSherpa:
|
|
99
|
+
raise FREAKOUT("Dude, where's my function?")
|
|
100
|
+
else:
|
|
101
|
+
FunctionDefTarget = cast(ast.FunctionDef, FunctionDefSherpa)
|
|
102
|
+
ast.fix_missing_locations(FunctionDefTarget)
|
|
103
|
+
return FunctionDefTarget
|
|
104
|
+
|
|
105
|
+
def findAndReplaceAnnAssignIn_body(FunctionDefTarget: ast.FunctionDef, allImports: UniversalImportTracker) -> Tuple[ast.FunctionDef, UniversalImportTracker]:
|
|
106
|
+
moduleConstructor = Z0Z_getDatatypeModuleScalar()
|
|
107
|
+
for stmt in FunctionDefTarget.body.copy():
|
|
108
|
+
if isinstance(stmt, ast.AnnAssign):
|
|
109
|
+
if isinstance(stmt.target, ast.Name) and isinstance(stmt.value, ast.Constant):
|
|
110
|
+
astAssignee: ast.Name = stmt.target
|
|
111
|
+
argData_dtypeName = hackSSOTdatatype(astAssignee.id)
|
|
112
|
+
allImports.addImportFromStr(moduleConstructor, argData_dtypeName)
|
|
113
|
+
astCall = ast.Call(func=ast.Name(id=argData_dtypeName, ctx=ast.Load()) , args=[stmt.value], keywords=[])
|
|
114
|
+
assignment = ast.Assign(targets=[astAssignee], value=astCall)
|
|
115
|
+
FunctionDefTarget.body.insert(0, assignment)
|
|
116
|
+
FunctionDefTarget.body.remove(stmt)
|
|
117
|
+
return FunctionDefTarget, allImports
|
|
118
|
+
|
|
119
|
+
def findThingyReplaceWithConstantIn_body(FunctionDefTarget: ast.FunctionDef, object: str, value: int) -> ast.FunctionDef:
|
|
120
|
+
"""
|
|
121
|
+
Replaces nodes in astFunction matching the AST of the string `object`
|
|
122
|
+
with a constant node holding the provided value.
|
|
123
|
+
"""
|
|
124
|
+
targetExpression = ast.parse(object, mode='eval').body
|
|
125
|
+
targetDump = ast.dump(targetExpression, annotate_fields=False)
|
|
126
|
+
|
|
127
|
+
def findNode(node: ast.AST) -> bool:
|
|
128
|
+
return ast.dump(node, annotate_fields=False) == targetDump
|
|
129
|
+
|
|
130
|
+
def replaceWithConstant(node: ast.AST) -> ast.AST:
|
|
131
|
+
return ast.copy_location(ast.Constant(value=value), node)
|
|
132
|
+
|
|
133
|
+
transformer = NodeReplacer(findNode, replaceWithConstant)
|
|
134
|
+
newFunction = cast(ast.FunctionDef, transformer.visit(FunctionDefTarget))
|
|
135
|
+
ast.fix_missing_locations(newFunction)
|
|
136
|
+
return newFunction
|
|
137
|
+
|
|
138
|
+
def findAstNameReplaceWithConstantIn_body(FunctionDefTarget: ast.FunctionDef, name: str, value: int) -> ast.FunctionDef:
|
|
139
|
+
def findName(node: ast.AST) -> bool:
|
|
140
|
+
return isinstance(node, ast.Name) and node.id == name
|
|
141
|
+
|
|
142
|
+
def replaceWithConstant(node: ast.AST) -> ast.AST:
|
|
143
|
+
return ast.copy_location(ast.Constant(value=value), node)
|
|
144
|
+
|
|
145
|
+
return cast(ast.FunctionDef, NodeReplacer(findName, replaceWithConstant).visit(FunctionDefTarget))
|
|
146
|
+
|
|
147
|
+
def insertReturnStatementIn_body(FunctionDefTarget: ast.FunctionDef, arrayTarget: numpy.ndarray, allImports: UniversalImportTracker) -> Tuple[ast.FunctionDef, UniversalImportTracker]:
|
|
148
|
+
"""Add multiplication and return statement to function, properly constructing AST nodes."""
|
|
149
|
+
# Create AST for multiplication operation
|
|
150
|
+
multiplicand = Z0Z_identifierCountFolds
|
|
151
|
+
datatype = hackSSOTdatatype(multiplicand)
|
|
152
|
+
multiplyOperation = ast.BinOp(
|
|
153
|
+
left=ast.Name(id=multiplicand, ctx=ast.Load()),
|
|
154
|
+
op=ast.Mult(), right=ast.Constant(value=int(arrayTarget[-1])))
|
|
155
|
+
|
|
156
|
+
returnStatement = ast.Return(value=multiplyOperation)
|
|
157
|
+
|
|
158
|
+
datatypeModuleScalar = Z0Z_getDatatypeModuleScalar()
|
|
159
|
+
allImports.addImportFromStr(datatypeModuleScalar, datatype)
|
|
160
|
+
|
|
161
|
+
FunctionDefTarget.body.append(returnStatement)
|
|
162
|
+
|
|
163
|
+
return FunctionDefTarget, allImports
|
|
164
|
+
|
|
165
|
+
def findAndReplaceWhileLoopIn_body(FunctionDefTarget: ast.FunctionDef, iteratorName: str, iterationsTotal: int) -> Any:
|
|
166
|
+
"""
|
|
167
|
+
Unroll all nested while loops matching the condition that their test uses `iteratorName`.
|
|
168
|
+
"""
|
|
169
|
+
# Helper transformer to replace iterator occurrences with a constant.
|
|
170
|
+
class ReplaceIterator(ast.NodeTransformer):
|
|
171
|
+
def __init__(self, iteratorName: str, constantValue: int) -> None:
|
|
172
|
+
super().__init__()
|
|
173
|
+
self.iteratorName = iteratorName
|
|
174
|
+
self.constantValue = constantValue
|
|
175
|
+
|
|
176
|
+
def visit_Name(self, node: ast.Name) -> ast.AST:
|
|
177
|
+
if node.id == self.iteratorName:
|
|
178
|
+
return ast.copy_location(ast.Constant(value=self.constantValue), node)
|
|
179
|
+
return self.generic_visit(node)
|
|
180
|
+
|
|
181
|
+
# NodeTransformer that finds while loops (even if deeply nested) and unrolls them.
|
|
182
|
+
class WhileLoopUnroller(ast.NodeTransformer):
|
|
183
|
+
def __init__(self, iteratorName: str, iterationsTotal: int) -> None:
|
|
184
|
+
super().__init__()
|
|
185
|
+
self.iteratorName = iteratorName
|
|
186
|
+
self.iterationsTotal = iterationsTotal
|
|
187
|
+
|
|
188
|
+
def visit_While(self, node: ast.While) -> List[ast.stmt]:
|
|
189
|
+
# Check if the while loop's test uses the iterator.
|
|
190
|
+
if isinstance(node.test, ast.Compare) and ifThis.nameIs(self.iteratorName)(node.test.left):
|
|
191
|
+
# Recurse the while loop body and remove AugAssign that increments the iterator.
|
|
192
|
+
cleanBodyStatements: List[ast.stmt] = []
|
|
193
|
+
for loopStatement in node.body:
|
|
194
|
+
# Recursively visit nested statements.
|
|
195
|
+
visitedStatement = self.visit(loopStatement)
|
|
196
|
+
# Remove direct AugAssign: iterator += 1.
|
|
197
|
+
if (isinstance(loopStatement, ast.AugAssign) and
|
|
198
|
+
isinstance(loopStatement.target, ast.Name) and
|
|
199
|
+
loopStatement.target.id == self.iteratorName and
|
|
200
|
+
isinstance(loopStatement.op, ast.Add) and
|
|
201
|
+
isinstance(loopStatement.value, ast.Constant) and
|
|
202
|
+
loopStatement.value.value == 1):
|
|
203
|
+
continue
|
|
204
|
+
cleanBodyStatements.append(visitedStatement)
|
|
205
|
+
|
|
206
|
+
newStatements: List[ast.stmt] = []
|
|
207
|
+
# Unroll using the filtered body.
|
|
208
|
+
for iterationIndex in range(self.iterationsTotal):
|
|
209
|
+
for loopStatement in cleanBodyStatements:
|
|
210
|
+
copiedStatement = copy.deepcopy(loopStatement)
|
|
211
|
+
replacer = ReplaceIterator(self.iteratorName, iterationIndex)
|
|
212
|
+
newStatement = replacer.visit(copiedStatement)
|
|
213
|
+
ast.fix_missing_locations(newStatement)
|
|
214
|
+
newStatements.append(newStatement)
|
|
215
|
+
# Optionally, process the orelse block.
|
|
216
|
+
if node.orelse:
|
|
217
|
+
for elseStmt in node.orelse:
|
|
218
|
+
visitedElse = self.visit(elseStmt)
|
|
219
|
+
if isinstance(visitedElse, list):
|
|
220
|
+
newStatements.extend(visitedElse)
|
|
221
|
+
else:
|
|
222
|
+
newStatements.append(visitedElse)
|
|
223
|
+
return newStatements
|
|
224
|
+
return [cast(ast.stmt, self.generic_visit(node))]
|
|
225
|
+
|
|
226
|
+
newFunctionDef = WhileLoopUnroller(iteratorName, iterationsTotal).visit(FunctionDefTarget)
|
|
227
|
+
ast.fix_missing_locations(newFunctionDef)
|
|
228
|
+
return newFunctionDef
|
|
229
|
+
|
|
230
|
+
def makeLauncherBasicJobNumba(callableTarget: str, pathFilenameFoldsTotal: pathlib.Path) -> ast.Module:
|
|
231
|
+
linesLaunch = f"""
|
|
592
232
|
if __name__ == '__main__':
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
233
|
+
import time
|
|
234
|
+
timeStart = time.perf_counter()
|
|
235
|
+
foldsTotal = {callableTarget}()
|
|
236
|
+
print(foldsTotal, time.perf_counter() - timeStart)
|
|
237
|
+
writeStream = open('{pathFilenameFoldsTotal.as_posix()}', 'w')
|
|
238
|
+
writeStream.write(str(foldsTotal))
|
|
239
|
+
writeStream.close()
|
|
600
240
|
"""
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
def
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
241
|
+
return ast.parse(linesLaunch)
|
|
242
|
+
|
|
243
|
+
def makeAstModuleForOneCallable(pythonSource: str, callableTarget: str, parametersNumba: Optional[ParametersNumba]=None, inlineCallables: Optional[bool]=False , unpackArrays: Optional[bool]=False , allImports: Optional[UniversalImportTracker]=None ) -> str:
|
|
244
|
+
astModule: ast.Module = ast.parse(pythonSource, type_comments=True)
|
|
245
|
+
|
|
246
|
+
if allImports is None:
|
|
247
|
+
allImports = UniversalImportTracker()
|
|
248
|
+
for statement in astModule.body:
|
|
249
|
+
if isinstance(statement, (ast.Import, ast.ImportFrom)):
|
|
250
|
+
allImports.addAst(statement)
|
|
251
|
+
|
|
252
|
+
if inlineCallables:
|
|
253
|
+
dictionaryFunctionDef = {statement.name: statement for statement in astModule.body if isinstance(statement, ast.FunctionDef)}
|
|
254
|
+
callableInlinerWorkhorse = RecursiveInliner(dictionaryFunctionDef)
|
|
255
|
+
FunctionDefTarget = callableInlinerWorkhorse.inlineFunctionBody(callableTarget)
|
|
256
|
+
else:
|
|
257
|
+
FunctionDefTarget = next((node for node in astModule.body if isinstance(node, ast.FunctionDef) and node.name == callableTarget), None)
|
|
258
|
+
if not FunctionDefTarget:
|
|
259
|
+
raise ValueError(f"Could not find function {callableTarget} in source code")
|
|
260
|
+
|
|
261
|
+
ast.fix_missing_locations(FunctionDefTarget)
|
|
262
|
+
|
|
263
|
+
FunctionDefTarget, allImports = decorateCallableWithNumba(FunctionDefTarget, allImports, parametersNumba)
|
|
264
|
+
|
|
265
|
+
# NOTE vestigial hardcoding
|
|
266
|
+
if unpackArrays:
|
|
267
|
+
for tupleUnpack in [(indexMy, 'my'), (indexTrack, 'track')]:
|
|
268
|
+
unpacker = UnpackArrays(*tupleUnpack)
|
|
269
|
+
FunctionDefTarget = cast(ast.FunctionDef, unpacker.visit(FunctionDefTarget))
|
|
270
|
+
ast.fix_missing_locations(FunctionDefTarget)
|
|
271
|
+
|
|
272
|
+
astModule = ast.Module(body=cast(List[ast.stmt], allImports.makeListAst() + [FunctionDefTarget]), type_ignores=[])
|
|
273
|
+
ast.fix_missing_locations(astModule)
|
|
274
|
+
return ast.unparse(astModule)
|
|
275
|
+
|
|
276
|
+
def decorateCallableWithNumba(FunctionDefTarget: ast.FunctionDef, allImports: UniversalImportTracker, parametersNumba: Optional[ParametersNumba]=None) -> Tuple[ast.FunctionDef, UniversalImportTracker]:
|
|
277
|
+
def Z0Z_UnhandledDecorators(astCallable: ast.FunctionDef) -> ast.FunctionDef:
|
|
278
|
+
# TODO: more explicit handling of decorators. I'm able to ignore this because I know `algorithmSource` doesn't have any decorators.
|
|
279
|
+
for decoratorItem in astCallable.decorator_list.copy():
|
|
280
|
+
import warnings
|
|
281
|
+
astCallable.decorator_list.remove(decoratorItem)
|
|
282
|
+
warnings.warn(f"Removed decorator {ast.unparse(decoratorItem)} from {astCallable.name}")
|
|
283
|
+
return astCallable
|
|
284
|
+
|
|
285
|
+
def make_arg4parameter(signatureElement: ast.arg) -> ast.Subscript | None:
|
|
286
|
+
if isinstance(signatureElement.annotation, ast.Subscript) and isinstance(signatureElement.annotation.slice, ast.Tuple):
|
|
287
|
+
annotationShape = signatureElement.annotation.slice.elts[0]
|
|
288
|
+
if isinstance(annotationShape, ast.Subscript) and isinstance(annotationShape.slice, ast.Tuple):
|
|
289
|
+
shapeAsListSlices = [ast.Slice() for axis in range(len(annotationShape.slice.elts))]
|
|
290
|
+
shapeAsListSlices[-1] = ast.Slice(step=ast.Constant(value=1))
|
|
291
|
+
shapeAST = ast.Tuple(elts=list(shapeAsListSlices), ctx=ast.Load())
|
|
292
|
+
else:
|
|
293
|
+
shapeAST = ast.Slice(step=ast.Constant(value=1))
|
|
294
|
+
|
|
295
|
+
annotationDtype = signatureElement.annotation.slice.elts[1]
|
|
296
|
+
if (isinstance(annotationDtype, ast.Subscript) and isinstance(annotationDtype.slice, ast.Attribute)):
|
|
297
|
+
datatypeAST = annotationDtype.slice.attr
|
|
298
|
+
else:
|
|
299
|
+
datatypeAST = None
|
|
300
|
+
|
|
301
|
+
ndarrayName = signatureElement.arg
|
|
302
|
+
Z0Z_hacky_dtype = hackSSOTdatatype(ndarrayName)
|
|
303
|
+
datatype_attr = datatypeAST or Z0Z_hacky_dtype
|
|
304
|
+
allImports.addImportFromStr(datatypeModuleDecorator, datatype_attr)
|
|
305
|
+
datatypeNumba = ast.Name(id=datatype_attr, ctx=ast.Load())
|
|
306
|
+
|
|
307
|
+
return ast.Subscript(value=datatypeNumba, slice=shapeAST, ctx=ast.Load())
|
|
308
|
+
return
|
|
309
|
+
|
|
310
|
+
datatypeModuleDecorator = Z0Z_getDatatypeModuleScalar()
|
|
311
|
+
list_argsDecorator: Sequence[ast.expr] = []
|
|
312
|
+
|
|
313
|
+
list_arg4signature_or_function: List[ast.expr] = []
|
|
314
|
+
for parameter in FunctionDefTarget.args.args:
|
|
315
|
+
signatureElement = make_arg4parameter(parameter)
|
|
316
|
+
if signatureElement:
|
|
317
|
+
list_arg4signature_or_function.append(signatureElement)
|
|
318
|
+
|
|
319
|
+
if FunctionDefTarget.returns and isinstance(FunctionDefTarget.returns, ast.Name):
|
|
320
|
+
theReturn: ast.Name = FunctionDefTarget.returns
|
|
321
|
+
list_argsDecorator = [cast(ast.expr, ast.Call(func=ast.Name(id=theReturn.id, ctx=ast.Load())
|
|
322
|
+
, args=list_arg4signature_or_function if list_arg4signature_or_function else [] , keywords=[] ) )]
|
|
323
|
+
elif list_arg4signature_or_function:
|
|
324
|
+
list_argsDecorator = [cast(ast.expr, ast.Tuple(elts=list_arg4signature_or_function, ctx=ast.Load()))]
|
|
325
|
+
|
|
326
|
+
for decorator in FunctionDefTarget.decorator_list.copy():
|
|
327
|
+
if thisIsAnyNumbaJitDecorator(decorator):
|
|
328
|
+
decorator = cast(ast.Call, decorator)
|
|
329
|
+
if parametersNumba is None:
|
|
330
|
+
parametersNumbaSherpa = Then.copy_astCallKeywords(decorator)
|
|
331
|
+
if (HunterIsSureThereAreBetterWaysToDoThis := True):
|
|
332
|
+
if parametersNumbaSherpa:
|
|
333
|
+
parametersNumba = cast(ParametersNumba, parametersNumbaSherpa)
|
|
334
|
+
FunctionDefTarget.decorator_list.remove(decorator)
|
|
335
|
+
|
|
336
|
+
FunctionDefTarget = Z0Z_UnhandledDecorators(FunctionDefTarget)
|
|
337
|
+
if parametersNumba is None:
|
|
338
|
+
parametersNumba = parametersNumbaDEFAULT
|
|
339
|
+
listDecoratorKeywords = [ast.keyword(arg=parameterName, value=ast.Constant(value=parameterValue)) for parameterName, parameterValue in parametersNumba.items()]
|
|
340
|
+
|
|
341
|
+
decoratorModule = Z0Z_getDatatypeModuleScalar()
|
|
342
|
+
decoratorCallable = Z0Z_getDecoratorCallable()
|
|
343
|
+
allImports.addImportFromStr(decoratorModule, decoratorCallable)
|
|
344
|
+
astDecorator = Then.make_astCall(decoratorCallable, list_argsDecorator, listDecoratorKeywords, None)
|
|
345
|
+
|
|
346
|
+
FunctionDefTarget.decorator_list = [astDecorator]
|
|
347
|
+
return FunctionDefTarget, allImports
|