async def HealthCheck(configFilePath: str, checkInterval: int, refreshInterval: int, cache: Dict[str, CheckResult]): folderPath = Utils.PrepareLocalAppdata() htmlFilePath = os.path.join(folderPath, "index.html") htmlWriter = HtmlWriter(htmlFilePath, refreshInterval) while True: try: # load config config = ConfigReader().Read(configFilePath) # load extensions extensions = Utils.LoadExtensions() # check health healthChecker = HealthChecker(config, extensions, cache) result = await healthChecker.CheckHealthAsync() # throttle notifications filteredResult = Utils.ThrottleNotifications(result) # notify notifyManager = NotifyManager(config, extensions) await notifyManager.NotifyAsync(filteredResult) # update html htmlWriter.WriteResult(result) except Exception as ex: print(ex) await asyncio.sleep(checkInterval)
def plot2dataframe(self, xaxes=Utils.X, yaxes=Utils.Y): set_white_chart() path = Utils.BUILD_CHART2D_PATH(self.dataset_name, self.info[Utils.NAME], self.info[Utils.SURNAME], self.info[Utils.WORD_NUMBER], self.info[Utils.HANDWRITING], self.label) chrono = chronometer.Chrono("Plotting 2D Chart for: {}...".format(self.title)) if os.path.isfile(path): chrono.end("already exixst") return ax = None colors = itertools.cycle(plt.rcParams['axes.prop_cycle']) for i, component in enumerate(g for _, g in self.word_dataframe.groupby(Utils.COMPONENT)): ax = component[["x", "y", Utils.TIME]].plot(x=xaxes, y=yaxes, kind="scatter", c=next(colors)['color'], ax=ax if ax else None) ax.set_xlim(0, self.width) ax.set_ylim(0, self.height) ax.set_xticklabels([]) ax.set_yticklabels([]) ax.xaxis.label.set_visible(False) ax.yaxis.label.set_visible(False) # plt.title(self.title) plt.axes().set_aspect('equal') plt.axes().invert_yaxis() Utils.mkdir(Utils.BUILD_CHART2D_FOLDER_PATH(self.dataset_name)) plt.savefig(path, dpi=400) chrono.end()
def inject_segmented_spectrogram(x, fs): """Inserts a cough-segmented spectrogram. Args: signal (np.array): audio as a 1-D numpy array fs (int): sample rate """ max_signal = np.max(np.abs(x)) normalized_audio = Utils.normalize_audio(x, fs, shouldTrim=False) cough_segments, cough_mask = Utils.segment_cough(normalized_audio, fs) if np.max(cough_mask) == 0: st.error('We did not detect strong coughs to segment.') cough_mask = cough_mask * max_signal x_values = np.arange(len(normalized_audio)) / fs with _lock: seg_fig = plt.figure(figsize=(10, 3)) ax = seg_fig.add_subplot(1, 1, 1) ax.plot(x_values, x) ax.plot(x_values, cough_mask) ax.set_xlabel("Time (s) ") ax.set_ylabel("Amplitude") st.pyplot(seg_fig)
def encrypt(message): encrypted_message = message for c in message: if c in Utils.keys(): encrypted_message = encrypted_message.replace( c, Utils.keys().get(c)) return encrypted_message
def plot3dataframe(self, scaling_rates=None): set_white_chart() chrono = chronometer.Chrono("Plotting 3D Charts for: {}...".format(self.title)) maxv = max(self.word_dataframe[Utils.TIME]) if not scaling_rates: scaling_rates = range(0, maxv + 1, 50) wrote_something = False for scaling in scaling_rates: path = Utils.BUILD_CHART3D_PATH(self.dataset_name, self.info[Utils.NAME], self.info[Utils.SURNAME], self.info[Utils.WORD_NUMBER], self.info[Utils.HANDWRITING], scaling, self.label) if os.path.isfile(path): continue wrote_something = True fig = plt.figure() ax = fig.add_subplot(111, projection='3d') colors = itertools.cycle(plt.rcParams['axes.prop_cycle']) for i, component in enumerate(g for _, g in self.word_dataframe.groupby(Utils.COMPONENT)): x = component[Utils.X] y = component[Utils.Y] z = component[Utils.TIME] / maxv * scaling ax.scatter(y, x, z, c=next(colors)['color']) ax.w_xaxis.set_pane_color((1, 1, 1, 0)) ax.w_yaxis.set_pane_color((1, 1, 1, 0)) ax.w_zaxis.set_pane_color((1, 1, 1, 0)) ax.set_xticklabels([]) ax.set_yticklabels([]) ax.set_zticklabels([]) # ax.xaxis.set_ticks_position('none') # tick markers # ax.yaxis.set_ticks_position('none') # plt.title(self.title) ax.set_xlim(0, self.height) ax.set_ylim(0, self.width) ax.set_zlim(0, maxv) ax.set_zlabel('\ntime', linespacing=-4) # ChartCreator.set_axes_equal(ax) Utils.mkdir(Utils.BUILD_CHART3D_FOLDER_PATH(self.dataset_name, self.info[Utils.NAME], self.info[Utils.SURNAME], self.info[Utils.WORD_NUMBER], self.info[Utils.HANDWRITING], self.label )) plt.savefig(path, dpi=400, bbox_inches='tight') plt.close(fig) if wrote_something: chrono.end() else: chrono.end("already exixst")
def remove_possibilities_same_square(list_of_possibilities, matrix, size, i, j): for l in Utils.coordinate_list_for_square(i, size): for c in Utils.coordinate_list_for_square(j, size): if matrix[l][c] != 0: try: list_of_possibilities.remove(matrix[l][c]) except: pass
def __init__(self): project_dir = dirname(dirname(dirname(dirname(abspath(__file__))))) Utils.set_keras_backend("theano") import keras model_path = os.path.join(project_dir, 'src/learn/dev_kar/model.h5') self.model = keras.models.load_model(model_path) mean_var_path = os.path.join(project_dir, 'src/learn/dev_kar/mean_var.txt') with open(mean_var_path, 'r') as f: lines = f.readlines() self.mean = float(lines[0]) self.std = float(lines[1])
def __init__(self, dataset_name, dataframe, wordid_userid_dataframe, user_data_dataframe, word_id=None, name=None, surname=None, handwriting=None, word_number=None, label=Utils.MOVEMENT_POINTS): self.word_dataframe, word_id = get_word_data(dataframe[label], wordid_userid_dataframe, user_data_dataframe, word_id, name, surname, handwriting, word_number) self.info = Utils.get_infos(wordid_userid_dataframe, user_data_dataframe, word_id) Utils.mkdir(Utils.BUILD_PICS_FOLDER(dataset_name)) self.dataset_name = dataset_name self.dataframe = dataframe self.word_id = word_id self.label = label self.height = self.info[Utils.HEIGHT_PIXELS] self.width = self.info[Utils.WIDTH_PIXELS] self.title = get_title(self.info)
def decrypt(message): decrypted_message = message keys_values = Utils.keys().values() for c in message: if c in keys_values: decrypted_message = decrypted_message.replace(c, Decryption.get_key(c)) return decrypted_message
def get_chain(self, loc: Tuple[int, int]) -> List[Tuple[int, int]]: # if run.py was started, we can use scipy and thereby improve performance if not Utils.in_pyinstaller_mode(): # This method uses morphological operations to find out the # connected components ie., chains. wikipedia link to # morphological operation - https://en.wikipedia.org/wiki/Mathematical_morphology test_matrix = self == self[loc] label_im, nb_labels = ndimage.label(test_matrix) label_im = label_im == label_im[loc] locations = np.where(label_im) group = list(zip(locations[0],locations[1])) return group # if GTPengine.py was started, via pyinstaller for instance, we can't use scipy # because pyinstaller doesn't seem to be able to handle it player = self[loc] # Check if neighbors of same player to_check = [loc] group = [] while len(to_check) > 0: current = to_check.pop() neighbors = self.get_adjacent_coords(current) for n in neighbors: if self[n] == player and n not in group and n not in to_check: to_check.append(n) group.append(current) return group
def get_word_data(dataframe, wordid_userid, user_data, wordid, name, surname, handwriting, wordnumber): assert bool(wordid) != bool( name and surname and handwriting and wordnumber), "Need a wordid xor a (name, surname, handwriting, word number" if not wordid: wordid = Utils.get_wordidfrom_wordnumber_name_surname(wordid_userid, user_data, name, surname, handwriting, wordnumber) return dataframe.loc[dataframe[Utils.WORD_ID] == wordid], wordid
def do_download(self, args): """Downloads the build based from the entered ftp-directory""" self.set_server_specs() wd = None try: wd = self.cwd_routine() if wd is False or wd is None: raise SystemExit except: pass self.server_specs["WorkingDirectory"] = wd logger.debug("Server specs set") for k, v in self.server_specs.items(): logger.debug("{} : {}".format(k, v)) file_name = "" desktop_path = Utils.get_desktop_path() output_file_dir = os.path.join(desktop_path, "Downloads") logger.info("Downloading application") download = Download(self.server_specs, file_name, output_file_dir) download.download()
def run(self): t = time.time() print('Z:', self.Z) EP_X_ID = utils.envolution(self) dt = time.time() - t print("PE size:%s,used time:%s s" % (len(EP_X_ID), dt)) print('Z:', self.Z) self.show()
def plotCMCs(self, svm_name, rank, cmcvalues, handwriting, pathname): assert isinstance(svm_name, list) labels = ["{} (rr = {:.4f})".format(s, r[1]) for s, r in zip(svm_name, cmcvalues)] self.simplePlot(Utils.BUILD_RESULTS_PATH(self._get_path_hand(IDENTIFICATION, handwriting), handwriting, pathname, "cmc"), rank, cmcvalues, None, labels, None, None, "Rank", "Cms Values", "Cumulative Match Curve - {}".format(Utils.prettify_name(handwriting)), xscale=False, yscale=False, integer_x=True)
def plotFRRvsFPR(self, svm_name, thresholds, frr, fpr, handwriting, balanced, pathname): xaxes = [thresholds, thresholds] yaxes = [frr, fpr] colors = ['darkorange', 'navy'] lws = [2, 2] labels = ["FRR - {}".format(svm_name), "FPR - {}".format(svm_name)] self.simplePlot(Utils.BUILD_RESULTS_PATH(self._get_path_hand(VERIFICATION, handwriting), handwriting, pathname, self.get_desc("frrVSfpr", balanced)), xaxes, yaxes, colors, labels, lws, None, "Thresholds", "Errors Rate", "FRR vs FPR - {}".format(Utils.prettify_name(handwriting)), legendpos="upper center")
def consent(session_state, recording, cough_conf): # Consent st.subheader('Contribute to Research') st.info(""" Your cough recording and extra information provided were used for prediction, but they aren't stored. You can help accelerate research if you contribute your cough and extra information for research purposes. If you are willing to, please check the boxes below.""") consent_cough = st.checkbox('I agree to anonymously donate my cough and extra information provided for research purposes.') if consent_cough and not session_state.cough_donated: if cough_conf > 0.5: with st.spinner('Uploading information ...'): Utils.upload_blob(COUGH_STORAGE_BUCKET, recording, f'user_cough_data/{session_state.cough_uuid}.wav') st.success('Successfully uploaded!') session_state.cough_donated = True session_state.symptoms_donated = True
def can_clone(self, name): if (Utils.is_local(name)): repo_path = os.path.join(os.getcwd(), name) is_dirty = git.Repo(repo_path).is_dirty() print('[*] found {0} locally, skipping! - working = {1}'.format(name, is_dirty)) return False else: print('[*] project: {0}, will be downloaded'.format(name)) return True
def solve_one_matrix(matrix): logger = logging.getLogger("sudoku_solver") size = len(matrix[0]) score = size * size while not Utils.check_matrix_is_finished(matrix, size): matrix = Normal.run_1_time(matrix, size) current_score = Utils.calculate_matrix_score(matrix, size) if score == current_score: logger.warning( "Using hypothesis, Sudoku is still blocked with %i empty cases", score) break score = current_score return matrix, score
def summarize_problems(): _, outfiles = load_files("data/raw") problems = [Utils.open_sp(filename) for filename in outfiles] labels = ["Problem name", "Number of vehicles", "Max capacity", "Customers"] labels += ["total demand", "Minimum vehicles to satisfy capacity"] lines = [make_line(p) for p in problems] return (labels, lines)
def plotRoc(self, svm_name, fpr, tpr, auc_score, handwriting, balanced, pathname): xaxes = [fpr] + [[0,1]] yaxes = [tpr] + [[0,1]] colors = ['darkorange', 'navy'] labels = ["{} (area = {:.4f})".format(svm_name, auc_score), None] linestyles = [None, "--"] self.simplePlot(Utils.BUILD_RESULTS_PATH(self._get_path_hand(VERIFICATION, handwriting), handwriting, pathname, self.get_desc("roc", balanced)), xaxes, yaxes, colors, labels, None, linestyles, "False Positive Rate", "True Positive Rate", "Receiver Operating Characteristic - {}".format(Utils.prettify_name(handwriting)))
def plotRocs(self, svm_name, fpr, tpr, auc_score, handwriting, balanced, pathname): assert isinstance(svm_name, list) assert isinstance(auc_score, list) xaxes = fpr + [[0, 1]] yaxes = tpr + [[0, 1]] colors = [None for _ in svm_name] + ["navy"] labels = ["{} (area = {:.4f})".format(svm_name, auc_score) for svm_name, auc_score in zip(svm_name, auc_score)] + [None] linestyles = [None for _ in svm_name] + ["--"] self.simplePlot(Utils.BUILD_RESULTS_PATH(self._get_path_hand(VERIFICATION, handwriting), handwriting, pathname, self.get_desc("roc", balanced)), xaxes, yaxes, colors, labels, None, linestyles, "False Positive Rate", "True Positive Rate", "Receiver Operating Characteristic - {}".format(Utils.prettify_name(handwriting)))
def plotCMC(self, svm_name, rank, cmc_values, handwriting, pathname): xaxes = [rank] yaxes = [cmc_values] colors = ['darkorange'] labels = ["{} (rr = {:.4f})".format(svm_name, cmc_values[1])] self.simplePlot(Utils.BUILD_RESULTS_PATH(self._get_path_hand(IDENTIFICATION, handwriting), handwriting, pathname, "cmc"), xaxes, yaxes, colors, labels, None, None, "Rank", "Cms Values", "Cumulative Match Curve - {}".format(Utils.prettify_name(handwriting)), xscale=False, yscale=False, integer_x=True)
def __init__(self): project_root_dir = dirname(dirname(dirname(dirname( abspath(__file__))))) Utils.set_keras_backend('theano') from keras.models import model_from_json model_dir = os.path.join(project_root_dir, 'src/learn/dev_ben') model_architecture_path = os.path.join(model_dir, 'model_architecture.json') if not os.path.isfile(model_architecture_path): print('model architecture not found: ' + model_architecture_path) sys.exit(1) model_weights_path = os.path.join(model_dir, 'model_weights.h5') if not os.path.isfile(model_weights_path): print('model weights not found: ' + model_weights_path) sys.exit(1) # self.model = keras.models.load_model(model_path) json_file = open(model_architecture_path, 'r') self.model = model_from_json(json_file.read()) json_file.close() self.model.load_weights(model_weights_path)
def give_solution_if_forced_by_other_lines_of_other_squares( matrix, i, j, size, list_of_possibilities): logger = logging.getLogger("sudoku_solver") # Column indices to check to know if line is full indices = Utils.indices_of_others(j) if Utils.are_full_line(matrix, i, j, indices): logger.debug("Line %i is full on column %i from square %i", i, j, j // 3) list_of_other_numbers = [] # Check other square' lines number that are on both lines for z in Utils.indices_of_others(i): line_to_check = (i // 3) * 3 + z logger.debug("Line to check is %i", line_to_check) # We are in other lines of the square & we add for every line all the numbers listed if they are not equal to 0 for x in range(0, size): if matrix[line_to_check][x] != 0: logger.debug("value of %i : %i is %i", line_to_check, x, matrix[line_to_check][x]) list_of_other_numbers.append(matrix[line_to_check][x]) # We have to get every number present on both other lines list_of_number_present_on_both = Utils.get_duplicates_of_a_list( list_of_other_numbers) for n1 in list_of_number_present_on_both: logger.debug("This number is present on both lines : %i ", n1) list_of_new_possibilities = [ x for x in list_of_number_present_on_both if x in list_of_possibilities ] for n1 in list_of_new_possibilities: logger.debug("This number is maybe a new possibility : %i ", n1) if len(list_of_new_possibilities) == 1: list_of_possibilities = list_of_new_possibilities return list_of_possibilities
def main(): validateArgs() msg, key = utils.getMsgAndKey(sys.argv) aes = Aes(key) if len(sys.argv) == 5: print "Loading Sbox Values: " + str(sys.argv[3]) + ", " + str(sys.argv[4]) aes.loadSboxValues(sys.argv[3], sys.argv[4]) cipher = aes.Cipher(msg) msg_dec = aes.EqInvCipher(cipher) displayResults(msg, cipher, msg_dec)
def solve(matrix): logger = logging.getLogger("sudoku_solver") size = len(matrix[0]) while not Utils.check_matrix_is_finished(matrix, size): score = Utils.calculate_matrix_score(matrix, size) matrix = Normal.run_1_time(matrix, size) # Utils.print_matrix(matrix, size) current_score = Utils.calculate_matrix_score(matrix, size) if score == current_score: solution = solve_with_hypothesis(matrix, size) if solution is None: logger.warning("Sudoku is blocked with %i empty cases", score) else: logger.info("A solution has been found with hypothesis") matrix = solution break Utils.print_matrix(matrix, size) return matrix
def __init__(self): if Utils.in_pyinstaller_mode(): # path to root dir, expects data to be added like this when calling pyinstaller: # --add-data ".\src\learn\[...]\model.json;." # the dot means: place it in root dir model_files_dir = sys._MEIPASS else: # if not in pyinstaller mode, use theano, because it works with pygame, tensorflow doesn't Utils.set_keras_backend('theano') model_files_dir = dirname(self.get_path_to_self()) from keras.models import model_from_json model_architecture_path = os.path.join(model_files_dir, 'model_architecture.json') if not os.path.isfile(model_architecture_path): print('model architecture not found: ' + model_architecture_path) sys.exit(1) model_weights_path = os.path.join(model_files_dir, 'model_weights.h5') if not os.path.isfile(model_weights_path): print('model weights not found: ' + model_weights_path) sys.exit(1) json_file = open(model_architecture_path, 'r') self.model = model_from_json(json_file.read()) json_file.close() self.model.load_weights(model_weights_path)
def main(): validateArgs() msg, key = utils.getMsgAndKey(sys.argv) aes = Aes(key) if len(sys.argv) == 5: print "Loading Sbox Values: " + str(sys.argv[3]) + ", " + str( sys.argv[4]) aes.loadSboxValues(sys.argv[3], sys.argv[4]) cipher = aes.Cipher(msg) msg_dec = aes.EqInvCipher(cipher) displayResults(msg, cipher, msg_dec)
def equation_builder(self): try: self.__raw_equation = self.equationLineEdit.text() \ .replace("=0", "").replace("= 0", "").replace("0=", "").replace("0 =", ""). \ replace("y=", "").replace("y =", "").replace("=y", "").replace("= y", "") # Render equation self.display_equation = parse_latex("y = " + self.__raw_equation) Utils.renderLatex(latex(self.display_equation), 15, file='equation.svg') image = QPixmap('equation.svg') self.equationLabel.setPixmap(image) # Fix exp (Add missing closing bracket) self.__raw_equation = self.__raw_equation.replace("e^", r"\exp(") pos = self.__raw_equation.find(r'\exp(') self.__raw_equation = self.__raw_equation[:pos] + \ self.__raw_equation[pos:len(r'\exp(') + 2] + ")" + \ self.__raw_equation[pos + len(r'\exp(') + 1:] self.equation = parse_latex(self.__raw_equation) self.f = lambdify(x, self.equation, 'numpy') # Enable buttons self.graphButton.setEnabled(True) self.bisezioneTable.setEnabled(True) # Set graph self.graphButton.clicked.connect(self.graph) # Calculate table values self.bisezione() except SyntaxError as error: msg = QErrorMessage() msg.showMessage("Errore di sintassi!") return msg
def __init__(self, dataset_name, dataframes, wordid_userid_dataframe, user_data_dataframe, word_id=None, name=None, surname=None, handwriting=None, word_number=None, label=Utils.MOVEMENT_POINTS, frames=120, after_delay=1000): set_white_chart() self.word_dataframe, word_id = get_word_data(dataframes[label], wordid_userid_dataframe, user_data_dataframe, word_id, name, surname, handwriting, word_number) self.info = Utils.get_infos(wordid_userid_dataframe, user_data_dataframe, word_id) self.frames = frames self.max_time = max(self.word_dataframe[Utils.TIME]) self.repeat_delay = after_delay self.height = self.info[Utils.HEIGHT_PIXELS] self.width = self.info[Utils.WIDTH_PIXELS] self.colors_cycle = itertools.cycle(plt.rcParams['axes.prop_cycle']) self.color_map = {} Utils.mkdir(Utils.BUILD_GIFS_FOLDER_PATH(dataset_name)) self.title = get_title(self.info) self.gif_path = Utils.BUILD_GIFS_PATH(dataset_name, self.info[Utils.NAME], self.info[Utils.SURNAME], self.info[Utils.WORD_NUMBER], self.info[Utils.HANDWRITING], label) self._generate_animation()
def __init__(self): if not os.path.exists(db_path): print('no db found at: ' + db_path) exit(1) self.db = sqlite3.connect(db_path) self.logger = Utils.get_unique_file_logger(self, logging.INFO) # self.numb_all_games = cursor.execute( # 'SELECT COUNT(*) FROM meta').fetchone()[0] # self.games_table_length = cursor.execute( # 'SELECT COUNT(*) FROM games').fetchone()[0] # numb_invalid_games = cursor.execute( # 'SELECT COUNT(*) FROM meta WHERE all_moves_imported=0').fetchone()[0] # self.log('''database contains {} games, # {} are invalid and won\'t be used for training'''.format( # self.numb_all_games, numb_invalid_games)) # self.training_size = self.games_table_length # override this in your Learn class as desired self.data_retrieval_command = '''SELECT games.*
def Serve(host: str, port: int, cache: Dict[str, CheckResult]): # mount "/" folderPath = Utils.PrepareLocalAppdata() htmlFilePath = os.path.join(folderPath, "index.html") appConfig = { "/": { "tools.staticdir.root": os.path.abspath(os.getcwd()) }, "/static": { "tools.staticdir.on": True, "tools.staticdir.dir": "./src/wwwroot/" } } cherrypy.tree.mount(Application(htmlFilePath), "/", appConfig) # mount "/api/checkresult" apiConfig = { "/": { # the api uses restful method dispatching "tools.encode.on": True, "request.dispatch": cherrypy.dispatch.MethodDispatcher(), 'request.error_response': handle_error } } cherrypy.tree.mount(API(cache), "/api/checkresults", apiConfig) # run print(f"Starting web server on address {host}:{port}.") cherrypy.config.update({ "engine.autoreload.on": False, "log.screen": False, "server.socket_host": host, "server.socket_port": port }) cherrypy.engine.start() cherrypy.engine.block()
from src import Cost, Utils solution = Utils.open_sp("c109.p", root = "data/solutions/search/").solution v0 = solution.vehicles[0] num_cs = 0 cs = set() for v in solution.vehicles: time = 0 capacity = 0 print("Next vehicle:") num_cs += len(v.customer_history) for ca,cb in zip(v.customer_history[:-1], v.customer_history[1:]): travel_time = Cost.euclidean_cust(ca.location, cb.location) time = max(cb.readyTime, time+travel_time) capacity += cb.demand print("({:03}->{:03}) t:{:06.2f}, cap:{:03} + {}.\ [{:03}, {:04}], -> {:05.2f}".format(\ ca.custNo, cb.custNo, time, capacity, cb.demand, \ cb.readyTime, cb.dueDate, travel_time)) assert(time < cb.dueDate) cs.add(cb) print(len(solution.vehicles)) print(num_cs) print(cs)