<commit_before>"""
Tests for prepifg.py: resampling, subsetting etc.

.. codeauthor:: <NAME>, <NAME>
"""

import os, sys, unittest
from os.path import exists, join

from math import floor
import numpy as np
from scipy.stats.stats import nanmean
from numpy import isnan, nanmax, nanmin
from numpy import ones, nan, reshape, sum as npsum
from numpy.testing import assert_array_almost_equal, assert_array_equal

from pyrate import config as cfg
from pyrate.shared import Ifg, DEM
from pyrate.prepifg import CUSTOM_CROP, MAXIMUM_CROP, MINIMUM_CROP, ALREADY_SAME_SIZE
from pyrate.prepifg import prepare_ifgs, resample, PreprocessError, CustomExts
from pyrate.prepifg import mlooked_path, extents_from_params
from pyrate.tests.common import SYD_TEST_MATLAB_PREPIFG_DIR
from pyrate.tests.common import PREP_TEST_TIF, SYD_TEST_DEM_DIR
from pyrate.tests.common import SYD_TEST_DEM_TIF

from osgeo import gdal
gdal.UseExceptions()

if not exists(PREP_TEST_TIF):
    sys.exit("ERROR: Missing 'prepifg' dir for unittests\n")


# convenience ifg creation funcs
def diff_exts_ifgs():
    """Returns pair of test Ifgs with different extents"""
    bases = ['geo_060619-061002.tif', 'geo_070326-070917.tif']
    return [Ifg(join(PREP_TEST_TIF, p)) for p in bases]


def same_exts_ifgs():
    """Return pair of Ifgs with same extents"""
    return [Ifg(join(PREP_TEST_TIF, f)) for f in ('0.tif', '1.tif')]


def test_extents_from_params():
    xf, yf = 1.0, 2.0
    xl, yl = 5.0, 7.0
    pars = {cfg.IFG_XFIRST: xf, cfg.IFG_XLAST: xl,
            cfg.IFG_YFIRST: yf, cfg.IFG_YLAST: yl}

    assert extents_from_params(pars) == CustomExts(xf, yf, xl, yl)


class PrepifgOutputTests(unittest.TestCase):
    """Tests aspects of the prepifg.py script, such as resampling."""

    def __init__(self, *args, **kwargs):
        super(PrepifgOutputTests, self).__init__(*args, **kwargs)

    @staticmethod
    def assert_geotransform_equal(files):
        """
        Asserts geotransforms for the given files are equivalent. Files can be paths
        to datasets, or GDAL dataset objects.
        """
        assert len(files) > 1, "Need more than 1 file to compare"
        if not all([hasattr(f, "GetGeoTransform") for f in files]):
            datasets = [gdal.Open(f) for f in files]
            assert all(datasets)
        else:
            datasets = files

        transforms = [ds.GetGeoTransform() for ds in datasets]
        head = transforms[0]
        for t in transforms[1:]:
            assert t == head, "Extents do not match!"

    def setUp(self):
        self.xs = 0.000833333
        self.ys = -self.xs

        # FIXME: dump output to tmp dir?
        self.ifgs = diff_exts_ifgs()
        paths = ["geo_060619-061002_1rlks_1cr.tif",
                 "geo_060619-061002_1rlks_2cr.tif",
                 "geo_060619-061002_1rlks_3cr.tif",
                 "geo_060619-061002_4rlks_3cr.tif",
                 "geo_070326-070917_1rlks_1cr.tif",
                 "geo_070326-070917_1rlks_2cr.tif",
                 "geo_070326-070917_1rlks_3cr.tif",
                 "geo_070326-070917_4rlks_3cr.tif"]
        self.exp_files = [join(PREP_TEST_TIF, p) for p in paths]

    def test_mlooked_paths(self):
        test_mlooked_path()

    def test_extents_from_params(self):
        test_extents_from_params()

    def tearDown(self):
        for f in self.exp_files:
            if exists(f):
                os.remove(f)

    def _custom_ext_latlons(self):
        return [150.91 + (7 * self.xs),  # xfirst
                -34.17 + (16 * self.ys),  # yfirst
                150.91 + (27 * self.xs),  # 20 cells from xfirst
                -34.17 + (44 * self.ys)]  # 28 cells from yfirst<commit_msg>[test_prepifg] actioned  `# FIXME: dump output to tmp dir?`<commit_after>"""
Tests for prepifg.py: resampling, subsetting etc.

