Skip to content

Instantly share code, notes, and snippets.

@jj-github-jj
Last active December 28, 2021 14:13
Show Gist options
  • Save jj-github-jj/f17b60b3d3ba577f64ade77094085d74 to your computer and use it in GitHub Desktop.
Save jj-github-jj/f17b60b3d3ba577f64ade77094085d74 to your computer and use it in GitHub Desktop.
numpy utility snippets
def find_nearest(array, value):
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return idx,array[idx] #return in x,y format x is index, y is value
[print (i,v/1e9) for i,v in enumerate(x)] #print index and value of elements in list
def avg_dB(dB):
"""
average in linear domain for dB data
"""
lin=np.power(10,dB/10)
return (10*np.log10(np.mean(lin)))
def drop_peak(x,y,nbins=1):
"""
drops the nbinsx2 surrounding the peak of spectrum trace of x , y values
"""
idx_max_power=np.argmax(y)
y=list(y[0:idx_max_power-nbins]) + list(y[idx_max_power+nbins:])
x=list(x[0:idx_max_power-nbins]) +list(x[idx_max_power+nbins:])
return (np.array(x),np.array(y))
#find closest yvalue point in xy trace in plotly fig
def xy_point (yval,fig,trace_index=0):
""" return x,y values and index for point closest to yval
"""
x=fig.data[trace_index].x
y=fig.data[trace_index].y
idx=np.where(y<yval)
imax=y[idx].argmax() #highest y value < yval
xv=x[imax];yv=y[imax]
return (xv,yv,imax)
def xy_points (yval,x,y):
""" returns list of indices where y is closest to yval and index of max y val by splitting into 2 regions around max point
"""
#split into two ranges above , below max point and find y points < yval in both subsets
max_y_index=find_nearest(y,np.max(y))[0]
p1=xy_point(yval,x[0:max_y_index],y[0:max_y_index])
p2=xy_point(yval,x[max_y_index:],y[max_y_index:])
idx=[0]*2 #init with size 2
idx[0]= p1[2]; idx[1]=p2[2]+max_y_index
#idx1;idx2;x[idx1];y[idx1];x[idx2];y[idx2]
return (idx,max_y_index)
def find_nearest(array, value):
array = np.asarray(array) #in case input is a list
idx = (np.abs(array - value)).argmin()
return idx,array[idx] #return in x,y format x is index, y is value
def xy_annotate (fig2, trace_index=0, y_vals=[],text_in='',angle=0):
""" annotates the closest point on the xy trace of plotly graph object fig2
"""
#print ('text in', text_in)
d=fig2.data[trace_index] # first trace
for y_val in y_vals:
x=d.x[find_nearest(d.y,y_val)[0]] #find index,val nearest to y_val
y= find_nearest(d.y,y_val)[1] #index 1 is y value, index 0 is array index
# print ('xy',x,y)
# print ('text ..', text_in)
if text_in=='': #use x y coordinates as text if text is not specified
text=f'{x:0.2G} {y:0.2f}'
else:
text=text_in
#print ('text',text)
fig2=fig2.add_annotation(x=x,y=y,text=text,visible=True)
#xshift and yshift move the entire text and arrow units pixels
fig2=fig2.update_annotations(font_size=10,textangle=angle,standoff=0,xshift=1,yshift=-1)
# standoff
# print ('xy',x,y)
return (fig2)
def xy_point_plotly (yval,fig,trace_index=0):
""" return x,y values and indices for point closest to yval
"""
x=fig.data[trace_index].x
y=fig.data[trace_index].y
idx=np.where(y<yval)
imax=y[idx].argmax() #highest y value < yval
xv=x[imax];yv=y[imax]
return (xv,yv,imax)
def xy_point (yval,x,y):
idx=np.where(y<yval)
imax=find_nearest(y,yval)[0]
# print ("index",imax)
xv=x[imax];yv=y[imax]
return (xv,yv,imax)
def xy_points (yval,x,y):
""" returns list of indices where y is closest to yval including index of max y val by splitting into 2 regions around max point
"""
#split into two ranges above , below max point and find y points < yval in both subsets
max_y_index=find_nearest(y,np.max(y))[0]
p1=xy_point(yval,x[0:max_y_index],y[0:max_y_index])
p2=xy_point(yval,x[max_y_index:],y[max_y_index:])
idx=[0]*3 #init with size 2
idx[0]= p1[2];idx[1]=max_y_index; idx[2]=p2[2]+max_y_index
#idx1;idx2;x[idx1];y[idx1];x[idx2];y[idx2]
return (idx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment