Python numpy.logical_and() Examples

The following are 30 code examples for showing how to use numpy.logical_and(). 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: neuropythy   Author: noahbenson   File: core.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def dataframe_select(df, *cols, **filters):
    '''
    dataframe_select(df, k1=v1, k2=v2...) yields df after selecting all the columns in which the
      given keys (k1, k2, etc.) have been selected such that the associated columns in the dataframe
      contain only the rows whose cells match the given values.
    dataframe_select(df, col1, col2...) selects the given columns.
    dataframe_select(df, col1, col2..., k1=v1, k2=v2...) selects both.
    
    If a value is a tuple/list of 2 elements, then it is considered a range where cells must fall
    between the values. If value is a tuple/list of more than 2 elements or is a set of any length
    then it is a list of values, any one of which can match the cell.
    '''
    ii = np.ones(len(df), dtype='bool')
    for (k,v) in six.iteritems(filters):
        vals = df[k].values
        if   pimms.is_set(v):                    jj = np.isin(vals, list(v))
        elif pimms.is_vector(v) and len(v) == 2: jj = (v[0] <= vals) & (vals < v[1])
        elif pimms.is_vector(v):                 jj = np.isin(vals, list(v))
        else:                                    jj = (vals == v)
        ii = np.logical_and(ii, jj)
    if len(ii) != np.sum(ii): df = df.loc[ii]
    if len(cols) > 0: df = df[list(cols)]
    return df 
Example 2
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: detection.py    License: Apache License 2.0 6 votes vote down vote up
def _update_labels(self, label, crop_box, height, width):
        """Convert labels according to crop box"""
        xmin = float(crop_box[0]) / width
        ymin = float(crop_box[1]) / height
        w = float(crop_box[2]) / width
        h = float(crop_box[3]) / height
        out = label.copy()
        out[:, (1, 3)] -= xmin
        out[:, (2, 4)] -= ymin
        out[:, (1, 3)] /= w
        out[:, (2, 4)] /= h
        out[:, 1:5] = np.maximum(0, out[:, 1:5])
        out[:, 1:5] = np.minimum(1, out[:, 1:5])
        coverage = self._calculate_areas(out[:, 1:]) * w * h / self._calculate_areas(label[:, 1:])
        valid = np.logical_and(out[:, 3] > out[:, 1], out[:, 4] > out[:, 2])
        valid = np.logical_and(valid, coverage > self.min_eject_coverage)
        valid = np.where(valid)[0]
        if valid.size < 1:
            return None
        out = out[valid, :]
        return out 
Example 3
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: detection.py    License: Apache License 2.0 6 votes vote down vote up
def _parse_label(self, label):
        """Helper function to parse object detection label.

        Format for raw label:
        n \t k \t ... \t [id \t xmin\t ymin \t xmax \t ymax \t ...] \t [repeat]
        where n is the width of header, 2 or larger
        k is the width of each object annotation, can be arbitrary, at least 5
        """
        if isinstance(label, nd.NDArray):
            label = label.asnumpy()
        raw = label.ravel()
        if raw.size < 7:
            raise RuntimeError("Label shape is invalid: " + str(raw.shape))
        header_width = int(raw[0])
        obj_width = int(raw[1])
        if (raw.size - header_width) % obj_width != 0:
            msg = "Label shape %s inconsistent with annotation width %d." \
                %(str(raw.shape), obj_width)
            raise RuntimeError(msg)
        out = np.reshape(raw[header_width:], (-1, obj_width))
        # remove bad ground-truths
        valid = np.where(np.logical_and(out[:, 3] > out[:, 1], out[:, 4] > out[:, 2]))[0]
        if valid.size < 1:
            raise RuntimeError('Encounter sample with no valid label.')
        return out[valid, :] 
