示例#1
0
def create_phantom(args):
    knot_vector = bsplines.uniform_regular_knot_vector(args.num_control_points,
                                                       args.spline_degree,
                                                       t0=args.t_start,
                                                       t1=args.t_end)
    knot_vector = np.array(knot_vector, dtype='float32')

    knot_avgs = bsplines.control_points(args.spline_degree, knot_vector)

    control_points = np.empty(
        (args.num_scatterers, args.num_control_points, 3), dtype='float32')
    amplitudes = np.random.uniform(low=0.0,
                                   high=1.0,
                                   size=(args.num_scatterers, ))
    for scatterer_i in range(args.num_scatterers):
        print('Scatterer %d of %d' % (scatterer_i, args.num_scatterers))
        # generate random start point
        x0 = random.uniform(args.x_min, args.x_max)
        y0 = random.uniform(args.y_min, args.y_max)
        z0 = args.z0 + random.uniform(-0.5, 0.5) * args.thickness
        for control_pt_i, t_star in enumerate(knot_avgs):
            x = x0
            y = y0
            z = z0 + args.ampl * np.cos(2 * np.pi * args.freq * t_star)
            control_points[scatterer_i, control_pt_i, :] = [x, y, z]

    with h5py.File(args.h5_file, 'w') as f:
        f["control_points"] = control_points
        f["amplitudes"] = np.array(amplitudes, dtype="float32")
        f["spline_degree"] = args.spline_degree
        f["file_format_version"] = "1"
        f["knot_vector"] = knot_vector

    print('Scatterer dataset written to %s' % args.h5_file)
    print('knot vector: %s' % knot_vector)
示例#2
0
def create_phantom(args):
    xs = np.random.uniform(size=(args.num_scatterers,), low=args.x_min, high=args.x_max)
    ys = np.random.uniform(size=(args.num_scatterers,), low=args.y_min, high=args.y_max)
    zs = np.random.uniform(size=(args.num_scatterers,), low=args.z_min, high=args.z_max)
    as_ = np.random.uniform(size=(args.num_scatterers,), low=0.0, high=1.0)
    
    scatterers = np.empty( (3, args.num_scatterers))
    scatterers[0, :] = xs
    scatterers[1, :] = ys
    scatterers[2, :] = zs
    
    control_points = np.empty( (args.num_scatterers, args.num_cs, 3), dtype='float32')
    
    knots = bsplines.uniform_regular_knot_vector(args.num_cs, args.spline_degree, t0=args.t0, t1=args.t1)
    knot_avgs = bsplines.control_points(args.spline_degree, knots)

    
    for control_point_i, t_star in enumerate(knot_avgs):    
        print('t=%f' % t_star)
        x_angle = args.x_angular_velocity*t_star
        y_angle = args.y_angular_velocity*t_star
        z_angle = args.z_angular_velocity*t_star
        rot_mat = general_rot_mat(x_angle, y_angle, z_angle)
        
        temp = np.dot(rot_mat, scatterers).transpose()
        temp[:, 2] += args.z0
        control_points[:, control_point_i, 0:3] = temp
           
    with h5py.File(args.h5_file, 'w') as f:
        f["spline_degree"] = args.spline_degree
        f["control_points"] = control_points
        f["amplitudes"] = np.array(as_, dtype="float32")
        f["knot_vector"] = np.array(knots, dtype="float32")
