Exemplo n.º 1
0
def getNodeSizes(net,size_by="strength",minsize=2.0,maxsize=6.0):
    """Returns a dictionary {node:size} for visualizations. The sizes
    are set using either node strength"""

    nodeSizes={}

    if size_by=="strength":

        if hasattr(net,'matrixtype'):
            if net.matrixtype==0:        
                net=transforms.dist_to_weights(net)

        strengths = netext.strengths(net)
        maxs = max(strengths.values())
        mins = min(strengths.values())           

        if maxs==mins:
            A=0
        else:
            A=(maxsize-minsize)/(maxs-mins)
        B=maxsize-A*maxs

        for node in net:
            nodeSizes[node]=A*strengths[node]+B

    elif size_by=="fixed":
        for node in net:
            nodeSizes[node]=maxsize
    else:
        numeric_props=netext.getNumericProperties(net)
        if size_by in numeric_props:
            values=[]
            for node in net:
                values.append(net.nodeProperty[size_by][node])

            minval=min(values)
            maxval=max(values)

            if maxval==minval:
                A=0
            else:
                A=(maxsize-minsize)/(maxval-minval)

            B=maxsize-A*maxval
            for node in net:
                nodeSizes[node]=A*net.nodeProperty[size_by][node]+B

    return nodeSizes
Exemplo n.º 2
0
def getNodeSizes(net, size_by="strength", minsize=2.0, maxsize=6.0):
    """Returns a dictionary {node:size} for visualizations. The sizes
    are set using either node strength"""

    nodeSizes = {}

    if size_by == "strength":

        if hasattr(net, 'matrixtype'):
            if net.matrixtype == 0:
                net = transforms.dist_to_weights(net)

        strengths = netext.strengths(net)
        maxs = max(strengths.values())
        mins = min(strengths.values())

        if maxs == mins:
            A = 0
        else:
            A = (maxsize - minsize) / (maxs - mins)
        B = maxsize - A * maxs

        for node in net:
            nodeSizes[node] = A * strengths[node] + B

    elif size_by == "fixed":
        for node in net:
            nodeSizes[node] = maxsize
    else:
        numeric_props = netext.getNumericProperties(net)
        if size_by in numeric_props:
            values = []
            for node in net:
                values.append(net.nodeProperty[size_by][node])

            minval = min(values)
            maxval = max(values)

            if maxval == minval:
                A = 0
            else:
                A = (maxsize - minsize) / (maxval - minval)

            B = maxsize - A * maxval
            for node in net:
                nodeSizes[node] = A * net.nodeProperty[size_by][node] + B

    return nodeSizes
Exemplo n.º 3
0
def getNodeColors(net,colorwith="strength",useColorMap="orange",parentnet=[]):
    """Returns a dictionary {node:color}. The colors are set based
    on either node strengh (colorwith="strength", default) 
    or any nodeProperty. For cases where e.g. nodes which have been thresholded
    out (k=0), the input parameter parentnet can be used - parentnet should contain the original
    network *before* thresholding, i.e. containing all original nodes and
    their attributes. IF parentnet is given, i) if strength is used, its nodes
    which are NOT in net colored gray, ii) if properties
    are used, its nodes are colored similarly to those nodes in net. Also the
    dictionary which is returned contains then all nodes in parentnet"""

    myNodeColors=setColorMap(useColorMap)

    nodeColors={}

    if colorwith=="strength":

        if hasattr(net,'matrixtype'):
            if net.matrixtype==0:        
                net=transforms.dist_to_weights(net)

        strengths = netext.strengths(net)
        max_value = max(strengths.values())
        min_value = min(strengths.values())

        if len(parentnet)>0:        # if we want the dict to include nodes not in net
            for node in parentnet:
                if node in net:     # if the node is in net, use its strength for color
                    nodeColors[node]=setColor(strengths[node],(min_value,max_value),myNodeColors)
                else:               # otherwise color it gray
                    nodeColors[node]=(0.5,0.5,0.5)
        else:
            for node in net:        # if parentnet not given, just color nodes by strength
                nodeColors[node]=setColor(strengths[node],(min_value,max_value),myNodeColors)
    else:

        numeric_props=netext.getNumericProperties(net)
        # first check if colorwith is a numeric property
        if colorwith in numeric_props:
            values=[]
            if len(parentnet)>0:    # again if we want to include nodes not in net
                for node in parentnet:  # first get min and max value of property
                    values.append(parentnet.nodeProperty[colorwith][node])

                min_value=min(values)
                max_value=max(values)
                for node in parentnet: # then set colors according to property
                    nodeColors[node]=setColor(parentnet.nodeProperty[colorwith][node],(min_value,max_value),myNodeColors)
            else:                   # otherwise do the above for nodes in net
                for node in net:
                    values.append(net.nodeProperty[colorwith][node])
                
                min_value=min(values)
                max_value=max(values)

                for node in net:
                    nodeColors[node]=setColor(net.nodeProperty[colorwith][node],(min_value,max_value),myNodeColors)
        
        else:
            # colorwith is not a numeric property, so look up unique values
            # and give them integer numbers

            values={} # empty dict for values
           
            if len(parentnet)>0:# if there are nodes not in net
                props=list(set(parentnet.nodeProperty[colorwith].values()))
            else:
                props=list(set(net.nodeProperty[colorwith].values()))

            #Check if properties can be converted to colors:
            if isListOfColors(props):
                propToColor={}
                cc=matplotlib.colors.ColorConverter()
                for p in props:
                    propToColor[p]=cc.to_rgb(p)
                if len(parentnet)>0:
                    for node in parentnet:
                        nodeColors[node]=propToColor[parentnet.nodeProperty[colorwith][node]]
                else:
                    for node in net:
                        nodeColors[node]=propToColor[parentnet.nodeProperty[colorwith][node]]                    
            else:
                for i,prop in enumerate(props):
                    values[prop]=i+1
                # now all property strings have a numerical value
                min_value=1
                max_value=max(values.values())
                if len(parentnet)>0:

                    for node in parentnet:
                        nodeColors[node]=setColor(values[parentnet.nodeProperty[colorwith][node]],(min_value,max_value),myNodeColors)
                else:
                    for node in net:
                        nodeColors[node]=setColor(values[net.nodeProperty[colorwith][node]],(min_value,max_value),myNodeColors)



    if len(nodeColors)==0:  # finally if for whatever reason no nodes were colored, just set them gray
        if len(parentnet)>0:
            for node in parentnet:
                nodeColors[node]=(0.5,0.5,0.5)
        else:
            for node in net:
                nodeColors[node]=(0.5, 0.5, 0.5)

    return nodeColors