Example 4
Project: DOTA_models   Author: ringringyi   File: graph_utils.py    License: Apache License 2.0 6 votes vote down vote up
def get_hardness_distribution(gtG, max_dist, min_dist, rng, trials, bins, nodes,
                              n_ori, step_size):
  heuristic_fn = lambda node_ids, node_id: \
    heuristic_fn_vec(nodes[node_ids, :], nodes[[node_id], :], n_ori, step_size)
  num_nodes = gtG.num_vertices()
  gt_dists = []; h_dists = [];
  for i in range(trials):
    end_node_id = rng.choice(num_nodes)
    gt_dist = gt.topology.shortest_distance(gt.GraphView(gtG, reversed=True),
                                            source=gtG.vertex(end_node_id),
                                            target=None, max_dist=max_dist)
    gt_dist = np.array(gt_dist.get_array())
    ind = np.where(np.logical_and(gt_dist <= max_dist, gt_dist >= min_dist))[0]
    gt_dist = gt_dist[ind]
    h_dist = heuristic_fn(ind, end_node_id)[:,0]
    gt_dists.append(gt_dist)
    h_dists.append(h_dist)
  gt_dists = np.concatenate(gt_dists)
  h_dists = np.concatenate(h_dists)
  hardness = 1. - h_dists*1./gt_dists
  hist, _ = np.histogram(hardness, bins)
  hist = hist.astype(np.float64)
  hist = hist / np.sum(hist)
  return hist 
Example 5
Project: DOTA_models   Author: ringringyi   File: balanced_positive_negative_sampler_test.py    License: Apache License 2.0 6 votes vote down vote up
def test_subsample_all_examples(self):
    numpy_labels = np.random.permutation(300)
    indicator = tf.constant(np.ones(300) == 1)
    numpy_labels = (numpy_labels - 200) > 0

    labels = tf.constant(numpy_labels)

    sampler = (balanced_positive_negative_sampler.
               BalancedPositiveNegativeSampler())
    is_sampled = sampler.subsample(indicator, 64, labels)
    with self.test_session() as sess:
      is_sampled = sess.run(is_sampled)
      self.assertTrue(sum(is_sampled) == 64)
      self.assertTrue(sum(np.logical_and(numpy_labels, is_sampled)) == 32)
      self.assertTrue(sum(np.logical_and(
          np.logical_not(numpy_labels), is_sampled)) == 32) 
Example 6
Project: EXOSIMS   Author: dsavransky   File: SurveySimulation.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def is_earthlike(self, plan_inds, sInd):
        """
        Is the planet earthlike?
        """
        TL = self.TargetList
        SU = self.SimulatedUniverse
        PPop = self.PlanetPopulation

        # extract planet and star properties
        Rp_plan = SU.Rp[plan_inds].value
        L_star = TL.L[sInd]
        if PPop.scaleOrbits:
            a_plan = (SU.a[plan_inds]/np.sqrt(L_star)).value
        else:
            a_plan = (SU.a[plan_inds]).value
        # Definition: planet radius (in earth radii) and solar-equivalent luminosity must be
        # between the given bounds.
        Rp_plan_lo = 0.80/np.sqrt(a_plan)
        # We use the numpy versions so that plan_ind can be a numpy vector.
        return np.logical_and(
           np.logical_and(Rp_plan >= Rp_plan_lo, Rp_plan <= 1.4),
           np.logical_and(a_plan  >= 0.95,     a_plan  <= 1.67)) 
Example 7
Project: object_detector_app   Author: datitran   File: balanced_positive_negative_sampler_test.py    License: MIT License 6 votes vote down vote up
def test_subsample_selection(self):
    # Test random sampling when only some examples can be sampled:
    # 100 samples, 20 positives, 10 positives cannot be sampled
    numpy_labels = np.arange(100)
    numpy_indicator = numpy_labels < 90
    indicator = tf.constant(numpy_indicator)
    numpy_labels = (numpy_labels - 80) >= 0

    labels = tf.constant(numpy_labels)

    sampler = (balanced_positive_negative_sampler.
               BalancedPositiveNegativeSampler())
    is_sampled = sampler.subsample(indicator, 64, labels)
    with self.test_session() as sess:
      is_sampled = sess.run(is_sampled)
      self.assertTrue(sum(is_sampled) == 64)
      self.assertTrue(sum(np.logical_and(numpy_labels, is_sampled)) == 10)
      self.assertTrue(sum(np.logical_and(
          np.logical_not(numpy_labels), is_sampled)) == 54)
      self.assertAllEqual(is_sampled, np.logical_and(is_sampled,
                                                     numpy_indicator)) 
