Пример #1
0
 def cmp_(cls0 , cls1):
     if np.issubclass_(cls0, cls1):
         return -1
     elif np.issubclass_(cls1, cls0):
         return 1
     else:
         return 0 # do not change order
Пример #2
0
 def sd_bias_model(self, val):
     if not isinstance(val, str) and not issubclass_(val, bias.ScaleDepBias) and val is not None:
         raise ValueError("scale_dependenent_bias must be a subclass of bias.ScaleDepBias")
     elif isinstance(val, str):
         model = get_model_(val, "halomod.bias")
         if not issubclass_(model, bias.ScaleDepBias):
             raise ValueError("scale_dependenent_bias must be a subclass of bias.ScaleDepBias")
         return model
     return val
Пример #3
0
 def _get_db_type_from_dataframe(self, col_type):
     """Helper method to get SQL data type from DataFrame."""
     if np.issubclass_(col_type, np.floating):
         return self._get_db_type(float)
     elif np.issubclass_(col_type, np.integer):
         return self._get_db_type(int)
     elif np.issubclass_(col_type, np.datetime64):
         return self._get_db_type(datetime)
     else:
         return self._get_db_type(str)
Пример #4
0
    def alter_dndm(self, val):
        """
        A model for empirical recalibration of the HMF.

        :type: None, str, or :class`WDMRecalibrateMF` subclass.
        """
        if not np.issubclass_(val, WDMRecalibrateMF) and val is not None and not np.issubclass_(val, str):
            raise TypeError("alter_dndm must be a WDMRecalibrateMF subclass, string, or None")
        else:
            return val
Пример #5
0
Файл: wdm.py Проект: gjsun/hmf
    def alter_dndm(self, val):
        """
        A model for empirical recalibration of the HMF.

        :type: None, str, or :class`WDMRecalibrateMF` subclass.
        """
        if not np.issubclass_(val, WDMRecalibrateMF) and val is not None and not np.issubclass_(val, basestring):
            raise TypeError("alter_dndm must be a WDMRecalibrateMF subclass, string, or None")
        else:
            return val
Пример #6
0
    def cm(self):
        """A class containing the elements necessary to calculate the concentration-mass relation"""
        this_filter = copy(self.filter)
        this_filter.power = self._power0
        this_profile = self.profile_model(None, self.mean_density0,
                                          self.delta_halo, self.z,
                                          **self.profile_params)

        if issubclass_(self.concentration_model, CMRelation):
            return self.concentration_model(filter0=this_filter,
                                            mean_density0=self.mean_density0,
                                            growth=self.growth,
                                            delta_c=self.delta_c,
                                            profile=this_profile,
                                            cosmo=self.cosmo,
                                            delta_halo=self.delta_halo,
                                            **self.concentration_params)
        else:
            return get_model(self.concentration_model,
                             "halomod.concentration",
                             filter0=this_filter,
                             mean_density0=self.mean_density0,
                             growth=self.growth,
                             delta_c=self.delta_c,
                             profile=this_profile,
                             cosmo=self.cosmo,
                             delta_halo=self.delta_halo,
                             **self.concentration_params)
Пример #7
0
    def bias(self):
        """A class containing the elements necessary to calculate the halo bias"""

        if issubclass_(self.bias_model, bias.Bias):
            return self.bias_model(nu=self.nu,
                                   delta_c=self.delta_c,
                                   m=self.m,
                                   mstar=self.mass_nonlinear,
                                   delta_halo=self.delta_halo,
                                   n=self.n,
                                   Om0=self.cosmo.Om0,
                                   h=self.cosmo.h,
                                   sigma_8=self.sigma_8,
                                   **self.bias_params).bias()
        else:
            # FIXME: this is an ugly hack just to get things fast for the paper.
            if self.bias_model in ["Jing98", "Seljak04"]:
                mstar = self.mass_nonlinear
            else:
                mstar = None
            return get_model(self.bias_model,
                             "halomod.bias",
                             nu=self.nu,
                             delta_c=self.delta_c,
                             m=self.m,
                             mstar=mstar,
                             delta_halo=self.delta_halo,
                             n=self.n,
                             Om0=self.cosmo.Om0,
                             h=self.cosmo.h,
                             sigma_8=self.sigma_8,
                             **self.bias_params).bias()