示例#3
0
def create_phantom(args):
    xs = np.random.uniform(size=(args.num_scatterers,), low=args.x_min, high=args.x_max)
    ys = np.random.uniform(size=(args.num_scatterers,), low=args.y_min, high=args.y_max)
    zs = np.random.uniform(size=(args.num_scatterers,), low=args.z_min, high=args.z_max)
    as_ = np.random.uniform(size=(args.num_scatterers,), low=0.0, high=1.0)
    
    scatterers = np.empty( (3, args.num_scatterers))
    scatterers[0, :] = xs
    scatterers[1, :] = ys
    scatterers[2, :] = zs
    
    control_points = np.empty( (args.num_scatterers, args.num_cs, 3), dtype='float32')
    
    knots = bsplines.uniform_regular_knot_vector(args.num_cs, args.spline_degree, t0=args.t0, t1=args.t1)
    knot_avgs = bsplines.control_points(args.spline_degree, knots)

    
    for control_point_i, t_star in enumerate(knot_avgs):    
        print 't=%f' % t_star
        x_angle = args.x_angular_velocity*t_star
        y_angle = args.y_angular_velocity*t_star
        z_angle = args.z_angular_velocity*t_star
        rot_mat = general_rot_mat(x_angle, y_angle, z_angle)
        
        temp = np.dot(rot_mat, scatterers).transpose()
        temp[:, 2] += args.z0
        control_points[:, control_point_i, 0:3] = temp
           
    with h5py.File(args.h5_file, 'w') as f:
        f["spline_degree"] = args.spline_degree
        f["control_points"] = control_points
        f["amplitudes"] = np.array(as_, dtype="float32")
        f["knot_vector"] = np.array(knots, dtype="float32")
示例#4
0
def create_phantom(args):    
    knot_vector = bsplines.uniform_regular_knot_vector(args.num_control_points,
                                                       args.spline_degree,
                                                       t0=args.t_start,
                                                       t1=args.t_end)
    knot_vector = np.array(knot_vector, dtype='float32')
    
    knot_avgs = bsplines.control_points(args.spline_degree, knot_vector)
    
    control_points = np.empty( (args.num_scatterers, args.num_control_points, 3), dtype='float32')        
    amplitudes = np.random.uniform(low=0.0, high=1.0, size=(args.num_scatterers,))
    for scatterer_i in range(args.num_scatterers):
        print 'Scatterer %d of %d' % (scatterer_i, args.num_scatterers)
        # generate random start point
        x0 = random.uniform(args.x_min, args.x_max)
        y0 = random.uniform(args.y_min, args.y_max)
        z0 = args.z0 + random.uniform(-0.5, 0.5)*args.thickness
        for control_pt_i, t_star in enumerate(knot_avgs):
            x = x0
            y = y0
            z = z0 + args.ampl*np.cos(2*np.pi*args.freq*t_star)
            control_points[scatterer_i, control_pt_i, :] = [x, y, z]                     
    
    with h5py.File(args.h5_file, 'w') as f:
        f["control_points"] = control_points
        f["amplitudes"] = np.array(amplitudes, dtype="float32")
        f["spline_degree"] = args.spline_degree
        f["file_format_version"] = "1"
        f["knot_vector"] = knot_vector

    print 'Scatterer dataset written to %s' % args.h5_file
    print 'knot vector: %s' % knot_vector
