Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make pandas dataframes traffic arrays #400

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion bluesky/core/trafficarrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# In python <3.3 collections.abc doesn't exist
from collections import Collection
import numpy as np
import pandas as pd

defaults = {"float": 0.0, "int": 0, "uint":0, "bool": False, "S": "", "str": ""}

Expand Down Expand Up @@ -51,6 +52,7 @@ def __init__(self):
self._children = []
self._ArrVars = []
self._LstVars = []
self._DFVars = []

def reparent(self, newparent):
''' Give TrafficArrays object a new parent. '''
Expand All @@ -69,6 +71,8 @@ def _init_trafarrays(self, keys):
self._LstVars.append(key)
elif isinstance(self.__dict__[key], np.ndarray):
self._ArrVars.append(key)
elif isinstance(self.__dict__[key], pd.DataFrame):
self._DFVars.append(key)
elif isinstance(self.__dict__[key], TrafficArrays):
self.__dict__[key].reparent(self)

Expand All @@ -91,9 +95,21 @@ def create(self, n=1):
vartype = ''.join(c for c in str(self.__dict__[v].dtype) if c.isalpha())
self.__dict__[v] = np.append(self.__dict__[v], [defaults.get(vartype, 0)] * n)

for v in self._DFVars:
# get types of all columns in dataframe
dtypes = self.__dict__[v].dtypes
new_rows = pd.DataFrame(index=np.arange(n), columns=self.__dict__[v].columns)

# append default values to dataframe
for column, dtype in dtypes.items():
# Get type without byte length
vartype = ''.join(c for c in str(dtype) if c.isalpha())
new_rows.loc[:, column] = pd.DataFrame([defaults.get(vartype, 0)] * n, columns=[column])
self.__dict__[v] = pd.concat([self.__dict__[v], new_rows], ignore_index=True)

def istrafarray(self, name):
''' Returns true if parameter 'name' is a traffic array. '''
return name in self._LstVars or name in self._ArrVars
return name in self._LstVars or name in self._ArrVars or name in self._DFVars

def create_children(self, n=1):
''' Call create (aircraft create) on all children. '''
Expand All @@ -119,6 +135,11 @@ def delete(self, idx):
for v in self._LstVars:
del self.__dict__[v][idx]

if self._DFVars:
for v in self._DFVars:
self.__dict__[v] = self.__dict__[v].drop(idx)
self.__dict__[v].reset_index(drop=True, inplace=True)

def reset(self):
''' Delete all elements from arrays and start at 0 aircraft. '''
for child in self._children:
Expand All @@ -129,3 +150,12 @@ def reset(self):

for v in self._LstVars:
self.__dict__[v] = []

for v in self._DFVars:
dtypes = self.__dict__[v].dtypes
pd_series = {}
for column, dtype in dtypes.items():
# Get type without byte length
vartype = ''.join(c for c in str(dtype) if c.isalpha())
pd_series[column] = pd.Series([], dtype=vartype)
self.__dict__[v] = pd.DataFrame(pd_series)
54 changes: 54 additions & 0 deletions plugins/dftraffarray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import numpy as np
import pandas as pd
import geopandas as gpd
from datetime import datetime, timedelta

from bluesky import core, sim

def init_plugin():
''' Plugin initialisation function. '''
example = DF_arrays()

config = {
'plugin_name': 'DFEXAMPLE',
'plugin_type': 'sim',
}

return config


class DF_arrays(core.Entity):

def __init__(self):
super().__init__()

with self.settrafarrays():
self.flight_data = pd.DataFrame({
'npassengers': pd.Series(dtype=int),
'mean_fare': pd.Series(dtype=float),
'departure_time': pd.Series(dtype=datetime),
'arrival_time': pd.Series(dtype=datetime),
'predicted_arrival_time': pd.Series(dtype=datetime),
'flight_distance': pd.Series(dtype=float),
'predicted_flight_distance': pd.Series(dtype=float),
})

def create(self, n=1):
super().create(n)

# Get some data at start of simulation
self.flight_data.loc[-n:, 'npassengers'] = 10
self.flight_data.loc[-n:, 'mean_fare'] = 100
self.flight_data.loc[-n:, 'departure_time'] = datetime(2022, 1, 1) + timedelta(seconds = sim.simt)
self.flight_data.loc[-n:, 'predicted_arrival_time'] = datetime(2022, 1, 1) + timedelta(seconds = sim.simt) + timedelta(seconds=randint(0, 100))
self.flight_data.loc[-n:, 'predicted_flight_distance'] = randint(0, 100)


def delete(self, idx):

# Get arrival time and flight distance
self.flight_data.loc[idx, 'arrival_time'] = self.flight_data.loc[idx, 'departure_time'] + timedelta(seconds = sim.simt)
self.flight_data.loc[idx, 'flight_distance'] = traf.distflown

# Call the actual delete function
super().delete(idx)