processing.py 5.81 KB
Newer Older
Michael Rudolf's avatar
Michael Rudolf committed
1
2
3
4
5
6
7
"""
Contains functions for full processing of the data.
"""
import os
import subprocess
import sys

Michael Rudolf's avatar
Michael Rudolf committed
8
9
10
11
from rstevaluation import analysis as rstanalysis
from rstevaluation import files as rstfiles
from rstevaluation import picking as rstpicking
from rstevaluation import plots as rstplots
Michael Rudolf's avatar
Michael Rudolf committed
12
13
14
15
16


def evaluate(config):
    """ Evaluates the data coming from the config """
    exp_data = get_data(config)
Michael Rudolf's avatar
Michael Rudolf committed
17
18
19
20
21
22
23
24
25
26
27
    if config.getint('options', 'rst'):
        eval_data = [
            rstpicking.eval_shearstress(
                cur_dat,
                config,
                review='auto'
            )
            for cur_dat in exp_data
        ]
        evaluate_data(eval_data, exp_data, config)
    else:
Michael Rudolf's avatar
Michael Rudolf committed
28
29
30
31
32
33
        print('Velocity Stepping Test')
        for exp in exp_data:
            pfit, perr, name_fit = rstanalysis.vst_analysis(exp)
            rstplots.plotVST(
                config['paths']['path_out'],
                exp, pfit, perr, name_fit)
Michael Rudolf's avatar
Michael Rudolf committed
34
35
    with open(config['paths']['path_cfg'], 'w') as cfg_file:
        config.write(cfg_file)
Michael Rudolf's avatar
Michael Rudolf committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    open_explorer(config['paths']['path_out'])


def get_data(config):
    # Generate a list containing all files
    supported_filetypes = ['.asc', '.dat', '.tdms']
    file_list = [
        f for f in os.listdir(config['paths']['path_in'])
        if os.path.splitext(f)[-1] in supported_filetypes
    ]
    # See if we have a tdms file in the list (for possible downsampling)
    has_tdms = False
    for f in file_list:
        if '.tdms' in f:
            has_tdms = True
    # Check which tdms files are too long and downsample if necessary
    if has_tdms:
53
        rstfiles.check_tdms(file_list, config)
Michael Rudolf's avatar
Michael Rudolf committed
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

    # Data is stored as a list of dictionaries containing the data
    exp_data = [
        rstfiles.convert(config['paths']['path_in'], f, config)
        for f in file_list
    ]
    return exp_data


def evaluate_data(eval_data, exp_data, config):
    # Create lists of parameters for analysis
    normal_stress = [ev[0] for ev in eval_data]
    peak_stress = [ev[1][1] for ev in eval_data]
    dyn_stress = [ev[2][1] for ev in eval_data]
    stat_stress = [ev[3][1] for ev in eval_data]
    # dilation = [ev[4] for ev in eval_data]
    weak = [ev[5] for ev in eval_data]
    weak_p = [ev[6] for ev in eval_data]
    # p_ind = [ev[7] for ev in eval_data]

    # ==========ANALYSIS OF FRICTION COEFFICIENTS AND COHESIONS=======
    # ===========Analysis 1: Mutual linear regression=================
    peakfric_mut, peakdata_mut = rstanalysis.rst_analmut(
        normal_stress,
        peak_stress
    )
    dynfric_mut, dyndata_mut = rstanalysis.rst_analmut(
        normal_stress,
        dyn_stress
    )
    reactfric_mut, reactdata_mut = rstanalysis.rst_analmut(
        normal_stress,
        stat_stress
    )

    # ===========ANALYSIS 2: Standard regression of all data pairs ==
    peakfric_std = rstanalysis.rst_analstd(normal_stress, peak_stress)
    dynfric_std = rstanalysis.rst_analstd(normal_stress, dyn_stress)
    reactfric_std = rstanalysis.rst_analstd(normal_stress, stat_stress)

    # ==========MERGE DATA===========================================
    strength = (normal_stress, peak_stress, dyn_stress,
                stat_stress, weak, weak_p)
    fric_mut = (peakfric_mut, dynfric_mut, reactfric_mut)
    data_mut = (peakdata_mut, dyndata_mut, reactdata_mut)
    fric_std = (peakfric_std, dynfric_std, reactfric_std)

    # ===================PLOT DATA=====================================
    rstplots.plotstd(
        config['paths']['path_out'],
        config['paths']['projectname'],
        strength, fric_std
    )
    rstplots.plothist(
        config['paths']['path_out'],
        config['paths']['projectname'],
        strength, data_mut
    )
    print('>>> Friction data plotted')

    if config.getint('options', 'plot_ts'):
        rstplots.plotts(
            config['paths']['path_out'],
            config['paths']['projectname'],
            exp_data, normal_stress
        )
        print('>>> Time series data plotted')

    # ====================Save DATA===================================
    if config.getint('options', 'rst'):
        rstfiles.saveStrength(
            config['paths']['path_out'], config['paths']['projectname'],
            strength
        )
        rstfiles.saveFric(
            config['paths']['path_out'], config['paths']['projectname'],
            fric_mut, fric_std
        )
        # lid_pos = rstfiles.savelidpos(
        #     path_out,
        #     projectname,
        #     p_ind,
        #     exp_data
        # )
        print('>>> Friction data saved')
        # prop_file = [
        #     path_in + f
        #     for f in os.listdir(path_in)
        #     if f.endswith('csv')
        # ]
        # if prop_file:
        #     print(
        #         '>>> Prop file detected! Starting dilation analysis...'
        #     )
        #     rfnc.dilation(
        #         path_out, projectname,
        #         lid_pos, exp_data,
        #         prop_file[0], config.cfg
        #     )
        # else:
        #     print('>>> There is no prop file! Going on as usual.')

    if config.getint('options', 'save_ts'):
        rstfiles.saveTS(
            config['paths']['path_out'], config['paths']['projectname'],
            exp_data, normal_stress
        )
        print('>>> Time series data saved')


def open_explorer(path_out):
    """ Opens file explorer at the end of each processing """
    if sys.platform == "linux" or sys.platform == "linux2":
        p = subprocess.Popen(
            ["xdg-open", "%s" % path_out],
            stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        p.communicate()
    elif sys.platform == "darwin":
        p = subprocess.Popen(
            ["open", "%s" % path_out],
            stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        p.communicate()
    elif sys.platform == "win32":
        os.startfile(path_out)  # pylint: disable=no-member