Performance increases by replacing "df.loc" by "df.at"

This commit is contained in:
dario 2023-12-18 00:13:22 +01:00
parent f5f30953ca
commit 58d362c8f9
8 changed files with 11 additions and 13 deletions

View File

@ -8,4 +8,4 @@ import spatz.transforms as transforms
from spatz.transforms import *
from spatz.dataset import *
from spatz.simulation import *
from spatz.simulation import *

View File

@ -0,0 +1 @@
from spatz.connections.serial import *

View File

@ -199,18 +199,18 @@ class Dataset(Advanceable):
float: Returns the requested value.
"""
if self.__interpolation == 'linear':
t_min = self.__df['time'].iloc[self.__idx]
t_max = self.__df['time'].iloc[self.__idx + 1]
t_min = self.__df.at[self.__idx, 'time']
t_max = self.__df.at[self.__idx + 1, 'time']
# Sometimes no time passes in-between two samples.
if t_max == t_min:
return self.__df[name].iloc[self.__idx]
return self.__df.at[name, self.__idx]
# Compute the weight for interpolation.
alpha = (self.get_time() - t_min) / (t_max - t_min)
# Interpolate linearly between the two data points.
return (1 - alpha) * self.__df[name].iloc[self.__idx] + alpha * self.__df[name].iloc[self.__idx + 1]
return (1 - alpha) * self.__df.at[self.__idx, name] + alpha * self.__df.at[self.__idx + 1, name]
def fetch_values(self, names: List[str]) -> np.array:
"""Get specific values from the dataframe.

View File

@ -49,9 +49,9 @@ class Logger(Advanceable):
self.__idx = -1
def _on_step(self, _: float):
self.__df = pd.concat([self.__df, pd.DataFrame()], ignore_index=True)
self.__df = pd.concat([pd.DataFrame(), self.__df], ignore_index=True, copy=False)
self.__idx += 1
self.__df.loc[self.__idx, 'time'] = self.get_time()
self.__df.at[self.__idx, 'time'] = self.get_time()
def _on_reset(self):
self.__df = pd.DataFrame.from_dict({'time': [self.get_time()]}).astype(np.float64)
@ -64,12 +64,12 @@ class Logger(Advanceable):
value (Any): The value to log.
domain (str, optional): The domain the value belongs to. Defaults to 'any'.
"""
name = domain + '/' + attrib
name = f'{domain}/{attrib}'
if name not in self.__df.columns:
self.__df[name] = pd.Series([pd.NA] * len(self.__df))
self.__df.loc[self.__idx, name] = value
self.__df.at[self.__idx, name] = value
def get_dataframe(self) -> pd.DataFrame:
return self.__df

View File

@ -2,7 +2,4 @@ from spatz.sensors.sensor import Sensor
from spatz.sensors.gps import GPS
from spatz.sensors.imu import Accelerometer, Gyroscope, IMU
from spatz.sensors.pressure import PressureSensor
from spatz.sensors.compound import CompoundSensor
from pandas import NA
from spatz.sensors.compound import CompoundSensor

View File

View File

View File