From 654704553949ba5497a3f0d7b854721d903a99d3 Mon Sep 17 00:00:00 2001 From: Helene Lachambre Date: Thu, 19 Dec 2024 15:45:00 +0100 Subject: [PATCH] feat: improve example files handling in examples scripts --- ...initialize_server_and_deal_with_license.py | 2 +- ...2_load_resample_amplify_write_wav_files.py | 2 +- examples/003_compute_stft.py | 2 +- examples/004_isolate_orders.py | 6 +-- examples/005_xtract_feature.py | 4 +- examples/006_calculate_PR_and_TNR.py | 4 +- ...007_calculate_psychoacoustic_indicators.py | 6 +-- .../sound/core/examples_helpers/download.py | 52 +++++++++---------- 8 files changed, 37 insertions(+), 41 deletions(-) diff --git a/examples/001_initialize_server_and_deal_with_license.py b/examples/001_initialize_server_and_deal_with_license.py index c5327cf2c..c199f565a 100644 --- a/examples/001_initialize_server_and_deal_with_license.py +++ b/examples/001_initialize_server_and_deal_with_license.py @@ -97,7 +97,7 @@ # Execute the PyAnsys Sound ``LoadWav`` operator several times in a row # and measure the execution time. -path_flute_wav = download_flute_wav() +path_flute_wav = download_flute_wav(server=my_server) for i in range(5): now = datetime.datetime.now() diff --git a/examples/002_load_resample_amplify_write_wav_files.py b/examples/002_load_resample_amplify_write_wav_files.py index a18027d12..f7cf9838e 100644 --- a/examples/002_load_resample_amplify_write_wav_files.py +++ b/examples/002_load_resample_amplify_write_wav_files.py @@ -57,7 +57,7 @@ # in the DPF-Core API documentation. # Return the input data of the example file -path_flute_wav = download_flute_wav() +path_flute_wav = download_flute_wav(server=server) # Load the WAV file. wav_loader = LoadWav(path_flute_wav) diff --git a/examples/003_compute_stft.py b/examples/003_compute_stft.py index a7603551c..5ff495d60 100644 --- a/examples/003_compute_stft.py +++ b/examples/003_compute_stft.py @@ -59,7 +59,7 @@ # in the DPF-Core API documentation. # Return the input data of the example file -path_flute_wav = download_flute_wav() +path_flute_wav = download_flute_wav(server=my_server) # Load the WAV file wav_loader = LoadWav(path_flute_wav) diff --git a/examples/004_isolate_orders.py b/examples/004_isolate_orders.py index 4b4e97585..c07d79ad3 100644 --- a/examples/004_isolate_orders.py +++ b/examples/004_isolate_orders.py @@ -129,7 +129,7 @@ def plot_stft(stft_class, vmax): # - The associated RPM profile # Return the input data of the example file -path_accel_wav = download_accel_with_rpm_wav() +path_accel_wav = download_accel_with_rpm_wav(server=my_server) # Load the WAV file. wav_loader = LoadWav(path_accel_wav) @@ -249,8 +249,8 @@ def plot_stft(stft_class, vmax): # Obtain parent folder of the 'accel_with_rpm.wav' file parent_folder = pathlib.Path(path_accel_wav).parent.absolute() -path_accel_wav_2 = download_accel_with_rpm_2_wav() -path_accel_wav_3 = download_accel_with_rpm_3_wav() +path_accel_wav_2 = download_accel_with_rpm_2_wav(server=my_server) +path_accel_wav_3 = download_accel_with_rpm_3_wav(server=my_server) paths = (path_accel_wav, path_accel_wav_2, path_accel_wav_3) fft_sizes = [256, 2048, 4096] diff --git a/examples/005_xtract_feature.py b/examples/005_xtract_feature.py index 790096072..a7c9d4e2e 100644 --- a/examples/005_xtract_feature.py +++ b/examples/005_xtract_feature.py @@ -141,7 +141,7 @@ def plot_stft(stft_class, SPLmax, title="STFT", maximum_frequency=MAX_FREQUENCY_ # The WAV file contains harmonics and shocks. # Return the input data of the example file -path_xtract_demo_signal_1 = download_xtract_demo_signal_1_wav() +path_xtract_demo_signal_1 = download_xtract_demo_signal_1_wav(my_server) # Load the WAV file wav_loader = LoadWav(path_to_wav=path_xtract_demo_signal_1) @@ -292,7 +292,7 @@ def plot_stft(stft_class, SPLmax, title="STFT", maximum_frequency=MAX_FREQUENCY_ # all previous classes. -path_xtract_demo_signal_2 = download_xtract_demo_signal_2_wav() +path_xtract_demo_signal_2 = download_xtract_demo_signal_2_wav(my_server) paths = [path_xtract_demo_signal_1, path_xtract_demo_signal_2] diff --git a/examples/006_calculate_PR_and_TNR.py b/examples/006_calculate_PR_and_TNR.py index 8eed25416..615c5eb9a 100644 --- a/examples/006_calculate_PR_and_TNR.py +++ b/examples/006_calculate_PR_and_TNR.py @@ -59,7 +59,7 @@ # Load the PSD contained in an ASCII file. This file has two columns: 'Frequency (Hz)' # and 'PSD amplitude (dB SPL/Hz)'. The data is located in # "C:\Users\username\AppData\Local\Ansys\ansys_sound_core\examples\". -path_flute_psd = download_flute_psd() +path_flute_psd = download_flute_psd(server=my_server) fid = open(path_flute_psd) fid.readline() # Skip the first line (header) all_lines = fid.readlines() @@ -146,7 +146,7 @@ # Use the PowerSpectralDensity class to calculate a PSD, and compute Prominence Ratio (PR). # Load example data from WAV file. -path_flute_wav = download_flute_wav() +path_flute_wav = download_flute_wav(server=my_server) wav_loader = LoadWav(path_flute_wav) wav_loader.process() flute_signal = wav_loader.get_output()[0] diff --git a/examples/007_calculate_psychoacoustic_indicators.py b/examples/007_calculate_psychoacoustic_indicators.py index 177d98146..160f64d10 100644 --- a/examples/007_calculate_psychoacoustic_indicators.py +++ b/examples/007_calculate_psychoacoustic_indicators.py @@ -87,7 +87,7 @@ # in the DPF-Core API documentation. # Load example data from WAV file -path_flute_wav = download_flute_wav() +path_flute_wav = download_flute_wav(server=my_server) wav_loader = LoadWav(path_flute_wav) wav_loader.process() fc_signal = wav_loader.get_output() @@ -117,7 +117,7 @@ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Load another WAV file and store it along with the first one. -path_flute2_wav = download_flute_2_wav() +path_flute2_wav = download_flute_2_wav(server=my_server) wav_loader = LoadWav(path_flute2_wav) wav_loader.process() @@ -151,7 +151,7 @@ # Calculate ISO 532-1 loudness for a non-stationary sound # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Load a new signal (non-stationary) from a WAV file. -path_accel_wav = download_accel_with_rpm_wav() +path_accel_wav = download_accel_with_rpm_wav(server=my_server) wav_loader = LoadWav(path_accel_wav) wav_loader.process() f_signal = wav_loader.get_output()[0] # Field 0 only, because the RPM profile is useless here. diff --git a/src/ansys/sound/core/examples_helpers/download.py b/src/ansys/sound/core/examples_helpers/download.py index 21d58f76e..724e283d7 100644 --- a/src/ansys/sound/core/examples_helpers/download.py +++ b/src/ansys/sound/core/examples_helpers/download.py @@ -28,6 +28,8 @@ import platformdirs import requests +from ansys.dpf.core import upload_file_in_tmp_folder + # Setup data directory USER_DATA_PATH = platformdirs.user_data_dir(appname="ansys_sound_core", appauthor="Ansys") if not os.path.exists(USER_DATA_PATH): # pragma: no cover @@ -93,17 +95,15 @@ def _retrieve_file(url, filename, _test=False): # pragma no cover return local_path -def _download_file(filename, directory=None, _test=False): # pragma no cover +def _get_file_path_in_dpf_server(filename, directory=None, _test=False, server=None): # pragma no cover + # get file url in git repo url = _get_file_url(filename, directory) try: + # download file locally local_path = _retrieve_file(url, filename, _test) - - # In case of CI/CD pipelines - port_in_env = os.environ.get("ANSRV_DPF_SOUND_PORT") - if port_in_env is not None: - return "C:\\data\\" + filename - else: - return local_path + # upload file to DPF server, so that we are independant on the local configuration (docker or not) + server_path = upload_file_in_tmp_folder(file_path=local_path, server=server) + return server_path except Exception as e: # Generate exception raise RuntimeError( @@ -116,7 +116,7 @@ def _download_file(filename, directory=None, _test=False): # pragma no cover ) -def download_flute_psd(): +def download_flute_psd(server = None): """Download the PSD of the ``flute.wav`` file. Examples @@ -132,14 +132,10 @@ def download_flute_psd(): Path on the Docker container for the ``flute_psd.txt`` file. """ - filename = "flute_psd.txt" - directory = "pyansys-sound" - url = _get_file_url(filename, directory) - local_path = _retrieve_file(url, filename, False) - return local_path + return _get_file_path_in_dpf_server("flute_psd.txt", "pyansys-sound", server = server) -def download_flute_wav(): +def download_flute_wav(server = None): """Download the ``flute.wav`` file. Returns @@ -147,10 +143,10 @@ def download_flute_wav(): str Path for the ``flute.wav`` file. """ - return _download_file("flute.wav", "pyansys-sound") + return _get_file_path_in_dpf_server("flute.wav", "pyansys-sound", server = server) -def download_flute_2_wav(): +def download_flute_2_wav(server = None): """Download the ``flute2.wav`` file. Returns @@ -158,10 +154,10 @@ def download_flute_2_wav(): str Path for the ``flute2.wav`` file. """ - return _download_file("flute2.wav", "pyansys-sound") + return _get_file_path_in_dpf_server("flute2.wav", "pyansys-sound", server = server) -def download_accel_with_rpm_wav(): +def download_accel_with_rpm_wav(server = None): """Download the ``accel_with_rpm.wav`` file. Returns @@ -169,10 +165,10 @@ def download_accel_with_rpm_wav(): str Path for the ``accel_with_rpm.wav`` file. """ - return _download_file("accel_with_rpm.wav", "pyansys-sound") + return _get_file_path_in_dpf_server("accel_with_rpm.wav", "pyansys-sound", server = server) -def download_accel_with_rpm_2_wav(): +def download_accel_with_rpm_2_wav(server = None): """Download the ``accel_with_rpm_2.wav`` file. Returns @@ -180,10 +176,10 @@ def download_accel_with_rpm_2_wav(): str Path for the ``accel_with_rpm_2.wav`` file. """ - return _download_file("accel_with_rpm_2.wav", "pyansys-sound") + return _get_file_path_in_dpf_server("accel_with_rpm_2.wav", "pyansys-sound", server = server) -def download_accel_with_rpm_3_wav(): +def download_accel_with_rpm_3_wav(server = None): """Download the ``accel_with_rpm_3.wav`` file. Returns @@ -191,10 +187,10 @@ def download_accel_with_rpm_3_wav(): str Path for the ``accel_with_rpm_3.wav`` file. """ - return _download_file("accel_with_rpm_3.wav", "pyansys-sound") + return _get_file_path_in_dpf_server("accel_with_rpm_3.wav", "pyansys-sound", server = server) -def download_xtract_demo_signal_1_wav(): +def download_xtract_demo_signal_1_wav(server = None): """Download the ``xtract_demo_signal_1.wav`` file. Returns @@ -202,10 +198,10 @@ def download_xtract_demo_signal_1_wav(): str Path for the ``xtract_demo_signal_1.wav`` file. """ - return _download_file("xtract_demo_signal_1.wav", "pyansys-sound") + return _get_file_path_in_dpf_server("xtract_demo_signal_1.wav", "pyansys-sound", server = server) -def download_xtract_demo_signal_2_wav(): +def download_xtract_demo_signal_2_wav(server = None): """Download the ``xtract_demo_signal_2.wav`` file. Returns @@ -213,4 +209,4 @@ def download_xtract_demo_signal_2_wav(): str Path for the ``xtract_demo_signal_2.wav`` file. """ - return _download_file("xtract_demo_signal_2.wav", "pyansys-sound") + return _get_file_path_in_dpf_server("xtract_demo_signal_2.wav", "pyansys-sound", server = server)