References:
        https://github.com/paulgb/runipy
        >>> from utool.util_ipynb import *  # NOQA
961959
def run_ipython_notebook(notebook_str):
    
    from runipy.notebook_runner import NotebookRunner
    import nbformat
    import logging
    log_format = '%(asctime)s %(levelname)s: %(message)s'
    log_datefmt = '%m/%d/%Y %I:%M:%S %p'
    logging.basicConfig(
        level=logging.INFO, format=log_format, datefmt=log_datefmt
    )
    #fpath = 'tmp.ipynb'
    #notebook_str = ut.readfrom(fpath)
    #nb3 = IPython.nbformat.reads(notebook_str, 3)
    #cell = nb4.cells[1]
    #self = runner
    #runner = NotebookRunner(nb3, mpl_inline=True)
    print('Executing IPython notebook')
    nb4 = nbformat.reads(notebook_str, 4)
    runner = NotebookRunner(nb4)
    runner.run_notebook(skip_exceptions=False)
    run_nb = runner.nb
    return run_nb
368469
function getIndexFile(answers) {
    const content = format(`
        import { create } from 'rung-sdk';
        import { String as Text } from 'rung-cli/dist/types';

        function render(name) {
            return <b>{ _('Hello {{name}}', { name }) }</b>;
        }

        function main(context) {
            const { name } = context.params;
            return {
                alerts: [{
                    title: _('Welcome'),
                    content: render(name),
                    resources: []
                }]
            };
        }

        const params = {
            name: {
                description: _('What is your name?'),
                type: Text
            }
        };

        export default create(main, {
            params,
            primaryKey: true,
            title: _(${JSON.stringify(answers.title)}),
            description: _(${JSON.stringify(answers.description)}),
            preview: render('Trixie')
        });
    `);

    return { filename: path.join(answers.name, 'index.js'), content };
}
1285601
def map_init(interface, params):
    """"""
    import numpy as np
    import random
    np.random.seed(params['seed'])
    random.seed(params['seed'])
    return params
