Example #1
0
def applySavitzkyGolay(X,window_size=31, order=3,plotting=False):
    """
    use filter
    """
    print "Use Savitzky-Golay (windowsize=%4d, order=%4d)"%(window_size,order)
    tutto=[]
    for ind in X.index:
	row=[]
	row.append(ind)
	yorig = X.ix[ind,:].values
	ysg = savitzky_golay(yorig, window_size=41, order=3)
	
	for el in ysg:
	    row.append(el)
	tutto.append(row)
	#
	if plotting:
	    plt.plot(yorig, label='Noisy signal')
	    plt.plot( ysg, 'r', label='Filtered signal')
	    plt.legend(loc="best")
	    plt.show()
	
    newdf = pd.DataFrame(tutto).set_index(0)
    colnames = [ "s"+str(x) for x in xrange(newdf.shape[1]) ]
    newdf.columns=colnames
    print newdf.head(10)
    return(newdf)
Example #2
0
def applySavitzkyGolay(X, window_size=31, order=3, plotting=False):
    """
    use filter
    """
    print("Use Savitzky-Golay (windowsize=%4d, order=%4d)" %
          (window_size, order))
    tutto = []
    for ind in X.index:
        row = []
        row.append(ind)
        yorig = X.ix[ind, :].values
        ysg = savitzky_golay(yorig, window_size=41, order=3)

        for el in ysg:
            row.append(el)
        tutto.append(row)
        #
        if plotting:
            plt.plot(yorig, label='Noisy signal')
            plt.plot(ysg, 'r', label='Filtered signal')
            plt.legend(loc="best")
            plt.show()

    newdf = pd.DataFrame(tutto).set_index(0)
    colnames = ["s" + str(x) for x in range(newdf.shape[1])]
    newdf.columns = colnames
    print(newdf.head(10))
    return (newdf)
Example #3
0
def filtvec(v,winsize=1001,prime=1):#returns a filtered vector appropriate for Boyle footprinting. It assumes the vector has been appropriately padded by 500 on both sides
	binary = v != 0
	win = ones(winsize)
	sum500 = convolve(v,win,mode='same')#sum of all elements in a 1kb window at each element
	#avg500 = convolve(v,win,mode='same')#average of all elements in a window at each element
	#avg500[avg500 == 0] = 0.0001#this ensures no zero division
	nonzero500 = convolve(binary,win,mode='same')#number of all non-zero elements in a 1kb window at each element
	avg500 = sum500/nonzero500#average of all non-zero elements in a 1kb window at each element
	vn = v/avg500#normalized vector
	vn[isnan(vn)] = 0
	return savitzky_golay(vn, 9, 2, deriv=prime, rate=1)
Example #4
0
                [3.32448, 6.66353], [3.31582, 5.68866], [3.35159, 5.17255],
                [3.48482, 4.73125], [3.70669, 4.51875], [4.23639, 4.58968],
                [4.39592, 4.94615], [4.33527, 5.33862], [3.95968, 5.61967],
                [3.56366, 5.73976], [3.78818, 6.55292], [4.27712, 6.8283],
                [4.89532, 6.78615], [5.35334, 6.72433], [5.71583, 6.54449],
                [6.13452, 6.46019], [6.54478, 6.26068], [6.7873, 5.74615],
                [6.64086, 5.25269], [6.45649, 4.86206], [6.41586, 4.46519],
                [5.44711, 4.26519], [5.04087, 4.10581], [4.70013, 3.67405],
                [4.83482, 3.4375], [5.34086, 3.43394], [5.76392, 3.55156],
                [6.37056, 3.8778], [6.53116, 3.47228]])

x, y = pts.T
i = np.arange(len(pts))

#You can try Rbf, fitpack2 method
# 8x the original number of points
interp_i = np.linspace(0, i.max(), 8 * i.max())

#use interp1d to increase data points
xi = interp1d(i, x, kind='cubic')(interp_i)
yi = interp1d(i, y, kind='cubic')(interp_i)

#use this savitzky filter from http://scipy-cookbook.readthedocs.io/items/SavitzkyGolay.html
yhat = savitzky_golay(yi, 31, 5)  # window size 51, polynomial order 3

# plt.plot(xi, yi, 'r')  # plots data w/out savitzky filtering
plt.plot(xi, yhat, 'b')  # plots data w/ savitzky filtering
plt.plot(x, y, 'ko')  # plots points to follow