Example 8
Project: pymoo   Author: msu-coinlab   File: usage_ga_custom.py    License: Apache License 2.0 6 votes vote down vote up
def _do(self, problem, X, **kwargs):
        n_parents, n_matings, n_var = X.shape

        _X = np.full((self.n_offsprings, n_matings, problem.n_var), False)

        for k in range(n_matings):
            p1, p2 = X[0, k], X[1, k]

            both_are_true = np.logical_and(p1, p2)
            _X[0, k, both_are_true] = True

            n_remaining = problem.n_max - np.sum(both_are_true)

            I = np.where(np.logical_xor(p1, p2))[0]

            S = I[np.random.permutation(len(I))][:n_remaining]
            _X[0, k, S] = True

        return _X 
Example 9
Project: pymoo   Author: msu-coinlab   File: subset.py    License: Apache License 2.0 6 votes vote down vote up
def _do(self, problem, X, **kwargs):
        n_parents, n_matings, n_var = X.shape

        _X = np.full((self.n_offsprings, n_matings, problem.n_var), False)

        for k in range(n_matings):
            p1, p2 = X[0, k], X[1, k]

            both_are_true = np.logical_and(p1, p2)
            _X[0, k, both_are_true] = True

            n_remaining = problem.n_max - np.sum(both_are_true)

            I = np.where(np.logical_xor(p1, p2))[0]

            S = I[np.random.permutation(len(I))][:n_remaining]
            _X[0, k, S] = True

        return _X 
Example 10
Project: NeuroKit   Author: neuropsychology   File: fractal_mandelbrot.py    License: MIT License 6 votes vote down vote up
def _mandelbrot(size=1000, real_range=(-2, 2), imaginary_range=(-2, 2), iterations=25, threshold=4):

    img, c = _mandelbrot_initialize(size=size, real_range=real_range, imaginary_range=imaginary_range)

    optim = _mandelbrot_optimize(c)

    z = np.copy(c)
    for i in range(1, iterations + 1):  # pylint: disable=W0612
        # Continue only where smaller than threshold
        mask = (z * z.conjugate()).real < threshold
        mask = np.logical_and(mask, optim)

        if np.all(~mask) is True:
            break

        # Increase
        img[mask] += 1

        # Iterate based on Mandelbrot equation
        z[mask] = z[mask] ** 2 + c[mask]

    # Fill otpimized area
    img[~optim] = np.max(img)
    return img 
Example 11
Project: NeuroKit   Author: neuropsychology   File: fractal_mandelbrot.py    License: MIT License 6 votes vote down vote up
def _mandelbrot_optimize(c):
    # Optimizations: most of the mset points lie within the
    # within the cardioid or in the period-2 bulb. (The two most
    # prominant shapes in the mandelbrot set. We can eliminate these
    # from our search straight away and save alot of time.
    # see: http://en.wikipedia.org/wiki/Mandelbrot_set#Optimizations

    # First eliminate points within the cardioid
    p = (((c.real - 0.25) ** 2) + (c.imag ** 2)) ** 0.5
    mask1 = c.real > p - (2 * p ** 2) + 0.25

    # Next eliminate points within the period-2 bulb
    mask2 = ((c.real + 1) ** 2) + (c.imag ** 2) > 0.0625

    # Combine masks
    mask = np.logical_and(mask1, mask2)
    return mask 