示例#5
0
def create_phantom(args):
    xs = np.array(np.random.uniform(size=(args.num_scatterers,), low=args.x_min, high=args.x_max))
    ys = np.zeros((args.num_scatterers,))
    zs = np.array(np.random.uniform(size=(args.num_scatterers,), low=args.z_min, high=args.z_max))
    ampls = np.array(np.random.uniform(size=(args.num_scatterers,), low=0.0, high=1.0))
    
    # indices to keep
    outside_inds = (zs-args.z0)**2 + xs**2 >= args.radius**2
    ampls[np.logical_not(outside_inds)] *= args.inside_ampl    
    
    num_tissue_scatterers = len(xs)
    
    plaque_xs = np.array(np.random.uniform(size=(args.num_plaque_scatterers,),
                         low=-args.plaque_radius, high=args.plaque_radius))
    plaque_ys = np.zeros((args.num_plaque_scatterers,))
    plaque_zs = np.array(np.random.uniform(size=(args.num_plaque_scatterers,),
                         low=-args.plaque_radius, high=args.plaque_radius))
    plaque_ampls = np.array(np.random.uniform(size=(args.num_plaque_scatterers,), low=0.0, high=1.0))
    
    plaque_ampls[plaque_xs**2 + plaque_zs**2 >= args.plaque_radius**2] = 0.0
    
    num_scatterers = num_tissue_scatterers + args.num_plaque_scatterers
    print 'Total number of scatterers: %d' % num_scatterers
    
    # knot vector for the approximation
    knot_vector = bsplines.uniform_regular_knot_vector(args.num_cs, args.spline_degree, t0=0.0, t1=1.0)
    knot_vector = np.array(knot_vector, dtype='float32')
    knot_avgs = bsplines.control_points(args.spline_degree, knot_vector)

    control_points = np.zeros( (num_scatterers, args.num_cs, 3), dtype='float32')
    amplitudes = np.zeros( (num_scatterers,), dtype="float32")
    
    for i in range(args.num_cs):
        theta = i*np.pi*2/args.num_cs
        control_points[:num_tissue_scatterers,i,0] = xs
        control_points[:num_tissue_scatterers,i,1] = ys
        control_points[:num_tissue_scatterers,i,2] = zs

        control_points[num_tissue_scatterers:,i,0] = plaque_xs + args.radius*np.sin(theta)
        control_points[num_tissue_scatterers:,i,1] = plaque_ys
        control_points[num_tissue_scatterers:,i,2] = plaque_zs + args.z0 + args.radius*np.cos(theta)

        amplitudes[num_tissue_scatterers:] = plaque_ampls
        amplitudes[:num_tissue_scatterers] = ampls
    
    with h5py.File(args.h5_file, 'w') as f:
        f["control_points"] = control_points
        f["amplitudes"] = amplitudes
        f["spline_degree"] = args.spline_degree
        f["knot_vector"] = knot_vector
def create_phantom(args):

    # Load scaling function
    start_time, end_time, scale_fn = load_scale_function(args.h5_scale)
    print('Loaded scale function on [%f, %f] sec.' % (start_time, end_time))

    # Generate scatterers in a box
    xs = np.random.uniform(low=-args.r0,
                           high=args.r0,
                           size=(args.num_scatterers, ))
    ys = np.random.uniform(low=-args.r0,
                           high=args.r0,
                           size=(args.num_scatterers, ))
    zs = np.random.uniform(low=0.0, high=args.z0, size=(args.num_scatterers, ))
    pts = list(zip(xs, ys, zs))

    # Discard scatterers outside of cylinder
    pts = [x_y_z for x_y_z in pts if x_y_z[0]**2 + x_y_z[1]**2 <= args.r0**2]
    xs, ys, zs = list(map(np.array, list(zip(*pts))))
    num_scatterers = len(xs)

    # Create random amplitudes
    amplitudes = np.random.uniform(low=0.0, high=1.0, size=(num_scatterers, ))

    # Create knot vector
    knots = bsplines.uniform_regular_knot_vector(args.num_control_points,
                                                 args.spline_degree,
                                                 t0=start_time,
                                                 t1=end_time)
    knot_avgs = bsplines.control_points(args.spline_degree, knots)

    control_points = np.empty((num_scatterers, args.num_control_points, 3),
                              dtype='float32')

    for c_i, t_star in enumerate(knot_avgs):
        alpha = scale_fn(t_star)
        control_points[:, c_i, 0] = xs / np.sqrt(alpha)
        control_points[:, c_i, 1] = ys / np.sqrt(alpha)
        control_points[:, c_i, 2] = zs * alpha

    with h5py.File(args.h5_out, 'w') as f:
        f["spline_degree"] = args.spline_degree
        f["control_points"] = control_points
        f["amplitudes"] = np.array(amplitudes, dtype="float32")
        f["knot_vector"] = np.array(knots, dtype='float32')
    print('Spline scatterer dataset written to %s' % args.h5_out)