1134672
def init(arg):
    

    answers = {'a': 1}
    if arg == 'interactive':
        input("""Hi There!
    DataFlows will now bootstrap a data processing flow based on your needs.

    Press any key to start...
    """)

    elif arg == 'hello-world':
        raise NotImplementedError()
    else:
        url = arg
        answers = dict(
            input='remote',
            title=os.path.basename(url),
            input_url=url,
            processing=[],
            output='print_n_pkg'
        )
        extract_format(answers, url)

    questions = [
        # Input
        inquirer.List('input_str',
                    message='What is the source of your data?',
                    choices=INPUTS.keys(),
                    ignore=lambda ctx: ctx.get('input') is not None,
                    validate=convert_input),

        # Input Parameters
        inquirer.Text('input_url',
                    message="What is the path of that file",
                    ignore=fany(lambda ctx: ctx.get('input') != 'file',
                                lambda ctx: ctx.get('input_url') is not None),
                    validate=fall(not_empty, extract_format)),
        inquirer.List('format',
                    message="We couldn't detect the file format - which is it?",
                    choices=FORMATS[:-1],
                    ignore=fany(lambda ctx: ctx.get('input') != 'file',
                                lambda ctx: ctx.get('format') in FORMATS)),

        inquirer.Text('input_url',
                    message="Where is that file located (URL)",
                    ignore=fany(lambda ctx: ctx.get('input') != 'remote',
                                lambda ctx: ctx.get('input_url') is not None),
                    validate=fall(extract_format, not_empty, valid_url)),
        inquirer.List('format',
                      message="We couldn't detect the source format - which is it",
                      choices=FORMATS,
                      ignore=fany(lambda ctx: ctx['input'] != 'remote',
                                  lambda ctx: ctx.get('format') in FORMATS)
                      ),
        inquirer.Text('sheet',
                      message="Which sheet in the spreadsheet should be processed (name or index)",
                      validate=not_empty,
                      ignore=lambda ctx: ctx.get('format') not in ('xls', 'xlsx', 'ods'),
                      ),
        inquirer.Text('input_url',
                      message="What is the connection string to the database",
                      validate=not_empty,
                      ignore=fany(lambda ctx: ctx['input'] != 'sql',
                                  lambda ctx: ctx.get('input_url') is not None),
                      ),
        inquirer.Text('input_db_table',
                      message="...and the name of the database table to extract",
                      validate=not_empty,
                      ignore=fany(lambda ctx: ctx['input'] != 'sql',
                                  lambda ctx: ctx.get('input_db_table') is not None),
                      ),

        inquirer.Text('input_url',
                      message="Describe that other source (shortly)",
                      ignore=fany(lambda ctx: ctx['input'] != 'other',
                                  lambda ctx: ctx.get('input_url') is not None),
                      ),

        # Processing
        inquirer.Checkbox('processing_str',
                        message="What kind of processing would you like to run on the data",
                        choices=PROCESSING.keys(),
                        ignore=lambda ctx: ctx.get('processing') is not None,
                        validate=convert_processing),

        # Output
        inquirer.List('output_str',
                    message="Finally, where would you like the output data",
                    choices=OUTPUTS.keys(),
                    ignore=lambda ctx: ctx.get('output') is not None,
                    validate=convert_output),
        inquirer.Text('output_url',
                      message="What is the connection string to the database",
                      validate=not_empty,
                      ignore=fany(lambda ctx: ctx['output'] != 'sql',
                                  lambda ctx: ctx.get('output_url') is not None),
                      ),
        inquirer.Text('output_db_table',
                      message="...and the name of the database table to write to",
                      validate=not_empty,
                      ignore=fany(lambda ctx: ctx['output'] != 'sql',
                                  lambda ctx: ctx.get('output_db_table') is not None),
                      ),

        # # Finalize
        inquirer.Text('title',
                    message="That's it! Now, just provide a title for your processing flow",
                    ignore=lambda ctx: ctx.get('title') is not None,
                    validate=not_empty),
    ]
    answers = inquirer.prompt(questions, answers=answers, theme=themes.GreenPassion())
    if answers is None:
        return
    answers['slug'] = slugify.slugify(answers['title'], separator='_')

    filename = '{slug}.py'.format(**answers)
    with open(filename, 'w') as out:
        print('Writing processing code into {}'.format(filename))
        out.write(render(answers))

    try:
        print('Running {}'.format(filename))
        ret = subprocess.check_output('python '+filename,
                                      stderr=subprocess.PIPE, universal_newlines=True, shell=True)
        print(ret)
        print('Done!')
    except subprocess.CalledProcessError as e:
        print("Processing failed, here's the error:")
        print(e.stderr)
        answers = inquirer.prompt([
            inquirer.Confirm('edit',
                            message='Would you like to open {} in the default editor?'.format(filename),
                            default=False)
        ])
        if answers['edit']:
            click.edit(filename=filename)
470858
def shell(script=None, args=()):
    
    if script:
        sys.argv = sys.argv[2:]  # strip ['oq', 'shell']
        runpy.run_path(script, run_name='__main__')
        return
    o = OpenQuake()  # noqa
    try:
        import IPython
        IPython.embed(banner1='IPython shell with a global object "o"')
    except ImportError:
        import code
        code.interact(banner='Python shell with a global object "o"',
                      local=dict(o=o))
1240019
def init(scope):
    
    # define wrapper compatibility symbols
    QtCore.THREADSAFE_NONE = XThreadNone()
    QtGui.QDialog = QDialog
    
    # define the importable symbols
    scope['QtCore'] = QtCore
    scope['QtGui'] = QtGui
    scope['QtWebKit'] = lazy_import('PySide.QtWebKit')
    scope['QtNetwork'] = lazy_import('PySide.QtNetwork')
    scope['QtXml'] = lazy_import('PySide.QtXml')
    
    scope['uic'] = Uic()
    scope['rcc_exe'] = 'pyside-rcc'
    
    # map overrides
    #QtCore.SIGNAL = SIGNAL
    
    # map shared core properties
    QtCore.QDate.toPyDate = lambda x: x.toPython()
    QtCore.QDateTime.toPyDateTime = lambda x: x.toPython()
    QtCore.QTime.toPyTime = lambda x: x.toPython()
    QtCore.QStringList = list
    QtCore.QString = unicode