Example 12
Project: NeuroKit   Author: neuropsychology   File: signal_fixpeaks.py    License: MIT License 6 votes vote down vote up
def _correct_missed(missed_idcs, peaks):

    corrected_peaks = peaks.copy()
    missed_idcs = np.array(missed_idcs)
    # Calculate the position(s) of new beat(s). Make sure to not generate
    # negative indices. prev_peaks and next_peaks must have the same
    # number of elements.
    valid_idcs = np.logical_and(missed_idcs > 1, missed_idcs < len(corrected_peaks))  # pylint: disable=E1111
    missed_idcs = missed_idcs[valid_idcs]
    prev_peaks = corrected_peaks[[i - 1 for i in missed_idcs]]
    next_peaks = corrected_peaks[missed_idcs]
    added_peaks = prev_peaks + (next_peaks - prev_peaks) / 2
    # Add the new peaks before the missed indices (see numpy docs).
    corrected_peaks = np.insert(corrected_peaks, missed_idcs, added_peaks)

    return corrected_peaks 
Example 13
Project: NeuroKit   Author: neuropsychology   File: signal_fixpeaks.py    License: MIT License 6 votes vote down vote up
def _correct_misaligned(misaligned_idcs, peaks):

    corrected_peaks = peaks.copy()
    misaligned_idcs = np.array(misaligned_idcs)
    # Make sure to not generate negative indices, or indices that exceed
    # the total number of peaks. prev_peaks and next_peaks must have the
    # same number of elements.
    valid_idcs = np.logical_and(
        misaligned_idcs > 1, misaligned_idcs < len(corrected_peaks) - 1  # pylint: disable=E1111
    )
    misaligned_idcs = misaligned_idcs[valid_idcs]
    prev_peaks = corrected_peaks[[i - 1 for i in misaligned_idcs]]
    next_peaks = corrected_peaks[[i + 1 for i in misaligned_idcs]]

    half_ibi = (next_peaks - prev_peaks) / 2
    peaks_interp = prev_peaks + half_ibi
    # Shift the R-peaks from the old to the new position.
    corrected_peaks = np.delete(corrected_peaks, misaligned_idcs)
    corrected_peaks = np.concatenate((corrected_peaks, peaks_interp)).astype(int)
    corrected_peaks.sort(kind="mergesort")

    return corrected_peaks 
Example 14
Project: NeuroKit   Author: neuropsychology   File: tests_complexity.py    License: MIT License 6 votes vote down vote up
def pyeeg_ap_entropy(X, M, R):
    N = len(X)

    Em = pyeeg_embed_seq(X, 1, M)
    A = np.tile(Em, (len(Em), 1, 1))
    B = np.transpose(A, [1, 0, 2])
    D = np.abs(A - B)  # D[i,j,k] = |Em[i][k] - Em[j][k]|
    InRange = np.max(D, axis=2) <= R

    # Probability that random M-sequences are in range
    Cm = InRange.mean(axis=0)

    # M+1-sequences in range if M-sequences are in range & last values are close
    Dp = np.abs(np.tile(X[M:], (N - M, 1)) - np.tile(X[M:], (N - M, 1)).T)

    Cmp = np.logical_and(Dp <= R, InRange[:-1, :-1]).mean(axis=0)

    Phi_m, Phi_mp = np.sum(np.log(Cm)), np.sum(np.log(Cmp))

    Ap_En = (Phi_m - Phi_mp) / (N - M)

    return Ap_En 
Example 15
Project: NeuroKit   Author: neuropsychology   File: tests_complexity.py    License: MIT License 6 votes vote down vote up
def pyeeg_samp_entropy(X, M, R):
    N = len(X)

    Em = pyeeg_embed_seq(X, 1, M)[:-1]
    A = np.tile(Em, (len(Em), 1, 1))
    B = np.transpose(A, [1, 0, 2])
    D = np.abs(A - B)  # D[i,j,k] = |Em[i][k] - Em[j][k]|
    InRange = np.max(D, axis=2) <= R
    np.fill_diagonal(InRange, 0)  # Don't count self-matches

    Cm = InRange.sum(axis=0)  # Probability that random M-sequences are in range
    Dp = np.abs(np.tile(X[M:], (N - M, 1)) - np.tile(X[M:], (N - M, 1)).T)

    Cmp = np.logical_and(Dp <= R, InRange).sum(axis=0)

    # Avoid taking log(0)
    Samp_En = np.log(np.sum(Cm + 1e-100) / np.sum(Cmp + 1e-100))

    return Samp_En


