예제 #1
0
def load_masks_cfl(filenames, image_shape=None):
    """Read masks from files."""
    if image_shape is None:
        # First find masks shape...
        image_shape = [0, 0]
        for f in filenames:
            f_cfl = os.path.splitext(f)[0]
            mask = np.squeeze(cfl.read(f_cfl))
            shape_z = mask.shape[-2]
            shape_y = mask.shape[-1]
            if image_shape[-2] < shape_z:
                image_shape[-2] = shape_z
            if image_shape[-1] < shape_y:
                image_shape[-1] = shape_y

    masks = np.zeros([len(filenames)] + image_shape, dtype=np.complex64)

    i_file = 0
    for f in filenames:
        f_cfl = os.path.splitext(f)[0]
        tmp = np.squeeze(cfl.read(f_cfl))
        tmp = recon.zeropad(tmp, image_shape)
        masks[i_file, :, :] = tmp
        i_file = i_file + 1

    return masks
예제 #2
0
def bart_generate_mask(
    shape,
    acc,
    variable_density=True,
    shape_calib=10,
    verbose=False,
    tmp_file="mask.tmp",
):
    """Use bart poisson to generate masks."""
    if verbose:
        print("Generating sampling mask...")
    random_seed = 1e6 * random.random()
    flags = "-Z %d -Y %d -z %g -y %g -C %d -s %d" % (
        shape[0],
        shape[1],
        acc[0],
        acc[1],
        shape_calib,
        random_seed,
    )
    if variable_density:
        flags = flags + " -v"
    cmd = "bart poisson %s %s" % (flags, tmp_file)
    if verbose:
        print("  %s" % cmd)
    subprocess.check_output(["bash", "-c", cmd])
    mask = np.abs(np.squeeze(cfl.read(tmp_file)))
    return mask
예제 #3
0
def bart_pics(
    ks_input,
    verbose=False,
    sensemap=None,
    shape_e=2,
    do_cs=True,
    do_imag_reg=False,
    filename_ks_tmp="ks.tmp",
    filename_map_tmp="map.tmp",
    filename_im_tmp="im.tmp",
    filename_ks_out_tmp="ks_out.tmp",
):
    """BART PICS reconstruction."""
    if verbose:
        print("PICS (l1-ESPIRiT) reconstruction...")

    cfl.write(filename_ks_tmp, ks_input)
    if sensemap is None:
        cmd = "bart ecalib -m %d -c 1e-9 %s %s" % (
            shape_e,
            filename_ks_tmp,
            filename_map_tmp,
        )
        if verbose:
            print("  %s" % cmd)
        subprocess.check_output(["bash", "-c", cmd])
    else:
        cfl.write(filename_map_tmp, sensemap)
    if do_cs:
        flags = "-l1 -r 1e-2"
    else:
        flags = "-l2 -r 1e-2"
    if do_imag_reg:
        flags = flags + " -R R1:7:1e-1"

    cmd = "bart pics %s -S %s %s %s" % (
        flags,
        filename_ks_tmp,
        filename_map_tmp,
        filename_im_tmp,
    )
    if verbose:
        print("  %s" % cmd)
    subprocess.check_output(["bash", "-c", cmd])

    cmd = "bart fakeksp -r %s %s %s %s" % (
        filename_im_tmp,
        filename_ks_tmp,
        filename_map_tmp,
        filename_ks_out_tmp,
    )
    if verbose:
        print("  %s" % cmd)
    subprocess.check_output(["bash", "-c", cmd])
    ks_pics = np.squeeze(cfl.read(filename_ks_out_tmp))
    ks_pics = np.expand_dims(ks_pics, axis=0)

    return ks_pics
