{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# File to be converted.\n", "input_filename = 'data/0023uLRpitc_NTP_20dT_0.5GndCl.sm'\n", "output_path = None # Use None for saving in the same folder as the SM file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Default configuration" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Used when the metadata does not contain the field identity\n", "default_identity = dict(\n", " author='Weiss Lab',\n", " author_affiliation='UCLA',\n", " creator='Antonino Ingargiola',\n", " creator_affiliation='UCLA')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Used when the metadata does not contain the field setup\n", "default_setup = dict(\n", " num_pixels = 2,\n", " num_spots = 1,\n", " num_spectral_ch = 2,\n", " num_polarization_ch = 1,\n", " num_split_ch = 1,\n", " modulated_excitation = True,\n", " lifetime = False,\n", " excitation_cw = [True, True],\n", " excitation_alternated=[True, True],\n", " excitation_wavelengths=[532e-9, 635e-9],\n", " detection_wavelengths=[580e-9, 660e-9]\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Used when the metadata does not contain the field measurement_specs\n", "def_measurement_specs = dict(\n", " measurement_type = 'smFRET-usALEX',\n", " alex_period = 4000,\n", " alex_offset = 700,\n", " alex_excitation_period1 = (2180, 3900),\n", " alex_excitation_period2 = (200, 1800),\n", " detectors_specs = dict(spectral_ch1 = [0],\n", " spectral_ch2 = [1]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "from pathlib import Path\n", "import matplotlib.pyplot as plt\n", "import yaml\n", "\n", "import phconvert as phc\n", "print('phconvert version: ' + phc.__version__)\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Resolve paths" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "input_filename = Path(input_filename)\n", "input_filename" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "assert input_filename.is_file(), 'Input SM file not found. Check the file name.'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if output_path is None:\n", " output_path = input_filename.parent" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_filename = Path(output_path, input_filename.stem + '.hdf5')\n", "out_filename" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create the output data folder if necessary\n", "#out_filename.parent.mkdir(parents=True, exist_ok=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Load metadata" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "meta_filename = input_filename.with_suffix('.yml')\n", "assert meta_filename.is_file(), 'Metadata YAML file not found.'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open(str(meta_filename)) as f:\n", " metadata = yaml.load(f)\n", "metadata" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Preprocess timestamps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def update_with_defaults(input_dict, default_dict):\n", " for k in default_dict:\n", " if k not in input_dict:\n", " input_dict[k] = default_dict[k]\n", " return input_dict\n", " \n", "\n", "def sm_load_photon_data(filename, metadata, def_measurement_specs):\n", " \"\"\"Load photon_data from a .sm us-ALEX file into a metadata dict.\n", " \"\"\"\n", " metadata = metadata.copy()\n", " timestamps, detectors, labels = phc.smreader.load_sm(str(filename),\n", " return_labels=True)\n", " measurement_specs = metadata.pop('measurement_specs', def_measurement_specs)\n", " update_with_defaults(measurement_specs, def_measurement_specs)\n", " \n", " photon_data = dict(\n", " timestamps = timestamps,\n", " timestamps_specs = dict(timestamps_unit=12.5e-9),\n", " detectors = detectors,\n", " measurement_specs = measurement_specs)\n", "\n", " acquisition_duration = (timestamps[-1] - timestamps[0]) * 12.5e-9\n", " \n", " provenance = dict(\n", " filename=str(filename), \n", " software='LabVIEW Data Acquisition usALEX')\n", " \n", " metadata.update(\n", " _filename = str(filename),\n", " acquisition_duration = round(acquisition_duration),\n", " photon_data = photon_data,\n", " provenance = provenance)\n", " return metadata\n", "\n", "\n", "def fill_with_defaults(metadata, default_setup, default_identity):\n", " \"\"\"Fill all missing values in metadata with defaults.\"\"\"\n", " setup = metadata.get('setup', default_setup)\n", " metadata['setup'] = update_with_defaults(setup, default_setup)\n", "\n", " identity = metadata.get('identity', default_identity)\n", " metadata['identity'] = update_with_defaults(identity, default_identity) \n", " \n", " sample = metadata['sample']\n", " sample['num_dyes'] = len(sample['dye_names'].split(','))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = sm_load_photon_data(input_filename, metadata, def_measurement_specs)\n", "fill_with_defaults(data, default_setup, default_identity)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "phc.plotter.alternation_hist(data)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Create Photon-HDF5" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "phc.hdf5.save_photon_hdf5(data, h5_fname=str(out_filename), overwrite=True, close=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "hide_input": false, "kernelspec": { "display_name": "py36", "language": "python", "name": "py36" }, "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.6.0" }, "nav_menu": {}, "toc": { "colors": { "hover_highlight": "#DAA520", "navigate_num": "#000000", "navigate_text": "#333333", "running_highlight": "#FF0000", "selected_highlight": "#FFD700", "sidebar_border": "#EEEEEE", "wrapper_background": "#FFFFFF" }, "moveMenuLeft": true, "nav_menu": { "height": "154px", "width": "252px" }, "navigate_menu": true, "number_sections": false, "sideBar": true, "threshold": 4, "toc_cell": false, "toc_position": { "height": "673px", "left": "0px", "right": "1235px", "top": "106.997px", "width": "109px" }, "toc_section_display": "block", "toc_window_display": true, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 1 }