479468
def embed(parent_locals=None, parent_globals=None, exec_lines=None,
          remove_pyqt_hook=True, N=0):
    
    import utool as ut
    from functools import partial
    import IPython

    if parent_globals is None:
        parent_globals = get_parent_frame(N=N).f_globals
    if parent_locals is None:
        parent_locals = get_parent_frame(N=N).f_locals

    stackdepth = N  # NOQA
    getframe = partial(ut.get_parent_frame, N=N)  # NOQA

    # exec(execstr_dict(parent_globals, 'parent_globals'))
    # exec(execstr_dict(parent_locals,  'parent_locals'))
    print('')
    print('================')
    print(ut.bubbletext('EMBEDDING'))
    print('================')
    print('[util] embedding')
    try:
        if remove_pyqt_hook:
            try:
                import guitool
                guitool.remove_pyqt_input_hook()
            except (ImportError, ValueError, AttributeError) as ex:
                #print(ex)
                printex(ex, iswarning=True)
                pass
            # make qt not loop forever (I had qflag loop forever with this off)
    except ImportError as ex:
        print(ex)

    #from IPython.config.loader import Config
    # cfg = Config()
    #config_dict = {}
    #if exec_lines is not None:
    #    config_dict['exec_lines'] = exec_lines
    #IPython.embed(**config_dict)
    print('[util]  Get stack location with: ')
    print('[util] ut.get_parent_frame(N=8).f_code.co_name')
    print('[util] set EXIT_NOW or qqq to True(ish) to hard exit on unembed')
    #print('set iup to True to draw plottool stuff')
    print('[util] call %pylab qt4 to get plottool stuff working')
    once = True
    # Allow user to set iup and redo the loop
    while once or vars().get('iup', False):
        if not once:
            # SUPER HACKY WAY OF GETTING FIGURES ON THE SCREEN BETWEEN UPDATES
            #vars()['iup'] = False
            # ALL YOU NEED TO DO IS %pylab qt4
            print('re-emebeding')
            #import plottool as pt
            #pt.update()
            #(pt.present())
            for _ in range(100):
                time.sleep(.01)

        once = False
        #vars().get('iup', False):
        print('[util] calling IPython.embed()')
        """
        Notes:
            /usr/local/lib/python2.7/dist-packages/IPython/terminal/embed.py
            IPython.terminal.embed.InteractiveShellEmbed

            # instance comes from  IPython.config.configurable.SingletonConfigurable.instance
        """
        #c = IPython.Config()
        #c.InteractiveShellApp.exec_lines = [
        #    '%pylab qt4',
        #    '%gui qt4',
        #    "print 'System Ready!'",
        #]
        #IPython.embed(config=c)
        parent_ns = parent_globals.copy()
        parent_ns.update(parent_locals)
        locals().update(parent_ns)
        try:
            IPython.embed()
        except RuntimeError as ex:
            ut.printex(ex, 'Failed to open ipython')
        #config = IPython.terminal.ipapp.load_default_config()
        #config.InteractiveShellEmbed = config.TerminalInteractiveShell
        #module = sys.modules[parent_globals['__name__']]
        #config['module'] = module
        #config['module'] = module
        #embed2(stack_depth=N + 2 + 1)
        #IPython.embed(config=config)
        #IPython.embed(config=config)
        #IPython.embed(module=module)
        # Exit python immediately if specifed
        if vars().get('EXIT_NOW', False) or vars().get('qqq', False):
            print('[utool.embed] EXIT_NOW specified')
            sys.exit(1)
805065
function Buildit$run() {
		var externals, locals, define, out, patterns, walked, that = this;

		define = invert(this.define);
		externals = [];
		locals = {};
		patterns = [];

		each(this.exports, function(filepath, name) {
			var fullpath;

			fullpath = path.join(that.cwd, filepath + '.js');

			patterns.push(fullpath);
		});

		each(this.extensions, function(filepath) {
			var fullpath;

			fullpath = path.join(that.cwd, filepath + '.js');

			patterns.push(fullpath);
		});

		each(this.define, function(filepath, name) {
			var fullpath;

			fullpath = path.join(that.cwd, filepath + '.js');

			patterns.push(fullpath);
		});

		walked = walk(patterns, this.cwd);

		Array.prototype.push.apply(externals, walked.externals);
		unique(externals);

		each(walked.locals, function(file, name) {
			locals[name] = file;
		});

		each(this.define, function(filepath, name) {
			if (!locals.hasOwnProperty(filepath)) {
				throw new Error('Module "' + filepath + '" not found');
			}
		});

		each(this.exports, function(filepath, name) {
			if (!locals.hasOwnProperty(filepath)) {
				throw new Error('Module "' + filepath + '" not found');
			}
		});

		each(this.extensions, function(filepath) {
			if (!locals.hasOwnProperty(filepath)) {
				throw new Error('Module "' + filepath + '" not found');
			}
		});

		out = '';
		out += print.header(this.name, externals);
		out += print.library(externals);
		out += print.mappings(locals, this.cwd);
		out += print.machinery();

		each(locals, function(file, name) {
			out += print.file(name, file.source) + (define[name] ? print.define(define[name], name) : '');
		});

		each(this.extensions, function(filepath) {
			out += print.extension(filepath);
		});

		out += print.exports(this.exports);
		out += print.footer();

		return out;
	}