# =============================================================================
# Entropy
# ============================================================================= 
Example 16
Project: radiometric_normalization   Author: planetlabs   File: display_wrapper.py    License: Apache License 2.0 6 votes vote down vote up
def create_pixel_plots(candidate_path, reference_path, base_name,
                       last_band_alpha=False, limits=None, custom_alpha=None):
    c_ds, c_alpha, c_band_count = _open_image_and_get_info(
        candidate_path, last_band_alpha)
    r_ds, r_alpha, r_band_count = _open_image_and_get_info(
        reference_path, last_band_alpha)

    _assert_consistent(c_alpha, r_alpha, c_band_count, r_band_count)

    if custom_alpha != None:
        combined_alpha = custom_alpha
    else:
        combined_alpha = numpy.logical_and(c_alpha, r_alpha)
    valid_pixels = numpy.nonzero(combined_alpha)

    for band_no in range(1, c_band_count + 1):
        c_band = gimage.read_single_band(c_ds, band_no)
        r_band = gimage.read_single_band(r_ds, band_no)
        file_name = '{}_{}.png'.format(base_name, band_no)
        display.plot_pixels(file_name, c_band[valid_pixels],
                            r_band[valid_pixels], limits) 
Example 17
Project: radiometric_normalization   Author: planetlabs   File: display_wrapper.py    License: Apache License 2.0 6 votes vote down vote up
def create_all_bands_histograms(candidate_path, reference_path, base_name,
                                last_band_alpha=False,
                                color_order=['b', 'g', 'r', 'y'],
                                x_limits=None, y_limits=None):
    c_gimg = gimage.load(candidate_path, last_band_alpha=last_band_alpha)
    r_gimg = gimage.load(reference_path, last_band_alpha=last_band_alpha)

    gimage.check_comparable([c_gimg, r_gimg])

    combined_alpha = numpy.logical_and(c_gimg.alpha, r_gimg.alpha)
    valid_pixels = numpy.nonzero(combined_alpha)

    file_name = '{}_histograms.png'.format(base_name)
    display.plot_histograms(
        file_name,
        [c_band[valid_pixels] for c_band in c_gimg.bands],
        [r_band[valid_pixels] for r_band in r_gimg.bands],
        color_order, x_limits, y_limits) 
Example 18
Project: PyRadarMet   Author: nguy   File: zdrcal.py    License: GNU General Public License v2.0 6 votes vote down vote up
def mask_variables(ZDR, dBZ, RhoHV, PhiDP, KDP, rhv_min=0.8):
    # Combine the masks for all variables to ensure no missing data points
    maskZDR = np.ma.getmask(ZDR)
    maskZ = np.ma.getmask(dBZ)
    maskRhv = np.ma.getmask(RhoHV)
    maskPdp = np.ma.getmask(PhiDP)
    is_cor = RhoHV < rhv_min # Mask values where Correl Coeff < threshold 
    
    mskt1 = np.logical_and(maskZ, maskZDR) # Combine refl and ZDR masks
    mskt2 = np.logical_and(mskt1, maskRhv) # Combine with missing Rho HV mask
    mskt3 = np.logical_and(mskt2, is_cor) # Combine with min threshold Rho HV
    mask = np.logical_and(mskt3, maskPdp) # Combine with missing Phidp mask
    
    ZDR = np.ma.masked_where(mask, ZDR)
    dBZ = np.ma.masked_where(mask, dBZ)
    Rhv = np.ma.masked_where(mask, RhoHV)
    PhiDP = np.ma.masked_where(mask, PhiDP)
    KDP = np.ma.masked_where(mask, KDP)
    
    return ZDR, dBZ, Rhv, PhiDP, KDP
