Skip to content

Commit 03f21ad

Browse files
committed
ENH: Add appveyor and improve test
1 parent baa6ec0 commit 03f21ad

File tree

3 files changed

+156
-6
lines changed

3 files changed

+156
-6
lines changed

appveyor.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# vim ft=yaml
2+
# CI on Windows via appveyor
3+
# This file was based on Olivier Grisel's python-appveyor-demo
4+
5+
environment:
6+
7+
matrix:
8+
- PYTHON: "C:\\Python34-conda32"
9+
PYTHON_VERSION: "3.4"
10+
PYTHON_ARCH: "32"
11+
12+
- PYTHON: "C:\\Python34-conda64"
13+
PYTHON_VERSION: "3.4"
14+
PYTHON_ARCH: "64"
15+
16+
install:
17+
# Install miniconda Python
18+
- "powershell ./nibabel-data/install_python.ps1"
19+
20+
# Prepend newly installed Python to the PATH of this build (this cannot be
21+
# done from inside the powershell script as it would require to restart
22+
# the parent CMD process).
23+
- "SET PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%"
24+
25+
# Check that we have the expected version and architecture for Python
26+
- "python --version"
27+
- "python -c \"import struct; print(struct.calcsize('P') * 8)\""
28+
29+
# Install the dependencies of the project.
30+
- "conda install --yes --quiet numpy scipy matplotlib nose h5py"
31+
- "pip install pydicom"
32+
- "python setup.py install"
33+
- "SET NIBABEL_DATA_DIR=%CD%\\nibabel-data"
34+
35+
build: false # Not a C# project, build stuff at the test step instead.
36+
37+
test_script:
38+
# Change into an innocuous directory and find tests from installation
39+
- "mkdir for_testing"
40+
- "cd for_testing"
41+
- "nosetests --with-doctest nibabel"

nibabel-data/install_python.ps1

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Sample script to install Python and pip under Windows
2+
# Authors: Olivier Grisel, Jonathan Helmus and Kyle Kastner
3+
# License: CC0 1.0 Universal: http://creativecommons.org/publicdomain/zero/1.0/
4+
5+
$MINICONDA_URL = "http://repo.continuum.io/miniconda/"
6+
$BASE_URL = "https://www.python.org/ftp/python/"
7+
8+
9+
function DownloadMiniconda ($python_version, $platform_suffix) {
10+
$webclient = New-Object System.Net.WebClient
11+
if ($python_version -eq "3.4") {
12+
$filename = "Miniconda3-3.5.5-Windows-" + $platform_suffix + ".exe"
13+
} else {
14+
$filename = "Miniconda-3.5.5-Windows-" + $platform_suffix + ".exe"
15+
}
16+
$url = $MINICONDA_URL + $filename
17+
18+
$basedir = $pwd.Path + "\"
19+
$filepath = $basedir + $filename
20+
if (Test-Path $filename) {
21+
Write-Host "Reusing" $filepath
22+
return $filepath
23+
}
24+
25+
# Download and retry up to 3 times in case of network transient errors.
26+
Write-Host "Downloading" $filename "from" $url
27+
$retry_attempts = 2
28+
for($i=0; $i -lt $retry_attempts; $i++){
29+
try {
30+
$webclient.DownloadFile($url, $filepath)
31+
break
32+
}
33+
Catch [Exception]{
34+
Start-Sleep 1
35+
}
36+
}
37+
if (Test-Path $filepath) {
38+
Write-Host "File saved at" $filepath
39+
} else {
40+
# Retry once to get the error message if any at the last try
41+
$webclient.DownloadFile($url, $filepath)
42+
}
43+
return $filepath
44+
}
45+
46+
47+
function InstallMiniconda ($python_version, $architecture, $python_home) {
48+
Write-Host "Installing Python" $python_version "for" $architecture "bit architecture to" $python_home
49+
if (Test-Path $python_home) {
50+
Write-Host $python_home "already exists, skipping."
51+
return $false
52+
}
53+
if ($architecture -eq "32") {
54+
$platform_suffix = "x86"
55+
} else {
56+
$platform_suffix = "x86_64"
57+
}
58+
$filepath = DownloadMiniconda $python_version $platform_suffix
59+
Write-Host "Installing" $filepath "to" $python_home
60+
$install_log = $python_home + ".log"
61+
$args = "/S /D=$python_home"
62+
Write-Host $filepath $args
63+
Start-Process -FilePath $filepath -ArgumentList $args -Wait -Passthru
64+
if (Test-Path $python_home) {
65+
Write-Host "Python $python_version ($architecture) installation complete"
66+
} else {
67+
Write-Host "Failed to install Python in $python_home"
68+
Get-Content -Path $install_log
69+
Exit 1
70+
}
71+
}
72+
73+
74+
function InstallMinicondaPip ($python_home) {
75+
$pip_path = $python_home + "\Scripts\pip.exe"
76+
$conda_path = $python_home + "\Scripts\conda.exe"
77+
if (-not(Test-Path $pip_path)) {
78+
Write-Host "Installing pip..."
79+
$args = "install --yes pip"
80+
Write-Host $conda_path $args
81+
Start-Process -FilePath "$conda_path" -ArgumentList $args -Wait -Passthru
82+
} else {
83+
Write-Host "pip already installed."
84+
}
85+
}
86+
87+
88+
function main () {
89+
InstallMiniconda $env:PYTHON_VERSION $env:PYTHON_ARCH $env:PYTHON
90+
InstallMinicondaPip $env:PYTHON
91+
}
92+
93+
main