488708
def runnable(args):
    
    w = fapi.get_workspace(args.project, args.workspace)
    fapi._check_response_code(w, 200)
    workspace_d = w.json()

    if args.config and args.namespace and not args.entity:
        # See what entities I can run on with this config
        r = fapi.validate_config(args.project, args.workspace, args.namespace,
                                                                    args.config)
        fapi._check_response_code(r, 200)
        config_d = r.json()



        # First validate without any sample sets
        errs = sum(_validate_helper(args, config_d, workspace_d, None), [])
        if errs:
            print("Configuration contains invalid expressions")
            return 1

        # Now get  all the possible entities, and evaluate each
        entity_type = config_d['methodConfiguration']['rootEntityType']
        ent_r = fapi.get_entities(args.project, args.workspace, entity_type)
        fapi._check_response_code(r, 200)
        entities = ent_r.json()

        can_run_on    = []
        cannot_run_on = []

        # Validate every entity
        for entity_d in entities:
            # If there are errors in the validation
            if sum(_validate_helper(args, config_d, workspace_d, entity_d), []):
                cannot_run_on.append(entity_d['name'])
            else:
                can_run_on.append(entity_d['name'])

        # Print what can be run
        if can_run_on:
            print("{0} CAN be run on {1} {2}(s):".format(args.config, len(can_run_on), entity_type))
            print("\n".join(can_run_on)+"\n")

        print("{0} CANNOT be run on {1} {2}(s)".format(args.config, len(cannot_run_on), entity_type))
            #print("\n".join(cannot_run_on))

    # See what method configs are possible for the given sample set
    elif args.entity and args.entity_type and not args.config:
        entity_r = fapi.get_entity(args.project, args.workspace,
                                   args.entity_type, args.entity)
        fapi._check_response_code(entity_r, [200,404])
        if entity_r.status_code == 404:
            print("Error: No {0} named '{1}'".format(args.entity_type, args.entity))
            return 2
        entity_d = entity_r.json()

        # Now get all the method configs in the workspace
        conf_r = fapi.list_workspace_configs(args.project, args.workspace)
        fapi._check_response_code(conf_r, 200)

        # Iterate over configs in the workspace, and validate against them
        for cfg in conf_r.json():
            # If we limit search to a particular namespace, skip ones that don't match
            if args.namespace and cfg['namespace'] != args.namespace:
                continue

            # But we have to get the full description
            r = fapi.validate_config(args.project, args.workspace,
                                    cfg['namespace'], cfg['name'])
            fapi._check_response_code(r, [200, 404])
            if r.status_code == 404:
                # Permission error, continue
                continue
            config_d = r.json()
            errs = sum(_validate_helper(args, config_d, workspace_d, entity_d),[])
            if not errs:
                print(cfg['namespace'] + "/" + cfg['name'])

    elif args.entity_type:
        # Last mode, build a matrix of everything based on the entity type
        # Get all of the entity_type
        ent_r = fapi.get_entities(args.project, args.workspace, args.entity_type)
        fapi._check_response_code(ent_r, 200)
        entities = ent_r.json()
        entity_names = sorted(e['name'] for e in entities)

        conf_r = fapi.list_workspace_configs(args.project, args.workspace)
        fapi._check_response_code(conf_r, 200)
        conf_list = conf_r.json()
        config_names = sorted(c['namespace'] + '/' + c['name'] for c in conf_list)
        mat = {c:dict() for c in config_names}

        # Now iterate over configs, building up the matrix
        # Iterate over configs in the workspace, and validate against them
        for cfg in conf_list:

            # If we limit search to a particular namespace, skip ones that don't match
            if args.namespace and cfg['namespace'] != args.namespace:
                continue
            # But we have to get the full description
            r = fapi.validate_config(args.project, args.workspace,
                                    cfg['namespace'], cfg['name'])
            fapi._check_response_code(r, [200, 404])
            if r.status_code == 404:
                # Permission error, continue
                continue
            config_d = r.json()

            # Validate against every entity
            for entity_d in entities:
                errs = sum(_validate_helper(args, config_d, workspace_d, entity_d),[])
                #TODO: True/False? Y/N?
                symbol = "X" if not errs else ""
                cfg_name = cfg['namespace'] + '/' + cfg['name']
                mat[cfg_name][entity_d['name']] = symbol

        # Now print the validation matrix
        # headers
        print("Namespace/Method Config\t" + "\t".join(entity_names))
        for conf in config_names:
            print(conf + "\t" + "\t".join(mat[conf][e] for e in entity_names))


    else:
        print("runnable requires a namespace+configuration or entity type")
        return 1
