Skip to content

Commit

Permalink
fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeplf committed May 15, 2024
1 parent 74798d5 commit 16bdf61
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
2 changes: 1 addition & 1 deletion atlas_densities/app/cell_densities.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ def measurements_to_average_densities(
@click.option(
"--min-data-points",
type=int,
default=5,
default=1,
help="minimum number of datapoints required for running the linear regression.",
show_default=True,
)
Expand Down
17 changes: 12 additions & 5 deletions atlas_densities/densities/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def _optimize_func(x, coefficient):
)
ss_reg = np.sum((_optimize_func(xdata, parameters[0][0]) - np.mean(ydata)) ** 2)
ss_tot = np.sum((np.array(ydata) - _optimize_func(xdata, parameters[0][0])) ** 2) + ss_reg
L.debug(f"Length of xdata = {xdata}.The ss_reg is {ss_reg} and ss_tot is {ss_tot}\n")
L.debug("Length of xdata = %s. The ss_reg is %s and ss_tot is %s", xdata, ss_reg, ss_tot)
# if total sum of square is null, no variance can be explained by the fitting
return {
"coefficient": parameters[0][0],
Expand Down Expand Up @@ -488,7 +488,7 @@ def compute_fitting_coefficients(
The "standard_deviation" value is the standard deviation of the coefficient value.
The "r_square" value is the coefficient of determination of the coefficient value.
"""

# pylint: disable=too-many-locals
if len(densities.index) != len(average_intensities.index) or np.any(
densities.index != average_intensities.index
):
Expand Down Expand Up @@ -557,7 +557,10 @@ def compute_fitting_coefficients(
for cell_type in tqdm(cell_types):
cloud = clouds[group_name][cell_type]
L.debug(
f"The length of training data for {group_name} and {cell_type} is {cloud['xdata']}"
"The length of training data for %s and %s is %s",
group_name,
cell_type,
cloud["xdata"],
)
result[group_name][cell_type] = linear_fitting_xy(
cloud["xdata"], cloud["ydata"], cloud["sigma"], min_data_points
Expand Down Expand Up @@ -801,6 +804,7 @@ def linear_fitting( # pylint: disable=too-many-arguments
fitting_coefficients: dict returned by
:fun:`atlas_densities.densities.fitting.compute_fitting_coefficients`.
"""
# pylint: disable=too-many-locals
assert group_ids_config is not None
L.info("Checking input data frames sanity ...")
_check_average_densities_sanity(average_densities)
Expand Down Expand Up @@ -852,8 +856,11 @@ def linear_fitting( # pylint: disable=too-many-arguments

L.info("Computing fitting coefficients ...")
fitting_coefficients = compute_fitting_coefficients(
groups, average_intensities, densities.drop(densities.index[indexes]),
min_data_points=min_data_points)
groups,
average_intensities,
densities.drop(densities.index[indexes]),
min_data_points=min_data_points,
)
L.info("Fitting unknown average densities ...")
fit_unknown_densities(groups, average_intensities, densities, fitting_coefficients)

Expand Down
44 changes: 31 additions & 13 deletions tests/densities/test_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,23 @@ def test_compute_average_intensities(region_map, hierarchy_info):

def test_linear_fitting_xy():
min_data_points = 1
actual = tested.linear_fitting_xy([0.0, 1.0, 2.0], [0.0, 2.0, 4.0], [1.0, 1.0, 1.0], min_data_points=min_data_points)
actual = tested.linear_fitting_xy(
[0.0, 1.0, 2.0], [0.0, 2.0, 4.0], [1.0, 1.0, 1.0], min_data_points=min_data_points
)
assert np.allclose(actual["coefficient"], 2.0)
assert np.allclose(actual["r_square"], 1.0)
assert not np.isinf(actual["standard_deviation"])

actual = tested.linear_fitting_xy([0.0, 1.0, 2.0], [0.0, 1.0, 4.0], [1.0, 1.0, 1e-5], min_data_points=min_data_points)
actual = tested.linear_fitting_xy(
[0.0, 1.0, 2.0], [0.0, 1.0, 4.0], [1.0, 1.0, 1e-5], min_data_points=min_data_points
)
assert np.allclose(actual["coefficient"], 2.0)
assert not np.isinf(actual["standard_deviation"])
assert np.allclose(actual["r_square"], 0.89286)

actual = tested.linear_fitting_xy([0.0, 1.0, 2.0], [0.0, 2.0, 4.0], [1.0, 0.0, 1.0], min_data_points=min_data_points)
actual = tested.linear_fitting_xy(
[0.0, 1.0, 2.0], [0.0, 2.0, 4.0], [1.0, 0.0, 1.0], min_data_points=min_data_points
)
assert np.allclose(actual["coefficient"], 2.0)
assert not np.isinf(actual["standard_deviation"])
assert np.allclose(actual["r_square"], 1.0)
Expand Down Expand Up @@ -320,7 +326,9 @@ def test_compute_fitting_coefficients(hierarchy_info):
data = get_fitting_input_data_(hierarchy_info)

actual = tested.compute_fitting_coefficients(
data["groups"], data["intensities"], data["densities"],
data["groups"],
data["intensities"],
data["densities"],
min_data_points=1,
)

Expand All @@ -342,24 +350,33 @@ def test_compute_fitting_coefficients_exceptions(hierarchy_info):
data["densities"].drop(index=["Central lobule"], inplace=True)

with pytest.raises(AtlasDensitiesError):
tested.compute_fitting_coefficients(data["groups"], data["intensities"], data["densities"],
min_data_points=1,
)
tested.compute_fitting_coefficients(
data["groups"],
data["intensities"],
data["densities"],
min_data_points=1,
)

data = get_fitting_input_data_(hierarchy_info)
data["densities"].drop(columns=["pv+"], inplace=True)

with pytest.raises(AtlasDensitiesError):
tested.compute_fitting_coefficients(data["groups"], data["intensities"], data["densities"],
min_data_points=1,
)
tested.compute_fitting_coefficients(
data["groups"],
data["intensities"],
data["densities"],
min_data_points=1,
)

data = get_fitting_input_data_(hierarchy_info)
data["densities"].at["Lobule II", "pv+_standard_deviation"] = np.nan
with pytest.raises(AssertionError):
tested.compute_fitting_coefficients(data["groups"], data["intensities"], data["densities"],
min_data_points=1,
)
tested.compute_fitting_coefficients(
data["groups"],
data["intensities"],
data["densities"],
min_data_points=1,
)


@pytest.fixture
Expand Down Expand Up @@ -533,6 +550,7 @@ def test_linear_fitting(group_ids_config):
data["average_densities"],
data["homogenous_regions"],
group_ids_config=group_ids_config,
min_data_points=1,
)
warnings_ = [w for w in warnings_ if isinstance(w.message, AtlasDensitiesWarning)]
# Three warnings for recording NaN coefficients, three warnings for using them
Expand Down

0 comments on commit 16bdf61

Please sign in to comment.