Python numpy.logical_and() Examples

The following are 30 code examples of numpy.logical_and(). 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 numpy , or try the search function .
Example #1
Source File: fractal_mandelbrot.py    From NeuroKit with 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 #2
Source File: core.py    From neuropythy with 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 #3
Source File: balanced_positive_negative_sampler_test.py    From vehicle_counting_tensorflow with MIT License 6 votes vote down vote up
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 #4
Source File: balanced_positive_negative_sampler_test.py    From vehicle_counting_tensorflow with MIT License 6 votes vote down vote up
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 #5
Source File: detection.py    From dynamic-training-with-apache-mxnet-on-aws with 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 #6
Source File: graph_utils.py    From DOTA_models with 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 #7
Source File: detection.py    From dynamic-training-with-apache-mxnet-on-aws with 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 #8
Source File: geometry.py    From connecting_the_dots with 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 #9
Source File: balanced_positive_negative_sampler_test.py    From DOTA_models with 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 #10
Source File: SurveySimulation.py    From EXOSIMS with 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 #11
Source File: zdrcal.py    From PyRadarMet with 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 #12
Source File: display_wrapper.py    From radiometric_normalization with 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 #13
Source File: balanced_positive_negative_sampler_test.py    From vehicle_counting_tensorflow with MIT License 6 votes vote down vote up
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 #14
Source File: fractal_mandelbrot.py    From NeuroKit with 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 #15
Source File: signal_fixpeaks.py    From NeuroKit with 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 #16
Source File: signal_fixpeaks.py    From NeuroKit with 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 #17
Source File: tests_complexity.py    From NeuroKit with 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 #18
Source File: tests_complexity.py    From NeuroKit with 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 #19
Source File: subset.py    From pymoo with 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 #20
Source File: usage_ga_custom.py    From pymoo with 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 #21
Source File: display_wrapper.py    From radiometric_normalization with 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 #22
Source File: balanced_positive_negative_sampler_test.py    From object_detector_app with 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 #23
Source File: iou_balanced_neg_sampler.py    From AerialDetection with Apache License 2.0 5 votes vote down vote up
def sample_via_interval(self, max_overlaps, full_set, num_expected):
        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
Source File: ecg_rsa.py    From NeuroKit with MIT License 5 votes vote down vote up
def _ecg_rsa_p2t(rsp_onsets, rpeaks, sampling_rate, continuous=False, ecg_period=None, rsp_peaks=None):
    """Peak-to-trough algorithm (P2T)"""

    # Find all RSP cycles and the Rpeaks within
    cycles_rri = []
    for idx in range(len(rsp_onsets) - 1):
        cycle_init = rsp_onsets[idx]
        cycle_end = rsp_onsets[idx + 1]
        cycles_rri.append(rpeaks[np.logical_and(rpeaks >= cycle_init, rpeaks < cycle_end)])

    # Iterate over all cycles
    rsa_values = np.full(len(cycles_rri), np.nan)
    for i, cycle in enumerate(cycles_rri):
        # Estimate of RSA during each breath
        RRis = np.diff(cycle) / sampling_rate
        if len(RRis) > 1:
            rsa_values[i] = np.max(RRis) - np.min(RRis)

    if continuous is False:
        rsa = {"RSA_P2T_Mean": np.nanmean(rsa_values)}
        rsa["RSA_P2T_Mean_log"] = np.log(rsa["RSA_P2T_Mean"])  # pylint: disable=E1111
        rsa["RSA_P2T_SD"] = np.nanstd(rsa_values, ddof=1)
        rsa["RSA_P2T_NoRSA"] = len(pd.Series(rsa_values).index[pd.Series(rsa_values).isnull()])
    else:
        rsa = signal_interpolate(
            x_values=rsp_peaks[~np.isnan(rsa_values)],
            y_values=rsa_values[~np.isnan(rsa_values)],
            x_new=np.arange(len(ecg_period)),
        )

    return rsa 
