Python collections.OrderedDict() Examples
The following are 30
code examples of collections.OrderedDict().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
collections
, or try the search function
.

Example #1
Source File: nq_eval.py From natural-questions with Apache License 2.0 | 7 votes |
def compute_f1(answer_stats, prefix=''): """Computes F1, precision, recall for a list of answer scores. Args: answer_stats: List of per-example scores. prefix (''): Prefix to prepend to score dictionary. Returns: Dictionary mapping string names to scores. """ has_gold, has_pred, is_correct, _ = list(zip(*answer_stats)) precision = safe_divide(sum(is_correct), sum(has_pred)) recall = safe_divide(sum(is_correct), sum(has_gold)) f1 = safe_divide(2 * precision * recall, precision + recall) return OrderedDict({ prefix + 'n': len(answer_stats), prefix + 'f1': f1, prefix + 'precision': precision, prefix + 'recall': recall })
Example #2
Source File: datahub.py From svviz with MIT License | 6 votes |
def __init__(self): self.args = None self.alignDistance = 0 self.samples = collections.OrderedDict() self.genome = None self.sources = {} self.annotationSets = collections.OrderedDict() # for storing axes, annotations, etc, by allele self.alleleTracks = collections.defaultdict(collections.OrderedDict) self.trackCompositor = None self.dotplots = {} self.info = {} self.reset()
Example #3
Source File: track.py From svviz with MIT License | 6 votes |
def __init__(self, chromPartsCollection, pixelWidth, dividerSize=25): # length is in genomic coordinates, starts is in pixels self.dividerSize = dividerSize self.partsToLengths = collections.OrderedDict() self.partsToStartPixels = collections.OrderedDict() self.chromPartsCollection = chromPartsCollection for part in chromPartsCollection: self.partsToLengths[part.id] = len(part) self.pixelWidth = pixelWidth totalLength = sum(self.partsToLengths.values()) + (len(self.partsToLengths)-1)*dividerSize self.basesPerPixel = totalLength / float(pixelWidth) curStart = 0 for regionID in self.partsToLengths: self.partsToStartPixels[regionID] = curStart curStart += (self.partsToLengths[regionID]+dividerSize) / self.basesPerPixel
Example #4
Source File: nq_eval.py From natural-questions with Apache License 2.0 | 6 votes |
def get_metrics_with_answer_stats(long_answer_stats, short_answer_stats): """Generate metrics dict using long and short answer stats.""" def _get_metric_dict(answer_stats, prefix=''): """Compute all metrics for a set of answer statistics.""" opt_result, pr_table = compute_pr_curves( answer_stats, targets=[0.5, 0.75, 0.9]) f1, precision, recall, threshold = opt_result metrics = OrderedDict({ 'best-threshold-f1': f1, 'best-threshold-precision': precision, 'best-threshold-recall': recall, 'best-threshold': threshold, }) for target, recall, precision, _ in pr_table: metrics['recall-at-precision>={:.2}'.format(target)] = recall metrics['precision-at-precision>={:.2}'.format(target)] = precision # Add prefix before returning. return dict([(prefix + k, v) for k, v in six.iteritems(metrics)]) metrics = _get_metric_dict(long_answer_stats, 'long-') metrics.update(_get_metric_dict(short_answer_stats, 'short-')) return metrics
Example #5
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 6 votes |
def _init_fields(self): self.name = None # User-specified name, defaults to build func name if None. self.scope = None # Unique TF graph scope, derived from the user-specified name. self.static_kwargs = dict() # Arguments passed to the user-supplied build func. self.num_inputs = 0 # Number of input tensors. self.num_outputs = 0 # Number of output tensors. self.input_shapes = [[]] # Input tensor shapes (NC or NCHW), including minibatch dimension. self.output_shapes = [[]] # Output tensor shapes (NC or NCHW), including minibatch dimension. self.input_shape = [] # Short-hand for input_shapes[0]. self.output_shape = [] # Short-hand for output_shapes[0]. self.input_templates = [] # Input placeholders in the template graph. self.output_templates = [] # Output tensors in the template graph. self.input_names = [] # Name string for each input. self.output_names = [] # Name string for each output. self.vars = OrderedDict() # All variables (localname => var). self.trainables = OrderedDict() # Trainable variables (localname => var). self._build_func = None # User-supplied build function that constructs the network. self._build_func_name = None # Name of the build function. self._build_module_src = None # Full source code of the module containing the build function. self._run_cache = dict() # Cached graph data for Network.run().
Example #6
Source File: dist_utils.py From mmdetection with Apache License 2.0 | 6 votes |
def _allreduce_coalesced(tensors, world_size, bucket_size_mb=-1): if bucket_size_mb > 0: bucket_size_bytes = bucket_size_mb * 1024 * 1024 buckets = _take_tensors(tensors, bucket_size_bytes) else: buckets = OrderedDict() for tensor in tensors: tp = tensor.type() if tp not in buckets: buckets[tp] = [] buckets[tp].append(tensor) buckets = buckets.values() for bucket in buckets: flat_tensors = _flatten_dense_tensors(bucket) dist.all_reduce(flat_tensors) flat_tensors.div_(world_size) for tensor, synced in zip( bucket, _unflatten_dense_tensors(flat_tensors, bucket)): tensor.copy_(synced)
Example #7
Source File: test_wrappers.py From mmdetection with Apache License 2.0 | 6 votes |
def test_max_pool_2d(): test_cases = OrderedDict([('in_w', [10, 20]), ('in_h', [10, 20]), ('in_channel', [1, 3]), ('out_channel', [1, 3]), ('kernel_size', [3, 5]), ('stride', [1, 2]), ('padding', [0, 1]), ('dilation', [1, 2])]) for in_h, in_w, in_cha, out_cha, k, s, p, d in product( *list(test_cases.values())): # wrapper op with 0-dim input x_empty = torch.randn(0, in_cha, in_h, in_w, requires_grad=True) wrapper = MaxPool2d(k, stride=s, padding=p, dilation=d) wrapper_out = wrapper(x_empty) # torch op with 3-dim input as shape reference x_normal = torch.randn(3, in_cha, in_h, in_w) ref = nn.MaxPool2d(k, stride=s, padding=p, dilation=d) ref_out = ref(x_normal) assert wrapper_out.shape[0] == 0 assert wrapper_out.shape[1:] == ref_out.shape[1:] assert torch.equal(wrapper(x_normal), ref_out)
Example #8
Source File: regnet2mmdet.py From mmdetection with Apache License 2.0 | 6 votes |
def convert(src, dst): """Convert keys in pycls pretrained RegNet models to mmdet style.""" # load caffe model regnet_model = torch.load(src) blobs = regnet_model['model_state'] # convert to pytorch style state_dict = OrderedDict() converted_names = set() for key, weight in blobs.items(): if 'stem' in key: convert_stem(key, weight, state_dict, converted_names) elif 'head' in key: convert_head(key, weight, state_dict, converted_names) elif key.startswith('s'): convert_reslayer(key, weight, state_dict, converted_names) # check if all layers are converted for key in blobs: if key not in converted_names: print(f'not converted: {key}') # save checkpoint checkpoint = dict() checkpoint['state_dict'] = state_dict torch.save(checkpoint, dst)
Example #9
Source File: main.py From ciocheck with MIT License | 6 votes |
def __init__(self, cmd_root, cli_args, folders=None, files=None): """Main tool runner.""" # Run options self.cmd_root = cmd_root # Folder on which the command was executed self.config = load_config(cmd_root, cli_args) self.file_manager = FileManager(folders=folders, files=files) self.folders = folders self.files = files self.all_results = OrderedDict() self.all_tools = {} self.test_results = None self.failed_checks = set() self.check = self.config.get_value('check') self.enforce = self.config.get_value('enforce') self.diff_mode = self.config.get_value('diff_mode') self.file_mode = self.config.get_value('file_mode') self.branch = self.config.get_value('branch') self.disable_formatters = cli_args.disable_formatters self.disable_linters = cli_args.disable_linters self.disable_tests = cli_args.disable_tests
Example #10
Source File: tools.py From ciocheck with MIT License | 6 votes |
def parse_coverage(self): """Parse .coverage json report generated by coverage.""" coverage_string = ("!coverage.py: This is a private format, don't " "read it directly!") coverage_path = os.path.join(self.cmd_root, '.coverage') covered_lines = {} if os.path.isfile(coverage_path): with open(coverage_path, 'r') as file_obj: data = file_obj.read() data = data.replace(coverage_string, '') cov = json.loads(data) covered_lines = OrderedDict() lines = cov['lines'] for path in sorted(lines): covered_lines[path] = lines[path] return covered_lines
Example #11
Source File: madry_mnist_model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def fprop(self, x): output = OrderedDict() # first convolutional layer h_conv1 = tf.nn.relu(self._conv2d(x, self.W_conv1) + self.b_conv1) h_pool1 = self._max_pool_2x2(h_conv1) # second convolutional layer h_conv2 = tf.nn.relu( self._conv2d(h_pool1, self.W_conv2) + self.b_conv2) h_pool2 = self._max_pool_2x2(h_conv2) # first fully connected layer h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, self.W_fc1) + self.b_fc1) # output layer logits = tf.matmul(h_fc1, self.W_fc2) + self.b_fc2 output = deterministic_dict(locals()) del output["self"] output[self.O_PROBS] = tf.nn.softmax(logits=logits) return output
Example #12
Source File: dataloader_m.py From models with MIT License | 6 votes |
def read_cpg_profiles(filenames, log=None, *args, **kwargs): """Read methylation profiles. Input files can be gzip compressed. Returns ------- dict `dict (key, value)`, where `key` is the output name and `value` the CpG table. """ cpg_profiles = OrderedDict() for filename in filenames: if log: log(filename) #cpg_file = dat.GzipFile(filename, 'r') cpg_file = get_fh(filename, 'r') output_name = split_ext(filename) cpg_profile = dat.read_cpg_profile(cpg_file, sort=True, *args, **kwargs) cpg_profiles[output_name] = cpg_profile cpg_file.close() return cpg_profiles
Example #13
Source File: dataloader_m.py From models with MIT License | 6 votes |
def map_cpg_tables(cpg_tables, chromo, chromo_pos): """Maps values from cpg_tables to `chromo_pos`. Positions in `cpg_tables` for `chromo` must be a subset of `chromo_pos`. Inserts `dat.CPG_NAN` for uncovered positions. """ chromo_pos.sort() mapped_tables = OrderedDict() for name, cpg_table in six.iteritems(cpg_tables): cpg_table = cpg_table.loc[cpg_table.chromo == chromo] cpg_table = cpg_table.sort_values('pos') mapped_table = map_values(cpg_table.value.values, cpg_table.pos.values, chromo_pos) assert len(mapped_table) == len(chromo_pos) mapped_tables[name] = mapped_table return mapped_tables
Example #14
Source File: 10_two_layer_net.py From deep-learning-note with MIT License | 6 votes |
def __init__(self, input_size, hidden_size, output_size, weight_init_std=0.01): # 初始化权重 self.params = {} # 用高斯分布初始化 self.params['W1'] = weight_init_std * np.random.randn(input_size, hidden_size) self.params['b1'] = np.zeros(hidden_size) self.params['W2'] = weight_init_std * np.random.randn(hidden_size, output_size) self.params['b2'] = np.zeros(output_size) # 生成层 self.layers = OrderedDict() self.layers['Affine1'] = Affine(self.params['W1'], self.params['b1']) self.layers['Relu1'] = Relu() self.layers['Affine2'] = Affine(self.params['W2'], self.params['b2']) self.lastLayer = SoftmaxWithLoss()
Example #15
Source File: multi_layer_net.py From deep-learning-note with MIT License | 6 votes |
def __init__(self, input_size, hidden_size_list, output_size, activation='relu', weight_init_std='relu', weight_decay_lambda=0): self.input_size = input_size self.output_size = output_size self.hidden_size_list = hidden_size_list self.hidden_layer_num = len(hidden_size_list) self.weight_decay_lambda = weight_decay_lambda self.params = {} # 初始化权重 self.__init_weight(weight_init_std) # 生成层 activation_layer = {'sigmoid': Sigmoid, 'relu': Relu} self.layers = OrderedDict() for idx in range(1, self.hidden_layer_num+1): self.layers['Affine' + str(idx)] = Affine(self.params['W' + str(idx)], self.params['b' + str(idx)]) self.layers['Activation_function' + str(idx)] = activation_layer[activation]() idx = self.hidden_layer_num + 1 self.layers['Affine' + str(idx)] = Affine(self.params['W' + str(idx)], self.params['b' + str(idx)]) self.last_layer = SoftmaxWithLoss()
Example #16
Source File: urlextract.py From video2commons with GNU General Public License v3.0 | 6 votes |
def escape_wikitext(wikitext): """Escape wikitext for use in file description.""" rep = OrderedDict([ ('{|', '{{(}}|'), ('|}', '|{{)}}'), ('||', '||'), ('|', '|'), ('[[', '{{!((}}'), (']]', '{{))!}}'), ('{{', '{{((}}'), ('}}', '{{))}}'), ('{', '{{(}}'), ('}', '{{)}}'), ]) rep = dict((re.escape(k), v) for k, v in rep.iteritems()) pattern = re.compile("|".join(rep.keys())) return pattern.sub(lambda m: rep[re.escape(m.group(0))], wikitext) # Source: mediawiki.Title.js@9df363d
Example #17
Source File: tokenization.py From BERT-Classification-Tutorial with Apache License 2.0 | 5 votes |
def load_vocab(vocab_file): """Loads a vocabulary file into a dictionary.""" vocab = collections.OrderedDict() index = 0 with tf.gfile.GFile(vocab_file, "r") as reader: while True: token = convert_to_unicode(reader.readline()) if not token: break token = token.strip() vocab[token] = index index += 1 return vocab
Example #18
Source File: modeling.py From BERT-Classification-Tutorial with Apache License 2.0 | 5 votes |
def get_assignment_map_from_checkpoint(tvars, init_checkpoint): """Compute the union of the current variables and checkpoint variables.""" assignment_map = {} initialized_variable_names = {} name_to_variable = collections.OrderedDict() for var in tvars: name = var.name m = re.match("^(.*):\\d+$", name) if m is not None: name = m.group(1) name_to_variable[name] = var init_vars = tf.train.list_variables(init_checkpoint) assignment_map = collections.OrderedDict() for x in init_vars: (name, var) = (x[0], x[1]) if name not in name_to_variable: continue assignment_map[name] = name initialized_variable_names[name] = 1 initialized_variable_names[name + ":0"] = 1 return (assignment_map, initialized_variable_names)
Example #19
Source File: run_classifier.py From BERT-Classification-Tutorial with Apache License 2.0 | 5 votes |
def file_based_convert_examples_to_features( examples, label_list, max_seq_length, tokenizer, output_file): """Convert a set of `InputExample`s to a TFRecord file.""" writer = tf.python_io.TFRecordWriter(output_file) label_map = {} for (i, label) in enumerate(sorted(label_list)): label_map[label] = i for (ex_index, example) in enumerate(examples): if ex_index % 10000 == 0: tf.logging.info("Writing example %d of %d" % (ex_index, len(examples))) feature = convert_single_example(ex_index, example, label_map, max_seq_length, tokenizer) def create_int_feature(values): f = tf.train.Feature(int64_list=tf.train.Int64List(value=list(values))) return f features = collections.OrderedDict() features["input_ids"] = create_int_feature(feature.input_ids) features["input_mask"] = create_int_feature(feature.input_mask) features["segment_ids"] = create_int_feature(feature.segment_ids) features["label_ids"] = create_int_feature([feature.label_id]) tf_example = tf.train.Example(features=tf.train.Features(feature=features)) writer.write(tf_example.SerializeToString()) return label_map
Example #20
Source File: composite.py From indras_net with GNU General Public License v3.0 | 5 votes |
def __init__(self, name, attrs=None, members=None, duration=INF, action=None, member_creator=None, num_members=None, serial_obj=None, reg=True, **kwargs): self.num_members_ever = 0 self.members = OrderedDict() super().__init__(name, attrs=attrs, duration=duration, action=action, serial_obj=serial_obj, reg=False) self.type = type(self).__name__ if serial_obj is not None: self.restore(serial_obj) else: if members is not None: for member in members: join(self, member) if num_members is None: num_members = 1 # A default if they forgot to pass this. self.num_members_ever = num_members self.member_creator = None if member_creator is not None: self.member_creator = member_creator # If we have a member creator function, call it # `num_members` times to create group members. for i in range(num_members): join(self, member_creator(self.name, i, **kwargs)) if reg: add_group(self.name, self)
Example #21
Source File: composite.py From indras_net with GNU General Public License v3.0 | 5 votes |
def subset(self, predicate, *args, name=None): # noqa E999 new_dict = OrderedDict() for mbr in self: if predicate(self[mbr], *args): new_dict[mbr] = self[mbr] return grp_from_nm_dict(name, new_dict)
Example #22
Source File: menu.py From indras_net with GNU General Public License v3.0 | 5 votes |
def __init__(self, name, user, level=0): super().__init__(name) self.user = user self.menu_items = OrderedDict() self.def_act = None self.level = level
Example #23
Source File: agent_pop.py From indras_net with GNU General Public License v3.0 | 5 votes |
def __init__(self, name): super().__init__(name) self.vars = OrderedDict() self.graph = nx.Graph()
Example #24
Source File: test_agent_pop.py From indras_net with GNU General Public License v3.0 | 5 votes |
def __init__(self, methodName): super().__init__(methodName=methodName) # A dictionary for different types of dummy agents in order to test agent pop class # First we will fill this dict with various dummy agents, # then we will fill the Agent Pop class, using dict_for_reference as source # The OrderedDict in Agent Pop should be the same as dic_for_reference. That will test if append works # If append works and we have the OrderedDict in Agent Pop, # we will test rest of the functions if they return correct result, with reference of dict_for_reference # format: {var:{"agents": [], "pop_data": 0,"pop_hist": [],"my_periods": 0,"disp_color": None}} self.dic_for_reference = OrderedDict() self.agentpop = ap.AgentPop("test") # add 3 varieties for i in range(3): self.dic_for_reference[str(i)] = {AGENTS: [], POP_DATA: 0,POP_HIST: [],MY_PERIODS: 0,DISP_COLOR: None} for i in range(3): # add color to each variety self.dic_for_reference[str(i)][DISP_COLOR] = "#" + str(i) # add agents; agent type is Node for r in range(random.randint(1,7)): self.dic_for_reference[str(i)][AGENTS].append(node.Node("agent"+str(r))) for var in self.dic_for_reference: for a in self.dic_for_reference[var][AGENTS]: a.ntype = var #assign ntype for each agent; each dic_for_reference's variety should be the same as its agents' ntype! # finish building dic_for_reference, # dic_for_reference should look like: { # '0':{"agents": [1-7 agents of ntype('0')], "pop_data": 0,"pop_hist": [],"my_periods": 0,"disp_color": '#0'} # '1':{"agents": [1-7 agents of ntype('1')], "pop_data": 0,"pop_hist": [],"my_periods": 0,"disp_color": '#1'} # '2':{"agents": [1-7 agents of ntype('2')], "pop_data": 0,"pop_hist": [],"my_periods": 0,"disp_color": '#2'} # } # Note: each individual test function has its own unique self.dic_for_reference and self.agent_pop. Change of self.dic_for_reference # in one function won't affect the use of it in other functions. (Two functions will have different self.dic_for_reference)
Example #25
Source File: variants.py From svviz with MIT License | 5 votes |
def __init__(self, parts=None): self.parts = collections.OrderedDict() if parts is not None: for part in parts: self.parts[part.id] = part
Example #26
Source File: datahub.py From svviz with MIT License | 5 votes |
def getCounts(self): if self._counts is None: self._counts = collections.OrderedDict() for name, sample in self.samples.items(): self._counts[name] = collections.Counter([alnCollection.choice for alnCollection in sample.alnCollections]) self._counts["Total"] = dict((allele, sum(self._counts[name][allele] for name in self.samples)) for allele in ["alt", "ref", "amb"]) return self._counts
Example #27
Source File: datahub.py From svviz with MIT License | 5 votes |
def reset(self): self.reads = [] self.alnCollections = [] self.tracks = collections.OrderedDict()
Example #28
Source File: export.py From svviz with MIT License | 5 votes |
def addTrackSVG(self, section, name, tracksvg, viewbox=None, height=100): if not section in self.sections: self.sections[section] = collections.OrderedDict() self.sections[section][name] = {"svg":tracksvg, "viewbox":viewbox, "height": height }
Example #29
Source File: flow_oa.py From incubator-spot with Apache License 2.0 | 5 votes |
def _initialize_members(self,date,limit,logger): # get logger if exists. if not, create new instance. self._logger = logging.getLogger('OA.Flow') if logger else Util.get_logger('OA.Flow',create_file=False) # initialize required parameters. self._scrtip_path = os.path.dirname(os.path.abspath(__file__)) self._date = date self._table_name = "flow" self._flow_results = [] self._limit = limit self._data_path = None self._ipynb_path = None self._ingest_summary_path = None self._flow_scores = [] self._results_delimiter = '\t' # get app configuration. self._spot_conf = Util.get_spot_conf() # # get scores fields conf conf_file = "{0}/flow_conf.json".format(self._scrtip_path) self._conf = json.loads(open (conf_file).read(),object_pairs_hook=OrderedDict) # initialize data engine self._db = self._spot_conf.get('conf', 'DBNAME').replace("'", "").replace('"', '')
Example #30
Source File: proxy_oa.py From incubator-spot with Apache License 2.0 | 5 votes |
def _initialize_members(self,date,limit,logger): # get logger if exists. if not, create new instance. self._logger = logging.getLogger('OA.PROXY') if logger else Util.get_logger('OA.PROXY',create_file=False) # initialize required parameters. self._scrtip_path = os.path.dirname(os.path.abspath(__file__)) self._date = date self._table_name = "proxy" self._proxy_results = [] self._limit = limit self._data_path = None self._ipynb_path = None self._ingest_summary_path = None self._proxy_scores = [] self._proxy_scores_headers = [] self._proxy_extra_columns = [] self._results_delimiter = '\t' # get app configuration. self._spot_conf = Util.get_spot_conf() # get scores fields conf conf_file = "{0}/proxy_conf.json".format(self._scrtip_path) self._conf = json.loads(open (conf_file).read(),object_pairs_hook=OrderedDict) # initialize data engine self._db = self._spot_conf.get('conf', 'DBNAME').replace("'", "").replace('"', '')