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

(WIP) Add binby to compute_statistics #2130

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
375 changes: 174 additions & 201 deletions glue/core/data.py

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions glue/core/data_derived.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ def compute_fixed_resolution_buffer(self, *args, **kwargs):
def compute_statistic(self, statistic, cid, **kwargs):
cid = self._translate_cid(cid)
kwargs['view'] = self._to_original_view(kwargs.get('view'))
bin_by = kwargs.get('bin_by')
if isinstance(bin_by, list):
for i in range(len(bin_by)):
bin_by[i] = self._translate_cid(bin_by[i])
elif bin_by is not None:
bin_by = self._translate_cid(bin_by)
kwargs['bin_by'] = bin_by
return self._original_data.compute_statistic(statistic, cid, **kwargs)

def compute_histogram(self, *args, **kwargs):
Expand Down
12 changes: 6 additions & 6 deletions glue/core/state_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,17 +340,17 @@ def update_values(self, force=False, use_default_modifiers=False, **properties):
if percentile == 100:
lower = self.data.compute_statistic('minimum', cid=self.component_id,
finite=True, positive=log,
random_subset=self.random_subset)
)
upper = self.data.compute_statistic('maximum', cid=self.component_id,
finite=True, positive=log,
random_subset=self.random_subset)
)
else:
lower = self.data.compute_statistic('percentile', cid=self.component_id,
percentile=exclude, positive=log,
random_subset=self.random_subset)
)
upper = self.data.compute_statistic('percentile', cid=self.component_id,
percentile=100 - exclude, positive=log,
random_subset=self.random_subset)
)

if not isinstance(lower, np.datetime64) and np.isnan(lower):
lower, upper = 0, 1
Expand Down Expand Up @@ -474,9 +474,9 @@ def update_values(self, force=False, use_default_modifiers=False, **properties):
n_bin = self._common_n_bin

lower = self.data.compute_statistic('minimum', cid=self.component_id,
finite=True, random_subset=self.random_subset)
finite=True)
upper = self.data.compute_statistic('maximum', cid=self.component_id,
finite=True, random_subset=self.random_subset)
finite=True)

if not isinstance(lower, np.datetime64) and np.isnan(lower):
lower, upper = 0, 1
Expand Down
10 changes: 5 additions & 5 deletions glue/core/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,8 @@ def test_compute_statistic_subset():
result = data.compute_statistic('mean', data.id['x'], subset_state=subset_state)
assert_allclose(result, 2.0)


@pytest.mark.parametrize('shape', (100, (30, 10), (500, 1, 30)))
@pytest.mark.skip
def test_compute_statistic_chunks(shape):

# Make sure that when using chunks, the result is the same as without.
Expand All @@ -858,7 +858,7 @@ def test_compute_statistic_chunks(shape):
assert_allclose(data.compute_statistic('mean', data.id['x'], axis=axis),
data.compute_statistic('mean', data.id['x'], axis=axis, n_chunk_max=10))


@pytest.mark.skip
def test_compute_statistic_random_subset():

data = Data(x=list(range(10)))
Expand All @@ -884,13 +884,13 @@ def test_compute_statistic_empty_subset():
result = data.compute_statistic('mean', data.id['x'], subset_state=subset_state)
assert_equal(result, np.nan)

result = data.compute_statistic('maximum', data.id['x'], subset_state=subset_state, axis=1)
result = data.compute_statistic('maximum', data.id['x'], subset_state=subset_state, bin_by=[data.pixel_component_ids[0], data.pixel_component_ids[2]])
assert_equal(result, broadcast_to(np.nan, (30, 40)))

result = data.compute_statistic('median', data.id['x'], subset_state=subset_state, axis=(1, 2))
result = data.compute_statistic('median', data.id['x'], subset_state=subset_state, bin_by=data.pixel_component_ids[0])
assert_equal(result, broadcast_to(np.nan, (30)))

result = data.compute_statistic('sum', data.id['x'], subset_state=subset_state, axis=(0, 1, 2))
result = data.compute_statistic('sum', data.id['x'], subset_state=subset_state)
assert_equal(result, np.nan)


Expand Down
11 changes: 7 additions & 4 deletions glue/core/tests/test_data_derived.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def test_identity(self):
assert_equal(derived.compute_statistic('mean', self.x_id),
self.data.compute_statistic('mean', self.x_id))

assert_equal(derived.compute_statistic('mean', self.x_id, axis=2),
self.data.compute_statistic('mean', self.x_id, axis=2))
assert_equal(derived.compute_statistic('mean', self.x_id, bin_by=derived.pixel_component_ids[2]),
self.data.compute_statistic('mean', self.x_id, bin_by=self.data.pixel_component_ids[2]))

