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

Distance File: Don't accept dropped .xlsx files #6601

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion Orange/widgets/unsupervised/owdistancefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ def open_file(self):
err = str(exc)
self.Error.invalid_file(" \n"[len(err) > 40] + err)
else:
# If you add any other checks before accepting the file,
# you should probably mirror them in canDropFile
if distances.shape[0] != distances.shape[1]:
self.Error.non_square_matrix()
else:
Expand Down Expand Up @@ -158,7 +160,14 @@ def parametersFromFile(self, path):
return {"recent_paths": stored_recent_paths_prepend(self.WIDGET, r)}

def canDropFile(self, path: str) -> bool:
return os.path.splitext(path)[1].lower() in (".dst", ".xlsx")
if os.path.splitext(path)[0] == ".dst":
return True
try:
Copy link
Contributor

Choose a reason for hiding this comment

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

You could keep the old condition (only .dst) for better performance.

distances = DistMatrix.from_file(path)
Copy link
Contributor

Choose a reason for hiding this comment

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

This may take too long for big files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What was the file dimension again? Can you tell me which file it was - or send it? I tried 600x1000 and takes a few seconds.

What I discovered was that, unfortunately, most time is spent in opening the Excel file. Hence, the File widget will take more or less the same time as well.

Of course, with this PR, the file is read twice and we must prevent this. I'm considering decorating functions that open excel files with a lru cache of size 1 that would remember the last read excel workbook - of course checking not only the file name but also the timestamp. In this way - and given that opening the file takes majority of time - the time spent in owdistancefile would be saved in owfile because it would reuse the workbook object.

Copy link
Contributor

Choose a reason for hiding this comment

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

It was 1000x1000 .xlsx.

except Exception: # pylint: disable=broad-except
return False
else:
return distances.shape[0] == distances.shape[1]
janezd marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__": # pragma: no cover
Expand Down
8 changes: 6 additions & 2 deletions Orange/widgets/unsupervised/tests/test_owdistancefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ def test_nan_to_num(self):

class TestOWDistanceFileDropHandler(unittest.TestCase):
def test_canDropFile(self):
def ren(name):
return os.path.join(os.path.split(Orange.tests.__file__)[0], name)

handler = OWDistanceFileDropHandler()
self.assertTrue(handler.canDropFile("test.dst"))
self.assertTrue(handler.canDropFile("test.xlsx"))
self.assertTrue(handler.canDropFile(ren("xlsx_files/distances_with_nans.xlsx")))
self.assertFalse(handler.canDropFile(ren("xlsx_files/distances_nonsquare.xlsx")))
self.assertFalse(handler.canDropFile(ren("datasets/lenses.tab")))
self.assertFalse(handler.canDropFile("test.bin"))

def test_parametersFromFile(self):
Expand Down
Loading