def save_metadata(collection, metadata):
    image_obj_list = []
    image_obj_errors = defaultdict(list)

    metadata_list = convert_to_list(metadata)
    metadata_dict = list_to_dict(metadata_list,
                                 key=lambda x: x['Filename'])

    image_obj_list = collection.basecollectionitem_set.instance_of(Image).all()
    image_obj_dict = list_to_dict(image_obj_list,
                                  key=file_basename)

    pairs = pair_data_and_objects(metadata_dict,
                                  image_obj_dict)
    for data, image_obj in pairs:
        try:
            set_object_data(image_obj, data)
            image_obj.full_clean()
        except __HOLE__ as e:
            image_obj_errors[file_basename(image_obj)].append(
                prepare_messages(image_obj, e.message_dict))

    if image_obj_errors:
        raise MetadataGridValidationError(image_obj_errors)

    for image_obj in image_obj_list:
        image_obj.is_valid = True
        image_obj.save()

    return metadata_list
dataset/ETHPy150Open NeuroVault/NeuroVault/neurovault/apps/statmaps/image_metadata.py/save_metadata