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

Refactor merge_samples() to safely cleanup in case of errors #2135

Merged
merged 4 commits into from
Oct 20, 2022
Merged
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
134 changes: 68 additions & 66 deletions fiftyone/core/clips.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,46 +647,47 @@ def _write_trajectories(dataset, src_collection, field, other_fields=None):
_allow_missing=True,
)

src_collection = fod._always_select_field(src_collection, _tmp_field)

id_field = "_id" if not src_dataset._is_clips else "_sample_id"

project = {
"_id": False,
"_sample_id": "$" + id_field,
_tmp_field: True,
"_media_type": True,
"filepath": True,
"metadata": True,
"tags": True,
field: True,
}

if other_fields:
project.update({f: True for f in other_fields})

src_collection._aggregate(
post_pipeline=[
{"$project": project},
{"$unwind": "$" + _tmp_field},
{
"$set": {
"support": {"$slice": ["$" + _tmp_field, 2, 2]},
field: {
"_cls": "Label",
"label": {"$arrayElemAt": ["$" + _tmp_field, 0]},
"index": {"$arrayElemAt": ["$" + _tmp_field, 1]},
try:
src_collection = fod._always_select_field(src_collection, _tmp_field)

id_field = "_id" if not src_dataset._is_clips else "_sample_id"

project = {
"_id": False,
"_sample_id": "$" + id_field,
_tmp_field: True,
"_media_type": True,
"filepath": True,
"metadata": True,
"tags": True,
field: True,
}

if other_fields:
project.update({f: True for f in other_fields})

src_collection._aggregate(
post_pipeline=[
{"$project": project},
{"$unwind": "$" + _tmp_field},
{
"$set": {
"support": {"$slice": ["$" + _tmp_field, 2, 2]},
field: {
"_cls": "Label",
"label": {"$arrayElemAt": ["$" + _tmp_field, 0]},
"index": {"$arrayElemAt": ["$" + _tmp_field, 1]},
},
"_rand": {"$rand": {}},
},
"_rand": {"$rand": {}},
},
},
{"$unset": _tmp_field},
{"$out": dataset._sample_collection_name},
]
)

cleanup_op = {"$unset": {_tmp_field: ""}}
src_dataset._sample_collection.update_many({}, cleanup_op)
{"$unset": _tmp_field},
{"$out": dataset._sample_collection_name},
]
)
finally:
cleanup_op = {"$unset": {_tmp_field: ""}}
src_dataset._sample_collection.update_many({}, cleanup_op)


def _write_expr_clips(
Expand Down Expand Up @@ -725,34 +726,35 @@ def _write_manual_clips(dataset, src_collection, clips, other_fields=None):
_allow_missing=True,
)

src_collection = fod._always_select_field(src_collection, _tmp_field)

id_field = "_id" if not src_dataset._is_clips else "_sample_id"

project = {
"_id": False,
"_sample_id": "$" + id_field,
"_media_type": True,
"filepath": True,
"support": "$" + _tmp_field,
"metadata": True,
"tags": True,
}

if other_fields:
project.update({f: True for f in other_fields})

src_collection._aggregate(
post_pipeline=[
{"$project": project},
{"$unwind": "$support"},
{"$set": {"_rand": {"$rand": {}}}},
{"$out": dataset._sample_collection_name},
]
)

cleanup_op = {"$unset": {_tmp_field: ""}}
src_dataset._sample_collection.update_many({}, cleanup_op)
try:
src_collection = fod._always_select_field(src_collection, _tmp_field)

id_field = "_id" if not src_dataset._is_clips else "_sample_id"

project = {
"_id": False,
"_sample_id": "$" + id_field,
"_media_type": True,
"filepath": True,
"support": "$" + _tmp_field,
"metadata": True,
"tags": True,
}

if other_fields:
project.update({f: True for f in other_fields})

src_collection._aggregate(
post_pipeline=[
{"$project": project},
{"$unwind": "$support"},
{"$set": {"_rand": {"$rand": {}}}},
{"$out": dataset._sample_collection_name},
]
)
finally:
cleanup_op = {"$unset": {_tmp_field: ""}}
src_dataset._sample_collection.update_many({}, cleanup_op)


def _get_trajectories(sample_collection, frame_field):
Expand Down
Loading