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

Add option to export to CSV via sleap-convert and API #1730

Merged
merged 15 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
23 changes: 22 additions & 1 deletion sleap/io/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def create_parser():
help="Output format. Default ('slp') is SLEAP dataset; "
"'analysis' results in analysis.h5 file; "
"'analysis.nix' results in an analysis nix file;"
"'analysis.csv' results in an analysis csv file;"
"'h5' or 'json' results in SLEAP dataset "
"with specified file format.",
)
Expand Down Expand Up @@ -105,6 +106,7 @@ def main(args: list = None):
"""
parser = create_parser()
args = parser.parse_args(args=args)
print(args)
Copy link

Choose a reason for hiding this comment

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

Remove the debug print statement to maintain a clean and professional codebase.

-    print(args)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
print(args)


video_callback = Labels.make_video_callback([os.path.dirname(args.input_path)])
try:
Expand Down Expand Up @@ -135,7 +137,12 @@ def main(args: list = None):
outnames = [path for path in args.outputs]
if len(outnames) < len(vids):
# if there are less outnames provided than videos to convert...
out_suffix = "nix" if "nix" in args.format else "h5"
if "nix" in args.format:
out_suffix = "nix"
elif "csv" in args.format:
out_suffix = "csv"
else:
out_suffix = "h5"
Comment on lines +139 to +144
Copy link

Choose a reason for hiding this comment

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

Refactor the logic for determining the output file suffix into a separate function to improve code readability and maintainability.

+ def determine_output_suffix(format_arg: str) -> str:
+     if "nix" in format_arg:
+         return "nix"
+     elif "csv" in format_arg:
+         return "csv"
+     else:
+         return "h5"
- if "nix" in args.format:
-     out_suffix = "nix"
- elif "csv" in args.format:
-     out_suffix = "csv"
- else:
-     out_suffix = "h5"
+ out_suffix = determine_output_suffix(args.format)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if "nix" in args.format:
out_suffix = "nix"
elif "csv" in args.format:
out_suffix = "csv"
else:
out_suffix = "h5"
def determine_output_suffix(format_arg: str) -> str:
if "nix" in format_arg:
return "nix"
elif "csv" in format_arg:
return "csv"
else:
return "h5"
out_suffix = determine_output_suffix(args.format)

fn = args.input_path
fn = re.sub("(\.json(\.zip)?|\.h5|\.slp)$", "", fn)
fn = PurePath(fn)
Expand All @@ -158,6 +165,20 @@ def main(args: list = None):
NixAdaptor.write(outname, labels, args.input_path, video)
except ValueError as e:
print(e.args[0])

elif "csv" in args.format:
from sleap.info.write_tracking_h5 import main as write_analysis

for video, output_path in zip(vids, outnames):
write_analysis(
labels,
output_path=output_path,
labels_path=args.input_path,
all_frames=True,
video=video,
csv=True,
)

else:
from sleap.info.write_tracking_h5 import main as write_analysis

Expand Down
15 changes: 15 additions & 0 deletions sleap/io/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,21 @@ def export(self, filename: str):

SleapAnalysisAdaptor.write(filename, self)


def export_csv(self, filename: str):
"""Export labels to CSV format.

Args:
filename: Output path for the CSV format file.

Notes:
This will write the contents of the labels out as a CSV file.
"""
from sleap.io.format.csv import CSVAdaptor

CSVAdaptor.write(filename, self)
eberrigan marked this conversation as resolved.
Show resolved Hide resolved


def export_nwb(
self,
filename: str,
Expand Down
Loading