def test_validator_1(data): x = data[0] y = data[1] class _Trainer(BaseRunner): def __init__(self): super().__init__() self.x_val = x self.y_val = y self.loss_type = 'train_loss' def predict(self, x_, y_): return x_, y_ val = Validator(metrics_func=regression_metrics, each_iteration=False) step_info = OrderedDict(train_loss=0, i_epoch=0) val.step_forward(trainer=_Trainer(), step_info=step_info) assert 'val_mae' not in step_info step_info = OrderedDict(train_loss=0, i_epoch=1) val.step_forward(trainer=_Trainer(), step_info=step_info) assert step_info['val_mae'] == regression_metrics(x, y)['mae'] assert set(step_info.keys()) == { 'i_epoch', 'val_mae', 'val_mse', 'val_rmse', 'val_r2', 'val_pearsonr', 'val_spearmanr', 'val_p_value', 'val_max_ae', 'train_loss' }
def test_validator_1(): x = np.random.randn(100) # input y = x + np.random.rand() * 0.001 # true values class _Trainer(BaseRunner): def __init__(self): super().__init__() self.x_val = x self.y_val = y self.loss_type = 'train_loss' def predict(self, x_, y_): return x_, y_ val = Validator('regress', each_iteration=False) step_info = OrderedDict(train_loss=0, i_epoch=0) val.step_forward(trainer=_Trainer(), step_info=step_info) # noqa assert 'val_mae' not in step_info step_info = OrderedDict(train_loss=0, i_epoch=1) val.step_forward(trainer=_Trainer(), step_info=step_info) # noqa assert step_info['val_mae'] == regression_metrics(y, x)['mae'] assert set(step_info.keys()) == { 'i_epoch', 'val_mae', 'val_mse', 'val_rmse', 'val_r2', 'val_pearsonr', 'val_spearmanr', 'val_p_value', 'val_max_ae', 'train_loss' }
def test_regression_metrics_1(data): x = data[0] y = data[1] noise = data[2] metric = regression_metrics(x, y) assert isinstance(metric, dict) assert set(metric.keys()) == {'mae', 'mse', 'rmse', 'r2', 'pearsonr', 'spearmanr', 'p_value', 'max_ae'} assert metric['mae'] < noise assert metric['mse'] < noise ** 2 assert metric['rmse'] < noise assert metric['r2'] > 0.9999 assert metric['pearsonr'] > 0.9999 assert metric['spearmanr'] > 0.9999 assert np.isclose(metric['p_value'], 0, 1e-4) assert metric['max_ae'] < noise assert metric['mae'] == regression_metrics(x.reshape(-1, 1), y)['mae'] assert metric['rmse'] == regression_metrics(x.reshape(-1, 1), y.reshape(-1, 1))['rmse'] assert metric['max_ae'] == regression_metrics(x, y.reshape(-1, 1))['max_ae']