Example #25
Source File: minibatch_sampler_test.py    From vehicle_counting_tensorflow with MIT License 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)) 
Example #26
Source File: fisheye.py    From DualFisheye with MIT License 5 votes vote down vote up
def _score(self, svec, xyz, wt_pixel, wt_blank):
        # Update lens parameters from state vector.
        self._set_state_vector(svec)
        # Determine masks for each input image.
        uv0 = self.sources[0].get_uv(xyz)
        uv1 = self.sources[1].get_uv(xyz)
        wt0 = self.sources[0].get_weight(uv0) > 0
        wt1 = self.sources[1].get_weight(uv1) > 0
        # Count overlapping pixels.
        ovr_mask = np.logical_and(wt0, wt1)             # Overlapping pixel
        pix_count = np.sum(wt0) + np.sum(wt1)           # Total drawn pixels
        blk_count = np.sum(np.logical_and(~wt0, ~wt1))  # Number of blank pixels
        # Allocate Nx3 or Nx1 "1D" pixel-list (raster-order).
        pcount = max(xyz.shape)
        img1d = np.zeros((pcount, self.clrs), dtype='float32')
        # Render the difference image, overlapping region only.
        self.sources[0].add_pixels(uv0, img1d, 1.0*ovr_mask)
        self.sources[1].add_pixels(uv1, img1d, -1.0*ovr_mask)
        # Sum-of-differences.
        sum_sqd = np.sum(np.sum(np.sum(np.square(img1d))))
        # Compute overall score.  (Note: Higher = Better)
        score = sum_sqd + wt_blank * blk_count - wt_pixel * pix_count
        # (Debug) Print status information.
        if (self.debug):
            print str(svec) + ' --> ' + str(score)
        return score


# Tkinter GUI window for loading a fisheye image. 
Example #27
Source File: minibatch_sampler_test.py    From vehicle_counting_tensorflow with MIT License 5 votes vote down vote up
def test_subsample_indicator_when_less_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, 5)
    with self.test_session() as sess:
      samples_out = sess.run(samples)
      self.assertTrue(np.sum(samples_out), 4)
      self.assertAllEqual(samples_out,
                          np.logical_and(samples_out, np_indicator)) 
Example #28
Source File: test_common.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def phase_difference(a, b, axis=0, threshold=1e-5):
    """The phase difference between vectors."""
    v1, v2 = numpy.asarray(a), numpy.asarray(b)
    testing.assert_equal(v1.shape, v2.shape)
    v1, v2 = pull_dim(v1, axis), pull_dim(v2, axis)
    g1 = abs(v1) > threshold
    g2 = abs(v2) > threshold
    g12 = numpy.logical_and(g1, g2)
    if numpy.any(g12.sum(axis=1) == 0):
        desired_threshold = numpy.minimum(abs(v1), abs(v2)).max(axis=1).min()
        raise ValueError("Cannot find an anchor for the rotation, maximal value for the threshold is: {:.3e}".format(
            desired_threshold
        ))
    anchor_index = tuple(numpy.where(i)[0][0] for i in g12)
    return sign(v2[numpy.arange(len(v2)), anchor_index]) / sign(v1[numpy.arange(len(v1)), anchor_index]) 
Example #29
Source File: test_common.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def adjust_mf_phase(model1, model2, threshold=1e-5):
    """Tunes the phase of the 2 mean-field models to a common value."""
    signatures = []
    orders = []

    for m in (model1, model2):
        if "kpts" in dir(m):
            signatures.append(numpy.concatenate(m.mo_coeff, axis=1))
            orders.append(numpy.argsort(numpy.concatenate(m.mo_energy)))
        else:
            signatures.append(m.mo_coeff)
            orders.append(numpy.argsort(m.mo_energy))

    m1, m2 = signatures
    o1, o2 = orders
    mdim = min(m1.shape[0], m2.shape[0])
    m1, m2 = m1[:mdim, :][:, o1], m2[:mdim, :][:, o2]

    p = phase_difference(m1, m2, axis=1, threshold=threshold)

    if "kpts" in dir(model2):
        fr = 0
        for k, i in enumerate(model2.mo_coeff):
            to = fr + i.shape[1]
            slc = numpy.logical_and(fr <= o2, o2 < to)
            i[:, o2[slc] - fr] /= p[slc][numpy.newaxis, :]
            fr = to
    else:
        model2.mo_coeff[:, o2] /= p[numpy.newaxis, :] 
Example #30
Source File: fisheye.py    From DualFisheye with MIT License 5 votes vote down vote up
def get_mask(self, uv_px):
        # Check whether each coordinate is within outer image bounds,
        # and within the illuminated area under the fisheye lens.
        x_mask = np.logical_and(0 <= uv_px[0], uv_px[0] < self.cols)
        y_mask = np.logical_and(0 <= uv_px[1], uv_px[1] < self.rows)
        # Check whether each coordinate is within the illuminated area.
        r_mask = matrix_len(uv_px - self.lens.center_px) < self.lens.radius_px
        # All three checks must pass to be considered visible.
        all_mask = np.logical_and(r_mask, np.logical_and(x_mask, y_mask))
        return np.squeeze(np.asarray(all_mask))

    # Given an 2xN array of UV pixel coordinates, return a weight score
    # that is proportional to the distance from the edge.