Ejemplo n.º 1
0
def make_pressure_color_arrays(vesselgraph):
    edges = vesselgraph.edgelist
    flags = vesselgraph.edges['flags']
    data = vesselgraph.nodes['pressure']
    num_nodes = len(vesselgraph.nodes['position'])
    nflags = krebsutils.edge_to_node_property(num_nodes, edges, flags, 'or')

    is_set = lambda flags_, flag: np.asarray(np.bitwise_and(flags_, flag), np.
                                             bool)

    gray = np.asarray((0.1, 0.1, 0.1))
    lgray = np.asarray((0.5, 0.5, 0.5))

    circulated = is_set(flags, krebsutils.CIRCULATED)
    ncirculated = is_set(nflags, krebsutils.CIRCULATED)
    capillary = is_set(flags, krebsutils.CAPILLARY)
    ncapillary = is_set(nflags, krebsutils.CAPILLARY)
    p0 = np.amin(data[ncirculated])
    p1 = np.amax(data[ncirculated])
    cm = matplotlib.cm.ScalarMappable(cmap=cm_redblue)
    cm.set_clim(p0, p1)

    edgedata = np.average((data[edges[:, 0]], data[edges[:, 1]]), axis=0)
    edgecolors = cm.to_rgba(edgedata)[:, :3]
    edgecolors[~circulated] = gray
    #edgecolors[capillary] = lgray

    nodecolors = cm.to_rgba(data)[:, :3]
    nodecolors[~ncirculated] = gray
    #nodecolors[ncapillary] = lgray

    vesselgraph.edges['colors'] = edgecolors
    vesselgraph.nodes['colors'] = nodecolors
Ejemplo n.º 2
0
def AddConductivityBoundaryConditions(vessels, vesselgroup, avgConductivity):
    print '---- resistor BCs to %s -----' % str(vesselgroup)
    fudgeFactor = 100.
    rootNodes = vessels.roots
    pressures = vessels['pressure']
    numNodes = len(pressures)
    nodeFlags = krebsutils.edge_to_node_property(numNodes, vessels.edgelist,
                                                 vessels['flags'], 'or')
    nodeRadi = krebsutils.edge_to_node_property(numNodes, vessels.edgelist,
                                                vessels['radius'], 'avg')
    minp = np.amin(pressures)
    # storage area for h5 datasets
    dataArrays = []
    # use radius^4 weighted average conductivity
    weights = dict()
    for rootNode in rootNodes:
        weights[rootNode] = nodeRadi[rootNode]**4
        assert nodeRadi[rootNode] > 10. or pressures[
            rootNode] > minp * 1.1 or (nodeFlags[rootNode] & krebsutils.VEIN)
    weightSum = np.sum(weights.values())
    for rootNode in rootNodes:
        weight = weights[rootNode] / weightSum
        #weight = 1
        conductivity = fudgeFactor * avgConductivity * weight
        dataArrays.append((rootNode, krebsutils.FLOWBC_RESIST,
                           pressures[rootNode], conductivity))
        print 'node %i -> S=%f, w=%f' % (rootNode, conductivity, weight)
    # write to h5
    dataArrays = zip(*dataArrays)
    names = ['bc_node_index', 'bc_type', 'bc_value', 'bc_conductivity_value']
    gnodes = vesselgroup['nodes']
    for name, values in zip(names, dataArrays):
        if name in gnodes:
            gnodes[name][...] = values
        else:
            gnodes.create_dataset(name, data=values)
Ejemplo n.º 3
0
def generate_samples(graph, name, association, scale):
    DATA_LINEAR = krebsutils.VesselSamplingFlags.DATA_LINEAR
    DATA_CONST = krebsutils.VesselSamplingFlags.DATA_CONST
    DATA_PER_NODE = krebsutils.VesselSamplingFlags.DATA_PER_NODE
    if not len(graph.edgelist):
        return np.asarray([], dtype=float)
    if association == 'edges':
        data = krebsutils.edge_to_node_property(
            int(np.amax(graph.edgelist) + 1), graph.edgelist,
            graph.edges[name], 'avg')
    else:
        data = graph.nodes[name]


