Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michiel Cottaar
fslpy
Commits
520143bb
Commit
520143bb
authored
7 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
FEATImage.fit function made standalone, so it can be tested.
parent
5870a4f6
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/data/featimage.py
+69
-43
69 additions, 43 deletions
fsl/data/featimage.py
with
69 additions
and
43 deletions
fsl/data/featimage.py
+
69
−
43
View file @
520143bb
...
...
@@ -6,6 +6,7 @@
#
"""
This module provides the :class:`FEATImage` class, a subclass of
:class:`.Image` designed to encapsulate data from a FEAT analysis.
This module also provides the :func:`modelFit` function.
"""
...
...
@@ -291,7 +292,7 @@ class FEATImage(fslimage.Image):
def
fit
(
self
,
contrast
,
xyz
):
"""
Calculates the model fit for the given contrast vector
at the given voxel.
at the given voxel.
See the :func:`modelFit` function.
:arg contrast: The contrast vector (pass all 1s for a full model
fit).
...
...
@@ -310,48 +311,11 @@ class FEATImage(fslimage.Image):
if
len
(
contrast
)
!=
numEVs
:
raise
ValueError
(
'
Contrast is wrong length
'
)
# Here we are basically trying to
# replicate the behaviour of tsplot.
# There are some differences though -
# by default, tsplot weights the
# data by Z statistics. We're not
# doing that here.
# Normalise the contrast vector.
# The scaling factor is arbitrary,
# but should result in a visually
# sensible scaling of the model fit.
# For a vector of all 1's (i.e. a
# full model fit) this is a no-op.
#
# We also take the absolute value
# of the values in the contrast
# vector, as the parameter estimates
# should be appropriately signed,
# so we don't negative contrast
# vector values to invert them.
contrast
=
np
.
array
(
contrast
)
nonzero
=
sum
(
~
np
.
isclose
(
contrast
,
0
))
contrast
=
contrast
/
np
.
sqrt
((
contrast
**
2
).
sum
())
contrast
=
np
.
abs
(
contrast
*
np
.
sqrt
(
nonzero
))
X
=
self
.
__design
.
getDesign
(
xyz
)
data
=
self
[
x
,
y
,
z
,
:]
modelfit
=
np
.
zeros
(
len
(
data
))
for
i
in
range
(
numEVs
):
ev
=
X
[:,
i
]
pe
=
self
.
getPE
(
i
)[
x
,
y
,
z
]
modelfit
+=
ev
*
pe
*
contrast
[
i
]
# Make sure the model fit has an
# appropriate mean. The data in
# first level analyses is demeaned
# before model fitting, so we need
# to add it back in.
if
firstLevel
:
return
modelfit
+
data
.
mean
()
else
:
return
modelfit
design
=
self
.
getDesign
(
xyz
)
data
=
self
[
x
,
y
,
z
,
:]
pes
=
[
self
.
getPE
(
i
)[
x
,
y
,
z
]
for
i
in
range
(
numEVs
)]
return
modelFit
(
data
,
design
,
contrast
,
pes
,
firstLevel
)
def
partialFit
(
self
,
contrast
,
xyz
):
...
...
@@ -366,3 +330,65 @@ class FEATImage(fslimage.Image):
modelfit
=
self
.
fit
(
contrast
,
xyz
)
return
residuals
+
modelfit
def
modelFit
(
data
,
design
,
contrast
,
pes
,
firstLevel
=
True
):
"""
Calculates the model fit to the given data for the given contrast
vector.
:arg data: The input data
:arg design: The design matrix
:arg contrast: The contrast vector (pass all 1s for a full model
fit)
:arg pes: Parameter estimates for each EV in the design matrix
:arg firstLevel: If ``True`` (the default), the mean of the input
data is added to the result.
:returns: The best fit of the model to the data.
"""
# Here we are basically trying to
# replicate the behaviour of tsplot.
# There are some differences though -
# by default, tsplot weights the
# data by Z statistics. We're not
# doing that here.
# Normalise the contrast vector.
# The scaling factor is arbitrary,
# but should result in a visually
# sensible scaling of the model fit.
# For a vector of all 1's (i.e. a
# full model fit) this is a no-op.
#
# We also take the absolute value
# of the values in the contrast
# vector, as the parameter estimates
# should be appropriately signed,
# so we don't want negative contrast
# vector values to invert them.
contrast
=
np
.
array
(
contrast
)
nevs
=
len
(
contrast
)
nonzero
=
sum
(
~
np
.
isclose
(
contrast
,
0
))
contrast
=
contrast
/
np
.
sqrt
((
contrast
**
2
).
sum
())
contrast
=
np
.
abs
(
contrast
*
np
.
sqrt
(
nonzero
))
modelfit
=
np
.
zeros
(
len
(
data
))
for
i
in
range
(
nevs
):
ev
=
design
[:,
i
]
pe
=
pes
[
i
]
modelfit
+=
ev
*
pe
*
contrast
[
i
]
# Make sure the model fit has an
# appropriate mean. The data in
# first level analyses is demeaned
# before model fitting, so we need
# to add it back in.
if
firstLevel
:
return
modelfit
+
data
.
mean
()
else
:
return
modelfit
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment