I am currently trying to identify peaks on a randomly generated plot that I have created.
My code is as follows:
x_range = np.arange(0,100,0.5) #my x values
for i in len(ys): #ys is my range of y values on the chart
for j in range(start,len(ys)): #Brute forcing peak detection
temp.append(ys[j])
check = int(classtest.isPeak(temp)[0])
if check == 1:
xval = temp.index(max(temp)) #getting the index
xlist = x_range.tolist()
plt.plot(xlist[xval],max(temp),"ro")
start = start + 1
temp = []
However when plotting the values on the graph, it seems to plot the correct y position, but not x. Here is an example of what is happening:
I am really not sure what is causing this problem, and I would love some help.
Thanks
Remember that temp is getting shorter and shorter as start increases.
So the index, xval, corresponding to a max in temp is not in itself the correct index into x_range. Rather, you have to increase xval by start to find the corresponding index in x_range:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2016)
N = 100
ys = (np.random.random(N)-0.5).cumsum()
xs = np.linspace(0, 100, len(ys))
plt.plot(xs, ys)
start = 0
temp = []
for i in range(len(ys)): #ys is my range of y values on the chart
for j in range(start,len(ys)): #Brute forcing peak detection
temp.append(ys[j])
xval = temp.index(max(temp)) #getting the index
plt.plot(xs[xval+start], max(temp),"ro")
start = start + 1
temp = []
plt.show()
While that does manage to place the red dots at points on the graph, as you can
see the algorithm is placing a dot at every point on the graph, not just at local
maxima. Part of the problem is that when temp contains only one point, it is
of course the max. And the double for-loop ensures that every point gets
considered, so at some point temp contains each point on the graph alone as a
single point.
A different algorithm is required. A local max can be identified as any
point which is bigger than its neighbors:
ys[i-1] <= ys[i] >= ys[i+1]
therefore, you could use:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2016)
N = 100
ys = (np.random.random(N)-0.5).cumsum()
xs = np.linspace(0, 100, len(ys))
plt.plot(xs, ys)
idx = []
for i in range(1, len(ys)-1):
if ys[i-1] <= ys[i] >= ys[i+1]:
idx.append(i)
plt.plot(xs[idx], ys[idx], 'ro')
plt.show()
Note that scipy.signal.argrelextrema or scipy.signal.argrelmax can also be used to find local maximums:
from scipy import signal
idx = signal.argrelextrema(ys, np.greater)
plt.plot(xs[idx], ys[idx], 'ro')
produces the same result.
Related
I have a Python project where I need to redraw a line many times with the points in random places but keeping the line's shape and point count roughly the same. The final output will be using polygonal points and not Bezier paths (though I wouldn't be opposed to using Bezier as an intermediary step).
This animation is demonstrating how the points could move along the line to different positions while maintaining the general shape.
I also have a working example below where I'm moving along the line and picking random new points between existing points (the red line, below). It works okay, but I'd love to hear some other approaches I might take if someone knows of a better one?
Though this code is using matplotlib to demonstrate the line, the final program will not.
import numpy as np
from matplotlib import pyplot as plt
import random
from random import (randint,uniform)
def move_along_line(p1, p2, scalar):
distX = p2[0] - p1[0]
distY = p2[1] - p1[1]
modX = (distX * scalar) + p1[0]
modY = (distY * scalar) + p1[1]
return [modX, modY]
x_coords = [213.5500031,234.3809357,255.211853,276.0427856,296.8737183,317.7046204,340.1997681,364.3751221,388.5505066,414.8896484,444.5192261,478.5549622,514.5779419,545.4779053,570.3830566,588.0241699,598.2469482,599.772583,596.758728,593.7449341,590.7310791,593.373291,610.0373535,642.1326294,677.4451904,710.0697021,737.6887817,764.4020386,791.1152954,817.8284912,844.541687,871.2550049,897.9682007,924.6813965,951.3945923,978.1078491,1009.909546,1042.689941,1068.179199,1089.543091]
y_coords = [487.3099976,456.8832703,426.4565125,396.0297852,365.6030273,335.1763,306.0349426,278.1913452,250.3477478,224.7166748,203.0908051,191.2358704,197.6810608,217.504303,244.4946136,276.7698364,312.0551453,348.6885986,385.4395447,422.1904297,458.9414063,495.5985413,527.0128479,537.1477661,527.6642456,510.959259,486.6988525,461.2799683,435.8611145,410.4422913,385.023468,359.6045532,334.18573,308.7669067,283.3480835,257.929184,239.4429474,253.6099091,280.1803284,310.158783]
plt.plot(x_coords,y_coords,color='b')
plt.scatter(x_coords,y_coords,s=2)
new_line_x = []
new_line_y = []
for tgt in range(len(x_coords)-1):
#tgt = randint(0, len(x_coords)-1)
next_pt = tgt+1
new_pt = move_along_line([x_coords[tgt],y_coords[tgt]], [x_coords[next_pt],y_coords[next_pt]], uniform(0, 1))
new_line_x.append(new_pt[0])
new_line_y.append(new_pt[1])
plt.plot(new_line_x,new_line_y,color='r')
plt.scatter(new_line_x,new_line_y,s=10)
ax = plt.gca()
ax.set_aspect('equal')
plt.show()
Thank you very much!
I'm not sure if this is the most optimal way to do this but essentially you want to follow these steps:
Calculate the distance of the entire path, and the distance between all the points. Then for each point, tally the distances to that point.
Generate a new set of random points along the path starting with 0, then for each pair of points calculate a random distance: random value between 0 and 1 * total length of the path.
Sort these distances from smallest to largest.
For each random distance loop over the distances find the index where the random distance is > than distance i, and less than distance i+1. Interpolate new x and y values from these points.
from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
import numpy
import random
import math
x_coords = [195.21,212.53,237.39,270.91,314.21,368.43,434.69,514.1,607.8,692.69,746.98,773.8,776.25,757.45,720.52,668.55,604.68,545.37,505.79,487.05,490.27,516.58,567.09,642.93,745.2,851.5,939.53,1010.54,1065.8,1106.58,1134.15,1149.75,1154.68]
y_coords = [195.34,272.27,356.59,438.98,510.14,560.76,581.52,563.13,496.27,404.39,318.83,242.15,176.92,125.69,91.02,75.48,81.62,113.49,168.57,239.59,319.29,400.38,475.6,537.67,579.32,586.78,558.32,504.7,436.69,365.05,300.55,253.95,236.03]
n_points = 100
x_coords = numpy.array(x_coords)
x_min = x_coords.min()
x_max = x_coords.max()
x_range = x_max - x_min
distances = []
tallied_distances = [0]
tallied_distance = 0
for i in range(0, len(x_coords) -1):
xi = x_coords[i]
xf = x_coords[i + 1]
yi= y_coords[i]
yf = y_coords[i+1]
d = math.sqrt((xf-xi)**2 + (yf-yi)**2)
tallied_distance += d
tallied_distances.append(tallied_distance)
random_distances_along_line = [0]
for i in range(0, n_points-2):
random_distances_along_line.append(random.random()*tallied_distance)
random_distances_along_line.sort()
new_x_points = [x_coords[0]]
new_y_points = [y_coords[0]]
for i in range(0, len(random_distances_along_line)):
dt = random_distances_along_line[i]
for j in range(0, len(tallied_distances)-1):
di = tallied_distances[j]
df = tallied_distances[j+1]
if di < dt and dt < df:
difference = dt - di
xi = x_coords[j]
xf = x_coords[j+1]
yi = y_coords[j]
yf = y_coords[j+1]
xt = xi+(xf-xi)*difference/(df-di)
yt = yi+(yf-yi)*difference/(df-di)
new_x_points.append(xt)
new_y_points.append(yt)
new_x_points.append(x_coords[len(x_coords)-1])
new_y_points.append(y_coords[len(y_coords)-1])
plt.plot(new_x_points, new_y_points)
plt.scatter(new_x_points, new_y_points,s=2)
ax = plt.gca()
ax.set_aspect('equal')
plt.show()
I am trying to plot 3 different functions on a log-log scale in python for three intervals of the x-range. Attaching the image of the kind of plot that I want to create and which functions, y, for what intervals of x.
My code attempt is as follows. Maybe I am overcomplicating it.
import math as m
import numpy as np
import pylab as pl
import matplotlib.pyplot as plt
x = np.arange(100) # this gives `array([0, 1, 2, ..., 9])`
y = np.arange(100)
for i in range (-50,20):
if x[i] < -43:
y[i] = m.log10((10**x[i])/(10**-43))**(1/2)
if x[i] > -43 and x[i] < -40:
y[i] = m.log10(np.exp((10**36)((10**x[i])-(10**-43))))
if x[i] >-40:
y[i] = m.log10((np.exp((10**36)((10**-40) - (10**-43)))(((10**x[i])/(10**-43))**(1/2)))
#i+=1
pl.plot(x,y)
#pl.xlim([-100.,100.])
#pl.ylim([-100.,100.])
pl.xlabel('log x')
pl.ylabel('log y')
pl.show()
PLEASE NOTE:
updated code with help from #Sembei which works but there's further question on colours below:
import matplotlib.pyplot as plt
x = np.linspace(-50,23,500)
y = []
for xval in x:
if xval < -36:
y.append(m.log10(((10**xval)/(10**-36))**(1/2)))
elif -36 <= xval <= -34:
y.append(m.log10(np.exp((10**36)*((10**xval)-(10**-36)))))
else:
y.append(m.log10((np.exp((10**36)*((10**-34) - (10**-36)))*(((10**xval)/(10**-36))**(1/2)))))
plt.plot(x,y)
pl.xlim([-44.,-30.])
#pl.ylim([-10.,20.])
pl.xlabel('log x')
pl.ylabel('log y')
plt.show()
FURTHER QUESTION:
how to set 3 different colours for the different y functions for the 3 x-intervals?
Any help is appreciated. Thanks!
you can do something like this:
x = range(-50,23)
y = []
for xval in x:
if xval < -43:
y.append(-43) #your function in this interval
elif -43 <= xval <= -40:
y.append(xval) #your function in this interval)
else:
y.append(-40) #your function in this interval)
plt.plot(x,y, '.-')
plt.xlabel('log x')
plt.ylabel('log y')
plt.show()
You just need to fill the #your function in this interval with a correct syntax (note that in your syntax you are missing product operators *)
Here I have used y as a list and I'm appending values. You can also initialize y to all zeros and assign values based on indexes. For that you will need to include an enumerate in the loop that will give you the index of y where you have to put the value.
Note: here, range steps one by one. if you want more resolution you might want to use np.linspace so you can control the resolution of your function.
Edit: I put some toy definitions of the function so you can see how it works. Now just change my function definitions for your own
I want to generate a toy example to illustrate a convex piecewise linear function in python, but I couldn't figure out the best way to do this. What I want to do is to indicate the number of lines and generate the function randomly.
A convex piecewise-linear function is defined as:
For instance, if I want to have four linear lines, then I want to generate something as shown below.
Since there are four lines. I need to generate four increasing random integers to determine the intervals in x-axis.
import random
import numpy as np
random.seed(1)
x_points = np.array(random.sample(range(1, 20), 4))
x_points.sort()
x_points = np.append(0, x_points)
x_points
[0 3 4 5 9]
I can now use the first two points and create a random linear function, but I don't know how I should continue from there to preserve the convexity. Note that a function is called convex if the line segment between any two points on the graph of the function does not lie below the graph between the two points.
The slope increases monotonously by a random value from the range [0,1), starting from 0. The first y value is also zero, see the comments.
import numpy as np
np.random.seed(0)
x_points = np.random.randint(low=1, high=20, size=4)
x_points.sort()
x_points = np.append(0, x_points) # the first 0 point is 0
slopes = np.add.accumulate(np.random.random(size=3))
slopes = np.append(0,slopes) # the first slope is 0
y_incr = np.ediff1d(x_points)*slopes
y_points = np.add.accumulate(y_incr)
y_points = np.append(0,y_points) # the first y values is 0
A possible output looks like this:
print(x_points)
print(y_points)
# [ 0 1 4 13 16]
# [ 0. 0. 2.57383685 17.92061306 24.90689622]
To print this figure:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(x_points,y_points, '-o', label="convex piecewise-linear function")
ax.legend()
fig.patch.set_facecolor('white')
plt.show()
make sure the gradient (=dx/dy) is increasing.
Pseudocode:
s = 1;
x = 0;
y = 0;
n = 4;
while(--n>0)
{
//increase x randomly
dx = rand(3);
dy = dx * s;
x += dx;
y += dy;
//increase gradient randomly
s += rand(3);
print x + "/" +y;
}
I am working with a projected coordinate dataset that contains x,y,z data (432 line csv with X Y Z headers, not attached). I wish to import this dataset, calculate a new grid based on user input and then start performing some statistics on points that fall within the new grid. I've gotten to the point that I have two lists (raw_lst with 431(x,y,z) and grid_lst with 16(x,y) (calling n,e)) but when I try to iterate through to start calculating average and density for the new grid it all falls apart. I am trying to output a final list that contains the grid_lst x and y values along with the calculated average z and density values.
I searched numpy and scipy libraries thinking that they may have already had something to do what I am wanting but was unable to find anything. Let me know if any of you all have any thoughts.
sample_xyz_reddot_is_newgrid_pictoral_representation
import pandas as pd
import math
df=pd.read_csv("Sample_xyz.csv")
N=df["X"]
E=df["Y"]
Z=df["Z"]
#grid = int(input("Specify grid value "))
grid = float(0.5) #for quick testing the grid value is set to 0.5
#max and total calculate the input area extents
max_N = math.ceil(max(N))
max_E = math.ceil(max(E))
min_E = math.floor(min(E))
min_N = math.floor(min(N))
total_N = max_N - min_N
total_E = max_E - min_E
total_N = int(total_N/grid)
total_E = int(total_E/grid)
#N_lst and E_lst calculate the mid points based on the input file extents and the specified grid file
N_lst = []
n=float(max_N)-(0.5*grid)
for x in range(total_N):
N_lst.append(n)
n=n-grid
E_lst = []
e=float(max_E)-(0.5*grid)
for x in range(total_E):
E_lst.append(e)
e=e-grid
grid_lst = []
for n in N_lst:
for e in E_lst:
grid_lst.append((n,e))
#converts the imported dataframe to list
raw_lst = df.to_records(index=False)
raw_lst = list(raw_lst)
#print(grid_lst) # grid_lst is a list of 16 (n,e) tuples for the new grid coordinates.
#print(raw_lst) # raw_lst is a list of 441 (n,e,z) tuples from the imported file - calling these x,y,z.
#The calculation where it all falls apart.
t=[]
average_lst = []
for n, e in grid_lst:
for x, y, z in raw_lst:
if n >= x-(grid/2) and n <= x+(grid/2) and e >= y-(grid/2) and e <= y+(grid/2):
t.append(z)
average = sum(t)/len(t)
density = len(t)/grid
average_lst = (n,e,average,density)
print(average_lst)
# print("The length of this list is " + str(len(average_lst)))
# print("The length of t is " + str(len(t)))
SAMPLE CODE FOR RUNNING
import random
grid=5
raw_lst = [(random.randrange(0,10), random.randrange(0,10), random.randrange(0,2))for i in range(100)]
grid_lst = [(2.5,2.5),(2.5,7.5),(7.5,2.5),(7.5,7.5)]
t=[]
average_lst = []
for n, e in grid_lst:
for x, y, z in raw_lst:
if n >= x-(grid/2) and n <= x+(grid/2) and e >= y-(grid/2) and e <= y+(grid/2):
t.append(z)
average = sum(t)/len(t)
density = len(t)/grid
average_lst = (n,e,average,density)
print(average_lst)
Some advices
when working with arrays, use numpy. It has more functionalities
when working with grids it's often more handy the use x-coords, y-coords as single arrays
Comments to the solution
obviousley you have a grid, or rather a box, grd_lst. We generate it as a numpy meshgrid (gx,gy)
you have a number of points raw_list. We generate each elemnt of it as 1-dimensional numpy arrays
you want to select the r_points that are in the g_box. We use the percentage formula for that: tx = (rx-gxMin)/(gxMax-gxMin)
if tx, ty are within [0..1] we store the index
as an intermediate result we get all indices of raw_list that are within the g_box
with that index you can extract the elements of raw_list that are within the g_box and can do some statistics
note that I have omitted the z-coord. You will have to improve this solution.
--
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.colors as mclr
from matplotlib import cm
f10 = 'C://gcg//picStack_10.jpg' # output file name
f20 = 'C://gcg//picStack_20.jpg' # output file name
def plot_grid(gx,gy,rx,ry,Rx,Ry,fOut):
fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(111)
myCmap = mclr.ListedColormap(['blue','lightgreen'])
ax.pcolormesh(gx, gy, gx, edgecolors='b', cmap=myCmap, lw=1, alpha=0.3)
ax.scatter(rx,ry,s=150,c='r', alpha=0.7)
ax.scatter(Rx,Ry,marker='s', s=150,c='gold', alpha=0.5)
ax.set_aspect('equal')
plt.savefig(fOut)
plt.show()
def get_g_grid(nx,ny):
ix = 2.5 + 5*np.linspace(0,1,nx)
iy = 2.5 + 5*np.linspace(0,1,ny)
gx, gy = np.meshgrid(ix, iy, indexing='ij')
return gx,gy
def get_raw_points(N):
rx,ry,rz,rv = np.random.randint(0,10,N), np.random.randint(0,10,N), np.random.randint(0,2,N), np.random.uniform(low=0.0, high=1.0, size=N)
return rx,ry,rz,rv
N = 100
nx, ny = 2, 2
gx,gy = get_base_grid(nx,ny)
rx,ry,rz,rv = get_raw_points(N)
plot_grid(gx,gy,rx,ry,0,0,f10)
def get_the_points_inside(gx,gy,rx,ry):
#----- run throuh the g-grid -------------------------------
nx,ny = gx.shape
N = len(rx)
index = []
for jx in range(0,nx-1):
for jy in range(0,ny-1):
#--- run through the r_points
for jr in range(N):
test_x = (rx[jr]-gx[jx,jy]) / (gx[jx+1,jy] - gx[jx,jy])
test_y = (ry[jr]-gy[jx,jy]) / (gy[jx,jy+1] - gy[jx,jy])
if (0.0 <= test_x <= 1.0) and (0.0 <= test_y <= 1.0):
index.append(jr)
return index
index = get_the_points_inside(gx,gy,rx,ry)
Rx, Ry, Rz, Rv = rx[index], ry[index], rz[index], rv[index]
plot_grid(gx,gy,rx,ry,Rx,Ry,f20)
linspace generates a linear space. How can I generate a grid using an arbitrary density function?
Say, I would like to have a grid from 0 to 1, with 100 grid points, and where the density of points is given by (x - 0.5)**2 - how would I create such a grid in Python?
That is, I want many grid-points where the function (x - 0.5)**2) is large, and few points where the function is small. I do not want a grid that has values according to this function.
For example like this:
x = (np.linspace(0.5,1.5,100)-0.5)**2
The start and end values have to be chosen so that f(start) = 0 and f(end)=1.
In that case the following solution should work. Be sure that func is positive throughout the range...
import numpy as np
from matplotlib import pyplot as plt
def func(x):
return (x-0.5)**2
start = 0
end = 1
npoints = 100
x = np.linspace(start,end,npoints)
fx = func(x)
# take density (or intervals) as inverse of fx
# g in [0,1] controls how much warping you want.
# g = 0: fully warped
# g = 1: linearly spaced
g = 0
density = (1+g*(fx-1))/fx
# sum the intervals to get new grid
x_density = np.cumsum(density)
# rescale to match old range
x_density -= x_density.min()
x_density/= x_density.max()
x_density *= (end-start)
x_density += start
fx_density = func(x_density)
plt.plot(x,fx,'ok',ms = 10,label = 'linear')
plt.plot(x_density,fx_density,'or',ms = 10,label = 'warped')
plt.legend(loc = 'upper center')
plt.show()