Skip to content

Commit

Permalink
convert remaining docstrings to numpydoc style
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Meech committed Apr 8, 2024
1 parent 416069a commit 2478450
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 36 deletions.
18 changes: 13 additions & 5 deletions brainglobe_utils/image/binning.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@

def get_bins(image_size, bin_sizes):
"""
Given an image size, and bin size, return a list of the bin boundaries
:param image_size: Size of the final image (tuple/list)
:param bin_sizes: Bin sizes corresponding to the dimensions of
"image_size" (tuple/list)
:return: List of arrays of bin boundaries
Given an image size, and bin size, return a list of the bin boundaries.
Parameters
----------
image_size : tuple of int or list of int
Size of the final image
bin_sizes : tuple of int or list of int
Bin sizes corresponding to the dimensions of "image_size"
Returns
-------
list of np.ndarray
List of arrays of bin boundaries
"""
bins = []
for dim in range(0, len(image_size)):
Expand Down
39 changes: 29 additions & 10 deletions brainglobe_utils/image/masking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
def mask_image_threshold(image, masking_image, threshold=0):
"""
Mask one image, based on the values in another image that are above a
threshold
:param image: Input image
:param masking_image: Image to base the mask on (same shape as image)
:param threshold: Threshold to base the mask on
:return: Masked image
threshold.
Parameters
----------
image : np.ndarray
Input image
masking_image : np.ndarray
Image to base the mask on (same shape as image)
threshold : int, optional
Threshold to base the mask on
Returns
-------
np.ndarray
Masked image
"""
masking_image = make_mask(masking_image, threshold=threshold)
return image * masking_image
Expand All @@ -17,11 +27,20 @@ def mask_image_threshold(image, masking_image, threshold=0):
def make_mask(masking_image, threshold=0):
"""
Given an image, and an optional threshold, returns a binary image that can
be used as a mask
:param masking_image: nD image
:param threshold: Optional threshold, default 0. Values above this are
included in the mask. Values below this are not.
:return: Binary mask
be used as a mask.
Parameters
----------
masking_image : np.ndarray
nD image
threshold : int, optional
Optional threshold, default 0. Values above this are
included in the mask. Values below this are not.
Returns
-------
np.ndarray
Binary mask
"""
mask = np.copy(masking_image)
mask[masking_image <= threshold] = 0
Expand Down
24 changes: 18 additions & 6 deletions brainglobe_utils/image/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ def scale_to_16_bits(img):
"""
Normalise the input image to the full 0-2^16 bit depth.
:param np.array img: The input image
:return: The normalised image
:rtype: np.array
Parameters
----------
img : np.ndarray
The input image
Returns
-------
np.ndarray
The normalised image
"""
normalised = img / img.max()
return normalised * (2**16 - 1)
Expand All @@ -18,9 +24,15 @@ def scale_and_convert_to_16_bits(img):
Normalise the input image to the full 0-2^16 bit depth, and return as
type: "np.uint16".
:param np.array img: The input image
:return: The normalised, 16 bit image
:rtype: np.array
Parameters
----------
img : np.ndarray
The input image
Returns
-------
np.ndarray
The normalised, 16 bit image
"""
img = scale_to_16_bits(img)
return img.astype(np.uint16, copy=False)
44 changes: 35 additions & 9 deletions brainglobe_utils/pandas/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,34 @@

def initialise_df(*column_names):
"""
Initialise a pandasdataframe with n column names
:param str column_names: N column names
:return: Empty pandas dataframe with specified column names
Initialise a pandas dataframe with n column names.
Parameters
----------
*column_names : str
N column names
Returns
-------
pd.DataFrame
Empty pandas dataframe with specified column names
"""
return pd.DataFrame(columns=column_names)


def sanitise_df(df):
"""
Replaces infinite values in a dataframe with NaN
:param df: Any dataframe
:return: Dataframe with Inf replaced with NaN
Replaces infinite values in a dataframe with NaN.
Parameters
----------
df : pd.DataFrame
Any dataframe
Returns
-------
pd.DataFrame
Dataframe with Inf replaced with NaN.
"""
df = df.replace(np.inf, np.nan)
return df
Expand All @@ -25,14 +41,24 @@ def safe_pandas_concat(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:
"""
Concatenate two DataFrames without relying on deprecated functionality
when one of the DataFrames is empty.
If df1 and df2 are non-empty, return the concatenation.
If df1 is empty and df2 is not, return a copy of df2.
If df1 is non-empty and df2 is, return a copy of df1.
If df1 and df2 are empty, return an empty DataFrame with the same column
names as df1.
:param df1: DataFrame to concatenate.
:param df2: DataFrame to concatenate.
:returns: DataFrame formed from concatenation of df1 and df2.
Parameters
----------
df1 : pd.DataFrame
DataFrame to concatenate.
df2 : pd.DataFrame
DataFrame to concatenate.
Returns
-------
pd.DataFrame
DataFrame formed from concatenation of df1 and df2.
"""
if df1.empty and df2.empty:
return pd.DataFrame(columns=df1.columns)
Expand Down
11 changes: 5 additions & 6 deletions tests/tests/test_brainreg/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ def test_transform_points_from_downsampled_to_atlas_space(
* check that deformation field of ones maps to 1,1,1*resolution
* check that too small deformation field maps points to out-of-bounds
Args:
mocker: The mocker object used to patch the reading of deformation
field tiffs.
Returns:
None
Parameters
----------
mocker : pytest_mock.plugin.MockerFixture
The mocker object used to patch the reading of deformation
field tiffs.
"""
mocker.patch(
"brainglobe_utils.brainreg.transform.tifffile.imread",
Expand Down

0 comments on commit 2478450

Please sign in to comment.