Skip to content

Commit 2794cb2

Browse files
committed
Updated the doc strings
1 parent 729e9a2 commit 2794cb2

84 files changed

Lines changed: 1687 additions & 1590 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

hed/errors/error_reporter.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
""""
1+
"""
22
Support functions for reporting validation errors.
33
44
You can scope the formatted errors with calls to push_error_context and pop_error_context.
5+
The standard context keys are:
6+
7+
CUSTOM_TITLE = 'ec_title'
8+
FILE_NAME = 'ec_filename'
9+
SIDECAR_COLUMN_NAME = 'ec_sidecarColumnName'
10+
SIDECAR_KEY_NAME = 'ec_sidecarKeyName'
11+
ROW = 'ec_row'
12+
COLUMN = 'ec_column'
13+
LINE = "ec_line"
14+
HED_STRING = 'ec_HedString'
15+
SCHEMA_SECTION = 'ec_section'
16+
SCHEMA_TAG = 'ec_schema_tag'
17+
SCHEMA_ATTRIBUTE = 'ec_attribute'
18+
19+
The standard error keys are:
20+
'code' -- the standard HED error code
21+
'message' -- the error message
22+
'severity' -- 1 is error and 10 is warning.
23+
'url' -- the web page in the HED Specification corresponding to the error (has explanation of how can occur).
24+
'source_tag' -- the source tag that caused the error if application.
25+
526
"""
627
from __future__ import annotations
728
from functools import wraps
@@ -490,6 +511,7 @@ def get_printable_issue_string(issues, title=None, severity=None, skip_filename=
490511
skip_filename (bool): If True, don't add the filename context to the printable string.
491512
add_link (bool): Add a link at the end of message to the appropriate error if True
492513
show_details (bool): If True, show details about the issues.
514+
493515
Returns:
494516
str: A string containing printable version of the issues or ''.
495517
@@ -531,13 +553,25 @@ def get_printable_issue_string_html(issues, title=None, severity=None, skip_file
531553
return et.tostring(root_element, encoding='unicode')
532554

533555
def iter_errors(issues):
534-
""" An iterator over issues represented as flat dictionaries.
556+
""" An iterator over issues that flattens the context into each issue dictionary.
557+
558+
This function takes a list of issues and transforms each one into a "flat" dictionary.
559+
A flat dictionary contains all the information about a single error, including its context,
560+
in a single, non-nested dictionary. This is useful for reporting or logging errors
561+
in a simple, straightforward format.
562+
563+
For example, context information like file name or row number, which might be stored
564+
in a nested structure or separate from the issue dictionary, is merged into the
565+
top level of the dictionary.
566+
567+
It also adds a 'url' key with a link to the documentation for known HED error codes.
568+
Any complex objects like HedTag or HedString are converted to their string representations.
535569
536570
Parameters:
537-
issues (list): Issues to iterator over.
571+
issues (list): A list of issue dictionaries to iterate over.
538572
539573
Yields:
540-
dict: Represents the information in a single error.
574+
dict: A flattened dictionary representing a single error.
541575
"""
542576

543577
for issue in issues:
@@ -563,7 +597,7 @@ def create_doc_link(error_code):
563597
error_code(str): A HED error code.
564598
565599
Returns:
566-
url(str or None): The URL if it's a valid code.
600+
Union[str, None]: The URL if it's a valid code.
567601
"""
568602
if error_code in known_error_codes["hed_validation_errors"] \
569603
or error_code in known_error_codes["schema_validation_errors"]:
@@ -684,7 +718,7 @@ def _get_error_prefix(single_issue):
684718
single_issue(dict): A single issue object.
685719
686720
Returns:
687-
error_prefix(str): the prefix to use.
721+
str: the prefix to use.
688722
"""
689723
severity = single_issue.get('severity', ErrorSeverity.ERROR)
690724
error_code = single_issue['code']

