Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, ts, *,
virtual=False,
structure=None,
structure_bsz=None,
nsites=None,
sites=None,
check_collisions=True):
# short-circuit for copying TensorNetworks
if isinstance(ts, TensorNetwork):
self.structure = ts.structure
self.nsites = ts.nsites
self.sites = ts.sites
self.structure_bsz = ts.structure_bsz
self.tag_map = valmap(lambda tid: tid.copy(), ts.tag_map)
self.ind_map = valmap(lambda tid: tid.copy(), ts.ind_map)
self.tensor_map = {}
for tid, t in ts.tensor_map.items():
self.tensor_map[tid] = t if virtual else t.copy()
self.tensor_map[tid].add_owner(self, tid)
return
# parameters
self.structure = structure
self.structure_bsz = structure_bsz
self.nsites = nsites
self.sites = sites
# internal structure
self.tensor_map = {}
self.tag_map = {}
self.ind_map = {}
argpairs2 = [(a, ind) for a, ind in argpairs if ind is not None]
L = concat([zip(inds, dims) for (x, inds), (x, dims)
in join(first, argpairs2, first, numblocks.items())])
g = groupby(0, L)
g = dict((k, set([d for i, d in v])) for k, v in g.items())
g2 = dict((k, v - set(sentinels) if len(v) > 1 else v) for k, v in g.items())
if consolidate:
return valmap(consolidate, g2)
if g2 and not set(map(len, g2.values())) == set([1]):
raise ValueError("Shapes do not align %s" % g)
return valmap(first, g2)
for s in dependencies.values():
for v in s:
if v not in self.futures:
raise CancelledError(v)
for k, v in dsk3.items():
dependencies[k] |= get_dependencies(dsk3, task=v)
if priority is None:
dependencies2 = {key: {dep for dep in deps if dep in dependencies}
for key, deps in dependencies.items()}
priority = dask.order.order(dsk3, dependencies2)
self._send_to_scheduler({'op': 'update-graph',
'tasks': valmap(dumps_task, dsk3),
'dependencies': valmap(list, dependencies),
'keys': list(flatkeys),
'restrictions': restrictions or {},
'loose_restrictions': loose_restrictions,
'priority': priority,
'user_priority': user_priority,
'resources': resources,
'submitting_task': getattr(thread_state, 'key', None),
'retries': retries,
})
return futures
flatkeys = list(map(tokey, keys))
futures = {key: Future(key, self, inform=False) for key in keyset}
values = {k for k, v in dsk.items() if isinstance(v, Future)
and k not in keyset}
if values:
dsk = dask.optimization.inline(dsk, keys=values)
d = {k: unpack_remotedata(v) for k, v in dsk.items()}
extra_keys = set.union(*[v[1] for v in d.values()]) if d else set()
dsk2 = str_graph({k: v[0] for k, v in d.items()}, extra_keys)
dsk3 = {k: v for k, v in dsk2.items() if k is not v}
if restrictions:
restrictions = keymap(tokey, restrictions)
restrictions = valmap(list, restrictions)
if loose_restrictions is not None:
loose_restrictions = list(map(tokey, loose_restrictions))
dependencies = {tokey(k): set(map(tokey, v[1])) for k, v in d.items()}
for s in dependencies.values():
for v in s:
if v not in self.futures:
raise CancelledError(v)
for k, v in dsk3.items():
dependencies[k] |= get_dependencies(dsk3, task=v)
if priority is None:
dependencies2 = {key: {dep for dep in deps if dep in dependencies}
for s in dependencies.values():
for v in s:
if v not in self.futures:
raise CancelledError(v)
for k, v in dsk3.items():
dependencies[k] |= get_dependencies(dsk3, task=v)
if priority is None:
dependencies2 = {key: {dep for dep in deps if dep in dependencies}
for key, deps in dependencies.items()}
priority = dask.order.order(dsk3, dependencies2)
self._send_to_scheduler({'op': 'update-graph',
'tasks': valmap(dumps_task, dsk3),
'dependencies': valmap(list, dependencies),
'keys': list(flatkeys),
'restrictions': restrictions or {},
'loose_restrictions': loose_restrictions,
'priority': priority,
'user_priority': user_priority,
'resources': resources,
'submitting_task': getattr(thread_state, 'key', None),
'retries': retries,
})
return futures
node_outline_colors.append(
tuple(0.8 * x for x in node_colors[-1])
)
edge_weights = [math.log2(d) for d in edge_weights]
fig, ax = plt.subplots(figsize=figsize, constrained_layout=True)
ax.axis('off')
ax.set_aspect('equal')
# use spectral layout as starting point
pos0 = getattr(nx, initial_layout + '_layout')(G)
# scale points to fit with specified positions
if fix:
# but update with fixed positions
pos0.update(valmap(lambda xy: np.array(
(2 * (xy[0] - xymin) / (xymax - xymin) - 1,
2 * (xy[1] - xymin) / (xymax - xymin) - 1)), fixed_positions))
fixed = fixed_positions.keys()
else:
fixed = None
# and then relax remaining using spring layout
pos = nx.spring_layout(G, pos=pos0, fixed=fixed,
k=k, iterations=iterations)
nx.draw_networkx_nodes(G, pos, node_color=node_colors, node_size=szs,
ax=ax, linewidths=node_outline_size,
edgecolors=node_outline_colors)
nx.draw_networkx_labels(G, pos, labels, font_size=10, ax=ax)
nx.draw_networkx_edges(G, pos, edge_color=edge_colors,
alpha=edge_alpha, width=edge_weights, ax=ax)
>>> d = {'x': ('Hello', 1), 'y': (1, (2, 3))}
>>> broadcast_dimensions(argpairs, d)
{'i': 'Hello', 'j': (2, 3)}
"""
# List like [('i', 2), ('j', 1), ('i', 1), ('j', 2)]
argpairs2 = [(a, ind) for a, ind in argpairs if ind is not None]
L = concat([zip(inds, dims) for (x, inds), (x, dims)
in join(first, argpairs2, first, numblocks.items())])
g = groupby(0, L)
g = dict((k, set([d for i, d in v])) for k, v in g.items())
g2 = dict((k, v - set(sentinels) if len(v) > 1 else v) for k, v in g.items())
if consolidate:
return valmap(consolidate, g2)
if g2 and not set(map(len, g2.values())) == set([1]):
raise ValueError("Shapes do not align %s" % g)
return valmap(first, g2)