예제 #4
0
def load_recon(file, file_sensemap):
    bart_recon = np.squeeze(cfl.read(file))
    if bart_recon.ndim == 2:
        bart_recon = np.transpose(bart_recon, [1, 0])
        bart_recon = np.expand_dims(bart_recon, axis=0)
        bart_recon = np.expand_dims(bart_recon, axis=-1)
    if bart_recon.ndim == 3:
        bart_recon = np.transpose(bart_recon, [2, 1, 0])
        bart_recon = np.expand_dims(bart_recon, axis=-1)

    return bart_recon
def load_recon(file, file_sensemap):
    bart_recon = np.squeeze(cfl.read(file))
    #         print("bart recon")
    #         print(bart_recon.ndim)
    # 18, 80, 180
    if bart_recon.ndim == 2:
        bart_recon = np.transpose(bart_recon, [1, 0])
        bart_recon = np.expand_dims(bart_recon, axis=0)
        bart_recon = np.expand_dims(bart_recon, axis=-1)
    if bart_recon.ndim == 3:
        bart_recon = np.transpose(bart_recon, [2, 1, 0])
        bart_recon = np.expand_dims(bart_recon, axis=-1)
    #         print(bart_recon.shape)
    return bart_recon
예제 #6
0
def bart_espirit(
    ks_input,
    shape=None,
    verbose=False,
    shape_e=2,
    crop_value=None,
    cal_size=None,
    smooth=False,
    filename_ks_tmp="ks.tmp",
    filename_map_tmp="map.tmp",
):
    """Estimate sensitivity maps using BART ESPIRiT.
    ks_input dimensions: [emaps, channels, kz, ky, kx]
    """
    if verbose:
        print("Estimating sensitivity maps...")
    if shape is not None:
        ks_input = recon.crop(ks_input, [-1, -1, shape[0], shape[1], -1])
        ks_input = recon.zeropad(ks_input, [-1, -1, shape[0], shape[1], -1])

    flags = ""
    if crop_value is not None:
        flags = flags + "-c %f " % crop_value
    if cal_size is not None:
        flags = flags + "-r %d " % cal_size
    if smooth:
        flags = flags + "-S "

    cfl.write(filename_ks_tmp, ks_input)
    cmd = "bart ecalib -m %d %s %s %s" % (
        shape_e,
        flags,
        filename_ks_tmp,
        filename_map_tmp,
    )
    if verbose:
        print("  %s" % cmd)
    time_start = timer()
    subprocess.check_output(["bash", "-c", cmd])
    time_end = timer()
    sensemap = cfl.read(filename_map_tmp)
    return sensemap, time_end - time_start
