ml_pid_cbm.tools.test_prepare_model
1import unittest 2from unittest.mock import mock_open, patch 3 4import pandas as pd 5from hipe4ml.tree_handler import TreeHandler 6 7from .prepare_model import PrepareModel 8 9 10class TestPrepareModel(unittest.TestCase): 11 @classmethod 12 def setUpClass(cls): 13 cls.train_model_without_opt = PrepareModel("config.json", False, False) 14 cls.train_model_without_opt_gpu = PrepareModel("config.json", False, True) 15 cls.train_model_with_opt = PrepareModel("config.json", True, False) 16 cls.proton_entry = { 17 "Complex_q": 1.0, 18 "Complex_p": 1.0, 19 "Complex_pid": 2212.0, 20 "Complex_mass2": 0.8, 21 } 22 cls.kaon_entry = { 23 "Complex_q": 1.0, 24 "Complex_p": 1.2, 25 "Complex_pid": 321.0, 26 "Complex_mass2": 0.4, 27 } 28 cls.pion_entry = { 29 "Complex_q": 1.0, 30 "Complex_p": 1.2, 31 "Complex_pid": 211.0, 32 "Complex_mass2": 0.2, 33 } 34 cls.json_data = """{"features_for_train":["Complex_mass2", "Complex_p"], 35 "hyper_params": {"values": {"n_estimators": 596,"max_depth": 5,"learning_rate": 0.07161792803939408}, 36 "ranges": {"n_estimators": [400, 1000],"max_depth": [2, 6],"learning_rate": [0.01, 0.1]}}}""" 37 cls.proton_tree_handler = TreeHandler() 38 cls.proton_tree_handler.set_data_frame(pd.DataFrame([cls.proton_entry] * 10)) 39 cls.kaon_tree_handler = TreeHandler() 40 cls.kaon_tree_handler.set_data_frame(pd.DataFrame([cls.kaon_entry] * 10)) 41 cls.pion_tree_handler = TreeHandler() 42 cls.pion_tree_handler.set_data_frame(pd.DataFrame([cls.pion_entry] * 10)) 43 44 def test_load_hyper_params_ranges(self): 45 target_ranges = { 46 "n_estimators": (400, 1000), 47 "max_depth": (2, 6), 48 "learning_rate": (0.01, 0.1), 49 } 50 with patch("builtins.open", mock_open(read_data=self.json_data)): 51 read_ranges = self.train_model_without_opt.load_hyper_params_ranges() 52 self.assertEqual(read_ranges, target_ranges) 53 54 def test_prepare_train_test_data(self): 55 with patch("builtins.open", mock_open(read_data=self.json_data)): 56 self.train_model_without_opt.prepare_train_test_data( 57 [ 58 self.proton_tree_handler, 59 self.kaon_tree_handler, 60 self.pion_tree_handler, 61 ], 62 0.5, 63 ) 64 self.train_model_with_opt.prepare_train_test_data( 65 [ 66 self.proton_tree_handler, 67 self.kaon_tree_handler, 68 self.pion_tree_handler, 69 ], 70 0.5, 71 ) 72 73 def test_prepare_model_handler(self): 74 with patch("builtins.open", mock_open(read_data=self.json_data)): 75 train_test_data = self.train_model_without_opt.prepare_train_test_data( 76 [ 77 self.proton_tree_handler, 78 self.kaon_tree_handler, 79 self.pion_tree_handler, 80 ], 81 0.1, 82 ) 83 self.train_model_with_opt.prepare_model_handler( # TODO modify and suppress output 84 train_test_data=train_test_data 85 ) 86 self.assertRaises( 87 TypeError, lambda: self.train_model_with_opt.prepare_model_handler() 88 ) 89 self.train_model_without_opt.prepare_model_handler() 90 self.train_model_without_opt_gpu.prepare_model_handler()
11class TestPrepareModel(unittest.TestCase): 12 @classmethod 13 def setUpClass(cls): 14 cls.train_model_without_opt = PrepareModel("config.json", False, False) 15 cls.train_model_without_opt_gpu = PrepareModel("config.json", False, True) 16 cls.train_model_with_opt = PrepareModel("config.json", True, False) 17 cls.proton_entry = { 18 "Complex_q": 1.0, 19 "Complex_p": 1.0, 20 "Complex_pid": 2212.0, 21 "Complex_mass2": 0.8, 22 } 23 cls.kaon_entry = { 24 "Complex_q": 1.0, 25 "Complex_p": 1.2, 26 "Complex_pid": 321.0, 27 "Complex_mass2": 0.4, 28 } 29 cls.pion_entry = { 30 "Complex_q": 1.0, 31 "Complex_p": 1.2, 32 "Complex_pid": 211.0, 33 "Complex_mass2": 0.2, 34 } 35 cls.json_data = """{"features_for_train":["Complex_mass2", "Complex_p"], 36 "hyper_params": {"values": {"n_estimators": 596,"max_depth": 5,"learning_rate": 0.07161792803939408}, 37 "ranges": {"n_estimators": [400, 1000],"max_depth": [2, 6],"learning_rate": [0.01, 0.1]}}}""" 38 cls.proton_tree_handler = TreeHandler() 39 cls.proton_tree_handler.set_data_frame(pd.DataFrame([cls.proton_entry] * 10)) 40 cls.kaon_tree_handler = TreeHandler() 41 cls.kaon_tree_handler.set_data_frame(pd.DataFrame([cls.kaon_entry] * 10)) 42 cls.pion_tree_handler = TreeHandler() 43 cls.pion_tree_handler.set_data_frame(pd.DataFrame([cls.pion_entry] * 10)) 44 45 def test_load_hyper_params_ranges(self): 46 target_ranges = { 47 "n_estimators": (400, 1000), 48 "max_depth": (2, 6), 49 "learning_rate": (0.01, 0.1), 50 } 51 with patch("builtins.open", mock_open(read_data=self.json_data)): 52 read_ranges = self.train_model_without_opt.load_hyper_params_ranges() 53 self.assertEqual(read_ranges, target_ranges) 54 55 def test_prepare_train_test_data(self): 56 with patch("builtins.open", mock_open(read_data=self.json_data)): 57 self.train_model_without_opt.prepare_train_test_data( 58 [ 59 self.proton_tree_handler, 60 self.kaon_tree_handler, 61 self.pion_tree_handler, 62 ], 63 0.5, 64 ) 65 self.train_model_with_opt.prepare_train_test_data( 66 [ 67 self.proton_tree_handler, 68 self.kaon_tree_handler, 69 self.pion_tree_handler, 70 ], 71 0.5, 72 ) 73 74 def test_prepare_model_handler(self): 75 with patch("builtins.open", mock_open(read_data=self.json_data)): 76 train_test_data = self.train_model_without_opt.prepare_train_test_data( 77 [ 78 self.proton_tree_handler, 79 self.kaon_tree_handler, 80 self.pion_tree_handler, 81 ], 82 0.1, 83 ) 84 self.train_model_with_opt.prepare_model_handler( # TODO modify and suppress output 85 train_test_data=train_test_data 86 ) 87 self.assertRaises( 88 TypeError, lambda: self.train_model_with_opt.prepare_model_handler() 89 ) 90 self.train_model_without_opt.prepare_model_handler() 91 self.train_model_without_opt_gpu.prepare_model_handler()
A class whose instances are single test cases.
By default, the test code itself should be placed in a method named 'runTest'.
If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.
Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
If it is necessary to override the __init__ method, the base class __init__ method must always be called. It is important that subclasses should not change the signature of their __init__ method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.
When subclassing TestCase, you can set these attributes:
- failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'.
- longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed.
- maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
12 @classmethod 13 def setUpClass(cls): 14 cls.train_model_without_opt = PrepareModel("config.json", False, False) 15 cls.train_model_without_opt_gpu = PrepareModel("config.json", False, True) 16 cls.train_model_with_opt = PrepareModel("config.json", True, False) 17 cls.proton_entry = { 18 "Complex_q": 1.0, 19 "Complex_p": 1.0, 20 "Complex_pid": 2212.0, 21 "Complex_mass2": 0.8, 22 } 23 cls.kaon_entry = { 24 "Complex_q": 1.0, 25 "Complex_p": 1.2, 26 "Complex_pid": 321.0, 27 "Complex_mass2": 0.4, 28 } 29 cls.pion_entry = { 30 "Complex_q": 1.0, 31 "Complex_p": 1.2, 32 "Complex_pid": 211.0, 33 "Complex_mass2": 0.2, 34 } 35 cls.json_data = """{"features_for_train":["Complex_mass2", "Complex_p"], 36 "hyper_params": {"values": {"n_estimators": 596,"max_depth": 5,"learning_rate": 0.07161792803939408}, 37 "ranges": {"n_estimators": [400, 1000],"max_depth": [2, 6],"learning_rate": [0.01, 0.1]}}}""" 38 cls.proton_tree_handler = TreeHandler() 39 cls.proton_tree_handler.set_data_frame(pd.DataFrame([cls.proton_entry] * 10)) 40 cls.kaon_tree_handler = TreeHandler() 41 cls.kaon_tree_handler.set_data_frame(pd.DataFrame([cls.kaon_entry] * 10)) 42 cls.pion_tree_handler = TreeHandler() 43 cls.pion_tree_handler.set_data_frame(pd.DataFrame([cls.pion_entry] * 10))
Hook method for setting up class fixture before running tests in the class.
45 def test_load_hyper_params_ranges(self): 46 target_ranges = { 47 "n_estimators": (400, 1000), 48 "max_depth": (2, 6), 49 "learning_rate": (0.01, 0.1), 50 } 51 with patch("builtins.open", mock_open(read_data=self.json_data)): 52 read_ranges = self.train_model_without_opt.load_hyper_params_ranges() 53 self.assertEqual(read_ranges, target_ranges)
55 def test_prepare_train_test_data(self): 56 with patch("builtins.open", mock_open(read_data=self.json_data)): 57 self.train_model_without_opt.prepare_train_test_data( 58 [ 59 self.proton_tree_handler, 60 self.kaon_tree_handler, 61 self.pion_tree_handler, 62 ], 63 0.5, 64 ) 65 self.train_model_with_opt.prepare_train_test_data( 66 [ 67 self.proton_tree_handler, 68 self.kaon_tree_handler, 69 self.pion_tree_handler, 70 ], 71 0.5, 72 )
74 def test_prepare_model_handler(self): 75 with patch("builtins.open", mock_open(read_data=self.json_data)): 76 train_test_data = self.train_model_without_opt.prepare_train_test_data( 77 [ 78 self.proton_tree_handler, 79 self.kaon_tree_handler, 80 self.pion_tree_handler, 81 ], 82 0.1, 83 ) 84 self.train_model_with_opt.prepare_model_handler( # TODO modify and suppress output 85 train_test_data=train_test_data 86 ) 87 self.assertRaises( 88 TypeError, lambda: self.train_model_with_opt.prepare_model_handler() 89 ) 90 self.train_model_without_opt.prepare_model_handler() 91 self.train_model_without_opt_gpu.prepare_model_handler()
Inherited Members
- unittest.case.TestCase
- TestCase
- addTypeEqualityFunc
- addCleanup
- addClassCleanup
- setUp
- tearDown
- tearDownClass
- countTestCases
- defaultTestResult
- shortDescription
- id
- subTest
- run
- doCleanups
- doClassCleanups
- debug
- skipTest
- fail
- assertFalse
- assertTrue
- assertRaises
- assertWarns
- assertLogs
- assertEqual
- assertNotEqual
- assertAlmostEqual
- assertNotAlmostEqual
- assertSequenceEqual
- assertListEqual
- assertTupleEqual
- assertSetEqual
- assertIn
- assertNotIn
- assertIs
- assertIsNot
- assertDictEqual
- assertDictContainsSubset
- assertCountEqual
- assertMultiLineEqual
- assertLess
- assertLessEqual
- assertGreater
- assertGreaterEqual
- assertIsNone
- assertIsNotNone
- assertIsInstance
- assertNotIsInstance
- assertRaisesRegex
- assertWarnsRegex
- assertRegex
- assertNotRegex
- failUnlessRaises
- failIf
- assertRaisesRegexp
- assertRegexpMatches
- assertNotRegexpMatches
- failUnlessEqual
- assertEquals
- failIfEqual
- assertNotEquals
- failUnlessAlmostEqual
- assertAlmostEquals
- failIfAlmostEqual
- assertNotAlmostEquals
- failUnless
- assert_