Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_dict_of_numpy():
try:
import numpy as np
except ImportError:
import pytest
pytest.skip('numpy is optional')
data = ub.odict(zip(
['one', 'two', 'three', 'four'],
[
np.zeros((3, 3), dtype=np.int32),
np.zeros((3, 10), dtype=np.int32),
np.zeros((3, 20), dtype=np.int32),
np.zeros((3, 30), dtype=np.int32),
]))
text = ub.repr2(data, nl=2)
print(text)
assert text == ub.codeblock(
'''
{
Timed best=59.392 µs, mean=63.395 ± 11.9 µs for dict_subset_list
Timed best=47.203 µs, mean=47.632 ± 0.2 µs for direct_itemgetter
"""
import operator as op
import ubelt as ub
import random
import string
rng = random.Random(0)
items = [rng.choice(string.printable) for _ in range(5000)]
hist_ = ub.ddict(lambda: 0)
for item in items:
hist_[item] += 1
OrderedDict = ub.odict
ti = ub.Timerit(1000, bestof=10, verbose=1)
for timer in ti.reset('dict_subset_iter'):
with timer:
getval = op.itemgetter(1)
key_order = (key for (key, value) in sorted(hist_.items(), key=getval))
hist = ub.dict_subset(hist_, key_order)
for timer in ti.reset('dict_subset_list'):
with timer:
getval = op.itemgetter(1)
key_order = [key for (key, value) in sorted(hist_.items(), key=getval)]
hist = ub.dict_subset(hist_, key_order)
for timer in ti.reset('direct_itemgetter'):
attr = match.groupdict()['attr']
if attr in ub.__all__:
pkg_to_hist[name][attr] += 1
hist_iter = iter(pkg_to_hist.values())
usage = next(hist_iter).copy()
for other in hist_iter:
for k, v in other.items():
usage[k] += v
for attr in ub.__all__:
usage[attr] += 0
for name in pkg_to_hist.keys():
pkg_to_hist[name] = ub.odict(sorted(pkg_to_hist[name].items(), key=lambda t: t[1])[::-1])
usage = ub.odict(sorted(usage.items(), key=lambda t: t[1])[::-1])
if config['print_packages']:
print(ub.repr2(pkg_to_hist, nl=2))
if config['remove_zeros']:
for k, v in list(usage.items()):
if v == 0:
usage.pop(k)
if config['hardcoded_ubelt_hack']:
for k in list(usage):
if k.startswith('util_'):
usage.pop(k)
if k.startswith('_util_'):
usage.pop(k)
# ub._util_deprecated
def __init__(self, n_classes=21, in_channels=3,
bn_size=2, growth_rate=16, is_deconv=True):
super(DenseUNet, self).__init__()
self.in_channels = in_channels
n_feat0 = 36
from torch import nn
features = nn.Sequential(ub.odict([
('conv0', nn.Conv2d(in_channels, n_feat0, kernel_size=7, stride=1,
padding=3,
bias=False)),
('norm0', nn.BatchNorm2d(n_feat0)),
('noli0', default_nonlinearity()),
# ('pool0', nn.MaxPool2d(kernel_size=3, stride=2, padding=1)),
]))
block_config = [2, 3, 3, 3, 3]
bn_size = bn_size
compress = .4
growth_rate = growth_rate
n_feat = n_feat0
# downsampling
text = open(fpath, 'r').read()
for match in pat.finditer(text):
attr = match.groupdict()['attr']
if attr in ub.__all__:
pkg_to_hist[name][attr] += 1
hist_iter = iter(pkg_to_hist.values())
usage = next(hist_iter).copy()
for other in hist_iter:
for k, v in other.items():
usage[k] += v
for attr in ub.__all__:
usage[attr] += 0
for name in pkg_to_hist.keys():
pkg_to_hist[name] = ub.odict(sorted(pkg_to_hist[name].items(), key=lambda t: t[1])[::-1])
usage = ub.odict(sorted(usage.items(), key=lambda t: t[1])[::-1])
if config['print_packages']:
print(ub.repr2(pkg_to_hist, nl=2))
if config['remove_zeros']:
for k, v in list(usage.items()):
if v == 0:
usage.pop(k)
if config['hardcoded_ubelt_hack']:
for k in list(usage):
if k.startswith('util_'):
usage.pop(k)
if k.startswith('_util_'):