Skip to content

Commit

Permalink
Clean ugly path os dependant construction
Browse files Browse the repository at this point in the history
  • Loading branch information
VannesteFelix committed Jun 14, 2024
1 parent 80f89a5 commit 69ec8f6
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 59 deletions.
23 changes: 10 additions & 13 deletions python/mor/reduction/container/packageBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@

from mor.utility import utility as u

slash = '/'
if "Windows" in platform.platform():
slash ='\\'


class PackageBuilder():
"""
Expand All @@ -34,9 +30,9 @@ def __init__(self,outputDir,packageName = None ,addToLib = False):
if os.path.isdir(pathToReducedModel+self.packageName) and addToLib:
raise Exception('A Package named %s already exist in the MOR lib !\nPlease choose another name for this new package' % packageName)

self.dataDir = self.outputDir+slash+'data'+slash
self.debugDir = self.outputDir+slash+'debug'+slash
self.meshDir = self.outputDir+slash+'mesh'+slash
self.dataDir = self.outputDir+os.sep+'data'+os.sep
self.debugDir = self.outputDir+os.sep+'debug'+os.sep
self.meshDir = self.outputDir+os.sep+'mesh'+os.sep

def checkNodeNbr(self,modeFileName):
'''
Expand Down Expand Up @@ -86,13 +82,13 @@ def copyAndCleanState(self,results,periodSaveGIE,stateFileName,velocityFileName=


for res in results:
u.copyFileIntoAnother(res["directory"]+slash+"stateFile.state",self.debugDir+stateFileName)
u.copyFileIntoAnother(os.sep.join([res["directory"],"stateFile.state"]),self.debugDir+stateFileName)

if velocityFileName is not None:
u.copyFileIntoAnother(res["directory"]+"/stateFileVelocity.state",self.debugDir+velocityFileName)
if gie:
for fileName in gie :
u.copyFileIntoAnother(res["directory"]+slash+fileName,self.debugDir+fileName)
u.copyFileIntoAnother(os.sep.join([res["directory"],fileName]),self.debugDir+fileName)


self.cleanStateFile(periodSaveGIE,stateFileName)
Expand All @@ -101,9 +97,10 @@ def finalizePackage(self,result):
'''
'''

shutil.move(result['directory']+slash+self.packageName+'.py', self.outputDir+slash+self.packageName+'.py')
shutil.move(os.sep.join([result['directory'],self.packageName+'.py']),
os.sep.join([self.outputDir,self.packageName+'.py']))

with open(result['directory']+slash+'meshFiles.txt', "r") as meshFiles:
with open(os.sep.join([result['directory'],'meshFiles.txt']), "r") as meshFiles:
self.meshes = meshFiles.read().splitlines()

u.checkExistance(self.meshDir)
Expand All @@ -120,7 +117,7 @@ def addToLib(self):
'''
'''

u.copy(self.outputDir, pathToReducedModel+self.packageName+slash)
u.copy(self.outputDir, pathToReducedModel+self.packageName+os.sep)

try:
with open(pathToTemplate+'myInit.txt', "r") as myfile:
Expand All @@ -129,7 +126,7 @@ def addToLib(self):
myInit = myInit.replace('MyReducedModel',self.packageName[0].upper()+self.packageName[1:])
myInit = myInit.replace('myReducedModel',self.packageName)

with open(pathToReducedModel+self.packageName+slash+'__init__.py', "a") as logFile:
with open(pathToReducedModel+os.sep.join([self.packageName,'__init__.py']), "a") as logFile:
logFile.write(myInit)

# print(myInit)
Expand Down
11 changes: 4 additions & 7 deletions python/mor/reduction/container/reductionParam.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# -*- coding: utf-8 -*-

import platform

slash = '/'
if "Windows" in platform.platform():
slash ='\\'
import os


class ReductionParam():
Expand All @@ -20,7 +17,7 @@ def __init__(self,tolModes,tolGIE,addRigidBodyModes,dataDir,saveVelocitySnapshot

self.addRigidBodyModes = addRigidBodyModes
self.dataDir = dataDir
self.dataFolder = slash+dataDir.split(slash)[-2]+slash
self.dataFolder = os.sep+dataDir.split(os.sep)[-2]+os.sep

self.stateFileName = "stateFile.state"
self.modesFileName = "modes.txt"
Expand Down Expand Up @@ -51,7 +48,7 @@ def addParamWrapper(self ,nodeToReduce ,prepareECSW = True, paramForcefield = No
'''
'''
nodeToReduce = "".join(nodeToReduce)
ndtSplit = nodeToReduce.split(slash)
ndtSplit = nodeToReduce.split(os.sep)
nodeToParse = '@./' + ndtSplit[-1]

defaultParamPrepare = {
Expand Down Expand Up @@ -98,7 +95,7 @@ def setFilesName(self):
'''

path , param = self.paramWrapper
nodeName = path.split(slash)[-1]
nodeName = path.split(os.sep)[-1]
self.gieFilesNames.append('HyperReducedFEMForceField_'+nodeName+'_Gie.txt')
self.RIDFilesNames.append('RID_'+nodeName+'.txt')
self.weightsFilesNames.append('weight_'+nodeName+'.txt')
11 changes: 4 additions & 7 deletions python/mor/reduction/reduceModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
from mor.reduction.container import ReductionParam
from mor.reduction import script

slash = '/'
if "Windows" in platform.platform():
slash ='\\'

class ReduceModel():
"""
Expand Down Expand Up @@ -282,7 +279,7 @@ def phase1(self,phasesToExecute=None):
print(" duration: "+str(res["duration"])+" sec")

