def fix_roi_values_freesurferLUT(roi_image, white_matter_file, csf_file,
                                 prob_thresh):
    from coma.helpers import wm_labels_only, csf_labels_only, prepare_for_uint8
    _, name, _ = split_filename(roi_image)

    white_matter_default = 2
    csf_default = 3

    # Get regions labelled white and csf
    wm_label_file = op.abspath(name + "_WM.nii.gz")
    wm_only = wm_labels_only(roi_image, out_filename=wm_label_file)
    wm_label_image = nb.load(wm_only)
    wm_labels = wm_label_image.get_data()

    csf_label_file = op.abspath(name + "_CSF.nii.gz")
    csf_only = csf_labels_only(roi_image, out_filename=csf_label_file)
    csf_label_image = nb.load(csf_only)
    csf_labels = csf_label_image.get_data()

    wm_image = nb.load(white_matter_file)
    wm_data = wm_image.get_data()
    csf_image = nb.load(csf_file)
    csf_data = csf_image.get_data()

    image = nb.load(roi_image)
    data = image.get_data()

    assert (data.shape == wm_data.shape == csf_data.shape)
    data[np.where(wm_labels > 0)] = white_matter_default
    data[np.where(csf_labels > 0)] = csf_default

    # Be careful with the brackets here. & takes priority over comparisons
    data[np.where((wm_data > prob_thresh)
                  & (data == 0))] = white_matter_default
    data[np.where((csf_data > prob_thresh) & (data == 0))] = csf_default

    wm_labels[np.where(data == white_matter_default)] = 1
    wm_labels[np.where(data == csf_default)] = 0
    wm_labels[np.where(data == 0)] = 0
    wm_labels = wm_labels.astype(np.uint8)

    new_wm_label_image = nb.Nifti1Image(dataobj=wm_labels,
                                        affine=wm_image.get_affine(),
                                        header=wm_image.get_header())
    new_wm_label_image.set_data_dtype(np.uint8)
    _, name, _ = split_filename(white_matter_file)
    wm_label_file = op.abspath(name + "_fixedWM.nii.gz")
    nb.save(new_wm_label_image, wm_label_file)

    csf_data[np.where(data != csf_default)] = 0
    csf_data[np.where(csf_labels > 0)] = 1

    csf_data = csf_data.astype(np.uint8)
    csf_label_image = nb.Nifti1Image(dataobj=csf_data,
                                     affine=csf_image.get_affine(),
                                     header=csf_image.get_header())
    csf_label_image.set_data_dtype(np.uint8)
    _, name, _ = split_filename(csf_file)
    csf_label_file = op.abspath(name + "_fixedCSF.nii.gz")
    nb.save(csf_label_image, csf_label_file)

    hdr = image.get_header()

    data_uint8, remap_dict = prepare_for_uint8(data, ignore=range(0, 3))
    data_uint8 = data_uint8.astype(np.uint8)
    data_uint8[np.where(data == csf_default)] = csf_default
    data_uint8[np.where(data == white_matter_default)] = white_matter_default

    fixed = nb.Nifti1Image(dataobj=data_uint8,
                           affine=image.get_affine(),
                           header=hdr)
    _, name, _ = split_filename(roi_image)
    fixed.set_data_dtype(np.uint8)
    fixed_roi_image = op.abspath(name + "_fixedROIs.nii.gz")
    nb.save(fixed, fixed_roi_image)
    return fixed_roi_image, wm_label_file, csf_label_file, remap_dict
