-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathECGModel.java
1006 lines (903 loc) · 28.4 KB
/
ECGModel.java
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JOptionPane;
/**
* class ECGModel - class for holding all datasets
*
* @author Dakota Williams
*/
public class ECGModel {
private ECGDataSet[] ignoredLeads;
private ECGDataSet[] points;
private int[][] layout;
private String[] titles;
private int dataOffset = 0;
private int actualSize = 130;
private double sampleFreq = 0;
private ArrayList<Annotation> annotations;
private ECGFileManager filePlugins;
private ECGFile file;
private String message;
private UndoStack<Change<HashMap<Integer, Undoable>, String>> history;
private <T> void printArrayList(ArrayList<T[]> arr) {
for(int i = 0; i < arr.size(); i++) {
System.out.println(Arrays.toString(arr.get(i)));
}
}
/**
* Constructor - initializes the model
*/
public ECGModel() {
ignoredLeads = new ECGDataSet[]{};
points = new ECGDataSet[]{};
layout = new int[][]{};
titles = new String[]{};
annotations = new ArrayList<Annotation>();
filePlugins = new ECGFileManager();
filePlugins.load();
file = null;
history = new UndoStack<Change<HashMap<Integer, Undoable>, String>>();
}
/**
* clone - deep copy of the model
*
* @return a copy of the model
*/
public ECGModel clone() {
ECGModel newModel = new ECGModel();
newModel.actualSize = this.actualSize;
newModel.sampleFreq = this.sampleFreq;
newModel.ignoredLeads = new ECGDataSet[this.ignoredLeads.length];
newModel.points = new ECGDataSet[this.points.length];
newModel.layout = new int[this.layout.length][this.layout[0].length];
newModel.titles = new String[this.titles.length];
for(int i = 0; i < this.ignoredLeads.length; i++) {
newModel.ignoredLeads[i] = (ECGDataSet)this.ignoredLeads[i].clone();
}
for(int i = 0; i < this.points.length; i++) {
newModel.points[i] = (ECGDataSet)this.points[i].clone();
}
for(Iterator<Annotation> i = this.annotations.iterator(); i.hasNext(); ) {
newModel.annotations.add(new Annotation(i.next()));
}
for(int i = 0; i < this.layout.length; i++) {
System.arraycopy(this.layout[i], 0, newModel.layout[i], 0, this.layout[0].length);
}
for(int i = 0; i < this.titles.length; i++) {
newModel.titles[i] = this.titles[i];
}
return newModel;
}
/**
* clear - clears all datasets from the model
*/
public void clear() {
ignoredLeads = new ECGDataSet[]{};
points = new ECGDataSet[]{};
layout = new int[][]{};
titles = new String[]{};
dataOffset = 0;
history.reset();
annotations.clear();
}
/**
* getAnnotations - gets a list of all annotations
*
* @return an ArrayList of all Annotations of this dataset
*/
public ArrayList<Annotation> getAnnotations() {
return new ArrayList<Annotation>(annotations);
}
/**
* addAnnotation - adds an annotation to the dataset
*
* @param type an integer representing the type of annotation
* @param i the time at which the annotation should be located
*/
public void addAnnotation(int type, double i) {
annotations.add(new Annotation(type, i));
}
/**
* setAnnotations - sets the annotations of the dataset
*
* @param annos the annotations
*/
public void setAnnotations(ArrayList<Annotation> annos) {
annotations = new ArrayList<Annotation>(annos);
}
/**
* removeAnnotation - removes an annotation
* @param i the index
*/
public void removeAnnotation(int i) {
annotations.remove(i);
}
/**
* clearAnnotations - removes all annotations associated with this dataset
*/
public void clearAnnotations() {
annotations.clear();
}
/**
* isAnnotated - removes an annotation at the specified location
*
* @param i the location to remove
*/
public boolean isAnnotated(double i) {
return annotations.contains(i);
}
/**
* toArray - creates an array representation of the model
*
* @return an array of arrays of arrays, nested like so:
* [channel][x, y][samples], sizes [# channels][2][# samples]
*/
public double[][][] toArray() {
double[][][] arr = new double[points.length][2][points[0].size()];
for(int i = 0; i < points.length; i++) {
for(int j = 0; j < points[0].size(); j++) {
arr[i][0][j] = points[i].getAt(j)[0];
arr[i][1][j] = points[i].getAt(j)[1];
}
}
return arr;
}
/**
* subsetToArray - creates an array representation of data between to times *
* @param start the time before the first sample in the subset
* @param end the time after the last sample in the subset
*/
@Deprecated
public double[][][] subsetToArray(double start, double end) {
long s = System.currentTimeMillis();
double[][][] arr = new double[points.length][2][points[0].subset(start, end).size()];
for(int i = 0; i < points.length; i++) {
for(int j = 0; j < points[0].subset(start, end).size(); j++) {
arr[i][0][j] = points[i].subset(start, end).getAt(j)[0];
arr[i][1][j] = points[i].subset(start, end).getAt(j)[1];
}
}
long e = System.currentTimeMillis();
System.out.println("Subset time (ms): " + (e - s));
return arr;
}
/**
* writeDataMat - creates a MATLAB file with the data contained in this model
*
* @param filename the name of the file to write to
*/
public void writeDataMat(String filename)
throws IOException {
int offset = 0;
if(points.length == 123) {
offset = 1;
}
(new MatFile(filename)).write(points, offset);
}
/**
* writeDataCSV - creates a Tab Separated Value file with the data
* contained in this model
*
* @param filename the name of the file to write to
*/
public void writeDataCSV(String filename)
throws IOException {
writeArrayCSV(filename, points);
}
private void writeArrayCSV(String filename, ECGDataSet[] data)
throws IOException {
int offset = 0;
if(data.length == 123) {
offset = 1;
}
(new CSVFile(filename)).write(data, offset);
}
/**
* writeDataSubsetMat - writes a subset of the data to a MATLAB file
*
* @param filename the name of the file to write to
* @param start the time before the first element in the subset
* @param end the time after the last element in the subset
*/
public void writeDataSubsetMat(String filename, double start, double end)
throws IOException {
int offset = 0;
if(points.length == 123) {
offset = 1;
}
(new MatFile(filename)).writeSubset(points,
points[0].indexBefore(start),
points[0].indexBefore(end), offset);
}
/**
* writeDataSubsetCSV - writes a subset of the data to a CSV file
*
* @param filename the name of the file to write to
* @param start the time before the first element in the subset
* @param end the time after the last element in the subset
*/
public void writeDataSubsetCSV(String filename, double start, double end)
throws IOException {
int offset = 0;
if(points.length == 123) {
offset = 1;
}
(new CSVFile(filename)).writeSubset(points,
points[0].indexBefore(start),
points[0].indexBefore(end), offset);
}
/**
* writeBadLeads - writes the bad lead numbers one per line to a file
*
* @param filename the file to write to
*/
public void writeBadLeads(String filename)
throws IOException {
int offset = 0;
if(points.length != 64) {
offset = 4;
}
PrintWriter out = new PrintWriter(filename);
for(int i = 0; i < points.length; i++) {
if(points[i].isBad()) {
out.println(getTitle(i));
}
}
out.flush();
out.close();
}
/**
* readBadLeads - reads bad leads from a file
*
* @param filename the file to read
*/
public void readBadLeads(String filename) {
int count = 1;
int offset = 0;
if(points.length != 64) {
offset = 4;
}
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String s = "";
int lead = 0;
while((s = reader.readLine()) != null) {
for(int i = 0; i < points.length; i++) {
if(getTitle(i).equals(s)) {
points[i].setBad(true);
}
}
count++;
}
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "No such file \"" + filename + "\"",
"Error", JOptionPane.ERROR_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(null,
"Error reading file \"" + filename + "\"",
"Error", JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"Improper format on line " + count,
"Error", JOptionPane.ERROR_MESSAGE);
}
}
/**
* readAnnotations - reads annotations from a previously saved annotation file
* @param filename the name of the file
*/
public void readAnnotations(String filename) {
int count = 1;
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String s = "";
String regex = "\\s+";
while((s = reader.readLine()) != null) {
String[] nums = s.split(regex);
if(nums.length < 2) {
throw new Exception();
}
addAnnotation((int)Double.parseDouble(nums[0]),Double.parseDouble(nums[1]));
count++;
}
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "No such file \"" + filename + "\"",
"Error", JOptionPane.ERROR_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(null,
"Error reading file \"" + filename + "\"",
"Error", JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"Improper format on line " + count,
"Error", JOptionPane.ERROR_MESSAGE);
}
}
/**
* writeAnnotations - writes the all of the annotations to a file
* Format: <annotation type> <annotation location>
*
* @param filename the name of the file to write to
*/
public void writeAnnotations(String filename)
throws IOException {
PrintWriter out = new PrintWriter(filename);
ArrayList<Annotation> annos = this.getAnnotations();
for(int j = 0; j < annos.size(); j++) {
out.println(annos.get(j));
}
out.flush();
out.close();
}
/**
* readData - reads raw data in from a file
* @throws IOException
* @throws FileNotFoundException
*
* @param filename the name of the file to read
*/
public void readData(String filename, double start, double length, String mode)
throws IOException, FileNotFoundException {
this.clear();
file = filePlugins.getECGFile(filename);
if(file==null) {
throw new IOException("Not a supported file extension");
}
if (length == Double.POSITIVE_INFINITY) {
length = file.getFileLength(filename);
}
int batchSize = 5000;
double loadStart = start;
double loadLength = (mode.equals("ecg") ? length : batchSize + 10);
int retval;
ArrayList<AbstractMap.SimpleEntry<Double, ArrayList<Double>>> raw;
ECGDataSet[] results = null;
if (!(mode.equals("ecg") || mode.equals("r-frequency") || mode.equals("r-intensity")))
throw new IOException("Mode '" + mode + "' not yet implemented!");
if (mode.equals("r-frequency") || mode.equals("r-intensity")) {
Main.setProgressBar("Locate R Waves", 0);
}
long startTime = System.currentTimeMillis()/1000L;
while (true) {
raw = new ArrayList<AbstractMap.SimpleEntry<Double, ArrayList<Double>>>();
retval = file.read(filename, loadStart, loadLength, raw);
if (retval != 0) {
throw new IOException("File " + filename + " is not compatible with this application.");
}
int tempLayout[][] = file.getLayout();
ArrayList<int[]> found = new ArrayList<int[]>();
int numGoodLeads = 0;
int numIgnoreLeads = 0;
boolean stop = false;
for(int i = 0; i < tempLayout.length; i++) {
if(tempLayout[i][0] < 0 || tempLayout[i][1] < 0) {
numIgnoreLeads++;
if(!stop) {
dataOffset++;
}
continue;
}
numGoodLeads++;
found.add(tempLayout[i]);
}
layout = new int[found.size()][2];
for(int i = 0; i < found.size(); i++) {
layout[i] = new int[]{found.get(i)[0], found.get(i)[1]};
}
titles = file.getTitles();
ignoredLeads = new ECGDataSet[numIgnoreLeads];
points = new ECGDataSet[numGoodLeads];
actualSize = tempLayout.length;
sampleFreq = file.getSampleInterval();
for(int i = 0; i < raw.size(); i++) {
int igoff = 0;
int normoff = 0;
for(int j = 0; j < actualSize; j++) {
if(tempLayout[j][0] < 0 || tempLayout[j][1] < 0) {
normoff++;
if(i == 0) {
ignoredLeads[j-igoff] = new ECGDataSet();
}
ignoredLeads[j-igoff].addTuple(raw.get(i).getKey(),
(double)raw.get(i).getValue().get(j));
} else {
igoff++;
if(i == 0) {
points[j-normoff] = new ECGDataSet();
}
points[j-normoff].addTuple(raw.get(i).getKey(),
(double)raw.get(i).getValue().get(j));
}
}
}
if (mode.equals("ecg")) return;
if (mode.equals("r-frequency") || mode.equals("r-intensity")) {
double progress = (loadStart+loadLength-start)/length;
long now = System.currentTimeMillis()/1000L;
float timeRemaining = (float)((now-startTime)*(1f/progress)-(now-startTime));
Main.setProgressBar("Locate R Waves", Math.min(100, (int)(100*progress)), timeRemaining);
this.extractFeatures(38);
Collections.sort(annotations);
int lastAnnotationIndex = 0;
double lastTime = annotations.get(0).getLoc();
for (int i = 0; i < annotations.size(); i++) {
Annotation annotation = annotations.get(i);
if (annotation.getLoc() > lastTime) {
lastTime = annotation.getLoc();
lastAnnotationIndex = i;
}
}
annotations.remove(lastAnnotationIndex);
if (results == null) {
results = new ECGDataSet[points.length];
for (int i = 0; i < points.length; i++) results[i] = new ECGDataSet();
}
for (int a = 0; a < annotations.size(); a++) {
for (int i = 0; i < points.length; i++) {
ECGDataSet subset = points[i].subset(annotations.get(a).getLoc(), annotations.get(a).getLoc() + 1);
ECGDataSet results_subset = results[i].subset(annotations.get(a).getLoc() - 10, annotations.get(a).getLoc() + 10);
if (subset.size() != 0 && results_subset.size() == 0) {
if (mode.equals("r-intensity")) {
results[i].addTuple(annotations.get(a).getLoc(), subset.getAt(0)[1]);
} else {
results[i].addTuple(annotations.get(a).getLoc(), subset.getAt(0)[0]);
}
}
}
}
lastTime = annotations.get(0).getLoc();
for (int i = 0; i < annotations.size(); i++) {
Annotation annotation = annotations.get(i);
if (annotation.getLoc() > lastTime) {
lastTime = annotation.getLoc();
}
}
if (loadStart + loadLength < start + length) loadStart = lastTime;
else break;
loadLength = Math.min(batchSize + 10, (start+length) - loadStart);
if (loadLength <= 0) break;
}
}
if (mode.equals("r-frequency")) {
clearAnnotations();
for (int i = 0; i < results.length; i++) {
for (int j = results[i].size() - 1; j > 0; j--) {
results[i].getAt(j)[1] -= results[i].getAt(j-1)[1];
}
results[i].getAt(0)[1] -= start;
}
}
if (mode.equals("r-frequency") || mode.equals("r-intensity")) {
points = results;
}
return;
}
public double getFileLength(String filename) throws IOException {
file = filePlugins.getECGFile(filename);
if(file==null) {
throw new IOException("Not a supported file extension");
}
return file.getFileLength(filename);
}
/**
* readSubsetData - reads a subset of raw data in from a file
* @throws IOException
* @throws FileNotFoundException
*
* @param filename the name of the file to read
* @param start the start time to read from
* @param end the end time to read to
*/
public void readSubsetData(String filename, double start, double end, String mode)
throws IOException, FileNotFoundException, NullPointerException {
readData(filename, start, end-start, mode);
for(int i = 0; i < points.length; i++) {
points[i] = points[i].subset(start, end);
}
}
/**
* getDataset - gets the dataset of channel number i
*
* @param i the index of the dataset
*/
public ECGDataSet getDataset(int i) {
return points[i];
}
/**
* size - the number of datasets in this model
*
* @return the number of datasets in this model
*/
public int size() {
return points.length;
}
/**
* setBad - sets or unsets the specified lead as a bad lead
*
* @param i the index of the lead
* @param isBad whether the lead should be set as bad
*/
public void setBad(int i, boolean isBad) {
points[i].setBad(isBad);
if(Settings.isBadInterp())
interpolateBadLead(i);
}
/**
* isBad - gets whether the lead is bad or not
*
* @param i the index of the lead
* @return the badness of the lead
*/
public boolean isBad(int i) {
return points[i].isBad();
}
/**
* getSamplesPerSecond - gets the rate at which the samples were taken
*
* @return the number of samples taken per second
*/
public double getSamplesPerSecond() {
return sampleFreq;
}
/**
* getLayout - gets the layout of leads specified by the file
* @return an array of coordinates
*/
public int[][] getLayout() {
return layout;
}
/**
* getTitles - gets the titles of the leads
* @return an array of titles
*/
public String[] getTitles() {
return titles;
}
/**
* getOffset - the number of leads before the data leads start
* @return the offset
*/
public int getOffset() {
return dataOffset;
}
/**
* getTitle - the title of the lead index requested
* @param i the index
* @return the title
*/
public String getTitle(int i) {
return titles[i];
}
/**
* extractFeatures - finds the peak of the R waves in a signal
* @param index the index of the lead to use
*/
public void extractFeatures(int index) {
HashMap<Integer, Undoable> changes = new HashMap<Integer, Undoable>();
changes.put(
index,
(ECGDataSet)this.getDataset(index).clone());
this.pushChange(new Change<HashMap<Integer, Undoable>, String>(
changes,
"Apply filter to lead " + this.getTitles()[index]));
// apply denoising filter
this.applyFilter(index, 5, new Number[] {150.0, 31, 8});
ECGDataSet lead = points[index];
final double range = 50, verticalRange = 200, verticalTimeRange = 300;
ArrayList<double[]> maxima = new ArrayList<>(); // time and value
if (lead.size() < 3) return;
double last = lead.getAt(0)[1];
double current = lead.getAt(1)[1];
double next;
for(int i = 1; i < lead.size() - 1; i++) {
next = lead.getAt(i+1)[1];
if ((last < current && next < current) || (last > current && next > current)) {
maxima.add(lead.getAt(i));
}
last = current;
current = next;
}
// undo filter
this.undo();
// find the average value for the dataset
double avg = 0;
int count = 0;
for(int i = 0; i < lead.size(); i++) {
avg += lead.getAt(i)[1];
count++;
}
avg /= count;
System.out.println(avg);
// remove any max's and min's that are within 'range' time, to ignore pacing machine and extra details
// prioritize removing those that are close to the average
while (true) {
int indexOfFirst = -1;
int indexOfSecond = -1;
double distance = -1;
for (int i = 0; i < maxima.size(); i++) {
for (int j = i + 1; j < maxima.size(); j++) {
if (Math.abs(maxima.get(j)[0] - maxima.get(i)[0]) < range || (Math.abs(maxima.get(j)[0] - maxima.get(i)[0]) < verticalTimeRange
&& Math.abs(maxima.get(j)[1] - maxima.get(i)[1]) < verticalRange)) {
double metric = Math.abs(maxima.get(j)[1] - avg) + Math.abs(maxima.get(i)[1] - avg);
if (distance == -1 || metric < distance || Math.abs(maxima.get(j)[0] - maxima.get(i)[0]) < 5) {
indexOfFirst = i;
indexOfSecond = j;
distance = metric;
}
}
}
}
if (indexOfFirst == -1) break;
System.out.println(maxima.get(indexOfFirst)[0] + ", " + maxima.get(indexOfFirst)[1] + "; "
+ maxima.get(indexOfSecond)[0] + ", " + maxima.get(indexOfSecond)[1]);
maxima.remove(indexOfSecond);
maxima.remove(indexOfFirst);
}
// determine whether we usually get min then max, or max then min
double a = 0;
double b = 0;
for (int i = 0; i < maxima.size() - 2; i += 2) {
a += maxima.get(i+1)[0] - maxima.get(i)[0];
b += maxima.get(i+2)[0] - maxima.get(i+1)[0];
}
// if (a > b) {
// for (int i = 0; i < maxima.size(); i++) {
// maxima.remove(i);
// }
// } else {
// for (int i = 1; i < maxima.size(); i++) {
// maxima.remove(i);
// }
// }
for(double[] p : maxima) {
this.annotations.add(new Annotation(2, p[0]));
}
}
/**
* interpolateBadLeads - finds bad leads and interpolates it with it's neighbors
* @param i the index
*/
public void interpolateBadLead(int i) {
if(!isBad(i)) {
return;
}
ECGDataSet newLead = new ECGDataSet();
newLead.setBad(points[i].isBad());
ArrayList<ECGDataSet> neighbors = new ArrayList<ECGDataSet>();
int north = file.getNorth(i+dataOffset);
int south = file.getSouth(i+dataOffset);
int east = file.getEast(i+dataOffset);
int west = file.getWest(i+dataOffset);
if(north >= dataOffset && !points[north-dataOffset].isBad())
neighbors.add(points[north-dataOffset]);
if(south >= dataOffset && !points[south-dataOffset].isBad())
neighbors.add(points[south-dataOffset]);
if(east >= dataOffset && !points[east-dataOffset].isBad())
neighbors.add(points[east-dataOffset]);
if(west >= dataOffset && !points[west-dataOffset].isBad())
neighbors.add(points[west-dataOffset]);
double sum;
for(int j = 0; j < points[i].size(); j++) {
sum = 0.0;
for(int k = 0; k < neighbors.size(); k++) {
sum += neighbors.get(k).getAt(j)[1];
}
newLead.addTuple(neighbors.get(0).getAt(j)[0], sum/((double)neighbors.size()));
}
points[i] = newLead;
}
/**
* interpolate12Lead - corrects certain 12 lead datasets
*/
public void interpolate12Lead() {
ECGDataSet pv1 = new ECGDataSet();
ECGDataSet pv2 = new ECGDataSet();
ECGDataSet pv3 = new ECGDataSet();
ECGDataSet pv5 = new ECGDataSet();
for(int i = 0; i < points[0].size(); i++) {
double I = points[0].getAt(i)[1];
double II = points[1].getAt(i)[1];
double V1 = points[6].getAt(i)[1];
double V2 = points[7].getAt(i)[1];
double V3 = points[8].getAt(i)[1];
double V4 = points[9].getAt(i)[1];
double V5 = points[10].getAt(i)[1];
double V6 = points[11].getAt(i)[1];
double PV1 = -0.48867*I - 0.14374*II + 0.53433*V2 - 0.14343*V3
+ 0.067212*V4 + 0.14037*V5 - 0.10467*V6;
double PV2 = 0.053699*I + 0.041180*II + 0.57189*V1 + 1.30444*V3
- 0.91712*V4 + 0.32831*V5 - 0.089629*V6;
double PV3 = 0.16719*I + 0.00098*II - 0.06027*V1 + 0.51212*V2
+ 0.84120*V4 - 0.40982*V5 + 0.04163*V6;
double PV5 = 0.15592*I - 0.055366*II + 0.040822*V1 + 0.089207*V2
- 0.28363*V3 + 0.65436*V4 + 0.63840*V6;
pv1.addTuple(points[0].getAt(i)[0], PV1);
pv2.addTuple(points[0].getAt(i)[0], PV2);
pv3.addTuple(points[0].getAt(i)[0], PV3);
pv5.addTuple(points[0].getAt(i)[0], PV5);
}
points[6] = pv1;
points[7] = pv2;
points[8] = pv3;
points[10] = pv5;
}
/**
* convertTo12 - converts the current model 120 lead format to 12 lead format and saves it
*/
public void convertTo12(String filename)
throws IOException {
// 0: make sure current model is 120 lead
if(points.length != 123) {
return; // do nothing
}
// 1: create new datasets
ECGDataSet[] newpoints = new ECGDataSet[12];
for(int i = 0; i < 12; i++) {
newpoints[i] = new ECGDataSet();
}
double time;
for(int i = 0; i < points[0].size(); i++) {
time = points[0].getAt(i)[0];
newpoints[0].addTuple(time, // I =
(points[64-1].getAt(i)[1] + points[65-1].getAt(i)[1]/2.0) // LA
- (points[15-1].getAt(i)[1] + points[16-1].getAt(i)[1]/2.0)); //- RA
newpoints[1].addTuple(time, // II=
(points[70-1].getAt(i)[1]) // LL
- (points[15-1].getAt(i)[1] + points[16-1].getAt(i)[1]/2.0)); //- RA
newpoints[2].addTuple(time, //III=
(points[70-1].getAt(i)[1]) // LL
- (points[64-1].getAt(i)[1] + points[65-1].getAt(i)[1]/2.0)); //- LA
newpoints[3].addTuple(time, //AVR=
-(newpoints[0].getAt(i)[1] + newpoints[1].getAt(i)[1])/2.0); //-(I+II)/2
newpoints[4].addTuple(time, //AVL=
(newpoints[0].getAt(i)[1] - newpoints[2].getAt(i)[1])/2.0); //(I-III)/2
newpoints[5].addTuple(time, //AVF=
(newpoints[1].getAt(i)[1] + newpoints[2].getAt(i)[1])/2.0); //(II+III)/2
newpoints[6].addTuple(time, points[25-1].getAt(i)[1]);
newpoints[7].addTuple(time, points[39-1].getAt(i)[1]);
newpoints[8].addTuple(time, points[46-1].getAt(i)[1]
+ points[53-1].getAt(i)[1]
+ points[47-1].getAt(i)[1]
+ points[54-1].getAt(i)[1]/4.0);
newpoints[9].addTuple(time, points[61-1].getAt(i)[1]);
newpoints[10].addTuple(time, points[68-1].getAt(i)[1]
+ 2*points[72-1].getAt(i)[1]/3.0);
newpoints[11].addTuple(time, points[76-1].getAt(i)[1]);
}
// 2: write dataset to file
writeArrayCSV(filename, newpoints);
}
/**
* applyFilter - applies a filter to the data
*
* @param index the lead to apply the filter to
* @param filterNum number associated with a filter
* 0 = savitzky-golay filter
* 1 = high pass
* 2 = low pass
* 3 = fft
* 4 = detrend
* @param params the params to pass to each filter
* sgfilter: 1 = left samples, 2 = right samples, 3 = polynomial degree
* high pass: 1 = threshold
* low pass: 1 = threshold
* fft: 1 = threshold
* detrend: 1 = polynomial degree
*/
public void applyFilter(int index, int filterNum, Number[] params) {
switch(filterNum) {
case 0:
points[index].sgolayfilt((int)params[0], (int)params[1], (int)params[2]);
break;
case 1:
points[index].highpassfilt((double)params[0]);
break;
case 2:
points[index].lowpassfilt((double)params[0]);
break;
case 3:
points[index].highpassfftfilt((double)params[0], 0);
break;
case 4:
points[index].detrend((int)params[0]);
break;
case 5:
points[index].waveletfilt((double)params[0], (int)params[1], (int)params[2]);
break;
case 6:
points[index].constofffilt((double)params[0]);
break;
case 7:
points[index].butterworthfilt((int)params[0],
sampleFreq,
(double)params[1],
(int)params[2]);
break;
case 8:
points[index].harmonicDetrend();
break;
case 9:
points[index].medianDetrend();
break;
default:
return;
}
if(Settings.isBadInterp())
interpolateBadLead(index);
}
public void pushChange(Change<HashMap<Integer, Undoable>, String> c) {
message = c.getMessage();
history.pushChange(c);
}
private HashMap<Integer, Undoable> getCurrentState() {
HashMap<Integer, Undoable> ret = new HashMap<Integer, Undoable>();
int i = 0;
for(; i < points.length; i++) {
ret.put(i, points[i]);
}
for(Annotation a : annotations) {
ret.put(i, a);
i++;
}
return ret;
}
public void undo() {
this.message = history.peekUndo().getMessage();
Change<HashMap<Integer, Undoable>, String> c = history.undo(
new Change<HashMap<Integer, Undoable>, String>(getCurrentState(), this.message));
ArrayList<Annotation> newanno = new ArrayList<Annotation>();
for(Map.Entry<Integer, Undoable> entry : c.getData().entrySet()) {
if(entry.getValue() instanceof ECGDataSet) {
points[entry.getKey()] = (ECGDataSet)entry.getValue();
} else if(entry.getValue() instanceof Annotation) {
newanno.add((Annotation)entry.getValue());
annotations = newanno;
}
}
this.message = c.getMessage();
}
public void redo() {
this.message = history.peekRedo().getMessage();
Change<HashMap<Integer, Undoable>, String> c = history.redo(
new Change<HashMap<Integer, Undoable>, String>(getCurrentState(), this.message));
ArrayList<Annotation> newanno = new ArrayList<Annotation>();
for(Map.Entry<Integer, Undoable> entry : c.getData().entrySet()) {
if(entry.getValue() instanceof ECGDataSet) {
points[entry.getKey()] = (ECGDataSet)entry.getValue();
} else if(entry.getValue() instanceof Annotation) {
newanno.add((Annotation)entry.getValue());
annotations = newanno;
}
}
this.message = c.getMessage();
}
public String undoMessage() {
return history.peekUndo().getMessage();
}
public String redoMessage() {
return history.peekRedo().getMessage();
}
public boolean canUndo() {
return history.canUndo();
}
public boolean canRedo() {
return history.canRedo();
}