{ "cells": [ { "cell_type": "code", "execution_count": 14, "id": "64103f44", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import os\n", "import matplotlib.pyplot as plt\n", "import h5py\n", "import shutil\n", "\n", "#from klusta.launch import klusta as klaunch" ] }, { "cell_type": "code", "execution_count": 15, "id": "0ebece1e", "metadata": {}, "outputs": [], "source": [ "# session paths\n", "source = '/home/sobolev/nevermind/Andrey/data'\n", "animal = '006865'\n", "session = '006865_hippoSIT_2021-12-09_15-28-36'\n", "session_path = os.path.join(source, animal, session)" ] }, { "cell_type": "code", "execution_count": 11, "id": "97dd5d9a", "metadata": {}, "outputs": [], "source": [ "#session_path = 'Z:\\\\Andrey\\\\sandbox'\n", "#ss_path = os.path.join(session_path, 'ss2')\n", "#animal = 'GB049'\n", "#session = '2021-07-13T15-12-29'\n", "#filebase = '%s_%s' % (animal, session)" ] }, { "cell_type": "markdown", "id": "07710b0c", "metadata": {}, "source": [ "## Copy Neurosuite settings files" ] }, { "cell_type": "code", "execution_count": 16, "id": "a308cdf5", "metadata": {}, "outputs": [], "source": [ "settings_source = os.path.join(source, animal)\n", "ns_settings_file = os.path.join(settings_source, 'default.xml')\n", "kwik_params_file = os.path.join(settings_source, 'default.prm')\n", "kwik_probes_file = os.path.join(settings_source, 'default.prb')\n", "\n", "shutil.copy(ns_settings_file, os.path.join(session_path, '%s.xml' % session))\n", "shutil.copy(kwik_params_file, os.path.join(session_path, '%s.prm' % session))\n", "shutil.copy(kwik_probes_file, os.path.join(session_path, '%s.prb' % session))\n", "\n", "with open(os.path.join(session_path, '%s.prm' % session), 'r+') as f:\n", " content = f.read()\n", " f.seek(0, 0)\n", " lines = \"experiment_name = '%s'\\nprb_file = '%s.prb'\\n \\n\" % (session, session)\n", " f.write(lines.rstrip('\\r\\n') + '\\n' + content)" ] }, { "cell_type": "markdown", "id": "b9e743ef", "metadata": {}, "source": [ "## Conversion of MCS .h5 to .dat file\n", "\n", "http://neurosuite.sourceforge.net/formats.html\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "3397fb7b", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Channels: 32, samples: 48008115, type: int32\n", "100 percent complete\r" ] } ], "source": [ "crossref = None\n", "#crossref = 11\n", "\n", "MCS_h5_name = [x for x in os.listdir(session_path) if x.startswith('Andrey') and x.endswith('.h5')][0]\n", "chunk_size = 25000 # 1 second at sampling rate 25kHz\n", "dest_file = os.path.join(session_path, '%s.dat' % session)\n", "if crossref is not None:\n", " dest_file = os.path.join(session_path, '%s.cf.dat' % session)\n", "if not os.path.exists(session_path):\n", " os.makedirs(session_path)\n", "if os.path.exists(dest_file):\n", " os.remove(dest_file)\n", "\n", "with h5py.File(os.path.join(session_path, MCS_h5_name), 'r') as f:\n", " #ds = f['Data']['Recording_0']['AnalogStream']['Stream_1']['ChannelData']\n", " ds = f['Data']['Recording_0']['AnalogStream']['Stream_2']['ChannelData']\n", " print(\"Channels: %s, samples: %s, type: %s\" % (ds.shape[0], ds.shape[1], ds.dtype))\n", " \n", " chunks_no = int(np.ceil(ds.shape[1]/chunk_size))\n", " #chunks_no = 60\n", " for i in range(chunks_no):\n", " idx = i * chunk_size\n", " testds = ds[:, idx:idx + chunk_size]\n", " data = np.array(ds[:, idx:idx + chunk_size]).astype(np.int16) # int16 is correct\n", " if crossref is not None:\n", " to_sub = np.array(data[crossref])\n", " data -= to_sub\n", " with open(dest_file, 'ab') as f:\n", " data.T.flatten().tofile(f)\n", " \n", " print(\"%s percent complete\\r\" % str(round(100*(i + 1)/float(chunks_no))), end='')" ] }, { "cell_type": "code", "execution_count": null, "id": "16b5ce0c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }