<commit_before># emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
""" Test scripts

Run scripts and check outputs
"""
from __future__ import division, print_function, absolute_import

import glob
import os
import shutil

from os.path import (dirname, join as pjoin, abspath)

from nose.tools import assert_true, assert_false, assert_equal
import numpy.testing as nt

import nibabel as nib
from nibabel.tmpdirs import InTemporaryDirectory

from dipy.data import get_data

# Quickbundles command-line requires matplotlib: 
try:
    import matplotlib
    no_mpl = False
except ImportError:
    no_mpl = True

from .scriptrunner import ScriptRunner

runner = ScriptRunner(
    script_sdir = 'bin',
    debug_print_var = 'NIPY_DEBUG_PRINT')
run_command = runner.run_command

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

def test_dipy_peak_extraction():
    # test dipy_peak_extraction script
    cmd = 'dipy_peak_extraction'
    code, stdout, stderr = run_command(cmd, check_code=False)
    assert_equal(code, 2)


def test_dipy_fit_tensor():
    # test dipy_fit_tensor script
    cmd = 'dipy_fit_tensor'
    code, stdout, stderr = run_command(cmd, check_code=False)
    assert_equal(code, 2)


def test_dipy_sh_estimate():
    # test dipy_sh_estimate script
    cmd = 'dipy_sh_estimate'
    code, stdout, stderr = run_command(cmd, check_code=False)
    assert_equal(code, 2)


def assert_image_shape_affine(filename, shape, affine):
    assert_true(os.path.isfile(filename))
    image = nib.load(filename)
    assert_equal(image.shape, shape)
    nt.assert_array_almost_equal(image.get_affine(), affine)


def test_dipy_fit_tensor_again():
    with InTemporaryDirectory():
        dwi, bval, bvec = get_data("small_25")
        # Copy data to tmp directory
        shutil.copyfile(dwi, "small_25.nii.gz")
        shutil.copyfile(bval, "small_25.bval")
        shutil.copyfile(bvec, "small_25.bvec")
        # Call script
        cmd = ["dipy_fit_tensor", "--mask=none", "small_25.nii.gz"]
        out = run_command(cmd)
        assert_equal(out[0], 0)
        # Get expected values
        img = nib.load("small_25.nii.gz")
        affine = img.get_affine()
        shape = img.shape[:-1]
        # Check expected outputs
        assert_image_shape_affine("small_25_fa.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_t2di.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_dirFA.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_ad.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_md.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_rd.nii.gz", shape, affine)

    with InTemporaryDirectory():
        dwi, bval, bvec = get_data("small_25")
        # Copy data to tmp directory
        shutil.copyfile(dwi, "small_25.nii.gz")
        shutil.copyfile(bval, "small_25.bval")
        shutil.copyfile(bvec, "small_25.bvec")
        # Call script
        cmd = ["dipy_fit_tensor", "--save-tensor", "--mask=none", "small_25.nii.gz"]
        out = run_command(cmd)
        assert_equal(out[0], 0)
        # Get expected values
        img = nib.load("small_25.nii.gz")
        affine = img.get_affine()
        shape = img.shape[:-1]
        # Check expected outputs
        assert_image_shape_affine("small_25_fa.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_t2di.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_dirFA.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_ad.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_md.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_rd.nii.gz", shape, affine)
        # small_25_tensor saves the tensor as a symmetric matrix following
        # the nifti standard.
        ten_shape = shape + (1, 6)
        assert_image_shape_affine("small_25_tensor.nii.gz", ten_shape,
                                  affine)

@nt.dec.skipif(no_mpl)
def test_qb_commandline():
    with InTemporaryDirectory():
        tracks_file = get_data('fornix')
        cmd = ["dipy_quickbundles", tracks_file, '--pkl_file', 'mypickle.pkl',
               '--out_file', 'tracks300.trk']
        out = run_command(cmd)
        assert_equal(out[0], 0)

@nt.dec.skipif(no_mpl)
def test_qb_commandline_output_path_handling():
    with InTemporaryDirectory():
        # Create temporary subdirectory for input and for output
        os.mkdir('work')
        os.mkdir('output')

        os.chdir('work')
        tracks_file = get_data('fornix')

        # Need to specify an output directory with a "../" style path
        # to trigger old bug.
        cmd = ["dipy_quickbundles", tracks_file, '--pkl_file', 'mypickle.pkl',
               '--out_file', '../output/tracks300.trk']
        out = run_command(cmd)
        assert_equal(out[0], 0)

        # Make sure the files were created in the output directory
        os.chdir('../')
        output_files_list = glob.glob('output/tracks300_*.trk')
        assert_true(output_files_list)<commit_msg>BF: cross platform path handling.<commit_after># emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
""" Test scripts

Run scripts and check outputs
"""
from __future__ import division, print_function, absolute_import

import glob
import os
import shutil