def create_phantom(args):

    # Load scaling function
    start_time, end_time, scale_fn = load_scale_function(args.h5_scale)
    print 'Loaded scale function on [%f, %f] sec.' % (start_time, end_time)
    
    # Generate scatterers in a box
    xs = np.random.uniform(low=-args.r0, high=args.r0, size=(args.num_scatterers,))
    ys = np.random.uniform(low=-args.r0, high=args.r0, size=(args.num_scatterers,))
    zs = np.random.uniform(low=0.0, high=args.z0, size=(args.num_scatterers,))
    pts = zip(xs, ys, zs)

    # Discard scatterers outside of cylinder
    pts = filter(lambda (x,y,z): x**2+y**2 <= args.r0**2, pts)
    xs, ys, zs = map(np.array, zip(*pts))
    num_scatterers = len(xs)
    
    # Create random amplitudes
    amplitudes = np.random.uniform(low=0.0, high=1.0, size=(num_scatterers,))  
    
    # Create knot vector
    knots = bsplines.uniform_regular_knot_vector(args.num_control_points, args.spline_degree,
                                                 t0=start_time, t1=end_time)
    knot_avgs = bsplines.control_points(args.spline_degree, knots)
    
    control_points = np.empty((num_scatterers, args.num_control_points, 3), dtype='float32')

    for c_i, t_star in enumerate(knot_avgs):
        alpha = scale_fn(t_star)
        control_points[:, c_i, 0] = xs/np.sqrt(alpha)
        control_points[:, c_i, 1] = ys/np.sqrt(alpha)
        control_points[:, c_i, 2] = zs*alpha

    with h5py.File(args.h5_out, 'w') as f:
        f["spline_degree"] = args.spline_degree
        f["control_points"] = control_points
        f["amplitudes"] = np.array(amplitudes, dtype="float32")
        f["knot_vector"] = np.array(knots, dtype='float32')    
    print 'Spline scatterer dataset written to %s' % args.h5_out
示例#8
0
def create_phantom(args):
    lv_model = ThickCappedZEllipsoid(args.x_min, args.x_max, args.y_min,
                                     args.y_max, args.z_min, args.z_max,
                                     args.thickness, args.z_ratio)

    # Create the LV scatterers in box and remove the ones not
    # inside of thick myocardium.
    xs, ys, zs = generate_random_scatterers_in_box(args.x_min, args.x_max,
                                                   args.y_min, args.y_max,
                                                   args.z_min, args.z_max,
                                                   args.num_scatterers_in_box,
                                                   args.thickness)

    xs, ys, zs = remove_points_outside_of_interior(xs, ys, zs, lv_model)
    _as = np.random.uniform(low=0.0,
                            high=args.lv_max_amplitude,
                            size=(len(xs), ))
    assert (len(ys) == len(xs) and len(zs) == len(xs))
    num_scatterers = len(xs)

    print('Total number of scatterers: %d' % num_scatterers)

    # Decide which scaling function to use
    if args.scale_h5_file != None:
        start_time, end_time, scale_fn = load_scale_function(
            args.scale_h5_file)
        print('Loaded scaling function on [%f, %f]' % (start_time, end_time))
        print('Hacking args.t0 and args.t1')
        args.t0 = start_time
        args.t1 = end_time
    else:
        scale_fn = lambda t: default_scale_function(t, ampl=args.motion_ampl)

    ts = np.linspace(args.t0, args.t1, 1000)

    # knot vector for the approximation
    knot_vector = bsplines.uniform_regular_knot_vector(args.num_cs,
                                                       args.spline_degree,
                                                       t0=args.t0,
                                                       t1=args.t1)
    knot_vector = np.array(knot_vector, dtype='float32')
    knot_avgs = bsplines.control_points(args.spline_degree, knot_vector)

    # value in [0, 1] for the normalized z coordinate of each scatterer
    # will be used to control rotation amplitude.
    zs_fractional = (zs - args.z_min) / (args.z_max - args.z_min)

    control_points = np.zeros((num_scatterers, args.num_cs, 3),
                              dtype='float32')
    for cs_i, t_star in enumerate(knot_avgs):
        print('Computing control points for knot average %d of %d' %
              (cs_i + 1, args.num_cs))

        s = scale_fn(t_star)

        temp_in = np.vstack([s * xs, s * ys, s * zs])
        temp_out = np.empty((3, num_scatterers))
        for i in range(num_scatterers):
            cur_angle = zs_fractional[i] * s * args.rotation_scale
            temp_out[:, i] = rot_mat_z(cur_angle).dot(temp_in[:, i])

        # Compute control point position. Amplitude is unchanged.
        control_points[:, cs_i, 0] = temp_out[0, :]  #s*np.array(xs)
        control_points[:, cs_i, 1] = temp_out[1, :]  #s*np.array(ys)
        control_points[:, cs_i, 2] = temp_out[2, :]  #s*np.array(zs)

    # Write results to disk
    with h5py.File(args.h5_file) as f:
        f['spline_degree'] = args.spline_degree
        f['knot_vector'] = knot_vector
        f['control_points'] = control_points
        f['amplitudes'] = np.array(_as, dtype="float32")

    print('Spline scatterer dataset written to %s' % args.h5_file)
