Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix imp import #131

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions python/mor/reduction/container/reductionParam.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,4 @@ def setFilesName(self):
nodeName = path.split(slash)[-1]
self.gieFilesNames.append('HyperReducedFEMForceField_'+nodeName+'_Gie.txt')
self.RIDFilesNames.append('RID_'+nodeName+'.txt')
self.weightsFilesNames.append('weight_'+nodeName+'.txt')
self.savedElementsFilesNames.append('elmts_'+nodeName+'.txt')
self.weightsFilesNames.append('weight_'+nodeName+'.txt')
28 changes: 0 additions & 28 deletions python/mor/reduction/reduceModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,17 +379,6 @@ def phase3(self,phasesToExecute=None,nbrOfModes=None):
print(" scene: "+res["scene"])
print(" duration: "+str(res["duration"])+" sec")

files = glob.glob(results[self.phaseToSaveIndex]["directory"]+slash+"*_elmts.txt")
if files:
for i,file in enumerate(files):
file = os.path.normpath(file)
files[i] = file.split(slash)[-1]
# print("FILES ----------->",files)
self.reductionParam.savedElementsFilesNames = files

for fileName in self.reductionParam.savedElementsFilesNames :
u.copyFileIntoAnother(results[self.phaseToSaveIndex]["directory"]+slash+fileName,self.packageBuilder.debugDir+fileName)

files = glob.glob(results[self.phaseToSaveIndex]["directory"]+slash+"*_Gie.txt")
if files:
for i,file in enumerate(files):
Expand Down Expand Up @@ -442,32 +431,15 @@ def phase4(self,nbrOfModes=None):
raise ValueError("nbrOfModes incorrect\n"\
+" nbrOfModes given :"+str(nbrOfModes)+" | nbrOfModes max possible : "+str(nbrOfModesPossible))

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

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


for i , gie in enumerate(self.reductionParam.gieFilesNames):
tmp = gie.replace('_Gie.txt','')
for j , elmts in enumerate(self.reductionParam.savedElementsFilesNames):
if tmp in elmts:
tmp = self.reductionParam.savedElementsFilesNames[j]
self.reductionParam.savedElementsFilesNames[j] = self.reductionParam.savedElementsFilesNames[i]
self.reductionParam.savedElementsFilesNames[i] = tmp

self.reductionParam.RIDFilesNames = []
self.reductionParam.weightsFilesNames = []
for fileName in self.reductionParam.gieFilesNames :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def selectECSW(G,b,tau,verbose):
if verbose :
print ("Final Error: ", errDif(G,xi,b) ," Target Error: ", valTarget)
else :
u.update_progress( 1 )
util.update_progress( 1 )

return (ECSWindex,xi)

Expand Down
8 changes: 4 additions & 4 deletions python/mor/reduction/template/debug_scene.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
import os
import imp
import platform
from sys import argv

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

# MOR IMPORT
from mor.utility import sceneCreation as u
from mor.utility import sceneCreation
from mor.utility import utility as u

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

paramWrapper = $PARAMWRAPPER

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

u.createDebug(rootNode,pathToNode,stateFileName)
sceneCreation.createDebug(rootNode,pathToNode,stateFileName)
4 changes: 2 additions & 2 deletions python/mor/reduction/template/phase1_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# MOR IMPORT
from mor.reduction.container import ObjToAnimate
from mor.utility import sceneCreation as u
from mor.utility import sceneCreation

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

u.addAnimation(rootNode,phase,timeExe,dt,listObjToAnimate)
sceneCreation.addAnimation(rootNode,phase,timeExe,dt,listObjToAnimate)

# Now that all the animations are defined we need to record their results
# for that we take the parent node normally given as an argument in paramWrapper
Expand Down
4 changes: 2 additions & 2 deletions python/mor/reduction/template/phase2_prepareECSW.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
+"Please install it : https://github.com/SofaDefrost/STLIB")

# MOR IMPORT
from mor.utility import sceneCreation as u
from mor.utility import sceneCreation
from mor.wrapper import replaceAndSave

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

u.modifyGraphScene(rootNode,nbrOfModes,paramWrapper)
sceneCreation.modifyGraphScene(rootNode,nbrOfModes,paramWrapper)


# We Update the link
Expand Down
12 changes: 6 additions & 6 deletions python/mor/reduction/template/phase3_performECSW.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
import os
import sys
import imp
import platform

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

# MOR IMPORT
from mor.utility import sceneCreation as u
from mor.utility import sceneCreation
from mor.utility import utility as u
from mor.utility import writeScene
from mor.wrapper import replaceAndSave

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

# Scene parameters
nbrOfModes = $NBROFMODES
Expand Down Expand Up @@ -52,16 +52,16 @@ def createScene(rootNode):
originalScene.createScene(Wrapper(rootNode, replaceAndSave.MORreplace, paramWrapper)) # 1

# Add MOR plugin if not found
u.addPlugin(rootNode,"ModelOrderReduction")
sceneCreation.addPlugin(rootNode,"ModelOrderReduction")
pluginName = []
plugins = u.searchObjectClassInGraphScene(rootNode,"RequiredPlugin")
plugins = sceneCreation.searchObjectClassInGraphScene(rootNode,"RequiredPlugin")
for plugin in plugins:
pluginName.append(plugin.pluginName)

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

u.modifyGraphScene(rootNode,nbrOfModes,paramWrapper)
sceneCreation.modifyGraphScene(rootNode,nbrOfModes,paramWrapper)

# We collect all the informations during 1 & 2 to be able to create with
# writeGraphScene a SOFA scene containing only our reduced model that we can instanciate
Expand Down
9 changes: 4 additions & 5 deletions python/mor/utility/template/importScene.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
import os
import imp
import platform

# MOR IMPORT
from mor.utility import graphScene as u
from mor.utility import graphScene
from mor.utility import utility as u

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

###############################################################################

def createScene(rootNode):
# Import Original scene
originalScene.createScene(rootNode)

u.dumpGraphScene(rootNode)
graphScene.dumpGraphScene(rootNode)
18 changes: 18 additions & 0 deletions python/mor/utility/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@
import time


import importlib.util
import importlib.machinery

def load_source(modname, filename):
'''
Fix of module import due to python update
from using imp to importlib
Not same API, waiting for update, using this fix meanwhile
'''
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module(module)
return module

def update_progress(progress):
barLength = 50 # Modify this to change the length of the progress bar
status = "Compute Weight&RID"
Expand Down
Loading