Python xarray.full_like() Examples

The following are 12 code examples of xarray.full_like(). 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 xarray , or try the search function .
Example #1
Source File: metsim.py    From MetSim with GNU General Public License v3.0 6 votes vote down vote up
def met_data(self):
        if self._met_data is None:
            self._met_data = io.read_met_data(self.params, self.domain)
            self._met_data['elev'] = self.domain['elev']
            self._met_data['lat'] = self.domain['lat']
            self._met_data['lon'] = self.domain['lon']

            # process constant_vars
            constant_vars = self.params.get('constant_vars', None)
            if constant_vars:
                da_template = self._met_data[list(self._met_data)[0]]
                for var in constant_vars.keys():
                    self._met_data[var] = xr.full_like(da_template,
                                                       float(constant_vars[var]))

            self._validate_force_times(force_times=self._met_data['time'])
        return self._met_data 
Example #2
Source File: core.py    From scikit-downscale with Apache License 2.0 5 votes vote down vote up
def _transform_wrapper(X, models, feature_dim=DEFAULT_FEATURE_DIM, **kwargs):

    xtrans = xr.full_like(X, np.nan)

    for index, model in xenumerate(models):
        xdf = X[index].pipe(_da_to_df, feature_dim).drop(models.coords.keys())
        xtrans_df = model.item().transform(xdf, **kwargs)
        xtrans[index] = xtrans_df.squeeze()
    return xtrans 
Example #3
Source File: core.py    From scikit-downscale with Apache License 2.0 5 votes vote down vote up
def fit(self, X, *args, **kwargs):
        """Fit the model

        Fit all the transforms one after the other and transform the
        data, then fit the transformed data using the final estimator.

        Parameters
        ----------
        X : xarray.DataArray or xarray.Dataset
            Training data. Must fulfill input requirements of first step of
            the pipeline. If an xarray.Dataset is passed, it will be converted
            to an array using `to_array()`.
        y : xarray.DataArray, optional
            Training targets. Must fulfill label requirements for all steps
            of the pipeline.
        feature_dim : str, optional
            Name of feature dimension.
        **fit_params : dict of string -> object
            Parameters passed to the ``fit`` method of the this model. If the
            model is a sklearn Pipeline, parameters can be passed to each
            step, where each parameter name is prefixed such that parameter
            ``p`` for step ``s`` has key ``s__p``.
        """
        kws = {'along_dim': self._dim, 'feature_dim': DEFAULT_FEATURE_DIM}
        kws.update(kwargs)

        assert len(args) <= 1
        args = list(args)
        args.append(self._model)

        X = self._to_feature_x(X, feature_dim=kws['feature_dim'])

        if X.chunks:
            reduce_dims = [self._dim, kws['feature_dim']]
            mask = _make_mask(X, reduce_dims)
            template = xr.full_like(mask, None, dtype=np.object)
            self._models = xr.map_blocks(_fit_wrapper, X, args=args, kwargs=kws, template=template)
        else:
            self._models = _fit_wrapper(X, *args, **kws) 
Example #4
Source File: test_xarray.py    From xhistogram with MIT License 5 votes vote down vote up
def test_weights(ones, ndims):
    dims = ones.dims
    if ones.ndim < ndims:
        pytest.skip("Don't need to test when number of dimension combinations "
                    "exceeds the number of array dimensions")

    bins = np.array([0, 0.9, 1.1, 2])
    bins_c = 0.5 * (bins[1:] + bins[:-1])

    weight_value = 0.5

    def _check_result(h, d):
        other_dims = [dim for dim in ones.dims if dim not in d]
        if len(other_dims) > 0:
            assert set(other_dims) <= set(h.dims)
        # check that all values are in the central bin
        h_sum = h.sum(other_dims)
        h_sum_expected = xr.DataArray([0, weight_value * ones.size, 0],
                                      dims=['ones_bin'],
                                      coords={'ones_bin': ('ones_bin', bins_c)},
                                      name='histogram_ones')
        xr.testing.assert_identical(h_sum, h_sum_expected)

    # get every possible combination of sub-dimensions
    for n_combinations in range(ones.ndim):
        for weight_dims in combinations(dims, n_combinations):
            i_selector = {dim: 0 for dim in weight_dims}
            weights = xr.full_like(ones.isel(**i_selector), weight_value)
            for nc in range(ndims):
                for d in combinations(dims, nc+1):
                    h = histogram(ones, weights=weights, bins=[bins], dim=d)
                    _check_result(h, d)


