示例#1
0
def TransitionPopulations(angle, temp, energy, X, QT, TQ, CT, CQ, TC=[], QC=[], Title='', smflag=1, pltflag=1, nu=49, hlrn=0, numpltflag=0):
    plt.clf()
    plt.cla()
    TIMESTEPS = len(QT)
    l = len(QT)
    DT = 1
    # fig, ax = plt.subplots(num='a'+angle+'t'+temp+'e'+energy)
    if hlrn == 1:
        name = 'a'+angle+'t'+temp+'e'+energy.split('.')[0]+'HLRN'
    else:
        name = 'a'+angle+'t'+temp+'e'+energy.split('.')[0]
    if numpltflag == 1:
        plt.plot(X*2.5e-2, QT, 'b--', label = 'QT')
        plt.plot(X*2.5e-2, TQ, 'C1--', label = 'TQ')
        plt.plot(X*2.5e-2, CT, 'g--', label = 'CT')
        plt.plot(X*2.5e-2, CQ, 'r--', label = 'CQ')
    if smflag == 1:
        start = int(5 * l/30)
        Edge = np.arange(-start-5,l-start-5,1)
        print("Smoothing Transition Populations")
        ctr = 0
        # sf = scipy.signal.savgol_filter
        QT[:] = sf(QT, nu, 3, deriv=0)
        TQ[:] = sf(TQ, nu, 3, deriv=0)
        CT[:] = sf(CT, nu, 3, deriv=0)
        CQ[:] = sf(CQ, nu, 3, deriv=0)
        '''
        for k in range(0,TIMESTEPS):# - int(0.01*l)):
            if k % (TIMESTEPS / 10) == 0:
                print(angle, temp, '\t'+str(ctr)+' %')
                ctr += 10

            QT[k] = smooth.GaussSmoothing(TIMESTEPS, k, QT, dt=DT, nu=nu, edge='', X=Edge)
            TQ[k] = smooth.GaussSmoothing(TIMESTEPS, k, TQ, dt=DT, nu=nu, edge='', X=Edge)
            CT[k] = smooth.GaussSmoothing(TIMESTEPS, k, CT, dt=DT, nu=nu, edge='', X=Edge)
            CQ[k] = smooth.GaussSmoothing(TIMESTEPS, k, CQ, dt=DT, nu=nu, edge='', X=Edge)
        print(angle, temp, '\t100 %')
        '''
    # plot the results
    if pltflag == 1:
        print("Plot Transition Populations")
        #fig, ax = plt.subplots(num='a'+angle+'t'+temp+'e'+energy)
        plt.plot(X*2.5e-2, QT, 'g--', label = r'd$N_{QT}$')
        plt.plot(X*2.5e-2, TQ, 'b-.', label = r'd$N_{TQ}$')
        #ax.plot(X*2.5e-2, CT, 'r--', label = 'CT')
        plt.plot(X*2.5e-2, CQ, 'r-', label = r'd$N_{CQ}$')
        try:
            plt.plot(X*2.5e-2, TC, '-', label = 'TC')
            plt.plot(X*2.5e-2, QC, '-', label = 'QC')
        except:
            dummy=1

        legend = plt.legend()

        svname = '/home/becker/lammps/newplot/Trans/' + name + 'Transition'
        SetupPlot(Title, 'Time / ps', 'Transition Populations', saveflag=g.S_TRANS, savename=svname+'.pdf', Block=1-g.S_TRANS)
    WritePlot(X=X, Y=[QT, TQ, CT, CQ], name=svname, xlabel='Time / ps', ylabel='Transition Populations QT, TQ, CT, CQ', saveflag=g.S_TRANS)
    return QT, TQ, CT, CQ, TC, QC
