def draw_scatterplot(figdir, ibs, datax, datay, xlabel, ylabel, color, fnum=None): from plottool import df2 datac = [color for _ in range(len(datax))] assert len(datax) == len(datay), '%r %r' % (len(datax), len(datay)) df2.figure(fnum=fnum, doclf=True, docla=True) df2.plt.scatter(datax, datay, c=datac, s=20, marker='o', alpha=.9) ax = df2.gca() title = '%s vs %s.\nnWords=%r. db=%r' % (xlabel, ylabel, len(datax), ibs.get_dbname()) df2.set_xlabel(xlabel) df2.set_ylabel(ylabel) ax.set_ylim(min(datay) - 1, max(datay) + 1) ax.set_xlim(min(datax) - 1, max(datax) + 1) df2.dark_background() df2.set_figtitle(title) figpath = join(figdir, title) df2.save_figure(fnum, figpath)
def make_wordfigures(ibs, metrics, invindex, figdir, wx_sample, wx2_dpath): """ Builds mosaics of patches assigned to words in sample ouptuts them to disk """ from plottool import draw_func2 as df2 import vtool as vt import parse vocabdir = join(figdir, 'vocab_patches2') ut.ensuredir(vocabdir) dump_word_patches(ibs, vocabdir, invindex, wx_sample, metrics) # COLLECTING PART --- collects patches in word folders #vocabdir seldpath = vocabdir + '_selected' ut.ensurepath(seldpath) # stack for show for wx, dpath in ut.progiter(six.iteritems(wx2_dpath), lbl='Dumping Word Images:', num=len(wx2_dpath), freq=1, backspace=False): #df2.rrr() fpath_list = ut.ls(dpath) fname_list = [basename(fpath_) for fpath_ in fpath_list] patch_list = [vt.imread(fpath_) for fpath_ in fpath_list] # color each patch by nid nid_list = [ int(parse.parse('{}_nid={nid}_{}', fname)['nid']) for fname in fname_list ] nid_set = set(nid_list) nid_list = np.array(nid_list) if len(nid_list) == len(nid_set): # no duplicate names newpatch_list = patch_list else: # duplicate names. do coloring sortx = nid_list.argsort() patch_list = np.array(patch_list, dtype=object)[sortx] fname_list = np.array(fname_list, dtype=object)[sortx] nid_list = nid_list[sortx] colors = (255 * np.array(df2.distinct_colors(len(nid_set)))).astype( np.int32) color_dict = dict(zip(nid_set, colors)) wpad, hpad = 3, 3 newshape_list = [ tuple( (np.array(patch.shape) + (wpad * 2, hpad * 2, 0)).tolist()) for patch in patch_list ] color_list = [color_dict[nid_] for nid_ in nid_list] newpatch_list = [ np.zeros(shape) + color[None, None] for shape, color in zip(newshape_list, color_list) ] for patch, newpatch in zip(patch_list, newpatch_list): newpatch[wpad:-wpad, hpad:-hpad, :] = patch #img_list = patch_list #bigpatch = vt.stack_image_recurse(patch_list) #bigpatch = vt.stack_image_list(patch_list, vert=False) bigpatch = vt.stack_square_images(newpatch_list) bigpatch_fpath = join(seldpath, basename(dpath) + '_patches.png') # def _dictstr(dict_): str_ = ut.dict_str(dict_, newlines=False) str_ = str_.replace('\'', '').replace(': ', '=').strip('{},') return str_ figtitle = '\n'.join([ 'wx=%r' % wx, 'stat(pdist): %s' % _dictstr(metrics.wx2_pdist_stats[wx]), 'stat(wdist): %s' % _dictstr(metrics.wx2_wdist_stats[wx]), ]) metrics.wx2_nMembers[wx] df2.figure(fnum=1, doclf=True, docla=True) fig, ax = df2.imshow(bigpatch, figtitle=figtitle) #fig.show() df2.set_figtitle(figtitle) df2.adjust_subplots(top=.878, bottom=0) df2.save_figure(1, bigpatch_fpath)
def make_wordfigures(ibs, metrics, invindex, figdir, wx_sample, wx2_dpath): """ Builds mosaics of patches assigned to words in sample ouptuts them to disk """ from plottool import draw_func2 as df2 import vtool as vt import parse vocabdir = join(figdir, 'vocab_patches2') ut.ensuredir(vocabdir) dump_word_patches(ibs, vocabdir, invindex, wx_sample, metrics) # COLLECTING PART --- collects patches in word folders #vocabdir seldpath = vocabdir + '_selected' ut.ensurepath(seldpath) # stack for show for wx, dpath in ut.progiter(six.iteritems(wx2_dpath), lbl='Dumping Word Images:', num=len(wx2_dpath), freq=1, backspace=False): #df2.rrr() fpath_list = ut.ls(dpath) fname_list = [basename(fpath_) for fpath_ in fpath_list] patch_list = [gtool.imread(fpath_) for fpath_ in fpath_list] # color each patch by nid nid_list = [int(parse.parse('{}_nid={nid}_{}', fname)['nid']) for fname in fname_list] nid_set = set(nid_list) nid_list = np.array(nid_list) if len(nid_list) == len(nid_set): # no duplicate names newpatch_list = patch_list else: # duplicate names. do coloring sortx = nid_list.argsort() patch_list = np.array(patch_list, dtype=object)[sortx] fname_list = np.array(fname_list, dtype=object)[sortx] nid_list = nid_list[sortx] colors = (255 * np.array(df2.distinct_colors(len(nid_set)))).astype(np.int32) color_dict = dict(zip(nid_set, colors)) wpad, hpad = 3, 3 newshape_list = [tuple((np.array(patch.shape) + (wpad * 2, hpad * 2, 0)).tolist()) for patch in patch_list] color_list = [color_dict[nid_] for nid_ in nid_list] newpatch_list = [np.zeros(shape) + color[None, None] for shape, color in zip(newshape_list, color_list)] for patch, newpatch in zip(patch_list, newpatch_list): newpatch[wpad:-wpad, hpad:-hpad, :] = patch #img_list = patch_list #bigpatch = vt.stack_image_recurse(patch_list) #bigpatch = vt.stack_image_list(patch_list, vert=False) bigpatch = vt.stack_square_images(newpatch_list) bigpatch_fpath = join(seldpath, basename(dpath) + '_patches.png') # def _dictstr(dict_): str_ = ut.dict_str(dict_, newlines=False) str_ = str_.replace('\'', '').replace(': ', '=').strip('{},') return str_ figtitle = '\n'.join([ 'wx=%r' % wx, 'stat(pdist): %s' % _dictstr(metrics.wx2_pdist_stats[wx]), 'stat(wdist): %s' % _dictstr(metrics.wx2_wdist_stats[wx]), ]) metrics.wx2_nMembers[wx] df2.figure(fnum=1, doclf=True, docla=True) fig, ax = df2.imshow(bigpatch, figtitle=figtitle) #fig.show() df2.set_figtitle(figtitle) df2.adjust_subplots(top=.878, bottom=0) df2.save_figure(1, bigpatch_fpath)