# test for issue #5 
Example #5
Source File: data_set.py    From auDeep with GNU General Public License v3.0 5 votes vote down vote up
def partitions(self,
                   partitions: Union[Partition, Sequence[Partition]]):
        """
        Returns a new data set containing only the instances in the specified partitions
        
        Parameters
        ----------
        partitions: Partition or list of Partition
            The partitions to include in the new data set
            
        Returns
        -------
        DataSet
            A new data set containing only the instances in the specified partitions
            
        Raises
        ------
        ValueError
            If there are instances with missing partition information
        """
        if not self.has_partition_info:
            raise ValueError("data set does not have partition info")

        if isinstance(partitions, Partition):
            partitions = [partitions]

        # noinspection PyTypeChecker
        conds = [self._data[_DataVar.PARTITION] == x.value for x in partitions]
        or_cond = xr.full_like(conds[0], fill_value=False, dtype=np.bool)

        for cond in conds:
            # noinspection PyUnresolvedReferences
            or_cond = xr.ufuncs.logical_or(or_cond, cond)

        new_data = self._data.where(or_cond, drop=True)

        return DataSet(data=new_data,
                       mutable=self._mutable) 
Example #6
Source File: run_length.py    From xclim with Apache License 2.0 5 votes vote down vote up
def first_run_after_date(
    da: xr.DataArray,
    window: int,
    date: str = "07-01",
    dim: str = "time",
    coord: Optional[Union[bool, str]] = "dayofyear",
):
    """Return the index of the first item of the first run after a given date.

    Parameters
    ----------
    da : xr.DataArray
      Input N-dimensional DataArray (boolean)
    window : int
      Minimum duration of consecutive run to accumulate values.
    date : str
      The date after which to look for the run.
    dim : str
      Dimension along which to calculate consecutive run (default: 'time').
    coord : Optional[Union[bool, str]]
      If not False, the function returns values along `dim` instead of indexes.
      If `dim` has a datetime dtype, `coord` can also be a str of the name of the
      DateTimeAccessor object to use (ex: 'dayofyear').

    Returns
    -------
    out : xr.DataArray
      Index (or coordinate if `coord` is not False) of first item in the first valid run. Returns np.nan if there are no valid run.
    """
    after_date = datetime.strptime(date, "%m-%d").timetuple().tm_yday

    mid_idx = np.where(da.time.dt.dayofyear == after_date)[0]
    if mid_idx.size == 0:  # The date is not within the group. Happens at boundaries.
        return xr.full_like(da.isel(time=0), np.nan, float).drop_vars("time")

    return first_run(
        da.where(da.time >= da.time[mid_idx][0]), window=window, dim=dim, coord=coord,
    ) 
Example #7
Source File: run_length.py    From xclim with Apache License 2.0 5 votes vote down vote up
def last_run_before_date(
    da: xr.DataArray,
    window: int,
    date: str = "07-01",
    dim: str = "time",
    coord: Optional[Union[bool, str]] = "dayofyear",
):
    """Return the index of the last item of the last run before a given date.

    Parameters
    ----------
    da : xr.DataArray
      Input N-dimensional DataArray (boolean)
    window : int
      Minimum duration of consecutive run to accumulate values.
    date : str
      The date before which to look for the last event.
    dim : str
      Dimension along which to calculate consecutive run (default: 'time').
    coord : Optional[Union[bool, str]]
      If not False, the function returns values along `dim` instead of indexes.
      If `dim` has a datetime dtype, `coord` can also be a str of the name of the
      DateTimeAccessor object to use (ex: 'dayofyear').

    Returns
    -------
    out : xr.DataArray
      Index (or coordinate if `coord` is not False) of last item in last valid run. Returns np.nan if there are no valid run.
    """
    before_date = datetime.strptime(date, "%m-%d").timetuple().tm_yday

    mid_idx = np.where(da.time.dt.dayofyear == before_date)[0]
    if mid_idx.size == 0:  # The date is not within the group. Happens at boundaries.
        return xr.full_like(da.isel(time=0), np.nan, float).drop_vars("time")

    run = da.where(da.time <= da.time[mid_idx][0])
    return last_run(run, window=window, dim=dim, coord=coord) 
