-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for CSVs in SVObs format. (#290)
- Loading branch information
Showing
20 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Copyright 2024 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from csv import DictReader | ||
import logging | ||
import random | ||
|
||
from stats import constants | ||
from stats.data import Observation | ||
from stats.db import Db | ||
from stats.importer import Importer | ||
from stats.nodes import Nodes | ||
from stats.reporter import FileImportReporter | ||
from util.filehandler import FileHandler | ||
|
||
_COLUMNS = [ | ||
constants.COLUMN_ENTITY, constants.COLUMN_VARIABLE, constants.COLUMN_DATE, | ||
constants.COLUMN_VALUE | ||
] | ||
_DEFAULT_COLUMN_MAPPINGS = {x: x for x in _COLUMNS} | ||
|
||
|
||
class VariablePerRowImporter(Importer): | ||
"""Imports a single observations input file where variables are specified in rows (aka "SVObs"). | ||
This is in contrast to the ObservationsImporter where variables are specified in columns. | ||
Currently this importer only writes observations and no entities. | ||
It also does not resolve any entities and expects all entities to be pre-resolved. | ||
""" | ||
|
||
def __init__(self, input_fh: FileHandler, db: Db, | ||
reporter: FileImportReporter, nodes: Nodes) -> None: | ||
self.input_fh = input_fh | ||
self.db = db | ||
self.reporter = reporter | ||
self.input_file_name = self.input_fh.basename() | ||
self.nodes = nodes | ||
self.config = nodes.config | ||
# Reassign after reading CSV. | ||
self.column_mappings = dict(_DEFAULT_COLUMN_MAPPINGS) | ||
self.reader: DictReader = None | ||
|
||
def do_import(self) -> None: | ||
self.reporter.report_started() | ||
try: | ||
self._read_csv() | ||
self._map_columns() | ||
self._write_observations() | ||
self.reporter.report_success() | ||
except Exception as e: | ||
self.reporter.report_failure(str(e)) | ||
raise e | ||
|
||
def _read_csv(self) -> None: | ||
self.reader = DictReader(self.input_fh.read_string_io()) | ||
|
||
def _map_columns(self): | ||
config_mappings = self.config.column_mappings(self.input_file_name) | ||
for key in self.column_mappings.keys(): | ||
if key in config_mappings: | ||
self.column_mappings[key] = config_mappings[key] | ||
|
||
# Ensure that the expected columns exist. | ||
expected_column_names = set(self.column_mappings.values()) | ||
logging.info("Expected column names: %s", expected_column_names) | ||
actual_column_names = set(self.reader.fieldnames) | ||
logging.info("Actual column names: %s", actual_column_names) | ||
difference = expected_column_names - actual_column_names | ||
if difference: | ||
raise ValueError( | ||
f"The following expected columns were not found: {difference}. You can specify column mappings using the columnMappings field." | ||
) | ||
|
||
def _write_observations(self) -> None: | ||
provenance = self.nodes.provenance(self.input_file_name).id | ||
observations: list[Observation] = [] | ||
for row in self.reader: | ||
observation = Observation( | ||
entity=row[self.column_mappings[constants.COLUMN_ENTITY]], | ||
variable=row[self.column_mappings[constants.COLUMN_VARIABLE]], | ||
date=row[self.column_mappings[constants.COLUMN_DATE]], | ||
value=row[self.column_mappings[constants.COLUMN_VALUE]], | ||
provenance=provenance) | ||
observations.append(observation) | ||
self.db.insert_observations(observations, self.input_file_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
simple/tests/stats/test_data/runner/input/config_driven/variable_per_row.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
entity,variable,date,value | ||
country/IND,var1,2020,0.16 | ||
country/IND,var2,2020,53 | ||
country/CHN,var1,2020,0.23 | ||
country/CHN,var2,2020,67 |
5 changes: 5 additions & 0 deletions
5
simple/tests/stats/test_data/runner/input/config_with_wildcards/variable_per_row.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
entity,variable,date,value | ||
country/IND,var1,2020,0.16 | ||
country/IND,var2,2020,53 | ||
country/CHN,var1,2020,0.23 | ||
country/CHN,var2,2020,67 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
simple/tests/stats/test_data/runner/input/input_dir_driven/variable_per_row.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
entity,variable,date,value | ||
country/IND,var1,2020,0.16 | ||
country/IND,var2,2020,53 | ||
country/CHN,var1,2020,0.23 | ||
country/CHN,var2,2020,67 |
7 changes: 7 additions & 0 deletions
7
simple/tests/stats/test_data/variable_per_row_importer/expected/custom_column_names.db.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
entity,variable,date,value,provenance | ||
country/BRA,var1,2023,0.19,c/p/default | ||
country/BRA,var2,2023,6,c/p/default | ||
country/JPN,var1,2023,0.21,c/p/default | ||
country/JPN,var2,2023,56,c/p/default | ||
country/USA,var2,2023,66,c/p/default | ||
country/CHN,var1,2022,-123.456,c/p/default |
7 changes: 7 additions & 0 deletions
7
simple/tests/stats/test_data/variable_per_row_importer/expected/default_column_names.db.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
entity,variable,date,value,provenance | ||
country/BRA,var1,2023,0.19,c/p/default | ||
country/BRA,var2,2023,6,c/p/default | ||
country/JPN,var1,2023,0.21,c/p/default | ||
country/JPN,var2,2023,56,c/p/default | ||
country/USA,var2,2023,66,c/p/default | ||
country/CHN,var1,2022,-123.456,c/p/default |
7 changes: 7 additions & 0 deletions
7
simple/tests/stats/test_data/variable_per_row_importer/input/custom_column_names.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Observed Variable,Observed Location,date,value | ||
var1,country/BRA,2023,0.19 | ||
var2,country/BRA,2023,6 | ||
var1,country/JPN,2023,0.21 | ||
var2,country/JPN,2023,56 | ||
var2,country/USA,2023,66 | ||
var1,country/CHN,2022,-123.456 |
7 changes: 7 additions & 0 deletions
7
simple/tests/stats/test_data/variable_per_row_importer/input/default_column_names.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
variable,entity,date,value | ||
var1,country/BRA,2023,0.19 | ||
var2,country/BRA,2023,6 | ||
var1,country/JPN,2023,0.21 | ||
var2,country/JPN,2023,56 | ||
var2,country/USA,2023,66 | ||
var1,country/CHN,2022,-123.456 |
Oops, something went wrong.