예제 #7
0
def setup_data_tfrecords(
    dir_in_root,
    dir_out,
    data_divide=(0.75, 0.05, 0.2),
    min_shape=[80, 180],
    num_maps=1,
    crop_maps=False,
    verbose=False,
):
    """Setups training data as tfrecords.

    prep_data.setup_data('/mnt/raid3/data/Studies_DCE/recon-ccomp6/',
        '/mnt/raid3/jycheng/Project/deepspirit/data/train/', verbose=True)
    """

    # Check for two echos in here
    # Use glob to find if have echo01

    if verbose:
        print("Directory names:")
        print("  Input root:  %s" % dir_in_root)
        print("  Output root: %s" % dir_out)

    file_kspace = "kspace"
    file_sensemap = "sensemap"

    case_list = os.listdir(dir_in_root)
    random.shuffle(case_list)
    num_cases = len(case_list)

    i_train_1 = np.round(data_divide[0] * num_cases).astype(int)
    i_validate_0 = i_train_1 + 1
    i_validate_1 = np.round(
        data_divide[1] * num_cases).astype(int) + i_validate_0

    if not os.path.exists(dir_out):
        os.mkdir(dir_out)
    if not os.path.exists(os.path.join(dir_out, "train")):
        os.mkdir(os.path.join(dir_out, "train"))
    if not os.path.exists(os.path.join(dir_out, "validate")):
        os.mkdir(os.path.join(dir_out, "validate"))
    if not os.path.exists(os.path.join(dir_out, "test")):
        os.mkdir(os.path.join(dir_out, "test"))

    i_case = 0
    for case_name in case_list:
        file_kspace_i = os.path.join(dir_in_root, case_name, file_kspace)
        file_sensemap_i = os.path.join(dir_in_root, case_name, file_sensemap)

        if i_case < i_train_1:
            dir_out_i = os.path.join(dir_out, "train")
        elif i_case < i_validate_1:
            dir_out_i = os.path.join(dir_out, "validate")
        else:
            dir_out_i = os.path.join(dir_out, "test")

        if verbose:
            print("Processing [%d] %s..." % (i_case, case_name))
        i_case = i_case + 1

        kspace = np.squeeze(cfl.read(file_kspace_i))
        if (min_shape is None) or (
            min_shape[0] <= kspace.shape[1] and min_shape[1] <= kspace.shape[2]
        ):
            if verbose:
                print("  Slice shape: (%d, %d)" %
                      (kspace.shape[1], kspace.shape[2]))
                print("  Num channels: %d" % kspace.shape[0])
            shape_x = kspace.shape[-1]
            kspace = fftc.ifftc(kspace, axis=-1)
            kspace = kspace.astype(np.complex64)

            # if shape_c_out < shape_c:
            #     if verbose:
            #         print("  applying coil compression (%d -> %d)..." %
            #               (shape_c, shape_c_out))
            #     shape_cal = 24
            #     ks_cal = recon.crop(ks, [-1, shape_cal, shape_cal, -1])
            #     ks_cal = np.reshape(ks_cal, [shape_c,
            #                                  shape_cal*shape_cal,
            #                                  shape_x])
            #     cc_mat = coilcomp.calc_gcc_weights_c(ks_cal, shape_c_out)
            #     ks_cc = np.reshape(ks, [shape_c, -1, shape_x])
            #     ks_cc = coilcomp.apply_gcc_weights_c(ks_cc, cc_mat)
            #     ks = np.reshape(ks_cc, [shape_c_out, shape_z, shape_y, shape_x])

            cmd_flags = ""
            if crop_maps:
                cmd_flags = cmd_flags + " -c 1e-9"
            cmd_flags = cmd_flags + (" -m %d" % num_maps)
            cmd = "%s ecalib %s %s %s" % (
                BIN_BART,
                cmd_flags,
                file_kspace_i,
                file_sensemap_i,
            )
            if verbose:
                print("  Estimating sensitivity maps (bart espirit)...")
                print("    %s" % cmd)
            subprocess.check_call(["bash", "-c", cmd])
            sensemap = np.squeeze(cfl.read(file_sensemap_i))
            sensemap = np.expand_dims(sensemap, axis=0)
            sensemap = sensemap.astype(np.complex64)

            if verbose:
                print("  Creating tfrecords (%d)..." % shape_x)
            for i_x in range(shape_x):
                file_out = os.path.join(
                    dir_out_i, "%s_x%03d.tfrecords" % (case_name, i_x)
                )
                kspace_x = kspace[:, :, :, i_x]
                sensemap_x = sensemap[:, :, :, :, i_x]

                example = tf.train.Example(
                    features=tf.train.Features(
                        feature={
                            "name": _bytes_feature(str.encode(case_name)),
                            "xslice": _int64_feature(i_x),
                            "ks_shape_x": _int64_feature(kspace.shape[3]),
                            "ks_shape_y": _int64_feature(kspace.shape[2]),
                            "ks_shape_z": _int64_feature(kspace.shape[1]),
                            "ks_shape_c": _int64_feature(kspace.shape[0]),
                            "map_shape_x": _int64_feature(sensemap.shape[4]),
                            "map_shape_y": _int64_feature(sensemap.shape[3]),
                            "map_shape_z": _int64_feature(sensemap.shape[2]),
                            "map_shape_c": _int64_feature(sensemap.shape[1]),
                            "map_shape_m": _int64_feature(sensemap.shape[0]),
                            "ks": _bytes_feature(kspace_x.tostring()),
                            "map": _bytes_feature(sensemap_x.tostring()),
                        }
                    )
                )

                tf_writer = tf.python_io.TFRecordWriter(file_out)
                tf_writer.write(example.SerializeToString())
                tf_writer.close()
