Skip to content

Commit 80f89a5

Browse files
Fix imp import (#131)
* Replace deprecated imp module with importlib * rm old mechanism of elements saving
1 parent 143c5d1 commit 80f89a5

File tree

9 files changed

+38
-50
lines changed

9 files changed

+38
-50
lines changed

python/mor/reduction/container/reductionParam.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,4 @@ def setFilesName(self):
101101
nodeName = path.split(slash)[-1]
102102
self.gieFilesNames.append('HyperReducedFEMForceField_'+nodeName+'_Gie.txt')
103103
self.RIDFilesNames.append('RID_'+nodeName+'.txt')
104-
self.weightsFilesNames.append('weight_'+nodeName+'.txt')
105-
self.savedElementsFilesNames.append('elmts_'+nodeName+'.txt')
104+
self.weightsFilesNames.append('weight_'+nodeName+'.txt')

python/mor/reduction/reduceModel.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -379,17 +379,6 @@ def phase3(self,phasesToExecute=None,nbrOfModes=None):
379379
print(" scene: "+res["scene"])
380380
print(" duration: "+str(res["duration"])+" sec")
381381

382-
files = glob.glob(results[self.phaseToSaveIndex]["directory"]+slash+"*_elmts.txt")
383-
if files:
384-
for i,file in enumerate(files):
385-
file = os.path.normpath(file)
386-
files[i] = file.split(slash)[-1]
387-
# print("FILES ----------->",files)
388-
self.reductionParam.savedElementsFilesNames = files
389-
390-
for fileName in self.reductionParam.savedElementsFilesNames :
391-
u.copyFileIntoAnother(results[self.phaseToSaveIndex]["directory"]+slash+fileName,self.packageBuilder.debugDir+fileName)
392-
393382
files = glob.glob(results[self.phaseToSaveIndex]["directory"]+slash+"*_Gie.txt")
394383
if files:
395384
for i,file in enumerate(files):
@@ -442,32 +431,15 @@ def phase4(self,nbrOfModes=None):
442431
raise ValueError("nbrOfModes incorrect\n"\
443432
+" nbrOfModes given :"+str(nbrOfModes)+" | nbrOfModes max possible : "+str(nbrOfModesPossible))
444433

445-
# print(files)
446-
files = glob.glob(self.packageBuilder.debugDir+"*_elmts.txt")
447-
if files:
448-
for i,file in enumerate(files):
449-
file = os.path.normpath(file)
450-
files[i] = file.split(slash)[-1]
451-
# print("FILES ----------->",files)
452-
self.reductionParam.savedElementsFilesNames = files
453434

454435
files = glob.glob(self.packageBuilder.debugDir+"*_Gie.txt")
455436
if files:
456437
for i,file in enumerate(files):
457438
file = os.path.normpath(file)
458439
files[i] = file.split(slash)[-1]
459-
# print("FILES ----------->",files)
460440
self.reductionParam.gieFilesNames = files
461441

462442

463-
for i , gie in enumerate(self.reductionParam.gieFilesNames):
464-
tmp = gie.replace('_Gie.txt','')
465-
for j , elmts in enumerate(self.reductionParam.savedElementsFilesNames):
466-
if tmp in elmts:
467-
tmp = self.reductionParam.savedElementsFilesNames[j]
468-
self.reductionParam.savedElementsFilesNames[j] = self.reductionParam.savedElementsFilesNames[i]
469-
self.reductionParam.savedElementsFilesNames[i] = tmp
470-
471443
self.reductionParam.RIDFilesNames = []
472444
self.reductionParam.weightsFilesNames = []
473445
for fileName in self.reductionParam.gieFilesNames :

python/mor/reduction/script/ReadGieFileAndComputeRIDandWeights.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def selectECSW(G,b,tau,verbose):
8787
if verbose :
8888
print ("Final Error: ", errDif(G,xi,b) ," Target Error: ", valTarget)
8989
else :
90-
u.update_progress( 1 )
90+
util.update_progress( 1 )
9191

9292
return (ECSWindex,xi)
9393

python/mor/reduction/template/debug_scene.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
import os
3-
import imp
43
import platform
54
from sys import argv
65

@@ -12,7 +11,8 @@
1211
+"Please install it : https://github.com/SofaDefrost/STLIB")
1312

1413
# MOR IMPORT
15-
from mor.utility import sceneCreation as u
14+
from mor.utility import sceneCreation
15+
from mor.utility import utility as u
1616

1717
slash = '/'
1818
if "Windows" in platform.platform():
@@ -21,7 +21,7 @@
2121
# Our Original Scene IMPORT
2222
originalScene = r'$ORIGINALSCENE'
2323
originalScene = os.path.normpath(originalScene)
24-
originalScene = imp.load_source(originalScene.split(slash)[-1], originalScene)
24+
originalScene = u.load_source(originalScene.split(slash)[-1], originalScene)
2525

2626
paramWrapper = $PARAMWRAPPER
2727

@@ -36,4 +36,4 @@ def createScene(rootNode):
3636
path , param = paramWrapper
3737
pathToNode = path[1:]
3838

39-
u.createDebug(rootNode,pathToNode,stateFileName)
39+
sceneCreation.createDebug(rootNode,pathToNode,stateFileName)

python/mor/reduction/template/phase1_snapshots.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
# MOR IMPORT
1818
from mor.reduction.container import ObjToAnimate
19-
from mor.utility import sceneCreation as u
19+
from mor.utility import sceneCreation
2020

