def export_node_subset(node_subset, node_loc, group_name, filename):
    node_subset.sort()
    subset_of_nodes = np.zeros((len(node_subset), 4))
    i = 0
    for node in node_subset:
        subset_of_nodes[i][0] = node_loc[node][0]
        subset_of_nodes[i][1] = node_loc[node][1]
        subset_of_nodes[i][2] = node_loc[node][2]
        subset_of_nodes[i][3] = node_loc[node][3]
        i = i + 1
    pg.export_ex_coords(subset_of_nodes, group_name, filename, 'exnode')

    return 0
def export_datapoint_subset(datapoint_subset, data_points, group_name,
                            filename):
    datapoint_subset.sort()
    subset_of_points = np.zeros((len(datapoint_subset), 4))
    i = 0
    for point in datapoint_subset:
        subset_of_points[i][0] = data_points[point][0]
        subset_of_points[i][1] = data_points[point][1]
        subset_of_points[i][2] = data_points[point][2]
        subset_of_points[i][3] = data_points[point][3]
        i = i + 1
    pg.export_ex_coords(subset_of_points, group_name, filename, 'exdata')

    return 0
#df = pd.DataFrame(old_to_new_elem)
#df.to_csv('old_to_new_elem_500.csv')

version = '_v8'

new_node_loc = np.zeros((num_nodes - len(nodes_to_remove), 4))
for i in range(0, num_nodes):
    new_node = old_to_new_node[i]
    if (new_node >= 0):
        new_node_loc[new_node][0] = new_node
        new_node_loc[new_node][1] = node_loc[i][1]
        new_node_loc[new_node][2] = node_loc[i][2]
        new_node_loc[new_node][3] = node_loc[i][3]
#write the new node file
name = 'new_nodes' + version
pg.export_ex_coords(new_node_loc, name, name, 'exnode')

new_elems = np.zeros((num_elems - len(elems_to_remove), 3), dtype=int)

for i in range(0, num_elems):
    new_elem = old_to_new_elem[i]
    if (new_elem >= 0):
        new_elems[new_elem][0] = new_elem
        new_elems[new_elem][1] = old_to_new_node[elems[i][1]]
        if (old_to_new_node[elems[i][1]] == -1):
            print('elem', i, 'new elem', new_elem, 'old node', elems[i][1])
        new_elems[new_elem][2] = old_to_new_node[elems[i][2]]
        if (old_to_new_node[elems[i][2]] == -1):
            print('elem', i, 'new elem', new_elem, 'old node', elems[i][2])
#write the new element file
name = 'new_elems' + version
total_start = time.time()

#Read in exnode and exelem files desrcibing whole placenta tree
nodes_input=pg.import_exnode_tree(node_file)
elems_input=pg.import_exelem_tree(el_file)

#Define element radii by Strahler order
elem_radius = pg.define_radius_by_order(nodes_input['nodes'],elems_input['elems'],'strahler',0,start_radius,strahler_order)
pg.export_exfield_1d_linear(elem_radius, 'arteries', 'radius', 'artery_radius')

#Calculate location of terminal branches
terminal_branches = pg.calc_terminal_branch(nodes_input['nodes'],elems_input['elems'])

#define rectangular mesh
rectangular_mesh = pg.gen_rectangular_mesh(volume, thickness, ellipticity,spacing,spacing,spacing)
pg.export_ex_coords(rectangular_mesh['nodes'],'samplingmesh','sampling_mesh','exnode')
pg.export_exelem_3d_linear(rectangular_mesh['elems'],'samplingmesh','sampling_mesh')

start = time.time()
#define the placental volume in each element of the sampling grid
placental_volume= pg.ellipse_volume_to_grid(rectangular_mesh, volume, thickness, ellipticity,20)
pg.export_exelem_3d_linear_list(rectangular_mesh['elems'], placental_volume['non_empty_rects'],'samplingmesh','sm_nonempty')
end = time.time()