Пример #8
0
 def sd_bias_model(self, val):
     if not isinstance(val, basestring) and not issubclass_(
             val, bias.ScaleDepBias) and val is not None:
         raise ValueError(
             "scale_dependenent_bias must be a subclass of bias.ScaleDepBias"
         )
     return val
Пример #9
0
 def default(self, obj):
     if isinstance(obj, BaseP2GObject):
         mod_str = str(obj.__class__.__module__)
         mod_str = mod_str + "." if mod_str != __name__ else ""
         cls_str = str(obj.__class__.__name__)
         obj = obj.copy(as_dict=True)
         # object should now be a builtin dict
         obj["__class__"] = mod_str + cls_str
         return obj
         # return super(P2GJSONEncoder, self).encode(obj)
     elif isinstance(obj, datetime):
         return obj.isoformat()
     elif numpy.issubclass_(obj, numpy.number) or isinstance(
             obj, numpy.dtype):
         return dtype_to_str(obj)
     elif hasattr(obj, 'dtype'):
         if obj.size > 1:
             return obj.tolist()
         return int(obj) if numpy.issubdtype(obj,
                                             numpy.integer) else float(obj)
     else:
         try:
             return super(P2GJSONEncoder, self).default(obj)
         except TypeError as e:
             print("TypeError:", str(e), type(obj))
             print(obj)
             raise
Пример #10
0
 def bias_model(self, val):
     if not isinstance(val, str) and not issubclass_(val, bias.Bias):
         raise ValueError("bias_model must be a subclass of bias.Bias")
     elif isinstance(val, str):
         return get_model_(val, "halomod.bias")
     else:
         return val
Пример #11
0
    def __init__(self, *args, **kwargs):
        self.__enums = dict((f, t) for f, t in self._fields_
                            if issubclass_(t, Enum))
        for field, val in iteritems(self._defaults_):
            setattr(self, field, val)

        Structure.__init__(self, *args, **kwargs)
Пример #12
0
 def default(self, obj):
     if isinstance(obj, BaseP2GObject):
         mod_str = str(obj.__class__.__module__)
         mod_str = mod_str + "." if mod_str != __name__ else ""
         cls_str = str(obj.__class__.__name__)
         obj = obj.copy(as_dict=True)
         # object should now be a builtin dict
         obj["__class__"] = mod_str + cls_str
         return obj
         # return super(P2GJSONEncoder, self).encode(obj)
     elif isinstance(obj, datetime):
         return obj.isoformat()
     elif numpy.issubclass_(obj, numpy.number) or isinstance(obj, numpy.dtype):
         return dtype_to_str(obj)
     elif hasattr(obj, 'dtype'):
         if obj.size > 1:
             return obj.tolist()
         return int(obj) if numpy.issubdtype(obj, numpy.integer) else float(obj)
     else:
         try:
             return super(P2GJSONEncoder, self).default(obj)
         except TypeError as e:
             print("TypeError:", str(e), type(obj))
             print(obj)
             raise
Пример #13
0
    def cm(self):
        this_filter = copy(self.filter)
        this_filter.power = self._power0
        this_profile = self.profile_model(None, self.mean_density0,
                                          self.delta_halo, self.z,
                                          **self.profile_params)

        kwargs = dict(filter0=this_filter,
                      mean_density0=self.mean_density0,
                      growth=self.growth,
                      delta_c=self.delta_c,
                      profile=this_profile,
                      cosmo=self.cosmo,
                      delta_halo=self.delta_halo,
                      **self.concentration_params)

        if np.issubclass_(self.concentration_model, CMRelation):
            if self.concentration_model.__class__.__name__.endswith("WDM"):
                cm = self.concentration_model(m_hm=self.wdm.m_hm, **kwargs)
            else:
                cm = self.concentration_model(**kwargs)
        elif self.concentration_model.endswith("WDM"):
            cm = CMRelationWDMRescaled(self.concentration_model[:-3],
                                       m_hm=self.wdm.m_hm,
                                       **kwargs)
        else:
            cm = get_model(self.concentration_model,
                           "halomod.halo_concentration", **kwargs)

        return cm
Пример #14
0
    def __init__(self, *args, **kwargs):
        self.__enums = dict(
            (f, t) for f, t in self._fields_ if issubclass_(t, Enum))
        for field, val in iteritems(self._defaults_):
            setattr(self, field, val)

        Structure.__init__(self, *args, **kwargs)
