poulet-lab / experiment-template

This template serves as a guide for maintaining a standardized folder structure within an experiment.
GNU General Public License v3.0
0 stars 0 forks source link

Helper functions for adding and editing logbook.csv #5

Closed iezqrom closed 4 months ago

iezqrom commented 6 months ago

Would be great to have some helper function to standardise how people complete the logbook and also help them automate this in their workflow. Can start small and then add as needs come up?

vkaramanis commented 6 months ago

can you explain more what the helper function will do?

iezqrom commented 5 months ago

we should talk about this in person, here's a simple example in the meantime.

One function could simply add the timestamp to the logbook.csv. It'd standardise how we timestamp across the lab cos you can expect that people might use different formatting for the timestamps even, different libraries, etc. It'd also make researchers life easier cos it'd just be writing the function at the beginning of their session. Assertions could be written to ensure certain minimum standards are met (e.g. the logbook.csv exists!)

This is one example. There are other helper functions that can be written just for timestamp (e.g. retrieve all rows on x day). Other columns will have their own helper functions. This will build up a lab's library. This could then be the foundation of the back-end. By writing them as helper functions here, we can do quick testing and shorten the feedback loops are we were discussing. In a way, this idea would be writing the back- and front-end "at the same time".

iezqrom commented 5 months ago

here's an example i've written

from datetime import datetime
import os

def append_timestamp(data):
    """
    Appends the current timestamp to the given data.

    Parameters:
        data (list): The data to which the timestamp will be appended.

    Returns:
        list: The data with the current timestamp appended at the beginning.
    """
    current_timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    return [current_timestamp] + data

def add_log_entry(file_path, subject_id, method, notes):
    """
    Adds an entry to the logbook CSV file with the current timestamp.

    Parameters:
        file_path (str): Path to the CSV file.
        subject_id (str): ID of the subject.
        method (str): Method used.
        notes (str): Additional notes.
    """
    name_logbook_file = 'logbook.csv'
    file_path = os.path.join(file_path, name_logbook_file)
    os.makedirs(os.path.dirname(file_path), exist_ok=True)
    log_entry_data = [subject_id, method, notes]
    log_entry_with_timestamp = append_timestamp(log_entry_data)

    try:
        file_exists = os.path.isfile(file_path)
        with open(file_path, mode='a', newline='') as file:
            writer = csv.writer(file)
            if not file_exists:
                writer.writerow(['timestamp', 'subject_id', 'method', 'notes'])
            writer.writerow(log_entry_with_timestamp)
        print(f"Log entry added: {log_entry_with_timestamp}")
    except Exception as e:
        print(f"Error adding log entry: {e}")
vkaramanis commented 4 months ago

will be in poulet_py I close this