-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathECGViewHandler.java
More file actions
332 lines (285 loc) · 8.03 KB
/
Copy pathECGViewHandler.java
File metadata and controls
332 lines (285 loc) · 8.03 KB
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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import java.awt.Color;
import java.awt.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.SwingWorker;
public class ECGViewHandler {
private ECGModel model;
private String xAxisLabel = "Time (msec)", yAxisLabel = "Potential (mV)";
public ECGViewHandler(ECGModel model) {
this.model = model;
}
public void loadFile(String file, String mode)
throws IOException {
model.readData(file, 0, Double.POSITIVE_INFINITY, mode);
}
public void loadFileSubset(String file, double start, double end, String mode)
throws IOException, NullPointerException {
try {
model.readSubsetData(file, start, end, mode);
} catch (NullPointerException ex) {
this.model = new ECGModel();
throw ex;
}
}
public double getFileLength(String filename) throws IOException {
return model.getFileLength(filename);
}
public void loadBadLeads(String file) {
model.readBadLeads(file);
}
public void loadAnnotations(String file) {
model.readAnnotations(file);
}
public void writeDataCSV(String file)
throws IOException {
model.writeDataCSV(file);
}
public void writeDataMat(String file)
throws IOException {
model.writeDataMat(file);
}
public void writeDataSubsetCSV(String file, double start, double end)
throws IOException {
model.writeDataSubsetCSV(file, start, end);
}
public void writeDataSubsetMat(String file, double start, double end)
throws IOException {
model.writeDataSubsetMat(file, start, end);
}
public void writeBadLeads(String file)
throws IOException {
model.writeBadLeads(file);
}
public void writeAnnotations(String file)
throws IOException {
model.writeAnnotations(file);
}
public void convertTo12(String file)
throws IOException {
model.convertTo12(file);
}
public int size() {
return model.size();
}
public int leadSize(int index) {
return model.getDataset(index).size();
}
public double getSampleInterval() {
return model.getSamplesPerSecond();
}
public int[][] getLayout() {
return model.getLayout();
}
public String[] getTitles() {
return model.getTitles();
}
public int getOffset() {
return model.getOffset();
}
public ECGView getView(int i, boolean withLabels) {
return new ECGView(this, model.getDataset(i), i, model.getTitle(i), withLabels);
}
public ECGView getCompositeView(boolean withLabels) {
ECGView v = new ECGView(this, model.getDataset(0), 0, "Composite", withLabels);
for(int i = 1; i < model.size(); i++) {
v.addDataset(model.getDataset(i));
}
if(withLabels) {
v.addLegend();
}
return v;
}
public void fix12Lead() {
HashMap<Integer, Undoable> changes = new HashMap<Integer, Undoable>();
for(int i = 0; i < model.size(); i++) {
changes.put(i, (ECGDataSet)model.getDataset(i).clone());
}
model.pushChange(new Change<HashMap<Integer, Undoable>, String>(
changes,
"12 lead fix"));
model.interpolate12Lead();
}
public void applyFilter(FilterDialog f, int index) {
HashMap<Integer, Undoable> changes = new HashMap<Integer, Undoable>();
changes.put(
index,
(ECGDataSet)model.getDataset(index).clone());
model.pushChange(new Change<HashMap<Integer, Undoable>, String>(
changes,
"Apply filter " + f.Id() + " to lead " + model.getTitles()[index]));
model.applyFilter(index, f.Id(), f.returnVals());
}
public void applyFilterAll(final FilterDialog f, final MainFrame attach) {
final HashMap<Integer, Undoable> changes = new HashMap<Integer, Undoable>(model.size());
for(int i = 0; i < model.size(); i++) {
changes.put(i, (ECGDataSet)model.getDataset(i).clone());
}
model.pushChange(new Change<HashMap<Integer, Undoable>, String>(
changes,
"Apply filter " + f.Id() + " to leads"));
ProgressDialog.make(new SwingWorker<Void, Void>() {
@Override
public Void doInBackground() {
setProgress(0);
for(Iterator<Integer> i = attach.getSelectedLeads().iterator(); i.hasNext();) {
int ind = i.next();
model.applyFilter(ind, f.Id(), f.returnVals());
setProgress((int)((double)ind/(double)model.size()*100));
if(isCancelled()) {
model.undo();
model.resetFuture();
break;
}
}
return null;
}
@Override
public void done() {
attach.relink();
}
}, attach);
}
public ECGView shallowFilter(int index,
int filterId,
Number[] params,
boolean withLabels) {
ECGDataSet data = (ECGDataSet)model.getDataset(index).clone();
switch(filterId) {
case 0:
data.sgolayfilt((int)params[0], (int)params[1], (int)params[2]);
break;
case 1:
data.highpassfilt((double)params[0]);
break;
case 2:
data.lowpassfilt((double)params[0]);
break;
case 3:
data.highpassfftfilt((double)params[0], 0);
break;
case 4:
data.detrend((int)params[0]);
break;
case 5:
data.waveletfilt((double)params[0], (int)params[1], (int)params[2]);
break;
case 6:
data.constofffilt((double)params[0]);
break;
case 7:
data.butterworthfilt((int)params[0],
model.getSamplesPerSecond(),
(double)params[1],
(int)params[2]);
break;
case 8:
data.harmonicDetrend();
break;
case 9:
data.medianDetrend();
break;
default:
break;
}
return new ECGView(this, data, index, getTitles()[index], withLabels);
}
public ArrayList<Annotation> getAnnotations() {
return model.getAnnotations();
}
public void addAnnotation(int type, double i) {
HashMap<Integer, Undoable> changes = new HashMap<Integer, Undoable>();
ArrayList<Annotation> annotations = model.getAnnotations();
int j = 0;
for(Annotation a : annotations) {
changes.put(j, a);
j++;
}
model.pushChange(new Change<HashMap<Integer, Undoable>, String>(
changes,
"Add type " + type + " annotation at time " + i));
model.addAnnotation(type, i);
}
public void removeAnnotation(int index) {
HashMap<Integer, Undoable> changes = new HashMap<Integer, Undoable>();
ArrayList<Annotation> annotations = model.getAnnotations();
int j = 0;
for(Annotation a : annotations) {
changes.put(j, a);
j++;
}
model.pushChange(new Change<HashMap<Integer, Undoable>, String>(
changes,
"Remove annotation"));
model.removeAnnotation(index);
}
public void clearAnnotations() {
HashMap<Integer, Undoable> changes = new HashMap<Integer, Undoable>();
ArrayList<Annotation> annotations = model.getAnnotations();
int i = 0;
for(Annotation a : annotations) {
changes.put(i, a);
i++;
}
model.pushChange(new Change<HashMap<Integer, Undoable>, String>(
changes,
"Clear annotations"));
model.clearAnnotations();
}
public void extractFeatures(int lead) {
HashMap<Integer, Undoable> changes = new HashMap<Integer, Undoable>();
ArrayList<Annotation> annotations = model.getAnnotations();
int i = 0;
for(Annotation a : annotations) {
changes.put(i, a);
i++;
}
model.pushChange(new Change<HashMap<Integer, Undoable>, String>(
changes,
"Auto Annotations"));
model.extractFeatures(lead);
}
public void setBad(int index, boolean isBad) {
HashMap<Integer, Undoable> changes = new HashMap<Integer, Undoable>();
changes.put(index, (ECGDataSet)model.getDataset(index).clone());
model.pushChange(new Change<HashMap<Integer, Undoable>, String>(
changes,
(isBad?"Set":"Unset") + " bad lead " + (index+model.getOffset())));
model.setBad(index, isBad);
}
public boolean isBad(int index) {
return model.isBad(index);
}
public void undo() {
model.undo();
}
public void redo() {
model.redo();
}
public String undoMessage() {
return model.undoMessage();
}
public String redoMessage() {
return model.redoMessage();
}
public boolean canUndo() {
return model.canUndo();
}
public boolean canRedo() {
return model.canRedo();
}
public String getxAxisLabel() {
return xAxisLabel;
}
public String getyAxisLabel() {
return yAxisLabel;
}
public void setxAxisLabel(String xAxisLabel) {
this.xAxisLabel = xAxisLabel;
}
public void setyAxisLabel(String yAxisLabel) {
this.yAxisLabel = yAxisLabel;
}
}