We are about to switch to a new forum software. Until then we have removed the registration on this forum.
In the code bellow i load a csv file and i want to find the maximum value from the column.. Where the name(header) of the column in the csv file is values. But i get the error NullPointerException
String filename = "abc.csv";
String[] rawData;
float[] values;
void setup() {
rawData = loadStrings(filename);
for (int i=1; i<rawData.length; i++) {
String[] thisRow = split(rawData[i], ",");
values[i-1] = int(thisRow[1]);
}
if (values.length == 0)
{
println("nothing in the array.");
} else {
float maxValue = values[0];
int maxIndex = 0;
for (int i = 1; i < values.length; i++) {
if ( values[i] > maxValue) {
maxValue = values[i];
maxIndex = i;
}
}
print("max value is " + maxValue);
println(" at position " + maxIndex);
}
}
void draw() {
}
Answers
We've moved to a new Forum...
Your code doesn't set up the values array anywhere. You'll need a line somewhere like this:
How big does it need to be?
And what about this forum??Will it close??
For ".csv" & ".tsv" files, use loadTable() in place of loadStrings():
https://Processing.org/reference/loadTable_.html
table = loadTable("abc.csv", "header");
Then, set the datatype of the "values" column:
table.setColumnType("values", Table.INT);
Now let's sort that "values" column via method sortReverse():
https://Processing.org/reference/Table_sort_.html
table.sortReverse("values");
Now we know the TableRow w/ the max value in column "values" is the 1st 1 (index 0):
https://Processing.org/reference/Table_getInt_.html
int maxValue = table.getInt(0, "values");
Of course I'm assuming the values within column "values" are of datatype
int
:https://Processing.org/reference/int.html
If they're
float
instead instead: https://Processing.org/reference/float.htmlSimply replace Table.INT w/ Table.FLOAT & getInt() w/ getFloat():
https://Processing.org/reference/Table_getFloat_.html
Thank you so much @GoToLoop
Something more...How can i locate the corresponding value to the maxvalue in the other column?For example at y axes the maximum value is 5,7 that corresponds at value 0.35 in x axes.
I read about
but when i add this function to my programm, i ''ll get the error of NullPointerException
@GoToLoop any idea???
this is the code bellow :
and the csv file is bellow:
now after i run this code i 'll get this message:
max value is 0.9 at time null
But i do not know why maxTime is null
Method Table::findRow() is for seeking out the 1st TableRow it finds which contains a specific element within a column: :-B
https://Processing.org/reference/Table_findRow_.html
It's a dumb vanilla searching though. It isn't smart enough for finding out elements which match a customized conditional test, such as: :@)
B/c the Table class doesn't provide such advanced filtering methods, we'd need to write our own! #:-S
You might need to store your data in a hash map and use functions available in java for these containers. If you do a search in the forum, you can find some examples where this has already been done. These should not be difficult to implement as you only have two columns. If you have more columns, then there is the challenge to manage them all together. Read this just as a little bit extra work.
Try implementing the hashmap and post your code, and I should be able to assist tonight.
Kf
So i did not understand what am i supposed to do?What's the next step???
I've already stated: If a class doesn't provide the features we need we'd need to write our own! 8-X
So you're now requesting the TableRow w/ the max value for both columns "Time" & "Intensity".
This is the function I've just written for it. Still untested though: :-\"
i test it but nothing...
If i have a bigger csv file, in which there is a max value about every 100seconds or 100 values..Is there a way to find how many max values appear in 1 minute for example.How many times do i have a beat or a pulse(max value) in one minute.. I think this is more helpfull for my project than finding the row of the maxValue.
This is beat detection aka peak detection aka finding local maxima. There are hundreds of implementations of a basic sliding window out there if you search those terms + Processing or + Java-- you may want to use an existing library or algorithm for this, rather than write your own.
Could you please give me an example or provide me a page..I am lost on the internet but i couldn't find something similar..
Here is the top search hit, which contains an example of working code marked as an answer:
https://stackoverflow.com/questions/13157510/finding-the-local-min-max-in-a-1d-array-histogram
There is an example that you could explore using hashMaps here: https://forum.processing.org/two/discussion/26371/how-to-create-a-heatmap-from-list-of-x-y-points
You don't have to use this but you could explore it and see if it could fit your project.
You need to design this part. It is hard to do this part as we don't own your project and we don't know anything about your design. For example, checking the max every 100 seconds or 100 points are two different things, unless if you are collecting 1 pt every seconds, so doing it either way should be the same. However, we do not know the sampling rate of your data, the size of your data, the format of your data, the structure of your table, and how everything fits together. Remember, every time we come back to a post, we forget the details about the post. This is my approach. Unfortunately I cannot go back and read the previous comments to figure out if something has change in your project. The best thing you can do is to provide a question with all the needed details and as concise as possible.
Also it is important to provide a sample data set. This way everybody should aim to get the same result and removes a level of complexity.
Kf
I want to find the rate of the plot or the rate of the intensity…So my plan is to find the max values of the intensity and then in which time.So then i can calculate how many times i one period (time interval) i have max values.After that i will have find the rate which is times(that the max values of signal appears) per period(time).For example here i see from the plot that i have 2 max values about every 200(time) but i want these to make it java code for processing.
and the dataset:
The code you posted above detects the maximum as it will be the first entry in the data set after it is reverse sorted.
How to find more than one maximum? You take the second entry and see if it classifies as a maximum of interest. In general, you can have more max values in you data set if they all have the same value. The algorithm below (a copy of yours) only shows the first max value. In case of multiple max values, it will show only the first one found in the list.
To show multiple max values that are the same value, you need to iterate through top elements in the list.
If you have multiple max values that are not the same value, then you need to set a threshold. Anything above a threshold is a max value, and you process them accordingly.
Simplified version of your code below.
Kf
If i set a threshold, how can i know **how many times **in one period or in one second will i have max values, values that are above threshold.
You capture the max values and their respective times. Setting up a threshold is one approach. It depends on the nature of your data. For instance, if you are collecting data regarding sun intensity as detected by a single sensor on a single spot during a day, you will have a maximum when the sun is right above the sensor (at noon). Notice this gives you not a single max but a range of max that could be in hours. Now, if you have cloud coverage, your sensor will detector the cloud coverage and you have max only when the sensor is not covered by a cloud.
The nature of the data dictates how to extract the maximum values.
In your data, you can have a look at your values and see if they do show up as single entries every so many entries. if it doesn't, then before implementing anything else, you will need to strictly defined how to extract the ma values (of interest) from your data set and based on this, one implements the algorithm. I hope this is clear.
This would be a good opportunity to explore Processing-R flavour to access some stat tools.
Kf
i did not understand this part :
But to explain you more..In the photo bellow the intensity is shows up..As you see in the graph there are about 3 max values(values above 0.8) every second..This is what i want to describe with java code..I want a function that can calculate how many times has max value( value above 0.8) per second(in one second).
Please provide this data set. If you want to detect each peak, then you need to [1]set a threshold, [2]extract values above threshold and [3] pick each section's centroid.
Kf
This is the dataset:
Time,Intensity 10,0.2 20,0.4 30,0.4 40,0.3 50,0.6 60,0.6 70,0.9 80,0.7 90,0.5 100,0.5 110,0.3 120,0.1 130,0.3 140,0.5 150,0.6 160,0.4 170,0.3 180,0.1 190,0.1 200,0.2 210,0.2 220,0.3 230,0.1 240,0.2 250,0.5 260,0.6 270,0.6 280,0.7 290,1.2 300,1.3 310,1.1 320,0.9 330,0.6 340,0.6 350,0.5 360,0.3 370,0.3 380,0.1 390,0.1 400,0.2 410,0.4 420,0.4 430,0.6 440,0.6 450,0.7 460,0.7 470,0.7 480,0.9 490,1 500,0.8 510,0.5 520,0.5 530,0.2 540,0.2 550,0.2 560,0.1 570,0.1 580,0.1 590,0.2 600,0.2 610,0.2 620,0.6 630,0.6 640,0.5 650,0.5 660,0.5 670,0.8 680,0.8 690,0.9 700,0.9 710,0.9 720,0.9 730,1.2 740,1.6 750,1.3 760,1.1 770,0.8 780,0.8 790,0.8 800,0.5 810,0.3 820,0.3 830,0.1 840,0.1 850,0.2 860,0.2 870,0.2 880,0.5 890,0.5 900,0.7 910,0.8 920,0.8 930,0.9 940,0.9 950,0.9 960,1.2 970,1.4 980,1.1 990,1 1000,0.8 1010,0.8 1020,0.7 1030,0.5 1040,0.5 1050,0.2 1060,0.2 1070,0.1 1080,0.1 1090,0.3 1100,0.5 1110,0.6 1120,0.9 1130,1.2 1140,1.8 1150,1.6 1160,1.5 1170,1.2 1180,1.1 1190,0.9 1200,0.9 1210,0.7 1220,0.5 1230,0.3 1240,0.1 1250,0.3 1260,0.5 1270,0.6 1280,0.4 1290,0.3 1300,0.1 1310,0.1 1320,0.2 1330,0.2 1340,0.3 1350,0.1 1360,0.2 1370,0.5 1380,0.6 1390,0.6 1400,0.7 1410,1.2 1420,1.3 1430,1.1 1440,0.9 1450,0.6 1460,0.6 1470,0.4 1480,0.6 1490,0.6 1500,0.5 1510,0.5 1520,0.3 1530,0.2 1540,0.2 1550,0.3 1560,0.5 1570,0.5 1580,0.2 1590,0.2 1600,0.2 1610,0.1 1620,0.1 1630,0.1 1640,0.2 1650,0.2 1660,0.2 1670,0.6 1680,0.6 1690,0.7 1700,1.2 1710,1.3 1720,1.1 1730,0.9 1740,0.6 1750,0.6 1760,0.5 1770,0.3 1780,0.3 1790,0.1 1800,0.1 1810,0.2 1820,0.1 1830,0.1 1840,0.2 1850,0.2 1860,0.2 1870,0.5 1880,0.5 1890,0.7 1900,0.8 1910,0.9 1920,1.2 1930,1.4 1940,1.1 1950,1 1960,0.7 1970,0.7 1980,0.3 1990,0.3 2000,0.2
Based on your data, this should do it. I get the following output:
Notice that while finding the max peak intensity is sort of robust, finding the time of this maximum is not. The provided algorithm provides a time associated to the peak biased toward the left (aka. earliest time).
Kf
Thank you for your time and of course for your help..I will study this code because it is a little hard for me..But this programm shows the max value and its corresponding time..Especially i want to find how many times in one second there are peaks, for examlpe to show me that in one second there are 3 max values...
Consider this data
This data is time-peakValue info. If you take the time interval of your data set and divide by the number of peaks, you get the rate these peaks occurs.
Max-peak-rate = numberOfPeaks / (endTimeDataSet - startTimeDataSet)
Based on your data, assuming time in seconds and for a threshold of 0.8 [intensity units]:
Max-peak-rate = 9 / (2000 - 0 ) = 0.0045 Hz
In other words, you get a peak about every 222 seconds. Notice for this task you do not need to know the time those peaks occurred. You only need to count those peaks.
Kf
Thank you so much...It was very helpfull this code to me...I have one more question:
What does this function bellow exactly?
`
It extracts peaks from data. It assumes you have a flat blackground, data noise is not an issue and your peaks are gaussian in nature (well, I also make sure it can detect single points above the threshold. It is a very simple algorithm to extract peaks based on your data.
The algorithm has two parts. It scans the data from left to right. Every time the data goes above the threshold [Line 1], the algorithm activates and perform some sums and data retention [Line 5-7]. As soon as the data goes below threshold[11], the algorithm deactivates and performs the peak calculation[17,18] before it resets its fields[23] for the next peak. Peak detection is based on a centroid averaging[17]. After I find the average max intensity, I run a third algorithm[18] (not robust but acceptable) to find the time associated to this peak.
Kf
Thank you..After a long time of study i managed to understand...Two last questions i want to put this
on the code...Which is the command that takes the endTimeDataSet, that can recognise and exports the last value of timeData??? Second, if i want to calculate only the number of the peaks, is all this code necessary or can i remove some lines??
There is no command. When you process your data, you have a data that is a time series. The first entry is the beginning and the last entry is the end of your time series, thus the begin and end times of your data set.
You can remove line 18 and the associated function. Then you need to replace the array of vectors for an array of floats to store only the peak intensity and not the time-peak value pairs.
The code could be simplify by only counting the number of times a data set goes above the threshold. This approach works if you assume you don't have multiple peaks in a region that is above the threshold.
Kf
If i load differnet datasets and i dont know the end time is there a command that can find the end time or the last value of the column of time in csv table??
Also the problem is that i have multiple peaks in a region that is above the threshold.
You answer your question yourself. It is the last data row in your table. For instance:
for (int row = 0; row < table.getRowCount(); row++) {
In the above line, your loop access all the data in the table. To access the last point you could use
int time = table.getInt(table.getRowCount()-1, COL_TIME);
This is what I wanted to make clear in my prev posts: the algorithm I provided is based on an assumption that there is single peak per region, which is extracted based on the selected Threshold. If that is not the case, then you need to find another algorithm. You can do a google search for "multiple peak detection" algorithm and you will get some ideas, or at least, what to consider in order to choose a proper algorithm suitable to your data set.
Kf
I hope this is solved
There is a new forum now
I have some mini problems but i think i will solve them..Thank you all of you for your help..If i stuck again i will ask for your opinions in the new forum..Thank you again very much...
;-)