示例#2
0
def PlotDensityOverTime(xlabel='', ylabel='', Population=[], Stationary=[], Slope=[], mdData=[], mdDataGas=[], TimeArr=[],
                        Population2=[], TimeArr2=[], ylabel2='', pressure=0.0,
                        saveflag=False, savedir='', writeflag=False, block=False, t0=0):


    # find time where data is non-zero
    # this corresponds to the time of the first particles reaching the surface
    l = 0
    for i in range(len(mdData)):
        if mdData[i] > 0:
            l = i
            break

    print("earliest non zero value:", l)
    mdData = sf(mdData, 77, 3, deriv=0)
    mdDataGas = sf(mdDataGas, 77, 3, deriv=0)
    # prepare to write all the data
    if writeflag == True:
        WritePlot(X=TimeArr[:-l], Y=mdData[l:], name=savedir, xlabel=xlabel, ylabel=ylabel, header=True, action='w')
        WritePlot(X=TimeArr[:t0], Y=Population[:t0], name=savedir+'Sol', xlabel=xlabel, ylabel=ylabel, header=True, action='w')
        WritePlot(X=TimeArr[t0:], Y=Population[t0:], name=savedir+'SolExtra', xlabel=xlabel, ylabel=ylabel, header=True, action='w')
        WritePlot(X=TimeArr[:-l], Y=mdDataGas[l:], name=savedir+'Gas', xlabel=xlabel, ylabel=ylabel2, header=True, action='w')
        WritePlot(X=TimeArr2, Y=Population2, name=savedir+'SRT', xlabel=xlabel, ylabel=ylabel, header=True, action='w')


    # if desired plot first the bound particle densities
    plt.plot(TimeArr[:-l], mdData[l:], 'k', label=str('MD Data'))
    if len(Population) != 0:
        plt.plot(TimeArr[:t0], Population[:t0], 'r', label="MD-RE")
    if len(Population2) != 0:
        plt.plot(TimeArr2, Population2, 'r:', label="SRT")
    if len(Slope) != 0:
        pass
        # Slope = Scaling(Slope, conversion)
        # shift = 100
        # plt.plot(TimeArr[shift:int(len(Slope))+shift], Slope[:int(len(Slope))], '--', color='blue', label="Slope Data")
    if len(Stationary) != 0:
        plt.plot(TimeArr, Stationary, 'r:', label="Stationary Solution")

    # find maximum value for y axis
    maxDens = 0.0
    if(np.max(mdData) > np.max(Population)):
        maxDens = np.max(mdData)
    else:
        maxDens = np.max(Population)
    if(np.max(Population2) > maxDens):
        maxDens = np.max(Population2)

    plt.axis((-10, TimeArr[-l], -0.005, maxDens+0.05))
    MakePlot(saveflag=saveflag, block=block, xlabel=xlabel, ylabel=ylabel, savepath=savedir)

    # afterwards plot the gas particle population (or rather its density)
    plt.plot(TimeArr[:-l], mdDataGas[l:], label=r'$N_g$')
    MakePlot(saveflag=saveflag, block=block, xlabel=xlabel, ylabel=ylabel2, savepath=savedir+'Gas')
示例#3
0
def apply_smoothing(data, smoothing):
    """
    Apply Savitzky-Golay smoothing to the data for better readability.
    See: https://docs.scipy.org/doc/scipy-0.18.1/reference\
    /generated/scipy.signal.savgol_filter.html
    """
    if smoothing: 
        smoothed = sf(data, window_length=3, polyorder=1, mode="mirror")
        smoothed = sf(data, window_length=35, polyorder=1, mode="mirror")
    else: 
       smoothed = data  # with this, no smoothing is applied.
    return smoothed
示例#4
0
def filters(route_df):
    """
        Uses a SG filter to smooth elevation profile. 

        Parameters
        ----------
        route_df: geodataframe 

        Returns
        -------
        y_new: filtered elevation values
        """

    points = route_df['elevation'].values

    y_new = sf(points, 43, 3, axis=0)

    return y_new
示例#5
0
# In[7]:

z1 = z.eval(feed_dict={x_: x_t})
plt.figure(figsize=(20, 10))
plt.plot(y_t, label='Test')
plt.plot(z1, label='Net')
plt.legend()
plt.savefig(save_dir + '/fit1.png')


# In[5]:

from scipy.signal import savgol_filter as sf

y_smooth = sf(y_t[:, 0], 13, 12)
plt.figure(figsize=(20, 10))
plt.plot(y_t[:, 0])
plt.plot(y_smooth)
plt.plot(y_t[:, 0] - y_smooth + 1)


# In[33]:

s = scale_params[2]*y_t[:, 0]
l = len(s)
        
first_diff = s[1:l] - s[0:l-1]


