示例#1
0
    def init_distance_matrix(self, profiles=["car"]):
        "Create distance_matrix database table"

        logger.info(f'Calculating distance matrix for {profiles}...')

        # TO DO: update this so that's it's in the same structure as self.calculated_centroid.df, etc.
        if not hasattr(self, 'centroid_df'):
            self.centroid_df = gp.GeoDataFrame.from_postgis("SELECT * FROM demand;", self.db_conn.conn, geom_col = 'centroid')

        if not hasattr(self, 'poi'):
            self.poi = gp.GeoDataFrame.from_postgis("SELECT * FROM poi;", "retrieved POI", self.db_conn.conn, geom_col = 'point')

        for profile in profiles:
            # TO DO: this will need to change based on different profiles
            try:
                DM = DistanceMatrix(self.poi, self.centroid_df, self.config.ORS_client, self.config.iso_catchment_range,
                    self.config.iso_catchment_type, self.config.iso_profile, self.config.iso_sleep_time, self.config.dm_metric,
                    self.config.dm_unit, self.config.dm_sleep_time, self.config.ORS_timeout)
                logger.info(f'Successfully calculated distance matrix for {profile}')
            except Exception as e:
                logger.error(f'Unsuccessfully calculated the distance matrix for {profile}: {e}')
                sys.exit(1)

            # TO DO: update query for different distance_matrix_* based on mode of transportation (will have to update config.json)
            query_create = """
                        DROP TABLE IF EXISTS distance_matrix_%s;
                        CREATE TABLE distance_matrix_%s(
                        id serial PRIMARY KEY,
                        geouid int""" % (profile, profile)

            # loop through all columns to build query statement to create the distance_matrix_* table
            # TO DO: consider different data type?

            DM.distance_matrix = DM.distance_matrix.where(pd.notnull(DM.distance_matrix), 'NULL')
            columns = ['geouid']

            for col in DM.distance_matrix.columns.values[1:]:
                columns.append(col)
                if col == DM.distance_matrix.columns.values[-1]:
                    query_create += ", " + col  + " float)"
                else:
                    query_create += ", " + col  + " float"

            self.execute_query(query_create, "created distance_matrix_" + profile)

            columns = ", ".join(DM.distance_matrix.columns.values.tolist()) # list of columns as a string
            rows = DM.distance_matrix.to_numpy().tolist() # list of rows

            # for each row in distance matrix
            for i in DM.distance_matrix.index:
                rows[i] = [str(value) for value in rows[i]] # cast each row value as string
                values = ", ".join(rows[i]) # store a row's values into a list as a string

                # insert row into database table
                query_insert = """ INSERT into distance_matrix_%s (%s) VALUES (%s);""" % (profile, columns, values)
                self.execute_query(query_insert, "updated distance_matrix")
                # print(query_insert)
            # create index for distance_matrix table
            self.execute_query("CREATE INDEX idx_distance_matrix_%s ON distance_matrix_%s (%s);" % (profile, profile, columns), "indexed distance_matrix_%s" % (profile))
示例#2
0
 def __init__(self, Time_Step, Mol_List, Box_Dim, Atom_List, N):
     self.Time_Step = Time_Step
     self.Mol_List = Mol_List
     self.Box_Dim = Box_Dim
     self.num_Mols = len(Mol_List)
     self.Atom_List = Atom_List
     self.DistanceMatrix = DistanceMatrix()
     self.DistanceMatrix.setPeriodicBound(Box_Dim)
     self.numCarbon = 5680.
     boxx, boxy, boxz = Box_Dim
     return
示例#3
0
def load_blossum(blos):
    """
    loads a BLOSUM matrix

    :param str blos: Specifeis the BLOSUM matrix to lead
    :return: dict(str, dict(str, float)) - A BLOSUM1 matrix
    """
    try:
        mod = __import__('DistanceMatrices', fromlist=[blos])
        return DistanceMatrix(getattr(mod, blos))
    except:
        mod = __import__('DistanceMatrices', fromlist=["BLOSUM50_distances"])
        return DistanceMatrix(getattr(mod, "BLOSUM50_distances"))
示例#4
0
    def topologyDistanceMatrix(self, metric='sd'):
        """Returns a DistanceMatrix object showing topology distances.

        Uses the :meth:`Tree.Tree.topologyDistance` method to compare
        trees.  That method returns distances between pairs of trees,
        and this method simply collates those distances into a
        :class:`DistanceMatrix.DistanceMatrix` object, which is
        returned.

        See :meth:`Tree.Tree.topologyDistance` for an explanation of the
        different metrics.  The metrics are given in
        ``var.topologyDistanceMetrics``"""

        from DistanceMatrix import DistanceMatrix
        d = DistanceMatrix()
        d.names = []
        for t in self.trees:
            d.names.append(t.name)
        d.dim = len(d.names)
        #print d.names
        #sys.exit()
        d.matrix = []
        for i in range(d.dim):
            d.matrix.append([0.0] * d.dim)

        for i in range(d.dim):
            t1 = self.trees[i]
            for j in range(i + 1, d.dim):
                t2 = self.trees[j]
                theDist = t1.topologyDistance(t2, metric=metric)
                d.matrix[i][j] = theDist
                d.matrix[j][i] = theDist
        return d
