Skip to content

Commit

Permalink
fixed bug with ingest function
Browse files Browse the repository at this point in the history
  • Loading branch information
Exu-112 committed Jul 8, 2024
1 parent d144f61 commit 8758e3e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
65 changes: 33 additions & 32 deletions simple/utils/astrometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def ingest_parallax(
# If no previous measurement exists, set the new one to the Adopted measurement
flags = {"added": False, "content": {}, "message": ""}
adopted = False
has_old_adopted = False
source_plx_data: Table = (
db.query(db.Parallaxes).filter(db.Parallaxes.c.source == source).table()
)
Expand Down Expand Up @@ -86,42 +87,11 @@ def ingest_parallax(
# check for previous adopted measurement and find new adopted
adopted_ind = source_plx_data["adopted"] == 1
if sum(adopted_ind):
old_adopted = source_plx_data[adopted_ind]
has_old_adopted = source_plx_data[adopted_ind]
# if errors of new data are less than other measurements,
# set Adopted = True.
if plx_error < min(source_plx_data["parallax_error"]):
adopted = True

# unset old adopted
if old_adopted:
with db.engine.connect() as conn:
conn.execute(
db.Parallaxes.update()
.where(
and_(
db.Parallaxes.c.source == old_adopted["source"][0],
db.Parallaxes.c.reference
== old_adopted["reference"][0],
)
)
.values(adopted=False)
)
conn.commit()
# check that adopted flag is successfully changed
old_adopted_data = (
db.query(db.Parallaxes)
.filter(
and_(
db.Parallaxes.c.source == old_adopted["source"][0],
db.Parallaxes.c.reference
== old_adopted["reference"][0],
)
)
.table()
)
logger.debug("Old adopted measurement unset")
if logger.level == 10:
old_adopted_data.pprint_all()
else:
adopted = False
logger.debug(f"The new measurement's adopted flag is:, {adopted}")
Expand All @@ -148,6 +118,37 @@ def ingest_parallax(
session.commit()
logger.info(f" Photometry added to database: {parallax_data}\n")
flags["added"] = True

# unset old adopted only after ingest is successful!
if has_old_adopted:
with db.engine.connect() as conn:
conn.execute(
db.Parallaxes.update()
.where(
and_(
db.Parallaxes.c.source == has_old_adopted["source"][0],
db.Parallaxes.c.reference
== has_old_adopted["reference"][0],
)
)
.values(adopted=False)
)
conn.commit()
# check that adopted flag is successfully changed
old_adopted_data = (
db.query(db.Parallaxes)
.filter(
and_(
db.Parallaxes.c.source == has_old_adopted["source"][0],
db.Parallaxes.c.reference == has_old_adopted["reference"][0],
)
)
.table()
)
logger.debug("Old adopted measurement unset")
if logger.level == 10:
old_adopted_data.pprint_all()

return flags
except sqlalchemy.exc.IntegrityError as error_msg:
flags["added"] = False
Expand Down
14 changes: 7 additions & 7 deletions tests/test_astrometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ def test_ingest_parallaxes(temp_db, t_plx):

def test_parallax_exceptions(temp_db):
with pytest.raises(AstroDBError) as error_message:
ingest_parallax(temp_db, "bad source", 1, 1, "Ref 1")
ingest_parallax(temp_db, "bad source", 1, 0, "Ref 1")
assert "does not exist in Sources table" in str(error_message.value)

flags = ingest_parallax(temp_db, "bad source", 1, 1, "Ref 1", "comment", False)
flags = ingest_parallax(temp_db, "bad source", 1, 0, "Ref 1", "comment", False)
assert flags == {
"added": False,
"content": {
"source": "bad source",
"parallax": 1,
"parallax_error": 1,
"parallax_error": 0,
"reference": "Ref 1",
"adopted": True,
"comments": "comment",
Expand All @@ -118,19 +118,19 @@ def test_parallax_exceptions(temp_db):
}

with pytest.raises(AstroDBError) as error_message:
ingest_parallax(temp_db, "Fake 1", 1, 1, "bad ref")
ingest_parallax(temp_db, "Fake 1", 1, 0, "bad ref")
assert "does not exist in Publications table" in str(error_message.value)

flags = ingest_parallax(temp_db, "Fake 1", 1, 1, "bad ref", "comment", False)
flags = ingest_parallax(temp_db, "Fake 1", 1, 0, "bad ref", "comment", False)
print(flags)
assert flags == {
"added": False,
"content": {
"source": "Fake 1",
"parallax": 1,
"parallax_error": 1,
"parallax_error": 0,
"reference": "bad ref",
"adopted": False,
"adopted": True,
"comments": "comment",
},
"message": "Reference 'bad ref' does not exist in Publications table. ",
Expand Down

0 comments on commit 8758e3e

Please sign in to comment.