Python numpy.full_like() Examples

The following are 30 code examples for showing how to use numpy.full_like(). 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: test_stride_tricks.py    License: Apache License 2.0 6 votes vote down vote up
def as_strided_writeable():
    arr = np.ones(10)
    view = as_strided(arr, writeable=False)
    assert_(not view.flags.writeable)

    # Check that writeable also is fine:
    view = as_strided(arr, writeable=True)
    assert_(view.flags.writeable)
    view[...] = 3
    assert_array_equal(arr, np.full_like(arr, 3))

    # Test that things do not break down for readonly:
    arr.flags.writeable = False
    view = as_strided(arr, writeable=False)
    view = as_strided(arr, writeable=True)
    assert_(not view.flags.writeable) 
Example 2
Project: glc   Author: mmazeika   File: train_confusion.py    License: Apache License 2.0 6 votes vote down vote up
def get_C_hat_transpose():
    probs = []
    net.eval()
    for batch_idx, (data, target) in enumerate(train_gold_deterministic_loader):
        # we subtract 10 because we added 10 to gold so we could identify which example is gold in train_phase2
        data, target = torch.autograd.Variable(data.cuda(), volatile=True),\
                       torch.autograd.Variable((target - num_classes).cuda(), volatile=True)

        # forward
        output = net(data)
        pred = F.softmax(output)
        probs.extend(list(pred.data.cpu().numpy()))

    probs = np.array(probs, dtype=np.float32)
    preds = np.argmax(probs, axis=1)
    C_hat = np.zeros([num_classes, num_classes])
    for i in range(len(train_data_gold.train_labels)):
        C_hat[int(np.rint(train_data_gold.train_labels[i] - num_classes)), preds[i]] += 1

    C_hat /= (np.sum(C_hat, axis=1, keepdims=True) + 1e-7)
    C_hat = C_hat * 0.99 + np.full_like(C_hat, 1/num_classes) * 0.01  # smoothing

    return C_hat.T.astype(np.float32) 
Example 3
Project: numcodecs   Author: zarr-developers   File: categorize.py    License: MIT License 6 votes vote down vote up
def decode(self, buf, out=None):

        # normalise input
        enc = ensure_ndarray(buf).view(self.astype)

        # flatten to simplify implementation
        enc = enc.reshape(-1, order='A')

        # setup output
        dec = np.full_like(enc, fill_value='', dtype=self.dtype)

        # apply decoding
        for i, l in enumerate(self.labels):
            dec[enc == (i + 1)] = l

        # handle output
        dec = ndarray_copy(dec, out)

        return dec 
Example 4
Project: lambda-packs   Author: ryfeus   File: test_stride_tricks.py    License: MIT License 6 votes vote down vote up
def as_strided_writeable():
    arr = np.ones(10)
    view = as_strided(arr, writeable=False)
    assert_(not view.flags.writeable)

    # Check that writeable also is fine:
    view = as_strided(arr, writeable=True)
    assert_(view.flags.writeable)
    view[...] = 3
    assert_array_equal(arr, np.full_like(arr, 3))

    # Test that things do not break down for readonly:
    arr.flags.writeable = False
    view = as_strided(arr, writeable=False)
    view = as_strided(arr, writeable=True)
    assert_(not view.flags.writeable) 
Example 5
Project: vnpy_crypto   Author: birforce   File: test_stride_tricks.py    License: MIT License 6 votes vote down vote up
def as_strided_writeable():
    arr = np.ones(10)
    view = as_strided(arr, writeable=False)
    assert_(not view.flags.writeable)

    # Check that writeable also is fine:
    view = as_strided(arr, writeable=True)
    assert_(view.flags.writeable)
    view[...] = 3
    assert_array_equal(arr, np.full_like(arr, 3))

    # Test that things do not break down for readonly:
    arr.flags.writeable = False
    view = as_strided(arr, writeable=False)
    view = as_strided(arr, writeable=True)
    assert_(not view.flags.writeable) 
