univariate_tools
Tools to fit, interpolate, synthesize univariate data.
Science Score: 52.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Academic email domains
-
✓Institutional organization owner
Organization usnistgov has institutional domain (www.nist.gov) -
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (11.4%) to scientific vocabulary
Repository
Tools to fit, interpolate, synthesize univariate data.
Basic Info
- Host: GitHub
- Owner: usnistgov
- License: other
- Language: Python
- Default Branch: main
- Size: 1.28 MB
Statistics
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
univariate_tools
Tools to fit, interpolate, and synthesize univariate data.

Installation
Method 1 - Directly from Github
shell
pip install git+https://github.com/usnistgov/univariate_tools.git
Method 2 - Clone then Install
- Use your favorite way to clone the repo or download the zip file and extract it.
- pip install to that location (note pip expects a unix style string 'C:/univariate_tools/')
shell
pip install <path to top folder>
Use case
Have you ever wanted to fit a really weird function of one variable? How about make a complicated synthetic time-domain signal? Have calibration data on an irregular grid and you need to interpolate? Then univariatetools is for you! FunctionalModel is a blend of sympy and numpy functionality that lets the user do all kinds of math with functions and then fit data. ```python from univariatetools import *
lets make a composite function
line=FunctionalModel(parameters=["m","b"],variables="x",equation="mx+b") gaussian=FunctionalModel(parameters="alpha x0 delta",variables="x",equation="alphaexp(-1(x-x0)^2/(2delta**2))") gaussline=line+gaussian xdata=np.linspace(-1,1,1000) plt.plot(xdata,gaussline(alpha=1,x0=.1,delta=.1,m=1.2,b=.1,x=xdata)) plt.title("${0}$".format(gaussline.to_latex()))
```

Workflow
Calculating Data
- To calculate a function, first create a FunctionalModel. There should be a set of parameters, one variable, and an equation.
python model = FunctionalModel(parameters=["a","b","c"],variables="x",equation="a*x**2+b*x+c") - Set the parameters
python model.set_parameters({"a":1.25,"b":3,"c":-.4}) - Calculate the result with an input of x
python x_data = np.linspace(-10,10,1000) y_data = model(x_data) plt.plot(x_data,y_data)
Fitting Data
- To fit data, start with defining a FunctionalModel.There should be a set of parameters, one variable, and an equation.
python model = FunctionalModel(parameters=["m","b"],variables="x",equation="m*x+b") - Use model.fitdata, for more complex fits provide an initial guess ```python xdata = np.linspace(-10,10,1000) ydata = 2.12*xdata+3.11 model.fitdata(xdata = xdata,ydata = ydata,initialguess = {"m":2,"b":3}) ```
- The fit parameters are now in the parametervalues
```python
model.parametervalues
python {'m': 2.12, 'b': 3.11} ``` ## Simulate Data - Specify the parameters, variable, equation, noise characteristics, xdata ```python simulateddata = DataSimulator(*{"parameters":["m","b"], "variables":"x", "equation":"mx+b", "parametervalues":{"m":2,"b":1}, "outputnoisetype":"normal", "outputnoisewidth":1, "outputnoisecenter":0, "outputnoiseamplitude":1., "randomseed":42, "x":np.linspace(-10,10,100)}) ```
- Now the simulated data is in the data attribute and the xdata is in the x attribute
```python
plt.plot(simulateddata.x,simulated_data.data)
```

Interpolate Data
This is a resampling of a data stream to a new x set of points, the methods are lowess, loess, 1d, gpr and spline. Each one of these methods has a different set of options that can be passed with keywords
python
"""
*********************************************************************************************
refs: https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html, options for kind are
‘linear’, ‘nearest’, ‘nearest-up’, ‘zero’, ‘slinear’,
‘quadratic’, ‘cubic’, ‘previous’, or ‘next’. ‘zero’, ‘slinear’, ‘quadratic’ and ‘cubic’
****************************************************************************************
https://www.statsmodels.org/dev/generated/statsmodels.nonparametric.smoothers_lowess.lowess.html
***********************************************************************************************
https://scikit-learn.org/stable/modules/gaussian_process.html
***************************************************************
https://docs.scipy.org/doc/scipy/tutorial/interpolate/smoothing_splines.html
********************************************************************************"""
- provide the data as an xdata and ydata array (need to be the same size) and decide the newxdata. There are parametric and non-parametric methods ```python xdata = np.linspace(-6,6,500) signal = np.sin(xdata)+np.random.normal(scale=.5,size=len(xdata)) plt.plot(xdata,signal,".",label="Original Data") for interptype in ["lowess","loess", "1d", "gpr","spline"]: newx = np.linspace(-2,2,500) interpdata = interpolatedata(xdata=xdata,ydata=signal,newxdata=newx,method=interptype) plt.plot(newx,interpdata,label=interptype) plt.legend() plt.show()
```

Reverse Regressor
This is used when you have a monotonic function that represents a calibration and you have new y points. It has similar methods and options as the interpolate data function. ```python xdata = np.linspace(-6,6,200) signal = 2*xdata+1+np.random.normal(scale=.2,size=len(xdata)) plt.plot(xdata,signal,".",label="Original Data",alpha=.3)
for interptype in ["lowess","loess", "1d", "gpr","spline"]:
try:
newy = np.linspace(-2,5,10)
newx = reverseregressor(xdata=xdata,ydata=signal,newydata=newy,method=interptype)
print(f"{interptype}:{newx},{newy}")
plt.plot(newx,newy,label=interp_type,linewidth=2,linestyle="dashed")
except Exception as e:
print(e)
plt.legend()
plt.show()
```

Example
An example of creating a functional model, fitting data, creating composite models and simulating data.
API Documentation
The API Documentation links to the init.py file and has the primary submodules linked.
Contact
Aric Sanders aric.sanders@nist.gov
NIST Disclaimer
Certain commercial equipment, instruments, or materials (or suppliers, or software, ...) are identified in this repository to foster understanding. Such identification does not imply recommendation or endorsement by the National Institute of Standards and Technology, nor does it imply that the materials or equipment identified are necessarily the best available for the purpose.
Owner
- Name: National Institute of Standards and Technology
- Login: usnistgov
- Kind: organization
- Location: Gaithersburg, Md.
- Website: https://www.nist.gov
- Repositories: 1,117
- Profile: https://github.com/usnistgov
Department of Commerce
Citation (CITATION.cff)
cff-version: 1.2.0 message: "If you use this software, please cite it as below." authors: - family-names: "Sanders" given-names: "Aric" orcid: "https://orcid.org/0000-0002-2305-543X" title: "Univariate Tools" version: 0.1.0 date-released: 2025-07-18 url: "https://github.com/usnistgov/univariate_tools"
GitHub Events
Total
- Member event: 1
- Push event: 6
- Create event: 3
Last Year
- Member event: 1
- Push event: 6
- Create event: 3
Dependencies
- matplotlib *
- numpy *
- scikit-learn *
- scikit-misc *
- scipy *
- seaborn *
- statsmodels *
- sympy *