Python collections.Sequence() Examples
The following are 30
code examples of collections.Sequence().
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: data_parallel.py From EMANet with GNU General Public License v3.0 | 6 votes |
def dict_gather(outputs, target_device, dim=0): """ Gathers variables from different GPUs on a specified device (-1 means the CPU), with dictionary support. """ def gather_map(outputs): out = outputs[0] if isinstance(out, Variable): # MJY(20180330) HACK:: force nr_dims > 0 if out.dim() == 0: outputs = [o.unsqueeze(0) for o in outputs] return Gather.apply(target_device, dim, *outputs) elif out is None: return None elif isinstance(out, collections.Mapping): return {k: gather_map([o[k] for o in outputs]) for k in out} elif isinstance(out, collections.Sequence): return type(out)(map(gather_map, zip(*outputs))) return gather_map(outputs)
Example #2
Source File: nest.py From lambda-packs with MIT License | 6 votes |
def _sequence_like(instance, args): """Converts the sequence `args` to the same type as `instance`. Args: instance: an instance of `tuple`, `list`, or a `namedtuple` class. args: elements to be converted to a sequence. Returns: `args` with the type of `instance`. """ if (isinstance(instance, tuple) and hasattr(instance, "_fields") and isinstance(instance._fields, collections.Sequence) and all(isinstance(f, six.string_types) for f in instance._fields)): # This is a namedtuple return type(instance)(*args) else: # Not a namedtuple return type(instance)(args)
Example #3
Source File: brain_fstrings.py From python-netsurv with MIT License | 6 votes |
def _clone_node_with_lineno(node, parent, lineno): cls = node.__class__ other_fields = node._other_fields _astroid_fields = node._astroid_fields init_params = {"lineno": lineno, "col_offset": node.col_offset, "parent": parent} postinit_params = {param: getattr(node, param) for param in _astroid_fields} if other_fields: init_params.update({param: getattr(node, param) for param in other_fields}) new_node = cls(**init_params) if hasattr(node, "postinit") and _astroid_fields: for param, child in postinit_params.items(): if child and not isinstance(child, collections.Sequence): cloned_child = _clone_node_with_lineno( node=child, lineno=new_node.lineno, parent=new_node ) postinit_params[param] = cloned_child new_node.postinit(**postinit_params) return new_node
Example #4
Source File: aggregated_classifier.py From Attention-Gated-Networks with MIT License | 6 votes |
def aggregate_output(self): """Given a list of predictions from net, make a decision based on aggreagation rule""" if isinstance(self.predictions, collections.Sequence): logits = [] for pred in self.predictions: logit = self.net.apply_argmax_softmax(pred).unsqueeze(0) logits.append(logit) logits = torch.cat(logits, 0) if self.aggregation == 'max': self.pred = logits.data.max(0)[0].max(1) elif self.aggregation == 'mean': self.pred = logits.data.mean(0).max(1) elif self.aggregation == 'weighted_mean': self.pred = (self.aggregation_weight.expand_as(logits) * logits).data.mean(0).max(1) elif self.aggregation == 'idx': self.pred = logits[self.aggregation_param].data.max(1) else: # Apply a softmax and return a segmentation map self.logits = self.net.apply_argmax_softmax(self.predictions) self.pred = self.logits.data.max(1)
Example #5
Source File: myImageTransformations.py From Attention-Gated-Networks with MIT License | 6 votes |
def __call__(self, image): if isinstance(self.sigma, collections.Sequence): sigma = random_num_generator( self.sigma, random_state=self.random_state) else: sigma = self.sigma if isinstance(self.mean, collections.Sequence): mean = random_num_generator( self.mean, random_state=self.random_state) else: mean = self.mean row, col, ch = image.shape gauss = self.random_state.normal(mean, sigma, (row, col, ch)) gauss = gauss.reshape(row, col, ch) image += image * gauss return image
Example #6
Source File: brain_fstrings.py From python-netsurv with MIT License | 6 votes |
def _clone_node_with_lineno(node, parent, lineno): cls = node.__class__ other_fields = node._other_fields _astroid_fields = node._astroid_fields init_params = {"lineno": lineno, "col_offset": node.col_offset, "parent": parent} postinit_params = {param: getattr(node, param) for param in _astroid_fields} if other_fields: init_params.update({param: getattr(node, param) for param in other_fields}) new_node = cls(**init_params) if hasattr(node, "postinit") and _astroid_fields: for param, child in postinit_params.items(): if child and not isinstance(child, collections.Sequence): cloned_child = _clone_node_with_lineno( node=child, lineno=new_node.lineno, parent=new_node ) postinit_params[param] = cloned_child new_node.postinit(**postinit_params) return new_node
Example #7
Source File: state.py From tattle with Mozilla Public License 2.0 | 6 votes |
def __init__(self, config, queue, events, loop=None): """ Initialize instance of the NodeManager class :param config: config object :param queue: broadcast queue :type config: tattle.config.Configuration :type events: tattle.event.EventManager :type queue: tattle.queue.BroadcastQueue """ self.config = config self._queue = queue self._events = events self._loop = loop or asyncio.get_event_loop() self._leaving = False self._nodes = list() self._nodes_map = dict() self._nodes_lock = asyncio.Lock() self._suspect_nodes = dict() self._local_node_name = None self._local_node_seq = sequence.Sequence()
Example #8
Source File: messages.py From tattle with Mozilla Public License 2.0 | 6 votes |
def _serialize_internal(cls, msg): # insert the name of the class data = [msg.__class__.__name__] # get list of fields fields = msg.__class__.get_fields() for field_name, field_type in fields: attr = getattr(msg, field_name) if field_type is not None and attr is not None: # if attr has a field type defined deserialize that field data.extend(cls._serialize_internal(attr)) else: if isinstance(attr, str) or isinstance(attr, bytes): data.append(attr) elif isinstance(attr, collections.Sequence): data.append([cls._serialize_internal(i) for i in attr]) elif isinstance(attr, collections.Mapping): data.append({k: cls._serialize_internal(v) for k, v in attr.items()}) else: data.append(attr) return data
Example #9
Source File: myImageTransformations.py From Attention-Gated-Networks with MIT License | 6 votes |
def __call__(self, img): for t in self.transforms: if isinstance(t, collections.Sequence): assert isinstance(img, collections.Sequence) and len(img) == len( t), "size of image group and transform group does not fit" tmp_ = [] for i, im_ in enumerate(img): if callable(t[i]): tmp_.append(t[i](im_)) else: tmp_.append(im_) img = tmp_ elif callable(t): img = t(img) elif t is None: continue else: raise Exception('unexpected type') return img
Example #10
Source File: utils.py From AerialDetection with Apache License 2.0 | 6 votes |
def to_tensor(data): """Convert objects of various python types to :obj:`torch.Tensor`. Supported types are: :class:`numpy.ndarray`, :class:`torch.Tensor`, :class:`Sequence`, :class:`int` and :class:`float`. """ if isinstance(data, torch.Tensor): return data elif isinstance(data, np.ndarray): return torch.from_numpy(data) elif isinstance(data, Sequence) and not mmcv.is_str(data): return torch.tensor(data) elif isinstance(data, int): return torch.LongTensor([data]) elif isinstance(data, float): return torch.FloatTensor([data]) else: raise TypeError('type {} cannot be converted to tensor.'.format( type(data)))
Example #11
Source File: functional.py From opencv_transforms with MIT License | 6 votes |
def normalize(tensor, mean, std): """Normalize a tensor image with mean and standard deviation. .. note:: This transform acts in-place, i.e., it mutates the input tensor. See :class:`~torchvision.transforms.Normalize` for more details. Args: tensor (Tensor): Tensor image of size (C, H, W) to be normalized. mean (sequence): Sequence of means for each channel. std (sequence): Sequence of standard deviations for each channely. Returns: Tensor: Normalized Tensor image. """ if not _is_tensor_image(tensor): raise TypeError('tensor is not a torch image.') # This is faster than using broadcasting, don't change without benchmarking for t, m, s in zip(tensor, mean, std): t.sub_(m).div_(s) return tensor
Example #12
Source File: pyparsing.py From vulscan with MIT License | 6 votes |
def __init__( self, exprs, savelist = False ): super(ParseExpression,self).__init__(savelist) if isinstance( exprs, _generatorType ): exprs = list(exprs) if isinstance( exprs, basestring ): self.exprs = [ Literal( exprs ) ] elif isinstance( exprs, collections.Sequence ): # if sequence of strings provided, wrap with Literal if all(isinstance(expr, basestring) for expr in exprs): exprs = map(Literal, exprs) self.exprs = list(exprs) else: try: self.exprs = list( exprs ) except TypeError: self.exprs = [ exprs ] self.callPreparse = False
Example #13
Source File: hierarchical_encoders.py From Counterfactual-StoryRW with MIT License | 6 votes |
def flatten(x): """Flattens a cell state by concatenating a sequence of cell states along the last dimension. If the cell states are :tf_main:`LSTMStateTuple <contrib/rnn/LSTMStateTuple>`, only the hidden `LSTMStateTuple.h` is used. This process is used by default if :attr:`medium` is not provided to :meth:`_build`. """ if isinstance(x, LSTMStateTuple): return x.h if isinstance(x, collections.Sequence): return tf.concat( [HierarchicalRNNEncoder.flatten(v) for v in x], -1) else: return x
Example #14
Source File: dataset.py From speaksee with BSD 3-Clause "New" or "Revised" License | 6 votes |
def collate_fn(self): def collate(batch): if len(self.fields) == 1: batch = [batch, ] else: batch = list(zip(*batch)) tensors = [] for field, data in zip(self.fields.values(), batch): tensor = field.process(data) if isinstance(tensor, collections.Sequence) and any(isinstance(t, torch.Tensor) for t in tensor): tensors.extend(tensor) else: tensors.append(tensor) if len(tensors) > 1: return tensors else: return tensors[0] return collate
Example #15
Source File: pyparsing.py From jbox with MIT License | 6 votes |
def __init__( self, exprs, savelist = False ): super(ParseExpression,self).__init__(savelist) if isinstance( exprs, _generatorType ): exprs = list(exprs) if isinstance( exprs, basestring ): self.exprs = [ Literal( exprs ) ] elif isinstance( exprs, collections.Sequence ): # if sequence of strings provided, wrap with Literal if all(isinstance(expr, basestring) for expr in exprs): exprs = map(Literal, exprs) self.exprs = list(exprs) else: try: self.exprs = list( exprs ) except TypeError: self.exprs = [ exprs ] self.callPreparse = False
Example #16
Source File: pyparsing.py From jbox with MIT License | 6 votes |
def __init__( self, exprs, savelist = False ): super(ParseExpression,self).__init__(savelist) if isinstance( exprs, _generatorType ): exprs = list(exprs) if isinstance( exprs, basestring ): self.exprs = [ Literal( exprs ) ] elif isinstance( exprs, collections.Sequence ): # if sequence of strings provided, wrap with Literal if all(isinstance(expr, basestring) for expr in exprs): exprs = map(Literal, exprs) self.exprs = list(exprs) else: try: self.exprs = list( exprs ) except TypeError: self.exprs = [ exprs ] self.callPreparse = False
Example #17
Source File: opencv_functional.py From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License | 6 votes |
def normalize(tensor, mean, std): """Normalize a tensor image with mean and standard deviation. .. note:: This transform acts in-place, i.e., it mutates the input tensor. See :class:`~torchvision.transforms.Normalize` for more details. Args: tensor (Tensor): Tensor image of size (C, H, W) to be normalized. mean (sequence): Sequence of means for each channel. std (sequence): Sequence of standard deviations for each channely. Returns: Tensor: Normalized Tensor image. """ if not _is_tensor_image(tensor): raise TypeError('tensor is not a torch image.') # This is faster than using broadcasting, don't change without benchmarking for t, m, s in zip(tensor, mean, std): t.sub_(m).div_(s) return tensor
Example #18
Source File: layers.py From deepchem with MIT License | 6 votes |
def build(self, input_shape): if isinstance(input_shape, collections.Sequence): input_shape = input_shape[0] out_channels = input_shape[1] if self.weights_initializer is None: weights_initializer = tf.keras.initializers.VarianceScaling else: weights_initializer = self.weights_initializer self.dense_H = tf.keras.layers.Dense( out_channels, activation=self.activation_fn, bias_initializer=self.biases_initializer, kernel_initializer=weights_initializer) self.dense_T = tf.keras.layers.Dense( out_channels, activation=tf.nn.sigmoid, bias_initializer=tf.constant_initializer(-1), kernel_initializer=weights_initializer) self.built = True
Example #19
Source File: input.py From lambda-packs with MIT License | 5 votes |
def _restore_sparse_tensors(stored_list, sparse_info_list): """Restore SparseTensors after dequeue in batch, batch_join, etc.""" received_sequence = isinstance(stored_list, collections.Sequence) if not received_sequence: stored_list = (stored_list,) tensors = [ _restore_sparse(sparse_map_op=info.map_op, sparse_handles=array_ops.squeeze(s, [1]), rank=(info.rank + 1).value) if info.sparse else s for (s, info) in zip(stored_list, sparse_info_list)] return tensors if received_sequence else tensors[0]
Example #20
Source File: config.py From flask-jwt-extended with MIT License | 5 votes |
def token_location(self): locations = current_app.config['JWT_TOKEN_LOCATION'] if isinstance(locations, str): locations = (locations,) elif not isinstance(locations, (Sequence, Set)): raise RuntimeError('JWT_TOKEN_LOCATION must be a sequence or a set') elif not locations: raise RuntimeError('JWT_TOKEN_LOCATION must contain at least one ' 'of "headers", "cookies", "query_string", or "json"') for location in locations: if location not in ('headers', 'cookies', 'query_string', 'json'): raise RuntimeError('JWT_TOKEN_LOCATION can only contain ' '"headers", "cookies", "query_string", or "json"') return locations
Example #21
Source File: nest.py From lambda-packs with MIT License | 5 votes |
def is_sequence(seq): """Returns a true if its input is a collections.Sequence (except strings). Args: seq: an input sequence. Returns: True if the sequence is a not a string and is a collections.Sequence. """ return (isinstance(seq, collections.Sequence) and not isinstance(seq, six.string_types))
Example #22
Source File: ops.py From lambda-packs with MIT License | 5 votes |
def internal_convert_n_to_tensor_or_indexed_slices(values, dtype=None, name=None, as_ref=False): """Converts `values` to a list of `Tensor` or `IndexedSlices` objects. Any `IndexedSlices` or `SparseTensor` objects in `values` are returned unmodified. Args: values: A list of `None`, `IndexedSlices`, `SparseTensor`, or objects that can be consumed by `convert_to_tensor()`. dtype: (Optional.) The required `DType` of the returned `Tensor` `IndexedSlices`. name: (Optional.) A name prefix to used when a new `Tensor` is created, in which case element `i` will be given the name `name + '_' + i`. as_ref: True if the caller wants the results as ref tensors. Returns: A list of `Tensor`, `IndexedSlices`, and/or `SparseTensor` objects. Raises: TypeError: If no conversion function is registered for an element in `values`. RuntimeError: If a registered conversion function returns an invalid value. """ if not isinstance(values, collections.Sequence): raise TypeError("values must be a list.") ret = [] for i, value in enumerate(values): if value is None: ret.append(value) else: n = None if name is None else "%s_%d" % (name, i) ret.append( internal_convert_to_tensor_or_indexed_slices( value, dtype=dtype, name=n, as_ref=as_ref)) return ret
Example #23
Source File: catalog.py From segpy with GNU Affero General Public License v3.0 | 5 votes |
def create(self): """Create a possibly more optimized representation of the mapping. In this worst case, this method returns an object which is essentially an immutable dictionary. In the best case, the space savings can be vast. Returns: A mapping, if a unique mapping from indexes to values is possible, otherwise None. """ # This method examines the contents of the mapping using # various heuristics to come up with a better representation. # In-place sort by index self._catalog.sort(key=first) if contains_duplicates(index for index, value in self._catalog): return None if all(isinstance(index, Sequence) and (len(index) == 2) for index, value in self._catalog): return self._create_catalog_2() return self._create_catalog_1()
Example #24
Source File: _typecheck.py From lambda-packs with MIT License | 5 votes |
def __instancecheck__(self, instance): return (isinstance(instance, collections.Sequence) and all(isinstance(x, self._type) for x in instance))
Example #25
Source File: layers.py From deepchem with MIT License | 5 votes |
def call(self, inputs): if isinstance(inputs, collections.Sequence): parent = inputs[0] else: parent = inputs dense_H = self.dense_H(parent) dense_T = self.dense_T(parent) return tf.multiply(dense_H, dense_T) + tf.multiply(parent, 1 - dense_T)
Example #26
Source File: dataloader.py From weakalign with MIT License | 5 votes |
def default_collate(batch): "Puts each data field into a tensor with outer dimension batch size" if torch.is_tensor(batch[0]): out = None if _use_shared_memory: # If we're in a background process, concatenate directly into a # shared memory tensor to avoid an extra copy numel = sum([x.numel() for x in batch]) storage = batch[0].storage()._new_shared(numel) out = batch[0].new(storage) return torch.stack(batch, 0, out=out) elif type(batch[0]).__module__ == 'numpy': elem = batch[0] if type(elem).__name__ == 'ndarray': return torch.stack([torch.from_numpy(b) for b in batch], 0) if elem.shape == (): # scalars py_type = float if elem.dtype.name.startswith('float') else int return numpy_type_map[elem.dtype.name](list(map(py_type, batch))) elif isinstance(batch[0], int): return torch.LongTensor(batch) elif isinstance(batch[0], float): return torch.DoubleTensor(batch) elif isinstance(batch[0], string_classes): return batch elif isinstance(batch[0], collections.Mapping): return {key: default_collate([d[key] for d in batch]) for key in batch[0]} elif isinstance(batch[0], collections.Sequence): transposed = zip(*batch) return [default_collate(samples) for samples in transposed] raise TypeError(("batch must contain tensors, numbers, dicts or lists; found {}" .format(type(batch[0]))))
Example #27
Source File: layers.py From deepchem with MIT License | 5 votes |
def call(self, inputs): if isinstance(inputs, collections.Sequence): if len(inputs) != 1: raise ValueError("NeighborList can only have one input") inputs = inputs[0] if len(inputs.get_shape()) != 2: # TODO(rbharath): Support batching raise ValueError("Parent tensor must be (num_atoms, ndum)") return self.compute_nbr_list(inputs)
Example #28
Source File: __init__.py From deepchem with MIT License | 5 votes |
def __init__(self, state_shape, n_actions=None, state_dtype=None, action_shape=None): """Subclasses should call the superclass constructor in addition to doing their own initialization. A value should be provided for either n_actions (for discrete action spaces) or action_shape (for continuous action spaces), but not both. Parameters ---------- state_shape: tuple or list of tuples the shape(s) of the array(s) making up the state n_actions: int the number of discrete actions that can be performed. If the action space is continuous, this should be None. state_dtype: dtype or list of dtypes the type(s) of the array(s) making up the state. If this is None, all arrays are assumed to be float32. action_shape: tuple the shape of the array describing an action. If the action space is discrete, this should be none. """ self._state_shape = state_shape self._n_actions = n_actions self._action_shape = action_shape self._state = None self._terminated = None if state_dtype is None: # Assume all arrays are float32. import numpy import collections if isinstance(state_shape[0], collections.Sequence): self._state_dtype = [numpy.float32] * len(state_shape) else: self._state_dtype = numpy.float32 else: self._state_dtype = state_dtype
Example #29
Source File: dataloader.py From weakalign with MIT License | 5 votes |
def pin_memory_batch(batch): if torch.is_tensor(batch): return batch.pin_memory() elif isinstance(batch, string_classes): return batch elif isinstance(batch, collections.Mapping): return {k: pin_memory_batch(sample) for k, sample in batch.items()} elif isinstance(batch, collections.Sequence): return [pin_memory_batch(sample) for sample in batch] else: return batch
Example #30
Source File: projection.py From resolwe with Apache License 2.0 | 5 votes |
def apply_projection(projection, value): """Apply projection.""" if isinstance(value, Sequence): # Apply projection to each item in the list. return [apply_projection(projection, item) for item in value] elif not isinstance(value, Mapping): # Non-dictionary values are simply ignored. return value # Extract projection for current level. try: current_projection = [p[0] for p in projection] except IndexError: return value # Apply projection. for name in list(value.keys()): if name not in current_projection: value.pop(name) elif isinstance(value[name], dict): # Apply projection recursively. value[name] = apply_projection( [p[1:] for p in projection if p[0] == name], value[name] ) return value