Commit 1baa6adc authored by Lauren Adoram-Kershner's avatar Lauren Adoram-Kershner Committed by GitHub
Browse files

Merge pull request #391 from ladoramkershner/model_refactor

Model.py refactor
parents 6df56fd2 c468f823
Loading
Loading
Loading
Loading
+9 −9
Original line number Diff line number Diff line
@@ -996,20 +996,20 @@ class NetworkEdge(Edge):
        session.close()
        return res

    def network_to_matches(self, active_point=True, active_measure=True, rejected_jigsaw=False):
    def network_to_matches(self, ignore_point=False, ignore_measure=False, rejected_jigsaw=False):
        """
        For the edge, take any points/measures that are in the database and
        convert them into matches on the associated edge.

        Parameters
        ----------
        active_point : bool
                       If True (default) only select the points that are
                       currently set to active.
        ignore_point : bool
                       If False (default) only select the points that are
                       not ignored (currently active).

        active_measure : bool
                         If True (default) only add the measures that are
                         currently active
        ignore_measure : bool
                         If False (default) only add the measures that are
                         not ignored (currently active).

        rejected_jigsaw : bool
                          If False (default) add any points that are not
@@ -1030,8 +1030,8 @@ class NetworkEdge(Edge):
                  Measures.line,
                  Measures.measuretype,
                  Measures.imageid).\
            filter(Points.active==active_point,
                   Measures.active==active_measure,
            filter(Points.ignore==ignore_point,
                   Measures.ignore==ignore_measure,
                   Measures.jigreject==rejected_jigsaw,
                   sqlalchemy.or_(Measures.imageid==source,
                                  Measures.imageid==destin)).join(Measures)
+45 −34
Original line number Diff line number Diff line
@@ -1448,10 +1448,33 @@ class NetworkCandidateGraph(CandidateGraph):
            n.generate_vrt(**kwargs)

    def to_isis(self, path, flistpath=None,sql = """
SELECT points.id, measures.serial, points.pointtype, points.apriori, points.adjusted,
measures.sample, measures.line, measures.measuretype, measures.imageid
FROM measures INNER JOIN points ON measures.pointid = points.id
WHERE points.active = True AND measures.active=TRUE AND measures.jigreject=FALSE;
SELECT points.id,
        points."pointType",
        points."apriori",
        points."adjusted",
        points."pointIgnore",
        measures."serialnumber",
        measures."sample",
        measures."line",
        measures."measureType",
        measures."imageid",
        measures."measureIgnore",
        measures."measureJigsawRejected",
        measures."aprioriline",
        measures."apriorisample"
FROM measures
INNER JOIN points ON measures."pointid" = points."id"
WHERE
    points."pointIgnore" = False AND
    measures."measureIgnore" = FALSE AND
    measures."measureJigsawRejected" = FALSE AND
    measures."imageid" NOT IN
        (SELECT measures."imageid"
        FROM measures
        INNER JOIN points ON measures."pointid" = points."id"
        WHERE measures."measureIgnore" = False and measures."measureJigsawRejected" = False AND points."pointIgnore" = False
        GROUP BY measures."imageid"
        HAVING COUNT(DISTINCT measures."pointid")  < 3);
"""):
        """
        Given a set of points/measures in an autocnet database, generate an ISIS
@@ -1472,9 +1495,8 @@ WHERE points.active = True AND measures.active=TRUE AND measures.jigreject=FALSE
              The sql query to execute in the database.

        """

        df = pd.read_sql(sql, engine)
        df.rename(columns={'imageid':'image_index','id':'point_id', 'pointtype' : 'type',
            'sample':'x', 'line':'y', 'serial': 'serialnumber'}, inplace=True)

        #create columns in the dataframe; zeros ensure plio (/protobuf) will
        #ignore unless populated with alternate values
@@ -1489,7 +1511,7 @@ WHERE points.active = True AND measures.active=TRUE AND measures.jigreject=FALSE
        #recalculate the control point lat/lon from control measures which where
        #"massaged" by the phase and template matcher.
        for i, row in df.iterrows():
            if row['type'] == 3 or row['type'] == 4:
            if row['pointType'] == 3 or row['pointType'] == 4:
                apriori_geom = swkb.loads(row['apriori'], hex=True)
                row['aprioriX'] = apriori_geom.x
                row['aprioriY'] = apriori_geom.y
@@ -1504,8 +1526,14 @@ WHERE points.active = True AND measures.active=TRUE AND measures.jigreject=FALSE
            flistpath = os.path.splitext(path)[0] + '.lis'
        target = config['spatial'].get('target', None)

        ids = df['imageid'].unique()
        fpaths = [self.nodes[i]['data']['image_path'] for i in ids]
        for f in self.files:
            if f not in fpaths:
                warnings.warn(f'{f} in candidate graph but not in output network.')

        cnet.to_isis(df, path, targetname=target)
        cnet.write_filelist(self.files, path=flistpath)
        cnet.write_filelist(fpaths, path=flistpath)

    @staticmethod
    def update_from_jigsaw(session, path):
@@ -1520,10 +1548,8 @@ WHERE points.active = True AND measures.active=TRUE AND measures.jigreject=FALSE
        """
        # Ingest isis control net as a df and do some massaging
        data = cnet.from_isis(path)
        data['jigsawFullRejected'] = data['pointJigsawRejected'] | data['jigsawRejected']
        data_to_update = data[['id', 'serialnumber', 'jigsawFullRejected', 'sampleResidual', 'lineResidual', 'samplesigma', 'linesigma', 'adjustedCovar', 'apriorisample', 'aprioriline']]
        data_to_update = data_to_update.rename(columns = {'serialnumber': 'serial', 'jigsawFullRejected': 'jigreject', 'sampleResidual': 'sampler', 'lineResidual': 'liner', 'adjustedCovar': 'covar'})
        data_to_update['covar'] = data_to_update['covar'].apply(lambda row : list(row))
        data_to_update = data[['id', 'serialnumber', 'measureJigsawRejected', 'sampleResidual', 'lineResidual', 'samplesigma', 'linesigma', 'adjustedCovar', 'apriorisample', 'aprioriline']]
        data_to_update['adjustedCovar'] = data_to_update['adjustedCovar'].apply(lambda row : list(row))
        data_to_update['id'] = data_to_update['id'].apply(lambda row : int(row))

        # Generate a temp table, update the real table, then drop the temp table
