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

fix sum problem in fixed effect elements #363

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion brainstat/context/genetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def surface_genetic_expression(
with tempfile.NamedTemporaryFile(suffix=".gii", delete=False) as f:
name = f.name
write_surface(surface, name, otype="gii")
surfaces_gii.append(nib.load(name))
surfaces_gii.append(nib.load(name))
finally:
Path(name).unlink()
else:
Expand Down
9 changes: 7 additions & 2 deletions brainstat/stats/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,14 @@ def __add__(
return self._add(t)

def __radd__(
self, t: Union[ArrayLike, "FixedEffect", "MixedEffect"]
self, t: Union[int, ArrayLike, "FixedEffect", "MixedEffect"]
) -> "FixedEffect":
return self._add(t, side="right")
if isinstance(t, int) and t == 0:
# When t is 0, return self without adding an extra intercept
return self
else:
# For other cases, proceed with the addition
return self._add(t, side="right")

def __sub__(self, t: Union[ArrayLike, "FixedEffect"]) -> "FixedEffect":
if self.empty:
Expand Down
20 changes: 20 additions & 0 deletions brainstat/tests/test_genetics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from brainstat.context.genetics import surface_genetic_expression
from brainstat.datasets import fetch_parcellation, fetch_template_surface, fetch_parcellation
import numpy as np
from nilearn import datasets

# Get Schaefer-100 genetic expression.
# def test_surface_genetic_expression():
# schaefer_100_fs5 = fetch_parcellation("fsaverage5", "schaefer", 100)
# surfaces = fetch_template_surface("fsaverage5", join=False)
# expression = surface_genetic_expression(schaefer_100_fs5, surfaces, space="fsaverage")
# assert expression is not None


# def test_surface_genetic_expression2():
# destrieux = datasets.fetch_atlas_surf_destrieux()
# labels = np.hstack((destrieux['map_left'], destrieux['map_right']))
# fsaverage = datasets.fetch_surf_fsaverage()
# surfaces = (fsaverage['pial_left'], fsaverage['pial_right'])
# expression = surface_genetic_expression(labels, surfaces, space='fsaverage')
# assert expression is not None
11 changes: 10 additions & 1 deletion brainstat/tests/test_terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import numpy as np
import pandas as pd

from functools import reduce
from brainstat.tutorial.utils import fetch_mics_data
from brainstat.stats.terms import FixedEffect, MixedEffect


Expand Down Expand Up @@ -144,3 +145,11 @@ def test_identity_detection():
def as_variance(M):
var = [np.reshape(x[:, None] @ x[None, :], (-1, 1)) for x in M.T]
return np.squeeze(var, axis=2).T


def test_sum():
_, demographics = fetch_mics_data()
model = []
term_age = FixedEffect(demographics.AGE_AT_SCAN)
model.append(term_age)
assert reduce(lambda x, y: x + y, model) == sum(model)
Loading