def markdown_cell(markdown): r""" Args: markdown (str): Returns: str: json formatted ipython notebook markdown cell CommandLine: python -m ibeis.templates.generate_notebook --exec-markdown_cell Example: >>> # DISABLE_DOCTEST >>> from ibeis.templates.generate_notebook import * # NOQA >>> markdown = '# Title' >>> result = markdown_cell(markdown) >>> print(result) """ markdown_header = ut.codeblock( """ { "cell_type": "markdown", "metadata": {}, "source": [ """ ) markdown_footer = ut.codeblock( """ ] } """ ) return (markdown_header + '\n' + ut.indent(repr_single(markdown), ' ' * 2) + '\n' + markdown_footer)
def make_ibeis_cell_list(ibs): cell_template_list = get_default_cell_template_list(ibs) autogen_str = make_autogen_str() dbname = ibs.get_dbname() #if ut.get_argflag('--hacktestscore'): # annotconfig_list_body = ut.codeblock( # ''' # 'timectrl', # ''' # ) #else: default_acfgstr = ut.get_argval('-a', type_=str, default='default:is_known=True') annotconfig_list_body = ut.codeblock( ut.repr2(default_acfgstr) + '\n' + ut.codeblock(''' # See ibeis/expt/annotation_configs.py for names of annot configuration options #'default:has_any=(query,),dpername=1,exclude_reference=True', #'default:is_known=True', #'default:qsame_encounter=True,been_adjusted=True,excluderef=True' #'default:qsame_encounter=True,been_adjusted=True,excluderef=True,qsize=10,dsize=20', #'default:require_timestamp=True,min_timedelta=3600', #'default:species=primary', #'timectrl:', #'timectrl:been_adjusted=True,dpername=3', #'timectrl:qsize=10,dsize=20', #'unctrl:been_adjusted=True', ''') ) #if ut.get_argflag('--hacktestscore'): # pipeline_list_body = ut.codeblock( # ''' # # See ibeis/algo/Config.py for names of pipeline config options # 'default:lnbnn_on=True,bar_l2_on=False,normonly_on=False,fg_on=True', # 'default:lnbnn_on=False,bar_l2_on=True,normonly_on=False,fg_on=True', # 'default:lnbnn_on=False,bar_l2_on=False,normonly_on=True,fg_on=True', # 'default:lnbnn_on=True,bar_l2_on=False,normonly_on=False,fg_on=False', # 'default:lnbnn_on=False,bar_l2_on=True,normonly_on=False,fg_on=False', # 'default:lnbnn_on=False,bar_l2_on=False,normonly_on=True,fg_on=False', # ''' # ) #elif True: default_pcfgstr_list = ut.get_argval(('-t', '-p'), type_=list, default='default') default_pcfgstr = ut.repr3(default_pcfgstr_list, nobr=True) pipeline_list_body = ut.codeblock( default_pcfgstr + '\n' + ut.codeblock(''' #'default', #'default:K=1', #'default:K=1,AI=False', #'default:K=1,AI=False,QRH=True', #'default:K=1,RI=True,AI=False', #'default:K=1,adapteq=True', #'default:fg_on=[True,False]', ''') ) locals_ = locals() _format = partial(format_cells, locals_=locals_) cell_list = ut.flatten(map(_format, cell_template_list)) return cell_list
def check_jedi_can_read_googlestyle(): import jedi import utool as ut source1 = ut.codeblock( r''' # STARTBLOCK def spam(data): r""" Args: data (utool.ColumnLists): a column list objct """ data. # ENDBLOCK ''' ) source2 = ut.codeblock( r''' # STARTBLOCK def spam(ibs, bar): r""" Args: ibs (ibeis.IBEISController): an object """ import jedi jedi.n x = '' x.l ibs.d bar.d # ENDBLOCK ''' ) print('\n---testing jedi with utool.ColumnLists') self = script = jedi.Script(source1, line=7, column=8) # NOQA completions = script.completions() # NOQA print('completions = %r' % (completions,)) vartype = script.goto_definitions() print('vartype = %r' % (vartype,)) print('\n---testing jedi with ibeis.IBEISController') script = jedi.Script(source2, line=10) script.completions() # Find the variable type of argument self = script = jedi.Script(source2, line=11, column=7) # NOQA completions = script.completions() # NOQA print('completions = %r' % (completions,)) vartype = script.goto_definitions() print('vartype = %r' % (vartype,)) print('\n---testing jedi with undefined object bar') self = script = jedi.Script(source2, line=12, column=7) # NOQA vartype = script.goto_definitions() # NOQA print('vartype = %r' % (vartype,)) vardefs = script.goto_assignments() # NOQA print('vardefs = %r' % (vardefs,))
def testdata_deck(): jeskai_black = ut.codeblock( """ 4 Jace, Vryn's Prodigy 2 Dispel 4 Mantis Rider 4 Crackling Doom 2 Dig Through Time 2 Fiery Impulse 2 Soulfire Grand Master 3 Kolaghan's Command 3 Ojutai's Command 3 Tasigur, the Golden Fang 1 Utter End 2 Wild Slash 1 Dragonmaster Outcast 1 Sarkhan, the Dragonspeaker 1 Island 1 Plains 2 Mountain 1 Swamp 1 Smoldering Marsh 1 Sunken Hollow 2 Prairie Stream 1 Nomad Outpost 4 Mystic Monastery 4 Flooded Strand 4 Polluted Delta 4 Bloodstained Mire SB: 2 Radiant Flames SB: 1 Felidar Cub SB: 1 Negate SB: 2 Arashin Cleric SB: 2 Duress SB: 2 Exert Influence SB: 1 Dragonmaster Outcast SB: 1 Virulent Plague SB: 1 Mastery of the Unseen SB: 2 Roast """ ) mydiff = ut.codeblock( """ +1 Plains +1 Sunken Hollow +1 Smoldering Marsh +1 Evolving Wilds +1 Battlefield Forge -4 Mystic Monastery -1 Nomad Outpost """ ) decklist_text = jeskai_black return decklist_text, mydiff
def make_notebook(cell_list): """ References: # Change cell width http://stackoverflow.com/questions/21971449/how-do-i-increase-the-cell-width-of-the-ipython-notebook-in-my-browser/24207353#24207353 """ import utool as ut header = ut.codeblock( ''' { "cells": [ ''' ) footer = ut.codeblock( ''' ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 } ''') cell_body = ut.indent(',\n'.join(cell_list), ' ') notebook_str = header + '\n' + cell_body + '\n' + footer try: import json json.loads(notebook_str) except ValueError as ex: ut.printex(ex, 'Invalid notebook JSON') raise return notebook_str
def as_table(self, caption=None): if caption is None: caption = self.caption tabular = self.as_tabular() table = ut.codeblock(r""" \begin{{table}}[h] \centering \caption{{{caption}}} """).format(caption=caption) if tabular: table += '\n' + ut.indent(tabular) table += ('\n' + ut.codeblock(""" \\end{{table}} """).format()) return table
def autogen_import_list(classname, conditional_imports=None): import utool as ut #ut.embed() #line_list = [] line_list = ['import sys # NOQA'] for modname in __CLASSNAME_CLASSKEY_REGISTER__[classname]: # <super hacky> condition = None for x in conditional_imports: if modname == x[1]: condition = x[0] # </super hacky> parts = modname.split('.') frompart = '.'.join(parts[:-1]) imppart = parts[-1] #line = 'from %s import %s # NOQA' % (frompart, imppart) if condition is None: line = 'from %s import %s' % (frompart, imppart) else: line = ut.codeblock( ''' if not ut.get_argflag({condition}) or '{frompart}' in sys.modules: from {frompart} import {imppart} ''').format(condition=condition, frompart=frompart, imppart=imppart) line_list.append(line) src = '\n'.join(line_list) return src
def interactive_commandline_prompt(msg, decisiontype): prompt_fmtstr = ut.codeblock(''' Accept system {decisiontype} decision? ========== {msg} ========== * press ENTER to ACCEPT * enter {no_phrase} to REJECT * enter {embed_phrase} to embed into ipython * any other inputs ACCEPT system decision * (input is case insensitive) ''') ans_list_embed = ['cmd', 'ipy', 'embed'] ans_list_no = ['no', 'n'] #ans_list_yes = ['yes', 'y'] prompt_str = prompt_fmtstr.format( no_phrase=ut.conj_phrase(ans_list_no), embed_phrase=ut.conj_phrase(ans_list_embed), msg=msg, decisiontype=decisiontype, ) prompt_block = ut.msgblock('USER_INPUT', prompt_str) ans = input(prompt_block).lower() if ans in ans_list_embed: ut.embed() #print(ibs2.get_dbinfo_str()) #qreq_ = ut.search_stack_for_localvar('qreq_') #qreq_.normalizer elif ans in ans_list_no: return False else: return True
def assert_eq(var1, var2, msg='', var1_name=None, var2_name=None, verbose=not util_arg.QUIET): import utool as ut failed = var1 != var2 if var1_name is None: var1_name = ut.get_varname_from_stack(var1, N=1, default='var1') if var2_name is None: var2_name = ut.get_varname_from_stack(var2, N=1, default='var2') fmtdict = dict(msg=msg, var1_name=var1_name, var2_name=var2_name, var1_repr=repr(var1), var2_repr=repr(var2)) if failed: msg_fmtstr = ut.codeblock(''' +===== ERROR {var1_name} != {var2_name} msg = {msg} --- {var1_name} = {var1_repr} --- {var2_name} = {var2_repr} L_____ ''') msg = msg_fmtstr.format(**fmtdict) raise AssertionError(msg) else: print('ASSERT_EQ_PASSED: {var1_name} == {var2_name} == {var1_repr}'. format(**fmtdict))
def _debug_repr_cpd(cpd): import re import utool as ut code_fmt = ut.codeblock( ''' {variable} = pgmpy.factors.TabularCPD( variable={variable_repr}, variable_card={variable_card_repr}, values={get_cpd_repr}, evidence={evidence_repr}, evidence_card={evidence_card_repr}, ) ''') keys = ['variable', 'variable_card', 'values', 'evidence', 'evidence_card'] dict_ = ut.odict(zip(keys, [getattr(cpd, key) for key in keys])) # HACK dict_['values'] = cpd.get_cpd() r = ut.repr2(dict_, explicit=True, nobraces=True, nl=True) print(r) # Parse props that are needed for this fmtstr fmt_keys = [match.groups()[0] for match in re.finditer('{(.*?)}', code_fmt)] need_reprs = [key[:-5] for key in fmt_keys if key.endswith('_repr')] need_keys = [key for key in fmt_keys if not key.endswith('_repr')] # Get corresponding props # Call methods if needbe tmp = [(prop, getattr(cpd, prop)) for prop in need_reprs] tmp = [(x, y()) if ut.is_funclike(y) else (x, y) for (x, y) in tmp] fmtdict = dict(tmp) fmtdict = ut.map_dict_vals(ut.repr2, fmtdict) fmtdict = ut.map_dict_keys(lambda x: x + '_repr', fmtdict) tmp2 = [(prop, getattr(cpd, prop)) for prop in need_keys] fmtdict.update(dict(tmp2)) code = code_fmt.format(**fmtdict) return code
def _get_models(ibs, species, modeldir="default", cfg_override=True, verbose=VERBOSE_RF): r""" Args: ibs (IBEISController): ibeis controller object species (?): modeldir (str): (default = 'default') cfg_override (bool): (default = True) verbose (bool): verbosity flag(default = False) Returns: ?: fpath_list CommandLine: python -m ibeis.algo.detect.randomforest --test-_get_models Example: >>> # ENABLE_DOCTEST >>> from ibeis.algo.detect.randomforest import * # NOQA >>> import ibeis >>> ibs = ibeis.opendb(defaultdb='testdb1') >>> species = ibeis.const.TEST_SPECIES.ZEB_PLAIN >>> modeldir = 'default' >>> cfg_override = True >>> verbose = False >>> fpath_list = _get_models(ibs, species, modeldir, cfg_override, verbose) >>> result = ('fpath_list = %s' % (str(fpath_list),)) >>> print(result) """ # with ut.embed_on_exception_context: if cfg_override and len(ibs.cfg.detect_cfg.trees_path) > 0: trees_path = ibs.cfg.detect_cfg.trees_path else: # Ensure all models downloaded and accounted for assert species is not None, "[_get_models] Cannot detect without specifying a species" grabmodels.ensure_models(modeldir=modeldir, verbose=verbose) trees_path = grabmodels.get_species_trees_paths(species, modeldir=modeldir) # Load tree paths if ut.checkpath(trees_path, verbose=verbose): fpath_list = ut.ls(trees_path, "*.txt") # direct = Directory(trees_path, include_extensions=['txt']) # files = direct.files() else: # If the models do not exist, return None fpath_list = None if fpath_list is None or len(fpath_list) == 0: msg = ( ut.codeblock( """ [_get_models] Error loading trees, either directory or fpath_list not found * trees_path = %r * fpath_list = %r * species = %r * model_dir = %r * cfg_override = %r """ ) % (trees_path, fpath_list, species, modeldir, cfg_override) ) raise AssertionError(msg) return fpath_list
def _debug_repr_model(model): cpd_code_list = [_debug_repr_cpd(cpd) for cpd in model.cpds] code_fmt = ut.codeblock( ''' import numpy as np import pgmpy import pgmpy.inference import pgmpy.factors import pgmpy.models {cpds} cpd_list = {nodes} input_graph = {edges} model = pgmpy.models.BayesianModel(input_graph) model.add_cpds(*cpd_list) infr = pgmpy.inference.BeliefPropagation(model) ''') code = code_fmt.format( cpds='\n'.join(cpd_code_list), nodes=ut.repr2(sorted(model.nodes()), strvals=True), edges=ut.repr2(sorted(model.edges()), nl=1), ) ut.print_code(code) ut.copy_text_to_clipboard(code)
def assert_cache_hits(ibs, ismiss_list, rowid_list, kwargs_hash, **kwargs): cached_rowid_list = ut.filterfalse_items(rowid_list, ismiss_list) cache_ = ibs.table_cache[tblname][colname][kwargs_hash] # Load cached values for each rowid cache_vals_list = ut.dict_take_list(cache_, cached_rowid_list, None) db_vals_list = getter_func(ibs, cached_rowid_list, **kwargs) # Assert everything is valid msg_fmt = ut.codeblock( """ [assert_cache_hits] tblname = %r [assert_cache_hits] colname = %r [assert_cache_hits] cfgkeys = %r [assert_cache_hits] CACHE INVALID: %r != %r """ ) msg = msg_fmt % (tblname, colname, cfgkeys, cache_vals_list, db_vals_list) try: list1 = cache_vals_list list2 = db_vals_list assert ut.lists_eq(list1, list2), msg # if isinstance(db_vals_list, list): # assert cache_vals_list == db_vals_list, msg # else: # assert np.all(cache_vals_list == db_vals_list), msg except AssertionError as ex: raise ex except Exception as ex2: print(type(cache_vals_list)) print(type(db_vals_list)) ut.printex(ex2) ut.embed() raise
def hack_create_aidpair_index(ibs): # HACK IN INDEX sqlfmt = ut.codeblock( """ CREATE INDEX IF NOT EXISTS {index_name} ON {table} ({index_cols}); """ ) with ibs.staging.connect() as conn: sqlcmd = sqlfmt.format( index_name='aidpair_to_rowid', table=ibs.const.REVIEW_TABLE, index_cols=','.join([REVIEW_AID1, REVIEW_AID2]), ) conn.execute(sqlcmd) sqlcmd = sqlfmt.format( index_name='aid1_to_rowids', table=ibs.const.REVIEW_TABLE, index_cols=','.join([REVIEW_AID1]), ) conn.execute(sqlcmd) sqlcmd = sqlfmt.format( index_name='aid2_to_rowids', table=ibs.const.REVIEW_TABLE, index_cols=','.join([REVIEW_AID2]), ) conn.execute(sqlcmd)
def assert_eq(var1, var2, msg='', var1_name=None, var2_name=None, verbose=not util_arg.QUIET): import utool as ut failed = var1 != var2 if var1_name is None: var1_name = ut.get_varname_from_stack(var1, N=1, default='var1') if var2_name is None: var2_name = ut.get_varname_from_stack(var2, N=1, default='var2') fmtdict = dict( msg=msg, var1_name=var1_name, var2_name=var2_name, var1_repr=repr(var1), var2_repr=repr(var2)) if failed: msg_fmtstr = ut.codeblock(''' +===== ERROR {var1_name} != {var2_name} msg = {msg} --- {var1_name} = {var1_repr} --- {var2_name} = {var2_repr} L_____ ''') msg = msg_fmtstr.format(**fmtdict) raise AssertionError(msg) else: print('ASSERT_EQ_PASSED: {var1_name} == {var2_name} == {var1_repr}'.format(**fmtdict))
def fix_pyinstaller_sip_api(): """ Hack to get the correct version of SIP for win32 References: http://stackoverflow.com/questions/21217399/pyqt4-qtcore-qvariant-object-instead-of-a-string """ import PyInstaller from os.path import dirname, join # NOQA hook_fpath = join(dirname(PyInstaller.__file__), 'loader', 'rthooks', 'pyi_rth_qt4plugins.py') patch_code = ut.codeblock(''' try: import sip # http://stackoverflow.com/questions/21217399/pyqt4-qtcore-qvariant-object-instead-of-a-string sip.setapi('QVariant', 2) sip.setapi('QString', 2) sip.setapi('QTextStream', 2) sip.setapi('QTime', 2) sip.setapi('QUrl', 2) sip.setapi('QDate', 2) sip.setapi('QDateTime', 2) if hasattr(sip, 'setdestroyonexit'): sip.setdestroyonexit(False) # This prevents a crash on windows except ValueError as ex: print('Warning: Value Error: %s' % str(ex)) pass ''') fpath = hook_fpath # Patch the hook file tag = 'SIP_API_2' ut.inject_python_code(fpath, patch_code, tag) #ut.editfile(hook_fpath) pass
def assert_cache_hits(ibs, ismiss_list, rowid_list, kwargs_hash, **kwargs): cached_rowid_list = ut.filterfalse_items(rowid_list, ismiss_list) cache_ = ibs.table_cache[tblname][colname][kwargs_hash] # Load cached values for each rowid cache_vals_list = ut.dict_take_list(cache_, cached_rowid_list, None) db_vals_list = getter_func(ibs, cached_rowid_list, **kwargs) # Assert everything is valid msg_fmt = ut.codeblock( ''' [assert_cache_hits] tblname = %r [assert_cache_hits] colname = %r [assert_cache_hits] cfgkeys = %r [assert_cache_hits] CACHE INVALID: %r != %r ''' ) msg = msg_fmt % (tblname, colname, cfgkeys, cache_vals_list, db_vals_list, ) try: list1 = cache_vals_list list2 = db_vals_list assert ut.lists_eq(list1, list2), msg #if isinstance(db_vals_list, list): # assert cache_vals_list == db_vals_list, msg #else: # assert np.all(cache_vals_list == db_vals_list), msg except AssertionError as ex: raise ex except Exception as ex2: print(type(cache_vals_list)) print(type(db_vals_list)) ut.printex(ex2) ut.embed() raise
def _get_models(ibs, species, modeldir='default', cfg_override=True, verbose=VERBOSE_RF): r""" Args: ibs (IBEISController): ibeis controller object species (?): modeldir (str): (default = 'default') cfg_override (bool): (default = True) verbose (bool): verbosity flag(default = False) Returns: ?: fpath_list CommandLine: python -m ibeis.algo.detect.randomforest --test-_get_models Example: >>> # ENABLE_DOCTEST >>> from ibeis.algo.detect.randomforest import * # NOQA >>> import ibeis >>> ibs = ibeis.opendb(defaultdb='testdb1') >>> species = ibeis.const.TEST_SPECIES.ZEB_PLAIN >>> modeldir = 'default' >>> cfg_override = True >>> verbose = False >>> fpath_list = _get_models(ibs, species, modeldir, cfg_override, verbose) >>> result = ('fpath_list = %s' % (str(fpath_list),)) >>> print(result) """ #with ut.embed_on_exception_context: if cfg_override and len(ibs.cfg.detect_cfg.trees_path) > 0: trees_path = ibs.cfg.detect_cfg.trees_path else: # Ensure all models downloaded and accounted for assert species is not None, '[_get_models] Cannot detect without specifying a species' grabmodels.ensure_models(modeldir=modeldir, verbose=verbose) trees_path = grabmodels.get_species_trees_paths(species, modeldir=modeldir) # Load tree paths if ut.checkpath(trees_path, verbose=verbose): fpath_list = ut.ls(trees_path, '*.txt') #direct = Directory(trees_path, include_extensions=['txt']) #files = direct.files() else: # If the models do not exist, return None fpath_list = None if fpath_list is None or len(fpath_list) == 0: msg = ut.codeblock(''' [_get_models] Error loading trees, either directory or fpath_list not found * trees_path = %r * fpath_list = %r * species = %r * model_dir = %r * cfg_override = %r ''') % (trees_path, fpath_list, species, modeldir, cfg_override) raise AssertionError(msg) return fpath_list
def _debug_repr_model(model): cpd_code_list = [_debug_repr_cpd(cpd) for cpd in model.cpds] code_fmt = ut.codeblock(''' import numpy as np import pgmpy import pgmpy.inference import pgmpy.factors import pgmpy.models {cpds} cpd_list = {nodes} input_graph = {edges} model = pgmpy.models.BayesianModel(input_graph) model.add_cpds(*cpd_list) infr = pgmpy.inference.BeliefPropagation(model) ''') code = code_fmt.format( cpds='\n'.join(cpd_code_list), nodes=ut.repr2(sorted(model.nodes()), strvals=True), edges=ut.repr2(sorted(model.edges()), nl=1), ) ut.print_code(code) ut.copy_text_to_clipboard(code)
def fix_pyinstaller_sip_api(): """ Hack to get the correct version of SIP for win32 References: http://stackoverflow.com/questions/21217399/pyqt4-qtcore-qvariant-object-instead-of-a-string """ import PyInstaller from os.path import dirname, join # NOQA hook_fpath = join(dirname(PyInstaller.__file__), 'loader', 'rthooks', 'pyi_rth_qt4plugins.py') patch_code = ut.codeblock( ''' try: import sip # http://stackoverflow.com/questions/21217399/pyqt4-qtcore-qvariant-object-instead-of-a-string sip.setapi('QVariant', 2) sip.setapi('QString', 2) sip.setapi('QTextStream', 2) sip.setapi('QTime', 2) sip.setapi('QUrl', 2) sip.setapi('QDate', 2) sip.setapi('QDateTime', 2) if hasattr(sip, 'setdestroyonexit'): sip.setdestroyonexit(False) # This prevents a crash on windows except ValueError as ex: print('Warning: Value Error: %s' % str(ex)) pass ''') fpath = hook_fpath # Patch the hook file tag = 'SIP_API_2' ut.inject_python_code(fpath, patch_code, tag) #ut.editfile(hook_fpath) pass
def add_data(a, dst, src): if dst == '': raise ValueError('dst path cannot be the empty string') if src == '': raise ValueError('src path cannot be the empty string') src_ = ut.platform_path(src) if not os.path.exists(dirname(dst)) and dirname(dst) != "": os.makedirs(dirname(dst)) _pretty_path = lambda str_: str_.replace('\\', '/') # Default datatype is DATA dtype = 'DATA' # Infer datatype from extension #extension = splitext(dst)[1].lower() #if extension == LIB_EXT.lower(): if LIB_EXT[1:] in dst.split('.'): dtype = 'BINARY' print( ut.codeblock(''' [installer] a.add_data( [installer] dst=%r, [installer] src=%r, [installer] dtype=%s)''') % (_pretty_path(dst), _pretty_path(src_), dtype)) assert exists(src_), 'src_=%r does not exist' a.datas.append((dst, src_, dtype))
def code_cell(sourcecode): r""" Args: sourcecode (str): Returns: str: json formatted ipython notebook code cell CommandLine: python -m ibeis.templates.generate_notebook --exec-code_cell Example: >>> # DISABLE_DOCTEST >>> from ibeis.templates.generate_notebook import * # NOQA >>> sourcecode = notebook_cells.timestamp_distribution[1] >>> sourcecode = notebook_cells.initialize[1] >>> result = code_cell(sourcecode) >>> print(result) """ import utool as ut sourcecode = ut.remove_codeblock_syntax_sentinals(sourcecode) cell_header = ut.codeblock( ''' { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": ''') cell_footer = ut.codeblock( ''' } ''') if sourcecode is None: source_line_repr = ' []\n' else: lines = sourcecode.split('\n') line_list = [line + '\n' if count < len(lines) else line for count, line in enumerate(lines, start=1)] #repr_line_list = [repr_single_for_md(line) for line in line_list] repr_line_list = [repr_single_for_md(line) for line in line_list] source_line_repr = ut.indent(',\n'.join(repr_line_list), ' ' * 2) source_line_repr = ' [\n' + source_line_repr + '\n ]\n' return (cell_header + source_line_repr + cell_footer)
def ensure_inno_script(): """ writes inno script to disk for win32 installer build """ cwd = get_setup_dpath() iss_script_fpath = join(cwd, '_installers', 'win_installer_script.iss') # THE ISS USES {} AS SYNTAX. CAREFUL #app_publisher = 'Rensselaer Polytechnic Institute' #app_name = 'IBEIS' import ibeis iss_script_code = ut.codeblock( r''' ; Script generated by the Inno Setup Script Wizard. ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES! ; http://www.jrsoftware.org/isdl.php [Setup] ; NOTE: The value of AppId uniquely identifies this application. ; Do not use the same AppId value in installers for other applications. ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.) ; Also it seems like the off-balanced curly brace is necessary AppId={{47BE3DA2-261D-4672-9849-18BB2EB382FC} AppName=IBEIS AppVersion=''' + str(ibeis.__version__) + ''' ;AppVerName=IBEIS 1 AppPublisher=Rensselaer Polytechnic Institute AppPublisherURL=ibeis.org ;www.rpi.edu/~crallj/ AppSupportURL=ibeis.org ;ww.rpi.edu/~crallj/ AppUpdatesURL=ibeis.org ;www.rpi.edu/~crallj/ DefaultDirName={pf}\IBEIS DefaultGroupName=IBEIS OutputBaseFilename=ibeis-win32-setup SetupIconFile=ibsicon.ico Compression=lzma SolidCompression=yes [Languages] Name: "english"; MessagesFile: "compiler:Default.isl" [Tasks] Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked [Files] Source: "..\dist\ibeis\IBEISApp.exe"; DestDir: "{app}"; Flags: ignoreversion Source: "..\dist\ibeis\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs ; NOTE: Don't use "Flags: ignoreversion" on any shared system files [Icons] Name: "{group}\ibeis"; Filename: "{app}\IBEISApp.exe" Name: "{commondesktop}\ibeis"; Filename: "{app}\IBEISApp.exe"; Tasks: desktopicon [Run] Filename: "{app}\IBEISApp.exe"; Description: "{cm:LaunchProgram,IBEIS}"; Flags: nowait postinstall skipifsilent ''' ) ut.write_to(iss_script_fpath, iss_script_code, onlyifdiff=True) assert ut.checkpath(iss_script_fpath, verbose=True, info=True), 'cannot find iss_script_fpath' return iss_script_fpath
def task_link(index): item = task_data[index] return ut.codeblock(''' <h1>Task</h1> <p>dbname={dbname}</p> <p>task={task}</p> <p>mcc={mcc}</p> <p>auc_ovr={auc_ovr}</p> <hr><small>index: {index}</small> ''').format(**item)
def make_notebook(cell_list): """ References: # Change cell width http://stackoverflow.com/questions/21971449/how-do-i-increase-the-cell-width-of-the-ipython-notebook-in-my-browser/24207353#24207353 """ header = ut.codeblock( """ { "cells": [ """ ) footer = ut.codeblock( """ ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 } """) cell_body = ut.indent(',\n'.join(cell_list), ' ') notebook_str = header + '\n' + cell_body + '\n' + footer return notebook_str
def make_bayes_notebook(): r""" CommandLine: python -m wbia.unstable.demobayes --exec-make_bayes_notebook Example: >>> # DISABLE_DOCTEST >>> from wbia.unstable.demobayes import * # NOQA >>> result = make_bayes_notebook() >>> print(result) """ from wbia.templates import generate_notebook initialize = ut.codeblock(r""" # STARTBLOCK import os os.environ['UTOOL_NO_CNN'] = 'True' from wbia.unstable.demobayes import * # NOQA # Matplotlib stuff import matplotlib as mpl %matplotlib inline %load_ext autoreload %autoreload from IPython.core.display import HTML HTML("<style>body .container { width:99% !important; }</style>") # ENDBLOCK """) cell_list_def = [ initialize, show_model_templates, demo_modes, demo_name_annot_complexity, # demo_model_idependencies, demo_single_add, demo_ambiguity, demo_conflicting_evidence, demo_annot_idependence_overlap, ] def format_cell(cell): if ut.is_funclike(cell): header = '# ' + ut.to_title_caps(ut.get_funcname(cell)) code = (header, ut.get_func_sourcecode(cell, stripdef=True, stripret=True)) else: code = (None, cell) return generate_notebook.format_cells(code) cell_list = ut.flatten([format_cell(cell) for cell in cell_list_def]) nbstr = generate_notebook.make_notebook(cell_list) logger.info('nbstr = %s' % (nbstr, )) fpath = 'demobayes.ipynb' ut.writeto(fpath, nbstr) ut.startfile(fpath)
def add_label(co_wgt): # Very simply adds the text _LABEL = partial(gt.newLabel, parent=co_wgt) if not co_wgt.hack: text = ut.codeblock(''' * Find the image of the clock * Set the sliders to correspond with the clock * Click Set * Skip if time synchonization is not relevant to you ''') main_label = _LABEL(text=text, align='left') co_wgt.text_layout.addWidget(main_label) else: text = ut.codeblock(''' * Set the time for image %r ''' % (co_wgt.gid_list, )) gpath = co_wgt.ibs.get_image_paths( co_wgt.gid_list[co_wgt.current_gindex]) image_label = _LABEL(text='', gpath=gpath) # align='left') co_wgt.image_label = image_label co_wgt.text_layout.addWidget(image_label)
def make_bayes_notebook(): r""" CommandLine: python -m ibeis.algo.hots.demobayes --exec-make_bayes_notebook Example: >>> # DISABLE_DOCTEST >>> from ibeis.algo.hots.demobayes import * # NOQA >>> result = make_bayes_notebook() >>> print(result) """ from ibeis.templates import generate_notebook initialize = ut.codeblock( r''' # STARTBLOCK import os os.environ['UTOOL_NO_CNN'] = 'True' from ibeis.algo.hots.demobayes import * # NOQA # Matplotlib stuff import matplotlib as mpl %matplotlib inline %load_ext autoreload %autoreload from IPython.core.display import HTML HTML("<style>body .container { width:99% !important; }</style>") # ENDBLOCK ''' ) cell_list_def = [ initialize, show_model_templates, demo_modes, demo_name_annot_complexity, ###demo_model_idependencies, demo_single_add, demo_ambiguity, demo_conflicting_evidence, demo_annot_idependence_overlap, ] def format_cell(cell): if ut.is_funclike(cell): header = '# ' + ut.to_title_caps(ut.get_funcname(cell)) code = (header, ut.get_func_sourcecode(cell, stripdef=True, stripret=True)) else: code = (None, cell) return generate_notebook.format_cells(code) cell_list = ut.flatten([format_cell(cell) for cell in cell_list_def]) nbstr = generate_notebook.make_notebook(cell_list) print('nbstr = %s' % (nbstr,)) fpath = 'demobayes.ipynb' ut.writeto(fpath, nbstr) ut.startfile(fpath)
def make_wbia_cell_list(ibs): cell_template_list = get_default_cell_template_list(ibs) autogen_str = '# python -m wbia autogen_ipynb --launch --dbdir %r' % (ibs.get_dbdir()) # autogen_str = ut.make_autogen_str() dbname = ibs.get_dbname() dbdir = ibs.dbdir default_acfgstr = ut.get_argval('-a', type_=str, default='default:is_known=True') asreport = ut.get_argflag('--asreport') default_pcfgstr_list = ut.get_argval(('-t', '-p'), type_=list, default='default') default_pcfgstr = ut.repr3(default_pcfgstr_list, nobr=True) if asreport: annotconfig_list_body = ut.codeblock(ut.repr2(default_acfgstr)) pipeline_list_body = ut.codeblock(default_pcfgstr) else: annotconfig_list_body = ut.codeblock( ut.repr2(default_acfgstr) + '\n' + ut.codeblock( """ #'default:has_any=(query,),dpername=1,exclude_reference=True', #'default:is_known=True', #'default:is_known=True,minqual=good,require_timestamp=True,dcrossval_enc=1,view=left' #'default:qsame_imageset=True,been_adjusted=True,excluderef=True,qsize=10,dsize=20', #'default:require_timestamp=True,min_timedelta=3600', #'default:species=primary', #'unctrl:been_adjusted=True', #'timectrl:', #'timectrl:view=primary,minqual=good', #'default:minqual=good,require_timestamp=True,view=left,dcrossval_enc=1,joinme=1', #'default:minqual=good,require_timestamp=True,view=right,dcrossval_enc=1,joinme=1', #'default:minqual=ok,require_timestamp=True,view=left,dcrossval_enc=1,joinme=2', #'default:minqual=ok,require_timestamp=True,view=right,dcrossval_enc=1,joinme=2', """ ) ) pipeline_list_body = ut.codeblock( default_pcfgstr + '\n' + ut.codeblock( """ #'default', #'default:K=1,AI=False,QRH=True', #'default:K=1,RI=True,AI=False', #'default:K=1,adapteq=True', #'default:fg_on=[True,False]', """ ) ) locals_ = locals() _format = partial(ut.format_cells, locals_=locals_) cell_list = ut.flatten(map(_format, cell_template_list)) return cell_list
def make_autogen_str(): import sys autogenkw = dict( stamp=ut.timestamp('printable'), regen_cmd=' '.join(sys.argv) ) return ut.codeblock( ''' # Autogenerated on {stamp} # Regen Command: # {regen_cmd} # ''' ).format(**autogenkw)
def kwiver(): import utool as ut ut.codeblock( r''' # STARTBLOCK bash git checkout master cd ~/code/kwiver rm -rf ~/code/kwiver/build-py2-nocuda mkdir -p build-py2-nocuda cd ~/code/kwiver/build-py2-nocuda cmake -G "Unix Makefiles" \ -D KWIVER_ENABLE_ARROWS:BOOL=True \ -D KWIVER_ENABLE_C_BINDINGS:BOOL=True \ -D KWIVER_ENABLE_PYTHON:BOOL=True \ -D KWIVER_ENABLE_TESTS:BOOL=True \ -D PYTHON_VERSION=$(python -c "import sys; print(sys.version[0:3])") \ -D fletch_DIR:PATH=~/code/fletch/build-py2-nocuda/ \ ~/code/kwiver ''')
def set_annot_pair_as_negative_match_(ibs, aid1, aid2, cm, qreq_, **kwargs): """ MARK AS INCORRECT """ def on_nontrivial_split(ibs, aid1, aid2): aid1_groundtruth = ibs.get_annot_groundtruth(aid1, noself=True) logger.info( 'There are %d annots in this name. Need more sophisticated split' % (len(aid1_groundtruth)) ) raise guiexcept.NeedsUserInput('non-trivial split') try: status = ibs.set_annot_pair_as_negative_match( aid1, aid2, on_nontrivial_split=on_nontrivial_split, logger=kwargs.get('logger', None), ) logger.info('status = %r' % (status,)) except guiexcept.NeedsUserInput: options = ['Flag for later', 'Review now'] reply = gt.user_option( msg=ut.codeblock( """ Marking this as False induces a split case. Choose how to handle this. """ ), options=options, ) if reply == options[0]: prop = 'SplitCase' if 'logger' in kwargs: log = kwargs['logger'].info else: log = print annot_uuid_pair = ibs.get_annot_uuids((aid1, aid2)) log('FLAG SplitCase: (annot_uuid_pair=%r)' % annot_uuid_pair) am_rowid = ibs.add_annotmatch_undirected([aid1], [aid2])[0] ibs.set_annotmatch_prop(prop, [am_rowid], [True]) ibs.set_annotmatch_evidence_decision( [am_rowid], [ibs.const.EVIDENCE_DECISION.NEGATIVE] ) elif reply == options[1]: review_match(ibs, aid1, aid2, qreq_=qreq_, cm=cm, **kwargs) except guiexcept.UserCancel: logger.info('user canceled negative match')
def add_label(co_wgt): # Very simply adds the text _LABEL = partial(guitool.newLabel, parent=co_wgt) if not co_wgt.hack: text = ut.codeblock( ''' * Find the image of the clock * Set the sliders to correspond with the clock * Click Set * Skip if time synchonization is not relevant to you ''' ) main_label = _LABEL(text=text, align='left') co_wgt.text_layout.addWidget(main_label) else: text = ut.codeblock( ''' * Set the time for image %r ''' % (co_wgt.gid_list,) ) gpath = co_wgt.ibs.get_image_paths(co_wgt.gid_list[co_wgt.current_gindex]) image_label = _LABEL(text='', gpath=gpath) # align='left') co_wgt.image_label = image_label co_wgt.text_layout.addWidget(image_label)
def markdown_cell(markdown): r""" Args: markdown (str): Returns: str: json formatted ipython notebook markdown cell CommandLine: python -m ibeis.templates.generate_notebook --exec-markdown_cell Example: >>> # DISABLE_DOCTEST >>> from ibeis.templates.generate_notebook import * # NOQA >>> markdown = '# Title' >>> result = markdown_cell(markdown) >>> print(result) """ import utool as ut markdown_header = ut.codeblock( ''' { "cell_type": "markdown", "metadata": {}, "source": [ ''' ) markdown_footer = ut.codeblock( ''' ] } ''' ) return (markdown_header + '\n' + ut.indent(repr_single_for_md(markdown), ' ' * 2) + '\n' + markdown_footer)
def init_theanorc(): theanorc_fpath = join(os.getenv('HOME'), '.theanorc') theanorc_text = ut.codeblock(''' [global] floatX = float32 device = gpu0 openmp = True [nvcc] fastmath = True ''') if ut.checkpath(theanorc_fpath, verbose=True): if not ut.arg_you_sure('overwrite?'): return ut.write_to(theanorc_fpath, theanorc_text)
def hack_create_aidpair_index(ibs): # HACK IN INDEX sqlfmt = ut.codeblock(''' CREATE INDEX IF NOT EXISTS {index_name} ON {table} ({index_cols}); ''') sqlcmd = sqlfmt.format(index_name='aidpair_to_rowid', table=ibs.const.REVIEW_TABLE, index_cols=','.join([REVIEW_AID1, REVIEW_AID2])) ibs.staging.connection.execute(sqlcmd).fetchall() sqlcmd = sqlfmt.format(index_name='aid1_to_rowids', table=ibs.const.REVIEW_TABLE, index_cols=','.join([REVIEW_AID1])) ibs.staging.connection.execute(sqlcmd).fetchall() sqlcmd = sqlfmt.format(index_name='aid2_to_rowids', table=ibs.const.REVIEW_TABLE, index_cols=','.join([REVIEW_AID2])) ibs.staging.connection.execute(sqlcmd).fetchall()
def main(self): """ python -m utool SetupRepo.main --modname=sklearn --repo=scikit-learn --codedir=~/code -w python -m utool SetupRepo.main --repo=ubelt --codedir=~/code --modname=ubelt -w Example: >>> # SCRIPT >>> from utool.util_project import * # NOQA >>> SetupRepo().main() """ self.regencmd = self.regenfmt.format(cmd='main', **self.__dict__) import utool as ut self.ensure_text( fname=join(self.modname, '__main__.py'), chmod='+x', text=ut.codeblock( r''' # STARTBLOCK #!/usr/bin/env python # -*- coding: utf-8 -*- """ Initially Generated By: {regencmd} """ from __future__ import absolute_import, division, print_function, unicode_literals def {modname}_main(): ignore_prefix = [] ignore_suffix = [] import utool as ut ut.main_function_tester('{modname}', ignore_prefix, ignore_suffix) if __name__ == '__main__': """ Usage: python -m {modname} <funcname> """ print('Running {modname} main') {modname}_main() # ENDBLOCK ''' ) )
def make_default_module_maintest(modname): """ make_default_module_maintest TODO: use path relative to home dir if the file is a script Args: modname (str): module name Returns: str: text References: http://legacy.python.org/dev/peps/pep-0338/ Example: >>> # ENABLE_DOCTEST >>> from utool.util_autogen import * # NOQA >>> modname = 'utool.util_autogen' >>> text = make_default_module_maintest(modname) >>> result = str(text) >>> print(result) """ import utool as ut # Need to use python -m to run a module # otherwise their could be odd platform specific errors. #python -c "import utool, {modname}; # utool.doctest_funcs({modname}, allexamples=True)" text = ut.codeblock( ''' if __name__ == '__main__': """ CommandLine: python -m {modname} python -m {modname} --allexamples python -m {modname} --allexamples --noface --nosrc """ import multiprocessing multiprocessing.freeze_support() # for win32 import utool as ut # NOQA ut.doctest_funcs() ''' ).format(modname=modname) return text
def make_autogen_str(): r""" Returns: str: CommandLine: python -m utool.util_ipynb --exec-make_autogen_str --show Example: >>> # DISABLE_DOCTEST >>> from utool.util_ipynb import * # NOQA >>> import utool as ut >>> result = make_autogen_str() >>> print(result) """ import utool as ut import sys def get_regen_cmd(): # TODO: move to utool try: if len(sys.argv) > 0 and ut.checkpath(sys.argv[0]): # Check if running python command if ut.is_python_module(sys.argv[0]): python_exe = ut.python_executable(check=False) modname = ut.get_modname_from_modpath(sys.argv[0]) new_argv = [python_exe, '-m', modname] + sys.argv[1:] return ' '.join(new_argv) except Exception as ex: ut.printex(ex, iswarning=True) return ' '.join(sys.argv) autogenkw = dict( stamp=ut.timestamp('printable'), regen_cmd=get_regen_cmd() #' '.join(sys.argv) ) return ut.codeblock( ''' # Autogenerated on {stamp} # Regen Command: # {regen_cmd} # ''' ).format(**autogenkw)
def vector_stats(vectors, name, verbose=True): line_list = [] line_list.append('+--- Vector Stats --') line_list.append(' * vectors = %r' % name) key_list = ut.codeblock(''' vectors.shape vectors.dtype vectors.max() vectors.min() ''').split('\n') strlist_ = ut.parse_locals_keylist(locals(), key_list) line_list.extend(strlist_) #line_list.append(vectors) line_list.append('L--- Vector Stats --') statstr = '\n'.join(line_list) if verbose: print(statstr) return statstr
def makeinit(module_path, exclude_modnames=[]): #module_name = basename(module_path) module_name = ut.get_modname_from_modpath(module_path) IMPORT_TUPLES = util_importer.make_import_tuples(module_path, exclude_modnames=exclude_modnames) initstr = util_importer.make_initstr(module_name, IMPORT_TUPLES) regen_command = 'cd %s\n' % (module_path) regen_command += ' makeinit.py' if len(exclude_modnames ) > 0: regen_command += ' -x ' + ' '.join(exclude_modnames) regen_block = (ut.codeblock(''' """ Regen Command: {regen_command} """ ''').format(regen_command=regen_command)) print('### __init__.py ###') print(initstr) print('\nIMPORT_TUPLES = ' + ut.list_str(IMPORT_TUPLES)) print(regen_block)
def get_short_infostr(ibs): """ Returns printable database information Args: ibs (IBEISController): wbia controller object Returns: str: infostr CommandLine: python -m wbia.other.dbinfo --test-get_short_infostr Example: >>> # ENABLE_DOCTEST >>> from wbia.other.dbinfo import * # NOQA >>> import wbia >>> ibs = wbia.opendb('testdb1') >>> infostr = get_short_infostr(ibs) >>> result = str(infostr) >>> print(result) dbname = 'testdb1' num_images = 13 num_annotations = 13 num_names = 7 """ dbname = ibs.get_dbname() # workdir = ut.unixpath(ibs.get_workdir()) num_images = ibs.get_num_images() num_annotations = ibs.get_num_annotations() num_names = ibs.get_num_names() # workdir = %r infostr = ut.codeblock( """ dbname = %s num_images = %r num_annotations = %r num_names = %r """ % (ut.repr2(dbname), num_images, num_annotations, num_names) ) return infostr
def vector_stats(vectors, name, verbose=True): line_list = [] line_list.append('+--- Vector Stats --') line_list.append(' * vectors = %r' % name) key_list = ut.codeblock( ''' vectors.shape vectors.dtype vectors.max() vectors.min() ''' ).split('\n') strlist_ = ut.parse_locals_keylist(locals(), key_list) line_list.extend(strlist_) #line_list.append(vectors) line_list.append('L--- Vector Stats --') statstr = '\n'.join(line_list) if verbose: print(statstr) return statstr
def ensure_wb_mysql(): r""" CommandLine: python -m ibeis ensure_wb_mysql Example: >>> # SCRIPT >>> from ibeis.control.wildbook_manager import * # NOQA >>> result = ensure_wb_mysql() """ print('Execute the following code to install mysql') print( ut.codeblock(r''' # STARTBLOCK bash # Install sudo apt-get install mysql-server-5.6 -y sudo apt-get install mysql-common-5.6 -y sudo apt-get install mysql-client-5.6 -y mysql_config_editor set --login-path=local --host=localhost --user=root --password # Initialize mysql --login-path=local -e "create user 'ibeiswb'@'localhost' identified by 'somepassword';" mysql --login-path=local -e "create database ibeiswbtestdb;" mysql --login-path=local -e "grant all privileges on ibeiswbtestdb.* to 'ibeiswb'@'localhost';" # Reset mysql --login-path=local -e "drop database ibeiswbtestdb" mysql --login-path=local -e "create database ibeiswbtestdb" mysql --login-path=local -e "grant all privileges on ibeiswbtestdb.* to 'ibeiswb'@'localhost'" # Check if running mysqladmin --login-path=local status # mysql --login-path=local -e "status" # mysql -u root -proot status # mysql -u root -proot # ENDBLOCK bash '''))
def ensure_wb_mysql(): r""" CommandLine: python -m ibeis ensure_wb_mysql Example: >>> # SCRIPT >>> from ibeis.control.wildbook_manager import * # NOQA >>> result = ensure_wb_mysql() """ print('Execute the following code to install mysql') print(ut.codeblock( r''' # STARTBLOCK bash # Install sudo apt-get install mysql-server-5.6 -y sudo apt-get install mysql-common-5.6 -y sudo apt-get install mysql-client-5.6 -y mysql_config_editor set --login-path=local --host=localhost --user=root --password # Initialize mysql --login-path=local -e "create user 'ibeiswb'@'localhost' identified by 'somepassword';" mysql --login-path=local -e "create database ibeiswbtestdb;" mysql --login-path=local -e "grant all privileges on ibeiswbtestdb.* to 'ibeiswb'@'localhost';" # Reset mysql --login-path=local -e "drop database ibeiswbtestdb" mysql --login-path=local -e "create database ibeiswbtestdb" mysql --login-path=local -e "grant all privileges on ibeiswbtestdb.* to 'ibeiswb'@'localhost'" # Check if running mysqladmin --login-path=local status # mysql --login-path=local -e "status" # mysql -u root -proot status # mysql -u root -proot # ENDBLOCK bash '''))
def make_ibeis_cell_list(ibs): cell_template_list = get_default_cell_template_list(ibs) autogen_str = ut.make_autogen_str() dbname = ibs.get_dbname() default_acfgstr = ut.get_argval('-a', type_=str, default='default:is_known=True') asreport = ut.get_argflag('--asreport') default_pcfgstr_list = ut.get_argval(('-t', '-p'), type_=list, default='default') default_pcfgstr = ut.repr3(default_pcfgstr_list, nobr=True) if asreport: annotconfig_list_body = ut.codeblock( ut.repr2(default_acfgstr) ) pipeline_list_body = ut.codeblock( default_pcfgstr ) else: annotconfig_list_body = ut.codeblock( ut.repr2(default_acfgstr) + '\n' + ut.codeblock(''' # See ibeis/expt/annotation_configs.py for names of annot configuration options #'default:has_any=(query,),dpername=1,exclude_reference=True', #'default:is_known=True', #'default:qsame_imageset=True,been_adjusted=True,excluderef=True,qsize=10,dsize=20', #'default:require_timestamp=True,min_timedelta=3600', #'default:species=primary', #'timectrl:', #'unctrl:been_adjusted=True', ''') ) pipeline_list_body = ut.codeblock( default_pcfgstr + '\n' + ut.codeblock(''' #'default', #'default:K=1,AI=False,QRH=True', #'default:K=1,RI=True,AI=False', #'default:K=1,adapteq=True', #'default:fg_on=[True,False]', ''') ) locals_ = locals() _format = partial(ut.format_cells, locals_=locals_) cell_list = ut.flatten(map(_format, cell_template_list)) return cell_list
def get_short_infostr(ibs): """ Returns printable database information Args: ibs (IBEISController): ibeis controller object Returns: str: infostr CommandLine: python -m ibeis.other.dbinfo --test-get_short_infostr Example: >>> # ENABLE_DOCTEST >>> from ibeis.other.dbinfo import * # NOQA >>> import ibeis >>> ibs = ibeis.opendb('testdb1') >>> infostr = get_short_infostr(ibs) >>> result = str(infostr) >>> print(result) dbname = 'testdb1' num_images = 13 num_annotations = 13 num_names = 7 """ dbname = ibs.get_dbname() #workdir = ut.unixpath(ibs.get_workdir()) num_images = ibs.get_num_images() num_annotations = ibs.get_num_annotations() num_names = ibs.get_num_names() #workdir = %r infostr = ut.codeblock(''' dbname = %s num_images = %r num_annotations = %r num_names = %r ''' % (ut.repr2(dbname), num_images, num_annotations, num_names)) return infostr
def test_libzoter(): zotero = get_libzotero() item_list = zotero.search('') for item in item_list: print(item.title) pass if False: #set(ut.flatten([dir(x) for x in item_list])) item_list = zotero.search('Combining Face with Face-Part Detectors under Gaussian Assumption') [x.simple_format() for x in item_list] item_list = zotero.search('Lowe') if False: import mozrepl repl = mozrepl.Mozrepl(4242, u'localhost') # NOQA temp_fpath = 'foo.txt' repl.connect(4242, u'localhost') r""" http://www.curiousjason.com/zoterotobibtex.html https://github.com/bard/mozrepl/wiki "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -profile "C:\Users\joncrall\AppData\Roaming\Mozilla\Firefox\Profiles\7kadig32.default" -repl 4242 telnet localhost 4242 """ execute_string = unicode(ut.codeblock( r''' filename = '%s'; var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); file.initWithPath(filename); var zotero = Components.classes['@zotero.org/Zotero;1'].getService(Components.interfaces.nsISupports).wrappedJSObject; var translatorObj = new Zotero.Translate('export'); translatorObj.setLocation(file); translatorObj.setTranslator('9cb70025-a888-4a29-a210-93ec52da40d4'); translatorObj.translate(); ''') % (temp_fpath)) print(execute_string) repl.execute(execute_string)
def gen_dbranks_tables(): r""" CommandLine: python -m ibeis.scripts.gen_cand_expts --exec-gen_dbranks_tables Example: >>> # SCRIPT >>> from ibeis.scripts.gen_cand_expts import * # NOQA >>> result = gen_dbranks_tables() >>> print(result) """ tex_file = ut.codeblock( # NOQA r''' \begin{comment} python -c "import utool as ut; ut.write_modscript_alias('ExptPrint.sh', 'ibeis.expt.experiment_printres --exec-print_latexsum')" python -c "import utool as ut; ut.write_modscript_alias('DrawRanks.sh', 'python -m ibeis.expt.experiment_drawing --exec-draw_rank_cdf')" \end{comment} ''') #gen_table_line = #sh ExptPrint.sh -t candidacy_baseline --allgt --species=primary --db #GZ_ALL --rank-lt-list=1,5,10,100 pass
def gen_dbranks_tables(): r""" CommandLine: python -m ibeis.scripts.gen_cand_expts --exec-gen_dbranks_tables Example: >>> # SCRIPT >>> from ibeis.scripts.gen_cand_expts import * # NOQA >>> result = gen_dbranks_tables() >>> print(result) """ tex_file = ut.codeblock( # NOQA r''' \begin{comment} python -c "import utool as ut; ut.write_modscript_alias('ExptPrint.sh', 'ibeis.expt.experiment_printres --exec-print_latexsum')" python -c "import utool as ut; ut.write_modscript_alias('DrawRanks.sh', 'python -m ibeis.expt.experiment_drawing --exec-draw_rank_cmc')" \end{comment} ''') #gen_table_line = #sh ExptPrint.sh -t candidacy_baseline --allgt --species=primary --db #GZ_ALL --rank-lt-list=1,5,10,100 pass
def _debug_repr_cpd(cpd): import re import utool as ut code_fmt = ut.codeblock(""" {variable} = pgmpy.factors.TabularCPD( variable={variable_repr}, variable_card={variable_card_repr}, values={get_cpd_repr}, evidence={evidence_repr}, evidence_card={evidence_card_repr}, ) """) keys = ['variable', 'variable_card', 'values', 'evidence', 'evidence_card'] dict_ = ut.odict(zip(keys, [getattr(cpd, key) for key in keys])) # HACK dict_['values'] = cpd.get_cpd() r = ut.repr2(dict_, explicit=True, nobraces=True, nl=True) logger.info(r) # Parse props that are needed for this fmtstr fmt_keys = [ match.groups()[0] for match in re.finditer('{(.*?)}', code_fmt) ] need_reprs = [key[:-5] for key in fmt_keys if key.endswith('_repr')] need_keys = [key for key in fmt_keys if not key.endswith('_repr')] # Get corresponding props # Call methods if needbe tmp = [(prop, getattr(cpd, prop)) for prop in need_reprs] tmp = [(x, y()) if ut.is_funclike(y) else (x, y) for (x, y) in tmp] fmtdict = dict(tmp) fmtdict = ut.map_dict_vals(ut.repr2, fmtdict) fmtdict = ut.map_dict_keys(lambda x: x + '_repr', fmtdict) tmp2 = [(prop, getattr(cpd, prop)) for prop in need_keys] fmtdict.update(dict(tmp2)) code = code_fmt.format(**fmtdict) return code
def on_nontrivial_merge(ibs, aid1, aid2): MERGE_NEEDS_INTERACTION = False MERGE_NEEDS_VERIFICATION = True if MERGE_NEEDS_INTERACTION: raise guiexcept.NeedsUserInput('confirm merge') elif MERGE_NEEDS_VERIFICATION: name1, name2 = ibs.get_annot_names([aid1, aid2]) aid1_and_groundtruth = ibs.get_annot_groundtruth(aid1, noself=False) aid2_and_groundtruth = ibs.get_annot_groundtruth(aid2, noself=False) msgfmt = ut.codeblock(""" Confirm merge of animal {name1} and {name2} {name1} has {num_gt1} annotations {name2} has {num_gt2} annotations """) msg = msgfmt.format( name1=name1, name2=name2, num_gt1=len(aid1_and_groundtruth), num_gt2=len(aid2_and_groundtruth), ) if not gt.are_you_sure(parent=None, msg=msg, default='Yes'): raise guiexcept.UserCancel('canceled merge')
def _assert_nodes(self, name=None): if False and self.root is not None: if self.root.parent is not None: treestr = self.get_ascii_tree() msg = ut.codeblock( r''' Root cannot have a parent. name = {} root = {} root.parent = {} '''.format(name, self.root, self.root.parent) ) msg = msg + '\n' + treestr raise AssertionError(msg) assert self.root.parent is None, 'must be root' for count, node in enumerate(self._traverse_nodes()): if node.left: assert node.left.parent is node, 'left child problem, %d' % count if node.right: assert node.right.parent is node, 'right child problem, %d' % count if node.parent: assert node in node.parent.kids, 'parent problem, %d' % count if name: print('Nodes in {} are ok'.format(name))
def interactive_commandline_prompt(msg, decisiontype): prompt_fmtstr = ut.codeblock( ''' Accept system {decisiontype} decision? ========== {msg} ========== * press ENTER to ACCEPT * enter {no_phrase} to REJECT * enter {embed_phrase} to embed into ipython * any other inputs ACCEPT system decision * (input is case insensitive) ''' ) ans_list_embed = ['cmd', 'ipy', 'embed'] ans_list_no = ['no', 'n'] #ans_list_yes = ['yes', 'y'] prompt_str = prompt_fmtstr.format( no_phrase=ut.conj_phrase(ans_list_no), embed_phrase=ut.conj_phrase(ans_list_embed), msg=msg, decisiontype=decisiontype, ) prompt_block = ut.msgblock('USER_INPUT', prompt_str) ans = input(prompt_block).lower() if ans in ans_list_embed: ut.embed() #print(ibs2.get_dbinfo_str()) #qreq_ = ut.search_stack_for_localvar('qreq_') #qreq_.normalizer elif ans in ans_list_no: return False else: return True