{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## CIRCADIPY\n",
"\n",
"This is the pipeline for basic execution of the CircadiPy library. Here you will find information needed to execute several features of the library."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1 - IMPORT LIBRARIES TO USE THE NOTEBOOK\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"## IMPORT LIBRARIES ----------------------------------------------------------------------------------------------------\n",
"\n",
"%matplotlib qt\n",
"\n",
"%reload_ext autoreload\n",
"%autoreload 3\n",
"\n",
"import matplotlib.pyplot as plt # Import matplotlib.pyplot to plot figures\n",
"import tkinter as tk # Import TK to open folder dialogs to select folders\n",
"from tkinter import filedialog # Import filedialog to open folder dialogs to select folders\n",
"import numpy # Import numpy to work with arrays and make calculations\n",
"import time # Import time to measure time \n",
"import warnings\n",
"from sys import platform # Import warnings to ignore warnings\n",
"warnings.filterwarnings('ignore') # Ignore warnings\n",
"\n",
"## IMPORT CIRCADIPY ----------------------------------------------------------------------------------------------------\n",
"\n",
"import chrono_reader as chr # Import chrono_reader to read data\n",
"import chrono_plotter as chp # Import chrono_plotter to plot data\n",
"import chrono_rithm as chrt # Import chrono_rithm to make calculations\n",
"import chrono_simulation as chs # Import chrono_simulation to simulate data\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2 - SELECT THE FOLDER TO SAVE THE SIMULATION\n",
"\n",
"In this first step we will generate a representative time series of the circadian activity of an experiment animal. The activity will be mimicked by sinusoids with noise that represent the number of movements made by an animal. To do this, we will first need to select a directory in which to save the simulated data"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"E:/github/circadipy/src/simulated_data\n"
]
}
],
"source": [
"## SET THE ENVIRONMENT -------------------------------------------------------------------------------------------------\n",
"\n",
"if platform == \"win32\":\n",
" root = tk.Tk() # Create the root window\n",
" root.attributes('-topmost',True) # Keep the window on top of others\n",
" root.iconify() # Hide the root window\n",
"\n",
" main_save_folder = filedialog.askdirectory(title=\"Select the folder to save the simulated data\", parent=root) # Ask for the folder to save the simulated data\n",
" root.destroy() # Destroy the root window\n",
"else:\n",
" ## If you are using a Mac, you will not be able to select the folder using the code above.\n",
" ## In this case, please set the folder manually here:\n",
" main_save_folder = \"Paste here the PATH/TO/SAVE/SIMULATED/DATA\"\n",
"\n",
"print(main_save_folder) # Print the folder to save the simulated data"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3 - RUN AND SAVE A SIMPLE SIMULATION\n",
"\n",
"Each simulation has a given number stages (_num_stages_), each stage can be configured with a single activity period. In this example we will simulate a protocol that has 3 stages and a sample rate of 5 minutes (this means that each 5 minutes, a measure is done): \n",
"\n",
"- A: a 5-day control period with a 12-12 hour light-dark (LD) cycle, \n",
"- B: a 10-day free-running period with a 24-hour dark (DD) cycle,\n",
"- C: a further 5 days of readaptation, with a 12-12 hour light-dark (LD) cycle.\n",
"
\n",
"\n",
"Following a biological pattern, animals regulated to a 12-12 hour light-dark cycle have an activity period of 24 hours, however, when turning off the light track, leaving it in a 24-hour dark cycle, these animals tend to present a shorter period (in the example we will use 23 hours and 30 minutes). These animals tend to synchronize again when the light-dark cycle is reactivated, returning to a 24-hour period.\n"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": [
"## CONFIGURE THE SIMULATION --------------------------------------------------------------------------------------------\n",
"\n",
"sampling_frequency = '5T' # Sampling frequency of the simulated data (30T = 30 minutes)\n",
"\n",
"num_stages = 3 # Number of cycles in each simulation\n",
"days_per_stage = [5, 10, 5] # Number of days in each stage\n",
"activity_period = [24, 23.5, 24] # Activity period in each stage\n",
"cycles_per_stage = ['LD', 'DD', 'LD'] # Type of cycles in each stage\n",
"stage_labels = ['A', 'B', 'C'] # Labels of each stage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To perform the simulation, the simulation module (_chrono_simulation_ imported as chs) of CircadiPy is used and the parameters discussed above are passed as arguments. The simulation can be generated in sine, square, square with low-pass filter, sawtooth and triangular format. In addition, Gaussian noise and its signal-to-noise ratio can be added. The user can also select whether negative values will be considered or not.\n",
"\n",
"**Note**: the simulated data was saved in an .asc file, which is a simple text file. As long as the pattern of this file is followed, CircadiPy is able to read and process your data."
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved successfully in E:/github/circadipy/src/simulated_data/simulated_data.asc\n"
]
}
],
"source": [
"## SIMULATE A EXPERIMENT AND SAVE THE DATA -----------------------------------------------------------------------------\n",
"\n",
"file_name = 'simulated_data' # Create the file name for the simulated data with noise\n",
"raw_data = chs.simulate_protocol(file_name, main_save_folder, sampling_frequency, days_per_stage, activity_period, \n",
" signal_type = 'square', noise = True, snr_db = 20, only_positive = True,\n",
" remove_file = True) # Simulate the data with noise (chrono_simulation.py)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run the next block to visualize the generated data. "
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [],
"source": [
"## PLOT THE RAW DATA ---------------------------------------------------------------------------------------------------\n",
"\n",
"fig = plt.figure(figsize = (12, 5)) # Create a figure\n",
"ax = fig.add_subplot(111) # Add a subplot\n",
"ax.plot(raw_data, color = 'maroon', linewidth = 2) # Plot the data with noise\n",
"ax.set_xlabel('Time (Hours)') # Set the x label\n",
"ax.set_ylabel('Activity') # Set the y label\n",
"ax.set_title('Simulated Data With Noise') # Set the title\n",
"plt.show() # Show the plot"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4 - IMPORT THE SIMULATION AND BUILD THE PROTOCOL TO BE STUDIED\n",
"\n",
"To study a certain protocol, simply use the _read_protocol_ function of the _chrono_reader_ module (imported as chr). Just configure the following parameters:\n",
"- Protocol name: a representative name (e.g. 'simulation_example')\n",
"- File containing the data: the .asc file path (can be other formats). It was previously saved using the variables _main_save_folder_ and _file_name_. It can also be accessed in one of the previously executed blocks.\n",
"- ZT0 reference: ZT is the time the light was switched on (in this example we will use 00:00) \n",
"- Labels dictionary: a dictionary containing the characteristics of each experimental stage. It is necessary to configure the type of cycle (e.g. light-dark , LD), a stage label (arbitrary name that will differentiate the stages) and the number of days of the stage. In our case, we will use the same variables used to create the simulation\n",
"- Type of data: the type of file to be read (it can be a generic .asc file, or a predefined template such as intellicage) \n",
"- Flag to consider the first day: if True, the first day of the experiment will be considered, if False, no\n",
"\n",
"At the end, the _protocol_ object (or variable) will be saved, it contains all the information needed to apply various features of CircadiPy. The data is stored in this object in the form of a table, which can be seen as a result of the block execution.\n"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Data read from: E:/github/circadipy/src/simulated_data/simulated_data.asc\n",
"\n",
"Table generated after the data importing\n"
]
},
{
"data": {
"text/html": [
"
| \n", " | values | \n", "is_night | \n", "cycle_types | \n", "test_labels | \n", "real_date | \n", "day | \n", "
|---|---|---|---|---|---|---|
| 2022-01-01 00:00:00 | \n", "101.0 | \n", "False | \n", "LD | \n", "A | \n", "2022-01-01 00:00:00 | \n", "2022-01-01 | \n", "
| 2022-01-01 00:05:00 | \n", "0.0 | \n", "False | \n", "LD | \n", "A | \n", "2022-01-01 00:05:00 | \n", "2022-01-01 | \n", "
| 2022-01-01 00:10:00 | \n", "10.0 | \n", "False | \n", "LD | \n", "A | \n", "2022-01-01 00:10:00 | \n", "2022-01-01 | \n", "
| 2022-01-01 00:15:00 | \n", "2.0 | \n", "False | \n", "LD | \n", "A | \n", "2022-01-01 00:15:00 | \n", "2022-01-01 | \n", "
| 2022-01-01 00:20:00 | \n", "0.0 | \n", "False | \n", "LD | \n", "A | \n", "2022-01-01 00:20:00 | \n", "2022-01-01 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 2022-01-20 23:35:00 | \n", "0.0 | \n", "True | \n", "LD | \n", "C | \n", "2022-01-20 23:35:00 | \n", "2022-01-20 | \n", "
| 2022-01-20 23:40:00 | \n", "8.0 | \n", "True | \n", "LD | \n", "C | \n", "2022-01-20 23:40:00 | \n", "2022-01-20 | \n", "
| 2022-01-20 23:45:00 | \n", "4.0 | \n", "True | \n", "LD | \n", "C | \n", "2022-01-20 23:45:00 | \n", "2022-01-20 | \n", "
| 2022-01-20 23:50:00 | \n", "3.0 | \n", "True | \n", "LD | \n", "C | \n", "2022-01-20 23:50:00 | \n", "2022-01-20 | \n", "
| 2022-01-20 23:55:00 | \n", "2.0 | \n", "True | \n", "LD | \n", "C | \n", "2022-01-20 23:55:00 | \n", "2022-01-20 | \n", "
5760 rows × 6 columns
\n", "| \n", " | significant | \n", "test | \n", "period | \n", "n_components | \n", "p | \n", "q | \n", "p_reject | \n", "q_reject | \n", "amplitude | \n", "CI(amplitude) | \n", "... | \n", "CI(acrophase) | \n", "p(acrophase) | \n", "q(acrophase) | \n", "mesor | \n", "CI(mesor) | \n", "p(mesor) | \n", "q(mesor) | \n", "acrophase_zt | \n", "acrophase_zt_lower | \n", "acrophase_zt_upper | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "1 | \n", "A | \n", "24.14 | \n", "1.0 | \n", "1.110223e-16 | \n", "1.158494e-16 | \n", "NaN | \n", "NaN | \n", "0.580089 | \n", "[0.5596956018197057, 0.6004821916021363] | \n", "... | \n", "[1.628973968528041, 1.7044476659115793] | \n", "0.0 | \n", "0.0 | \n", "0.481137 | \n", "[0.4677809779739576, 0.49449351542240333] | \n", "0.0 | \n", "0.0 | \n", "17.596496 | \n", "17.741481 | \n", "17.451512 | \n", "
| 1 | \n", "1 | \n", "B | \n", "23.51 | \n", "1.0 | \n", "1.110223e-16 | \n", "1.158494e-16 | \n", "NaN | \n", "NaN | \n", "0.586008 | \n", "[0.5680602716599603, 0.6039556246030819] | \n", "... | \n", "[0.8993948938205021, 0.9748685912040402] | \n", "0.0 | \n", "0.0 | \n", "0.482428 | \n", "[0.47275207399766567, 0.4921039752479259] | \n", "0.0 | \n", "0.0 | \n", "20.493504 | \n", "20.634705 | \n", "20.352302 | \n", "
| 2 | \n", "1 | \n", "C | \n", "23.96 | \n", "1.0 | \n", "1.110223e-16 | \n", "1.158494e-16 | \n", "1.110223e-16 | \n", "NaN | \n", "0.578468 | \n", "[0.5536516768067955, 0.6032844665068849] | \n", "... | \n", "[2.660447832769734, 2.735921530153274] | \n", "0.0 | \n", "0.0 | \n", "0.481172 | \n", "[0.4675651208797714, 0.4947779483121282] | \n", "0.0 | \n", "0.0 | \n", "13.710871 | \n", "13.854775 | \n", "13.566967 | \n", "
3 rows × 23 columns
\n", "