@@ -1531,9 +1557,9 @@ WHERE points.active = True AND measures.active=TRUE AND measures.jigreject=FALSE

        sql = """
        UPDATE measures AS f
        SET jigreject = t.jigreject, sampler = t.sampler, liner = t.liner, samplesigma = t.samplesigma, linesigma = t.linesigma, apriorisample = t.apriorisample, aprioriline = t.aprioriline
        SET "measureJigsawRejected" = t."measureJigsawRejected", sampler = t."sampleResidual", liner = t."lineResidual", samplesigma = t."samplesigma", linesigma = t."linesigma", apriorisample = t."apriorisample", aprioriline = t."aprioriline"
        FROM temp_measures AS t
        WHERE f.serial = t.serial AND f.pointid = t.id;
        WHERE f.serialnumber = t.serialnumber AND f.pointid = t.id;

        DROP TABLE temp_measures;
        """
@@ -1782,21 +1808,6 @@ WHERE points.active = True AND measures.active=TRUE AND measures.jigreject=FALSE
        if isinstance(cnet, str):
            cnet = from_isis(cnet)

        # rename some columns
        newcols = []
        for i, c in enumerate(cnet.columns):
            if i == 1:
                newcols.append('pointtype')
            elif i == 5:
                newcols.append('pointignore')
            elif i == 6:
                newcols.append('pointjigsawRejected')
            elif i == 25:
                newcols.append('measuretype')
            else:
                newcols.append(c)
        cnet.columns = newcols

        cnetpoints = cnet.groupby('id')
        points = []
        session = Session()
@@ -1806,14 +1817,14 @@ WHERE points.active = True AND measures.active=TRUE AND measures.jigreject=FALSE
                res = session.query(Images).filter(Images.serial == row.serialnumber).one()
                return Measures(pointid=id,
                         imageid=int(res.id), # Need to grab this
                         measuretype=int(row.measuretype),
                         measuretype=int(row.measureType),
                         serial=row.serialnumber,
                         sample=float(row['sample']),
                         line=float(row['line']),
                         sampler=float(row.sampleResidual),
                         liner=float(row.lineResidual),
                         active=not row.ignore, # active = ~ignored
                         jigreject=row.jigsawRejected,
                         ignore=row.measureIgnore,
                         jigreject=row.measureJigsawRejected,
                         aprioriline=float(row.aprioriline),
                         apriorisample=float(row.apriorisample),
                         linesigma=float(row.linesigma),
@@ -1826,10 +1837,10 @@ WHERE points.active = True AND measures.active=TRUE AND measures.jigreject=FALSE
            lon, lat, alt = pyproj.transform(ecef, lla, x, y, z)

            point = Points(identifier=id,
                           active=not row.pointignore, # active = ~ignored
                           ignore=row.pointIgnore,
                           apriori= shapely.geometry.Point(float(row.aprioriX), float(row.aprioriY), float(row.aprioriZ)),
                           adjusted= shapely.geometry.Point(float(row.adjustedX),float(row.adjustedY),float(row.adjustedZ)),
                           pointtype=float(row.pointtype))
                           pointtype=float(row.pointType))

            point.measures = list(measures)
            points.append(point)
