Python numpy.index_exp() Examples

The following are 30 code examples for showing how to use numpy.index_exp(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: recruit   Author: Frank-qlu   File: arrayprint.py    License: Apache License 2.0 6 votes vote down vote up
def _leading_trailing(a, edgeitems, index=()):
    """
    Keep only the N-D corners (leading and trailing edges) of an array.

    Should be passed a base-class ndarray, since it makes no guarantees about
    preserving subclasses.
    """
    axis = len(index)
    if axis == a.ndim:
        return a[index]

    if a.shape[axis] > 2*edgeitems:
        return concatenate((
            _leading_trailing(a, edgeitems, index + np.index_exp[ :edgeitems]),
            _leading_trailing(a, edgeitems, index + np.index_exp[-edgeitems:])
        ), axis=axis)
    else:
        return _leading_trailing(a, edgeitems, index + np.index_exp[:]) 
Example 2
Project: westpa   Author: westpa   File: w_pdist.py    License: MIT License 6 votes vote down vote up
def process_args(self, args):
        self.progress.process_args(args)
        self.data_reader.process_args(args)
        self.input_dssynth.h5filename = self.data_reader.we_h5filename
        self.input_dssynth.process_args(args)
        self.dsspec = self.input_dssynth.dsspec
        
        # Carrying an open HDF5 file across a fork() seems to corrupt the entire HDF5 library
        # Open the WEST HDF5 file just long enough to process our iteration range, then close
        # and reopen in go() [which executes after the fork]
        with self.data_reader:
            self.iter_range.process_args(args)
        
        self.wt_dsspec = SingleIterDSSpec(self.data_reader.we_h5filename, 'seg_index', slice=numpy.index_exp['weight'])
        
        self.binspec = args.bins
        self.output_filename = args.output
        self.ignore_out_of_range = bool(args.ignore_out_of_range)
        self.compress_output = args.compress or False 
Example 3
Project: westpa   Author: westpa   File: h5io.py    License: MIT License 6 votes vote down vote up
def get_iteration_slice(h5object, iter_start, iter_stop=None, iter_stride=None):
    '''Create a slice for data corresponding to iterations [iter_start,iter_stop),
    with stride iter_step, in the given ``h5object``.'''
    obj_iter_start, obj_iter_stop = get_iter_range(h5object)
    
    if iter_stop is None: iter_stop = iter_start+1
    if iter_stride is None: iter_stride = 1
    
    if iter_start < obj_iter_start:
        raise IndexError('data for iteration {} not available in dataset {!r}'.format(iter_start, h5object))
    elif iter_start > obj_iter_stop:
        raise IndexError('data for iteration {} not available in dataset {!r}'.format(iter_stop, h5object))
    
    start_index = iter_start - obj_iter_start
    stop_index = iter_stop - obj_iter_start
    return numpy.index_exp[start_index:stop_index:iter_stride]



        
    
###
# Axis label metadata
### 
Example 4
Project: westpa   Author: westpa   File: h5io.py    License: MIT License 6 votes vote down vote up
def from_string(cls, dsspec_string, default_h5file):
        alias = None
        
        h5file = default_h5file
        fields = dsspec_string.split(',')
        dsname = fields[0]
        slice = None
        
        for field in (field.strip() for field in fields[1:]):
            k,v = field.split('=')
            k = k.lower()
            if k == 'alias':
                alias = v
            elif k == 'slice':
                try:
                    slice = eval('numpy.index_exp' + v)
                except SyntaxError:
                    raise SyntaxError('invalid index expression {!r}'.format(v))
            elif k == 'file':
                h5file = v
            else:
                raise ValueError('invalid dataset option {!r}'.format(k))
            
        return cls(h5file, dsname, alias, slice) 
Example 5
Project: lambda-packs   Author: ryfeus   File: arrayprint.py    License: MIT License 6 votes vote down vote up
def _leading_trailing(a, edgeitems, index=()):
    """
    Keep only the N-D corners (leading and trailing edges) of an array.

    Should be passed a base-class ndarray, since it makes no guarantees about
    preserving subclasses.
    """
    axis = len(index)
    if axis == a.ndim:
        return a[index]

    if a.shape[axis] > 2*edgeitems:
        return concatenate((
            _leading_trailing(a, edgeitems, index + np.index_exp[ :edgeitems]),
            _leading_trailing(a, edgeitems, index + np.index_exp[-edgeitems:])
        ), axis=axis)
    else:
        return _leading_trailing(a, edgeitems, index + np.index_exp[:]) 
Example 6
Project: vnpy_crypto   Author: birforce   File: arrayprint.py    License: MIT License 6 votes vote down vote up
def _leading_trailing(a, edgeitems, index=()):
    """
    Keep only the N-D corners (leading and trailing edges) of an array.

    Should be passed a base-class ndarray, since it makes no guarantees about
    preserving subclasses.
    """
    axis = len(index)
    if axis == a.ndim:
        return a[index]

    if a.shape[axis] > 2*edgeitems:
        return concatenate((
            _leading_trailing(a, edgeitems, index + np.index_exp[ :edgeitems]),
            _leading_trailing(a, edgeitems, index + np.index_exp[-edgeitems:])
        ), axis=axis)
    else:
        return _leading_trailing(a, edgeitems, index + np.index_exp[:]) 
Example 7
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: arrayprint.py    License: MIT License 6 votes vote down vote up
def _leading_trailing(a, edgeitems, index=()):
    """
    Keep only the N-D corners (leading and trailing edges) of an array.

    Should be passed a base-class ndarray, since it makes no guarantees about
    preserving subclasses.
    """
    axis = len(index)
    if axis == a.ndim:
        return a[index]

    if a.shape[axis] > 2*edgeitems:
        return concatenate((
            _leading_trailing(a, edgeitems, index + np.index_exp[ :edgeitems]),
            _leading_trailing(a, edgeitems, index + np.index_exp[-edgeitems:])
        ), axis=axis)
    else:
        return _leading_trailing(a, edgeitems, index + np.index_exp[:]) 
Example 8
Project: nata   Author: GoLP-IST   File: axes.py    License: MIT License 6 votes vote down vote up
def __getitem__(
        self, key: Union[int, slice, Tuple[Union[int, slice]]]
    ) -> "GridAxis":
        if not is_basic_indexing(key):
            raise IndexError("Only basic indexing is supported!")

        key = np.index_exp[key]
        requires_new_axis = False

        # first index corresponds to temporal slicing if ndim == axis_dim + 1
        # or alternatively -> check len of the axis -> number of temporal slices
        if len(self) != 1:
            # revert dimensionality reduction
            if isinstance(key[0], int):
                requires_new_axis = True
        else:
            requires_new_axis = True

        return self.__class__(
            self.data[key][np.newaxis] if requires_new_axis else self.data[key],
            name=self.name,
            label=self.label,
            unit=self.unit,
            axis_type=self.axis_type,
        ) 
Example 9
Project: nata   Author: GoLP-IST   File: types.py    License: MIT License 6 votes vote down vote up
def is_basic_indexing(key: Any):
    indexing = np.index_exp[key]
    passes = []
    for ind in indexing:
        if isinstance(ind, (int, slice)):
            passes.append(True)
        elif ind is Ellipsis:
            passes.append(True)
        elif ind is np.newaxis:
            passes.append(True)
        else:
            passes.append(False)

    if all(passes):
        return True
    return False 
Example 10
Project: GraphicDesignPatternByPython   Author: Relph1119   File: arrayprint.py    License: MIT License 6 votes vote down vote up
def _leading_trailing(a, edgeitems, index=()):
    """
    Keep only the N-D corners (leading and trailing edges) of an array.

    Should be passed a base-class ndarray, since it makes no guarantees about
    preserving subclasses.
    """
    axis = len(index)
    if axis == a.ndim:
        return a[index]

    if a.shape[axis] > 2*edgeitems:
        return concatenate((
            _leading_trailing(a, edgeitems, index + np.index_exp[ :edgeitems]),
            _leading_trailing(a, edgeitems, index + np.index_exp[-edgeitems:])
        ), axis=axis)
    else:
        return _leading_trailing(a, edgeitems, index + np.index_exp[:]) 
Example 11
def _leading_trailing(a, edgeitems, index=()):
    """
    Keep only the N-D corners (leading and trailing edges) of an array.

    Should be passed a base-class ndarray, since it makes no guarantees about
    preserving subclasses.
    """
    axis = len(index)
    if axis == a.ndim:
        return a[index]

    if a.shape[axis] > 2*edgeitems:
        return concatenate((
            _leading_trailing(a, edgeitems, index + np.index_exp[ :edgeitems]),
            _leading_trailing(a, edgeitems, index + np.index_exp[-edgeitems:])
        ), axis=axis)
    else:
        return _leading_trailing(a, edgeitems, index + np.index_exp[:]) 
Example 12
Project: pySINDy   Author: luckystarufo   File: arrayprint.py    License: MIT License 6 votes vote down vote up
def _leading_trailing(a, edgeitems, index=()):
    """
    Keep only the N-D corners (leading and trailing edges) of an array.

    Should be passed a base-class ndarray, since it makes no guarantees about
    preserving subclasses.
    """
    axis = len(index)
    if axis == a.ndim:
        return a[index]

    if a.shape[axis] > 2*edgeitems:
        return concatenate((
            _leading_trailing(a, edgeitems, index + np.index_exp[ :edgeitems]),
            _leading_trailing(a, edgeitems, index + np.index_exp[-edgeitems:])
        ), axis=axis)
    else:
        return _leading_trailing(a, edgeitems, index + np.index_exp[:]) 
Example 13
Project: plat   Author: dribnet   File: canvas.py    License: MIT License 6 votes vote down vote up
def additive_composite(src, src_mask, dst):
    '''
    Return the additive composite of src and dst.
    '''
    out = np.empty(dst.shape, dtype = 'float')
    alpha = np.index_exp[3:, :, :]
    rgb = np.index_exp[:3, :, :]
    if src_mask is not None:
        out[alpha] = np.maximum(src_mask,dst[alpha])
    else:
        out[alpha] = 1.0
    out[rgb] = np.maximum(src[rgb],dst[rgb])
    np.clip(out,0,1.0)
    return out

# gsize = 64
# gsize2 = gsize/2 
Example 14
Project: RC-experiments   Author: cairoHy   File: rc_dataset.py    License: GNU General Public License v3.0 6 votes vote down vote up
def get_next_batch(self, mode, idx):
        """
        return next batch of data samples
        """
        batch_size = self.args.batch_size
        if mode == "train":
            dataset = self.train_data
            sample_num = self.train_sample_num
        elif mode == "valid":
            dataset = self.valid_data
            sample_num = self.valid_sample_num
        else:
            dataset = self.test_data
            sample_num = self.test_sample_num
        if mode == "train":
            start = self.train_idx[idx] * batch_size
            stop = (self.train_idx[idx] + 1) * batch_size
        else:
            start = idx * batch_size
            stop = (idx + 1) * batch_size if start < sample_num and (idx + 1) * batch_size < sample_num else -1
        samples = batch_size if stop != -1 else len(dataset[0]) - start
        _slice = np.index_exp[start:stop]
        return self.next_batch_feed_dict_by_dataset(dataset, _slice, samples) 
Example 15
Project: coffeegrindsize   Author: jgagneastro   File: arrayprint.py    License: MIT License 6 votes vote down vote up
def _leading_trailing(a, edgeitems, index=()):
    """
    Keep only the N-D corners (leading and trailing edges) of an array.

    Should be passed a base-class ndarray, since it makes no guarantees about
    preserving subclasses.
    """
    axis = len(index)
    if axis == a.ndim:
        return a[index]

    if a.shape[axis] > 2*edgeitems:
        return concatenate((
            _leading_trailing(a, edgeitems, index + np.index_exp[ :edgeitems]),
            _leading_trailing(a, edgeitems, index + np.index_exp[-edgeitems:])
        ), axis=axis)
    else:
        return _leading_trailing(a, edgeitems, index + np.index_exp[:]) 
Example 16
Project: Carnets   Author: holzschu   File: arrayprint.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _leading_trailing(a, edgeitems, index=()):
    """
    Keep only the N-D corners (leading and trailing edges) of an array.

    Should be passed a base-class ndarray, since it makes no guarantees about
    preserving subclasses.
    """
    axis = len(index)
    if axis == a.ndim:
        return a[index]

    if a.shape[axis] > 2*edgeitems:
        return concatenate((
            _leading_trailing(a, edgeitems, index + np.index_exp[ :edgeitems]),
            _leading_trailing(a, edgeitems, index + np.index_exp[-edgeitems:])
        ), axis=axis)
    else:
        return _leading_trailing(a, edgeitems, index + np.index_exp[:]) 
Example 17
Project: Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda   Author: PacktPublishing   File: arrayprint.py    License: MIT License 6 votes vote down vote up
def _leading_trailing(a, edgeitems, index=()):
    """
    Keep only the N-D corners (leading and trailing edges) of an array.

    Should be passed a base-class ndarray, since it makes no guarantees about
    preserving subclasses.
    """
    axis = len(index)
    if axis == a.ndim:
        return a[index]

    if a.shape[axis] > 2*edgeitems:
        return concatenate((
            _leading_trailing(a, edgeitems, index + np.index_exp[ :edgeitems]),
            _leading_trailing(a, edgeitems, index + np.index_exp[-edgeitems:])
        ), axis=axis)
    else:
        return _leading_trailing(a, edgeitems, index + np.index_exp[:]) 
Example 18
Project: twitter-stock-recommendation   Author: alvarobartt   File: arrayprint.py    License: MIT License 6 votes vote down vote up
def _leading_trailing(a, edgeitems, index=()):
    """
    Keep only the N-D corners (leading and trailing edges) of an array.

    Should be passed a base-class ndarray, since it makes no guarantees about
    preserving subclasses.
    """
    axis = len(index)
    if axis == a.ndim:
        return a[index]

    if a.shape[axis] > 2*edgeitems:
        return concatenate((
            _leading_trailing(a, edgeitems, index + np.index_exp[ :edgeitems]),
            _leading_trailing(a, edgeitems, index + np.index_exp[-edgeitems:])
        ), axis=axis)
    else:
        return _leading_trailing(a, edgeitems, index + np.index_exp[:]) 
Example 19
Project: ffn   Author: google   File: bounding_box.py    License: Apache License 2.0 5 votes vote down vote up
def to_slice(self):
    """Returns slice in C-order (ZYX)."""
    return np.index_exp[self.start[2]:self.end[2],  #
                        self.start[1]:self.end[1],  #
                        self.start[0]:self.end[0]] 
Example 20
Project: westpa   Author: westpa   File: ploterr.py    License: MIT License 5 votes vote down vote up
def process_args(self, args):
        self.output_filename = args.output
        (pathname, slicestr) = re.search(r'([^[]+)(\[[^\]]+\])?$', args.dsspec).groups()
        if slicestr:
            sl = eval('numpy.index_exp' + slicestr)
        else:
            sl = numpy.index_exp[...]
        self.h5file, self.h5dset = h5io.resolve_filepath(pathname, mode='r')
        self.dset_slice = sl 
Example 21
Project: westpa   Author: westpa   File: data_reader.py    License: MIT License 5 votes vote down vote up
def weight_dsspec(self):
        if self._weight_dsspec is None:
            assert self.we_h5filename is not None
            self._weight_dsspec = SingleIterDSSpec(self.we_h5filename, 'seg_index', slice=index_exp['weight'])
        return self._weight_dsspec 
Example 22
Project: westpa   Author: westpa   File: data_reader.py    License: MIT License 5 votes vote down vote up
def parent_id_dsspec(self):
        if self._parent_id_dsspec is None:
            assert self.we_h5filename is not None
            #self._parent_id_dsspec = SingleIterDSSpec(self.we_h5filename, 'seg_index', slice=index_exp['parent_id'])
            self._parent_id_dsspec = FnDSSpec(self.we_h5filename, _get_parent_ids)
        return self._parent_id_dsspec 
Example 23
Project: westpa   Author: westpa   File: w_assign.py    License: MIT License 5 votes vote down vote up
def _assign_label_pop(n_iter, lb, ub, mapper, nstates, state_map, last_labels, parent_id_dsspec, weight_dsspec, pcoord_dsspec, subsample):

    nbins = len(state_map)-1
    parent_ids = parent_id_dsspec.get_iter_data(n_iter,index_exp[lb:ub])
    weights = weight_dsspec.get_iter_data(n_iter,index_exp[lb:ub])
    pcoords = pcoord_dsspec.get_iter_data(n_iter,index_exp[lb:ub])
    
    assignments, trajlabels, statelabels = assign_and_label(lb, ub, parent_ids,
                                               mapper.assign, nstates, state_map, last_labels, pcoords, subsample)
    pops = numpy.zeros((nstates+1,nbins+1), weight_dtype)
    accumulate_labeled_populations(weights, assignments, trajlabels, pops)
    return (assignments, trajlabels, pops, lb, ub, statelabels) 
Example 24
Project: westpa   Author: westpa   File: h5io.py    License: MIT License 5 votes vote down vote up
def get_iteration_entry(h5object, n_iter):
    '''Create a slice for data corresponding to iteration ``n_iter`` in ``h5object``.'''
    obj_iter_start, obj_iter_stop = get_iter_range(h5object)
    if n_iter < obj_iter_start or n_iter >= obj_iter_stop:
        raise IndexError('data for iteration {} not available in dataset {!r}'.format(n_iter, h5object))
    return numpy.index_exp[n_iter-obj_iter_start] 
Example 25
Project: westpa   Author: westpa   File: h5io.py    License: MIT License 5 votes vote down vote up
def get_iter_data(self, n_iter, seg_slice=index_exp[:]):
        raise NotImplementedError 
Example 26
Project: westpa   Author: westpa   File: h5io.py    License: MIT License 5 votes vote down vote up
def get_iter_data(self, n_iter, seg_slice=index_exp[:]):
        if self.slice:
            return self.h5file.get_iter_group(n_iter)[self.dsname][seg_slice + self.slice]
        else:
            return self.h5file.get_iter_group(n_iter)[self.dsname][seg_slice] 
Example 27
Project: westpa   Author: westpa   File: h5io.py    License: MIT License 5 votes vote down vote up
def get_iter_data(self, n_iter, seg_slice=index_exp[:]):
        if self.slice:
            return self.h5file.get_iter_group(n_iter)[self.dsname][seg_slice + index_exp[:] + self.slice]
        else:
            return self.h5file.get_iter_group(n_iter)[self.dsname][seg_slice] 
Example 28
Project: westpa   Author: westpa   File: h5io.py    License: MIT License 5 votes vote down vote up
def get_segment_data(self, n_iter, seg_id):
        if self.slice:
            return self.h5file.get_iter_group(n_iter)[numpy.index_exp[seg_id,:] + self.slice]
        else:
            return self.h5file.get_iter_group(n_iter)[seg_id] 
Example 29
Project: westpa   Author: westpa   File: h5io.py    License: MIT License 5 votes vote down vote up
def get_iter_data(self, n_iter, seg_slice=index_exp[:]):
        return self.fn(n_iter, self.h5file.get_iter_group(n_iter))[seg_slice] 
Example 30
Project: rpitelecine   Author: Alexamder   File: perforation.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setROI(self):
        # Sets the ROI where to look for a perforation
        # If an expected perforation size is set, then ROI is based on size of perforation
        img_h,img_w = self.imageSize
        if self.isInitialised:
            # Already know expected size, so use smaller ROI
            # ROI height and position on Y axis
            # Top of ROI for initialised perforation detection
            h = int(img_h/2)  # Use 1/2 of image height for ROI
            if self.filmType == 'super8':
                # Middle of image height
                y = int(img_h/4)
            else:
                # Standard 8 - top part of image
                y = int(img_h/50)  # 39 pixels with 1944px high image
            # Base width on previously detected perforation - centre ib ROIcx
            w = int((self.expectedSize[0] + (self.expectedSize[0]*self.sizeMargin))/2)
            roiL = max(0, self.ROIcentrexy[0]-w)
            roiR = min(img_w, self.ROIcentrexy[0]+w)
            self.ROIcentrexy = [ int(roiL+(roiR-roiL)/2), int(y+(h/2)) ]
        else:
            # Not found before - so use larger area for detection
            # Use whole image height + half image width
            y = 0
            h = img_h
            roiL = 0
            roiR = int(img_w/2)
            self.ROIcentrexy = [0,0]
        self.ROIxy = ( roiL, y )
        self.ROIwh = ( roiR-roiL, h )
        self.ROIslice = np.index_exp[ y:y+h, roiL:roiR ]         # Create the slice object for making the ROI
        self.ROIimg = np.zeros( (roiR-roiL, h), dtype=np.uint8)  # Initialise space for the ROI image