sustaingym.envs.evcharging.utils#

Implements utility methods for interacting with ACN-data and GMMs.

When run on its own, this script downloads the default data found in

sustaingym/data/evcharging/acn_data.

Module Contents#

Functions#

to_la_dt(→ datetime.datetime)

Converts string '%Y-%m-%d' to datetime localized in LA Time.

site_str_to_site(→ acnportal.acnsim.ChargingNetwork)

Returns charging network from string.

get_sessions(→ collections.abc.Iterator[dict[str, Any]])

Retrieves charging sessions using ACNData.

fetch_real_events(→ pandas.DataFrame)

Returns a pandas DataFrame of charging events from ACN-Data.

get_real_events(→ pandas.DataFrame)

Returns a pandas DataFrame of charging events.

get_model_name(→ str)

Returns folder name for a trained GMM.

save_gmm_model(→ None)

Saves GMM (presumably trained) and other information to directory.

load_gmm_model(→ dict[str, ...)

Load pickled GMM and other data from folder.

round(→ numpy.ndarray)

Round array values when decimal is above threshold.

download_default_acndata(→ None)

Downloads default data from ACNData.

Attributes#

sustaingym.envs.evcharging.utils.API_TOKEN = 'DEMO_TOKEN'[source]#
sustaingym.envs.evcharging.utils.GMMS_DIR = 'gmms'[source]#
sustaingym.envs.evcharging.utils.AM_LA[source]#
sustaingym.envs.evcharging.utils.GMT[source]#
sustaingym.envs.evcharging.utils.DATE_FORMAT = '%Y-%m-%d'[source]#
sustaingym.envs.evcharging.utils.DT_STRING_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'[source]#
sustaingym.envs.evcharging.utils.MINS_IN_DAY = 1440[source]#
sustaingym.envs.evcharging.utils.ONE_DAY[source]#
sustaingym.envs.evcharging.utils.REQ_ENERGY_SCALE = 100[source]#
sustaingym.envs.evcharging.utils.START_DATE[source]#
sustaingym.envs.evcharging.utils.END_DATE[source]#
sustaingym.envs.evcharging.utils.DEFAULT_DATE_RANGES = (('2019-05-01', '2019-08-31'), ('2019-09-01', '2019-12-31'), ('2020-02-01', '2020-05-31'),...[source]#
sustaingym.envs.evcharging.utils.DEFAULT_PERIOD_TO_RANGE[source]#
sustaingym.envs.evcharging.utils.DefaultPeriodStr[source]#
sustaingym.envs.evcharging.utils.SiteStr[source]#
sustaingym.envs.evcharging.utils.GMM_KEY = 'gmm'[source]#
sustaingym.envs.evcharging.utils.COUNT_KEY = 'count'[source]#
sustaingym.envs.evcharging.utils.STATION_USAGE_KEY = 'station_usage'[source]#
sustaingym.envs.evcharging.utils.to_la_dt(s: str) datetime.datetime[source]#

Converts string ‘%Y-%m-%d’ to datetime localized in LA Time.

Parameters:

s (str) –

Return type:

datetime.datetime

sustaingym.envs.evcharging.utils.site_str_to_site(site: SiteStr) acnportal.acnsim.ChargingNetwork[source]#

Returns charging network from string.

Parameters:

site (SiteStr) –

Return type:

acnportal.acnsim.ChargingNetwork

sustaingym.envs.evcharging.utils.get_sessions(start_date: datetime.datetime, end_date: datetime.datetime, site: SiteStr = 'caltech') collections.abc.Iterator[dict[str, Any]][source]#

Retrieves charging sessions using ACNData.

Parameters:
  • start_date (datetime.datetime) – beginning time of interval. Only year, month, and day are considered. The datetime is expected to be localized in LA time, the timezone of the charging garages.

  • end_date (datetime.datetime) – ending time of interval, exclusive. See start_date.

  • site (SiteStr) – ‘caltech’ or ‘jpl’

Returns:

sessions – iterator of sessions with a connection time starting on start_date and ending the day before end_date

Return type:

collections.abc.Iterator[dict[str, Any]]

Example:

fall2020_sessions = get_sessions(
        datetime(2020, 9, 1), datetime(2020, 12, 1))
sustaingym.envs.evcharging.utils.fetch_real_events(start_date: datetime.datetime, end_date: datetime.datetime, site: SiteStr) pandas.DataFrame[source]#

Returns a pandas DataFrame of charging events from ACN-Data.

See get_sessions() for arguments.

Returns:

events – DataFrame containing charging info

arrival                   datetime64[ns, America/Los_Angeles]
departure                 datetime64[ns, America/Los_Angeles]
requested_energy (kWh)    float64
delivered_energy (kWh)    float64
station_id                str
session_id                str
estimated_departure       datetime64[ns, America/Los_Angeles]
claimed                   bool
Parameters:
  • start_date (datetime.datetime) –

  • end_date (datetime.datetime) –

  • site (SiteStr) –

Return type:

pandas.DataFrame

sustaingym.envs.evcharging.utils.get_real_events(start_date: datetime.datetime, end_date: datetime.datetime, site: SiteStr) pandas.DataFrame[source]#

Returns a pandas DataFrame of charging events.

Either loads data from package or retrieves from ACN-Data.

See fetch_real_events() for arguments and return value, except function is now inclusive of end_date.

Parameters:
  • start_date (datetime.datetime) –

  • end_date (datetime.datetime) –

  • site (SiteStr) –

Return type:

pandas.DataFrame

sustaingym.envs.evcharging.utils.get_model_name(begin: datetime.datetime, end: datetime.datetime, n_components: int) str[source]#

Returns folder name for a trained GMM.

Parameters:
  • begin (datetime.datetime) –

  • end (datetime.datetime) –

  • n_components (int) –

Return type:

str

sustaingym.envs.evcharging.utils.save_gmm_model(site: SiteStr, gmm: sklearn.mixture.GaussianMixture, cnt: numpy.ndarray, sid: numpy.ndarray, begin: datetime.datetime, end: datetime.datetime, n_components: int) None[source]#

Saves GMM (presumably trained) and other information to directory.

Parameters:
  • site (SiteStr) – either ‘caltech’ or ‘jpl’

  • gmm (sklearn.mixture.GaussianMixture) – trained Gaussian Mixture Model

  • cnt (numpy.ndarray) – a 1-D np.ndarray session counts per day during date period, expected to have the same length as the number of days, inclusive, in the date period

  • sid (numpy.ndarray) – a 1-D np.ndarray stations’ usage counts for entire date period, expected to have the same length as the number of stations in the network

  • begin (datetime.datetime) – beginning of training period, for folder name

  • end (datetime.datetime) – ending of training period, for folder name

  • n_components (int) – number of GMM components

Return type:

None

sustaingym.envs.evcharging.utils.load_gmm_model(site: SiteStr, begin: datetime.datetime, end: datetime.datetime, n_components: int) dict[str, numpy.ndarray | sklearn.mixture.GaussianMixture][source]#

Load pickled GMM and other data from folder.

If searching for a custom model, searches relative to the current working directory in GMMS_DIR. If searching for a default model, searches inside the data folder.

Parameters:
  • site (SiteStr) – either ‘caltech’ or ‘jpl’

  • begin (datetime.datetime) – start date of date range GMM is trained in

  • end (datetime.datetime) – end date of date range GMM is trained in

  • n_components (int) – number of GMM components

Returns:

data – dict containing the following key-value pairs:

  • ‘gmm’: mixture.GaussianMixture, trained gmm, date range and components are specified on folder

  • ‘count’: np.ndarray, session counts per day

  • ‘station_usage’: np.ndarray, stations’ usage counts for date range

Return type:

dict[str, numpy.ndarray | sklearn.mixture.GaussianMixture]

sustaingym.envs.evcharging.utils.round(arr: numpy.ndarray, thresh: float = 0.7) numpy.ndarray[source]#

Round array values when decimal is above threshold.

Same as np.round if thresh = 0.5

Parameters:
  • arr (numpy.ndarray) – input array

  • thresh (float) – decimal between 0 and 1

Returns:

rounded array

Return type:

numpy.ndarray

sustaingym.envs.evcharging.utils.download_default_acndata() None[source]#

Downloads default data from ACNData.

Return type:

None