def annealing1(self): if self.sequences['current'].error <= self.sequences['prev'].error: self.sequences['prev'] = Sequence( self.sequences['current'].inbound_sequence, self.sequences['current'].outbound_sequence) self.sequences['prev'].error = self.sequences['current'].error if self.sequences['current'].error < self.sequences['best'].error: self.sequences['best'] = Sequence( self.sequences['current'].inbound_sequence, self.sequences['current'].outbound_sequence) self.sequences['best'].error = self.sequences['current'].error logging.info( "current error {0}, best error {1}, prev error {2}".format( self.sequences['current'].error, self.sequences['best'].error, self.sequences['prev'].error)) else: p_accept = math.exp( (self.sequences['prev'].error - self.sequences['current'].error) / self.temperature) logging.info( "p current error {0}, prev error {1}, temperature {2}, p_accept{3}" .format(self.sequences['current'].error, self.sequences['prev'].error, self.temperature, p_accept)) if p_accept >= random.random(): logging.info("p accepted") self.sequences['prev'] = Sequence( self.sequences['current'].inbound_sequence, self.sequences['current'].outbound_sequence) self.sequences['prev'].error = self.sequences['current'].error self.temperature = self.temperature_reduction_rate * self.temperature
def test_get_matrix_nested_sequence(self): uut = Sequence() nested = Sequence() nested.register_node(MockComponent("T1"), None) nested.register_node(MockComponent("T2"), np.add) uut.register_node(MockComponent("T3", [[3, 0], [0, 3]]), None) uut.register_node(nested, np.multiply) expected = [[6, 0], [0, 6]] actual = uut.get_matrix().tolist() self.assertEqual(expected, actual)
def read(self, io_buffer): header = '' bases = '' for line in io_buffer: if line[0] == '>': if len(header) > 0: # Save the data self.seqs.append(Sequence(header, bases)) header = line[1:].strip().split()[0] # Get the next header bases = '' else: bases += line.strip() # Add the last sequence self.seqs.append(Sequence(header, bases)) return self.seqs
def test_max_gene_length_filter(self): gene_length_range = MaxGeneLengthFilter(30) # Create a mock sequence seq = Sequence() test_gene0 = Mock(identifier='foo1') test_gene1 = Mock(identifier='foo2') test_gene2 = Mock(identifier='foo3') test_gene0.death_flagged = False test_gene1.death_flagged = False test_gene2.death_flagged = False test_gene0.length = Mock(return_value=20) test_gene1.length = Mock(return_value=30) test_gene2.length = Mock(return_value=40) # Give the sequence some genes seq.genes = [test_gene0, test_gene1, test_gene2] # Apply the filter gene_length_range.apply(seq) self.assertEqual(seq.genes, [test_gene0, test_gene1])
def __init__(self, axes, origin=None, scale=1, add_coords=None, style=None, convert_2d=None, label_vertices=False): """Construct an instance.""" self._sequence = Sequence() if convert_2d is None: self._convert_2d = self._first_two_coordinates else: self._convert_2d = convert_2d self._square = utility.square(origin, scale, add_coords=add_coords) if style: self._patch = patches.Polygon(self._square[:, :2], **style) else: self._patch = patches.Polygon(self._square[:, :2]) axes.add_patch(self._patch) self._labels = [] if label_vertices: for (x, y), label in zip(self._patch.get_xy(), ["BL", "TL", "TR", "BR"]): text = axes.text(x, y, label) text.set_clip_on(True) self._labels.append(text) self._update_index = 0
def test_remove_terminal_ns_beginning_and_end(self): badseq = Sequence('badseq', 'nnGATTACAnNNn') mockgene = Mock() mockgene.indices = [3, 8] badseq.genes = [mockgene] badseq.remove_terminal_ns() self.assertEquals("GATTACA", badseq.bases)
def __init__(self, data=DataStore): QDialog.__init__(self) self.data = data self.inbound_sequence_list = [] self.outbound_sequence_list = [] self.inbound_buttons = [] self.outbound_buttons = [] self.inboundlayout = QHBoxLayout() self.outboundlayout = QHBoxLayout() self.layout = QVBoxLayout() self.layout.addLayout(self.inboundlayout) self.layout.addLayout(self.outboundlayout) self.done_button = QPushButton('Done') self.done_button.clicked.connect(self.sequence_done) self.layout.addWidget(self.done_button) self.setWindowTitle('Set Sequence') self.setGeometry(300, 400, 500, 500) self.setWindowModality(Qt.ApplicationModal) self.setLayout(self.layout) self.set_truck_and_doors() self.set_buttons() self.sequence = Sequence()
def generate_next_sequence(self, prev_sequence=Sequence()): new_coming = self.generate_random(prev_sequence.coming_sequence) new_going = self.generate_random(prev_sequence.going_sequence) prev_sequence.coming_sequence = new_coming prev_sequence.going_sequence = new_going self.sequence = prev_sequence self.sequence.set_sequences(self.sequence.coming_sequence, self.sequence.going_sequence) return self.sequence
def __init__(self, data, sequence=Sequence()): super(EnterSequenceWidget, self).__init__() self.setupUi(self) self.data = data self.comingSequenceList = [] self.goingSequenceList = [] self.setup_sequence_elements() self.sequence = sequence self.enterSequenceButton.clicked.connect(self.get_sequence)
def test_get_matrix_two_elements(self): uut = Sequence() uut.register_node(MockComponent("T1"), None) uut.register_node(MockComponent("T2"), np.add) expected = [[2, 0], [0, 2]] actual = uut.get_matrix().tolist() self.assertEqual(expected, actual)
def test_register_node(self): uut = Sequence() component = MockComponent("T") coalescer = lambda: None uut.register_node(component, coalescer) self.assertEqual(1, len(uut._nodes)) self.assertIs(component, uut.get_node(0).get_component()) self.assertIs(coalescer, uut.get_node(0).get_coalescer())
def test_max_intron_length_filter(self): intron_length = MaxIntronLengthFilter(30) # Create a mock sequence seq = Sequence() # Give the sequence some genes seq.genes = [ Gene('foo_seq', 'geib_labs', [1, 2], '+', 'foo1'), Gene('foo_seq', 'geib_labs', [1, 2], '+', 'foo2'), Gene('foo_seq', 'geib_labs', [1, 2], '+', 'foo3') ] # Give the mock mrnas some exon's test_mrna0 = Mock() test_mrna0.identifier = 'foo1-RA' test_mrna0.death_flagged = False test_mrna0.exon = Mock() test_mrna0.get_shortest_intron = Mock(return_value=20) test_mrna0.get_longest_intron = Mock(return_value=20) test_mrna1 = Mock() test_mrna1.identifier = 'foo2-RA' test_mrna1.death_flagged = False test_mrna1.exon = Mock() test_mrna1.get_shortest_intron = Mock(return_value=30) test_mrna1.get_longest_intron = Mock(return_value=30) test_mrna2 = Mock() test_mrna2.identifier = 'foo2-RB' test_mrna2.death_flagged = False test_mrna2.exon = None test_mrna3 = Mock() test_mrna3.identifier = 'foo3-RA' test_mrna3.death_flagged = False test_mrna3.exon = Mock() test_mrna3.get_shortest_intron = Mock(return_value=40) test_mrna3.get_longest_intron = Mock(return_value=40) # Give the mock genes some mrnas seq.genes[0].mrnas = [test_mrna0] seq.genes[0].death_flagged = False seq.genes[1].mrnas = [test_mrna1, test_mrna2] seq.genes[1].death_flagged = False seq.genes[2].mrnas = [test_mrna3] seq.genes[2].death_flagged = False # Apply the filter intron_length.apply(seq) self.assertEqual(len(seq.genes), 3) self.assertEqual(seq.genes[0].mrnas, [test_mrna0]) self.assertEqual(seq.genes[1].mrnas, [test_mrna1, test_mrna2])
def __init__(self): self.iteration_number = 0 self.solution_type = "" self.solution_name = "" self.model = Model() self.solver_data = SolverData() self.data = DataStore() self.iteration_results = IterationResults() self.sequence_list = SequenceList() self.current_sequence = Sequence() self.iteration_saved = False
def test_get_matrix_one_element(self): uut = Sequence() component = MockComponent("T") coalescer = lambda: None uut.register_node(component, coalescer) expected = component.get_matrix().tolist() actual = uut.get_matrix().tolist() self.assertEqual(expected, actual)
def generate(self, sess, num_notes, save_midi_path): # initialize sequence object with starting sequence (required for next state) # notes = np.zeros((1,self.hparams.input_len,hparams.input_size)) note_sequence = Sequence(epsilon=self.hparams.epsilon) for i in range(self.hparams.input_len): row = np.zeros(self.hparams.input_size) row[np.random.randint(60, 72)] = 1 note_sequence.add(row) def weighted_pick( weights): # because np.random.choice behaves messed up t = np.cumsum(weights) s = np.sum(weights) return (int(np.searchsorted(t, np.random.rand(1) * s))) ## TODO AKASH: Incorporate beam-search type method for generation for _ in range(num_notes): input = note_sequence[-self.hparams.input_len:] input = np.expand_dims(np.vstack(input), axis=0) feed_dict = self.create_feed_dict(inputs=input) newstate_np, no_notes = sess.run( [self.y_hat_chord, self.y_hat_num_notes], feed_dict=feed_dict) no_notes = int(np.around(no_notes)) # generate next state by sampling from output distribution logit = np.squeeze(newstate_np) avg = np.mean(logit) probs = np.exp(logit - avg) / np.sum( np.exp(logit - avg)) # computing softmax gen_notes = np.zeros(self.hparams.input_size) if no_notes == 1: gen_notes[np.random.choice(self.hparams.input_size, p=probs)] = 1 else: p = probs[:-1] / np.sum(probs[:-1]) indices = np.random.choice(self.hparams.input_size - 1, size=no_notes, p=p) gen_notes[indices] = 1 # convert, and add state to sequence object note_sequence.add(gen_notes) # notes_temp = np.vstack((notes[0,:,:], sampled_state)) # notes = notes_temp.reshape(1,notes_temp.shape[0],notes_temp.shape[1]) # return sequence object / write to file note_sequence.many_hot_to_midi(save_midi_path) # matrix_to_midi(notes.reshape(notes.shape[1],notes.shape[2]), save_midi_path) ###############################################################################
def __init__(self): self.data_set_number = None self.iteration_number = None self.solution_name = None self.number_of_inbound_trucks = 0 self.number_of_outbound_trucks = 0 self.number_of_compound_trucks = 0 self.solution_sequence = Sequence() self.coming_truck_times = {} self.going_truck_times = {}
def test_max_cds_length_filter(self): cds_length = MaxCDSLengthFilter(100) # Create a mock sequence seq = Sequence() # Give the sequence some genes seq.genes = [ Gene('foo_seq', 'geib_labs', [1, 2], '+', 'foo1'), Gene('foo_seq', 'geib_labs', [1, 2], '+', 'foo2'), Gene('foo_seq', 'geib_labs', [1, 2], '+', 'foo3') ] # Give the mock mrnas some cds's test_mrna0 = Mock() test_mrna0.identifier = 'foo1-RA' test_mrna0.death_flagged = False test_mrna0.cds = Mock() test_mrna0.cds.length = Mock(return_value=90) test_mrna1 = Mock() test_mrna1.identifier = 'foo2-RA' test_mrna1.death_flagged = False test_mrna1.cds = None test_mrna2 = Mock() test_mrna2.identifier = 'foo2-RB' test_mrna2.death_flagged = False test_mrna2.cds = Mock() test_mrna2.cds.length = Mock(return_value=100) test_mrna3 = Mock() test_mrna3.identifier = 'foo3-RA' test_mrna3.death_flagged = False test_mrna3.cds = Mock() test_mrna3.cds.length = Mock(return_value=110) # Give the mock genes some mrnas seq.genes[0].mrnas = [test_mrna0] seq.genes[0].death_flagged = False seq.genes[1].mrnas = [test_mrna1, test_mrna2] seq.genes[1].death_flagged = False seq.genes[2].mrnas = [test_mrna3] seq.genes[2].death_flagged = False # Apply the filter cds_length.apply(seq) self.assertEqual(len(seq.genes), 3) self.assertEqual(seq.genes[0].mrnas, [test_mrna0]) self.assertEqual(seq.genes[1].mrnas, [test_mrna1, test_mrna2])
def read_fastq(io_buffer): """Returns a generator of Sequence objects""" scores_are_next = False header = '' bases = '' scores = [] for line in io_buffer: if line[0] == '@' and not scores_are_next: if len(header) > 0: # Yield the previous Sequence yield Sequence(header, bases, scores) header = line[1:].strip() # Get the next header bases = "" elif line[0] == '+': scores_are_next = True else: if scores_are_next: scores = translate_scores(line.strip()) scores_are_next = False else: bases += line.strip() # Add the last sequence yield Sequence(header, bases, scores)
def test_min_intron_length_filter_doesnt_remove_single_exon_mrnas(self): intron_length = MinIntronLengthFilter(30) seq = Sequence() gene, mrna, exon = Mock(), Mock(), Mock() gene.death_flagged = False seq.genes = [gene] gene.mrnas = [mrna] mrna.identifier = "badmrna" mrna.get_shortest_intron.return_value = 0 mrna.death_flagged = False mrna.exon = exon self.assertEquals(1, len(seq.genes[0].mrnas)) intron_length.apply(seq) # Filter shouldn't remove mRNA based on a shortest_intron = 0 return value self.assertEquals(1, len(seq.genes[0].mrnas)) assert not gene.remove_mrna.called
def __init__(self): QThread.__init__(self) self.inbound_trucks = OrderedDict() self.outbound_trucks = OrderedDict() self.compound_trucks = OrderedDict() self.coming_trucks = OrderedDict() self.going_trucks = OrderedDict() self.all_trucks = OrderedDict() self.shipping_doors = OrderedDict() self.receiving_doors = OrderedDict() self.errors = OrderedDict() self.all_doors = OrderedDict() self.element_list = [] self.current_time = 0 self.time_limit = 0 self.current_sequence = Sequence() self.done = False self.time_list = [] self.simulation_on = False self.simulator = None
def __init__(self): super(MainWindow, self).__init__() self.setupUi(self) self.data = DataStore() self.solver_data = SolverData() self.solver = Solver(self.data, self.solver_data) self.update_data_table() self.setup_data() self.value_connections() self.connections() self.combobox_coming_sequence = [] self.combobox_going_sequence = [] self.statusBar().showMessage('Ready') self.load_generated_data() self.results = OrderedDict() self.shoved_solution_name = "" self.shoved_iteration = IterationResults() self.shoved_solution = ModelResult() self.shoved_iteration_number = 0 self.continue_solution = True self.showing_result = [] self.result_times = {} self.function_type = "normal" self.solution_name = "" self.solution_number = 0 self.sequence_solver = SequenceSolver() self.solution_results = dict() self.enter_sequence_widget = EnterSequenceWidget(self.data) self.current_sequence = Sequence() self.load_data() self.simulationStartButton.setEnabled(False) self.simulationStepForwardButton.setEnabled(False) self.result_show_best_solution_button.setEnabled(False) self.result_show_errors_button.setEnabled(False) self.result_show_sequences_button.setEnabled(False) self.result_show_trucktimes_button.setEnabled(False) self.result_show_truckgoods_button.setEnabled(False) self.run_simulation_button.setEnabled(False)
def run_epoch(self, sess, files): num_files, epoch_loss = 0, 0 # iterate over files, read in as sequence object # create batches in RNN representation (one/many hot) train for file in files: sequence = Sequence(epsilon=self.hparams.epsilon) sequence.load_midi(file, join_tracks=False) if sequence is None or len(sequence) < self.hparams.input_len + 1: continue else: num_files += 1 inputs = [] labels = [] for i in range(len(sequence) - self.hparams.input_len): inputs.append(sequence[i:i + self.hparams.input_len]) labels.append(sequence[i + self.hparams.input_len]) inputs = np.stack(inputs) labels = np.vstack(labels) epoch_loss += self.train_batch(sess, inputs, labels) epoch_loss /= num_files return epoch_loss
def test_get_matrix_no_elements(self): uut = Sequence() with self.assertRaises(ValueError): uut.get_matrix()
def test_how_many_Ns_forward_returns_zero_if_no_Ns(self): badseq = Sequence('seq2', 'GATTACA') self.assertEqual(0, badseq.how_many_Ns_forward(3))
def test_how_many_Ns_backward(self): badseq = Sequence('seq3', 'gattaNnN') self.assertEqual(3, badseq.how_many_Ns_backward(8))
def test_how_many_Ns_forward(self): badseq = Sequence('seq1', 'NNnNNGATTACA') self.assertEqual(5, badseq.how_many_Ns_forward(1))
def setUp(self): self.seq1 = Sequence("seq1", "GATTACA")
def enter_sequence(self): self.current_sequence = Sequence(self.data) self.enter_sequence_widget = EnterSequenceWidget( self.data, self.current_sequence) self.enter_sequence_widget.show() self.simulationStartButton.setEnabled(True)
def __init__(self, data=DataStore()): self.algorithm_name = "" self.data_set_number = 0 self.data = data self.arrivals = {} self.sequence = Sequence(self.data)
def test_instance(self): uut = Sequence() self.assertIsNotNone(uut)