Ejemplo n.º 1
0
def measure_picmus(beamformer, moniker, center_angle=False, verbose=True):
    # Load PICMUS dataset
    database_path = os.path.join("datasets", "picmus")
    acq = "simulation"
    target = "contrast_speckle"
    dtype = "rf"
    P = PICMUSData(database_path, acq, target, dtype)

    # Define pixel grid limits (assume y == 0)
    xlims = [P.ele_pos[0, 0], P.ele_pos[-1, 0]]
    zlims = [5e-3, 55e-3]
    wvln = P.c / P.fc
    dx = wvln / 3
    dz = dx  # Use square pixels
    grid = make_pixel_grid(xlims, zlims, dx, dz)
    xext = (np.array([-0.5, grid.shape[1] - 0.5]) * dx + xlims[0]) * 1e3
    zext = (np.array([-0.5, grid.shape[0] - 0.5]) * dz + zlims[0]) * 1e3
    extent = [xext[0], xext[1], zext[1], zext[0]]

    if center_angle:
        # Grab only the center angle's worth of data
        aidx = len(P.angles) // 2
        P.idata = P.idata[[aidx]]
        P.qdata = P.qdata[[aidx]]
        P.angles = P.angles[[aidx]]
        P.time_zero = P.time_zero[[aidx]]

    # Beamform the ROI
    bimg = beamformer(P, grid)

    plt.figure(figsize=[5, 6])
    plt.imshow(20 * np.log10(bimg),
               vmin=-60,
               cmap="gray",
               extent=extent,
               origin="upper")
    plt.suptitle("%s: PICMUS" % moniker)
    plt.pause(0.01)
    outdir = os.path.join("results", moniker)
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    plt.savefig(os.path.join(outdir, "picmus.jpg"))
Ejemplo n.º 2
0
def rois_additional(idx, outdir=os.path.join("scoring","rois","additional")):
    images = [
        ("JHU", 24),
        ("JHU", 25),
        ("JHU", 26),
        ("JHU", 27),
        ("JHU", 28),
        ("JHU", 29),
        ("JHU", 30),
        ("JHU", 31),
        ("JHU", 32),
        ("JHU", 33),
        ("JHU", 34),
    ]
    data_source, acq = images[idx]
    P, xl, zl = load_data(data_source, acq)

    # Define pixel grid limits (assume y == 0)
    wvln = P.c / P.fc
    dx = wvln / 3
    dz = dx  # Use square pixels
    desired_grid = [400, 300]
    xlims = np.array([-0.5, 0.5]) * (desired_grid[1] - 1) * dx + (xl[0] + xl[1]) / 2
    zlims = np.array([-0.5, 0.5]) * (desired_grid[0] - 1) * dz + (zl[0] + zl[1]) / 2
    xlims = [np.maximum(xlims[0], xl[0]), np.minimum(xlims[1], xl[1])]
    zlims = [np.maximum(zlims[0], zl[0]), np.minimum(zlims[1], zl[1])]
    grid = make_pixel_grid(xlims, zlims, dx, dz)
    fnum = 1

    # Normalize input to [-1, 1] range
    maxval = np.maximum(np.abs(P.idata).max(), np.abs(P.qdata).max())
    P.idata /= maxval
    P.qdata /= maxval

    # Make data torch tensors
    x = (P.idata, P.qdata)

    # Make 75-angle image
    dasN = DAS_PW(P, grid, rxfnum=fnum)
    idasN, qdasN = dasN(x)
    idasN, qdasN = idasN.detach().cpu().numpy(), qdasN.detach().cpu().numpy()
    iqN = idasN + 1j * qdasN
    ground_truth = np.abs(iqN)
    ground_truth /= np.amax(ground_truth)

    # Normalize to have RMS = 1
    # ground_truth /= np.sqrt(np.mean(ground_truth ** 2))

    # Display images via matplotlib
    xext = (np.array([-0.5, grid.shape[1] - 0.5]) * dx + xlims[0]) * 1e3
    zext = (np.array([-0.5, grid.shape[0] - 0.5]) * dz + zlims[0]) * 1e3
    extent = [xext[0], xext[1], zext[1], zext[0]]
    plt.clf()
    bimgN = 20 * np.log10(ground_truth)  # Log-compress
    bimgN -= np.amax(bimgN)  # Normalize by max value
    plt.imshow(bimgN, vmin=-60, cmap="gray", extent=extent, origin="upper")
    plt.suptitle("%s%03d (c = %d m/s)" % (data_source, acq, np.round(P.c)))
    # Check to make sure save directory exists, if not, create it
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    plt.savefig(os.path.join(outdir,"roi%02d.png" % (idx)))

    # Save
    mdict = {
        "grid": grid,
        "data_source": data_source,
        "acq": acq,
        "extent": extent,
        "ground_truth": ground_truth,
    }
    hdf5storage.savemat(os.path.join(outdir,"roi%02d" % (idx)), mdict)
