Skip to content

Commit

Permalink
Fix observations column ordering
Browse files Browse the repository at this point in the history
Updates displayed observations to have the same order as the CSV.

Fixes #69
  • Loading branch information
johnbradley committed Oct 20, 2023
1 parent fea1a01 commit 2c0bd25
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
5 changes: 3 additions & 2 deletions andromeda-ui/components/ObservationTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Image from 'next/image'
interface ObservationTableProps {
iNatUser: string | undefined;
observations: any[];
observationFieldnames: string[];
totalObservations: number;
maxObs: number;
}
Expand All @@ -23,11 +24,11 @@ const KNOWN_COLUMNS = new Set([
]);

export default function ObservationTable(props: ObservationTableProps) {
const { observations, maxObs, iNatUser, totalObservations} = props;
const { observations, observationFieldnames, maxObs, iNatUser, totalObservations} = props;
const showing = Math.min(observations.length, maxObs);
let extraColumns: string[] = [];
if (observations.length > 0) {
extraColumns = Object.keys(observations[0]).filter((x) => !KNOWN_COLUMNS.has(x))
extraColumns = observationFieldnames.filter((x) => !KNOWN_COLUMNS.has(x))
}
const exampleObservations = observations.slice().slice(0, maxObs);
const rows = exampleObservations.map((x) => {
Expand Down
3 changes: 3 additions & 0 deletions andromeda-ui/pages/fetch-data/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default function GeneratePage() {
const [fetching, setFetching] = useState<boolean>(false);
const [showObservations, setShowObservations] = useState<boolean>(false);
const [observations, setObservations] = useState<any[]>([]);
const [observationFieldnames, setObservationFieldnames] = useState<string[]>([]);
const [totalObservations, setTotalObservations] = useState<number>(0);
const [generateObservationDataset, setGenerateObservationDataset] = useState<boolean>(false);
const [observationsURL, setObservationsURL] = useState<string>();
Expand All @@ -58,6 +59,7 @@ export default function GeneratePage() {
try {
const result = await fetchObservations(iNatUser, addSatRGBData, addLandCover, SHOW_OBS_MAX);
setObservations(result.data);
setObservationFieldnames(result.fieldnames);
setTotalObservations(result.total);
setObservationsURL(undefined);
if (result.warnings.length) {
Expand Down Expand Up @@ -106,6 +108,7 @@ export default function GeneratePage() {
<ObservationTable
iNatUser={iNatUser}
observations={observations}
observationFieldnames={observationFieldnames}
totalObservations={totalObservations}
maxObs={SHOW_OBS_MAX} />
{warningNotice}
Expand Down
4 changes: 2 additions & 2 deletions andromeda/inaturalist.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ def get_label(idx, total_observations, reversed):
def get_inaturalist_observations(user_id, add_sat_rgb_data, add_landcover_data, limit):
idx = 0
missing_lat_long = False
obeservation_ary, total_observations = get_observations(user_id=user_id, limit=limit)
observation_ary, total_observations = get_observations(user_id=user_id, limit=limit)
observations = Observations(fieldnames=CSV_FIELDS[:], total=total_observations)
for obs in obeservation_ary:
for obs in observation_ary:
observed_on = obs.get("observed_on")
if not observed_on:
raise BadObservationException(OBSERVED_ON_MISSING_MSG)
Expand Down
1 change: 1 addition & 0 deletions andromeda/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def get_inaturalist(user_id):
"user_id": user_id,
"total": observations.total,
"data": observations.data,
"fieldnames": observations.fieldnames,
"warnings": list(observations.warnings)
})
elif format == "csv":
Expand Down
12 changes: 8 additions & 4 deletions andromeda/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ def test_inverse_dimensional_reduction(self, mock_dataset_store):
def test_get_inaturalist(self, mock_get_inaturalist_observations):
observations = [{"Image_Label": "p1"}]
warnings = ["missing_lat_long"]
fieldnames = ["Image_Label"]
mock_get_inaturalist_observations.return_value = Mock(
data=observations, warnings=warnings, total=1)
data=observations, fieldnames=fieldnames, warnings=warnings, total=1)
client = app.test_client()
result = client.get(f"/api/inaturalist/bob")
self.assertEqual(result.status_code, 200)
Expand All @@ -102,6 +103,7 @@ def test_get_inaturalist(self, mock_get_inaturalist_observations):
{
'total': 1,
"data": [{"Image_Label": "p1"}],
'fieldnames': ["Image_Label"],
"user_id": "bob",
"warnings": ["missing_lat_long"],
},
Expand All @@ -113,9 +115,10 @@ def test_get_inaturalist(self, mock_get_inaturalist_observations):
@patch("main.get_inaturalist_observations")
def test_get_inaturalist_limit(self, mock_get_inaturalist_observations):
observations = [{"Image_Label": "p1"}]
fieldnames = ["Image_Label"]
warnings = ["missing_lat_long"]
mock_get_inaturalist_observations.return_value = Mock(
data=observations, warnings=warnings, total=1)
data=observations, fieldnames=fieldnames, warnings=warnings, total=1)
client = app.test_client()
result = client.get(f"/api/inaturalist/bob?limit=6")
self.assertEqual(result.status_code, 200)
Expand All @@ -124,6 +127,7 @@ def test_get_inaturalist_limit(self, mock_get_inaturalist_observations):
{
'total': 1,
"data": [{"Image_Label": "p1"}],
'fieldnames': ['Image_Label'],
"user_id": "bob",
"warnings": ["missing_lat_long"],
},
Expand Down Expand Up @@ -176,7 +180,7 @@ def test_get_inaturalist_xml(self, mock_get_inaturalist_observations):

@patch("main.get_inaturalist_observations")
def test_get_inaturalist_add_rgb(self, mock_get_inaturalist_observations):
mock_get_inaturalist_observations.return_value = Mock(data=[], warnings=[], total=0)
mock_get_inaturalist_observations.return_value = Mock(data=[], fieldnames=[], warnings=[], total=0)
client = app.test_client()
result = client.get(f"/api/inaturalist/bob?format=json&add_sat_rgb_data=true")
self.assertEqual(result.status_code, 200)
Expand All @@ -185,7 +189,7 @@ def test_get_inaturalist_add_rgb(self, mock_get_inaturalist_observations):

@patch("main.get_inaturalist_observations")
def test_get_inaturalist_add_landcover(self, mock_get_inaturalist_observations):
mock_get_inaturalist_observations.return_value = Mock(data=[], warnings=[], total=0)
mock_get_inaturalist_observations.return_value = Mock(data=[], fieldnames=[], warnings=[], total=0)
client = app.test_client()
result = client.get(f"/api/inaturalist/bob?format=json&add_landcover_data=true")
self.assertEqual(result.status_code, 200)
Expand Down

0 comments on commit 2c0bd25

Please sign in to comment.