<commit_before>import os
import unittest
import datetime as dt

from webtest import TestApp

from backend.config import WORK_DIR
from backend.run_webservices import app
import backend.processing as bp

import ocw.metrics as metrics
import ocw.data_source.rcmed as rcmed
from ocw.dataset import Dataset
from ocw.evaluation import Evaluation

import numpy

test_app = TestApp(app)

class TestLocalDatasetLoad(unittest.TestCase):
    def setUp(self):
        self.dataset_object = {
            'dataset_id': os.path.abspath('/tmp/d1.nc'),
            'var_name': 'tasmax',
            'lat_name': 'lat',
            'lon_name': 'lon',
            'time_name': 'time'
        }

    def test_valid_load(self):
        dataset = bp._load_local_dataset_object(self.dataset_object)
        self.assertEqual(dataset.variable, self.dataset_object['var_name'])

    def test_default_name_assignment(self):
        dataset = bp._load_local_dataset_object(self.dataset_object)
        self.assertEqual(dataset.name, 'd1.nc')

    def test_custom_name_assignment(self):
        self.dataset_object['name'] = 'CustomName'
        dataset = bp._load_local_dataset_object(self.dataset_object)
        self.assertEqual(dataset.name, self.dataset_object['name'])

class TestDatasetProessingHelper(unittest.TestCase):
    def test_invalid_process_dataset_objects(self):
        invalid_dataset_object = {'data_source_id': 3, 'dataset_info': {}}
        self.assertRaises(
                ValueError,
                bp._process_dataset_object,
                invalid_dataset_object, 'fake parameter')

class TestRCMEDDatasetLoad(unittest.TestCase):
    def setUp(self):
        metadata = rcmed.get_parameters_metadata()
        # Load TRMM from RCMED
        dataset_dat = [m for m in metadata if m['parameter_id'] == '36'][0]

        self.dataset_info = {
            'dataset_id': int(dataset_dat['dataset_id']),
            'parameter_id': int(dataset_dat['parameter_id'])
        }

        self.eval_bounds = {
            'start_time': dt.datetime(1998, 02, 01),
            'end_time': dt.datetime(1998, 03, 01),
            'lat_min': -10,
            'lat_max': 10,
            'lon_min': -15,
            'lon_max': 15
        }

    def test_valid_load(self):
        dataset = bp._load_rcmed_dataset_object(self.dataset_info, self.eval_bounds)
        lat_min, lat_max, lon_min, lon_max = dataset.spatial_boundaries()
        start_time, end_time = dataset.time_range()

        self.assertTrue(self.eval_bounds['lat_min'] <= lat_min)
        self.assertTrue(self.eval_bounds['lat_max'] >= lat_max)
        self.assertTrue(self.eval_bounds['lon_min'] <= lon_min)
        self.assertTrue(self.eval_bounds['lon_max'] >= lon_max)
        self.assertTrue(self.eval_bounds['start_time'] <= start_time)
        self.assertTrue(self.eval_bounds['end_time'] >= end_time)

    def test_default_name_assignment(self):
        dataset = bp._load_rcmed_dataset_object(self.dataset_info, self.eval_bounds)
        self.assertEquals(dataset.name, 'TRMM v.7 Monthly Precipitation')

    def test_custom_name_assignment(self):
        self.dataset_info['name'] = 'CustomName'
        dataset = bp._load_rcmed_dataset_object(self.dataset_info, self.eval_bounds)
        self.assertEquals(dataset.name, self.dataset_info['name'])


class TestMetricLoad(unittest.TestCase):
    def test_get_valid_metric_options(self):
        metric_map = bp._get_valid_metric_options()
        bias = metric_map['Bias']()
        self.assertTrue(isinstance(bias, metrics.Bias))

    def test_valid_metric_load(self):
        metric_objs = bp._load_metrics(['Bias', 'TemporalStdDev'])
        self.assertTrue(isinstance(metric_objs[0], metrics.Bias))
        self.assertTrue(isinstance(metric_objs[1], metrics.TemporalStdDev))

    def test_invalid_metric_load(self):
        self.assertRaises(ValueError, bp._load_metrics, ['AAA'])

class TestSpatialRebinHelpers(unittest.TestCase):
    def test_latlon_bin_helper(self):
        eval_bounds = {
            'lat_min': -57.2,
            'lat_max': 58.2,
            'lon_min': -45.3,
            'lon_max': 39.2,
        }<commit_msg>CLIMATE-416 - Remove Unary metric load from processing test<commit_after>import os
import unittest
import datetime as dt

from webtest import TestApp

