Treatment of non-finite values in doubles array

12 views
Skip to first unread message

Demitri Muna

unread,
Jan 17, 2017, 2:36:29 PM1/17/17
to coreplot-discuss
Hi,

Objects that speak the CPTPlotDataSource protocol have the option to return an array of double, e.g. doublesForPlot:field:recordIndexRange. For the method numbersForPlot:field:recordIndex, the presence of an NSNull instead of an NSNumber tells Core Plot to just skip that point. Would it be possible for Core Plot to do the same for double values that fail isfinite()?

I know one answer would be to just use an NSNumber array, but it feels like inf, -inf, and NaN values are unplottable in the same way that an explicit NSNull is.

I'm happy to put in a patch, but I thought I'd ask the question here first, and, if a patch is deemed a good idea, a pointer to where in the code this should be made.

Thanks,
Demitri

Eric

unread,
Jan 17, 2017, 7:30:44 PM1/17/17
to coreplot-discuss
Core Plot already treats NaN and NSNull as missing values. It doesn't check for infinity—I'm not sure what would happen. The proper response probably depends on the plot type.

Each plot has its own drawing code which is where the data is converted to drawable values. The relevant sections of the code are marked with #pragma mark - statements.

Eric

Demitri Muna

unread,
Jan 17, 2017, 7:42:43 PM1/17/17
to coreplot-discuss
Hi Eric,

I'm currently working with a CPTScatterPlot. When I have inf values in my data (passed in the array of doubles), none of the plot is currently drawn. When I replace the inf values with zero, the plot draws ok. This is a workaround at the moment since this is not an accurate representation of the data.

I found in CPTScatterPlot.m:

-(void)calculatePointsToDraw:(nonnull BOOL *)pointDrawFlags forPlotSpace:(nonnull CPTXYPlotSpace *)xyPlotSpace includeVisiblePointsOnly:(BOOL)visibleOnly numberOfPoints:(NSUInteger)dataCount


this line:

nanFlags[i] = isnan(x) || isnan(y);


and changed it to: 

nanFlags[i] = !isfinite(x) || !isfinite(y); //isnan(x) || isnan(y);


but this didn't make a difference when I left the inf values (i.e. blank plot). Is there another part of the code I should be looking?

Cheers,
Demitri

Eric

unread,
Jan 17, 2017, 8:22:03 PM1/17/17
to coreplot-discuss
You found the right place in the scatter plot code. That should be the only code that checks for missing values. The rest of the drawing code just uses the flags in that array.

None of the data is plotted when only some of the values are inf? Remember that it takes two adjacent point to draw the connecting line. You won't see the data points individually unless the plot uses plot symbols.

I just did a quick test with the "Control Chart" in the Plot Gallery example app. Infinite values behaved like NaN when plotting, but caused issues with -scaleToFitPlots: (the resulting plot range had a infinite length). Watch out for that and you should be ok.

Eric

Demitri Muna

unread,
Jan 18, 2017, 2:10:52 AM1/18/17
to coreplot-discuss
Yep, found the problem - it was similar to what you describe. My code was calculating a min/max value to set the range of the plot. When I excluded inf values from that, things worked as expected. Thanks!

Demitri
Reply all
Reply to author
Forward
0 new messages