Ejemplo n.º 3
0
from cubdl.PixelGrid import make_pixel_grid

# Load PICMUS dataset
database_path = os.path.join("datasets", "data", "picmus")
acq = "simulation"
target = "contrast_speckle"
dtype = "iq"
P = PICMUSData(database_path, acq, target, dtype)

# Define pixel grid limits (assume y == 0)
xlims = [P.ele_pos[0, 0], P.ele_pos[-1, 0]]
zlims = [5e-3, 55e-3]
wvln = P.c / P.fc
dx = wvln / 3
dz = dx  # Use square pixels
grid = make_pixel_grid(xlims, zlims, dx, dz)
fnum = 1

# Create a DAS_PW neural network for all angles, for 1 angle
dasN = DAS_PW(P, grid)
idx = len(P.angles) // 2  # Choose center angle for 1-angle DAS
das1 = DAS_PW(P, grid, idx)

# Store I and Q components as a tuple
iqdata = (P.idata, P.qdata)

# Make 75-angle image
idasN, qdasN = dasN(iqdata)
idasN, qdasN = idasN.detach().cpu().numpy(), qdasN.detach().cpu().numpy()
iqN = idasN + 1j * qdasN  # Tranpose for display purposes
bimgN = 20 * np.log10(np.abs(iqN))  # Log-compress
Ejemplo n.º 4
0
def rois_lesion(idx, outdir=os.path.join("scoring", "rois", "lesion")):
    if idx == 0:
        data_source, acq = "UFL", 1
        xctr, zctr, r0, r1 = -0.8e-3, 30.3e-3, 2.8e-3, 4.5e-3
        P, _, _ = load_data(data_source, acq)
    elif idx == 1:
        data_source, acq = "UFL", 5
        xctr, zctr, r0, r1 = 0.2e-3, 16.4e-3, 2.2e-3, 3.5e-3
        P, _, _ = load_data(data_source, acq)
    elif idx == 2:
        data_source, acq = "UFL", 5
        xctr, zctr, r0, r1 = -0.5e-3, 45e-3, 2.2e-3, 3.5e-3
        P, _, _ = load_data(data_source, acq)
    elif idx == 3:
        data_source, acq = "OSL", 7
        xctr, zctr, r0, r1 = -8.2e-3, 39.1e-3, 2.5e-3, 4.5e-3
        P, _, _ = load_data(data_source, acq)
    elif idx == 4:
        data_source, acq = "MYO", 1
        xctr, zctr, r0, r1 = 15.5e-3, 16.5e-3, 1.3e-3, 3e-3
        P, _, _ = load_data(data_source, acq)
    elif idx == 5:
        data_source, acq = "MYO", 4
        xctr, zctr, r0, r1 = 12.2e-3, 25.8e-3, 1.3e-3, 3e-3
        P, _, _ = load_data(data_source, acq)
    elif idx == 6:
        data_source, acq = "INS", 8
        xctr, zctr, r0, r1 = 11.4e-3, 42.3e-3, 2.8e-3, 4.5e-3
        P, _, _ = load_data(data_source, acq)
    elif idx == 7:
        data_source, acq = "INS", 21
        xctr, zctr, r0, r1 = -7e-3, 41.2e-3, 2.8e-3, 4.5e-3
        P, _, _ = load_data(data_source, acq)
    else:
        raise NotImplementedError

    xlims = [-6e-3 + xctr, 6e-3 + xctr]
    zlims = [-6e-3 + zctr, 6e-3 + zctr]
    r2 = np.sqrt(r0**2 + r1**2)

    # Define pixel grid limits (assume y == 0)
    wvln = P.c / P.fc
    dx = wvln / 3
    dz = dx  # Use square pixels
    grid = make_pixel_grid(xlims, zlims, dx, dz)
    fnum = 1

    # Normalize input to [-1, 1] range
    maxval = np.maximum(np.abs(P.idata).max(), np.abs(P.qdata).max())
    P.idata /= maxval
    P.qdata /= maxval

    # Make ROI
    dist = np.sqrt((grid[:, :, 0] - xctr)**2 + (grid[:, :, 2] - zctr)**2)
    roi_i = dist <= r0
    roi_o = (r1 <= dist) * (dist <= r2)

    # Make data torch tensors
    x = (P.idata, P.qdata)

    # Make 75-angle image
    dasN = DAS_PW(P, grid, rxfnum=fnum)
    idasN, qdasN = dasN(x)
    idasN, qdasN = idasN.detach().cpu().numpy(), qdasN.detach().cpu().numpy()
    iqN = idasN + 1j * qdasN
    bimgN = 20 * np.log10(np.abs(iqN))  # Log-compress
    bimgN -= np.amax(bimgN)  # Normalize by max value

    # Display images via matplotlib
    xext = (np.array([-0.5, grid.shape[1] - 0.5]) * dx + xlims[0]) * 1e3
    zext = (np.array([-0.5, grid.shape[0] - 0.5]) * dz + zlims[0]) * 1e3
    extent = [xext[0], xext[1], zext[1], zext[0]]
    plt.clf()
    opts = {"extent": extent, "origin": "upper"}
    plt.imshow(bimgN, vmin=-40, cmap="gray", **opts)
    plt.contour(roi_i, [0.5], colors="c", **opts)
    plt.contour(roi_o, [0.5], colors="m", **opts)
    plt.suptitle("%s%03d (c = %d m/s)" % (data_source, acq, np.round(P.c)))
    # Check to make sure save directory exists, if not, create it
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    plt.savefig(os.path.join(outdir, "roi%02d.png" % (idx)))

    # Save ROI locations
    xgrid = grid[:, :, 0]
    ygrid = grid[:, :, 1]
    zgrid = grid[:, :, 2]
    grid_i = np.stack([xgrid[roi_i], ygrid[roi_i], zgrid[roi_i]], axis=1)
    grid_o = np.stack([xgrid[roi_o], ygrid[roi_o], zgrid[roi_o]], axis=1)
    grid_i = np.expand_dims(grid_i, 1)
    grid_o = np.expand_dims(grid_o, 1)
    mdict = {
        "grid": grid,
        "grid_i": grid_i,
        "grid_o": grid_o,
        "data_source": data_source,
        "acq": acq,
        "xctr": xctr,
        "zctr": zctr,
        "r0": r0,
        "r1": r1,
        "r2": r2,
        "extent": extent,
        "roi_i": roi_i,
        "roi_o": roi_o,
    }
    hdf5storage.savemat(os.path.join(outdir, "roi%02d" % (idx)), mdict)
