Skip to content
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

adding test data folder and unit test for rescale bvec #9

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
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
31 changes: 31 additions & 0 deletions tests/test_data/bvec_scaled.bvec
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
-2.033253493328788519e-01 5.191425167298733268e-01 -8.301504981881513423e-01
1.977081165290608677e-01 5.196681659363497907e-01 -8.311777775968758331e-01
4.018050792304720953e-01 1.756857064686520586e-01 -8.987141986456009812e-01
-4.031232896024871115e-01 7.315950994365449933e-01 -5.497819784791977415e-01
-2.017372527568954965e-01 9.417546889448769054e-01 -2.690728278746084556e-01
-8.534215120105359542e-01 5.179216878381357470e-01 -5.855636690016949308e-02
-7.312383684642190795e-01 5.197490477110885410e-01 -4.417594095083925421e-01
-4.067892940004660396e-01 1.761111280856997030e-01 -8.963857098654488542e-01
-7.326343113734050672e-01 1.759595287941236319e-01 -6.574839998243049433e-01
-6.505047457118273702e-01 7.302047957321597949e-01 2.089127379939905715e-01
-3.223554568316183144e-01 9.409120901667874515e-01 1.037853459253569310e-01
-3.239056163210728423e-01 5.188188542002923898e-01 7.911460979123651205e-01
-6.499877119641050971e-01 5.198718424278614414e-01 5.543006781037963515e-01
-9.787562000010596730e-01 1.756262202307987685e-01 1.057909813118716735e-01
-8.543251023447319126e-01 1.757810088374901492e-01 4.891110880318884080e-01
7.351200102798949049e-04 7.312280906641346334e-01 6.821326403436903663e-01
1.884360179654446556e-03 9.415929137758602963e-01 3.367480273346800668e-01
6.541350652685095080e-01 5.188895597835903040e-01 5.503280304815979518e-01
3.286347067729832871e-01 5.199866853774535169e-01 7.884244266474781382e-01
1.973678352949524983e-01 -1.753764330242377489e-01 -9.645149269610500120e-01
2.038724103925809439e-01 1.754682642453940788e-01 9.631442926807112226e-01
6.511546289927737652e-01 7.302924301027146070e-01 2.065686706060597677e-01
3.236193315131545289e-01 9.410258404305261726e-01 9.869597718752717852e-02
1.996235361681136800e-01 9.406987842073736283e-01 -2.742922550829809958e-01
4.031232896024871115e-01 7.315950994365449933e-01 -5.497819784791977415e-01
7.319223515982337114e-01 1.758058722731208057e-01 -6.583175271137770013e-01
7.286410965136864704e-01 5.186801579370168280e-01 -4.472728990602538590e-01
8.524194359344654526e-01 5.188670754168032140e-01 -6.448304652838203477e-02
8.572526403435390963e-01 1.761364603732337719e-01 4.838324688889937275e-01
9.793287628014578639e-01 1.755445172664188702e-01 1.004952576366961586e-01
Binary file added tests/test_data/eddy_corrected_data.nii.gz
Binary file not shown.
Binary file not shown.
188 changes: 139 additions & 49 deletions tests/test_scripts/test_rescale_bvec.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,141 @@
import pytest
import ndmg
import numpy as np
import warnings

warnings.simplefilter("ignore")
import os.path as op

# def rescale_bvec(bvec, bvec_new):
#
# bv1 = np.array(np.loadtxt(bvec))
# # Enforce proper dimensions
# bv1 = bv1.T if bv1.shape[0] == 3 else bv1
#
# # Normalize values not close to norm 1
# bv2 = [
# b / np.linalg.norm(b) if not np.isclose(np.linalg.norm(b), 0) else b
# for b in bv1
# ]
#
# try:
# assert "bvec" in bvec_new
# np.savetxt(bvec_new, bv2)
# pass
# except AssertionError:
# print(
# "Error: your new b-vector file must have extension .bvec to"
# + " be compatible with the the pipeline."
# )
# pass
# else:
# pass
# pass


