From 43dbc7f7c3cffb24ca16af8697749bbeca28f413 Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Tue, 8 Oct 2024 13:23:53 +0200 Subject: [PATCH 1/5] Close test annotation files --- tests/test_annotation.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_annotation.py b/tests/test_annotation.py index e7d86b50..db3e71d0 100644 --- a/tests/test_annotation.py +++ b/tests/test_annotation.py @@ -33,7 +33,8 @@ def test_1(self): # no null to detect in the output text file of rdann. # Target data from WFDB software package - lines = tuple(open("tests/target-output/ann-1", "r")) + with open("tests/target-output/ann-1", "r") as f: + lines = tuple(f) nannot = len(lines) target_time = [None] * nannot @@ -108,7 +109,8 @@ def test_2(self): annotation = wfdb.rdann("sample-data/12726", "anI") # Target data from WFDB software package - lines = tuple(open("tests/target-output/ann-2", "r")) + with open("tests/target-output/ann-2", "r") as f: + lines = tuple(f) nannot = len(lines) target_time = [None] * nannot @@ -181,7 +183,8 @@ def test_3(self): annotation = wfdb.rdann("sample-data/1003", "atr") # Target data from WFDB software package - lines = tuple(open("tests/target-output/ann-3", "r")) + with open("tests/target-output/ann-3", "r") as f: + lines = tuple(f) nannot = len(lines) target_time = [None] * nannot From 3d51cecd014e31d026a2c024e873587722ebcacf Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Tue, 8 Oct 2024 13:24:49 +0200 Subject: [PATCH 2/5] Close multiprocessing pool --- wfdb/io/download.py | 6 ++---- wfdb/io/record.py | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/wfdb/io/download.py b/wfdb/io/download.py index d494ad0e..6c7e2694 100644 --- a/wfdb/io/download.py +++ b/wfdb/io/download.py @@ -541,8 +541,6 @@ def dl_files(db, dl_dir, files, keep_subdirs=True, overwrite=False): print("Downloading files...") # Create multiple processes to download files. # Limit to 2 connections to avoid overloading the server - pool = multiprocessing.dummy.Pool(processes=2) - pool.map(dl_pn_file, dl_inputs) + with multiprocessing.dummy.Pool(processes=2) as pool: + pool.map(dl_pn_file, dl_inputs) print("Finished downloading files") - - return diff --git a/wfdb/io/record.py b/wfdb/io/record.py index 09496396..4b900b17 100644 --- a/wfdb/io/record.py +++ b/wfdb/io/record.py @@ -3117,8 +3117,6 @@ def dl_database( print("Downloading files...") # Create multiple processes to download files. # Limit to 2 connections to avoid overloading the server - pool = multiprocessing.dummy.Pool(processes=2) - pool.map(download.dl_pn_file, dl_inputs) + with multiprocessing.dummy.Pool(processes=2) as pool: + pool.map(download.dl_pn_file, dl_inputs) print("Finished downloading files") - - return From 8857b7ed7c33e54f1f7a6665d35d033d93acfb4c Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Tue, 8 Oct 2024 13:25:38 +0200 Subject: [PATCH 3/5] Use np.frombuffer instead of np.fromstring --- wfdb/io/download.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wfdb/io/download.py b/wfdb/io/download.py index 6c7e2694..3109963e 100644 --- a/wfdb/io/download.py +++ b/wfdb/io/download.py @@ -143,7 +143,7 @@ def _stream_dat(file_name, pn_dir, byte_count, start_byte, dtype): content = f.read(byte_count) # Convert to numpy array - sig_data = np.fromstring(content, dtype=dtype) + sig_data = np.frombuffer(content, dtype=dtype) return sig_data @@ -173,7 +173,7 @@ def _stream_annotation(file_name, pn_dir): content = f.read() # Convert to numpy array - ann_data = np.fromstring(content, dtype=np.dtype(" Date: Tue, 8 Oct 2024 13:25:52 +0200 Subject: [PATCH 4/5] Use is in type comparison --- wfdb/io/download.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wfdb/io/download.py b/wfdb/io/download.py index 3109963e..338d8b97 100644 --- a/wfdb/io/download.py +++ b/wfdb/io/download.py @@ -343,7 +343,7 @@ def get_annotators(db_dir, annotators): annotators = ann_list else: # In case they didn't input a list - if type(annotators) == str: + if type(annotators) is str: annotators = [annotators] # user input ones. Check validity. for a in annotators: From 1ea701ebfac8c47f63144c1f8931c8b7b3476289 Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Tue, 8 Oct 2024 13:26:07 +0200 Subject: [PATCH 5/5] Close EDF file --- wfdb/io/convert/edf.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wfdb/io/convert/edf.py b/wfdb/io/convert/edf.py index e77cda59..e3096884 100644 --- a/wfdb/io/convert/edf.py +++ b/wfdb/io/convert/edf.py @@ -438,6 +438,8 @@ def read_edf( int(np.sum(v) % 65536) for v in np.transpose(sig_data) ] # not all values correct? + edf_file.close() + record = Record( record_name=record_name_out, n_sig=n_sig,