EnSpec / SpecDAL

Website landing page for SpecDAL project. https://specdal.github.io now redirects here.
MIT License
28 stars 17 forks source link

Added operations for the spectrum class #20

Open fnemina opened 2 years ago

fnemina commented 2 years ago

Added common arithmetic operations as well as numpy functions to be used in the SPECTRUM class. To do this we defined the __array_ufunc__ method as recommended here

https://numpy.org/doc/stable/reference/arrays.classes.html

as well as changed the kind to numpy.lib.mixins.NDArrayOperatorsMixin to allow for the normal operations to be handled by numpy.

An SPECTRUM class is returned with the same metadata, a new name and a 'TRANS_TYPE' measure_type.

Also it was defined how the array method should be handled.

This should make it easier to operate with SPECTRUM data, for example

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from specdal import *

measurement1 = pd.Series([1, 2, 3, 4], index=pd.Index([1, 2, 3, 4], name='wavelength'),
                                name='pct_reflect')
measurement2 = pd.Series([2, 4, 6, 8], index=pd.Index([1, 2, 3, 4], name='wavelength'),
                                name='pct_reflect')
s1 = Spectrum(name='s1', measurement=measurement1)
s2 = Spectrum(name='s2', measurement=measurement2)

s = (s1+s2)/2

s1.plot(label="s1")
s2.plot(label="s2")
s.plot(label="average")
plt.legend()
plt.show()

returns in s an SPECTRUM class object with the average of both spectra. Also more complicated functions as np.sin(s) can be calculated.

It was also incorporated the __array__ method, so that functions as np.mean(s) returns the means over all wavelengths.