#====================================================================== 
Example 19
Project: connecting_the_dots   Author: autonomousvision   File: geometry.py    License: MIT License 6 votes vote down vote up
def color_pcl(pcl, K, im, color_axis=0, as_int=True, invalid_color=[0,0,0]):
  uvd = K @ pcl.T
  uvd /= uvd[2]
  uvd = np.round(uvd).astype(np.int32)
  mask = np.logical_and(uvd[0] >= 0, uvd[1] >= 0)
  color = np.empty((pcl.shape[0], 3), dtype=im.dtype)
  if color_axis == 0:
    mask = np.logical_and(mask, uvd[0] < im.shape[2])
    mask = np.logical_and(mask, uvd[1] < im.shape[1])
    uvd = uvd[:,mask]
    color[mask,:] = im[:,uvd[1],uvd[0]].T
  elif color_axis == 2:
    mask = np.logical_and(mask, uvd[0] < im.shape[1])
    mask = np.logical_and(mask, uvd[1] < im.shape[0])
    uvd = uvd[:,mask]
    color[mask,:] = im[uvd[1],uvd[0], :]
  else:
    raise Exception('invalid color_axis')
  color[np.logical_not(mask),:3] = invalid_color
  if as_int:
    color = (255.0 * color).astype(np.int32)
  return color 
Example 20
def test_subsample_all_examples_static(self):
    numpy_labels = np.random.permutation(300)
    indicator = np.array(np.ones(300) == 1, np.bool)
    numpy_labels = (numpy_labels - 200) > 0

    labels = np.array(numpy_labels, np.bool)

    def graph_fn(indicator, labels):
      sampler = (
          balanced_positive_negative_sampler.BalancedPositiveNegativeSampler(
              is_static=True))
      return sampler.subsample(indicator, 64, labels)

    is_sampled = self.execute(graph_fn, [indicator, labels])
    self.assertTrue(sum(is_sampled) == 64)
    self.assertTrue(sum(np.logical_and(numpy_labels, is_sampled)) == 32)
    self.assertTrue(sum(np.logical_and(
        np.logical_not(numpy_labels), is_sampled)) == 32) 
Example 21
def test_subsample_selection_dynamic(self):
    # Test random sampling when only some examples can be sampled:
    # 100 samples, 20 positives, 10 positives cannot be sampled
    numpy_labels = np.arange(100)
    numpy_indicator = numpy_labels < 90
    indicator = tf.constant(numpy_indicator)
    numpy_labels = (numpy_labels - 80) >= 0

    labels = tf.constant(numpy_labels)

    sampler = (
        balanced_positive_negative_sampler.BalancedPositiveNegativeSampler())
    is_sampled = sampler.subsample(indicator, 64, labels)
    with self.test_session() as sess:
      is_sampled = sess.run(is_sampled)
      self.assertTrue(sum(is_sampled) == 64)
      self.assertTrue(sum(np.logical_and(numpy_labels, is_sampled)) == 10)
      self.assertTrue(sum(np.logical_and(
          np.logical_not(numpy_labels), is_sampled)) == 54)
      self.assertAllEqual(is_sampled, np.logical_and(is_sampled,
                                                     numpy_indicator)) 
Example 22
def test_subsample_selection_static(self):
    # Test random sampling when only some examples can be sampled:
    # 100 samples, 20 positives, 10 positives cannot be sampled.
    numpy_labels = np.arange(100)
    numpy_indicator = numpy_labels < 90
    indicator = np.array(numpy_indicator, np.bool)
    numpy_labels = (numpy_labels - 80) >= 0

    labels = np.array(numpy_labels, np.bool)

    def graph_fn(indicator, labels):
      sampler = (
          balanced_positive_negative_sampler.BalancedPositiveNegativeSampler(
              is_static=True))
      return sampler.subsample(indicator, 64, labels)

    is_sampled = self.execute(graph_fn, [indicator, labels])
    self.assertTrue(sum(is_sampled) == 64)
    self.assertTrue(sum(np.logical_and(numpy_labels, is_sampled)) == 10)
    self.assertTrue(sum(np.logical_and(
        np.logical_not(numpy_labels), is_sampled)) == 54)
    self.assertAllEqual(is_sampled, np.logical_and(is_sampled, numpy_indicator)) 