Пример #15
0
 def mf_fit(self, val):
     if not issubclass_(val, FittingFunction) and not isinstance(
             val, basestring):
         raise ValueError(
             "mf_fit must be a FittingFunction or string, got %s" %
             type(val))
     return val
Пример #16
0
def build_sequential_model(model_states, input_state, output_state,
                           model_compile_dict, **kwargs):
    """
    Parameters
    ----------
    model_states: a list of _operators sampled from operator space
    input_state:
    output_state: specifies the output tensor, e.g. Dense(1, activation='sigmoid')
    model_compile_dict: a dict of `loss`, `optimizer` and `metrics`

    Returns
    ---------
    Keras.Model
    """
    inp = get_layer(None, input_state)
    x = inp
    model_space = kwargs.pop("model_space", None)
    for i, state in enumerate(model_states):
        if issubclass(type(state), Operation):
            x = get_layer(x, state)
        elif issubclass(type(state), int) or np.issubclass_(
                type(state), np.integer):
            assert model_space is not None, "if provided integer model_arc, must provide model_space in kwargs"
            x = get_layer(x, model_space[i][state])
        else:
            raise Exception("cannot understand %s of type %s" %
                            (state, type(state)))
    out = get_layer(x, output_state)
    model = Model(inputs=inp, outputs=out)
    if not kwargs.pop('stop_compile', False):
        model.compile(**model_compile_dict)
    return model
Пример #17
0
 def growth(self):
     "The instantiated growth model"
     if np.issubclass_(self.growth_model, gf.GrowthFactor):
         return self.growth_model(self.cosmo, **self.growth_params)
     else:
         return get_model(self.growth_model, "hmf.growth_factor", cosmo=self.cosmo,
                          **self.growth_params)
Пример #18
0
def type_to_builtin_type(type):
    # Infer from numpy type if it is one
    if type.__module__ == np.__name__:
        return numpy_type_to_builtin_type(type)

    # Otherwise, try to infer from a few generic python types
    if np.issubclass_(type, bool):
        return types_bool
    elif np.issubclass_(type, int):
        return types_int32
    elif np.issubclass_(type, str):
        return types_str
    elif np.issubclass_(type, float):
        return types_fp32
    else:
        raise TypeError("Could not determine builtin type for " + str(type))
Пример #19
0
def stack_petab(dict_of_petabs, keystostack, keytosort=None):
    '''
    stack peTabs along new dimension. 
    dict_of_petabs - the peTabs to stack. The keys of dict_of_petabs do not matter
    keystostack - a list of peTab keys that have to be stacked. Values should have the same shape
    keytosort - a key that is used to sort peTabs along new dimension. 
If None, will be sorted in ascending order of the keys of dict_of_petabs.  
'''
    if keytosort is not None:
        dict_of_petabs_keys = sorted(
            dict_of_petabs.keys(),
            key=lambda k: dict_of_petabs[k].eval_expr(keytosort))
    else:
        dict_of_petabs_keys = sorted(dict_of_petabs.keys())
    LMO_loen_ptb = dict_of_petabs[list(dict_of_petabs_keys)[0]].copy()
    keys = list(LMO_loen_ptb.keys())
    print(keys)
    for key in keys:
        if key in keystostack:
            LMO_loen_ptb[key] = np.stack(
                [dict_of_petabs[i][key] for i in dict_of_petabs_keys], axis=0)
            if len(LMO_loen_ptb[key].shape) == 1:
                LMO_loen_ptb[key] = LMO_loen_ptb[key].reshape(
                    (list(LMO_loen_ptb[key].shape) + [1, 1]))
        elif np.issubclass_(type(LMO_loen_ptb[key]), np.ndarray):
            LMO_loen_ptb[key] = LMO_loen_ptb[key].reshape(
                ([1] + list(LMO_loen_ptb[key].shape)))
    return LMO_loen_ptb