self.packageBuilder.copyAndCleanState(results,self.reductionParam.periodSaveGIE,self.reductionParam.stateFileName,self.reductionParam.velocityFileName)
u.copy(results[self.phaseToSaveIndex]["directory"]+slash+"debug_scene.py", self.packageBuilder.debugDir)
u.copy(os.sep.join([results[self.phaseToSaveIndex]["directory"],"debug_scene.py"]), self.packageBuilder.debugDir)

print("PHASE 1 --- %s seconds ---" % (time.time() - start_time))

Expand Down Expand Up @@ -379,11 +376,11 @@ 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+"*_Gie.txt")
files = glob.glob(results[self.phaseToSaveIndex]["directory"]+os.sep+"*_Gie.txt")
if files:
for i,file in enumerate(files):
file = os.path.normpath(file)
files[i] = file.split(slash)[-1]
files[i] = file.split(os.sep)[-1]
# print("FILES ----------->",files)
self.reductionParam.gieFilesNames = files
else:
Expand Down Expand Up @@ -436,7 +433,7 @@ def phase4(self,nbrOfModes=None):
if files:
for i,file in enumerate(files):
file = os.path.normpath(file)
files[i] = file.split(slash)[-1]
files[i] = file.split(os.sep)[-1]
self.reductionParam.gieFilesNames = files