Example 6
Project: ip-nonlinear-solver   Author: antonior92   File: _constraints.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _is_feasible(kind, enforce_feasibility, f0):
    keyword = kind[0]
    if keyword == "equals":
        lb = np.asarray(kind[1], dtype=float)
        ub = np.asarray(kind[1], dtype=float)
    elif keyword == "greater":
        lb = np.asarray(kind[1], dtype=float)
        ub = np.full_like(lb, np.inf, dtype=float)
    elif keyword == "less":
        ub = np.asarray(kind[1], dtype=float)
        lb = np.full_like(ub, -np.inf, dtype=float)
    elif keyword == "interval":
        lb = np.asarray(kind[1], dtype=float)
        ub = np.asarray(kind[2], dtype=float)
    else:
        raise RuntimeError("Never be here.")

    return ((lb[enforce_feasibility] <= f0[enforce_feasibility]).all()
            and (f0[enforce_feasibility] <= ub[enforce_feasibility]).all()) 
Example 7
Project: gridfinder   Author: carderne   File: gridfinder.py    License: MIT License 6 votes vote down vote up
def estimate_mem_use(targets, costs):
    """Estimate memory usage in GB, probably not very accurate.

    Parameters
    ----------
    targets : numpy array
        2D array of targets.
    costs : numpy array
        2D array of costs.

    Returns
    -------
    est_mem : float
        Estimated memory requirement in GB.
    """

    # make sure these match the ones used in optimise below
    visited = np.zeros_like(targets, dtype=np.int8)
    dist = np.full_like(costs, np.nan, dtype=np.float32)
    prev = np.full_like(costs, np.nan, dtype=object)

    est_mem_arr = [targets, costs, visited, dist, prev]
    est_mem = len(pickle.dumps(est_mem_arr, -1))

    return est_mem / 1e9 
Example 8
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: test_stride_tricks.py    License: MIT License 6 votes vote down vote up
def as_strided_writeable():
    arr = np.ones(10)
    view = as_strided(arr, writeable=False)
    assert_(not view.flags.writeable)

    # Check that writeable also is fine:
    view = as_strided(arr, writeable=True)
    assert_(view.flags.writeable)
    view[...] = 3
    assert_array_equal(arr, np.full_like(arr, 3))

    # Test that things do not break down for readonly:
    arr.flags.writeable = False
    view = as_strided(arr, writeable=False)
    view = as_strided(arr, writeable=True)
    assert_(not view.flags.writeable) 
Example 9
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: test_data.py    License: MIT License 6 votes vote down vote up
def test_power_transformer_nans(method):
    # Make sure lambda estimation is not influenced by NaN values
    # and that transform() supports NaN silently

    X = np.abs(X_1col)
    pt = PowerTransformer(method=method)
    pt.fit(X)
    lmbda_no_nans = pt.lambdas_[0]

    # concat nans at the end and check lambda stays the same
    X = np.concatenate([X, np.full_like(X, np.nan)])
    X = shuffle(X, random_state=0)

    pt.fit(X)
    lmbda_nans = pt.lambdas_[0]

    assert_almost_equal(lmbda_no_nans, lmbda_nans, decimal=5)

    X_trans = pt.transform(X)
    assert_array_equal(np.isnan(X_trans), np.isnan(X)) 
Example 10
Project: fastats   Author: fastats   File: windowed_pass.py    License: MIT License 6 votes vote down vote up
def windowed_pass_2d(x, win):
    """
    The same as windowed pass, but explicitly
    iterates over the `value()` return array
    and allocates it in the `result`.

    This allows 2-dimensional arrays to be returned
    from `value()` functions, before we support
    the behaviour properly using AST transforms.

    This allows for extremely fast iteration
    for items such as OLS, and at the same time
    calculating t-stats / r^2.
    """
    result = np.full_like(x, np.nan)
    for i in range(win, x.shape[0]+1):
        res = value(x[i-win:i])
        for j, j_val in enumerate(res):
            result[i-1, j] = j_val
    return result 