# In[29]:
示例#6
0
def Correct_Overlap_Images_Intensities(infiles,
                                       window_length=101,
                                       polyorder=5,
                                       overlap_width=58,
                                       badpixel_width=10):
    """YG Correct WAXS Images intensities by using overlap area intensity
    Image intensity keep same for the first image
    Other image intensity is scaled by a pixel-width intensity array, which is averaged in the overlap area and then smoothed by 
    scipy.signal import savgol_filter with parameters as  window_length=101, polyorder=5, 
    
    from scipy.signal import savgol_filter as sf
    
    Return: data: array, stitched image with corrected intensity
           dataM: dict, each value is the image with correted intensity
           scale: scale for each image, the first scale=1 by defination
           scale_smooth: smoothed scale
           
   Exampe:
   data, dataM, scale,scale_smooth = Correct_Overlap_Images_Intensities( infiles, window_length=101, polyorder=5, 
                                       overlap_width=58, badpixel_width =10  )
                                       
   show_img(data, logs = True,   vmin=0.8* data.min(), vmax= 1.2*data.max()
         cmap = cmap_vge_hdr,   aspect=1,  image_name = 'Merged_Sca_Img')
         
         
    fig = plt.figure()# figsize=[2,8]) 
    for i in range(len(infiles)):
        #print(i)
        ax = fig.add_subplot(1,8, i+1)
        d = process.load(  infiles[i]  )
        show_img( dataM[i], logs = True, show_colorbar= False,show_ticks =False,
                 ax= [fig, ax], image_name= '%02d'%(i+1), cmap = cmap_vge_hdr, vmin=100, vmax=2e3,
                aspect=1, save=True, path=ResDir)
        
        
    
    """

    w = overlap_width
    ow = badpixel_width
    Nf = len(infiles)
    dataM = {}

    for i in range(len(infiles)):
        d = np.array(PIL.Image.open(infiles[i]).convert('I')).T / 1.0
        if i == 0:
            M, N = d.shape[0], d.shape[1]
            data = np.zeros([M, N * Nf - w * (Nf - 1)])
            data[:, :N] = d
            scale = np.zeros([len(infiles), M])
            scale_smooth = np.zeros([len(infiles), M])
            overlap_int = np.zeros([2 * len(infiles) - 1, M])
            overlap_int[0] = np.average(d[:, N - w:N - ow], axis=1)
            scale[0] = 1
            scale_smooth[0] = 1
            dataM[0] = d
        else:
            a1, a2, b1, b2 = N * i - w * (i - 1) - ow, N * (
                i + 1) - w * i, w - ow, N
            overlap_int[2 * i - 1] = np.average(d[:, 0:w - ow], axis=1)
            overlap_int[2 * i] = np.average(d[:, N - w:N - ow], axis=1)
            scale[i] = overlap_int[2 * i - 2] / overlap_int[2 * i -
                                                            1] * scale[i - 1]
            scale_smooth[i] = sf(scale[i],
                                 window_length=window_length,
                                 polyorder=polyorder,
                                 deriv=0,
                                 delta=1.0,
                                 axis=-1,
                                 mode='mirror',
                                 cval=0.0)
            data[:, a1:a2] = d[:, b1:b2] * np.repeat(
                scale_smooth[i], b2 - b1, axis=0).reshape([M, b2 - b1])
            dataM[i] = np.zeros_like(dataM[i - 1])
            dataM[i][:, 0:w - ow] = dataM[i - 1][:, N - w:N - ow]
            dataM[i][:, w - ow:] = data[:, a1:a2]
    return data, dataM, scale, scale_smooth
示例#7
0
columns = list(data)
sc_columns = list(sch_data)
tot_cases = np.nan_to_num(np.array(data['Total Cases'])).astype(np.int64)
new_cases = [tot_cases[x] - tot_cases[x - 1] for x in range(2, len(tot_cases))]
new_sch_cases = np.array(sch_data[sc_columns[5]])

tot_tests = np.nan_to_num(np.array(data[columns[9]])).astype(np.int64)
dates = pd.to_datetime(data[columns[0]])[2:]
dates_num = np.arange(1, len(dates) - 1)

tot_deaths = np.nan_to_num(np.array(data['Deaths']).astype(np.int64))
new_deaths = [tot_deaths[x] - tot_deaths[x - 1] for x in range(2, len(tot_deaths))]
axis2 = np.nan_to_num(np.array(new_deaths))  # Change column selection here

axis3 = np.nan_to_num(np.array(data[columns[9]][2:]))
smoothened_y1 = sf(new_cases, window_length=31, polyorder=3)

# Creating first figure and setting parameters
fig = plt.figure(x_axis_type="datetime", sizing_mode='stretch_both')
ticker = SingleIntervalTicker(interval=5, num_minor_ticks=10)
fig.xaxis.axis_label = 'Date'
fig.y_range = Range1d(start=0, end=max(new_cases) * 1.1)
fig.yaxis.axis_label = 'New Daily Cases'

# Create second axis and add it to plot
fig.extra_y_ranges = {"axis2": Range1d(start=0, end=max(axis2) * 1.1)}
fig.add_layout(LinearAxis(y_range_name="axis2", axis_label='Total Deaths'), 'right')

