Skip to content
Open
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
34 changes: 34 additions & 0 deletions .github/workflows/testing.yml
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
4 changes: 4 additions & 0 deletions findoutlie/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
""" Init for findoutlie module
"""

__version__ = '0.1a0'
40 changes: 40 additions & 0 deletions findoutlie/metrics.py
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
Comment on lines +33 to +36

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
first = data[:,:,:,1:]
second = data[:,:,:,:-1]
diff = first - second
diff = data[:,:,:,1:] - data[:,:,:,:-1]

dvar_val = np.sqrt(np.mean(diff ** 2, axis = (0,1,2)))
#import pdb;
#pdb.set_trace()
return(dvar_val)
35 changes: 35 additions & 0 deletions findoutlie/tests/test_dvars.py
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)
20 changes: 20 additions & 0 deletions pyproject.toml
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"
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
addopts = --ignore-glob=solutions/*
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
nibabel
numpy
scipy
matplotlib
nipraxis
3 changes: 3 additions & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Test requirements
-r requirements.txt
pytest