Set points outside plot to lower and upper limit - python-2.7

Maybe this question exists already, but I could not find it.
I am making plots in Python. I don't want to set my axes range such that all points are included - there are some really high or really low values, and all I care about in those points is that they exist - that is, they need to be in the plot, but not on their actual value - rather, somewhere on the top of the canvas.
So i found something that helps a bit in achieving what i want to do in this question Link
So basically this thing works:
xmax=0.18
plt.(np.minimum(x,xmax),y)
But when I tried something like this then it didn't work.
xmin=0.8
xmax=0.18
plt.(np.minimum(x, xmin,xmax),y)
How can i solve this?

To force the points above a threshold to a maximum level you may use np.minimum(x,xmax).
To force the points below a threshold to a minimum level you may use np.maximum(x,xmin).
To do both you may combine the two commands
xlimited = np.minimum(np.maximum(x,xmin),xmax)
Note that to have the points restricted in the vertical direction you would do this to the y values of course.

Related

How to cluster a group of near points to single point?

I'm using ORB in openCV3 C++ to detect some features in image and get back the real coordinates. But I'm having some points that are very very near to each other which I don't need I just need one of them.
X=[0.493953,0.490301,0.540664,0.575473,0.423641,0.49213,0.366055,0.395635,0.488464,0.486621,0.49213,0.358992,0.397844,0.575473,0.397844,0.425734,0.576992,0.580014,0.425734,-0.810798];
Y=[0.141909,0.154724,-0.03982,0.260174,-0.0699365,0.140797,0.121944,0.31197,0.13856,0.153795,0.137043,0.0239328,0.310085,0.256748,0.312835,-0.0683147,0.255281,0.253498,-0.0629622,-0.932006];
I need to group the near points from the x and their corresponding in Y in a new array so that it will be:
X_new=[-0.810798, 0.358992, 0.395635, 0.423641, 0.486621, 0.540664, 0.576992]
y_new=[-0.932006,0.0239328, 0.31197, -0.0699365, 0.153795, -0.03982, 0.255281]
I tried first to sort the data from x and run nested loops and if condition based on the distance between the x coordinates, but I didn't get the output as needed.
As I said in the comments, a nicer idea will be to use k means clustering. Although, you may not get the exact same points as you mentioned in your question, yet those will be a good approximation of what you desire to achieve. Hope it helps.

Eliminating outliers / best fit for "spiky" data in Python

Fairly new to processing data like this; I have two curves that I'm not sure how to process, but I know what I'd like to have as an outcome. The original plots of two datasets are shown below (left); the rough fit that I think I would like to have for them is shown to below (right) with the overlayed fit in red.
First example:
The sudden drops in amplitude are an artifact on how the data was taken. This means it's inherently unpredictable, and I would ideally like to find a method that is robust to this behavior.
In the first case, I could try to eliminate the sharp drops in amplitude by using a threshold, but that would not help me in the second case:
,
where I still get strong oscillation, but the minima are no longer at 0.
Edit: After writing a short script to use #JamesPhillips suggestion, fitting results are shown below; can confirm this is what I was looking for, and works better/faster than other fitting algorithms.
and
A possible algothm: filter the data something like this...
Start with the smallest X-valued point shown on the graph, iterating from smallest X value to largest X value. For each point:
1) If the next point's Y value is greater than or equal to this point's Y value, include it.
2) If the next point's value is less that [cutoff] percent of this point's Y value, exclude it.
3) Go to next point.
Run the filter and test different values for [cutoff], each time graphing the result to see if the value of [cutoff] meets your requirements. You may need an additional filter condition or two, but that should be a good start to filtering the data as you describe.

Opencv: Hough Circles Automating parameters?

I'm currently working with Hough Circles. Are there any methods to automatically find suitable parameters for the Hough circles? Right now, I am just manually changing values until it draws the circles correctly.
I think you should also look at http://www.cse.yorku.ca/~kosta/CompVis_Notes/isophote_curvature.pdf and http://www.science.uva.nl/research/publications/2008/ValentiCVPR2008/CVPR%2008.pdf
This will help you find isophote curvature, values for your image. Curvature is inverse of curve radius at a point. After you calculate isophote curvature values for every pixel, you'll have range of radius values you should check.
If you are able to evaluate the output of Hough Circles automatically, a brute force search should be enough for most of the cases. Just loop over all possibilities for all parameters and take the values that gave the best result.
If you need to speed things up, you can reduce the space search by locking some parameters to values you already know work fine or reducing its range.
Another option for more accurate searching is using a Genetic Algorithm.
If you have an idea what size circles you are looking for, then it would be best to set min_radius and max_radius accordingly. Otherwise, it will return anything circular of any size and your total purpose is destroyed
Parameters 1 and 2 don't affect accuracy as such, more reliability. Param 1 will set the sensitivity; how strong the edges of the circles need to be. Too high and it won't detect anything, too low and it will find too much clutter. Param 2 will set how many edge points it needs to find to declare that it's found a circle. Again, too high will detect nothing, too low will declare anything to be a circle. The ideal value of param 2 will be related to the circumference of the circles.

basic idea to implement contour plotting of image

I've been using matplotlib.pyplot.contour to plot the contour of images, and I wonder how to implement the contour plotting, but I found that the code of pyplot.contour hard for me to understand. I have this idea that, for a grayscale image, each pixel has an intensity value, to plot its contour, I might choose a set of intensity values, like partition the range [min-intensity-value, max-intensity-value] to 10 segments [min-intensity-value, val0, val1,..., val8, max-intensity-value], then for each segment's boundary intensity value (like val0, val1,...,val8), find out all those pixels which has the same intensity value, and I think those pixels will form a contour line.
Is my idea a right way to go? Hope anyone can give me a basic idea about how to implement it.
Thanks.
Your idea of connecting all values with the same colour won't work, as there is no guarantee that these are connected. Also, consider e.g. the case of two equal-coloured pixels touching at the corner: There is no way to say whether these are connected or e.g. the other diagonal.
I believe the question is basically what you want to achieve. If you just want to vectorize the image, there are existing tools for that, like e.g. POtrace. If you need something special or have special input data, then you will get better results when you tell people about this. In that case, I would also take an hour or two to look up the very good description for the POtrace algorithm from their website, maybe you can borrow a few good ideas from it?

Sustaining value in specified range

Lets say i have a point with its position on 2d plane.
This point is going to change it position randomly, but thats not the point, so lets assume that it has its own velocity and its moving on plane with restricted width and height;
So after a while of movement this point is going to reach plane boundary.
But its not allowed to leave plane.
So now i can check point position each frame to see is it reached bound or not.
if(point.x>bound.xMax)point.x=bound.xMax
if i want point to teleport itself to second side of plane i can simply :
point.x = point.x%bound.xMax;
but then i need to store point position in integers.
For 10 milion values on my corei7 1.6 both solutions
have similar timings. 41ms vs 47 on second,
so there is no sense in using modulo function in that case, its faster to just check value.
But, is there any kind of trick to make it faster?
Multiple threads for iterating array approach is not a solution.
Maybe i can scale my bound value to some wierd value and for example discard a part of binary interpretation of position value.
And if there is some trick to do it i think that somebody did it before me :)
Do you know any kind of solution that could help me?
If there is some way you can add information around the plane coordinates you could very well make a "border" around the plane which contains a value that is identified as "out of boundaries". For example if you have a 10x10 board, make it 12x12 and use the 2 extra rows and columns to insert that information.
Now you can do (pseudo-code):
IF point IN board IS "out of boundaries value" THEN
do your thing
END IF
Note that this method is only an optimization if your point has both x and y values (my assumption on your case).