Skip to content

Commit

Permalink
Add tests for get/set as gdc for harmo 2 params
Browse files Browse the repository at this point in the history
  • Loading branch information
ansaminard committed Jan 7, 2025
1 parent dbd2624 commit 3437c64
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ def get_as_generic_data_containers(self) -> tuple[GenericDataContainer]:
if not self.is_source_control_valid():
warnings.warn(
PyAnsysSoundWarning(
"Cannot create source control generic data container because there is no "
"source control data."
"Cannot create source control generic data container because at least one "
"source control data is missing."
)
)
gdc_source_control = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,15 @@ def test_source_audio_set_from_generic_data_containers():
"""Test SourceAudio set_from_generic_data_containers method."""
loader = LoadWav(path_to_wav=pytest.data_path_flute_nonUnitaryCalib_in_container)
loader.process()
data = loader.get_output()[0]
f_data: Field = loader.get_output()[0]

gdc_source = GenericDataContainer()
gdc_source.set_property("sound_composer_source", data)
gdc_source.set_property("sound_composer_source", f_data)

source_audio = SourceAudio()
source_audio.set_from_generic_data_containers(gdc_source, None)
assert isinstance(source_audio.source_audio_data, Field)
assert len(source_audio.source_audio_data.data) == len(f_data.data)
assert source_audio.source_audio_data.data[17640] == pytest.approx(EXP_AUDIO_DATA17640)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ def test_source_broadband_noise_set_from_generic_data_containers():
op = Operator("sound_composer_load_source_bbn")
op.connect(0, pytest.data_path_sound_composer_bbn_source_in_container)
op.run()
data = op.get_output(0, "fields_container")
fc_data: FieldsContainer = op.get_output(0, "fields_container")

gdc_source = GenericDataContainer()
gdc_source.set_property("sound_composer_source", data)
gdc_source.set_property("sound_composer_source", fc_data)

