def test_nddataset_issue_29_mulitlabels(): DS = scp.NDDataset(np.random.rand(3, 4)) with pytest.raises(ValueError): # shape data and label mismatch DS.set_coordset(DS.y, scp.Coord(title='xaxis', units='s', data=[1, 2, 3, 4], labels=['a', 'b', 'c'])) c = scp.Coord(title='xaxis', units='s', data=[1, 2, 3, 4], labels=['a', 'b', 'c', 'd']) DS.set_coordset(x=c) c = scp.Coord(title='xaxis', units='s', data=[1, 2, 3, 4], labels=[['a', 'c', 'b', 'd'], ['e', 'f', 'g', 'h']]) d = DS.y DS.set_coordset(d, c) DS.x.labels = ['alpha', 'beta', 'omega', 'gamma'] assert DS.x.labels.shape == (4, 3) # sort DS1 = DS.sort(axis=1, by='value', descend=True) assert_array_equal(DS1.x, [4, 3, 2, 1]) # sort assert DS.dims == ['y', 'x'] DS1 = DS.sort(dim='x', by='label', descend=False) assert_array_equal(DS1.x, [1, 3, 2, 4]) DS1 = DS.sort(dim='x', by='label', pos=2, descend=False) assert_array_equal(DS1.x, [1, 2, 4, 3]) DS.sort(dim='y') DS.y.labels = ['alpha', 'omega', 'gamma'] DS2 = DS.sort(dim='y') assert_array_equal(DS2.y.labels, ['alpha', 'gamma', 'omega'])
def test_nddataset_repr_html(): dx = np.random.random((10, 100, 3)) coord0 = scp.Coord( data=np.linspace(4000.0, 1000.0, 10), labels="a b c d e f g h i j".split(), mask=None, units="cm^-1", title="wavelength", ) coord1 = scp.Coord( data=np.linspace(0.0, 60.0, 100), labels=None, mask=None, units="s", title="time-on-stream", ) coord2 = scp.Coord( data=np.linspace(200.0, 300.0, 3), labels=["cold", "normal", "hot"], mask=None, units="K", title="temperature", ) da = scp.NDDataset( dx, coordset=[coord0, coord1, coord2], title="absorbance", units="absorbance" ) da._repr_html_()
def test_nddataset_repr_html(): dx = np.random.random((10, 100, 3)) coord0 = scp.Coord(data=np.linspace(4000., 1000., 10), labels='a b c d e f g h i j'.split(), mask=None, units="cm^-1", title='wavelength') coord1 = scp.Coord(data=np.linspace(0., 60., 100), labels=None, mask=None, units="s", title='time-on-stream') coord2 = scp.Coord(data=np.linspace(200., 300., 3), labels=['cold', 'normal', 'hot'], mask=None, units="K", title='temperature') da = scp.NDDataset(dx, coordset=[coord0, coord1, coord2], title='absorbance', units='absorbance') da._repr_html_()
def test_nddataset_bug_par_arnaud(): import spectrochempy as scp import numpy as np x = scp.Coord(data=np.linspace(1000., 4000., num=6000), title='x') y = scp.Coord(data=np.linspace(0., 10, num=5), title='y') data = np.random.rand(x.size, y.size) ds = scp.NDDataset(data, coordset=[x, y]) ds2 = ds[2000.0:3200.0, :] assert ds2.coordset.y.data.shape[0] == 2400, 'taille axe 0 doit être 2400' assert ds2.data.shape[0] == 2400, "taille dimension 0 doit être 2400"
def test_nddataset_coordset(): # init coordinates set at NDDataset initialization dx = np.random.random((10, 7, 3)) coord0 = np.arange(10) coord1 = np.arange(7) coord2 = np.arange(3) * 100. da = scp.NDDataset(dx, coordset=(coord0, coord1, coord2), title='absorbance', coordtitles=['wavelength', 'time-on-stream', 'temperature'], coordunits=['cm^-1', 's', 'K'], ) assert da.shape == (10, 7, 3) assert da.coordset.titles == ['temperature', 'time-on-stream', 'wavelength'] assert da.coordset.names == ['x', 'y', 'z'] assert da.coordunits == [ur.Unit('K'), ur.Unit('s'), ur.Unit('cm^-1')] # order of dims follow data shape, but not necessarily the coord list ( # which is ordered by name) assert da.dims == ['z', 'y', 'x'] assert da.coordset.names == sorted(da.dims) # transpose dat = da.T assert dat.dims == ['x', 'y', 'z'] # dims changed but did not change coords order ! assert dat.coordset.names == sorted(dat.dims) assert dat.coordtitles == da.coordset.titles assert dat.coordunits == da.coordset.units # too many coordinates cadd = scp.Coord(labels=['d%d' % i for i in range(6)]) coordtitles = ['wavelength', 'time-on-stream', 'temperature'] coordunits = ['cm^-1', 's', None] daa = scp.NDDataset(dx, coordset=[coord0, coord1, coord2, cadd, coord2.copy()], title='absorbance', coordtitles=coordtitles, coordunits=coordunits, ) assert daa.coordset.titles == coordtitles[::-1] assert daa.dims == ['z', 'y', 'x'] # with a CoordSet c0, c1 = scp.Coord(labels=['d%d' % i for i in range(6)]), scp.Coord(data=[1, 2, 3, 4, 5, 6]) cc = scp.CoordSet(c0, c1) cd = scp.CoordSet(x=cc, y=c1) ds = scp.NDDataset([1, 2, 3, 6, 8, 0], coordset=cd, units='m') assert ds.dims == ['x'] assert ds.x == cc ds.history = 'essai: 1' ds.history = 'try:2' # wrong type with pytest.raises(TypeError): ds.coord[1.3] # extra coordinates with pytest.raises(AttributeError): ds.y # invalid_length coord1 = scp.Coord(np.arange(9), title='wavelengths') # , units='m') coord2 = scp.Coord(np.arange(20), title='time') # , units='s') with pytest.raises(ValueError): scp.NDDataset(np.random.random((10, 20)), coordset=(coord1, coord2))
def test_bug_462(): A = scp.random((10, 100)) A.x = scp.Coord(np.arange(0.0, 100.0, 1), title="coord1") A.write("A.scp", confirm=False) B = scp.read("A.scp") assert B.x == A.x, "incorrect encoding/decoding" C = scp.random((10, 100)) C.x = [ scp.Coord(np.arange(0.0, 100.0, 1), title="coord1"), scp.Coord(np.arange(0.0, 1000.0, 10), title="coord2"), ] C.write("C.scp", confirm=False) D = scp.read("C.scp") assert len(D.x) == 2, "incorrect encoding/decoding"
def test_nddataset_set_complex_1D_during_math_op(): nd = scp.NDDataset([1., 2.], coordset=[scp.Coord([10, 20])], units='meter') assert nd.data.size == 2 assert nd.size == 2 assert nd.shape == (2,) assert nd.units == ur.meter assert not nd.is_complex ndj = nd * 1j assert ndj.data.size == 2 assert ndj.is_complex
def _make_spectra_matrix(modelname, ampl, pos, width, ratio=None, asym=None): x = scp.Coord(np.linspace(6000.0, 1000.0, 4000), units="cm^-1", title="wavenumbers") s = [] for arg in zip(modelname, ampl, pos, width, ratio, asym): model = getattr(scp, arg[0] + "model")() kwargs = {argname: arg[index + 1] for index, argname in enumerate(model.args)} s.append(model.f(x.data, **kwargs)) st = np.vstack(s) st = scp.NDDataset( data=st, units="absorbance", title="absorbance", coordset=[range(len(st)), x] ) return st
def test_nddataset_issue_29_mulitlabels(): DS = scp.NDDataset(np.random.rand(3, 4)) with pytest.raises(ValueError): # shape data and label mismatch DS.set_coordset( DS.y, scp.Coord( title="xaxis", units="s", data=[1, 2, 3, 4], labels=["a", "b", "c"] ), ) c = scp.Coord( title="xaxis", units="s", data=[1, 2, 3, 4], labels=["a", "b", "c", "d"] ) DS.set_coordset(x=c) c = scp.Coord( title="xaxis", units="s", data=[1, 2, 3, 4], labels=[["a", "c", "b", "d"], ["e", "f", "g", "h"]], ) d = DS.y DS.set_coordset(d, c) DS.x.labels = ["alpha", "beta", "omega", "gamma"] assert DS.x.labels.shape == (4, 3) # sort DS1 = DS.sort(axis=1, by="value", descend=True) assert_array_equal(DS1.x, [4, 3, 2, 1]) # sort assert DS.dims == ["y", "x"] DS1 = DS.sort(dim="x", by="label", descend=False) assert_array_equal(DS1.x, [1, 3, 2, 4]) DS1 = DS.sort(dim="x", by="label", pos=2, descend=False) assert_array_equal(DS1.x, [1, 2, 4, 3]) DS.sort(dim="y") DS.y.labels = ["alpha", "omega", "gamma"] DS2 = DS.sort(dim="y") assert_array_equal(DS2.y.labels, ["alpha", "gamma", "omega"])
def test_nddataset_square_dataset_with_identical_coordinates(): a = np.random.rand(3, 3) c = scp.Coord(np.arange(3) * 0.25, title="time", units="us") nd = scp.NDDataset(a, coordset=scp.CoordSet(x=c, y="x")) assert nd.x == nd.y
(ref. Keller and Massart, Chemometrics and Intelligent Laboratory Systems, 12 (1992) 209-224 ) """ import spectrochempy as scp import numpy as np # sphinx_gallery_thumbnail_number = 5 ######################################################################################################################## # Generate a test dataset # ---------------------------------------------------------------------------------------------------------------------- # 1) simulated chromatogram # ************************* t = scp.Coord(np.arange(15), units='minutes', title='time') # time coordinates c = scp.Coord(range(2), title='components') # component coordinates data = np.zeros((2, 15), dtype=np.float64) data[0, 3:8] = [1, 3, 6, 3, 1] # compound 1 data[1, 5:11] = [1, 3, 5, 3, 1, 0.5] # compound 2 dsc = scp.NDDataset(data=data, coords=[c, t]) ######################################################################################################################## # 2) absorption spectra # ********************** spec = np.array([[2., 3., 4., 2.], [3., 4., 2., 1.]]) w = scp.Coord(np.arange(1, 5, 1), units='nm', title='wavelength')
def test_nddataset_square_dataset_with_identical_coordinates(): a = np.random.rand(3, 3) c = scp.Coord(np.arange(3) * .25, title='time', units='us') nd = scp.NDDataset(a, coordset=scp.CoordSet(x=c, y='x')) assert nd.x == nd.y
(ref. Keller and Massart, Chemometrics and Intelligent Laboratory Systems, 12 (1992) 209-224 ) """ import spectrochempy as scp import numpy as np # sphinx_gallery_thumbnail_number = 5 ######################################################################################################################## # Generate a test dataset # ------------------------------------------------------------------ # 1) simulated chromatogram # ************************* t = scp.Coord(np.arange(15), units="minutes", title="time") # time coordinates c = scp.Coord(range(2), title="components") # component coordinates data = np.zeros((2, 15), dtype=np.float64) data[0, 3:8] = [1, 3, 6, 3, 1] # compound 1 data[1, 5:11] = [1, 3, 5, 3, 1, 0.5] # compound 2 dsc = scp.NDDataset(data=data, coords=[c, t]) ######################################################################################################################## # 2) absorption spectra # ********************** spec = np.array([[2.0, 3.0, 4.0, 2.0], [3.0, 4.0, 2.0, 1.0]]) w = scp.Coord(np.arange(1, 5, 1), units="nm", title="wavelength")
# axis and the array of data c0 = np.linspace(200., 300., 3) c1 = np.linspace(0., 60., 100) c2 = np.linspace(4000., 1000., 100) nd_data = np.array([ np.array([np.sin(2. * np.pi * c2 / 4000.) * np.exp(-y / 60) for y in c1]) * t for t in c0 ]) ############################################################################### # Coordinates # +++++++++++ # The `Coord` object allow making an array of coordinates # with additional metadata such as units, labels, title, etc coord0 = scp.Coord(data=c0, labels=['cold', 'normal', 'hot'], units="K", title='temperature') coord1 = scp.Coord(data=c1, labels=None, units="minutes", title='time-on-stream') coord2 = scp.Coord(data=c2, labels=None, units="cm^-1", title='wavenumber') ############################################################################### # Labels can be useful for instance for indexing a = coord0['normal'] print(a) #################################################### # nd-Dataset # +++++++++++
# %% slideshow={"slide_type": "fragment"} smoothed = region.smooth(window_length=51, window='hanning') _ = smoothed.plot(colormap='magma') # %% [markdown] slideshow={"slide_type": "subslide"} # #### Baseline correction # %% slideshow={"slide_type": "fragment"} region = ds[:, 4000.0:2000.0] smoothed = region.smooth(window_length=51, window='hanning') blc = scp.BaselineCorrection(smoothed) basc = blc.compute([2000., 2300.], [3800., 3900.], method='multivariate', interpolation='pchip', npc=5) _ = basc.plot() # %% [markdown] slideshow={"slide_type": "subslide"} # ## IRIS processing # %% slideshow={"slide_type": "subslide"} ds = scp.read('irdata/CO@Mo_Al2O3.SPG')[:, 2250.:1950.] pressure = [0.00300, 0.00400, 0.00900, 0.01400, 0.02100, 0.02600, 0.03600, 0.05100, 0.09300, 0.15000, 0.20300, 0.30000, 0.40400, 0.50300, 0.60200, 0.70200, 0.80100, 0.90500, 1.00400] ds.y = scp.Coord(pressure, title='Pressure', units='torr') _ = ds.plot(colormap='magma') # %% slideshow={"slide_type": "subslide"} param = {'epsRange': [-8, -1, 50], 'lambdaRange': [-10, 1, 12], 'kernel': 'langmuir'} iris = scp.IRIS(ds, param, verbose=False) _ = iris.plotdistribution(-7, colormap='magma')
def test_EFA(IR_dataset_2D): ######################################################################################################################## # Generate a test dataset # ------------------------------------------------------------------ # 1) simulated chromatogram # ************************* ntimes = 250 ncomponents = 2 t = scp.LinearCoord.arange(ntimes, units="minutes", title="time") # time coordinates c = scp.Coord(range(ncomponents), title="components") # component coordinates data = np.zeros((ncomponents, ntimes), dtype=np.float64) data[0] = asymmetricvoigtmodel().f(t, ampl=4, width=10, ratio=0.5, asym=0.4, pos=50.0) # compound 1 data[1] = asymmetricvoigtmodel().f(t, ampl=5, width=20, ratio=0.2, asym=0.9, pos=120.0) # compound 2 dsc = scp.NDDataset(data=data, coords=[c, t]) dsc.plot() show() ######################################################################################################################## # 2) absorption spectra # ********************** spec = np.array([[2.0, 3.0, 4.0, 2.0], [3.0, 4.0, 2.0, 1.0]]) w = scp.Coord(np.arange(1, 5, 1), units="nm", title="wavelength") dss = scp.NDDataset(data=spec, coords=[c, w]) dss.plot() ######################################################################################################################## # 3) simulated data matrix # ************************ dataset = scp.dot(dsc.T, dss) dataset.data = np.random.normal(dataset.data, 0.2) dataset.title = "intensity" dataset.plot() show() ######################################################################################################################## # 4) evolving factor analysis (EFA) # ********************************* efa = scp.EFA(dataset) ######################################################################################################################## # Plots of the log(EV) for the forward and backward analysis # efa.f_ev.T.plot(yscale="log", legend=efa.f_ev.y.labels) efa.b_ev.T.plot(yscale="log") ######################################################################################################################## # Looking at these EFA curves, it is quite obvious that only two components # are really significant, and this corresponds to the data that we have in # input. # We can consider that the third EFA components is mainly due to the noise, # and so we can use it to set a cut of values n_pc = 2 efa.cutoff = np.max(efa.f_ev[:, n_pc].data) f2 = efa.f_ev b2 = efa.b_ev # we concatenate the datasets to plot them in a single figure both = scp.concatenate(f2, b2) both.T.plot(yscale="log") # TODO: add "legend" keyword in NDDataset.plot() # ######################################################################################################################## # # Get the abstract concentration profile based on the FIFO EFA analysis # # # efa.cutoff = None # c = efa.get_conc(n_pc) # c.T.plot() # # # scp.show() # uncomment to show plot if needed (not necessary in jupyter notebook) # # ds = IR_dataset_2D.copy() # # # columns masking # ds[:, 1230.0:920.0] = MASKED # do not forget to use float in slicing # ds[:, 5900.0:5890.0] = MASKED # # # difference spectra # ds -= ds[-1] # # # column masking for bad columns # ds[10:12] = MASKED # # efa = EFA(ds) # # n_pc = 4 # c = efa.get_conc(n_pc) # c.T.plot() # show()
def test_nddataset_coordset(): # init coordinates set at NDDataset initialization dx = np.random.random((10, 7, 3)) coord0 = np.arange(10) coord1 = np.arange(7) coord2 = np.arange(3) * 100.0 da = scp.NDDataset( dx, coordset=(coord0, coord1, coord2), title="absorbance", coordtitles=["wavelength", "time-on-stream", "temperature"], coordunits=["cm^-1", "s", "K"], ) assert da.shape == (10, 7, 3) assert da.coordset.titles == ["temperature", "time-on-stream", "wavelength"] assert da.coordset.names == ["x", "y", "z"] assert da.coordunits == [ur.Unit("K"), ur.Unit("s"), ur.Unit("cm^-1")] # order of dims follow data shape, but not necessarily the coord list ( # which is ordered by name) assert da.dims == ["z", "y", "x"] assert da.coordset.names == sorted(da.dims) # transpose dat = da.T assert dat.dims == ["x", "y", "z"] # dims changed but did not change coords order ! assert dat.coordset.names == sorted(dat.dims) assert dat.coordtitles == da.coordset.titles assert dat.coordunits == da.coordset.units # too many coordinates cadd = scp.Coord(labels=["d%d" % i for i in range(6)]) coordtitles = ["wavelength", "time-on-stream", "temperature"] coordunits = ["cm^-1", "s", None] daa = scp.NDDataset( dx, coordset=[coord0, coord1, coord2, cadd, coord2.copy()], title="absorbance", coordtitles=coordtitles, coordunits=coordunits, ) assert daa.coordset.titles == coordtitles[::-1] assert daa.dims == ["z", "y", "x"] # with a CoordSet c0, c1 = scp.Coord(labels=["d%d" % i for i in range(6)]), scp.Coord( data=[1, 2, 3, 4, 5, 6] ) cc = scp.CoordSet(c0, c1) cd = scp.CoordSet(x=cc, y=c1) ds = scp.NDDataset([1, 2, 3, 6, 8, 0], coordset=cd, units="m") assert ds.dims == ["x"] assert ds.x == cc ds.history = "essai: 1" ds.history = "try:2" # wrong type with pytest.raises(TypeError): ds.coord[1.3] # extra coordinates with pytest.raises(AttributeError): ds.y # invalid_length coord1 = scp.Coord(np.arange(9), title="wavelengths") # , units='m') coord2 = scp.Coord(np.arange(20), title="time") # , units='s') with pytest.raises(ValueError): scp.NDDataset(np.random.random((10, 20)), coordset=(coord1, coord2))
# %% [markdown] # It should be noted that this method finds only true maxima, not shoulders (!). For the detection of such underlying # peaks, the use of methods based on derivatives or advanced detection methods - which will be treated in separate # tutorial - are required. Once their maxima of a given peak have been found, it is possible, for instance, # to plot its evolution with, e.g. the time. For instance for the peaks located at 2220-2180 cm$^{-1}$: # %% # Find peak's position positions = [s.find_peaks()[0].x.values.m for s in reg[:, 2220.:2180.]] # Make a NDDataset evol = scp.NDDataset(positions, title="wavenumber at the maximum", units="1 /cm") evol.x = scp.Coord( reg.y, title='acquisition time' ) # the x coordinate is st to the acquisition time for each specra evol.preferences.method_1D = 'scatter+pen' # plot it _ = evol.plot(ls=':') # %% [markdown] # ### Options of `find_peaks()` <a id='options'></a> # %% [markdown] # The default behaviour of find_peaks() will return *all* the detected maxima. The user can choose various options to # select among these peaks: # # **Parameters relative to "peak intensity":** # - `height`: minimal required height of the peaks (single number) or minimal and maximal heights
# * and `timestamps` (*i.e.,* the time of recording) named "y". print(X.coordset) ######################################################################################################################## # ## Setting new coordinates # # Each experiments corresponding to a timestamp correspond to a given pressure of CO in the intrared cell. # # Hence it would be interesting to replace the "useless" timestamps (``y``) by a pressure coordinates: pressures = [0.00300, 0.00400, 0.00900, 0.01400, 0.02100, 0.02600, 0.03600, 0.05100, 0.09300, 0.15000, 0.20300, 0.30000, 0.40400, 0.50300, 0.60200, 0.70200, 0.80100, 0.90500, 1.00400] "" c_pressures = scp.Coord(pressures, title='pressure', units='torr') ############################################################################### # Now we can set multiple coordinates: c_times = X.y.copy() # the original coordinate X.y = [c_times, c_pressures] print(X.y) ############################################################################### # By default, the current coordinate is the first one (here `c_times`). For example, it will be used for plotting: prefs = X.preferences prefs.figure.figsize = (7,3) _ = X.plot(colorbar=True) _ = X.plot_map(colorbar=True)
@pytest.fixture(scope="function") def ndarrayquaternion(): # return a quaternion ndarray return scp.NDComplexArray(ref_data, units="m/s", dtype=np.quaternion, copy=True).copy() # ------------------------------------------------------------------ # Fixtures: Some scp.NDDatasets # ------------------------------------------------------------------ coord0_ = scp.Coord( data=np.linspace(4000.0, 1000.0, 10), labels=list("abcdefghij"), units="cm^-1", title="wavenumber", ) @pytest.fixture(scope="function") def coord0(): return coord0_.copy() coord1_ = scp.Coord(data=np.linspace(0.0, 60.0, 100), units="s", title="time-on-stream") @pytest.fixture(scope="function")
0.036, 0.051, 0.093, 0.150, 0.203, 0.300, 0.404, 0.503, 0.602, 0.702, 0.801, 0.905, 1.004, ] c_pressures = scp.Coord(pressures, title="pressure", units="torr") ############################################################################### # Now we can set multiple coordinates: c_times = X.y.copy() # the original coordinate X.y = [c_times, c_pressures] print(X.y) ############################################################################### # By default, the current coordinate is the first one (here `c_times`). For example, it will be used by default for # plotting: prefs = X.preferences prefs.figure.figsize = (7, 3) _ = X.plot(colorbar=True)
def test_ndmath_and_api_methods(IR_dataset_1D, IR_dataset_2D): # CREATION _LIKE METHODS # ---------------------- # from a list x = [1, 2, 3] # _like as an API method ds = NDDataset(x).full_like(2.5, title="empty") ds = scp.full_like(x, 2) assert np.all(ds.data == np.full((3, ), 2)) assert ds.implements("NDDataset") # _like as a classmethod ds = NDDataset.full_like(x, 2) assert np.all(ds.data == np.full((3, ), 2)) assert ds.implements("NDDataset") # _like as an instance method ds = NDDataset(x).full_like(2) assert np.all(ds.data == np.full((3, ), 2)) assert ds.implements("NDDataset") # _like as an instance method ds = NDDataset(x).empty_like(title="empty") assert ds.implements("NDDataset") assert ds.title == "empty" # from an array x = np.array([1, 2, 3]) ds = NDDataset(x).full_like(2) assert np.all(ds.data == np.full((3, ), 2)) assert ds.implements("NDDataset") # from a NDArray subclass with units x = NDDataset([1, 2, 3], units="km") ds = scp.full_like(x, 2) assert np.all(ds.data == np.full((3, ), 2)) assert ds.implements("NDDataset") assert ds.units == ur.km ds1 = scp.full_like(ds, np.nan, dtype=np.double, units="m") assert ds1.units == Unit("m") # change of units is forced ds2 = scp.full_like(ds, 2, dtype=np.double, units="s") assert ds2.units == ur.s # other like creation functions nd = scp.empty_like(ds, dtype=np.double, units="m") assert str(nd) == "NDDataset: [float64] m (size: 3)" assert nd.dtype == np.dtype(np.double) nd = scp.zeros_like(ds, dtype=np.double, units="m") assert str(nd) == "NDDataset: [float64] m (size: 3)" assert np.all(nd.data == np.zeros((3, ))) nd = scp.ones_like(ds, dtype=np.double, units="m") assert str(nd) == "NDDataset: [float64] m (size: 3)" assert np.all(nd.data == np.ones((3, ))) # FULL # ---- ds = NDDataset.full((6, ), 0.1) assert ds.size == 6 assert str(ds) == "NDDataset: [float64] unitless (size: 6)" # ZEROS # ----- ds = NDDataset.zeros((6, ), units="km") assert ds.size == 6 assert str(ds) == "NDDataset: [float64] km (size: 6)" # ONES # ---- ds = NDDataset.ones((6, )) ds = scp.full((6, ), 0.1) assert ds.size == 6 assert str(ds) == "NDDataset: [float64] unitless (size: 6)" ds = NDDataset.ones((6, ), units="absorbance", dtype="complex128") assert ds.size == 3 assert str(ds) == "NDDataset: [complex128] a.u. (size: 3)" assert ds[0].data == 1.0 + 1.0j # LINSPACE # -------- c2 = Coord.linspace(1, 20, 200, units="m", name="mycoord") assert c2.name == "mycoord" assert c2.size == 200 assert c2[-1].data == 20 assert c2[0].values == Quantity(1, "m") # ARANGE # ------- c3 = Coord.arange(1, 20.0001, 1, units="s", name="mycoord") assert c3.name == "mycoord" assert c3.size == 20 assert c3[-1].data == 20 assert c3[0].values == Quantity(1, "s") # EYE # ---- ds1 = scp.NDDataset.eye(2, dtype=int) assert str(ds1) == "NDDataset: [float64] unitless (shape: (y:2, x:2))" ds = scp.eye(3, k=1, units="km") assert (ds.data == np.eye(3, k=1)).all() assert ds.units == ur.km # IDENTITY # -------- ds = scp.identity(3, units="km") assert (ds.data == np.identity(3, )).all() assert ds.units == ur.km # RANDOM # ------ ds = scp.random((3, 3), units="km") assert str(ds) == "NDDataset: [float64] km (shape: (y:3, x:3))" # adding coordset c1 = Coord.linspace(1, 20, 200, units="m", name="axe_x") ds = scp.random((200, ), units="km", coordset=scp.CoordSet(x=c1)) # DIAGONAL # -------- # extract diagonal nd = scp.full((2, 2), 0.5, units="s", title="initial") assert str(nd) == "NDDataset: [float64] s (shape: (y:2, x:2))" ndd = scp.diagonal(nd, title="diag") assert str(ndd) == "NDDataset: [float64] s (size: 2)" assert ndd.units == Unit("s") cx = scp.Coord([0, 1]) cy = scp.Coord([2, 5]) nd = NDDataset.full((2, 2), 0.5, units="s", coordset=scp.CoordSet(cx, cy), title="initial") assert str(nd) == "NDDataset: [float64] s (shape: (y:2, x:2))" ndd = nd.diagonal(title="diag2") assert str(ndd) == "NDDataset: [float64] s (size: 2)" assert ndd.units == Unit("s") assert ndd.title == "diag2" cx = scp.Coord([0, 1, 2]) cy = scp.Coord([2, 5]) nd = NDDataset.full((2, 3), 0.5, units="s", coordset=scp.CoordSet(x=cx, y=cy), title="initial") assert str(nd) == "NDDataset: [float64] s (shape: (y:2, x:3))" ndd = nd.diagonal(title="diag3") assert str(ndd) == "NDDataset: [float64] s (size: 2)" assert ndd.units == Unit("s") assert ndd.title == "diag3" assert_array_equal(nd.x.data[:ndd.x.size], ndd.x.data) ndd = nd.diagonal(title="diag4", dim="y") assert str(ndd) == "NDDataset: [float64] s (size: 2)" assert ndd.units == Unit("s") assert ndd.title == "diag4" assert_array_equal(nd.y.data[:ndd.y.size], ndd.y.data) # DIAG # ---- ref = NDDataset(np.diag((3, 3.4, 2.3)), units="m", title="something") # Three forms should return the same NDDataset ds = scp.diag((3, 3.4, 2.3), units="m", title="something") assert_dataset_equal(ds, ref) ds = NDDataset.diag((3, 3.4, 2.3), units="m", title="something") assert_dataset_equal(ds, ref) ds = NDDataset((3, 3.4, 2.3)).diag(units="m", title="something") assert_dataset_equal(ds, ref) # and this too ds1 = NDDataset((3, 3.4, 2.3), units="s", title="another") ds = scp.diag(ds1, units="m", title="something") assert_dataset_equal(ds, ref) ds = ds1.diag(units="m", title="something") assert_dataset_equal(ds, ref) # BOOL : ALL and ANY # ------------------ ds = NDDataset([[True, False], [True, True]]) b = np.all(ds) assert not b b = scp.all(ds) assert not b b = ds.all() assert not b b = NDDataset.any(ds) assert b b = ds.all(dim="y") assert_array_equal(b, np.array([True, False])) b = ds.any(dim="y") assert_array_equal(b, np.array([True, True])) # ARGMAX, MAX # ----------- nd1 = IR_dataset_1D.copy() nd1[1290.0:890.0] = MASKED assert nd1.is_masked assert str(nd1) == "NDDataset: [float64] a.u. (size: 5549)" idx = nd1.argmax() assert idx == 3122 mx = nd1.max() # alternative mx = scp.max(nd1) mx = NDDataset.max(nd1) assert mx == Quantity(3.8080601692199707, "absorbance") mxk = nd1.max(keepdims=True) assert isinstance(mxk, NDDataset) assert str(mxk) == "NDDataset: [float64] a.u. (size: 1)" assert mxk.values == mx # test on a 2D NDDataset nd2 = IR_dataset_2D.copy() nd2[:, 1290.0:890.0] = MASKED mx = nd2.max() # no axis specified assert mx == Quantity(3.8080601692199707, "absorbance") mxk = nd2.max(keepdims=True) assert str(mxk) == "NDDataset: [float64] a.u. (shape: (y:1, x:1))" nd2m = nd2.max("y") # axis selected ax = nd2m.plot() nd2[0].plot(ax=ax, clear=False) nd2m2 = nd2.max("x") # axis selected nd2m2.plot() nd2m = nd2.max("y", keepdims=True) assert nd2m.shape == (1, 5549) nd2m = nd2.max("x", keepdims=True) assert nd2m.shape == (55, 1) mx = nd2.min() # no axis specified assert mx == Quantity(-0.022955093532800674, "absorbance") mxk = nd2.min(keepdims=True) assert str(mxk) == "NDDataset: [float64] a.u. (shape: (y:1, x:1))" nd2m = nd2.min("y") # axis selected ax = nd2m.plot() nd2[0].plot(ax=ax, clear=False) nd2m2 = nd2.min("x") # axis selected nd2m2.plot() nd2m = nd2.min("y", keepdims=True) assert nd2m.shape == (1, 5549) nd2m = nd2.min("x", keepdims=True) assert nd2m.shape == (55, 1) # CLIP # ---- nd3 = nd2 - 2.0 assert nd3.units == nd2.units nd3c = nd3.clip(-0.5, 1.0) assert nd3c.max().m == 1.0 assert nd3c.min().m == -0.5 # COORDMIN AND COORDMAX # --------------------- cm = nd2.coordmin() assert np.around(cm["x"], 3) == Quantity(1290.165, "cm^-1") cm = nd2.coordmin(dim="y") assert cm.size == 1 cm = nd2.coordmax(dim="y") assert cm.size == 1 cm = nd2.coordmax(dim="x") assert cm.size == 1 # ABS # ---- nd2a = scp.abs(nd2) mxa = nd2a.min() assert mxa > 0 nd2a = NDDataset.abs(nd2) mxa = nd2a.min() assert mxa > 0 nd2a = np.abs(nd2) mxa = nd2a.min() assert mxa > 0 ndd = NDDataset([1.0, 2.0 + 1j, 3.0]) val = np.abs(ndd) val = ndd[1] * 1.2 - 10.0 val = np.abs(val) # FROMFUNCTION # ------------ # 1D def func1(t, v): d = v * t return d time = Coord.linspace( 0, 9, 10, ) distance = NDDataset.fromfunction(func1, v=134, coordset=CoordSet(t=time)) assert distance.dims == ["t"] assert_array_equal(distance.data, np.fromfunction(func1, (10, ), v=134)) time = Coord.linspace(0, 90, 10, units="min") distance = NDDataset.fromfunction(func1, v=Quantity(134, "km/hour"), coordset=CoordSet(t=time)) assert distance.dims == ["t"] assert_array_equal(distance.data, np.fromfunction(func1, (10, ), v=134) * 10 / 60) # 2D def func2(x, y): d = x + 1 / y return d c0 = Coord.linspace(0, 9, 3) c1 = Coord.linspace(10, 20, 2) # implicit ordering of coords (y,x) distance = NDDataset.fromfunction(func2, coordset=CoordSet(c1, c0)) assert distance.shape == (2, 3) assert distance.dims == ["y", "x"] # or equivalent distance = NDDataset.fromfunction(func2, coordset=[c1, c0]) assert distance.shape == (2, 3) assert distance.dims == ["y", "x"] # explicit ordering of coords (y,x) # distance = NDDataset.fromfunction(func2, coordset=CoordSet(u=c0, v=c1)) assert distance.shape == (2, 3) assert distance.dims == ["v", "u"] assert distance[0, 2].data == distance.u[2].data + 1.0 / distance.v[0].data # with units def func3(x, y): d = x + y return d c0u = Coord.linspace(0, 9, 3, units="km") c1u = Coord.linspace(10, 20, 2, units="m") distance = NDDataset.fromfunction(func3, coordset=CoordSet(u=c0u, v=c1u)) assert distance.shape == (2, 3) assert distance.dims == ["v", "u"] assert distance[0, 2].values == distance.u[2].values + distance.v[0].values c0u = Coord.linspace(0, 9, 3, units="km") c1u = Coord.linspace(10, 20, 2, units="m^-1") distance = NDDataset.fromfunction(func2, coordset=CoordSet(u=c0u, v=c1u)) assert distance.shape == (2, 3) assert distance.dims == ["v", "u"] assert distance[ 0, 2].values == distance.u[2].values + 1.0 / distance.v[0].values # FROMITER # -------- iterable = (x * x for x in range(5)) nit = scp.fromiter(iterable, float, units="km") assert str(nit) == "NDDataset: [float64] km (size: 5)" assert_array_equal(nit.data, np.array([0, 1, 4, 9, 16])) # MEAN, AVERAGE # ----- nd = IR_dataset_2D.copy() m = scp.mean(nd) assert m.shape == () assert m == Quantity(np.mean(nd.data), "absorbance") m = scp.average(nd) assert m.shape == () assert m == Quantity(np.average(nd.data), "absorbance") mx = scp.mean(nd, keepdims=True) assert mx.shape == (1, 1) mxd = scp.mean(nd, dim="y") assert str(mxd) == "NDDataset: [float64] a.u. (size: 5549)" assert str(mxd.x) == "LinearCoord: [float64] cm⁻¹ (size: 5549)" # ---- nd2 = NDDataset([[0, 1, 2], [3, 4, 5]]) # no coord (check issues m = scp.mean(nd2) assert m.shape == () assert m == np.mean(nd2.data) assert m == 2.5 m = scp.mean(nd2, keepdims=True) assert m.shape == (1, 1) assert m.data == [[2.5]] m = scp.mean(nd2, dim="y") assert m.shape == (3, ) assert_array_equal(m.data, [1.5, 2.5, 3.5]) assert str(m) == "NDDataset: [float64] unitless (size: 3)" m = scp.mean(nd2, dim=0, keepdims=True) assert m.shape == (1, 3) assert_array_equal(m.data, [[1.5, 2.5, 3.5]]) assert str(m) == "NDDataset: [float64] unitless (shape: (y:1, x:3))" m = nd2.mean(dim="y") assert m.shape == (3, ) assert_array_equal(m.data, [1.5, 2.5, 3.5]) assert str(m) == "NDDataset: [float64] unitless (size: 3)"
# %% tags=[] ds = scp.NDDataset.read_omnic("irdata/CO@Mo_Al2O3.SPG")[:, 2250.0:1950.0] pressure = [ 0.00300, 0.00400, 0.00900, 0.01400, 0.02100, 0.02600, 0.03600, 0.05100, 0.09300, 0.15000, 0.20300, 0.30000, 0.40400, 0.50300, 0.60200, 0.70200, 0.80100, 0.90500, 1.00400, ] ds.y = scp.Coord(pressure, title="Pressure", units="torr") _ = ds.plot(colormap="magma") # %% jupyter={"source_hidden": true} pycharm={"name": "#%%\n"} iris = scp.IRIS(ds, "langmuir", q=[-8, -1, 50], reg_par=[-10, 1, 12]) _ = iris.plotdistribution(-7, colormap="magma")