data.py 2.86 KB
Newer Older
Michael Rudolf's avatar
Michael Rudolf committed
1
2
""" Functions for data handling such as filtering, downsampling and fitting """

Michael Rudolf's avatar
Michael Rudolf committed
3
4
5
6
import os
import shutil

import nptdms
Michael Rudolf's avatar
Michael Rudolf committed
7
import numpy as np
Michael Rudolf's avatar
Michael Rudolf committed
8
9
import rstevaluation.tools.files as rstfiles
from tqdm import tqdm
Michael Rudolf's avatar
Michael Rudolf committed
10
11


Michael Rudolf's avatar
Michael Rudolf committed
12
def downsample_data(data, R):
Michael Rudolf's avatar
Michael Rudolf committed
13
14
    """
    Downsamples data by factor R using the mean over R values.
Michael Rudolf's avatar
Michael Rudolf committed
15
16
    Pads the data with NaN at the edges if not divisible by R so that the last
    value uses less data points for the mean.
Michael Rudolf's avatar
Michael Rudolf committed
17
18
19
20
21
    """
    pad_size = int(np.ceil(float(data.size)/R)*R - data.size)
    data_padded = np.append(data, np.ones(pad_size)*np.NaN)
    new_data = np.nanmean(data_padded.reshape(-1, R), axis=1)
    return new_data
Michael Rudolf's avatar
Michael Rudolf committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91


def downsample_file(file_path, freq=5):
    """
    Opens a tdms file and saves it in a downsampled version.

    Keywords:
        - freq: Frequency to downsample to (default=5Hz)
    """
    new_file_path = file_path.replace(
        '.tdms', '_downsampled.tdms'
    )

    with nptdms.TdmsFile(file_path) as tdms_file, \
            nptdms.TdmsWriter(new_file_path) as new_file:
        root_object = nptdms.RootObject(tdms_file.properties)
        original_groups = tdms_file.groups()
        channels = [
            downsample_channel(chan, freq=freq)
            for group in original_groups
            for chan in group.channels()
        ]
        new_file.write_segment([root_object] + original_groups + channels)
    return new_file_path


def downsample_channel(channel, freq):
    """ Downsamples a channel to given frequency """
    old_freq = 1/channel.time_track()[1]
    R = int(old_freq / freq)

    props = channel.properties
    props['wf_increment'] *= R
    downsampled = nptdms.ChannelObject(
        group=channel.group_name,
        channel=channel.name,
        data=downsample_data(channel.data, R),
        properties=props
    )
    return downsampled


def check_tdms(file_list, config):
    """
    Checks if a tdms file on the list is longer than allowed. If yes then the
    data is automatically downsampled.
    """
    max_len = 0
    for file_name in tqdm(file_list, desc='Checking TDMS file length'):
        cur_len = rstfiles.get_length(
            os.path.join(config['paths']['path_in'], file_name)
        )
        if cur_len > max_len:
            max_len = cur_len

    if max_len > config.getint('parameters', 'nsamples'):
        print(
            'Converting folder. Raw data files will be moved to raw_data.'
        )
        raw_folder = os.path.join(config['paths']['path_in'], 'raw_data')
        os.makedirs(
            raw_folder,
            exist_ok=True
        )
        for file_name in tqdm(file_list, desc='Downsampling Files'):
            file_path = os.path.join(config['paths']['path_in'], file_name)
            new_path = os.path.join(raw_folder, file_name)
            shutil.move(file_path, new_path)
            new_file_path = downsample_file(new_path)
            shutil.move(new_file_path, file_path)