# Path: dicom2nifti/common.py
# def get_nifti_data(nifti_image):
#     """
#     Function that replicates the deprecated nifti.get_data behavior
#     """
#     return numpy.asanyarray(nifti_image.dataobj)


import os
import logging
import tempfile
import nibabel
import numpy
import shutil
import dicom2nifti.image_reorientation as image_reorientation
from dicom2nifti.common import get_nifti_data


def assert_compare_nifti(nifti_file_1, nifti_file_2):
    logging.info("%s %s" % (nifti_file_1, nifti_file_2))
    work_dir = tempfile.mkdtemp()
    try:
        tmp_nifti_file_1 = os.path.join(work_dir, os.path.basename(nifti_file_1))
        tmp_nifti_file_2 = os.path.join(work_dir, os.path.basename(nifti_file_2))
        image_reorientation.reorient_image(nifti_file_1, tmp_nifti_file_1)
        image_reorientation.reorient_image(nifti_file_2, tmp_nifti_file_2)
        nifti_1 = nibabel.load(tmp_nifti_file_1)
        nifti_2 = nibabel.load(tmp_nifti_file_2)

        # check the affine
        if not numpy.allclose(nifti_1.affine, nifti_2.affine):
            raise Exception('affine mismatch')

        # check the data
        nifti_1_data = get_nifti_data(nifti_1)
        nifti_2_data = get_nifti_data(nifti_2)

        # in case of rgba data we should stack the data again
        if nifti_1.get_data_dtype() == [('R', 'u1'), ('G', 'u1'), ('B', 'u1'), ('A', 'u1')]:
            nifti_1_data = numpy.stack([nifti_1_data['R'], nifti_1_data['G'], nifti_1_data['B'], nifti_1_data['A']])
        if nifti_2.get_data_dtype() == [('R', 'u1'), ('G', 'u1'), ('B', 'u1'), ('A', 'u1')]:
            nifti_2_data = numpy.stack([nifti_2_data['R'], nifti_2_data['G'], nifti_2_data['B'], nifti_2_data['A']])
            
        if nifti_1.get_data_dtype() != nifti_2.get_data_dtype():
            raise Exception('dtype mismatch')
        if not numpy.allclose(nifti_1_data, nifti_2_data, rtol=0.01, atol=1):

# Path: dicom2nifti/common.py
# def get_nifti_data(nifti_image):
#     """
#     Function that replicates the deprecated nifti.get_data behavior
#     """
#     return numpy.asanyarray(nifti_image.dataobj)


# Path: tests/test_tools.py
import os
import logging
import tempfile
import nibabel
import numpy
import shutil
import dicom2nifti.image_reorientation as image_reorientation
from dicom2nifti.common import get_nifti_data



def assert_compare_nifti(nifti_file_1, nifti_file_2):
    logging.info("%s %s" % (nifti_file_1, nifti_file_2))
    work_dir = tempfile.mkdtemp()
    try:
        tmp_nifti_file_1 = os.path.join(work_dir, os.path.basename(nifti_file_1))
        tmp_nifti_file_2 = os.path.join(work_dir, os.path.basename(nifti_file_2))
        image_reorientation.reorient_image(nifti_file_1, tmp_nifti_file_1)
        image_reorientation.reorient_image(nifti_file_2, tmp_nifti_file_2)
        nifti_1 = nibabel.load(tmp_nifti_file_1)
        nifti_2 = nibabel.load(tmp_nifti_file_2)

        # check the affine
        if not numpy.allclose(nifti_1.affine, nifti_2.affine):
            raise Exception('affine mismatch')

        # check the data
        nifti_1_data = get_nifti_data(nifti_1)
        nifti_2_data = get_nifti_data(nifti_2)

        # in case of rgba data we should stack the data again
        if nifti_1.get_data_dtype() == [('R', 'u1'), ('G', 'u1'), ('B', 'u1'), ('A', 'u1')]:
            nifti_1_data = numpy.stack([nifti_1_data['R'], nifti_1_data['G'], nifti_1_data['B'], nifti_1_data['A']])
        if nifti_2.get_data_dtype() == [('R', 'u1'), ('G', 'u1'), ('B', 'u1'), ('A', 'u1')]:
            nifti_2_data = numpy.stack([nifti_2_data['R'], nifti_2_data['G'], nifti_2_data['B'], nifti_2_data['A']])
            
        if nifti_1.get_data_dtype() != nifti_2.get_data_dtype():
            raise Exception('dtype mismatch')
        if not numpy.allclose(nifti_1_data, nifti_2_data, rtol=0.01, atol=1):

            difference = get_nifti_data(nifti_1) - get_nifti_data(nifti_2)