hed/models/base_input.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ def __init__(self, file, file_type=None, worksheet_name=None, has_column_names=T
3636
name (str or None): Optional field for how this file will report errors.
3737
allow_blank_names(bool): If True, column names can be blank
3838
39-
:raises HedFileError:
39+
Raises:
40+
HedFileError: For various issues.
41+
42+
Notes: Reasons for raising HedFileError include:
4043
- file is blank.
4144
- An invalid dataframe was passed with size 0.
4245
- An invalid extension was provided.
@@ -96,7 +99,8 @@ def dataframe_a(self) ->pd.DataFrame:
9699
"""Return the assembled dataframe Probably a placeholder name.
97100
98101
Returns:
99-
pd.Dataframe: the assembled dataframe"""
102+
pd.Dataframe: the assembled dataframe
103+
"""
100104
return self.assemble()
101105

102106
@property
@@ -114,7 +118,7 @@ def series_filtered(self) -> Union[pd.Series, None]:
114118
"""Return the assembled dataframe as a series, with rows that have the same onset combined.
115119
116120
Returns:
117-
Union[pd.Series, None] the assembled dataframe with columns merged, and the rows filtered together.
121+
Union[pd.Series, None]: the assembled dataframe with columns merged, and the rows filtered together.
118122
"""
119123
if self.onsets is not None:
120124
return filter_series_by_onset(self.series_a, self.onsets)
@@ -210,11 +214,9 @@ def to_excel(self, file):
210214
Parameters:
211215
file (str or file-like): Location to save this base input.
212216
213-
:raises ValueError:
214-
- If empty file object was passed.
215-
216-
:raises OSError:
217-
- Cannot open the indicated file.
217+
Raises:
218+
ValueError: If empty file object was passed.
219+
OSError: If the file cannot be opened.
218220
"""
219221
if not file:
220222
raise ValueError("Empty file name or object passed in to BaseInput.save.")
@@ -242,11 +244,12 @@ def to_csv(self, file=None):
242244
243245
Parameters:
244246
file (str, file-like, or None): Location to save this file. If None, return as string.
247+
245248
Returns:
246249
None or str: None if file is given or the contents as a str if file is None.
247250
248-
:raises OSError:
249-
- Cannot open the indicated file.
251+
Raises:
252+
OSError: If the file cannot be opened.
250253
"""
251254
dataframe = self._dataframe
252255
csv_string_if_filename_none = dataframe.to_csv(file, sep='\t', index=False, header=self._has_column_names)
@@ -259,7 +262,7 @@ def columns(self):
259262
Empty if no column names.
260263
261264
Returns:
262-
columns(list): The column names.
265+
list: The column names.
263266
"""
264267
columns = []
265268
if self._dataframe is not None and self._has_column_names:
@@ -288,14 +291,10 @@ def set_cell(self, row_number, column_number, new_string_obj, tag_form="short_ta
288291
Notes:
289292
Any attribute of a HedTag that returns a string is a valid value of tag_form.
290293
291-
:raises ValueError:
292-
- There is not a loaded dataframe.
293-
294-
:raises KeyError:
295-
- The indicated row/column does not exist.
296-
297-
:raises AttributeError:
298-
- The indicated tag_form is not an attribute of HedTag.
294+
Raises:
295+
ValueError: If there is not a loaded dataframe.
296+
KeyError: If the indicated row/column does not exist.
297+
AttributeError: If the indicated tag_form is not an attribute of HedTag.
299298
"""
300299
if self._dataframe is None:
301300
raise ValueError("No data frame loaded")
@@ -315,8 +314,8 @@ def get_worksheet(self, worksheet_name=None) -> Union[openpyxl.workbook.Workbook
315314
Notes:
316315
If None, returns the first worksheet.
317316
318-
:raises KeyError:
319-
- The specified worksheet name does not exist.
317+
Raises:
318+
KeyError: If the specified worksheet name does not exist.
320319
"""
321320
if worksheet_name and self._loaded_workbook:
322321
# return self._loaded_workbook.get_sheet_by_name(worksheet_name)
@@ -380,6 +379,7 @@ def assemble(self, mapper=None, skip_curly_braces=False) ->pd.DataFrame:
380379
Parameters:
381380
mapper (ColumnMapper or None): Generally pass none here unless you want special behavior.
382381
skip_curly_braces (bool): If True, don't plug in curly brace values into columns.
382+
383383
Returns:
384384
pd.Dataframe: The assembled dataframe.
385385
"""

hed/models/column_mapper.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,14 @@ def _set_sidecar(self, sidecar):
151151
Parameters:
152152
sidecar (Sidecar or None): The sidecar to use.
153153
154-
:raises ValueError:
155-
- A sidecar was previously set.
154+
Raises:
155+
ValueError: A sidecar was previously set.
156+
156157
"""
157158
if self._sidecar:
158159
raise ValueError("Trying to set a second sidecar on a column mapper.")
159160
if not sidecar:
160-
return None
161+
return
161162

162163
self._sidecar = sidecar
163164

hed/models/def_expand_gather.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ def add_def(self, def_tag, def_expand_group):
5151
def resolve_definition(self):
5252
""" Try to resolve the definition based on the information available.
5353
54-
Returns: boolean - True if successfully resolved and False if it can't be resolved from information available.
54+
Returns:
55+
boolean: True if successfully resolved and False if it can't be resolved from information available.
5556
56-
Raises: ValueError - If the actual_contents conflict.
57+
Raises:
58+
ValueError: If the actual_contents conflict.
5759
5860
If the definition has already been resolved, this rechecks based on the information.
5961

hed/models/definition_dict.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def __init__(self, def_dicts=None, hed_schema=None):
2020
a single string whose definitions should be added.
2121
hed_schema (HedSchema or None): Required if passing strings or lists of strings, unused otherwise.
2222
23-
:raises TypeError:
24-
- Bad type passed as def_dicts.
23+
Raises:
24+
TypeError: Bad type passed as def_dicts.
2525
"""
2626

2727
self.defs = {}
@@ -41,8 +41,8 @@ def add_definitions(self, def_dicts, hed_schema=None):
4141
Note - str or list of strings will parse the strings using the hed_schema.
4242
Note - You can mix and match types, eg [DefinitionDict, str, list of str] would be valid input.
4343
44-
:raises TypeError:
45-
- Bad type passed as def_dicts.
44+
Raises:
45+
TypeError: Bad type passed as def_dicts.
4646
"""
4747
if not isinstance(def_dicts, list):
4848
def_dicts = [def_dicts]
@@ -181,8 +181,9 @@ def _validate_placeholders(def_tag_name, group, def_takes_value, error_handler):
181181
def_takes_value (bool): True if the definition takes a value (should have #).
182182
error_handler (ErrorHandler or None): Error context used to identify where definitions are found.
183183
184-
Returns:
185-
list: List of issues encountered in checking for definitions. Each issue is a dictionary.
184+
Returns:
185+
list: List of issues encountered in checking for definitions. Each issue is a dictionary.
186+
186187
"""
187188
new_issues = []
188189
placeholder_tags = []
@@ -232,8 +233,9 @@ def _find_group(definition_tag, group, error_handler):
232233
group (HedGroup): The entire definition group include the Definition tag.
233234
error_handler (ErrorHandler or None): Error context used to identify where definitions are found.
234235
235-
Returns:
236-
list: List of issues encountered in checking for definitions. Each issue is a dictionary.
236+
Returns:
237+
list: List of issues encountered in checking for definitions. Each issue is a dictionary.
238+
237239
"""
238240
# initial validation
239241
groups = group.groups()
@@ -306,8 +308,7 @@ def _get_definition_contents(self, def_tag):
306308
def_tag (HedTag): Source HED tag that may be a Def or Def-expand tag.
307309
308310
Returns:
309-
def_contents: HedGroup
310-
The contents to replace the previous def-tag with.
311+
HedGroup: The contents to replace the previous def-tag with.
311312
"""
312313
tag_label, _, placeholder = def_tag.extension.partition('/')
313314

@@ -328,7 +329,7 @@ def get_as_strings(def_dict) -> dict[str, str]:
328329
def_dict (dict): A dict of definitions
329330
330331
Returns:
331-
dict[str,str]: definition name and contents
332+
dict[str,str]: Definition name and contents
332333
"""
333334
if isinstance(def_dict, DefinitionDict):
334335
def_dict = def_dict.defs

hed/models/definition_entry.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ def get_definition(self, replace_tag, placeholder_value=None, return_copy_of_tag
3737
return_copy_of_tag(bool): Set to True for validation.
3838
3939
Returns:
40-
HedGroup: The contents of this definition(including the def tag itself).
40+
Union[HedGroup, None]: The contents of this definition(including the def tag itself).
4141
42-
:raises ValueError:
43-
- Something internally went wrong with finding the placeholder tag. This should not be possible.
42+
Raises:
43+
ValueError: Something internally went wrong with finding the placeholder tag. This should not be possible.
4444
"""
4545
if self.takes_value == (not placeholder_value):
4646
return None

hed/models/df_util.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def sort_dataframe_by_onsets(df):
112112
df(pd.Dataframe): Dataframe to sort.
113113
114114
Returns:
115-
The sorted dataframe, or the original dataframe if it didn't have an onset column.
115+
pd.DataFrame: The sorted dataframe, or the original dataframe if it didn't have an onset column.
116116
"""
117117
if "onset" in df.columns:
118118
# Create a copy and sort by onsets as floats(if needed), but continue to keep the string version.
@@ -177,7 +177,7 @@ def _handle_curly_braces_refs(df, refs, column_names):
177177
column_names(list): the columns we are interested in(should include all ref columns)
178178
179179
Returns:
180-
modified_df(pd.DataFrame): The modified dataframe with refs replaced
180+
pd.DataFrame: The modified dataframe with refs replaced
181181
"""
182182
# Filter out columns and refs that don't exist.
183183
refs_new = [ref for ref in refs if ref in column_names]
@@ -221,7 +221,7 @@ def split_delay_tags(series, hed_schema, onsets):
221221
onsets(pd.Series or None)
222222
223223
Returns:
224-
sorted_df(pd.Dataframe or None): If we had onsets, a dataframe with 3 columns
224+
Union[pd.Dataframe, None]: If we had onsets, a dataframe with 3 columns
225225
"HED": The HED strings(still str)
226226
"onset": the updated onsets
227227
"original_index": the original source line. Multiple lines can have the same original source line.
@@ -260,10 +260,11 @@ def filter_series_by_onset(series, onsets):
260260
"""Return the series, with rows that have the same onset combined.
261261
262262
Parameters:
263-
series(pd.Series or pd.Dataframe): the series to filter. If dataframe, it filters the "HED" column
264-
onsets(pd.Series): the onset column to filter by
263+
series(pd.Series or pd.Dataframe): The series to filter. If dataframe, it filters the "HED" column.
264+
onsets(pd.Series): The onset column to filter by.
265+
265266
Returns:
266-
Series or Dataframe: the series with rows filtered together.
267+
Union[Series, Dataframe]: the series with rows filtered together.
267268
"""
268269

269270
indexed_dict = _indexed_dict_from_onsets(pd.to_numeric(onsets, errors='coerce'))

0 commit comments

Comments
 (0)