We will introduce the SpecStack
, and container class for operating on several TimeSpectra at once. This is useful for slicing a TimeSpectra into subsets/stages, or comparing multiple TimeSpectra.
Configure notebook style (see NBCONFIG.ipynb), add imports and paths. The %run magic used below requires IPython 2.0 or higher.
%run NBCONFIG.ipynb
Populating the interactive namespace from numpy and matplotlib
Let's manually create a SpecStack from two separate test datasets. We'll pass a list of TimeSpectra, and keys denoting their nicknames. Alternatively, we can pass a mapping (e.g. dictionary or (k,v) pairs) to the SpecStack constructor. Keys are required to be strings, and will by recast as such by SpecStack.
from skspec.data import aunps_glass
from skspec import SpecStack
t1 = aunps_glass(style=1)
t2 = aunps_glass(style=3)
stack = SpecStack( [t1,t2], keys=['nice AuNPs', 'crap AuNPs'] )
stack.plot();
WARNING:skspec.core.timespectra:SpecIndex unit was changed internally from "None" to "nm" WARNING:skspec.core.timespectra:SpecIndex unit was changed internally from "None" to "nm"
First, note that SpecStack.plot()
returns a multiplot implicitly. SpecStack publicizes the most common batch operations such as changing iunit
or specunit
. For example:
stack.iunit = 'r'
stack.plot();
The following timespectra attributes accessed and modified directly form the SpecStack API:
However, we can get and set any attribute using the stack.get_all()
and stack.set_all()
. Additionaly, arbitrary functions can be applied itemwise using the stack.apply()
method. Both apply()
and set_all
optionall can be done in place, but by default, they return new SpecStack objects.
Let's use get_all()
to examine the shape attribute. By default, get_all will return a tuple of k,v pairs; however, use the get_all(as_type=...)
option to return other types (e.g. astype='generator', astype=list, astype=dict).
stack.get_all('shape', astype=dict)
{'crap AuNPs': (704, 100), 'nice AuNPs': (704, 100)}
stack_sqrt = stack.apply(np.sqrt)
stack_sqrt.plot();
Let's apply a custom function that adds noise to a curve with a 10% chance
def random_noise(curve):
if randint(1,10) == 5: #10% chance of choosing 5
curve = curve + 0.025*np.random.randn(len(curve))
return curve
stack.apply(random_noise).plot(colormap='afmhot_r');
SpecStack supports simple indexing with the following lookup convention:
str : A string is assumed to be a key in the Stack `stack['spec1']
int : Corresponds to the index in the stack `stack[0]
slice: Integer slice
stack[50:80]
list: Integers or string lookup by item
stack['spec1', 4, 'spec3', 18 ...]
String slicing (e.g. stack['spec1':'spec5'])
is not supported.
Let's change one of the curves spectral unit and intensity unit.
stack[0].iunit = 't' #Transmission
stack[0].specunit = 'ev' #Electron volts
stack.plot();
Another useful function is stack._get_unique()
, which will return unique values of an arbitrary attribute. For example, to see all of the various spec units used in this stack (in this case only 2):
stack._get_unique('specunit') #REMOVE THIS
'mixed'