def VGPy(inputData, saveKey, append=False): """ saves a python FEM_VGPy object instance (or list/tuple of FEM_VGPy object instances) to a MATLAB database input: inputData = single or list/tuple of VGPy objects saveKey = string name to save the MATLAB structure append = optional logical flag (default = False) to indicate whether the data should be appended to the (already existing) .MAT file. Meaningless if the file doesn't already exist. """ # remove .MAT from saveKey if it was specified if saveKey.endswith('.mat'): saveKey = saveKey[:-4] # get CWD wd = os.getcwd() # change to save dir os.chdir( myPaths.saveResults() ) # start matlab engine eng = matlab.engine.start_matlab() if (type(inputData) is list) or (type(inputData) is tuple): #if we have a list or tuple #then assume the elements are VGIPy classes #initialize saveData dict which will be exported saveData = {} for instance in inputData: #save instance dicts to saveData dict #we want the dict to have matlab dtypes saveData[instance.name] = _convert_dict_dtypes( instance.__dict__ ) # now, convert dict to struct saveStruct = eng.struct(saveData) # transport struct to matlab workspace eng.workspace[saveKey] = saveStruct else: #assume input is a VGIPy class or otherwise has a name attribute saveData = {} try: #this works if inputData is an instance saveData[inputData.name] = _convert_dict_dtypes( inputData.__dict__ ) except: #this works if inputData is an instance.__dict__ saveData[inputData["name"]] = _convert_dict_dtypes( inputData ) # now, convert dict to struct saveStruct = eng.struct(saveData) # transport struct to matlab workspace eng.workspace[saveKey] = saveStruct # save the transported struct if append: eng.save(saveKey + '.mat','-struct',saveKey,'-append',nargout=0) else: eng.save(saveKey + '.mat','-struct',saveKey,nargout=0) # exit matlab engine eng.quit() # return to previous dir os.chdir( wd ) # alert user print "MATLAB Binary Database saved to: " + myPaths.saveResults() return
def VGPy(cls, inputData, saveKey, append=False, saveDir=None, ml_eng=None): """ saves a python FEM_VGPy object instance (or list/tuple of FEM_VGPy object instances) to a MATLAB database input: inputData = single or list/tuple of VGPy objects saveKey = string var & file name to save the MATLAB structure append = optional logical flag (default = False) to indicate whether the data should be appended to the (already existing) .MAT file. Meaningless if the file doesn't already exist. saveDir = optional string directory to save database. default = C:\Temp\VGPy_Databases ml_eng = optional MATLAB Python Engine handle. If not provided, a new one will be created. """ # remove .MAT from saveKey if it was specified if saveKey.endswith('.mat'): saveKey = saveKey[:-4] # ensure saveKey is a valid and "safe" name to use saveKey = cls._safe_keyname(saveKey) # get CWD fundir = os.getcwd() # set saveDir if not defined if saveDir is None: saveDir = myPaths.saveResults() # cd to save dir os.chdir( saveDir ) # start matlab engine if one is not passed in if (ml_eng is None) or (not type(ml_eng) is matlab.engine.matlabengine.MatlabEngine): eng = matlab.engine.start_matlab() ml_eng = None # require ml_eng to be None in this case else: eng = ml_eng if (type(inputData) is list) or (type(inputData) is tuple): # if we have a list or tuple # then assume the elements are VGIPy classes # initialize saveData dict which will be exported saveData = {} for instance in inputData: # save instance dicts to saveData dict # we want the dict to have matlab dtypes saveData[instance.name] = cls._convert_dict_dtypes( instance.__dict__ ) # now, convert dict to struct saveStruct = eng.struct(saveData) # transport struct to matlab workspace eng.workspace[saveKey] = saveStruct else: # assume input is a VGIPy class or otherwise has a name attribute saveData = {} try: # this works if inputData is an instance saveData[inputData.name] = cls._convert_dict_dtypes( inputData.__dict__ ) except: # this works if inputData is an instance.__dict__ saveData[inputData["name"]] = cls._convert_dict_dtypes( inputData ) # now, convert dict to struct saveStruct = eng.struct(saveData) # transport struct to matlab workspace eng.workspace[saveKey] = saveStruct # save the transported struct if append: eng.save(saveKey + '.mat','-struct',saveKey,'-append',nargout=0) else: eng.save(saveKey + '.mat','-struct',saveKey,nargout=0) # exit matlab engine if one was created if ml_eng is None: eng.quit() # return to previous dir os.chdir( fundir ) # alert user print 'MATLAB Binary Database "' + saveKey + '.mat" saved to: ' + saveDir return