Example 23
Project: mmdetection   Author: open-mmlab   File: iou_balanced_neg_sampler.py    License: Apache License 2.0 5 votes vote down vote up
def sample_via_interval(self, max_overlaps, full_set, num_expected):
        """Sample according to the iou interval.

        Args:
            max_overlaps (torch.Tensor): IoU between bounding boxes and ground
                truth boxes.
            full_set (set(int)): A full set of indices of boxes。
            num_expected (int): Number of expected samples。

        Returns:
            np.ndarray: Indices  of samples
        """
        max_iou = max_overlaps.max()
        iou_interval = (max_iou - self.floor_thr) / self.num_bins
        per_num_expected = int(num_expected / self.num_bins)

        sampled_inds = []
        for i in range(self.num_bins):
            start_iou = self.floor_thr + i * iou_interval
            end_iou = self.floor_thr + (i + 1) * iou_interval
            tmp_set = set(
                np.where(
                    np.logical_and(max_overlaps >= start_iou,
                                   max_overlaps < end_iou))[0])
            tmp_inds = list(tmp_set & full_set)
            if len(tmp_inds) > per_num_expected:
                tmp_sampled_set = self.random_choice(tmp_inds,
                                                     per_num_expected)
            else:
                tmp_sampled_set = np.array(tmp_inds, dtype=np.int)
            sampled_inds.append(tmp_sampled_set)

        sampled_inds = np.concatenate(sampled_inds)
        if len(sampled_inds) < num_expected:
            num_extra = num_expected - len(sampled_inds)
            extra_inds = np.array(list(full_set - set(sampled_inds)))
            if len(extra_inds) > num_extra:
                extra_inds = self.random_choice(extra_inds, num_extra)
            sampled_inds = np.concatenate([sampled_inds, extra_inds])

        return sampled_inds 
Example 24
Project: neuropythy   Author: noahbenson   File: cmag.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def sigma_bins(sigma, sigma_bin_walls):
        bins = []
        for (mn,mx) in zip(sigma_bin_walls[:-1], sigma_bin_walls[1:]):
            ii = np.logical_and(mn < sigma, sigma <= mx)
            bins.append(pimms.imm_array(np.where(ii)[0]))
        return tuple(bins) 
Example 25
Project: neuropythy   Author: noahbenson   File: core.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def dataframe_except(df, *cols, **filters):
    '''
    dataframe_except(df, k1=v1, k2=v2...) yields df after selecting all the columns in which the
      given keys (k1, k2, etc.) have been selected such that the associated columns in the dataframe
      contain only the rows whose cells match the given values.
    dataframe_except(df, col1, col2...) selects all columns except for the given columns.
    dataframe_except(df, col1, col2..., k1=v1, k2=v2...) selects on both conditions.
    
    The dataframe_except() function is identical to the dataframe_select() function with the single
    difference being that the column names provided to dataframe_except() are dropped from the
    result while column names passed to dataframe_select() are kept.

    If a value is a tuple/list of 2 elements, then it is considered a range where cells must fall
    between the values. If value is a tuple/list of more than 2 elements or is a set of any length
    then it is a list of values, any one of which can match the cell.
    '''
    ii = np.ones(len(df), dtype='bool')
    for (k,v) in six.iteritems(filters):
        vals = df[k].values
        if   pimms.is_set(v):                    jj = np.isin(vals, list(v))
        elif pimms.is_vector(v) and len(v) == 2: jj = (v[0] <= vals) & (vals < v[1])
        elif pimms.is_vector(v):                 jj = np.isin(vals, list(v))
        else:                                    jj = (vals == v)
        ii = np.logical_and(ii, jj)
    if len(ii) != np.sum(ii): df = df.loc[ii]
    if len(cols) > 0: df = df.drop(list(cols), axis=1, inplace=False)
    return df 
