code
stringlengths
52
7.75k
docs
stringlengths
1
5.85k
def create_graph_from_adjacency_matrix(adjacency_matrix): if is_adjacency_matrix_symmetric(adjacency_matrix): graph = UndirectedGraph() else: graph = DirectedGraph() node_column_mapping = [] num_columns = len(adjacency_matrix) for _ in range(num_columns): node_id = gra...
Generates a graph from an adjacency matrix specification. Returns a tuple containing the graph and a list-mapping of node ids to matrix column indices. The graph will be an UndirectedGraph if the provided adjacency matrix is symmetric. The graph will be a DirectedGraph if the provided adjacency ma...
def is_adjacency_matrix_symmetric(adjacency_matrix): # Verify that the matrix is square num_columns = len(adjacency_matrix) for column in adjacency_matrix: # In a square matrix, every row should be the same length as the number of columns if len(column) != num_columns: retur...
Determines if an adjacency matrix is symmetric. Ref: http://mathworld.wolfram.com/SymmetricMatrix.html
def a_star_search(graph, start, goal): all_nodes = graph.get_all_node_ids() if start not in all_nodes: raise NonexistentNodeError(start) if goal not in all_nodes: raise NonexistentNodeError(goal) came_from, cost_so_far, goal_reached = _a_star_search_internal(graph, start, goal) ...
Runs an A* search on the specified graph to find a path from the ''start'' node to the ''goal'' node. Returns a list of nodes specifying a minimal path between the two nodes. If no path exists (disconnected components), returns an empty list.
def _a_star_search_internal(graph, start, goal): frontier = PriorityQueue() frontier.put(start, 0) came_from = {start: None} cost_so_far = {start: 0} goal_reached = False while not frontier.empty(): current = frontier.get() if current == goal: goal_reached = Tr...
Performs an A* search, returning information about whether the goal node was reached and path cost information that can be used to reconstruct the path.
def add_set(self): self.__label_counter += 1 new_label = self.__label_counter self.__forest[new_label] = -1 # All new sets have their parent set to themselves self.__set_counter += 1 return new_label
Adds a new set to the forest. Returns a label by which the new set can be referenced
def find(self, node_label): queue = [] current_node = node_label while self.__forest[current_node] >= 0: queue.append(current_node) current_node = self.__forest[current_node] root_node = current_node # Path compression for n in queue: ...
Finds the set containing the node_label. Returns the set label.
def union(self, label_a, label_b): # Base case to avoid work if label_a == label_b: return # Find the tree root of each node root_a = self.find(label_a) root_b = self.find(label_b) # Avoid merging a tree to itself if root_a == root_b: ...
Joins two sets into a single new set. label_a, label_b can be any nodes within the sets
def __internal_union(self, root_a, root_b): # Merge the trees, smaller to larger update_rank = False # --Determine the larger tree rank_a = self.__forest[root_a] rank_b = self.__forest[root_b] if rank_a < rank_b: larger = root_b smaller = ...
Internal function to join two set trees specified by root_a and root_b. Assumes root_a and root_b are distinct.
def is_planar(graph): # Determine connected components as subgraphs; their planarity is independent of each other connected_components = get_connected_components_as_subgraphs(graph) for component in connected_components: # Biconnected components likewise have independent planarity bicon...
Determines whether a graph is planar or not.
def __is_subgraph_planar(graph): # --First pass: Determine edge and vertex counts validate Euler's Formula num_nodes = graph.num_nodes() num_edges = graph.num_edges() # --We can guarantee that if there are 4 or less nodes, then the graph is planar # --A 4-node simple graph has a maximum of 6 p...
Internal function to determine if a subgraph is planar.
def __setup_dfs_data(graph, adj): dfs_data = __get_dfs_data(graph, adj) dfs_data['graph'] = graph dfs_data['adj'] = adj L1, L2 = __low_point_dfs(dfs_data) dfs_data['lowpoint_1_lookup'] = L1 dfs_data['lowpoint_2_lookup'] = L2 edge_weights = __calculate_edge_weights(dfs_data) dfs_d...
Sets up the dfs_data object, for consistency.
def __calculate_edge_weights(dfs_data): graph = dfs_data['graph'] weights = {} for edge_id in graph.get_all_edge_ids(): edge_weight = __edge_weight(edge_id, dfs_data) weights[edge_id] = edge_weight return weights
Calculates the weight of each edge, for embedding-order sorting.
def __sort_adjacency_lists(dfs_data): new_adjacency_lists = {} adjacency_lists = dfs_data['adj'] edge_weights = dfs_data['edge_weights'] edge_lookup = dfs_data['edge_lookup'] for node_id, adj_list in list(adjacency_lists.items()): node_weight_lookup = {} frond_lookup = {} ...
Sorts the adjacency list representation by the edge weights.
def __branch_point_dfs(dfs_data): u = dfs_data['ordering'][0] large_n = {} large_n[u] = 0 stem = {} stem[u] = u b = {} b[u] = 1 __branch_point_dfs_recursive(u, large_n, b, stem, dfs_data) dfs_data['N_u_lookup'] = large_n dfs_data['b_u_lookup'] = b return
DFS that calculates the b(u) and N(u) lookups, and also reorders the adjacency lists.
def __embed_branch(dfs_data): u = dfs_data['ordering'][0] dfs_data['LF'] = [] dfs_data['RF'] = [] dfs_data['FG'] = {} n = dfs_data['graph'].num_nodes() f0 = (0, n) g0 = (0, n) L0 = {'u': 0, 'v': n} R0 = {'x': 0, 'y': n} dfs_data['LF'].append(f0) dfs_data['RF'].append(g0)...
Builds the combinatorial embedding of the graph. Returns whether the graph is planar.
def __insert_branch(u, v, dfs_data): w = L1(v, dfs_data) d_u = D(u, dfs_data) d_w = D(w, dfs_data) # Embed uw successful = __embed_frond(u, w, dfs_data) if not successful: return False # Embed a branch marker uu on the side opposite to uw, in the same frond block #successf...
Embeds a branch Bu(v) (as described on page 22 of the paper). Returns whether the embedding was successful.
def __do_case_5_work(d_w, d_u, case_1, case_2, case_3, dfs_data): # --We should only ever see u-cases 1 and 2 if case_3: # --We should never get here return False comp_d_w = abs(d_w) #if case_1: # --Add the frond to the left side __insert_frond_LF(d_w, d_u, dfs_data) ...
Encapsulates the work that will be done for case 5 of __embed_frond, since it gets used in more than one place.
def __do_case_6_work(d_w, d_u, case_1, case_2, case_3, dfs_data): # --We should only ever see u-cases 1 and 3 if case_2: # --We should never get here return False comp_d_w = abs(d_w) # --Add the frond to the right side __insert_frond_RF(d_w, d_u, dfs_data) # --Add uw to R...
Encapsulates the work that will be done for case 6 of __embed_frond, since it gets used in more than one place.
def __insert_frond_RF(d_w, d_u, dfs_data): # --Add the frond to the right side dfs_data['RF'].append( (d_w, d_u) ) dfs_data['FG']['r'] += 1 dfs_data['last_inserted_side'] = 'RF'
Encapsulates the process of inserting a frond uw into the right side frond group.
def __insert_frond_LF(d_w, d_u, dfs_data): # --Add the frond to the left side dfs_data['LF'].append( (d_w, d_u) ) dfs_data['FG']['l'] += 1 dfs_data['last_inserted_side'] = 'LF'
Encapsulates the process of inserting a frond uw into the left side frond group.
def merge_Fm(dfs_data): FG = dfs_data['FG'] m = FG['m'] FGm = FG[m] FGm1 = FG[m-1] if FGm[0]['u'] < FGm1[0]['u']: FGm1[0]['u'] = FGm[0]['u'] if FGm[0]['v'] > FGm1[0]['v']: FGm1[0]['v'] = FGm[0]['v'] if FGm[1]['x'] < FGm1[1]['x']: FGm1[1]['x'] = FGm[1]['x'] ...
Merges Fm-1 and Fm, as defined on page 19 of the paper.
def __check_left_side_conflict(x, y, dfs_data): l = dfs_data['FG']['l'] w, z = dfs_data['LF'][l] return __check_conflict_fronds(x, y, w, z, dfs_data)
Checks to see if the frond xy will conflict with a frond on the left side of the embedding.
def __check_right_side_conflict(x, y, dfs_data): r = dfs_data['FG']['r'] w, z = dfs_data['RF'][r] return __check_conflict_fronds(x, y, w, z, dfs_data)
Checks to see if the frond xy will conflict with a frond on the right side of the embedding.
def __check_conflict_fronds(x, y, w, z, dfs_data): # Case 1: False frond and corresponding branch marker # --x and w should both be negative, and either xy or wz should be the same value uu if x < 0 and w < 0 and (x == y or w == z): # --Determine if the marker and frond correspond (have the sa...
Checks a pair of fronds to see if they conflict. Returns True if a conflict was found, False otherwise.
def __dfsify_branch_uv(u, v, dfs_data): buv = B(u, v, dfs_data) new_list = [] for edge_id in buv: edge = dfs_data['graph'].get_edge(edge_id) j, k = edge['vertices'] d_x = D(j, dfs_data) d_y = D(k, dfs_data) if d_x < d_y: smaller = d_x larg...
Helper function to convert the output of Bu(v) from edge ids to dfs-ordered fronds.
def __get_dfs_data(graph, adj=None): ordering, parent_lookup, children_lookup = depth_first_search_with_parent_data(graph, adjacency_lists=adj) ordering_lookup = dict(list(zip(ordering, list(range(1, len(ordering) + 1))))) node_lookup = dict(list(zip(list(range(1, len(ordering) + 1)), ordering))) e...
Internal function that calculates the depth-first search of the graph. Returns a dictionary with the following data: * 'ordering': A dfs-ordering list of nodes * 'ordering_lookup': A lookup dict mapping nodes to dfs-ordering * 'node_lookup': A lookup dict mapping dfs-ordering to n...
def __calculate_adjacency_lists(graph): adj = {} for node in graph.get_all_node_ids(): neighbors = graph.neighbors(node) adj[node] = neighbors return adj
Builds an adjacency list representation for the graph, since we can't guarantee that the internal representation of the graph is stored that way.
def __get_all_lowpoints(dfs_data): lowpoint_1_lookup = {} lowpoint_2_lookup = {} ordering = dfs_data['ordering'] for node in ordering: low_1, low_2 = __get_lowpoints(node, dfs_data) lowpoint_1_lookup[node] = low_1 lowpoint_2_lookup[node] = low_2 return lowpoint_1_look...
Calculates the lowpoints for each node in a graph.
def __get_lowpoints(node, dfs_data): ordering_lookup = dfs_data['ordering_lookup'] t_u = T(node, dfs_data) sorted_t_u = sorted(t_u, key=lambda a: ordering_lookup[a]) lowpoint_1 = sorted_t_u[0] lowpoint_2 = sorted_t_u[1] return lowpoint_1, lowpoint_2
Calculates the lowpoints for a single node in a graph.
def __edge_weight(edge_id, dfs_data): graph = dfs_data['graph'] edge_lookup = dfs_data['edge_lookup'] edge = graph.get_edge(edge_id) u, v = edge['vertices'] d_u = D(u, dfs_data) d_v = D(v, dfs_data) lp_1 = L1(v, dfs_data) d_lp_1 = D(lp_1, dfs_data) if edge_lookup[edge_id] == '...
Calculates the edge weight used to sort edges.
def __calculate_bu_dfs(dfs_data): u = dfs_data['ordering'][0] b = {} b[u] = D(u, dfs_data) __calculate_bu_dfs_recursively(u, b, dfs_data) return b
Calculates the b(u) lookup table.
def __calculate_bu_dfs_recursively(u, b, dfs_data): first_time = True for v in dfs_data['adj'][u]: if a(v, dfs_data) == u: if first_time: b[v] = b[u] else: b[v] = D(u, dfs_data) __calculate_bu_dfs_recursively(v, b, dfs_data) ...
Calculates the b(u) lookup table with a recursive DFS.
def is_type_I_branch(u, v, dfs_data): if u != a(v, dfs_data): return False if u == L2(v, dfs_data): return True return False
Determines whether a branch uv is a type I branch.
def is_type_II_branch(u, v, dfs_data): if u != a(v, dfs_data): return False if u < L2(v, dfs_data): return True return False
Determines whether a branch uv is a type II branch.
def is_frond(u, v, dfs_data): d_u = D(u, dfs_data) d_v = D(v, dfs_data) edge_id = dfs_data['graph'].get_first_edge_id_by_node_ids(u, v) return True if dfs_data['edge_lookup'][edge_id] == 'backedge' and d_v < d_u else False
Determines if the edge uv is a frond ("backedge").
def __get_descendants(node, dfs_data): list_of_descendants = [] stack = deque() children_lookup = dfs_data['children_lookup'] current_node = node children = children_lookup[current_node] dfs_current_node = D(current_node, dfs_data) for n in children: dfs_child = D(n, dfs_data...
Gets the descendants of a node.
def S_star(u, dfs_data): s_u = S(u, dfs_data) if u not in s_u: s_u.append(u) return s_u
The set of all descendants of u, with u added.
def T(u, dfs_data): return list(set([w for v in S_star(u, dfs_data) for w in A(v, dfs_data)]))
T(u) consists of all vertices adjacent to u or any descendant of u.
def B(u, v, dfs_data): """Bu(v) = {wx | w is in S*(v)}""" if a(v, dfs_data) != u: return None return list(set([edge_id for w in S_star(v, dfs_data) for edge_id in dfs_data['graph'].get_node(w)['edges']]))
The branch at u containing v is the set of all edges incident on v or any descendant of v, if a(v) == u.
def stem(u, v, dfs_data): #return dfs_data['graph'].get_first_edge_id_by_node_ids(u, v) uv_edges = dfs_data['graph'].get_edge_ids_by_node_ids(u, v) buv_edges = B(u, v, dfs_data) for edge_id in uv_edges: if edge_id in buv_edges: return edge_id return None
The stem of Bu(v) is the edge uv in Bu(v).
def wt(u, v, dfs_data): # Determine the edge_id edge_id = dfs_data['graph'].get_first_edge_id_by_node_ids(u, v) # Pull the weight of that edge return dfs_data['edge_weights'][edge_id]
The wt_u[v] function used in the paper.
def _L(dfs_data): """L(T) = {v | the first w in Adj[v] corresponds to a frond vw}.""" node_set = set() for v, adj in list(dfs_data['adj'].items()): w = adj[0] if is_frond(v, w, dfs_data): node_set.add(v) return list(node_set)
L(T) contains leaves and branch points for the DFS-tree T.
def fn_x(i, dfs_data): try: return R(i, dfs_data)['x'] except Exception as e: # Page 17 states that if Ri is empty, then we take xi to be n return dfs_data['graph'].num_nodes()
The minimum vertex (DFS-number) in a frond contained in Ri.
def classify_segmented_recording(recording, result_format=None): global single_symbol_classifier if single_symbol_classifier is None: single_symbol_classifier = SingleClassificer() return single_symbol_classifier.predict(recording, result_format)
Use this function if you are sure you have a single symbol. Parameters ---------- recording : string The recording in JSON format Returns ------- list of dictionaries Each dictionary contains the keys 'symbol' and 'probability'. The list is sorted descending by probabil...
def predict(self, recording, result_format=None): evaluate = utils.evaluate_model_single_recording_preloaded results = evaluate(self.preprocessing_queue, self.feature_list, self.model, self.output_semantics, ...
Predict the class of the given recording. Parameters ---------- recording : string Recording of a single handwritten dataset in JSON format. result_format : string, optional If it is 'LaTeX', then only the latex code will be returned Returns ----...
def main(symbol_yml_file, raw_pickle_file, pickle_dest_path): metadata = get_metadata() symbol_ids = get_symbol_ids(symbol_yml_file, metadata) symbol_ids = transform_sids(symbol_ids) raw = load_raw(raw_pickle_file) filter_and_save(raw, symbol_ids, pickle_dest_path)
Parameters ---------- symbol_yml_file : str Path to a YAML file which contains recordings. raw_pickle_file : str Path to a pickle file which contains raw recordings. pickle_dest_path : str Path where the filtered dict gets serialized as a pickle file again.
def get_metadata(): misc_path = pkg_resources.resource_filename('hwrt', 'misc/') wm_symbols = os.path.join(misc_path, 'wm_symbols.csv') wm_tags = os.path.join(misc_path, 'wm_tags.csv') wm_tags2symbols = os.path.join(misc_path, 'wm_tags2symbols.csv') return {'symbols': read_csv(wm_symbols), ...
Get metadata of symbols, like their tags, id on write-math.com, LaTeX command and unicode code point. Returns ------- dict
def read_csv(filepath): symbols = [] with open(filepath, 'rb') as csvfile: spamreader = csv.DictReader(csvfile, delimiter=',', quotechar='"') for row in spamreader: symbols.append(row) return symbols
Read a CSV into a list of dictionarys. The first line of the CSV determines the keys of the dictionary. Parameters ---------- filepath : string Returns ------- list of dictionaries
def load_raw(raw_pickle_file): with open(raw_pickle_file, 'rb') as f: raw = pickle.load(f) logging.info("Loaded %i recordings.", len(raw['handwriting_datasets'])) return raw
Load a pickle file of raw recordings. Parameters ---------- raw_pickle_file : str Path to a pickle file which contains raw recordings. Returns ------- dict The loaded pickle file.
def filter_and_save(raw, symbol_ids, destination_path): logging.info('Start filtering...') new_hw_ds = [] for el in raw['handwriting_datasets']: if el['formula_id'] in symbol_ids: el['formula_id'] = symbol_ids[el['formula_id']] el['handwriting'].formula_id = symbol_ids[e...
Parameters ---------- raw : dict with key 'handwriting_datasets' symbol_ids : dict Maps LaTeX to write-math.com id destination_path : str Path where the filtered dict 'raw' will be saved
def get_metrics(metrics_description): return utils.get_objectlist(metrics_description, config_key='data_analyzation_plugins', module=sys.modules[__name__])
Get metrics from a list of dictionaries.
def prepare_file(filename): directory = os.path.join(utils.get_project_root(), "analyzation/") if not os.path.exists(directory): os.makedirs(directory) workfilename = os.path.join(directory, filename) open(workfilename, 'w').close() # Truncate the file return workfilename
Truncate the file and return the filename.
def sort_by_formula_id(raw_datasets): by_formula_id = defaultdict(list) for el in raw_datasets: by_formula_id[el['handwriting'].formula_id].append(el['handwriting']) return by_formula_id
Sort a list of formulas by `id`, where `id` represents the accepted formula id. Parameters ---------- raw_datasets : list of dictionaries A list of raw datasets. Examples -------- The parameter `raw_datasets` has to be of the format >>> rd = [{'is_in_testset': 0, ... ...
def get_features(model_description_features): return utils.get_objectlist(model_description_features, config_key='features', module=sys.modules[__name__])
Get features from a list of dictionaries Parameters ---------- model_description_features : list of dictionaries Examples -------- >>> l = [{'StrokeCount': None}, \ {'ConstantPointCoordinates': \ [{'strokes': 4}, \ {'points_per_stroke': 81}, \ ...
def print_featurelist(feature_list): input_features = sum(map(lambda n: n.get_dimension(), feature_list)) print("## Features (%i)" % input_features) print("```") for algorithm in feature_list: print("* %s" % str(algorithm)) print("```")
Print the feature_list in a human-readable form. Parameters ---------- feature_list : list feature objects
def get_dimension(self): if self.strokes > 0: if self.pixel_env > 0: return (2 + (1 + 2*self.pixel_env)**2) \ * self.strokes * self.points_per_stroke else: return 2*self.strokes * self.points_per_stroke else: ...
Get the dimension of the returned feature. This equals the number of elements in the returned list of numbers.
def _features_without_strokes(self, hwr_obj): x = [] for point in hwr_obj.get_pointlist()[0]: if len(x) >= 3*self.points_per_stroke or \ (len(x) >= 2*self.points_per_stroke and not self.pen_down): break x.append(point['x']) x.ap...
Calculate the ConstantPointCoordinates features for the case of a single (callapesed) stroke with pen_down features.
def _stroke_simplification(self, pointlist): # Find the point with the biggest distance dmax = 0 index = 0 for i in range(1, len(pointlist)): d = geometry.perpendicular_distance(pointlist[i], pointlist[0], ...
The Douglas-Peucker line simplification takes a list of points as an argument. It tries to simplifiy this list by removing as many points as possible while still maintaining the overall shape of the stroke. It does so by taking the first and the last point, connecting them by...
def get_dimension(self): return int(round(float(self.strokes**2)/2 + float(self.strokes)/2))
Get the dimension of the returned feature. This equals the number of elements in the returned list of numbers.
def chunks_to_string(chunks): string = '' began_context = False context_depth = 0 context_triggers = ['_', '^'] for chunk in chunks: if began_context and chunk != '{': string += '{' + chunk + '}' began_context = False elif began_context and chunk == '{': ...
Parameters ---------- chunks : list of strings A list of single entities in order Returns ------- string : A LaTeX-parsable string Examples -------- >>> chunks_to_string(['\\\\sum', '_', 'i', '^', 'n', 'i', '^', '2']) '\\\\sum_{i}^{n}i^{2}' >>> chunks_to_string(...
def get_preprocessing_queue(preprocessing_list): return utils.get_objectlist(preprocessing_list, config_key='preprocessing', module=sys.modules[__name__])
Get preprocessing queue from a list of dictionaries >>> l = [{'RemoveDuplicateTime': None}, {'ScaleAndShift': [{'center': True}]} ] >>> get_preprocessing_queue(l) [RemoveDuplicateTime, ScaleAndShift - center: True - max_width: 1 - max_height: 1 ]
def print_preprocessing_list(preprocessing_queue): print("## Preprocessing") print("```") for algorithm in preprocessing_queue: print("* " + str(algorithm)) print("```")
Print the ``preproc_list`` in a human-readable form. Parameters ---------- preprocessing_queue : list of preprocessing objects Algorithms that get applied for preprocessing.
def _get_parameters(self, hwr_obj): a = hwr_obj.get_bounding_box() width = a['maxx'] - a['minx'] + self.width_add height = a['maxy'] - a['miny'] + self.height_add factor_x, factor_y = 1, 1 if width != 0: factor_x = self.max_width / width if height ...
Take a list of points and calculate the factors for scaling and moving it so that it's in the unit square. Keept the aspect ratio. Optionally center the points inside of the unit square.
def _calculate_pen_down_strokes(self, pointlist, times=None): if times is None: times = [] for stroke in pointlist: stroke_info = {"start": stroke[0]['time'], "end": stroke[-1]['time'], "pen_down": True} #...
Calculate the intervall borders 'times' that contain the information when a stroke started, when it ended and how it should be interpolated.
def _calculate_pen_up_strokes(self, pointlist, times=None): if times is None: times = [] for i in range(len(pointlist) - 1): stroke_info = {"start": pointlist[i][-1]['time'], "end": pointlist[i + 1][0]['time'], "pen_d...
'Pen-up' strokes are virtual strokes that were not drawn. It models the time when the user moved from one stroke to the next.
def _space(self, hwr_obj, stroke, kind): new_stroke = [] stroke = sorted(stroke, key=lambda p: p['time']) x, y, t = [], [], [] for point in stroke: x.append(point['x']) y.append(point['y']) t.append(point['time']) x, y = numpy.array...
Do the interpolation of 'kind' for 'stroke
def _calculate_average(self, points): assert len(self.theta) == len(points), \ "points has length %i, but should have length %i" % \ (len(points), len(self.theta)) new_point = {'x': 0, 'y': 0, 'time': 0} for key in new_point: new_point[key] = self.the...
Calculate the arithmetic mean of the points x and y coordinates seperately.
def create_model(model_folder, model_type, topology, override): latest_model = utils.get_latest_in_folder(model_folder, ".json") if (latest_model == "") or override: logging.info("Create a base model...") model_src = os.path.join(model_folder, "model-0.json") command = "%s make %s %...
Create a model if it doesn't exist already. Parameters ---------- model_folder : The path to the folder where the model is described with an `info.yml` model_type : MLP topology : Something like 160:500:369 - that means the first layer has 160 neurons, the second lay...
def main(model_folder, override=False): model_description_file = os.path.join(model_folder, "info.yml") # Read the model description file with open(model_description_file, 'r') as ymlfile: model_description = yaml.load(ymlfile) project_root = utils.get_project_root() # Read the feature...
Parse the info.yml from ``model_folder`` and create the model file.
def get_parser(): project_root = utils.get_project_root() # Get latest model folder models_folder = os.path.join(project_root, "models") latest_model = utils.get_latest_folder(models_folder) # Get command line arguments from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter ...
Return the parser object for this script.
def submit_recording(raw_data_json): url = "http://www.martin-thoma.de/write-math/classify/index.php" headers = {'User-Agent': 'Mozilla/5.0', 'Content-Type': 'application/x-www-form-urlencoded'} payload = {'drawnJSON': raw_data_json} s = requests.Session() req = requests.Request...
Submit a recording to the database on write-math.com. Parameters ---------- raw_data_json : str Raw data in JSON format Raises ------ requests.exceptions.ConnectionError If the internet connection is lost.
def show_results(results, n=10): import nntoolkit.evaluate classification = nntoolkit.evaluate.show_results(results, n) return "<pre>" + classification.replace("\n", "<br/>") + "</pre>"
Show the TOP n results of a classification. >>> results = [{'\\alpha': 0.67}, {'\\propto': 0.25}] >>> show_results(results)
def interactive(): global n if request.method == 'GET' and request.args.get('heartbeat', '') != "": return request.args.get('heartbeat', '') if request.method == 'POST': logging.warning('POST to /interactive is deprecated. ' 'Use /worker instead') else: ...
Interactive classifier.
def get_json_result(results, n=10): s = [] last = -1 for res in results[:min(len(results), n)]: if res['probability'] < last*0.5 and res['probability'] < 0.05: break if res['probability'] < 0.01: break s.append(res) last = res['probability'] r...
Return the top `n` results as a JSON list. >>> results = [{'probability': 0.65, ... 'whatever': 'bar'}, ... {'probability': 0.21, ... 'whatever': 'bar'}, ... {'probability': 0.05, ... 'whatever': 'bar'},] >>> get_json_result(results, ...
def worker(): global n global use_segmenter_flag if request.method == 'POST': raw_data_json = request.form['classify'] try: secret_uuid = request.form['secret'] except: logging.info("No secret uuid given. Create one.") secret_uuid = str(uuid.u...
Implement a worker for write-math.com.
def _get_part(pointlist, strokes): result = [] strokes = sorted(strokes) for stroke_index in strokes: result.append(pointlist[stroke_index]) return result
Get some strokes of pointlist Parameters ---------- pointlist : list of lists of dicts strokes : list of integers Returns ------- list of lists of dicts
def _get_translate(): translate = {} model_path = pkg_resources.resource_filename('hwrt', 'misc/') translation_csv = os.path.join(model_path, 'latex2writemathindex.csv') arguments = {'newline': '', 'encoding': 'utf8'} with open(translation_csv, 'rt', **arguments) as csvfile: contents = ...
Get a dictionary which translates from a neural network output to semantics.
def get_writemath_id(el, translate): semantics = el['semantics'].split(";")[1] if semantics not in translate: logging.debug("Could not find '%s' in translate.", semantics) logging.debug("el: %s", el) return None else: writemathid = translate[semantics] return writema...
Parameters ---------- el : dict with key 'semantics' results element Returns ------- int or None: ID of the symbol on write-math.com
def fix_writemath_answer(results): new_results = [] # Read csv translate = _get_translate() for i, el in enumerate(results): writemathid = get_writemath_id(el, translate) if writemathid is None: continue new_results.append({'symbolnr': el['symbolnr'], ...
Bring ``results`` into a format that is accepted by write-math.com. This means using the ID for the formula that is used by the write-math server. Examples -------- >>> results = [{'symbolnr': 214, ... 'semantics': '\\triangleq', ... 'probability': 0.03}, ...] >>> fi...
def get_parser(): from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter parser = ArgumentParser(description=__doc__, formatter_class=ArgumentDefaultsHelpFormatter) parser.add_argument("-n", dest="n", default=10, type=int, ...
Return the parser object for this script.
def main(port=8000, n_output=10, use_segmenter=False): global n global use_segmenter_flag n = n_output use_segmenter_flag = use_segmenter logging.info("Start webserver...") app.run(port=port)
Main function starting the webserver.
def update_if_outdated(folder): folders = [] while os.path.isdir(folder): folders.append(folder) # Get info.yml with open(os.path.join(folder, "info.yml")) as ymlfile: content = yaml.load(ymlfile) folder = os.path.join(utils.get_project_root(), content['data-sou...
Check if the currently watched instance (model, feature or preprocessing) is outdated and update it eventually.
def generate_training_command(model_folder): update_if_outdated(model_folder) model_description_file = os.path.join(model_folder, "info.yml") # Read the model description file with open(model_description_file, 'r') as ymlfile: model_description = yaml.load(ymlfile) # Get the data paths...
Generate a string that contains a command with all necessary parameters to train the model.
def train_model(model_folder): os.chdir(model_folder) training = generate_training_command(model_folder) if training is None: return -1 logging.info(training) os.chdir(model_folder) os.system(training)
Train the model in ``model_folder``.
def main(model_folder): model_description_file = os.path.join(model_folder, "info.yml") # Read the model description file with open(model_description_file, 'r') as ymlfile: model_description = yaml.load(ymlfile) # Analyze model logging.info(model_description['model']) data = {} ...
Main part of the training script.
def get_parser(): from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter parser = ArgumentParser(description=__doc__, formatter_class=ArgumentDefaultsHelpFormatter) parser.add_argument("-m", "--model", dest="model", ...
Return the parser object for this script.
def get_bounding_box(points): assert len(points) > 0, "At least one point has to be given." min_x, max_x = points[0]['x'], points[0]['x'] min_y, max_y = points[0]['y'], points[0]['y'] for point in points: min_x, max_x = min(min_x, point['x']), max(max_x, point['x']) min_y, max_y = m...
Get the bounding box of a list of points. Parameters ---------- points : list of points Returns ------- BoundingBox
def do_bb_intersect(a, b): return a.p1.x <= b.p2.x \ and a.p2.x >= b.p1.x \ and a.p1.y <= b.p2.y \ and a.p2.y >= b.p1.y
Check if BoundingBox a intersects with BoundingBox b.
def segments_distance(segment1, segment2): assert isinstance(segment1, LineSegment), \ "segment1 is not a LineSegment, but a %s" % type(segment1) assert isinstance(segment2, LineSegment), \ "segment2 is not a LineSegment, but a %s" % type(segment2) if len(get_segments_intersections(segm...
Calculate the distance between two line segments in the plane. >>> a = LineSegment(Point(1,0), Point(2,0)) >>> b = LineSegment(Point(0,1), Point(0,2)) >>> "%0.2f" % segments_distance(a, b) '1.41' >>> c = LineSegment(Point(0,0), Point(5,5)) >>> d = LineSegment(Point(2,2), Point(4,4)) >>> e =...
def perpendicular_distance(p3, p1, p2): px = p2['x']-p1['x'] py = p2['y']-p1['y'] squared_distance = px*px + py*py if squared_distance == 0: # The line is in fact only a single dot. # In this case the distance of two points has to be # calculated line_point = Point(...
Calculate the distance from p3 to the stroke defined by p1 and p2. The distance is the length of the perpendicular from p3 on p1. Parameters ---------- p1 : dictionary with "x" and "y" start of stroke p2 : dictionary with "x" and "y" end of stroke p3 : dictionary with "x" and "y...
def dist_to(self, p2): return math.hypot(self.x - p2.x, self.y - p2.y)
Measure the distance to another point.
def get_slope(self): # y1 = m*x1 + t # y2 = m*x2 + t => y1-y2 = m*(x1-x2) <=> m = (y1-y2)/(x1-x2) return ((self.p1.y-self.p2.y) / (self.p1.x-self.p2.x))
Return the slope m of this line segment.
def get_offset(self): return self.p1.y-self.get_slope()*self.p1.x
Get the offset t of this line segment.
def count_selfintersections(self): # This can be solved more efficiently with sweep line counter = 0 for i, j in itertools.combinations(range(len(self.lineSegments)), 2): inters = get_segments_intersections(self.lineSegments[i], ...
Get the number of self-intersections of this polygonal chain.
def count_intersections(self, line_segments_b): line_segments_a = self.lineSegments # Calculate intersections intersection_points = [] for line1, line2 in itertools.product(line_segments_a, line_segments_b): intersection...
Count the intersections of two strokes with each other. Parameters ---------- line_segments_b : list A list of line segemnts Returns ------- int The number of intersections between A and B.
def get_area(self): return (self.p2.x-self.p1.x)*(self.p2.y-self.p1.y)
Calculate area of bounding box.
def get_center(self): return Point((self.p1.x+self.p2.x)/2.0, (self.p1.y+self.p2.y)/2.0)
Get the center point of this bounding box.
def _fetch_data_from_server(raw_data_id, mysql_cfg): import pymysql import pymysql.cursors # Import configuration file cfg = utils.get_database_configuration() if cfg is None: return None # Establish database connection connection = pymysql.connect(host=cfg[mysql_cfg]['host'],...
Get the data from raw_data_id from the server. :returns: The ``data`` if fetching worked, ``None`` if it failed.
def _get_data_from_rawfile(path_to_data, raw_data_id): loaded = pickle.load(open(path_to_data, "rb")) raw_datasets = loaded['handwriting_datasets'] for raw_dataset in raw_datasets: if raw_dataset['handwriting'].raw_data_id == raw_data_id: return raw_dataset['handwriting'] return...
Get a HandwrittenData object that has ``raw_data_id`` from a pickle file ``path_to_data``. :returns: The HandwrittenData object if ``raw_data_id`` is in path_to_data, otherwise ``None``.
def _list_ids(path_to_data): loaded = pickle.load(open(path_to_data, "rb")) raw_datasets = loaded['handwriting_datasets'] raw_ids = {} for raw_dataset in raw_datasets: raw_data_id = raw_dataset['handwriting'].raw_data_id if raw_dataset['formula_id'] not in raw_ids: raw_i...
List raw data IDs grouped by symbol ID from a pickle file ``path_to_data``.