def fix_roi_values_freesurferLUT(roi_image, white_matter_file, csf_file, prob_thresh):    
    from coma.helpers import wm_labels_only, csf_labels_only, prepare_for_uint8
    _, name, _ = split_filename(roi_image)

    white_matter_default = 2
    csf_default = 3

    # Get regions labelled white and csf
    wm_label_file = op.abspath(name + "_WM.nii.gz")
    wm_only = wm_labels_only(roi_image, out_filename=wm_label_file)
    wm_label_image = nb.load(wm_only)
    wm_labels = wm_label_image.get_data()

    csf_label_file = op.abspath(name + "_CSF.nii.gz")
    csf_only = csf_labels_only(roi_image, out_filename=csf_label_file)
    csf_label_image = nb.load(csf_only)
    csf_labels = csf_label_image.get_data()

    wm_image = nb.load(white_matter_file)
    wm_data = wm_image.get_data()
    csf_image = nb.load(csf_file)
    csf_data = csf_image.get_data()

    image = nb.load(roi_image)
    data = image.get_data()

    assert(data.shape == wm_data.shape == csf_data.shape)
    data[np.where(wm_labels > 0)] = white_matter_default
    data[np.where(csf_labels > 0)] = csf_default

    # Be careful with the brackets here. & takes priority over comparisons
    data[np.where((wm_data > prob_thresh) & (data == 0))] = white_matter_default
    data[np.where((csf_data > prob_thresh) & (data == 0))] = csf_default

    wm_labels[np.where(data == white_matter_default)] = 1
    wm_labels[np.where(data == csf_default)] = 0
    wm_labels[np.where(data == 0)] = 0
    wm_labels = wm_labels.astype(np.uint8)

    new_wm_label_image = nb.Nifti1Image(
        dataobj=wm_labels, affine=wm_image.get_affine(), header=wm_image.get_header())
    new_wm_label_image.set_data_dtype(np.uint8)
    _, name, _ = split_filename(white_matter_file)
    wm_label_file = op.abspath(name + "_fixedWM.nii.gz")
    nb.save(new_wm_label_image, wm_label_file)

    csf_data[np.where(data != csf_default)] = 0
    csf_data[np.where(csf_labels > 0)] = 1

    csf_data = csf_data.astype(np.uint8)
    csf_label_image = nb.Nifti1Image(
        dataobj=csf_data, affine=csf_image.get_affine(), header=csf_image.get_header())
    csf_label_image.set_data_dtype(np.uint8)
    _, name, _ = split_filename(csf_file)
    csf_label_file = op.abspath(name + "_fixedCSF.nii.gz")
    nb.save(csf_label_image, csf_label_file)

    hdr = image.get_header()

    data_uint8, remap_dict = prepare_for_uint8(data, ignore=range(0, 3))
    data_uint8 = data_uint8.astype(np.uint8)
    data_uint8[np.where(data == csf_default)] = csf_default
    data_uint8[np.where(data == white_matter_default)] = white_matter_default

    fixed = nb.Nifti1Image(
        dataobj=data_uint8, affine=image.get_affine(), header=hdr)
    _, name, _ = split_filename(roi_image)
    fixed.set_data_dtype(np.uint8)
    fixed_roi_image = op.abspath(name + "_fixedROIs.nii.gz")
    nb.save(fixed, fixed_roi_image)
    return fixed_roi_image, wm_label_file, csf_label_file, remap_dict
def fix_roi_values_noLUT(roi_image, gm_file, white_matter_file, csf_file,
                         prob_thresh):
    from coma.helpers import prepare_for_uint8
    _, name, _ = split_filename(roi_image)

    white_matter_default = 2
    csf_default = 3

    gm_image = nb.load(gm_file)
    gm_data = gm_image.get_data()
    wm_image = nb.load(white_matter_file)
    wm_data = wm_image.get_data()
    csf_image = nb.load(csf_file)
    csf_data = csf_image.get_data()

    image = nb.load(roi_image)
    data = image.get_data()

    assert (data.shape == gm_data.shape == wm_data.shape == csf_data.shape)

    data_uint8, remap_dict = prepare_for_uint8(data, ignore=[0])
    data_uint8 = data_uint8.astype(np.uint8)
    data_uint8[np.where(data_uint8 == csf_default)] = csf_default
    data_uint8[np.where(
        data_uint8 == white_matter_default)] = white_matter_default

    # Be careful with the brackets here. & takes priority over comparisons
    data_uint8[np.where((wm_data > prob_thresh)
                        & (data_uint8 == 0))] = white_matter_default
    data_uint8[np.where((csf_data > prob_thresh)
                        & (data_uint8 == 0))] = csf_default

    wm_data[np.where(data_uint8 == white_matter_default)] = 1
    wm_data[np.where(data_uint8 == csf_default)] = 0
    wm_data[np.where(data_uint8 == 0)] = 0
    wm_data = wm_data.astype(np.uint8)

    new_wm_label_image = nb.Nifti1Image(dataobj=wm_data,
                                        affine=wm_image.get_affine(),
                                        header=wm_image.get_header())
    new_wm_label_image.set_data_dtype(np.uint8)
    _, name, _ = split_filename(white_matter_file)
    wm_label_file = op.abspath(name + "_fixedWM.nii.gz")
    nb.save(new_wm_label_image, wm_label_file)

    csf_data[np.where(data_uint8 != csf_default)] = 0
    csf_data = csf_data.astype(np.uint8)
    csf_label_image = nb.Nifti1Image(dataobj=csf_data,
                                     affine=csf_image.get_affine(),
                                     header=csf_image.get_header())
    csf_label_image.set_data_dtype(np.uint8)
    _, name, _ = split_filename(csf_file)
    csf_label_file = op.abspath(name + "_fixedCSF.nii.gz")
    nb.save(csf_label_image, csf_label_file)

    hdr = image.get_header()

    unlabelled = np.where((gm_data > prob_thresh) & (data_uint8 == 0))[0]

    # Create extra ROI if there are extra GM regions in the GM mask
    if len(unlabelled) > 0:
        highestlabel = np.max(data_uint8)
        assert (highestlabel != 255)
        data_uint8[np.where((gm_data > 0)
                            & (data_uint8 == 0))] = highestlabel + 1

    fixed = nb.Nifti1Image(dataobj=data_uint8,
                           affine=image.get_affine(),
                           header=hdr)
    _, name, _ = split_filename(roi_image)
    fixed.set_data_dtype(np.uint8)
    fixed_roi_image = op.abspath(name + "_fixedROIs.nii.gz")
    nb.save(fixed, fixed_roi_image)
    return fixed_roi_image, wm_label_file, csf_label_file, remap_dict