nibabel/tests/test_scripts.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def script_test(func):
5252

5353
DATA_PATH = abspath(pjoin(dirname(__file__), 'data'))
5454

55+
5556
@script_test
5657
def test_nib_ls():
5758
# test nib-ls script
@@ -84,7 +85,7 @@ def test_nib_nifti_dx():
8485

8586

8687
def vox_size(affine):
87-
return np.sqrt(np.sum(affine[:3,:3] ** 2, axis=0))
88+
return np.sqrt(np.sum(affine[:3, :3] ** 2, axis=0))
8889

8990

9091
def check_conversion(cmd, pr_data, out_fname):
@@ -184,7 +185,7 @@ def test_parrec2nii_with_data():
184185
par_root, ext = splitext(basename(par))
185186
# NA.PAR appears to be a localizer, with three slices in each of
186187
# the three orientations: sagittal; coronal, transverse
187-
if par_root == 'NA':
188+
if par_root == 'NA':
188189
continue
189190
# Do conversion
190191
run_command(['parrec2nii', par])
@@ -221,10 +222,6 @@ def test_parrec2nii_with_data():
221222
assert_true(exists('DTI.nii'))
222223
assert_false(exists('DTI.bvals'))
223224
assert_false(exists('DTI.bvecs'))
224-
# ensure trace was removed
225-
img = load('DTI.nii')
226-
assert_equal(img.shape[-1], len(DTI_PAR_BVALS) - 1)
227-
del img
228225
# Does not overwrite unless option given
229226
code, stdout, stderr = run_command(['parrec2nii', dti_par],
230227
check_code=False)
@@ -233,6 +230,9 @@ def test_parrec2nii_with_data():
233230
run_command(['parrec2nii', '--overwrite', '--keep-trace',
234231
'--bvs', dti_par])
235232
assert_almost_equal(np.loadtxt('DTI.bvals'), DTI_PAR_BVALS)
233+
img = load('DTI.nii')
234+
data = img.get_data().copy()
235+
del img
236236
# Bvecs in header, transposed from PSL to LPS
237237
bvecs_LPS = DTI_PAR_BVECS[:, [2, 0, 1]]
238238
# Adjust for output flip of Y axis in data and bvecs
@@ -252,3 +252,19 @@ def test_parrec2nii_with_data():
252252
with open('DTI.dwell_time', 'rt') as fobj:
253253
contents = fobj.read().strip()
254254
assert_almost_equal(float(contents), exp_dwell)
255+
# ensure trace is removed by default
256+
run_command(['parrec2nii', '--overwrite', '--bvs', dti_par])
257+
assert_true(exists('DTI.bvals'))
258+
assert_true(exists('DTI.bvecs'))
259+
img = load('DTI.nii')
260+
bvecs_notrace = np.loadtxt('DTI.bvecs').T
261+
bvals_notrace = np.loadtxt('DTI.bvals')
262+
data_notrace = img.get_data().copy()
263+
assert_equal(data_notrace.shape[-1], len(bvecs_notrace))
264+
del img
265+
# ensure correct volume was removed
266+
good_mask = np.logical_or((bvecs_notrace != 0).any(axis=1),
267+
bvals_notrace == 0)
268+
assert_almost_equal(data_notrace, data[..., good_mask])
269+
assert_almost_equal(bvals_notrace, np.array(DTI_PAR_BVALS)[good_mask])
270+
assert_almost_equal(bvecs_notrace, bvecs_LAS[good_mask])

0 commit comments

Comments
 (0)