from backend.config import WORK_DIR
from backend.run_webservices import app
import backend.processing as bp

import ocw.metrics as metrics
import ocw.data_source.rcmed as rcmed
from ocw.dataset import Dataset
from ocw.evaluation import Evaluation

import numpy

test_app = TestApp(app)

class TestLocalDatasetLoad(unittest.TestCase):
    def setUp(self):
        self.dataset_object = {
            'dataset_id': os.path.abspath('/tmp/d1.nc'),
            'var_name': 'tasmax',
            'lat_name': 'lat',
            'lon_name': 'lon',
            'time_name': 'time'
        }

    def test_valid_load(self):
        dataset = bp._load_local_dataset_object(self.dataset_object)
        self.assertEqual(dataset.variable, self.dataset_object['var_name'])

    def test_default_name_assignment(self):
        dataset = bp._load_local_dataset_object(self.dataset_object)
        self.assertEqual(dataset.name, 'd1.nc')

    def test_custom_name_assignment(self):
        self.dataset_object['name'] = 'CustomName'
        dataset = bp._load_local_dataset_object(self.dataset_object)
        self.assertEqual(dataset.name, self.dataset_object['name'])

class TestDatasetProessingHelper(unittest.TestCase):
    def test_invalid_process_dataset_objects(self):
        invalid_dataset_object = {'data_source_id': 3, 'dataset_info': {}}
        self.assertRaises(
                ValueError,
                bp._process_dataset_object,
                invalid_dataset_object, 'fake parameter')

class TestRCMEDDatasetLoad(unittest.TestCase):
    def setUp(self):
        metadata = rcmed.get_parameters_metadata()
        # Load TRMM from RCMED
        dataset_dat = [m for m in metadata if m['parameter_id'] == '36'][0]

        self.dataset_info = {
            'dataset_id': int(dataset_dat['dataset_id']),
            'parameter_id': int(dataset_dat['parameter_id'])
        }

        self.eval_bounds = {
            'start_time': dt.datetime(1998, 02, 01),
            'end_time': dt.datetime(1998, 03, 01),
            'lat_min': -10,
            'lat_max': 10,
            'lon_min': -15,
            'lon_max': 15
        }

    def test_valid_load(self):
        dataset = bp._load_rcmed_dataset_object(self.dataset_info, self.eval_bounds)
        lat_min, lat_max, lon_min, lon_max = dataset.spatial_boundaries()
        start_time, end_time = dataset.time_range()

        self.assertTrue(self.eval_bounds['lat_min'] <= lat_min)
        self.assertTrue(self.eval_bounds['lat_max'] >= lat_max)
        self.assertTrue(self.eval_bounds['lon_min'] <= lon_min)
        self.assertTrue(self.eval_bounds['lon_max'] >= lon_max)
        self.assertTrue(self.eval_bounds['start_time'] <= start_time)
        self.assertTrue(self.eval_bounds['end_time'] >= end_time)

    def test_default_name_assignment(self):
        dataset = bp._load_rcmed_dataset_object(self.dataset_info, self.eval_bounds)
        self.assertEquals(dataset.name, 'TRMM v.7 Monthly Precipitation')

    def test_custom_name_assignment(self):
        self.dataset_info['name'] = 'CustomName'
        dataset = bp._load_rcmed_dataset_object(self.dataset_info, self.eval_bounds)
        self.assertEquals(dataset.name, self.dataset_info['name'])


class TestMetricLoad(unittest.TestCase):
    def test_get_valid_metric_options(self):
        metric_map = bp._get_valid_metric_options()
        bias = metric_map['Bias']()
        self.assertTrue(isinstance(bias, metrics.Bias))

    def test_valid_metric_load(self):
        metric_objs = bp._load_metrics(['Bias'])
        self.assertTrue(isinstance(metric_objs[0], metrics.Bias))

    def test_invalid_metric_load(self):
        self.assertRaises(ValueError, bp._load_metrics, ['AAA'])

class TestSpatialRebinHelpers(unittest.TestCase):
    def test_latlon_bin_helper(self):
        eval_bounds = {
            'lat_min': -57.2,
            'lat_max': 58.2,
            'lon_min': -45.3,
            'lon_max': 39.2,
        }
{'fraction_non_alphanumeric': 0.1118117875333495, 'fraction_numerical': 0.011156924569488236, 'mean_word_length': 3.144221105527638, 'pattern_counts': {'":': 0, '<': 9, '<?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': '3002490', 'n_tokens_mistral': 2653, 'n_tokens_neox': 2568, 'n_words': 434}