Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unncessary arg from convert_int_to_float() function. #222

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions android_env/components/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

from dm_env import specs
import numpy as np
import numpy.typing as np_typing


def touch_position_to_pixel_position(
Expand Down Expand Up @@ -55,14 +54,11 @@ def orient_pixels(frame: np.ndarray, orientation: int) -> np.ndarray:
)


def convert_int_to_float(data: np.ndarray,
data_spec: specs.Array,
float_type: np_typing.DTypeLike = np.float32):
def convert_int_to_float(data: np.ndarray, data_spec: specs.Array):
"""Converts an array of int values to floats between 0 and 1."""

if not np.issubdtype(data.dtype, np.integer):
raise TypeError(f'{data.dtype} is not an integer type')
if not np.issubdtype(float_type, np.floating):
raise TypeError(f'{float_type} is not a floating-point type')
if isinstance(data_spec, specs.BoundedArray):
value_min = data_spec.minimum
value_max = data_spec.maximum
Expand All @@ -71,4 +67,4 @@ def convert_int_to_float(data: np.ndarray,
iinfo = np.iinfo(data_spec.dtype)
value_min = iinfo.min
value_max = iinfo.max
return float_type(1.0 * (data - value_min) / (value_max - value_min))
return np.float32(1.0 * (data - value_min) / (value_max - value_min))
9 changes: 5 additions & 4 deletions android_env/components/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ def test_convert_int_to_float_bounded_array(self):
maximum=[5, 5, 20, 2],
name='bounded_array')
data = np.array([2, 2, 10, 0], dtype=np.int32)
float_data = utils.convert_int_to_float(data, spec, np.float64)
float_data = utils.convert_int_to_float(data, spec)
np.testing.assert_equal(
np.array([2. / 5., 1. / 4., 0., 0.5], dtype=np.float64), float_data)
np.array([2.0 / 5.0, 1.0 / 4.0, 0.0, 0.5], dtype=np.float32), float_data
)

def test_convert_int_to_float_bounded_array_broadcast(self):
spec = specs.BoundedArray(
shape=(3,), dtype=np.int16, minimum=2, maximum=4, name='bounded_array')
data = np.array([2, 3, 4], dtype=np.int16)
float_data = utils.convert_int_to_float(data, spec, np.float32)
float_data = utils.convert_int_to_float(data, spec)
np.testing.assert_equal(
np.array([0.0, 0.5, 1.0], dtype=np.float32), float_data)

Expand All @@ -93,7 +94,7 @@ def test_convert_int_to_float_no_bounds(self):
dtype=np.int8, # int8 implies min=-128, max=127
name='bounded_array')
data = np.array([-128, 0, 127], dtype=np.int16)
float_data = utils.convert_int_to_float(data, spec, np.float32)
float_data = utils.convert_int_to_float(data, spec)
np.testing.assert_equal(
np.array([0.0, 128. / 255., 1.0], dtype=np.float32), float_data)

Expand Down
5 changes: 3 additions & 2 deletions android_env/wrappers/float_pixels_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ def _process_observation(
self, observation: dict[str, np.ndarray]
) -> dict[str, np.ndarray]:
if self._should_convert_int_to_float:
float_pixels = utils.convert_int_to_float(observation['pixels'],
self._input_spec, np.float32)
float_pixels = utils.convert_int_to_float(
observation['pixels'], self._input_spec
)
observation['pixels'] = float_pixels
return observation

Expand Down
Loading