from os.path import (dirname, join as pjoin, abspath)

from nose.tools import assert_true, assert_false, assert_equal
import numpy.testing as nt

import nibabel as nib
from nibabel.tmpdirs import InTemporaryDirectory

from dipy.data import get_data

# Quickbundles command-line requires matplotlib: 
try:
    import matplotlib
    no_mpl = False
except ImportError:
    no_mpl = True

from .scriptrunner import ScriptRunner

runner = ScriptRunner(
    script_sdir = 'bin',
    debug_print_var = 'NIPY_DEBUG_PRINT')
run_command = runner.run_command

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

def test_dipy_peak_extraction():
    # test dipy_peak_extraction script
    cmd = 'dipy_peak_extraction'
    code, stdout, stderr = run_command(cmd, check_code=False)
    assert_equal(code, 2)


def test_dipy_fit_tensor():
    # test dipy_fit_tensor script
    cmd = 'dipy_fit_tensor'
    code, stdout, stderr = run_command(cmd, check_code=False)
    assert_equal(code, 2)


def test_dipy_sh_estimate():
    # test dipy_sh_estimate script
    cmd = 'dipy_sh_estimate'
    code, stdout, stderr = run_command(cmd, check_code=False)
    assert_equal(code, 2)


def assert_image_shape_affine(filename, shape, affine):
    assert_true(os.path.isfile(filename))
    image = nib.load(filename)
    assert_equal(image.shape, shape)
    nt.assert_array_almost_equal(image.get_affine(), affine)


def test_dipy_fit_tensor_again():
    with InTemporaryDirectory():
        dwi, bval, bvec = get_data("small_25")
        # Copy data to tmp directory
        shutil.copyfile(dwi, "small_25.nii.gz")
        shutil.copyfile(bval, "small_25.bval")
        shutil.copyfile(bvec, "small_25.bvec")
        # Call script
        cmd = ["dipy_fit_tensor", "--mask=none", "small_25.nii.gz"]
        out = run_command(cmd)
        assert_equal(out[0], 0)
        # Get expected values
        img = nib.load("small_25.nii.gz")
        affine = img.get_affine()
        shape = img.shape[:-1]
        # Check expected outputs
        assert_image_shape_affine("small_25_fa.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_t2di.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_dirFA.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_ad.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_md.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_rd.nii.gz", shape, affine)

    with InTemporaryDirectory():
        dwi, bval, bvec = get_data("small_25")
        # Copy data to tmp directory
        shutil.copyfile(dwi, "small_25.nii.gz")
        shutil.copyfile(bval, "small_25.bval")
        shutil.copyfile(bvec, "small_25.bvec")
        # Call script
        cmd = ["dipy_fit_tensor", "--save-tensor", "--mask=none", "small_25.nii.gz"]
        out = run_command(cmd)
        assert_equal(out[0], 0)
        # Get expected values
        img = nib.load("small_25.nii.gz")
        affine = img.get_affine()
        shape = img.shape[:-1]
        # Check expected outputs
        assert_image_shape_affine("small_25_fa.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_t2di.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_dirFA.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_ad.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_md.nii.gz", shape, affine)
        assert_image_shape_affine("small_25_rd.nii.gz", shape, affine)
        # small_25_tensor saves the tensor as a symmetric matrix following
        # the nifti standard.
        ten_shape = shape + (1, 6)
        assert_image_shape_affine("small_25_tensor.nii.gz", ten_shape,
                                  affine)

@nt.dec.skipif(no_mpl)
def test_qb_commandline():
    with InTemporaryDirectory():
        tracks_file = get_data('fornix')
        cmd = ["dipy_quickbundles", tracks_file, '--pkl_file', 'mypickle.pkl',
               '--out_file', 'tracks300.trk']
        out = run_command(cmd)
        assert_equal(out[0], 0)

@nt.dec.skipif(no_mpl)
def test_qb_commandline_output_path_handling():
    with InTemporaryDirectory():
        # Create temporary subdirectory for input and for output
        os.mkdir('work')
        os.mkdir('output')

        os.chdir('work')
        tracks_file = get_data('fornix')

        # Need to specify an output directory with a "../" style path
        # to trigger old bug.
        cmd = ["dipy_quickbundles", tracks_file, '--pkl_file', 'mypickle.pkl',
               '--out_file', os.path.join('..', 'output', 'tracks300.trk')]
        out = run_command(cmd)
        assert_equal(out[0], 0)

        # Make sure the files were created in the output directory
        os.chdir('../')
        output_files_list = glob.glob('output/tracks300_*.trk')
        assert_true(output_files_list)
{'fraction_non_alphanumeric': 0.11279953243717125, 'fraction_numerical': 0.015975063315799728, 'mean_word_length': 3.1940359477124183, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2729609', 'n_tokens_mistral': 3668, 'n_tokens_neox': 3460, 'n_words': 717}