Example 11
Project: trax   Author: google   File: array_ops.py    License: Apache License 2.0 6 votes vote down vote up
def full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None):  # pylint: disable=missing-docstring,redefined-outer-name
  """order, subok and shape arguments mustn't be changed."""
  if order != 'K':
    raise ValueError('Non-standard orders are not supported.')
  if not subok:
    raise ValueError('subok being False is not supported.')
  if shape:
    raise ValueError('Overriding the shape is not supported.')

  a = asarray(a).data
  dtype = dtype or utils.result_type(a)
  fill_value = asarray(fill_value, dtype=dtype)
  return arrays_lib.tensor_to_ndarray(
      tf.broadcast_to(fill_value.data, tf.shape(a)))


# TODO(wangpeng): investigate whether we can make `copy` default to False.
# TODO(wangpeng): utils.np_doc can't handle np.array because np.array is a
#   builtin function. Make utils.np_doc support builtin functions. 
Example 12
Project: trax   Author: google   File: array_ops_test.py    License: Apache License 2.0 6 votes vote down vote up
def testFullLike(self):
    # List of 2-tuples of fill value and shape.
    data = [
        (5, ()),
        (5, (7,)),
        (5., (7,)),
        ([5, 8], (2,)),
        ([5, 8], (3, 2)),
        ([[5], [8]], (2, 3)),
        ([[5], [8]], (3, 2, 5)),
        ([[5.], [8.]], (3, 2, 5)),
    ]
    zeros_builders = [array_ops.zeros, np.zeros]
    for f, s in data:
      for fn1, fn2, arr_dtype in itertools.product(
          self.array_transforms, zeros_builders, self.all_types):
        fill_value = fn1(f)
        arr = fn2(s, arr_dtype)
        self.match(
            array_ops.full_like(arr, fill_value), np.full_like(arr, fill_value))
        for dtype in self.all_types:
          self.match(
              array_ops.full_like(arr, fill_value, dtype=dtype),
              np.full_like(arr, fill_value, dtype=dtype)) 
Example 13
Project: kite   Author: pyrocko   File: sandbox_scene.py    License: GNU General Public License v3.0 6 votes vote down vote up
def setLOS(self, phi, theta):
        """Set the sandbox's LOS vector

        :param phi: phi in degree
        :type phi: int
        :param theta: theta in degree
        :type theta: int
        """
        if self.reference is not None:
            self._log.warning('Cannot change a referenced model!')
            return

        self._log.debug(
            'Changing model LOS to %d phi and %d theta', phi, theta)

        self.theta = num.full_like(self.theta, theta*r2d)
        self.phi = num.full_like(self.phi, phi*r2d)
        self.frame.updateExtent()

        self._clearModel()
        self.evChanged.notify() 
Example 14
Project: pvlib-python   Author: pvlib   File: test_tracking.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_arrays_multi():
    apparent_zenith = np.array([[10, 10], [10, 10]])
    apparent_azimuth = np.array([[180, 180], [180, 180]])
    # singleaxis should fail for num dim > 1
    with pytest.raises(ValueError):
        tracking.singleaxis(apparent_zenith, apparent_azimuth,
                            axis_tilt=0, axis_azimuth=0,
                            max_angle=90, backtrack=True,
                            gcr=2.0/7.0)
    # uncomment if we ever get singleaxis to support num dim > 1 arrays
    # assert isinstance(tracker_data, dict)
    # expect = {'tracker_theta': np.full_like(apparent_zenith, 0),
    #           'aoi': np.full_like(apparent_zenith, 10),
    #           'surface_azimuth': np.full_like(apparent_zenith, 90),
    #           'surface_tilt': np.full_like(apparent_zenith, 0)}
    # for k, v in expect.items():
    #     assert_allclose(tracker_data[k], v) 
Example 15
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_stride_tricks.py    License: MIT License 6 votes vote down vote up
def as_strided_writeable():
    arr = np.ones(10)
    view = as_strided(arr, writeable=False)
    assert_(not view.flags.writeable)

    # Check that writeable also is fine:
    view = as_strided(arr, writeable=True)
    assert_(view.flags.writeable)
    view[...] = 3
    assert_array_equal(arr, np.full_like(arr, 3))

    # Test that things do not break down for readonly:
    arr.flags.writeable = False
    view = as_strided(arr, writeable=False)
    view = as_strided(arr, writeable=True)
    assert_(not view.flags.writeable) 
