Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read data with specific numpy floating dtype #287

Open
snowtechblog opened this issue Oct 28, 2022 · 0 comments
Open

Read data with specific numpy floating dtype #287

snowtechblog opened this issue Oct 28, 2022 · 0 comments

Comments

@snowtechblog
Copy link

snowtechblog commented Oct 28, 2022

Hi,
I have the need to read in the channel data of my 20Gb Tdms file in lower precision than float64, e.g. in float32 or even in float16, to reduce my memory consumption. I could not find an nice inbuild solution to that, so I fiddled my own little function that I would like to share and potentially raise as a feature request. (I am not so familiar with the module structure and structure of Tdms files in general to implement such feature via a pull request...)

import numpy as np
from nptdms import TdmsFile

def read_tdms_channel_dtyped(tdms_channel, float_dtype='float32'):
    # unscaled data is int16
    data = tdms_channel.read_data(scaled=False)[0]
    # but when setting the polynomial coefficients to float32 or float64
    c = np.asarray(tdms_channel._scaling.scalings[1].coefficients, dtype=float_dtype)
    # the resulting scaled version is of that same dtype
    # but apply np.astype again to support float16
    return np.polynomial.polynomial.polyval(data, c).astype(float_dtype)

tdms_channel = TdmsFile.open(<file>)[<group>][<channel>]
data = read_tdms_channel_dtyped(tdms_channel)

This function is motivated from the scale method in class PolynomialScaling(object) (line 61 in scaling.py).

@snowtechblog snowtechblog changed the title Read data with specifiy numpy floating dtype Read data with specific numpy floating dtype Oct 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant