<jupyter_start><jupyter_text>Example Notebook and Installation CheckThis notebook provides an example of how to run an experiment with the `locomotion` package. It also acts as a check to make sure your package has been installed correctly.--- Import CheckFirst, we make sure that we are able to import the locomotion package.<jupyter_code>import sys
import os
PATH_TO_DIRECTORY = os.getcwd()

# If this works, then locomotion has been installed into your system.
import locomotion<jupyter_output><empty_output><jupyter_text>Data SetupNext, we setup the data for our experiment, which involves writing `.json` files that captures the experimental data for each dataset. This can be done manually, as we've done below, or by using `scripts/json_generator.ipynb`. Note that 1 `.json` file needs to be generated for each experimental group, with a JSON item for each dataset in the group. In our example, we have 2 experimental groups with 2 datasets each, thus we create 2 `.json` files, each containing 2 JSON items.<jupyter_code># No need to touch this
species = "medaka"
exp_type = "SS"
x_lims = (0, 200)
y_lims = (0, 100)
frames_per_sec = 20
pixels_per_mm = 1.6
start_time = 0
end_time = 2
baseline_start_time = 0
baseline_end_time = 1

import json
json_items_ss = [{
    "name": "SS_01",
    "data_file_location": PATH_TO_DIRECTORY + "/../samples/sample_data/SS_01.dat",
    "animal_attributes":
    {
      "species": species,
      "exp_type": exp_type,
      "ID": "01"
    },
    "capture_attributes": 
    {
      "x_lims": x_lims,
      "y_lims": y_lims,
      "pixels_per_mm": pixels_per_mm,
      "frames_per_sec": frames_per_sec,
      "start_time": start_time,
      "end_time": end_time,
      "baseline_start_time": baseline_start_time,
      "baseline_end_time": baseline_end_time
    },
    "additional_info": 
    {
      "point": [100, 100]
    }
  },
{
    "name": "SS_02",
    "data_file_location": PATH_TO_DIRECTORY + "/../samples/sample_data/SS_02.dat",
    "animal_attributes":
    {
      "species": species,
      "exp_type": exp_type,
      "ID": "02"
    },
    "capture_attributes": 
    {
      "x_lims": x_lims,
      "y_lims": y_lims,
      "pixels_per_mm": pixels_per_mm,
      "frames_per_sec": frames_per_sec,
      "start_time": start_time,
      "end_time": end_time,
      "baseline_start_time": baseline_start_time,
      "baseline_end_time": baseline_end_time
    },
    "additional_info": 
    {
      "point": [50, 50]
    }
  }]
json_items_nss = [{
    "name": "NSS_01",
    "data_file_location": PATH_TO_DIRECTORY + "/../samples/sample_data/NSS_01.dat",
    "animal_attributes":
    {
      "species": species,
      "exp_type": exp_type,
      "ID": "01"
    },
    "capture_attributes": 
    {
      "x_lims": x_lims,
      "y_lims": y_lims,
      "pixels_per_mm": pixels_per_mm,
      "frames_per_sec": frames_per_sec,
      "start_time": start_time,
      "end_time": end_time,
      "baseline_start_time": baseline_start_time,
      "baseline_end_time": baseline_end_time
    },
    "additional_info": 
    {
      "point": [30, 30]
    }
  },
{
    "name": "NSS_02",
    "data_file_location": PATH_TO_DIRECTORY + "/../samples/sample_data/NSS_02.dat",
    "animal_attributes":
    {
      "species": species,
      "exp_type": exp_type,
      "ID": "02"
    },
    "capture_attributes": 
    {
      "x_lims": x_lims,
      "y_lims": y_lims,
      "pixels_per_mm": pixels_per_mm,
      "frames_per_sec": frames_per_sec,
      "start_time": start_time,
      "end_time": end_time,
      "baseline_start_time": baseline_start_time,
      "baseline_end_time": baseline_end_time
    },
    "additional_info": 
    {
      "point": [10, 10]
    }
  }]

outfile_ss = PATH_TO_DIRECTORY + "/../samples/sample_check_SS.json"
outfile_nss = PATH_TO_DIRECTORY + "/../samples/sample_check_NSS.json"
jsonstr_ss = json.dumps(json_items_ss, indent=4)
jsonstr_nss = json.dumps(json_items_nss, indent=4)
with open(outfile_ss, "w") as outfile:
    outfile.write(jsonstr_ss)
    print("SS information wrote to %s" % outfile_ss)
with open(outfile_nss, "w") as outfile:
    outfile.write(jsonstr_nss)
    print("NSS information wrote to %s" % outfile_nss)<jupyter_output><empty_output><jupyter_text>Trajectory MethodsThis section will run through the 2 main methods provided in `trajectory.py`:* `trajectory.compute_all_bdd()` computes the pair-wise Behavioral Distortion Distance (BDD) between all animals in the experiment,* `trajectory.compute_all_iibdd()` computes the Intra-Individual BDD of each animal in the experiment, by computing the BDD of an animal to itself over a pair of non-overlapping time intervals of equal length.<jupyter_code>info_files = [outfile_ss, outfile_nss]
animals = locomotion.setup_animal_objs( info_files )
for a in animals:
    first_deriv, velocity = locomotion.trajectory.populate_velocity( a )
    locomotion.trajectory.populate_curvature(a, first_deriv=first_deriv, velocity=velocity)
    locomotion.trajectory.populate_distance_from_point(a, "point", 'Dist to Point', col_names=['X', 'Y'])
    
# Testing BDD methods
variables = ['Velocity','Curvature', 'Dist to Point']
norm_mode = 'spec'
number_of_comparisons_per_animal, specified_durations = 100, None
start_time, end_time = 0, 1
distances = locomotion.trajectory.compute_all_bdd(animals,
                                                variables,
                                                start_time,
                                                end_time,
                                                norm_mode)

output_directory, outfile_name = PATH_TO_DIRECTORY + "/results", "check_BDD"
try: # Safety check to ensure that data folder exists, and makes it otherwise.
    os.mkdir(output_directory)
except FileExistsError:
    pass
sort_table, square_table = False, False
color_min, color_max = 0.1, 0.2
locomotion.write.post_process(animals,
                             distances,
                             output_directory,
                             outfile_name,
                             sort_table,
                             square_table,
                             color_min,
                             color_max)
print(distances)
# Print result should be similar to the following:
# [['', 0.13316423888910794, 0.1479803274285245, 0.1514418903180464], 
# ['', '', 0.15586433304624217, 0.13717273747247993], 
# ['', '', '', 0.1608370955689277], 
# ['', '', '', '']]
# Testing IIBDD Methods
variables = ['Y','Velocity','Curvature']
norm_mode = 'spec'
num_samples = 5
iibdds = locomotion.trajectory.compute_all_iibdd(animals, variables, norm_mode, num_samples)
output_directory, outfile_name = PATH_TO_DIRECTORY + "/results", "check_IIBDD.csv"
try: # Safety check to ensure that data folder exists, and makes it otherwise.
    os.mkdir(output_directory)
except FileExistsError:
    pass
locomotion.write.write_iibdd_to_csv(animals, iibdds, output_directory, outfile_name)<jupyter_output><empty_output><jupyter_text>Heatmap MethodsThis section will run through the main method provided in `heatmap.py`, namely `heatmap.compute_all_csd()`, which calculates the pairwise Conformal Spatiotemporal Distance (CSD) between all animals in the experiment.<jupyter_code>grid_size, start_time, end_time = 10, 0, 2
for a in animals:
    locomotion.heatmap.populate_surface_data(a, grid_size, start_time, end_time)
distances = locomotion.heatmap.compute_all_csd(animals)
output_directory, outfile_name = PATH_TO_DIRECTORY + "/results", "results"
sort_table, square_table = False, False
color_min, color_max = 0, 0.02
locomotion.write.post_process(animals,
                             distances,
                             output_directory,
                             outfile_name,
                             sort_table,
                             square_table,
                             color_min,
                             color_max)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10378787878787879, 'fraction_numerical': 0.023863636363636365, 'mean_word_length': 3.0516624040920717, 'pattern_counts': {'":': 68, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '242225', 'license': 'MIT', 'n_tokens_mistral': 2758, 'n_tokens_neox': 2543, 'n_words': 604, 'path': 'scripts/example_notebook.ipynb', 'repo_name': 'mechunderlyingbehavior/locomotion', 'size': '7920'}