Skip to content

Commit

Permalink
fix: training data upload_folder method (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpbeing authored Jan 24, 2024
1 parent 2bf2ba6 commit dd48e6d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
18 changes: 10 additions & 8 deletions src/ansys/simai/core/data/training_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ def upload_folder(
self,
folder_path: Path,
compute: bool = True,
monitor_callback: Optional[MonitorCallback] = None,
) -> List["TrainingDataPart"]:
"""Upload all the parts contained in a folder to a :class:`~ansys.simai.core.data.training_data.TrainingData` instance.
Expand All @@ -141,14 +140,11 @@ def upload_folder(
Args:
folder_path: Path to the folder with the files to upload.
compute: Whether to compute the training data after upload. The default is ``True``.
monitor_callback: Optional callback for monitoring the progress of the upload.
For more information, see the :obj:`~ansys.simai.core.data.types.MonitorCallback`
object.
Returns:
List of uploaded training data parts.
"""
return self._directory.upload_folder(self.id, folder_path, compute, monitor_callback)
return self._directory.upload_folder(self.id, folder_path, compute)

def add_to_project(self, project: Identifiable["Project"]):
"""Add the training data to a :class:`~ansys.simai.core.data.projects.Project` object.
Expand Down Expand Up @@ -249,7 +245,10 @@ def upload_part(
)

def upload_folder(
self, training_data: Identifiable[TrainingData], folder_path: Path, compute: bool = True
self,
training_data: Identifiable[TrainingData],
folder_path: Path,
compute: bool = True,
) -> List["TrainingDataPart"]:
"""Upload all files in a folder to a :class:`~ansys.simai.core.data.training_data.TrainingData` object.
Expand All @@ -261,14 +260,17 @@ def upload_folder(
folder_path: Path to the folder that contains the files to upload.
compute: Whether to compute the training data after upload. The default is ``True``.
"""
training_data_id = get_id_from_identifiable(training_data)
path = pathlib.Path(folder_path)
if not path.is_dir():
raise InvalidArguments("Provided path is not a folder.")
path_content = path.glob("[!.]*")
files = (obj for obj in path_content if obj.is_file())
uploaded_parts = []
for file in files:
uploaded_parts.append(_upload_training_data_part(id, file, self._client, None))
uploaded_parts.append(
_upload_training_data_part(training_data_id, file, self._client, None)
)
if compute:
self._client._api.compute_training_data(id)
self._client._api.compute_training_data(training_data_id)
return uploaded_parts
10 changes: 5 additions & 5 deletions src/ansys/simai/core/utils/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ def handle_http_errors(response: requests.Response) -> None:
logger.debug("Checking for HTTP errors.")
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
except requests.exceptions.HTTPError as e:
try:
json_response = response.json()
except (ValueError, JSONDecodeError):
# raise the errors from None
# as we want to ignore the JSONDecodeError
if response.status_code == 404:
raise NotFoundError("Not Found", response) from None
raise NotFoundError("Not Found", response) from e
else:
raise ApiClientError(
f"{response.status_code} {response.reason}", response
Expand All @@ -66,14 +66,14 @@ def handle_http_errors(response: requests.Response) -> None:
)

if response.status_code == 404:
raise NotFoundError(f"{message}", response) from None
raise NotFoundError(f"{message}", response) from e
else:
raise ApiClientError(
f"{response.status_code} {message}",
response,
) from None
) from e
else:
raise ApiClientError(f"{response.status_code}: {response.reason}", response) from None
raise ApiClientError(f"{response.status_code}: {response.reason}", response) from e


def handle_response(response: requests.Response, return_json: bool = True) -> APIResponse:
Expand Down

0 comments on commit dd48e6d

Please sign in to comment.