import h5py from dolfin import * import numpy as np from matplotlib import pyplot as plt import scipy as sc import scipy.io as sio from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.mplot3d import proj3d import os, sys import csv from common import inout import time from matplotlib import rc #rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']}) rc('text', usetex=True) def MATmedical(path): datatype = None if 'ODV' in path: datatype = 'odv' print('The DATA file has the next structure: [x,y,z,coil,CardiacPhase,gradient,NSA] where x \ and y are the spatial dimensions for the image. z is one because is only one slice. Coil is \ the number of coils (5), CardiacPhase is the time dimension. Gradient is the number of gradients used in the \ adquisition (2). NSA is the number of signal average.') if 'Phantom' in path: datatype = 'cib_phantom' #print('CIB phantom data which contains magnitude and velocity measurements.') if not datatype=='cib_phantom': STRUC = sio.loadmat(path) DAT0 = STRUC['data'] if datatype=='odv': KDAT0 = STRUC['kdata'] [row,col,coil,numt2,p4,p5] = DAT0.shape else: KDAT0 = STRUC['k_data'] [row,col,slicesel,coil,numt2,p4,p5] = DAT0.shape z0 = 0 NSA = 0 coilnum = 3 DAT1 = np.zeros([row,col,numt2],dtype=complex) DAT2 = np.zeros([row,col,numt2],dtype=complex) KDAT1 = np.zeros([row,col,numt2],dtype=complex) KDAT2 = np.zeros([row,col,numt2],dtype=complex) if datatype=='odv': for k in range(numt2): DAT1[:,:,k] = DAT0[:,:,coilnum,k,0,NSA] DAT2[:,:,k] = DAT0[:,:,coilnum,k,1,NSA] KDAT1[:,:,k] = KDAT0[:,:,coilnum,k,0,NSA] KDAT2[:,:,k] = KDAT0[:,:,coilnum,k,1,NSA] return [DAT1,DAT2,KDAT1,KDAT2] else: UK1 = np.zeros([row,col,numt2],dtype=complex) UK2 = np.zeros([row,col,numt2],dtype=complex) for k in range(numt2): DAT1[:,:,k] = DAT0[:,:,z0,coilnum,k,0,NSA] DAT2[:,:,k] = DAT0[:,:,z0,coilnum,k,1,NSA] KDAT1[:,:,k] = KDAT0[:,:,z0,coilnum,k,0,NSA] KDAT2[:,:,k] = KDAT0[:,:,z0,coilnum,k,1,NSA] UK1[:,:,k] = np.fft.fftshift(np.fft.fft2(DAT1[:,:,k])) UK2[:,:,k] = np.fft.fftshift(np.fft.fft2(DAT2[:,:,k])) return [DAT1,DAT2,KDAT1,KDAT2] elif datatype=='cib_phantom': lp = len(path) for k in range(lp): if path[lp-1-k]=='/': name_var = path[lp-k:lp] break name_var = name_var.replace('.mat','') if name_var in ['VENC','Segmented_Aorta','voxel_MR']: STRUC = sio.loadmat(path) return STRUC[name_var] else: #import h5py arrays = {} f = h5py.File(path) for k, v in f.items(): arrays[k] = np.array(v) return arrays[name_var].transpose((1,2,3,0)) def Plot_Inlet(): ''' To Show the flow given at the inlet in the FEM simulation ''' Tf = 0.9 dt = 0.0005 t = np.linspace(0,Tf,Tf/dt) U = 60 Th = 0.37 DOLFIN_PI = 3.1416 y = U*np.sin(DOLFIN_PI*t/Th)*(t<=Th) + (Th0: plt.ylim(ywin) plt.xlim(xwin) ax.set_aspect('auto') plt.xticks([]) plt.yticks([]) #plt.xlabel('$x$',Fontsize=20) #plt.ylabel('$y$',Fontsize=20) plt.title('phase ' + str(k)) plt.show() if recorder: plt.savefig('results/pictures/frame'+str(k),bbox_inches='tight') plt.pause(0.101) plt.clf() def PrintSequence2(A,X,Y,Z,x,y,z): plt.ion() fig = plt.figure(figsize=(7, 7), dpi=100) ax = fig.add_subplot(1,1,1) [row,col,dep] = A.shape xi = np.linspace(np.min(x),np.max(x),row) yi = np.linspace(np.min(y),np.max(y),col) yi2 = np.min(yi) + np.max(yi) - yi from matplotlib.colors import LinearSegmentedColormap basic_cols = ['#ff4e2b', '#000000', '#1893db'] my_cmap = LinearSegmentedColormap.from_list('mycmap', basic_cols) ccmap = mpl.cm.Spectral xt = x yt = y zt = z yt2 = (np.min(y) + np.max(y)) - y Dz = (np.max(z) - np.min(z))/dep def shiftedColorMap(cmap, start=0, midpoint=0.5, stop=1.0, name='shifted'): ''' Function to offset the "center" of a colormap. Useful for data with a negative min and positive max and you want the middle of the colormap's dynamic range to be at zero. Input ----- cmap : The matplotlib colormap to be altered start : Offset from lowest point in the colormap's range. Defaults to 0.0 (no lower offset). Should be between 0.0 and `midpoint`. midpoint : The new center of the colormap. Defaults to 0.5 (no shift). Should be between 0.0 and 1.0. In general, this should be 1 - vmax / (vmax + abs(vmin)) For example if your data range from -15.0 to +5.0 and you want the center of the colormap at 0.0, `midpoint` should be set to 1 - 5/(5 + 15)) or 0.75 stop : Offset from highest point in the colormap's range. Defaults to 1.0 (no upper offset). Should be between `midpoint` and 1.0. ''' cdict = { 'red': [], 'green': [], 'blue': [], 'alpha': [] } # regular index to compute the colors reg_index = np.linspace(start, stop, 257) # shifted index to match the data shift_index = np.hstack([ np.linspace(0.0, midpoint, 128, endpoint=False), np.linspace(midpoint, 1.0, 129, endpoint=True) ]) for ri, si in zip(reg_index, shift_index): r, g, b, a = cmap(ri) cdict['red'].append((si, r, r)) cdict['green'].append((si, g, g)) cdict['blue'].append((si, b, b)) cdict['alpha'].append((si, a, a)) newcmap = mpl.colors.LinearSegmentedColormap(name, cdict) plt.register_cmap(cmap=newcmap) return newcmap ccmap = mpl.cm.Spectral shifted_ccmap = shiftedColorMap(ccmap, midpoint=0.75, name='shifted') for k in range(dep): zmin = np.min(z) + k*Dz zmax = zmin + Dz xs = np.array([]) ys = np.array([]) zs = np.array([]) for j in range(z.size): if z[j]>= zmin and z[j] 1: if os.path.exists(sys.argv[1]): inputfile = sys.argv[1] print('Found input file ' + inputfile) else: raise Exception('Command line arg given but input file does not exist:' ' {}'.format(sys.argv[1])) else: raise Exception('An input file is required as argument!') start_time = time.time() options = inout.read_parameters(inputfile) ROUTINE(options) end_time = time.time() CLOCK(start_time,end_time) # END