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][ENH] Neighbors: data info displayed in status bar #4141

Closed
wants to merge 4 commits into from
Closed
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
54 changes: 39 additions & 15 deletions Orange/widgets/data/owneighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ def __init__(self):
self.reference = None
self.distances = None

box = gui.vBox(self.controlArea, "Info")
self.data_info_label = gui.widgetLabel(box, "")
self.reference_info_label = gui.widgetLabel(box, "")
self._set_label_text("data")
self._set_label_text("reference")

box = gui.vBox(self.controlArea, box=True)
gui.comboBox(
box, self, "distance_index", orientation=Qt.Horizontal,
Expand All @@ -88,24 +82,53 @@ def __init__(self):

self.apply_button = gui.auto_apply(self.controlArea, self, commit=self.apply)

def _set_label_text(self, name):
data = getattr(self, name)
label = getattr(self, f"{name}_info_label")
if data is None:
label.setText(f"No {name} instances")
def _set_input_summary(self):
n_data, n_refs = 0, 0
if self.data is not None and self.reference is not None:
n_data = len(self.data)
n_refs = len(self.reference)
elif self.data is not None:
n_data = len(self.data)
elif self.reference is not None:
n_refs = len(self.reference)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can shorten this by:

n_data = len(self.data) if self.data else 0
n_refs = len(self.reference) if self.reference else 0

and move the following else statement into the second if

else:
pl = "s" if data else ""
label.setText(f"{len(data)} {name} instance{pl} on input.")
self.info.set_input_summary(self.info.NoInput)
return

inst = f"{n_data} | {n_refs} "
if n_data > 0 and n_refs > 0:
details = f"{n_data} data instances on input; " \
f"{n_refs} reference instances on input "
elif n_data > 0 and n_refs == 0:
details = f"{n_data} data instances on input; " \
f"No reference instances on input "
elif n_data == 0 and n_refs > 0:
details = f"No data instances on input; " \
f"{n_refs} reference instances on input "

self.info.set_input_summary(inst, details)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A shorter version:

        if n_data or n_refs:
            details = \
                f"{n_data if n_data else 'No'} data instance(s) on input\n" \
                f"{n_refs if n_refs else 'No'} reference instance(s) on input "
            self.info.set_input_summary(f"{n_data} | {n_refs} ", details)
        else:
            self.info.set_input_summary(self.info.NoInput)


def _set_output_summary(self):
if self.data is None:
self.Outputs.data.send(None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outputting the data is already handled in apply(). Besides, it should not be sent twice, if not necessary.

self.info.set_output_summary(self.info.NoOutput)
return

indices = self._compute_indices()
if np.any(indices):
neighbors = self._data_with_similarity(indices)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calculating the neighbors just for sake of the info is wasteful, and unnecessary.
The "info" code could be placed in an apply function, but if you insist to have a separate function for that, just pass it the neighbors calculated in the apply().

self.Outputs.data.send(neighbors)
self.info.set_output_summary(str(len(neighbors)))

@Inputs.data
def set_data(self, data):
self.data = data
self._set_label_text("data")
self._set_input_summary()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can place this into handleNewSignals() instead. handleNewSignals() is emitted on any input change.


@Inputs.reference
def set_ref(self, refs):
self.reference = refs
self._set_label_text("reference")
self._set_input_summary()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.


def handleNewSignals(self):
self.compute_distances()
Expand Down Expand Up @@ -146,6 +169,7 @@ def apply(self):
else:
neighbors = self._data_with_similarity(indices)
self.Outputs.data.send(neighbors)
self._set_output_summary()

def _compute_indices(self):
self.Warning.all_data_as_reference.clear()
Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/data/owpreprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ def __init__(self, parent=None, **kwargs):
fixedrb = QRadioButton("Fixed", checked=True)
group.addButton(fixedrb, RandomFeatureSelectEditor.Fixed)
kspin = QSpinBox(
minimum=1, value=self.__k,
minimum=1, maximum=10000000, value=self.__k,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should be removed from this PR.

enabled=self.__strategy == RandomFeatureSelectEditor.Fixed
)
kspin.valueChanged[int].connect(self.setK)
Expand Down