def demo_toy_training(): animals = cap.plugin.toy.animal.load_animals() test_samples = [ cap.plugin.toy.extra_animal.load_animals( samples_type=TYPE_TEST_SAMPLE)[8] ] features_size = len(animals[0].features) model = SOM2D( features_size, max_nbh_size=9, nbh_step_size=0.3, map_rows=17, map_cols=17, ) model.train(animals) model.load_visualize_samples(animals, test_samples) model.visualize_term() fig = plt.figure() ax = plt.subplot2grid((2, 3), (0, 0), colspan=2, rowspan=2) model.visualize_plt( ax, 29, plt_style={ 0: 'r^', 1: 'b*', }, ) plt.tight_layout() plt.show()
def test_nbhs2D(self): """ to check if the function can locate 2D neighborhoods """ self.init_test(self.current_func_name) model = SOM2D(5, map_rows=8, map_cols=9, random_seed=TEST_SEED) nbh_indices = list(model.nbhs(42, 3)) self.assertEqual(23 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(24 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(25 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(26 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(27 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(28 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(29 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(60 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(61 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(17 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(18 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(45 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(53 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(len(nbh_indices), 28, 'Incorrect number of neighborhoods') nbh_indices = list(model.nbhs(42, 4)) self.assertEqual(10 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(20 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(21 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(22 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(23 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(46 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(len(nbh_indices), 42, 'Incorrect number of neighborhoods') nbh_indices = list(model.nbhs(42, 3.9)) self.assertEqual(10 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(20 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(21 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(22 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(23 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(46 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(len(nbh_indices), 40, 'Incorrect number of neighborhoods') nbh_indices = list(model.nbhs(29, 1)) self.assertEqual(28 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(29 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(30 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(21 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(37 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(len(nbh_indices), 5, 'Incorrect number of neighborhoods') nbh_indices = list(model.nbhs(32, 0)) self.assertEqual(32 in nbh_indices, True, 'Incorrect neighborhood') self.assertEqual(33 in nbh_indices, False, 'Incorrect neighborhood') self.assertEqual(len(nbh_indices), 1, 'Incorrect number of neighborhoods')
def test_init_default(self): """ to check if SOM2D initialize correctly (default) """ self.init_test(self.current_func_name) model = SOM2D(5) self.assertEqual(model.features_size, 5, 'Incorrect number of features') self.assertEqual(model.map_size, DFLT_MAP_ROWS * DFLT_MAP_COLS, 'Incorrect size of model mapping') self.assertEqual(model.weight_step_size, DFLT_WEIGHT_STEP_SIZE, 'Incorrect step size') self.assertEqual(model.max_nbh_size, DFLT_MAX_NBH_SIZE, 'Incorrect maximum number of neighborhood')
def test_from_grid(self): """ to check if SOM2D can transform 2D positions to indices """ self.init_test(self.current_func_name) model = SOM2D( 5, map_rows=7, map_cols=3, ) test_row = 3 test_col = 2 test_idx = model.from_grid(test_row, test_col) self.assertEqual(test_idx, 17, 'Incorrect idx') test_row = 4 test_col = 1 test_idx = model.from_grid(test_row, test_col) self.assertEqual(test_idx, 11, 'Incorrect idx')
def test_to_from_grid(self): """ to check if SOM2D can transform between 2D positions to indices """ self.init_test(self.current_func_name) model = SOM2D( 5, map_rows=6, map_cols=2, ) test_row = 3 test_col = 1 test_idx = model.from_grid(test_row, test_col) out_row, out_col = model.to_grid(test_idx) self.assertEqual(out_row, 3, 'Incorrect 2D row') self.assertEqual(out_col, 1, 'Incorrect 2D col') test_idx = 5 test_row, test_col = model.to_grid(test_idx) out_idx = model.from_grid(test_row, test_col) self.assertEqual(out_idx, 5, 'Incorrect idx')
def test_to_grid(self): """ to check if SOM2D can transform indices to 2D positions """ self.init_test(self.current_func_name) model = SOM2D( 5, map_rows=5, map_cols=4, ) test_row, test_col = model.to_grid(5) self.assertEqual(test_row, 0, 'Incorrect 2D row') self.assertEqual(test_col, 1, 'Incorrect 2D col') test_row, test_col = model.to_grid(9) self.assertEqual(test_row, 4, 'Incorrect 2D row') self.assertEqual(test_col, 1, 'Incorrect 2D col') test_row, test_col = model.to_grid(11) self.assertEqual(test_row, 1, 'Incorrect 2D row') self.assertEqual(test_col, 2, 'Incorrect 2D col') test_row, test_col = model.to_grid(19) self.assertEqual(test_row, 4, 'Incorrect 2D row') self.assertEqual(test_col, 3, 'Incorrect 2D col')
def som2d( training_samples, test_samples=[], visualize_params=[], out_folder=None, map_rows=DFLT_MAP_ROWS, map_cols=DFLT_MAP_COLS, weight_step_size=DFLT_WEIGHT_STEP_SIZE, nbh_step_size=DFLT_NBH_STEP_SIZE, max_nbh_size=DFLT_MAX_NBH_SIZE, random_seed=DFLT_SEED, ): features_size = len(training_samples[0].features) model = SOM2D( features_size, map_rows=map_rows, map_cols=map_cols, weight_step_size=weight_step_size, nbh_step_size=nbh_step_size, max_nbh_size=max_nbh_size, random_seed=random_seed, ) #train and load sample for visualize model.train(training_samples) visualize_samples = [] for sample in training_samples: visualize_samples.append(sample) for sample in test_samples: visualize_samples.append(sample) model.load_visualize_samples(visualize_samples) pdf_font = {'family': 'monospace', 'size': 3} matplotlib.rc('font', **pdf_font) #prepare text to visualize training attributes training_samples_size = len(training_samples) test_samples_size = len(test_samples) iterations = int(ceil(float(model.max_nbh_size) / model.nbh_step_size)) col1_txt_fmt = "{caption:<28}:{value:>7}" col1_txt = [] col1_txt.append( col1_txt_fmt.format(caption="number of training samples", value=training_samples_size)) col1_txt.append( col1_txt_fmt.format(caption="number of test samples", value=test_samples_size)) col1_txt.append( col1_txt_fmt.format(caption="features size", value=model.features_size)) col1_txt.append( col1_txt_fmt.format(caption="training iterations", value=iterations)) col2_txt_fmt = "{caption:<24}:{value:>12}" col2_txt = [] col2_txt.append( col2_txt_fmt.format(caption="map rows", value=model.map_rows)) col2_txt.append( col2_txt_fmt.format(caption="map cols", value=model.map_cols)) col2_txt.append( col2_txt_fmt.format(caption="max neighborhod size", value=model.max_nbh_size)) col2_txt.append( col2_txt_fmt.format(caption="neighborhood step size", value=model.nbh_step_size)) col2_txt.append( col2_txt_fmt.format(caption="random seed", value=model.random_seed)) #generate individual reports fig_rows = 1 fig_cols = 1 legend_width = 1 description_height = 1 fig_width = 2 fig_height = 2 plt_rows = fig_rows * fig_height + description_height plt_cols = (fig_width + legend_width) * fig_cols plt_txt_size = int(ceil(12 / fig_rows)) desc_txt_size = 10 - fig_rows fig = plt.figure() eps_file_names = [] idx = 0 for params in visualize_params: fig_col = (idx % fig_cols) * (fig_width + legend_width) fig_row = ((idx // fig_cols) * fig_height) + description_height plt.subplot2grid((1, 1), (0, 0)) ax = plt.subplot2grid( (plt_rows, plt_cols), (fig_row, fig_col), colspan=fig_width, rowspan=fig_height, ) if params['type'] == 'terminal': out_file = os.path.join(out_folder, 'terminal_out.txt') out_term = model.visualize_term( txt_width=params['txt_width'], out_file=out_file, ) model.visualize_sample_name(ax, txt_size=plt_txt_size) eps_file_name = os.path.join(out_folder, 'sample_names.eps') elif params['type'] == 'scatter': prop_name = params['prop_name'] model.visualize_plt( ax, prop_name, params['plt_style'], txt_size=plt_txt_size, ) eps_file_name = os.path.join(out_folder, 'scatter_' + prop_name + '.eps') elif params['type'] == 'debugging contour text': prop_name = params['prop_name'] model.debugging_contour_txt( ax, prop_name, txt_size=plt_txt_size, ) eps_file_name = os.path.join(out_folder, 'dbg_contour_' + prop_name + '.eps') elif params['type'] == 'debugging contour filter': prop_name = params['prop_name'] model.debugging_contour_filter( ax, prop_name, min_cutoff=params['min_cutoff'], max_cutoff=params['max_cutoff'], txt_size=plt_txt_size, ) eps_file_name = os.path.join( out_folder, 'dbg_contour_filter_' + prop_name + '.eps') elif params['type'] == 'contour': prop_name = params['prop_name'] out_plt = model.visualize_contour( ax, prop_name, min_cutoff=params['min_cutoff'], max_cutoff=params['max_cutoff'], txt_size=plt_txt_size, ) cbaxes = plt.subplot2grid( (plt_rows, plt_cols * 2), (fig_row, (fig_col + 2) * 2), rowspan=fig_height, ) plt.colorbar(out_plt, cax=cbaxes) eps_file_name = os.path.join(out_folder, 'contour_' + prop_name + '.eps') else: continue ax = plt.subplot2grid((plt_rows, plt_cols), (0, 0), colspan=fig_cols * (fig_width + legend_width)) model.visualize_txt(ax, col1_txt, col2_txt, txt_size=desc_txt_size) fig.savefig(eps_file_name, bbox_inches='tight', pad_inches=0.1) eps_file_names.append(eps_file_name) #generate summary pdf report fig_rows = 2 fig_cols = 3 legend_width = 1 description_height = 1 fig_width = 2 fig_height = 2 plt_rows = fig_rows * fig_height + description_height plt_cols = (fig_width + legend_width) * fig_cols plt_txt_size = int(ceil(12 / fig_rows)) desc_txt_size = 12 - fig_rows fig = plt.figure() idx = 0 #plot figures for params in visualize_params: fig_col = (idx % fig_cols) * (fig_width + legend_width) fig_row = ((idx // fig_cols) * fig_height) + description_height ax = plt.subplot2grid( (plt_rows, plt_cols), (fig_row, fig_col), colspan=fig_width, rowspan=fig_height, ) if params['type'] == 'terminal': out_file = os.path.join(out_folder, 'terminal_out.txt') out_term = model.visualize_term( txt_width=params['txt_width'], out_file=out_file, ) model.visualize_sample_name(ax, txt_size=plt_txt_size) elif params['type'] == 'scatter': out_plt = model.visualize_plt( ax, params['prop_name'], params['plt_style'], txt_size=plt_txt_size, ) elif params['type'] == 'debugging contour filter': out_plt = model.debugging_contour_filter( ax, params['prop_name'], min_cutoff=params['min_cutoff'], max_cutoff=params['max_cutoff'], txt_size=plt_txt_size, ) elif params['type'] == 'contour': out_plt = model.visualize_contour( ax, params['prop_name'], min_cutoff=params['min_cutoff'], max_cutoff=params['max_cutoff'], txt_size=plt_txt_size, ) cbaxes = plt.subplot2grid( (plt_rows, plt_cols * 2), (fig_row, (fig_col + 2) * 2), rowspan=fig_height, ) plt.colorbar(out_plt, cax=cbaxes) elif params['type'] == 'debugging contour text': out_plt = model.debugging_contour_txt( ax, params['prop_name'], txt_size=plt_txt_size, ) idx += 1 ax = plt.subplot2grid((plt_rows, plt_cols), (0, 0), colspan=fig_cols * (fig_width + legend_width)) out_plt = model.visualize_txt(ax, col1_txt, col2_txt, txt_size=desc_txt_size) plt.tight_layout() summary_pdf_file_name = os.path.join(out_folder, 'summary.pdf') fig.savefig(summary_pdf_file_name, bbox_inches='tight', pad_inches=0.1) return { "summary file": summary_pdf_file_name, "eps reports": eps_file_names, "terminal file": out_term, }