def fix_roi_values_noLUT(roi_image, gm_file, white_matter_file, csf_file, prob_thresh):
    from coma.helpers import prepare_for_uint8
    _, name, _ = split_filename(roi_image)

    white_matter_default = 2
    csf_default = 3

    gm_image = nb.load(gm_file)
    gm_data = gm_image.get_data()
    wm_image = nb.load(white_matter_file)
    wm_data = wm_image.get_data()
    csf_image = nb.load(csf_file)
    csf_data = csf_image.get_data()

    image = nb.load(roi_image)
    data = image.get_data()

    assert(data.shape == gm_data.shape == wm_data.shape == csf_data.shape)

    data_uint8, remap_dict = prepare_for_uint8(data, ignore=[0])
    data_uint8 = data_uint8.astype(np.uint8)
    data_uint8[np.where(data_uint8 == csf_default)] = csf_default
    data_uint8[np.where(data_uint8 == white_matter_default)] = white_matter_default

    # Be careful with the brackets here. & takes priority over comparisons
    data_uint8[np.where((wm_data > prob_thresh) & (data_uint8 == 0))] = white_matter_default
    data_uint8[np.where((csf_data > prob_thresh) & (data_uint8 == 0))] = csf_default

    wm_data[np.where(data_uint8 == white_matter_default)] = 1
    wm_data[np.where(data_uint8 == csf_default)] = 0
    wm_data[np.where(data_uint8 == 0)] = 0
    wm_data = wm_data.astype(np.uint8)

    new_wm_label_image = nb.Nifti1Image(
        dataobj=wm_data, affine=wm_image.get_affine(), header=wm_image.get_header())
    new_wm_label_image.set_data_dtype(np.uint8)
    _, name, _ = split_filename(white_matter_file)
    wm_label_file = op.abspath(name + "_fixedWM.nii.gz")
    nb.save(new_wm_label_image, wm_label_file)

    csf_data[np.where(data_uint8 != csf_default)] = 0
    csf_data = csf_data.astype(np.uint8)
    csf_label_image = nb.Nifti1Image(
        dataobj=csf_data, affine=csf_image.get_affine(), header=csf_image.get_header())
    csf_label_image.set_data_dtype(np.uint8)
    _, name, _ = split_filename(csf_file)
    csf_label_file = op.abspath(name + "_fixedCSF.nii.gz")
    nb.save(csf_label_image, csf_label_file)

    hdr = image.get_header()

    unlabelled = np.where((gm_data > prob_thresh) & (data_uint8 == 0))[0]

    # Create extra ROI if there are extra GM regions in the GM mask
    if len(unlabelled) > 0:
        highestlabel = np.max(data_uint8)
        assert(highestlabel != 255)
        data_uint8[np.where((gm_data > 0) & (data_uint8 == 0))] = highestlabel + 1

    fixed = nb.Nifti1Image(
        dataobj=data_uint8, affine=image.get_affine(), header=hdr)
    _, name, _ = split_filename(roi_image)
    fixed.set_data_dtype(np.uint8)
    fixed_roi_image = op.abspath(name + "_fixedROIs.nii.gz")
    nb.save(fixed, fixed_roi_image)
    return fixed_roi_image, wm_label_file, csf_label_file, remap_dict