Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions specparam/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from specparam.data.data import Data3D
from specparam.data.conversions import event_group_to_dataframe, dict_to_df
from specparam.data.utils import flatten_results_dict
from specparam.modutils.docs import (copy_doc_func_to_method, docs_get_section,
from specparam.modutils.docs import (copy_func_docstring_drop_first, docs_get_section,
replace_docstring_sections)
from specparam.reports.save import save_event_report
from specparam.reports.strings import gen_event_results_str
Expand Down Expand Up @@ -205,20 +205,20 @@ def print(self, info='results', concise=False):
super().print(info, concise=concise)


@copy_doc_func_to_method(plot_event_model)
@copy_func_docstring_drop_first(plot_event_model)
def plot(self, save_fig=False, file_name=None, file_path=None, **plot_kwargs):

plot_event_model(self, save_fig=save_fig, file_name=file_name,
file_path=file_path, **plot_kwargs)


@copy_doc_func_to_method(save_event_report)
@copy_func_docstring_drop_first(save_event_report)
def save_report(self, file_name, file_path=None, add_settings=True):

save_event_report(self, file_name, file_path, add_settings)


@copy_doc_func_to_method(save_event)
@copy_func_docstring_drop_first(save_event)
def save(self, file_name, file_path=None, append=False,
save_results=False, save_settings=False, save_data=False):

Expand Down
12 changes: 6 additions & 6 deletions specparam/models/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from specparam.io.files import load_jsonlines
from specparam.reports.save import save_group_report
from specparam.reports.strings import gen_group_results_str
from specparam.modutils.docs import (copy_doc_func_to_method,
from specparam.modutils.docs import (copy_func_docstring, copy_func_docstring_drop_first,
docs_get_section, replace_docstring_sections)
from specparam.utils.checks import check_inds

Expand Down Expand Up @@ -186,13 +186,13 @@ def report(self, freqs=None, power_spectra=None, freq_range=None, n_jobs=1,
self.print('results')


@copy_doc_func_to_method(plot_group_model)
@copy_func_docstring_drop_first(plot_group_model)
def plot(self, **plot_kwargs):

plot_group_model(self, **plot_kwargs)


@copy_doc_func_to_method(save_group)
@copy_func_docstring_drop_first(save_group)
def save(self, file_name, file_path=None, append=False,
save_results=False, save_settings=False, save_data=False):

Expand Down Expand Up @@ -243,13 +243,13 @@ def load(self, file_name, file_path=None):
self._reset_data_results(clear_spectrum=True, clear_results=True)


@copy_doc_func_to_method(Results2D.get_params)
@copy_func_docstring(Results2D.get_params)
def get_params(self, component, field=None):

return self.results.get_params(component, field)


@copy_doc_func_to_method(Results2D.get_metrics)
@copy_func_docstring(Results2D.get_metrics)
def get_metrics(self, category, measure=None):

return self.results.get_metrics(category, measure)
Expand Down Expand Up @@ -326,7 +326,7 @@ def get_group(self, inds):
return group


@copy_doc_func_to_method(save_group_report)
@copy_func_docstring_drop_first(save_group_report)
def save_report(self, file_name, file_path=None, add_settings=True):

save_group_report(self, file_name, file_path, add_settings)
Expand Down
14 changes: 7 additions & 7 deletions specparam/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
from specparam.reports.save import save_model_report
from specparam.reports.strings import gen_model_results_str
from specparam.modutils.errors import NoDataError, FitError
from specparam.modutils.docs import (copy_doc_func_to_method, replace_docstring_sections,
docs_get_section)
from specparam.modutils.docs import (copy_func_docstring, copy_func_docstring_drop_first,
docs_get_section, replace_docstring_sections)
from specparam.utils.checks import check_all_none
from specparam.io.files import load_json
from specparam.io.models import save_model
Expand Down Expand Up @@ -234,7 +234,7 @@ def print(self, info='results', concise=False):
super().print(info, concise=concise)


@copy_doc_func_to_method(plot_model)
@copy_func_docstring_drop_first(plot_model)
def plot(self, plot_peaks=None, plot_aperiodic=True, freqs=None, power_spectrum=None,
freq_range=None, plt_log=False, add_legend=True, ax=None, data_kwargs=None,
model_kwargs=None, aperiodic_kwargs=None, peak_kwargs=None, **plot_kwargs):
Expand All @@ -245,7 +245,7 @@ def plot(self, plot_peaks=None, plot_aperiodic=True, freqs=None, power_spectrum=
aperiodic_kwargs=aperiodic_kwargs, peak_kwargs=peak_kwargs, **plot_kwargs)


@copy_doc_func_to_method(save_model)
@copy_func_docstring_drop_first(save_model)
def save(self, file_name, file_path=None, append=False,
save_results=False, save_settings=False, save_data=False):

Expand Down Expand Up @@ -286,19 +286,19 @@ def load(self, file_name, file_path=None, regenerate=True):
self.results._regenerate_model(self.data.freqs)


@copy_doc_func_to_method(Results.get_params)
@copy_func_docstring(Results.get_params)
def get_params(self, component, field=None):

return self.results.get_params(component, field)


@copy_doc_func_to_method(Results.get_metrics)
@copy_func_docstring(Results.get_metrics)
def get_metrics(self, category, measure=None):

return self.results.get_metrics(category, measure)


@copy_doc_func_to_method(save_model_report)
@copy_func_docstring_drop_first(save_model_report)
def save_report(self, file_name, file_path=None, add_settings=True, **plot_kwargs):

save_model_report(self, file_name, file_path, add_settings, **plot_kwargs)
Expand Down
8 changes: 4 additions & 4 deletions specparam/models/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from specparam.plts.time import plot_time_model
from specparam.reports.save import save_time_report
from specparam.reports.strings import gen_time_results_str
from specparam.modutils.docs import (copy_doc_func_to_method, docs_get_section,
from specparam.modutils.docs import (copy_func_docstring_drop_first, docs_get_section,
replace_docstring_sections)
from specparam.utils.checks import check_inds

Expand Down Expand Up @@ -157,7 +157,7 @@ def print(self, info='results', concise=False, report_type='time'):
super().print(info, concise=concise)


@copy_doc_func_to_method(plot_time_model)
@copy_func_docstring_drop_first(plot_time_model)
def plot(self, plot_type='time', save_fig=False, file_name=None, file_path=None, **plot_kwargs):

if plot_type == 'time':
Expand All @@ -167,14 +167,14 @@ def plot(self, plot_type='time', save_fig=False, file_name=None, file_path=None,
super().plot(save_fig=save_fig, file_name=file_name, file_path=file_path, **plot_kwargs)


@copy_doc_func_to_method(save_time)
@copy_func_docstring_drop_first(save_time)
def save(self, file_name, file_path=None, append=False,
save_results=False, save_settings=False, save_data=False):

save_time(self, file_name, file_path, append, save_results, save_settings, save_data)


@copy_doc_func_to_method(save_time_report)
@copy_func_docstring_drop_first(save_time_report)
def save_report(self, file_name, file_path=None, add_settings=True):

save_time_report(self, file_name, file_path, add_settings)
Expand Down
27 changes: 25 additions & 2 deletions specparam/modutils/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,31 @@ def docs_add_section(docstring, section):
return new_docstring


def copy_doc_func_to_method(source):
"""Decorator that copies method docstring from function, dropping first parameter.
def copy_func_docstring(source):
"""Decorator that copies docstring from source.

Parameters
----------
source : function
Source function to copy docstring from.

Returns
-------
wrapper : function
The decorated function, with updated docs.
"""

def wrapper(func):

func.__doc__ = deepcopy(source.__doc__)

return func

return wrapper


def copy_func_docstring_drop_first(source):
"""Decorator that copies docstring from source, dropping first parameter.

Parameters
----------
Expand Down
6 changes: 3 additions & 3 deletions specparam/results/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from specparam.metrics.metrics import Metrics
from specparam.utils.checks import check_inds
from specparam.modutils.errors import NoModelError
from specparam.modutils.docs import (copy_doc_func_to_method, docs_get_section,
from specparam.modutils.docs import (copy_func_docstring, docs_get_section,
replace_docstring_sections)
from specparam.data.stores import FitResults
from specparam.data.conversions import group_to_dict, event_group_to_dict
Expand Down Expand Up @@ -210,7 +210,7 @@ def get_params(self, component, field=None, version=None):
return getattr(self.params, component).get_params(version, field)


@copy_doc_func_to_method(Metrics.get_metrics)
@copy_func_docstring(Metrics.get_metrics)
def get_metrics(self, category, measure=None):

return self.metrics.get_metrics(category, measure)
Expand Down Expand Up @@ -419,7 +419,7 @@ def get_params(self, component, field=None):
return get_group_params(self.group_results, self.modes, component, field)


@copy_doc_func_to_method(Metrics.get_metrics)
@copy_func_docstring(Metrics.get_metrics)
def get_metrics(self, category, measure=None):

return get_group_metrics(self.group_results, category, measure)
Expand Down
19 changes: 16 additions & 3 deletions specparam/tests/modutils/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,35 @@ def test_docs_add_section(tdocstring):
assert '%' not in new_docstring
assert 'new note' in new_docstring

def test_copy_doc_func_to_method(tdocstring):
def test_copy_func_docstring(tdocstring):

def tfunc(): pass
tfunc.__doc__ = tdocstring

@copy_func_docstring(tfunc)
def tfunc_out():
pass

assert tfunc_out.__doc__

for el in ['first', 'second']:
assert el in tfunc_out.__doc__

def test_copy_func_docstring_drop_first(tdocstring):

def tfunc(): pass
tfunc.__doc__ = tdocstring

class tobj():

@copy_doc_func_to_method(tfunc)
@copy_func_docstring_drop_first(tfunc)
def tmethod():
pass

assert tobj.tmethod.__doc__
assert 'first' not in tobj.tmethod.__doc__
assert 'second' in tobj.tmethod.__doc__


def test_copy_doc_class(tdocstring):

class tobj1():
Expand Down
Loading