Re-extract particles after losing correspondence to original micrographs

Hi @stephan

We had to repeat the motion correction due to the wrong gain application in the original motion corrections job. We would like to extract our already processed particles from the new motion-corrected and CTF estimated micrographs.

I have followed your instructions here and I get this error

Traceback (most recent call last):
  File "a7cs_reassign_mics.py", line 23, in <module>
    new_mic_uid = corresponding_micrograph.data['uid'][0]
IndexError: index 0 is out of bounds for axis 0 with size 0

Would you please help me understand where is the problem? Thank you. Here is my code.

#bash commands

eval $(cryosparcm env)
export PYTHONPATH="${CRYOSPARC_ROOT_DIR}"
python reassign_micrographs_uid.py

#python code for reassign_micrographs_uid.py

import numpy as n
# https://discuss.cryosparc.com/t/re-extract-particles-after-losing-correspondence-to-original-micrographs/5248/2
# dataset is the main module required to interact with cryoSPARC .cs files
from cryosparc_compute import dataset

particle_dset = dataset.Dataset()
micrograph_dset = dataset.Dataset()

dataset_path = '/path/to/P37_J253_particles_exported.cs'
particle_dset.from_file(dataset_path)

micrographs_path = '/path/to/P37_J350_passthrough_exposures.cs'
micrograph_dset.from_file(micrographs_path)

output_path = '/path/to/test/P37_J253_particles_micidchanged.cs'

for index, particle in enumerate(particle_dset.get_items()):
    # get the micrograph path associated with the particle
    path_to_match = particle['location/micrograph_path']
    # get the corresponding micrograph using this path
    corresponding_micrograph = micrograph_dset.subset_query(lambda x: x['micrograph_blob/path'] == path_to_match)
    # store this micrograph's uid
    new_mic_uid = corresponding_micrograph.data['uid'][0]
    # assign the uid to the particle dataset
    particle_dset.data['micrograph_uid'][index] = new_mic_uid

particle_dataset.to_file(output_path)