I have extracted the basic area where the problem happens.
This works just fine with PNG format:
# create gradient
blackpt=15934
whitept=49344
convert -size 20x256 gradient: -rotate 90 grad256x20a.png

# threshold a range of values
convert grad256x20a.png \
\( -clone 0 -white-threshold $blackpt \
-channel rgba -alpha on -fill none -opaque white \) \
\( -clone 0 -black-threshold $whitept \
-channel rgba -alpha on -fill none -opaque black \) \
-delete 0 -compose over -composite \
-fill white +opaque none \
-fill black -opaque none \
-alpha off \
grad256x20_b15934_w49344a.png

But when I use MPC images (and then convert to PNG so I can display them here), I get spotty thresholding that I don't understand. Perhaps Magick can shed some light on this.
# create gradient
blackpt=15934
whitept=49344
convert -size 20x256 gradient: -rotate 90 grad256x20.mpc
convert grad256x20.mpc grad256x20b.png

# threshold a range of values
convert grad256x20.mpc \
\( -clone 0 -white-threshold $blackpt \
-channel rgba -alpha on -fill none -opaque white \) \
\( -clone 0 -black-threshold $whitept \
-channel rgba -alpha on -fill none -opaque black \) \
-delete 0 -compose over -composite \
-fill white +opaque none \
-fill black -opaque none \
-alpha off \
grad256x20_b15934_w49344.mpc
convert grad256x20_b15934_w49344.mpc grad256x20_b15934_w49344b.png

Note this spotty issue is visible in the MPC file and not an issue of converting to PNG.
Thanks