示例#9
0
def create_phantom(args):
    lv_model = ThickCappedZEllipsoid(args.x_min,
                                     args.x_max,
                                     args.y_min,
                                     args.y_max,
                                     args.z_min,
                                     args.z_max,
                                     args.thickness,
                                     args.z_ratio)

    # Create the LV scatterers in box and remove the ones not
    # inside of thick myocardium. 
    xs,ys,zs = generate_random_scatterers_in_box(args.x_min,
                                                 args.x_max,
                                                 args.y_min,
                                                 args.y_max,
                                                 args.z_min,
                                                 args.z_max,
                                                 args.num_scatterers_in_box,
                                                 args.thickness)
    
    xs,ys,zs = remove_points_outside_of_interior(xs, ys, zs, lv_model)
    _as = np.random.uniform(low=0.0, high=args.lv_max_amplitude, size=(len(xs),))
    assert(len(ys) == len(xs) and len(zs) == len(xs))
    num_scatterers = len(xs)
    
    print 'Total number of scatterers: %d' % num_scatterers

    # Decide which scaling function to use
    if args.scale_h5_file != None:
        start_time, end_time, scale_fn = load_scale_function(args.scale_h5_file)
        print 'Loaded scaling function on [%f, %f]' % (start_time, end_time)
        print 'Hacking args.t0 and args.t1'
        args.t0 = start_time
        args.t1 = end_time
    else:
        scale_fn = lambda t: default_scale_function(t, ampl=args.motion_ampl)
    
    
    ts = np.linspace(args.t0, args.t1, 1000)

    # knot vector for the approximation
    knot_vector = bsplines.uniform_regular_knot_vector(args.num_cs, args.spline_degree, t0=args.t0, t1=args.t1)
    knot_vector = np.array(knot_vector, dtype='float32')
    knot_avgs = bsplines.control_points(args.spline_degree, knot_vector)

    # value in [0, 1] for the normalized z coordinate of each scatterer
    # will be used to control rotation amplitude.
    zs_fractional = (zs-args.z_min)/(args.z_max-args.z_min)

    control_points = np.zeros( (num_scatterers, args.num_cs, 3), dtype='float32')
    for cs_i, t_star in enumerate(knot_avgs):
        print 'Computing control points for knot average %d of %d' % (cs_i+1, args.num_cs)

        s = scale_fn(t_star)

        temp_in  = np.vstack([s*xs, s*ys, s*zs])
        temp_out = np.empty((3, num_scatterers))
        for i in range(num_scatterers):
            cur_angle = zs_fractional[i]*s*args.rotation_scale
            temp_out[:, i] = rot_mat_z(cur_angle).dot(temp_in[:, i])
        
        # Compute control point position. Amplitude is unchanged.
        control_points[:,cs_i,0] = temp_out[0,:] #s*np.array(xs)
        control_points[:,cs_i,1] = temp_out[1,:] #s*np.array(ys)
        control_points[:,cs_i,2] = temp_out[2,:] #s*np.array(zs)

    
    # Write results to disk
    with h5py.File(args.h5_file) as f:
        f['spline_degree'] = args.spline_degree
        f['knot_vector'] = knot_vector
        f['control_points'] = control_points
        f['amplitudes'] = np.array(_as, dtype="float32")
    
    print 'Spline scatterer dataset written to %s' % args.h5_file
