-
Notifications
You must be signed in to change notification settings - Fork 4
Add dvars #6
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
Open
demidenm
wants to merge
3
commits into
nipraxis-spring-2022:main
Choose a base branch
from
demidenm:add-dvars
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add dvars #6
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| name: Build and run tests | ||
|
|
||
| on: | ||
| push: | ||
| pull_request: | ||
|
|
||
| jobs: | ||
| tests: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@master | ||
| with: | ||
| submodules: true | ||
| - uses: actions/setup-python@v2 | ||
| with: | ||
| python-version: "3.x" | ||
| - name: Install test dependencies | ||
| run: | | ||
| pip install -r test_requirements.txt | ||
| pip install pytest-cov | ||
| - name: Install package | ||
| run: | | ||
| pip install flit | ||
| flit install | ||
| - name: Run tests | ||
| run: | | ||
| pytest . --log-level DEBUG --cov-config=.coveragerc --cov=findoutlie --doctest-modules | ||
| - name: Collect code coverage data | ||
| run: | | ||
| coverage xml | ||
| - name: Upload coverage to Codecov | ||
| uses: codecov/codecov-action@v2 | ||
| with: | ||
| fail_ci_if_error: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| """ Init for findoutlie module | ||
| """ | ||
|
|
||
| __version__ = '0.1a0' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| """ Scan outlier metrics | ||
| """ | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
||
| def dvars(img): | ||
| """ Calculate dvars metric on Nibabel image `img` | ||
|
|
||
| The dvars calculation between two volumes is defined as the square root of | ||
| (the sum of the (voxel differences squared) divided by the number of | ||
| voxels). | ||
|
|
||
| Parameters | ||
| ---------- | ||
| img : nibabel image | ||
|
|
||
| Returns | ||
| ------- | ||
| dvals : 1D array | ||
| One-dimensional array with n-1 elements, where n is the number of | ||
| volumnes in `img`. | ||
| """ | ||
| # Hint: remember 'axis='. For example: | ||
| # In [2]: arr = np.array([[2, 3, 4], [5, 6, 7]]) | ||
| # In [3]: np.mean(arr, axis=1) | ||
| # Out[2]: array([3., 6.]) | ||
| # | ||
| # You may be be able to solve this in four lines, without a loop. | ||
| # But solve it any way you can. | ||
|
|
||
| data = img.get_fdata() | ||
| first = data[:,:,:,1:] | ||
| second = data[:,:,:,:-1] | ||
|
|
||
| diff = first - second | ||
| dvar_val = np.sqrt(np.mean(diff ** 2, axis = (0,1,2))) | ||
| #import pdb; | ||
| #pdb.set_trace() | ||
| return(dvar_val) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| """ Test dvars implementation | ||
|
|
||
| You can run the tests from the root directory (containing ``README.md``) with:: | ||
|
|
||
| python3 -m pytest . | ||
| """ | ||
|
|
||
| import numpy as np | ||
|
|
||
| import nibabel as nib | ||
|
|
||
| import nipraxis as npx | ||
|
|
||
| from findoutlie.metrics import dvars | ||
|
|
||
|
|
||
| TEST_FNAME = npx.fetch_file('ds114_sub009_t2r1.nii') | ||
|
|
||
|
|
||
| def test_dvars(): | ||
| img = nib.load(TEST_FNAME) | ||
| n_trs = img.shape[-1] | ||
| n_voxels = np.prod(img.shape[:-1]) | ||
| dvals = dvars(img) | ||
| assert len(dvals) == n_trs - 1 | ||
| # Calculate the values the long way round | ||
| data = img.get_fdata() | ||
| prev_vol = data[..., 0] | ||
| long_dvals = [] | ||
| for i in range(1, n_trs): | ||
| this_vol = data[..., i] | ||
| d = this_vol - prev_vol | ||
| long_dvals.append(np.sqrt(np.sum(d ** 2) / n_voxels)) | ||
| prev_vol = this_vol | ||
| assert np.allclose(dvals, long_dvals) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [build-system] | ||
| requires = ["flit_core >=2,<4"] | ||
| build-backend = "flit_core.buildapi" | ||
|
|
||
| [tool.flit.metadata] | ||
| module = "findoutlie" | ||
| author = "The team" | ||
| author-email = "team@email" | ||
| home-page = "https://github.com/nipraxis-spring-2022/diagnostics-example" | ||
| classifiers = ["License :: OSI Approved :: BSD License", | ||
| "Programming Language :: Python :: 3"] | ||
| description-file="README.md" | ||
| # Check against requirements.txt | ||
| requires = [ | ||
| 'nibabel', | ||
| 'numpy', | ||
| 'scipy', | ||
| 'matplotlib', | ||
| ] | ||
| requires-python=">=3.6" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [pytest] | ||
| addopts = --ignore-glob=solutions/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| nibabel | ||
| numpy | ||
| scipy | ||
| matplotlib | ||
| nipraxis |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Test requirements | ||
| -r requirements.txt | ||
| pytest |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.