cindeem/nibabel — comment on commit c5192e8c0e89d5fbd90467b077b2b6009f44e812
File:  line 0
By matthew-brett:

How about:

ids = mlist[:, 0].copy()
n_valid = np.sum(ids > 0)
ids[ids <=0] = ids.max() + 1 # put invalid frames at end after sort
valid_order = np.argsort(ids)
for i in range(n_valid):
id_dict[i] = [valid_order[i], ids[valid_order[i]]]