Testing the "org.apache.commons.math3.analysis.interpolation"

Hi all,
I'm trying to test the math3 interpolation to find a desired interpolated value based on a x input. I've got the following code:

import system
	from org.apache.commons.math3.analysis.interpolation import LinearInterpolator
	from org.apache.commons.math3.analysis.polynomials import PolynomialSplineFunction
	
	# Example data points
	x_data = [18, 19, 20, 21, 22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]  # Replace with your x values
	y_data = [35,39,43,44,45,47,49,51,52,52,53,54,55,55,56,57,57,58,58,59,59,59,60,60,60]  # Replace with your y values
	
	# Create a linear interpolator
	interpolator = LinearInterpolator()
	interpolation_function = interpolator.interpolate(x_data, y_data)
	
	# Find the value on the curve at a given point
	x_value = 18  # Replace with the desired x value
	interpolated_value = interpolation_function.value(x_value)
	
	# Print the interpolated value
	system.tag.writeAsync("[default]site_lathe/graph/DesiredRecovery",interpolated_value)

It worked!
Then, I changed the xvalue = 18 to something like x_value = system.tag.read(" a path to a memory float tag"), and error in gateway said:

org.python.core.PyException: TypeError: value(): 1st arg can't be coerced to org.apache.commons.math3.analysis.differentiation.DerivativeStructure, double

Any advises will be much appreciated!

Ignition's tag reading functions return Qualified Value objects. You probably want something more like: x_value = system.tag.read(" a path to a memory float tag").value

1 Like

Spot on PGriffith!

Hello folks,
Just need some clarity.
I found below lines code in one of the Ignition exchange projects.

# Java Imports
from org.apache.commons.math3.ml.clustering import Clusterable
from org.apache.commons.math3.ml.clustering.evaluation import ClusterEvaluator
from org.apache.commons.math3.ml.clustering.evaluation import SumOfClusterVariances
from org.apache.commons.math3.ml.clustering import CentroidCluster

I have doubt that are these lines pointing to apache website or these libraries are already in Ignition? If yes then where are they located?

These math libraries are automatically included in Ignition, specifically for use in scripting. They're on the filesystem on the gateway, but you shouldn't do anything to those files on disk. The import statement simply loads the data that's already there into the current scripting scope, it's not fetching anything over the network.

Got it. Thank you @PGriffith