36 def __init__(self, observation_size, *, order=2, scale=None, time_channel=False):
59 def encode(self, trajectory, *, times=None):
60 """Use exact first and second iterated integrals for linear segments."""
61 points = np.asarray(trajectory, dtype=float)
63 or not len(points)
or not np.isfinite(points).all()):
64 raise ValueError(
"Supply a nonempty finite path with the declared observation width.")
66 raise ValueError(
"Explicit times require time_channel=True.")
67 with np.errstate(over=
"ignore", invalid=
"ignore", divide=
"ignore"):
68 increments = np.diff(points, axis=0)
69 overflowed = np.isinf(increments)
72 rows, cols = np.nonzero(overflowed)
73 increments[rows, cols] = (points[rows + 1, cols] / self.
_scale[cols]
74 - points[rows, cols] / self.
_scale[cols])
77 time_increments = np.full(len(points) - 1, 1 / max(1, len(points) - 1))
79 timestamps = np.asarray(times, dtype=float)
80 if (timestamps.shape != (len(points),)
or not np.isfinite(timestamps).all()
81 or (timestamps[1:] <= timestamps[:-1]).any()):
82 raise ValueError(
"Supply one finite, strictly increasing timestamp per observation.")
84 if len(timestamps) > 1
and not np.isfinite(timestamps[-1] - timestamps[0]):
85 timestamps = timestamps * .5
86 time_increments = np.diff(timestamps)
87 if len(time_increments):
88 time_increments /= timestamps[-1] - timestamps[0]
89 increments = np.column_stack((increments, time_increments))
90 level_one = increments.sum(axis=0)
93 prefixes = np.zeros_like(increments)
94 prefixes[1:] = np.cumsum(increments[:-1], axis=0)
95 level_two = prefixes.T @ increments + .5 * (increments.T @ increments)
96 result = np.concatenate((level_one, level_two.ravel()))
97 if not np.isfinite(result).all():
98 raise ValueError(
"The path signature overflowed. Rescale the input channels.")
108 def make_archive(self, trajectories, cells, *, fitness_domain=(-np.inf, np.inf), fitness_weight=1., seed=0):
109 """Use reference paths for centroids and allow finite extrapolations at search time."""
110 cells = _positive_int(cells,
"cells")
111 if not np.isfinite(fitness_weight)
or fitness_weight == 0:
112 raise ValueError(
"Use a finite nonzero fitness weight.")
113 descriptors = np.asarray([self.
encode(path)
for path
in trajectories])
114 if len(descriptors) < cells
or len(np.unique(descriptors, axis=0)) < cells:
115 raise ValueError(
"Provide at least one distinct reference signature per cell.")
116 rng = np.random.default_rng(seed)
117 with threadpool_limits(limits=1):
118 model = KMeans(cells, n_init=1, random_state=int(rng.integers(2**32)))
119 centers = model.fit(descriptors).cluster_centers_
121 return CVTGrid(shape=cells, centroids=centers, features_domain=((-np.inf, np.inf),) * self.
descriptor_size,
122 fitness_domain=(tuple(fitness_domain),), fitness_weights=(fitness_weight,))