def one_hot(ii, oo, Tx, Ty, input_vocab, output_vocab): """ Convert inputs and outputs to one-hot representations. """ icoded = np.array([string_to_int(i, Tx, input_vocab) for i in ii]) ocoded = np.array([string_to_int(o, Ty, output_vocab) for o in oo]) ioh = np.array(list(map(lambda x: to_categorical(x, num_classes=len(input_vocab)), icoded))) ooh = np.array(list(map(lambda x: to_categorical(x, num_classes=len(output_vocab)), ocoded))) return ioh, ooh
def __valider_etape(self): """ Valide la modification d'une étape :return: """ if self.textEtape.toPlainText() and string_to_int( self.textOrdre.text()): my_date = None if self.checkBoxDate.isChecked(): my_date = self.inputDate.dateTime().toPyDateTime() if self.etape_en_cours: ControllerView.modifier_etape( self.etape_en_cours.id, self.selected_idee, string_to_int(self.textOrdre.text()), self.textEtape.toPlainText(), self.etape_en_cours.fait, my_date) else: etat = False deux_min_question = QMessageBox() deux_min_question.setIcon(QMessageBox.Question) deux_min_question.setText( " Cette étape vous prend-elle moins de deux minutes à être réalisé ?" ) deux_min_question.setWindowTitle("Moins de deux minutes ?") deux_min_question.setStandardButtons(QMessageBox.Yes | QMessageBox.No) result = deux_min_question.exec_() if result == QMessageBox.Yes: fin_deux_min_question = QMessageBox() fin_deux_min_question.setIcon(QMessageBox.Warning) fin_deux_min_question.setText( " Alors faites le maintenant!") fin_deux_min_question.setWindowTitle("Faites-le!") fin_deux_min_question.setInformativeText( "La tache sera automatiquement validé") fin_deux_min_question.setStandardButtons(QMessageBox.Ok) fin_deux_min_question.exec_() etat = True self.inputDate.date = None ControllerView.ajouter_etape( self.selected_idee, string_to_int(self.textOrdre.text()), self.textEtape.toPlainText(), etat, my_date) self.__charger_etapes()
def tex_integrals(string): integ = "|".join((r'\int',r'\dint',r'\bigint')) d = "[{(]?-?[a-zA-Z0-9][)}]?" op = re.match("^\W*("+integ+")(_({d})\^({d})|\^({d})_({d}))?(.*)(\W|\\,)*d(.)".format(d=d), string) if op: print op.groups() x = Symbol(op.group(9)) body = fix_implicit_mult(op.group(7),[str(x)]) inner = tex_integrals(body) if inner: body = inner.doit() if op.group(2): up = string_to_int(op.group(4)) or string_to_int(op.group(5)) down = string_to_int(op.group(3)) or string_to_int(op.group(6)) return Integral(body,(x,down, up)).doit() else: return Integral(body,x).doit()
def plain_integrals(string): x, y, z = symbols("x y z") string = string.strip() if get_close_matches(string[:string.find(" ")],['integrate','int']): string = string[string.find(" ")+1:] fr = re.search("\w{3,4}",string) if fr and get_close_matches(fr.group(), ['from']): body, (down, up) = string[:fr.start()], string[fr.end():].split("to" if "to" in string[fr.end():] else "..") if re.match("\W*\w+=\d+\W*",down): x,down = down.split('=') x = Symbol(x) down, up = string_to_int(down), string_to_int(up) body = fix_implicit_mult(body, map(str,[x])) return Integral(body,(x, down, up)).doit() else: down, up = string_to_int(down), string_to_int(up) body = fix_implicit_mult(body, map(str,[x,y,z])) x = sympify(body).free_symbols.pop() return Integral(body,(x, down, up)).doit() else: body = fix_implicit_mult(string, map(str,[x,y,z])) x = sympify(string).free_symbols.pop() return Integral(body,x).doit()
def generate_str(): t = datetime.datetime.now() #t = datetime.datetime.strptime("00:09:00", "%H:%M:%S") h = string_to_int(t.hour) m = string_to_int(t.minute) h_str = "" m_str = "" if h == 2: h_str = "两点" else: h_str = "{}点".format(h) if m == 0: m_str = "整" elif m < 10: m_str = "零{}分".format(m) else: m_str = "{}分".format(m) echo_str = "现在时刻:{}{} ".format(h_str, m_str) return echo_str
def read_csv(filename): statuses = [] with open(filename) as csv_file: reader = csv.reader(csv_file) column_names = next(reader) prev_total_cases = 0 prev_total_deaths = 0 prev_total_tests = 0 for row in reader: try: tmp = { 'reported_date': datetime.strptime(row[0], '%Y-%m-%d'), } except: # noqa continue for index, column in enumerate(row): column_name = column_names[index] field_name = get_field_name_from_column_name(column_name) if field_name: tmp[field_name] = string_to_int(column) # Handle change in test reporting tmp['total_tests_reported'] = tmp['total_tested'] if tmp['negative'] > 0: total_tests_reported = tmp['negative'] + tmp['total_cases'] tmp['total_tests_reported'] = total_tests_reported tmp['new_cases'] = tmp['total_cases'] - prev_total_cases prev_total_cases = tmp['total_cases'] tmp['new_deaths'] = tmp['total_deaths'] - prev_total_deaths prev_total_deaths = tmp['total_deaths'] tmp['new_tests'] = tmp['total_tests_reported'] - prev_total_tests prev_total_tests = tmp['total_tests_reported'] # As of April 15, tests are samples not patients tmp['total_patients_tested'] = tmp['total_tests_reported'] tmp['total_samples_tested'] = None if tmp['reported_date'] > datetime(2020, 4, 14): tmp['total_patients_tested'] = None tmp['total_samples_tested'] = tmp['total_tests_reported'] statuses.append(tmp) return statuses
def read_csv(filename): statuses = [] with open(filename) as f: reader = csv.reader(f) column_names = next(reader) for row in reader: tmp = {'reported_date': datetime.strptime(row[0], '%Y-%m-%d')} for index, value in enumerate(row): field_name = column_names[index] if field_name == 'date' or field_name == 'location': tmp[field_name] = value else: tmp[field_name] = string_to_int(value) statuses.append(tmp) return statuses
def populate_sources(self): logger.info('Populating sources.') session = self._db_client.get_session() for row in self._read_workbook('Sources'): r_source_id, r_jurisdiction_id, r_name, r_code, r_rank = row session.add( models.Source(sourceId=r_source_id, jurisdictionId=utils.format_string( r_jurisdiction_id, utils.NullBehaviour.NULL_TO_NULL), name=utils.format_string(r_name), code=utils.format_string(r_code), rank=utils.string_to_int(r_rank))) session.commit()
def populate_deceased(self): logger.info('Populating deceased.') session = self._db_client.get_session() for row in self._read_workbook('Deceased'): (r_inquest_id, r_last_name, r_given_name, r_age, r_age_unit, r_sex, r_death_location, r_death_date, r_death_cause, r_death_cause_id, r_death_manner_id, r_inquest_reason_id) = row # Validate inquest type. # TODO: remove. if r_inquest_reason_id.startswith('MANDATORY_'): # An inquest is either 'Discretionary' or 'Mandatory-<reason>'; this makes 'Mandatory' redundant. inquest_reason_id = r_inquest_reason_id.replace( 'MANDATORY_', '') else: inquest_reason_id = r_inquest_reason_id # TODO: validate that deceased age is < 18. last_name = utils.format_as_title(r_last_name, utils.NullBehaviour.NULL_TO_NULL) given_names = utils.format_string(r_given_name, utils.NullBehaviour.NULL_TO_NULL) session.add( models.Deceased( inquestId=r_inquest_id, inquestReasonId=inquest_reason_id, deathMannerId=utils.format_as_id( r_death_manner_id), # TODO: remove format_as_id call. deathCauseId=r_death_cause_id, deathCause=utils.format_string(r_death_cause), deathDate=utils.string_to_date(r_death_date), deathLocation=utils.format_string( r_death_location, utils.NullBehaviour.NULL_TO_NULL), lastName=last_name, givenNames=given_names, age=utils.string_to_int(r_age), ageUnit=utils.format_string(r_age_unit), sex=utils.format_string(r_sex))) session.commit()
def read_csv(filename): updates = [] with open(filename) as csv_file: reader = csv.reader(csv_file) column_names = next(reader) for row in reader: tmp = {'reported_date': datetime.strptime(row[3], '%d-%m-%Y')} for index, value in enumerate(row): column_name = column_names[index] field_name = get_field_name_from_column_name(column_name) if field_name: if 'province' in field_name: tmp[field_name] = value else: tmp[field_name] = string_to_int(value) updates.append(tmp) return updates
def get_report(self, country_name): country = self.get_country(country_name) if country.get("Country"): if country.get("ActiveCases") != "N/A": emojis = utils.get_virus_emoji( utils.string_to_int(country.get("ActiveCases"))) else: emojis = "❓❓❓" position = ("World ranking: #" + country["Position"] if country.get("Position") else "") cases_trend = self.get_cases_trend(country.get("NewCases")) deaths_trend = self.get_deaths_trend(country.get("NewDeaths")) cases_per_million = f"({country.get('Total Cases/1M pop')} per million)" deaths_per_million = f"({country.get('Deaths/1M pop')} per million)" if country.get("Tests/1M pop"): tests_per_million = f"They have performed {country.get('Tests/1M pop')} tests per million people." else: tests_per_million = f"Test data is not available." report = f""" _COVID-19 Report for {country.get('Country')}_ {FLAGS.get(country.get('Country'),"")} {emojis} {position} There are {country.get("ActiveCases")} active cases in {country.get("Country")}. {cases_trend} Total cases: {country.get("TotalCases")} {cases_per_million} Total deaths: {country.get("TotalDeaths")} {deaths_trend} {deaths_per_million} Recovered: {country.get("TotalRecovered")} {tests_per_million} """ else: report = "Is the country name correctly spelled? Try /report USA" return report
s, _, c = post_activation_LSTM_cell(context, initial_state=[s, c]) out = output_layer(s) outputs.append(out) model = Model(inputs=[X, s0, c0], outputs=outputs) return model model = model(Tx, Ty, n_a, n_s, len(human_vocab), len(machine_vocab)) #model.summary() opt = Adam(lr=0.005, beta_1=0.9, beta_2=0.999,decay=0.01) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) s0 = np.zeros((m, n_s)) c0 = np.zeros((m, n_s)) outputs = list(Yoh.swapaxes(0,1)) model.fit([Xoh, s0, c0], outputs, epochs=10, batch_size=100) model.save_weights('my_model_weights.h5') EXAMPLES = ['3 May 1979', '5 April 09', '21th of August 2016', 'Tue 10 Jul 2007', 'Saturday May 9 2018', 'March 3 2001', 'March 3rd 2001', '1 March 2001'] for example in EXAMPLES: source = string_to_int(example, Tx, human_vocab) source = np.array(list(map(lambda x: to_categorical(x, num_classes=len(human_vocab)), source))).swapaxes(0, 1) prediction = model.predict([source, s0, c0]) prediction = np.argmax(prediction, axis=-1) output = [inv_machine_vocab[int(i)] for i in prediction] print("source:", example) print("output:", ''.join(output))
except getopt.GetoptError: logger.info("getopt error", exc_info=True) sys.exit(1) mode = DEFAULT size = -1 name = "" volume = 21 for o, v in opts: if o in ("-m", "--mode"): mode = v if v.lower().find("rand") != -1: mode = RANDOM if o in ("-s", "--size"): size = string_to_int(v) if size <= 0: size = -1 if o in ("-n", "--name"): name = v if o in ("-v", "--volume"): volume = string_to_int(v) generate_playlist(mode=mode, size=size) #cmd = "mplayer -playlist {}playlist.lst".format(song_dir) #cmd_res = subprocess.getstatusoutput(cmd) play_songs(name=name, volume=volume)
def populate_references(self): session = self._db_client.get_session() authority_id_to_related = { } # From ID to tuple of citations, related authorities, and related inquests. inquest_id_to_related = { } # From ID to tuple of related authorities and related inquests. logger.info('Populating authorities.') for row in self._read_workbook('Authorities'): (r_authority_id, r_name, r_overview, r_synopsis, r_quotes, r_notes, r_primary, r_primary_text, r_judicial_review, r_keywords, r_tags, r_citations, r_see_authorities, r_see_inquests) = row session.add( models.Authority(authorityId=r_authority_id, isPrimary=r_primary, primaryField=utils.format_string( r_primary_text, utils.NullBehaviour.NULL_TO_NULL), isJudicialReview=r_judicial_review, name=utils.format_string(r_name), overview=utils.format_string( r_overview, utils.NullBehaviour.NULL_TO_STRING), synopsis=utils.format_string( r_synopsis, utils.NullBehaviour.NULL_TO_STRING), quotes=utils.format_string( r_quotes, utils.NullBehaviour.NULL_TO_NULL), notes=utils.format_string( r_notes, utils.NullBehaviour.NULL_TO_NULL), tags=utils.format_as_list( r_tags, utils.NullBehaviour.NULL_TO_NULL))) session.flush() # TODO: behaviour should be NOT NULL. for keyword_id in utils.string_to_list( r_keywords, utils.NullBehaviour.NULL_TO_NULL): session.add( models.AuthorityKeywords( authorityId=r_authority_id, authorityKeywordId=keyword_id, )) authority_id_to_related[r_authority_id] = (r_citations, r_see_authorities, r_see_inquests) self._reference_to_name[( self._REFERENCE_TYPE_AUTHORITY, int(r_authority_id))] = utils.format_string(r_name) logger.info('Populating inquests.') for row in self._read_workbook('Inquests'): (r_inquest_id, r_name, r_overview, r_synopsis, r_notes, r_presiding_officer, r_location, r_start, r_end, r_sitting_days, r_exhibits, r_recommendations, r_jurisdiction_id, r_primary, r_primary_text, r_keywords, r_tags, r_see_authorities, r_see_inquests) = row session.add( models.Inquest(inquestId=r_inquest_id, jurisdictionId=r_jurisdiction_id, location=utils.format_string( r_location, utils.NullBehaviour.NULL_TO_NULL), isPrimary=r_primary, name=utils.format_string(r_name), overview=utils.format_string( r_overview, utils.NullBehaviour.NULL_TO_NULL), synopsis=utils.format_string( r_synopsis, utils.NullBehaviour.NULL_TO_STRING), notes=utils.format_string( r_notes, utils.NullBehaviour.NULL_TO_NULL), presidingOfficer=utils.format_string( r_presiding_officer, utils.NullBehaviour.NULL_TO_STRING), start=utils.string_to_date(r_start), end=utils.string_to_date(r_end), sittingDays=utils.string_to_int( r_sitting_days, utils.NullBehaviour.NULL_TO_NULL), exhibits=utils.string_to_int( r_exhibits, utils.NullBehaviour.NULL_TO_NULL), recommendations=utils.string_to_int( r_recommendations, utils.NullBehaviour.NULL_TO_NULL), tags=utils.format_as_list( r_tags, utils.NullBehaviour.NULL_TO_NULL))) session.flush() inquest_id_to_related[r_inquest_id] = (r_see_authorities, r_see_inquests) self._reference_to_name[( self._REFERENCE_TYPE_INQUEST, int(r_inquest_id))] = utils.format_string(r_name) # TODO: use behaviour NOT_NULL. for keyword_id in utils.string_to_list( r_keywords, utils.NullBehaviour.NULL_TO_NULL): # TODO: remove this if statement. if keyword_id != 'INQUEST_FEDERAL_PROVINCIAL': session.add( models.InquestKeywords( inquestId=r_inquest_id, inquestKeywordId=keyword_id, )) logger.info('Populating authority relationships.') for (r_authority_id, (r_citations, r_see_authorities, r_see_inquests)) in authority_id_to_related.items(): for citation in utils.string_to_list( r_citations, utils.NullBehaviour.NULL_TO_NULL): session.add( models.AuthorityCitations( authorityId=r_authority_id, citedAuthorityId=citation, )) for related_authority in utils.string_to_list( r_see_authorities, utils.NullBehaviour.NULL_TO_NULL): session.add( models.AuthorityRelated( authorityId=r_authority_id, relatedAuthorityId=related_authority, )) for related_inquest in utils.string_to_list( r_see_inquests, utils.NullBehaviour.NULL_TO_NULL): session.add( models.AuthorityInquests( authorityId=r_authority_id, inquestId=related_inquest, )) logger.info('Populating inquest relationships.') # TODO: add tables for inquest relationships. session.commit()