예제 #8
0
def setup_data_tfrecords(
    dir_in_root,
    dir_out,
    data_divide=(0.75, 0.05, 0.2),
    min_shape=[80, 180],
    num_maps=1,
    crop_maps=False,
    verbose=False,
):
    """Setups training data as tfrecords.
    prep_data.setup_data('/mnt/raid3/data/Studies_DCE/recon-ccomp6/',
        '/mnt/raid3/jycheng/Project/deepspirit/data/train/', verbose=True)
    """
    if verbose:
        print("Directory names:")
        print("  Input root:  %s" % dir_in_root)
        print("  Output root: %s" % dir_out)

    file_kspace = "kspace"
    file_sensemap = "sensemap"

    case_list = os.listdir(dir_in_root)
    random.shuffle(case_list)
    num_cases = len(case_list)

    i_train_1 = np.round(data_divide[0] * num_cases).astype(int)
    i_validate_0 = i_train_1 + 1
    i_validate_1 = np.round(
        data_divide[1] * num_cases).astype(int) + i_validate_0

    if not os.path.exists(dir_out):
        os.mkdir(dir_out)
    if not os.path.exists(os.path.join(dir_out, "train")):
        os.mkdir(os.path.join(dir_out, "train"))
    if not os.path.exists(os.path.join(dir_out, "validate")):
        os.mkdir(os.path.join(dir_out, "validate"))
    if not os.path.exists(os.path.join(dir_out, "test")):
        os.mkdir(os.path.join(dir_out, "test"))

    i_case = 0
    for case_name in case_list:
        file_kspace_i = os.path.join(dir_in_root, case_name, file_kspace)
        file_sensemap_i = os.path.join(dir_in_root, case_name, file_sensemap)

        if verbose:
            print("Processing [%d] %s..." % (i_case, case_name))

        if i_case < i_train_1:
            dir_out_i = os.path.join(dir_out, "train")
        elif i_case < i_validate_1:
            dir_out_i = os.path.join(dir_out, "validate")
        else:
            dir_out_i = os.path.join(dir_out, "test")

        i_case = i_case + 1

        if not os.path.exists(file_kspace_i + ".hdr"):
            print("skipping due to kspace not existing in this folder")
            continue

        kspace = np.squeeze(cfl.read(file_kspace_i))
        print("original kspace shape")
        print(kspace.shape)

        shape_x = kspace.shape[3]
        shape_y = kspace.shape[2]
        shape_z = kspace.shape[1]
        num_coils = kspace.shape[0]

        if num_coils is not 32:
            print("skipping due to incorrect number of coils")
            continue

        if min_shape[0] == kspace.shape[1] and min_shape[1] == kspace.shape[2]:
            if verbose:
                print("  Slice shape: (%d, %d)" %
                      (kspace.shape[1], kspace.shape[2]))
                print("  Num channels: %d" % kspace.shape[0])

            #  shape_x = kspace.shape[-1]
            # fix +1, -1 modulation along readout direction
            # for n in range(shape_x):
            #     modulation = (-1)**n
            #     kspace[:,:,:,n] = kspace[:,:,:,n]*np.exp(-1j*modulation)
            # print("kspace shape after modulation")
            # print(kspace.shape)
            # readout in kx
            kspace = fftc.ifftc(kspace, axis=-1)

            cmd_flags = ""
            if crop_maps:
                cmd_flags = cmd_flags + " -c 1e-9"
            # smoothing flag
            cmd_flags = cmd_flags + (" -S")

            cmd_flags = cmd_flags + (" -m %d" % num_maps)
            cmd = "%s ecalib %s %s %s" % (
                BIN_BART,
                cmd_flags,
                file_kspace_i,
                file_sensemap_i,
            )
            if verbose:
                print("  Estimating sensitivity maps (bart espirit)...")
                print("    %s" % cmd)
            subprocess.check_call(["bash", "-c", cmd])
            sensemap = np.squeeze(cfl.read(file_sensemap_i))
            sensemap = np.expand_dims(sensemap, axis=0)
            sensemap = sensemap.astype(np.complex64)

            if verbose:
                print("  Creating tfrecords (%d)..." % shape_x)
            for i_x in range(shape_x):
                file_out = os.path.join(
                    dir_out_i, "%s_x%03d.tfrecords" % (case_name, i_x))
                kspace_x = kspace[:, :, :, i_x]
                sensemap_x = sensemap[:, :, :, :, i_x]

                example = tf.train.Example(features=tf.train.Features(
                    feature={
                        "name": _bytes_feature(str.encode(case_name)),
                        "xslice": _int64_feature(i_x),
                        "ks_shape_x": _int64_feature(kspace.shape[3]),
                        "ks_shape_y": _int64_feature(kspace.shape[2]),
                        "ks_shape_z": _int64_feature(kspace.shape[1]),
                        "ks_shape_c": _int64_feature(kspace.shape[0]),
                        "map_shape_x": _int64_feature(sensemap.shape[4]),
                        "map_shape_y": _int64_feature(sensemap.shape[3]),
                        "map_shape_z": _int64_feature(sensemap.shape[2]),
                        "map_shape_c": _int64_feature(sensemap.shape[1]),
                        "map_shape_m": _int64_feature(sensemap.shape[0]),
                        "ks": _bytes_feature(kspace_x.tostring()),
                        "map": _bytes_feature(sensemap_x.tostring()),
                    }))

                tf_writer = tf.python_io.TFRecordWriter(file_out)
                tf_writer.write(example.SerializeToString())
                tf_writer.close()
        else:
            print("skipping due to wrong slice dimensions")
