idx
int64
0
63k
question
stringlengths
53
5.28k
target
stringlengths
5
805
59,300
def training_set_multiplication ( training_set , mult_queue ) : logging . info ( "Multiply data..." ) for algorithm in mult_queue : new_trning_set = [ ] for recording in training_set : samples = algorithm ( recording [ 'handwriting' ] ) for sample in samples : new_trning_set . append ( { 'id' : recording [ 'id' ] , 'is...
Multiply the training set by all methods listed in mult_queue .
59,301
def _calculate_feature_stats ( feature_list , prepared , serialization_file ) : feats = [ x for x , _ in prepared ] means = numpy . mean ( feats , 0 ) mins = numpy . min ( feats , 0 ) maxs = numpy . max ( feats , 0 ) start = 0 mode = 'w' arguments = { 'newline' : '' } if sys . version_info . major < 3 : mode += 'b' arg...
Calculate min max and mean for each feature . Store it in object .
59,302
def make_hdf5 ( dataset_name , feature_count , data , output_filename , create_learning_curve ) : if dataset_name == "traindata" and create_learning_curve : max_trainingexamples = 501 output_filename_save = output_filename steps = 10 for trainingexamples in range ( 100 , max_trainingexamples , steps ) : tmp = output_fi...
Create the hdf5 file .
59,303
def get_dataset ( ) : seg_data = "segmentation-X.npy" seg_labels = "segmentation-y.npy" if os . path . isfile ( seg_data ) and os . path . isfile ( seg_labels ) : X = numpy . load ( seg_data ) y = numpy . load ( seg_labels ) with open ( 'datasets.pickle' , 'rb' ) as f : datasets = pickle . load ( f ) return ( X , y , d...
Create a dataset for machine learning of segmentations .
59,304
def get_segmented_raw_data ( top_n = 10000 ) : cfg = utils . get_database_configuration ( ) mysql = cfg [ 'mysql_online' ] connection = pymysql . connect ( host = mysql [ 'host' ] , user = mysql [ 'user' ] , passwd = mysql [ 'passwd' ] , db = mysql [ 'db' ] , cursorclass = pymysql . cursors . DictCursor ) cursor = conn...
Fetch data from the server .
59,305
def get_stroke_features ( recording , strokeid1 , strokeid2 ) : stroke1 = recording [ strokeid1 ] stroke2 = recording [ strokeid2 ] assert isinstance ( stroke1 , list ) , "stroke1 is a %s" % type ( stroke1 ) X_i = [ ] for s in [ stroke1 , stroke2 ] : hw = HandwrittenData ( json . dumps ( [ s ] ) ) feat1 = features . Co...
Get the features used to decide if two strokes belong to the same symbol or not .
59,306
def get_segmentation ( recording , single_clf , single_stroke_clf , stroke_segmented_classifier ) : mst_wood = get_mst_wood ( recording , single_clf ) return [ ( normalize_segmentation ( [ mst [ 'strokes' ] for mst in mst_wood ] ) , 1.0 ) ] X_symbol = [ get_median_stroke_distance ( recording ) ] g_top_segmentations = [...
Get a list of segmentations of recording with the probability of the segmentation being correct .
59,307
def break_mst ( mst , i ) : for j in range ( len ( mst [ 'mst' ] ) ) : mst [ 'mst' ] [ i ] [ j ] = 0 mst [ 'mst' ] [ j ] [ i ] = 0 _ , components = scipy . sparse . csgraph . connected_components ( mst [ 'mst' ] ) comp_indices = { } for el in set ( components ) : comp_indices [ el ] = { 'strokes' : [ ] , 'strokes_i' : ...
Break mst into multiple MSTs by removing one node i .
59,308
def _is_out_of_order ( segmentation ) : last_stroke = - 1 for symbol in segmentation : for stroke in symbol : if last_stroke > stroke : return True last_stroke = stroke return False
Check if a given segmentation is out of order .
59,309
def get_bb_intersections ( recording ) : intersections = numpy . zeros ( ( len ( recording ) , len ( recording ) ) , dtype = bool ) for i in range ( len ( recording ) - 1 ) : a = geometry . get_bounding_box ( recording [ i ] ) . grow ( 0.2 ) for j in range ( i + 1 , len ( recording ) ) : b = geometry . get_bounding_box...
Get all intersections of the bounding boxes of strokes .
59,310
def p_strokes ( symbol , count ) : global stroke_prob assert count >= 1 epsilon = 0.00000001 if stroke_prob is None : misc_path = pkg_resources . resource_filename ( 'hwrt' , 'misc/' ) stroke_prob_file = os . path . join ( misc_path , 'prob_stroke_count_by_symbol.yml' ) with open ( stroke_prob_file , 'r' ) as stream : ...
Get the probability of a written symbol having count strokes .
59,311
def _add_hypotheses_assuming_new_stroke ( self , new_stroke , stroke_nr , new_beam ) : guesses = single_clf . predict ( { 'data' : [ new_stroke ] , 'id' : None } ) [ : self . m ] for hyp in self . hypotheses : new_geometry = deepcopy ( hyp [ 'geometry' ] ) most_right = new_geometry if len ( hyp [ 'symbols' ] ) == 0 : w...
Get new guesses by assuming new_stroke is a new symbol .
59,312
def add_stroke ( self , new_stroke ) : global single_clf if len ( self . hypotheses ) == 0 : self . hypotheses = [ { 'segmentation' : [ ] , 'symbols' : [ ] , 'geometry' : { } , 'probability' : Decimal ( 1 ) } ] stroke_nr = len ( self . history [ 'data' ] ) new_history = deepcopy ( self . history ) new_history [ 'data' ...
Update the beam so that it considers new_stroke .
59,313
def _prune ( self ) : self . hypotheses = sorted ( self . hypotheses , key = lambda e : e [ 'probability' ] , reverse = True ) [ : self . k ]
Shorten hypotheses to the best k ones .
59,314
def get_matrices ( ) : with open ( 'hwrt/misc/is_one_symbol_classifier.pickle' , 'rb' ) as f : a = pickle . load ( f ) arrays = [ ] for el1 in a . input_storage : for el2 in el1 . __dict__ [ 'storage' ] : if isinstance ( el2 , cuda . CudaNdarray ) : arrays . append ( { 'storage' : numpy . asarray ( el2 ) , 'name' : el1...
Get the matrices from a pickled files .
59,315
def create_model_tar ( matrices , tarname = "model-cuda-converted.tar" ) : filenames = [ ] for layer in range ( len ( matrices ) ) : if matrices [ layer ] [ 'name' ] == 'W' : weights = matrices [ layer ] [ 'storage' ] weights_file = h5py . File ( 'W%i.hdf5' % ( layer / 2 ) , 'w' ) weights_file . create_dataset ( weight...
Create a tar file which contains the model .
59,316
def check_python_version ( ) : req_version = ( 2 , 7 ) cur_version = sys . version_info if cur_version >= req_version : print ( "Python version... %sOK%s (found %s, requires %s)" % ( Bcolors . OKGREEN , Bcolors . ENDC , str ( platform . python_version ( ) ) , str ( req_version [ 0 ] ) + "." + str ( req_version [ 1 ] ) ...
Check if the currently running Python version is new enough .
59,317
def main ( ) : check_python_version ( ) check_python_modules ( ) check_executables ( ) home = os . path . expanduser ( "~" ) print ( "\033[1mCheck files\033[0m" ) rcfile = os . path . join ( home , ".hwrtrc" ) if os . path . isfile ( rcfile ) : print ( "~/.hwrtrc... %sFOUND%s" % ( Bcolors . OKGREEN , Bcolors . ENDC ) )...
Execute all checks .
59,318
def merge ( d1 , d2 ) : if d1 [ 'formula_id2latex' ] is None : formula_id2latex = { } else : formula_id2latex = d1 [ 'formula_id2latex' ] . copy ( ) formula_id2latex . update ( d2 [ 'formula_id2latex' ] ) handwriting_datasets = d1 [ 'handwriting_datasets' ] for dataset in d2 [ 'handwriting_datasets' ] : handwriting_dat...
Merge two raw datasets into one .
59,319
def is_file_consistent ( local_path_file , md5_hash ) : return os . path . isfile ( local_path_file ) and hashlib . md5 ( open ( local_path_file , 'rb' ) . read ( ) ) . hexdigest ( ) == md5_hash
Check if file is there and if the md5_hash is correct .
59,320
def main ( ) : project_root = utils . get_project_root ( ) infofile = os . path . join ( project_root , "raw-datasets/info.yml" ) logging . info ( "Read '%s'..." , infofile ) with open ( infofile , 'r' ) as ymlfile : datasets = yaml . load ( ymlfile ) for dataset in datasets : local_path_file = os . path . join ( proje...
Main part of the download script .
59,321
def load_model ( ) : logging . info ( "Load language model..." ) ngram_arpa_t = pkg_resources . resource_filename ( 'hwrt' , 'misc/ngram.arpa.tar.bz2' ) with tarfile . open ( ngram_arpa_t , 'r:bz2' ) as tar : tarfolder = tempfile . mkdtemp ( ) tar . extractall ( path = tarfolder ) ngram_arpa_f = os . path . join ( tarf...
Load a n - gram language model for mathematics in ARPA format which gets shipped with hwrt .
59,322
def load_from_arpa_str ( self , arpa_str ) : data_found = False end_found = False in_ngram_block = 0 for i , line in enumerate ( arpa_str . split ( "\n" ) ) : if not end_found : if not data_found : if "\\data\\" in line : data_found = True else : if in_ngram_block == 0 : if line . startswith ( "ngram" ) : ngram_type , ...
Initialize N - gram model by reading an ARPA language model string .
59,323
def get_probability ( self , sentence ) : if len ( sentence ) == 1 : return Decimal ( 10 ) ** self . get_unigram_log_prob ( sentence ) elif len ( sentence ) == 2 : return Decimal ( 10 ) ** self . get_bigram_log_prob ( sentence ) else : log_prob = Decimal ( 0.0 ) for w1 , w2 , w3 in zip ( sentence , sentence [ 1 : ] , s...
Calculate the probability of a sentence given this language model .
59,324
def evaluate_dir ( sample_dir ) : results = [ ] if sample_dir [ - 1 ] == "/" : sample_dir = sample_dir [ : - 1 ] for filename in glob . glob ( "%s/*.inkml" % sample_dir ) : results . append ( evaluate_inkml ( filename ) ) return results
Evaluate all recordings in sample_dir .
59,325
def evaluate_inkml ( inkml_file_path ) : logging . info ( "Start evaluating '%s'..." , inkml_file_path ) ret = { 'filename' : inkml_file_path } recording = inkml . read ( inkml_file_path ) results = evaluate ( json . dumps ( recording . get_sorted_pointlist ( ) ) , result_format = 'LaTeX' ) ret [ 'results' ] = results ...
Evaluate an InkML file .
59,326
def generate_output_csv ( evaluation_results , filename = 'results.csv' ) : with open ( filename , 'w' ) as f : for result in evaluation_results : for i , entry in enumerate ( result [ 'results' ] ) : if entry [ 'semantics' ] == ',' : result [ 'results' ] [ 'semantics' ] = 'COMMA' f . write ( "%s, " % result [ 'filenam...
Generate the evaluation results in the format
59,327
def get_project_configuration ( ) : home = os . path . expanduser ( "~" ) rcfile = os . path . join ( home , ".hwrtrc" ) if not os . path . isfile ( rcfile ) : create_project_configuration ( rcfile ) with open ( rcfile , 'r' ) as ymlfile : cfg = yaml . load ( ymlfile ) return cfg
Get project configuration as dictionary .
59,328
def create_project_configuration ( filename ) : home = os . path . expanduser ( "~" ) project_root_folder = os . path . join ( home , "hwr-experiments" ) config = { 'root' : project_root_folder , 'nntoolkit' : None , 'dropbox_app_key' : None , 'dropbox_app_secret' : None , 'dbconfig' : os . path . join ( home , "hwrt-c...
Create a project configuration file which contains a configuration that might make sense .
59,329
def get_project_root ( ) : cfg = get_project_configuration ( ) for dirname in [ "raw-datasets" , "preprocessed" , "feature-files" , "models" , "reports" ] : directory = os . path . join ( cfg [ 'root' ] , dirname ) if not os . path . exists ( directory ) : os . makedirs ( directory ) raw_yml_path = pkg_resources . reso...
Get the project root folder as a string .
59,330
def get_template_folder ( ) : cfg = get_project_configuration ( ) if 'templates' not in cfg : home = os . path . expanduser ( "~" ) rcfile = os . path . join ( home , ".hwrtrc" ) cfg [ 'templates' ] = pkg_resources . resource_filename ( 'hwrt' , 'templates/' ) with open ( rcfile , 'w' ) as f : yaml . dump ( cfg , f , d...
Get path to the folder where th HTML templates are .
59,331
def get_database_config_file ( ) : cfg = get_project_configuration ( ) if 'dbconfig' in cfg : if os . path . isfile ( cfg [ 'dbconfig' ] ) : return cfg [ 'dbconfig' ] else : logging . info ( "File '%s' was not found. Adjust 'dbconfig' in your " "~/.hwrtrc file." , cfg [ 'dbconfig' ] ) else : logging . info ( "No databa...
Get the absolute path to the database configuration file .
59,332
def get_database_configuration ( ) : db_config = get_database_config_file ( ) if db_config is None : return None with open ( db_config , 'r' ) as ymlfile : cfg = yaml . load ( ymlfile ) return cfg
Get database configuration as dictionary .
59,333
def input_int_default ( question = "" , default = 0 ) : answer = input_string ( question ) if answer == "" or answer == "yes" : return default else : return int ( answer )
A function that works for both Python 2 . x and Python 3 . x . It asks the user for input and returns it as a string .
59,334
def create_run_logfile ( folder ) : with open ( os . path . join ( folder , "run.log" ) , "w" ) as f : datestring = datetime . datetime . utcnow ( ) . strftime ( "%Y-%m-%d %H:%M:%S" ) f . write ( "timestamp: '%s'" % datestring )
Create a run . log within folder . This file contains the time of the latest successful run .
59,335
def choose_raw_dataset ( currently = "" ) : folder = os . path . join ( get_project_root ( ) , "raw-datasets" ) files = [ os . path . join ( folder , name ) for name in os . listdir ( folder ) if name . endswith ( ".pickle" ) ] default = - 1 for i , filename in enumerate ( files ) : if os . path . basename ( currently ...
Let the user choose a raw dataset . Return the absolute path .
59,336
def get_readable_time ( t ) : ms = t % 1000 t -= ms t /= 1000 s = t % 60 t -= s t /= 60 minutes = t % 60 t -= minutes t /= 60 if t != 0 : return "%ih, %i minutes %is %ims" % ( t , minutes , s , ms ) elif minutes != 0 : return "%i minutes %is %ims" % ( minutes , s , ms ) elif s != 0 : return "%is %ims" % ( s , ms ) else...
Format the time to a readable format .
59,337
def default_model ( ) : project_root = get_project_root ( ) models_dir = os . path . join ( project_root , "models" ) curr_dir = os . getcwd ( ) if os . path . commonprefix ( [ models_dir , curr_dir ] ) == models_dir and curr_dir != models_dir : latest_model = curr_dir else : latest_model = get_latest_folder ( models_d...
Get a path for a default value for the model . Start searching in the current directory .
59,338
def create_adjusted_model_for_percentages ( model_src , model_use ) : shutil . copyfile ( model_src , model_use ) with open ( model_src ) as f : content = f . read ( ) content = content . replace ( "logreg" , "sigmoid" ) with open ( model_use , "w" ) as f : f . write ( content )
Replace logreg layer by sigmoid to get probabilities .
59,339
def create_hdf5 ( output_filename , feature_count , data ) : import h5py logging . info ( "Start creating of %s hdf file" , output_filename ) x = [ ] y = [ ] for features , label in data : assert len ( features ) == feature_count , "Expected %i features, got %i features" % ( feature_count , len ( features ) ) x . appen...
Create a HDF5 feature files .
59,340
def load_model ( model_file ) : with tarfile . open ( model_file ) as tar : tarfolder = tempfile . mkdtemp ( ) tar . extractall ( path = tarfolder ) from . import features from . import preprocessing with open ( os . path . join ( tarfolder , "preprocessing.yml" ) , 'r' ) as ymlfile : preprocessing_description = yaml ....
Load a model by its file . This includes the model itself but also the preprocessing queue the feature list and the output semantics .
59,341
def evaluate_model_single_recording_preloaded ( preprocessing_queue , feature_list , model , output_semantics , recording , recording_id = None ) : handwriting = handwritten_data . HandwrittenData ( recording , raw_data_id = recording_id ) handwriting . preprocessing ( preprocessing_queue ) x = handwriting . feature_ex...
Evaluate a model for a single recording after everything has been loaded .
59,342
def evaluate_model_single_recording_preloaded_multisymbol ( preprocessing_queue , feature_list , model , output_semantics , recording ) : import json import nntoolkit . evaluate recording = json . loads ( recording ) logging . info ( ( "## start (%i strokes)" % len ( recording ) ) + "#" * 80 ) hypotheses = [ ] for spli...
Evaluate a model for a single recording after everything has been loaded . Multiple symbols are recognized .
59,343
def evaluate_model_single_recording_multisymbol ( model_file , recording ) : ( preprocessing_queue , feature_list , model , output_semantics ) = load_model ( model_file ) logging . info ( "multiple symbol mode" ) logging . info ( recording ) results = evaluate_model_single_recording_preloaded ( preprocessing_queue , fe...
Evaluate a model for a single recording where possibly multiple symbols are .
59,344
def evaluate_model ( recording , model_folder , verbose = False ) : from . import preprocess_dataset from . import features for target_folder in get_recognizer_folders ( model_folder ) : if "preprocessed" in target_folder : logging . info ( "Start applying preprocessing methods..." ) t = target_folder _ , _ , preproces...
Evaluate model for a single recording .
59,345
def get_index2latex ( model_description ) : index2latex = { } translation_csv = os . path . join ( get_project_root ( ) , model_description [ "data-source" ] , "index2formula_id.csv" ) with open ( translation_csv ) as csvfile : csvreader = csv . DictReader ( csvfile , delimiter = ',' , quotechar = '"' ) for row in csvr...
Get a dictionary that maps indices to LaTeX commands .
59,346
def get_online_symbol_data ( database_id ) : import pymysql import pymysql . cursors cfg = get_database_configuration ( ) mysql = cfg [ 'mysql_online' ] connection = pymysql . connect ( host = mysql [ 'host' ] , user = mysql [ 'user' ] , passwd = mysql [ 'passwd' ] , db = mysql [ 'db' ] , cursorclass = pymysql . cursor...
Get from the server .
59,347
def classify_single_recording ( raw_data_json , model_folder , verbose = False ) : evaluation_file = evaluate_model ( raw_data_json , model_folder , verbose ) with open ( os . path . join ( model_folder , "info.yml" ) ) as ymlfile : model_description = yaml . load ( ymlfile ) index2latex = get_index2latex ( model_descr...
Get the classification as a list of tuples . The first value is the LaTeX code the second value is the probability .
59,348
def get_objectlist ( description , config_key , module ) : object_list = [ ] for feature in description : for feat , params in feature . items ( ) : feat = get_class ( feat , config_key , module ) if params is None : object_list . append ( feat ( ) ) else : parameters = { } for dicts in params : for param_name , param_...
Take a description and return a list of classes .
59,349
def get_class ( name , config_key , module ) : clsmembers = inspect . getmembers ( module , inspect . isclass ) for string_name , act_class in clsmembers : if string_name == name : return act_class cfg = get_project_configuration ( ) if config_key in cfg : modname = os . path . splitext ( os . path . basename ( cfg [ c...
Get the class by its name as a string .
59,350
def get_mysql_cfg ( ) : environment = get_project_configuration ( ) [ 'environment' ] cfg = get_database_configuration ( ) if environment == 'production' : mysql = cfg [ 'mysql_online' ] else : mysql = cfg [ 'mysql_dev' ] return mysql
Get the appropriate MySQL configuration
59,351
def softmax ( w , t = 1.0 ) : w = [ Decimal ( el ) for el in w ] e = numpy . exp ( numpy . array ( w ) / Decimal ( t ) ) dist = e / numpy . sum ( e ) return dist
Calculate the softmax of a list of numbers w .
59,352
def get_beam_cache_directory ( ) : home = os . path . expanduser ( "~" ) cache_dir = os . path . join ( home , '.hwrt-beam-cache' ) if not os . path . exists ( cache_dir ) : os . makedirs ( cache_dir ) return cache_dir
Get a directory where pickled Beam Data can be stored .
59,353
def get_beam ( secret_uuid ) : beam_dir = get_beam_cache_directory ( ) beam_filename = os . path . join ( beam_dir , secret_uuid ) if os . path . isfile ( beam_filename ) : with open ( beam_filename , 'rb' ) as handle : beam = pickle . load ( handle ) return beam else : return None
Get a beam from the session with secret_uuid .
59,354
def is_valid_uuid ( uuid_to_test , version = 4 ) : try : uuid_obj = UUID ( uuid_to_test , version = version ) except ValueError : return False return str ( uuid_obj ) == uuid_to_test
Check if uuid_to_test is a valid UUID .
59,355
def prepare_table ( table ) : n = len ( table ) for i , row in enumerate ( table ) : assert len ( row ) == n for j , el in enumerate ( row ) : if i == j : table [ i ] [ i ] = 0.0 elif i > j : table [ i ] [ j ] = 1 - table [ j ] [ i ] return table
Make the table symmetric where the lower left part of the matrix is the reverse probability
59,356
def neclusters ( l , K ) : for c in clusters ( l , K ) : if all ( x for x in c ) : yield c
Partition list l in K partitions without empty parts .
59,357
def all_segmentations ( l ) : for K in range ( 1 , len ( l ) + 1 ) : gen = neclusters ( l , K ) for el in gen : yield el
Get all segmentations of a list l .
59,358
def q ( segmentation , s1 , s2 ) : index1 = find_index ( segmentation , s1 ) index2 = find_index ( segmentation , s2 ) return index1 == index2
Test if s1 and s2 are in the same symbol given the segmentation .
59,359
def score_segmentation ( segmentation , table ) : stroke_nr = sum ( 1 for symbol in segmentation for stroke in symbol ) score = 1 for i in range ( stroke_nr ) : for j in range ( i + 1 , stroke_nr ) : qval = q ( segmentation , i , j ) if qval : score *= table [ i ] [ j ] else : score *= table [ j ] [ i ] return score
Get the score of a segmentation .
59,360
def push ( self , element , value ) : insert_pos = 0 for index , el in enumerate ( self . tops ) : if not self . find_min and el [ 1 ] >= value : insert_pos = index + 1 elif self . find_min and el [ 1 ] <= value : insert_pos = index + 1 self . tops . insert ( insert_pos , [ element , value ] ) self . tops = self . tops...
Push an element into the datastrucutre together with its value and only save it if it currently is one of the top n elements .
59,361
def _array2cstr ( arr ) : out = StringIO ( ) np . save ( out , arr ) return b64encode ( out . getvalue ( ) )
Serializes a numpy array to a compressed base64 string
59,362
def _str2array ( d ) : if type ( d ) == list : return np . asarray ( [ _str2array ( s ) for s in d ] ) ins = StringIO ( d ) return np . loadtxt ( ins )
Reconstructs a numpy array from a plain - text string
59,363
def create_output_semantics ( model_folder , outputs ) : with open ( 'output_semantics.csv' , 'wb' ) as csvfile : model_description_file = os . path . join ( model_folder , "info.yml" ) with open ( model_description_file , 'r' ) as ymlfile : model_description = yaml . load ( ymlfile ) logging . info ( "Start fetching t...
Create a output_semantics . csv file which contains information what the output of the single output neurons mean .
59,364
def elementtree_to_dict ( element ) : d = dict ( ) if hasattr ( element , 'text' ) and element . text is not None : d [ 'text' ] = element . text d . update ( element . items ( ) ) for c in list ( element ) : if c . tag not in d : d [ c . tag ] = elementtree_to_dict ( c ) else : if not isinstance ( d [ c . tag ] , list...
Convert an xml ElementTree to a dictionary .
59,365
def strip_end ( text , suffix ) : if not text . endswith ( suffix ) : return text return text [ : len ( text ) - len ( suffix ) ]
Strip suffix from the end of text if text has that suffix .
59,366
def formula_to_dbid ( formula_str , backslash_fix = False ) : global __formula_to_dbid_cache if __formula_to_dbid_cache is None : mysql = utils . get_mysql_cfg ( ) connection = pymysql . connect ( host = mysql [ 'host' ] , user = mysql [ 'user' ] , passwd = mysql [ 'passwd' ] , db = mysql [ 'db' ] , charset = 'utf8mb4'...
Convert a LaTeX formula to the database index .
59,367
def insert_recording ( hw ) : mysql = utils . get_mysql_cfg ( ) connection = pymysql . connect ( host = mysql [ 'host' ] , user = mysql [ 'user' ] , passwd = mysql [ 'passwd' ] , db = mysql [ 'db' ] , charset = 'utf8mb4' , cursorclass = pymysql . cursors . DictCursor ) try : cursor = connection . cursor ( ) sql = ( "IN...
Insert recording hw into database .
59,368
def insert_symbol_mapping ( raw_data_id , symbol_id , user_id , strokes ) : mysql = utils . get_mysql_cfg ( ) connection = pymysql . connect ( host = mysql [ 'host' ] , user = mysql [ 'user' ] , passwd = mysql [ 'passwd' ] , db = mysql [ 'db' ] , charset = 'utf8mb4' , cursorclass = pymysql . cursors . DictCursor ) curs...
Insert data into wm_strokes_to_symbol .
59,369
def filter_label ( label , replace_by_similar = True ) : bad_names = [ 'celsius' , 'degree' , 'ohm' , 'venus' , 'mars' , 'astrosun' , 'fullmoon' , 'leftmoon' , 'female' , 'male' , 'checked' , 'diameter' , 'sun' , 'Bowtie' , 'sqrt' , 'cong' , 'copyright' , 'dag' , 'parr' , 'notin' , 'dotsc' , 'mathds' , 'mathfrak' ] if ...
Some labels currently don t work together because of LaTeX naming clashes . Those will be replaced by simple strings .
59,370
def analyze_feature ( raw_datasets , feature , basename = "aspect_ratios" ) : csv_file = dam . prepare_file ( basename + '.csv' ) raw_file = dam . prepare_file ( basename + '.raw' ) csv_file = open ( csv_file , 'a' ) raw_file = open ( raw_file , 'a' ) csv_file . write ( "label,mean,std\n" ) raw_file . write ( "latex,ra...
Apply feature to all recordings in raw_datasets . Store the results in two files . One file stores the raw result the other one groups the results by symbols and stores the mean standard deviation and the name of the symbol as a csv file .
59,371
def main ( handwriting_datasets_file , analyze_features ) : logging . info ( "Start loading data '%s' ..." , handwriting_datasets_file ) loaded = pickle . load ( open ( handwriting_datasets_file ) ) raw_datasets = loaded [ 'handwriting_datasets' ] logging . info ( "%i datasets loaded." , len ( raw_datasets ) ) logging ...
Start the creation of the wanted metric .
59,372
def remove_matching_braces ( latex ) : if latex . startswith ( '{' ) and latex . endswith ( '}' ) : opened = 1 matches = True for char in latex [ 1 : - 1 ] : if char == '{' : opened += 1 elif char == '}' : opened -= 1 if opened == 0 : matches = False if matches : latex = latex [ 1 : - 1 ] return latex
If latex is surrounded by matching braces remove them . They are not necessary .
59,373
def read_folder ( folder ) : recordings = [ ] for filename in glob . glob ( os . path . join ( folder , '*.ink' ) ) : recording = parse_scg_ink_file ( filename ) recordings . append ( recording ) return recordings
Read all files of folder and return a list of HandwrittenData objects .
59,374
def _get_colors ( segmentation ) : symbol_count = len ( segmentation ) num_colors = symbol_count color_array = [ "#000000" , "#FFFF00" , "#1CE6FF" , "#FF34FF" , "#FF4A46" , "#008941" , "#006FA6" , "#A30059" , "#FFDBE5" , "#7A4900" , "#0000A6" , "#63FFAC" , "#B79762" , "#004D43" , "#8FB0FF" , "#997D87" , "#5A0007" , "#8...
Get a list of colors which is as long as the segmentation .
59,375
def fix_times ( self ) : pointlist = self . get_pointlist ( ) times = [ point [ 'time' ] for stroke in pointlist for point in stroke ] times_min = max ( min ( times ) , 0 ) for i , stroke in enumerate ( pointlist ) : for j , point in enumerate ( stroke ) : if point [ 'time' ] is None : pointlist [ i ] [ j ] [ 'time' ] ...
Some recordings have wrong times . Fix them so that nothing after loading a handwritten recording breaks .
59,376
def get_pointlist ( self ) : try : pointlist = json . loads ( self . raw_data_json ) except Exception as inst : logging . debug ( "pointStrokeList: strokelistP" ) logging . debug ( self . raw_data_json ) logging . debug ( "didn't work" ) raise inst if len ( pointlist ) == 0 : logging . warning ( "Pointlist was empty. S...
Get a list of lists of tuples from JSON raw data string . Those lists represent strokes with control points .
59,377
def get_sorted_pointlist ( self ) : pointlist = self . get_pointlist ( ) for i in range ( len ( pointlist ) ) : pointlist [ i ] = sorted ( pointlist [ i ] , key = lambda p : p [ 'time' ] ) pointlist = sorted ( pointlist , key = lambda stroke : stroke [ 0 ] [ 'time' ] ) return pointlist
Make sure that the points and strokes are in order .
59,378
def set_pointlist ( self , pointlist ) : assert type ( pointlist ) is list , "pointlist is not of type list, but %r" % type ( pointlist ) assert len ( pointlist ) >= 1 , "The pointlist of formula_id %i is %s" % ( self . formula_id , self . get_pointlist ( ) ) self . raw_data_json = json . dumps ( pointlist )
Overwrite pointlist .
59,379
def get_bounding_box ( self ) : pointlist = self . get_pointlist ( ) minx , maxx = pointlist [ 0 ] [ 0 ] [ "x" ] , pointlist [ 0 ] [ 0 ] [ "x" ] miny , maxy = pointlist [ 0 ] [ 0 ] [ "y" ] , pointlist [ 0 ] [ 0 ] [ "y" ] mint , maxt = pointlist [ 0 ] [ 0 ] [ "time" ] , pointlist [ 0 ] [ 0 ] [ "time" ] for stroke in poi...
Get the bounding box of a pointlist .
59,380
def get_bitmap ( self , time = None , size = 32 , store_path = None ) : img = Image . new ( 'L' , ( size , size ) , 'black' ) draw = ImageDraw . Draw ( img , 'L' ) bb = self . get_bounding_box ( ) for stroke in self . get_sorted_pointlist ( ) : for p1 , p2 in zip ( stroke , stroke [ 1 : ] ) : if time is not None and ( ...
Get a bitmap of the object at a given instance of time . If time is None then the bitmap is generated for the last point in time .
59,381
def preprocessing ( self , algorithms ) : assert type ( algorithms ) is list for algorithm in algorithms : algorithm ( self )
Apply preprocessing algorithms .
59,382
def feature_extraction ( self , algorithms ) : assert type ( algorithms ) is list features = [ ] for algorithm in algorithms : new_features = algorithm ( self ) assert len ( new_features ) == algorithm . get_dimension ( ) , "Expected %i features from algorithm %s, got %i features" % ( algorithm . get_dimension ( ) , st...
Get a list of features .
59,383
def show ( self ) : import matplotlib . pyplot as plt pointlist = self . get_pointlist ( ) if 'pen_down' in pointlist [ 0 ] [ 0 ] : assert len ( pointlist ) > 1 , "Lenght of pointlist was %i. Got: %s" % ( len ( pointlist ) , pointlist ) new_pointlist = [ ] last_pendown_state = None stroke = [ ] for point in pointlist [...
Show the data graphically in a new pop - up window .
59,384
def count_single_dots ( self ) : pointlist = self . get_pointlist ( ) single_dots = 0 for stroke in pointlist : if len ( stroke ) == 1 : single_dots += 1 return single_dots
Count all strokes of this recording that have only a single dot .
59,385
def to_single_symbol_list ( self ) : symbol_stream = getattr ( self , 'symbol_stream' , [ None for symbol in self . segmentation ] ) single_symbols = [ ] pointlist = self . get_sorted_pointlist ( ) for stroke_indices , label in zip ( self . segmentation , symbol_stream ) : strokes = [ ] for stroke_index in stroke_indic...
Convert this HandwrittenData object into a list of HandwrittenData objects . Each element of the list is a single symbol .
59,386
def get_git_postversion ( addon_dir ) : addon_dir = os . path . realpath ( addon_dir ) last_version = read_manifest ( addon_dir ) . get ( 'version' , '0.0.0' ) last_version_parsed = parse_version ( last_version ) if not is_git_controlled ( addon_dir ) : return last_version if get_git_uncommitted ( addon_dir ) : uncommi...
return the addon version number with a developmental version increment if there were git commits in the addon_dir after the last version change .
59,387
def _get_odoo_version_info ( addons_dir , odoo_version_override = None ) : odoo_version_info = None addons = os . listdir ( addons_dir ) for addon in addons : addon_dir = os . path . join ( addons_dir , addon ) if is_installable_addon ( addon_dir ) : manifest = read_manifest ( addon_dir ) _ , _ , addon_odoo_version_inf...
Detect Odoo version from an addons directory
59,388
def _get_version ( addon_dir , manifest , odoo_version_override = None , git_post_version = True ) : version = manifest . get ( 'version' ) if not version : warn ( "No version in manifest in %s" % addon_dir ) version = '0.0.0' if not odoo_version_override : if len ( version . split ( '.' ) ) < 5 : raise DistutilsSetupE...
Get addon version information from an addon directory
59,389
def get_install_requires_odoo_addon ( addon_dir , no_depends = [ ] , depends_override = { } , external_dependencies_override = { } , odoo_version_override = None ) : manifest = read_manifest ( addon_dir ) _ , _ , odoo_version_info = _get_version ( addon_dir , manifest , odoo_version_override , git_post_version = False ...
Get the list of requirements for an addon
59,390
def get_install_requires_odoo_addons ( addons_dir , depends_override = { } , external_dependencies_override = { } , odoo_version_override = None ) : addon_dirs = [ ] addons = os . listdir ( addons_dir ) for addon in addons : addon_dir = os . path . join ( addons_dir , addon ) if is_installable_addon ( addon_dir ) : add...
Get the list of requirements for a directory containing addons
59,391
def make_declarative_base ( self , metadata = None ) : return make_declarative_base ( self . session , Model = self . Model , metadata = metadata )
Override parent function with alchy s
59,392
def prep_doc ( self , doc_obj ) : doc = doc_obj . _data . copy ( ) for key , prop in list ( doc_obj . _base_properties . items ( ) ) : prop . validate ( doc . get ( key ) , key ) raw_value = prop . get_python_value ( doc . get ( key ) ) if prop . unique : self . check_unique ( doc_obj , key , raw_value ) value = prop ....
This method Validates gets the Python value checks unique indexes gets the db value and then returns the prepared doc dict object . Useful for save and backup functions .
59,393
def apply_zappa_settings ( zappa_obj , zappa_settings , environment ) : settings_all = json . load ( zappa_settings ) settings = settings_all [ environment ] for key , value in DEFAULT_SETTINGS . items ( ) : settings [ key ] = settings . get ( key , value ) if '~' in settings [ 'settings_file' ] : settings [ 'settings_...
Load Zappa settings set defaults if needed and apply to the Zappa object
59,394
def deploy ( environment , zappa_settings ) : print ( ( "Deploying " + environment ) ) zappa , settings , lambda_name , zip_path = _package ( environment , zappa_settings ) s3_bucket_name = settings [ 's3_bucket' ] try : zappa . load_credentials ( ) zappa . create_iam_roles ( ) zip_arn = zappa . upload_to_s3 ( zip_path...
Package create and deploy to Lambda .
59,395
def update ( environment , zappa_settings ) : print ( ( "Updating " + environment ) ) zappa , settings , lambda_name , zip_path = _package ( environment , zappa_settings ) s3_bucket_name = settings [ 's3_bucket' ] try : zappa . load_credentials ( ) zappa . create_iam_roles ( ) zip_arn = zappa . upload_to_s3 ( zip_path ...
Update an existing deployment .
59,396
def lambda_handler ( event , context , settings_name = "zappa_settings" ) : settings = importlib . import_module ( settings_name ) app_module = importlib . import_module ( settings . APP_MODULE ) app = getattr ( app_module , settings . APP_OBJECT ) app . config . from_object ( 'zappa_settings' ) app . wsgi_app = ZappaW...
An AWS Lambda function which parses specific API Gateway input into a WSGI request feeds it to Flask procceses the Flask response and returns that back to the API Gateway .
59,397
def get_context_data ( self , ** kwargs ) : queryset = kwargs . pop ( 'object_list' ) page_template = kwargs . pop ( 'page_template' , None ) context_object_name = self . get_context_object_name ( queryset ) context = { 'object_list' : queryset , 'view' : self } context . update ( kwargs ) if context_object_name is not...
Get the context for this view .
59,398
def clean_var ( text ) : text = re_invalid_var . sub ( '' , text ) text = re_invalid_start . sub ( '' , text ) return text
Turn text into a valid python classname or variable
59,399
def full_tasktrace ( self ) : if self . prev_error : return self . prev_error . tasktrace + self . tasktrace else : return self . tasktrace
List of all failed tasks caused by this and all previous errors .