I am trying get the count of each series point over specific areas of my plot.
The plot is made up of grids (boxes) and I wish to know the count of each of my series points that is present in each of these boxes. I want to get information like (grid 1 had 2 of series 1, 0 of series 2, 3 of series3, 4 of series 5, etc)
Any help is greatly appreciated.
When you have XYItems you can get the bounds of each item:
final Collection<ChartEntity> entities =
chartpanel.getChartRenderingInfo().getEntityCollection().getEntities();
for (final ChartEntity e : entities) {
if (e instanceof XYItemEntity) {
final XYItemEntity xyItem = (XYItemEntity) e;
final int index = xyItem.getItem();
final int series = xyItem.getSeriesIndex();
Rectangle2D r = e.getArea().getBounds2D();
checkPosition(r); // here you can check if the coordinates are inside your "box"
}
}
Related
I have this picture:
I want to create a mask from this image, to place on top of the original image. The mask I want to obtain is the black part at the top.
I tried to use simpleBlobDetector from OpenCV to try to detect the white part as one big blob. I do not obtain the results I am hoping for and am unsure what to do.
R has been used, but my question is not specifically on how to achieve this in R. The result I have is below the code.
library(Rvision)
x <- simpleBlobDetector(im, min_threshold = 0, max_threshold = 255)
plot(x)
I do not understand why those three black boxes are selected as blobs, while there are a lot more of those black boxes that are not selected.
EDIT: when I add blob_color = 255 so white blobs are searched, nothing is detected.
You can do something like this using OpenCV:
// read input image
Mat inputImg = imread("test1.tif", IMREAD_GRAYSCALE);
// create binary image
Mat binImg;
threshold(inputImg, binImg, 254, 1, THRESH_BINARY_INV);
// compute connected components
Mat labelImg;
connectedComponents(binImg, labelImg, 8, CV_16U);
// compute histogram
Mat histogram;
int histSize = 256;
float range[] = { 0, 256 } ;
const float* histRange = { range };
calcHist(&labelImg, 1, 0, Mat(), histogram, 1, &histSize, &histRange, true, false);
// retrieve maximal population
float maxVal = 0;
int maxIdx;
for (int i=1; i<histSize; ++i) {
if (histogram.at<float>(i) > maxVal) {
maxVal = histogram.at<float>(i);
maxIdx = i;
}
}
// create output mask with bigest population
Mat resImg;
threshold(labelImg, labelImg, maxIdx, 0, THRESH_TOZERO_INV);
threshold(labelImg, resImg, maxIdx-1, 1, THRESH_BINARY);
// write result
imwrite("res.tif", resImg);
And you should obtain something like this:
I think you can convert input to bianry, then extract connected components, compute associated histogram and simply keep (by thresholding) histogram class with highest population
Regards
This is my solution to Course Scheduling Problem from leetcode. I am looking for any suggestions to improve my code, even slightest ones.
Here is the question:
There are a total of n courses you have to take, labeled from 0 to n-1.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.
There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
Example 1:
Input: 2, [[1,0]]
Output: [0,1]
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1] .
Example 2:
Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
Here is my solution:
class Solution:
def findOrder(self, numCourses, prerequisites):
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
#Convert prerequisites into an adjacency list
adj = []
for i in range(numCourses):
adj.append(set())
for pair in prerequisites:
adj[pair[0]].add(pair[1])
def DFSHelper(s):
visited.add(s)
stack.add(s)
for neighbor in adj[s]:
# if neighbor vertex has never been visted before, there is no way it could be a backedge.
# visit this unvisited vertex
if(neighbor not in visited):
if(not DFSHelper(neighbor)):
return False
Sorted.append(neighbor)
else:
if(neighbor in stack):
return False
stack.remove(s)
return True
visited = set()
stack = set()
Sorted = []
for j in range(len(adj)):
if(j not in visited):
if(not DFSHelper(j)):
print(j)
return []
Sorted.append(j)
return Sorted
I first converted given prerequisites list into an adjacency list representation of graph, then did topological sorting of the graph. I used DFS recursively to topologically sort the graph. The list Sorted stores the result of sorting. While doing DFS I also checked if the graph contains any cycle, if it does just return []. For purpose of checking cycle I maintained a set called stack that stores all the vertices that are currently in call stack.
This is a simple question first create a graph and then find topological sorting on nodes.
If topological order contains all nodes then we have our answer else not possible to finish all the courses.
class Solution {
public int[] findOrder(int n, int[][] prerequisites) {
List<Integer>[] g = new ArrayList[n];
for(int i = 0; i < n; i++)g[i] = new ArrayList();
int[] deg = new int[n];
for(int[] e: prerequisites) {
g[e[1]].add(e[0]);
deg[e[0]]++;
}
Queue<Integer> q = new LinkedList();
for(int i = 0; i < n; i++) {
if(deg[i] == 0)q.add(i);
}
int[] res = new int[n];
int idx = 0;
while(!q.isEmpty()) {
int u = q.poll();
res[idx++] = u;
for(int v: g[u]) {
deg[v]--;
if(deg[v] == 0) q.add(v);
}
}
return idx == n ? res: new int[0];
}}
I need to find the first number in any of the rows as shown on this: (http://puu.sh/rbVEJ/10a2086c82.png). I got it to work for only rowStart(6); but for nothing else. Can anyone help?
class ShelfRows{
public static void main (String[] args){
rowStart(6); // ans = 16
rowStart(10); // ans = 46
}
public static int rowStart(int row){
int n = row - 1;
if(n == 0) return 1;
return n*2 + rowStart(n);
}
}
The row starting number is count of squares in all the rows below. One can easily see that they are organized in triangle. Space taken by triangle is half of space taken by a rectangle. The exact formula for your example is:
public static int rowStart(int row){
return (row * (row - 1))/2;
}
the row - 1 vs row is to account for the diagonal squares. Instead of being "split" (to create a proper triangle) they add row/2 of squares to the smooth triangle. (Hope it can be understood...).
Read about trianglular numbers.
smallest_in_n-th_row = n*(n-1)+1
How can I generate or update variables without using a loop? mutate doesn't work here (at least I don't know how to get it to work for this problem) because I need to calculate stuff from multiple rows in another data set.
I'm supposed to replicate the regression results of an academic paper, and I'm trying to generate some variables required in the regression. The following is what I need.
I have 2 relevant data sets for this question, subset (containing
geocoded residential property transactions) and sch_relocs (containing the date
of school relocation events as well as their locations)
I need to calculate the distance between each residential property and the nearest (relocated) school
If the closest school is one that relocated to the area near the residential property, the dummy variable new should be 1 (if the school relocated away from the area, then new should be 0)
If the relocated school moved only a small distance, and a house is within the overlapping portion of the respective 2km radii around the school locations, the dummy variable overlap should be 1, otherwise 0
If the distance to the nearest school is <= 2km, the dummy variable in_zone should be 1. If the distance is between 2km and 4km, these transactions are considered controls, and hence in_zone should be 0. If the distance is greater than 4km, I should drop the observations from the data
I have tried to do this using a for loop, but it's taking ages to run (it's still not done running after one night), so I need a better way to do it. Here's my code (very messy, I think the above explanation is a lot easier if you want to figure out what I'm trying to do.
for (i in 1:as.integer(tally(subset))) {
# dist to new sch locations
for (j in 1:as.integer(tally(sch_relocs))) {
dist = distHaversine(c(subset[i,]$longitude, subset[i,]$latitude),
c(sch_relocs[j,]$new_lon, sch_relocs[j,]$new_lat)) / 1000
if (dist < subset[i,]$min_dist_new) {
subset[i,]$min_dist_new = dist
subset[i,]$closest_new_sch = sch_relocs[j,]$school_name
subset[i,]$date_new_loc = sch_relocs[j,]$date_reloc
}
}
# dist to old sch locations
for (j in 1:as.integer(tally(sch_relocs))) {
dist = distHaversine(c(subset[i,]$longitude, subset[i,]$latitude),
c(sch_relocs[j,]$old_lon, sch_relocs[j,]$old_lat)) / 1000
if (dist < subset[i,]$min_dist_old) {
subset[i,]$min_dist_old = dist
subset[i,]$closest_old_sch = sch_relocs[j,]$school_name
subset[i,]$date_old_loc = sch_relocs[j,]$date_reloc
}
}
# generate dummy "new"
if (subset[i,]$min_dist_new < subset[i,]$min_dist_old) {
subset[i,]$new = 1
subset[i,]$date_move = subset[i,]$date_new_loc
}
else if (subset[i,]$min_dist_new >= subset[i,]$min_dist_old) {
subset[i,]$date_move = subset[i,]$date_old_loc
}
# find overlaps
if (subset[i,]$closest_old_sch == subset[i,]$closest_new_sch &
subset[i,]$min_dist_old <= 2 &
subset[i,]$min_dist_new <= 2) {
subset[i,]$overlap = 1
}
# find min dist
subset[i,]$min_dist = min(subset[i,]$min_dist_old, subset[i,]$min_dist_new)
# zoning
if (subset[i,]$min_dist <= 2) {
subset[i,]$in_zone = 1
}
else if (subset[i,]$min_dist <= 4) {
subset[i,]$in_zone = 0
}
else {
subset[i,]$in_zone = 2
}
}
Here's how the data sets look like (just the relevant variables)
subset data set with desired result (first 2 rows):
sch_relocs data set (full with only relevant columns)
Y axis tick label should only show non decimal values / whole numbers as as series . if i set set TickUnit to 1 it should be 1,2,3,4,5,.. if i set Unit Ticks to 2 ..2,4,6,8,.. if i set to 5 5,10,15,20,25.
i set the Unit Ticks to 1 still it sometimes adding the decimal values also and showing 2.5 ,5.0,7.5,10.0,12.5......how to prevent this and show only whole numbers(Non decimal Numbers).?
option 1. store the number as an int, int num = (int)Math.floor(myDouble);
option 2. in your method make the parameter a double and inside the method cast it to an int
this will allow you to use the method with both a double and a int. Please keep in mind that this is C# code but java should be very similar.
private List<int> numberSeries(double aDouble)
{
List<int> number = new List<int>();
int base = (int)Math.floor(aDouble);
for(int x = 1;x++ < 10) //change 10 to whatever you want
{
number.Add(aDouble * x);
}
return number;
}