Skip to content

Commit f195ea6

Browse files
authored
[ODSC-51508] : Refactor results dir unique path creation (#568)
2 parents 31dd70e + c23c0ca commit f195ea6

File tree

3 files changed

+45
-33
lines changed

3 files changed

+45
-33
lines changed

ads/opctl/operator/lowcode/anomaly/model/base_model.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from ads.opctl import logger
1818

19+
from ...common.utils import get_unique_report_dir
1920
from ..operator_config import AnomalyOperatorConfig, AnomalyOperatorSpec
2021
from .anomaly_dataset import AnomalyDatasets, AnomalyOutput
2122
from ..const import OutputColumns, SupportedMetrics
@@ -266,17 +267,9 @@ def _save_report(
266267
"""Saves resulting reports to the given folder."""
267268
import datapane as dp
268269

269-
if self.spec.output_directory:
270-
output_dir = self.spec.output_directory.url
271-
else:
272-
output_dir = "tmp_operator_result"
273-
logger.warn(
274-
"Since the output directory was not specified, the output will be saved to {} directory.".format(
275-
output_dir
276-
)
277-
)
270+
unique_output_dir = get_unique_report_dir(self.spec.output_directory)
278271

279-
if ObjectStorageDetails.is_oci_path(output_dir):
272+
if ObjectStorageDetails.is_oci_path(unique_output_dir):
280273
storage_options = default_signer()
281274
else:
282275
storage_options = dict()
@@ -287,7 +280,7 @@ def _save_report(
287280
dp.save_report(report_sections, report_local_path)
288281
with open(report_local_path) as f1:
289282
with fsspec.open(
290-
os.path.join(output_dir, self.spec.report_file_name),
283+
os.path.join(unique_output_dir, self.spec.report_file_name),
291284
"w",
292285
**default_signer(),
293286
) as f2:
@@ -297,15 +290,15 @@ def _save_report(
297290
inliers = anomaly_output.get_inliers(self.datasets.data)
298291
write_data(
299292
data=inliers,
300-
filename=os.path.join(output_dir, self.spec.inliers_filename),
293+
filename=os.path.join(unique_output_dir, self.spec.inliers_filename),
301294
format="csv",
302295
storage_options=storage_options,
303296
)
304297

305298
outliers = anomaly_output.get_outliers(self.datasets.data)
306299
write_data(
307300
data=outliers,
308-
filename=os.path.join(output_dir, self.spec.outliers_filename),
301+
filename=os.path.join(unique_output_dir, self.spec.outliers_filename),
309302
format="csv",
310303
storage_options=storage_options,
311304
)
@@ -314,15 +307,15 @@ def _save_report(
314307
write_data(
315308
data=validation_metrics.rename_axis("metrics").reset_index(),
316309
filename=os.path.join(
317-
output_dir, self.spec.validation_metrics_filename
310+
unique_output_dir, self.spec.validation_metrics_filename
318311
),
319312
format="csv",
320313
storage_options=storage_options,
321314
)
322315

323316
logger.warn(
324317
f"The report has been successfully "
325-
f"generated and placed to the: {output_dir}."
318+
f"generated and placed to the: {unique_output_dir}."
326319
)
327320

328321
@abstractmethod

ads/opctl/operator/lowcode/common/utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import yaml
1818
from typing import Union
1919

20+
from ads.opctl import logger
2021
from ads.opctl.operator.lowcode.common.errors import (
2122
InputDataError,
2223
InvalidParameterError,
@@ -77,6 +78,40 @@ def write_data(data, filename, format, storage_options, index=False, **kwargs):
7778
)
7879

7980

81+
def get_unique_report_dir(output_dir: str) -> str:
82+
"""
83+
Generate a unique directory path for the report output.
84+
85+
Parameters
86+
------------
87+
output_dir: str
88+
The requested output directory path.
89+
Returns
90+
--------
91+
str: The unique directory path for the report output.
92+
"""
93+
94+
if output_dir:
95+
output_dir = output_dir.url
96+
# set the unique directory path as the requested path by the user
97+
unique_output_dir = output_dir
98+
else:
99+
output_dir = "results"
100+
101+
# If the directory exists, find the next unique directory name by appending an incrementing suffix
102+
counter = 1
103+
unique_output_dir = f"{output_dir}"
104+
while os.path.exists(unique_output_dir):
105+
unique_output_dir = f"{output_dir}_{counter}"
106+
counter += 1
107+
logger.warn(
108+
"Since the output directory was not specified, the output will be saved to {} directory.".format(
109+
unique_output_dir
110+
)
111+
)
112+
return unique_output_dir
113+
114+
80115
def merge_category_columns(data, target_category_columns):
81116
result = data.apply(
82117
lambda x: "__".join([str(x[col]) for col in target_category_columns]), axis=1

ads/opctl/operator/lowcode/forecast/model/base_model.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
SupportedModels,
4545
SpeedAccuracyMode,
4646
)
47+
from ...common.utils import get_unique_report_dir
4748
from ..operator_config import ForecastOperatorConfig, ForecastOperatorSpec
4849
from ads.common.decorator.runtime_dependency import runtime_dependency
4950
from .forecast_datasets import ForecastDatasets, ForecastOutput
@@ -408,24 +409,7 @@ def _save_report(
408409
"""Saves resulting reports to the given folder."""
409410
import datapane as dp
410411

411-
if self.spec.output_directory:
412-
output_dir = self.spec.output_directory.url
413-
# set the unique directory path as the requested path by the user
414-
unique_output_dir = output_dir
415-
else:
416-
output_dir = "results"
417-
418-
# If the directory exists, find the next unique directory name by appending an incrementing suffix
419-
counter = 1
420-
unique_output_dir = f"{output_dir}"
421-
while os.path.exists(unique_output_dir):
422-
unique_output_dir = f"{output_dir}_{counter}"
423-
counter += 1
424-
logger.warn(
425-
"Since the output directory was not specified, the output will be saved to {} directory.".format(
426-
unique_output_dir
427-
)
428-
)
412+
unique_output_dir = get_unique_report_dir(self.spec.output_directory)
429413

430414
if ObjectStorageDetails.is_oci_path(unique_output_dir):
431415
storage_options = default_signer()

0 commit comments

Comments
 (0)