#  return data
    return krebsutils.sample_edges(graph.nodes['position'], graph.edgelist,
                                   data, scale, DATA_LINEAR | DATA_PER_NODE)
Ejemplo n.º 4
0
def make_any_color_arrays(vesselgraph, data_name):
    edges = vesselgraph.edgelist
    num_nodes = len(vesselgraph.nodes['position'])
    flags = vesselgraph.edges['flags']
    flags = np.asarray(flags, dtype='uint32')
    nflags = krebsutils.edge_to_node_property(num_nodes, edges, flags, 'or')

    mask = myutils.bbitwise_and(flags, krebsutils.CIRCULATED)
    nmask = myutils.bbitwise_and(nflags, krebsutils.CIRCULATED)

    if data_name in vesselgraph.edges:
        edgedata = vesselgraph.edges[data_name]
        nodedata = krebsutils.edge_to_node_property(num_nodes, edges, edgedata,
                                                    'avg')
    else:
        nodedata = vesselgraph.nodes[data_name]
        edgedata = np.average((nodedata[edges[:, 0]], nodedata[edges[:, 1]]),
                              axis=0)

    gray = np.asarray((0.1, 0.1, 0.1))
    edgecolors = np.repeat(gray.reshape(1, -1), len(edgedata), axis=0)
    nodecolors = np.repeat(gray.reshape(1, -1), len(nodedata), axis=0)
    #colors = lambda arr: cm.to_rgba(arr)[:,:3]
    colors = lambda arr: np.power(cm.to_rgba(arr)[:, :3], 2.4)

    if data_name == 'hematocrit':
        cm = matplotlib.cm.ScalarMappable(cmap=cm_hematocrit)
        cm.set_clim(0, 1)
        unmapped_range = (0., 1.)
        edgecolors[mask] = colors(edgedata[mask])
        nodecolors[nmask] = colors(nodedata[nmask])
    elif data_name == 'pressure':
        #this looks really ugly if there is a zero pressure node
        #p0 = np.amin(nodedata)
        p0 = np.min(nodedata[np.nonzero(nodedata)])
        p1 = np.amax(nodedata)
        unmapped_range = (p0, p1)
        cm = matplotlib.cm.ScalarMappable(cmap=cm_redblue)
        cm.set_clim(p0, p1)
        edgecolors[mask] = colors(edgedata[mask])
        nodecolors[nmask] = colors(nodedata[nmask])
    elif data_name == 'shearforce':
        mask = mask & (edgedata > 0)
        nmask = nmask & (nodedata > 0)
        edgedata = edgedata[mask]
        nodedata = nodedata[nmask]
        unmapped_range = edgedata.min(), edgedata.max()
        edgedata = np.log10(edgedata)
        nodedata = np.log10(nodedata)
        p0 = -4  #np.amin(edgedata)
        p1 = -1  #np.amax(edgedata)
        cm = matplotlib.cm.ScalarMappable(cmap=matplotlib.cm.spectral)
        cm.set_clim(p0, p1)
        edgecolors[mask] = colors(edgedata)
        nodecolors[nmask] = colors(nodedata)
    elif data_name == 'S_tot':
        #mask = mask & (edgedata>0)
        #nmask = nmask & (nodedata>0)
        edgedata = edgedata[mask]
        nodedata = nodedata[nmask]
        unmapped_range = edgedata.min(), edgedata.max()
        p0 = np.amin(edgedata)
        p1 = np.amax(edgedata)
        #print("p0: %f, p1: %f" % (p0,p1))
        #unmapped_range = (p0, p1)
        cm = matplotlib.cm.ScalarMappable(cmap=matplotlib.cm.jet)
        cm.set_clim(p0, p1)
        #edgedata = np.log10(edgedata)
        #nodedata = np.log10(nodedata)
        #p0 = -4#np.amin(edgedata)
        #p1 = -1#np.amax(edgedata)
        #cm = matplotlib.cm.ScalarMappable(cmap=matplotlib.cm.spectral)
        #cm.set_clim(p0, p1)
        edgecolors[mask] = colors(edgedata)
        nodecolors[nmask] = colors(nodedata)
    elif data_name == 'flow':
        mask = mask & (edgedata > 0)
        nmask = nmask & (nodedata > 0)
        edgedata = edgedata[mask]
        nodedata = nodedata[nmask]
        unmapped_range = edgedata.min(), edgedata.max()
        edgedata = np.log10(edgedata)
        nodedata = np.log10(nodedata)
        p0 = np.floor(np.amin(edgedata))
        p1 = np.ceil(np.amax(edgedata))
        cm = matplotlib.cm.ScalarMappable(cmap=matplotlib.cm.jet)
        cm.set_clim(p0, p1)
        edgecolors[mask] = colors(edgedata)
        nodecolors[nmask] = colors(nodedata)
    elif data_name == 'conductivitySignal':
        edgedata = edgedata[mask]
        nodedata = nodedata[nmask]
        unmapped_range = edgedata.min(), edgedata.max()
        p0 = np.amin(edgedata)
        p1 = np.amax(edgedata)
        cm = matplotlib.cm.ScalarMappable(cmap=matplotlib.cm.jet)
        cm.set_clim(p0, p1)
        edgecolors[mask] = colors(edgedata)
        nodecolors[nmask] = colors(nodedata)
    elif data_name == 'metabolicSignal':
        edgedata = edgedata[mask]
        nodedata = nodedata[nmask]
        unmapped_range = edgedata.min(), edgedata.max()
        p0 = np.amin(edgedata)
        p1 = np.amax(edgedata)
        cm = matplotlib.cm.ScalarMappable(cmap=matplotlib.cm.jet)
        cm.set_clim(p0, p1)
        edgecolors[mask] = colors(edgedata)
        nodecolors[nmask] = colors(nodedata)
    elif data_name == 'flags':
        edgecolors[mask
                   & (flags & krebsutils.ARTERY).astype(np.bool)] = np.asarray(
                       (1., 0., 0.))
        nodecolors[nmask & (nflags
                            & krebsutils.ARTERY).astype(np.bool)] = np.asarray(
                                (1., 0., 0.))
        edgecolors[mask
                   & (flags & krebsutils.VEIN).astype(np.bool)] = np.asarray(
                       (0., 0., 1.))
        nodecolors[nmask
                   & (nflags & krebsutils.VEIN).astype(np.bool)] = np.asarray(
                       (0., 0., 1.))
        edgecolors[mask
                   & (flags
                      & krebsutils.CAPILLARY).astype(np.bool)] = np.asarray(
                          (0., 1., 0.))
        nodecolors[nmask
                   & (nflags
                      & krebsutils.CAPILLARY).astype(np.bool)] = np.asarray(
                          (0., 1., 0.))
        for idx in vesselgraph.roots:
            nodecolors[idx] = np.asarray((1., 1., 0.))
        cm, unmapped_range = None, (None, None)
    vesselgraph.edges['colors'] = edgecolors
    vesselgraph.nodes['colors'] = nodecolors
    return cm, unmapped_range