Example 26
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: detection.py    License: Apache License 2.0 5 votes vote down vote up
def _check_valid_label(self, label):
        """Validate label and its shape."""
        if len(label.shape) != 2 or label.shape[1] < 5:
            msg = "Label with shape (1+, 5+) required, %s received." % str(label)
            raise RuntimeError(msg)
        valid_label = np.where(np.logical_and(label[:, 0] >= 0, label[:, 3] > label[:, 1],
                                              label[:, 4] > label[:, 2]))[0]
        if valid_label.size < 1:
            raise RuntimeError('Invalid label occurs.') 
Example 27
Project: DOTA_models   Author: ringringyi   File: graph_utils.py    License: Apache License 2.0 5 votes vote down vote up
def convert_traversible_to_graph(traversible, ff_cost=1., fo_cost=1.,
                                 oo_cost=1., connectivity=4):
  assert(connectivity == 4 or connectivity == 8)

  sz_x = traversible.shape[1]
  sz_y = traversible.shape[0]
  g, nodes = generate_lattice(sz_x, sz_y)

  # Assign costs.
  edge_wts = g.new_edge_property('float')
  g.edge_properties['wts'] = edge_wts
  wts = np.ones(g.num_edges(), dtype=np.float32)
  edge_wts.get_array()[:] = wts

  if connectivity == 8:
    add_diagonal_edges(g, nodes, sz_x, sz_y, np.sqrt(2.))

  se = np.array([[int(e.source()), int(e.target())] for e in g.edges()])
  s_xy = nodes[se[:,0]]
  t_xy = nodes[se[:,1]]
  s_t = np.ravel_multi_index((s_xy[:,1], s_xy[:,0]), traversible.shape)
  t_t = np.ravel_multi_index((t_xy[:,1], t_xy[:,0]), traversible.shape)
  s_t = traversible.ravel()[s_t]
  t_t = traversible.ravel()[t_t]

  wts = np.zeros(g.num_edges(), dtype=np.float32)
  wts[np.logical_and(s_t == True, t_t == True)] = ff_cost
  wts[np.logical_and(s_t == False, t_t == False)] = oo_cost
  wts[np.logical_xor(s_t, t_t)] = fo_cost

  edge_wts = g.edge_properties['wts']
  for i, e in enumerate(g.edges()):
    edge_wts[e] = edge_wts[e] * wts[i]
  # d = edge_wts.get_array()*1.
  # edge_wts.get_array()[:] = d*wts 
  return g, nodes 
Example 28
Project: DOTA_models   Author: ringringyi   File: np_box_list_ops.py    License: Apache License 2.0 5 votes vote down vote up
def _update_valid_indices_by_removing_high_iou_boxes(
    selected_indices, is_index_valid, intersect_over_union, threshold):
  max_iou = np.max(intersect_over_union[:, selected_indices], axis=1)
  return np.logical_and(is_index_valid, max_iou <= threshold) 
Example 29
Project: DOTA_models   Author: ringringyi   File: minibatch_sampler_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_subsample_indicator_when_more_true_elements_than_num_samples(self):
    np_indicator = [True, False, True, False, True, True, False]
    indicator = tf.constant(np_indicator)
    samples = minibatch_sampler.MinibatchSampler.subsample_indicator(
        indicator, 3)
    with self.test_session() as sess:
      samples_out = sess.run(samples)
      self.assertTrue(np.sum(samples_out), 3)
      self.assertAllEqual(samples_out,
                          np.logical_and(samples_out, np_indicator)) 
Example 30
Project: DOTA_models   Author: ringringyi   File: minibatch_sampler_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_subsample_when_more_true_elements_than_num_samples_no_shape(self):
    np_indicator = [True, False, True, False, True, True, False]
    indicator = tf.placeholder(tf.bool)
    feed_dict = {indicator: np_indicator}

    samples = minibatch_sampler.MinibatchSampler.subsample_indicator(
        indicator, 3)
    with self.test_session() as sess:
      samples_out = sess.run(samples, feed_dict=feed_dict)
      self.assertTrue(np.sum(samples_out), 3)
      self.assertAllEqual(samples_out,
                          np.logical_and(samples_out, np_indicator))