Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 8, 2024
1 parent b6cd6fa commit f9d9340
Showing 1 changed file with 29 additions and 97 deletions.
126 changes: 29 additions & 97 deletions src/biocframe/BiocFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ def _validate_rows(
if row_names is not None:
if len(row_names) != number_of_rows:
raise ValueError(
"Length of `row_names` and `number_of_rows` do not match, "
f"{len(row_names)} != {number_of_rows}"
"Length of `row_names` and `number_of_rows` do not match, " f"{len(row_names)} != {number_of_rows}"
)
if any(x is None for x in row_names):
raise ValueError("`row_names` cannot contain None values.")
Expand All @@ -67,9 +66,7 @@ def _validate_columns(

if column_data is not None:
if column_data.shape[0] != len(column_names):
raise ValueError(
"Number of rows in `column_data` should be equal to the number of columns."
)
raise ValueError("Number of rows in `column_data` should be equal to the number of columns.")


############################
Expand All @@ -96,11 +93,7 @@ def __iter__(self):

def __next__(self):
if self._current_index < len(self._bframe):
iter_row_index = (
self._bframe.row_names[self._current_index]
if self._bframe.row_names is not None
else None
)
iter_row_index = self._bframe.row_names[self._current_index] if self._bframe.row_names is not None else None

iter_slice = self._bframe.row(self._current_index)
self._current_index += 1
Expand Down Expand Up @@ -280,9 +273,7 @@ def __str__(self) -> str:
data = self._data[col]
showed = ut.show_as_cell(data, indices)
header = [col, "<" + ut.print_type(data) + ">"]
showed = ut.truncate_strings(
showed, width=max(40, len(header[0]), len(header[1]))
)
showed = ut.truncate_strings(showed, width=max(40, len(header[0]), len(header[1])))
if insert_ellipsis:
showed = showed[:3] + ["..."] + showed[3:]
columns.append(header + showed)
Expand Down Expand Up @@ -333,9 +324,7 @@ def get_row_names(self) -> Optional[ut.Names]:
"""
return self._row_names

def set_row_names(
self, names: Optional[List], in_place: bool = False
) -> "BiocFrame":
def set_row_names(self, names: Optional[List], in_place: bool = False) -> "BiocFrame":
"""
Args:
names:
Expand Down Expand Up @@ -505,9 +494,7 @@ def get_column_data(self, with_names: bool = True) -> Union[None, "BiocFrame"]:
output = output.set_row_names(self._column_names)
return output

def set_column_data(
self, column_data: Union[None, "BiocFrame"], in_place: bool = False
) -> "BiocFrame":
def set_column_data(self, column_data: Union[None, "BiocFrame"], in_place: bool = False) -> "BiocFrame":
"""
Args:
column_data:
Expand All @@ -524,9 +511,7 @@ def set_column_data(
"""
if column_data is not None:
if column_data.shape[0] != self.shape[1]:
raise ValueError(
"Number of rows in `column_data` should be equal to the number of columns."
)
raise ValueError("Number of rows in `column_data` should be equal to the number of columns.")

output = self._define_output(in_place)
output._column_data = column_data
Expand Down Expand Up @@ -570,9 +555,7 @@ def set_metadata(self, metadata: dict, in_place: bool = False) -> "BiocFrame":
or as a reference to the (in-place-modified) original.
"""
if not isinstance(metadata, dict):
raise TypeError(
f"`metadata` must be a dictionary, provided {type(metadata)}."
)
raise TypeError(f"`metadata` must be a dictionary, provided {type(metadata)}.")
output = self._define_output(in_place)
output._metadata = metadata
return output
Expand Down Expand Up @@ -633,9 +616,7 @@ def get_column(self, column: Union[str, int]) -> Any:

return self._data[column]

raise TypeError(
f"'column' must be a string or integer, provided '{type(column)}'."
)
raise TypeError(f"'column' must be a string or integer, provided '{type(column)}'.")

def column(self, column: Union[str, int]) -> Any:
"""Alias for :py:meth:`~get_column`, provided for back-compatibility only."""
Expand Down Expand Up @@ -714,9 +695,7 @@ def get_slice(
"""
new_column_names = self._column_names
if not (isinstance(columns, slice) and columns == slice(None)):
new_column_indices, _ = ut.normalize_subscript(
columns, len(new_column_names), new_column_names
)
new_column_indices, _ = ut.normalize_subscript(columns, len(new_column_names), new_column_names)
new_column_names = ut.subset_sequence(new_column_names, new_column_indices)

new_data = {}
Expand All @@ -727,9 +706,7 @@ def get_slice(
new_number_of_rows = self.shape[0]
if not (isinstance(rows, slice) and rows == slice(None)):
new_row_names = self.row_names
new_row_indices, _ = ut.normalize_subscript(
rows, self.shape[0], new_row_names
)
new_row_indices, _ = ut.normalize_subscript(rows, self.shape[0], new_row_names)

new_number_of_rows = len(new_row_indices)
for k, v in new_data.items():
Expand Down Expand Up @@ -761,9 +738,7 @@ def slice(self, rows: Sequence, columns: Sequence) -> "BiocFrame":
columns = slice(None)
return self.__getitem__((rows, columns))

def __getitem__(
self, args: Union[int, str, Sequence, tuple]
) -> Union["BiocFrame", Any]:
def __getitem__(self, args: Union[int, str, Sequence, tuple]) -> Union["BiocFrame", Any]:
"""Wrapper around :py:attr:`~get_column` and :py:attr:`~get_slice` to obtain a slice of a ``BiocFrame`` or any
of its columns.
Expand Down Expand Up @@ -879,13 +854,9 @@ def set_slice(
if not in_place:
output._data = copy(output._data)

row_idx, _ = ut.normalize_subscript(
rows, output.shape[0], names=output._row_names
)
row_idx, _ = ut.normalize_subscript(rows, output.shape[0], names=output._row_names)

col_idx, _ = ut.normalize_subscript(
columns, output.shape[1], names=output._column_names
)
col_idx, _ = ut.normalize_subscript(columns, output.shape[1], names=output._column_names)

for i, x in enumerate(col_idx):
nm = output._column_names[x]
Expand All @@ -912,9 +883,7 @@ def __delitem__(self, name: str):
)
self.remove_column(name, in_place=True)

def set_column(
self, column: Union[int, str], value: Any, in_place: bool = False
) -> "BiocFrame":
def set_column(self, column: Union[int, str], value: Any, in_place: bool = False) -> "BiocFrame":
"""Modify an existing column or add a new column. This is a convenience wrapper around :py:attr:`~set_columns`.
Args:
Expand All @@ -935,9 +904,7 @@ def set_column(
"""
return self.set_columns({column: value}, in_place=in_place)

def set_columns(
self, columns: Dict[str, Any], in_place: bool = False
) -> "BiocFrame":
def set_columns(self, columns: Dict[str, Any], in_place: bool = False) -> "BiocFrame":
"""Modify existing columns or add new columns.
Args:
Expand Down Expand Up @@ -984,9 +951,7 @@ def set_columns(

return output

def remove_column(
self, column: Union[int, str], in_place: bool = False
) -> "BiocFrame":
def remove_column(self, column: Union[int, str], in_place: bool = False) -> "BiocFrame":
"""Remove a column. This is a convenience wrapper around :py:attr:`~remove_columns`.
Args:
Expand All @@ -1002,9 +967,7 @@ def remove_column(
"""
return self.remove_columns([column], in_place=in_place)

def remove_columns(
self, columns: Sequence[Union[int, str]], in_place: bool = False
) -> "BiocFrame":
def remove_columns(self, columns: Sequence[Union[int, str]], in_place: bool = False) -> "BiocFrame":
"""Remove any number of existing columns.
Args:
Expand Down Expand Up @@ -1057,19 +1020,15 @@ def __deepcopy__(self, memo=None, _nil=[]):
_num_rows_copy = deepcopy(self._number_of_rows)
_rownames_copy = deepcopy(self.row_names)
_metadata_copy = deepcopy(self.metadata)
_column_data_copy = (
deepcopy(self._column_data) if self._column_data is not None else None
)
_column_data_copy = deepcopy(self._column_data) if self._column_data is not None else None

# copy dictionary first
_data_copy = OrderedDict()
for col in _colnames_copy:
try:
_data_copy[col] = deepcopy(self.column(col))
except Exception as e:
raise Exception(
f"Cannot `deepcopy` column '{col}', full error: {str(e)}"
) from e
raise Exception(f"Cannot `deepcopy` column '{col}', full error: {str(e)}") from e

current_class_const = type(self)
return current_class_const(
Expand Down Expand Up @@ -1106,9 +1065,7 @@ def copy(self):
######>> split by <<######
##########################

def split(
self, column_name: str, only_indices: bool = False
) -> Dict[str, Union["BiocFrame", List[int]]]:
def split(self, column_name: str, only_indices: bool = False) -> Dict[str, Union["BiocFrame", List[int]]]:
"""Split the object by a column.
Args:
Expand Down Expand Up @@ -1246,9 +1203,7 @@ def to_polars(self):
######>> Miscellaneous <<######
###############################

def flatten(
self, as_type: Literal["dict", "biocframe"] = "dict", delim: str = "."
) -> "BiocFrame":
def flatten(self, as_type: Literal["dict", "biocframe"] = "dict", delim: str = ".") -> "BiocFrame":
"""Flatten a nested BiocFrame object.
Args:
Expand Down Expand Up @@ -1404,11 +1359,7 @@ def _combine_cols_bframes(*x: BiocFrame):
all_column_data.append(df._column_data)
for n in df._column_names:
if n in all_data:
raise ValueError(
"All objects to combine must have different columns (duplicated '"
+ n
+ "')."
)
raise ValueError("All objects to combine must have different columns (duplicated '" + n + "').")
all_data[n] = df._data[n]

combined_column_data = None
Expand All @@ -1419,10 +1370,7 @@ def _combine_cols_bframes(*x: BiocFrame):
try:
combined_column_data = ut.combine_rows(*all_column_data)
except Exception as ex:
raise ValueError(
"Failed to combine 'column_data' when combining 'BiocFrame' objects by column. "
+ str(ex)
)
raise ValueError("Failed to combine 'column_data' when combining 'BiocFrame' objects by column. " + str(ex))

current_class_const = type(first)
return current_class_const(
Expand Down Expand Up @@ -1463,9 +1411,7 @@ def _show_as_cell_BiocFrame(x: BiocFrame, indices: Sequence[int]) -> List[str]:


@ut.assign_rows.register(BiocFrame)
def _assign_rows_BiocFrame(
x: BiocFrame, indices: Sequence[int], replacement: BiocFrame
) -> BiocFrame:
def _assign_rows_BiocFrame(x: BiocFrame, indices: Sequence[int], replacement: BiocFrame) -> BiocFrame:
return x.set_slice(indices, replacement.get_column_names(), replacement)


Expand Down Expand Up @@ -1536,20 +1482,12 @@ def relaxed_combine_rows(*x: BiocFrame) -> BiocFrame:
def _normalize_merge_key_to_index(x, i, by):
if by is None:
if x[i]._row_names is None:
raise ValueError(
"Row names required as key but are absent in object " + str(i) + "."
)
raise ValueError("Row names required as key but are absent in object " + str(i) + ".")
return None
elif isinstance(by, int):
nc = x[i].shape[1]
if by < -nc or by >= nc:
raise ValueError(
"Integer 'by' is out of range for object "
+ str(i)
+ " ("
+ str(nc)
+ " columns)."
)
raise ValueError("Integer 'by' is out of range for object " + str(i) + " (" + str(nc) + " columns).")
if by < 0:
return by + nc
else:
Expand All @@ -1560,9 +1498,7 @@ def _normalize_merge_key_to_index(x, i, by):
raise ValueError("No key column '" + by + "' in object " + str(i) + ".")
return ib
else:
raise TypeError(
"Unknown type '" + type(by).__name__ + "' for the 'by' argument."
)
raise TypeError("Unknown type '" + type(by).__name__ + "' for the 'by' argument.")


def _get_merge_key(x, i, by):
Expand Down Expand Up @@ -1679,11 +1615,7 @@ def merge(
counter += 1
y = original + " (" + str(counter) + ")"
elif y in new_data:
raise ValueError(
"Detected duplicate columns across objects to be merged ('"
+ y
+ "')."
)
raise ValueError("Detected duplicate columns across objects to be merged ('" + y + "').")

new_columns.append(y)
if noop:
Expand Down

0 comments on commit f9d9340

Please sign in to comment.