source = plt.ColumnDataSource(data={
    'x': dates,
    'y1': new_cases,
示例#8
0
def Correct_Overlap_Images_Intensities( infiles,Data=None, scale_smooth=None, window_length=101, polyorder=5, 
                                       overlap_width=58, badpixel_width =10, do_smooth = True,
                                       pixel_start_smooth = 0, pixel_stop_smooth = None  ):    
    """March 2018, update do_smooth option, if False, will not do smooth
                   update  pixel_start_smooth: by default=0, the starting pixel number for smoothing
                   update  pixel_stop_smooth: by default=None, the stop pixel nu number for smoothing
                   
    YG DEV Nov 19,2017 add data option
    
    Bug correction: Jan 12, 2018, correct intensity difference between DataM and data
                                  Now the intensity in the overlap area in DataM is same to the corresponding area in data
    
    YG Correct WAXS Images intensities by using overlap area intensity
    Image intensity keep same for the first image
    Other image intensity is scaled by a pixel-width intensity array, which is averaged in the overlap area and then smoothed by 
    scipy.signal import savgol_filter with parameters as  window_length=101, polyorder=5, 
    
    from scipy.signal import savgol_filter as sf
    

    Input:
    infiles: list, full data filename, in format of tif
    Data: 3D array, data[i] is a frame

    Return: data: array, stitched image with corrected intensity
           dataM: dict, each value is the image with correted intensity
           scale: scale for each image, the first scale=1 by defination
           scale_smooth: smoothed scale
           
   Exampe:
   data, dataM, scale,scale_smooth = Correct_Overlap_Images_Intensities( infiles, window_length=101, polyorder=5, 
                                       overlap_width=58, badpixel_width =10  )
                                       
   show_img(data, logs = True,   vmin=0.8* data.min(), vmax= 1.2*data.max()
         cmap = cmap_vge_hdr,   aspect=1,  image_name = 'Merged_Sca_Img')
         
         
    fig = plt.figure()# figsize=[2,8]) 
    for i in range(len(infiles)):
        #print(i)
        ax = fig.add_subplot(1,8, i+1)
        d = process.load(  infiles[i]  )
        show_img( dataM[i], logs = True, show_colorbar= False,show_ticks =False,
                 ax= [fig, ax], image_name= '%02d'%(i+1), cmap = cmap_vge_hdr, vmin=100, vmax=2e3,
                aspect=1, save=True, path=ResDir)
        
        
    
    """
    ps,pe = pixel_start_smooth,pixel_stop_smooth
    w  = overlap_width
    ow =  badpixel_width 
    if Data is None:
        Nf = len(infiles) 
    else:
        Nf = len(Data)
    dataM = {}
    if scale_smooth is not None:
        scale_smooth_flag = False
    else:
        scale_smooth_flag = True
    for i in range( Nf ):
        if Data is None:
            d = np.array(  PIL.Image.open(infiles[i]).convert('I') ).T/1.0
        else:
            d = Data[i].T
        if i ==0:        
            M,N = d.shape[0], d.shape[1]
            if pixel_stop_smooth is None:
                pixel_stop_smooth = M
                #print(  pixel_stop_smooth )
            data = np.zeros( [ M, N*Nf - w*( Nf -1) ]  )        
            data[:,  :N ] = d  
            scale=np.ones( [len(infiles), M] )
            if scale_smooth_flag:
                scale_smooth=np.ones( [len(infiles), M] )
                scale_smooth[0] = 1
                
            overlap_int = np.zeros(  [ 2* len(infiles) - 1, M]  )
            overlap_int[0] =  np.average( d[:, N - w: N-ow   ], axis=1)         
            scale[0] = 1 
            dataM[0] = d       
        else:        
            a1,a2, b1, b2 = N*i - w*(i-1) - ow,    N*(i+1) - w*i,     w - ow,  N 
            overlap_int[2*i-1] = np.average( d[:,  0: w - ow   ], axis=1  )
            overlap_int[2*i] =   np.average( d[:, N - w: N-ow   ] , axis=1  ) 
            
            if scale_smooth_flag:
                #scale[i] = np.ones( M )                
                scale[i][ps:pe] =   overlap_int[2*i-2][ps:pe]/overlap_int[2*i-1][ps:pe] *scale[i-1][ps:pe] 
                scale_smooth[i][ps:pe] = sf( scale[i][ps:pe], window_length=window_length, polyorder= polyorder, deriv=0, delta=1.0, axis=-1, mode='mirror', cval=0.0) 
                
                #print(scale[i][ps:pe].shape, scale_smooth[i][ps:pe].shape)                
                
            if do_smooth:    
                data[:,a1:a2] = d[:, b1:b2  ] * np.repeat(scale_smooth[i], b2-b1, axis=0).reshape([M,b2-b1])  
            else:
                data[:,a1:a2] = d[:, b1:b2  ]  
                scale_smooth = np.ones([ M,Nf] ).T
                scale = np.ones( [M,Nf] ).T
            dataM[i] = np.zeros_like( dataM[i-1])
            dataM[i][:,0:w-ow] =dataM[i-1][:,N-w:N-ow]
            dataM[i][:,w-ow:] = data[:,a1:a2] 
            
    for i in range( Nf ):
        #print( i, N-w, N, N*(1+i)-w*i - w, N*(1+i) - w *i )
        dataM[i][:,N-w:N] =  data[:, N*(1+i)-w*i - w: N*(1+i) - w *i]  
        
    return data, dataM, scale,scale_smooth
def select_image():
	files = [f for f in os.listdir('.') if os.path.isfile(f)]
	j=1
	for f in files:
		if ".pdf" in f:
			origF=f.split('.')[0]
			
			print ("start")
			#if you get error with subprocess.call try 'chmod +x file.sh' in cmd
			subprocess.call([os.getcwd()+"/burst_pdf.sh", f, origF])
			print ("end")
			break

	mypath = os.getcwd() + '/temp'
	files = [f for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath,f))]
	
	j=1
	
	for f in files:
		print (f)
		if all([".png" in f, "___cropped___" not in f, '-0000' not in f]):
			rotate_image(os.getcwd()+'/temp/'+f)
	# grab a reference to the image panels
	#global panelA
 
	# open a file chooser dialog and allow the user to select an input
	# image
	path = filedialog.askopenfilename()

	newP=path.split('.')
	indexPic=int(newP[0][len(newP[0])-1])
	print (indexPic)

	while True:
		from pathlib import Path
		myfile=Path(newP[0][0:len(newP[0])-1] + str(indexPic) + '.png')
		
		print(myfile)
		if myfile.is_file():
			
			path = newP[0][0:len(newP[0])-1] + str(indexPic) + '.png'
			indexPic+=1
			# ensure a file path was selected
			if len(path) > 0:
				# load the image from disk, convert it to grayscale, and detect
				# edges in it
				image = cv2.imread(path)
				gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
				#edged = cv2.Canny(gray, 50, 100)
		 
				# OpenCV represents images in BGR order; however PIL represents
				# images in RGB order, so we need to swap the channels
				image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
		 
				# convert the images to PIL format...
				image = Image.fromarray(image)
				#edged = Image.fromarray(edged)
		 
				# ...and then to ImageTk format
				image = ImageTk.PhotoImage(image)
				#edged = ImageTk.PhotoImage(edged)
						# if the panels are None, initialize them
				fname = path
				img = cv2.imread(fname)
				img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
				img2 = cv2.adaptiveThreshold(cv2.bitwise_not(img), 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, -2)


				# 3) Horizontal lines
				horizontal = img2.copy()
				# prepare structure
				horizontalsize = int(horizontal.shape[1] / 20) 
				# note: if I use a value > 30, I capture also some notes; 
				# if much lower, I lose some lines
				horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, 
				                                                (horizontalsize,1))
				## Apply morphology operations
				horizontal = cv2.erode(horizontal, horizontalStructure, (-1, 1))
				horizontal = cv2.dilate(horizontal, horizontalStructure, (-1, 1))

				show_small(horizontal)
				kernel = np.ones((5,5), np.uint8)
				horizontal = cv2.dilate(horizontal, kernel, iterations = 1)
				horizontal = cv2.bitwise_not(horizontal)
				show_small(horizontal)
				 


				# In[ ]:

				# 4) Vertical stuff
				vertical = img2.copy()
				# prepare structure
				verticalsize = int(vertical.shape[0] / 650) 
				verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, 
				                                                (1, verticalsize))
				## Apply morphology operations
				vertical = cv2.erode(vertical, verticalStructure, (-1, -1))
				vertical = cv2.dilate(vertical, verticalStructure, (-1, -1))
				#show_small(vertical)


				#x and y axis
				rs = np.sum(1- horizontal, axis =1)
				cl = list(range(horizontal.shape[0]))


				import matplotlib.pyplot as plt
				from matplotlib import interactive
				interactive(True)
				plt.axhline(y = 6)
				import scipy.interpolate
				f = scipy.interpolate.interp1d(cl, rs, kind = 'cubic')
				plt1 = plt.plot(cl, rs, 'o', cl, f(cl), '--')


				def savitzky_golay(y, window_size, order, deriv=0, rate=1):


				    r"""Smooth (and optionally differentiate) data with a Savitzky-Golay filter.
				    The Savitzky-Golay filter removes high frequency noise from data.
				    It has the advantage of preserving the original shape and
				    features of the signal better than other types of filtering
				    approaches, such as moving averages techniques.
				    Parameters
				    ----------
				    y : array_like, shape (N,)
				        the values of the time history of the signal.
				    window_size : int
				        the length of the window. Must be an odd integer number.
				    order : int
				        the order of the polynomial used in the filtering.
				        Must be less then `window_size` - 1.
				    deriv: int
				        the order of the derivative to compute (default = 0 means only smoothing)
				    Returns
				    -------
				    ys : ndarray, shape (N)
				        the smoothed signal (or it's n-th derivative).
				    Notes
				    -----
				    The Savitzky-Golay is a type of low-pass filter, particularly
				    suited for smoothing noisy data. The main idea behind this
				    approach is to make for each point a least-square fit with a
				    polynomial of high order over a odd-sized window centered at
				    the point.
				    Examples
				    --------
				    t = np.linspace(-4, 4, 500)
				    y = np.exp( -t**2 ) + np.random.normal(0, 0.05, t.shape)
				    ysg = savitzky_golay(y, window_size=31, order=4)
				    import matplotlib.pyplot as plt
				    plt.plot(t, y, label='Noisy signal')
				    plt.plot(t, np.exp(-t**2), 'k', lw=1.5, label='Original signal')
				    plt.plot(t, ysg, 'r', label='Filtered signal')
				    plt.legend()
				    plt.show()
				    References
				    ----------
				    .. [1] A. Savitzky, M. J. E. Golay, Smoothing and Differentiation of
				       Data by Simplified Least Squares Procedures. Analytical
				       Chemistry, 1964, 36 (8), pp 1627-1639.
				    .. [2] Numerical Recipes 3rd Edition: The Art of Scientific Computing
				       W.H. Press, S.A. Teukolsky, W.T. Vetterling, B.P. Flannery
				       Cambridge University Press ISBN-13: 9780521880688
				    """
				    import numpy as np
				    from math import factorial
				    
				    try:
				        window_size = np.abs(np.int(window_size))
				        order = np.abs(np.int(order))
				    except ValueError:
				        raise ValueError("window_size and order have to be of type int")
				    if window_size % 2 != 1 or window_size < 1:
				        raise TypeError("window_size size must be a positive odd number")
				    if window_size < order + 2:
				        raise TypeError("window_size is too small for the polynomials order")
				    order_range = range(order+1)
				    half_window = (window_size -1) // 2
				    # precompute coefficients
				    b = np.mat([[k**i for i in order_range] for k in range(-half_window, half_window+1)])
				    m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
				    # pad the signal at the extremes with
				    # values taken from the signal itself
				    firstvals = y[0] - np.abs( y[1:half_window+1][::-1] - y[0] )
				    lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
				    y = np.concatenate((firstvals, y, lastvals))
				    return np.convolve( m[::-1], y, mode='valid')
				x = cl
				y = rs
				from scipy.signal import savgol_filter as sf
				yhat = sf(y, 191, 3) # window size 191, polynomial order 3
				plt2 = plt.plot(x, y, '.', x, yhat,'-')
				#plt.show(plt.plot(x, y, 'o'))

				new = np.histogram(rs, bins = 50)

				#from peakdetect import peakdetect
				#cb = np.array(yhat)
				#peaks = peakdetect(cb, lookahead=100)

				xyArray=[x,yhat]

				def FindMaxima(numbers):


				  maxima = []
				  length = len(numbers[1])
				  if length >= 2:
				    if numbers[1][0] > numbers[1][1]:
				      maxima.append([numbers[0][0], numbers[1][0]])
				    if length > 3:
				      for i in range(1, length-1):
				        if numbers[1][i] > numbers[1][i-1] and numbers[1][i] > numbers[1][i-2] and numbers[1][i] > numbers[1][i-3] and numbers[1][i] > numbers[1][i-4] and numbers[1][i] > numbers[1][i-5] and numbers[1][i] > numbers[1][i+1] and numbers[1][i] > numbers[1][i+2] and numbers[1][i] > numbers[1][i+3] and numbers[1][i] > numbers[1][i+4] and numbers[1][i] > numbers[1][i+5]:
				            maxima.append([numbers[0][i],numbers[1][i]])
				            maxima.append([numbers[0][i],numbers[1][i]])
				    if numbers[1][length-1] > numbers[1][length-2]:    
				      maxima.append([numbers[0][length-1],numbers[1][length-1]])        
				  return maxima
				   
				maximaArray = FindMaxima(xyArray)

				maxAr=[]
				import csv
				import sys

				x=0
				y=1
				freq=[]
				while y<len(maximaArray):
					freq.append(maximaArray[y][0]-maximaArray[x][0])
					x+=1
					y+=1
				count, division = np.histogram(freq)
				secLarge=sorted(count, reverse=True)[1]
				a=0
				while a<len(count):
					if secLarge == count[a]:
						threshh=division[a]
					a+=1
				print(threshh)

				fn = 'localMaxima_OUTPUT.csv'
				with open(fn, 'w') as myfile:
				    outputFile = csv.writer(myfile)
				    i=0
				    j=1
				    startStaff='false'
				    while j<len(maximaArray):
				    	if (maximaArray[j][0]-maximaArray[i][0])>(threshh):
				    		outputFile.writerow([maximaArray[i][0],maximaArray[i][1]])
				    		maxAr.append(maximaArray[i])
				    	else:
				    		maxAr.append(maximaArray[i-1])
				    	i+=1
				    	j+=1
				    myfile.close()
				print (maxAr)
				# maximaArray = FindMaxima(xyArray)
				# maxAr=[]
				
				# i=0
				# j=1

				# while j<len(maximaArray):
				# 	if maximaArray[j][1]>((maximaArray[int(len(maximaArray)/2)][1]/3)*2):
				# 		maxAr.append(maximaArray[j])
				# 	i+=1
				# 	j+=1


				import ctypes
				if __name__ == '__main__' :
					# Read image
					im = cv2.imread(path)
					imNoLines = cv2.imread(path)
					i=1
					j=2
					#c=0
					rectangles=[]
					
					colors=[(255,147,0), (21,152,34), (255,147,170), (244,250,34), (102,102,153),(153,0,255),
					(255,153,0)]
					c=1
					while j<len(maxAr):
					    # if i==0:
					    #     y=maxAr[i][0]-50
					    # else:
					    y = maxAr[i][0]
					    
					    h = maxAr[j][0]
					    
					    x = 50+h-y
					    import random
					    crop_rect = cv2.rectangle(im,(0,y),(1000,h),random.choice(colors),2)
					    rectangles.append(crop_rect)
					    crop_img = img[y:h, 0:1000]
					    saver = path.split('.png')
					    imgAdd = saver[0]+"___cropped___"+str(y)+".png"

					    cv2.imwrite(imgAdd,crop_img)
					    i+=2
					    j+=2
					    c+=2


					cv2.namedWindow("output", cv2.WINDOW_NORMAL)
					imS=cv2.resize(im,(0,0),fx=0.5,fy=0.5)
					imCut=cv2.resize(imNoLines,(0,0),fx=0.5,fy=0.5)
					im2 = cv2.imread(path)

					while True:


						fromCenter = False
						showCrosshair = False
						r = cv2.selectROI("output", imS, fromCenter, showCrosshair)
						imCrop = im2[2*int(r[1]):2*int(r[1]+r[3]), 0:1000]
				
						# if the 'r' key is pressed, reset the cropping region
						# if key == ord("i"):
						# 	image = clone.copy()
						# 	imCrop = clone[2*int(r[1])+25:2*int(r[1]+r[3]), 0:1000]
						# 	cv2.imshow(imCrop)
						# 	cv2.waitKey(0)
					 
						# # if the 'c' key is pressed, break from the loop
						# elif key == ord("m"):
						# 	image = clone.copy()
						# 	imCrop = clone[2*int(r[1])-25:2*int(r[1]+r[3]), 0:1000]
						# 	cv2.imshow(imCrop)
						# 	cv2.waitKey(0)
						# Crop image
						#imCrop = im2[2*int(r[1]):2*int(r[1]+r[3]), 2*int(r[0]):2*int(r[0]+r[2])]
						

						saver = path.split('.png')
						
						#imgReturn=saver[0]+"_croppedimage*.png"

						if r[2] != 0.0 and r[3] != 0.0:
							yC=2*int(r[1])
							hC=2*int(r[1]+r[3])
							print (yC)
							yFix= (min(maxAr, key=lambda x,:abs(yC-x[0]))[0])
							print (hC)
							hFix= (min(maxAr, key=lambda x,:abs(hC-x[1]))[0])

							imgAdd = saver[0]+"___cropped___"+str(yFix)+".png"

							cv2.imwrite(imgAdd, imCrop)
						else:
							break
						cv2.waitKey(0)
						cv2.destroyWindow("output")
						i+=1
		    
		else:
			cv2.waitKey(0)
			cv2.destroyWindow("output")
			break
