Skip to content

Commit

Permalink
Fixed the usage of the regression data class.
Browse files Browse the repository at this point in the history
  • Loading branch information
airvzxf committed Apr 19, 2024
1 parent 945d86c commit ab98e61
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 272 deletions.
188 changes: 48 additions & 140 deletions benchmarks/benchmark_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)
from tardis.simulation import Simulation
from tardis.tests.fixtures.atom_data import DEFAULT_ATOM_DATA_UUID
from tardis.tests.fixtures.regression_data import RegressionData


class BenchmarkBase:
Expand Down Expand Up @@ -198,157 +199,64 @@ def config_verysimple(self):
f"{self.example_configuration_dir}/tardis_configv1_verysimple.yml"
)

class RegressionData:
def __init__(self) -> None:
# TODO: This route is fixed but needs to get from the arguments given in the command line.
# /app/tardis-regression-data
self.regression_data_path = "/app/tardis-regression-data"
# TODO: Parameter `--generate-reference` is set in the command line.
# It is fixed but needs to get from the arguments.
self.enable_generate_reference = False
self.fname = f"{self.fname_prefix}.UNKNOWN_FORMAT"
class CustomPyTestRequest:
def __init__(
self,
tardis_regression_data_path: str,
node_name: str,
node_module_name: str,
regression_data_dir: str,
):
self.tardis_regression_data_path = tardis_regression_data_path
self.node_name = node_name
self.node_module_name = node_module_name
self.regression_data_dir = regression_data_dir

@property
def module_name(self):
return self.__name__
def config(self):
class SubClass:
@staticmethod
def getoption(option):
if option == '--tardis-regression-data':
return self.tardis_regression_data_path
return None

@property
def test_name(self):
return self.name
return SubClass()

@property
def fname_prefix(self):
double_under = re.compile(r"[:\[\]{}]")
no_space = re.compile(r'[,"\']') # quotes and commas
def node(self):
class SubClass:
def __init__(self, parent):
self.parent = parent

name = double_under.sub("__", self.test_name)
name = no_space.sub("", name)
return name
@property
def name(self):
return self.parent.node_name

@property
def relative_regression_data_dir(self):
relative_data_dir = Path(self.module_name.replace(".", "/"))
if self.cls is not None:
relative_data_dir /= HDFWriterMixin.convert_to_snake_case(
self.cls.__name__
)
return relative_data_dir
@property
def module(self):
class SubSubClass:
def __init__(self, parent):
self.parent = parent

@property
def __name__(self):
return self.parent.node_module_name
return SubSubClass(self.parent)

return SubClass(self)

@property
def absolute_regression_data_dir(self):
return f"{self.regression_data_path}/{self.relative_regression_data_dir}"
def cls(self):
return None

@property
def fpath(self):
return f"{self.absolute_regression_data_dir}/{self.fname}"

def sync_dataframe(self, data, key="data"):
"""
Synchronizes the dataframe with the regression data.
Parameters
----------
data : DataFrame
The dataframe to be synchronized.
key : str, optional
The key to use for storing the dataframe in the regression data file. Defaults to "data".
Returns
-------
DataFrame or None
The synchronized dataframe if `enable_generate_reference` is `False`, otherwise `None`.
"""
self.fname = f"{self.fname_prefix}.h5"
if self.enable_generate_reference:
Path(self.fpath).parent.mkdir(parents=True, exist_ok=True)
data.to_hdf(
self.fpath,
key=key,
)
raise Exception("Skipping test to generate reference data")
else:
return pd.read_hdf(self.fpath, key=key)

def sync_ndarray(self, data):
"""
Synchronizes the ndarray with the regression data.
Parameters
----------
data : ndarray
The ndarray to be synchronized.
Returns
-------
ndarray or None
The synchronized ndarray if `enable_generate_reference` is `False`, otherwise `None`.
"""
self.fname = f"{self.fname_prefix}.npy"
if self.enable_generate_reference:
Path(self.fpath).parent.mkdir(parents=True, exist_ok=True)
Path(self.fpath).parent.mkdir(parents=True, exist_ok=True)
np.save(self.fpath, data)
raise Exception("Skipping test to generate reference data")
else:
return np.load(self.fpath)

def sync_str(self, data):
"""
Synchronizes the string with the regression data.
Parameters
----------
data : str
The string to be synchronized.
Returns
-------
str or None
The synchronized string if `enable_generate_reference` is `False`, otherwise `None`.
"""
self.fname = f"{self.fname_prefix}.txt"
if self.enable_generate_reference:
Path(self.fpath).parent.mkdir(parents=True, exist_ok=True)
with Path(self.fpath).open("w") as fh:
fh.write(data)
raise Exception(
f"Skipping test to generate regression_data {self.fpath} data"
)
else:
with Path(self.fpath).open("r") as fh:
return fh.read()

def sync_hdf_store(self, tardis_module, update_fname=True):
"""
Synchronizes the HDF store with the regression data.
Parameters
----------
tardis_module : object
The module to be synchronized.
update_fname : bool, optional
Whether to update the file name. Defaults to True.
Returns
-------
HDFStore or None
The synchronized HDF store if `enable_generate_reference` is `False`, otherwise `None`.
"""
if update_fname:
self.fname = f"{self.fname_prefix}.h5"
if self.enable_generate_reference:
Path(self.fpath).parent.mkdir(parents=True, exist_ok=True)
with pd.HDFStore(self.fpath, mode="w") as store:
tardis_module.to_hdf(store, overwrite=True)
raise Exception(
f"Skipping test to generate regression_data {self.fpath} data"
)
else:
return pd.HDFStore(self.fpath, mode="r")

@property
def regression_data(self):
return self.RegressionData()
def relative_regression_data_dir(self):
return self.regression_data_dir

@staticmethod
def regression_data(request: CustomPyTestRequest):
return RegressionData(request)

@property
def packet(self):
Expand Down
Loading

0 comments on commit ab98e61

Please sign in to comment.