Пример #20
0
    def __truediv__(self, other):
        """Divides the spectra by another object.

        Parameters
        ----------
        other : ndarray, float, int, list or Spectra

        Returns
        -------
        Spectra
            New :class:`Spectra` instance which has the divided spectra.

        Notes
        -----
        This special function, together with :meth:`Spectra.__rtruediv__`, allows the use fo the symbol ``/`` to divide :class:`Spectra` objects.

        See Also
        --------
        :meth:`Spectra.__rtruediv__`
        """
        if np.issubclass_(type(other), Spectra):
            inv_spectra = Spectra([])
            inv_spectra._eng = other.eng
            inv_spectra._in_eng = other.in_eng
            inv_spectra._grid_vals = 1 / other.grid_vals
            inv_spectra._rs = other.rs
            inv_spectra._spec_type = other.spec_type
            inv_spectra._N_underflow = 0
            inv_spectra._eng_underflow = 0
            return self * inv_spectra
        else:
            return self * (1 / other)
Пример #21
0
    def view(self, dtype=None, type=None, fill_value=None):
        """ Return a view of the PDS_marray data.

        Subclassed to fix a NumPy bug that breaks setting fill_value when subselecting a field from a
        structured array.
        """

        try:

            obj = super(PDS_marray, self).view(dtype=dtype,
                                               type=type,
                                               fill_value=fill_value)

            # Fix bug in NumPy < v1.10, which resets fill value on ``view`` if mask is not nomask
            if ((dtype is None) or ((type is None) and np.issubclass_(dtype, np.ma.MaskedArray))) and \
                (fill_value is None):

                obj._fill_value = self._fill_value

        except TypeError:

            # NumPy < v1.8 did not have a fill value attribute for ``view``
            obj = super(PDS_marray, self).view(dtype=dtype, type=type)

        return obj
Пример #22
0
 def concentration_model(self, val):
     """A concentration-mass relation"""
     if not isinstance(val, basestring) and not issubclass_(
             val, CMRelation):
         raise ValueError(
             "concentration_model must be a subclass of concentration.CMRelation"
         )
     return val
Пример #23
0
 def _wdm(self):
     if issubclass_(self.wdm_transfer, WDM):
         return self.wdm_transfer(self.wdm_mass, self.omegac, self.h, self.mean_dens,
                                  **self.wdm_params)
     elif isinstance(self.wdm_transfer, basestring):
         return get_wdm(self.wdm_transfer, mx=self.wdm_mass, omegac=self.omegac,
                        h=self.h, rho_mean=self.mean_dens,
                        **self.wdm_params)
Пример #24
0
 def halo_concentration_model(self, val):
     """A halo_concentration-mass relation"""
     if not isinstance(val, str) and not issubclass_(val, CMRelation):
         raise ValueError("halo_concentration_model must be a subclass of halo_concentration.CMRelation")
     elif isinstance(val, str):
         return get_model_(val, "halomod.concentration")
     else:
         return val
Пример #25
0
 def hod_model(self, val):
     """:class:`~hod.HOD` class"""
     if not isinstance(val, str) and not issubclass_(val, hod.HOD):
         raise ValueError("hod_model must be a subclass of hod.HOD")
     elif isinstance(val, str):
         return get_model_(val, "halomod.hod")
     else:
         return val
Пример #26
0
 def halo_profile_model(self, val):
     """The halo density halo_profile model"""
     if not isinstance(val, str) and not issubclass_(val, profiles.Profile):
         raise ValueError("halo_profile_model must be a subclass of profiles.Profile")
     elif isinstance(val, str):
         return get_model_(val, "halomod.profiles")
     else:
         return val
Пример #27
0
 def replace_dim(self, olddim, newdim):
     if isinstance(olddim, string_types):
         olddim = self.dims_index(olddim)
         olddim = self.dims[olddim]
         if np.issubclass_(newdim, DimBase):
             newdim = newdim(olddim)
     self.dims = replace_dim(self.dims, olddim, newdim)
     return self.dims
Пример #28
0
 def sd_bias(self):
     """A class containing relevant methods to calculate scale-dependent bias corrections"""
     if self.sd_bias_model is None:
         return None
     elif issubclass_(self.sd_bias_model, bias.ScaleDepBias):
         return self.sd_bias_model(self.corr_mm_base, **self.sd_bias_params)
     else:
         return get_model(self.sd_bias_model, "halomod.bias",
                          xi_dm=self.corr_mm_base, **self.sd_bias_params)
Пример #29
0
    def growth_model(self, val):
        """
        The model to use to calculate the growth function/growth rate.

        :type: str or `hmf.growth_factor.GrowthFactor` subclass
        """
        if not np.issubclass_(val, gf.GrowthFactor) and not isinstance(val, str):
            raise ValueError("growth_model must be a GrowthFactor or string, got %s" % type(val))
        return val