Example 16
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_distance.py    License: MIT License 6 votes vote down vote up
def test_old_wminkowski(self):
        with suppress_warnings() as wrn:
            wrn.filter(message="`wminkowski` is deprecated")
            w = np.array([1.0, 2.0, 0.5])
            for x, y in self.cases:
                dist1 = old_wminkowski(x, y, p=1, w=w)
                assert_almost_equal(dist1, 3.0)
                dist1p5 = old_wminkowski(x, y, p=1.5, w=w)
                assert_almost_equal(dist1p5, (2.0**1.5+1.0)**(2./3))
                dist2 = old_wminkowski(x, y, p=2, w=w)
                assert_almost_equal(dist2, np.sqrt(5))

            # test weights Issue #7893
            arr = np.arange(4)
            w = np.full_like(arr, 4)
            assert_almost_equal(old_wminkowski(arr, arr + 1, p=2, w=w), 8.0)
            assert_almost_equal(wminkowski(arr, arr + 1, p=2, w=w), 4.0) 
Example 17
Project: pymoo   Author: msu-coinlab   File: usage_nsga2_custom.py    License: Apache License 2.0 5 votes vote down vote up
def _do(self, problem, X, **kwargs):

        # The input of has the following shape (n_parents, n_matings, n_var)
        _, n_matings, n_var = X.shape

        # The output owith the shape (n_offsprings, n_matings, n_var)
        # Because there the number of parents and offsprings are equal it keeps the shape of X
        Y = np.full_like(X, None, dtype=np.object)

        # for each mating provided
        for k in range(n_matings):

            # get the first and the second parent
            a, b = X[0, k, 0], X[1, k, 0]

            # prepare the offsprings
            off_a = ["_"] * problem.n_characters
            off_b = ["_"] * problem.n_characters

            for i in range(problem.n_characters):
                if np.random.random() < 0.5:
                    off_a[i] = a[i]
                    off_b[i] = b[i]
                else:
                    off_a[i] = b[i]
                    off_b[i] = a[i]

            # join the character list and set the output
            Y[0, k, 0], Y[1, k, 0] = "".join(off_a), "".join(off_b)

        return Y 
Example 18
Project: chainerrl   Author: chainer   File: test_distribution.py    License: MIT License 5 votes vote down vote up
def _assert_array_in_range(array, low, high):
    assert isinstance(array, np.ndarray)
    assert low < high
    np.testing.assert_array_less(np.full_like(array, low), array)
    np.testing.assert_array_less(array, np.full_like(array, high)) 
Example 19
Project: PyRadarMet   Author: nguy   File: variables.py    License: GNU General Public License v2.0 5 votes vote down vote up
def zdp(z_h, z_v):
    """
    Reflectivity difference [dB].

    From Rinehart (1997), Eqn 10.3

    Parameters
    ----------
    z_h : float
        Horizontal reflectivity [mm^6/m^3]
    z_v : float
        Horizontal reflectivity [mm^6/m^3]

    Notes
    -----
    Ensure that both powers have the same units!

    Alternating horizontally and linearly polarized pulses are averaged.
    """
    zh = np.atleast_1d(z_h)
    zv = np.atleast_1d(z_v)
    if len(zh) != len(zv):
        raise ValueError('Input variables must be same length')
        return

    zdp = np.full_like(zh, np.nan)
    good = np.where(zh > zv)
    zdp[good] = 10.* np.log10(zh[good] - zv[good])
    return zdp 