440662
def do(cmdline=None, runas=None):
    '''
    
    '''
    path = _pyenv_path(runas)
    cmd_split = cmdline.split()
    quoted_line = ''
    for cmd in cmd_split:
        quoted_line = quoted_line + ' ' + _cmd_quote(cmd)
    result = __salt__['cmd.run_all'](
        'env PATH={0}/shims:$PATH {1}'.format(_cmd_quote(path), quoted_line),
        runas=runas,
        python_shell=True
    )

    if result['retcode'] == 0:
        rehash(runas=runas)
        return result['stdout']
    else:
        return False
820644
def _u2i(uint32):
    
    v = u2i(uint32)
    if v < 0:
        if exceptions:
            raise ApigpioError(error_text(v))
    return v
445820
def load_nb(cls, inline=True):
        
        import matplotlib.pyplot as plt
        backend = plt.get_backend()
        if backend not in ['agg', 'module://ipykernel.pylab.backend_inline']:
            plt.switch_backend('agg')
271875
def pseudo_import(fname):
  
  is_d_import = lambda n: isinstance(n, ast.Name) and n.id == "__import__"
  is_assign = lambda n: isinstance(n, ast.Assign)
  is_valid = lambda n: is_assign(n) and not any(map(is_d_import, ast.walk(n)))
  with open(fname, "r") as f:
    astree = ast.parse(f.read(), filename=fname)
  astree.body = [node for node in astree.body if is_valid(node)]
  return locals_from_exec(compile(astree, fname, mode="exec"))
202047
def orientnii(imfile):
    ''''''
    strorient = ['L-R', 'S-I', 'A-P']
    niiorient = []
    niixyz = np.zeros(3,dtype=np.int8)
    if os.path.isfile(imfile):
        nim = nib.load(imfile)
        pct = nim.get_data()
        A = nim.get_sform()
        for i in range(3):
            niixyz[i] = np.argmax(abs(A[i,:-1]))
            niiorient.append( strorient[ niixyz[i] ] )
        print niiorient
910802
def run(self, tool, args, new_import_path=''):
        
        if new_import_path != '':
            #print('APPENDING PATH = ', new_import_path)
            sys.path.append(new_import_path)
        
        #if silent == 'N':
        print('main called ' + tool['file'] + '->' + tool['function'] + ' with ', args, ' = ', tool['return'])
        mod = __import__( os.path.basename(tool['file']).split('.')[0]) # for absolute folder names
        # mod = __import__( tool['file'][:-2]) # for aikif folders (doesnt work)
        func = getattr(mod, tool['function'])
        tool['return'] = func(args)
        return tool['return']