plt.show()
Example #5
0
    def plot_graph_with_function(self,x_axis, y_axis,rescale_y=1,save=False,filename ="output.png"):
        fig, ax = plt.subplots()
        G = self.get_graph()
        pos = nx.get_node_attributes(G, 'pos')

        max_h = int(self._get_max_height())
        function_y0 = max_h+ 1
        max_y = function_y0 + rescale_y + 0.5
        best_arm_x = self.get_best_arm_value()
        text_x_offset = 0.01*(self.arm_range_max-self.arm_range_min)
        text_y_offset = -0.5


        #plotting the estimated underlying function
        try:
            self._sort_list_arm_by_arm()
            est_x = self.arm_list[:,0]
            number_arms = self.arm_list.shape[0]
            window = convert2odd(number_arms/2)
            est_y = savitzky_golay((self.arm_list[:,9]),window_size=window,order=max_h)*rescale_y+ function_y0
            plt.plot(est_x,est_y, label="Estimated function")
        except:
            pass

        #ploting the function after the max height
        func_y = rescale_y*y_axis+function_y0
        func_x = x_axis
        ax.plot(func_x, func_y, label="True function")

        #Drawing the graph
        nx.draw(G, pos=pos, node_size=20,ax=ax)

        # Line of the best arm
        plt.axvline(x=best_arm_x, linestyle='--')
        ax.text(best_arm_x + text_x_offset, function_y0 + text_y_offset, "Best arm:\n" + str(format(best_arm_x, '.4f')))


        # Line for the heights
        for i in range(1,max_h+1):
            plt.axhline(y=i, linestyle='--', linewidth=0.5)
            ax.text(0,i,str(i), size='smaller')

        #line for the zero in the underlying function
        plt.axhline(y=function_y0, linestyle='-',linewidth=0.5, color='black')
        plt.axhline(y=function_y0+rescale_y, linestyle='-', linewidth=0.5, color='black')

        #Setting figure style
        #axis on but just with the x axis, the y axis is off
        plt.axis('on')
        plt.xlim(self.arm_range_min,self.arm_range_max)
        ax.get_yaxis().set_visible(False)
        plt.ylim([1,max_y])

        #setting title for the plot and axis
        ax.set_title('Best value optimization using LG-HOO')
        ax.set_xlabel('Values in the $\chi$-space')

        lgd = ax.legend(loc = 9, bbox_to_anchor=(0.5,-0.15),ncol=2)
        fig.subplots_adjust(left=None, bottom=0.2, right=None, top=None, wspace=None, hspace=None)
#
        if save == True:
            mainfile = sys.argv[0]
            pathname = os.path.join(os.path.dirname(mainfile),"pictures")
            output = os.path.join(pathname,filename)
            plt.savefig(output, format='png', dpi=300)#, bblock_extra_artists=(lgd,), bbox_inches='tight')
        else:
            #plt.tight_layout()
            plt.show()
Example #6
0
times = np.array(times)
#times -= times[0]

t_center=1812.8965 #78.1875#np.median(times)
Delta_t=(times[59]-times[5])/2
I_o=7550#np.median(corrected)
I=I_o*((2./5)+((3./5)*(np.sqrt(1-(((times-t_center)**2)/(Delta_t**2))))))
#I_o = 7550
#t_center = 78.77
#Delta_t = -64.67
#I = I_o*((2./5)+(3./5)*np.sqrt(1-((times-t_center)/Delta_t)**2))

#Smoothing
correct=(files[7]-dark_value)
wavelength=(pixel*(0.014))+524.61
smoothed=savitzky_golay(correct,window_size=31, order=4)


#plt.plot(times,corrected,'o')
plt.plot(wavelength,correct)
plt.plot(wavelength,smoothed,'r')
#plt.plot(times,I,'r')
plt.title('Relative flux vs. Time -Sun 170ms')
#plt.xlabel('Time(seconds)')
plt.ylabel('Relative Flux')
plt.xlabel('Wavelength(nm)')
#plt.legend(('Dark Subtracted Data','Limb Darkening'),loc='best')
plt.legend(('Corrected Data','Smoothed'),loc='best')
plt.show()

