├── src ├── test │ ├── resources │ │ ├── 20120206171956.EDF │ │ ├── 20120206171956.FIF │ │ └── 20120206171956.LOG │ └── java │ │ └── at │ │ └── jku │ │ └── pervasive │ │ └── ecg │ │ └── wfdb │ │ ├── SqrsTest.java │ │ ├── RdsampTest.java │ │ ├── Edf2MitTest.java │ │ ├── DetectQRSTest.java │ │ ├── WqrsOptionsTest.java │ │ ├── PhysioToolkitTestCase.java │ │ ├── Edf2MitOptionsTest.java │ │ ├── DetectQRSOptionsTest.java │ │ ├── HrvTest.java │ │ ├── HRVOptionsTest.java │ │ └── RdsampOptionsTest.java └── main │ └── java │ └── at │ └── jku │ └── pervasive │ └── ecg │ └── wfdb │ ├── PhysicalUnit.java │ ├── WqrsOptions.java │ ├── Edf2MitOptions.java │ ├── DetectQRSOptions.java │ ├── HRV.java │ ├── RdsampOptions.java │ ├── PhysioToolkit.java │ └── HRVOptions.java ├── Rakefile ├── .gitignore ├── README.md ├── installation_osx.md ├── LICENSE └── pom.xml /src/test/resources/20120206171956.EDF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pangratz/physiotoolkit-wrapper/master/src/test/resources/20120206171956.EDF -------------------------------------------------------------------------------- /src/test/resources/20120206171956.FIF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pangratz/physiotoolkit-wrapper/master/src/test/resources/20120206171956.FIF -------------------------------------------------------------------------------- /src/main/java/at/jku/pervasive/ecg/wfdb/PhysicalUnit.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | public enum PhysicalUnit { 4 | p, P, pd, Pd, pe, Pe, ph, Ph, pm, Pm, ps, pS, Ps, PS; 5 | 6 | public String getParameter() { 7 | return "-" + name(); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/test/java/at/jku/pervasive/ecg/wfdb/SqrsTest.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | 5 | public class SqrsTest extends PhysioToolkitTestCase { 6 | 7 | public void testSqrs() throws Exception { 8 | File testFile = getWFDBFile("/chf03.dat"); 9 | physioToolkit.sqrs(testFile); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | task :default => [:download_test_sources] 2 | 3 | task :download_test_sources do 4 | dir = "src/test/resources" 5 | `wget -P #{dir} http://www.physionet.org/atm/chfdb/chf03/export/chf03.zip` 6 | `tar -xvzf #{dir}/chf03.zip -C #{dir}` 7 | `rm #{dir}/chf03.zip` 8 | 9 | `wget -P #{dir} http://www.physionet.org/atm/fantasia/f1o01/export/f1o01.zip` 10 | `tar -xvzf #{dir}/f1o01.zip -C #{dir}` 11 | `rm #{dir}/f1o01.zip` 12 | end -------------------------------------------------------------------------------- /src/test/resources/20120206171956.LOG: -------------------------------------------------------------------------------- 1 | 2 | Begin of Measurement: 20120206171956 3 | Down Sample Frequency = 200 Hz 4 | Battery Voltage = 1.2125000000000001 V 5 | Free Card Space = 24763392 Bytes 6 | Shutdown Duration = 1500 min 7 | Manual Off = true 8 | Event Press Time = 1.0 sec 9 | Double Key Press Time = 0.0 sec 10 | Hold Off Time = 10.0 sec 11 | Hold Off Release Time = 0.5 sec 12 | End of Measurement: 20120206180456 13 | HeartMAN SW.2.2 14 | HeartMa 15 | Measurement ok! Stopped by user -------------------------------------------------------------------------------- /src/test/java/at/jku/pervasive/ecg/wfdb/RdsampTest.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | 5 | public class RdsampTest extends PhysioToolkitTestCase { 6 | 7 | public void testRdsamp() throws Exception { 8 | copyFileToTemp(getFile("/f1o01.hea")); 9 | File datFile = copyFileToTemp(getFile("/f1o01.dat")); 10 | 11 | File record = new File(datFile.getParentFile(), "f1o01"); 12 | RdsampOptions options = new RdsampOptions(record); 13 | 14 | File outputFile = physioToolkit.rdsamp(options); 15 | assertNotNull(outputFile); 16 | assertTrue(outputFile.exists()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### Project specific 2 | src/test/resources/chf03* 3 | src/test/resources/f1o01* 4 | 5 | 6 | 7 | ### Maven 8 | target 9 | 10 | 11 | 12 | 13 | 14 | ### Mac OS X 15 | .DS_Store 16 | 17 | # Thumbnails 18 | ._* 19 | 20 | # Files that might appear on external disk 21 | .Spotlight-V100 22 | .Trashes 23 | 24 | 25 | 26 | 27 | 28 | ### Eclipse 29 | *.pydevproject 30 | .project 31 | .metadata 32 | bin 33 | tmp/** 34 | tmp/**/* 35 | *.tmp 36 | *.bak 37 | *.swp 38 | *~.nib 39 | local.properties 40 | .classpath 41 | .settings/ 42 | .loadpath 43 | 44 | # External tool builders 45 | .externalToolBuilders/ 46 | 47 | # Locally stored "Eclipse launch configurations" 48 | *.launch 49 | 50 | # CDT-specific 51 | .cproject 52 | 53 | # PDT-specific 54 | .buildpath 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##Java Wrapper for [PhysioNet](http://www.physionet.org/)'s [PhysioToolkit](http://www.physionet.org/physiotools/) 2 | 3 | ### Prerequisites 4 | 5 | - Installed [WFDB](http://www.physionet.org/physiotools/wfdb.shtml) software package 6 | - Installed [HRV Toolkit](http://www.physionet.org/tutorials/hrv-toolkit/) 7 | - [Maven](http://maven.apache.org/) for building 8 | - Rake (to get the test files) 9 | 10 | ### Installation 11 | 12 | ``` 13 | git clone git@github.com:pangratz/physiotoolkit-wrapper.git 14 | cd physiotoolkit-wrapper 15 | ``` 16 | 17 | #### Execute tests 18 | 19 | Download the files needed for the tests via executing ```rake```. This has to be done once. 20 | 21 | Run all tests via ```mvn clean test``` 22 | 23 | #### Create JAR 24 | 25 | Run ````mvn package```` -------------------------------------------------------------------------------- /src/test/java/at/jku/pervasive/ecg/wfdb/Edf2MitTest.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | 5 | public class Edf2MitTest extends PhysioToolkitTestCase { 6 | 7 | public void testEdf2Mit() throws Exception { 8 | File edfFile = copyFileToTemp(getFile("/20120206171956.EDF")); 9 | 10 | Edf2MitOptions options = new Edf2MitOptions(edfFile); 11 | options.setBaseDirectory(tmpDir); 12 | System.out.println(options.getCommand()); 13 | 14 | String name = physioToolkit.edf2mit(options); 15 | 16 | File datFile = new File(tmpDir, name + ".dat"); 17 | assertTrue(datFile.exists()); 18 | assertTrue(datFile.length() > 0L); 19 | 20 | File heaFile = new File(tmpDir, name + ".hea"); 21 | assertTrue(heaFile.exists()); 22 | assertTrue(heaFile.length() > 0L); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /installation_osx.md: -------------------------------------------------------------------------------- 1 | ### Prerequisites 2 | 3 | Installed [Homebrew](http://mxcl.github.com/homebrew/) so ```WFDB``` and ```HRV Toolkit``` can be easily added to the ```PATH``` environment variable. If Homebrew is not installed, you have to make sure the installed package locations are set in the ```PATH```. 4 | 5 | ## Install WFDB 6 | 7 | - download and unzip http://physionet.org/physiotools/wfdb.tar.gz 8 | - cd into the unzipped folder 9 | - mate conf/darwin* 10 | - remove all "-arch ppc" 11 | - execute: "./configure --prefix=/usr/local/Cellar/wfdb/10.5.10 -m32" 12 | - execute: "make install" 13 | - execute: "make check" to see if tests pass 14 | - add WFDB to path by "brew link wfdb" 15 | 16 | ## Install HRV Toolkit 17 | 18 | - download and unzip http://physionet.org/tutorials/hrv-toolkit/HRV-i386-Darwin.tar.gz 19 | - cd into unzipped folder 20 | - execute "mkdir -p /usr/local/Cellar/hrv/1.0/bin" 21 | - copy all files: "cp * /usr/local/Cellar/hrv/1.0/bin" 22 | - add HRV to path by "brew link hrv" -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2012 Clemens Müller 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 7 | of the Software, and to permit persons to whom the Software is furnished to do 8 | so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. -------------------------------------------------------------------------------- /src/main/java/at/jku/pervasive/ecg/wfdb/WqrsOptions.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.util.List; 5 | 6 | public class WqrsOptions extends DetectQRSOptions { 7 | 8 | private boolean annotateJPoints, resample; 9 | private int powerLine = -1; 10 | 11 | public WqrsOptions(File record) { 12 | super(record); 13 | setCommand("wqrs"); 14 | } 15 | 16 | @Override 17 | public List getCommand() { 18 | List cmd = super.getCommand(); 19 | 20 | if (annotateJPoints) { 21 | cmd.add("-j"); 22 | } 23 | 24 | if (resample) { 25 | cmd.add("-R"); 26 | } 27 | 28 | if (powerLine != -1) { 29 | cmd.add("-p"); 30 | cmd.add(Integer.toString(powerLine)); 31 | } 32 | 33 | return cmd; 34 | } 35 | 36 | public int getPowerLine() { 37 | return powerLine; 38 | } 39 | 40 | public boolean isAnnotateJPoints() { 41 | return annotateJPoints; 42 | } 43 | 44 | public boolean isResample() { 45 | return resample; 46 | } 47 | 48 | public WqrsOptions setAnnotateJPoints(boolean annotateJPoints) { 49 | this.annotateJPoints = annotateJPoints; 50 | return this; 51 | } 52 | 53 | public WqrsOptions setPowerLine(int powerLine) { 54 | this.powerLine = powerLine; 55 | return this; 56 | } 57 | 58 | public WqrsOptions setResample(boolean resample) { 59 | this.resample = resample; 60 | return this; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/at/jku/pervasive/ecg/wfdb/DetectQRSTest.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.net.URISyntaxException; 6 | 7 | public class DetectQRSTest extends PhysioToolkitTestCase { 8 | 9 | public void testSqrs() throws URISyntaxException, IOException, 10 | InterruptedException { 11 | File testFile = getTestFile(); 12 | 13 | DetectQRSOptions options = new DetectQRSOptions(testFile); 14 | physioToolkit.detectQRS(options); 15 | 16 | File qrsFile = new File(options.getBaseDirectory(), "chf03.qrs"); 17 | assertTrue(qrsFile.exists()); 18 | } 19 | 20 | public void testSqrs125() throws URISyntaxException, IOException, 21 | InterruptedException { 22 | File testFile = getTestFile(); 23 | 24 | DetectQRSOptions options = new DetectQRSOptions(testFile); 25 | options.setCommand("sqrs125"); 26 | physioToolkit.detectQRS(options); 27 | 28 | File qrsFile = new File(options.getBaseDirectory(), "chf03.qrs"); 29 | assertTrue(qrsFile.exists()); 30 | } 31 | 32 | public void testWqrs() throws URISyntaxException, IOException, 33 | InterruptedException { 34 | File testFile = getTestFile(); 35 | 36 | DetectQRSOptions options = new WqrsOptions(testFile); 37 | physioToolkit.detectQRS(options); 38 | 39 | File qrsFile = new File(options.getBaseDirectory(), "chf03.wqrs"); 40 | assertTrue(qrsFile.exists()); 41 | } 42 | 43 | protected File getTestFile() throws IOException, URISyntaxException { 44 | copyFileToTemp(getFile("/chf03.hea")); 45 | copyFileToTemp(getFile("/chf03.dat")); 46 | return new File(tmpDir, "chf03"); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/at/jku/pervasive/ecg/wfdb/WqrsOptionsTest.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.util.List; 5 | 6 | public class WqrsOptionsTest extends PhysioToolkitTestCase { 7 | 8 | public void testAnnotateJPoints() throws Exception { 9 | File record = getWFDBFile("/chf03.dat"); 10 | WqrsOptions options = new WqrsOptions(record); 11 | options.setAnnotateJPoints(true); 12 | 13 | List command = options.getCommand(); 14 | assertEquals(4, command.size()); 15 | assertTrue(command.contains("-j")); 16 | assertTrue(command.indexOf("-j") > command.indexOf("wqrs")); 17 | } 18 | 19 | public void testResample() throws Exception { 20 | File record = getWFDBFile("/chf03.dat"); 21 | WqrsOptions options = new WqrsOptions(record); 22 | options.setResample(true); 23 | 24 | List command = options.getCommand(); 25 | assertEquals(4, command.size()); 26 | assertTrue(command.contains("-R")); 27 | assertTrue(command.indexOf("-R") > command.indexOf("wqrs")); 28 | } 29 | 30 | public void testSpecifyPowerLine() throws Exception { 31 | File record = getWFDBFile("/chf03.dat"); 32 | WqrsOptions options = new WqrsOptions(record); 33 | options.setPowerLine(100); 34 | 35 | List command = options.getCommand(); 36 | assertEquals(5, command.size()); 37 | assertTrue(command.contains("-p")); 38 | int pIndex = command.indexOf("-p"); 39 | assertTrue(pIndex > command.indexOf("wqrs")); 40 | assertTrue(pIndex + 1 == command.indexOf("100")); 41 | } 42 | 43 | public void testWqrs() throws Exception { 44 | File record = getWFDBFile("/chf03.dat"); 45 | WqrsOptions options = new WqrsOptions(record); 46 | 47 | List command = options.getCommand(); 48 | assertEquals("wqrs", command.get(0)); 49 | assertFalse(command.contains("sqrs")); 50 | assertFalse(command.contains("sqrs125")); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/at/jku/pervasive/ecg/wfdb/Edf2MitOptions.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import java.util.UUID; 7 | 8 | import org.apache.commons.lang3.ArrayUtils; 9 | import org.apache.commons.lang3.StringUtils; 10 | 11 | public class Edf2MitOptions { 12 | 13 | private File baseDirectory; 14 | private final File edfFile; 15 | private boolean isBigEndian; 16 | private final String outputName; 17 | private int[] signals; 18 | 19 | public Edf2MitOptions(File edfFile) { 20 | super(); 21 | this.outputName = UUID.randomUUID().toString(); 22 | this.edfFile = edfFile; 23 | if (edfFile != null && edfFile.exists()) { 24 | this.baseDirectory = edfFile.getParentFile(); 25 | } 26 | } 27 | 28 | public File getBaseDirectory() { 29 | return baseDirectory; 30 | } 31 | 32 | public List getCommand() { 33 | List command = new ArrayList(); 34 | command.add("edf2mit"); 35 | if (edfFile != null) { 36 | command.add("-i"); 37 | command.add(edfFile.getName()); 38 | } 39 | 40 | command.add("-r"); 41 | command.add(outputName); 42 | 43 | if (isBigEndian) { 44 | command.add("-b"); 45 | } 46 | 47 | if (ArrayUtils.isNotEmpty(signals)) { 48 | command.add("-s"); 49 | command.add(StringUtils.join(ArrayUtils.toObject(signals), " ")); 50 | } 51 | 52 | return command; 53 | } 54 | 55 | public String getOutputName() { 56 | return outputName; 57 | } 58 | 59 | public boolean isBigEndian() { 60 | return isBigEndian; 61 | } 62 | 63 | public Edf2MitOptions setBaseDirectory(File baseDirectory) { 64 | this.baseDirectory = baseDirectory; 65 | return this; 66 | } 67 | 68 | public Edf2MitOptions setBigEndian(boolean isBigEndian) { 69 | this.isBigEndian = isBigEndian; 70 | return this; 71 | } 72 | 73 | public Edf2MitOptions setSignals(int... signals) { 74 | this.signals = signals; 75 | return this; 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/test/java/at/jku/pervasive/ecg/wfdb/PhysioToolkitTestCase.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.net.URISyntaxException; 6 | import java.net.URL; 7 | 8 | import junit.framework.TestCase; 9 | 10 | import org.apache.commons.io.FileUtils; 11 | import org.apache.commons.io.FilenameUtils; 12 | 13 | import com.google.common.io.Files; 14 | 15 | public class PhysioToolkitTestCase extends TestCase { 16 | 17 | protected PhysioToolkit physioToolkit; 18 | protected File tmpDir; 19 | 20 | public void testGetFile() throws Exception { 21 | File file = getFile("/chf03.dat"); 22 | assertNotNull(file); 23 | assertEquals("chf03.dat", file.getName()); 24 | } 25 | 26 | public void testGetWFDBFile() throws Exception { 27 | File wfdbFile = getWFDBFile("/chf03.dat"); 28 | assertNotNull(wfdbFile); 29 | assertEquals("chf03", wfdbFile.getName()); 30 | } 31 | 32 | public void testIsPhysioToolkitInstalled() throws Exception { 33 | assertTrue(physioToolkit.isInstalled()); 34 | } 35 | 36 | protected File copyFileToTemp(File f) throws IOException { 37 | FileUtils.copyFileToDirectory(f, tmpDir); 38 | return new File(tmpDir, f.getName()); 39 | } 40 | 41 | protected File getFile(String path) throws URISyntaxException { 42 | URL url = this.getClass().getResource(path); 43 | return new File(url.toURI()); 44 | } 45 | 46 | protected File getWFDBFile(String pathToDat) throws URISyntaxException { 47 | URL url = this.getClass().getResource(pathToDat); 48 | File datFile = new File(url.toURI()); 49 | String wfdbFile = FilenameUtils.getBaseName(pathToDat); 50 | return new File(datFile.getParentFile(), wfdbFile); 51 | } 52 | 53 | @Override 54 | protected void setUp() throws Exception { 55 | physioToolkit = new PhysioToolkit(); 56 | 57 | tmpDir = Files.createTempDir(); 58 | } 59 | 60 | @Override 61 | protected void tearDown() throws Exception { 62 | physioToolkit = null; 63 | 64 | if (tmpDir != null) { 65 | tmpDir.delete(); 66 | } 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | at.jku.pervasive.ecg 5 | physiotoolkit-wrapper 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | UTF-8 10 | 11 | 12 | UTF-8 13 | 14 | 15 | 16 | 17 | com.google.guava 18 | guava 19 | 11.0.1 20 | 21 | 22 | joda-time 23 | joda-time 24 | 2.0 25 | 26 | 27 | commons-io 28 | commons-io 29 | 1.4 30 | 31 | 32 | org.apache.commons 33 | commons-lang3 34 | 3.1 35 | 36 | 37 | junit 38 | junit 39 | 4.10 40 | test 41 | 42 | 43 | 44 | 45 | 46 | org.apache.maven.plugins 47 | maven-compiler-plugin 48 | 2.3.2 49 | 50 | 1.6 51 | 1.6 52 | 53 | 54 | 55 | org.apache.maven.plugins 56 | maven-surefire-plugin 57 | 2.11 58 | 59 | 60 | org.apache.maven.surefire 61 | surefire-junit47 62 | 2.11 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | org.codehaus.mojo 72 | cobertura-maven-plugin 73 | 2.5.1 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /src/test/java/at/jku/pervasive/ecg/wfdb/Edf2MitOptionsTest.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.net.URISyntaxException; 5 | import java.util.List; 6 | 7 | import org.apache.commons.lang3.StringUtils; 8 | 9 | public class Edf2MitOptionsTest extends PhysioToolkitTestCase { 10 | 11 | public void test() throws URISyntaxException { 12 | File edfFile = getFile("/20120206171956.EDF"); 13 | Edf2MitOptions options = new Edf2MitOptions(edfFile); 14 | 15 | List command = options.getCommand(); 16 | assertNotNull(command); 17 | assertEquals(5, command.size()); 18 | assertEquals("edf2mit", command.get(0)); 19 | 20 | // test if edf file is available and directly after -i option 21 | assertTrue(command.contains("-i")); 22 | int iIndex = command.indexOf("-i"); 23 | assertTrue(command.contains(edfFile.getName())); 24 | assertTrue(iIndex + 1 == command.indexOf(edfFile.getName())); 25 | 26 | // test if output file is available and directly after -r option 27 | assertTrue(command.contains("-r")); 28 | int rIndex = command.indexOf("-r"); 29 | String outputName = options.getOutputName(); 30 | assertTrue(StringUtils.isNotBlank(options.getOutputName())); 31 | assertTrue(command.contains(outputName)); 32 | assertTrue(rIndex + 1 == command.indexOf(outputName)); 33 | } 34 | 35 | public void testBigEndian() throws URISyntaxException { 36 | File edfFile = getFile("/20120206171956.EDF"); 37 | Edf2MitOptions options = new Edf2MitOptions(edfFile); 38 | options.setBigEndian(true); 39 | 40 | List command = options.getCommand(); 41 | assertEquals(6, command.size()); 42 | assertTrue(command.contains("-b")); 43 | assertTrue(command.indexOf("-b") > command.indexOf("edf2mit")); 44 | } 45 | 46 | public void testDefaultEndian() throws URISyntaxException { 47 | File edfFile = getFile("/20120206171956.EDF"); 48 | Edf2MitOptions options = new Edf2MitOptions(edfFile); 49 | 50 | List command = options.getCommand(); 51 | assertEquals(5, command.size()); 52 | assertFalse(command.contains("-b")); 53 | } 54 | 55 | public void testNoSignals() throws URISyntaxException { 56 | File edfFile = getFile("/20120206171956.EDF"); 57 | Edf2MitOptions options = new Edf2MitOptions(edfFile); 58 | options.setSignals(); 59 | 60 | List command = options.getCommand(); 61 | assertEquals(5, command.size()); 62 | assertFalse(command.contains("-s")); 63 | } 64 | 65 | public void testSignals() throws URISyntaxException { 66 | File edfFile = getFile("/20120206171956.EDF"); 67 | Edf2MitOptions options = new Edf2MitOptions(edfFile); 68 | options.setSignals(0, 1, 2); 69 | 70 | List command = options.getCommand(); 71 | assertEquals(7, command.size()); 72 | assertTrue(command.contains("-s")); 73 | int sIndex = command.indexOf("-s"); 74 | assertTrue(command.contains("0 1 2")); 75 | assertTrue(sIndex + 1 == command.indexOf("0 1 2")); 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/at/jku/pervasive/ecg/wfdb/DetectQRSOptions.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | 7 | import org.joda.time.LocalTime; 8 | 9 | public class DetectQRSOptions { 10 | 11 | private File baseDirectory; 12 | private String command; 13 | private boolean highResolutionMode; 14 | private final File record; 15 | private int signal = -1, treshold = -1; 16 | private LocalTime startTime, endTime; 17 | 18 | public DetectQRSOptions(File record) { 19 | super(); 20 | this.command = "sqrs"; 21 | this.record = record; 22 | if (record != null) { 23 | this.baseDirectory = record.getParentFile(); 24 | } 25 | } 26 | 27 | public File getBaseDirectory() { 28 | return baseDirectory; 29 | } 30 | 31 | public List getCommand() { 32 | List cmd = new LinkedList(); 33 | cmd.add(command); 34 | cmd.add("-r"); 35 | cmd.add(record.getName()); 36 | 37 | if (startTime != null) { 38 | cmd.add("-f"); 39 | cmd.add(startTime.toString("HH:mm:ss")); 40 | } 41 | 42 | if (endTime != null) { 43 | cmd.add("-t"); 44 | cmd.add(endTime.toString("HH:mm:ss")); 45 | } 46 | 47 | if (highResolutionMode) { 48 | cmd.add("-H"); 49 | } 50 | 51 | if (signal != -1) { 52 | cmd.add("-s"); 53 | cmd.add(Integer.toString(signal)); 54 | } 55 | 56 | if (treshold != -1) { 57 | cmd.add("-m"); 58 | cmd.add(Integer.toString(treshold)); 59 | } 60 | 61 | return cmd; 62 | } 63 | 64 | public LocalTime getEndTime() { 65 | return endTime; 66 | } 67 | 68 | public File getRecord() { 69 | return record; 70 | } 71 | 72 | public int getSignal() { 73 | return signal; 74 | } 75 | 76 | public LocalTime getStartTime() { 77 | return startTime; 78 | } 79 | 80 | public int getTreshold() { 81 | return treshold; 82 | } 83 | 84 | public boolean isHighResolutionMode() { 85 | return highResolutionMode; 86 | } 87 | 88 | public DetectQRSOptions setBaseDirectory(File baseDirectory) { 89 | this.baseDirectory = baseDirectory; 90 | return this; 91 | } 92 | 93 | public DetectQRSOptions setCommand(String command) { 94 | this.command = command; 95 | return this; 96 | } 97 | 98 | public DetectQRSOptions setEndTime(LocalTime endTime) { 99 | this.endTime = endTime; 100 | return this; 101 | } 102 | 103 | public DetectQRSOptions setHighResolutionMode(boolean highResolutionMode) { 104 | this.highResolutionMode = highResolutionMode; 105 | return this; 106 | } 107 | 108 | public DetectQRSOptions setSignal(int signal) { 109 | this.signal = signal; 110 | return this; 111 | } 112 | 113 | public DetectQRSOptions setStartTime(LocalTime startTime) { 114 | this.startTime = startTime; 115 | return this; 116 | } 117 | 118 | public DetectQRSOptions setTreshold(int treshold) { 119 | this.treshold = treshold; 120 | return this; 121 | } 122 | 123 | } 124 | -------------------------------------------------------------------------------- /src/main/java/at/jku/pervasive/ecg/wfdb/HRV.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.util.Scanner; 4 | 5 | public class HRV { 6 | 7 | private double AVNN, SDNN, SDANN, SDNNIDX, NNRR, rMSSD, pNN20, pNN50, 8 | TOT_PWR, ULF_PWR, VLF_PWR, LF_PWR, HF_PWR, LFHF; 9 | 10 | public HRV(String getHrvResult) { 11 | super(); 12 | System.out.println(getHrvResult); 13 | parse(getHrvResult); 14 | } 15 | 16 | public double getAVNN() { 17 | return AVNN; 18 | } 19 | 20 | public double getHF_PWR() { 21 | return HF_PWR; 22 | } 23 | 24 | public double getLF_PWR() { 25 | return LF_PWR; 26 | } 27 | 28 | public double getLFHF() { 29 | return LFHF; 30 | } 31 | 32 | public double getNNRR() { 33 | return NNRR; 34 | } 35 | 36 | public double getpNN20() { 37 | return pNN20; 38 | } 39 | 40 | public double getpNN50() { 41 | return pNN50; 42 | } 43 | 44 | public double getrMSSD() { 45 | return rMSSD; 46 | } 47 | 48 | public double getSDANN() { 49 | return SDANN; 50 | } 51 | 52 | public double getSDNN() { 53 | return SDNN; 54 | } 55 | 56 | public double getSDNNIDX() { 57 | return SDNNIDX; 58 | } 59 | 60 | public double getTOT_PWR() { 61 | return TOT_PWR; 62 | } 63 | 64 | public double getULF_PWR() { 65 | return ULF_PWR; 66 | } 67 | 68 | public double getVLF_PWR() { 69 | return VLF_PWR; 70 | } 71 | 72 | private void parse(String string) { 73 | Scanner s = new Scanner(string); 74 | while (s.hasNextLine()) { 75 | processLine(s.nextLine()); 76 | } 77 | } 78 | 79 | private void processLine(String line) { 80 | Scanner s = new Scanner(line); 81 | s.useDelimiter(" = "); 82 | if (s.hasNext()) { 83 | String name = s.next().trim(); 84 | if (s.hasNext()) { 85 | String valueStr = s.next().trim(); 86 | double value = Double.parseDouble(valueStr); 87 | 88 | /** 89 | * next: beautiful String switch 90 | * 91 | * we're using JDK6 and the String switch statement has been made 92 | * available in JDK7 - 16 years after requesting it: 93 | * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=1223179 94 | **/ 95 | if ("AVNN".equals(name)) { 96 | this.AVNN = value; 97 | } else if ("SDNN".equals(name)) { 98 | this.SDNN = value; 99 | } else if ("SDANN".equals(name)) { 100 | this.SDANN = value; 101 | } else if ("SDNNIDX".equals(name)) { 102 | this.SDNNIDX = value; 103 | } else if ("NN/RR".equals(name)) { 104 | this.NNRR = value; 105 | } else if ("rMSSD".equals(name)) { 106 | this.rMSSD = value; 107 | } else if ("pNN20".equals(name)) { 108 | this.pNN20 = value; 109 | } else if ("pNN50".equals(name)) { 110 | this.pNN50 = value; 111 | } else if ("TOT PWR".equals(name)) { 112 | this.TOT_PWR = value; 113 | } else if ("ULF PWR".equals(name)) { 114 | this.ULF_PWR = value; 115 | } else if ("VLF PWR".equals(name)) { 116 | this.VLF_PWR = value; 117 | } else if ("LF PWR".equals(name)) { 118 | this.LF_PWR = value; 119 | } else if ("HF PWR".equals(name)) { 120 | this.HF_PWR = value; 121 | } else if ("LF/HF".equals(name)) { 122 | this.LFHF = value; 123 | } 124 | } 125 | } 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/main/java/at/jku/pervasive/ecg/wfdb/RdsampOptions.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | import java.util.UUID; 7 | 8 | import org.apache.commons.lang3.ArrayUtils; 9 | import org.apache.commons.lang3.StringUtils; 10 | import org.joda.time.LocalTime; 11 | 12 | public class RdsampOptions { 13 | 14 | private File baseDirectory; 15 | private final String outputName; 16 | private PhysicalUnit physicalUnit; 17 | private final File record; 18 | private int[] signals; 19 | private LocalTime startTime, endTime; 20 | private boolean useCSV, highResolutionMode, printColumHeadings; 21 | 22 | public RdsampOptions(File record) { 23 | super(); 24 | this.outputName = UUID.randomUUID().toString(); 25 | this.record = record; 26 | if (record != null) { 27 | this.baseDirectory = record.getParentFile(); 28 | } 29 | } 30 | 31 | public File getBaseDirectory() { 32 | return baseDirectory; 33 | } 34 | 35 | // -s SIGNAL [SIGNAL ...] print only the specified signal(s) 36 | /** 37 | * TODO -l INTERVAL truncate output after the specified time interval 38 | * (hh:mm:ss) TODO -S SIGNAL search for a valid sample of the specified SIGNAL 39 | * at or after // the time specified with -f, and begin printing then 40 | */ 41 | public List getCommand() { 42 | List cmd = new LinkedList(); 43 | cmd.add("rdsamp"); 44 | cmd.add("-r"); 45 | cmd.add(record.getName()); 46 | 47 | if (useCSV) { 48 | cmd.add("-c"); 49 | } 50 | if (printColumHeadings) { 51 | cmd.add("-v"); 52 | } 53 | 54 | if (ArrayUtils.isNotEmpty(signals)) { 55 | cmd.add("-s"); 56 | cmd.add(StringUtils.join(ArrayUtils.toObject(signals), " ")); 57 | } 58 | 59 | if (highResolutionMode) { 60 | cmd.add("-H"); 61 | } 62 | 63 | if (physicalUnit != null) { 64 | cmd.add(physicalUnit.getParameter()); 65 | } 66 | 67 | if (startTime != null) { 68 | cmd.add("-f"); 69 | cmd.add(startTime.toString("HH:mm:ss")); 70 | } 71 | if (endTime != null) { 72 | cmd.add("-t"); 73 | cmd.add(endTime.toString("HH:mm:ss")); 74 | } 75 | 76 | return cmd; 77 | } 78 | 79 | public LocalTime getEndTime() { 80 | return endTime; 81 | } 82 | 83 | public String getOutputName() { 84 | return outputName; 85 | } 86 | 87 | public PhysicalUnit getPhysicalUnit() { 88 | return physicalUnit; 89 | } 90 | 91 | public File getRecord() { 92 | return record; 93 | } 94 | 95 | public int[] getSignals() { 96 | return signals; 97 | } 98 | 99 | public LocalTime getStartTime() { 100 | return startTime; 101 | } 102 | 103 | public boolean isHighResolutionMode() { 104 | return highResolutionMode; 105 | } 106 | 107 | public boolean isPrintColumHeadings() { 108 | return printColumHeadings; 109 | } 110 | 111 | public boolean isUseCSV() { 112 | return useCSV; 113 | } 114 | 115 | public RdsampOptions setBaseDirectory(File baseDirectory) { 116 | this.baseDirectory = baseDirectory; 117 | return this; 118 | } 119 | 120 | public RdsampOptions setEndTime(LocalTime endTime) { 121 | this.endTime = endTime; 122 | return this; 123 | } 124 | 125 | public RdsampOptions setHighResolutionMode(boolean highResolutionMode) { 126 | this.highResolutionMode = highResolutionMode; 127 | return this; 128 | } 129 | 130 | public RdsampOptions setPhysicalUnit(PhysicalUnit physicalUnit) { 131 | this.physicalUnit = physicalUnit; 132 | return this; 133 | } 134 | 135 | public RdsampOptions setPrintColumHeadings(boolean printColumHeadings) { 136 | this.printColumHeadings = printColumHeadings; 137 | return this; 138 | } 139 | 140 | public RdsampOptions setSignals(int... signals) { 141 | this.signals = signals; 142 | return this; 143 | } 144 | 145 | public RdsampOptions setStartTime(LocalTime startTime) { 146 | this.startTime = startTime; 147 | return this; 148 | } 149 | 150 | public RdsampOptions setUseCSV(boolean useCSV) { 151 | this.useCSV = useCSV; 152 | return this; 153 | } 154 | 155 | } 156 | -------------------------------------------------------------------------------- /src/test/java/at/jku/pervasive/ecg/wfdb/DetectQRSOptionsTest.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.net.URISyntaxException; 5 | import java.util.List; 6 | 7 | import org.joda.time.LocalTime; 8 | 9 | public class DetectQRSOptionsTest extends PhysioToolkitTestCase { 10 | 11 | public void testBaseDirectory() throws URISyntaxException { 12 | File record = getWFDBFile("/chf03.dat"); 13 | DetectQRSOptions options = new DetectQRSOptions(record); 14 | 15 | File baseDirectory = options.getBaseDirectory(); 16 | assertNotNull(baseDirectory); 17 | assertEquals(record.getParentFile(), baseDirectory); 18 | } 19 | 20 | public void testEndTime() throws URISyntaxException { 21 | File record = getWFDBFile("/chf03.dat"); 22 | DetectQRSOptions options = new DetectQRSOptions(record); 23 | options.setEndTime(new LocalTime(3, 4, 5)); 24 | 25 | List command = options.getCommand(); 26 | assertEquals(5, command.size()); 27 | assertTrue(command.contains("-t")); 28 | int tIndex = command.indexOf("-t"); 29 | assertTrue(tIndex > command.indexOf("sqrs")); 30 | assertTrue(tIndex + 1 == command.indexOf("03:04:05")); 31 | } 32 | 33 | public void testFile() throws URISyntaxException { 34 | File record = getWFDBFile("/chf03.dat"); 35 | DetectQRSOptions options = new DetectQRSOptions(record); 36 | 37 | List command = options.getCommand(); 38 | assertNotNull(command); 39 | assertEquals(3, command.size()); 40 | assertEquals("sqrs", command.get(0)); 41 | assertEquals("-r", command.get(1)); 42 | assertEquals(record.getName(), command.get(2)); 43 | } 44 | 45 | public void testHighResolutionMode() throws URISyntaxException { 46 | File record = getWFDBFile("/chf03.dat"); 47 | DetectQRSOptions options = new DetectQRSOptions(record); 48 | options.setHighResolutionMode(true); 49 | 50 | List command = options.getCommand(); 51 | assertTrue(command.contains("-H")); 52 | assertTrue(command.indexOf("-H") > command.indexOf("sqrs")); 53 | } 54 | 55 | public void testSignal() throws URISyntaxException { 56 | File record = getWFDBFile("/chf03.dat"); 57 | DetectQRSOptions options = new DetectQRSOptions(record); 58 | options.setSignal(1); 59 | 60 | List command = options.getCommand(); 61 | assertEquals(5, command.size()); 62 | assertTrue(command.contains("-s")); 63 | int sIndex = command.indexOf("-s"); 64 | assertTrue(sIndex > command.indexOf("sqrs")); 65 | assertTrue(sIndex + 1 == command.indexOf("1")); 66 | } 67 | 68 | public void testSqrs() throws URISyntaxException { 69 | File record = getWFDBFile("/chf03.dat"); 70 | DetectQRSOptions options = new DetectQRSOptions(record); 71 | options.setCommand("sqrs"); 72 | 73 | List command = options.getCommand(); 74 | assertEquals("sqrs", command.get(0)); 75 | } 76 | 77 | public void testSqrs125() throws URISyntaxException { 78 | File record = getWFDBFile("/chf03.dat"); 79 | DetectQRSOptions options = new DetectQRSOptions(record); 80 | options.setCommand("sqrs125"); 81 | 82 | List command = options.getCommand(); 83 | assertEquals("sqrs125", command.get(0)); 84 | } 85 | 86 | public void testStartTime() throws URISyntaxException { 87 | File record = getWFDBFile("/chf03.dat"); 88 | DetectQRSOptions options = new DetectQRSOptions(record); 89 | options.setStartTime(new LocalTime(1, 2, 3)); 90 | 91 | List command = options.getCommand(); 92 | assertEquals(5, command.size()); 93 | assertTrue(command.contains("-f")); 94 | int fIndex = command.indexOf("-f"); 95 | assertTrue(fIndex > command.indexOf("sqrs")); 96 | assertTrue(fIndex + 1 == command.indexOf("01:02:03")); 97 | } 98 | 99 | public void testTreshold() throws URISyntaxException { 100 | File record = getWFDBFile("/chf03.dat"); 101 | DetectQRSOptions options = new DetectQRSOptions(record); 102 | options.setTreshold(123); 103 | 104 | List command = options.getCommand(); 105 | assertEquals(5, command.size()); 106 | assertTrue(command.contains("-m")); 107 | int mIndex = command.indexOf("-m"); 108 | assertTrue(mIndex > command.indexOf("sqrs")); 109 | assertTrue(mIndex + 1 == command.indexOf("123")); 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/test/java/at/jku/pervasive/ecg/wfdb/HrvTest.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.net.URISyntaxException; 6 | 7 | import org.joda.time.LocalTime; 8 | 9 | /** 10 | * Test assertions taken from http://www.physionet.org/tutorials/hrv-toolkit/ 11 | */ 12 | public class HrvTest extends PhysioToolkitTestCase { 13 | 14 | private static final double DELTA = 0.00000D; 15 | 16 | public void testBaseDirectoryForRecord() throws Exception { 17 | File chf03 = getTestFile(); 18 | HRVOptions options = new HRVOptions("chf03", "ecg"); 19 | options.setBaseDirectory(chf03.getParentFile()); 20 | HRV hrv = physioToolkit.hrv(options); 21 | 22 | assertNotNull(hrv); 23 | assertEquals(0.892769, hrv.getAVNN(), DELTA); 24 | assertEquals(0.0612485, hrv.getSDNN(), DELTA); 25 | } 26 | 27 | public void testLargeFile() throws Exception { 28 | File chf03 = getTestFile(); 29 | HRV hrv = physioToolkit.hrv(chf03, "ecg"); 30 | 31 | assertNotNull(hrv); 32 | assertEquals(0.892769, hrv.getAVNN(), DELTA); 33 | assertEquals(0.0612485, hrv.getSDNN(), DELTA); 34 | } 35 | 36 | public void testLargeFileHRVOptions() throws Exception { 37 | File chf03 = getTestFile(); 38 | HRVOptions options = new HRVOptions(chf03, "ecg"); 39 | HRV hrv = physioToolkit.hrv(options); 40 | 41 | assertNotNull(hrv); 42 | assertEquals(0.892769, hrv.getAVNN(), DELTA); 43 | assertEquals(0.0612485, hrv.getSDNN(), DELTA); 44 | } 45 | 46 | public void testLargeFileHRVOptionsWithOutlierDetection() throws Exception { 47 | File chf03 = getTestFile(); 48 | HRVOptions options = new HRVOptions(chf03, "ecg"); 49 | options.setFilter("0.2 20 -x 0.4 2.0"); 50 | options.setNnDiff("20 50"); 51 | HRV hrv = physioToolkit.hrv(options); 52 | 53 | assertNotNull(hrv); 54 | assertEquals(0.942899, hrv.getNNRR(), DELTA); 55 | assertEquals(0.054712, hrv.getSDNN(), DELTA); 56 | assertEquals(0.054712, hrv.getSDNN(), DELTA); 57 | assertEquals(0.0466773, hrv.getSDANN(), DELTA); 58 | assertEquals(0.0241124, hrv.getSDNNIDX(), DELTA); 59 | assertEquals(0.0177694, hrv.getrMSSD(), DELTA); 60 | assertEquals(0.0688698, hrv.getpNN20(), DELTA); 61 | assertEquals(0.0252389, hrv.getpNN50(), DELTA); 62 | assertEquals(0.0033882, hrv.getTOT_PWR(), DELTA); 63 | assertEquals(0.00270489, hrv.getULF_PWR(), DELTA); 64 | assertEquals(0.000124104, hrv.getLF_PWR(), DELTA); 65 | assertEquals(0.734226, hrv.getLFHF(), DELTA); 66 | } 67 | 68 | public void testLargeFileWithHRVOptionsAndOutlierDetectionAndStartAndEndTime() 69 | throws Exception { 70 | File chf03 = getTestFile(); 71 | HRVOptions options = new HRVOptions(chf03, "ecg"); 72 | options.setOutputMsec(true).setShortTermStats(true); 73 | options.setStart(0, 0).setEnd(new LocalTime(1, 0)); 74 | options.setFilter("0.2 20 -x 0.4 2.0"); 75 | options.setNnDiff("20 50"); 76 | HRV hrv = physioToolkit.hrv(options); 77 | 78 | assertNotNull(hrv); 79 | assertEquals(0.90173, hrv.getNNRR(), DELTA); 80 | assertEquals(867.958, hrv.getAVNN(), DELTA); 81 | assertEquals(39.7191, hrv.getSDNN(), DELTA); 82 | assertEquals(20.8659, hrv.getrMSSD(), DELTA); 83 | assertEquals(6.26553, hrv.getpNN20(), DELTA); 84 | assertEquals(2.20811, hrv.getpNN50(), DELTA); 85 | assertEquals(1702.96, hrv.getTOT_PWR(), DELTA); 86 | assertEquals(1423.01, hrv.getVLF_PWR(), DELTA); 87 | assertEquals(103.941, hrv.getLF_PWR(), DELTA); 88 | assertEquals(176.008, hrv.getHF_PWR(), DELTA); 89 | assertEquals(0.590547, hrv.getLFHF(), DELTA); 90 | } 91 | 92 | public void testLargeFileWithOutlierDetection() throws Exception { 93 | File chf03 = getTestFile(); 94 | HRV hrv = physioToolkit.hrv(chf03, "ecg", "-f 0.2 20 -x 0.4 2.0", 95 | "-p 20 50"); 96 | 97 | assertNotNull(hrv); 98 | assertEquals(0.942899, hrv.getNNRR(), DELTA); 99 | assertEquals(0.054712, hrv.getSDNN(), DELTA); 100 | assertEquals(0.054712, hrv.getSDNN(), DELTA); 101 | assertEquals(0.0466773, hrv.getSDANN(), DELTA); 102 | assertEquals(0.0241124, hrv.getSDNNIDX(), DELTA); 103 | assertEquals(0.0177694, hrv.getrMSSD(), DELTA); 104 | assertEquals(0.0688698, hrv.getpNN20(), DELTA); 105 | assertEquals(0.0252389, hrv.getpNN50(), DELTA); 106 | assertEquals(0.0033882, hrv.getTOT_PWR(), DELTA); 107 | assertEquals(0.00270489, hrv.getULF_PWR(), DELTA); 108 | assertEquals(0.000124104, hrv.getLF_PWR(), DELTA); 109 | assertEquals(0.734226, hrv.getLFHF(), DELTA); 110 | } 111 | 112 | public void testLargeFileWithOutlierDetectionAndStartAndEndTime() 113 | throws Exception { 114 | File chf03 = getTestFile(); 115 | HRV hrv = physioToolkit.hrv(chf03, "ecg", new LocalTime(0, 0), 116 | new LocalTime(1, 0), "-s", "-M", "-f 0.2 20 -x 0.4 2.0", "-p 20 50"); 117 | 118 | assertNotNull(hrv); 119 | assertEquals(0.90173, hrv.getNNRR(), DELTA); 120 | assertEquals(867.958, hrv.getAVNN(), DELTA); 121 | assertEquals(39.7191, hrv.getSDNN(), DELTA); 122 | assertEquals(20.8659, hrv.getrMSSD(), DELTA); 123 | assertEquals(6.26553, hrv.getpNN20(), DELTA); 124 | assertEquals(2.20811, hrv.getpNN50(), DELTA); 125 | assertEquals(1702.96, hrv.getTOT_PWR(), DELTA); 126 | assertEquals(1423.01, hrv.getVLF_PWR(), DELTA); 127 | assertEquals(103.941, hrv.getLF_PWR(), DELTA); 128 | assertEquals(176.008, hrv.getHF_PWR(), DELTA); 129 | assertEquals(0.590547, hrv.getLFHF(), DELTA); 130 | } 131 | 132 | protected File getTestFile() throws URISyntaxException, IOException { 133 | copyFileToTemp(getFile("/chf03.ecg")); 134 | copyFileToTemp(getFile("/chf03.dat")); 135 | copyFileToTemp(getFile("/chf03.hea")); 136 | return new File(tmpDir, "chf03"); 137 | } 138 | 139 | } 140 | -------------------------------------------------------------------------------- /src/main/java/at/jku/pervasive/ecg/wfdb/PhysioToolkit.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.util.ArrayList; 8 | import java.util.Arrays; 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | import org.apache.commons.io.IOUtils; 13 | import org.joda.time.LocalTime; 14 | 15 | public class PhysioToolkit { 16 | 17 | public void detectQRS(DetectQRSOptions options) throws IOException, 18 | InterruptedException { 19 | ProcessBuilder pb = createProcessBuilder(options.getCommand()); 20 | pb.redirectErrorStream(true); 21 | 22 | File baseDirectory = options.getBaseDirectory(); 23 | if (baseDirectory != null && baseDirectory.exists()) { 24 | pb.directory(baseDirectory); 25 | } 26 | 27 | Process process = pb.start(); 28 | InputStream in = process.getInputStream(); 29 | System.out.println(IOUtils.toString(in)); 30 | } 31 | 32 | public String edf2mit(Edf2MitOptions options) throws IOException, 33 | InterruptedException { 34 | List command = options.getCommand(); 35 | ProcessBuilder pb = createProcessBuilder(command); 36 | File baseDirectory = options.getBaseDirectory(); 37 | if (baseDirectory != null && baseDirectory.exists()) { 38 | pb.directory(baseDirectory); 39 | } 40 | pb.redirectErrorStream(true); 41 | Process process = pb.start(); 42 | process.waitFor(); 43 | InputStream in = process.getInputStream(); 44 | System.out.println(IOUtils.toString(in)); 45 | 46 | return options.getOutputName(); 47 | } 48 | 49 | public HRV hrv(File edf, String annotator, LocalTime startTime, 50 | LocalTime endTime, String... params) throws Exception { 51 | 52 | List cmd = new ArrayList(); 53 | cmd.add("get_hrv"); 54 | if (params != null && params.length > 0) { 55 | cmd.addAll(Arrays.asList(params)); 56 | } 57 | cmd.add(edf.getName()); 58 | cmd.add(annotator); 59 | 60 | if (startTime != null) { 61 | cmd.add(startTime.toString("HH:mm:ss")); 62 | } 63 | if (endTime != null) { 64 | cmd.add(endTime.toString("HH:mm:ss")); 65 | } 66 | 67 | ProcessBuilder pb = createProcessBuilder(cmd); 68 | pb.redirectErrorStream(true); 69 | pb.directory(edf.getParentFile()); 70 | Process start = pb.start(); 71 | start.waitFor(); 72 | InputStream in = start.getInputStream(); 73 | HRV hrvResult = new HRV(IOUtils.toString(in)); 74 | IOUtils.closeQuietly(in); 75 | return hrvResult; 76 | } 77 | 78 | public HRV hrv(File edf, String annotator, String... params) throws Exception { 79 | return hrv(edf, annotator, null, null, params); 80 | } 81 | 82 | public HRV hrv(HRVOptions options) throws Exception { 83 | ProcessBuilder pb = createProcessBuilder(options.getCommand()); 84 | pb.redirectErrorStream(true); 85 | File baseDirectory = options.getBaseDirectory(); 86 | if (baseDirectory != null && baseDirectory.exists()) { 87 | pb.directory(baseDirectory); 88 | } 89 | Process start = pb.start(); 90 | start.waitFor(); 91 | InputStream in = start.getInputStream(); 92 | HRV hrvResult = new HRV(IOUtils.toString(in)); 93 | IOUtils.closeQuietly(in); 94 | return hrvResult; 95 | } 96 | 97 | public boolean isInstalled() throws IOException, InterruptedException { 98 | /** 99 | * Implementation: check if every needed command is available. This is done 100 | * by executing the hash command and looking at the exit code. The exit code 101 | * 0 indicates the command is installed correctly. Assuming every needed 102 | * command is available, he exit code sum up to 0. 103 | */ 104 | int status = 0; 105 | status += execute("hash", "edf2mit"); 106 | status += execute("hash", "get_hrv"); 107 | status += execute("hash", "rdsamp"); 108 | status += execute("hash", "sqrs"); 109 | return (status == 0); 110 | } 111 | 112 | public void rdsamp(File edf) throws IOException, InterruptedException { 113 | ProcessBuilder pb = createProcessBuilder("rdsamp", edf.getName()); 114 | pb.directory(edf.getParentFile()); 115 | Process process = pb.start(); 116 | process.waitFor(); 117 | } 118 | 119 | public File rdsamp(RdsampOptions options) throws IOException, 120 | InterruptedException { 121 | ProcessBuilder pb = createProcessBuilder(options.getCommand()); 122 | File baseDirectory = options.getBaseDirectory(); 123 | pb.directory(baseDirectory); 124 | File outputFile = new File(baseDirectory, options.getOutputName()); 125 | Process process = pb.start(); 126 | InputStream in = process.getInputStream(); 127 | IOUtils.copy(in, new FileOutputStream(outputFile)); 128 | process.waitFor(); 129 | return outputFile; 130 | } 131 | 132 | public void sqrs(File testFile) throws IOException, InterruptedException { 133 | ProcessBuilder pb = createProcessBuilder("sqrs", "-r", testFile.getName()); 134 | pb.redirectErrorStream(true); 135 | pb.directory(testFile.getParentFile()); 136 | Process process = pb.start(); 137 | process.waitFor(); 138 | InputStream in = process.getInputStream(); 139 | System.out.println(IOUtils.toString(in)); 140 | } 141 | 142 | public void testLocale() throws Exception { 143 | ProcessBuilder pb = createProcessBuilder("locale"); 144 | Process process = pb.start(); 145 | process.waitFor(); 146 | InputStream in = process.getInputStream(); 147 | 148 | System.out.println(IOUtils.toString(in)); 149 | } 150 | 151 | protected ProcessBuilder createProcessBuilder(List command) { 152 | ProcessBuilder pb = new ProcessBuilder(command); 153 | setLocale(pb); 154 | return pb; 155 | } 156 | 157 | protected ProcessBuilder createProcessBuilder(String... command) { 158 | return createProcessBuilder(Arrays.asList(command)); 159 | } 160 | 161 | protected int execute(String... command) throws IOException, 162 | InterruptedException { 163 | ProcessBuilder pb = createProcessBuilder(command); 164 | Process process = pb.start(); 165 | return process.waitFor(); 166 | } 167 | 168 | private void setLocale(Map env) { 169 | String lang = "en_US.UTF-8"; 170 | env.put("LANG", lang); 171 | env.put("LC_COLLATE", lang); 172 | env.put("LC_CTYPE", lang); 173 | env.put("LC_MESSAGES", lang); 174 | env.put("LC_MONETARY", lang); 175 | env.put("LC_NUMERIC", lang); 176 | env.put("LC_TIME", lang); 177 | // env.put("LC_ALL", lang); 178 | } 179 | 180 | private void setLocale(ProcessBuilder pb) { 181 | this.setLocale(pb.environment()); 182 | } 183 | 184 | } 185 | -------------------------------------------------------------------------------- /src/main/java/at/jku/pervasive/ecg/wfdb/HRVOptions.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | import org.apache.commons.lang3.StringUtils; 8 | import org.joda.time.LocalTime; 9 | 10 | public class HRVOptions { 11 | 12 | private File baseDirectory; 13 | private String file, annotation; 14 | private double filt, hwin; 15 | private String filter; 16 | private String inputTimeFormat; 17 | private boolean isRR; 18 | private double lo1, hi1, lo2, hi2, lo3, hi3, lo4, hi4; 19 | private String nnDiff; 20 | private boolean outputMsec; 21 | private boolean rrInMSec; 22 | private boolean shortTermStats; 23 | private LocalTime start, end; 24 | 25 | public HRVOptions(File rrFile) { 26 | super(); 27 | if (rrFile != null) { 28 | this.file = rrFile.getName(); 29 | this.baseDirectory = rrFile.getParentFile(); 30 | this.isRR = true; 31 | } 32 | } 33 | 34 | public HRVOptions(File recordFile, String annotation) { 35 | super(); 36 | if (recordFile != null) { 37 | this.file = recordFile.getName(); 38 | this.baseDirectory = recordFile.getParentFile(); 39 | } 40 | this.annotation = annotation; 41 | } 42 | 43 | public HRVOptions(String file) { 44 | super(); 45 | this.file = file; 46 | this.isRR = true; 47 | } 48 | 49 | public HRVOptions(String file, String annotation) { 50 | super(); 51 | this.file = file; 52 | this.annotation = annotation; 53 | } 54 | 55 | public String getAnnotation() { 56 | return annotation; 57 | } 58 | 59 | public File getBaseDirectory() { 60 | return baseDirectory; 61 | } 62 | 63 | public List getCommand() { 64 | List cmd = new ArrayList(); 65 | cmd.add("get_hrv"); 66 | if (outputMsec) { 67 | cmd.add("-M"); 68 | } 69 | if (shortTermStats) { 70 | cmd.add("-s"); 71 | } 72 | if (StringUtils.isNotBlank(filter)) { 73 | cmd.add("-f " + filter); 74 | } 75 | if (StringUtils.isNotBlank(nnDiff)) { 76 | cmd.add("-p " + nnDiff); 77 | } 78 | if (isRR) { 79 | if (rrInMSec) { 80 | cmd.add("-m"); 81 | } 82 | cmd.add("-R"); 83 | cmd.add(file); 84 | } else { 85 | cmd.add(file); 86 | cmd.add(annotation); 87 | } 88 | if (start != null) { 89 | cmd.add(start.toString("HH:mm:ss")); 90 | } 91 | if (end != null) { 92 | cmd.add(end.toString("HH:mm:ss")); 93 | } 94 | return cmd; 95 | } 96 | 97 | public LocalTime getEnd() { 98 | return end; 99 | } 100 | 101 | public String getFile() { 102 | return file; 103 | } 104 | 105 | public double getFilt() { 106 | return filt; 107 | } 108 | 109 | public double getHi1() { 110 | return hi1; 111 | } 112 | 113 | public double getHi2() { 114 | return hi2; 115 | } 116 | 117 | public double getHi3() { 118 | return hi3; 119 | } 120 | 121 | public double getHi4() { 122 | return hi4; 123 | } 124 | 125 | public double getHwin() { 126 | return hwin; 127 | } 128 | 129 | public String getInputTimeFormat() { 130 | return inputTimeFormat; 131 | } 132 | 133 | public double getLo1() { 134 | return lo1; 135 | } 136 | 137 | public double getLo2() { 138 | return lo2; 139 | } 140 | 141 | public double getLo3() { 142 | return lo3; 143 | } 144 | 145 | public double getLo4() { 146 | return lo4; 147 | } 148 | 149 | public String getNnDiff() { 150 | return nnDiff; 151 | } 152 | 153 | public LocalTime getStart() { 154 | return start; 155 | } 156 | 157 | public boolean isOutputMsec() { 158 | return outputMsec; 159 | } 160 | 161 | public boolean isRrInMSec() { 162 | return rrInMSec; 163 | } 164 | 165 | public boolean isShortTermStats() { 166 | return shortTermStats; 167 | } 168 | 169 | public HRVOptions setAnnotation(String annotation) { 170 | this.annotation = annotation; 171 | return this; 172 | } 173 | 174 | public HRVOptions setBaseDirectory(File baseDirectory) { 175 | this.baseDirectory = baseDirectory; 176 | return this; 177 | } 178 | 179 | public HRVOptions setEnd(int m, int s) { 180 | setEnd(new LocalTime(0, m, s)); 181 | return this; 182 | } 183 | 184 | public HRVOptions setEnd(LocalTime end) { 185 | this.end = end; 186 | if (this.start == null) { 187 | this.start = new LocalTime(0, 0, 0); 188 | } 189 | return this; 190 | } 191 | 192 | public HRVOptions setFile(String file) { 193 | this.file = file; 194 | return this; 195 | } 196 | 197 | public HRVOptions setFilt(double filt) { 198 | this.filt = filt; 199 | return this; 200 | } 201 | 202 | public HRVOptions setFilter(String filter) { 203 | this.filter = filter; 204 | return this; 205 | } 206 | 207 | public HRVOptions setHi1(double hi1) { 208 | this.hi1 = hi1; 209 | return this; 210 | } 211 | 212 | public HRVOptions setHi2(double hi2) { 213 | this.hi2 = hi2; 214 | return this; 215 | } 216 | 217 | public HRVOptions setHi3(double hi3) { 218 | this.hi3 = hi3; 219 | return this; 220 | } 221 | 222 | public HRVOptions setHi4(double hi4) { 223 | this.hi4 = hi4; 224 | return this; 225 | } 226 | 227 | public HRVOptions setHwin(double hwin) { 228 | this.hwin = hwin; 229 | return this; 230 | } 231 | 232 | public HRVOptions setInputTimeFormat(String inputTimeFormat) { 233 | this.inputTimeFormat = inputTimeFormat; 234 | return this; 235 | } 236 | 237 | public HRVOptions setLo1(double lo1) { 238 | this.lo1 = lo1; 239 | return this; 240 | } 241 | 242 | public HRVOptions setLo2(double lo2) { 243 | this.lo2 = lo2; 244 | return this; 245 | } 246 | 247 | public HRVOptions setLo3(double lo3) { 248 | this.lo3 = lo3; 249 | return this; 250 | } 251 | 252 | public HRVOptions setLo4(double lo4) { 253 | this.lo4 = lo4; 254 | return this; 255 | } 256 | 257 | public HRVOptions setNnDiff(String nnDiff) { 258 | this.nnDiff = nnDiff; 259 | return this; 260 | } 261 | 262 | public HRVOptions setOutputMsec(boolean outputMsec) { 263 | this.outputMsec = outputMsec; 264 | return this; 265 | } 266 | 267 | public HRVOptions setRrInMSec(boolean rrInMSec) { 268 | this.rrInMSec = rrInMSec; 269 | return this; 270 | } 271 | 272 | public HRVOptions setShortTermStats(boolean shortTermStats) { 273 | this.shortTermStats = shortTermStats; 274 | return this; 275 | } 276 | 277 | public HRVOptions setStart(int m, int s) { 278 | return setStart(new LocalTime(0, m, s)); 279 | } 280 | 281 | public HRVOptions setStart(LocalTime start) { 282 | this.start = start; 283 | return this; 284 | } 285 | 286 | public HRVOptions startTime(LocalTime start) { 287 | this.start = start; 288 | return this; 289 | } 290 | 291 | } 292 | -------------------------------------------------------------------------------- /src/test/java/at/jku/pervasive/ecg/wfdb/HRVOptionsTest.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.net.URISyntaxException; 5 | import java.util.List; 6 | 7 | import org.joda.time.LocalTime; 8 | 9 | public class HRVOptionsTest extends PhysioToolkitTestCase { 10 | 11 | public void testBaseDirectory() throws URISyntaxException { 12 | File chf03dat = getFile("/chf03.dat"); 13 | HRVOptions opts = new HRVOptions("chf03", "ecg"); 14 | opts.setBaseDirectory(chf03dat.getParentFile()); 15 | 16 | List cmd = opts.getCommand(); 17 | assertEquals(3, cmd.size()); 18 | assertEquals("chf03", cmd.get(1)); 19 | assertEquals("ecg", cmd.get(2)); 20 | } 21 | 22 | public void testBaseDirectoryForRecord() throws Exception { 23 | File chf03dat = getFile("/chf03.dat"); 24 | File base = chf03dat.getParentFile(); 25 | File chf03 = new File(base, "chf03"); 26 | HRVOptions opts = new HRVOptions(chf03, "ecg"); 27 | 28 | List cmd = opts.getCommand(); 29 | assertEquals(3, cmd.size()); 30 | assertEquals(base, opts.getBaseDirectory()); 31 | assertEquals("chf03", cmd.get(1)); 32 | assertEquals("ecg", cmd.get(2)); 33 | } 34 | 35 | public void testBaseDirectoryForRR() throws Exception { 36 | File chf03dat = getFile("/chf03.dat"); 37 | File base = chf03dat.getParentFile(); 38 | File chf03rr = new File(base, "chf03.rr"); 39 | HRVOptions opts = new HRVOptions(chf03rr); 40 | 41 | List cmd = opts.getCommand(); 42 | assertEquals(3, cmd.size()); 43 | assertEquals(base, opts.getBaseDirectory()); 44 | assertEquals("-R", cmd.get(1)); 45 | assertEquals("chf03.rr", cmd.get(2)); 46 | } 47 | 48 | public void testEndTime() { 49 | HRVOptions opts = new HRVOptions("/rrfile.rr"); 50 | opts.setStart(5, 42); 51 | opts.setEnd(10, 11); 52 | 53 | List cmd = opts.getCommand(); 54 | assertEquals(5, cmd.size()); 55 | assertEquals("00:05:42", cmd.get(3)); 56 | assertEquals("00:10:11", cmd.get(4)); 57 | } 58 | 59 | public void testEndTimeWithoutStartTime() { 60 | HRVOptions opts = new HRVOptions("/rrfile.rr"); 61 | opts.setEnd(10, 11); 62 | 63 | List cmd = opts.getCommand(); 64 | assertEquals(5, cmd.size()); 65 | assertEquals("00:00:00", cmd.get(3)); 66 | assertEquals("00:10:11", cmd.get(4)); 67 | } 68 | 69 | public void testFilter() { 70 | HRVOptions options = new HRVOptions("rrfile.rr"); 71 | options.setFilter("0.2 20 -x 0.4 2.0"); 72 | 73 | List command = options.getCommand(); 74 | assertTrue(command.contains("-f 0.2 20 -x 0.4 2.0")); 75 | } 76 | 77 | public void testInvalidAnnotation() throws URISyntaxException { 78 | File testFile = getWFDBFile("/chf03.dat"); 79 | try { 80 | new HRVOptions(testFile, ""); 81 | } catch (Exception e) { 82 | fail("should not throw an exception"); 83 | } 84 | } 85 | 86 | public void testInvalidFile() throws Exception { 87 | File temporaryFile = File.createTempFile("tmpFile", "tmp"); 88 | try { 89 | new HRVOptions(temporaryFile, ""); 90 | } catch (Exception e) { 91 | fail("should not throw an exception"); 92 | } finally { 93 | temporaryFile.delete(); 94 | } 95 | } 96 | 97 | public void testMsec() { 98 | HRVOptions options = new HRVOptions("rrfile.rr"); 99 | options.setOutputMsec(true); 100 | options.setEnd(10, 0); 101 | 102 | List cmd = options.getCommand(); 103 | assertEquals(6, cmd.size()); 104 | assertEquals("-M", cmd.get(1)); 105 | assertEquals("-R", cmd.get(2)); 106 | assertEquals("rrfile.rr", cmd.get(3)); 107 | assertEquals("00:00:00", cmd.get(4)); 108 | assertEquals("00:10:00", cmd.get(5)); 109 | } 110 | 111 | public void testNnDiff() { 112 | HRVOptions options = new HRVOptions("rrfile.rr"); 113 | options.setNnDiff("20 50"); 114 | 115 | List command = options.getCommand(); 116 | assertTrue(command.contains("-p 20 50")); 117 | } 118 | 119 | public void testNullAnnotation() throws URISyntaxException { 120 | File testFile = getWFDBFile("/chf03.dat"); 121 | try { 122 | new HRVOptions(testFile, null); 123 | } catch (Exception e) { 124 | fail("should not throw an exception"); 125 | } 126 | } 127 | 128 | public void testNullFile() throws Exception { 129 | try { 130 | new HRVOptions((File) null, ""); 131 | } catch (Exception e) { 132 | fail("should not throw an exception"); 133 | } 134 | } 135 | 136 | public void testNullFileString() throws Exception { 137 | try { 138 | new HRVOptions((String) null, ""); 139 | } catch (Exception e) { 140 | fail("should not throw an exception"); 141 | } 142 | } 143 | 144 | public void testRecordFile() throws Exception { 145 | HRVOptions options = new HRVOptions("testFile", "qrs"); 146 | 147 | List cmd = options.getCommand(); 148 | assertEquals(3, cmd.size()); 149 | assertEquals("testFile", cmd.get(1)); 150 | assertEquals("qrs", cmd.get(2)); 151 | } 152 | 153 | public void testRecordFileWithStartAndEndTime() throws Exception { 154 | HRVOptions options = new HRVOptions("testFile", "qrs"); 155 | options.setStart(10, 11); 156 | options.setEnd(new LocalTime(1, 10, 11)); 157 | 158 | List cmd = options.getCommand(); 159 | assertEquals(5, cmd.size()); 160 | assertEquals("testFile", cmd.get(1)); 161 | assertEquals("qrs", cmd.get(2)); 162 | assertEquals("00:10:11", cmd.get(3)); 163 | assertEquals("01:10:11", cmd.get(4)); 164 | } 165 | 166 | public void testRRFile() { 167 | HRVOptions opts = new HRVOptions("rrfile.rr"); 168 | 169 | List cmd = opts.getCommand(); 170 | assertEquals(3, cmd.size()); 171 | assertEquals("-R", cmd.get(1)); 172 | assertEquals("rrfile.rr", cmd.get(2)); 173 | } 174 | 175 | public void testRRIntervalsInMsec() { 176 | HRVOptions options = new HRVOptions("rrfile.rr"); 177 | options.setRrInMSec(true); 178 | options.setEnd(10, 0); 179 | 180 | List cmd = options.getCommand(); 181 | assertEquals(6, cmd.size()); 182 | assertEquals("-m", cmd.get(1)); 183 | assertEquals("-R", cmd.get(2)); 184 | assertEquals("rrfile.rr", cmd.get(3)); 185 | assertEquals("00:00:00", cmd.get(4)); 186 | assertEquals("00:10:00", cmd.get(5)); 187 | } 188 | 189 | public void testRRIntervalsInMsecWhenNoRRFile() { 190 | HRVOptions options = new HRVOptions("myfile", "qrs"); 191 | options.setRrInMSec(true); 192 | 193 | List cmd = options.getCommand(); 194 | assertEquals(3, cmd.size()); 195 | assertEquals("myfile", cmd.get(1)); 196 | assertEquals("qrs", cmd.get(2)); 197 | } 198 | 199 | public void testShortTerm() { 200 | HRVOptions opts = new HRVOptions("/rrfile.rr"); 201 | opts.setShortTermStats(true); 202 | 203 | List command = opts.getCommand(); 204 | assertTrue(command.contains("-s")); 205 | } 206 | 207 | public void testStartTime() { 208 | HRVOptions opts = new HRVOptions("/rrfile.rr"); 209 | opts.setStart(10, 11); 210 | 211 | List cmd = opts.getCommand(); 212 | assertEquals(4, cmd.size()); 213 | assertEquals("00:10:11", cmd.get(3)); 214 | } 215 | 216 | } 217 | -------------------------------------------------------------------------------- /src/test/java/at/jku/pervasive/ecg/wfdb/RdsampOptionsTest.java: -------------------------------------------------------------------------------- 1 | package at.jku.pervasive.ecg.wfdb; 2 | 3 | import java.io.File; 4 | import java.net.URISyntaxException; 5 | import java.util.List; 6 | 7 | import org.apache.commons.lang3.StringUtils; 8 | import org.joda.time.LocalTime; 9 | 10 | public class RdsampOptionsTest extends PhysioToolkitTestCase { 11 | 12 | public void test() throws URISyntaxException { 13 | File record = getWFDBFile("/chf03.dat"); 14 | RdsampOptions options = new RdsampOptions(record); 15 | 16 | assertTrue(StringUtils.isNotBlank(options.getOutputName())); 17 | 18 | List cmd = options.getCommand(); 19 | assertNotNull(cmd); 20 | assertEquals(3, cmd.size()); 21 | assertEquals("rdsamp", cmd.get(0)); 22 | assertEquals("-r", cmd.get(1)); 23 | assertEquals(record.getName(), cmd.get(2)); 24 | } 25 | 26 | public void testCSV() throws URISyntaxException { 27 | File record = getWFDBFile("/chf03.dat"); 28 | RdsampOptions options = new RdsampOptions(record); 29 | options.setUseCSV(true); 30 | 31 | List cmd = options.getCommand(); 32 | assertTrue(cmd.contains("-c")); 33 | assertTrue(cmd.indexOf("-c") > cmd.indexOf("rdsamp")); 34 | } 35 | 36 | public void testEndTime() throws URISyntaxException { 37 | File record = getWFDBFile("/chf03.dat"); 38 | RdsampOptions options = new RdsampOptions(record); 39 | options.setEndTime(new LocalTime(1, 2, 3)); 40 | 41 | List cmd = options.getCommand(); 42 | assertTrue(cmd.contains("-t")); 43 | assertTrue(cmd.indexOf("-t") > cmd.indexOf("rdsamp")); 44 | assertTrue(cmd.contains("01:02:03")); 45 | int tIndex = cmd.indexOf("-t"); 46 | assertTrue(tIndex + 1 == cmd.indexOf("01:02:03")); 47 | } 48 | 49 | public void testHighResolutionMode() throws URISyntaxException { 50 | File record = getWFDBFile("/chf03.dat"); 51 | RdsampOptions options = new RdsampOptions(record); 52 | options.setHighResolutionMode(true); 53 | 54 | List cmd = options.getCommand(); 55 | assertTrue(cmd.contains("-H")); 56 | assertTrue(cmd.indexOf("-H") > cmd.indexOf("rdsamp")); 57 | } 58 | 59 | public void testNullPhysicalUnits() throws URISyntaxException { 60 | File record = getWFDBFile("/chf03.dat"); 61 | RdsampOptions options = new RdsampOptions(record); 62 | options.setPhysicalUnit(null); 63 | 64 | List cmd = options.getCommand(); 65 | for (PhysicalUnit pu : PhysicalUnit.values()) { 66 | assertFalse(cmd.contains(pu.getParameter())); 67 | } 68 | } 69 | 70 | public void testNullSignals() throws URISyntaxException { 71 | File record = getWFDBFile("/chf03.dat"); 72 | RdsampOptions options = new RdsampOptions(record); 73 | options.setSignals(null); 74 | 75 | List cmd = options.getCommand(); 76 | assertFalse(cmd.contains("-s")); 77 | } 78 | 79 | public void testPhysicalUnits() throws URISyntaxException { 80 | File record = getWFDBFile("/chf03.dat"); 81 | RdsampOptions options = new RdsampOptions(record); 82 | options.setPhysicalUnit(PhysicalUnit.p); 83 | 84 | List cmd = options.getCommand(); 85 | assertTrue(cmd.contains("-p")); 86 | assertTrue(cmd.indexOf("-p") > cmd.indexOf("rdsamp")); 87 | } 88 | 89 | public void testPhysicalUnitsElapsedHours() throws URISyntaxException { 90 | File record = getWFDBFile("/chf03.dat"); 91 | RdsampOptions options = new RdsampOptions(record); 92 | options.setPhysicalUnit(PhysicalUnit.ph); 93 | 94 | List cmd = options.getCommand(); 95 | assertTrue(cmd.contains("-ph")); 96 | assertTrue(cmd.indexOf("-ph") > cmd.indexOf("rdsamp")); 97 | } 98 | 99 | public void testPhysicalUnitsElapsedHoursMinutesSeconds() 100 | throws URISyntaxException { 101 | File record = getWFDBFile("/chf03.dat"); 102 | RdsampOptions options = new RdsampOptions(record); 103 | options.setPhysicalUnit(PhysicalUnit.pe); 104 | 105 | List cmd = options.getCommand(); 106 | assertTrue(cmd.contains("-pe")); 107 | assertTrue(cmd.indexOf("-pe") > cmd.indexOf("rdsamp")); 108 | } 109 | 110 | public void testPhysicalUnitsElapsedMinutes() throws URISyntaxException { 111 | File record = getWFDBFile("/chf03.dat"); 112 | RdsampOptions options = new RdsampOptions(record); 113 | options.setPhysicalUnit(PhysicalUnit.pm); 114 | 115 | List cmd = options.getCommand(); 116 | assertTrue(cmd.contains("-pm")); 117 | assertTrue(cmd.indexOf("-pm") > cmd.indexOf("rdsamp")); 118 | } 119 | 120 | public void testPhysicalUnitsElapsedSampleIntervals() 121 | throws URISyntaxException { 122 | File record = getWFDBFile("/chf03.dat"); 123 | RdsampOptions options = new RdsampOptions(record); 124 | options.setPhysicalUnit(PhysicalUnit.pS); 125 | 126 | List cmd = options.getCommand(); 127 | assertTrue(cmd.contains("-pS")); 128 | assertTrue(cmd.indexOf("-pS") > cmd.indexOf("rdsamp")); 129 | } 130 | 131 | public void testPhysicalUnitsElapsedSeconds() throws URISyntaxException { 132 | File record = getWFDBFile("/chf03.dat"); 133 | RdsampOptions options = new RdsampOptions(record); 134 | options.setPhysicalUnit(PhysicalUnit.ps); 135 | 136 | List cmd = options.getCommand(); 137 | assertTrue(cmd.contains("-ps")); 138 | assertTrue(cmd.indexOf("-ps") > cmd.indexOf("rdsamp")); 139 | } 140 | 141 | public void testPhysicalUnitsHigherPrecision() throws URISyntaxException { 142 | File record = getWFDBFile("/chf03.dat"); 143 | RdsampOptions options = new RdsampOptions(record); 144 | options.setPhysicalUnit(PhysicalUnit.P); 145 | 146 | List cmd = options.getCommand(); 147 | assertTrue(cmd.contains("-P")); 148 | assertTrue(cmd.indexOf("-P") > cmd.indexOf("rdsamp")); 149 | } 150 | 151 | public void testPhysicalUnitsHigherPrecisionElapsedHours() 152 | throws URISyntaxException { 153 | File record = getWFDBFile("/chf03.dat"); 154 | RdsampOptions options = new RdsampOptions(record); 155 | options.setPhysicalUnit(PhysicalUnit.Ph); 156 | 157 | List cmd = options.getCommand(); 158 | assertTrue(cmd.contains("-Ph")); 159 | assertTrue(cmd.indexOf("-Ph") > cmd.indexOf("rdsamp")); 160 | } 161 | 162 | public void testPhysicalUnitsHigherPrecisionElapsedHoursMinutesSeconds() 163 | throws URISyntaxException { 164 | File record = getWFDBFile("/chf03.dat"); 165 | RdsampOptions options = new RdsampOptions(record); 166 | options.setPhysicalUnit(PhysicalUnit.Pe); 167 | 168 | List cmd = options.getCommand(); 169 | assertTrue(cmd.contains("-Pe")); 170 | assertTrue(cmd.indexOf("-Pe") > cmd.indexOf("rdsamp")); 171 | } 172 | 173 | public void testPhysicalUnitsHigherPrecisionElapsedMinutes() 174 | throws URISyntaxException { 175 | File record = getWFDBFile("/chf03.dat"); 176 | RdsampOptions options = new RdsampOptions(record); 177 | options.setPhysicalUnit(PhysicalUnit.Pm); 178 | 179 | List cmd = options.getCommand(); 180 | assertTrue(cmd.contains("-Pm")); 181 | assertTrue(cmd.indexOf("-Pm") > cmd.indexOf("rdsamp")); 182 | } 183 | 184 | public void testPhysicalUnitsHigherPrecisionElapsedSampleIntervals() 185 | throws URISyntaxException { 186 | File record = getWFDBFile("/chf03.dat"); 187 | RdsampOptions options = new RdsampOptions(record); 188 | options.setPhysicalUnit(PhysicalUnit.PS); 189 | 190 | List cmd = options.getCommand(); 191 | assertTrue(cmd.contains("-PS")); 192 | assertTrue(cmd.indexOf("-PS") > cmd.indexOf("rdsamp")); 193 | } 194 | 195 | public void testPhysicalUnitsHigherPrecisionElapsedSeconds() 196 | throws URISyntaxException { 197 | File record = getWFDBFile("/chf03.dat"); 198 | RdsampOptions options = new RdsampOptions(record); 199 | options.setPhysicalUnit(PhysicalUnit.Ps); 200 | 201 | List cmd = options.getCommand(); 202 | assertTrue(cmd.contains("-Ps")); 203 | assertTrue(cmd.indexOf("-Ps") > cmd.indexOf("rdsamp")); 204 | } 205 | 206 | public void testPhysicalUnitsHigherPrecisionTimeOfDayAndDate() 207 | throws URISyntaxException { 208 | File record = getWFDBFile("/chf03.dat"); 209 | RdsampOptions options = new RdsampOptions(record); 210 | options.setPhysicalUnit(PhysicalUnit.Pd); 211 | 212 | List cmd = options.getCommand(); 213 | assertTrue(cmd.contains("-Pd")); 214 | assertTrue(cmd.indexOf("-Pd") > cmd.indexOf("rdsamp")); 215 | } 216 | 217 | public void testPhysicalUnitsTimeOfDayAndDate() throws URISyntaxException { 218 | File record = getWFDBFile("/chf03.dat"); 219 | RdsampOptions options = new RdsampOptions(record); 220 | options.setPhysicalUnit(PhysicalUnit.pd); 221 | 222 | List cmd = options.getCommand(); 223 | assertTrue(cmd.contains("-pd")); 224 | assertTrue(cmd.indexOf("-pd") > cmd.indexOf("rdsamp")); 225 | } 226 | 227 | public void testPrintColumnHeadings() throws URISyntaxException { 228 | File record = getWFDBFile("/chf03.dat"); 229 | RdsampOptions options = new RdsampOptions(record); 230 | options.setPrintColumHeadings(true); 231 | 232 | List cmd = options.getCommand(); 233 | assertTrue(cmd.contains("-v")); 234 | assertTrue(cmd.indexOf("-v") > cmd.indexOf("rdsamp")); 235 | } 236 | 237 | public void testSignals() throws URISyntaxException { 238 | File record = getWFDBFile("/chf03.dat"); 239 | RdsampOptions options = new RdsampOptions(record); 240 | options.setSignals(0, 1, 2); 241 | 242 | List cmd = options.getCommand(); 243 | assertTrue(cmd.contains("-s")); 244 | int sIndex = cmd.indexOf("-s"); 245 | assertEquals("0 1 2", cmd.get(sIndex + 1)); 246 | } 247 | 248 | public void testStartTime() throws URISyntaxException { 249 | File record = getWFDBFile("/chf03.dat"); 250 | RdsampOptions options = new RdsampOptions(record); 251 | options.setStartTime(new LocalTime(1, 2, 3)); 252 | 253 | List cmd = options.getCommand(); 254 | assertTrue(cmd.contains("-f")); 255 | assertTrue(cmd.indexOf("-f") > cmd.indexOf("rdsamp")); 256 | assertTrue(cmd.contains("01:02:03")); 257 | int fIndex = cmd.indexOf("-f"); 258 | assertTrue(fIndex + 1 == cmd.indexOf("01:02:03")); 259 | } 260 | 261 | } 262 | --------------------------------------------------------------------------------