Ejemplo n.º 5
0
                             dtype=torch.float,
                             device=torch.device("cuda:0"))
        x = (idata, qdata)

        # Make focused transmit
        das = DAS_FT(F, grid, rxfnum=fnum)
        idas, qdas = das(x)
        idas, qdas = idas.detach().cpu().numpy(), qdas.detach().cpu().numpy()
        iq = idas + 1j * qdas
        bimg = np.abs(iq).T

        # Scan convert if necessary
        if scan_convert:
            xlims = rlims[1] * np.array([-0.7, 0.7])
            zlims = rlims[1] * np.array([0, 1])
            img_grid = make_pixel_grid(xlims, zlims, wvln / 2, wvln / 2)
            grid = np.transpose(grid, (1, 0, 2))
            g1 = np.stack((grid[:, :, 2], grid[:, :, 0]), -1).reshape(-1, 2)
            g2 = np.stack((img_grid[:, :, 2], img_grid[:, :, 0]),
                          -1).reshape(-1, 2)
            bsc = griddata(g1, bimg.reshape(-1), g2, "linear", 1e-10)
            bimg = np.reshape(bsc, img_grid.shape[:2])
            grid = img_grid.transpose(1, 0, 2)

        bimg = 20 * np.log10(bimg)  # Log-compress
        bimg -= np.amax(bimg)  # Normalize by max value

        # Display images via matplotlib
        extent = [grid[0, 0, 0], grid[-1, 0, 0], grid[0, -1, 2], grid[0, 0, 2]]
        extent = np.array(extent) * 1e3  # Convert to mm
        plt.clf()
