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

OSError: [Errno 22] Invalid argument when loading an SLP file #106

Closed
aaprasad opened this issue Jul 26, 2024 · 3 comments · Fixed by #107
Closed

OSError: [Errno 22] Invalid argument when loading an SLP file #106

aaprasad opened this issue Jul 26, 2024 · 3 comments · Fixed by #107

Comments

@aaprasad
Copy link

aaprasad commented Jul 26, 2024

Currently when I try to load a .slp file with a bad video file name eg:

print(sio.load_slp(slp_file))

I get the following error:

OSError                                   Traceback (most recent call last)
Cell In[8], line 2
      1 for slp_file, vid_file in zip(slp_files, vid_files):
----> 2     print(sio.load_slp(slp_file))

File /opt/conda/envs/dreem/lib/python3.11/site-packages/sleap_io/io/main.py:19, in load_slp(filename)
     10 def load_slp(filename: str) -> Labels:
     11     """Load a SLEAP dataset.
     12 
     13     Args:
   (...)
     17         The dataset as a `Labels` object.
     18     """
---> 19     return slp.read_labels(filename)

File /opt/conda/envs/dreem/lib/python3.11/site-packages/sleap_io/io/slp.py:1012, in read_labels(labels_path)
   1003 """Read a SLEAP labels file.
   1004 
   1005 Args:
   (...)
   1009     The processed `Labels` object.
   1010 """
   1011 tracks = read_tracks(labels_path)
-> 1012 videos = read_videos(labels_path)
   1013 skeletons = read_skeletons(labels_path)
   1014 points = read_points(labels_path)

File /opt/conda/envs/dreem/lib/python3.11/site-packages/sleap_io/io/slp.py:130, in read_videos(labels_path)
    126 for video_ind, video_data in enumerate(
    127     read_hdf5_dataset(labels_path, "videos_json")
    128 ):
    129     video_json = json.loads(video_data)
--> 130     video = make_video(labels_path, video_json, video_ind=video_ind)
    131     videos.append(video)
    132 return videos

File /opt/conda/envs/dreem/lib/python3.11/site-packages/sleap_io/io/slp.py:72, in make_video(labels_path, video_json, video_ind)
     69 if not video_path.exists():
     70     # Check for the same filename in the same directory as the labels file.
     71     video_path_ = Path(labels_path).parent / video_path.name
---> 72     if video_path_.exists():
     73         video_path = video_path_
     74     else:
     75         # TODO (TP): Expand capabilities of path resolution to support more
     76         # complex path finding strategies.

File /opt/conda/envs/dreem/lib/python3.11/pathlib.py:1235, in Path.exists(self)
   1231 """
   1232 Whether this path exists.
   1233 """
   1234 try:
-> 1235     self.stat()
   1236 except OSError as e:
   1237     if not _ignore_error(e):

File /opt/conda/envs/dreem/lib/python3.11/pathlib.py:1013, in Path.stat(self, follow_symlinks)
   1008 def stat(self, *, follow_symlinks=True):
   1009     """
   1010     Return the result of the stat() system call on this path, like
   1011     os.stat() does.
   1012     """
-> 1013     return os.stat(self, follow_symlinks=follow_symlinks)

OSError: [Errno 22] Invalid argument: 'train/top\\top-10072022134804-0000_h265_CRF12_denoised.mp4'

I know theres a Labels.replace_filename but you need to be able to load the file first. Therefore, it would be nice if either A. you can pass in a videos argument to override the current video OR if it can't open it just ignore and warn the user so you can at least access the actual labels

PS:
sleap.load_file(slp_file)
works fine so I'm not sure whats different at an implementation level

@talmo
Copy link
Contributor

talmo commented Jul 29, 2024

Here are the relevant offending lines:

backend_metadata = video_json["backend"]
video_path = backend_metadata["filename"]
# Marker for embedded videos.
source_video = None
is_embedded = False
if video_path == ".":
video_path = labels_path
is_embedded = True
# Basic path resolution.
video_path = Path(video_path)
if not video_path.exists():
# Check for the same filename in the same directory as the labels file.
video_path_ = Path(labels_path).parent / video_path.name
if video_path_.exists():
video_path = video_path_
else:
# TODO (TP): Expand capabilities of path resolution to support more
# complex path finding strategies.
pass
# Convert video path to string.
video_path = video_path.as_posix()

From the original issue, the path stored in the SLP file is:

G:/.shortcut-targets-by-id/1ABZ_lZYLzT3qOjXEaUZSd9-JFyvk5lSl/2021-10-27_Talmo/Scratch/Proofreading/2022-10-07/10072022134804/top\\top-10072022134804-0000_h265_CRF12_denoised.mp4

There's clearly both forward slashes and (escaped) backslashes, but pathlib should handle it.

Follow up investigation: cannot reproduce on Colab.

Not only does the SLP file load, but I can't even get the same error when running the actual erroring line:

import os
os.stat("train/top\\top-10072022134804-0000_h265_CRF12_denoised.mp4")

Output:

FileNotFoundError                         Traceback (most recent call last)
[<ipython-input-18-45758ebc4f0a>](https://localhost:8080/#) in <cell line: 3>()
      1 import os
      2 
----> 3 os.stat("train/top\\top-10072022134804-0000_h265_CRF12_denoised.mp4")

FileNotFoundError: [Errno 2] No such file or directory: 'train/top\\top-10072022134804-0000_h265_CRF12_denoised.mp4'

Note that it's FileNotFoundError: [Errno 2], and not OSError: [Errno 22] Invalid argument.

This tells me that it's maybe a file system issue?

Either way, keeping this open as a reminder that we should probably expose the logic in Labels.replace_filenames to load_slp so we can do filename replacement at load time using the same signature as Labels.replace_filenames.

@talmo
Copy link
Contributor

talmo commented Jul 29, 2024

Relevant pathlib sections:

But it does seem like errno 22 means invalid argument.

Maybe on some platforms we need to sanitize the filename manually?

@talmo talmo changed the title Replace video file name while loading slp file OSError: [Errno 22] Invalid argument when loading an SLP file Aug 2, 2024
@talmo
Copy link
Contributor

talmo commented Aug 2, 2024

Fixed in sleap-io v0.1.7.

@talmo talmo closed this as completed Aug 2, 2024
@gitttt-1234 gitttt-1234 linked a pull request Aug 7, 2024 that will close this issue
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 a pull request may close this issue.

2 participants