We recently had a question about replacing values below 0 with 0 so I thought I’d write a quick note to demonstrate one way you can do that using iolite’s python functionality.
Getting channel data as numpy arrays
If you have used python for some kind of scientific computing or data analysis before, you have almost certainly used or at least heard of numpy. If you’re new to python, numpy is the ubiquitous python vector/matrix math library and is the foundation of countless other libraries. Naturally, we use numpy to interface iolite’s c++-based channel data to python.
Getting the data and time arrays for an iolite channel in python is as easy as:
|
|
One important thing to keep in mind is that the data is shared by c++ and python/numpy, so changes made in python are automatically reflected in the c++ side and vice versa. If you do not want to modify the data you may want to make a copy of it instead:
|
|
Note that much of the iolite-specific data structures and functions are accessed via a built in data
object. However, there is nothing preventing you from replacing that by doing something like data = data.timeSeries('U238').data()
. If you were to do that, you would no longer be able to access iolite’s data and might encounter some difficult to decipher errors!
Replacing values matching some criteria
Numpy is extremely powerful and simple to use. Supposing you want to replace values less than 0 with 0, this can be accomplished in one simple statement d[d < 0] = 0
. So, if we want to replace data less than 0 for all input channels we can do this with a very simple script:
|
|
Note that we can also do this for all channels by omitting the data.Input
or we could combine channel types, e.g. data.Intermediate | data.Output
.
There is only one thing left to do – since we have changed the channel data, we should tell iolite that the data has changed and update our results.
|
|
Technically, the last line will cause an error if there is no active selection group (AttributeError: 'NoneType' object has no attribute 'changed'
). This can be safely ignored, or if you don’t want to see it, you could either check if data.activeSelectionGroup()
is None
or wrap it in a try
/except
.
Putting it all together
|
|
And that’s it!
Click here to discuss.