cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
spacejmpuser
Level II

How to find both peaks and valleys in a data using JSL

I am new to creating scripts and having trouble creating one that labels the peaks and valleys on the data.

The goal is to actually label all valleys after the first Peak is detected. I am trying to simplify it by creating a script that graphs the data and labels all peaks and valleys to allow easier data analysis by my team. Tried to follow past instructions and it seems I just can't get it to work. 

I have tried to explore all existing solutions for this but had trouble implementing a solution that worked for me. 

1 ACCEPTED SOLUTION

Accepted Solutions
lwencx
Level II

Re: How to find both peaks and valleys in a data using JSL

CoverMarmoset89_0-1761028166706.png

dt = Open("$DESKTOP/Sample.jmp");
 
max_value = Col Maximum(Column(dt, "Heat"));
 
total_rows = N Row(dt);
Time_col = Column(dt, "Heat");
row_num = 0;
For(i = total_rows, i >= 1, i--,
	If(Time_col[i] == max_value,
		row_num = i;
		Break();
	)
);
max_time = :Time[row_num];
 
col_data = Column(dt, "Heat") << Get Values;
min_data = col_data[row_num :: total_rows];
min_value = Minimum(min_data);
min_positions = Loc Min(min_data);
min_time_col = min_positions + (row_num - 1);
min_time = :Time[min_time_col];
 
Graph Builder(
	Size(1032, 646),
	Show Control Panel(0),
	Fit to Window("Off"),
	Variables(X(:Time), Y(:Heat)),
	Elements(Line(X, Y, Legend(14))),
	SendToReport(
		Dispatch({}, "Time", ScaleBox,
			{Format("Best", 12), Min(1.88), Max(5.07800951567129), Inc(0.5), Minor Ticks(4),
			Add Ref Line(
				max_time,
				"Dotted",
				"Black",
				Char(max_time),
				2,
				1,
				Label Settings({Label Color("Red")})
			), Add Ref Line(
				min_time,
				"Dotted",
				"Black",
				Char(min_time),
				2,
				1,
				Label Settings({Label Color("Red")})
			)}
		),
		Dispatch({}, "Heat", ScaleBox,
			{Add Ref Line(
				max_value,
				"Dotted",
				"Black",
				Char(max_value),
				2,
				1,
				Label Settings({Label Color("Red")})
			), Add Ref Line(
				min_value,
				"Dotted",
				"Black",
				Char(min_value),
				2,
				1,
				Label Settings({Label Color("Red")})
			)}
		)
	)
);

View solution in original post

9 REPLIES 9
jthi
Super User

Re: How to find both peaks and valleys in a data using JSL

Your data seems to have character data type which at least requires changing the data to numeric/continuous. Could you use your data, create a plot and mark what you are interested in finding/detecting. Usually you can find peaks/valleys using some combination of Lag/Dif and maybe some if statements

-Jarmo
lwencx
Level II

Re: How to find both peaks and valleys in a data using JSL

CoverMarmoset89_0-1761028166706.png

dt = Open("$DESKTOP/Sample.jmp");
 
max_value = Col Maximum(Column(dt, "Heat"));
 
total_rows = N Row(dt);
Time_col = Column(dt, "Heat");
row_num = 0;
For(i = total_rows, i >= 1, i--,
	If(Time_col[i] == max_value,
		row_num = i;
		Break();
	)
);
max_time = :Time[row_num];
 
col_data = Column(dt, "Heat") << Get Values;
min_data = col_data[row_num :: total_rows];
min_value = Minimum(min_data);
min_positions = Loc Min(min_data);
min_time_col = min_positions + (row_num - 1);
min_time = :Time[min_time_col];
 
Graph Builder(
	Size(1032, 646),
	Show Control Panel(0),
	Fit to Window("Off"),
	Variables(X(:Time), Y(:Heat)),
	Elements(Line(X, Y, Legend(14))),
	SendToReport(
		Dispatch({}, "Time", ScaleBox,
			{Format("Best", 12), Min(1.88), Max(5.07800951567129), Inc(0.5), Minor Ticks(4),
			Add Ref Line(
				max_time,
				"Dotted",
				"Black",
				Char(max_time),
				2,
				1,
				Label Settings({Label Color("Red")})
			), Add Ref Line(
				min_time,
				"Dotted",
				"Black",
				Char(min_time),
				2,
				1,
				Label Settings({Label Color("Red")})
			)}
		),
		Dispatch({}, "Heat", ScaleBox,
			{Add Ref Line(
				max_value,
				"Dotted",
				"Black",
				Char(max_value),
				2,
				1,
				Label Settings({Label Color("Red")})
			), Add Ref Line(
				min_value,
				"Dotted",
				"Black",
				Char(min_value),
				2,
				1,
				Label Settings({Label Color("Red")})
			)}
		)
	)
);

jthi
Super User

Re: How to find both peaks and valleys in a data using JSL

Is there something that isn't being done by that script?

-Jarmo
spacejmpuser
Level II

Re: How to find both peaks and valleys in a data using JSL

Thank you for taking the time to share this code! I am running into a problem where when I run the same script the chart that is generated is very different. what am I doing wrong? 

jthi
Super User

Re: How to find both peaks and valleys in a data using JSL

If you use the sample table provided it won't work unless you fix the column data types to numeric and modeling types to continuous. My guess is that you didn't do the modeling type conversion.

-Jarmo
spacejmpuser
Level II

Re: How to find both peaks and valleys in a data using JSL

Thank you, after updating everything the chart shows a mean line for summary statistic. It now shows the peak and valley so I will try to figure out how to make the line show the actual data. 

spacejmpuser
Level II

Re: How to find both peaks and valleys in a data using JSL

I got everything to work, accepted as solution. However when I try to do chart builder it keeps making the line a mean. I'm assuming something in the script is causing this. Can anyone support in addressing this issue? 

jthi
Super User

Re: How to find both peaks and valleys in a data using JSL

It will use mean line because you have multiple measurements happening exactly at the same time.

Edit: Make sure your data is ordered correctly and then use Row order

jthi_0-1761068155941.png

This way it shouldn't be using summary statistics and will order the data on x-axis based on the row order of your table

-Jarmo
spacejmpuser
Level II

Re: How to find both peaks and valleys in a data using JSL

Thank you, the solution was to ensure all data columns were labeled as numeric and continuous. Thank you all for your patience while helping me.

Recommended Articles