Пример #30
0
    def profile_model(self, val):
        """The halo density profile model"""
        if not isinstance(val, basestring) and not issubclass_(val, profiles.Profile):
            raise ValueError("profile_model must be a subclass of profiles.Profile")

        if isinstance(val, basestring):
            return get_model_(val, "halomod.profiles")
        else:
            return val
Пример #31
0
 def transfer(self):
     """
     The instantiated transfer model
     """
     if np.issubclass_(self.transfer_model, tm.TransferComponent):
         return self.transfer_model(self.cosmo, **self.transfer_params)
     elif isinstance(self.transfer_model, str):
         return get_model(self.transfer_model, "hmf.transfer_models", cosmo=self.cosmo,
                          **self.transfer_params)
Пример #32
0
 def cross_hod_model(self, val):
     if not isinstance(val, str) and not np.issubclass_(val, HODCross):
         raise ValueError(
             "cross_hod_model must be a subclass of cross_correlations.HODCross"
         )
     elif isinstance(val, str):
         return get_model_(val, "")
     else:
         return val
Пример #33
0
def warp(gray_img, old_pts, pt1, A):
    """

    Args:
        gray_img: (W, H) The grayscaled input image.
        old_pts : (pts, 2) Old/original point coordinates.
        pt1     : (2,) The first reference point (tl - top left).
        A       : (24,) The 24 mapping coefficients.
    Returns:
        Dewarped image
    """
    # Asserting input
    if gray_img.max() <= 1.0:
        gray_img = gray_img * 255
    if not np.issubclass_(gray_img.dtype.type, np.uint8):
        gray_img = gray_img.astype(np.uint8)

    # Init.
    width, height = gray_img.shape
    old_x, old_y = old_pts[:, 0], old_pts[:, 1]

    # preparation for the place of dewarping values (1 Megapixel = 1000x1000 pixels).
    w_grid, h_grid = np.meshgrid(np.arange(0, width), np.arange(0, height))

    # calculating the new X and Y coordinates of the dewarped image.
    x = w_grid - old_x[pt1]
    y = h_grid - old_y[pt1]

    new_x, new_y = nl_trans(x, y, A)

    new_x = np.round(new_x + old_x[pt1])
    new_y = np.round(new_y + old_y[pt1])

    # recover the image area that are lost during the mapping with black color.
    mask_x = np.multiply((new_x >= 0), (new_x <= (width - 1)))
    mask_y = np.multiply((new_y >= 0), (new_y <= (height - 1)))
    fill_x = np.logical_or((new_x > (width - 1)), (new_x < 0))
    fill_y = np.logical_or((new_y > (height - 1)), (new_y < 0))

    new_x = np.multiply(mask_x, new_x) + np.multiply(fill_x, (width - 1))
    new_y = np.multiply(mask_y, new_y) + np.multiply(fill_y, (height - 1))

    # dewarping the raw images into the new symetrical image.
    gray_img = np.transpose(gray_img)
    dewarp = np.reshape(gray_img, width * height)  # Vector
    i = new_y + (new_x * height)
    i = np.transpose(i)
    i = np.int64(np.reshape(i, width * height))

    i_min, i_max = np.amin(i), np.amax(i)
    min_pos = np.where(i == i_min)

    dewarp_new = dewarp[i]
    dewarp_new = np.reshape(dewarp_new, [height, width])  # Array
    dewarp_new = np.transpose(dewarp_new)

    return dewarp_new
Пример #34
0
Файл: hmf.py Проект: gjsun/hmf
    def hmf_model(self, val):
        """
        A model to use as the fitting function :math:`f(\sigma)`

        :type: str or `hmf.fitting_functions.FittingFunction` subclass
        """
        if not issubclass_(val, ff.FittingFunction) and not isinstance(val, basestring):
            raise ValueError("hmf_model must be a ff.FittingFunction or string, got %s" % type(val))
        return val
Пример #35
0
Файл: hmf.py Проект: gjsun/hmf
    def filter_model(self, val):
        """
        A model for the window/filter function.

        :type: str or :class:`hmf.filters.Filter` subclass
        """
        if not issubclass_(val, Filter) and not isinstance(val, basestring):
            raise ValueError("filter must be a Filter or string, got %s" % type(val))
        return val
