Skip to content

Commit

Permalink
Merge pull request #16 from yingtluo/master
Browse files Browse the repository at this point in the history
Finding and plotting spread of data for one subject's run
  • Loading branch information
AlonDaks committed Nov 19, 2015
2 parents e0f989b + cf31bc1 commit ae27c19
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 0 deletions.
88 changes: 88 additions & 0 deletions code/utils/plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import numpy as np
import matplotlib.pyplot as plt


def plot_slice(vol, m, color='gray', ipn='nearest'):
"""
Plots the (m+1)th slice over the 3rd dimension across all voxels.
Parameters
----------
vol : 3-D array
Specifies which 3-D volume to plot.
m : number
Specifies which slice to plot
color : String
Optional input that specifies the color of the plot
ipon : String
Optional input that specifies how the interpolation will be conducted
Returns
-------
Nothing
"""
plt.imshow(vol[:, :, m], cmap=color, interpolation=ipn)


def plot_central_slice(vol, color='gray', ipn='nearest'):
"""
Plots the central slice over the 3rd dimension across all voxels.
If the 3-D volume given does not have a center, then plot_central_slice
plots the smaller index of the two centermost slices.
Parameters
----------
vol : 3-D array
Specifies which 3-D volume to plot.
color : String
Optional input that specifies the color of the plot
ipn : String
Optional input that specifies how the interpolation will be conducted
Returns
-------
Nothing
"""
c = vol.shape[2] // 2
plt.imshow(vol[:, :, c], cmap=color, interpolation=ipn)


def plot_sd(data):
"""
Finds standard deviation for each volume and plots those values.
Parameters
----------
data : 4-D array
Returns
-------
stds : 1-D array
List of standard deviations that were plotted
"""
stds = [np.std(data[..., i]) for i in range(data.shape[-1])]
plt.plot(stds)
return stds


def plot_var(data):
"""
Finds standard deviation for each volume and plots those values.
Parameters
----------
data : 4-D array
Returns
-------
variances : 1-D array
List of variances that were plotted
"""
var = [np.var(data[..., i]) for i in range(data.shape[-1])]
plt.plot(var)
return var
51 changes: 51 additions & 0 deletions code/utils/tests/test_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from __future__ import absolute_import
from .. import plot

import numpy as np
import matplotlib

from numpy.testing import assert_almost_equal, assert_array_equal
try:
from mock import patch
except:
from unittest.mock import patch
import unittest

class PlotSlicesTestCase(unittest.TestCase):
@patch.object(matplotlib.pyplot, 'imshow')
def test_plot_slice(self, mock_imshow):
plot.plot_slice(np.array([[[1, 2], [1, 2]], [[1, 2], [1, 2]]]), 0)
assert_array_equal(mock_imshow.call_args[0][0],
np.array([[1, 1], [1, 1]]))
assert_array_equal(mock_imshow.call_args[1],
{'cmap': 'gray',
'interpolation': 'nearest'})

@patch.object(matplotlib.pyplot, 'imshow')
def test_plot_central_slice(self, mock_imshow):
plot.plot_central_slice(np.array([[[1, 2], [1, 2]], [[1, 2], [1, 2]
]]))
assert_array_equal(mock_imshow.call_args[0][0],
np.array([[2, 2], [2, 2]]))
assert_array_equal(mock_imshow.call_args[1],
{'cmap': 'gray',
'interpolation': 'nearest'})

plot.plot_central_slice(np.array([[[1, 2], [1, 2]], [[1, 2], [1, 2]
]]))
assert_array_equal(mock_imshow.call_args[0][0],
np.array([[2, 2], [2, 2]]))
assert_array_equal(mock_imshow.call_args[1],
{'cmap': 'gray',
'interpolation': 'nearest'})

class PlotSpreadTestCase(unittest.TestCase):
@patch.object(matplotlib.pyplot, 'plot')
def test_plot_sd(self, mock_pyplot):
plot.plot_sd(np.array([[[1, 2], [1, 2]], [[1, 2], [1, 2]]]))
assert_array_equal(mock_pyplot.call_args[0][0], [0.0, 0.0])

@patch.object(matplotlib.pyplot, 'plot')
def test_plot_var(self, mock_pyplot):
plot.plot_var(np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]))
assert_array_equal(mock_pyplot.call_args[0][0], [5.0, 5.0])
49 changes: 49 additions & 0 deletions code/utils/tests/test_variance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import absolute_import
from .. import variance

import numpy as np
import nibabel as nib

from numpy.testing import assert_almost_equal, assert_array_equal
import unittest
import os


class LoadDataTestCase(unittest.TestCase):
def test_load_image(self):
# Creating mock .nii file for testing
data = np.array([[[[7, 9], [7, 8]], [[1, 2], [1, 8]]],
[[[2, 3], [2, 1]], [[5, 4], [4, 3]]]])
img = nib.Nifti1Image(data, affine=np.diag([1, 1, 1, 1]))
nib.save(img, 'test_data.nii')
# Calling load_image function and testing the loaded output
data = variance.load_image('test_data.nii')
assert_array_equal(data.shape, (2, 2, 2, 2))
assert_array_equal(data,
np.array([[[[7, 9], [7, 8]], [[1, 2], [1, 8]]],
[[[2, 3], [2, 1]], [[5, 4], [4, 3]]]]))
# Removing .nii file when test ends
os.remove('test_data.nii')

def test_isolate_vol(self):
data = np.array([[[[7, 9], [7, 8]], [[1, 2], [1, 8]]],
[[[2, 3], [2, 1]], [[5, 4], [4, 3]]]])
vol0 = variance.isolate_vol(data, 0)
assert_array_equal(vol0,
np.array([[[7, 7], [1, 1]], [[2, 2], [5, 4]]]))

vol1 = variance.isolate_vol(data, 1)
assert_array_equal(vol1,
np.array([[[9, 8], [2, 8]], [[3, 1], [4, 3]]]))

def test_find_sd(self):
data = np.array([[[[7, 9], [7, 8]], [[1, 2], [1, 8]]],
[[[2, 3], [2, 1]], [[5, 4], [4, 3]]]])
sd = variance.find_sd(data)
assert_almost_equal(sd, 2.69765523)

def test_find_var(self):
data = np.array([[[[7, 9], [7, 8]], [[1, 2], [1, 8]]],
[[[2, 3], [2, 1]], [[5, 4], [4, 3]]]])
var = variance.find_var(data)
assert_almost_equal(var, 7.27734375)
70 changes: 70 additions & 0 deletions code/utils/variance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import numpy as np
import nibabel as nib


def load_image(fname):
"""
Loads the .nii file
Parameters
----------
fname : String
The path to the .nii file that we wish to load and get the data of
Returns
-------
data : a 4-D array
"""
img = nib.load(fname)
data = img.get_data()
return data


def isolate_vol(data, n):
"""
Selects the (n+1)th volume from the 4-D image data array by slicing over
the last dimension.
Parameters
----------
data : 4-D image data array
n : number
Indicates which index of the last dimension to slice
Returns
-------
voln : 3-D array
The (n+1)th volume
"""
return data[..., n]


def find_sd(vol):
"""
Finds standard deviation across all voxels for one volume
Parameters
----------
vol: The voxel which we wish to calculate the standard deviation on.
Returns
-------
sd : number
"""
return np.std(vol)


def find_var(vol):
"""
Finds variance across all voxels for one volume
Parameters
----------
vol: The voxel which we wish to calculate the standard deviation on.
Returns
-------
var : number
"""
return np.var(vol)

0 comments on commit ae27c19

Please sign in to comment.