def InsertGraphColors(vesselgraph, po2field, data_name):
    edges = vesselgraph.edgelist
    num_nodes = len(vesselgraph.nodes['position'])

    if data_name in vesselgraph.edges:
        edgedata = data = vesselgraph.edges[data_name]
        nodedata = krebsutils.edge_to_node_property(num_nodes, edges, data,
                                                    'avg')
    else:
        nodedata = data = vesselgraph.nodes[data_name]
        edgedata = np.average((data[edges[:, 0]], data[edges[:, 1]]), axis=0)

    if data_name == 'po2vessels':
        try:
            p1 = np.amax(data)
        except ValueError:
            print("p1 not found")
            pass
        if po2field is not None:
            p1 = max(p1, np.amax(po2field))
        try:
            p0 = np.amin(data)
        except ValueError:
            print("p0 not found")
            pass
        if po2field is not None:
            p0 = min(p0, np.amin(po2field))
        #p1 = math.ceil(p1/10.0)*10.0  # round to powers of something
        #p1 = 100.0
        value_range = (p0, p1)
        cm = matplotlib.cm.ScalarMappable(cmap=cm_po2)
    elif data_name == 'saturation':
        cm = matplotlib.cm.ScalarMappable(cmap=matplotlib.cm.spectral)
        vesselgraph.edges['saturation']
        value_range = (np.min(vesselgraph.edges['saturation']),
                       np.max(vesselgraph.edges['saturation']))
        #value_range = (0,1.)
    elif data_name == 'hboconc':
        cm = matplotlib.cm.ScalarMappable(cmap=matplotlib.cm.gnuplot)
        p1 = math.ceil(np.amax(data))
        value_range = (0., p1)
    cm.set_clim(*value_range)

    colors = lambda arr: np.power(cm.to_rgba(arr)[:, :3], 2.4)
    if data_name in vesselgraph.edges:
        edgecolors = colors(data)
        nodecolors = colors(nodedata)
    else:
        edgecolors = colors(edgedata)
        nodecolors = colors(data)

    flags = vesselgraph.edges['flags']
    nflags = krebsutils.edge_to_node_property(num_nodes, edges, flags, 'or')
    is_not_set = lambda flags_, flag: np.bitwise_not(
        np.asarray(np.bitwise_and(flags_, flag), np.bool))
    gray = np.asarray((0.3, 0.3, 0.3))
    uncirculated = is_not_set(flags, krebsutils.CIRCULATED)
    nuncirculated = is_not_set(nflags, krebsutils.CIRCULATED)
    edgecolors[uncirculated] = gray
    nodecolors[nuncirculated] = gray

    print 'colormap range ', cm.get_clim()

    vesselgraph.edges['colors'] = edgecolors
    vesselgraph.nodes['colors'] = nodecolors
    return cm
