bluesky icon indicating copy to clipboard operation
bluesky copied to clipboard

Make pandas dataframes traffic arrays

Open amorfinv opened this issue 1 year ago • 1 comments

Here is a draft pull requests that makes pandas DataFrame traffic arrays. It should also work with a GeoDataFrame. This should help with performing vectorised calculations like when using state based detection along track.

-Andres

amorfinv avatar Jul 10 '22 21:07 amorfinv

Here is a simple example of when a dataframe could be helpful.


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
        
        # do some logging

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

However, the main improvements I think would come from using geopandas. I will make an example soon.

-Andres

amorfinv avatar Jul 20 '22 10:07 amorfinv