Пример #36
0
    def exclusion_model(self, val):
        """A string identifier for the type of halo exclusion used (or None)"""
        if val is None:
            val = "NoExclusion"

        if issubclass_(val, Exclusion):
            return val
        else:
            return get_model_(val, "halomod.halo_exclusion")
Пример #37
0
Файл: wdm.py Проект: gjsun/hmf
    def wdm_model(self, val):
        """
        A model for the WDM effect on the transfer function.

        :type: str or :class:`WDM` subclass
        """
        if not np.issubclass_(val, WDM) and not isinstance(val, basestring):
            raise ValueError("wdm_model must be a WDM subclass or string, got %s" % type(val))
        return val
Пример #38
0
    def wdm_model(self, val):
        """
        A model for the WDM effect on the transfer function.

        :type: str or :class:`WDM` subclass
        """
        if not np.issubclass_(val, WDM) and not isinstance(val, str):
            raise ValueError("wdm_model must be a WDM subclass or string, got %s" % type(val))
        return val
Пример #39
0
def str_to_dtype(dtype_str):
    if numpy.issubclass_(dtype_str, numpy.number):
        # if they gave us a numpy dtype
        return dtype_str

    try:
        return str2dtype[dtype_str]
    except KeyError:
        raise ValueError("Not a valid data type string: %s" % (dtype_str,))
Пример #40
0
Файл: hmf.py Проект: gjsun/hmf
 def filter(self):
     """
     Instantiated model for filter/window functions.
     """
     if issubclass_(self.filter_model, Filter):
         return self.filter_model(self.k,self._unnormalised_power, **self.filter_params)
     elif isinstance(self.filter_model, basestring):
         return get_model(self.filter_model, "hmf.filters", k=self.k,
                             power=self._unnormalised_power, **self.filter_params)
Пример #41
0
    def exclusion_model(self, val):
        """A string identifier for the type of halo exclusion used (or None)"""
        if val is None:
            val = "NoExclusion"

        if issubclass_(val, Exclusion):
            return val
        else:
            return get_model_(val, "halomod.halo_exclusion")
Пример #42
0
    def get_np(self, dtype=None, start=0, n=None):
        '''
        Return vv data in a numpy array

        :param start:   index of first value, must be >=0
        :param n:       number of values wanted
        :param dtype:   numpy data type wanted
        :returns:       (data, (fid_start, fid_incr))

        .. versionadded:: 9.1
        '''

        if dtype is None:
            dtype = self._dtype
        else:
            dtype = np.dtype(dtype)

        if n is None:
            n = self.length - start
        else:
            n = min((self.length - start), n)

        if (n <= 0) or (start < 0):
            raise VVException(
                _t('Cannot get (start,n) ({},{}) from vv of length {}').format(
                    start, n, self.length))

        # strings wanted
        if dtype.type is np.str_:
            if self._sr is None:
                self._sr = gxapi.str_ref()
            npd = np.empty((n, ), dtype=dtype)
            for i in range(start, start + n):
                self._vv.get_string(i, self._sr)
                npd[i - start] = self._sr.value

        # numeric wanted
        else:

            # strings to numeric
            if self._gxtype < 0:
                if np.issubclass_(dtype.type, np.integer):
                    vvd = gxapi.GXVV.create_ext(gxapi.GS_LONG, n)
                else:
                    vvd = gxapi.GXVV.create_ext(gxapi.GS_DOUBLE, n)

                vvd.copy(self._vv)  # this will do the conversion
                npd = vvd.get_data_np(start, n, dtype)

            # numeric to numeric
            else:
                npd = self._vv.get_data_np(start, n, dtype)

        fid = self.fid
        start = fid[0] + start * fid[1]
        return npd, (start, fid[1])
Пример #43
0
 def _unnormalised_lnT(self):
     """
     The un-normalised transfer function
     
     This wraps the individual transfer_fit methods to provide unified access.
     """
     if issubclass_(self.transfer_fit, GetTransfer):
         return self.transfer_fit(self).lnt(self.lnk)
     elif isinstance(self.transfer_fit, basestring):
         return get_transfer(self.transfer_fit, self).lnt(self.lnk)