2121
slash = '/'
2222
if "Windows" in platform.platform():
@@ -84,7 +84,7 @@ def createScene(rootNode):
8484
# Now that we have the AnimationManager & a list of the nodes we want to animate
8585
# we can add an animation to then according to the arguments in listObjToAnimate
8686

87-
u.addAnimation(rootNode,phase,timeExe,dt,listObjToAnimate)
87+
sceneCreation.addAnimation(rootNode,phase,timeExe,dt,listObjToAnimate)
8888

8989
# Now that all the animations are defined we need to record their results
9090
# for that we take the parent node normally given as an argument in paramWrapper

python/mor/reduction/template/phase2_prepareECSW.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
+"Please install it : https://github.com/SofaDefrost/STLIB")
1414

1515
# MOR IMPORT
16-
from mor.utility import sceneCreation as u
16+
from mor.utility import sceneCreation
1717
from mor.wrapper import replaceAndSave
1818

1919
# Our Phase1 Scene IMPORT
@@ -50,7 +50,7 @@ def createScene(rootNode):
5050
# Modify the scene to perform hyper-reduction according
5151
# to the informations collected by the wrapper
5252

53-
u.modifyGraphScene(rootNode,nbrOfModes,paramWrapper)
53+
sceneCreation.modifyGraphScene(rootNode,nbrOfModes,paramWrapper)
5454

5555

5656
# We Update the link

python/mor/reduction/template/phase3_performECSW.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import os
33
import sys
4-
import imp
54
import platform
65

76
# STLIB IMPORT
@@ -12,7 +11,8 @@
1211
+"Please install it : https://github.com/SofaDefrost/STLIB")
1312

1413
# MOR IMPORT
15-
from mor.utility import sceneCreation as u
14+
from mor.utility import sceneCreation
15+
from mor.utility import utility as u
1616
from mor.utility import writeScene
1717
from mor.wrapper import replaceAndSave
1818

@@ -23,7 +23,7 @@
2323
# Our Original Scene IMPORT
2424
originalScene = r'$ORIGINALSCENE'
2525
originalScene = os.path.normpath(originalScene)
26-
originalScene = imp.load_source(originalScene.split(slash)[-1], originalScene)
26+
originalScene = u.load_source(originalScene.split(slash)[-1], originalScene)
2727

2828
# Scene parameters
2929
nbrOfModes = $NBROFMODES
@@ -52,16 +52,16 @@ def createScene(rootNode):
5252
originalScene.createScene(Wrapper(rootNode, replaceAndSave.MORreplace, paramWrapper)) # 1
5353

5454
# Add MOR plugin if not found
55-
u.addPlugin(rootNode,"ModelOrderReduction")
55+
sceneCreation.addPlugin(rootNode,"ModelOrderReduction")
5656
pluginName = []
57-
plugins = u.searchObjectClassInGraphScene(rootNode,"RequiredPlugin")
57+
plugins = sceneCreation.searchObjectClassInGraphScene(rootNode,"RequiredPlugin")
5858
for plugin in plugins:
5959
pluginName.append(plugin.pluginName)
6060

6161
# Modify the scene to perform hyper-reduction according
6262
# to the informations collected by the wrapper
6363

64-
u.modifyGraphScene(rootNode,nbrOfModes,paramWrapper)
64+
sceneCreation.modifyGraphScene(rootNode,nbrOfModes,paramWrapper)
6565

6666
# We collect all the informations during 1 & 2 to be able to create with
6767
# writeGraphScene a SOFA scene containing only our reduced model that we can instanciate
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# -*- coding: utf-8 -*-
22
import os
3-
import imp
43
import platform
54

65
# MOR IMPORT
7-
from mor.utility import graphScene as u
6+
from mor.utility import graphScene
7+
from mor.utility import utility as u
88

99
slash = '/'
1010
if "Windows" in platform.platform():
@@ -13,12 +13,11 @@
1313
# Our Original Scene IMPORT
1414
originalScene = r'$ORIGINALSCENE'
1515
originalScene = os.path.normpath(originalScene)
16-
originalScene = imp.load_source(originalScene.split(slash)[-1], originalScene)
16+
originalScene = u.load_source(originalScene.split(slash)[-1], originalScene)
1717

18-
###############################################################################
1918

2019
def createScene(rootNode):
2120
# Import Original scene
2221
originalScene.createScene(rootNode)
2322

24-
u.dumpGraphScene(rootNode)
23+
graphScene.dumpGraphScene(rootNode)

python/mor/utility/utility.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@
3232
import time
3333

3434

35+
import importlib.util
36+
import importlib.machinery
37+
38+
def load_source(modname, filename):
39+
'''
40+
Fix of module import due to python update
41+
from using imp to importlib
42+
Not same API, waiting for update, using this fix meanwhile
43+
'''
44+
loader = importlib.machinery.SourceFileLoader(modname, filename)
45+
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
46+
module = importlib.util.module_from_spec(spec)
47+
# The module is always executed and not cached in sys.modules.
48+
# Uncomment the following line to cache the module.
49+
# sys.modules[module.__name__] = module
50+
loader.exec_module(module)
51+
return module
52+
3553
def update_progress(progress):
3654
barLength = 50 # Modify this to change the length of the progress bar
3755
status = "Compute Weight&RID"

0 commit comments

Comments
 (0)