Skip to main content

Renishaw spectroscopy data file accessor classes

Project description

This package provides Python support for using Renishaw Wdf data files. These files are used to hold spectroscopic data from the line of Raman microscopes manufactured by Renishaw plc.

This package requires Python 3.8 or later.

See the accompanying file LICENSE for license details.

Documentation

The package is documented using python docstrings which yield assistance in most editors. Documentation can be generated using pydoc:

$ python -m pydoc *Entity name*

For instance:

$ python -m pydoc wdf.Wdf

Basic usage

Use the Wdf class to open data files. For instance, to print the values for the first spectrum in a file to two columns:

from wdf import Wdf
with Wdf(filename) as data:
    for x, i in zip(data.xlist(), data[0]):
        print(f"{x}\t{i}")

Or using matplotlib to plot the spectral data graphically:

import matplotlib.pyplot as plt
from wdf import Wdf
with Wdf(filename) as data:
    plt.plot(data.xlist(), data[0])
    plt.show()

The Wdf class is iterable and supports indexing to obtain spectra. The result of Wdf[] or Wdf.spectrum() is a non-mutable sequence of floating point values.

A Wdf file is divided into sections, many of which store a collection of named properties. Known section identifiers are provided by constants in the WdfBlockId module and sections are identified by an ID (defining a type of section) and a unique ID (defining a specific instance). To obtain the properties for a the first map section stored in a file the get_section_properties method can be used as shown below.

from wdf import WdfBlockId
props = data.get_section_properties(WdfBlockId.MAP, -1)
print(props["Title"].value)

Spectra often have additional information stored about the collection environment such as the time collected or the X and Y position of the spectrum if part of an area map, or the temperature for a member of a temperature series. These values are stored as data origins and are accessed using the origins property with the data type as a key to obtain the sequence of values that can then be indexed by the spectrum index.

from wdf import Wdf, WdfDataType
index = 1  # spectrum index
with Wdf(filename) as data:
    # the spectrum timestamp (as a datetime)
    timestamp = data.origins[WdfDataType.Time][index]
    # Print all data origin values:
    for origin in data.origins:
        print(origin, data.origins[origin][index], sep="\t")

Map information

If the file contains maps generated from the collected data these can be plotted using numpy and matplotlib.

import numpy as np
import matplotlib.pyplot as plt
from wdf import Wdf, WdfBlockId
mapindex = -1  # first available map
with Wdf(filename) as data:
    shape = data.map_area.count.x, data.map_area.count.y
    mapinfo = data.get_section_properties(WdfBlockId.MAP, mapindex)
    mapdata = np.array(data.get_map_data(mapindex), dtype=float)

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.set_title(mapinfo["Label"].value)
    ax.imshow(mapdata.reshape(shape))
    fig.show()

Map data files often contain whitelight images collected from the microscope view. These are stored as a JPEG image in the WHITELIGHT data section and have embedded EXIF fields to give the position of the top left of the image in microns and the field of view; width and height of the image in microns.

To extract this whitelight image to an image file the data stream can be copied:

from wdf import Wdf, WdfBlockId
def extract_whitelight(wdffilename, whitelightfile):
    with Wdf(wdffilename) as data:
        stream = data.get_section_stream(WdfBlockId.WHITELIGHT, -1)
        with open(whitelightfile, 'wb') as img:
            img.write(stream.read(-1))

If the EXIF data fields are required the following example obtains the custom properties:

import PIL.Image
from wdf import Wdf, WdfBlockId
from PIL.ExifTags import Base as ExifTagsBase

def get_image_properties(data: Wdf):
    props = {}
    with data.get_section_stream(WdfBlockId.WHITELIGHT, -1) as stream:
        image = PIL.Image.open(stream)
        exif = image.getexif()
        for tag, name in ((0xfea0, 'position'), (0xfea1, 'fov'), (0xfea2, 'objective')):
            if tag in exif:
                props[name] = exif[tag]
    return props

To read images stored per-dataset see the demos/extract_whitelight.py example.

Example files

There are some examples of using the library in the demos/ folder that is included with the package source distribution.

The installation will also register wdfbrowser as an executable application. This is implemented from the wdf.browser module and provides a view of all the sections and properties provided in a Wdf file.

The wdfbrowser utility depends on additional packages: numpy, matplotlib and Pillow. These can be installed using pip or any other package management tool.

$ pip install numpy matplotlib Pillow

The wdf package does not have any package dependencies beyond the python standard library.

Installation from source

$ python setup.py install

Bugs and issues

Bug reports should be e-mailed to the support contact for the package.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

renishaw_wdf-1.4.0.tar.gz (272.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

renishaw_wdf-1.4.0-py3-none-any.whl (37.1 kB view details)

Uploaded Python 3

File details

Details for the file renishaw_wdf-1.4.0.tar.gz.

File metadata

  • Download URL: renishaw_wdf-1.4.0.tar.gz
  • Upload date:
  • Size: 272.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for renishaw_wdf-1.4.0.tar.gz
Algorithm Hash digest
SHA256 90c300f720781aa8faab25056ba5af15c976063581fb4f4d851b3df33f68f410
MD5 6686ef7aac80e808009d6325b9bbf6a9
BLAKE2b-256 3bf596e92039f86f59853e32854ab5a7e54f62de8bee0b8638b6e60d34f9cabf

See more details on using hashes here.

File details

Details for the file renishaw_wdf-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: renishaw_wdf-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 37.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for renishaw_wdf-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2e778347a14181021e6051f08f11c2bf384873df203e08cb2ae37cc07326f6fa
MD5 2821d669da9265137575e4d7abd3aed5
BLAKE2b-256 0a188c9a9b1e08770655905ed2feecff76c39af474106caa3ccd3ebeec893548

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page