def test_init(self): with self.subTest( "Do not allow monomers in the configuration -- only polymers"): with self.assertRaises(AssertionError): Configuration({self.polymer_1x: 1, self.y: 1}) with self.assertRaises(AssertionError): Configuration({self.x: 1, self.polymer_1y: 1}) with self.subTest( "Do not allow domains in the configuration -- only polymers"): with self.assertRaises(AssertionError): Configuration({self.polymer_1x: 1, Domain("y"): 1}) with self.assertRaises(AssertionError): Configuration({Domain("x"): 1, self.polymer_1y: 1}) with self.subTest("Allow multiple of same type of polymer"): test_configuration = Configuration({ self.polymer_1x: 1, self.polymer_1y: 2 }) self.assertTrue(3, test_configuration.number_of_polymers()) with self.subTest("Allow infinite quantities of polymers"): test_configuration = Configuration({ self.polymer_1x: infinity, self.polymer_1y: 2 }) self.assertTrue(infinity, test_configuration.number_of_polymers())
def test_eq(self): test_configurations = [] for i in range(2): test_configurations.append( Configuration({ self.polymer_1y: 1, self.polymer_1x_1y: 2, self.polymer_2x_3y: 3, self.polymer_1x: 4 })) self.assertEqual(test_configurations[0], test_configurations[0]) self.assertEqual(test_configurations[1], test_configurations[1]) self.assertEqual(test_configurations[0], test_configurations[1]) self.assertNotEqual(test_configurations[0], Configuration({})) self.assertNotEqual(Configuration({}), test_configurations[0])
def _interpret_solution( self, variable_to_value_dictionary: Dict[Any, int]) -> Configuration: """ uses the provided dictionary to convert solution variables into solution values and from this, converts the solutions values into the corresponding configuration """ this_configuration_dict = {} n = len(self.ordered_monomers) discovered = [False for _ in range(n)] for i in range(n): if not discovered[i]: discovered[i] = True this_polymer_dict = {self.ordered_monomers[i]: 1} for j in range(i + 1, n): if variable_to_value_dictionary[self.grouping_vars[ i, j]] > 0: # Monomers i and j are bound discovered[j] = True this_polymer_dict[self.ordered_monomers[j]] =\ 1 + this_polymer_dict.get(self.ordered_monomers[j], 0) this_polymer = Polymer(this_polymer_dict) this_configuration_dict[ this_polymer] = 1 + this_configuration_dict.get( this_polymer, 0) return Configuration(this_configuration_dict)
def highlight_syntax(self, enable=True): syntax_file = Path(Path( self.file_io.file_name).suffix[1:]).with_suffix('.hs') hs_config_data = Configuration.get_hs_configuration(syntax_file) if hs_config_data is None: print('No syntax file ', syntax_file) return #print(hs_config_data) keywords = hs_config_data['keyword']['tags'] keyword_fgcolor = hs_config_data['keyword']['color'] constant_fgcolor = hs_config_data['constant']['color'] numbers = re.findall(r'\d{1,3}', self.file_io.file_data) self.editor.tag_config('tg_kw', foreground=keyword_fgcolor) self.editor.tag_config('tg_num', foreground=constant_fgcolor) #keywords = ['package', 'public', 'private', 'abstract', 'internal', 'new', 'static', 'final', 'long', 'extends', # 'class', 'import', 'null', 'for', 'if', 'return', 'int', 'char', 'float', 'double', 'implements'] for keyword in keywords: self.editor.mark_set(tk.INSERT, '1.0') while True: located_end = self.highlight_keyword(keyword + ' ', 'tg_kw') if located_end == 0: break self.editor.mark_set(tk.INSERT, located_end) self.editor.mark_set(tk.INSERT, '1.0') for each_number in numbers: located_end = self.highlight_keyword(each_number, 'tg_num') if located_end != 0: self.editor.mark_set(tk.INSERT, located_end) print("Syntax highlight executed.")
def test_number_of_merges(self): self.assertEqual(0, Configuration({}).number_of_merges()) self.assertEqual( 1, Configuration({ self.polymer_1x_1y: 1 }).number_of_merges()) self.assertEqual( 4, Configuration({ self.polymer_2x_3y: 1 }).number_of_merges()) self.assertEqual( 5, Configuration({ self.polymer_1x_1y: 1, self.polymer_2x_3y: 1 }).number_of_merges()) self.assertEqual( 9, Configuration({ self.polymer_1x_1y: 1, self.polymer_2x_3y: 2 }).number_of_merges()) self.assertEqual( 10, Configuration({ self.polymer_1x_1y: 2, self.polymer_2x_3y: 2 }).number_of_merges()) self.assertEqual( 0, Configuration({ self.polymer_1x: 1, self.polymer_1y: infinity }).number_of_merges()) self.assertEqual( 1, Configuration({ self.polymer_1x_1y: 1, self.polymer_1y: infinity }).number_of_merges()) self.assertEqual( infinity, Configuration({ self.polymer_1x_1y: infinity, self.polymer_1y: 1 }).number_of_merges())
def test_energy(self): # values determined by regression testing monomer_a = Monomer.from_string("a a b b c d") monomer_b = Monomer.from_string("a* b* c*") polymer_a = Polymer({monomer_a: 1}) polymer_b = Polymer({monomer_b: 1}) polymer_ab = Polymer({monomer_a: 1, monomer_b: 1}) polymer_aab = Polymer({monomer_a: 2, monomer_b: 1}) polymer_abb = Polymer({monomer_a: 1, monomer_b: 2}) self.assertEqual(0.0, Configuration({}).energy(bond_weighting_factor=0.4)) self.assertEqual( 1.0, Configuration({ polymer_a: 1, polymer_ab: 1 }).energy(bond_weighting_factor=0.4)) self.assertEqual( 2.2, Configuration({ polymer_a: infinity, polymer_b: 1, polymer_ab: 1 }).energy(bond_weighting_factor=0.4)) self.assertEqual( 1.8, Configuration({ polymer_b: infinity, polymer_ab: 1 }).energy(bond_weighting_factor=0.4)) self.assertEqual( 4.8, Configuration({ polymer_b: infinity, polymer_aab: 1 }).energy(bond_weighting_factor=0.4)) self.assertEqual( 2.4, Configuration({ polymer_a: infinity, polymer_abb: 1 }).energy(bond_weighting_factor=0.4)) # case to test a fix to a bug in a specific example in which energy was misreported as 0.8 (should be 1.6) # two limiting monomers with two domains each, all as singletons => 0.4(4 domains) = 1.6 monomer_x = Monomer.from_string("a b") monomer_y = Monomer.from_string("a* b*") polymer_x = Polymer({monomer_x: 1}) polymer_y = Polymer({monomer_y: 1}) self.assertEqual( 1.6, Configuration({ polymer_x: 2, polymer_y: infinity }).energy(bond_weighting_factor=0.4))
def __run_program(configuration_line): """ Run DeepSpeech - save log file. """ configuration_file_path, *parameters = configuration_line.split('|') configuration = Configuration(configuration_file_path) __update_parameters(configuration, parameters) __create_experiment_dir(configuration) deepspeech_output = os.path.join(configuration.exp_dir, 'program.out') with open(deepspeech_output, 'w') as f: with redirect_stdout(f): ds = DeepSpeech(configuration) ds.train() ds.save()
def _interpret_solution( self, variable_to_value_dictionary: Dict[Any, int]) -> Configuration: """ uses the provided dictionary to convert solution variables into solution values and from this, converts the solutions values into the corresponding configuration """ this_configuration_dict = {} for i, basis_vector in enumerate(self.polymer_basis.T): this_polymer = self._make_polymer_from_vector(basis_vector) count = variable_to_value_dictionary[self.basis_coefficients[i]] if count > 0: this_configuration_dict[this_polymer] = count return Configuration(this_configuration_dict)
def construct(cls, config_path: str, alphabet_path: str) -> 'DeepSpeech': """ Construct DeepSpeech object base on the configuration and the alphabet files. """ config = Configuration(config_path) model_dir = os.path.dirname(config_path) gpus = get_available_gpus() model = cls.get_model(is_gpu=len(gpus) > 0, **config.model) loss = cls.get_loss() optimizer = cls.get_optimizer(**config.optimizer) callbacks = cls.get_callbacks(home_dir=model_dir, configurations=config.callbacks) alphabet = cls.get_alphabet(alphabet_path) features_extractor = cls.get_features_extractor(**config.features_extractor) decoder = cls.get_decoder(alphabet=alphabet, model=model, **config.decoder) return cls(model, loss, optimizer, callbacks, alphabet, decoder, features_extractor, gpus)
def _interpret_solution( self, variable_to_value_dictionary: Dict[Any, int]) -> Configuration: """ uses the provided dictionary to convert solution variables into solution values and from this, converts the solutions values into the corresponding configuration """ number_of_polymers = 1 + max(j for _, j in self.polymer_composition_vars) this_configuration_dict = {} for j in range(number_of_polymers): this_polymer_dict = {} for i, monomer in enumerate(self.ordered_monomer_types): monomer_count = variable_to_value_dictionary[ self.polymer_composition_vars[i, j]] if monomer_count > 0: this_polymer_dict[ monomer] = monomer_count + this_polymer_dict.get( monomer, 0) if this_polymer_dict: # not an empty polymer this_polymer = Polymer(this_polymer_dict) this_configuration_dict[ this_polymer] = 1 + this_configuration_dict.get( this_polymer, 0) partial_configuration = Configuration(this_configuration_dict) difference_tbn = self.tbn - partial_configuration.flatten() for monomer_type in difference_tbn.monomer_types(): singleton_polymer = Polymer({monomer_type: 1}) this_configuration_dict[singleton_polymer] = \ difference_tbn.count(monomer_type) + this_configuration_dict.get(singleton_polymer, 0) return Configuration(this_configuration_dict)
def test_size(self): test_polymers = [ self.polymer_1x, self.polymer_1y, self.polymer_1x_1y, self.polymer_2x_3y, ] with self.subTest("Sums over single copies of different polymers"): self.assertEqual( len(test_polymers), Configuration({polymer: 1 for polymer in test_polymers }).number_of_polymers()) with self.subTest("Sums over multiple copies of the same polymer(s)"): for i in range(1, 4): for j in range(1, 4): self.assertEqual( i + j, Configuration({ test_polymers[0]: i, test_polymers[1]: j }).number_of_polymers()) with self.subTest( "Reports 'inf' if any polymer has infinite quantity"): self.assertEqual( infinity, Configuration({ test_polymers[2]: 3, test_polymers[3]: infinity }).number_of_polymers()) self.assertEqual( infinity, Configuration({ test_polymers[2]: infinity, test_polymers[3]: infinity }).number_of_polymers())
def test_str(self): test_configuration = Configuration({ self.polymer_1y: 1, self.polymer_1x_1y: 2, self.polymer_2x_3y: 3, self.polymer_1x: 4 }) with self.subTest( "able to display singletons in a configuration string"): intended_output_with_singletons = "3{2(X), 3(Y)}; 2{X, Y}; 4{X}; {Y}" self.assertEqual(intended_output_with_singletons, test_configuration.full_str()) with self.subTest( "able to suppress singleton output in a configuration string"): intended_output = "3{2(X), 3(Y)}; 2{X, Y}" self.assertEqual(intended_output, str(test_configuration)) inf_test_configuration = Configuration({ self.polymer_1y: infinity, self.polymer_1x_1y: infinity, self.polymer_2x_3y: 3, self.polymer_1x: 4 }) with self.subTest( "able to display singletons in a configuration string"): intended_output_with_singletons = "3{2(X), 3(Y)}; inf{X, Y}; 4{X}; inf{Y}" self.assertEqual(intended_output_with_singletons, inf_test_configuration.full_str()) with self.subTest( "able to suppress singleton output in a configuration string"): intended_output = "3{2(X), 3(Y)}; inf{X, Y}" self.assertEqual(intended_output, str(inf_test_configuration))
def test_flatten(self): self.assertEqual(Tbn({}), Configuration({}).flatten()) self.assertEqual(Tbn({ self.x: 1, self.y: 1 }), Configuration({ self.polymer_1x_1y: 1 }).flatten()) self.assertEqual(Tbn({ self.x: 2, self.y: 3 }), Configuration({ self.polymer_2x_3y: 1 }).flatten()) self.assertEqual( Tbn({ self.x: 3, self.y: 4 }), Configuration({ self.polymer_1x_1y: 1, self.polymer_2x_3y: 1 }).flatten()) self.assertEqual( Tbn({ self.x: 5, self.y: 7 }), Configuration({ self.polymer_1x_1y: 1, self.polymer_2x_3y: 2 }).flatten()) self.assertEqual( Tbn({ self.x: 6, self.y: 8 }), Configuration({ self.polymer_1x_1y: 2, self.polymer_2x_3y: 2 }).flatten()) self.assertEqual( Tbn({ self.x: 1, self.y: infinity }), Configuration({ self.polymer_1x: 1, self.polymer_1y: infinity }).flatten()) self.assertEqual( Tbn({ self.x: 1, self.y: infinity }), Configuration({ self.polymer_1x_1y: 1, self.polymer_1y: infinity }).flatten()) self.assertEqual( Tbn({ self.x: infinity, self.y: infinity }), Configuration({ self.polymer_1x_1y: infinity, self.polymer_1y: 1 }).flatten())
import argparse import os from source.deepspeech import DeepSpeech from source.configuration import Configuration abspath = os.path.abspath(__file__) ROOT_DIR = os.path.dirname(abspath) os.chdir(ROOT_DIR) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('--configuration', help='Experiment configuration') args = parser.parse_args() # Read configuration file config = Configuration(file_path=args.configuration) # Set up DeepSpeech object ds = DeepSpeech(config) # Model optimization ds.train() # Save whole deepspeech model ds.save()