Esempio n. 1
0
 def __init__(self, guess, plus, minus=None, name=None):
     self.guess = ensure_scalar(guess)
     self.plus = ensure_scalar(plus)
     if minus is None:
         self.minus = self.plus
     else:
         self.minus = ensure_scalar(minus)
     self.name = name
Esempio n. 2
0
 def _lnlike(self, pars, data):
     """
     Internal function taking pars as a list only
     """
     noise_sd = self._find_noise(pars, data)
     N = data.size
     log_likelihood = ensure_scalar(
         -N / 2 * np.log(2 * np.pi) -
         N * np.mean(np.log(ensure_array(noise_sd))) - 0.5 *
         (self._residuals(pars, data, noise_sd)**2).sum())
     return log_likelihood
Esempio n. 3
0
    def lnlike(self, pars, data):
        """
        Compute the log-likelihood for pars given data

        Parameters
        -----------
        pars: dict(string, float)
            Dictionary containing values for each parameter
        data: xarray
            The data to compute likelihood against

        Returns
        --------
        lnlike: float
        """
        noise_sd = self._find_noise(pars, data)
        N = data.size
        log_likelihood = ensure_scalar(
            -N/2 * np.log(2 * np.pi) -
            N * np.mean(np.log(ensure_array(noise_sd))) -
            0.5 * (self._residuals(pars, data, noise_sd)**2).sum())
        return log_likelihood
Esempio n. 4
0
def display_image(im,
                  scaling='auto',
                  vert_axis='x',
                  horiz_axis='y',
                  depth_axis='z',
                  colour_axis='illumination'):
    im = im.copy()
    if isinstance(im, xr.DataArray):
        if hasattr(im, 'z') and len(im['z']) == 1 and depth_axis is not 'z':
            im = im[{'z': 0}]
        if depth_axis == 'z' and 'z' not in im.dims:
            im = im.expand_dims('z')
        if im.ndim > 3 + (colour_axis in im.dims):
            raise BadImage("Too many dims on DataArray to output properly.")
        attrs = im.attrs
    else:
        attrs = {}
        im = ensure_array(im)
        if im.ndim > 3:
            raise BadImage("Too many dims on ndarray to output properly.")
        elif im.ndim == 2:
            im = np.array([im])
        elif im.ndim < 2:
            raise BadImage("Too few dims on ndarray to output properly.")
        axes = [0, 1, 2]
        for axis in [vert_axis, horiz_axis, depth_axis]:
            if isinstance(axis, int):
                try:
                    axes.remove(axis)
                except KeyError:
                    raise ValueError("Cannot interpret axis specifications.")
        if len(axes) > 0:
            if not isinstance(depth_axis, int):
                depth_axis = axes[np.argmin([im.shape[i] for i in axes])]
                axes.remove(depth_axis)
            if not isinstance(vert_axis, int):
                vert_axis = axes[0]
                axes.pop(0)
            if not isinstance(horiz_axis, int):
                horiz_axis = axes[0]
        im = im.transpose([depth_axis, vert_axis, horiz_axis])
        depth_axis = 'z'
        vert_axis = 'x'
        horiz_axis = 'y'
        im = data_grid(im, spacing=1, z=range(len(im)))
    if np.iscomplex(im).any():
        warn("Image contains complex values. Taking image magnitude.")
        im = np.abs(im)
    if scaling is 'auto':
        scaling = (ensure_scalar(im.min()), ensure_scalar(im.max()))
    if scaling is not None:
        im = np.maximum(im, scaling[0])
        im = np.minimum(im, scaling[1])
        im = (im - scaling[0]) / (scaling[1] - scaling[0])
    im.attrs = attrs
    im.attrs['_image_scaling'] = scaling

    if colour_axis in im.dims:
        cols = [
            col[0].capitalize() if isinstance(col, str) else ' '
            for col in im[colour_axis].values
        ]
        RGB_names = np.all([letter in 'RGB' for letter in cols])
        if len(im[colour_axis]) == 1:
            im = im.squeeze(dim=colour_axis)
        elif len(im[colour_axis]) > 3:
            raise BadImage('Cannot output more than 3 colour channels')
        elif RGB_names:
            channels = {
                col: im[{
                    colour_axis: i
                }]
                for i, col in enumerate(cols)
            }
            if len(channels) == 2:
                dummy = im[{colour_axis: 0}].copy()
                dummy[:] = im.min()
                for i, col in enumerate('RGB'):
                    if col not in cols:
                        dummy[colour_axis] = col
                        channels[col] = dummy
                        channels['R'].attrs['_dummy_channel'] = i
                        break
            channels = [channels[col] for col in 'RGB']
            im = clean_concat(channels, colour_axis)
        elif len(im[colour_axis]) == 2:
            dummy = xr.full_like(im[{colour_axis: 0}], fill_value=im.min())
            dummy = dummy.expand_dims({colour_axis: [np.NaN]})
            im.attrs['_dummy_channel'] = -1
            im = clean_concat([im, dummy], colour_axis)
    dim_order = [depth_axis, vert_axis, horiz_axis, colour_axis][:im.ndim]
    return im.transpose(*dim_order)
Esempio n. 5
0
 def test_ensure_scalar(self):
     self.assertEqual(ensure_scalar(1), 1)
     self.assertEqual(ensure_scalar(np.array(1)), 1)
     self.assertEqual(ensure_scalar(np.array([1])), 1)