Ejemplo n.º 6
0
def rois_point(idx, outdir=os.path.join("scoring", "rois", "point")):
    if idx == 0:
        data_source, acq = "MYO", 3
        P, _, _ = load_data(data_source, acq)
        xlims = [0e-3, 20e-3]
        zlims = [22e-3, 32e-3]
        pts = [[13.4e-3, 0, 27.75e-3], [14.55e-3, 0, 26.8e-3],
               [15.15e-3, 0, 25.85e-3]]
    elif idx == 1:
        data_source, acq = "UFL", 4
        P, _, _ = load_data(data_source, acq)
        xlims = [-6e-3, 10e-3]
        zlims = [22e-3, 32e-3]
        pts = [[-2.8e-3, 0, 27.65e-3], [-3.95e-3, 0, 26.7e-3],
               [-4.56e-3, 0, 25.7e-3]]
    elif idx == 2:
        data_source, acq = "UFL", 2
        P, _, _ = load_data(data_source, acq)
        xlims = [-3e-3, 3e-3]
        zlims = [12e-3, 38e-3]
        pts = [
            [-0.29e-3, 0, 15.5e-3],
            [-0.31e-3, 0, 20.6e-3],
            [-0.43e-3, 0, 25.4e-3],
            [-0.20e-3, 0, 30.8e-3],
            [-0.12e-3, 0, 35.7e-3],
        ]
    elif idx == 3:
        data_source, acq = "MYO", 2
        P, _, _ = load_data(data_source, acq)
        xlims = [-15e-3, 11e-3]
        zlims = [38e-3, 42e-3]
        pts = [[-11.95e-3, 0, 39.4e-3], [-2e-3, 0, 39.55e-3],
               [8e-3, 0, 39.6e-3]]
    else:
        raise NotImplementedError

    # Define pixel grid limits (assume y == 0)
    wvln = P.c / P.fc
    dx = wvln / 3
    dz = dx  # Use square pixels
    grid = make_pixel_grid(xlims, zlims, dx, dz)
    fnum = 1

    # Normalize input to [-1, 1] range
    maxval = np.maximum(np.abs(P.idata).max(), np.abs(P.qdata).max())
    P.idata /= maxval
    P.qdata /= maxval

    # Make data torch tensors
    x = (P.idata, P.qdata)

    # Make 75-angle image
    dasN = DAS_PW(P, grid, rxfnum=fnum)
    idasN, qdasN = dasN(x)
    idasN, qdasN = idasN.detach().cpu().numpy(), qdasN.detach().cpu().numpy()
    iqN = idasN + 1j * qdasN
    bimgN = 20 * np.log10(np.abs(iqN))  # Log-compress
    bimgN -= np.amax(bimgN)  # Normalize by max value

    # Display images via matplotlib
    xext = (np.array([-0.5, grid.shape[1] - 0.5]) * dx + xlims[0]) * 1e3
    zext = (np.array([-0.5, grid.shape[0] - 0.5]) * dz + zlims[0]) * 1e3
    extent = [xext[0], xext[1], zext[1], zext[0]]
    plt.clf()
    plt.imshow(bimgN, vmin=-40, cmap="gray", extent=extent, origin="upper")
    plt.suptitle("%s%03d (c = %d m/s)" % (data_source, acq, np.round(P.c)))
    bar = np.array([-1e-3, 1e-3])
    tmp = np.array([0, 0])
    for pt in pts:
        plt.plot((pt[0] + bar) * 1e3, (pt[2] + tmp) * 1e3, "c-")
        plt.plot((pt[0] + tmp) * 1e3, (pt[2] + bar) * 1e3, "c-")
    # Check to make sure save directory exists, if not, create it
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    plt.savefig(os.path.join(outdir, "roi%02d.png" % (idx)))

    # Save
    mdict = {
        "grid": grid,
        "data_source": data_source,
        "acq": acq,
        "extent": extent,
        "pts": pts,
    }
    hdf5storage.savemat(os.path.join(outdir, "roi%02d" % (idx)), mdict)