示例#10
0
def Correct_Overlap_Images_Intensities( infiles, window_length=101, polyorder=5, 
                                       overlap_width=58, badpixel_width =10  ):    
    """YG Correct WAXS Images intensities by using overlap area intensity
    Image intensity keep same for the first image
    Other image intensity is scaled by a pixel-width intensity array, which is averaged in the overlap area and then smoothed by 
    scipy.signal import savgol_filter with parameters as  window_length=101, polyorder=5, 
    
    from scipy.signal import savgol_filter as sf
    
    Return: data: array, stitched image with corrected intensity
           dataM: dict, each value is the image with correted intensity
           scale: scale for each image, the first scale=1 by defination
           scale_smooth: smoothed scale
           
   Exampe:
   data, dataM, scale,scale_smooth = Correct_Overlap_Images_Intensities( infiles, window_length=101, polyorder=5, 
                                       overlap_width=58, badpixel_width =10  )
                                       
   show_img(data, logs = True,   vmin=0.8* data.min(), vmax= 1.2*data.max()
         cmap = cmap_vge_hdr,   aspect=1,  image_name = 'Merged_Sca_Img')
         
         
    fig = plt.figure()# figsize=[2,8]) 
    for i in range(len(infiles)):
        #print(i)
        ax = fig.add_subplot(1,8, i+1)
        d = process.load(  infiles[i]  )
        show_img( dataM[i], logs = True, show_colorbar= False,show_ticks =False,
                 ax= [fig, ax], image_name= '%02d'%(i+1), cmap = cmap_vge_hdr, vmin=100, vmax=2e3,
                aspect=1, save=True, path=ResDir)
        
        
    
    """

    w  = overlap_width
    ow =  badpixel_width 
    Nf = len(infiles) 
    dataM = {}

    for i in range(len(infiles)):
        d = np.array(  PIL.Image.open(infiles[i]).convert('I') ).T/1.0
        if i ==0:        
            M,N = d.shape[0], d.shape[1]
            data = np.zeros( [ M, N*Nf - w*( Nf -1) ]  )        
            data[:,  :N ] = d  
            scale=np.zeros( [len(infiles), M] )
            scale_smooth=np.zeros( [len(infiles), M] )
            overlap_int = np.zeros(  [ 2* len(infiles) - 1, M]  )
            overlap_int[0] =  np.average( d[:, N - w: N-ow   ], axis=1)         
            scale[0] = 1   
            scale_smooth[0] = 1
            dataM[0] = d       
        else:        
            a1,a2, b1, b2 = N*i - w*(i-1) - ow,    N*(i+1) - w*i,     w - ow,  N 
            overlap_int[2*i-1] = np.average( d[:,  0: w - ow   ], axis=1  )
            overlap_int[2*i] =   np.average( d[:, N - w: N-ow   ] , axis=1  ) 
            scale[i] =   overlap_int[2*i-2]/overlap_int[2*i-1] *scale[i-1] 
            scale_smooth[i] = sf( scale[i], window_length=window_length, polyorder= polyorder, deriv=0, delta=1.0, axis=-1,
                       mode='mirror', cval=0.0)           
            data[:,a1:a2] = d[:, b1:b2  ] * np.repeat(scale_smooth[i], b2-b1, axis=0).reshape([M,b2-b1])
            dataM[i] = np.zeros_like( dataM[i-1])
            dataM[i][:,0:w-ow] =dataM[i-1][:,N-w:N-ow]
            dataM[i][:,w-ow:] = data[:,a1:a2] 
    return data, dataM, scale,scale_smooth