Ejemplo n.º 6
0
def InsertGraphColors(vesselgraph, ifffield, data_name, automatic_scale=False, max_conc=1.0):
  edges = vesselgraph.edgelist
  num_nodes = len(vesselgraph.nodes['position'])

#  if data_name in vesselgraph.edges:
#    edgedata = data = vesselgraph.edges[data_name]
#    nodedata = krebsutils.edge_to_node_property(num_nodes, edges, data, 'avg')
#  else:
#    nodedata = data = vesselgraph.nodes[data_name]
#    edgedata = np.average((data[edges[:,0]], data[edges[:,1]]), axis=0)
  #note everything is uncirculated anyway
  #edgedata = data = vesselgraph.edges['radius']
  #nodedata = krebsutils.edge_to_node_property(num_nodes, edges, data, 'avg')

  if data_name == 'auc':
    #p1 = np.amax(data)
    #if po2field is not None:
    #  p1 = max(p1, np.amax(po2field))
    #p1 = math.ceil(p1/10.0)*10.0  # round to powers of something
    
    #p1 = 100.0
    #value_range = (np.amin(ifffield), np.amax(ifffield))
    color_norm = matplotlib.colors.LogNorm().autoscale(ifffield)
    cm = matplotlib.cm.ScalarMappable(cmap = 'Reds', norm=color_norm)
    #value_range = (0, 37000)
    value_range = (np.amin(ifffield), np.amax(ifffield))
    if value_range[0]==0 and value_range[1] == 0:
      value_range = (-1,1)
    #cm = matplotlib.cm.ScalarMappable(cmap = 'Reds')
    cm.set_clim(*value_range)
    #cm = matplotlib.colors.LinearSegmentedColormap.from_list('my_cmap',[(0,0,0,0),'red'],256)
    #cm = plt.get_cmap('Reds')
  elif data_name == 'iff_pressure':
    cm = matplotlib.cm.ScalarMappable(cmap = matplotlib.cm.spectral)
    value_range = (np.amin(ifffield), np.amax(ifffield))
    cm.set_clim(*value_range)
  elif data_name == 'drug_conc':
    cm = matplotlib.cm.ScalarMappable(cmap = matplotlib.cm.Spectral, norm=matplotlib.colors.LogNorm())
    if automatic_scale:
      value_range = (np.amin(ifffield), np.amax(ifffield))
    else:
      value_range= (1e-3,0.5e0)
    # new global variable
    if not max_conc == 1.0:
      value_range = (1e-3, max_conc)
    else:
      value_range = (np.amin(ifffield), np.amax(ifffield))
    print(value_range)
    if value_range[0]==0 and value_range[1] == 0:
      value_range = (1,2)
    #value_range = (0, 0.42)
    #color_norm = matplotlib.colors.LogNorm(*value_range)
    #color_norm = matplotlib.colors.LogNorm().autoscale(ifffield)
    #cm = matplotlib.cm.ScalarMappable(cmap = 'Reds', norm=color_norm)
    #cm = matplotlib.cm.ScalarMappable(cmap = 'Reds', norm=color_norm)
    #if np.amin(ifffield) <0:
    #  value_range = (0, np.amax(ifffield))
    #else:
    #value_range = (np.amin(ifffield), np.amax(ifffield))
    #value_range = (1e-7, 0.5)
    
    #value_range = (0, 0.42)
    
    #value_range = (0.01, 0.3)
    cm.set_clim(*value_range)
    #cm.autoscale(ifffield)
    #cm.LogNorm()
  else:
    cm = matplotlib.cm.ScalarMappable(cmap = matplotlib.cm.gnuplot)
    p1 = math.ceil(np.amax(data))
    value_range = (0., p1)
    
  

  #colors = lambda arr: np.power(cm.to_rgba(arr)[:,:3], 2.4)
  edgedata = vesselgraph.edges['radius']
  nodedata = krebsutils.edge_to_node_property(num_nodes, edges, edgedata, 'avg')
  
  if 1:
    print('custom cm:')
    mycm = matplotlib.colors.Colormap('Reds')
    
    theEdgeColors = np.ones((len(edgedata),3))
    theNodeColors = np.ones((len(nodedata),3))
    blue=matplotlib.colors.colorConverter.to_rgba('blue')
    normalizedEdgeData = matplotlib.colors.Normalize(vmin=np.amin(edgedata),vmax=np.amax(edgedata))(edgedata)
    normalizedNodeData = matplotlib.colors.Normalize(vmin=np.amin(edgedata),vmax=np.amax(edgedata))(nodedata)
    #normalizedEdgeData = matplotlib.colors.LogNorm(vmin=np.amin(edgedata),vmax=np.amax(edgedata))(edgedata)
    #normalizedNodeData = matplotlib.colors.LogNorm(vmin=np.amin(edgedata),vmax=np.amax(edgedata))(nodedata)
    #normalizedEdgeData = matplotlib.colors.LogNorm()(edgedata)
    #normalizedNodeData = matplotlib.colors.LogNorm()(nodedata)
    for (no, intensity) in enumerate(normalizedEdgeData):
      theEdgeColors[no,:] = np.multiply(blue[0:3],intensity)
    for (no, intensity) in enumerate(normalizedNodeData):
      theNodeColors[no,:] = np.multiply(blue[0:3],intensity)
    vesselgraph.edges['colors'] = theEdgeColors
    vesselgraph.nodes['colors'] = theNodeColors
  return cm
