Table Of Contents

Previous topic

Create a new data file

Next topic

Assign a condition to a data set

Read a data file

In this example, we will read one of the example HMSA data files provided by the authors: Brecci EDS spectrum. You can download the data file here.

We first import the DataFile class.

from pyhmsa.datafile import DataFile

Then assuming that the Breccia EDS spectrum files are in the same folder as this script, we simply called the class method read.

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

And that’s all!

Advanced

Reading a large data file may take a long time since all the information is transfered in the memory. To get a progress report or to prevent blocking operation, the pyHMSA API provides an advanced reader DataFileReader which operates inside a thread. It can be used as follows:

import time
from pyhmsa.fileformat.datafile import DataFileReader

reader = DataFileReader()
reader.read('Breccia - EDS sum spectrum.xml')

while reader.is_alive():
    print('{0:n}% - {1}'.format(reader.progress * 100.0, reader.status))
    time.sleep(0.1)
print('{0:n}% - {1}'.format(reader.progress * 100.0, reader.status))

Note that the read only initiates the reading process, but does not any data file. The method get must be called to return the data file.

Full source code

#!/usr/bin/env python

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

# Advanced
import time
from pyhmsa.fileformat.datafile import DataFileReader

reader = DataFileReader()
reader.read('Breccia - EDS sum spectrum.xml')

while reader.is_alive():
    print('{0:n}% - {1}'.format(reader.progress * 100.0, reader.status))
    time.sleep(0.1)
print('{0:n}% - {1}'.format(reader.progress * 100.0, reader.status))

datafile = reader.get()