예제 #9
0
def setup_data_tfrecords_3d(
    dir_in_root,
    dir_out,
    data_divide=(0.8, 0.1, 0.2),
    min_shape=[80, 180],
    num_maps=1,
    crop_maps=False,
    verbose=False,
    shuffle=True,
):
    """Setups training data as tfrecords.

    prep_data.setup_data('/mnt/raid3/data/Studies_DCE/recon-ccomp6/',
        '/mnt/raid3/jycheng/Project/deepspirit/data/train/', verbose=True)
    """

    # Check for two echos in here
    # Use glob to find if have echo01

    if verbose:
        print("Directory names:")
        print("  Input root:  %s" % dir_in_root)
        print("  Output root: %s" % dir_out)

    # edits for /mnt/dense/data/MFAST_DCE and /home_local/ekcole/MFAST_DCE
    dir_kspace = "sort-ccomp6"
    dir_map = "recon-ccomp6"

    dir_cases = os.path.join(dir_in_root, dir_kspace)

    file_kspace = "ks_sorted"
    file_sensemap = "map"

    case_list = os.listdir(dir_cases)
    if shuffle is True:
        random.shuffle(case_list)
    else:
        print("don't shuffle dataset")
    num_cases = len(case_list)

    i_train_1 = np.round(data_divide[0] * num_cases).astype(int)
    i_validate_0 = i_train_1 + 1
    i_validate_1 = np.round(
        data_divide[1] * num_cases).astype(int) + i_validate_0

    if not os.path.exists(dir_out):
        os.mkdir(dir_out)
    if not os.path.exists(os.path.join(dir_out, "train")):
        os.mkdir(os.path.join(dir_out, "train"))
    if not os.path.exists(os.path.join(dir_out, "validate")):
        os.mkdir(os.path.join(dir_out, "validate"))
    if not os.path.exists(os.path.join(dir_out, "test")):
        os.mkdir(os.path.join(dir_out, "test"))

    i_case = 0
    for case_name in case_list:
        file_kspace_i = os.path.join(dir_in_root, dir_kspace, case_name,
                                     file_kspace)
        file_sensemap_i = os.path.join(dir_in_root, dir_map, case_name,
                                       file_sensemap)
        file_sensemap_i_check = os.path.join(dir_in_root, dir_map, case_name,
                                             file_sensemap + ".cfl")
        if i_case < i_train_1:
            dir_out_i = os.path.join(dir_out, "train")
        elif i_case < i_validate_1:
            dir_out_i = os.path.join(dir_out, "validate")
        else:
            dir_out_i = os.path.join(dir_out, "test")
        print("dir out")
        print(dir_out_i)

        if verbose:
            print("Processing [%d] %s..." % (i_case, case_name))
        i_case = i_case + 1

        # if no map, skip this case
        # do nothing
        if not path.exists(file_sensemap_i_check):
            print("Sensitivity map does not exist")
            continue

        # get dims from .hdr
        h = open(file_kspace_i + ".hdr", "r")
        h.readline()  # skip
        l = h.readline()
        h.close()
        dims = [int(i) for i in l.split()]
        print(dims)
        ky = dims[1]
        kz = dims[2]
        if ky != 180 or kz != 80:
            print("wrong dimensions")
            continue

        kspace = np.squeeze(cfl.read(file_kspace_i))

        # it wants coils x y z frames
        kspace = np.transpose(kspace, [1, -1, -2, 2, 0])

        if verbose:
            print("  Slice shape: (%d, %d)" %
                  (kspace.shape[2], kspace.shape[3]))
            print("  Num channels: %d" % kspace.shape[0])
            print("  Num frames: %d" % kspace.shape[-1])
        # number of frames
        shape_f = kspace.shape[-1]
        # number of slices in x direction
        num_slices = kspace.shape[1]

        kspace = fftc.ifftc(kspace, axis=1)
        kspace = kspace.astype(np.complex64)

        print("Exists")
        sensemap = np.squeeze(cfl.read(file_sensemap_i))
        sensemap = sensemap[0, :, :, :, :]
        # it has coils z y x
        # 6, 80, 156, 192
        #         print(sensemap.shape)
        # we want coils x y z
        sensemap = np.transpose(sensemap, [0, -1, 2, 1])
        sensemap = np.expand_dims(sensemap, axis=0)
        sensemap = sensemap.astype(np.complex64)

        if verbose:
            print("  Creating tfrecords (%d)..." % num_slices)
        # for 2D plus time, only iterate over slices, not time frames

        for i_slice in range(num_slices):
            # normalization across time frames
            kspace_x = kspace[:, i_slice, :, :, :]
            file_out = os.path.join(
                dir_out_i, "%s_x%03d.tfrecords" % (case_name, i_slice))
            #             kspace_x = kspace[:, i_x, :, :, i_f]/max_frames
            sensemap_x = sensemap[:, :, i_slice, :, :]
            example = tf.train.Example(features=tf.train.Features(
                feature={
                    "name": _bytes_feature(str.encode(case_name)),
                    "slice": _int64_feature(i_slice),
                    "ks_shape_x": _int64_feature(kspace.shape[1]),
                    "ks_shape_y": _int64_feature(kspace.shape[2]),
                    "ks_shape_z": _int64_feature(kspace.shape[3]),
                    "ks_shape_t": _int64_feature(kspace.shape[4]),
                    "ks_shape_c": _int64_feature(kspace.shape[0]),
                    "map_shape_x": _int64_feature(sensemap.shape[2]),
                    "map_shape_y": _int64_feature(sensemap.shape[3]),
                    "map_shape_z": _int64_feature(sensemap.shape[4]),
                    "map_shape_c": _int64_feature(sensemap.shape[1]),
                    "map_shape_m": _int64_feature(sensemap.shape[0]),
                    "ks": _bytes_feature(kspace_x.tostring()),
                    "map": _bytes_feature(sensemap_x.tostring()),
                }))

            tf_writer = tf.python_io.TFRecordWriter(file_out)
            tf_writer.write(example.SerializeToString())
            tf_writer.close()