.. codeauthor:: <NAME>, <NAME>
"""

import os, sys, unittest
from os.path import exists, join
import shutil

from math import floor
import numpy as np
from scipy.stats.stats import nanmean
from numpy import isnan, nanmax, nanmin
from numpy import ones, nan, reshape, sum as npsum
from numpy.testing import assert_array_almost_equal, assert_array_equal

from pyrate import config as cfg
from pyrate.shared import Ifg, DEM
from pyrate.prepifg import CUSTOM_CROP, MAXIMUM_CROP, MINIMUM_CROP, ALREADY_SAME_SIZE
from pyrate.prepifg import prepare_ifgs, resample, PreprocessError, CustomExts
from pyrate.prepifg import mlooked_path, extents_from_params
from pyrate.tests.common import SYD_TEST_MATLAB_PREPIFG_DIR
from pyrate.tests.common import PREP_TEST_TIF, SYD_TEST_DEM_DIR
from pyrate.tests.common import SYD_TEST_DEM_TIF

from osgeo import gdal
gdal.UseExceptions()

if not exists(PREP_TEST_TIF):
    sys.exit("ERROR: Missing 'prepifg' dir for unittests\n")


# convenience ifg creation funcs
def diff_exts_ifgs():
    """Returns pair of test Ifgs with different extents"""
    bases = ['geo_060619-061002.tif', 'geo_070326-070917.tif']
    for p in bases:
        shutil.copy(src=os.path.join(PREP_TEST_TIF, p),
                    dst=os.path.join('/tmp', p))
    return [Ifg(join('/tmp', p)) for p in bases]


def same_exts_ifgs():
    """Return pair of Ifgs with same extents"""
    return [Ifg(join(PREP_TEST_TIF, f)) for f in ('0.tif', '1.tif')]


def test_extents_from_params():
    xf, yf = 1.0, 2.0
    xl, yl = 5.0, 7.0
    pars = {cfg.IFG_XFIRST: xf, cfg.IFG_XLAST: xl,
            cfg.IFG_YFIRST: yf, cfg.IFG_YLAST: yl}

    assert extents_from_params(pars) == CustomExts(xf, yf, xl, yl)


class PrepifgOutputTests(unittest.TestCase):
    """Tests aspects of the prepifg.py script, such as resampling."""

    def __init__(self, *args, **kwargs):
        super(PrepifgOutputTests, self).__init__(*args, **kwargs)

    @staticmethod
    def assert_geotransform_equal(files):
        """
        Asserts geotransforms for the given files are equivalent. Files can be paths
        to datasets, or GDAL dataset objects.
        """
        assert len(files) > 1, "Need more than 1 file to compare"
        if not all([hasattr(f, "GetGeoTransform") for f in files]):
            datasets = [gdal.Open(f) for f in files]
            assert all(datasets)
        else:
            datasets = files

        transforms = [ds.GetGeoTransform() for ds in datasets]
        head = transforms[0]
        for t in transforms[1:]:
            assert t == head, "Extents do not match!"

    def setUp(self):
        self.xs = 0.000833333
        self.ys = -self.xs
        self.ifgs = diff_exts_ifgs()
        paths = ["geo_060619-061002_1rlks_1cr.tif",
                 "geo_060619-061002_1rlks_2cr.tif",
                 "geo_060619-061002_1rlks_3cr.tif",
                 "geo_060619-061002_4rlks_3cr.tif",
                 "geo_070326-070917_1rlks_1cr.tif",
                 "geo_070326-070917_1rlks_2cr.tif",
                 "geo_070326-070917_1rlks_3cr.tif",
                 "geo_070326-070917_4rlks_3cr.tif"]
        self.exp_files = [join('/tmp', p) for p in paths]

    def test_mlooked_paths(self):
        test_mlooked_path()

    def test_extents_from_params(self):
        test_extents_from_params()

    def tearDown(self):
        for f in self.exp_files:
            if exists(f):
                os.remove(f)

    def _custom_ext_latlons(self):
        return [150.91 + (7 * self.xs),  # xfirst
                -34.17 + (16 * self.ys),  # yfirst
                150.91 + (27 * self.xs),  # 20 cells from xfirst
                -34.17 + (44 * self.ys)]  # 28 cells from yfirst
{'fraction_non_alphanumeric': 0.10334265548009056, 'fraction_numerical': 0.05033959248901319, 'mean_word_length': 2.7891019172552975, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '638478', 'n_tokens_mistral': 2937, 'n_tokens_neox': 2611, 'n_words': 672}