Browse Source

added headers description for the position tracking file

asobolev 4 years ago
parent
commit
b4b8774dc3
2 changed files with 79 additions and 58 deletions
  1. 1 0
      .gitignore
  2. 78 58
      Jupyter Notebook - One Target Island - openCV 4-0-1.ipynb

+ 1 - 0
.gitignore

@@ -1,6 +1,7 @@
 # user generated files
 *.png
 *parameters.txt
+*parameters.json
 *.xlsx
 *.avi
 

+ 78 - 58
Jupyter Notebook - One Target Island - openCV 4-0-1.ipynb

@@ -26,7 +26,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 55,
+   "execution_count": 1,
    "metadata": {},
    "outputs": [
     {
@@ -69,7 +69,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 56,
+   "execution_count": 2,
    "metadata": {
     "scrolled": false
    },
@@ -94,7 +94,7 @@
       "    \"arena_x\": 400,\n",
       "    \"arena_y\": 300,\n",
       "    \"arena_radius\": 300,\n",
-      "    \"experiment_date\": \"2020-06-07_18-02-04\"\n",
+      "    \"experiment_date\": \"2020-06-07_18-26-44\"\n",
       "}\n"
      ]
     }
@@ -124,7 +124,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 58,
+   "execution_count": 3,
    "metadata": {
     "scrolled": true
    },
@@ -133,7 +133,7 @@
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "EXPERIMENT ID: 003901_aSIT_2020-06-07_18-02-04\n",
+      "EXPERIMENT ID: 003901_aSIT_2020-06-07_18-26-44\n",
       "Trials per session: 50\n",
       "Session duration [s]: 3600\n",
       "Trial duration [s]: 60\n",
@@ -149,7 +149,7 @@
       "Arena X coordinate [pixels]: 400\n",
       "Arena Y coordinate [pixels]: 300\n",
       "Arena radius [pixels]: 300\n",
-      "Experiment date: 2020-06-07_18-02-04\n"
+      "Experiment date: 2020-06-07_18-26-44\n"
      ]
     }
    ],
@@ -161,7 +161,8 @@
     "        \n",
     "def update_config():\n",
     "    for key, entry_field in entry_fields.items():\n",
-    "        cfg[key] = entry_field.get()\n",
+    "        tp = type(cfg[key])\n",
+    "        cfg[key] = tp(entry_field.get())\n",
     "    experiment_id = \"%s_%s_%s\" % (cfg['subject'], cfg['experiment_type'], cfg['experiment_date'])\n",
     "        \n",
     "def show_and_update_config():\n",
