Replacing channel data

· ☕ 3 min read · ✍️ Joe

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:

1
2
d = data.timeSeries('U238').data()
t = data.timeSeries('U238').time()

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:

1
2
3
dcopy = np.copy(d)
# or
dcopy = d.copy()

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:

1
2
for c in data.timeSeriesList(data.Input):
    c.data()[c.data() < 0] = 0

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.

1
2
3
data.updateResults() # This will update all results (and update things in the results view)
data.emitDataChanged() # This will cause the time series view to replot the data
data.activeSelectionGroup().changed.emit() # This will cause splines and selections to be replotted

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

1
2
3
4
5
6
for c in data.timeSeriesList(data.Input):
    c.data()[c.data() < 0] = 0

data.updateResults()
data.emitDataChanged()
data.activeSelectionGroup().changed.emit()

And that’s it!

Click here to discuss.


iolite team
WRITTEN BY
Joe
iolite developer


What's on this Page