Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.ipynb_checkpoints
__pycache__
Empty file added Jeans_Package/__init__.py
Empty file.
49 changes: 49 additions & 0 deletions Jeans_Package/compute_motion_displacement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#Importing relevant packages
import pandas as pd
import numpy as np
from os import mkdir, chdir, getcwd, path, remove

#Below are the code used to compute displacement mean for each participant
def move_into_directory(participant):
"""Move into the directory where each participant's motion file is saved"""
path = "Motion_files/" + participant
chdir(path)

def set_motion_file():
"""Check to make sure motion file exist and set the motion file to be the one the script wants to operate on"""
if path.isfile('rp_s_full.txt'):
column_names = ['roll', 'pitch', 'yaw', 'z', 'x', 'y']
file = pd.read_csv('rp_s_full.txt', names = column_names, delim_whitespace = True)
df = pd.DataFrame(file, columns = column_names)
else:
print("no motion file found!")
return df

def compute_mean_for_each_column(motion_dataframe):
"""Compute means for each of the six motion parameters"""
means_separated = []
for i in range(6):
mean = motion_dataframe.iloc[:,i].mean()
means_separated.append(mean)
return means_separated

def compute_mean_of_all_columns(means_separated):
"""Compute the mean of the six motion parameter averages"""
column_means = sum(means_separated)/6
return column_means

def saving_out_text_files_with_averages(means,column_means):
"""Save the averages computed by the previous two functions in text files in the participant's directory"""
np.savetxt('Mean_of_Each_Motion_Parameters', X = means)
np.savetxt('Mean_of_All_Motion_Parameters', X = [column_means])

def main(participant):
"""This function puts all the previous functions in the script together"""
basedir = '/Users/yesji/test_project_folder/project_spring_2020'
chdir(basedir)
move_into_directory(participant)
motion_dataframe = set_motion_file()
means_separated = compute_mean_for_each_column(motion_dataframe)
column_means = compute_mean_of_all_columns(means_separated)
saving_out_text_files_with_averages(means_separated,column_means)
chdir(basedir)
1 change: 1 addition & 0 deletions Jeans_Package/sample_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import sys
472 changes: 472 additions & 0 deletions Motion_files/Participant1/rp_s_full.txt

Large diffs are not rendered by default.

