From 58d362c8f9f324c9178c608e14a34f854ba6d7dc Mon Sep 17 00:00:00 2001 From: dario Date: Mon, 18 Dec 2023 00:13:22 +0100 Subject: [PATCH] Performance increases by replacing "df.loc" by "df.at" --- spatz/__init__.py | 2 +- spatz/connections/__init__.py | 1 + spatz/dataset.py | 8 ++++---- spatz/logger.py | 8 ++++---- spatz/sensors/__init__.py | 5 +---- spatz/simulations/__init__.py | 0 spatz/simulations/astos/__init__.py | 0 spatz/simulations/rocketpy/__init__.py | 0 8 files changed, 11 insertions(+), 13 deletions(-) create mode 100644 spatz/simulations/__init__.py create mode 100644 spatz/simulations/astos/__init__.py create mode 100644 spatz/simulations/rocketpy/__init__.py diff --git a/spatz/__init__.py b/spatz/__init__.py index 9e26784..44d9aee 100644 --- a/spatz/__init__.py +++ b/spatz/__init__.py @@ -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 * \ No newline at end of file diff --git a/spatz/connections/__init__.py b/spatz/connections/__init__.py index e69de29..5de1c59 100644 --- a/spatz/connections/__init__.py +++ b/spatz/connections/__init__.py @@ -0,0 +1 @@ +from spatz.connections.serial import * \ No newline at end of file diff --git a/spatz/dataset.py b/spatz/dataset.py index 9e70ce3..55a9680 100644 --- a/spatz/dataset.py +++ b/spatz/dataset.py @@ -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. diff --git a/spatz/logger.py b/spatz/logger.py index 897f2df..1ae1aa2 100644 --- a/spatz/logger.py +++ b/spatz/logger.py @@ -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 \ No newline at end of file diff --git a/spatz/sensors/__init__.py b/spatz/sensors/__init__.py index 8084f96..fa85c4b 100644 --- a/spatz/sensors/__init__.py +++ b/spatz/sensors/__init__.py @@ -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 \ No newline at end of file +from spatz.sensors.compound import CompoundSensor \ No newline at end of file diff --git a/spatz/simulations/__init__.py b/spatz/simulations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/spatz/simulations/astos/__init__.py b/spatz/simulations/astos/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/spatz/simulations/rocketpy/__init__.py b/spatz/simulations/rocketpy/__init__.py new file mode 100644 index 0000000..e69de29