def test_structured_array(): source_array = np.array([(1, 2.0, 'Hello'), (2, 3.0, 'World')], dtype=[('foo', 'i4'), ('bar', 'f4'), ('baz', 'S10')]) array = DynamicArray(source_array) assert array.shape == source_array.shape for _ in range(4): array.append(source_array[0].tolist())
def test_extending_array_constructor(source_array): array = DynamicArray(source_array) for row in source_array: array.extend(source_array) comparison_array = np.concatenate([source_array] * (1 + source_array.shape[0])) try: assert np.all(np.logical_or(np.isnan(comparison_array), array[:] == comparison_array)) except TypeError: assert np.all(array[:] == comparison_array)
def __init__(self): self.map = DynamicArray((None, None)) self.position = (0, 0, 0) # x, y, heading src = np.float32([[100, 255], [429, 255], [0, 0], [513, 0]]) dst = np.float32([[202, 255], [328, 255], [0, 0], [513, 0]]) self.warp_matrix = cv2.getPerspectiveTransform(src, dst)
def test_appending(source_array): dtype = source_array.dtype input_arg = (None,) + source_array.shape[1:] array = DynamicArray(input_arg, dtype, allow_views_on_resize=True) _ = array[:] # NOQA for row in source_array: array.append(row) try: assert np.all(np.logical_or(np.isnan(source_array), array[:] == source_array)) except TypeError: assert np.all(array[:] == source_array)
def test_extending(source_array): dtype = source_array.dtype input_arg = (None,) + source_array.shape[1:] array = DynamicArray(input_arg, dtype) for row in source_array: array.extend(source_array) comparison_array = np.concatenate([source_array] * source_array.shape[0]) try: assert np.all(np.logical_or(np.isnan(comparison_array), array[:] == comparison_array)) except TypeError: assert np.all(array[:] == comparison_array)
def test_array_constructor(source_array): array = DynamicArray(source_array) try: assert np.all(np.logical_or(np.isnan(source_array), array[:] == source_array)) except TypeError: assert np.all(array[:] == source_array)
def _init_buffers(self): self.observations = DynamicArray( array_or_shape=(None, ) + tuple(self.env.observation_space.shape), dtype=self.env.observation_space.dtype, capacity=int(self.initial_capacity)) self.actions = DynamicArray(array_or_shape=(None, ) + tuple(self.env.action_space.shape), dtype=self.env.action_space.dtype, capacity=int(self.initial_capacity)) self.rewards = DynamicArray(array_or_shape=(None, ), dtype=np.float32, capacity=int(self.initial_capacity)) self.dones = DynamicArray(array_or_shape=(None, ), dtype=np.bool, capacity=int(self.initial_capacity))
def test_appending_lists(source_array): dtype = source_array.dtype input_arg = (None,) + source_array.shape[1:] array = DynamicArray(input_arg, dtype, allow_views_on_resize=True) _ = array[:] # NOQA for row in source_array: row_list = row.tolist() if isinstance(row_list, basestring): # Numpy has problems parsing unicode return array.append(row_list) try: assert np.all(np.logical_or(np.isnan(source_array), array[:] == source_array)) except TypeError: assert np.all(array[:] == source_array)
def test_appending_with_views_fails(source_array): dtype = source_array.dtype input_arg = (None,) + source_array.shape[1:] array = DynamicArray(input_arg, dtype) array.shrink_to_fit() # Force a reallocation on first append _ = array[:] # NOQA try: for row in source_array: array.append(row) assert False, 'An exception should have been raised.' except ValueError as e: assert 'allow_views_on_resize' in e.message
class TrajectorySampler: def __init__( self, env: EnvironmentSpec, agent: Union[ActorSpec, CriticSpec], device, deterministic: bool = False, initial_capacity: int = int(1e3), sample_flag: mp.Value = None ): self.env = env self.agent = agent self._device = device self.deterministic = deterministic self.initial_capacity = initial_capacity self.policy_handler = PolicyHandler( env=self.env, agent=self.agent, device=device ) self._sample_flag = sample_flag or mp.Value(c_bool, True) self._init_buffers() def _init_buffers(self): self.observations = DynamicArray( array_or_shape=(None, ) + tuple(self.env.observation_space.shape), dtype=self.env.observation_space.dtype, capacity=int(self.initial_capacity) ) self.actions = DynamicArray( array_or_shape=(None, ) + tuple(self.env.action_space.shape), dtype=self.env.action_space.dtype, capacity=int(self.initial_capacity) ) self.rewards = DynamicArray( array_or_shape=(None, ), dtype=np.float32, capacity=int(self.initial_capacity) ) self.dones = DynamicArray( array_or_shape=(None, ), dtype=np.bool, capacity=int(self.initial_capacity) ) def _to_tensor(self, *args, **kwargs): return torch.Tensor(*args, **kwargs).to(self._device) def _init_with_observation(self, observation): self.observations.append(observation) def _put_transition(self, transition): """ transition = [o_tp1, a_t, r_t, d_t] """ o_tp1, a_t, r_t, d_t = transition self.observations.append(o_tp1) self.actions.append(a_t) self.rewards.append(r_t) self.dones.append(d_t) def _get_states_history(self, history_len=None): history_len = history_len or self.env.history_len states = [ self.get_state(history_len=history_len, index=i) for i in range(len(self.observations)) ] states = np.array(states) return states def get_state(self, index=None, history_len=None): index = index if index is not None else len(self.observations) - 1 history_len = history_len \ if history_len is not None \ else self.env.history_len state = np.zeros( (history_len, ) + self.env.observation_space.shape, dtype=self.env.observation_space.dtype ) indices = np.arange(max(0, index - history_len + 1), index + 1) state[-len(indices):] = self.observations[indices] return state def get_trajectory(self): trajectory = ( np.array(self.observations[:-1]), np.array(self.actions), np.array(self.rewards), np.array(self.dones) ) return trajectory @torch.no_grad() def reset(self, exploration_strategy=None): if isinstance(exploration_strategy, OrnsteinUhlenbeckProcess): exploration_strategy.reset_state(self.env.action_space.shape[0]) if isinstance(exploration_strategy, ParameterSpaceNoise) \ and len(self.observations) > 1: states = self._get_states_history() states = self._to_tensor(states) exploration_strategy.update_actor(self.agent, states) self._init_buffers() self._init_with_observation(self.env.reset()) def sample(self, exploration_strategy=None): reward, num_steps, done_t = 0, 0, False while not done_t and self._sample_flag.value: state_t = self.get_state() action_t = self.policy_handler.action_fn( agent=self.agent, state=state_t, device=self._device, exploration_strategy=exploration_strategy, deterministic=self.deterministic ) observation_tp1, reward_t, done_t, info = self.env.step(action_t) reward += reward_t transition = [observation_tp1, action_t, reward_t, done_t] self._put_transition(transition) num_steps += 1 if not self._sample_flag.value: return None, None trajectory = self.get_trajectory() trajectory_info = {"reward": reward, "num_steps": num_steps} return trajectory, trajectory_info
def get_amazon_ratings(subset, test_set_fraction=0.1, test_user_fraction=0.1, min_ratings=5): users = DynamicArray(dtype=np.int32) items = DynamicArray(dtype=np.int32) ratings = DynamicArray(dtype=np.float32) timestamps = DynamicArray(dtype=np.int32) for user, item, rating, timestamp in _read_amazon_ratings(subset): users.append(user) items.append(item) ratings.append(rating) timestamps.append(timestamp) shape = users[:].max() + 1, items[:].max() + 1 test_cutoff_index = int(len(timestamps) * (1.0 - test_set_fraction)) test_cutoff_timestamp = np.sort(timestamps[:])[test_cutoff_index] in_train = timestamps < test_cutoff_timestamp in_test_time = np.logical_not(in_train) unique_profiles_after_split = np.unique(users[in_test_time]) test_and_validation = np.random.choice( unique_profiles_after_split, int(test_user_fraction * len(unique_profiles_after_split))) test_users, validation_users = np.array_split(test_and_validation, 2) test_users, validation_users = set(test_users), set(validation_users) in_test = np.logical_and(in_test_time, np.array([x in test_users for x in users])) in_validation = np.logical_and( in_test_time, np.array([x in validation_users for x in users])) return ( sp.coo_matrix((ratings[in_train], (users[in_train], items[in_train])), shape=shape), sp.coo_matrix((ratings[in_test], (users[in_test], items[in_test])), shape=shape), sp.coo_matrix((ratings[in_validation], (users[in_validation], items[in_validation])), shape=shape), )
def make_neighbors(path): global titler lang = os.path.basename(path) emb = LangEmbedding(lang, path, titler) emb.build_fast_knn() n = emb.nrows() rows = DynamicArray(dtype='int32') cols = DynamicArray(dtype='int32') vals = DynamicArray(dtype='float32') for i, id in enumerate(emb.ids): if i % 10000 == 0: logging.info('generating neighbors for id %d of %d in %s', i, len(emb.ids), emb.lang) neighbors = emb.neighbors(id, n=100, include_distances=True, use_indexes=True) for (j, dist) in neighbors: if j != i: rows.append(i) cols.append(j) vals.append(dist) csr = csr_matrix((vals, (rows, cols)), shape=(n, n), dtype='float32') save_npz(os.path.join(emb.dir, 'neighbors'), csr)
def test_explicit_dtype_overrides_input_dtype(): source = np.zeros(10, dtype=np.float64) array = DynamicArray(source, dtype=np.float16) assert array[:].dtype == np.float16
def test_shape(source_array): array = DynamicArray(source_array) assert array.shape == source_array.shape
def test_attr_delegation(source_array): array = DynamicArray(source_array) try: source_array + source_array except TypeError: return try: assert_equal_or_nan(array + source_array, source_array + source_array) except TypeError: assert (array + source_array == source_array + source_array) try: assert_equal_or_nan(array - source_array, source_array - source_array) except TypeError: assert (array - source_array == source_array - source_array) try: assert_equal_or_nan(array * source_array, source_array * source_array) except TypeError: assert (array * source_array == source_array * source_array) try: assert_equal_or_nan(array / source_array, source_array / source_array) except TypeError: assert (array / source_array == source_array / source_array) try: assert_equal_or_nan(array ** 2, source_array ** 2) except TypeError: assert (array ** 2 == source_array ** 2) # In-place operators try: array = DynamicArray(source_array) array += source_array assert_equal_or_nan(array, source_array + source_array) except TypeError: array += source_array assert (array == source_array + source_array) try: array = DynamicArray(source_array) array -= source_array assert_equal_or_nan(array, source_array - source_array) except TypeError: array -= source_array assert (array == source_array - source_array) try: array = DynamicArray(source_array) array *= source_array assert_equal_or_nan(array, source_array * source_array) except TypeError: array *= source_array assert (array == source_array * source_array) try: array = DynamicArray(source_array) array /= source_array assert_equal_or_nan(array, source_array / source_array) except TypeError: array /= source_array assert (array == source_array / source_array)