@@ -216,30 +217,18 @@
    "source": [
     "## Protocol 1\n",
     "\n",
-    "Run the upcoming cell, if you want to save the chosen experimetal parameters to a txt-file (\"ExperimentID_parameters.txt\"). The file will be saved to the folder containing this notebook."
+    "Run the upcoming cell, if you want to save the chosen experimetal parameters to a JSON-file (\"experiment_id_parameters.json\"). The file will be saved to the folder containing this notebook."
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": 4,
    "metadata": {},
    "outputs": [],
    "source": [
-    "# Saves all parameters to a txt-file with the user-defined \"Experiment ID\" as filename\n",
-    "parametersName = experimentID + '_parameters.txt'\n",
-    "with open(parametersName, 'w') as f:\n",
-    "    print(time.asctime(time.localtime(time.time())), file=f)\n",
-    "    print('Trials per Session: %s' % trialNumber, file=f)\n",
-    "    print('Session Duration: %s' % sessionDuration, file=f)\n",
-    "    print('Trial Duration: %s' % trialDuration, file=f)\n",
-    "    print('Radius of the Starting Platform: %s' % startRadius, file=f)\n",
-    "    print('X-Coordinate of the Starting Platform: %s' % startX, file=f)\n",
-    "    print('Y-Coordinate of the Starting Platform: %s' % startY, file=f)\n",
-    "    print('Radius of the target platform: %s' % targetRadius, file=f)\n",
-    "    print('Duration the subject has to stay in the target area: %s' % targetDuration, file=f)\n",
-    "    print('Subject and Date: %s' % experimentID, file=f)\n",
-    "    print('Subject is darker than background: %s' % backgroundColor, file=f)\n",
-    "    print('Initialization Duration: %s' % initDuration, file=f)\n"
+    "# Saves all parameters to a JSON file with the user-defined \"Experiment ID\" as filename\n",
+    "with open(experiment_id + '_parameters.json', 'w') as f:\n",
+    "    json.dump(cfg, f, indent=4)"
    ]
   },
   {
@@ -255,7 +244,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
+   "execution_count": 5,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -310,18 +299,18 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
+   "execution_count": 6,
    "metadata": {},
    "outputs": [
     {
      "name": "stderr",
      "output_type": "stream",
      "text": [
-      "<ipython-input-7-1969cbc2e2a8>:13: WavFileWarning: Chunk (non-data) not understood, skipping it.\n",
+      "<ipython-input-6-c27cd5fb2fdf>:13: WavFileWarning: Chunk (non-data) not understood, skipping it.\n",
       "  distractorSoundTrial = wavfile.read('./10kHz-short-68.wav')[1]\n",
-      "<ipython-input-7-1969cbc2e2a8>:14: WavFileWarning: Chunk (non-data) not understood, skipping it.\n",
+      "<ipython-input-6-c27cd5fb2fdf>:14: WavFileWarning: Chunk (non-data) not understood, skipping it.\n",
       "  attractorSoundTarget1 = wavfile.read('./4000Hz-short-68.wav')[1]\n",
-      "<ipython-input-7-1969cbc2e2a8>:15: WavFileWarning: Chunk (non-data) not understood, skipping it.\n",
+      "<ipython-input-6-c27cd5fb2fdf>:15: WavFileWarning: Chunk (non-data) not understood, skipping it.\n",
       "  silenceSound = wavfile.read('./silence-short-68.wav')[1]\n"
      ]
     }
@@ -341,8 +330,7 @@
     "# Open sound files\n",
     "distractorSoundTrial = wavfile.read('./10kHz-short-68.wav')[1]\n",
     "attractorSoundTarget1 = wavfile.read('./4000Hz-short-68.wav')[1]\n",
-    "silenceSound = wavfile.read('./silence-short-68.wav')[1]\n",
-    "  "
+    "silenceSound = wavfile.read('./silence-short-68.wav')[1]"
    ]
   },
   {
@@ -356,7 +344,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 8,
+   "execution_count": 7,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -369,7 +357,7 @@
     "             'white': (255,255,255)}\n",
     "\n",
     "# Define the codec and create VideoWriter object\n",
-    "videoName = experimentID + '_video.avi'\n",
+    "videoName = experiment_id + '_video.avi'\n",
     "fourcc = cv2.VideoWriter_fourcc(*'XVID')\n",
     "# Make sure that the frame rate of your output appoximately matches \n",
     "# the number of cycles per second, to avoid time lapsed output videos\n",
@@ -391,7 +379,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 9,
+   "execution_count": 8,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -408,12 +396,12 @@
     "    img2 = img\n",
     "\n",
     "    # Display the resulting frame\n",
-    "    imgArena = cv2.circle(img,(arenaX,arenaY), arenaRadius, (0,0,255), 2)\n",
-    "    imgArenaStart = cv2.circle(imgArena,(startX,startY), startRadius, (255,0,255), 2)\n",
+    "    imgArena = cv2.circle(img, (cfg['arena_x'], cfg['arena_y']), cfg['arena_radius'], (0,0,255), 2)\n",
+    "    imgArenaStart = cv2.circle(imgArena, (cfg['start_x'], cfg['start_x']), cfg['start_radius'], (255,0,255), 2)\n",
     "\n",
     "    # Mask the space outside the arena\n",
     "    mask = np.zeros(shape = img.shape, dtype = \"uint8\")\n",
-    "    cv2.circle(mask, (arenaX,arenaY), arenaRadius, (255,255,255), -1)\n",
+    "    cv2.circle(mask, (cfg['arena_x'], cfg['arena_y']), cfg['arena_radius'], (255,255,255), -1)\n",
     "\n",
     "    maskedImg2 = cv2.bitwise_and(src1 = img2, src2 = mask)\n",
     "    imgArenaStart = cv2.bitwise_and(src1 = imgArenaStart, src2 = mask)\n",
@@ -480,7 +468,19 @@
    "cell_type": "code",
    "execution_count": 11,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "ename": "NameError",
+     "evalue": "name 'arenaX' is not defined",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
+      "\u001b[0;32m<ipython-input-11-ad6c592f385a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;31m# Mask the space outside the arena\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m \u001b[0mmask\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mzeros\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshape\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mimg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"uint8\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcircle\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmask\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0marenaX\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0marenaY\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marenaRadius\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m255\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m255\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m255\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m \u001b[0;31m# Experiment starts in phase 0 with 0 trials\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+      "\u001b[0;31mNameError\u001b[0m: name 'arenaX' is not defined"
+     ]
+    }
+   ],
    "source": [
     "# Define video capture device for live-stream (0 = webcam1) and tracking\n",
     "cap = cv2.VideoCapture(0)\n",
@@ -490,7 +490,7 @@
     "\n",
     "# Mask the space outside the arena\n",
     "mask = np.zeros(shape = img.shape, dtype = \"uint8\")\n",
-    "cv2.circle(mask, (arenaX,arenaY), arenaRadius, (255,255,255), -1)\n",
+    "cv2.circle(mask, (cfg['arena_x'], cfg['arena_y']), cfg['arena_radius'], (255,255,255), -1)\n",
     "\n",
     "# Experiment starts in phase 0 with 0 trials\n",
     "expPhase = 0   \n",
@@ -534,12 +534,45 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 12,
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "headers = [\n",
+    "    'FrameID',            # Frame ID\n",
+    "    'Time [s]',           # Time stamp\n",
+    "    'Phase',              # Phase of experiment\n",
+    "    'Animal_x',           # X-Coordinate of the subject\n",
+    "    'Animal_y',           # Y-Coordinate of the subject\n",
+    "    'Start_x',            # X-Coordinate of the starting platform\n",
+    "    'Start_y',            # Y-Coordinate of the starting platform\n",
+    "    'Start_rad',          # Radius of the starting platform\n",
+    "    'Target_x',           # X-Coordinate of the target \n",
+    "    'Target_y',           # Y-Coordinate of the target \n",
+    "    'Target_rad',         # Radius of the target platform\n",
+    "    'TrialID',            # Trial ID\n",
+    "    'Rewarded Trials [%]',# Percentage of trials rewarded\n",
+    "    'Sound Played',       # sound played\n",
+    "    'Common cycle ID',    # common cycle ID\n",
+    "    'Odd1_x',             # X-Coordinate of the odd1 target \n",
+    "    'Odd1_Y',             # Y-Coordinate of the odd1 target \n",
+    "    'Odd2_x',             # X-Coordinate of the odd2 target \n",
+    "    'Odd2_Y',             # Y-Coordinate of the odd2 target \n",
+    "    'Odd3_x',             # X-Coordinate of the odd3 target \n",
+    "    'Odd3_Y',             # Y-Coordinate of the odd3 target \n",
+    "    'Odd4_x',             # X-Coordinate of the odd4 target \n",
+    "    'Odd4_Y'              # Y-Coordinate of the odd4 target \n",
+    "]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
    "metadata": {},
    "outputs": [],
    "source": [
     "# Create an Excel workbook and worksheet\n",
-    "protocolName  = experimentID + '_protocol.xlsx'\n",
+    "protocolName  = experiment_id + '_protocol.xlsx'\n",
     "workbook = xlsxwriter.Workbook(protocolName, {'constant_memory': True, 'tmpdir': './'})\n",
     "\n",
     "# Workbook = xlsxwriter.Workbook(protocolName)\n",
@@ -591,7 +624,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 13,
+   "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -606,8 +639,7 @@
     "#def abs():\n",
     "#    root.destroy\n",
     "\n",
-    "root.mainloop()\n",
-    "\n"
+    "root.mainloop()"
    ]
   },
   {
@@ -629,21 +661,9 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 15,
+   "execution_count": null,
    "metadata": {},
-   "outputs": [
-    {
-     "name": "stdout",
-     "output_type": "stream",
-     "text": [
-      "Digiral 12 got a message: 1\n",
-      "Digiral 12 got a message: 0\n",
-      "Digiral 4 got a message: 1\n",
-      "Digiral 4 got a message: 0\n",
-      "Fake board - exiting...\n"
-     ]
-    }
-   ],
+   "outputs": [],
    "source": [
     "# Define and start the experiment timer\n",
     "expTime = time.time()\n",