示例#5
0
    def topologyDistanceMatrix(self, metric='sd'):
        """Returns a DistanceMatrix object showing topology distances.

        Uses the :meth:`Tree.Tree.topologyDistance` method to compare
        trees.  That method returns distances between pairs of trees,
        and this method simply collates those distances into a
        :class:`DistanceMatrix.DistanceMatrix` object, which is
        returned.

        See :meth:`Tree.Tree.topologyDistance` for an explanation of the
        different metrics.  The metrics are given in
        ``var.topologyDistanceMetrics``"""

        from DistanceMatrix import DistanceMatrix
        d = DistanceMatrix()
        d.names = []
        for t in self.trees:
            d.names.append(t.name)
        d.dim = len(d.names)
        #print d.names
        #sys.exit()
        d.matrix = []
        for i in range(d.dim):
            d.matrix.append([0.0] * d.dim)

        for i in range(d.dim):
            t1 = self.trees[i]
            for j in range(i + 1, d.dim):
                t2 = self.trees[j]
                theDist = t1.topologyDistance(t2, metric=metric)
                d.matrix[i][j] = theDist
                d.matrix[j][i] = theDist
        return d
示例#6
0
def patristicDistanceMatrix(self):
    """Matrix of distances along tree path.

    This method sums the branch lengths between each pair of taxa, and
    puts the result in a DistanceMatrix object, which is returned.

    Self.taxNames is required.
    """

    gm = ['Tree.patristicDistanceMatrix()']

    if not self.taxNames:
        gm.append("No taxNames.")
        raise Glitch, gm

    # The tree will be rearranged, so make a copy to play with, so
    # self is undisturbed.
    t = self.dupe()

    d = DistanceMatrix()
    d.names = self.taxNames
    d.dim = len(d.names)
    #print d.names
    d.matrix = []
    for i in range(d.dim):
        d.matrix.append([0.0] * d.dim)

    for i in range(d.dim):
        n1 = t.node(d.names[i])
        t.reRoot(n1, moveInternalName=False)
        for j in range(i + 1, d.dim):
            n2 = t.node(d.names[j])
            # sum up distances between n1 and n2
            sum = n2.br.len
            n2 = n2.parent
            while n2 != n1:
                sum = sum + n2.br.len
                n2 = n2.parent
            #print "Dist from %s to %s is %f" % (d.names[i], d.names[j], sum)
            d.matrix[i][j] = sum
            d.matrix[j][i] = sum
    return d
示例#7
0
def patristicDistanceMatrix(self):
    """Matrix of distances along tree path.

    This method sums the branch lengths between each pair of taxa, and
    puts the result in a DistanceMatrix object, which is returned.

    Self.taxNames is required.
    """

    gm = ["Tree.patristicDistanceMatrix()"]

    if not self.taxNames:
        gm.append("No taxNames.")
        raise Glitch, gm

    # The tree will be rearranged, so make a copy to play with, so
    # self is undisturbed.
    t = self.dupe()

    d = DistanceMatrix()
    d.names = self.taxNames
    d.dim = len(d.names)
    # print d.names
    d.matrix = []
    for i in range(d.dim):
        d.matrix.append([0.0] * d.dim)

    for i in range(d.dim):
        n1 = t.node(d.names[i])
        t.reRoot(n1, moveInternalName=False)
        for j in range(i + 1, d.dim):
            n2 = t.node(d.names[j])
            # sum up distances between n1 and n2
            sum = n2.br.len
            n2 = n2.parent
            while n2 != n1:
                sum = sum + n2.br.len
                n2 = n2.parent
            # print "Dist from %s to %s is %f" % (d.names[i], d.names[j], sum)
            d.matrix[i][j] = sum
            d.matrix[j][i] = sum
    return d