#Locate terminals in sampling grid
start = time.time()
terminals_in_grid = pg.terminals_in_sampling_grid_fast(rectangular_mesh,terminal_branches,nodes_input['nodes'])
end = time.time()
 
print ('Total time for terminals in grid = '+ str((end-start)/60.0) + ' mins')
pg.export_exfield_3d_linear(terminals_in_grid['terminals_in_grid'],'samplingmesh','terminal_no','terminal_no')
Example #5
0
    node_loc[i][2] = node_file[0][n]

    i = i + 1

i = 0
for n in range(9, len(node_file), 4):
    node_loc[i][3] = node_file[0][n]
    i = i + 1

offset = i
print('offset', offset)
i = offset
for n in range(7, len(node_file_sv), 4):
    node_loc[i][0] = i
    node_loc[i][1] = node_file_sv[0][n]
    i = i + 1

i = offset
for n in range(8, len(node_file_sv), 4):
    node_loc[i][2] = node_file_sv[0][n]

    i = i + 1

i = offset
for n in range(9, len(node_file_sv), 4):
    node_loc[i][3] = node_file_sv[0][n]
    i = i + 1

#write the new node file
pg.export_ex_coords(node_loc, 'combined_nodes_v1',
                    'chorionic_vessels/combined_nodes_v1', 'exnode')
Example #6
0
#df1 = pd.DataFrame(np.array(chorion_and_stem['nodes']).reshape(len(chorion_and_stem['nodes']),4))
#df1.to_csv('chorion_and_stem.csv')

#populate element connectivity
elem_connectivity = pg.element_connectivity_1D(chorion_and_stem['nodes'],chorion_and_stem['elems'])
chorion_and_stem['elem_up'] = elem_connectivity['elem_up']
chorion_and_stem['elem_down'] = elem_connectivity['elem_down']

if (seed_points_option== 'generate'):
    # Define data points that represent the density of villous tissue, equispaced within an ellipsoidal geometry
    datapoints_villi=pg.equispaced_data_in_ellipsoid(n_seed,volume,thickness,ellipticity)
else:
    #read in seed points filling the volume of the placenta based on images
    datapoints_villi = pg.import_exnode_tree(seed_points_in_file)['nodes'][:, 1:4]

if(export_intermediates):
    export_file = export_directory + '/villous_data'
    pg.export_ex_coords(datapoints_villi,'villous',export_file,'exdata')

#Now grow a tree to these data points, optimised for larger trees
full_geom=pg.grow_large_tree(angle_max_ft, angle_min_ft, fraction_ft, min_length_ft, point_limit_ft, volume, thickness, ellipticity, datapoints_villi, chorion_and_stem)

# Export the final results
if(export_results or export_intermediates):
    export_file = export_directory + '/full_tree'
    pg.export_ex_coords(full_geom['nodes'],'placenta', export_file,'exnode')
    pg.export_exelem_1d(full_geom['elems'],'placenta', export_file)
    export_file = export_directory + '/terminals'
    pg.export_ex_coords(full_geom['term_loc'],'villous',export_file,'exdata')

get_tree_stats(export_directory)
Example #7
0
angle_min = 5 * np.pi / 180
fraction_chorion = 0.5
min_length = 5.0  #mm
point_limit = 1

sv_length = 2.0

angle_max_ft = 180 * np.pi / 180
angle_min_ft = 0 * np.pi / 180
fraction_ft = 0.4
min_length_ft = 0.0  #mm
point_limit_ft = 1

datapoints_chorion = pg.uniform_data_on_ellipsoid(n_chorion, volume, thickness,
                                                  ellipticity, 0)
pg.export_ex_coords(datapoints_chorion, 'chorion', 'chorion_data', 'exdata')

seed_geom = pg.umbilical_seed_geometry(volume, thickness, ellipticity,
                                       cord_insertion_x, cord_insertion_y,
                                       umb_artery_distance, umb_artery_length,
                                       datapoints_chorion)