236 changes: 236 additions & 0 deletions Motion_files/Participant2/rp_s_full.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Participant_List.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Participant1
Participant2
191 changes: 191 additions & 0 deletions Python_projects_JY.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Packaging with python"
]
},
{
"cell_type": "code",
"execution_count": 289,
"metadata": {},
"outputs": [],
"source": [
"# Please make sure to run the script below in the project_spring_2020 folder!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/Users/yesji/test_project_folder/project_spring_2020'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%pwd"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"#Importing relevant packages\n",
"import pandas as pd\n",
"import numpy as np \n",
"from os import mkdir, chdir, getcwd, path, remove\n",
"\n",
"#Below are the code used to compute displacement mean for each participant \n",
"def move_into_directory(participant):\n",
" \"\"\"Move into the directory where each participant's motion file is saved\"\"\"\n",
" path = \"Motion_files/\" + participant\n",
" chdir(path)\n",
" \n",
"def set_motion_file():\n",
" \"\"\"Check to make sure motion file exist and set the motion file to be the one the script wants to operate on\"\"\"\n",
" if path.isfile('rp_s_full.txt'):\n",
" column_names = ['roll', 'pitch', 'yaw', 'z', 'x', 'y']\n",
" file = pd.read_csv('rp_s_full.txt', names = column_names, delim_whitespace = True)\n",
" df = pd.DataFrame(file, columns = column_names)\n",
" else:\n",
" print(\"no motion file found!\")\n",
" return df \n",
"\n",
"def compute_mean_for_each_column(motion_dataframe):\n",
" \"\"\"Compute means for each of the six motion parameters\"\"\"\n",
" means_separated = []\n",
" for i in range(6):\n",
" mean = motion_dataframe.iloc[:,i].mean()\n",
" means_separated.append(mean)\n",
" return means_separated\n",
"\n",
"def compute_mean_of_all_columns(means_separated):\n",
" \"\"\"Compute the mean of the six motion parameter averages\"\"\"\n",
" column_means = sum(means_separated)/6\n",
" return column_means\n",
"\n",
"def saving_out_text_files_with_averages(means,column_means):\n",
" \"\"\"Save the averages computed by the previous two functions in text files\"\"\"\n",
" np.savetxt('Mean_of_Each_Motion_Parameters', X = means)\n",
" np.savetxt('Mean_of_All_Motion_Parameters', X = [column_means])\n",
"\n",
"def main(participant):\n",
" \"\"\"This function puts all the previous functions in the script together\"\"\"\n",
" basedir = '/Users/yesji/test_project_folder/project_spring_2020'\n",
" chdir(basedir)\n",
" move_into_directory(participant)\n",
" motion_dataframe = set_motion_file()\n",
" means_separated = compute_mean_for_each_column(motion_dataframe)\n",
" column_means = compute_mean_of_all_columns(means_separated)\n",
" saving_out_text_files_with_averages(means_separated,column_means)\n",
" chdir(basedir)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/Users/yesji/test_project_folder/project_spring_2020\n"
]
}
],
"source": [
"# Here is a block of code that will allow the user to compute motion displacement means for a list of participants \n",
"%cd /Users/yesji/test_project_folder/project_spring_2020\n",
"\n",
"participant_list = []\n",
"list_file = open(\"Participant_List.txt\",'r')\n",
"for line in list_file.readlines():\n",
" participant_list.append(line.split()[0])\n",
"#print(participant_list)\n",
" \n",
"for participant in participant_list:\n",
" main(participant)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1m============================= test session starts ==============================\u001b[0m\n",
"platform darwin -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0\n",
"rootdir: /Users/yesji/test_project_folder/project_spring_2020\n",
"plugins: arraydiff-0.3, doctestplus-0.4.0, openfiles-0.4.0, remotedata-0.3.2\n",
"collected 0 items / 1 errors \u001b[0m\n",
"\n",
"==================================== ERRORS ====================================\n",
"\u001b[31m\u001b[1m______________ ERROR collecting tests/motion_displacement_test.py ______________\u001b[0m\n",
"\u001b[31mImportError while importing test module '/Users/yesji/test_project_folder/project_spring_2020/tests/motion_displacement_test.py'.\n",
"Hint: make sure your test modules/packages have valid Python names.\n",
"Traceback:\n",
"tests/motion_displacement_test.py:1: in <module>\n",
" from Jeans_Package.compute_motion_displacement import *\n",
"E ModuleNotFoundError: No module named 'Jeans_Package'\u001b[0m\n",
"!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!\n",
"\u001b[31m\u001b[1m=============================== 1 error in 0.19s ===============================\u001b[0m\n"
]
}
],
"source": [
"!pytest"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# project_spring_2020

[![CircleCI](https://circleci.com/gh/biof309/project_spring_2020/tree/master.svg?style=shield)](https://circleci.com/gh/biof309/project_spring_2020/tree/master)

This script computes the mean framewise displacement for the fMRI data our lab collected. After the fMRI data go through our existing preprocessing pipeline, we would end up with a text file containing motion displacement information for each time point for all the runs the participant completed. The text file has 6 different columns. Each columns represent a different motion parameters - the first three columns tell us how much participant rotated in three different ways, and the last three columns give us information on how much participant moved in three different directions. Each row give us motion information for each time point.

The python script will compute the mean of each motion parameter and save out a text file containing the 6 averages. Additionally, the script will compute the mean of these six averages to give us a summary displacement measure. Hopefully these displacement values will give us some additional information on motion and help with QC and later analysis.
23 changes: 23 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name="Jeans_Package",
version="0.0.1",
author="JY",
author_email="[email protected]",
description="A small example package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/packaging_demo",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',

)
37 changes: 37 additions & 0 deletions tests/motion_displacement_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from Jeans_Package.compute_motion_displacement import *

def test_column_avg1():
sample_data = {'roll':[1, 1, 1], 'pitch':[2, 2, 2], 'yaw':[3, 3, 3], 'z':[4, 4, 4], 'x':[5, 5, 5], 'y':[6, 6, 6]}
sample_df = pd.DataFrame(data = sample_data)
results = compute_mean_for_each_column(sample_df)
assert len(results) == 6

def test_column_avg2():
sample_data = {'roll':[1, 1, 1], 'pitch':[2, 2, 2], 'yaw':[3, 3, 3], 'z':[4, 4, 4], 'x':[5, 5, 5], 'y':[6, 6, 6]}
sample_df = pd.DataFrame(data = sample_data)
results = compute_mean_for_each_column(sample_df)
output = [1,2,3,4,5,6]
assert results == output

def test_column_avg3():
"""This is to make sure the function can handle negative numbers too"""
sample_data = {'roll':[0, 1, -4], 'pitch':[2, 2, -10], 'yaw':[-6, 3, 3], 'z':[4, -8, 4], 'x':[5, 5, 5], 'y':[6, 6, 6]}
sample_df = pd.DataFrame(data = sample_data)
results = compute_mean_for_each_column(sample_df)
output = [-1,-2,0,0,5,6]
assert results == output

def test_avg_sum1():
sample_data = {'roll':[1, 1, 1], 'pitch':[2, 2, 2], 'yaw':[3, 3, 3], 'z':[4, 4, 4], 'x':[5, 5, 5], 'y':[6, 6, 6]}
sample_df = pd.DataFrame(data = sample_data)
results = compute_mean_for_each_column(sample_df)
sum_results = compute_mean_of_all_columns(results)
assert sum_results == 3.5

def test_avg_sum2():
"""This is to make sure the function can handle negative numbers too"""
sample_data = {'roll':[0, 1, -4], 'pitch':[2, 2, -10], 'yaw':[-6, 3, 3], 'z':[4, -8, 4], 'x':[3, 3, 3], 'y':[6, 6, 6]}
sample_df = pd.DataFrame(data = sample_data)
results = compute_mean_for_each_column(sample_df)
sum_results = compute_mean_of_all_columns(results)
assert sum_results == 1
9 changes: 0 additions & 9 deletions tests/sample_test.py

This file was deleted.