Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docstrings for the DataFrameDimension related methods #441

Merged
merged 5 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 10 additions & 5 deletions nixio/data_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,23 @@ def append_range_dimension(self, ticks, label=None, unit=None):
self.force_updated_at()
return rdim

def append_data_frame_dimension(self, data_frame, column=None):
def append_data_frame_dimension(self, data_frame, column_idx=None):
"""
Append a new DataFrameDimension to the list of existing dimension
descriptors.
:param data_frame:
:param column:
:return:
:param data_frame: The referenced DataFrame
:type data_frame: nix.DataFrame
:param column_idx: Index of the referenced column of the DataFrame.
The default column determines the default label, ticks, and unit of this Dimension.
:type column_idx: int or None

:returns: Thew newly created DataFrameDimension.
:rtype: DataFrameDimension
"""
dimgroup = self._h5group.open_group("dimensions")
index = len(dimgroup) + 1
dfdim = DataFrameDimension._create_new(dimgroup, index,
data_frame, column)
data_frame, column_idx)
if self._parent._parent.time_auto_update:
self.force_updated_at()
return dfdim
Expand Down
55 changes: 43 additions & 12 deletions nixio/dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,43 +333,74 @@ def _create_new(cls, parent, index, data_frame, column):
"""
newdim = super(DataFrameDimension, cls)._create_new(parent, index)
newdim.data_frame = data_frame
newdim.column = column
newdim.column_idx = column
newdim._dimension_type = DimensionType.DataFrame
return newdim

def get_unit(self, index=None):
"""
Get the unit of the Dimension. If an index is specified,
it will return the unit of the column in the referenced DataFrame at that index.
:param index: Index of the needed column
:type index: int

:return: Unit of the specified column
:rtype: str or None
"""
if index is None:
if self.column is None:
if self.column_idx is None:
raise ValueError("No default column index is set "
"for this Dimension. Please supply one")
else:
idx = self.column
idx = self.column_idx
else:
idx = index
unit = None
if self.data_frame.units is not None:

unit = self.data_frame.units[idx]
return unit

def get_ticks(self, index=None):
"""
Get the ticks of the Dimension from the referenced DataFrame.
If an index is specified, it will return the values of the column
in the referenced DataFrame at that index.
:param index: Index of the needed column
:type index: int

:return: values in the specified column
:rtype: ndarray
"""
if index is None:
if self.column is None:
if self.column_idx is None:
raise ValueError("No default column index is set "
"for this Dimension. Please supply one")
else:
idx = self.column
idx = self.column_idx
else:
idx = index
df = self.data_frame
ticks = df[df.column_names[idx]]
return ticks

def get_label(self, index=None):
"""
```suggestion
achilleas-k marked this conversation as resolved.
Show resolved Hide resolved
Get the label of the Dimension. If an index is specified,
it will return the name of the column in the referenced
DataFrame at that index.
:param index: Index of the referred column
:type index: int or None

:return: the header of the specified column or the name of DataFrame if index is None
:rtype: str
"""
if index is None:
if self.column is None:
if self.column_idx is None:
label = self.data_frame.name
else:
label = self.data_frame.column_names[self.column]
label = self.data_frame.column_names[self.column_idx]
else:
label = self.data_frame.column_names[index]
return label
Expand All @@ -386,10 +417,10 @@ def data_frame(self, df):
self._h5group.create_link(df, "data_frame")

@property
def column(self):
colidx = self._h5group.get_attr("column")
def column_idx(self):
colidx = self._h5group.get_attr("column_idx")
return colidx

@column.setter
def column(self, col):
self._h5group.set_attr("column", col)
@column_idx.setter
def column_idx(self, col):
self._h5group.set_attr("column_idx", col)
2 changes: 1 addition & 1 deletion nixio/test/test_dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def test_df_dim(self):
col_dict=di, data=arr)
df.units = unit
dfdim1 = self.array.append_data_frame_dimension(df)
dfdim2 = self.array.append_data_frame_dimension(df, column=1)
dfdim2 = self.array.append_data_frame_dimension(df, column_idx=1)
self.assertRaises(ValueError,lambda: dfdim1.get_ticks())
for ti, tu in enumerate(arr):
for idx, item in enumerate(tu):
Expand Down