#phi_theory_alm = kappa_theory_alm * (2.0 / (theory_KK_ell*(theory_KK_ell+1.0)))
#phi_theory_alm[theory_KK_ell==0] = 0
#theory_CL_PP = hp.alm2cl(phi_theory_alm)
#theory_PP_ell = np.arange(theory_CL_PP.size)

print "include ell factors for simulation power spectra..."
#theory_CL_TT = theory_TT_ell * (theory_TT_ell+1) * theory_CL_TT / (2*np.pi)
theory_CL_PP = (theory_PP_ell * (theory_PP_ell + 1))**2 * theory_CL_PP / (2*np.pi)
sim_CL_TT_lensed = sim_TT_ell * (sim_TT_ell + 1) * sim_CL_TT_lensed / (2*np.pi)# * (sim_TT_ell*(sim_TT_ell+1)/4.0)
sim_CL_EE_lensed = sim_TT_ell * (sim_TT_ell + 1) * sim_CL_EE_lensed / (2*np.pi)
sim_CL_BB_lensed = sim_TT_ell * (sim_TT_ell + 1) * sim_CL_BB_lensed / (2*np.pi)
#smooth the spectra
#primary_cl = savitzky_golay(primary_cl, 75, 3)
#phi_cl = savitzky_golay(phi_cl, 75, 3)
print "smoothing power spectrum from map..."
sim_CL_TT_lensed = savitzky_golay(sim_CL_TT_lensed, 75, 3)
sim_CL_EE_lensed = savitzky_golay(sim_CL_EE_lensed, 75, 3)
sim_CL_BB_lensed = savitzky_golay(sim_CL_BB_lensed, 75, 3)

#print theory_TT_ell
#print theory_CL_TT.shape

TEB_cls = np.array([[theory_CL_TT[_l], theory_CL_EE[_l], 0, theory_CL_TE[_l]] for _l in theory_TT_ell]) #fill BB with zeroes
print TEB_cls.shape
print "starting lensing convolution..."
lensed_theory_TEB = correlations.lensed_cls(TEB_cls, theory_CL_PP)
theory_CL_TT_lensed = np.array([_cl[0] for _cl in lensed_theory_TEB])
theory_CL_EE_lensed = np.array([_cl[1] for _cl in lensed_theory_TEB])
theory_CL_BB_lensed = np.array([_cl[2] for _cl in lensed_theory_TEB])
# theory_CL_TE_lensed = np.array([_cl[3] for _cl in lensed_theory_TEB])
print "writing camb cl..."
Example #8
0
def normalize_filter_maxderdivide(v):
    v2 = savitzky_golay(v,5,2,1,1)
    v3 = v2/max(abs(v2))
    return v3
Example #9
0
def normalize_filter_threshold4(v):
    v2 = v/mean(v[v>4])
    v3 = savitzky_golay(v2,5,2,1,1)
    return v3
Example #10
0
def normalize_filter_quantilemap(v,r):
    ranks = rankdata(v)/len(v)
    v2 = mquantiles(r,prob=ranks)
    v3 = savitzky_golay(v2,5,2,1,1)
    return v3
Example #11
0
nice_values= np.delete(nice_values,54, axis=0)

#times -= times[0]

t_center=1716.7909999999999#times[6]+(times[82]-times[6])#1812.8965 #78.1875#np.median(times)
Delta_t=(times[82]-times[6])/2
I_o=  5200#5089.0822103348455
I=I_o*((2./5)+((3./5)*(np.sqrt(1-(((times-t_center)**2)/(Delta_t**2))))))

##### plotting wavelength of two ending points ########
file1=nice_values[6]#(files[6]-dark_value)/flat_correct
file2=nice_values[82]#(files[82]-dark_value)/flat_correct

wavelength=(pixel*(0.014))+524.61

smoothed=savitzky_golay(file1,window_size=601, order=1)
smoothed1=savitzky_golay(file2,window_size=601, order=1)

flat_curve1=file1/smoothed
flat_curve2=file2/smoothed1

'''
plt.plot(wavelength,file1,'b')
plt.plot(wavelength,file2,'r')
plt.title("Intensity vs. Wavelength for sun 125ms")
plt.xlabel("Wavelength(nm)")
plt.ylabel("Intensity")
plt.legend(('First Spectrum','Second Spectrum'),loc='best')
'''
'''
#### plotting limb darkening#######