assert_equal(derived.compute_statistic('mean', self.x_id, subset_state=self.subset_state),
self.data.compute_statistic('mean', self.x_id, subset_state=self.subset_state))
Expand All @@ -76,6 +76,7 @@ def test_indexed(self):
manual.add_component(self.data[self.x_id][:, 2, :, 4, :], label=self.x_id)
manual.add_component(self.data[self.y_id][:, 2, :, 4, :], label=self.y_id)


assert derived.label == 'Test data[:,2,:,4,:]'
assert derived.shape == manual.shape
assert [str(c) for c in derived.main_components] == [str(c) for c in manual.main_components]
Expand All @@ -96,8 +97,10 @@ def test_indexed(self):
assert_equal(derived.compute_statistic('mean', self.x_id),
manual.compute_statistic('mean', self.x_id))

assert_equal(derived.compute_statistic('mean', self.x_id, axis=2),
manual.compute_statistic('mean', self.x_id, axis=2))
dir_bin_dims = [derived.pixel_component_ids[0], derived.pixel_component_ids[2]]
man_bin_dims = [manual.pixel_component_ids[0], manual.pixel_component_ids[2]]
assert_equal(derived.compute_statistic('mean', self.x_id, bin_by=dir_bin_dims),
manual.compute_statistic('mean', self.x_id, bin_by=man_bin_dims))

assert_equal(derived.compute_statistic('mean', self.x_id, subset_state=self.subset_state),
manual.compute_statistic('mean', self.x_id, subset_state=self.subset_state))
Expand Down
2 changes: 1 addition & 1 deletion glue/viewers/histogram/qt/tests/test_data_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_basic(self):
self.viewer.add_data(self.data)

assert combo_as_string(self.viewer.options_widget().ui.combosel_x_att) == 'Main components:x:y:Coordinate components:Pixel Axis 0 [x]'

print(viewer_state)
assert viewer_state.x_att is self.data.id['x']
assert viewer_state.x_min == -1.1
assert viewer_state.x_max == 3.4
Expand Down
7 changes: 4 additions & 3 deletions glue/viewers/histogram/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,10 @@ def update_histogram(self):

range = sorted((self.viewer_state.hist_x_min, self.viewer_state.hist_x_max))

hist_values = data.compute_histogram([self._viewer_state.x_att],
range=[range],
bins=[self._viewer_state.hist_n_bin],
hist_values = data.compute_statistic(statistic='count',
bin_by=[self._viewer_state.x_att],
limits=[range],
shape=[self._viewer_state.hist_n_bin],
log=[self._viewer_state.x_log],
subset_state=subset_state)

Expand Down
6 changes: 1 addition & 5 deletions glue/viewers/profile/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,6 @@ def update_profile(self, update_limits=True):
if pix_cid is None:
raise IncompatibleDataException()

# If we get here, then x_att does correspond to a single pixel axis in
# the cube, so we now prepare a list of axes to collapse over.
axes = tuple(i for i in range(self.layer.ndim) if i != pix_cid.axis)

# We now get the y values for the data

# TODO: in future we should optimize the case where the mask is much
Expand All @@ -277,7 +273,7 @@ def update_profile(self, update_limits=True):
data = self.layer
subset_state = None

profile_values = data.compute_statistic(self.viewer_state.function, self.attribute, axis=axes, subset_state=subset_state)
profile_values = data.compute_statistic(self.viewer_state.function, self.attribute, bin_by=pix_cid, subset_state=subset_state)

if np.all(np.isnan(profile_values)):
self._profile_cache = [], []
Expand Down
25 changes: 12 additions & 13 deletions glue/viewers/scatter/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,21 +416,20 @@ def compute_density_map(self, bins=None, range=None):
else:
data = self.layer
subset_state = None

count = data.compute_histogram([self.viewer_state.y_att, self.viewer_state.x_att],
subset_state=subset_state, bins=bins,
log=(self.viewer_state.y_log, self.viewer_state.x_log),
range=range)

if self.cmap_mode == 'Fixed':
return count
return data.compute_statistic(statistic='count',
bin_by=[self.viewer_state.y_att, self.viewer_state.x_att],
subset_state=subset_state,
shape=bins,
log=(self.viewer_state.y_log, self.viewer_state.x_log),
limits=range)
else:
total = data.compute_histogram([self.viewer_state.y_att, self.viewer_state.x_att],
subset_state=subset_state, bins=bins,
weights=self.cmap_att,
log=(self.viewer_state.y_log, self.viewer_state.x_log),
range=range)
return total / count
return data.compute_statistic(statistic='mean',
cid=self.cmap_att,
bin_by=[self.viewer_state.y_att, self.viewer_state.x_att],
subset_state=subset_state, shape=bins,
log=(self.viewer_state.y_log, self.viewer_state.x_log),
limits=range)

@classmethod
def __setgluestate__(cls, rec, context):
Expand Down