Parsers#

Concrete class to handler parser models.

This module provides the abstract class to create parser objects.

class r2x.parser.handler.BaseParser(config, data=<factory>)

Class that defines the shared methods of parsers.

Note

This class is meant to be use for developing new parsers. Do not use it directly.

Parameters:
config

Scenario configuration

Type:

Scenario

data

We save each file read in a data dictionary

Type:

dict

get_data(key='load')

Return the parsed data for load.

Parameters:

key (str)

Return type:

Any

read_file(fpath='load.csv')

Read load data.

Parameters:
parse_data()

Read all files from a configuration file map.

Parameters:
Return type:

None

abstract build_system()

Create the infra_sys model.

Return type:

System

get_data(key)

Return data.

Parameters:

key (str)

Return type:

Any

parse_data(*, base_folder, fmap, filter_func=None, **kwargs)

Parse all the data for the given translation.

Parameters:
Return type:

None

read_file(fpath, filter_funcs=None, **kwargs)

Read input model data from the file system.

Currently supported formats:
  • .csv

  • .h5

  • .xml

More to come!

Parameters:
  • fpath (Path, str) – Absolute location of the file in the system

  • filter_func (List) – Filter functions to apply

  • filter_funcs (list[Callable] | None)

class r2x.parser.handler.PCMParser(config, data=<factory>)

Class defining shared methods for PCM (currently plexos and sienna) parsers.

Parameters:
r2x.parser.handler.csv_handler(fpath, csv_file_encoding='utf8', **kwargs)

Parse CSV files and return a Polars DataFrame with all column names in lowercase.

Parameters:
  • fpath (str) – The file path of the CSV file to read.

  • csv_file_encoding (str, optional) – The encoding format of the CSV file, by default “utf8”.

  • **kwargs (dict, optional) – Additional keyword arguments passed to the pl.read_csv function.

Returns:

The parsed CSV file as a Polars DataFrame with lowercase column names if successful, or None if the file was not found.

Return type:

pl.DataFrame or None

Raises:
  • pl.exceptions.ComputeError – Raised if there are issues with the data types in the CSV file.

  • FileNotFoundError – Raised if the file is not found.

See also

pl_lowercase

Function to convert all column names of a Polars DataFrame to lowercase.

Example

>>> df = csv_handler("data/example.csv")
>>> print(df)
shape: (2, 3)
┌─────┬────────┬──────┐
│ id  │ name   │ age  │
│ --- │ ---    │ ---  │
│ i64 │ str    │ i64  │
╞═════╪════════╪══════╡
│ 1   │ Alice  │ 30   │
│ 2   │ Bob    │ 24   │
└─────┴────────┴──────┘
r2x.parser.handler.file_handler(fpath, optional=False, **kwargs)

Return FileHandler based on file extension.

Raises:
Parameters:
Return type:

LazyFrame | DataFrame | Sequence | XMLHandler | None

r2x.parser.handler.get_parser_data(config, parser_class, filter_funcs=None, **kwargs)

Return parsed system.

This function will create the ReEDS DataPortal and populate with the most common set of data needed

Paremters#

config Scenario configuration class parser

Parser to process

filter_func

Functions that will applied to read_data process

param kwargs:
year

For filtering by solve year

year_column

To change the column to apply the filter

column_mapping

For renaming columns

See also

BaseParser, pl_filter_year, pl_lower_case, pl_rename

Parameters:
Return type:

BaseParser

ReEDS#

Functions related to parsers.

class r2x.parser.reeds.ReEDSParser(*args, **kwargs)#

ReEDS parser class.

build_system()#

Create IS system for the ReEDS model.

Return type:

System

get_data(key)#

Return data.

Parameters:

key (str)

Return type:

Any

parse_data(*, base_folder, fmap, filter_func=None, **kwargs)#

Parse all the data for the given translation.

Parameters:
Return type:

None

read_file(fpath, filter_funcs=None, **kwargs)#

Read input model data from the file system.

Currently supported formats:
  • .csv

  • .h5

  • .xml

More to come!

Parameters:
  • fpath (Path, str) – Absolute location of the file in the system

  • filter_func (List) – Filter functions to apply

  • filter_funcs (list[Callable] | None)

r2x.parser.reeds.cli_arguments(parser)#

CLI arguments for the plugin.

Parameters:

parser (ArgumentParser)

Sienna#

Plexos#

Plexos parser class.

class r2x.parser.plexos.PlexosParser(*args, xml_file=None, **kwargs)#

Plexos parser class.

Parameters:

xml_file (str | None)

build_system()#

Create infrasys system.

Return type:

System

get_data(key)#

Return data.

Parameters:

key (str)

Return type:

Any

parse_data(*, base_folder, fmap, filter_func=None, **kwargs)#

Parse all the data for the given translation.

Parameters:
Return type:

None

read_file(fpath, filter_funcs=None, **kwargs)#

Read input model data from the file system.

Currently supported formats:
  • .csv

  • .h5

  • .xml

More to come!

Parameters:
  • fpath (Path, str) – Absolute location of the file in the system

  • filter_func (List) – Filter functions to apply

  • filter_funcs (list[Callable] | None)

r2x.parser.plexos.cli_arguments(parser)#

CLI arguments for the plugin.

Parameters:

parser (ArgumentParser)