Skip to content

Commit

Permalink
feat: improve example files handling in examples scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
anshlachamb committed Dec 19, 2024
1 parent cb4d73d commit 6547045
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 41 deletions.
2 changes: 1 addition & 1 deletion examples/001_initialize_server_and_deal_with_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion examples/002_load_resample_amplify_write_wav_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/003_compute_stft.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions examples/004_isolate_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions examples/005_xtract_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]

Expand Down
4 changes: 2 additions & 2 deletions examples/006_calculate_PR_and_TNR.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions examples/007_calculate_psychoacoustic_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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.
Expand Down
52 changes: 24 additions & 28 deletions src/ansys/sound/core/examples_helpers/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -132,85 +132,81 @@ 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
-------
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
-------
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
-------
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
-------
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
-------
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
-------
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
-------
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)

0 comments on commit 6547045

Please sign in to comment.