pg.export_ex_coords(seed_geom['umb_nodes'], 'umb', 'umb_nodes', 'exnode')
pg.export_exelem_1d(seed_geom['umb_elems'], 'umb', 'umb_elems')

chorion_geom = pg.grow_chorionic_surface(angle_max, angle_min,
                                         fraction_chorion, min_length,
                                         point_limit, volume, thickness,
                                         ellipticity, datapoints_chorion,
                                         seed_geom, 'surface')

pg.export_ex_coords(chorion_geom['nodes'], 'chorion', 'chorion_nodes',
Example #8
0
node_info = pd.read_csv(node_in_file)

nodes = node_info[['comx', 'comy', 'comz']].copy()
nodes.columns = ['y', 'x','z'] #swap x and y coordinates, so that they're the same as imagej skeleton
num_nodes = len(nodes)

#populate nodes array
node_loc = np.zeros((num_nodes, 4))
for i in range(0,num_nodes):
    node_loc[i][0] = i
    node_loc[i][1] = nodes.iloc[i]['x']
    node_loc[i][2] = nodes.iloc[i]['y']
    node_loc[i][3] = nodes.iloc[i]['z']

#write the exnode file
pg.export_ex_coords(node_loc,group_name,node_out_file,'exnode')

#populate elems array
element_info = pd.read_csv(elems_in_file)
elements = element_info[['n1','n2']].copy()
elements.columns = ['n1', 'n2']
num_elems = len(elements)

elems = np.zeros((num_elems, 3), dtype=int)

for i in range(0,num_elems):
    elems[i][0] = i  # creating new element;
    #export_exelem_1d adds 1 to all values
    elems[i][1] = elements.iloc[i]['n1'] -1  # starts at this node
    elems[i][2] = elements.iloc[i]['n2'] -1  # ends at this node
unique_nodes = node_coordinates.drop_duplicates()
num_nodes = len(unique_nodes)
nodes = unique_nodes.sort_values(['x', 'y', 'z'], inplace=True)
#update indices in the sorted dataframe
nodes.index = range(0, num_nodes)

#populate nodes array
node_loc = np.zeros((num_nodes, 4))
for i in range(0, num_nodes):
    node_loc[i][0] = i
    node_loc[i][1] = nodes.iloc[i]['x']
    node_loc[i][2] = nodes.iloc[i]['y']
    node_loc[i][3] = nodes.iloc[i]['z']

#write the exnode file
pg.export_ex_coords(node_loc, 'p51_large_vessels_v2', 'p51_large_vessels_v2',
                    'exnode')

#populate elems array
num_elems = len(branch_info)
elems = np.zeros((num_elems, 3), dtype=int)

for i in range(0, num_elems):
    #get indices for node 1 and 2
    node1_index = nodes.index[(nodes['x'] == branch_info.iloc[i]['V1 x'])
                              & (nodes['y'] == branch_info.iloc[i]['V1 y']) &
                              (nodes['z'] == branch_info.iloc[i]['V1 z'])]
    node2_index = nodes.index[(nodes['x'] == branch_info.iloc[i]['V2 x'])
                              & (nodes['y'] == branch_info.iloc[i]['V2 y']) &
                              (nodes['z'] == branch_info.iloc[i]['V2 z'])]

    elems[i][0] = i  # creating new element
    node_loc[i][0] = i
    node_loc[i][1] = node_file[0][n]
    i=i+1

i=0
for n in range(8,len(node_file),4):
    node_loc[i][2] = node_file[0][n]
    i=i+1

i=0
for n in range(9,len(node_file),4):
    node_loc[i][3] = node_file[0][n]
    i=i+1

#write the exnode file
pg.export_ex_coords(node_loc,'chor_nodes_cycle3_v3','chorionic_vessels/chor_nodes_cycle3_v3','exnode')


#read the element file
element_file = pd.read_csv('chorionic_vessels/chor_elems_cycle3_v2.exelem',sep="\n", header=None)

num_elems = (len(element_file)-31)/5
elems = np.zeros((num_elems, 3), dtype=int)

i=0
for n in range(33, len(element_file),5):
    elems[i][0] = i  # creating new element
    nodes = element_file[0][n].split()
    elems[i][1] = old_to_new_node[int(nodes[0])]  # starts at this node
    elems[i][2] = old_to_new_node[int(nodes[1])]  # ends at this node
    i = i+1
#!/usr/bin/env python

import placentagen as pg
import numpy as np
import pandas as pd
from os.path import expanduser
home = expanduser("~")

#parameters
point_cloud_in_file = home + '/placenta_patient_49/seed_points/p49_seed_points_heterogeneous_5_10_17.txt'
point_cloud_out_file = home + '/placenta_patient_49/seed_points/p49_seed_points_heterogeneous_5_10_17'
group_name = 'p49_seed_points_heterogeneous_5_10_17'

point_cloud = pd.read_csv(point_cloud_in_file, sep="\t", header=None)
point_cloud.columns = ['x', 'y', 'z']
num_nodes = len(point_cloud)

#populate nodes array
node_loc = np.zeros((num_nodes, 4))
for i in range(0, num_nodes):
    node_loc[i][0] = i
    node_loc[i][1] = point_cloud.iloc[i]['x']
    node_loc[i][2] = point_cloud.iloc[i]['y']
    node_loc[i][3] = point_cloud.iloc[i]['z']

#write the exnode file
pg.export_ex_coords(node_loc, group_name, point_cloud_out_file, 'exdata')
Example #12
0
dv_bc_value = 1  # cm per sec outlet vel of decidual vein

sa_bc_type = 'velocity'  # or pressure
sa_bc_value = 10  # cm per sec inlet vel of spiral artery

wall_bc_type = 'no_slip'  # or no_penetration

debug = False
# Generate an ellipodial mesh for simulation, w
pl_mesh = pg.gen_half_ellipsoid_structured(size_el, volume, thickness,
                                           ellipticity, squareSizeRatio,
                                           circle_prop, el_type, debug)

# Optional - export mesh prior to solution
if (export_mesh):
    pg.export_ex_coords(pl_mesh['nodes'], 'placenta', filename_mesh, 'exnode')
    if (el_type == 1):
        pg.export_exelem_3d_linear(pl_mesh['placental_el_con'], 'placenta',
                                   filename_mesh)  # Use this for linear
    elif (el_type == 2):
        pg.export_exelem_3d_quadratic(pl_mesh['elems'], 'placenta',
                                      filename_mesh)  # Use this for quadratic
    else:
        print(
            'Cannot export elements for visualisation as element type does not exist'
        )

numberOfDimensions = 3
numberOfComponents = 1

# Material properties of this example.....
                           geom['radii'], geom['branch_angles'],
                           geom['diam_ratio'], geom['length_ratio'])

#3d plots
print('Plotting')
plotVasculature3D(geom['nodes'], geom['elems'], geom['branch_angles'],
                  geom['radii'])

#csv files
output = 0
if output:
    print('Writing files')
    elems = geom['elems']
    outPutData = np.column_stack(
        [elems[:, 1:3], geom['radii'], geom['branch_angles'], strahler])

    np.savetxt('ElementInfo.csv',
               outPutData,
               fmt='%.2f',
               delimiter=',',
               header=" ,elems,  radii(mm),  angles(degrees),  order")
    np.savetxt('NodeInfo.csv',
               geom['nodes'],
               fmt='%.4f',
               delimiter=',',
               header=" ,nodes(voxels)")

    #cmgui files
    pg.export_ex_coords(geom['nodes'], 'placenta', 'full_tree', 'exnode')
    pg.export_exelem_1d(geom['elems'], 'placenta', 'full_tree')