示例#10
0
def create_phantom(args):
    xs = np.array(
        np.random.uniform(size=(args.num_scatterers, ),
                          low=args.x_min,
                          high=args.x_max))
    ys = np.zeros((args.num_scatterers, ))
    zs = np.array(
        np.random.uniform(size=(args.num_scatterers, ),
                          low=args.z_min,
                          high=args.z_max))
    ampls = np.array(
        np.random.uniform(size=(args.num_scatterers, ), low=0.0, high=1.0))

    # indices to keep
    outside_inds = (zs - args.z0)**2 + xs**2 >= args.radius**2
    ampls[np.logical_not(outside_inds)] *= args.inside_ampl

    num_tissue_scatterers = len(xs)

    plaque_xs = np.array(
        np.random.uniform(size=(args.num_plaque_scatterers, ),
                          low=-args.plaque_radius,
                          high=args.plaque_radius))
    plaque_ys = np.zeros((args.num_plaque_scatterers, ))
    plaque_zs = np.array(
        np.random.uniform(size=(args.num_plaque_scatterers, ),
                          low=-args.plaque_radius,
                          high=args.plaque_radius))
    plaque_ampls = np.array(
        np.random.uniform(size=(args.num_plaque_scatterers, ),
                          low=0.0,
                          high=1.0))

    plaque_ampls[plaque_xs**2 + plaque_zs**2 >= args.plaque_radius**2] = 0.0

    num_scatterers = num_tissue_scatterers + args.num_plaque_scatterers
    print 'Total number of scatterers: %d' % num_scatterers

    # knot vector for the approximation
    knot_vector = bsplines.uniform_regular_knot_vector(args.num_cs,
                                                       args.spline_degree,
                                                       t0=0.0,
                                                       t1=1.0)
    knot_vector = np.array(knot_vector, dtype='float32')
    knot_avgs = bsplines.control_points(args.spline_degree, knot_vector)

    control_points = np.zeros((num_scatterers, args.num_cs, 3),
                              dtype='float32')
    amplitudes = np.zeros((num_scatterers, ), dtype="float32")

    for i in range(args.num_cs):
        theta = i * np.pi * 2 / args.num_cs
        control_points[:num_tissue_scatterers, i, 0] = xs
        control_points[:num_tissue_scatterers, i, 1] = ys
        control_points[:num_tissue_scatterers, i, 2] = zs

        control_points[num_tissue_scatterers:, i,
                       0] = plaque_xs + args.radius * np.sin(theta)
        control_points[num_tissue_scatterers:, i, 1] = plaque_ys
        control_points[num_tissue_scatterers:, i,
                       2] = plaque_zs + args.z0 + args.radius * np.cos(theta)

        amplitudes[num_tissue_scatterers:] = plaque_ampls
        amplitudes[:num_tissue_scatterers] = ampls

    with h5py.File(args.h5_file, 'w') as f:
        f["control_points"] = control_points
        f["amplitudes"] = amplitudes
        f["spline_degree"] = args.spline_degree
        f["knot_vector"] = knot_vector