fairvis.data_io¶
Loading and validation of FAIR assessments and the indicators.
REQUIRED_COLUMNS
module-attribute
¶
REQUIRED_COLUMNS = (
"ID",
"Short",
"Type",
"Category",
"Priority",
"Indicator",
"Description",
"Assessment details",
"Assessment",
)
Columns of the template an assessment needs.
ASSESSMENT_VALUES
module-attribute
¶
Allowed assessment values besides NA (not applicable, an empty cell).
NA_STRINGS
module-attribute
¶
NA_STRINGS = frozenset(
{
"",
"#N/A",
"#N/A N/A",
"#NA",
"-1.#IND",
"-1.#QNAN",
"-NaN",
"-nan",
"1.#IND",
"1.#QNAN",
"<NA>",
"N/A",
"NA",
"NULL",
"NaN",
"None",
"n/a",
"nan",
"null",
}
)
Texts of a cell read as NA, the defaults of pandas.read_excel.
METADATA_COLUMNS
module-attribute
¶
Columns describing an indicator; taken from the template, not from an assessment.
MAX_ROWS
module-attribute
¶
Rows read from an assessment; the template has one row per indicator, far fewer.
Bounds the work of parsing an upload.
MAX_COLUMNS
module-attribute
¶
Columns read from an assessment; the template has 11.
A single cell in the last column of a sheet would otherwise make every row 16384 cells wide.
MAX_UNPACKED_BYTES
module-attribute
¶
Size of the unpacked content of an xlsx file, an assessment has less than 1 MB.
An xlsx file is a zip archive; the limit rejects a zip bomb before it is parsed.
assessment_class
¶
Class of an assessment value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
assessment of an indicator, NaN for NA. |
required |
Returns:
| Type | Description |
|---|---|
int
|
0 for NA, 1 for 0.0, 2 for 0.5 and 3 for 1.0. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the value is none of NA, 0, 0.5 and 1. |
template
¶
The FAIR assessment template, the definition of the indicators.
Returns:
| Type | Description |
|---|---|
DataFrame
|
the template as read from its xlsx file, one row per indicator. |
template_indicator_ids
¶
Indicator ids of the template, in the order of the template.
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
the |
validate_assessment
¶
Validate the structure and the values of a raw assessment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
assessment as read from the xlsx file, before the |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if a column of the template is missing, the indicators differ from the ones of the template or an assessment value is none of NA, 0, 0.5 and 1. |
read_first_sheet
¶
Read the first sheet of an xlsx file, at most MAX_ROWS rows and MAX_COLUMNS.
The first row is the header; empty rows are skipped, empty cells are NaN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path | BinaryIO
|
path of the xlsx file or an uploaded file. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
the rows of the sheet with the header as column names. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the file is no readable xlsx file, unpacks to more than
|
load_assessment
¶
Load an assessment from an xlsx file.
Only the assessment comes from the file; the columns describing the
indicators, see METADATA_COLUMNS, are the ones of the template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path | BinaryIO
|
path of the xlsx file or an uploaded file. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
assessment indexed by the indicator |
DataFrame
|
and without columns lacking a header (named |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the file is no readable xlsx file, unpacks to more than
|
load_model_assessments
¶
load_model_assessments(assessment_dir=ASSESSMENTS_PATH)
Load all model assessments of a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assessment_dir
|
Path
|
directory with one xlsx file per model. |
ASSESSMENTS_PATH
|
Returns:
| Type | Description |
|---|---|
dict[str, DataFrame]
|
assessments by model id (the file name without suffix), sorted by model id. |
load_indicators
¶
Load the indicators and count their assessments over all models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_models
|
dict[str, DataFrame]
|
assessments by model id, see |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
indicators indexed by |
DataFrame
|
models with NA, 0, 0.5 and 1 for the indicator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if an assessment has other indicators than the template. |