def render_simple_plot(name, vars, title, legloc, ylabel, ncols=1, ymin=None, ymax=None): import matplotlib.pyplot as plt import math xlocales.set_matplotlib() # Location of results (relative to extention directory) resdir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..", "results")) var0name = vars[0]["name"] fn = os.path.join(resdir, name+"_res.mat") res = DyMatFile(fn) fig, ax = plt.subplots() # Set background to be transparent #fig.patch.set_facecolor('none') #fig.patch.set_alpha(0.0); #ax.patch.set_facecolor('none') #ax.patch.set_alpha(0.0); try: t = res.abscissa(var0name, valuesOnly=True) print "len(t) = "+str(len(t)) except KeyError as e: raise NameError("Unknown key: "+var0name+" among "+str(res.names())) for var in vars: varname = var["name"] scale = var["scale"] legend = var["legend"] style = var["style"] try: x = res.data(varname) except KeyError as e: raise NameError("Unknown key: "+varname+" among "+str(res.names())) if len(x)==2: xv = x[0] print "xv = "+str(xv) x = [xv]*len(t) x = map(lambda y: y*scale, x) print "len("+varname+") = "+str(len(x)) ax.plot(t, x, style, label=legend) #legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) legend = ax.legend(shadow=True, ncol=ncols, loc=legloc) if title==None: title = name.replace("_", ".") plt.title(title) plt.ylabel(ylabel) plt.xlabel(_('Time [s]')) if ymax!=None: plt.axis(ymax=ymax) if ymin!=None: plt.axis(ymin=ymin) plt.show()
def render_simple_plot(name, vars, title, legloc, ylabel, ncols=1, ymax=None): import matplotlib.pyplot as plt import math # Location of results (relative to extention directory) resdir = os.path.abspath( os.path.join(os.path.dirname(__file__), "..", "..", "..", "results")) var0name = vars[0]["name"] fn = os.path.join(resdir, name + "_res.mat") res = DyMatFile(fn) fig, ax = plt.subplots() # Set background to be transparent #fig.patch.set_facecolor('none') #fig.patch.set_alpha(0.0); #ax.patch.set_facecolor('none') #ax.patch.set_alpha(0.0); try: t = res.abscissa(var0name, valuesOnly=True) print "len(t) = " + str(len(t)) except KeyError as e: raise NameError("Unknown key: " + var0name + " among " + str(res.names())) for var in vars: varname = var["name"] scale = var["scale"] legend = var["legend"] style = var["style"] try: x = res.data(varname) except KeyError as e: raise NameError("Unknown key: " + varname + " among " + str(res.names())) if len(x) == 2: xv = x[0] print "xv = " + str(xv) x = [xv] * len(t) x = map(lambda y: y * scale, x) print "len(" + varname + ") = " + str(len(x)) ax.plot(t, x, style, label=legend) #legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) legend = ax.legend(shadow=True, ncol=ncols, loc=legloc) if title == None: title = name.replace("_", ".") plt.title(title) plt.ylabel(ylabel) plt.xlabel('Time [s]') if ymax != None: plt.axis(ymax=ymax) plt.show()
def dsres2meld(df, mfp, verbose=False, compression=True, single=True): """ This function reads in a file in 'dsres' format and then writes it back out in meld format. Note there is a dependency in this code on numpy and dymat. """ import numpy from dymat import DyMatFile # Read dsres file mf = DyMatFile(df) # Open a meld file to write to meld = MeldWriter(mfp, compression=compression, single=single) # Initialize a couple of internal data structures tables = {} signal_map = {} alias_map = {} # This is the key to use for "description" fields DESC = "desc" # We loop over the blocks in the dsres file and each block # will end up being a table. for block in mf.blocks(): # Extract the abscissa data for this block if verbose: print "Block: "+str(block) (abscissa, aname, adesc) = mf.abscissa(block) # Determine all columns in the dsres file and associate # the signals and aliases with these columns columns = {} if verbose: print "Abscissa: "+str(aname)+" '"+adesc+"'" signals = [] aliases = [] # Loop over all variables in this block and either make them # signals or aliases (if some other variable has already # claimed that column) for name in mf.names(block): col = mf._vars[name][2] if col in columns: if verbose: print " Alias "+name+" (of: "+columns[col]+")" aliases.append((name, mf._vars[name][0], columns[col], mf._vars[name][3])) else: if verbose: print " Signal "+name+" ("+str(col)+")" columns[col] = name signals.append(name) if verbose: print "Number of columns: "+str(len(columns.keys())) print "Number of signals: "+str(len(signals)) print "Number of aliases: "+str(len(aliases)) signal_map[block] = signals alias_map[block] = aliases # Generate table for this block (and store it away for later) tables[block] = meld.add_table("T"+str(block)) # Add abscissa tables[block].add_signal(aname, metadata={DESC: adesc}) for signal in signals: tables[block].add_signal(signal, metadata={DESC: mf.description(signal)}) # Add aliases (and their metadata) for alias in aliases: transform = None if alias[3]<0.0: tables[block].add_alias(alias=alias[0], of=alias[2], transform="aff(-1,0)", metadata={DESC:mf.description(alias[0])}) else: tables[block].add_alias(alias=alias[0], of=alias[2], metadata={DESC:mf.description(alias[0])}) # Finalize structure meld.finalize() # Now loop again, this time with the intention to write data for block in mf.blocks(): # Write abscissa for this block (abscissa, aname, adesc) = mf.abscissa(block) abscissa = list(abscissa.astype(numpy.float)) tables[block].write(aname, list(abscissa)) signals = signal_map[block] print "Block: "+str(block) print " Writing signals: "+str(signals) # Then write signals (no need to write aliases) for signal in signals: vec = list(mf.data(signal).astype(numpy.float)) tables[block].write(signal, vec) # Close the MeldWriter meld.close()