Example #8
Source File: calendar.py    From xclim with Apache License 2.0 5 votes vote down vote up
def resample_doy(doy: xr.DataArray, arr: xr.DataArray) -> xr.DataArray:
    """Create a temporal DataArray where each day takes the value defined by the day-of-year.

    Parameters
    ----------
    doy : xr.DataArray
      Array with `dayofyear` coordinate.
    arr : xr.DataArray
      Array with `time` coordinate.

    Returns
    -------
    xr.DataArray
      An array with the same `time` dimension as `arr` whose values are filled according to the day-of-year value in
      `doy`.
    """
    if "dayofyear" not in doy.coords:
        raise AttributeError("Source should have `dayofyear` coordinates.")

    # Adjust calendar
    adoy = adjust_doy_calendar(doy, arr)

    # Create array with arr shape and coords
    out = xr.full_like(arr, np.nan)

    # Fill with values from `doy`
    d = out.time.dt.dayofyear.values
    out.data = adoy.sel(dayofyear=d)

    return out 
Example #9
Source File: yaml_reader.py    From satpy with GNU General Public License v3.0 5 votes vote down vote up
def _load_dataset(dsid, ds_info, file_handlers, dim='y', pad_data=True):
        """Load only a piece of the dataset."""
        if not pad_data:
            return FileYAMLReader._load_dataset(dsid, ds_info,
                                                file_handlers)

        counter, expected_segments, slice_list, failure, projectable = \
            _find_missing_segments(file_handlers, ds_info, dsid)

        if projectable is None or failure:
            raise KeyError(
                "Could not load {} from any provided files".format(dsid))

        empty_segment = xr.full_like(projectable, np.nan)
        for i, sli in enumerate(slice_list):
            if sli is None:
                slice_list[i] = empty_segment

        while expected_segments > counter:
            slice_list.append(empty_segment)
            counter += 1

        if dim not in slice_list[0].dims:
            return slice_list[0]
        res = xr.concat(slice_list, dim=dim)

        combined_info = file_handlers[0].combine_info(
            [p.attrs for p in slice_list])

        res.attrs = combined_info
        return res 
Example #10
Source File: call_bases.py    From starfish with MIT License 4 votes vote down vote up
def _call_bases(
        self, image: xr.DataArray, intensity_threshold: float,
        quality_threshold: float
    ) -> xr.DataArray:
        """
        Determines the nucleotide present in each pixel of each
        (round, channel). The pixel values in the resulting image are the base quality
        score. The base quality score is calculated as the intensity of the pixel
        divided by the L2 norm of the all channels for that pixel. The base
        call score as a range of (0, 1) with a value of 0  and 1 being no call and a
        perfect call, respectively.

        Parameters
        ----------
        image : xr.DataArray
            Image for base calling.
            Should have the following dims: Axes.CH, Axes.X, Axes.Y
        intensity_threshold : float
            Minimum intensity a pixel must have to be called a base.
            Set to zero for no thresholding.
        quality_threshold : float
            Minimum intensity a pixel must have to be called a base.
            Set to zero for no thresholding.

        """
        # Get the maximum value for each round/z
        max_chan = image.argmax(dim=Axes.CH.value)
        max_values = image.max(dim=Axes.CH.value)

        # Get the norms for each pixel
        norms = self._vector_norm(x=image, dim=Axes.CH)

        # Calculate the base qualities
        base_qualities = max_values / norms

        # Filter the base call qualities
        base_qualities_filtered = xr.where(
            base_qualities < quality_threshold, 0, base_qualities
        )

        # Threshold the intensity values
        base_qualities_filtered = xr.where(
            max_values < intensity_threshold, 0, base_qualities_filtered
        )

        # Put the base calls in place
        base_calls = xr.full_like(other=image, fill_value=0)
        base_calls[max_chan] = base_qualities_filtered

        return base_calls 
