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

fix: bugfixes and debug hooks #106

Merged
merged 1 commit into from
Oct 29, 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
3 changes: 2 additions & 1 deletion src/data_hub/axes_callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const x_data = df[x_select.value];
const y_data = df[y_select.value];
const size_data = df[size_select.value];
const id_data = df[ID.value];
sg-s marked this conversation as resolved.
Show resolved Hide resolved

// Normalize size data for better visualization (e.g., map values to a range)
const min_size = Math.min(...size_data);
Expand All @@ -16,7 +17,7 @@ const sizes = size_data.map(value => {
});

// Update the data source
scatter_source.data = { 'x': x_data, 'y': y_data, 'size': sizes };
scatter_source.data = { 'x': x_data, 'y': y_data, 'size': sizes, 'id': id_data };
sg-s marked this conversation as resolved.
Show resolved Hide resolved

// Update the axis labels
x_axis.axis_label = x_select.value;
Expand Down
10 changes: 0 additions & 10 deletions src/data_hub/button_callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ console.log("Will write to this column:", labelColumn);
const rowIds = selectedData.ids;

if (typeof window.deeporigin !== "undefined") {
// Reset selections for all the rows in question
const resetChanges = rowIds.map(rowId => ({
rowId: rowId,
fieldChangeEvents: [{
columnId: labelColumn,
newValue: { selectedOptions: [] }
}]
}));

deeporigin.dataHub.primaryDatabase.editRows({ changes: resetChanges });
sg-s marked this conversation as resolved.
Show resolved Hide resolved

// Write the new value
const updateChanges = rowIds.map(rowId => ({
Expand Down
40 changes: 21 additions & 19 deletions src/data_hub/hover_callback.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
// callback when a user hovers over a point in the scatter plot
// Callback when a user hovers over a point in the scatter plot
if (cb_data.index && cb_data.index.indices && cb_data.index.indices.length > 0) {
// User is actually hovering over a point.
// Bokeh allows hovering over multiple points; we select the first.
const chosenIndex = cb_data.index.indices[0];

if (cb_data.index.indices.length > 0) {
// user is actually hovering over a point.
// bokeh has an annoying "feature" where you can hover
// over more than 1 point, which we don't want.
// we're going to arbitarily choose the first point
var chosen_index = (cb_data.index.indices[0]);

// update the red circle marker on all linked plots
// to indicate the currently hovered point
// Update the red circle marker on all linked plots to indicate the hovered point
for (const key in marker_source.data) {
marker_source.data[key][0] = scatter_source.data[key][chosen_index];
}
if (scatter_source.data[key]) {
marker_source.data[key][0] = scatter_source.data[key][chosenIndex];
}
}

marker_source.change.emit();

// callback to update selection in database
var id = scatter_source.data['id'][chosen_index];
if (typeof window.deeporigin !== "undefined") {
deeporigin.dataHub.primaryDatabase.clearRangeSelection();
deeporigin.dataHub.primaryDatabase.addSelection({ selections: [{ rowId: id }] });

// Callback to update selection in the database
const id = scatter_source.data['id'] ? scatter_source.data['id'][chosenIndex] : null;
if (id && typeof window.deeporigin !== "undefined") {
try {
deeporigin.dataHub.primaryDatabase.clearRangeSelection();
deeporigin.dataHub.primaryDatabase.addSelection({ selections: [{ rowId: id }] });
} catch (error) {
console.error("Error updating selection in database:", error);
}
}

};
console.log("Hovered point ID:", id);
}
9 changes: 7 additions & 2 deletions src/data_hub/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def scatter(
x: Optional[str] = None,
y: Optional[str] = None,
size: Optional[str] = None,
hover_callback_code: Optional[str] = None,
):
"""function to make a scatter plot from a Deep Origin dataframe, with support for interactivity

Expand All @@ -31,6 +32,7 @@ def scatter(
x (Optional[str], optional): name of column to use for x axis. Defaults to None.
y (Optional[str], optional): name of column to use for y axis. Defaults to None.
size (Optional[str], optional): name of column to use for size. Defaults to None.
hover_callback_code: Optional[str], optional): JavaScript callback to use for hover tool. Defaults to None.

`df` should be a Deep Origin dataframe with at least two numeric columns.

Expand Down Expand Up @@ -193,7 +195,7 @@ def scatter(
x_select=x_select,
y_select=y_select,
size_select=size_select,
df=df.to_dict("list"),
df=df.reset_index().to_dict("list"),
sg-s marked this conversation as resolved.
Show resolved Hide resolved
x_axis=p.xaxis[0],
y_axis=p.yaxis[0],
),
Expand All @@ -204,8 +206,11 @@ def scatter(
# this updates the value of the slider to the currently
# hovered point

if hover_callback_code is None:
hover_callback_code = js_code["hover_callback"]

hover_callback = CustomJS(
code=js_code["hover_callback"],
code=hover_callback_code,
sg-s marked this conversation as resolved.
Show resolved Hide resolved
args=dict(
marker_source=marker_source,
scatter_source=scatter_source,
Expand Down