f_source_control = fields_factory.create_scalar_field(
num_entities=1, location=locations.time_freq
Expand All @@ -245,7 +245,7 @@ def test_source_broadband_noise_set_from_generic_data_containers():
source_bbn_obj = SourceBroadbandNoise()
source_bbn_obj.set_from_generic_data_containers(gdc_source, gdc_source_control)
assert isinstance(source_bbn_obj.source_bbn, FieldsContainer)
assert len(source_bbn_obj.source_bbn) == len(data)
assert len(source_bbn_obj.source_bbn) == len(fc_data)
assert isinstance(source_bbn_obj.source_control, SourceControlTime)
assert len(source_bbn_obj.source_control.control.data) == 5

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ def test_source_broadband_noise_two_parameters_set_from_generic_data_containers(
op = Operator("sound_composer_load_source_bbn_two_parameters")
op.connect(0, pytest.data_path_sound_composer_bbn_source_2p_in_container)
op.run()
data = op.get_output(0, "fields_container")
fc_data: FieldsContainer = op.get_output(0, "fields_container")

gdc_source = GenericDataContainer()
gdc_source.set_property("sound_composer_source", data)
gdc_source.set_property("sound_composer_source", fc_data)

f_source_control = fields_factory.create_scalar_field(
num_entities=1, location=locations.time_freq
Expand All @@ -284,7 +284,7 @@ def test_source_broadband_noise_two_parameters_set_from_generic_data_containers(
source_bbn_2p_obj = SourceBroadbandNoiseTwoParameters()
source_bbn_2p_obj.set_from_generic_data_containers(gdc_source, gdc_source_control)
assert isinstance(source_bbn_2p_obj.source_bbn_two_parameters, FieldsContainer)
assert len(source_bbn_2p_obj.source_bbn_two_parameters) == len(data)
assert len(source_bbn_2p_obj.source_bbn_two_parameters) == len(fc_data)
assert isinstance(source_bbn_2p_obj.source_control1, SourceControlTime)
assert len(source_bbn_2p_obj.source_control1.control.data) == 5
assert isinstance(source_bbn_2p_obj.source_control2, SourceControlTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from ansys.dpf.core import (
Field,
FieldsContainer,
GenericDataContainer,
Operator,
TimeFreqSupport,
fields_container_factory,
fields_factory,
Expand Down Expand Up @@ -330,6 +332,84 @@ def test_source_specrum_load_source_harmonics_two_parameters():
)


def test_source_harmonics_two_parameters_set_from_generic_data_containers():
"""Test SourceHarmonicsTwoParameters set_from_generic_data_containers method."""
op = Operator("sound_composer_load_source_harmonics_two_parameters")
op.connect(0, pytest.data_path_sound_composer_harmonics_source_2p_in_container)
op.run()
fc_data: FieldsContainer = op.get_output(0, "fields_container")

gdc_source = GenericDataContainer()
gdc_source.set_property("sound_composer_source", fc_data)

f_source_control = fields_factory.create_scalar_field(
num_entities=1, location=locations.time_freq
)
f_source_control.append([1.0, 2.0, 3.0, 4.0, 5.0], 1)
gdc_source_control = GenericDataContainer()
gdc_source_control.set_property("sound_composer_source_control_parameter_1", f_source_control)
gdc_source_control.set_property("sound_composer_source_control_parameter_2", f_source_control)

source_harmo_2p_obj = SourceHarmonicsTwoParameters()
source_harmo_2p_obj.set_from_generic_data_containers(gdc_source, gdc_source_control)
assert isinstance(source_harmo_2p_obj.source_harmonics_two_parameters, FieldsContainer)
assert len(source_harmo_2p_obj.source_harmonics_two_parameters) == len(fc_data)
assert isinstance(source_harmo_2p_obj.source_control_rpm, SourceControlTime)
assert len(source_harmo_2p_obj.source_control_rpm.control.data) == 5
assert isinstance(source_harmo_2p_obj.source_control2, SourceControlTime)
assert len(source_harmo_2p_obj.source_control2.control.data) == 5


def test_source_harmonics_two_parameters_get_as_generic_data_containers():
"""Test SourceHarmonicsTwoParameters get_as_generic_data_containers method."""
# Source controls undefined => warning.
source_harmo_2p_obj = SourceHarmonicsTwoParameters(
file=pytest.data_path_sound_composer_harmonics_source_2p_in_container
)
with pytest.warns(
PyAnsysSoundWarning,
match=(
"Cannot create source control generic data container because at least one source "
"control data is missing."
),
):
_, gdc_source_control = source_harmo_2p_obj.get_as_generic_data_containers()
assert gdc_source_control is None

# Source data undefined => warning.
source_harmo_2p_obj.source_harmonics_two_parameters = None
f_source_control = fields_factory.create_scalar_field(
num_entities=1, location=locations.time_freq
)
f_source_control.append([1.0, 2.0, 3.0, 4.0, 5.0], 1)
source_control_obj = SourceControlTime()
source_control_obj.control = f_source_control
source_harmo_2p_obj.source_control_rpm = source_control_obj
source_harmo_2p_obj.source_control2 = source_control_obj
with pytest.warns(
PyAnsysSoundWarning,
match="Cannot create source generic data container because there is no source data.",
):
gdc_source, _ = source_harmo_2p_obj.get_as_generic_data_containers()
assert gdc_source is None

# Both source and source control are defined.
source_harmo_2p_obj.load_source_harmonics_two_parameters(
pytest.data_path_sound_composer_harmonics_source_2p_in_container,
)
gdc_source, gdc_source_control = source_harmo_2p_obj.get_as_generic_data_containers()

assert isinstance(gdc_source, GenericDataContainer)
assert isinstance(gdc_source.get_property("sound_composer_source"), FieldsContainer)
assert isinstance(gdc_source_control, GenericDataContainer)
assert isinstance(
gdc_source_control.get_property("sound_composer_source_control_parameter_1"), Field
)
assert isinstance(
gdc_source_control.get_property("sound_composer_source_control_parameter_2"), Field
)


def test_source_harmonics_two_parameters_process():
"""Test SourceHarmonicsTwoParameters process method."""
# Create a field to use in a SourceControlTime object.
Expand Down
2 changes: 1 addition & 1 deletion tests/tests_sound_composer/test_sound_composer_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def test_track_set_from_generic_data_containers():
op_frf = Operator("load_FRF_from_txt")
op_frf.connect(0, pytest.data_path_filter_frf)
op_frf.run()
f_filter_frf = op_frf.get_output(0, "field")
f_filter_frf: Field = op_frf.get_output(0, "field")

gdc_track.set_property("track_is_filter", 1)
gdc_track.set_property("track_filter", f_filter_frf)
Expand Down

0 comments on commit 3437c64

Please sign in to comment.