+1 −2
Original line number Diff line number Diff line
@@ -603,8 +603,7 @@ class NetworkNode(Node):
        Get the number of keypoints from the database
        """
        res = self._from_db(Keypoints)
        nkps = res.nkeypoints
        return nkps
        return res.nkeypoints if res is not None else 0

    def create_camera(self):
        # Create the camera entry
+6 −8
Original line number Diff line number Diff line
@@ -12,12 +12,12 @@ from unittest.mock import patch, PropertyMock, MagicMock
def cnet():
    return pd.DataFrame.from_dict({
            'id' : [1],
            'pointtype' : 2,
            'pointType' : 2,
            'serialnumber' : ['BRUH'],
            'jigsawRejected' : [False],
            'measureJigsawRejected': [False],
            'sampleResidual' : [0.1],
            'pointingore' : [False],
            'pointjigsawRejected': [False],
            'pointIgnore' : [False],
            'pointJigsawRejected': [False],
            'lineResidual' : [0.1],
            'linesigma' : [0],
            'samplesigma': [0],
@@ -26,14 +26,14 @@ def cnet():
            'aprioriline' : [0],
            'line' : [1],
            'sample' : [2],
            'ignore': [False],
            'measureIgnore': [False],
            'adjustedX' : [0],
            'adjustedY' : [0],
            'adjustedZ' : [0],
            'aprioriX' : [0],
            'aprioriY' : [0],
            'aprioriZ' : [0],
            'measuretype' : [1]
            'measureType' : [1]
            })

def test_creation():
@@ -50,5 +50,3 @@ def test_place_points_from_cnet(session, cnet, image_data, expected_npoints):
    resp = session.query(model.Points)
    assert len(resp.all()) == expected_npoints
    assert len(resp.all()) == cnet.shape[0]

+8 −8
Original line number Diff line number Diff line
@@ -131,7 +131,7 @@ class Edges(BaseMixin, Base):
    destination = Column(Integer)
    ring = Column(ArrayType())
    fundamental = Column(ArrayType())
    active = Column(Boolean)
    ignore = Column(Boolean)
    masks = Column(Json())

class Costs(BaseMixin, Base):
@@ -187,7 +187,7 @@ class Images(BaseMixin, Base):
    name = Column(String)
    path = Column(String)
    serial = Column(String, unique=True)
    active = Column(Boolean, default=True)
    ignore = Column(Boolean, default=False)
    _footprint_latlon = Column("footprint_latlon", Geometry('MultiPolygon', srid=latitudinal_srid, dimension=2, spatial_index=True))
    footprint_bodyfixed = Column(Geometry('MULTIPOLYGON', dimension=2))
    cam_type = Column(String)
@@ -272,11 +272,11 @@ class PointType(enum.IntEnum):
class Points(BaseMixin, Base):
    __tablename__ = 'points'
    id = Column(Integer, primary_key=True, autoincrement=True)
    _pointtype = Column("pointtype", IntEnum(PointType), nullable=False)  # 2, 3, 4 - Could be an enum in the future, map str to int in a decorator
    _pointtype = Column("pointType", IntEnum(PointType), nullable=False)  # 2, 3, 4 - Could be an enum in the future, map str to int in a decorator
    identifier = Column(String, unique=True)
    _geom = Column("geom", Geometry('POINT', srid=latitudinal_srid, dimension=2, spatial_index=True))
    cam_type = Column(String)
    active = Column(Boolean, default=True)
    ignore = Column("pointIgnore", Boolean, default=False)
    _apriori = Column("apriori", Geometry('POINTZ', srid=rectangular_srid, dimension=3, spatial_index=False))
    _adjusted = Column("adjusted", Geometry('POINTZ', srid=rectangular_srid, dimension=3, spatial_index=False))
    measures = relationship('Measures')
@@ -350,14 +350,14 @@ class Measures(BaseMixin, Base):
    id = Column(Integer,primary_key=True, autoincrement=True)
    pointid = Column(Integer, ForeignKey('points.id'), nullable=False)
    imageid = Column(Integer, ForeignKey('images.id'))
    serial = Column(String, nullable=False)
    _measuretype = Column("measuretype", IntEnum(MeasureType), nullable=False)  # [0,3]  # Enum as above
    serial = Column("serialnumber", String, nullable=False)
    _measuretype = Column("measureType", IntEnum(MeasureType), nullable=False)  # [0,3]  # Enum as above
    sample = Column(Float, nullable=False)
    line = Column(Float, nullable=False)
    sampler = Column(Float)  # Sample Residual
    liner = Column(Float)  # Line Residual
    active = Column(Boolean, default=True)
    jigreject = Column(Boolean, default=False)  # jigsaw rejected
    ignore = Column("measureIgnore", Boolean, default=False)
    jigreject = Column("measureJigsawRejected", Boolean, default=False)  # jigsaw rejected
    aprioriline = Column(Float)
    apriorisample = Column(Float)
    samplesigma = Column(Float)
Loading