Ejemplo n.º 7
0
def make_any_color_arrays(vesselgraph, data_name):
    edges = vesselgraph.edgelist
    num_nodes = len(vesselgraph.nodes['position'])
    flags = vesselgraph.edges['flags']
    nflags = krebsutils.edge_to_node_property(num_nodes, edges, flags, 'or')

    mask = myutils.bbitwise_and(flags, krebsutils.CIRCULATED)
    nmask = myutils.bbitwise_and(nflags, krebsutils.CIRCULATED)

    if data_name in vesselgraph.edges:
        edgedata = vesselgraph.edges[data_name]
        nodedata = krebsutils.edge_to_node_property(num_nodes, edges, edgedata,
                                                    'avg')
    else:
        nodedata = vesselgraph.nodes[data_name]
        edgedata = np.average((nodedata[edges[:, 0]], nodedata[edges[:, 1]]),
                              axis=0)

    gray = np.asarray((0.1, 0.1, 0.1))
    edgecolors = np.repeat(gray.reshape(1, -1), len(edgedata), axis=0)
    nodecolors = np.repeat(gray.reshape(1, -1), len(nodedata), axis=0)
    #colors = lambda arr: cm.to_rgba(arr)[:,:3]
    colors = lambda arr: np.power(cm.to_rgba(arr)[:, :3], 2.4)

    if data_name == 'hematocrit':
        cm = matplotlib.cm.ScalarMappable(cmap=cm_hematocrit)
        cm.set_clim(0, 1)
        unmapped_range = (0., 1.)
        edgecolors[mask] = colors(edgedata[mask])
        nodecolors[nmask] = colors(nodedata[nmask])
    elif data_name == 'pressure':
        p0 = np.amin(nodedata)
        p1 = np.amax(nodedata)
        unmapped_range = (p0, p1)
        cm = matplotlib.cm.ScalarMappable(cmap=cm_redblue)
        cm.set_clim(p0, p1)
        edgecolors[mask] = colors(edgedata[mask])
        nodecolors[nmask] = colors(nodedata[nmask])
    elif data_name == 'shearforce':
        mask = mask & (edgedata > 0)
        nmask = nmask & (nodedata > 0)
        edgedata = edgedata[mask]
        nodedata = nodedata[nmask]
        unmapped_range = edgedata.min(), edgedata.max()
        edgedata = np.log10(edgedata)
        nodedata = np.log10(nodedata)
        p0 = -4  #np.amin(edgedata)
        p1 = -1  #np.amax(edgedata)
        cm = matplotlib.cm.ScalarMappable(cmap=matplotlib.cm.spectral)
        cm.set_clim(p0, p1)
        edgecolors[mask] = colors(edgedata)
        nodecolors[nmask] = colors(nodedata)
    elif data_name == 'flow':
        mask = mask & (edgedata > 0)
        nmask = nmask & (nodedata > 0)
        edgedata = edgedata[mask]
        nodedata = nodedata[nmask]
        unmapped_range = edgedata.min(), edgedata.max()
        edgedata = np.log10(edgedata)
        nodedata = np.log10(nodedata)
        p0 = np.floor(np.amin(edgedata))
        p1 = np.ceil(np.amax(edgedata))
        cm = matplotlib.cm.ScalarMappable(cmap=matplotlib.cm.jet)
        cm.set_clim(p0, p1)
        edgecolors[mask] = colors(edgedata)
        nodecolors[nmask] = colors(nodedata)
    elif data_name == 'flags':
        unmapped_range = (0., 1.)
        mask = mask & (edgedata > 0)
        nmask = nmask & (nodedata > 0)
        edgedata = edgedata[mask]
        nodedata = nodedata[nmask]
        edgedata = np.bitwise_and(edgedata, krebsutils.ARTERY)
        cm = matplotlib.cm.ScalarMappable(cmap=matplotlib.cm.jet)
        edgecolors[mask] = colors(edgedata)
        nodecolors[nmask] = colors(nodedata)
    vesselgraph.edges['colors'] = edgecolors
    vesselgraph.nodes['colors'] = nodecolors
    return cm, unmapped_range