Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve example files handling in examples scripts #193

Merged
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6547045
feat: improve example files handling in examples scripts
anshlachamb Dec 19, 2024
8a063e8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 19, 2024
aaeb624
chore: adding changelog file 193.added.md [dependabot-skip]
pyansys-ci-bot Dec 19, 2024
c5a6a03
feat: spelling for precommit
anshlachamb Dec 19, 2024
129edd4
Merge branch 'feat/182-improve-example-files-handling-in-examples-scr…
anshlachamb Dec 19, 2024
1e41b66
feat: spelling and formating for precommit
anshlachamb Dec 19, 2024
fcaf0d8
feat: rename function, add documentation, remove unused parameter
anshlachamb Dec 20, 2024
95d2c2a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 20, 2024
5e343bd
feat: reformat following precommit
anshlachamb Dec 20, 2024
348d970
Merge branch 'feat/182-improve-example-files-handling-in-examples-scr…
anshlachamb Dec 20, 2024
420344f
feat: make function private
anshlachamb Dec 20, 2024
4ccda3d
Merge branch 'main' into feat/182-improve-example-files-handling-in-e…
a-bouth Dec 20, 2024
7c09fdd
feat: force pipeline to fail when example generation fails
anshlachamb Dec 20, 2024
7d42538
Merge branch 'feat/182-improve-example-files-handling-in-examples-scr…
anshlachamb Dec 20, 2024
61fb5f9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 20, 2024
2b6babf
feat: force pipeline to fail when example generation fails - still an…
anshlachamb Dec 20, 2024
7486123
Merge branch 'feat/182-improve-example-files-handling-in-examples-scr…
anshlachamb Dec 20, 2024
f82ba0a
feat: improve functions documentation
anshlachamb Dec 20, 2024
65c74e2
feat: fix example 1 to re-upload file to the new server
anshlachamb Dec 20, 2024
bbe28b8
feat: skipping some tests voluntarily
a-bouth Dec 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/193.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: improve example files handling in examples scripts
1 change: 1 addition & 0 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"image_scrapers": (DynamicScraper(), "matplotlib"),
"ignore_pattern": r"__init__\.py",
"thumbnail_size": (350, 350),
"abort_on_example_error": True,
}

suppress_warnings = ["config.cache"]
Expand Down
4 changes: 3 additions & 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 Expand Up @@ -131,6 +131,8 @@
my_server = connect_to_or_start_server(use_license_context=True)

# Execute the same and measure the execution time
path_flute_wav = download_flute_wav(server=my_server)

for i in range(5):
now = datetime.datetime.now()
wav_loader = LoadWav(path_flute_wav)
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
2 changes: 1 addition & 1 deletion examples/006_calculate_PR_and_TNR.py
Original file line number Diff line number Diff line change
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
126 changes: 88 additions & 38 deletions src/ansys/sound/core/examples_helpers/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import os
import shutil

from ansys.dpf.core import upload_file_in_tmp_folder
import platformdirs
import requests

Expand Down Expand Up @@ -66,16 +67,40 @@
return True


def _get_file_url(filename, directory=None): # pragma no cover
if directory:
return (
f"https://github.com/ansys/example-data/raw/master/{directory}/{filename}" # noqa: E231
)
return f"https://github.com/ansys/example-data/raw/master/{filename}" # noqa: E231
def _get_example_file_url(filename): # pragma no cover
"""Get the URL of the example file in the PyAnsys Sound examples repository.

Parameters
----------
filename : str
File in https://github.com/ansys/example-data/raw/master/pyansys-sound/

Returns
-------
File url
"""
return (
f"https://github.com/ansys/example-data/raw/master/pyansys-sound/{filename}" # noqa: E231
)


@check_directory_exist(EXAMPLES_PATH)
def _retrieve_file(url, filename, _test=False): # pragma no cover
def _download_file_in_local_tmp_folder(url, filename): # pragma no cover
"""Download a file from the PyAnsys Sound examples repository to the local tmp folder.

Parameters
----------
url : str
Url of the file to download.
This url can be provided using _get_example_file_url() function.

filename : str
File name in the local tmp folder.

Returns
-------
Local path of the downloaded file.
"""
# First check if file has already been downloaded
local_path = os.path.join(EXAMPLES_PATH, os.path.basename(filename))
local_path_no_zip = local_path.replace(".zip", "")
Expand All @@ -93,17 +118,32 @@
return local_path


def _download_file(filename, directory=None, _test=False): # pragma no cover
url = _get_file_url(filename, directory)
try:
local_path = _retrieve_file(url, filename, _test)
def _download_example_file_to_server_tmp_folder(filename, server=None): # pragma no cover
"""Download a file from the PyAnsys Sound examples repository and upload it to the DPF server.

# 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
Parameters
----------
filename : str
Example file name.

server : ansys.dpf.core.server.Server, optional
DPF server to which to upload the file.
If None, attempts to use the global server.

Returns
-------
str
Path of the file on the DPF server.
"""
# get file url in git repo
url = _get_example_file_url(filename)
try:
# download file locally
local_path = _download_file_in_local_tmp_folder(url, filename)
# upload file to DPF server,
# so that we are independent on the server configuration
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 @@ -119,6 +159,9 @@
def download_flute_psd():
"""Download the PSD of the ``flute.wav`` file.

As flute_psd.txt file is opened using python 'open' function,
we need the local path of the file.

Examples
--------
>>> from ansys.sound.core.examples_helpers import download_flute_psd
Expand All @@ -128,89 +171,96 @@
-------
str
Local path for the ``flute_psd.txt`` file.
str
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)
url = _get_example_file_url("flute_psd.txt")
try:
# download file locally
local_path = _download_file_in_local_tmp_folder(url, "flute_psd.txt")
except Exception as e: # Generate exception
raise RuntimeError(

Check warning on line 180 in src/ansys/sound/core/examples_helpers/download.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/sound/core/examples_helpers/download.py#L179-L180

Added lines #L179 - L180 were not covered by tests
"For the reason that follows, retrieving the file failed.\n"
"You can download this file from:\n"
f"{url}\n"
"\n"
"The reported error message is:\n"
f"{str(e)}"
)

return local_path


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 _download_example_file_to_server_tmp_folder("flute.wav", 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 _download_example_file_to_server_tmp_folder("flute2.wav", 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 _download_example_file_to_server_tmp_folder("accel_with_rpm.wav", 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 _download_example_file_to_server_tmp_folder("accel_with_rpm_2.wav", 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 _download_example_file_to_server_tmp_folder("accel_with_rpm_3.wav", 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 _download_example_file_to_server_tmp_folder("xtract_demo_signal_1.wav", 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 _download_example_file_to_server_tmp_folder("xtract_demo_signal_2.wav", server=server)
Loading