def _aggregate_batch(data_holder, use_list=False):
size = len(data_holder[0])
result = []
for k in range(size):
if use_list:
result.append(
[x[k] for x in data_holder])
else:
dt = data_holder[0][k]
if type(dt) in [int, bool]:
tp = 'int32'
elif type(dt) == float:
tp = 'float32'
else:
try:
tp = dt.dtype
except Exception:
raise TypeError("Unsupported type to batch: {}"
.format(type(dt)))
try:
result.append(
np.asarray([x[k] for x in data_holder], dtype=tp))
except KeyboardInterrupt:
raise
except Exception:
logger.exception("Cannot batch data. Perhaps they are of "
"inconsistent shape?")
import IPython as IP
IP.embed(config=IP
.terminal # @UndefinedVariable
.ipapp.load_default_config())
return result
python类embed()的实例源码
def start_shell(opts):
seqrepo_dir = os.path.join(opts.root_directory, opts.instance_name)
sr = SeqRepo(seqrepo_dir)
import IPython
IPython.embed(header="\n".join([
"seqrepo (https://github.com/biocommons/biocommons.seqrepo/)", "version: " + __version__,
"instance path: " + seqrepo_dir
]))
def shell():
"""Run a Python shell in the app context."""
try:
import IPython
except ImportError:
IPython = None
if IPython is not None:
IPython.embed(banner1='', user_ns=current_app.make_shell_context())
else:
import code
code.interact(banner='', local=current_app.make_shell_context())
def insert_ipython(num_up=1):
"""
Placed inside a function, this will insert an IPython interpreter at that
current location. This will enabled detailed inspection of the current
execution environment, as well as (optional) modification of that environment.
*num_up* refers to how many frames of the stack get stripped off, and
defaults to 1 so that this function itself is stripped off.
"""
import IPython
from IPython.terminal.embed import InteractiveShellEmbed
try:
from traitlets.config.loader import Config
except ImportError:
from IPython.config.loader import Config
frame = inspect.stack()[num_up]
loc = frame[0].f_locals.copy()
glo = frame[0].f_globals
dd = dict(fname = frame[3], filename = frame[1],
lineno = frame[2])
cfg = Config()
cfg.InteractiveShellEmbed.local_ns = loc
cfg.InteractiveShellEmbed.global_ns = glo
IPython.embed(config=cfg, banner2 = __header % dd)
ipshell = InteractiveShellEmbed(config=cfg)
del ipshell
#
# Our progress bar types and how to get one
#
def embed():
vars = globals()
vars.update(locals())
shell = code.InteractiveConsole(vars)
shell.interact()
def run(self, line):
ishellCompleter = readline.get_completer()
embed()
readline.set_completer(ishellCompleter)
def REPL():
# collect all variables outside this scope
local = {}
# set stack context to 0 to avoid the slow loading of source file
for sinfo in inspect.stack(0):
local.update(sinfo[0].f_globals)
local.update(sinfo[0].f_locals)
code.interact(local=local)
def shell(metadir, accept_metadir, controller, ctrlopt, modelsetup, modelopt, backend, local,
verbosity
):
handle_common_options(verbosity)
ys = handle_connection_options(metadir, accept_metadir, controller, ctrlopt, modelsetup, modelopt, backend, local)
assert ys
import IPython
IPython.embed()
def shell():
import IPython
IPython.embed()
def run(self):
matches = self.db.match(self.args.selectors)
if len(matches) == 0:
logger.warning("No results")
return 10
# Just for convenience
meta = self.meta
files = self.files
db = self.db
ipy.embed()
return 0
def parse_and_save():
en = spacy.load('en')
reader = WikiReader(wikidump)
records = reader.records()
def section_texts_flat(records):
while 1:
try:
record = next(records)
except OSError as e:
print('error: %s' % e)
else:
for section in record['sections']:
yield section['text']
pipe = en.pipe(section_texts_flat(records),
n_threads=cpu_count(),
batch_size=1000)
# pipe = (en(txt) for txt in section_texts_flat(records))
preproc = Preprocessor(en.vocab)
with FilePoolWriter(wikidoc_dir, wikidoc_fn_template) as f:
for i, doc in enumerate(tqdm.tqdm(pipe)):
if len(doc._py_tokens) <= 7:
# short sentences -- nah
continue
for sent in doc.sents:
packed = preproc.pack(sent)
f.write(packed)
if i % 10000 == 0:
print('i=%s, saving vocab' % i)
save_vocab(en.vocab)
save_vocab(en.vocab)
import IPython
IPython.embed()
def load_frozen(self, DEBUG=False, feats_dict=None, points_dict=None):
if feats_dict is not None:
print("loading imgs from memory")
self._feats_dict = feats_dict
self._points_dict = points_dict
return
if cfgs.layer:
def subfile(filename):
return osp.join(self._frozen_layer, filename)
with open(subfile(self._points_dict_name), 'rb') as f:
self._points_dict = pickle.load(f)
convs = self.type2names()
self._feats_dict = dict()
for conv in convs:
filename = subfile(conv)
if osp.exists(filename):
with open(filename, 'rb') as f:
self._feats_dict[conv] = pickle.load(f)
else:
frozen = self._frozen
print("loading imgs from", frozen)
with open(frozen, 'rb') as f:
self._feats_dict, self._points_dict = pickle.load(f)
if DEBUG:
convs = self.type2names()
feats_dict = self.extract_features(convs, points_dict=self._points_dict, save=1)
print("feats_dict", feats_dict)
print("self._feats_dict", self._feats_dict)
embed()
for i in feats_dict:
for x, y in zip(np.nditer(self._feats_dict[i]), np.nditer(feats_dict[i])):
assert x == y
OK("frozen ")
print("loaded")
def python_shell(options):
logger = setup_logger("Robot", debug=options.verbose)
def conn_callback(*args):
sys.stdout.write(".")
sys.stdout.flush()
return True
if options.shell == "ipython":
import IPython
else:
import importlib
sys.path.append(os.path.abspath(""))
module_name, entrance_name = options.shell.rsplit(".", 1)
module_instance = importlib.import_module(module_name)
entrance = module_instance.__getattribute__(entrance_name)
robot, device = connect_robot_helper(options.target, options.clientkey)
if options.shell == "ipython":
logger.info("----> READY")
logger.info("""
* Hint: Try 'robot?' and 'dir(robot)' to get more informations)\n""")
IPython.embed()
return 0
else:
return entrance(robot, device)
def ipython_shell(user_ns,banner) :
IPython.embed(banner1=banner,user_ns=user_ns)
def debug_mode(self):
test_pol_grad = PolicyGradient(net_dims=[2,2,2,2])
IPython.embed()
def solve_kkt(U_Q, d, G, A, U_S, rx, rs, rz, ry, dbg=False):
""" Solve KKT equations for the affine step"""
nineq, nz, neq, _ = get_sizes(G, A)
invQ_rx = torch.potrs(rx.view(-1, 1), U_Q).view(-1)
if neq > 0:
h = torch.cat([torch.mv(A, invQ_rx) - ry,
torch.mv(G, invQ_rx) + rs / d - rz], 0)
else:
h = torch.mv(G, invQ_rx) + rs / d - rz
w = -torch.potrs(h.view(-1, 1), U_S).view(-1)
g1 = -rx - torch.mv(G.t(), w[neq:])
if neq > 0:
g1 -= torch.mv(A.t(), w[:neq])
g2 = -rs - w[neq:]
dx = torch.potrs(g1.view(-1, 1), U_Q).view(-1)
ds = g2 / d
dz = w[neq:]
dy = w[:neq] if neq > 0 else None
# if np.all(np.array([x.norm() for x in [rx, rs, rz, ry]]) != 0):
if dbg:
import IPython
import sys
IPython.embed()
sys.exit(-1)
# if rs.norm() > 0: import IPython, sys; IPython.embed(); sys.exit(-1)
return dx, ds, dz, dy
instrumentation_results_manager.py 文件源码
项目:bootloader_instrumentation_suite
作者: bx
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def _browse_db(self, name, enabled):
tasks = []
class Do():
def __init__(self):
pass
def __call__(self):
rwe = self
IPython.embed()
a = ActionListTask([PythonInteractiveAction(Do())],
[], [], name)
tasks.append(a)
return tasks
def console():
banner = """
[Sea Console]:
the following vars are included:
`app` (the current app)
"""
ctx = {'app': current_app}
try:
from IPython import embed
h, kwargs = embed, dict(banner1=banner, user_ns=ctx)
except ImportError:
import code
h, kwargs = code.interact, dict(banner=banner, local=ctx)
h(**kwargs)
return 0
def exit_raise(self, parameter_s=''):
"""%exit_raise Make the current embedded kernel exit and raise and exception.
This function sets an internal flag so that an embedded IPython will
raise a `IPython.terminal.embed.KillEmbeded` Exception on exit, and then exit the current I. This is
useful to permanently exit a loop that create IPython embed instance.
"""
self.shell.should_raise = True
self.shell.ask_exit()
def _default_embed_callback(tensor, var):
logger.info('embed for {}, access by tensor and var'.format(tensor.name))
from IPython import embed
embed()