497797
def check_ns_run_members(run):
    
    run_keys = list(run.keys())
    # Mandatory keys
    for key in ['logl', 'nlive_array', 'theta', 'thread_labels',
                'thread_min_max']:
        assert key in run_keys
        run_keys.remove(key)
    # Optional keys
    for key in ['output']:
        try:
            run_keys.remove(key)
        except ValueError:
            pass
    # Check for unexpected keys
    assert not run_keys, 'Unexpected keys in ns_run: ' + str(run_keys)
    # Check type of mandatory members
    for key in ['logl', 'nlive_array', 'theta', 'thread_labels',
                'thread_min_max']:
        assert isinstance(run[key], np.ndarray), (
            key + ' is type ' + type(run[key]).__name__)
    # check shapes of keys
    assert run['logl'].ndim == 1
    assert run['logl'].shape == run['nlive_array'].shape
    assert run['logl'].shape == run['thread_labels'].shape
    assert run['theta'].ndim == 2
    assert run['logl'].shape[0] == run['theta'].shape[0]
966991
def main(args):

  
  sys.stderr.write("reading reference genome\n")
  ref = FASTAData(open(args.genome).read())
  sys.stderr.write("read in "+str(len(ref.keys()))+" chromosomes\n")

  """ Next make the transcriptome """
  txome = {}
  sys.stderr.write("write the transcriptome\n")
  inf = None
  if args.gpd[-3:] == '.gz':
    inf = gzip.open(args.gpd)
  else:
    inf = open(args.gpd)
  stream = GPDStream(inf)
  tof = open(args.tempdir+'/transcriptome.fa','w')
  z = 0
  for gpd in stream:
    z += 1
    if gpd.transcript_name in txome:
      sys.stderr.write("WARNING already have a transcript "+gpd.transcript_name+" ignoring line "+str(z)+" of the gpd\n")
      continue
    txome[gpd.transcript_name] = gpd.gene_name
    tof.write('>'+gpd.transcript_name+"\n"+str(gpd.get_sequence(ref))+"\n")
  tof.close()
  inf.close()
  sys.stderr.write("wrote "+str(len(txome.keys()))+" transcripts\n")

  """Build the salmon index"""
  sys.stderr.write("building a salmon index\n")
  cmd = 'salmon index -p '+str(args.numThreads)+' -t '+args.tempdir+'/transcriptome.fa -i '+args.tempdir+'/salmon_index'
  p = Popen(cmd.split())
  p.communicate()
  sys.stderr.write("finished building the index\n")

  """Use the index to quanitfy"""
  sys.stderr.write("quanitfy reads\n")
  reads = ''
  if args.rU:
    reads = '-r '+args.rU
  else:
    reads = '-1 '+args.r1+' -2 '+args.r2
  cmd = 'salmon quant -p '+str(args.numThreads)+' -i '+args.tempdir+'/salmon_index -l A '+reads+' -o '+args.tempdir+'/output_quant'
  p = Popen(cmd.split())
  p.communicate()
  sys.stderr.write("finished quanitfying\n")

  """Now parse the salmon output to add gene name"""
  salmon = {}
  with open(args.tempdir+'/output_quant/quant.sf') as inf:
    header = inf.readline()
    for line in inf:
      f = line.rstrip().split("\t")
      # by each transcript name hold a data strcture of the information
      salmon[f[0]] = {'name':f[0],'length':int(f[1]),'EffectiveLength':float(f[2]),'TPM':float(f[3]),'NumReads':float(f[4])}
  genes = {}
  for name in salmon:
    gene = txome[name]
    if gene not in genes: genes[gene] = []
    genes[gene].append(salmon[name])
  genetot = {}
  for gene in genes:
    tot = sum([x['TPM'] for x in genes[gene]])
    genetot[gene] = tot
  ordered_gene_names = sorted(genetot.keys(), key=lambda x: genetot[x],reverse=True)
      
  """Collected enough information to make output"""
  sys.stderr.write("generating output\n")
  of = sys.stdout
  if args.output != '-':
    of = open(args.output,'w')
  of.write("geneName\ttranscriptName\tlength\tEffectiveLength\ttxTPM\tNumReads\tgeneTPM\n")
  for gene in ordered_gene_names:
    txs = sorted(genes[gene],key=lambda x: x['TPM'],reverse=True)
    for tx in txs:
      of.write(gene+"\t"+tx['name']+"\t"+str(tx['length'])+"\t"+str(tx['EffectiveLength'])+"\t"+str(tx['TPM'])+"\t"+str(tx['NumReads'])+"\t"+str(genetot[gene])+"\n")
  of.close()

  # Temporary working directory step 3 of 3 - Cleanup
  if not args.specific_tempdir:
    rmtree(args.tempdir)