예제 #10
0
def setup_data_tfrecords_DCE(
    dir_in_root,
    dir_out,
    data_divide=(0.75, 0.05, 0.2),
    min_shape=[80, 180],
    num_maps=1,
    crop_maps=False,
    verbose=False,
    shuffle=True,
):
    """Setups training data as tfrecords.

    prep_data.setup_data('/mnt/raid3/data/Studies_DCE/recon-ccomp6/',
        '/mnt/raid3/jycheng/Project/deepspirit/data/train/', verbose=True)
    """
    if verbose:
        print("Directory names:")
        print("  Input root:  %s" % dir_in_root)
        print("  Output root: %s" % dir_out)

    # edits for /mnt/dense/data/MFAST_DCE and /home_local/ekcole/MFAST_DCE
    dir_kspace = "sort-ccomp6"
    dir_map = "recon-ccomp6"
    #     dir_kspace = "dce-ccomp6"

    dir_cases = os.path.join(dir_in_root, dir_kspace)

    file_kspace = "ks_sorted"
    file_sensemap = "map"

    case_list = os.listdir(dir_cases)
    # shuffle cases (patients)
    if shuffle is True:
        random.shuffle(case_list)
    else:
        print("don't shuffle dataset")
    num_cases = len(case_list)

    i_train_1 = np.round(data_divide[0] * num_cases).astype(int)
    i_validate_0 = i_train_1 + 1
    i_validate_1 = np.round(
        data_divide[1] * num_cases).astype(int) + i_validate_0

    if not os.path.exists(dir_out):
        os.mkdir(dir_out)
    if not os.path.exists(os.path.join(dir_out, "train")):
        os.mkdir(os.path.join(dir_out, "train"))
    if not os.path.exists(os.path.join(dir_out, "validate")):
        os.mkdir(os.path.join(dir_out, "validate"))
    if not os.path.exists(os.path.join(dir_out, "test")):
        os.mkdir(os.path.join(dir_out, "test"))

    i_case = 0
    for case_name in case_list:
        file_kspace_i = os.path.join(dir_in_root, dir_kspace, case_name,
                                     file_kspace)
        file_sensemap_i = os.path.join(dir_in_root, dir_map, case_name,
                                       file_sensemap)
        file_sensemap_i_check = os.path.join(dir_in_root, dir_map, case_name,
                                             file_sensemap + ".cfl")

        # if no map, skip this case and do nothing
        if not path.exists(file_sensemap_i_check):
            print("Does not exist")
            continue

        # get dims from .hdr
        h = open(file_kspace_i + ".hdr", "r")
        h.readline()  # skip
        l = h.readline()
        h.close()
        dims = [int(i) for i in l.split()]
        print(dims)
        ky = dims[1]
        kz = dims[2]
        if ky != 180 or kz != 80:
            print("wrong dimensions")
            continue

        if i_case < i_train_1:
            dir_out_i = os.path.join(dir_out, "train")
        elif i_case < i_validate_1:
            dir_out_i = os.path.join(dir_out, "validate")
        else:
            dir_out_i = os.path.join(dir_out, "test")

        if verbose:
            print("Processing [%d] %s..." % (i_case, case_name))
        i_case = i_case + 1

        #         if(i_case >= 50):
        #             break

        kspace = np.squeeze(cfl.read(file_kspace_i))
        # it wants coils x y z frames
        kspace = np.transpose(kspace, [1, -1, -2, 2, 0])
        if verbose:
            print("  Slice shape: (%d, %d)" %
                  (kspace.shape[2], kspace.shape[3]))
            print("  Num channels: %d" % kspace.shape[0])
            print("  Num frames: %d" % kspace.shape[-1])
        # number of frames
        shape_f = kspace.shape[-1]
        # number of slices in x direction
        shape_x = kspace.shape[1]

        kspace = fftc.ifftc(kspace, axis=1)
        kspace = kspace.astype(np.complex64)

        sensemap = np.squeeze(cfl.read(file_sensemap_i))
        sensemap = sensemap[0, :, :, :, :]
        # we want coils x y z
        sensemap = np.transpose(sensemap, [0, -1, 2, 1])
        sensemap = np.expand_dims(sensemap, axis=0)
        sensemap = sensemap.astype(np.complex64)

        if verbose:
            print("  Creating tfrecords (%d)..." % shape_x)
        # Need to iterate over both z and frames

        for i_x in range(shape_x):
            # normalization across time frames
            kspace_x = kspace[:, i_x, :, :, :]
            max_frames = np.max(np.abs(kspace_x))
            #             print(max_frames)
            for i_f in range(shape_f):
                file_out = os.path.join(
                    dir_out_i,
                    "%s_x%03d_f%03d.tfrecords" % (case_name, i_x, i_f))
                kspace_x = kspace[:, i_x, :, :, i_f] / max_frames
                sensemap_x = sensemap[:, :, i_x, :, :]

                #                 #save images as pngs to check if time frames shuffling is done here
                # #                 ks = np.squeeze(kspace_x)
                #                 ks = kspace_x
                #                 print(ks.shape)
                #                 ks = np.transpose(ks, [1,2,0])
                # #                 ks = np.expand_dims(ks, 0)
                #                 ks = tf.convert_to_tensor(ks)
                #                 print("ks")
                #                 print(ks)

                #                 sense = np.squeeze(sensemap_x)
                #                 print(sense.shape)
                #                 sense = np.transpose(sense, [1,2,0])
                #                 sense = np.expand_dims(sense, -2)
                #                 sense = tf.convert_to_tensor(sense)
                #                 print("sensemap")
                #                 print(sense)

                #                 image_x = tf_util.model_transpose(ks, sense)

                #                 sess = tf.Session()

                #                 # Evaluate the tensor `c`.
                #                 image_x = sess.run(image_x)

                #                 filename = dir_out_i + '/images/case' + str(i_x) + '_f' + str(i_f) + '.png'
                #                 print(filename)
                #                 scipy.misc.imsave(filename, np.squeeze(np.abs(image_x)))

                # at this stage, the images were not shuffled

                example = tf.train.Example(features=tf.train.Features(
                    feature={
                        "name": _bytes_feature(str.encode(case_name)),
                        "xslice": _int64_feature(i_x),
                        "ks_shape_x": _int64_feature(kspace.shape[1]),
                        "ks_shape_y": _int64_feature(kspace.shape[2]),
                        "ks_shape_z": _int64_feature(kspace.shape[3]),
                        "ks_shape_c": _int64_feature(kspace.shape[0]),
                        "map_shape_x": _int64_feature(sensemap.shape[2]),
                        "map_shape_y": _int64_feature(sensemap.shape[3]),
                        "map_shape_z": _int64_feature(sensemap.shape[4]),
                        "map_shape_c": _int64_feature(sensemap.shape[1]),
                        "map_shape_m": _int64_feature(sensemap.shape[0]),
                        "ks": _bytes_feature(kspace_x.tostring()),
                        "map": _bytes_feature(sensemap_x.tostring()),
                    }))

                tf_writer = tf.python_io.TFRecordWriter(file_out)
                tf_writer.write(example.SerializeToString())
                tf_writer.close()