Skip to content

Commit

Permalink
[FIX] Fix categorical contrasts (#251)
Browse files Browse the repository at this point in the history
* Tutorial update,categorical variables not unsigned

* Version 0.3.1
  • Loading branch information
ReinderVosDeWael committed Nov 12, 2021
1 parent f685085 commit 5d7d35f
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 103 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -1,3 +1,6 @@
*.prof
local_scripts/*

docs/_build
docs/generated
test_python
Expand Down
2 changes: 1 addition & 1 deletion brainstat/__init__.py
@@ -1,2 +1,2 @@
"""Neuroimaging statistics toolbox."""
__version__ = "0.3.0"
__version__ = "0.3.1"
23 changes: 11 additions & 12 deletions brainstat/mesh/utils.py
Expand Up @@ -198,16 +198,12 @@ def mesh_edges(


def _mask_edges(edges: np.ndarray, mask: ArrayLike) -> Tuple[np.ndarray, np.ndarray]:
# TODO: this section is sloppily written.
missing_edges = np.where(np.logical_not(mask))
remove_edges = np.zeros(edges.shape, dtype=bool)
for i in range(edges.shape[0]):
for j in range(edges.shape[1]):
remove_edges[i, j] = (edges[i, j] == missing_edges).any()
remove_edges = np.isin(edges, missing_edges)
idx = ~np.any(remove_edges, axis=1)
edges = edges[idx, :]
edges = _make_contiguous(edges)
return edges, idx
edges_new = edges[idx, :]
edges_new = _make_contiguous(edges_new)
return edges_new, idx


def _make_contiguous(Y: np.ndarray) -> np.ndarray:
Expand All @@ -223,7 +219,10 @@ def _make_contiguous(Y: np.ndarray) -> np.ndarray:
numpy.ndarray
Array Y converted to contiguous numbers in range(np.unique(Y).size).
"""
val = np.unique(Y)
for i in range(val.size):
Y[Y == val[i]] = i
return Y
Y_flat = Y.copy().flatten()
val = np.unique(Y_flat)
new_val = np.arange(val.size)

D = dict(np.array([val, new_val]).T)
Y_new = [D[i] for i in Y_flat]
return np.reshape(Y_new, Y.shape)
2 changes: 1 addition & 1 deletion brainstat/stats/SLM.py
Expand Up @@ -248,7 +248,7 @@ def _surfstat_to_brainstat_rft(self) -> None:
yeo7_index = yeo7[self.P["peak"]["vertid"][i]]
if "yeo7" not in self.P["peak"]:
self.P["peak"]["yeo7"] = []
self.P["peak"]["yeo7"][i] = np.take(yeo_names, yeo7_index)
self.P["peak"]["yeo7"].append(np.take(yeo_names, yeo7_index))

if "clus" in self.P:
for i in range(len(self.P["clus"]["clusid"])):
Expand Down
2 changes: 1 addition & 1 deletion brainstat/stats/terms.py
Expand Up @@ -84,7 +84,7 @@ def to_df(
)
df.columns = names

return pd.get_dummies(df)
return pd.get_dummies(df, dtype=np.int8)


def get_index(df: pd.DataFrame) -> Optional[int]:
Expand Down
Binary file modified brainstat_matlab/tutorials/tutorial_1_statistics.mlx
Binary file not shown.
30 changes: 22 additions & 8 deletions docs/matlab/tutorials/tutorial_1_statistics.html

Large diffs are not rendered by default.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions docs/python/generated_tutorials/plot_tutorial_01_basics.ipynb
Expand Up @@ -62,7 +62,7 @@
},
"outputs": [],
"source": [
"from brainstat.stats.terms import FixedEffect\n\nterm_age = FixedEffect(demographics.AGE_AT_SCAN)\n# Subtract 1 from DX_GROUP so patient == 0 and healthy == 1.\nterm_patient = FixedEffect(demographics.DX_GROUP - 1)\nmodel = term_age + term_patient"
"from brainstat.stats.terms import FixedEffect\n\ndemographics.DX_GROUP[demographics.DX_GROUP == 1] = \"Patient\"\ndemographics.DX_GROUP[demographics.DX_GROUP == 2] = \"Control\"\n\nterm_age = FixedEffect(demographics.AGE_AT_SCAN)\nterm_patient = FixedEffect(demographics.DX_GROUP)\nmodel = term_age + term_patient"
]
},
{
Expand Down Expand Up @@ -134,7 +134,7 @@
},
"outputs": [],
"source": [
"from brainstat.stats.SLM import SLM\n\ncontrast_age = model.AGE_AT_SCAN\nslm_age = SLM(\n model, contrast_age, surf=pial_combined, mask=mask, correction=[\"fdr\", \"rft\"]\n)\nslm_age.fit(thickness)"
"from brainstat.stats.SLM import SLM\n\ncontrast_age = model.AGE_AT_SCAN\nslm_age = SLM(\n model, contrast_age, surf=\"civet41k\", mask=mask, correction=[\"fdr\", \"rft\"]\n)\nslm_age.fit(thickness)"
]
},
{
Expand Down Expand Up @@ -206,7 +206,7 @@
},
"outputs": [],
"source": [
"# Note the minus in front of contrast_age to test for decreasing thickness with age.\nslm_age_onetailed = SLM(\n model,\n -contrast_age,\n surf=pial_combined,\n correction=[\"fdr\", \"rft\"],\n mask=mask,\n two_tailed=False,\n)\nslm_age_onetailed.fit(thickness)\n\nplot_slm_results(slm_age_onetailed)"
"# Note the minus in front of contrast_age to test for decreasing thickness with age.\nslm_age_onetailed = SLM(\n model,\n -contrast_age,\n surf=\"civet41k\",\n correction=[\"fdr\", \"rft\"],\n mask=mask,\n two_tailed=False,\n)\nslm_age_onetailed.fit(thickness)\n\nplot_slm_results(slm_age_onetailed)"
]
},
{
Expand All @@ -224,7 +224,7 @@
},
"outputs": [],
"source": [
"contrast_patient = model.DX_GROUP\nslm_patient = SLM(\n model,\n contrast_patient,\n surf=pial_combined,\n mask=mask,\n correction=[\"fdr\", \"rft\"],\n)\nslm_patient.fit(thickness)\n\nplot_slm_results(slm_patient)"
"contrast_patient = model.DX_GROUP_Patient - model.DX_GROUP_Control\nslm_patient = SLM(\n model,\n contrast_patient,\n surf=\"civet41k\",\n mask=mask,\n correction=[\"fdr\", \"rft\"],\n)\nslm_patient.fit(thickness)\n\nplot_slm_results(slm_patient)"
]
},
{
Expand All @@ -242,7 +242,7 @@
},
"outputs": [],
"source": [
"from brainstat.stats.terms import MixedEffect\n\nrandom_site = MixedEffect(demographics.SITE_ID, name_ran=\"Site\")\n\nmodel_random = term_age + term_patient + random_site\nslm_random = SLM(\n model_random,\n contrast_age,\n surf=pial_combined,\n mask=mask,\n correction=[\"fdr\", \"rft\"],\n)\nslm_random.fit(thickness)"
"from brainstat.stats.terms import MixedEffect\n\nrandom_site = MixedEffect(demographics.SITE_ID, name_ran=\"Site\")\n\nmodel_random = term_age + term_patient + random_site\nslm_random = SLM(\n model_random,\n contrast_age,\n surf=\"civet41k\",\n mask=mask,\n correction=[\"fdr\", \"rft\"],\n)\nslm_random.fit(thickness)"
]
},
{
Expand Down
16 changes: 9 additions & 7 deletions docs/python/generated_tutorials/plot_tutorial_01_basics.py
Expand Up @@ -53,9 +53,11 @@

from brainstat.stats.terms import FixedEffect

demographics.DX_GROUP[demographics.DX_GROUP == 1] = "Patient"
demographics.DX_GROUP[demographics.DX_GROUP == 2] = "Control"

term_age = FixedEffect(demographics.AGE_AT_SCAN)
# Subtract 1 from DX_GROUP so patient == 0 and healthy == 1.
term_patient = FixedEffect(demographics.DX_GROUP - 1)
term_patient = FixedEffect(demographics.DX_GROUP)
model = term_age + term_patient

###################################################################
Expand Down Expand Up @@ -90,7 +92,7 @@

contrast_age = model.AGE_AT_SCAN
slm_age = SLM(
model, contrast_age, surf=pial_combined, mask=mask, correction=["fdr", "rft"]
model, contrast_age, surf="civet41k", mask=mask, correction=["fdr", "rft"]
)
slm_age.fit(thickness)

Expand Down Expand Up @@ -189,7 +191,7 @@ def plot_slm_results(slm):
slm_age_onetailed = SLM(
model,
-contrast_age,
surf=pial_combined,
surf="civet41k",
correction=["fdr", "rft"],
mask=mask,
two_tailed=False,
Expand All @@ -202,11 +204,11 @@ def plot_slm_results(slm):
# Similarly, we could perform an analysis to assess cortical thickness
# differences across healthy and patient groups whilst correcting for age.

contrast_patient = model.DX_GROUP
contrast_patient = model.DX_GROUP_Patient - model.DX_GROUP_Control
slm_patient = SLM(
model,
contrast_patient,
surf=pial_combined,
surf="civet41k",
mask=mask,
correction=["fdr", "rft"],
)
Expand All @@ -232,7 +234,7 @@ def plot_slm_results(slm):
slm_random = SLM(
model_random,
contrast_age,
surf=pial_combined,
surf="civet41k",
mask=mask,
correction=["fdr", "rft"],
)
Expand Down
@@ -1 +1 @@
723808218958d768ba424e630d2f8fea
bbd08ec3b86f5c05a2b26cbb9e58b554
139 changes: 81 additions & 58 deletions docs/python/generated_tutorials/plot_tutorial_01_basics.rst

Large diffs are not rendered by default.

Binary file not shown.
4 changes: 2 additions & 2 deletions docs/python/generated_tutorials/sg_execution_times.rst
Expand Up @@ -5,10 +5,10 @@

Computation times
=================
**04:11.183** total execution time for **python_generated_tutorials** files:
**01:57.138** total execution time for **python_generated_tutorials** files:

+----------------------------------------------------------------------------------------------------------+-----------+--------+
| :ref:`sphx_glr_python_generated_tutorials_plot_tutorial_01_basics.py` (``plot_tutorial_01_basics.py``) | 04:11.183 | 0.0 MB |
| :ref:`sphx_glr_python_generated_tutorials_plot_tutorial_01_basics.py` (``plot_tutorial_01_basics.py``) | 01:57.138 | 0.0 MB |
+----------------------------------------------------------------------------------------------------------+-----------+--------+
| :ref:`sphx_glr_python_generated_tutorials_plot_tutorial_02_context.py` (``plot_tutorial_02_context.py``) | 00:00.000 | 0.0 MB |
+----------------------------------------------------------------------------------------------------------+-----------+--------+
16 changes: 9 additions & 7 deletions docs/python/tutorials/plot_tutorial_01_basics.py
Expand Up @@ -53,9 +53,11 @@

from brainstat.stats.terms import FixedEffect

demographics.DX_GROUP[demographics.DX_GROUP == 1] = "Patient"
demographics.DX_GROUP[demographics.DX_GROUP == 2] = "Control"

term_age = FixedEffect(demographics.AGE_AT_SCAN)
# Subtract 1 from DX_GROUP so patient == 0 and healthy == 1.
term_patient = FixedEffect(demographics.DX_GROUP - 1)
term_patient = FixedEffect(demographics.DX_GROUP)
model = term_age + term_patient

###################################################################
Expand Down Expand Up @@ -90,7 +92,7 @@

contrast_age = model.AGE_AT_SCAN
slm_age = SLM(
model, contrast_age, surf=pial_combined, mask=mask, correction=["fdr", "rft"]
model, contrast_age, surf="civet41k", mask=mask, correction=["fdr", "rft"]
)
slm_age.fit(thickness)

Expand Down Expand Up @@ -189,7 +191,7 @@ def plot_slm_results(slm):
slm_age_onetailed = SLM(
model,
-contrast_age,
surf=pial_combined,
surf="civet41k",
correction=["fdr", "rft"],
mask=mask,
two_tailed=False,
Expand All @@ -202,11 +204,11 @@ def plot_slm_results(slm):
# Similarly, we could perform an analysis to assess cortical thickness
# differences across healthy and patient groups whilst correcting for age.

contrast_patient = model.DX_GROUP
contrast_patient = model.DX_GROUP_Patient - model.DX_GROUP_Control
slm_patient = SLM(
model,
contrast_patient,
surf=pial_combined,
surf="civet41k",
mask=mask,
correction=["fdr", "rft"],
)
Expand All @@ -232,7 +234,7 @@ def plot_slm_results(slm):
slm_random = SLM(
model_random,
contrast_age,
surf=pial_combined,
surf="civet41k",
mask=mask,
correction=["fdr", "rft"],
)
Expand Down

0 comments on commit 5d7d35f

Please sign in to comment.