-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
142 lines (125 loc) · 8.72 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May 27 14:11:39 2019
@author: Emil S. Walsted
------------------------------------------------------------------------
EXAMPLE: Respiratory mechanics and work of breathing calculation
------------------------------------------------------------------------
(c) Copyright 2019 Emil Schwarz Walsted <emilwalsted@gmail.com>
This file demonstrates the use of "respemech.py". Copy and modify to your own
needs.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public$ License
along with this program. If not, see <http://www.gnu.org/licenses/>
"""
import importlib.util
import os.path
#Modify to the location of your respmech.py:
rmpath = "/Users/emilnielsen/Documents/Medicin/Forskning/Code/RespMech/respmech.py"
if not os.path.isfile(rmpath): raise ValueError("respech.py not found at the specified location: [" + rmpath + "]")
spec = importlib.util.spec_from_file_location("analyse", rmpath)
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
settings = {
"input": {
"inputfolder": "/Users/emilnielsen/Documents/Medicin/Forskning/Code/Respiratory mechanics/txtimport",
"files": "RIU_H1*.txt", #Filename or mask (e.g. "*.csv" for all CSV files in folder)
"format": {
#General settings
"samplingfrequency": 2000, #No. of data recordings per second
"decimalcharacter": ".", #Decimal character. Default is "."
},
"data": {
"column_poes": 7, #Column number containing oesophageal pressure
"column_pgas": 8, #Column number containing gastric pressure
"column_pdi": 10, #Column number containing transdiaphragmatic pressure
"column_volume": 14, #Column number containing (inspired) volume, BTPS. Exclude this and specify "integratevolumefromflow: True" to instead obtain volume by integrating the flow signal.
"column_flow": 13, #Column number containing flow
"columns_emg": [2,3,4,5,6], #The data columns containing EMG signals to calculate RMS from, e.g. [4,5,6,7,8]. Leave as [] to skip EMG calculation.
"columns_entropy": [2,3,4,5,6], #The data columns to perform entropy calculation on, e.g. [4,5,6,7,8]. Leave as [] to skip entropy calculation.
}
},
"processing": {
"mechanics": {
"breathseparationbuffer": 400, #Number of measurements to average over for breathing cycle detection (default=800). Will depend on your sampling frequency.
#Calculations:
"separateby": "volume", #Which signal to use for detecting breathing cycles. Possible values: "flow" or "volume". Default: "flow".
"inverseflow": False, #True: For calculations, inspired flow should be negative. This setting inverses the input flow signal. Default is False.
"integratevolumefromflow": False, #True: Creates the volume signal by integrating the (optionally reversed) flow signal. False: Volume is specified in input data.
"inversevolume": True, #True: For calculations, inspired volume should be positive and expired should be negative. This setting inverses the volume input signal. Default is False.
"correctvolumedrift": True, #True: Correct volume drift. False: Do not correct volume drift. Default is True
"correctvolumetrend": True, #True: Correct volume for trend changes. False: Do not correct. Default is False
"volumetrendadjustmethod": "linear",
"volumetrendpeakminheight": 0.5, #For trend adjustment: How tall (absolute value) a volume peak should at least be, to be considered a peak. Default is 0.8
"volumetrendpeakminwidth": 0.005, #For trend adjustment: How far apart (seconds) should peaks at least be, to be considered a peak. Default is 0.01
"volumetrendpeakmindistance": 0.25, #For trend adjustment: How far apart (seconds) should peaks at least be, to be considered a peak. Default is 0.25
"calcwobfromaverage": True, #False: calculates WOB for each breath, then averages. True: Averages breaths to produce an averaged Campbell diagram, from which WOB is calculated.
"avgresamplingobs": 500, #Downsampling to # of observations for breath P/V averaging. A good default would be sampling frequency divided by 8-10. Must be lower than the lowest # of observation in any inspiration or expiration in the file.
#Exclude individual breaths from analysis, if appropriate. Takes input in the format [["file1.mat", [04, 07]], ["file2.mat", [01]]]
#If no breaths should be excluded, set to []. NOTE: File name is case sensitive!
"excludebreaths": [
["RIU_H1_100W.txt", [7]],
["RIU_H14_20W.txt", [11,12]]
],
#Breath count is detected automatically. However, in some cases, the breathing pattern can lead to erroneous breathing frequencies,
#which will affect the mechanics calculations. In this case you can override the autumatically detected breath count in a specific
#file by adding below:
"breathcounts": [],
},
"emg": {
"rms_s": 0.050, #The size of the rolling window centered around the data point, to calculate EMG RMS from (in seconds). Default is 0.05s.
"remove_ecg": True, #Perform ECG removal before calculating RMS. Default is False.
"column_detect": 4, #Which of the EMG columns to use for ECG detection. Default is the first (0). Use the data column where the ECG is most prominent.
"minheight": 0.0005, #(For peak detection): The minimum height (in volt) of an R wave. Default is 0.001.
"mindistance": 0.5, #The minimum distance between R waves (in seconds). Default is 0.25
"minwidth": 0.001, #The minimum width of R waves (in seconds). Default is 0.005
"windowsize": 0.4, #Window size (in seconds) for averaging an ECG complex. Default is 0.4.
"emgplotyscale": [-0.05, 0.05], #The [lower, upper] limits of the y-axis on the diagnostic EMG plots
"remove_noise": True, #Perform EMG noise removal before calculating RMS. Default is False.
#Required for noise removal, for each file processed. Specifies the start- and end times (in seconds) for the noise profile (i.e. an area with no relevant EMG activity).
"noise_profile": [
["RIU_H1_100W.txt", "/Users/emilnielsen/Documents/Medicin/Forskning/Code/Respiratory mechanics/txtimport/RIU_H14_20W.txt", [1.5, 2.5]],
["RIU_H14_20W.txt", "", [1.5, 2.5]]
],
"save_sound": True, #Save EMG channels as sound files (at individual processing steps). Default: False.
},
"entropy": {
"entropy_epochs": 2, #Epochs used for entropy calculation. Default is 2.
"entropy_tolerance": 0.1 #Tolerance (in standard deviations) used for entropy calculation. Default is 0.1 SD.
},
},
"output": {
"outputfolder": "/Users/emilnielsen/Documents/Medicin/Forskning/Code/Respiratory mechanics/txtimport/output", #Note: Output folder must have two subfolders named "data" and "plots", respectively.
"data": {
#Data input/output
"saveaveragedata": True, #False: don"t save, True: save.
"savebreathbybreathdata": True, #False: don"t save, True: save.
"saveprocesseddata": True, #Saves the processed input data as a new datafile. False: don"t save, True: save. Default is False.
"includeignoredbreaths": False, #Whether to include ignored breaths in processed data output. Default is False.
},
"diagnostics": {
#Diagnostics plots (saved as PDF in output folder). False: don"t save, True: save.
"savepvaverage": True,
"savepvoverview": True,
"savepvindividualworkload": True,
"pvcolumns": 2,
"pvrows": 2,
"savedataviewraw": True,
"savedataviewtrimmed": True,
"savedataviewdriftcor": True
},
}
}
#Perform the analysis. Output will be saved to the specified output folder.
print("\nRunning analysis...\n")
outfolder = m.analyse(settings)
print("\n... Done. Find the results here:\n")
print(outfolder, "\n\n")