Ejemplo n.º 7
0
def rois_speckle(idx, outdir=os.path.join("scoring", "rois", "speckle")):
    if idx == 0:
        data_source, acq = "UFL", 4
        P, _, _ = load_data(data_source, acq)
        xlims = [0e-3, 12e-3]
        zlims = [14e-3, 26e-3]
    elif idx == 1:
        data_source, acq = "OSL", 7
        P, _, _ = load_data(data_source, acq)
        xlims = [-6e-3, 6e-3]
        zlims = [14e-3, 26e-3]
    elif idx == 2:
        data_source, acq = "MYO", 2
        P, _, _ = load_data(data_source, acq)
        xlims = [-6e-3, 6e-3]
        zlims = [14e-3, 26e-3]
    elif idx == 3:
        data_source, acq = "EUT", 3
        P, _, _ = load_data(data_source, acq)
        xlims = [-6e-3, 6e-3]
        zlims = [25e-3, 37e-3]
    elif idx == 4:
        data_source, acq = "INS", 4
        P, _, _ = load_data(data_source, acq)
        xlims = [-6e-3, 6e-3]
        zlims = [25e-3, 37e-3]
    elif idx == 5:
        data_source, acq = "INS", 16
        P, _, _ = load_data(data_source, acq)
        xlims = [-6e-3, 6e-3]
        zlims = [25e-3, 37e-3]
    else:
        raise NotImplementedError

    # Define pixel grid limits (assume y == 0)
    wvln = P.c / P.fc
    dx = wvln / 3
    dz = dx  # Use square pixels
    grid = make_pixel_grid(xlims, zlims, dx, dz)
    fnum = 1

    # Normalize input to [-1, 1] range
    maxval = np.maximum(np.abs(P.idata).max(), np.abs(P.qdata).max())
    P.idata /= maxval
    P.qdata /= maxval

    # Make data torch tensors
    x = (P.idata, P.qdata)

    # Make 75-angle image
    dasN = DAS_PW(P, grid, rxfnum=fnum)
    idasN, qdasN = dasN(x)
    idasN, qdasN = idasN.detach().cpu().numpy(), qdasN.detach().cpu().numpy()
    iqN = idasN + 1j * qdasN
    bimgN = 20 * np.log10(np.abs(iqN))  # Log-compress
    bimgN -= np.amax(bimgN)  # Normalize by max value

    # Display images via matplotlib
    xext = (np.array([-0.5, grid.shape[1] - 0.5]) * dx + xlims[0]) * 1e3
    zext = (np.array([-0.5, grid.shape[0] - 0.5]) * dz + zlims[0]) * 1e3
    extent = [xext[0], xext[1], zext[1], zext[0]]
    plt.clf()
    plt.imshow(bimgN, vmin=-40, cmap="gray", extent=extent, origin="upper")
    plt.suptitle("%s%03d (c = %d m/s)" % (data_source, acq, np.round(P.c)))
    # Check to make sure save directory exists, if not, create it
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    plt.savefig(os.path.join(outdir, "roi%02d.png" % (idx)))

    # Save
    mdict = {
        "grid": grid,
        "data_source": data_source,
        "acq": acq,
        "extent": extent
    }
    hdf5storage.savemat(os.path.join(outdir, "roi%02d" % (idx)), mdict)