Example 20
Project: PyRadarMet   Author: nguy   File: variables.py    License: GNU General Public License v2.0 5 votes vote down vote up
def hdr(dbz_h, zdr):
    """
    Differential reflectivity [dB] hail signature.

    From Aydin et al. (1986), Eqns 4-5

    Parameters
    ----------
    dbz_h : float or array
        Horizontal reflectivity [dBZ]
    zdr : float or array
        Differential reflectivity [dBZ]

    Notes
    -----
    Ensure that both powers have the same units!

    Positive HDR and strong gradients at edges signify ice. The larger HDR,
       the greater likelihood that ice is present.

    Considerations for this equation (see paper for more details):
       1) Standar error of disdrometer data allowed for
       2) Drop oscillation accounted for based on 50% model of Seliga et al (1984)
       3) Lower (27) bound chose to provide constant Zh ref level
       4) Upper cutoff of 60 (may be too low)

    Picca and Ryzhkof (2012) mention that this does not take into account
    the hail melting process.  So use at your own risk!
    """
    zdr = np.atleast_1d(zdr)
    # Set the f(zdr) based upon observations
    f = np.full_like(zdr, np.nan)
    negind = np.where(zdr <= 0)
    lowind = np.where((zdr > 0) & (zdr <= 1.74))
    highind = np.where(zdr > 1.74)
    f[negind] = 27.
    f[lowind] = 19. * zdr[lowind] + 27.
    f[highind] = 60.
    # Calculate HDR
    return np.asarray(dbz_h) - f 
Example 21
Project: baseband   Author: mhvk   File: frame.py    License: GNU General Public License v3.0 5 votes vote down vote up
def tofile(self, fh):
        """Write encoded frame to filehandle."""
        self.header.tofile(fh)
        if self.valid:
            self.payload.tofile(fh)
        else:
            fh.write(np.full_like(self.payload.words,
                                  self._fill_pattern).tobytes()) 
Example 22
Project: deepchem   Author: deepchem   File: genomic_metrics.py    License: MIT License 5 votes vote down vote up
def get_pssm_scores(encoded_sequences, pssm):
  """
  Convolves pssm and its reverse complement with encoded sequences
  and returns the maximum score at each position of each sequence.

  Parameters
  ----------
  encoded_sequences: 3darray
       (N_sequences, N_letters, sequence_length, 1) array
  pssm: 2darray
      (4, pssm_length) array

  Returns
  -------
  scores: 2darray
      (N_sequences, sequence_length)
  """
  encoded_sequences = encoded_sequences.squeeze(axis=3)
  # initialize fwd and reverse scores to -infinity
  fwd_scores = np.full_like(encoded_sequences, -np.inf, float)
  rc_scores = np.full_like(encoded_sequences, -np.inf, float)
  # cross-correlate separately for each base,
  # for both the PSSM and its reverse complement
  for base_indx in range(encoded_sequences.shape[1]):
    base_pssm = pssm[base_indx][None]
    base_pssm_rc = base_pssm[:, ::-1]
    fwd_scores[:, base_indx, :] = correlate2d(
        encoded_sequences[:, base_indx, :], base_pssm, mode='same')
    rc_scores[:, base_indx, :] = correlate2d(
        encoded_sequences[:, -(base_indx + 1), :], base_pssm_rc, mode='same')
  # sum over the bases
  fwd_scores = fwd_scores.sum(axis=1)
  rc_scores = rc_scores.sum(axis=1)
  # take max of fwd and reverse scores at each position
  scores = np.maximum(fwd_scores, rc_scores)
  return scores 
Example 23
Project: deepchem   Author: deepchem   File: utils.py    License: MIT License 5 votes vote down vote up
def get_pssm_scores(encoded_sequences, pssm):
  """
  Convolves pssm and its reverse complement with encoded sequences
  and returns the maximum score at each position of each sequence.

  Parameters
  ----------
  encoded_sequences: 3darray
        (num_examples, 1, 4, seq_length) array
  pssm: 2darray
      (4, pssm_length) array

  Returns
  -------
  scores: 2darray
      (num_examples, seq_length) array
  """
  encoded_sequences = encoded_sequences.squeeze(axis=1)
  # initialize fwd and reverse scores to -infinity
  fwd_scores = np.full_like(encoded_sequences, -np.inf, float)
  rc_scores = np.full_like(encoded_sequences, -np.inf, float)
  # cross-correlate separately for each base,
  # for both the PSSM and its reverse complement
  for base_indx in range(encoded_sequences.shape[1]):
    base_pssm = pssm[base_indx][None]
    base_pssm_rc = base_pssm[:, ::-1]
    fwd_scores[:, base_indx, :] = correlate2d(
        encoded_sequences[:, base_indx, :], base_pssm, mode='same')
    rc_scores[:, base_indx, :] = correlate2d(
        encoded_sequences[:, -(base_indx + 1), :], base_pssm_rc, mode='same')
  # sum over the bases
  fwd_scores = fwd_scores.sum(axis=1)
  rc_scores = rc_scores.sum(axis=1)
  # take max of fwd and reverse scores at each position
  scores = np.maximum(fwd_scores, rc_scores)
  return scores 
