From f5ab249350663adae57e0d1b664da3a06b9a80f6 Mon Sep 17 00:00:00 2001 From: Tom Donoghue Date: Mon, 9 Mar 2026 20:21:28 +0000 Subject: [PATCH 1/4] add space input to Metric --- specparam/metrics/definitions.py | 2 +- specparam/metrics/metric.py | 10 ++++++++-- specparam/tests/metrics/test_metric.py | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/specparam/metrics/definitions.py b/specparam/metrics/definitions.py index e49c707e..d9632196 100644 --- a/specparam/metrics/definitions.py +++ b/specparam/metrics/definitions.py @@ -22,7 +22,7 @@ category='error', measure='mse', description='Mean squared error of the model fit to the data.', - func=compute_mean_squared_error + func=compute_mean_squared_error, ) error_rmse = Metric( diff --git a/specparam/metrics/metric.py b/specparam/metrics/metric.py index 122b4091..c3a8e1ea 100644 --- a/specparam/metrics/metric.py +++ b/specparam/metrics/metric.py @@ -18,6 +18,8 @@ class Metric(): Description of the metric. func : callable The function that computes the metric. + space : {'log', 'linear'} + Spacing of the data & model to use for metric evaluation. kwargs : dictionary Additional keyword argument to compute the metric. Each key should be the name of the additional argument. @@ -25,13 +27,14 @@ class Metric(): and returns the desired parameter / computed value. """ - def __init__(self, category, measure, description, func, kwargs=None): + def __init__(self, category, measure, description, func, space='log', kwargs=None): """Initialize metric.""" self.category = category self.measure = measure self.description = description self.func = func + self.space = space self.result = np.nan self.kwargs = {} if not kwargs else kwargs @@ -76,7 +79,10 @@ def compute_metric(self, data, results): for key, lfunc in self.kwargs.items(): kwargs[key] = lfunc(data, results) - self.result = self.func(data.power_spectrum, results.model.modeled_spectrum, **kwargs) + self.result = self.func( + data.get_data('full', space=self.space), + results.model.get_component('full', space=self.space), + **kwargs) def reset(self): diff --git a/specparam/tests/metrics/test_metric.py b/specparam/tests/metrics/test_metric.py index 03d2865e..7e96a394 100644 --- a/specparam/tests/metrics/test_metric.py +++ b/specparam/tests/metrics/test_metric.py @@ -20,7 +20,7 @@ def test_metric(tfm): def test_metric_kwargs(tfm): metric = Metric('gof', 'ar2', 'Description.', compute_adj_r_squared, - {'n_params' : lambda data, results: \ + kwargs={'n_params' : lambda data, results: \ results.params.periodic.params.size + results.params.aperiodic.params.size}) assert isinstance(metric, Metric) From 66e07c10ddf72c1872bbb48d639f31d39721d6d9 Mon Sep 17 00:00:00 2001 From: Tom Donoghue Date: Mon, 9 Mar 2026 20:31:40 +0000 Subject: [PATCH 2/4] add linear metrics --- specparam/metrics/definitions.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/specparam/metrics/definitions.py b/specparam/metrics/definitions.py index d9632196..45387aff 100644 --- a/specparam/metrics/definitions.py +++ b/specparam/metrics/definitions.py @@ -16,6 +16,7 @@ measure='mae', description='Mean absolute error of the model fit to the data.', func=compute_mean_abs_error, + space='log', ) error_mse = Metric( @@ -23,6 +24,7 @@ measure='mse', description='Mean squared error of the model fit to the data.', func=compute_mean_squared_error, + space='log', ) error_rmse = Metric( @@ -30,6 +32,7 @@ measure='rmse', description='Root mean squared error of the model fit to the data.', func=compute_root_mean_squared_error, + space='log', ) error_medae = Metric( @@ -37,6 +40,15 @@ measure='medae', description='Median absolute error of the model fit to the data.', func=compute_median_abs_error, + space='log', +) + +error_mae_lin = Metric( + category='error', + measure='maelin', + description='Mean absolute error of the model fit to the data, in linear space.', + func=compute_mean_abs_error, + space='linear', ) ################################################################################################### @@ -47,6 +59,15 @@ measure='rsquared', description='R-squared between the model fit and the data.', func=compute_r_squared, + space='log', +) + +gof_rsquared_lin = Metric( + category='gof', + measure='rsquaredlin', + description='R-squared between the model fit and the data, in linear space.', + func=compute_r_squared, + space='linear', ) gof_adjrsquared = Metric( @@ -56,6 +77,7 @@ func=compute_adj_r_squared, kwargs={'n_params' : lambda data, results: \ results.params.periodic.params.size + results.params.aperiodic.params.size}, + space='log', ) ################################################################################################### @@ -63,16 +85,22 @@ METRICS = { - # Available error metrics + # Error metrics - log spacing 'error_mae' : error_mae, 'error_mse' : error_mse, 'error_rmse' : error_rmse, 'error_medae' : error_medae, - # Available GOF / r-squared metrics + # Error metrics - linear spacing + 'error_mae_lin' : error_mae_lin, + + # GOF / r-squared metrics - log spacing 'gof_rsquared' : gof_rsquared, 'gof_adjrsquared' : gof_adjrsquared, + # GOF / r-squared metrics - linear spacing + 'gof_rsquared_lin' : gof_rsquared_lin, + } From 6fb385d06264a722c46f60187a1d2c5531fd8de4 Mon Sep 17 00:00:00 2001 From: Tom Donoghue Date: Mon, 9 Mar 2026 20:43:53 +0000 Subject: [PATCH 3/4] tweak new linear metric names --- specparam/metrics/definitions.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/specparam/metrics/definitions.py b/specparam/metrics/definitions.py index 45387aff..1aeb01fe 100644 --- a/specparam/metrics/definitions.py +++ b/specparam/metrics/definitions.py @@ -43,7 +43,7 @@ space='log', ) -error_mae_lin = Metric( +error_maelin = Metric( category='error', measure='maelin', description='Mean absolute error of the model fit to the data, in linear space.', @@ -62,7 +62,7 @@ space='log', ) -gof_rsquared_lin = Metric( +gof_rsquaredlin = Metric( category='gof', measure='rsquaredlin', description='R-squared between the model fit and the data, in linear space.', @@ -92,14 +92,14 @@ 'error_medae' : error_medae, # Error metrics - linear spacing - 'error_mae_lin' : error_mae_lin, + 'error_maelin' : error_maelin, # GOF / r-squared metrics - log spacing 'gof_rsquared' : gof_rsquared, 'gof_adjrsquared' : gof_adjrsquared, # GOF / r-squared metrics - linear spacing - 'gof_rsquared_lin' : gof_rsquared_lin, + 'gof_rsquaredlin' : gof_rsquaredlin, } From 8a82102ee0bab727e40a33bbbb473d8bebe456ca Mon Sep 17 00:00:00 2001 From: Tom Donoghue Date: Fri, 17 Apr 2026 01:09:34 +0100 Subject: [PATCH 4/4] fix linear definition collections --- specparam/metrics/definitions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/specparam/metrics/definitions.py b/specparam/metrics/definitions.py index 863e2627..c03a5dfb 100644 --- a/specparam/metrics/definitions.py +++ b/specparam/metrics/definitions.py @@ -59,7 +59,7 @@ 'medae' : error_medae, # linear spacing - 'error_maelin' : error_maelin, + 'maelin' : error_maelin, } ################################################################################################### @@ -99,7 +99,7 @@ 'adjrsquared' : gof_adjrsquared, # linear spacing - 'gof_rsquaredlin' : gof_rsquaredlin, + 'rsquaredlin' : gof_rsquaredlin, } ###################################################################################################