Best Python code snippet using green
test_ma_scoring.py
Source:test_ma_scoring.py  
1import unittest2import sys3sys.path.append("..")4from main.express_score import (5    Scorer,6    MaScorer,7    Defaults8)9from main.schema import (10    JSONField,11    ScoreComponents12)13import pandas as pd14import numpy as np15from dateutil.parser import parse16import json17import os18from collections import Counter19EXPRESS_SCORE_HOME = os.path.abspath("..")20RESOURCE_PATH = os.path.join(EXPRESS_SCORE_HOME, "resources")21TEST_RESOURCE_PATH = os.path.join(RESOURCE_PATH, "test")22LB_MA_TEST_PATH = os.path.join(TEST_RESOURCE_PATH, "lb_ma_may_2018")23SA_MA_TEST_PATH = os.path.join(TEST_RESOURCE_PATH, "sa_ma_may_2018")24EG_MA_TEST_PATH = os.path.join(TEST_RESOURCE_PATH, "eg_ma_may_2018")25IQ_MA_TEST_PATH = os.path.join(TEST_RESOURCE_PATH, "iq_ma_may_2018")26SY_MA_TEST_PATH = os.path.join(TEST_RESOURCE_PATH, "sy_ma_may_2018")27LB_PER1_TEST_PATH = os.path.join(TEST_RESOURCE_PATH, "lb_ma_period1")28class ScorerTest(unittest.TestCase):29    def test_slope_score(self):30        min_value = 031        max_value = 10032        too_low = -533        too_high = 10534        just_right = 5035        result = Scorer.slope_score(too_low, min_value, max_value)36        self.assertAlmostEqual(result, 1)37        result = Scorer.slope_score(min_value, min_value, max_value)38        self.assertAlmostEqual(result, 1)39        result = Scorer.slope_score(just_right, min_value, max_value)40        self.assertAlmostEqual(result, 0.5)41        result = Scorer.slope_score(max_value, min_value, max_value)42        self.assertAlmostEqual(result, 0)43        result = Scorer.slope_score(too_high, min_value, max_value)44        self.assertAlmostEqual(result, 0)45        self.assertRaises(ValueError, Scorer.slope_score, just_right, min_value, min_value)46        self.assertRaises(ValueError, Scorer.slope_score, just_right, max_value, min_value)47    def test_f1(self):48        """49        Tests Scorer.f150        :return:51        """52        p, r = (0,0)53        expected = 054        result = Scorer.f1(p,r)55        self.assertAlmostEqual(result, expected, 3)56        p, r = (1,1)57        expected = 158        result = Scorer.f1(p, r)59        self.assertAlmostEqual(result, expected, 3)60        p, r = (1,1)61        expected = 162        result = Scorer.f1(p, r)63        self.assertAlmostEqual(result, expected, 3)64        p, r = (.5,.5)65        expected = .566        result = Scorer.f1(p, r)67        self.assertAlmostEqual(result, expected, 3)68        p, r = (0,1)69        expected = 070        result = Scorer.f1(p, r)71        self.assertAlmostEqual(result, expected, 3)72        p, r = (.25,.75)73        expected = 0.37574        result = Scorer.f1(p, r)75        self.assertAlmostEqual(result, expected, 3)76        p, r = (-.5, 1)77        self.assertRaises(ValueError, Scorer.f1, p, r)78        p, r = (2,1)79        self.assertRaises(ValueError, Scorer.f1, p, r)80        p, r = (.5, -.1)81        self.assertRaises(ValueError, Scorer.f1, p, r)82        p, r = (.5,2)83        self.assertRaises(ValueError, Scorer.f1, p, r)84    def test_date_diff(self):85        # Test when both are 086        warn_date = "2018-06-22"87        gsr_date_range = pd.date_range("2018-06-17", "2018-06-27")88        gsr_dates = [d.strftime("%Y-%m-%d") for d in gsr_date_range]89        expected_values = range(-5, 6)90        for i, d in enumerate(gsr_dates):91            result = Scorer.date_diff(warn_date, d)92            expected = expected_values[i]93            self.assertAlmostEqual(result, expected)94    def test_date_score(self):95        date_diffs = range(-6, 7)96        results = [Scorer.date_score(dd) for dd in date_diffs]97        expected = [0, 0, 0, .25, .5, .75, 1, .75, .5, .25, 0, 0, 0]98        for i, e in enumerate(expected):99            self.assertAlmostEqual(results[i], e, 3)100        max_date_diff = 5101        results = [Scorer.date_score(dd, max_date_diff) for dd in date_diffs]102        expected = [0, 0, .2, .4, .6, .8, 1, .8, .6, .4, .2, 0, 0]103        for i, e in enumerate(expected):104            self.assertAlmostEqual(results[i], e, 3)105    def test_make_index_mats(self):106        """107        Tests Scorer.make_index_mats method108        :return:109        """110        row_names = ["a", "b", "c", "d"]111        row_indices = list(range(len(row_names)))112        col_names = ["x", "y", "z"]113        col_indices = list(range(len(col_names)))114        row_array = np.array(row_indices*3).reshape(3,4).T115        col_array = np.array(col_indices*4).reshape(4,3)116        results = Scorer.make_index_mats(row_names, col_names)117        try:118            np.testing.assert_equal(row_array, results[0])119            test_res = True120        except AssertionError as e:121            test_res = False122            print(repr(e))123        self.assertTrue(test_res)124        try:125            np.testing.assert_equal(col_array, results[1])126            test_res = True127        except AssertionError as e:128            test_res = False129            print(repr(e))130        self.assertTrue(test_res)131    def test_make_combination_mats(self):132        """133        Tests Scorer.make_combination_mats method134        :return:135        """136        row_names = ["a", "b", "c", "d"]137        col_names = ["x", "y", "z"]138        row_array = np.array(row_names*3).reshape(3,4).T139        col_array = np.array(col_names*4).reshape(4,3)140        results = Scorer.make_combination_mats(row_names, col_names)141        try:142            np.testing.assert_equal(row_array, results[0])143            test_res = True144        except AssertionError as e:145            test_res = False146            print(repr(e))147        self.assertTrue(test_res)148        try:149            np.testing.assert_equal(col_array, results[1])150            test_res = True151        except AssertionError as e:152            test_res = False153            print(repr(e))154        self.assertTrue(test_res)155class MaScorerTest(unittest.TestCase):156    country = "Egypt"157    scorer = MaScorer(country=country)158    warn_dict = dict()159    warn_dict[JSONField.WARNING_ID] = "test_1"160    warn_dict[JSONField.EVENT_TYPE] = "Military Action"161    warn_dict[JSONField.COUNTRY] = country162    warn_dict[JSONField.EVENT_DATE] = "2018-05-27"163    warn_dict[JSONField.TIMESTAMP] = "20160324T00:01:01"164    gsr_dict = dict()165    gsr_dict[JSONField.EVENT_TYPE] = "Disease"166    gsr_dict[JSONField.EVENT_ID] = "Disease_Saudi_Arabia_MERS_2016-03-27"167    gsr_dict[JSONField.DISEASE] =  "MERS"168    gsr_dict[JSONField.COUNTRY] = "Saudi Arabia"169    gsr_dict[JSONField.EVENT_DATE] = "2016-03-27"170    gsr_dict[JSONField.EARLIEST_REPORTED_DATE] = "2016-04-01"171    result_dict = dict()172    result_dict[JSONField.WARNING_ID] = "test_1"173    result_dict[JSONField.EVENT_ID] = "Disease_Saudi_Arabia_MERS_2016-03-27"174    def test_ls(self):175        result = MaScorer.location_score(0, is_approximate=False)176        expected = 1.0177        self.assertAlmostEqual(result, expected)178        result = MaScorer.location_score(0, is_approximate="False")179        expected = 1.0180        self.assertAlmostEqual(result, expected)181        # 22 km distance182        result = MaScorer.location_score(22)183        expected = 0.78184        self.assertAlmostEqual(result, expected, 2)185        result = MaScorer.location_score(22.17, is_approximate=True)186        expected = 0.934187        self.assertAlmostEqual(result, expected, 3)188        result = MaScorer.location_score(22.1, max_dist=44.2)189        expected = 0.50190        self.assertAlmostEqual(result, expected, 2)191        result = MaScorer.location_score(150)192        expected = 0.0193        self.assertAlmostEqual(result, expected)194    def test_make_dist_mat(self):195        """196        Tests MaScorer.make_dist_mat197        :return:198        """199        test_warn_filename = "ma_test_warnings.json"200        test_warn_path = os.path.join(TEST_RESOURCE_PATH, test_warn_filename)201        with open(test_warn_path, "r", encoding="utf8") as f:202            test_warnings = json.load(f)203        test_gsr_filename = "ma_test_gsr.json"204        test_gsr_path = os.path.join(TEST_RESOURCE_PATH, test_gsr_filename)205        with open(test_gsr_path, "r", encoding="utf8") as f:206            test_gsr = json.load(f)207        result = MaScorer.make_dist_mat(test_warnings, test_gsr)208        expected = np.array([156.672, 156.672, 22.173, 22.173]).reshape(4,1)209        try:210            np.testing.assert_allclose(result, expected, 3)211            test_res = True212        except AssertionError as e:213            test_res = False214            print(repr(e))215        self.assertTrue(test_res)216        test_gsr.append(test_warnings[-1])217        expected = np.array([156.672, 145.956, 156.672, 145.956, 22.173, 0, 22.173, 0]).reshape(4,2)218        result = MaScorer.make_dist_mat(test_warnings, test_gsr)219        try:220            np.testing.assert_allclose(result, expected, 3)221            test_res = True222        except AssertionError as e:223            test_res = False224            print(repr(e))225        self.assertTrue(test_res)226        test_warn_filename = "test_lb_warnings.json"227        test_warn_path = os.path.join(LB_MA_TEST_PATH, test_warn_filename)228        with open(test_warn_path, "r", encoding="utf8") as f:229            test_warnings = json.load(f)230        test_gsr_filename = "test_lb_gsr.json"231        test_gsr_path = os.path.join(LB_MA_TEST_PATH, test_gsr_filename)232        with open(test_gsr_path, "r", encoding="utf8") as f:233            test_gsr = json.load(f)234        dist_mat_filename = "test_lb_dist_matrix.csv"235        dist_mat_path = os.path.join(LB_MA_TEST_PATH, dist_mat_filename)236        expected = np.genfromtxt(dist_mat_path, delimiter=",", skip_header=True)[:, 1:]237        result = MaScorer.make_dist_mat(test_warnings, test_gsr)238        try:239            np.testing.assert_allclose(result, expected, 3)240            test_res = True241        except AssertionError as e:242            test_res = False243            print(repr(e))244        self.assertTrue(test_res)245    def test_make_ls_mat(self):246        """247        Tests MaScorer.make_ls_mat248        :return:249        """250        test_warn_filename = "ma_test_warnings.json"251        test_warn_path = os.path.join(TEST_RESOURCE_PATH, test_warn_filename)252        with open(test_warn_path, "r", encoding="utf8") as f:253            test_warnings = json.load(f)254        test_gsr_filename = "ma_test_gsr.json"255        test_gsr_path = os.path.join(TEST_RESOURCE_PATH, test_gsr_filename)256        with open(test_gsr_path, "r", encoding="utf8") as f:257            test_gsr = json.load(f)258        expected = np.array([0, 0, 0.778, 0.778]).reshape(4,1)259        result = MaScorer.make_ls_mat(test_warnings, test_gsr)260        try:261            np.testing.assert_allclose(result, expected, 3)262            test_res = True263        except AssertionError as e:264            test_res = False265            print(repr(e))266        self.assertTrue(test_res)267        test_gsr.append(test_warnings[-1])268        test_gsr[-1]["Approximate_Location"] = "False"269        expected = np.array([0, 0, 0, 0, 0.778, 1, 0.778, 1]).reshape(4,2)270        result = MaScorer.make_ls_mat(test_warnings, test_gsr)271        try:272            np.testing.assert_allclose(result, expected, 3)273            test_res = True274        except AssertionError as e:275            test_res = False276            print(repr(e))277        self.assertTrue(test_res)278        test_warn_filename = "test_lb_warnings.json"279        test_warn_path = os.path.join(LB_MA_TEST_PATH, test_warn_filename)280        with open(test_warn_path, "r", encoding="utf8") as f:281            test_warnings = json.load(f)282        test_gsr_filename = "test_lb_gsr.json"283        test_gsr_path = os.path.join(LB_MA_TEST_PATH, test_gsr_filename)284        with open(test_gsr_path, "r", encoding="utf8") as f:285            test_gsr = json.load(f)286        ls_mat_filename = "test_ls_matrix.csv"287        ls_mat_path = os.path.join(LB_MA_TEST_PATH, ls_mat_filename)288        expected = np.genfromtxt(ls_mat_path, delimiter=",", skip_header=True)[:, 1:]289        result = MaScorer.make_ls_mat(test_warnings, test_gsr)290        try:291            np.testing.assert_allclose(result, expected, 3)292            test_res = True293        except AssertionError as e:294            test_res = False295            print(repr(e))296        self.assertTrue(test_res)297    def test_make_ds_mat(self):298        """299        Tests MaScorer.make_ds_mat300        :return:301        """302        test_warn_filename = "ma_test_warnings.json"303        test_warn_path = os.path.join(TEST_RESOURCE_PATH, test_warn_filename)304        with open(test_warn_path, "r", encoding="utf8") as f:305            test_warnings = json.load(f)306        test_gsr_filename = "ma_test_gsr.json"307        test_gsr_path = os.path.join(TEST_RESOURCE_PATH, test_gsr_filename)308        with open(test_gsr_path, "r", encoding="utf8") as f:309            test_gsr = json.load(f)310        expected = np.array([0, .75, 0, .75]).reshape(4,1)311        result = MaScorer.make_ds_mat(test_warnings, test_gsr)312        try:313            np.testing.assert_allclose(result, expected, 3)314            test_res = True315        except AssertionError as e:316            test_res = False317            print(repr(e))318        self.assertTrue(test_res)319        test_warn_filename = "test_lb_warnings.json"320        test_warn_path = os.path.join(LB_MA_TEST_PATH, test_warn_filename)321        with open(test_warn_path, "r", encoding="utf8") as f:322            test_warnings = json.load(f)323        test_gsr_filename = "test_lb_gsr.json"324        test_gsr_path = os.path.join(LB_MA_TEST_PATH, test_gsr_filename)325        with open(test_gsr_path, "r", encoding="utf8") as f:326            test_gsr = json.load(f)327        ds_mat_filename = "test_ds_matrix.csv"328        ds_mat_path = os.path.join(LB_MA_TEST_PATH, ds_mat_filename)329        expected = np.genfromtxt(ds_mat_path, delimiter=",", skip_header=True)[:, 1:]330        result = MaScorer.make_ds_mat(test_warnings, test_gsr)331        try:332            np.testing.assert_allclose(result, expected, 3)333            test_res = True334        except AssertionError as e:335            test_res = False336            print(repr(e))337        self.assertTrue(test_res)338    def test_make_ess_mat(self):339        """340        Tests MaScorer.make_ess_mat341        :return:342        """343        test_warn_filename = "test_lb_warnings.json"344        test_warn_path = os.path.join(LB_MA_TEST_PATH, test_warn_filename)345        with open(test_warn_path, "r", encoding="utf8") as f:346            test_warnings = json.load(f)347        test_gsr_filename = "test_lb_gsr.json"348        test_gsr_path = os.path.join(LB_MA_TEST_PATH, test_gsr_filename)349        with open(test_gsr_path, "r", encoding="utf8") as f:350            test_gsr = json.load(f)351        ess_mat_filename = "test_es_match_matrix.csv"352        ess_mat_path = os.path.join(LB_MA_TEST_PATH, ess_mat_filename)353        expected = np.genfromtxt(ess_mat_path, delimiter=",", skip_header=True)[:, 1:]354        result = MaScorer.make_ess_mat(test_warnings, test_gsr)355        try:356            np.testing.assert_allclose(result, expected, 3)357            test_res = True358        except AssertionError as e:359            test_res = False360            print(repr(e))361        self.assertTrue(test_res)362    def test_make_as_mat(self):363        """364        Tests MaScorer.make_as_mat365        :return:366        """367        test_warn_filename = "test_lb_warnings.json"368        test_warn_path = os.path.join(LB_MA_TEST_PATH, test_warn_filename)369        with open(test_warn_path, "r", encoding="utf8") as f:370            test_warnings = json.load(f)371        test_gsr_filename = "test_lb_gsr.json"372        test_gsr_path = os.path.join(LB_MA_TEST_PATH, test_gsr_filename)373        with open(test_gsr_path, "r", encoding="utf8") as f:374            test_gsr = json.load(f)375        acs_mat_filename = "test_actor_match_matrix.csv"376        acs_mat_path = os.path.join(LB_MA_TEST_PATH, acs_mat_filename)377        expected = np.genfromtxt(acs_mat_path, delimiter=",", skip_header=True)[:, 1:]378        result = MaScorer.make_as_mat(test_warnings, test_gsr)379        try:380            np.testing.assert_allclose(result, expected, 3)381            test_res = True382        except AssertionError as e:383            test_res = False384            print(repr(e))385        self.assertTrue(test_res)386    def test_make_qs_mat(self):387        """388        Tests MaScorer.make_qs_df389        :return:390        """391        test_warn_filename = "test_lb_warnings.json"392        test_warn_path = os.path.join(LB_MA_TEST_PATH, test_warn_filename)393        with open(test_warn_path, "r", encoding="utf8") as f:394            test_warnings = json.load(f)395        test_gsr_filename = "test_lb_gsr.json"396        test_gsr_path = os.path.join(LB_MA_TEST_PATH, test_gsr_filename)397        with open(test_gsr_path, "r", encoding="utf8") as f:398            test_gsr = json.load(f)399        mat_filename = "test_qs_mat.csv"400        mat_path = os.path.join(LB_MA_TEST_PATH, mat_filename)401        expected = pd.read_csv(mat_path, index_col=0)402        result = MaScorer.make_qs_df(test_warnings, test_gsr)403        try:404            np.testing.assert_allclose(result, expected, 3)405            test_res = True406        except AssertionError as e:407            test_res = False408            print(repr(e))409        self.assertTrue(test_res)410        test_warn_filename = "test_cc_warnings.json"411        test_warn_path = os.path.join(EG_MA_TEST_PATH, test_warn_filename)412        with open(test_warn_path, "r", encoding="utf8") as f:413            test_warnings = json.load(f)414        test_gsr_filename = "test_cc_gsr.json"415        test_gsr_path = os.path.join(EG_MA_TEST_PATH, test_gsr_filename)416        with open(test_gsr_path, "r", encoding="utf8") as f:417            test_gsr = json.load(f)418        mat_filename = "test_qs_mat.csv"419        mat_path = os.path.join(EG_MA_TEST_PATH, mat_filename)420        expected = np.genfromtxt(mat_path, delimiter=",", skip_header=True)[:, 1:]421        result = MaScorer.make_qs_df(test_warnings, test_gsr)422        try:423            np.testing.assert_allclose(result, expected, 3)424            test_res = True425        except AssertionError as e:426            test_res = False427            print(repr(e))428        self.assertTrue(test_res)429        test_warn_filename = "test_cc_warnings.json"430        test_warn_path = os.path.join(SA_MA_TEST_PATH, test_warn_filename)431        with open(test_warn_path, "r", encoding="utf8") as f:432            test_warnings = json.load(f)433        test_gsr_filename = "test_cc_gsr.json"434        test_gsr_path = os.path.join(SA_MA_TEST_PATH, test_gsr_filename)435        with open(test_gsr_path, "r", encoding="utf8") as f:436            test_gsr = json.load(f)437        mat_filename = "test_qs_mat.csv"438        mat_path = os.path.join(SA_MA_TEST_PATH, mat_filename)439        expected = np.genfromtxt(mat_path, delimiter=",", skip_header=True)[:, 1:]440        result = MaScorer.make_qs_df(test_warnings, test_gsr)441        try:442            np.testing.assert_allclose(result, expected, 3)443            test_res = True444        except AssertionError as e:445            test_res = False446            print(repr(e))447        self.assertTrue(test_res)448        test_warn_filename = "test_cc_warnings.json"449        test_warn_path = os.path.join(IQ_MA_TEST_PATH, test_warn_filename)450        with open(test_warn_path, "r", encoding="utf8") as f:451            test_warnings = json.load(f)452        test_gsr_filename = "test_cc_gsr.json"453        test_gsr_path = os.path.join(IQ_MA_TEST_PATH, test_gsr_filename)454        with open(test_gsr_path, "r", encoding="utf8") as f:455            test_gsr = json.load(f)456        mat_filename = "test_qs_mat.csv"457        mat_path = os.path.join(IQ_MA_TEST_PATH, mat_filename)458        expected = np.genfromtxt(mat_path, delimiter=",", skip_header=True)[:, 1:]459        result = MaScorer.make_qs_df(test_warnings, test_gsr)460        try:461            np.testing.assert_allclose(result, expected, 3)462            test_res = True463        except AssertionError as e:464            test_res = False465            print(repr(e))466        self.assertTrue(test_res)467    def test_facet_score(self):468        """469        Tests Scorer.facet_score470        :return:471        """472        wildcards = ["Unspecified", "Wildcard"]473        warn_value = "Fred"474        gsr_value = "Ethel"475        expected = 0476        result = Scorer.facet_score(warn_value, gsr_value, wildcards)477        self.assertEqual(result, expected)478        gsr_value = wildcards[0]479        expected = 1480        result = Scorer.facet_score(warn_value, gsr_value, wildcards)481        self.assertEqual(result, expected)482        gsr_value = wildcards[1]483        expected = 1484        result = Scorer.facet_score(warn_value, gsr_value, wildcards)485        self.assertEqual(result, expected)486        warn_value = wildcards[0]487        gsr_value = "Ethel"488        expected = 0489        result = Scorer.facet_score(warn_value, gsr_value, wildcards)490        self.assertEqual(result, expected)491        warn_value = "Fred"492        gsr_value = ["Ethel", "Fred"]493        expected = 1494        result = Scorer.facet_score(warn_value, gsr_value, wildcards)495        self.assertEqual(result, expected)496        gsr_value = "Ethel;Fred"497        expected = 1498        result = Scorer.facet_score(warn_value, gsr_value, wildcards)499        self.assertEqual(result, expected)500        gsr_value = "Ethel,Fred"501        expected = 1502        result = Scorer.facet_score(warn_value, gsr_value, wildcards, gsr_value_delim=",")503        self.assertEqual(result, expected)504    def test_actor_score(self):505        """506        Test MaScorer.actor_score507        :return:508        """509        wildcards = ["Unspecified", "Wildcard"]510        legits = ["Fred", "Ethel"]511        warn_value = "Fred"512        gsr_value = "Ethel"513        expected = 0514        result = MaScorer.actor_score(warn_value, gsr_value, legits, wildcards)515        self.assertEqual(result, expected)516        gsr_value = wildcards[0]517        expected = 1518        result = MaScorer.actor_score(warn_value, gsr_value, legits, wildcards)519        self.assertEqual(result, expected)520        gsr_value = wildcards[1]521        expected = 1522        result = MaScorer.actor_score(warn_value, gsr_value, legits, wildcards)523        self.assertEqual(result, expected)524        warn_value = wildcards[0]525        gsr_value = "Ethel"526        expected = 0527        result = MaScorer.actor_score(warn_value, gsr_value, legits, wildcards)528        self.assertEqual(result, expected)529        warn_value = "Fred"530        gsr_value = ["Ethel", "Fred"]531        expected = 1532        result = MaScorer.actor_score(warn_value, gsr_value, legits, wildcards)533        self.assertEqual(result, expected)534        warn_value = "Fred"535        gsr_value = "Ethel;Fred"536        expected = 1537        result = MaScorer.actor_score(warn_value, gsr_value, legits, wildcards)538        self.assertEqual(result, expected)539        warn_value = "Fred"540        gsr_value = "Ethel,Fred"541        expected = 1542        result = MaScorer.actor_score(warn_value, gsr_value, legits, wildcards, gsr_value_delim=",")543        self.assertEqual(result, expected)544        warn_value = "Lucy"545        gsr_value = ["Ethel", "Fred"]546        expected = 0547        result = MaScorer.actor_score(warn_value, gsr_value, legits, wildcards)548        self.assertEqual(result, expected)549    def test_subtype_score(self):550        """551        Test MaScorer.event_subtype_score552        :return:553        """554        warn_value = "Force Posture"555        gsr_value = "Conflict"556        expected = 0557        result = MaScorer.event_subtype_score(warn_value, gsr_value)558        self.assertEqual(result, expected)559        gsr_value = "Force Posture"560        expected = 1561        result = MaScorer.event_subtype_score(warn_value, gsr_value)562        self.assertEqual(result, expected)563        warn_value = "Sharpening Swords"564        expected = 0565        result = MaScorer.event_subtype_score(warn_value, gsr_value)566        self.assertEqual(result, expected)567    def test_score(self):568        """569        Tests MaScorer.score method570        :return:571        """572        test_warn_filename = "test_lb_warnings.json"573        test_warn_path = os.path.join(LB_MA_TEST_PATH, test_warn_filename)574        with open(test_warn_path, "r", encoding="utf8") as f:575            test_warnings = json.load(f)576        test_gsr_filename = "test_lb_gsr.json"577        test_gsr_path = os.path.join(LB_MA_TEST_PATH, test_gsr_filename)578        with open(test_gsr_path, "r", encoding="utf8") as f:579            test_gsr = json.load(f)580        result = MaScorer.score(test_warnings, test_gsr)581        expected_filename = "match_results.json"582        path_ = os.path.join(LB_MA_TEST_PATH, expected_filename)583        with open(path_, "r", encoding="utf8") as f:584            expected = json.load(f)585        expected_matches = sorted(set([(m["Warning"], m["Event"]) for m in expected["Matches"]]))586        expected_qs_ser = expected["Details"]["Quality Scores"]587        expected_qs_mean = expected["Quality Score"]588        expected_precision = expected["Precision"]589        expected_recall = expected["Recall"]590        expected_f1 = expected["F1"]591        expected_merc_score = expected["Mercury Score"]592        self.assertEqual(sorted(set(result["Matches"])), expected_matches)593        self.assertAlmostEqual(result["Quality Score"], expected_qs_mean, 3)594        self.assertAlmostEqual(result["Mercury Score"], expected_merc_score, 3)595        self.assertAlmostEqual(result["Precision"], expected_precision, 3)596        self.assertAlmostEqual(result["Recall"], expected_recall, 3)597        self.assertAlmostEqual(result["F1"], expected_f1, 3)598        for i, qs in enumerate(expected_qs_ser):599            res_qs = result["Details"]["Quality Scores"][i]600            self.assertAlmostEqual(res_qs, qs, 3)601    def test_match(self):602        """603        Tests MaScorer.match604        :return:605        """606        # Simple Matrix, 3 by 4607        test_matrix_filename = "test_qs_matrix_1.csv"608        path_ = os.path.join(TEST_RESOURCE_PATH, test_matrix_filename)609        test_mat = pd.read_csv(path_, index_col=0)610        expected_matches = [("warn_0", "evt_0"), ("warn_1", "evt_1"), ("warn_2", "evt_3")]611        expected_qs_ser = [1, 0.85, 0.8]612        expected_qs_mean = np.mean(expected_qs_ser)613        result = MaScorer.match(input_matrix=test_mat)614        self.assertEqual(result["Matches"], expected_matches)615        self.assertAlmostEqual(result["Quality Score"], expected_qs_mean, 3)616        self.assertAlmostEqual(result["Precision"], 1.0)617        self.assertAlmostEqual(result["Recall"], 0.75)618        self.assertAlmostEqual(result["F1"], 1.5/1.75)619        self.assertAlmostEqual(result["Mercury Score"], (expected_qs_mean + 1.5/1.75)/2.)620        self.assertAlmostEqual(result["Details"]["Quality Scores"], expected_qs_ser, 3)621        # Simple matrix, 4 by 3622        test_matrix_filename = "test_qs_matrix_2.csv"623        path_ = os.path.join(TEST_RESOURCE_PATH, test_matrix_filename)624        test_mat = pd.read_csv(path_, index_col=0)625        expected_matches = [("warn_0", "evt_0"), ("warn_1", "evt_1"), ("warn_3", "evt_2")]626        expected_qs_ser = [1, 0.85, 0.75]627        expected_qs_mean = np.mean(expected_qs_ser)628        result = MaScorer.match(input_matrix=test_mat)629        self.assertEqual(result["Matches"], expected_matches)630        self.assertAlmostEqual(result["Quality Score"], expected_qs_mean, 3)631        self.assertAlmostEqual(result["Precision"], 0.75)632        self.assertAlmostEqual(result["Recall"], 1.00)633        self.assertAlmostEqual(result["F1"], 1.5/1.75)634        self.assertAlmostEqual(result["Mercury Score"], (expected_qs_mean + 1.5/1.75)/2.)635        self.assertAlmostEqual(result["Details"]["Quality Scores"], expected_qs_ser, 3)636        # Null Matrix637        test_matrix_filename = "test_null_matrix.csv"638        path_ = os.path.join(TEST_RESOURCE_PATH, test_matrix_filename)639        test_mat = pd.read_csv(path_, index_col=0)640        expected_matches = []641        expected_qs_ser = []642        expected_qs_mean = 0643        result = MaScorer.match(input_matrix=test_mat)644        self.assertEqual(result["Matches"], expected_matches)645        self.assertAlmostEqual(result["Quality Score"], expected_qs_mean, 3)646        self.assertAlmostEqual(result["Precision"], 0)647        self.assertAlmostEqual(result["Recall"], 0)648        self.assertAlmostEqual(result["F1"], 0)649        self.assertAlmostEqual(result["Mercury Score"], 0)650        self.assertAlmostEqual(result["Details"]["Quality Scores"], expected_qs_ser, 3)651        # Matrix with negative entries652        test_matrix_filename = "test_neg_matrix.csv"653        path_ = os.path.join(TEST_RESOURCE_PATH, test_matrix_filename)654        test_mat = pd.read_csv(path_, index_col=0)655        expected_matches = [("warn_0", "evt_0"), ("warn_2", "evt_2")]656        expected_qs_ser = [0.75, 1.0]657        expected_qs_mean = 0.875658        result = MaScorer.match(input_matrix=test_mat)659        self.assertEqual(result["Matches"], expected_matches)660        self.assertAlmostEqual(result["Quality Score"], expected_qs_mean, 3)661        self.assertAlmostEqual(result["Precision"], 0.5)662        self.assertAlmostEqual(result["Recall"], 0.667, 3)663        self.assertAlmostEqual(result["F1"], 0.667/1.167, 3)664        self.assertAlmostEqual(result["Mercury Score"], (expected_qs_mean + 0.667/1.167)/2, 3)665        self.assertAlmostEqual(result["Details"]["Quality Scores"], expected_qs_ser, 3)666        # Matrix with Lebanon data667        test_matrix_filename = "test_qs_mat.csv"668        path_ = os.path.join(LB_MA_TEST_PATH, test_matrix_filename)669        test_mat = pd.read_csv(path_, index_col=0)670        expected_filename = "match_results.json"671        path_ = os.path.join(LB_MA_TEST_PATH, expected_filename)672        with open(path_, "r", encoding="utf8") as f:673            expected = json.load(f)674        expected_matches = sorted(set([(m["Warning"], m["Event"]) for m in expected["Matches"]]))675        expected_qs_ser = expected["Details"]["Quality Scores"]676        expected_qs_mean = expected["Quality Score"]677        expected_precision = expected["Precision"]678        expected_recall = expected["Recall"]679        expected_f1 = expected["F1"]680        result = MaScorer.match(input_matrix=test_mat)681        self.assertEqual(sorted(set(result["Matches"])), expected_matches)682        self.assertAlmostEqual(result["Quality Score"], expected_qs_mean, 3)683        self.assertAlmostEqual(result["Precision"], expected_precision, 3)684        self.assertAlmostEqual(result["Recall"], expected_recall, 3)685        self.assertAlmostEqual(result["F1"], expected_f1, 3)686        self.assertAlmostEqual(result["Mercury Score"], (expected["Quality Score"] + expected["F1"])/2, 3)687        for i, qs in enumerate(expected_qs_ser):688            res_qs = result["Details"]["Quality Scores"][i]689            self.assertAlmostEqual(res_qs, qs, 3)690    def test_score_one(self):691        """692        Test MaScorer.score_one weights input693        :return:694        """695        test_warn_filename = "ma_test_warnings.json"696        test_warn_path = os.path.join(TEST_RESOURCE_PATH, test_warn_filename)697        with open(test_warn_path, "r", encoding="utf8") as f:698            test_warnings= json.load(f)699        test_gsr_filename = "ma_test_gsr.json"700        test_gsr_path = os.path.join(TEST_RESOURCE_PATH, test_gsr_filename)701        with open(test_gsr_path, "r", encoding="utf8") as f:702            test_gsr = json.load(f)703        #print(test_gsr[0])704        LEGIT_ACTORS = ["Egyptian Police"]705        bad_weight = -1706        sub_weight = .5707        super_weight = 2708        # Test with default weights709        # Test with bad weights710        result = MaScorer.score_one(test_warnings[0], test_gsr[0], legit_actors=LEGIT_ACTORS,711                                    ls_weight=bad_weight)712        self.assertTrue("Errors" in result)713        result = MaScorer.score_one(test_warnings[0], test_gsr[0], legit_actors=LEGIT_ACTORS,714                                    ds_weight=bad_weight)715        self.assertTrue("Errors" in result)716        result = MaScorer.score_one(test_warnings[0], test_gsr[0], legit_actors=LEGIT_ACTORS,717                                    as_weight=bad_weight)718        self.assertTrue("Errors" in result)719        result = MaScorer.score_one(test_warnings[0], test_gsr[0], legit_actors=LEGIT_ACTORS,720                                    ess_weight=bad_weight)721        self.assertTrue("Errors" in result)722        # Test with weights summing to less than 4723        result = MaScorer.score_one(test_warnings[0], test_gsr[0], legit_actors=LEGIT_ACTORS,724                                    ls_weight=sub_weight)725        self.assertTrue("Notices" in result)726        result = MaScorer.score_one(test_warnings[0], test_gsr[0], legit_actors=LEGIT_ACTORS,727                                    ds_weight=sub_weight)728        self.assertTrue("Notices" in result)729        result = MaScorer.score_one(test_warnings[0], test_gsr[0], legit_actors=LEGIT_ACTORS,730                                    as_weight=sub_weight)731        self.assertTrue("Notices" in result)732        result = MaScorer.score_one(test_warnings[0], test_gsr[0], legit_actors=LEGIT_ACTORS,733                                    ess_weight=sub_weight)734        self.assertTrue("Notices" in result)735        # Test with weights summing to more than 4736        result = MaScorer.score_one(test_warnings[0], test_gsr[0], legit_actors=LEGIT_ACTORS,737                                    ls_weight=super_weight)738        self.assertTrue("Notices" in result)739        result = MaScorer.score_one(test_warnings[0], test_gsr[0], legit_actors=LEGIT_ACTORS,740                                    ds_weight=super_weight)741        self.assertTrue("Notices" in result)742        result = MaScorer.score_one(test_warnings[0], test_gsr[0], legit_actors=LEGIT_ACTORS,743                                    as_weight=super_weight)744        self.assertTrue("Notices" in result)745        result = MaScorer.score_one(test_warnings[0], test_gsr[0], legit_actors=LEGIT_ACTORS,746                                    ess_weight=super_weight)747        self.assertTrue("Notices" in result)748        print("Result using default weights")749        result = MaScorer.score_one(test_warnings[3], test_gsr[0])750        print(result)751        # Test a warning with LS = 0752        result = MaScorer.score_one(test_warnings[1], test_gsr[0])753        self.assertEqual(result[ScoreComponents.QS], 0)754        self.assertEqual(result[ScoreComponents.LS], 0)755        self.assertAlmostEqual(result[ScoreComponents.DS], 0.75)756        # Test a warning with DS = 0757        result = MaScorer.score_one(test_warnings[2], test_gsr[0])758        self.assertEqual(result[ScoreComponents.QS], 0)759        self.assertAlmostEqual(result[ScoreComponents.LS], 0.778, 3)760        self.assertEqual(result[ScoreComponents.DS], 0)761        # Test a legitimately matched warning762        result = MaScorer.score_one(test_warnings[3], test_gsr[0], legit_actors=LEGIT_ACTORS)763        self.assertEqual(result[ScoreComponents.AS], 1)764        self.assertEqual(result[ScoreComponents.ESS], 1)765        self.assertAlmostEqual(result[ScoreComponents.LS], 0.778, 3)766        self.assertAlmostEqual(result[ScoreComponents.DS], 0.75)767        self.assertAlmostEqual(result[ScoreComponents.QS], 0.882, 3)768        self.assertFalse("Notices" in result)769        self.assertFalse("Errors" in result)770    def test_duplicate_matches(self):771        """772        Tests if the same GSR event or warning are matched multiply.773        :return:774        """775        # Test using Lebanon for one participant.  Known faiure case.776        test_warn_filename = "test_warn.json"777        test_warn_path = os.path.join(LB_PER1_TEST_PATH, test_warn_filename)778        with open(test_warn_path, "r", encoding="utf8") as f:779            test_warnings= json.load(f)780        test_gsr_filename = "test_gsr.json"781        test_gsr_path = os.path.join(LB_PER1_TEST_PATH, test_gsr_filename)782        with open(test_gsr_path, "r", encoding="utf8") as f:783            test_gsr = json.load(f)784        result = MaScorer.score(test_warnings, test_gsr)785        matches = result["Matches"]786        warn_ids = [m[0] for m in matches]787        warn_id_counter = Counter(warn_ids)788        max_warn_usage = warn_id_counter.most_common(1)[0][1]789        self.assertEqual(max_warn_usage, 1)790        gsr_ids = [m[1] for m in matches]791        gsr_id_counter = Counter(gsr_ids)792        max_gsr_usage = gsr_id_counter.most_common(1)[0][1]793        self.assertEqual(max_gsr_usage, 1)794        # Test using Syria, May 2018.  This will take a while to run.795        test_warn_filename = "test_cc_warnings.json"796        test_warn_path = os.path.join(SY_MA_TEST_PATH, test_warn_filename)797        with open(test_warn_path, "r", encoding="utf8") as f:798            test_warnings= json.load(f)799        test_gsr_filename = "test_cc_gsr.json"800        test_gsr_path = os.path.join(SY_MA_TEST_PATH, test_gsr_filename)801        with open(test_gsr_path, "r", encoding="utf8") as f:802            test_gsr = json.load(f)803        result = MaScorer.score(test_warnings, test_gsr)804        matches = result["Matches"]805        warn_ids = [m[0] for m in matches]806        warn_id_counter = Counter(warn_ids)807        max_warn_usage = warn_id_counter.most_common(1)[0][1]808        self.assertEqual(max_warn_usage, 1)809        gsr_ids = [m[1] for m in matches]810        gsr_id_counter = Counter(gsr_ids)811        max_gsr_usage = gsr_id_counter.most_common(1)[0][1]812        self.assertEqual(max_gsr_usage, 1)813if __name__ == "__main__":...test_warnings.py
Source:test_warnings.py  
1# Copyright The PyTorch Lightning team.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""Test that the warnings actually appear and they have the correct `stacklevel`15Needs to be run outside of `pytest` as it captures all the warnings.16"""17import os18from contextlib import redirect_stderr19from io import StringIO20from pytorch_lightning.utilities.rank_zero import _warn, rank_zero_deprecation, rank_zero_warn21from pytorch_lightning.utilities.warnings import WarningCache22standalone = os.getenv("PL_RUN_STANDALONE_TESTS", "0") == "1"23if standalone and __name__ == "__main__":24    stderr = StringIO()25    # recording26    with redirect_stderr(stderr):27        _warn("test1")28        _warn("test2", category=DeprecationWarning)29        rank_zero_warn("test3")30        rank_zero_warn("test4", category=DeprecationWarning)31        rank_zero_deprecation("test5")32        cache = WarningCache()33        cache.warn("test6")34        cache.deprecation("test7")35    output = stderr.getvalue()36    assert "test_warnings.py:31: UserWarning: test1" in output37    assert "test_warnings.py:32: DeprecationWarning: test2" in output38    assert "test_warnings.py:34: UserWarning: test3" in output39    assert "test_warnings.py:35: DeprecationWarning: test4" in output40    assert "test_warnings.py:37: LightningDeprecationWarning: test5" in output41    assert "test_warnings.py:40: UserWarning: test6" in output42    assert "test_warnings.py:41: LightningDeprecationWarning: test7" in output43    # check that logging is properly configured44    import logging45    from pytorch_lightning import _DETAIL46    root_logger = logging.getLogger()47    lightning_logger = logging.getLogger("pytorch_lightning")48    # should have a `StreamHandler`49    assert lightning_logger.hasHandlers() and len(lightning_logger.handlers) == 150    # set our own stream for testing51    handler = lightning_logger.handlers[0]52    assert isinstance(handler, logging.StreamHandler)53    stderr = StringIO()54    # necessary with `propagate = False`55    lightning_logger.handlers[0].stream = stderr56    # necessary with `propagate = True`57    with redirect_stderr(stderr):58        # Lightning should not configure the root `logging` logger by default59        logging.info("test1")60        root_logger.info("test1")61        # but our logger instance62        lightning_logger.info("test2")63        # level is set to INFO64        lightning_logger.debug("test3")65    output = stderr.getvalue()66    assert output == "test2\n", repr(output)67    stderr = StringIO()68    lightning_logger.handlers[0].stream = stderr69    with redirect_stderr(stderr):70        # Lightning should not output DETAIL level logging by default71        lightning_logger.detail("test1")72        lightning_logger.setLevel(_DETAIL)73        lightning_logger.detail("test2")74        # logger should not output anything for DEBUG statements if set to DETAIL75        lightning_logger.debug("test3")76    output = stderr.getvalue()...Test.py
Source:Test.py  
1#2# LUMOS 3# $Header: /tmp/cvsroot/lumos/Test/Test.py,v 1.4 2008-12-31 00:13:32 steve Exp $4#5# Lumos Light Orchestration System6# Copyright (c) 2005, 2006, 2007, 2008 by Steven L. Willoughby, Aloha,7# Oregon, USA.  All Rights Reserved.  Licensed under the Open Software8# License version 3.0.9#10# This product is provided for educational, experimental or personal11# interest use, in accordance with the terms and conditions of the12# aforementioned license agreement, ON AN "AS IS" BASIS AND WITHOUT13# WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION,14# THE WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A15# PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL16# WORK IS WITH YOU.  (See the license agreement for full details, 17# including disclaimer of warranty and limitation of liability.)18#19# Under no curcumstances is this product intended to be used where the20# safety of any person, animal, or property depends upon, or is at21# risk of any kind from, the correct operation of this software or22# the hardware devices which it controls.23#24# USE THIS PRODUCT AT YOUR OWN RISK.25# 26from unittest import TestSuite, findTestCases27import sys28TEST_WARNINGS={}29class SkipWarning (object):30    def __init__(self, msg):31        self.count=132        self.msg=msg33    def incr(self):34        self.count += 135    def __lt__(self, x):36        return self.count < x.count37def warn_once(tag, message):38    global TEST_WARNINGS39    if not already_warned_about(tag):40        TEST_WARNINGS[tag] = SkipWarning(message)41        sys.stderr.write("WARNING: "+message+"\n")42        sys.stderr.flush()43    else:44        TEST_WARNINGS[tag].incr()45def already_warned_about(tag):46    global TEST_WARNINGS47    return tag in TEST_WARNINGS48def accumulated_warnings():49    global TEST_WARNINGS50    return sorted(TEST_WARNINGS.values())51def reset_accumulated_warnings():52    global TEST_WARNINGS53    for i in TEST_WARNINGS:54        TEST_WARNINGS[i].count = 055def suite():56    modules_to_test = (57      'ChannelTest',58	  'ControllersTest',59      'ControllerUnitTest',60	  'DeviceTest',61      'DimmerVirtualChannelTest',62#	  'EventTest',              # DEPRECATED CLASS63	  'FirecrackerX10ControllerUnitTest',64	  'FireGodControllerUnitTest',65	  'LCheckTest',66	  'LumosTest',67      'LynX10ControllerUnitTest',68      'NetworkTest',69	  'NetworksTest',70	  'Olsen595ControllerUnitTest',71	  'PowerSourceTest',72	  'RenardControllerUnitTest',73      'RGBVirtualChannelTest',74	  'ShowTest',75	  'SerialNetworkTest',        # XXX device tests?76	  'ParallelBitNetworkTest',   # XXX device tests?77	  'SequenceTest',78	  'SerialBitNetworkTest',     # XXX device tests?79#     'SpectrumReaderboardUnitTest',80	  'LumosControllerUnitTest',81	  'TestNetworkTest',82	  'TestParallelNetworkTest',83      'TimeRangeTest',84      'ValueEventTest',85      'VirtualChannelTest',86      'ToggleVirtualChannelTest',87	  'VixenSequenceTest',88      'X10ControllerUnitTest'89    )90    suite = TestSuite()91    for module in map(__import__, modules_to_test):92        suite.addTest(findTestCases(module))93    return suite94# 95# $Log: not supported by cvs2svn $...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