Example 24
Project: pygbm   Author: ogrisel   File: test_loss.py    License: MIT License 5 votes vote down vote up
def get_derivatives_helper(loss):
    """Return get_gradients() and get_hessians() functions for a given loss.

    Loss classes used to have get_gradients() and
    get_hessians() methods, but now the update is done inplace in
    update_gradient_and_hessians(). This helper is used to keep the tests
    almost unchanged.
    """

    def get_gradients(y_true, raw_predictions):
        # create gradients and hessians array, update inplace, and return
        shape = raw_predictions.shape[0] * raw_predictions.shape[1]
        gradients = np.empty(shape=shape, dtype=raw_predictions.dtype)
        hessians = np.empty(shape=shape, dtype=raw_predictions.dtype)
        loss.update_gradients_and_hessians(gradients, hessians, y_true,
                                           raw_predictions)

        if loss.__class__ is _LOSSES['least_squares']:
            gradients *= 2  # ommitted a factor of 2 to be consistent with LGBM

        return gradients

    def get_hessians(y_true, raw_predictions):
        # create gradients and hessians array, update inplace, and return
        shape = raw_predictions.shape[0] * raw_predictions.shape[1]
        gradients = np.empty(shape=shape, dtype=raw_predictions.dtype)
        hessians = np.empty(shape=shape, dtype=raw_predictions.dtype)
        loss.update_gradients_and_hessians(gradients, hessians, y_true,
                                           raw_predictions)

        if loss.__class__ is _LOSSES['least_squares']:
            # hessians aren't updated because they're constant
            hessians = np.full_like(y_true, fill_value=2)

        return hessians

    return get_gradients, get_hessians 
Example 25
Project: pygbm   Author: ogrisel   File: test_splitting.py    License: MIT License 5 votes vote down vote up
def test_histogram_split(n_bins):
    rng = np.random.RandomState(42)
    feature_idx = 0
    l2_regularization = 0
    min_hessian_to_split = 1e-3
    min_samples_leaf = 1
    min_gain_to_split = 0.
    X_binned = np.asfortranarray(
        rng.randint(0, n_bins, size=(int(1e4), 2)), dtype=np.uint8)
    binned_feature = X_binned.T[feature_idx]
    sample_indices = np.arange(binned_feature.shape[0], dtype=np.uint32)
    ordered_hessians = np.ones_like(binned_feature, dtype=np.float32)
    all_hessians = ordered_hessians

    for true_bin in range(1, n_bins - 1):
        for sign in [-1, 1]:
            ordered_gradients = np.full_like(binned_feature, sign,
                                             dtype=np.float32)
            ordered_gradients[binned_feature <= true_bin] *= -1
            all_gradients = ordered_gradients

            n_bins_per_feature = np.array([n_bins] * X_binned.shape[1],
                                          dtype=np.uint32)
            context = SplittingContext(X_binned,
                                       n_bins,
                                       n_bins_per_feature,
                                       all_gradients, all_hessians,
                                       l2_regularization,
                                       min_hessian_to_split,
                                       min_samples_leaf, min_gain_to_split)

            split_info, _ = _find_histogram_split(context, feature_idx,
                                                  sample_indices)

            assert split_info.bin_idx == true_bin
            assert split_info.gain >= 0
            assert split_info.feature_idx == feature_idx
            assert (split_info.n_samples_left + split_info.n_samples_right
                    == sample_indices.shape[0])
            # Constant hessian: 1. per sample.
            assert split_info.n_samples_left == split_info.hessian_left 
