def __init__(self): QtGui.QMainWindow.__init__(self) self.logfile = open(BASEPATH+"/log.txt", "w") self.logfile.write("") self.logfile.close() self.logfile = open(BASEPATH+"/log.txt", "a") self.addons = [] #load the config self.config = Config(CONFIGPATH) if not os.path.exists(CONFIGPATH): self.initializeConfig() self.config.loadData() self.history = History(100) self.translations = translations.Translations(self.config["language"]) self.project = widgets.ProjectExplorer(self, "") self.projectPath = "" self.baseModClass = None self.guiClass = None self.initUI() self.initializeAddons()
def __init__(self): super(MyCalcWindow, self).__init__() self.setupUi(self) self.setFixedSize(self.width(), self.height()) # lock the window size ################################## self.PRECISION = self.verticalSlider.value() self.history = "" self._history = History(parent=self) self.compute_times = 0 self.lcdNumber.display(self.PRECISION) self.connecter() self.show() self.press_AC()
def from_initial(): # Returns a chess object with pieces at their initial positions history_stack = History() board = Board(history_stack) for piece_cls, player, square in INITIAL_POSITIONS: board.add(piece_cls(player, board), square) return Chess(board, Player.WHITE, history_stack)
def __init__(self): QtGui.QMainWindow.__init__(self) self.logfile = open(BASEPATH + "/log.txt", "w") self.logfile.write("") self.logfile.close() self.logfile = open(BASEPATH + "/log.txt", "a") self.addons = [] # load the config self.config = Config(CONFIGPATH) if not os.path.exists(CONFIGPATH): self.initializeConfig() self.config.loadData() self.history = History(100) self.translations = translations.Translations(self.config["language"]) self.project = widgets.ProjectExplorer(self, "") self.projectPath = "" self.baseModClass = None self.guiClass = None self.initUI() self.initializeAddons()
def main(): # Load augmented images and keypoints training_images = np.load('data/training_images_augs.npy') training_labels = np.load('data/training_keypoints_augs.npy') valid_images = np.load('data/valid_images_augs.npy') valid_labels = np.load('data/training_keypoints_augs.npy') # divide by 255 training_images = training_images / 255 valid_images = training_images / 255 # Construct a model model = my_model() # Compile # opt = keras.optimizers.SGD(lr=1e-2, momentum=0.9, nesterov=True) opt = keras.optimizers.Adam(lr=1e-4, decay=1e-6) model.compile(optimizer=opt, loss=root_mean_squared_error) # Callbacks save_dir = 'tmp/saved_model/weights.{epoch:02d}-{val_loss:.2f}.hdf5' ckpt = keras.callbacks.ModelCheckpoint(save_dir, monitor='val_loss', verbose=1, save_best_only=True, period=25) hists = History() callbacks = [hists, ckpt] # Training model.fit(training_images, training_labels, batch_size=64, epochs=5, validation_data=(valid_images, valid_labels), callbacks=callbacks) # Plot loss curves hists.plot() # Visualize predictions test_images = np.load('data/test_images.npy') / 255 preds = model.predict(test_images[..., np.newaxis], batch_size=256) idx = np.random.randint(preds.shape[0]) plt.imshow(test_images[idx], cmap='gray') plt.scatter(preds[idx][:, 0], preds[idx][:, 1], c='r', s=20, marker='.') plt.show()
def fill_comp_ordered_history_list(file_path): with open(file_path) as f: hist_sentence_list = [] for idx, line in enumerate(f): splited_words = split(' |,\n', line[:-1]) if line[-1] == '\n' else split(' |,\n', line) new_sentence_hist_list = [] for word in splited_words: cur_hist = History(cword=word, pptag=None, ptag=None, ctag=None, nword=None, pword=None, ppword=None, nnword=None) new_sentence_hist_list.append(cur_hist) hist_sentence_list.append(new_sentence_hist_list) return hist_sentence_list
def _calc_hist_dicts(hist_sentence_list: [[History]], q: mp.Queue, self): """ method that calculates two dictionaries - hist_to_all_tag_feature_matrix_dict and hist_to_feature_vec_dict :param hist_sentence_list: sentenc :param q: :param self: :return: """ hist_to_feature_vec_dict = dict() hist_to_all_tag_feature_matrix_dict = dict() for idx_sentence, sentence in enumerate(hist_sentence_list): if idx_sentence % 500 == 0: gc.collect() print(f'filling sentence number {idx_sentence}') for hist in sentence: tag_set = self.tags_set cur_feature_vecs = [] hist_to_feature_vec_dict[hist] = csr_matrix(self.get_non_zero_feature_vec_indices_from_history(hist)) for ctag in tag_set: new_hist = History(cword=hist.cword, pptag=hist.pptag, ptag=hist.ptag, ctag=ctag, nword=hist.nword, pword=hist.pword, nnword=hist.nnword, ppword=hist.ppword) cur_feature_vecs.append(self.get_non_zero_feature_vec_indices_from_history(new_hist)) key_all_tag_hist = History(cword=hist.cword, pptag=hist.pptag, ptag=hist.ptag, ctag=None, nword=hist.nword, pword=hist.pword, nnword=hist.nnword, ppword=hist.ppword) # fill dict that contains matrices with dim num_tagsXnum_features, it will be used to speed up operations if hist_to_all_tag_feature_matrix_dict.get(key_all_tag_hist, None) is None: sparse_res = csr_matrix(cur_feature_vecs) # sparse_mem = sparse_res.data.nbytes + sparse_res.indptr.nbytes + sparse_res.indices.nbytes hist_to_all_tag_feature_matrix_dict[key_all_tag_hist] = sparse_res q.put((hist_to_all_tag_feature_matrix_dict, hist_to_feature_vec_dict))
def calc_normalization_term(v, sentence_history_list, hist_to_all_tag_feature_matrix_dict): norm = 0. for sentence in sentence_history_list: for hist in sentence: new_hist_key = History(cword=hist.cword, pptag=hist.pptag, ptag=hist.ptag, ctag=None, nword=hist.nword, pword=hist.pword, nnword=hist.nnword, ppword=hist.ppword) mat = hist_to_all_tag_feature_matrix_dict[new_hist_key] norm += np.log(np.sum(np.exp(mat @ v))) return norm
def calc_expected_counts(history_sentence_list, v, hist_to_all_tag_feature_matrix_dict): expected_counts = np.zeros(v.shape, np.float64) for sentence in history_sentence_list: for hist in sentence: new_hist_key = History(cword=hist.cword, pptag=hist.pptag, ptag=hist.ptag, ctag=None, nword=hist.nword, pword=hist.pword, nnword=hist.nnword, ppword=hist.ppword) mat = hist_to_all_tag_feature_matrix_dict[new_hist_key] exp_mat = np.exp(mat @ v) prob_mat = exp_mat / np.sum(exp_mat) expected_counts += prob_mat * mat return expected_counts
def fill_tagged_ordered_history_list(file_path, is_test=False): with open(file_path) as f: hist_sentence_list = [] for idx, line in enumerate(f): # splited_words = split(' |,\n', line[:-1]) if line[-1] == '\n' else split(' |,\n', line) # remove \n from last part of sentence if not is_test: splited_words = split(' |,\n', line[:-1]) del splited_words[-1] else: splited_words = split(' |,\n', line[:-1]) if line[-1] == '\n' else split(' |,\n', line) new_sentence_hist_list = [] for word_idx in range(len(splited_words)): cword, ctag = split('_', splited_words[word_idx]) # check if first in sentence if word_idx == 0: pword = WordAndTagConstants.PWORD_SENTENCE_BEGINNING ptag = WordAndTagConstants.PTAG_SENTENCE_BEGINNING pptag = WordAndTagConstants.PPTAG_SENTENCE_BEGINNING ppword = WordAndTagConstants.PPWORD_SENTENCE_BEGINNING else: prev_hist_idx = word_idx - 1 pword = new_sentence_hist_list[prev_hist_idx].cword ptag = new_sentence_hist_list[prev_hist_idx].ctag pptag = new_sentence_hist_list[prev_hist_idx].ptag ppword = new_sentence_hist_list[prev_hist_idx].pword # check if last in sentence if word_idx + 2 < len(splited_words): nword, _ = split('_', splited_words[word_idx+1]) nnword, _ = split('_', splited_words[word_idx+2]) elif word_idx + 1 < len(splited_words): nword, _ = split('_', splited_words[word_idx+1]) nnword = WordAndTagConstants.NWORD_SENTENCE_END else: nword = WordAndTagConstants.NWORD_SENTENCE_END nnword = WordAndTagConstants.NNWORD_SENTENCE_END cur_hist = History(cword=cword, pptag=pptag, ptag=ptag, ctag=ctag, nword=nword, pword=pword, ppword=ppword, nnword=nnword) new_sentence_hist_list.append(cur_hist) hist_sentence_list.append(new_sentence_hist_list) return hist_sentence_list
def calc_prob_for_hist(self, cur_hist, prev_pi, u): pptag_len = len(prev_pi) cur_pi = [] for pptag in range(pptag_len): if prev_pi[pptag] == 0: cur_pi.append(np.zeros(len(self.tags_list), dtype=np.float64)) continue pptag = self.index_to_tag[pptag] dots = [] tags_set = self.get_possible_tag_set_from_word(cur_hist.cword) if len(tags_set) > 1: for c_tag in self.tags_list[:-2]: if c_tag in tags_set: n_hist = History(cword=cur_hist.cword, pptag=pptag, ptag=u, nword=cur_hist.nword, pword=cur_hist.pword, ctag=c_tag, nnword=cur_hist.nnword, ppword=cur_hist.ppword) dot_prod = self.v.dot( self.get_feature_from_hist(n_hist)) dots.append(dot_prod) else: dots.append(-np.inf) # exp_arr = np.exp(np.array(dots) - max(dots)) exp_arr = np.exp(np.array(dots)) prob_arr = exp_arr / np.sum(exp_arr) else: prob_arr = np.zeros(shape=(self.pi_tables.shape[1] - 2)) prob_arr[self.tag_to_index[tags_set.pop()]] = 1 prob_arr = np.append(np.append(prob_arr, 0), 0) cur_pi.append(prev_pi[self.tag_to_index[pptag]] * prob_arr) return np.array(cur_pi)
batch_size=64, shuffle=True, num_workers=5, pin_memory=True) testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False, num_workers=5, pin_memory=True) classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') model = Cifar10_ResNet44().cuda() loss_fn = nn.CrossEntropyLoss(reduce=True) optimizer_ft = optim.Adam(model.parameters(), lr=0.01) history = History(model, "cifar10_transform", resume=True) train_model(history, trainloader, testloader, loss_fn, optimizer_ft, num_epochs=30)
def preprocessing(project_directory, var_matrix_path, full_matrix_path, flag_file_path, **kwargs): """ Groups variant sites into amplicon windows and filters out any amplicons that do not have well conserved upstream and downstream primer regions """ logger = logging.getLogger(__name__) logger.info("BEGIN Preprocessing") args = _set_parameters(**kwargs) start_time = time.time() history = History(project_directory.make_new_file("history", "preprocessing_history"), "Preprocessing", project_directory.timestamp, param_dict=args) # Get strains and sites from matrix strains = _get_strains_from_file(var_matrix_path, args["sep"]) sites = _get_sites_from_file(var_matrix_path, args["sep"]) # Remove excluded strains if args["exclude_strains"] is not None: strains = _remove_strains(args["exclude_strains"], strains) var_matrix = _parse_var_matrix(var_matrix_path, strains, args["sep"]) if args["strict"]: n_sites_before = len(sites) var_matrix, sites = _remove_ambiguous_sites(var_matrix, sites) logger.info("Strict Mode: {} sites with ambiguous " "or missing data were removed".format(n_sites_before - len(sites))) history.add_path("VARIANT SITE MATRIX FILE", var_matrix_path) history.add_parameter("Number of Sites", len(sites)) history.add_parameter("Number of Strains", len(strains)) _check_inputs(args["pz_size"], args["pz_filter_length"], args["strain_cutoff"], len(strains)) if full_matrix_path is not None: flag_df = get_flags_from_matrix(full_matrix_path, strains, history, project_directory, **args) if flag_file_path is not None: flag_df = get_flags_from_file(flag_file_path, history) flag_dic = _get_flags_from_counts(flag_df, args["strain_cutoff"]) amplicon_filter = AmpliconFilter(sites, var_matrix, flag_dic, args['window'], args['pz_size'], args['pz_filter_length'], args['pz_filter_percent'], args['strict']) patterns = amplicon_filter.filter_amplicons_get_patterns() # Write patterns to a json file pattern_json_file = project_directory.make_new_file( "patterns", "patterns", "json") patterns.to_json(pattern_json_file, list(strains)) history.add_path("PATTERN JSON", pattern_json_file) # Write history logger.info("FINISHED Preprocessing") run_time = time.time() - start_time history.add_other("Run Time", run_time) history.write()
def __tabla_completa(self, datos): # funciones internas def clean_lists(datos): salida = [] for i in datos: if type(i) == float: salida.append("Sin nombre") elif len(i) <1: salida.append("Sin nombre") else: salida.append(i[0]) return salida def clean_tags(datos): salida = [] for i in datos: if type(i) == float: salida.append("Sin tags") else: salida.append("_".join(i)) return salida def fix_columns(datos) : columns = datos.columns for i in columns : datos = datos.rename( columns = {i:"{}".format(i.replace(".", "_"))} ) #En algun momento aparece una columna llamada 'to' que produce syntax error try : datos = datos.rename(columns = {'to': 'to_'}) except : pass return datos tabla = pd.DataFrame() for i in datos: tabla = tabla.append(json_normalize(i), ignore_index = True, sort=True) tabla = tabla.drop(columns =['session.user_agent']) try: tabla = tabla.drop(columns = ['message']) except: pass print("Tickets Extraidos : " + str(len(tabla))) tabla["agent_names"] = clean_lists(tabla['agent_names']) tabla['agent_ids'] = clean_lists(tabla['agent_ids']) tabla['tags'] = clean_tags(tabla['tags']) #Construye un dataframe para history y otro para webpath tabla_h = History(tabla).history tabla_wp = Webpath(tabla).webpath try: tabla_h['tags'] = clean_tags(tabla_h['tags']) except : pass try : tabla_h['new_tags'] = clean_tags(tabla_h['new_tags']) except : pass tabla = tabla.drop(columns=['history','webpath']) tabla['triggered_response'].fillna('False', inplace = True) tabla = tabla.astype({'triggered_response' : 'bool'}) tabla['missed'].fillna('False', inplace = True) tabla = tabla.astype({'missed' : 'bool'}) tabla['triggered'].fillna('False', inplace = True) tabla = tabla.astype({'triggered' : 'bool'}) tabla['unread'].fillna('False', inplace = True) tabla = tabla.astype({'unread' : 'bool'}) # print("Tickets extraidos (history)" ,len(tabla_h['id'].value_counts())) # print("Tickets extraidos (webpath)" ,len(tabla_wp['id'].value_counts())) tabla = fix_columns(tabla) tabla_h = fix_columns(tabla_h) tabla_wp = fix_columns(tabla_wp) tabla = tabla.astype({'response_time_avg' : 'float64'}) tabla = tabla.astype({'response_time_first' : 'float64'}) tabla = tabla.astype({'response_time_max' : 'float64'}) self.history = tabla_h self.webpath = tabla_wp self.tabla = tabla return tabla
experiments = args.name.split(',') labels = args.label.split(',') # construct matplotlib fig fig, (ax_acc, ax_loss) = plt.subplots(1, 2, figsize=(18,7)) # plot axis for i, (exp, label) in enumerate(zip(experiments, labels)): # open file f = open(os.path.join(args.base_path, exp, 'history.json'), 'r') hist = json.load(f) f.close() history = History().from_dict(hist).epoch_history # plot accuracy axis ax_acc.plot(history['acc'], color=('C'+str(i)), linestyle=':') ax_acc.plot(history['val_acc'], label=label, color=('C'+str(i))) # plot loss axis ax_loss.plot(history['loss'], color=('C'+str(i)), linestyle=':') ax_loss.plot(history['val_loss'], label=label, color=('C'+str(i))) # add axis label ax_acc.plot([], color='black', linestyle=':', label='dotted for train accuracy') ax_acc.plot([], color='black', linestyle='-', label='solid for validation accuracy') ax_acc.set_xlabel('epoch') ax_acc.set_ylabel('accuracy')
class MainWindow(QtGui.QMainWindow): """ Main Window operates as a common parent of all objects, over which they can communicate. """ def __init__(self): QtGui.QMainWindow.__init__(self) self.logfile = open(BASEPATH + "/log.txt", "w") self.logfile.write("") self.logfile.close() self.logfile = open(BASEPATH + "/log.txt", "a") self.addons = [] # load the config self.config = Config(CONFIGPATH) if not os.path.exists(CONFIGPATH): self.initializeConfig() self.config.loadData() self.history = History(100) self.translations = translations.Translations(self.config["language"]) self.project = widgets.ProjectExplorer(self, "") self.projectPath = "" self.baseModClass = None self.guiClass = None self.initUI() self.initializeAddons() def initUI(self): self.ui = uic.loadUi(BASEPATH + "/ui/MainWindow.ui", self) self.editor = widgets.Editor(self) self.console = widgets.Console(self) # self.console.streamToConsole(sys.stdout) self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.project) self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.editor) self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.console) self.newProjectMenubar = QtGui.QAction( QtGui.QIcon(BASEPATH + "/assets/icons/newProject.png"), self.translations.getTranslation("newProject"), self ) self.openProjectMenubar = QtGui.QAction(self.translations.getTranslation("open"), self) self.openProjectMenubar.setShortcut("Ctrl+O") self.saveProjectMenubar = QtGui.QAction(self.translations.getTranslation("save"), self) self.saveProjectMenubar.setShortcut("Ctrl+S") self.exportProjectMenubar = QtGui.QAction( QtGui.QIcon(BASEPATH + "/assets/icons/export.png"), self.translations.getTranslation("exportProject"), self ) self.exportProjectMenubar.setShortcut("Ctrl+E") self.exportJarMenubar = QtGui.QAction(self.translations.getTranslation("exportJar"), self) self.exportJarMenubar.setShortcut("Ctrl+Shift+E") self.runClientMenubar = QtGui.QAction(self.translations.getTranslation("runClient"), self) self.runClientMenubar.setShortcut("F5") self.undoMenubar = QtGui.QAction(self.translations.getTranslation("undo"), self) self.undoMenubar.setShortcut("Ctrl+Z") self.redoMenubar = QtGui.QAction(self.translations.getTranslation("redo"), self) self.redoMenubar.setShortcut("Ctrl+Y") self.addonsMenubar = QtGui.QAction(self.translations.getTranslation("addons"), self) self.delMenubar = QtGui.QAction(self.translations.getTranslation("delete"), self) self.delMenubar.setShortcut("Del") self.menubar = self.menuBar() self.fileMenubar = self.menubar.addMenu(self.translations.getTranslation("file")) self.editMenubar = self.menubar.addMenu(self.translations.getTranslation("edit")) self.newMenubar = self.menubar.addMenu(self.translations.getTranslation("new")) self.runMenubar = self.menubar.addMenu(self.translations.getTranslation("run")) self.optionMenubar = self.menubar.addMenu(self.translations.getTranslation("options")) self.projectToolbar = self.addToolBar("Project") self.runToolbar = self.addToolBar("Run") self.fileMenubar.addAction(self.newProjectMenubar) self.fileMenubar.addAction(self.openProjectMenubar) self.fileMenubar.addAction(self.saveProjectMenubar) self.fileMenubar.addAction(self.exportProjectMenubar) self.fileMenubar.addAction(self.exportJarMenubar) self.editMenubar.addAction(self.undoMenubar) self.editMenubar.addAction(self.redoMenubar) self.editMenubar.addAction(self.delMenubar) self.runMenubar.addAction(self.runClientMenubar) self.optionMenubar.addAction(self.addonsMenubar) self.projectToolbar.addAction(self.newProjectMenubar) self.projectToolbar.addAction(self.exportProjectMenubar) self.runToolbar.addAction(self.runClientMenubar) self.connect(self.newProjectMenubar, QtCore.SIGNAL("triggered()"), self.createNewProject) self.connect(self.openProjectMenubar, QtCore.SIGNAL("triggered()"), self.openProject) self.connect(self.saveProjectMenubar, QtCore.SIGNAL("triggered()"), self.save) self.connect(self.exportProjectMenubar, QtCore.SIGNAL("triggered()"), self.exportProject) self.connect(self.exportJarMenubar, QtCore.SIGNAL("triggered()"), self.exportJar) self.connect(self.undoMenubar, QtCore.SIGNAL("triggered()"), self.undo) self.connect(self.redoMenubar, QtCore.SIGNAL("triggered()"), self.redo) self.connect(self.runClientMenubar, QtCore.SIGNAL("triggered()"), self.runClient) self.connect(self.addonsMenubar, QtCore.SIGNAL("triggered()"), self.openAddonDialog) self.connect(self.delMenubar, QtCore.SIGNAL("triggered()"), self.delete) self.setCentralWidget(None) self.setDockNestingEnabled(True) def initializeConfig(self): """ Write the most rudamentary config to disk. """ self.config["language"] = "English" self.config["addons"] = [ BASEPATH + "/addons/BaseMod/BaseMod.py", BASEPATH + "/addons/Block/Block.py", BASEPATH + "/addons/Item/Item.py", BASEPATH + "/addons/GUI/CraftingTable.py", ] self.config.saveData() def initializeAddons(self): """ Load all the addons from the paths found in the config. """ self.addons = [] for path in self.config["addons"]: for f in getPythonFiles(path): name = f.split("/")[-1].split(".")[0] mod = imp.load_source(name, f) if "init" in dir(mod): self.addons.append((name, path, mod)) for name, path, mod in self.addons: if "init" in dir(mod): mod.init(self) print("Initialized " + name) if name == "BaseMod": self.baseModClass = mod elif name == "GUI": self.guiClass = mod def openAddonDialog(self): widgets.Addons(self) @dec.accepts(_base, str) def updateName(self, obj, name): """ MainWindow.updateName(classes._base, str) Update the name of a Minecraft object. Args: obj (classes._base): The object to rename name (str): The new name of the object """ self.editor.renameTab(obj, name) self.project.renameObject(obj, name) obj.name = name @dec.accepts(_base) def addObject(self, obj): """ MainWindow.addObject(classes._base) Add a new Minecraft object to the project. Args: obj (classes._base): The object to add """ self.project.addObject(obj) self.editor.openTab(obj) self.editor.tabWidget.setCurrentWidget(obj) def delete(self): """ Delete the currently selected object, if the 'deleteable' flag is set to true. """ selected = self.project.selectedObject() if selected.deleteable: self.project.removeObject(selected) self.editor.closeTab(self.editor.tabWidget.indexOf(selected)) def runClient(self): """ Run Minecraft with the current Mod loaded. """ self.save() self.console.clear() self.exportProject() path = self.projectPath gradlew.runClient(path, self.console) def createNewProject(self): """ Create a new project and set up the forge modding environment. """ self.console.clear() projectpath = BASEPATH + "/Projects" if os.path.exists(BASEPATH + "/Projects") else "C:/" path = str( QtGui.QFileDialog.getExistingDirectory( None, "Select a folder:", projectpath, QtGui.QFileDialog.ShowDirsOnly ) ) if path != "": name, ok = QtGui.QInputDialog.getText( self, self.translations.getTranslation("newProject"), self.translations.getTranslation("name") ) name = str(name) if ok: self.projectPath = path + "/" + name self.project.name = name # build the file structure os.mkdir(self.projectPath) # file to mark the folder as a project f = open(self.projectPath + "/mcmodderproject", "w") f.write(VERSION) f.close() # install forge modloader gradlew.installForge(self.projectPath, self.console) # generate a BaseMod instance for n, p, addon in self.addons: if "onProjectCreated" in dir(addon): addon.onProjectCreated(self) self.save() def openProject(self): """ Open a project from disk. """ projectpath = BASEPATH + "/Projects" if os.path.exists(BASEPATH + "/Projects") else "C:/" path = str( QtGui.QFileDialog.getOpenFileName( self, self.translations.getTranslation("openProject"), projectpath, "JSON-Files" + " (*.json)" ) ) if not path == "": self.projectPath = "/".join(path.replace("\\", "/").split("/")[:-1]) self.project.load(path) def exportProject(self): """ Export the current project's java source code. """ self.save() # clear the current project path = self.projectPath + "/src/main" if os.path.exists(path): shutil.rmtree(path) os.makedirs(path + "/java") os.makedirs(path + "/resources") # export the newly compiled source code for t in self.project.objects.keys(): for cls in self.project.objects[t]: cls.completeModData() for t in self.project.objects.keys(): for cls in self.project.objects[t]: cls.export() def exportJar(self): """ Export the current project as a .jar file. """ path = str( QtGui.QFileDialog.getExistingDirectory( None, self.translations.getTranslation("destinationSelection"), self.projectPath + "/java/build/libs", QtGui.QFileDialog.ShowDirsOnly, ) ) if path != "": buildGradle = open(self.projectPath + "/java/build.gradle", "w") gradleSrc = source.SrcBuildGradle.main gradleSrc = gradleSrc.replace("<version>", self.project.objects["BaseMod"][0].version) gradleSrc = gradleSrc.replace("<mainPackage>", self.project.objects["BaseMod"][0].package()) gradleSrc = gradleSrc.replace("<modname>", self.project.objects["BaseMod"][0].name) buildGradle.write(gradleSrc) buildGradle.close() self.exportProject() gradlew.exportMod(self.projectPath, self.console) if ( self.projectPath + "/java/build/libs/" + self.project.objects["BaseMod"][0].modid() + "-" + self.project.objects["BaseMod"][0].version + ".jar" != path + "/" + self.project.objects["BaseMod"][0].modid() + "-" + self.project.objects["BaseMod"][0].version + ".jar" ): shutil.copy2( self.projectPath + "/java/build/libs/" + self.project.objects["BaseMod"][0].modid() + "-" + self.project.objects["BaseMod"][0].version + ".jar", path + "/" + self.project.objects["BaseMod"][0].modid() + "-" + self.project.objects["BaseMod"][0].version + ".jar", ) os.remove( self.projectPath + "/java/build/libs/" + self.project.objects["BaseMod"][0].modid() + "-" + self.project.objects["BaseMod"][0].version + ".jar" ) def save(self): """ Save the current project to disk after creating a backup of the current savefile. """ if os.path.exists(self.projectPath + "/moddata.json"): shutil.copy2(self.projectPath + "/moddata.json", self.projectPath + "/moddata.backup") f = open(self.projectPath + "/moddata.json", "w") data = self.project.save() data["Version"] = VERSION json.dump(data, f, indent=4, separators=(",", ": ")) f.close() self.console.write("Saved Mod to " + self.projectPath) self.project.unsavedChanges = False def undo(self): self.history.undo() def redo(self): self.history.redo()
from torchsummary import summary model = inception_v3(aux_logits=False) inchannel = model.fc.in_features print(inchannel) model.fc = nn.Linear(inchannel, 5) model = model.cuda() summary(model, (3, 300, 300)) loss_fn = nn.CrossEntropyLoss(reduce=True) optimizer_ft = optim.Adam(model.parameters(), lr=0.01) transform = transforms.Compose( [transforms.Resize((300, 300)), transforms.ToTensor(), transforms.Normalize((0.64639061, 0.56044774, 0.61909978), (0.1491973, 0.17535066, 0.12751725))]) train_data = datasets.ImageFolder( "/hdd/yejiandong/datasets/overlap-0.25-dataset/wsi_train", transform=transform) train_loader = DataLoader(train_data, batch_size=64, shuffle=True, pin_memory=True, num_workers=10) valid_data = datasets.ImageFolder( "/hdd/yejiandong/datasets/overlap-0.25-dataset/wsi_valid", transform=transform) valid_loader = DataLoader(valid_data, batch_size=64, shuffle=False, pin_memory=True, num_workers=10) history = History(model, "inception", resume=False) train_model(history, train_loader, valid_loader, loss_fn, optimizer_ft, num_epochs=30)
class MainWindow(QtGui.QMainWindow): """ Main Window operates as a common parent of all objects, over which they can communicate. """ def __init__(self): QtGui.QMainWindow.__init__(self) self.logfile = open(BASEPATH+"/log.txt", "w") self.logfile.write("") self.logfile.close() self.logfile = open(BASEPATH+"/log.txt", "a") self.addons = [] #load the config self.config = Config(CONFIGPATH) if not os.path.exists(CONFIGPATH): self.initializeConfig() self.config.loadData() self.history = History(100) self.translations = translations.Translations(self.config["language"]) self.project = widgets.ProjectExplorer(self, "") self.projectPath = "" self.baseModClass = None self.guiClass = None self.initUI() self.initializeAddons() def initUI(self): self.ui = uic.loadUi(BASEPATH+"/ui/MainWindow.ui", self) self.editor = widgets.Editor(self) self.console = widgets.Console(self) # self.console.streamToConsole(sys.stdout) self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.project) self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.editor) self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.console) self.newProjectMenubar = QtGui.QAction(QtGui.QIcon(BASEPATH+"/assets/icons/newProject.png"), self.translations.getTranslation("newProject"), self) self.openProjectMenubar = QtGui.QAction(self.translations.getTranslation("open"), self) self.openProjectMenubar.setShortcut("Ctrl+O") self.saveProjectMenubar = QtGui.QAction(self.translations.getTranslation("save"), self) self.saveProjectMenubar.setShortcut("Ctrl+S") self.exportProjectMenubar = QtGui.QAction(QtGui.QIcon(BASEPATH+"/assets/icons/export.png"), self.translations.getTranslation("exportProject"), self) self.exportProjectMenubar.setShortcut("Ctrl+E") self.exportJarMenubar = QtGui.QAction(self.translations.getTranslation("exportJar"), self) self.exportJarMenubar.setShortcut("Ctrl+Shift+E") self.runClientMenubar = QtGui.QAction(self.translations.getTranslation("runClient"), self) self.runClientMenubar.setShortcut("F5") self.undoMenubar = QtGui.QAction(self.translations.getTranslation("undo"), self) self.undoMenubar.setShortcut("Ctrl+Z") self.redoMenubar = QtGui.QAction(self.translations.getTranslation("redo"), self) self.redoMenubar.setShortcut("Ctrl+Y") self.addonsMenubar = QtGui.QAction(self.translations.getTranslation("addons"), self) self.delMenubar = QtGui.QAction(self.translations.getTranslation("delete"), self) self.delMenubar.setShortcut("Del") self.menubar = self.menuBar() self.fileMenubar = self.menubar.addMenu(self.translations.getTranslation("file")) self.editMenubar = self.menubar.addMenu(self.translations.getTranslation("edit")) self.newMenubar = self.menubar.addMenu(self.translations.getTranslation("new")) self.runMenubar = self.menubar.addMenu(self.translations.getTranslation("run")) self.optionMenubar = self.menubar.addMenu(self.translations.getTranslation("options")) self.projectToolbar = self.addToolBar("Project") self.runToolbar = self.addToolBar("Run") self.fileMenubar.addAction(self.newProjectMenubar) self.fileMenubar.addAction(self.openProjectMenubar) self.fileMenubar.addAction(self.saveProjectMenubar) self.fileMenubar.addAction(self.exportProjectMenubar) self.fileMenubar.addAction(self.exportJarMenubar) self.editMenubar.addAction(self.undoMenubar) self.editMenubar.addAction(self.redoMenubar) self.editMenubar.addAction(self.delMenubar) self.runMenubar.addAction(self.runClientMenubar) self.optionMenubar.addAction(self.addonsMenubar) self.projectToolbar.addAction(self.newProjectMenubar) self.projectToolbar.addAction(self.exportProjectMenubar) self.runToolbar.addAction(self.runClientMenubar) self.connect(self.newProjectMenubar, QtCore.SIGNAL('triggered()'), self.createNewProject) self.connect(self.openProjectMenubar, QtCore.SIGNAL('triggered()'), self.openProject) self.connect(self.saveProjectMenubar, QtCore.SIGNAL('triggered()'), self.save) self.connect(self.exportProjectMenubar, QtCore.SIGNAL('triggered()'), self.exportProject) self.connect(self.exportJarMenubar, QtCore.SIGNAL('triggered()'), self.exportJar) self.connect(self.undoMenubar, QtCore.SIGNAL('triggered()'), self.undo) self.connect(self.redoMenubar, QtCore.SIGNAL('triggered()'), self.redo) self.connect(self.runClientMenubar, QtCore.SIGNAL('triggered()'), self.runClient) self.connect(self.addonsMenubar, QtCore.SIGNAL('triggered()'), self.openAddonDialog) self.connect(self.delMenubar, QtCore.SIGNAL('triggered()'), self.delete) self.setCentralWidget(None) self.setDockNestingEnabled(True) def initializeConfig(self): """ Write the most rudamentary config to disk. """ self.config["language"] = "English" self.config["addons"] = [BASEPATH+"/addons/BaseMod/BaseMod.py", BASEPATH+"/addons/Block/Block.py", BASEPATH+"/addons/Item/Item.py", BASEPATH+"/addons/GUI/CraftingTable.py"] self.config.saveData() def initializeAddons(self): """ Load all the addons from the paths found in the config. """ self.addons = [] for path in self.config["addons"]: for f in getPythonFiles(path): name = f.split("/")[-1].split(".")[0] mod = imp.load_source(name, f) if "init" in dir(mod): self.addons.append((name, path, mod)) for name, path, mod in self.addons: if "init" in dir(mod): mod.init(self) print("Initialized "+name) if name == "BaseMod": self.baseModClass = mod elif name == "GUI": self.guiClass = mod def openAddonDialog(self): widgets.Addons(self) @dec.accepts(_base, str) def updateName(self, obj, name): """ MainWindow.updateName(classes._base, str) Update the name of a Minecraft object. Args: obj (classes._base): The object to rename name (str): The new name of the object """ self.editor.renameTab(obj, name) self.project.renameObject(obj, name) obj.name = name @dec.accepts(_base) def addObject(self, obj): """ MainWindow.addObject(classes._base) Add a new Minecraft object to the project. Args: obj (classes._base): The object to add """ self.project.addObject(obj) self.editor.openTab(obj) self.editor.tabWidget.setCurrentWidget(obj) def delete(self): """ Delete the currently selected object, if the 'deleteable' flag is set to true. """ selected = self.project.selectedObject() if selected.deleteable: self.project.removeObject(selected) self.editor.closeTab(self.editor.tabWidget.indexOf(selected)) def runClient(self): """ Run Minecraft with the current Mod loaded. """ self.save() self.console.clear() self.exportProject() path = self.projectPath gradlew.runClient(path, self.console) def createNewProject(self): """ Create a new project and set up the forge modding environment. """ self.console.clear() projectpath = BASEPATH+"/Projects" if os.path.exists(BASEPATH+"/Projects") else "C:/" path = str(QtGui.QFileDialog.getExistingDirectory(None, 'Select a folder:', projectpath, QtGui.QFileDialog.ShowDirsOnly)) if path != "": name, ok = QtGui.QInputDialog.getText(self, self.translations.getTranslation("newProject"), self.translations.getTranslation("name")) name = str(name) if ok: self.projectPath = path+"/"+name self.project.name = name #build the file structure os.mkdir(self.projectPath) #file to mark the folder as a project f = open(self.projectPath+"/mcmodderproject", "w") f.write(VERSION) f.close() #install forge modloader gradlew.installForge(self.projectPath, self.console) #generate a BaseMod instance for n, p, addon in self.addons: if "onProjectCreated" in dir(addon): addon.onProjectCreated(self) self.save() def openProject(self): """ Open a project from disk. """ projectpath = BASEPATH+"/Projects" if os.path.exists(BASEPATH+"/Projects") else "C:/" path = str(QtGui.QFileDialog.getOpenFileName(self, self.translations.getTranslation("openProject"), projectpath, "JSON-Files"+" (*.json)")) if not path == "": self.projectPath = "/".join(path.replace("\\", "/").split("/")[:-1]) self.project.load(path) def exportProject(self): """ Export the current project's java source code. """ self.save() #clear the current project path = self.projectPath+"/src/main" if os.path.exists(path): shutil.rmtree(path) os.makedirs(path+"/java") os.makedirs(path+"/resources") #export the newly compiled source code for t in self.project.objects.keys(): for cls in self.project.objects[t]: cls.completeModData() for t in self.project.objects.keys(): for cls in self.project.objects[t]: cls.export() def exportJar(self): """ Export the current project as a .jar file. """ path = str(QtGui.QFileDialog.getExistingDirectory(None, self.translations.getTranslation("destinationSelection"), self.projectPath+"/java/build/libs", QtGui.QFileDialog.ShowDirsOnly)) if path != "": buildGradle = open(self.projectPath+"/java/build.gradle", "w") gradleSrc = source.SrcBuildGradle.main gradleSrc = gradleSrc.replace("<version>", self.project.objects["BaseMod"][0].version) gradleSrc = gradleSrc.replace("<mainPackage>", self.project.objects["BaseMod"][0].package()) gradleSrc = gradleSrc.replace("<modname>", self.project.objects["BaseMod"][0].name) buildGradle.write(gradleSrc) buildGradle.close() self.exportProject() gradlew.exportMod(self.projectPath, self.console) if self.projectPath+"/java/build/libs/"+self.project.objects["BaseMod"][0].modid()+"-"+self.project.objects["BaseMod"][0].version+".jar" != path+"/"+self.project.objects["BaseMod"][0].modid()+"-"+self.project.objects["BaseMod"][0].version+".jar": shutil.copy2(self.projectPath+"/java/build/libs/"+self.project.objects["BaseMod"][0].modid()+"-"+self.project.objects["BaseMod"][0].version+".jar", path+"/"+self.project.objects["BaseMod"][0].modid()+"-"+self.project.objects["BaseMod"][0].version+".jar") os.remove(self.projectPath+"/java/build/libs/"+self.project.objects["BaseMod"][0].modid()+"-"+self.project.objects["BaseMod"][0].version+".jar") def save(self): """ Save the current project to disk after creating a backup of the current savefile. """ if os.path.exists(self.projectPath+"/moddata.json"): shutil.copy2(self.projectPath+"/moddata.json", self.projectPath+"/moddata.backup") f = open(self.projectPath+"/moddata.json", "w") data = self.project.save() data["Version"] = VERSION json.dump(data, f, indent=4, separators=(',', ': ')) f.close() self.console.write("Saved Mod to "+self.projectPath) self.project.unsavedChanges = False def undo(self): self.history.undo() def redo(self): self.history.redo()
def train(trn_loader, model, criterion, optimizer, epoch, logger, sublogger, work_dir=os.path.join(args.work_dir, args.exp)): batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() ious = AverageMeter() dices = AverageMeter() model.train() end = time.time() slice_level_acc = 0 total_data_counts = 0 lr_schedule = args.lr_schedule train_history = History(len(trn_loader.dataset)) length_data = 0 for i, (input, target, idx) in enumerate(trn_loader): data_time.update(time.time() - end) input = input.cuda() target = target.cuda() output = model(input) loss = criterion(output, target) # Segmentation Measure pos_probs = torch.sigmoid(output) pos_preds = (pos_probs > 0.5).float() p = 0 k = 0 correct_per_pixel = [] for dap, predict in zip(target, pos_preds): if dap.max() == 1 and predict.max() == 1: p += 1 k += 1 elif dap.max() == 0 and predict.max() == 0: p += 1 k += 1 else: k += 1 slice_level_acc += p total_data_counts += k iou, dice = performance(output, target) losses.update(loss.item(), input.size(0)) ious.update(iou, input.size(0)) dices.update(dice, input.size(0)) optimizer.zero_grad() loss.backward() optimizer.step() batch_time.update(time.time() - end) end = time.time() pos_probs = torch.sigmoid(output) pos_preds = (pos_probs > 0.5).float() #import ipdb; ipdb.set_trace() print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 'IoU {iou.val:.4f} ({iou.avg:.4f})\t' 'Dice {dice.val:.4f} ({dice.avg:.4f})\t'.format( epoch, i, len(trn_loader), batch_time=batch_time, data_time=data_time, loss=losses, iou=ious, dice=dices)) if i % 10 == 0: sublogger.write([epoch, i, loss.item(), iou, dice]) slice_acc = slice_level_acc / total_data_counts writer_train.add_scalar('Loss', losses.avg, epoch) writer_train.add_scalar('IoU', ious.avg, epoch) writer_train.add_scalar('Dice Score', dices.avg, epoch) writer_train.add_scalar('Slice-Level-Accuracy', slice_level_acc / total_data_counts, epoch) logger.write([epoch, losses.avg, ious.avg, dices.avg, slice_acc])
class MyCalcWindow(Ui_MainWindow, QMainWindow): numbers = {str(i): str(i) for i in range(10)} science_numbers = {'pi': 'π', 'e': 'e'} number_keys = dict(numbers.items() | science_numbers.items()) operators = { "plus": "+", "sub": "-", "mul": "×", "div": "÷", "point": ".", "square": "²", "exp": "exp(", "sqrt": "√(", "log": "log(", "ln": "ln(", "leftpart": "(", "rightpart": ")", "sin": "sin(", "cos": "cos(", "tan": "tan(", "power": "^(", "factorial": "!", } operations = { "DEL": "DEL", "AC": "AC", "equal": "equal", "rand": "rand", "inverse": "inverse", "percent": "percent", } def __init__(self): super(MyCalcWindow, self).__init__() self.setupUi(self) self.setFixedSize(self.width(), self.height()) # lock the window size ################################## self.PRECISION = self.verticalSlider.value() self.history = "" self._history = History(parent=self) self.compute_times = 0 self.lcdNumber.display(self.PRECISION) self.connecter() self.show() self.press_AC() def keyPressEvent(self, event): # 这里event.key()显示的是按键的编码 # print("按下:" + str(event.key())) if QApplication.keyboardModifiers() == Qt.ShiftModifier: if event.key() == Qt.Key_J: print("press J") self._history.next() elif event.key() == Qt.Key_K: print("press K") self._history.last() def update_history(self, equation): self.compute_times += 1 if self.history == "": self.history += "[{}] : {}".format(self.compute_times, equation) else: self.history = "\n[{}] : {}".format(self.compute_times, equation) self.textBrowser.insertPlainText(self.history) self.textBrowser.moveCursor(self.textBrowser.textCursor().End) def valueChanged(self): self.PRECISION = self.verticalSlider.value() self.lcdNumber.display(self.PRECISION) def connect(self, keys_dict, use_func=False, set_shortcut=False): """ Use specific key-value to connect button clicked response :param keys_dict: :param use_func: if True, use `press_value` function """ for button_name, button_text in keys_dict.items(): btn = self.__dict__['pushButton_' + button_name] if use_func: func = eval("self.press_" + button_name) btn.clicked.connect(func) else: btn.clicked.connect(functools.partial(self.press, button_text)) def set_shortcut(self, buttons_dict): for button_name, button_text in buttons_dict.items(): btn = self.__dict__['pushButton_' + button_name] btn.setShortcut(button_name[0]) def connecter(self): # 1. numbers self.connect(self.number_keys) self.set_shortcut(self.number_keys) # 2. operators self.connect(self.operators) self.pushButton_mul.setShortcut("*") self.pushButton_div.setShortcut("/") self.pushButton_plus.setShortcut("+") self.pushButton_sub.setShortcut("-") self.pushButton_leftpart.setShortcut("(") self.pushButton_rightpart.setShortcut(")") self.pushButton_power.setShortcut("^") self.pushButton_factorial.setShortcut("!") self.pushButton_point.setShortcut(".") self.pushButton_sin.setShortcut("S") self.pushButton_cos.setShortcut("C") self.pushButton_tan.setShortcut("T") self.pushButton_sqrt.setShortcut("shift+^") self.pushButton_square.setShortcut("shift+2") self.pushButton_log.setShortcut("l") self.pushButton_ln.setShortcut("n") self.pushButton_rand.setShortcut("r") self.pushButton_inverse.setShortcut("shift+/") # 3. operations self.connect(self.operations, use_func=True) self.pushButton_equal.setShortcut("return") self.pushButton_DEL.setShortcut("backspace") self.pushButton_AC.setShortcut("shift+l") # 4. other functions self.verticalSlider.valueChanged.connect(self.valueChanged) def press_inverse(self): self.press_equal(reciprocal=True, percent=False) def press_percent(self): self.press_equal(reciprocal=False, percent=True) def press_rand(self): self.lineEdit.insert(str(random.random())) def press(self, button_key): self.lineEdit.insert(button_key) def press_DEL(self): self.lineEdit.backspace() def press_AC(self): self.lineEdit.setText("") def press_equal(self, reciprocal=False, percent=False): text = self.lineEdit.text() if reciprocal: text = wrapper_inverse(text) try: result, equation = calculate(text, self.PRECISION, percent) self.lineEdit.setText(str(result)) self.update_history(equation) self._history.append(text) except Exception as e: self.lineEdit.setText(str(e))
def pattern_selection(project_directory, **kwargs): logger = logging.getLogger(__name__) logger.info("BEGIN Pattern Selection") args = _set_parameters(**kwargs) start_time = time.time() _check_inputs(args['max_loci'], args['required_loci'], args['exclude_loci']) history = History(project_directory.make_new_file( "history", "pattern_selection_history"), "Pattern_Selection", project_directory.timestamp, param_dict=args) preprocessing_history = History( project_directory.get_parent_subdirectory_file( "history", "preprocessing_history_{}.txt".format( project_directory.get_parent_directory_timestamp())), "Preprocessing", exists=True) # Get JSON file path from preprocessing step json_file = preprocessing_history.get_path("PATTERN JSON") variant_matrix = preprocessing_history.get_path("VARIANT SITE MATRIX FILE") sep = { 'comma': ",", "space": " ", "tab": "\t" }[preprocessing_history.get_parameter("SEP")] # Get flag file path from preprocessing step flag_file = preprocessing_history.get_path("PRIMER ZONE FLAGS") primer_zone_size = preprocessing_history.get_parameter("PZ_SIZE") history.add_path("PATTERN JSON", json_file) logger.info("Reading from pattern JSON: %s", json_file) # Read in pattern JSON patterns = Patterns() patterns.load_patterns(json_file) if len(args['exclude_loci']): patterns.remove_sites(args['exclude_loci']) if len(args['required_loci']): patterns.add_required_sites(args['required_loci']) if len(args['exclude_strains']): patterns.remove_strains(args['exclude_strains']) patterns.set_resolution(args['res'], args['stop_at_res']) best_set = _get_minimum_spanning_set( patterns, args['reps'], args['max_loci'], args['max_res'], args['n_threads'], int(preprocessing_history.get_parameter("PZ_SIZE"))) haplotype_file = project_directory.make_new_file("minimum_spanning_set", ".haplotype", "csv") amplicon_json = project_directory.make_new_file("minimum_spanning_set", ".amplicons", "json") haplotype_matrix = project_directory.make_new_file("minimum_spanning_set", "haplotypes", "csv") amplicon_matrix = project_directory.make_new_file("minimum_spanning_set", "amplicons", "csv") pattern_matrix = project_directory.make_new_file("minimum_spanning_set", "patterns", "csv") summary_file = project_directory.make_new_file("summary", "summary") haplotype = Haplotype(patterns, best_set, flag_file, primer_zone_size, variant_matrix, sep) haplotype.write_haplotype(haplotype_file) history.add_path("Haplotype File", haplotype_file) haplotype.write_json(amplicon_json) history.add_path("Amplicon JSON", amplicon_json) haplotype.write_summary(summary_file) history.add_path("Summary", summary_file) haplotype.write_output(haplotype_matrix, pattern_matrix, amplicon_matrix) history.add_path("Haplotype Matrix", haplotype_matrix) history.add_path("Amplicon Matrix", amplicon_matrix) history.add_path("Pattern Matrix", pattern_matrix) logger.info("FINISHED Pattern Selection") run_time = time.time() - start_time history.add_other("Run Time", run_time) history.write()