Expand Down
6 changes: 1 addition & 5 deletions python/mor/reduction/script/ReadLambdaFilesAndComputeNNMF.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
:code:`python readLambdaFilesAndComputeNNMF.py stateFilename tol modesFilename addRigidBodyModesBOOL`
"""

from sys import argv
import numpy as np
from scipy.linalg import solve
import platform

from sys import argv

slash = '/'
if "Windows" in platform.platform():
slash = "\\"

def readLambdaFilesAndComputeNNMF(lambdaIndicesPath, lambdaValsPath, dim, withFriction, nbOtherConstraints, NNMFfileName, NonZerosCoeffTable, nbModes):# , verbose=False ):

Expand Down
6 changes: 1 addition & 5 deletions python/mor/reduction/script/ReadStateFilesAndComputeModes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

from sys import argv

slash = '/'
if "Windows" in platform.platform():
slash = "\\"


def readStateFilesAndComputeModes(stateFilePath, tol, modesFileName, addRigidBodyModes=None, verbose=False):
print("###################################################")
Expand Down Expand Up @@ -100,7 +96,7 @@ def readStateFilesAndComputeModes(stateFilePath, tol, modesFileName, addRigidBod
sumSVD = np.sum(sSquare)

stateFilePath = os.path.normpath(stateFilePath)
outputDir = slash.join(stateFilePath.split(slash)[:-1]) + slash
outputDir = os.sep.join(stateFilePath.split(os.sep)[:-1]) + os.sep
np.savetxt(outputDir + "Sdata.txt", s)

i = 0
Expand Down
5 changes: 1 addition & 4 deletions python/mor/reduction/template/debug_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
from mor.utility import sceneCreation
from mor.utility import utility as u

slash = '/'
if "Windows" in platform.platform():
slash = "\\"

# Our Original Scene IMPORT
originalScene = r'$ORIGINALSCENE'
originalScene = os.path.normpath(originalScene)
originalScene = u.load_source(originalScene.split(slash)[-1], originalScene)
originalScene = u.load_source(originalScene.split(os.sep)[-1], originalScene)

paramWrapper = $PARAMWRAPPER

Expand Down
7 changes: 2 additions & 5 deletions python/mor/reduction/template/phase1_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
from mor.reduction.container import ObjToAnimate
from mor.utility import sceneCreation

slash = '/'
if "Windows" in platform.platform():
slash = "\\"

# Import animation
#for $file in $LISTANIMATIONTOIMPORT:
fileToImport = os.path.normpath(r'$file')
scene = fileToImport.split(slash)[-1].split('.')[0]
scene = fileToImport.split(os.sep)[-1].split('.')[0]
os.sys.path.insert(0,os.path.dirname(os.path.abspath(fileToImport)))
animationFct = importlib.import_module(scene)

Expand All @@ -42,7 +39,7 @@

# Our Original Scene IMPORT
pathScene = r'$ORIGINALSCENE'
scene = pathScene.split(slash)[-1]
scene = pathScene.split(os.sep)[-1]
spec = spec_from_loader(scene, SourceFileLoader(scene, pathScene))
originalScene = module_from_spec(spec)
spec.loader.exec_module(originalScene)
Expand Down
5 changes: 1 addition & 4 deletions python/mor/reduction/template/phase3_performECSW.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@
from mor.utility import writeScene
from mor.wrapper import replaceAndSave

slash = '/'
if "Windows" in platform.platform():
slash = "\\"

# Our Original Scene IMPORT
originalScene = r'$ORIGINALSCENE'
originalScene = os.path.normpath(originalScene)
originalScene = u.load_source(originalScene.split(slash)[-1], originalScene)
originalScene = u.load_source(originalScene.split(os.sep)[-1], originalScene)

# Scene parameters
nbrOfModes = $NBROFMODES
Expand Down
5 changes: 1 addition & 4 deletions python/mor/utility/template/importScene.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
from mor.utility import graphScene
from mor.utility import utility as u

slash = '/'
if "Windows" in platform.platform():
slash = "\\"

# Our Original Scene IMPORT
originalScene = r'$ORIGINALSCENE'
originalScene = os.path.normpath(originalScene)
originalScene = u.load_source(originalScene.split(slash)[-1], originalScene)
originalScene = u.load_source(originalScene.split(os.sep)[-1], originalScene)


def createScene(rootNode):
Expand Down
7 changes: 2 additions & 5 deletions python/mor/utility/writeScene.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
import sys
import platform

slash = '/'
if "Windows" in platform.platform():
slash ='\\'

path = os.path.dirname(os.path.abspath(__file__))+'/template/'

Expand Down Expand Up @@ -229,9 +226,9 @@ def MyReducedModel(
filesName.append(arg['filename'])

filename = os.path.normpath(arg['filename'])
filename = filename.split(slash)[-1]
filename = filename.split(os.sep)[-1]

arg['filename'] = slash+'mesh'+slash+filename
arg['filename'] = os.sep+'mesh'+os.sep+filename

if 'translation' not in arg:
arg['translation'] = [0.0,0.0,0.0]
Expand Down

0 comments on commit 69ec8f6

Please sign in to comment.