Skip to content

Commit

Permalink
Merge pull request #363 from MICA-MNI/290-inconsistent-sum-of-fixedef…
Browse files Browse the repository at this point in the history
…fect-elements

fix sum problem in fixed effect elements
  • Loading branch information
zihuaihuai authored Sep 27, 2024
2 parents aa12eb3 + 2ef78e6 commit c824c62
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
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)

0 comments on commit c824c62

Please sign in to comment.