示例#11
0
def using_pygal(DataFile):
    print("--using_pygal...")
    # Get the data as dataframe
    RightData = mold_data(DataFile)  
    
    # Remove unnecessary information
    RightData = RightData.drop("edit-no", axis=1)
    RightData = RightData.drop("type", axis=1)
    RightData = RightData.drop("analysis", axis=1)
    RightData = RightData.drop("char-diff", axis=1)
    RightData = RightData.drop("char-diff-abs", axis=1)

    # RightData1: set levenshtein to zero for substantives (effectively ignoring them) 
    RightData1 = RightData.copy(deep=True)
    RightData1.loc[RightData1["category"] =="other","levenshtein"] = 0
    # Fix some details: levenshtein as numerical, remove x from line-id
    RightData1["levenshtein"] = RightData1["levenshtein"].astype(str).convert_objects(convert_numeric=True)
    RightData1["line-id"] = RightData1["line-id"].map(lambda x: x.rstrip("x"))
    RightData1 = RightData1.groupby("line-id").sum()
    #print(RightData1.head(20))

    # RightData2: set levenshtein to zero for copyedits (effectively ignoring them) 
    RightData2 = RightData.copy(deep=True)
    RightData2.loc[RightData2["category"] =="copyedit","levenshtein"] = 0
    # Fix some details: levenshtein as numerical, remove x from line-id
    RightData2["levenshtein"] = RightData2["levenshtein"].astype(str).convert_objects(convert_numeric=True)
    RightData2["line-id"] = RightData2["line-id"].map(lambda x: x.rstrip("x"))
    RightData2 = RightData2.groupby("line-id").sum()
    #print(RightData2.head(20))
        
    # Select the range of data we want
    Lines = RightData1.index.values
    Copyedits = RightData1.loc[:,"levenshtein"]
    Substantives = RightData2.loc[:,"levenshtein"]
    
    # Apply some interpolation    
    CopyeditsSF = sf(Copyedits, 35, 1, mode="nearest")
    SubstantivesSF = sf(Substantives, 35, 1, mode="nearest")

    # Graph general configuration
    config = Config()
    config.show_legend = False
    config.human_readable = True
    config.fill = False

    # Line chart
    LineChart = pygal.StackedLine(config, 
                           height=1000,
                           width = 2000,
                           x_label_rotation=300, 
                           show_legend=False,
                           x_labels_major_every=200,
                           show_minor_x_labels=False,
                           show_dots=False,
                           fill=True,
                           logarithmic=False,
                           range=(0, 60))
    LineChart.title ="The Martian Modifications (copyedits and substantives)"
    LineChart.y_title = "Levenshtein distance (smoothed)"
    LineChart.x_title = "Lines in the novel"
    LineChart.x_labels = Lines
    LineChart.add("Copyedits", CopyeditsSF)
    LineChart.add("Substantives", SubstantivesSF)
    LineChart.render_to_file("MartianMods_copyedits+substantives.svg")