diff --git a/packs/ana/ana.py b/packs/ana/ana.py index fb49325..7babf1b 100644 --- a/packs/ana/ana.py +++ b/packs/ana/ana.py @@ -20,21 +20,22 @@ def ana(config_file : str) -> (np.ndarray): window_overlap_check(window_args) - if isinstance(conf_args["files"], list): - print("Averaging waveform....") - - avgwf = average_waveforms(**conf_args) - - checked_save_path = check_save_path( - save_path, - overwrite + if not isinstance(conf_args["files"], list): + raise TypeError( + f"'files' must be a list of file paths, got {type(conf_args['files']).__name__}" ) + print("Averaging waveform....") + + checked_save_path = check_save_path( + save_path, + overwrite + ) + + avgwf = average_waveforms(**conf_args) - with h5py.File(checked_save_path, 'w') as f: # Save as a h5 - f.create_dataset('Average_waveform', data=avgwf) + with h5py.File(checked_save_path, 'w') as f: # Save as a h5 + f.create_dataset('Average_waveform', data=avgwf) - print(f"Saved to: {checked_save_path}") + print(f"Saved to: {checked_save_path}") - else: - print("Please input files as a list.") return \ No newline at end of file diff --git a/packs/ana/analysis_utils.py b/packs/ana/analysis_utils.py index b7d1dd3..9663296 100644 --- a/packs/ana/analysis_utils.py +++ b/packs/ana/analysis_utils.py @@ -245,11 +245,11 @@ def window_overlap_check(window_args: dict): # Define intervals w_start, w_end = WINDOW_START, WINDOW_END - b1_start = BASELINE_POINT_1 - BASELINE_RANGE_1 / 2 - b1_end = BASELINE_POINT_1 + BASELINE_RANGE_1 / 2 + b1_start = BASELINE_POINT_1 - BASELINE_RANGE_1 + b1_end = BASELINE_POINT_1 + BASELINE_RANGE_1 - b2_start = BASELINE_POINT_2 - BASELINE_RANGE_2 / 2 - b2_end = BASELINE_POINT_2 + BASELINE_RANGE_2 / 2 + b2_start = BASELINE_POINT_2 - BASELINE_RANGE_2 + b2_end = BASELINE_POINT_2 + BASELINE_RANGE_2 # Check for negative values if (w_start < 0 or w_end < 0 or @@ -290,7 +290,7 @@ def window_wf_check(wf : np.ndarray, BASELINE_POINT_2 = window_args['BASELINE_POINT_2'] BASELINE_RANGE_1 = window_args['BASELINE_RANGE_1'] BASELINE_RANGE_2 = window_args['BASELINE_RANGE_2'] - if WINDOW_START>length or WINDOW_END>length or BASELINE_POINT_1+BASELINE_RANGE_1/2>length or BASELINE_POINT_2+BASELINE_RANGE_2>length: + if WINDOW_START>length or WINDOW_END>length or BASELINE_POINT_1+BASELINE_RANGE_1>length or BASELINE_POINT_2+BASELINE_RANGE_2>length: return False else: return True \ No newline at end of file diff --git a/packs/proc/processing_utils.py b/packs/proc/processing_utils.py index 5b41059..9cc48af 100644 --- a/packs/proc/processing_utils.py +++ b/packs/proc/processing_utils.py @@ -420,8 +420,9 @@ def check_save_path(save_path: str, ------ FileNotFoundError : If the directory of save_path does not exist ''' - if not os.path.exists(os.path.dirname(save_path)): - raise FileNotFoundError(2, 'Save path not found', os.path.dirname(save_path)) + dirname = os.path.dirname(os.path.abspath(save_path)) + if not os.path.exists(dirname): + raise FileNotFoundError(2, 'Save path not found', dirname) if overwrite: return save_path diff --git a/packs/tests/data/configs/ana_bad_baseline_mode.conf b/packs/tests/data/configs/ana_bad_baseline_mode.conf new file mode 100644 index 0000000..0b13cc8 --- /dev/null +++ b/packs/tests/data/configs/ana_bad_baseline_mode.conf @@ -0,0 +1,21 @@ +[required] + +files = ['run19.h5'] + +window_args = {'WINDOW_START' : 100, + 'WINDOW_END' : 3e4, + 'BASELINE_POINT_1' : 1e6, + 'BASELINE_POINT_2' : 1.5e6, + 'BASELINE_RANGE_1' : 40e3, + 'BASELINE_RANGE_2' : 40e3} + +bin_size = 4 +chunk_size = 5 +negative = True +baseline_mode = 'three hundred baselines please' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True +save_path = 'test.h5' \ No newline at end of file diff --git a/packs/tests/data/configs/ana_baseline_overlaps_window.conf b/packs/tests/data/configs/ana_baseline_overlaps_window.conf new file mode 100644 index 0000000..75ff162 --- /dev/null +++ b/packs/tests/data/configs/ana_baseline_overlaps_window.conf @@ -0,0 +1,21 @@ +[required] + +files = ['run19.h5'] + +window_args = {'WINDOW_START' : 4e2, + 'WINDOW_END' : 2e6, + 'BASELINE_POINT_1' : 1e6, + 'BASELINE_POINT_2' : 1.5e6, + 'BASELINE_RANGE_1' : 40e3, + 'BASELINE_RANGE_2' : 40e3} + +bin_size = 4 +chunk_size = 5 +negative = True +baseline_mode = 'median' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True +save_path = 'test.h5' \ No newline at end of file diff --git a/packs/tests/data/configs/ana_baselines_overlap.conf b/packs/tests/data/configs/ana_baselines_overlap.conf new file mode 100644 index 0000000..51f838d --- /dev/null +++ b/packs/tests/data/configs/ana_baselines_overlap.conf @@ -0,0 +1,21 @@ +[required] + +files = ['run19.h5'] + +window_args = {'WINDOW_START' : 100, + 'WINDOW_END' : 3e4, + 'BASELINE_POINT_1' : 1e6, + 'BASELINE_POINT_2' : 1.01e6, + 'BASELINE_RANGE_1' : 40e3, + 'BASELINE_RANGE_2' : 40e3} + +bin_size = 4 +chunk_size = 5 +negative = True +baseline_mode = 'median' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True +save_path = 'test.h5' \ No newline at end of file diff --git a/packs/tests/data/configs/ana_empty_files.conf b/packs/tests/data/configs/ana_empty_files.conf new file mode 100644 index 0000000..e221fce --- /dev/null +++ b/packs/tests/data/configs/ana_empty_files.conf @@ -0,0 +1,21 @@ +[required] + +files = [''] + +window_args = {'WINDOW_START' : 100, + 'WINDOW_END' : 3e4, + 'BASELINE_POINT_1' : 1e6, + 'BASELINE_POINT_2' : 1.5e6, + 'BASELINE_RANGE_1' : 40e3, + 'BASELINE_RANGE_2' : 40e3} + +bin_size = 4 +chunk_size = 5 +negative = True +baseline_mode = 'median' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True +save_path = 'test.h5' \ No newline at end of file diff --git a/packs/tests/data/configs/ana_files_not_list.conf b/packs/tests/data/configs/ana_files_not_list.conf new file mode 100644 index 0000000..60d3ba4 --- /dev/null +++ b/packs/tests/data/configs/ana_files_not_list.conf @@ -0,0 +1,21 @@ +[required] + +files = 'run19.h5' + +window_args = {'WINDOW_START' : 100, + 'WINDOW_END' : 3e4, + 'BASELINE_POINT_1' : 1e6, + 'BASELINE_POINT_2' : 1.5e6, + 'BASELINE_RANGE_1' : 40e3, + 'BASELINE_RANGE_2' : 40e3} + +bin_size = 4 +chunk_size = 5 +negative = True +baseline_mode = 'median' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True +save_path = 'test.h5' \ No newline at end of file diff --git a/packs/tests/data/configs/ana_inverted_window.conf b/packs/tests/data/configs/ana_inverted_window.conf new file mode 100644 index 0000000..a2c00e0 --- /dev/null +++ b/packs/tests/data/configs/ana_inverted_window.conf @@ -0,0 +1,21 @@ +[required] + +files = ['run19.h5'] + +window_args = {'WINDOW_START' : 3e4, + 'WINDOW_END' : 4e2, + 'BASELINE_POINT_1' : 1e6, + 'BASELINE_POINT_2' : 1.5e6, + 'BASELINE_RANGE_1' : 40e3, + 'BASELINE_RANGE_2' : 40e3} + +bin_size = 4 +chunk_size = 5 +negative = True +baseline_mode = 'median' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True +save_path = 'test.h5' \ No newline at end of file diff --git a/packs/tests/data/configs/ana_missing_bin_size.conf b/packs/tests/data/configs/ana_missing_bin_size.conf new file mode 100644 index 0000000..5c60469 --- /dev/null +++ b/packs/tests/data/configs/ana_missing_bin_size.conf @@ -0,0 +1,21 @@ +[required] + +files = ['run19.h5'] + +window_args = {'WINDOW_START' : 100, + 'WINDOW_END' : 3e4, + 'BASELINE_POINT_1' : 1e6, + 'BASELINE_POINT_2' : 1.5e6, + 'BASELINE_RANGE_1' : 40e3, + 'BASELINE_RANGE_2' : 40e3} + + +chunk_size = 5 +negative = True +baseline_mode = 'median' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True +save_path = 'test.h5' \ No newline at end of file diff --git a/packs/tests/data/configs/ana_missing_save_path.conf b/packs/tests/data/configs/ana_missing_save_path.conf new file mode 100644 index 0000000..7254d28 --- /dev/null +++ b/packs/tests/data/configs/ana_missing_save_path.conf @@ -0,0 +1,20 @@ +[required] + +files = ['run19.h5'] + +window_args = {'WINDOW_START' : 100, + 'WINDOW_END' : 3e4, + 'BASELINE_POINT_1' : 1e6, + 'BASELINE_POINT_2' : 1.5e6, + 'BASELINE_RANGE_1' : 40e3, + 'BASELINE_RANGE_2' : 40e3} + +bin_size = 4 +chunk_size = 5 +negative = True +baseline_mode = 'median' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True diff --git a/packs/tests/data/configs/ana_missing_window_args.conf b/packs/tests/data/configs/ana_missing_window_args.conf new file mode 100644 index 0000000..9521d13 --- /dev/null +++ b/packs/tests/data/configs/ana_missing_window_args.conf @@ -0,0 +1,16 @@ +[required] + +files = ['run19.h5'] + + + +bin_size = 4 +chunk_size = 5 +negative = True +baseline_mode = 'median' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True +save_path = 'test.h5' \ No newline at end of file diff --git a/packs/tests/data/configs/ana_missing_window_key.conf b/packs/tests/data/configs/ana_missing_window_key.conf new file mode 100644 index 0000000..b3f1bee --- /dev/null +++ b/packs/tests/data/configs/ana_missing_window_key.conf @@ -0,0 +1,20 @@ +[required] + +files = ['run19.h5'] + +window_args = {'WINDOW_START' : 100, + 'WINDOW_END' : 3e4, + 'BASELINE_POINT_1' : 1e6, + 'BASELINE_POINT_2' : 1.5e6, + 'BASELINE_RANGE_1' : 40e3} + +bin_size = 4 +chunk_size = 5 +negative = True +baseline_mode = 'median' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True +save_path = 'test.h5' \ No newline at end of file diff --git a/packs/tests/data/configs/ana_negative_window.conf b/packs/tests/data/configs/ana_negative_window.conf new file mode 100644 index 0000000..d7831f4 --- /dev/null +++ b/packs/tests/data/configs/ana_negative_window.conf @@ -0,0 +1,21 @@ +[required] + +files = ['run19.h5'] + +window_args = {'WINDOW_START' : -100, + 'WINDOW_END' : 3e4, + 'BASELINE_POINT_1' : 1e6, + 'BASELINE_POINT_2' : 1.5e6, + 'BASELINE_RANGE_1' : 40e3, + 'BASELINE_RANGE_2' : 40e3} + +bin_size = 4 +chunk_size = 5 +negative = True +baseline_mode = 'median' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True +save_path = 'test.h5' \ No newline at end of file diff --git a/packs/tests/data/configs/ana_nonexistent_file.conf b/packs/tests/data/configs/ana_nonexistent_file.conf new file mode 100644 index 0000000..f5ad75d --- /dev/null +++ b/packs/tests/data/configs/ana_nonexistent_file.conf @@ -0,0 +1,21 @@ +[required] + +files = ['secretfakefile.h5'] + +window_args = {'WINDOW_START' : 100, + 'WINDOW_END' : 3e4, + 'BASELINE_POINT_1' : 1e6, + 'BASELINE_POINT_2' : 1.5e6, + 'BASELINE_RANGE_1' : 40e3, + 'BASELINE_RANGE_2' : 40e3} + +bin_size = 4 +chunk_size = 5 +negative = True +baseline_mode = 'median' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True +save_path = 'test.h5' \ No newline at end of file diff --git a/packs/tests/data/configs/ana_save_dir_missing.conf b/packs/tests/data/configs/ana_save_dir_missing.conf new file mode 100644 index 0000000..4e39285 --- /dev/null +++ b/packs/tests/data/configs/ana_save_dir_missing.conf @@ -0,0 +1,21 @@ +[required] + +files = ['run19.h5'] + +window_args = {'WINDOW_START' : 100, + 'WINDOW_END' : 3e4, + 'BASELINE_POINT_1' : 1e6, + 'BASELINE_POINT_2' : 1.5e6, + 'BASELINE_RANGE_1' : 40e3, + 'BASELINE_RANGE_2' : 40e3} + +bin_size = 4 +chunk_size = 5 +negative = True +baseline_mode = 'median' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True +save_path = 'nosuchthing/test.h5' \ No newline at end of file diff --git a/packs/tests/data/configs/ana_unknown_key.conf b/packs/tests/data/configs/ana_unknown_key.conf new file mode 100644 index 0000000..25fc11a --- /dev/null +++ b/packs/tests/data/configs/ana_unknown_key.conf @@ -0,0 +1,23 @@ +[required] + +files = ['run19.h5'] + +window_args = {'WINDOW_START' : 100, + 'WINDOW_END' : 3e4, + 'BASELINE_POINT_1' : 1e6, + 'BASELINE_POINT_2' : 1.5e6, + 'BASELINE_RANGE_1' : 40e3, + 'BASELINE_RANGE_2' : 40e3} + +bin_size = 4 +chunk_size = 5 +negative = True +baseline_mode = 'median' +verbose = 1 +peak_threshold = 1000 +suppression_threshold = 10 + +overwrite = True +save_path = 'test.h5' + +mules = 'bad' \ No newline at end of file diff --git a/packs/tests/setup_test.py b/packs/tests/setup_test.py index 5901862..d7a54eb 100644 --- a/packs/tests/setup_test.py +++ b/packs/tests/setup_test.py @@ -56,7 +56,7 @@ def test_malformed_config(config, error, data_dir): x = read_config_file(file_path) @mark.parametrize("config, error", [('nonexistent_WD_version.conf', RuntimeError), - ('nonexistent_process.conf', ValueError), + ('nonexistent_process.conf', RuntimeError), ('single_multi_chan.conf', RuntimeError)]) # these will change to value errors when other # packs are implemented @@ -66,7 +66,32 @@ def test_processing_catches(config, error, MULE_dir, data_dir): run_pack = [sys.executable, MULE_dir + "/bin/mule", "proc", config_path] - with raises(subprocess.CalledProcessError): - subprocess.run(run_pack, check = True) + with raises(subprocess.CalledProcessError) as excinfo: + subprocess.run(run_pack, check=True, capture_output=True, text=True, cwd = data_dir) + assert error.__name__ in excinfo.value.stderr +@mark.parametrize("config, error", [('ana_negative_window.conf', ValueError), + ('ana_inverted_window.conf', ValueError), + ('ana_baseline_overlaps_window.conf', ValueError), + ('ana_baselines_overlap.conf', ValueError), + ('ana_missing_window_key.conf', KeyError), + ('ana_missing_window_args.conf', KeyError), + ('ana_missing_save_path.conf', KeyError), + ('ana_missing_bin_size.conf', TypeError), + ('ana_unknown_key.conf', TypeError), + ('ana_nonexistent_file.conf', ValueError), + ('ana_empty_files.conf', ValueError), + ('ana_bad_baseline_mode.conf', ValueError), + ('ana_save_dir_missing.conf', FileNotFoundError), + ('ana_files_not_list.conf', TypeError)]) +def test_ana_catches(config, error, MULE_dir, data_dir): + + config_path = data_dir + "configs/" + config + + run_pack = [sys.executable, MULE_dir + "/bin/mule", "ana", config_path] + + with raises(subprocess.CalledProcessError) as excinfo: + subprocess.run(run_pack, check=True, capture_output=True, text=True) + assert error.__name__ in excinfo.value.stderr + \ No newline at end of file