def do_compute(self, chunk_i=None, **kwargs): """Wrapper for the user-defined compute method This is the 'job' that gets executed in different processes/threads during multiprocessing """ if self.compute_takes_chunk_i: result = self.compute(chunk_i=chunk_i, **kwargs) else: result = self.compute(**kwargs) if isinstance(result, dict): if not len(result): # TODO: alt way of getting length? raise RuntimeError("if returning dict, must have a key") some_key = list(result.keys())[0] n = len(result[some_key]) r = np.zeros(n, dtype=self.dtype) for k, v in result.items(): r[k] = v result = r if result.dtype != self.dtype: raise strax.PluginGaveWrongOutput( f"Plugin {self.__class__.__name__} did not deliver " f"the data type it promised.\n" f"Promised: {self.dtype}\n" f"Delivered: {result.dtype}.") return result
def _check_dtype(self, x, d=None): if d is None: assert not self.multi_output d = self.provides[0] expect = self.dtype_for(d) pname = self.__class__.__name__ if not isinstance(x, np.ndarray): raise strax.PluginGaveWrongOutput( f"Plugin {pname} did not deliver " f"data type {d} as promised.\n" f"Delivered a {type(x)}") if not isinstance(expect, np.dtype): raise ValueError(f"Plugin {pname} expects {expect} as dtype??") if x.dtype != expect: raise strax.PluginGaveWrongOutput( f"Plugin {pname} did not deliver " f"data type {d} as promised.\n" f"Promised: {self.dtype_for(d)}\n" f"Delivered: {x.dtype}.")
def _check_dtype(self, x, d=None): # There is an additional 'last resort' data type check # in the chunk initialization. # This one is broader and gives a more context-aware message. if d is None: assert not self.multi_output d = self.provides[0] pname = self.__class__.__name__ if not isinstance(x, np.ndarray): raise strax.PluginGaveWrongOutput( f"Plugin {pname} did not deliver " f"data type {d} as promised.\n" f"Delivered a {type(x)}") expect = strax.remove_titles_from_dtype(self.dtype_for(d)) if not isinstance(expect, np.dtype): raise ValueError(f"Plugin {pname} expects {expect} as dtype??") got = strax.remove_titles_from_dtype(x.dtype) if got != expect: raise strax.PluginGaveWrongOutput( f"Plugin {pname} did not deliver " f"data type {d} as promised.\n" f"Promised: {expect}\n" f"Delivered: {got}.")