Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hooman-b committed Dec 14, 2022
0 parents commit d79a7d2
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/flanker_test_blue.xlsx
/flanker_test_green.xlsx
/flanker_test_orange.xlsx
/flanker_test_pink.xlsx
/flanker_test_red.xlsx
5 changes: 5 additions & 0 deletions config_flanker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
flanker_test_blue : 'flanker_test_blue.xlsx'
flanker_test_green : 'flanker_test_green.xlsx'
flanker_test_orange : 'flanker_test_orange.xlsx'
flanker_test_pink : 'flanker_test_pink.xlsx'
flanker_test_red : 'flanker_test_red.xlsx'
196 changes: 196 additions & 0 deletions flanker_test.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 312,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import panel as pn\n",
"import os\n",
"import re\n",
"import yaml\n",
"from bokeh.io import output_notebook\n",
"from bokeh.plotting import figure, show\n",
"from bokeh.layouts import gridplot"
]
},
{
"cell_type": "code",
"execution_count": 313,
"metadata": {},
"outputs": [],
"source": [
"def configReader():\n",
" \"\"\"\n",
" explanation: This function open config,yaml file \n",
" and fetch the gonfigue file information\n",
" input: ...\n",
" output: configue file\n",
" \"\"\"\n",
" with open(\"config_flanker.yaml\", \"r\") as inputFile:\n",
" config = yaml.safe_load(inputFile)\n",
" return config"
]
},
{
"cell_type": "code",
"execution_count": 314,
"metadata": {},
"outputs": [],
"source": [
"\n",
"def render_plot(x_axis='index', y_axis='reaction_time', plot_type='boxPlot'):\n",
" \"\"\"\n",
" explanation: This function opens all files finds those that have data\n",
" and erase rest of them. thenadd some property to data and \n",
" use the plot function to sketch the data.\n",
" input: type of graph, x_axis, y_axis\n",
" output: a set of graphs\n",
" \"\"\"\n",
" #read file names and enter the heet names\n",
" config = configReader()\n",
" sheet_name = ['dehydration_1', 'dehydration_2', 'dehydration_3', 'control_1', 'control_2', 'control_3']\n",
" graph = []\n",
"\n",
" #save the name of the sheets as the keys and data frames as values, determining color\n",
" for flanker_test in config.values():\n",
" flanker_test_dic = pd.read_excel(flanker_test,sheet_name=sheet_name)\n",
" filename = os.path.basename(flanker_test)\n",
" color = re.split(\"[_.]\", filename)[-2]\n",
"\n",
" #erase empty cells\n",
" for key in list(flanker_test_dic.keys()):\n",
" if flanker_test_dic[key].empty: \n",
" del flanker_test_dic[key]\n",
"\n",
" #add some properties\n",
" for key in flanker_test_dic:\n",
" flanker_test_dic[key].columns = ['flanker', 'homology', 'accuracy', 'reaction_time']\n",
" flanker_test_dic[key].insert(0, 'participant', color)\n",
" flanker_test_dic[key].insert(0, 'index', flanker_test_dic[key].index)\n",
"\n",
" #making the plots\n",
" fig = making_plots(flanker_test_dic, color, graph, x_axis, y_axis, plot_type)\n",
" graph.append(fig)\n",
" return(gridplot(graph, ncols=2, plot_width=450, plot_height=300))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 315,
"metadata": {},
"outputs": [],
"source": [
"def making_plots(flanker_test_dic, color, graph,x_axis, y_axis, plot_type):\n",
" \"\"\"\n",
" explanation: This function makes plots based on plot type, x and y axises\n",
" input: test dictionary, color, main graph, x axis, y axis, plot type\n",
" output: figure of each color\n",
" \"\"\"\n",
" fig = figure(title=f'Flanker Test {color}', plot_width=800, plot_height=900)\n",
" colors = ['red', 'blue','green', 'purple', 'black']\n",
"\n",
" for counter, key in enumerate(flanker_test_dic):\n",
"\n",
" #make a plot base on the type\n",
" if plot_type=='line':\n",
" fig.line(x=flanker_test_dic[key][x_axis], y=flanker_test_dic[key][y_axis],\n",
" legend_label= key, line_width=0.5, color=colors[counter])\n",
" \n",
" elif plot_type=='scatter':\n",
" fig.scatter( x=flanker_test_dic[key][x_axis], y=flanker_test_dic[key][y_axis],\n",
" legend_label= key, marker='circle', fill_color=colors[counter], line_color='white', size=5)\n",
" \n",
" elif plot_type=='vbar':\n",
" df = pd.DataFrame(flanker_test_dic[key])\n",
" dff = pd.DataFrame(flanker_test_dic[key].groupby([x_axis]).count()) \n",
" fig.vbar(x=dff.index, width=0.5, bottom=0, top=dff[y_axis], \n",
" color=colors[counter], legend_label= key) \n",
"\n",
" return fig"
]
},
{
"cell_type": "code",
"execution_count": 316,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"BokehDeprecationWarning: plot_width and plot_height was deprecated in Bokeh 2.4.0 and will be removed, use width or height instead.\n"
]
}
],
"source": [
"#making an interactive plot\n",
"columns_name = ['index', 'homology', 'accuracy', 'reaction_time']\n",
"plot_name = ['line', 'scatter', 'vbar']\n",
"inter_plot = pn.interact(render_plot, x_axis=columns_name, y_axis=columns_name, plot_type=plot_name)"
]
},
{
"cell_type": "code",
"execution_count": 318,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Launching server at http://localhost:63094\n"
]
},
{
"data": {
"text/plain": [
"<bokeh.server.server.Server at 0x1eafa0de1f0>"
]
},
"execution_count": 318,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#making a dashboard of interactive plot\n",
"dashboard = pn.template.BootstrapTemplate(title='Flanker Test',sidebar_width=200)\n",
"dashboard.sidebar.append(inter_plot[0])\n",
"dashboard.main.append(inter_plot[1])\n",
"dashboard.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.9.13"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "e4cce46d6be9934fbd27f9ca0432556941ea5bdf741d4f4d64c6cd7f8dfa8fba"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit d79a7d2

Please sign in to comment.