示例#8
0
class Snap_Shot(object):
    """
        Class defining a snap shot of the trajectory
        instance variables: Time_step, Mol_List, Box_Dim, Rg_Dist, E2E_Dist, RDF_Dist
    """
    def __init__(self, Time_Step, Mol_List, Box_Dim, Atom_List, N):
        self.Time_Step = Time_Step
        self.Mol_List = Mol_List
        self.Box_Dim = Box_Dim
        self.num_Mols = len(Mol_List)
        self.Atom_List = Atom_List
        self.DistanceMatrix = DistanceMatrix()
        self.DistanceMatrix.setPeriodicBound(Box_Dim)
        self.numCarbon = 5680.
        boxx, boxy, boxz = Box_Dim
        return
    
    def showDistanceDistribution(self):
        self.DistanceMatrix.showDistanceDistribution()

    def pairCorrelation_Hist(self, numbins=200):
        # print "Calculating Snapshot Distance Matrix"
        for Mol in self.Mol_List:
            self.DistanceMatrix.addPoint(Mol.COM())
        # print "Finished placing atoms...Calculating Distance Distribution"
        self.DistanceMatrix.calculateDistanceDistribution()
        # print "returning pyplot"
        n, bins = self.DistanceMatrix.pyplotDistanceDistribution(numbins = 200)
        print "n is: "
        print n
        print bins
        return n, bins

    def Compute_COM(self):
        """
            Compute the centers of mass of all the polymer chains
            """
        for Mol in self.Mol_List:
            Mass_Weighted_Sum = np.zeros(3,dtype=float)
            for Atom in Mol.Atom_List:
                Atom.unwrapped_position = Atom.position + np.multiply(self.Box_Dim, Atom.image_flags)
                Mass_Weighted_Sum += Polymer.Mass[Atom.Type]*Atom.unwrapped_position
            Mol.COM = Mass_Weighted_Sum/Mol.MW
        return

    def Compute_Tangent_Correlation(self):
        TangentCorrelation = []
        for Mol in self.Mol_List:
            MolTangentCorrelation = []
            RelevantAtoms = [Atom for Atom in Mol.Atom_List.tolist() if ((Atom.Mass > 2.) and (Atom.Mass < 196))]
            # print "mass of second atom is: " + str(Mol.Atom_List[1].Mass)
            initialvector = RelevantAtoms[1].position-RelevantAtoms[0].position
            initialvector /= np.linalg.norm(initialvector)
            lastpos = RelevantAtoms[0].position
            for Atom in RelevantAtoms[1:]:
                # print "adding new pos: " + str(Atom.position)
                newpos = Atom.position
                newvector = newpos-lastpos
                newvector /= np.linalg.norm(newvector)
                # print "tangent correlation is: " + str(self.Tangent_Correlation(latestpos[0],latestpos[1],newpos))
                MolTangentCorrelation.append(np.dot(initialvector,newvector))
                lastpos =newpos
            # print "Mol Tangent Correlation: " + str(np.asarray(MolTangentCorrelation)) + " with mass"
            TangentCorrelation.append(np.asarray(MolTangentCorrelation))
        TangentCorrelation = np.asarray(TangentCorrelation) 
        # print "TangentCorrelation is: " + str(TangentCorrelation)
        TangentCorrelation = np.mean(TangentCorrelation, axis = 0)
        # print "TangentCorrelation is: " + str(TangentCorrelation)
        return TangentCorrelation

    def carbonDensity_Hist(self, numbins = 200):
        Z_pos = []
        Z_max = []
        theta = []
        N = 0
        for Mol in self.Mol_List:
            mol_z = []
            for Atom in Mol.Atom_List:
                if Atom.Mass == 12.011:
                    # print "found Carbon"
                    N += 1
                    Z_pos.append(Atom.position[2])
                    mol_z.append(Atom.position[2])
            try:
                Z_max.append(np.asarray(mol_z).max())
            except:
                continue
        # Z_max_av = np.asarray(Z_max).mean()
        # Z_max_std = np.asarray(Z_max).std()
        n, bins = np.histogram(Z_pos, bins= numbins, range=(0.07,0.3))
        return n/self.numCarbon, bins*100-7.6
        # return Zn, Zbins, Z_max_av, Z_max_std

    def get_net_dipole(self, molclass, molName):
        # partial_charges = OPLS.get_partial_charges(molclass, molName)
        partial_charges = [(0,.32), (0,-.64)]
        print partial_charges
        net_dipole = np.zeros(3)
        # print pc
        # splicing out gold surface
        for mol in self.Mol_List:
            for atom in mol.Atom_List:
                if atom.Element == "Au":
                    continue
                else:
                    partial_charge = partial_charges[atom.Type-1][1]
                    # print atom.position
                    # print partial_charge
                    dipole_moment = partial_charge*atom.position
                    # print dipole_moment
                    net_dipole = net_dipole+dipole_moment
                    mol.dipoleMoment = mol.dipoleMoment+dipole_moment
        print "Net dipole is: ..." + str(net_dipole)
        # return net_dipole
        # for mol in self.Mol_List[:-1]:
        #     if (np.dot(mol.dipoleMoment,net_dipole)/(np.linalg.norm(mol.dipoleMoment)*np.linalg.norm(net_dipole)) < -.9):
        #         print "alert!"
        #         print mol.dipoleMoment
        #         for atom in mol.Atom_List:
        #             print atom.Element, str(atom.position)
        #         # print net_dipole
        #         break
        plt.hist([np.dot(mol.dipoleMoment,net_dipole)/(np.linalg.norm(mol.dipoleMoment)*np.linalg.norm(net_dipole)) for mol in self.Mol_List[:-1]], bins=100)
        print os.getcwd()
        plt.savefig(molclass+"_"+molName+"_dipoleDistribution.png")
        print "saved!"
        plt.clf()
        return net_dipole