Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/github_actions/actions-8f1bff295e
Browse files Browse the repository at this point in the history
  • Loading branch information
pkrull-ansys authored Oct 3, 2024
2 parents f89f09d + 49d3cb1 commit ee3612d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "flit_core.buildapi"
[project]
# Check https://flit.readthedocs.io/en/latest/pyproject_toml.html for all available sections
name = "ansys-additive-core"
version = "0.19.dev29"
version = "0.19.dev30"
description = "A Python client for the Ansys Additive service"
readme = "README.rst"
requires-python = ">=3.10,<4"
Expand Down Expand Up @@ -45,7 +45,7 @@ dependencies = [
[project.optional-dependencies]
tests = [
"ansys-platform-instancemanagement==1.1.2",
"dill==0.3.8",
"dill==0.3.9",
"google-api-python-client==2.147.0",
"googleapis-common-protos==1.65.0",
"grpcio==1.60.0",
Expand All @@ -62,7 +62,7 @@ tests = [
]

doc = [
"ansys-sphinx-theme[autoapi]==1.0.11",
"ansys-sphinx-theme[autoapi]==1.1.2",
"enum-tools==0.12.0",
"jupyter_sphinx==0.5.3",
"matplotlib==3.9.2",
Expand Down
23 changes: 18 additions & 5 deletions src/ansys/additive/core/parametric_study/parametric_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -1661,14 +1661,27 @@ def _add_simulations_from_csv(self, file_path: str | os.PathLike) -> list[str]:
except Exception as e:
raise ValueError(f"Unable to read CSV file: {e}")

columns = [getattr(ColumnNames, k) for k in ColumnNames.__dict__ if not k.startswith("_")]
required_columns = [col for col in columns if col != ColumnNames.PV_RATIO]
columns = set(
[getattr(ColumnNames, k) for k in ColumnNames.__dict__ if not k.startswith("_")]
)
# older CSV files may not have the PV_RATIO column
columns.remove(ColumnNames.PV_RATIO)

if all([set(df.columns) == set(required_columns)]):
if not set(df.columns).issuperset(columns):
raise ValueError(
f"CSV is missing expected columns: {', '.join(str(v) for v in (columns - set(df.columns)))}"
)

# add PV_RATIO column to the dataframe if not present
if ColumnNames.PV_RATIO not in df:
df = ParametricStudy._add_pv_ratio(df)

if not all([set(df.columns) == set(columns)]):
raise ValueError("CSV file does not have the expected columns.")
# check material name
csv_material = str(df[ColumnNames.MATERIAL].iloc[0])
if self.material_name and not csv_material.lower() == self.material_name.lower():
raise ValueError(
f"Material in CSV '{csv_material}' does not match study material '{self.material_name}'"
)

# check valid inputs
drop_indices, error_list = list(), list()
Expand Down
43 changes: 32 additions & 11 deletions tests/parametric_study/test_parametric_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import os
import pathlib
import platform
import re
import shutil
import tempfile
from unittest.mock import Mock, PropertyMock, create_autospec, patch
Expand Down Expand Up @@ -2136,12 +2137,13 @@ def test_import_csv_study_raises_exception_when_file_does_not_have_correct_heade
incorrect_headers_file = test_utils.get_test_file_path(
pathlib.Path("csv") / "incorrect-headers.csv"
)

# act
study = ParametricStudy(tmp_path / study_name, "material")

# assert
with pytest.raises(ValueError, match="does not have the expected columns"):
# act, assert
with pytest.raises(
ValueError,
match=re.escape("CSV is missing expected columns: Scan Speed (m/s)"),
):
study.import_csv_study(incorrect_headers_file)


Expand All @@ -2163,6 +2165,25 @@ def test_import_csv_study_adds_simulations_to_new_study(
assert len(study.data_frame()[ColumnNames.LASER_POWER].unique()) == 5


def test_import_csv_raises_exception_when_material_does_not_match(
tmp_path: pytest.TempPathFactory,
):
# arrange
material = "not-a-material"
study_name = "test_study"
single_bead_demo_csv_file = test_utils.get_test_file_path(
pathlib.Path("csv") / "single-bead-test-study.csv"
)
study = ParametricStudy(tmp_path / study_name, material)

# act, assert
with pytest.raises(
ValueError,
match="Material in CSV 'material' does not match study material 'not-a-material'",
):
study.import_csv_study(single_bead_demo_csv_file)


def test_import_csv_study_adds_pv_column_for_csv_file_without_the_column(
tmp_path: pytest.TempPathFactory,
):
Expand All @@ -2189,7 +2210,7 @@ def test_import_csv_study_reads_pv_column_for_csv_file_containing_the_column(
pathlib.Path("csv") / "pv-column-study.csv"
)
# act
study = ParametricStudy(tmp_path / study_name, "material")
study = ParametricStudy(tmp_path / study_name, "IN718")
errors = study.import_csv_study(single_bead_demo_csv_file)

# assert
Expand Down Expand Up @@ -2260,7 +2281,7 @@ def test_import_csv_study_calls_remove_duplicates_entries_correctly(
monkeypatch, file_name, argument, tmp_path: pytest.TempPathFactory
):
# arrange
study = ParametricStudy(tmp_path / "test_study", "material")
study = ParametricStudy(tmp_path / "test_study", "IN718")
csv_file = test_utils.get_test_file_path(pathlib.Path("csv") / file_name)
patched_simulate = create_autospec(ParametricStudy._remove_duplicate_entries, return_value=0)
monkeypatch.setattr(ParametricStudy, "_remove_duplicate_entries", patched_simulate)
Expand All @@ -2278,7 +2299,7 @@ def test_import_csv_study_drops_duplicates_with_correct_simulation_status_heirar
# arrange
study_name = "test_study"
duplicate_rows_file = test_utils.get_test_file_path(pathlib.Path("csv") / "duplicate-rows.csv")
study = ParametricStudy(tmp_path / study_name, "material")
study = ParametricStudy(tmp_path / study_name, "IN718")

# act
errors = study.import_csv_study(duplicate_rows_file)
Expand Down Expand Up @@ -2310,7 +2331,7 @@ def test_import_csv_study_does_not_add_simulations_with_invalid_inputs_and_retur
)

# act
study = ParametricStudy(tmp_path / study_name, "material")
study = ParametricStudy(tmp_path / study_name, "IN718")
error_list = study.import_csv_study(invalid_input_parameters_file)

# assert
Expand All @@ -2337,7 +2358,7 @@ def test_import_csv_study_does_not_add_simulations_with_nan_inputs_and_returns_e
nan_input_parameters_file = test_utils.get_test_file_path(pathlib.Path("csv") / file_name)

# act
study = ParametricStudy(tmp_path / study_name, "material")
study = ParametricStudy(tmp_path / study_name, "IN718")
error_list = study.import_csv_study(nan_input_parameters_file)

# assert
Expand All @@ -2357,7 +2378,7 @@ def test_import_csv_adds_microstructure_simulations_with_nan_thermal_parameter_v
)

# act
study = ParametricStudy(tmp_path / study_name, "material")
study = ParametricStudy(tmp_path / study_name, "IN718")
error_list = study.import_csv_study(nan_thermal_parameters_file)

# assert
Expand All @@ -2375,7 +2396,7 @@ def test_import_csv_study_does_not_add_simulations_with_invalid_status_or_type_a
)

# act
study = ParametricStudy(tmp_path / study_name, "material")
study = ParametricStudy(tmp_path / study_name, "IN718")
error_list = study.import_csv_study(invalid_type_status_file)

# assert
Expand Down

0 comments on commit ee3612d

Please sign in to comment.