<s> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> from matplotlib . widgets import Slider , Button <EOL> from shape_learning . shape_modeler import ShapeModeler <EOL> from functools import partial <EOL> import argparse <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = "<STR_LIT:store>" , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = "<STR_LIT:store>" , nargs = '<STR_LIT:?>' , default = <NUM_LIT:3> , type = int , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = "<STR_LIT:store>" , nargs = '<STR_LIT:?>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT:file>' , action = "<STR_LIT:store>" , <EOL> help = '<STR_LIT>' ) <EOL> def update ( shapeModeler , val ) : <EOL> global sliders , mainPlot , fig , pca_params <EOL> pca_params = np . zeros ( ( num_params , <NUM_LIT:1> ) ) <EOL> for i in range ( num_params ) : <EOL> pca_params [ i ] = sliders [ i ] . val <EOL> shape = shapeModeler . makeShape ( pca_params ) <EOL> shape = ShapeModeler . normaliseShape ( shape ) <EOL> numPointsInShape = len ( shape ) / <NUM_LIT:2> <EOL> x_shape = shape [ <NUM_LIT:0> : numPointsInShape ] <EOL> y_shape = shape [ numPointsInShape : ] <EOL> mainPlot . set_data ( x_shape , - y_shape ) <EOL> fig . canvas . draw_idle ( ) <EOL> def reset ( event ) : <EOL> for i in range ( num_params ) : <EOL> sliders [ i ] . reset ( ) <EOL> def param_gui ( letter_name , shapeModeler ) : <EOL> global sliders , mainPlot , fig , pca_params <EOL> fig , ax = plt . subplots ( ) <EOL> whiteSpace = <NUM_LIT> + num_params * <NUM_LIT> <EOL> plt . subplots_adjust ( bottom = whiteSpace ) <EOL> plt . axis ( '<STR_LIT>' ) <EOL> params = np . zeros ( ( num_params , <NUM_LIT:1> ) ) <EOL> shape = shapeModeler . makeShape ( params ) <EOL> shape = ShapeModeler . normaliseShape ( shape ) <EOL> numPointsInShape = len ( shape ) / <NUM_LIT:2> <EOL> x_shape = shape [ <NUM_LIT:0> : numPointsInShape ] <EOL> y_shape = shape [ numPointsInShape : ] <EOL> mainPlot , = plt . plot ( x_shape , - y_shape ) <EOL> plt . axis ( [ - <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:1> ] , autoscale_on = False , aspect = '<STR_LIT>' ) <EOL> plt . title ( letter_name ) <EOL> parameterVariances = shapeModeler . getParameterVariances ( ) <EOL> sliders = [ <NUM_LIT:0> ] * num_params <EOL> for i in range ( num_params ) : <EOL> slider = Slider ( plt . axes ( [ <NUM_LIT> , <NUM_LIT:0.1> + <NUM_LIT> * ( num_params - i - <NUM_LIT:1> ) , <NUM_LIT> , <NUM_LIT> ] , axisbg = axcolor ) , <EOL> '<STR_LIT>' + str ( i + <NUM_LIT:1> ) , - <NUM_LIT:5> * parameterVariances [ i ] , <NUM_LIT:5> * parameterVariances [ i ] , valinit = <NUM_LIT:0> ) <EOL> slider . on_changed ( partial ( update , shapeModeler ) ) <EOL> sliders [ i ] = slider <EOL> resetax = plt . axes ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] ) <EOL> button = Button ( resetax , '<STR_LIT>' , color = axcolor , hovercolor = '<STR_LIT>' ) <EOL> button . on_clicked ( reset ) <EOL> plt . show ( ) <EOL> return pca_params <EOL> def prepareShapesModel ( datasetDirectory , regexp , num_params ) : <EOL> import glob <EOL> import os . path <EOL> import re <EOL> shapes = { } <EOL> nameFilter = re . compile ( regexp ) <EOL> datasets = glob . glob ( datasetDirectory + '<STR_LIT>' ) <EOL> for dataset in datasets : <EOL> name = os . path . splitext ( os . path . basename ( dataset ) ) [ <NUM_LIT:0> ] <EOL> if nameFilter . match ( name ) : <EOL> shapeModeler = ShapeModeler ( filename = dataset , num_principle_components = num_params ) <EOL> shapes [ name ] = shapeModeler <EOL> return shapes <EOL> pca_params = None <EOL> axcolor = '<STR_LIT>' <EOL> sliders = None <EOL> mainPlot = None <EOL> fig = None <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> args = parser . parse_args ( ) <EOL> num_params = args . num_params <EOL> datasetDirectory = args . dataset_directory <EOL> regexp = args . symbols <EOL> filename = args . file <EOL> if ( not datasetDirectory ) : <EOL> import inspect <EOL> fileName = inspect . getsourcefile ( ShapeModeler ) <EOL> installDirectory = fileName . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> datasetDirectory = installDirectory + '<STR_LIT>' <EOL> shapes = prepareShapesModel ( datasetDirectory , regexp , num_params ) <EOL> params = { } <EOL> for k , v in shapes . items ( ) : <EOL> pca_params = np . zeros ( ( num_params , <NUM_LIT:1> ) ) <EOL> params [ k ] = param_gui ( k , v ) <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> f . write ( "<STR_LIT>" % num_params ) <EOL> for k , v in params . items ( ) : <EOL> f . write ( "<STR_LIT:[>" + k + "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( [ str ( i [ <NUM_LIT:0> ] ) for i in v ] ) ) </s>
