Skip to content
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
13 changes: 5 additions & 8 deletions databases/experiment_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ def setup_groups(self, group_names, cage_capacity):
def add_measurement(self, animal_id, value):
'''Adds a new measurement for an animal.'''
timestamp = datetime.now()
self._c.execute('''INSERT INTO animal_measurements (animal_id, timestamp, value)
VALUES (?, ?, ?)''',
(animal_id, timestamp, value))
self._c.execute('''INSERT INTO animal_measurements (animal_id, timestamp, value, measurement_id)
VALUES (?, ?, ?,?)''',
(animal_id, timestamp, value, 1))
self._conn.commit()

def get_measurements_by_date(self, date):
Expand Down Expand Up @@ -502,7 +502,7 @@ def export_to_single_formatted_csv(self, directory):
experiment_df = pd.read_sql_query("SELECT * FROM experiment", self._conn)
animals_df = pd.read_sql_query("SELECT * FROM animals", self._conn)
groups_df = pd.read_sql_query("SELECT * FROM groups", self._conn)
measurements_df = pd.read_sql_query("SELECT * FROM animal_measurements", self._conn)
measurements_df = pd.read_sql_query("SELECT * FROM animal_measurements WHERE measurement_id IS NOT 0", self._conn)

# Add fixed metadata (like measurement name) to experiment table if needed
experiment_df["measurement"] = "Weight" # or fetch from DB if stored
Expand All @@ -511,9 +511,6 @@ def export_to_single_formatted_csv(self, directory):
merged_df = measurements_df.merge(animals_df, on="animal_id", how="left")
merged_df = merged_df.merge(groups_df, on="group_id", how="left")

# Ensure timestamp is datetime
merged_df["timestamp"] = pd.to_datetime(merged_df["timestamp"])

# Pivot the data
pivot_df = merged_df.pivot_table(
index=["name", "animal_id"], # group name and animal id
Expand All @@ -540,7 +537,7 @@ def export_to_single_formatted_csv(self, directory):


def export_to_csv(self, directory):
'''Exports all relevant tables in the database to a folder named after the experiment in the specified directory.'''
'''Exports the full database for testing purposes. Exports each table as a separate file to a folder in the specified directory.'''
import pandas as pd

# Get the experiment name
Expand Down
2 changes: 1 addition & 1 deletion experiment_pages/experiment/data_collection_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def check_for_data():
current_index = self.animal_ids.index(animal_id)

while current_index < len(self.animal_ids) and self.thread_running:
if len(data_handler.received_data) >= 2: # Customize condition
if len(data_handler.received_data) >= 2 and data_handler.received_data != " " and data_handler.received_data is not None and data_handler.received_data != 0:
received_data = data_handler.get_stored_data()
entry.insert(1, received_data)
data_handler.stop()
Expand Down