Exemplo n.º 1
0
 def request(self, index: int, axis: int = -1):
     name, sl = self._request(index)
     if isiterable(name):
         arr = [self.h5file[n].value for n in name]
         arr = [a[:, None] if a.ndim == 1 else a for a in arr]
         return np.concatenate(arr, axis)
     else:
         return self.h5file[name][sl]
Exemplo n.º 2
0
 def dataset_to_numpy(dset):
     if isiterable(dset.value):
         return np.array(dset.value, copy=True)
     elif isinstance(dset.value, numbers.Integral):
         return int(dset.value)
     elif isinstance(dset.value, numbers.Real):
         return float(dset.value)
     else:
         raise TypeError("{} not a compatible type.".format(dset.value))
Exemplo n.º 3
0
    def __init__(self, sigma, pixelsize):
        if isiterable(sigma) and len(sigma) == 2:
            sigma = np.array(sigma, dtype=np.float32)
        elif isinstance(sigma, numbers.Real):
            sigma = np.array([sigma, sigma], dtype=np.float32)

        if isinstance(pixelsize, Coordinate):
            pixelsize = pixelsize['nm'] / pixelsize['px']

        self.sigma = Coordinate(nm=sigma, px=sigma / pixelsize)
Exemplo n.º 4
0
 def _render_children(self, child, add):
     # there is probably a more elegant way to do this, like with
     # Python's function dispatching
     if isiterable(child) and not isinstance(child, dict):
         self._render_iterable(child, add)
     elif isinstance(child, dict):
         self._render_iterable(child.values(), add)
     elif isinstance(child, Spot):
         self._render_spot(child, add)
     elif isinstance(child, Cell):
         self._render_cell(child, add)
     else:
         raise TypeError('Cannot render child.')
def write_results(filename, attrs=None, **kwargs):
    # XXX: super clunky, but good enough for now
    with h5py.File(filename) as f:
        for k, v in kwargs.items():
            if isinstance(v, (list, tuple)):
                arrays = [isiterable(item) for item in v]
                if all(arrays):
                    grp = f.create_group(k)
                    for i, arr in enumerate(v):
                        i = "{:0>5}".format(i)
                        _ = grp.create_dataset(str(i), data=arr)
                else:
                    _ = f.create_dataset(k, data=v)
        if attrs is not None:
            for k, v in attrs.items():
                f.attrs.create(k, v)

    return True
Exemplo n.º 6
0
    def pixelsize(self, value):
        """

        """
        if isinstance(value, numbers.Real):
            self._pixelsize = np.array([value, value], dtype=np.float32)

        elif isinstance(value, Coordinate):
            nm = value['nm'] / value['px']
            if len(value) == 1:
                self._pixelsize = np.array([nm, nm], dtype=np.float32)
            elif len(value) == 2 and all(
                    [i in value.keys() for i in ['nm', 'px']]):
                self._pixelsize = np.array(nm, dtype=np.float32)

        elif isiterable(value) and len(value) == 2:
            self._pixelsize = np.array(value, dtype=np.float32)

        else:
            raise TypeError('pixelsize must be a number, iterable or '
                            'Coordinate.')
Exemplo n.º 7
0
 def filter_dict(dic):
     """
     Parameters
     -----------
     dic : dict
         At least one value should be a list and the others should be
         integers.
         
     If a value in the dictionary is not iterable, repeat it 'length'
     times, where 'length' is the minimum length of an iterable in
     the dictionary's values.
     """
     keys_to_non_iterable_values = []
     lengths = []
     for k, v in dic.items():
         if isiterable(v) and not isinstance(v, dict):
             lengths.append(len(v))
         else:
             keys_to_non_iterable_values.append(k)
     lengths = min(lengths)
     for k in keys_to_non_iterable_values:
         dic.update({k: [dic[k]] * lengths})
     return lengths, dic
Exemplo n.º 8
0
 def to_tuple(arg):
     if isiterable(arg) and not isinstance(arg, dict):
         return tuple(arg)
     else:
         return arg