# -*- coding: utf-8 -*-
"""
dicom2nifti

@author: abrys
"""

import os
import shutil
import tempfile
import unittest

import dicom2nifti.convert_ge as convert_ge
import dicom2nifti.tests.test_data as test_data
from dicom2nifti.common import read_dicom_directory
from dicom2nifti.tests.test_tools import compare_nifti, compare_bval, compare_bvec, ground_thruth_filenames


class TestConversionGE(unittest.TestCase):
    def test_diffusion_images(self):
        tmp_output_dir = tempfile.mkdtemp()
        try:
            results = convert_ge.dicom_to_nifti(read_dicom_directory(test_data.GE_DTI),
                                                os.path.join(tmp_output_dir, 'test.nii.gz'))
            assert compare_nifti(results['NII_FILE'],
                                 ground_thruth_filenames(test_data.GE_DTI)[0]) == True
            assert compare_bval(results['BVAL_FILE'],
                                ground_thruth_filenames(test_data.GE_DTI)[2]) == True
            assert compare_bvec(results['BVEC_FILE'],
                                ground_thruth_filenames(test_data.GE_DTI)[3]) == True

            convert_ge.dicom_to_nifti(read_dicom_directory(test_data.GE_DTI_IMPLICIT),
                                      os.path.join(tmp_output_dir, 'test.nii.gz'))
            assert compare_nifti(results['NII_FILE'],
                                 ground_thruth_filenames(test_data.GE_DTI_IMPLICIT)[0]) == True
            assert compare_bval(results['BVAL_FILE'],
                                ground_thruth_filenames(test_data.GE_DTI_IMPLICIT)[2]) == True
            assert compare_bvec(results['BVEC_FILE'],
                                ground_thruth_filenames(test_data.GE_DTI_IMPLICIT)[3]) == True
        finally:
            shutil.rmtree(tmp_output_dir)

    def test_4d(self):
        tmp_output_dir = tempfile.mkdtemp()
        try:
            results = convert_ge.dicom_to_nifti(read_dicom_directory(test_data.GE_FMRI),
                                                os.path.join(tmp_output_dir, 'test.nii.gz'))
            assert compare_nifti(results['NII_FILE'],
                                 ground_thruth_filenames(test_data.GE_FMRI)[0]) == True
            results = convert_ge.dicom_to_nifti(read_dicom_directory(test_data.GE_FMRI_IMPLICIT),
                                                os.path.join(tmp_output_dir, 'test.nii.gz'))
            assert compare_nifti(results['NII_FILE'],
                                 ground_thruth_filenames(test_data.GE_FMRI_IMPLICIT)[0]) == True
        finally:
            shutil.rmtree(tmp_output_dir)

    def test_anatomical(self):
        tmp_output_dir = tempfile.mkdtemp()
        try:
            results = convert_ge.dicom_to_nifti(read_dicom_directory(test_data.GE_ANATOMICAL),
                                                os.path.join(tmp_output_dir, 'test.nii.gz'))
            assert compare_nifti(results['NII_FILE'],
                                 ground_thruth_filenames(test_data.GE_ANATOMICAL)[0]) == True
            results = convert_ge.dicom_to_nifti(read_dicom_directory(test_data.GE_ANATOMICAL_IMPLICIT),
                                                os.path.join(tmp_output_dir, 'test.nii.gz'))
            assert compare_nifti(results['NII_FILE'],
                                 ground_thruth_filenames(test_data.GE_ANATOMICAL_IMPLICIT)[0]) == True
        finally:
            shutil.rmtree(tmp_output_dir)

    def test_is_ge(self):
        assert not convert_ge.is_ge(read_dicom_directory(test_data.SIEMENS_ANATOMICAL))
        assert convert_ge.is_ge(read_dicom_directory(test_data.GE_ANATOMICAL))
        assert not convert_ge.is_ge(read_dicom_directory(test_data.PHILIPS_ANATOMICAL))

    def test_is_4d(self):
        diffusion_group = convert_ge._get_grouped_dicoms(read_dicom_directory(test_data.GE_DTI))
        _4d_group = convert_ge._get_grouped_dicoms(read_dicom_directory(test_data.GE_FMRI))
        anatomical_group = convert_ge._get_grouped_dicoms(read_dicom_directory(test_data.GE_ANATOMICAL))
        assert convert_ge._is_4d(diffusion_group)
        assert convert_ge._is_4d(_4d_group)
        assert not convert_ge._is_4d(anatomical_group)

    def test_is_diffusion_imaging(self):
        diffusion_group = convert_ge._get_grouped_dicoms(read_dicom_directory(test_data.GE_DTI))
        _4d_group = convert_ge._get_grouped_dicoms(read_dicom_directory(test_data.GE_FMRI))
        anatomical_group = convert_ge._get_grouped_dicoms(read_dicom_directory(test_data.GE_ANATOMICAL))
        assert convert_ge._is_diffusion_imaging(diffusion_group)
        assert not convert_ge._is_diffusion_imaging(_4d_group)
        assert not convert_ge._is_diffusion_imaging(anatomical_group)


if __name__ == '__main__':
    unittest.main()