Пример #44
0
    def get_fill_value(self, item, default_dtype=numpy.float32):
        """If the caller of `get_swath_data` doesn't force the output fill value then they need to know what it is now.
        Defaults version expects a 'data_type' attribute in the value returned from `file_type_info`.

            - Unsigned Integers: Highest valid integer (i.e. -1 converted to unsigned integer)
            - Signed Integers: -999
            - Float: NaN

        :raises: RuntimeError if unknown data type
        """
        data_type = self.get_data_type(item, default_dtype=default_dtype)
        if numpy.issubclass_(data_type, numpy.unsignedinteger):
            return data_type(-1)
        elif numpy.issubclass_(data_type, numpy.integer):
            return -999
        elif numpy.issubclass_(data_type, numpy.floating):
            return numpy.nan
        else:
            raise RuntimeError("Unknown data type for %s: %s" % (item, data_type))
Пример #45
0
 def __init__(self, theta, phi, r, w=None, s=0.0, eps=1e-16):
     if np.issubclass_(w, float):
         w = ones(len(theta)) * w
     nt_, tt_, np_, tp_, c, fp, ier = dfitpack.spherfit_smth(theta, phi, r, w=w, s=s, eps=eps)
     if not ier in [0, -1, -2]:
         message = _spherefit_messages.get(ier, "ier=%s" % (ier))
         raise ValueError(message)
     self.fp = fp
     self.tck = tt_[:nt_], tp_[:np_], c[: (nt_ - 4) * (np_ - 4)]
     self.degrees = (3, 3)
Пример #46
0
    def filter_mod(self):

        if issubclass_(self.filter, Filter):
            filter = self.filter(self.mean_dens, self.delta_c, self.lnk, self._lnP_0,
                                 **self.filter_params)
        elif isinstance(self.filter, basestring):
            filter = get_filter(self.filter, rho_mean=self.mean_dens,
                              delta_c=self.delta_c, lnk=self.lnk, lnp=self._lnP_0,
                              **self.filter_params)

        return filter
Пример #47
0
    def create_output_from_product(self, gridded_product, output_pattern=None,
                                   data_type=None, inc_by_one=None, fill_value=None, **kwargs):
        inc_by_one = inc_by_one or False
        data_type = data_type or gridded_product["data_type"]
        fill_value = fill_value or gridded_product["fill_value"]
        same_fill = numpy.isnan(fill_value) and numpy.isnan(gridded_product["fill_value"]) or fill_value == gridded_product["fill_value"]
        grid_def = gridded_product["grid_definition"]
        if not output_pattern:
            output_pattern = DEFAULT_OUTPUT_PATTERN
        if "{" in output_pattern:
            # format the filename
            of_kwargs = gridded_product.copy(as_dict=True)
            of_kwargs["data_type"] = data_type
            output_filename = self.create_output_filename(output_pattern,
                                                          grid_name=grid_def["grid_name"],
                                                          rows=grid_def["height"],
                                                          columns=grid_def["width"],
                                                          **of_kwargs)
        else:
            output_filename = output_pattern

        if os.path.isfile(output_filename):
            if not self.overwrite_existing:
                LOG.error("Geotiff file already exists: %s", output_filename)
                raise RuntimeError("Geotiff file already exists: %s" % (output_filename,))
            else:
                LOG.warning("Geotiff file already exists, will overwrite: %s", output_filename)

        # if we have a floating point data type, then scaling doesn't make much sense
        if data_type == gridded_product["data_type"] and same_fill:
            LOG.info("Saving product %s to binary file %s", gridded_product["product_name"], output_filename)
            shutil.copyfile(gridded_product["grid_data"], output_filename)
            return output_filename
        elif numpy.issubclass_(data_type, numpy.floating):
            # we didn't rescale any data, but we need to convert it
            data = gridded_product.get_data_array()
        else:
            try:
                LOG.debug("Scaling %s data to fit data type", gridded_product["product_name"])
                data = self.rescaler.rescale_product(gridded_product, data_type,
                                                     inc_by_one=inc_by_one, fill_value=fill_value)
                data = clip_to_data_type(data, data_type)
            except ValueError:
                if not self.keep_intermediate and os.path.isfile(output_filename):
                    os.remove(output_filename)
                raise

        LOG.info("Saving product %s to binary file %s", gridded_product["product_name"], output_filename)
        data = data.astype(data_type)
        fill_mask = gridded_product.get_data_mask()
        data[fill_mask] = fill_value
        data.tofile(output_filename)

        return output_filename
