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] OWSom: Fix crash for data without class variable #6763

Merged
merged 4 commits into from
Mar 22, 2024

Conversation

Anamari-ctrl
Copy link
Contributor

@Anamari-ctrl Anamari-ctrl commented Mar 19, 2024

Issue

Fixes #6750

Description of changes

Fixed TypeError: 'DiscreteVariable' object is not iterable.

Includes
  • Code changes
  • Tests
  • Documentation

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Contributor

@janezd janezd left a comment

Choose a reason for hiding this comment

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

Also, rename the PR to "[FIX] OWSom: Fix crash for data without class variable".

In description, replace Bugs in SOM #6750 with Fixes #6750, which will automatically close #6750 when PR is merged.

@@ -1018,7 +1018,8 @@ def make_domain(values, default_grp, offset):
compute_value=GetGroups(id_to_group, default_grp, offset))

if not self.data.domain.class_vars:
class_vars, metas = grp_var, som_attrs
self.data.domain.class_vars = ()
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove the above. We consider Domain immutable, so class_vars must not be changed, neither is there any need to change them because that already equal ().

def test_make_domain_without_class_vars(self):
widget = self.widget
self.send_signal(self.widget.Inputs.data, self.iris)
widget.data.domain.class_vars = None
Copy link
Contributor

Choose a reason for hiding this comment

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

You must not do this. widget.data is a data table, and widget.data.domain describes its columns. By removing classes, the table and its domain are inconsistent, and anything can happen. (Also. domain is internally inconsistent because when class_var is not None, class_vars equals (class_var, ).)

Instead, you must construct a new domain and convert the table.

        data = self.iris.transform(Domain(self.iris.domain.attributes))
        self.send_signal(self.widget.Inputs.data, data)

The first line creates a new data table without class, and the second line sends it to the widget. Then you can remove widget.update_output().

self.send_signal(self.widget.Inputs.data, self.iris)
widget.data.domain.class_vars = None
widget.update_output()
self.assertIsNotNone(self.get_output(widget.Outputs.annotated_data))
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't test much. You may want to test two things. First, if you want to test that the widget no longer crashes, this line is unnecessary -- it would crash in the previous line (prior to your fix). But a better test would be that the widget constructs a correct domain. The proper test is

    def test_make_domain_without_class_vars(self):
        widget = self.widget
        data = self.iris.transform(Domain(self.iris.domain.attributes))
        self.send_signal(self.widget.Inputs.data, data)

        domain = self.get_output((widget.Outputs.annotated_data)).domain
        self.assertEqual(domain.attributes, data.domain.attributes)
        self.assertEqual(domain.class_var.name, ANNOTATED_DATA_FEATURE_NAME)
        self.assertEqual([var.name for var in domain.metas],
                         ["som_cell", "som_row", "som_col", "som_error"])

@Anamari-ctrl Anamari-ctrl changed the title fix type error in owsom [FIX] OWSom: Fix crash for data without class variable Mar 21, 2024
@janezd
Copy link
Contributor

janezd commented Mar 21, 2024

You incorrectly copied the test that I suggested. Please fix the indent.

Copy link

codecov bot commented Mar 22, 2024

Codecov Report

Merging #6763 (2a3427d) into master (88fae8a) will decrease coverage by 0.01%.
Report is 12 commits behind head on master.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6763      +/-   ##
==========================================
- Coverage   88.15%   88.15%   -0.01%     
==========================================
  Files         323      323              
  Lines       70747    70748       +1     
==========================================
- Hits        62369    62368       -1     
- Misses       8378     8380       +2     

@janezd janezd merged commit 39bd0ec into biolab:master Mar 22, 2024
28 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bugs in SOM
3 participants