Exemple #1
0
 def _add_toyalgorithpair(self, name: str, value: InterpAlgorithm) -> None:
     toy_new = timetools.TOY(name)
     if len(self._toy2algorithm) == 0:
         self._toy2algorithm.append((toy_new, value))
     secs_new = toy_new.seconds_passed
     if secs_new > self._toy2algorithm[-1][0].seconds_passed:
         self._toy2algorithm.append((toy_new, value))
     else:
         for idx, (toy_old, _) in enumerate(self._toy2algorithm[:]):
             secs_old = toy_old.seconds_passed
             if secs_new == secs_old:
                 self._toy2algorithm[idx] = toy_new, value
                 break
             if secs_new < secs_old:
                 self._toy2algorithm.insert(idx, (toy_new, value))
                 break
Exemple #2
0
 def __delattr__(self, name: str) -> None:
     if name.startswith("toy_"):
         try:
             try:
                 del self._toy2algorithm[timetools.TOY(name)]
             except KeyError:
                 raise AttributeError(
                     f"No interpolator is registered under a TOY object named "
                     f"`{timetools.TOY(name)}`.") from None
             self.refresh()
         except BaseException:
             objecttools.augment_excmessage(
                 f"While trying to remove an interpolator from parameter "
                 f"{objecttools.elementphrase(self)} based on the string `{name}`"
             )
     else:
         object.__delattr__(self, name)
Exemple #3
0
 def __setattr__(self, name: str, value: object) -> None:
     if name.startswith("toy_"):
         try:
             if not isinstance(value, InterpAlgorithm):
                 raise TypeError(
                     f"{objecttools.value_of_type(value).capitalize()} has been "
                     f"given, but an object of type `InterpAlgorithm` is required."
                 )
             self._toy2algorithm[timetools.TOY(name)] = value
             self.refresh()
         except BaseException:
             objecttools.augment_excmessage(
                 f"While trying to assign a new interpolator to parameter "
                 f"{objecttools.elementphrase(self)} based on the string `{name}`"
             )
     else:
         object.__setattr__(self, name, value)
Exemple #4
0
 def __getattr__(self, name: str) -> InterpAlgorithm:
     if name.startswith("toy_"):
         try:
             try:
                 return self._toy2algorithm[timetools.TOY(name)]
             except KeyError:
                 raise AttributeError(
                     f"No interpolator is registered under a TOY object named "
                     f"`{timetools.TOY(name)}`.") from None
         except BaseException:
             objecttools.augment_excmessage(
                 f"While trying to look up for an interpolator handled by "
                 f"parameter {objecttools.elementphrase(self)} based on the string "
                 f"`{name}`")
     else:
         raise AttributeError(
             f"'{type(self).__name__}' object has no attribute '{name}'")
Exemple #5
0
 def __delattr__(self, name: str) -> None:
     if name.startswith("toy_"):
         try:
             selected = timetools.TOY(name)
             for idx, (available, _) in enumerate(self._toy2algorithm):
                 if selected == available:
                     break
             else:
                 raise AttributeError(
                     f"No interpolator is registered under a TOY object named "
                     f"`{timetools.TOY(name)}`.") from None
             del self._toy2algorithm[idx]
             self.refresh()
         except BaseException:
             objecttools.augment_excmessage(
                 f"While trying to remove an interpolator from parameter "
                 f"{objecttools.elementphrase(self)} based on the string `{name}`"
             )
     else:
         object.__delattr__(self, name)
Exemple #6
0
 def _interp(self) -> None:
     ratios = self.ratios
     ratios[:, :] = numpy.nan
     toys = self.toys
     centred = timetools.TOY.centred_timegrid()
     for tdx, (date, rel) in enumerate(zip(*centred)):
         if rel:
             xnew = timetools.TOY(date)
             for idx_1, x_1 in enumerate(toys):
                 if x_1 > xnew:
                     idx_0 = idx_1 - 1
                     x_0 = toys[idx_0]
                     break
             else:
                 idx_0 = -1
                 idx_1 = 0
                 x_0 = toys[idx_0]
                 x_1 = toys[idx_1]
             ratios[tdx, :] = 0.0
             ratios[tdx, idx_1] = (xnew - x_0) / (x_1 - x_0)
             ratios[tdx, idx_0] = 1.0 - ratios[tdx, idx_1]
Exemple #7
0
 def __call__(
     self,
     *algorithm: InterpAlgorithm,
     **algorithms: InterpAlgorithm,
 ) -> None:
     self._toy2algorithm.clear()
     self._do_refresh = False
     try:
         if (len(algorithm) > 1) or (algorithm and algorithms):
             raise ValueError(
                 f"Type `{type(self).__name__}` accepts either a single positional "
                 f"argument or an arbitrary number of keyword arguments, but for "
                 f"the corresponding parameter of element "
                 f"`{objecttools.devicename(self)}` {len(algorithm)} positional "
                 f"and {len(algorithms)} keyword arguments have been given."
             )
         if algorithm:
             algorithms["_1"] = algorithm[0]
         for (toystr, value) in algorithms.items():
             if not isinstance(value, InterpAlgorithm):
                 raise TypeError(
                     f"Type `{type(value).__name__}` is not (a subclass of) type "
                     f"`InterpAlgorithm`.")
             try:
                 setattr(self, str(timetools.TOY(toystr)), value)
             except BaseException:
                 objecttools.augment_excmessage(
                     f"While trying to add a season specific interpolator to "
                     f"parameter `{self.name}` of element "
                     f"`{objecttools.devicename(self)}`")
     except BaseException as exc:
         self._toy2algorithm.clear()
         raise exc
     finally:
         self._do_refresh = True
         self.refresh()