Heudiconv: Wrong labelling of mprage and fmaps

Dear all,
we are running heudiconv on docker in order to convert and sort our data according to BIDS. In our study we have three runs with an MPRAGE-sequence each. During the first run we record a fieldmap intended for the EPIs, during the third we record two further fieldmaps, one intended for the EPIs and one intended for perfusion imaging.
However, both the output of the MPRAGE sequences as well as the output of the fieldmaps is not correct: There are four anatomical images selected instead of three, one being a localizer. Furthermore, there are four *phasediff.nii images as well as a non-corresponding number of magnitude images (see below).

Here you can see the heuristic file:

import os

def create_key(template, outtype=(‘nii.gz’,‘dicom’), annotation_classes=None):
if template is None or not template:
raise ValueError(‘Template must be a valid format string’)
return (template, outtype, annotation_classes)

def infotodict(seqinfo):
t1w = create_key(‘sub-{subject}/{session}/anat/sub-{subject}_{session}_run-{item:02d}_T1w’)

fmap_mag      = create_key('sub-{subject}/{session}/fmap/sub-{subject}_{session}_run-{item:02d}_magnitude')
fmap_phase    = create_key('sub-{subject}/{session}/fmap/sub-{subject}_{session}_run-{item:02d}_phasediff')

func_rest     = create_key('sub-{subject}/{session}/func/sub-{subject}_{session}_task-rest_run-{item:02d}_bold')

info = {t1w:[], fmap_mag:[], fmap_phase:[], func_rest:[], asl:[]}

last_run = len(seqinfo)

for idx, s in enumerate(seqinfo):

    if ('mprage' in s.protocol_name) and (s.dim1 == 512) and (s.dim2 == 592) and (s.is_derived == False):
        info[t1w].append(s.series_id)

    if ('field_mapping' in s.protocol_name) and ((s.dim3 == 148)):
        info[fmap_mag].append(s.series_id)
    if ('field_mapping' in s.protocol_name) and ((s.dim3 == 34) or (s.dim3 == 40) or (s.dim3 == 74)):
        info[fmap_phase].append(s.series_id)

    if ('ep2d_bold_moco_p2_rs' in s.protocol_name):
        info[func_rest].append(s.series_id)

    ….

return info

Here, you can see the output from the terminal listing the */anat and the */fmap directories:

(base) root@C3LVSY2:/mnt/d/bidsTest/sourcedata/Nifti# cd sub-001/
(base) root@C3LVSY2:/mnt/d/bidsTest/sourcedata/Nifti/sub-001# cd ses-001/
(base) root@C3LVSY2:/mnt/d/bidsTest/sourcedata/Nifti/sub-001/ses-001# ls
anat fmap func sub-001_ses-001_scans.json sub-001_ses-001_scans.tsv
(base) root@C3LVSY2:/mnt/d/bidsTest/sourcedata/Nifti/sub-001/ses-001# cd anat
(base) root@C3LVSY2:/mnt/d/bidsTest/sourcedata/Nifti/sub-001/ses-001/anat# ls
sub-001_ses-001_run-01_T1w.json sub-001_ses-001_run-02_T1w1.json sub-001_ses-001_run-02_T1w2.json sub-001_ses-001_run-02_T1w3.json
sub-001_ses-001_run-01_T1w.nii.gz sub-001_ses-001_run-02_T1w1.nii.gz sub-001_ses-001_run-02_T1w2.nii.gz sub-001_ses-001_run-02_T1w3.nii.gz
(base) root@C3LVSY2:/mnt/d/bidsTest/sourcedata/Nifti/sub-001/ses-001/anat# cd …
(base) root@C3LVSY2:/mnt/d/bidsTest/sourcedata/Nifti/sub-001/ses-001# cd fmap
(base) root@C3LVSY2:/mnt/d/bidsTest/sourcedata/Nifti/sub-001/ses-001/fmap# ls
sub-001_ses-001_run-01_magnitude1.json sub-001_ses-001_run-01_magnitude3.json sub-001_ses-001_run-01_phasediff1.json sub-001_ses-001_run-02_phasediff.json
sub-001_ses-001_run-01_magnitude1.nii.gz sub-001_ses-001_run-01_magnitude3.nii.gz sub-001_ses-001_run-01_phasediff1.nii.gz sub-001_ses-001_run-02_phasediff.nii.gz
sub-001_ses-001_run-01_magnitude2.json sub-001_ses-001_run-01_magnitude4.json sub-001_ses-001_run-01_phasediff2.json sub-001_ses-001_run-03_phasediff.json
sub-001_ses-001_run-01_magnitude2.nii.gz sub-001_ses-001_run-01_magnitude4.nii.gz sub-001_ses-001_run-01_phasediff2.nii.gz sub-001_ses-001_run-03_phasediff.nii.gz

Here is a selection of the dicominfo.tsv file. As you can see, a couple of entries are not correct (e.g. dimension 3 of the mprage are supposed to be 192):

Thank you very much for your help!
Best,
Christian