Table Of Contents

Previous topic

Assign a condition to a data set

Next topic

Use alternative language

Extract a spectrum to a CSV file

We show in this example how to extract a spectrum from a HMSA data file and save it in a CSV file with Python. As in the second example, we use one of the example HMSA data files provided by the authors: Brecci EDS spectrum. You can download the data file here.

First, let’s read the data file.

from pyhmsa.datafile import DataFile
datafile = DataFile.read('Breccia - EDS sum spectrum.xml')

Then, we must find the data set corresponding to our spectrum. In this case, the data set is called EDS sum spectrum so we could retrieve it from its ID.

spectrum = datafile.data['EDS sum spectrum']

However, in some cases, we might not know the name of a data set or an only a portion of the name. The library offers search methods to find both data sets and conditions.

For instance, we could search for all data sets containing the word spectrum as follow:

results = datafile.data.findvalues('*spectrum*')
print(len(results)) # Returns: 1

The * in the search pattern are wild cards and indicates to match any character.

We could also search based on the type of data set. In this case, we are looking for an Analysis1D data set.

from pyhmsa.spec.datum.analysis import Analysis1D
results = datafile.data.findvalues(Analysis1D)
print(len(results)) # Returns: 1

Once we have our data set, we can use the utility method get_xy to retrieve a two-dimensional array where the first column contains x values and the second y values. This method is particularly useful since it will search through the associated conditions to the data set to see if a calibration was defined for the x values. In this example, a linear calibration with an offset of -237.098251 was defined. The first x value should therefore be equal to this value.

spectrum = next(iter(results)) # Take first result
xy = spectrum.get_xy()
print(xy[0, 0]) # Returns -237.098251

The get_xy can also returns labels for the x and y values as defined in the conditions.

xlabel, ylabel, xy = spectrum.get_xy(with_labels=True)

Finally, we can use Python’s csv module to create the CSV file.

import csv
with open('breccia.csv', 'w') as fp:
    writer = csv.writer(fp) # Create CSV writer
    writer.writerow([xlabel, ylabel]) # Header
    writer.writerows(xy)

Full source code

#!/usr/bin/env python

from pyhmsa.datafile import DataFile
datafile = DataFile.read('Breccia - EDS sum spectrum.xml')

spectrum = datafile.data['EDS sum spectrum']

# Search
results = datafile.data.findvalues('*spectrum*')
print(len(results)) # Returns: 1

from pyhmsa.spec.datum.analysis import Analysis1D
results = datafile.data.findvalues(Analysis1D)
print(len(results)) # Returns: 1

spectrum = next(iter(results)) # Take first result
xy = spectrum.get_xy()
print(xy[0, 0]) # Returns -237.098251

xlabel, ylabel, xy = spectrum.get_xy(with_labels=True)

# Save
import csv
with open('breccia.csv', 'w') as fp:
    writer = csv.writer(fp) # Create CSV writer
    writer.writerow([xlabel, ylabel]) # Header
    writer.writerows(xy)