Example #11
Source File: run_length.py    From xclim with Apache License 2.0 4 votes vote down vote up
def run_length_with_date(
    da: xr.DataArray, window: int, date: str = "07-01", dim: str = "time",
):
    """Return the length of the longest consecutive run of True values found
    to be semi-continuous before and after a given date.

    Parameters
    ----------
    da : xr.DataArray
      Input N-dimensional DataArray (boolean)
    window : int
      Minimum duration of consecutive run to accumulate values.
    date:
      The date that a run must include to be considered valid.
    dim : str
      Dimension along which to calculate consecutive run (default: 'time').

    Returns
    -------
    out : xr.DataArray
      Length of longest run of True values along a given dimension inclusive of a given date.

    Notes
    -----
    The run can include holes of False or NaN values, so long as they do not exceed the window size.
    """
    include_date = datetime.strptime(date, "%m-%d").timetuple().tm_yday

    mid_index = np.where(da.time.dt.dayofyear == include_date)[0]
    if mid_index.size == 0:  # The date is not within the group. Happens at boundaries.
        return xr.full_like(da.isel(time=0), np.nan, float).drop_vars("time")

    end = first_run(
        (~da).where(da.time >= da.time[mid_index][0]), window=window, dim=dim,
    )
    beg = first_run(da, window=window, dim=dim)
    sl = end - beg
    sl = xr.where(beg.isnull() & end.notnull(), 0, sl)  # If series is never triggered
    sl = xr.where(
        beg.notnull() & end.isnull(), da.time.size - beg, sl
    )  # If series is not ended by end of resample time frequency
    return sl.where(sl >= 0) 
Example #12
Source File: run_length.py    From xclim with Apache License 2.0 4 votes vote down vote up
def run_end_after_date(
    da: xr.DataArray,
    window: int,
    date: str = "07-01",
    dim: str = "time",
    coord: Optional[Union[bool, str]] = "dayofyear",
):
    """Return the index of the first item after the end of a run after a given date. The run must begin before the date.

    Parameters
    ----------
    da : xr.DataArray
      Input N-dimensional DataArray (boolean)
    window : int
      Minimum duration of consecutive run to accumulate values.
    date : str
      The date after which to look for the end of a run.
    dim : str
      Dimension along which to calculate consecutive run (default: 'time').
    coord : Optional[Union[bool, str]]
      If not False, the function returns values along `dim` instead of indexes.
      If `dim` has a datetime dtype, `coord` can also be a str of the name of the
      DateTimeAccessor object to use (ex: 'dayofyear').

    Returns
    -------
    out : xr.DataArray
      Index (or coordinate if `coord` is not False) of last item in last valid run. Returns np.nan if there are no valid run.
    """
    after_date = datetime.strptime(date, "%m-%d").timetuple().tm_yday

    mid_idx = np.where(da.time.dt.dayofyear == after_date)[0]
    if mid_idx.size == 0:  # The date is not within the group. Happens at boundaries.
        return xr.full_like(da.isel(time=0), np.nan, float).drop_vars("time")

    end = first_run(
        (~da).where(da.time >= da.time[mid_idx][0]),
        window=window,
        dim=dim,
        coord=coord,
    )
    beg = first_run(da.where(da.time < da.time[mid_idx][0]), window=window, dim=dim)
    end = xr.where(
        end.isnull() & beg.notnull(), da.time.isel(time=-1).dt.dayofyear, end
    )
    return end.where(beg.notnull())