Exemplo n.º 4
0
def getNodeColors(net,
                  colorwith="strength",
                  useColorMap="orange",
                  parentnet=[]):
    """Returns a dictionary {node:color}. The colors are set based
    on either node strengh (colorwith="strength", default) 
    or any nodeProperty. For cases where e.g. nodes which have been thresholded
    out (k=0), the input parameter parentnet can be used - parentnet should contain the original
    network *before* thresholding, i.e. containing all original nodes and
    their attributes. IF parentnet is given, i) if strength is used, its nodes
    which are NOT in net colored gray, ii) if properties
    are used, its nodes are colored similarly to those nodes in net. Also the
    dictionary which is returned contains then all nodes in parentnet"""

    myNodeColors = setColorMap(useColorMap)

    nodeColors = {}

    if colorwith == "strength":

        if hasattr(net, 'matrixtype'):
            if net.matrixtype == 0:
                net = transforms.dist_to_weights(net)

        strengths = netext.strengths(net)
        max_value = max(strengths.values())
        min_value = min(strengths.values())

        if len(parentnet
               ) > 0:  # if we want the dict to include nodes not in net
            for node in parentnet:
                if node in net:  # if the node is in net, use its strength for color
                    nodeColors[node] = setColor(strengths[node],
                                                (min_value, max_value),
                                                myNodeColors)
                else:  # otherwise color it gray
                    nodeColors[node] = (0.5, 0.5, 0.5)
        else:
            for node in net:  # if parentnet not given, just color nodes by strength
                nodeColors[node] = setColor(strengths[node],
                                            (min_value, max_value),
                                            myNodeColors)
    else:

        numeric_props = netext.getNumericProperties(net)
        # first check if colorwith is a numeric property
        if colorwith in numeric_props:
            values = []
            if len(parentnet
                   ) > 0:  # again if we want to include nodes not in net
                for node in parentnet:  # first get min and max value of property
                    values.append(parentnet.nodeProperty[colorwith][node])

                min_value = min(values)
                max_value = max(values)
                for node in parentnet:  # then set colors according to property
                    nodeColors[node] = setColor(
                        parentnet.nodeProperty[colorwith][node],
                        (min_value, max_value), myNodeColors)
            else:  # otherwise do the above for nodes in net
                for node in net:
                    values.append(net.nodeProperty[colorwith][node])

                min_value = min(values)
                max_value = max(values)

                for node in net:
                    nodeColors[node] = setColor(
                        net.nodeProperty[colorwith][node],
                        (min_value, max_value), myNodeColors)

        else:
            # colorwith is not a numeric property, so look up unique values
            # and give them integer numbers

            values = {}  # empty dict for values

            if len(parentnet) > 0:  # if there are nodes not in net
                props = list(set(parentnet.nodeProperty[colorwith].values()))
            else:
                props = list(set(net.nodeProperty[colorwith].values()))

            #Check if properties can be converted to colors:
            if isListOfColors(props):
                propToColor = {}
                cc = matplotlib.colors.ColorConverter()
                for p in props:
                    propToColor[p] = cc.to_rgb(p)
                if len(parentnet) > 0:
                    for node in parentnet:
                        nodeColors[node] = propToColor[
                            parentnet.nodeProperty[colorwith][node]]
                else:
                    for node in net:
                        nodeColors[node] = propToColor[
                            parentnet.nodeProperty[colorwith][node]]
            else:
                for i, prop in enumerate(props):
                    values[prop] = i + 1
                # now all property strings have a numerical value
                min_value = 1
                max_value = max(values.values())
                if len(parentnet) > 0:

                    for node in parentnet:
                        nodeColors[node] = setColor(
                            values[parentnet.nodeProperty[colorwith][node]],
                            (min_value, max_value), myNodeColors)
                else:
                    for node in net:
                        nodeColors[node] = setColor(
                            values[net.nodeProperty[colorwith][node]],
                            (min_value, max_value), myNodeColors)

    if len(
            nodeColors
    ) == 0:  # finally if for whatever reason no nodes were colored, just set them gray
        if len(parentnet) > 0:
            for node in parentnet:
                nodeColors[node] = (0.5, 0.5, 0.5)
        else:
            for node in net:
                nodeColors[node] = (0.5, 0.5, 0.5)

    return nodeColors