Example 26
Project: argus-tgs-salt   Author: lRomul   File: transforms.py    License: MIT License 5 votes vote down vote up
def __call__(self, image, depth):
        cumsum_img = image_cumsum(image)
        depth_img = np.full_like(image, depth // 4)
        input = np.stack([image, cumsum_img, depth_img], axis=2)
        return input 
Example 27
Project: pycolab   Author: deepmind   File: shockwave.py    License: Apache License 2.0 5 votes vote down vote up
def update(self, actions, board, layers, backdrop, things, the_plot):

    if not self.curtain.any():
      impact_point = np.unravel_index(
          np.random.randint(0, self.curtain.size),
          self.curtain.shape)

      impact_map = np.full_like(self.curtain, True)
      impact_map[impact_point] = False

      self._distance_from_impact = ndimage.distance_transform_edt(impact_map)
      self._steps_since_impact = 0

      the_plot.log('BOOM! Shockwave initiated at {} at frame {}.'.format(
          impact_point, the_plot.frame))

    self.curtain[:] = (
        (self._distance_from_impact > self._steps_since_impact) &
        (self._distance_from_impact <= self._steps_since_impact + self._width) &
        (np.logical_not(layers['=']))
    )

    # Check if the player is safe, dead, or has won.
    player_position = things['P'].position

    if layers['^'][player_position]:
      the_plot.add_reward(1)
      the_plot.terminate_episode()

    under_fire = self.curtain[player_position]
    in_danger_zone = things[' '].curtain[player_position]

    if under_fire and in_danger_zone:
      the_plot.add_reward(-1)
      the_plot.terminate_episode()

    self._steps_since_impact += 1 
Example 28
Project: iccv2019-inc   Author: kibok90   File: inc_ext.py    License: MIT License 5 votes vote down vote up
def load_dataset(self, prev, t, train=True):
        if train:
            self.data, self.targets = self.archive[t]
        else:
            self.data    = np.concatenate([self.archive[s][0] for s in range(t+1)], axis=0)
            self.targets = np.concatenate([self.archive[s][1] for s in range(t+1)], axis=0)
        self.srcs = np.full_like(self.targets, self.CUR, dtype=np.uint8)
        self.srcs[np.isin(self.targets, prev)] |= self.PRE
        self.t = t 
Example 29
Project: iccv2019-inc   Author: kibok90   File: inc_ext.py    License: MIT License 5 votes vote down vote up
def append_coreset(self, only=False, interp=False):
        if self.train and (len(self.coreset[0]) > 0):
            if only:
                self.data, self.targets = self.coreset
                self.srcs = np.full_like(self.targets, self.PRE, dtype=np.uint8)
                self.srcs[np.isin(self.targets, self.tasks[self.t])] |= self.CUR
            else:
                srcs = np.full_like(self.coreset[1], self.PRE, dtype=np.uint8)
                srcs[np.isin(self.coreset[1], self.tasks[self.t])] |= self.CUR
                self.data = np.concatenate([self.data, self.coreset[0]], axis=0)
                self.targets = np.concatenate([self.targets, self.coreset[1]], axis=0)
                self.srcs = np.concatenate([self.srcs, srcs], axis=0) 
Example 30
Project: iccv2019-inc   Author: kibok90   File: inc_ext.py    License: MIT License 5 votes vote down vote up
def append_ex(self, ex_dataset, keep_label=False):
        if len(ex_dataset) > 0:
            self.data = np.concatenate([self.data, ex_dataset.data], axis=0)
            if keep_label: self.targets = np.concatenate([self.targets, ex_dataset.targets], axis=0)
            else:          self.targets = np.concatenate([self.targets, np.full_like(ex_dataset.targets, -1)], axis=0)
            self.srcs = np.concatenate([self.srcs, np.full_like(ex_dataset.targets, self.EXT, dtype=np.uint8)], axis=0)
        if hasattr(ex_dataset, 'ood') and (ex_dataset.ood is not None):
            self.data = np.concatenate([self.data, ex_dataset.ood], axis=0)
            self.targets = np.concatenate([self.targets, np.full(len(ex_dataset.ood), -2)], axis=0)
            self.srcs = np.concatenate([self.srcs, np.full(len(ex_dataset.ood), self.OOD, dtype=np.uint8)], axis=0)