def test_rescale_bvec(tmp_path):
d = tmp_path/"sub"
d.mkdir()
test_input = d / "text_input.bvec"
test_output = d / "text_output.bvec"
test_input_array = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
np.savetxt(test_input, test_input_array)
ndmg.preproc.rescale_bvec(str(test_input), str(test_output))
test_output_array = np.array(np.loadtxt(str(test_output)))
b = np.array([[1.230914909793327239e-01, 4.923659639173308955e-01, 8.616404368553290949e-01],
[2.073903389460850510e-01, 5.184758473652126831e-01, 8.295613557843402042e-01],
[2.672612419124243965e-01, 5.345224838248487931e-01, 8.017837257372731896e-01]])
assert np.allclose(test_output_array, b)
from ndmg import preproc as mgp
from ndmg.utils import gen_utils as mgu
from ndmg.register import gen_reg as mgr
from ndmg.track import gen_track as mgt
from ndmg.graph import gen_graph as mgg
from ndmg.utils.bids_utils import name_resource
from unittest.mock import Mock
import numpy as np
import pytest
import os

#requires most up to date pytest for tmp_path to work
def test_rescale_bvac(tmp_path):
#create temp file dir
d = tmp_path/"sub"
d.mkdir()
temp_in1 = d/ "test1.bvec"
temp_in2 = d/ "test2.bvec"

#create temp output dir
temp_out1 = d/ "test1_new.bvec"
temp_out2 = d/ "test2_new.bvec"

#write cases for test1 Test w/ proper dimensions
bvec_test_1 = np.array([[ 0.00000000e+00, -2.03325346e-01, 1.97708115e-01,
4.01805073e-01, -4.03123289e-01, -2.01737255e-01,
-8.53421509e-01, -7.31238365e-01, -4.06789303e-01,
-7.32634306e-01, -6.50504768e-01, -3.22355449e-01,
-3.23905617e-01, -6.49987698e-01, -9.78756189e-01,
-8.54325116e-01, 7.35120033e-04, 1.88436022e-03,
6.54135048e-01, 3.28634709e-01, 1.97367832e-01,
2.03872412e-01, 6.51154637e-01, 3.23619336e-01,
1.99623540e-01, 4.03123289e-01, 7.31922328e-01,
7.28641093e-01, 8.52419436e-01, 8.57252657e-01,
9.79328752e-01],
[ 0.00000000e+00, 5.19142509e-01, 5.19668162e-01,
1.75685704e-01, 7.31595099e-01, 9.41754699e-01,
5.17921686e-01, 5.19749045e-01, 1.76111132e-01,
1.75959527e-01, 7.30204821e-01, 9.40912068e-01,
5.18818855e-01, 5.19871831e-01, 1.75626218e-01,
1.75781012e-01, 7.31228113e-01, 9.41592932e-01,
5.18889546e-01, 5.19986689e-01, -1.75376430e-01,
1.75468266e-01, 7.30292439e-01, 9.41025853e-01,
9.40698802e-01, 7.31595099e-01, 1.75805867e-01,
5.18680155e-01, 5.18867075e-01, 1.76136464e-01,
1.75544515e-01],
[ 0.00000000e+00, -8.30150485e-01, -8.31177771e-01,
-8.98714185e-01, -5.49781978e-01, -2.69072831e-01,
-5.85563667e-02, -4.41759408e-01, -8.96385729e-01,
-6.57483995e-01, 2.08912745e-01, 1.03785343e-01,
7.91146100e-01, 5.54300666e-01, 1.05790980e-01,
4.89111096e-01, 6.82132661e-01, 3.36748034e-01,
5.50328016e-01, 7.88424432e-01, -9.64514911e-01,
9.63144302e-01, 2.06568673e-01, 9.86959785e-02,
-2.74292260e-01, -5.49781978e-01, -6.58317506e-01,
-4.47272897e-01, -6.44830465e-02, 4.83832479e-01,
1.00495256e-01]])
#save file
np.savetxt(temp_in1,bvec_test_1)

