Skip to content

Python script

How to create a Python script for multiple datasets execution

This example shows how to read data files, process them, and write results for a RAISE experiment.

In this example, we will have this directory structure:

|-- utils/
| |-- cluster.py
|-- main.py
|-- requirements.txt
  • The main.py and requirements.txt files are mandatory and must have these specific filenames.
  • Other files can be added to use different Python modules; in this example, we are using the utils directory. These modules can have any name and directory structure, as long as they are correctly loaded in the main.py file.
  • The requirements.txt file should list all the required library versions for execution. All of them must be compatible with the Python runtime version selected for the experiment: 3.8, 3.9, 3.10, 3.11 or 3.12.
Compatible Python versions.

Import Required Libraries

There are no specific required libraries. Import what your script needs, and import other modules from your own files.

import json
from dotenv import load_dotenv
import pandas as pd
from utils.cluster import cluster_data,plot_clusters_figure

Import Data Files (shareable samples)

Only when working locally, you’ll have to download and organize your data files according to the required folder structure. (see also Dynamic Dataset(s) Accessing.) Each data file should be stored in a folder named after its ID (the data file’s UUID can be found in its metadata) in the same directory as the main.py script.

Experiment root directory. Dataset directory with datafile.

You can copy the dataset ID from the dataset details page on the RAISE portal.

Copy dataset ID.

Then, create a .env file that defines the variable RAISE_DATASET_ID_LIST. This variable should contain an ordered list of the dataset UUIDs in the following format:

RAISE_DATASET_ID_LIST=["00000000-0000-0000-0000-000000000001","00000000-0000-0000-0000-000000000002"]

In RAISE this list is filled in for you from the experiment’s dataset order. Regular files are named datafile.{ext} (for example .csv). A streamed dataset uses a snapshot file named datafile in the same folder.

Read Data Files

Once your experimental environment is ready, you can read the files:

load_dotenv()
dataset_ids = json.loads(os.getenv("RAISE_DATASET_ID_LIST"))
dataset_id_0 = dataset_ids[0]
dataset_id_1 = dataset_ids[1]
datafile_extension_0 = "csv"
datafile_extension_1 = "csv"
file_path_0 = f"{dataset_id_0}/datafile.{datafile_extension_0}"
file_path_1 = f"{dataset_id_1}/datafile.{datafile_extension_1}"

The data file must always be named datafile with the appropriate extension: e.g., .csv, .txt, .edf, .json, .parquet. There is no limit to the number of datasets that can be loaded. Use different functions to load data depending on the data type. For example, if you have CSV files, you can use read_csv from pandas:

data_csv_0 = pd.read_csv(file_path_0)
data_csv_1 = pd.read_csv(file_path_1)
data = pd.concat([data_csv_0, data_csv_1], ignore_index=True).sort_values(by=data_csv_0.columns[0])

Run the Code

In this section, you can add the code to process the data. The code will depend on the specific task you want to perform (e.g., clustering, analysis, etc.). Here’s an example:

clustered_data, reduced_clustered_data, cluster_centers = cluster_data(data=data)
clusters_figure = plot_clusters_figure(data=reduced_clustered_data, centroids=cluster_centers)

Gather Results

Any type of file can be taken as a result: images, CSV, text, and so on. The number of results is not limited. Write every output under the results directory at the same level as main.py.

os.makedirs("results", exist_ok=True)
clustered_data.to_csv("results/clustered_data.csv")
reduced_clustered_data.to_csv("results/reduced_clustered_data.csv")

Complete main.py script (download)

# Import Required Libraries
import json
from dotenv import load_dotenv
import pandas as pd
from utils.cluster import cluster_data,plot_clusters_figure
# NOTE: Remember to prepare the environment for local execution (see the "Import Data Files" section above)
# Read Data Files
load_dotenv()
dataset_ids = json.loads(os.getenv("RAISE_DATASET_ID_LIST"))
dataset_id_0 = dataset_ids[0]
dataset_id_1 = dataset_ids[1]
datafile_extension_0 = "csv"
datafile_extension_1 = "csv"
file_path_0 = f"{dataset_id_0}/datafile.{datafile_extension_0}"
file_path_1 = f"{dataset_id_1}/datafile.{datafile_extension_1}"
data_csv_0 = pd.read_csv(file_path_0)
data_csv_1 = pd.read_csv(file_path_1)
data = pd.concat([data_csv_0, data_csv_1], ignore_index=True).sort_values(by=data_csv_0.columns[0])
# Run the Code
# Add here the code to run. You can create different functions and the results should be clear stated.
clustered_data, reduced_clustered_data, cluster_centers = cluster_data(data=data)
clusters_figure = plot_clusters_figure(data=reduced_clustered_data, centroids=cluster_centers)
# Gather Results
os.makedirs("results", exist_ok=True)
clustered_data.to_csv("results/clustered_data.csv")
reduced_clustered_data.to_csv("results/reduced_clustered_data.csv")

requirements.txt file

dotenv

Logs

If the experiment does not fail, print() output from the script is logged. If it fails, the logs include the error that stopped execution. If main.py fails, you will see the reason (undefined variables, unexpected indentations, and so on). If the child container cannot be created, the logs include that reason (incompatible versions in requirements.txt, missing package versions, and so on).

You can find some examples at the templates section.