Пример #48
0
    def filter_model(self, val):
        """
        A model for the window/filter function.

        :type: :class:`hmf.filters.Filter` subclass
        """
        if not issubclass_(val, Filter) and not isinstance(val, str):
            raise ValueError("filter must be a Filter or string, got %s" % type(val))
        elif isinstance(val, str):
            return get_model_(val, "hmf.filters")
        else:
            return val
Пример #49
0
Файл: wdm.py Проект: gjsun/hmf
    def wdm(self):
        """
        The instantiated WDM model.

        Contains quantities relevant to WDM.
        """
        if np.issubclass_(self.wdm_transfer, WDM):
            return self.wdm_transfer(self.wdm_mass, self.cosmo,self.z,
                                     **self.wdm_params)
        elif isinstance(self.wdm_transfer, basestring):
            return get_model(self.wdm_model, __name__, mx=self.wdm_mass, cosmo=self.cosmo,
                             z=self.z,**self.wdm_params)
Пример #50
0
def str_to_dtype(dtype_str):
    if numpy.issubclass_(dtype_str, numpy.number):
        # if they gave us a numpy dtype
        return dtype_str
    elif isinstance(dtype_str, str) and hasattr(numpy, dtype_str):
        # they gave us the numpy name of the dtype
        return getattr(numpy, dtype_str)

    try:
        return str2dtype[dtype_str]
    except KeyError:
        raise ValueError("Not a valid data type string: %s" % (dtype_str,))
Пример #51
0
    def cm(self):
        if np.issubclass_(self.cm_relation, CMRelation):
            cm = self.cm_relation(self.filter_mod, delta_c=self.delta_c, z=self.z,
                                  cdict=self.cosmolopy_dict, m_hm=self._wdm.m_hm,
                                  **self.cm_params)
        elif isinstance(self.cm_relation, basestring):
            cm = get_cm(self.cm_relation, filter=self.filter_mod,
                        delta_c=self.delta_c, z=self.z, cdict=self.cosmolopy_dict,
                        m_hm=self._wdm.m_hm,
                        ** self.cm_params)

        return cm
Пример #52
0
    def wdm_model(self, val):
        """
        A model for the WDM effect on the transfer function.

        :type: str or :class:`WDM` subclass
        """
        if np.issubclass_(val, WDM):
            return val
        elif isinstance(val, str):
            return get_model_(val, "hmf.wdm")
        else:
            raise ValueError("wdm_model must be a WDM subclass or string, got %s" % type(val))
Пример #53
0
    def cm(self):
        """A class containing the elements necessary to calculate the concentration-mass relation"""
        if issubclass_(self.cm_relation, CMRelation):
            cm = self.cm_relation(self.filter_mod, delta_c=self.delta_c, z=self.z,
                         cdict=self.cosmolopy_dict, m_hm=None,
                         **self.cm_params)
        elif isinstance(self.cm_relation, basestring):
            cm = get_cm(self.cm_relation, filter=self.filter_mod, delta_c=self.delta_c,
                        z=self.z, cdict=self.cosmolopy_dict, m_hm=None,
                         **self.cm_params)

        return cm
Пример #54
0
 def _fit(self):
     """The actual fitting function class (as opposed to string identifier)"""
     if issubclass_(self.mf_fit, FittingFunction):
         fit = self.mf_fit(M=self.M, nu2=self.nu, z=self.z,
                           delta_halo=self.delta_halo, omegam_z=self.omegam_z,
                           delta_c=self.delta_c, sigma=self.sigma, n_eff=self.n_eff,
                           ** self._fsig_params)
     elif isinstance(self.mf_fit, basestring):
         fit = get_fit(self.mf_fit, M=self.M, nu2=self.nu, z=self.z,
                       delta_halo=self.delta_halo, omegam_z=self.omegam_z,
                       delta_c=self.delta_c, sigma=self.sigma, n_eff=self.n_eff,
                       ** self._fsig_params)
     return fit
Пример #55
0
    def alter_model(self, val):
        """
        A model for empirical recalibration of the HMF.

        :type: None, str, or :class`WDMRecalibrateMF` subclass.
        """
        if np.issubclass_(val, WDMRecalibrateMF):
            return val
        elif val is None:
            return None
        elif isinstance(val, str):
            return get_model_(val, __name__)
        else:
            raise TypeError("alter_model must be a WDMRecalibrateMF subclass, string, or None")