#write cases for test2 Test non proper dimensions
bvec_test_2 = np.array([[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[-2.03325346e-01, 5.19142509e-01, -8.30150485e-01],
[ 1.97708115e-01, 5.19668162e-01, -8.31177771e-01],
[ 4.01805073e-01, 1.75685704e-01, -8.98714185e-01],
[-4.03123289e-01, 7.31595099e-01, -5.49781978e-01],
[-2.01737255e-01, 9.41754699e-01, -2.69072831e-01],
[-8.53421509e-01, 5.17921686e-01, -5.85563667e-02],
[-7.31238365e-01, 5.19749045e-01, -4.41759408e-01],
[-4.06789303e-01, 1.76111132e-01, -8.96385729e-01],
[-7.32634306e-01, 1.75959527e-01, -6.57483995e-01],
[-6.50504768e-01, 7.30204821e-01, 2.08912745e-01],
[-3.22355449e-01, 9.40912068e-01, 1.03785343e-01],
[-3.23905617e-01, 5.18818855e-01, 7.91146100e-01],
[-6.49987698e-01, 5.19871831e-01, 5.54300666e-01],
[-9.78756189e-01, 1.75626218e-01, 1.05790980e-01],
[-8.54325116e-01, 1.75781012e-01, 4.89111096e-01],
[ 7.35120033e-04, 7.31228113e-01, 6.82132661e-01],
[ 1.88436022e-03, 9.41592932e-01, 3.36748034e-01],
[ 6.54135048e-01, 5.18889546e-01, 5.50328016e-01],
[ 3.28634709e-01, 5.19986689e-01, 7.88424432e-01],
[ 1.97367832e-01, -1.75376430e-01, -9.64514911e-01],
[ 2.03872412e-01, 1.75468266e-01, 9.63144302e-01],
[ 6.51154637e-01, 7.30292439e-01, 2.06568673e-01],
[ 3.23619336e-01, 9.41025853e-01, 9.86959785e-02],
[ 1.99623540e-01, 9.40698802e-01, -2.74292260e-01],
[ 4.03123289e-01, 7.31595099e-01, -5.49781978e-01],
[ 7.31922328e-01, 1.75805867e-01, -6.58317506e-01],
[ 7.28641093e-01, 5.18680155e-01, -4.47272897e-01],
[ 8.52419436e-01, 5.18867075e-01, -6.44830465e-02],
[ 8.57252657e-01, 1.76136464e-01, 4.83832479e-01],
[ 9.79328752e-01, 1.75544515e-01, 1.00495256e-01]])
#save file
np.savetxt(temp_in2,bvec_test_2)

# #desired ouput
output = np.array([[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[-2.03325349e-01, 5.19142517e-01, -8.30150498e-01],
[ 1.97708117e-01, 5.19668166e-01, -8.31177778e-01],
[ 4.01805079e-01, 1.75685707e-01, -8.98714199e-01],
[-4.03123289e-01, 7.31595100e-01, -5.49781978e-01],
[-2.01737253e-01, 9.41754689e-01, -2.69072828e-01],
[-8.53421512e-01, 5.17921688e-01, -5.85563669e-02],
[-7.31238368e-01, 5.19749047e-01, -4.41759410e-01],
[-4.06789294e-01, 1.76111128e-01, -8.96385710e-01],
[-7.32634311e-01, 1.75959528e-01, -6.57484000e-01],
[-6.50504746e-01, 7.30204796e-01, 2.08912738e-01],
[-3.22355457e-01, 9.40912090e-01, 1.03785345e-01],
[-3.23905616e-01, 5.18818854e-01, 7.91146098e-01],
[-6.49987712e-01, 5.19871842e-01, 5.54300678e-01],
[-9.78756200e-01, 1.75626220e-01, 1.05790981e-01],
[-8.54325102e-01, 1.75781009e-01, 4.89111088e-01],
[ 7.35120011e-04, 7.31228091e-01, 6.82132640e-01],
[ 1.88436018e-03, 9.41592914e-01, 3.36748027e-01],
[ 6.54135065e-01, 5.18889560e-01, 5.50328031e-01],
[ 3.28634707e-01, 5.19986685e-01, 7.88424427e-01],
[ 1.97367835e-01, -1.75376433e-01, -9.64514927e-01],
[ 2.03872410e-01, 1.75468264e-01, 9.63144293e-01],
[ 6.51154629e-01, 7.30292430e-01, 2.06568670e-01],
[ 3.23619332e-01, 9.41025840e-01, 9.86959772e-02],
[ 1.99623536e-01, 9.40698784e-01, -2.74292255e-01],
[ 4.03123289e-01, 7.31595100e-01, -5.49781978e-01],
[ 7.31922351e-01, 1.75805873e-01, -6.58317527e-01],
[ 7.28641097e-01, 5.18680158e-01, -4.47272899e-01],
[ 8.52419436e-01, 5.18867075e-01, -6.44830465e-02],
[ 8.57252640e-01, 1.76136461e-01, 4.83832469e-01],
[ 9.79328763e-01, 1.75544517e-01, 1.00495257e-01]])

#run through data
mgp.rescale_bvec(str(temp_in1),str(temp_out1))
mgp.rescale_bvec(str(temp_in2),str(temp_out2))
#open data
test1 = np.loadtxt(str(temp_out1))
test2 = np.loadtxt(str(temp_out2))

assert np.allclose(test1,output)
assert np.allclose(test2,output)