[
  {
    "path": ".gitignore",
    "content": ".ipynb_checkpoints/\n__pycache__/\n*.swp\nechonet.cfg\n.echonet.cfg\n*.pyc\nechonet.egg-info/\n"
  },
  {
    "path": ".travis.yml",
    "content": "language: minimal\n\nos:\n  - linux\n\nenv:\n  # - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.1 TORCHVISION_VERSION=0.2 (torchvision 0.2 does not have VisionDataset)\n  # - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.1 TORCHVISION_VERSION=0.3  (torchvision 0.3 has a cuda issue)\n  - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.1 TORCHVISION_VERSION=0.4\n  - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.1 TORCHVISION_VERSION=0.5\n    # - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.2 TORCHVISION_VERSION=0.2\n  # - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.2 TORCHVISION_VERSION=0.3\n  - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.2 TORCHVISION_VERSION=0.4\n  - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.2 TORCHVISION_VERSION=0.5\n  # - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.3 TORCHVISION_VERSION=0.2\n  # - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.3 TORCHVISION_VERSION=0.3\n  - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.3 TORCHVISION_VERSION=0.4\n  - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.3 TORCHVISION_VERSION=0.5\n  # - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.4 TORCHVISION_VERSION=0.2\n  # - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.4 TORCHVISION_VERSION=0.3\n  - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.4 TORCHVISION_VERSION=0.4\n  - PYTHON_VERSION=3.6 PYTORCH_VERSION=1.4 TORCHVISION_VERSION=0.5\n  # - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.1 TORCHVISION_VERSION=0.2\n  # - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.1 TORCHVISION_VERSION=0.3\n  - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.1 TORCHVISION_VERSION=0.4\n  - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.1 TORCHVISION_VERSION=0.5\n  # - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.2 TORCHVISION_VERSION=0.2\n  # - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.2 TORCHVISION_VERSION=0.3\n  - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.2 TORCHVISION_VERSION=0.4\n  - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.2 TORCHVISION_VERSION=0.5\n  # - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.3 TORCHVISION_VERSION=0.2\n  # - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.3 TORCHVISION_VERSION=0.3\n  - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.3 TORCHVISION_VERSION=0.4\n  - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.3 TORCHVISION_VERSION=0.5\n  # - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.4 TORCHVISION_VERSION=0.2\n  # - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.4 TORCHVISION_VERSION=0.3\n  - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.4 TORCHVISION_VERSION=0.4\n  - PYTHON_VERSION=3.7 PYTORCH_VERSION=1.4 TORCHVISION_VERSION=0.5 \n\ninstall:\n  - if [[ \"$TRAVIS_OS_NAME\" == \"linux\" ]];\n    then\n        MINICONDA_OS=Linux;\n        sudo apt-get update;\n    else\n        MINICONDA_OS=MacOSX;\n        brew update;\n    fi\n  - wget https://repo.anaconda.com/miniconda/Miniconda3-latest-${MINICONDA_OS}-x86_64.sh -O miniconda.sh\n  - bash miniconda.sh -b -p $HOME/miniconda\n  - source \"$HOME/miniconda/etc/profile.d/conda.sh\"\n  - hash -r\n  - conda config --set always_yes yes --set changeps1 no\n  - conda update -q conda\n  # Useful for debugging any issues with conda\n  - conda info -a\n  - conda search pytorch || true\n\n  - conda create -q -n test-environment python=${PYTHON_VERSION} pytorch=${PYTORCH_VERSION} \n  - conda activate test-environment\n  - pip install -q torchvision==${TORCHVISION_VERSION} \"pillow<7.0.0\"\n  - pip install -q .\n  - pip install -q flake8 pylint\n\nscript:\n  - flake8 --ignore=E501\n  - pylint --disable=C0103,C0301,R0401,R0801,R0902,R0912,R0913,R0914,R0915 --extension-pkg-whitelist=cv2,torch --generated-members=torch.* echonet/ scripts/*.py setup.py\n  - python -c \"import echonet\"\n"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2020 the authors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "LICENSE.txt",
    "content": "Copyright Notice\nThe authors are the proprietor of certain copyrights of and to EchoNet-Dynamic software, source code and associated material.  Code also contains source code created by certain third parties.  Redistribution and use of the Code with or without modification is not permitted without explicit written permission by the authors. \nCopyright 2019 The authors.  All rights reserved.\n"
  },
  {
    "path": "README.md",
    "content": "EchoNet-Dynamic:<br/>Interpretable AI for beat-to-beat cardiac function assessment\r\n------------------------------------------------------------------------------\r\n\r\nEchoNet-Dynamic is a end-to-end beat-to-beat deep learning model for\r\n  1) semantic segmentation of the left ventricle\r\n  2) prediction of ejection fraction by entire video or subsampled clips, and\r\n  3) assessment of cardiomyopathy with reduced ejection fraction.\r\n\r\nFor more details, see the accompanying paper,\r\n\r\n> [**Video-based AI for beat-to-beat assessment of cardiac function**](https://www.nature.com/articles/s41586-020-2145-8)<br/>\r\n  David Ouyang, Bryan He, Amirata Ghorbani, Neal Yuan, Joseph Ebinger, Curt P. Langlotz, Paul A. Heidenreich, Robert A. Harrington, David H. Liang, Euan A. Ashley, and James Y. Zou. <b>Nature</b>, March 25, 2020. https://doi.org/10.1038/s41586-020-2145-8\r\n\r\nDataset\r\n-------\r\nWe share a deidentified set of 10,030 echocardiogram images which were used for training EchoNet-Dynamic.\r\nPreprocessing of these images, including deidentification and conversion from DICOM format to AVI format videos, were performed with OpenCV and pydicom. Additional information is at https://echonet.github.io/dynamic/. These deidentified images are shared with a non-commerical data use agreement.\r\n\r\nExamples\r\n--------\r\n\r\nWe show examples of our semantic segmentation for nine distinct patients below.\r\nThree patients have normal cardiac function, three have low ejection fractions, and three have arrhythmia.\r\nNo human tracings for these patients were used by EchoNet-Dynamic.\r\n\r\n| Normal                                 | Low Ejection Fraction                  | Arrhythmia                             |\r\n| ------                                 | ---------------------                  | ----------                             |\r\n| ![](docs/media/0X10A28877E97DF540.gif) | ![](docs/media/0X129133A90A61A59D.gif) | ![](docs/media/0X132C1E8DBB715D1D.gif) |\r\n| ![](docs/media/0X1167650B8BEFF863.gif) | ![](docs/media/0X13CE2039E2D706A.gif ) | ![](docs/media/0X18BA5512BE5D6FFA.gif) |\r\n| ![](docs/media/0X148FFCBF4D0C398F.gif) | ![](docs/media/0X16FC9AA0AD5D8136.gif) | ![](docs/media/0X1E12EEE43FD913E5.gif) |\r\n\r\nInstallation\r\n------------\r\n\r\nFirst, clone this repository and enter the directory by running:\r\n\r\n    git clone https://github.com/echonet/dynamic.git\r\n    cd dynamic\r\n\r\nEchoNet-Dynamic is implemented for Python 3, and depends on the following packages:\r\n  - NumPy\r\n  - PyTorch\r\n  - Torchvision\r\n  - OpenCV\r\n  - skimage\r\n  - sklearn\r\n  - tqdm\r\n\r\nEchonet-Dynamic and its dependencies can be installed by navigating to the cloned directory and running\r\n\r\n    pip install --user .\r\n\r\nUsage\r\n-----\r\n### Preprocessing DICOM Videos\r\n\r\nThe input of EchoNet-Dynamic is an apical-4-chamber view echocardiogram video of any length. The easiest way to run our code is to use videos from our dataset, but we also provide a Jupyter Notebook, `ConvertDICOMToAVI.ipynb`, to convert DICOM files to AVI files used for input to EchoNet-Dynamic. The Notebook deidentifies the video by cropping out information outside of the ultrasound sector, resizes the input video, and saves the video in AVI format. \r\n\r\n### Setting Path to Data\r\n\r\nBy default, EchoNet-Dynamic assumes that a copy of the data is saved in a folder named `a4c-video-dir/` in this directory.\r\nThis path can be changed by creating a configuration file named `echonet.cfg` (an example configuration file is `example.cfg`).\r\n\r\n### Running Code\r\n\r\nEchoNet-Dynamic has three main components: segmenting the left ventricle, predicting ejection fraction from subsampled clips, and assessing cardiomyopathy with beat-by-beat predictions.\r\nEach of these components can be run with reasonable choices of hyperparameters with the scripts below.\r\nWe describe our full hyperparameter sweep in the next section.\r\n\r\n#### Frame-by-frame Semantic Segmentation of the Left Ventricle\r\n\r\n    echonet segmentation --save_video\r\n\r\nThis creates a directory named `output/segmentation/deeplabv3_resnet50_random/`, which will contain\r\n  - log.csv: training and validation losses\r\n  - best.pt: checkpoint of weights for the model with the lowest validation loss\r\n  - size.csv: estimated size of left ventricle for each frame and indicator for beginning of beat\r\n  - videos: directory containing videos with segmentation overlay\r\n\r\n#### Prediction of Ejection Fraction from Subsampled Clips\r\n\r\n  echonet video\r\n\r\nThis creates a directory named `output/video/r2plus1d_18_32_2_pretrained/`, which will contain\r\n  - log.csv: training and validation losses\r\n  - best.pt: checkpoint of weights for the model with the lowest validation loss\r\n  - test_predictions.csv: ejection fraction prediction for subsampled clips\r\n\r\n#### Beat-by-beat Prediction of Ejection Fraction from Full Video and Assesment of Cardiomyopathy\r\n\r\nThe final beat-by-beat prediction and analysis is performed with `scripts/beat_analysis.R`.\r\nThis script combines the results from segmentation output in `size.csv` and the clip-level ejection fraction prediction in `test_predictions.csv`. The beginning of each systolic phase is detected by using the peak detection algorithm from scipy (`scipy.signal.find_peaks`) and a video clip centered around the beat is used for beat-by-beat prediction.\r\n\r\n### Hyperparameter Sweeps\r\n\r\nThe full set of hyperparameter sweeps from the paper can be run via `run_experiments.sh`.\r\nIn particular, we choose between pretrained and random initialization for the weights, the model (selected from `r2plus1d_18`, `r3d_18`, and `mc3_18`), the length of the video (1, 4, 8, 16, 32, 64, and 96 frames), and the sampling period (1, 2, 4, 6, and 8 frames).\r\n"
  },
  {
    "path": "docs/google662250efb9603afb.html",
    "content": "google-site-verification: google662250efb9603afb.html"
  },
  {
    "path": "docs/header.html",
    "content": "<div id=\"top\">\n  <div class=\"container\">\n    <!--=== Skip links ===-->\n    <div id=\"skip\"> <a href=\"#content\" onClick=\"$('#content').focus()\">Skip to content</a> </div>\n    <!-- /Skip links -->\n  </div>\n</div>\n<div id=\"brandbar\">\n  <div class=\"container\"> <a href=\"http://www.stanford.edu\"> <img src=\"lagunita/images/brandbar-stanford-logo%402x.png\" alt=\"Stanford University\" width=\"152\" height=\"23\"> </a> </div>\n  <!-- .container end -->\n</div>\n<div id=\"header\" class=\"clearfix\" role=\"banner\">\n  <div class=\"container\">\n    <div class=\"row\">\n      <div class=\"col-md-8\">\n        <div id=\"logo\" class=\" clearfix\"><a href=\"/\"><img class=\"img-responsive\" src=\"SU_Seal_Red.png\" alt=\"Stanford University\" /></a></div>\n        <div id=\"signature\">\n          <div id=\"site-name\"> <a href=\"/\"><span id=\"site-name-1\" style=\"font-size:20pt\">CS 221: Artificial Intelligence: Principles and Techniques</span></a> </div>\n          <div id=\"site-slogan\"> <a href=\"/\"><span id=\"site_slogan\">Spring 2017-2018</span></a> </div>\n        </div>\n      </div>\n    </div>\n  </div>\n</div>\n<!-- Menu -->\n<div id=\"mainmenu\" class=\"clearfix\" role=\"navigation\">\n  <div class=\"container\">\n    <div class=\"navbar navbar-default\">\n      <!-- main navigation -->\n      <button type=\"button\" class=\"navbar-toggle btn-navbar\" data-toggle=\"collapse\" data-target=\".navbar-collapse\"> <span class=\"icon-bar\"></span> <span class=\"icon-bar\"></span> <span class=\"icon-bar\"></span> <span class=\"menu-text\">Menu</span></button>\n      <!-- /nav-collapse -->\n      <div class=\"navbar-collapse collapse\">\n        <div  role=\"navigation\">\n          <div id=\"primary-nav\">\n            <ul class=\"nav navbar-nav\" aria-label=\"primary navigation\">\n              <li id=\"nav-1\"> <a href=\"index.html\">Home</a> </li>\n              <li id=\"nav-1\"> <a href=\"index.html#calendar\">Calendar</a> </li>\n              <li id=\"nav-1\"> <a href=\"index.html#coursework\">Coursework</a> </li>\n              <li id=\"nav-1\"> <a href=\"https://gradescope.com/courses/17217\">Gradescope</a> </li>\n              <li id=\"nav-1\"> <a href=\"https://piazza.com/class/jfa5tlzb1me38q\">Piazza</a> </li>\n              <li id=\"nav-1\"> <a href=\"project.html\">Project</a> </li>\n              <li id=\"nav-1\"> <a href=\"http://queuestatus.com/queues/129\">Queuestatus</a> </li>\n              <li id=\"nav-1\"> <a href=\"index.html#schedule\">Schedule</a> </li>\n            </ul>\n          </div>\n        </div>\n      </div>\n      <!-- /nav-collapse -->\n    </div>\n    <!-- /navbar -->\n  </div>\n  <!-- /container -->\n</div>\n<!-- /mainmenu -->\n"
  },
  {
    "path": "docs/index.html",
    "content": "<!DOCTYPE html>\n<!-- Authors: David Ouyang, Bryan He 2019 -->\n<html lang=\"en\">\n<head>\n  <meta name=\"generator\" content=\"HTML Tidy for HTML5 for Linux version 5.7.16\">\n  <meta charset=\"utf-8\">\n  <title>EchoNet Dynamic</title>\n  <meta name=\"description\" content=\"EchoNet Dynamic: a Large New Cardiac Motion Video Data Resource for Medical Machine Learning.\"><!--#include file=\"lagunita.html\" -->\n  <!--Lagunita Theme (TODO: Server side include)-->\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n  <link rel=\"stylesheet\" href=\"lagunita/css/bootstrap.min.css\" type=\"text/css\">\n  <link rel=\"stylesheet\" href=\"//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css\" type=\"text/css\">\n  <link rel=\"stylesheet\" href=\"lagunita/css/base.min.css?v=0.1\" type=\"text/css\">\n  <link rel=\"stylesheet\" href=\"lagunita/css/custom.css?v=0.1\" type=\"text/css\">\n  <!--End Lagunita Theme-->\n  <link rel=\"icon\" type=\"image/ico\" href=\"favicon.ico\">\n</head>\n<body class=\"site-slogan\">\n  <!--#include file=\"header.html\" -->\n  <!--Header (TODO: Server side include)-->\n  <div id=\"top\">\n    <div class=\"container\">\n      <!--=== Skip links ===-->\n      <div id=\"skip\">\n        <a href=\"#content\" onclick=\"$('#content').focus()\">Skip to content</a>\n      </div><!-- /Skip links -->\n    </div>\n  </div>\n  <div id=\"brandbar\">\n    <div class=\"container\">\n      <a href=\"http://www.stanford.edu\"><img src=\"lagunita/images/brandbar-stanford-logo%402x.png\" alt=\"Stanford University\" width=\"152\" height=\"23\"></a>\n    </div><!-- .container end -->\n  </div>\n  <div id=\"header\" class=\"clearfix\" role=\"banner\">\n    <div class=\"container\">\n      <div class=\"row\">\n        <div class=\"col-md-8\">\n          <div id=\"signature\">\n            <div id=\"site-name\">\n              <a href=\"https://echonet.github.io/dynamic/index.html\"><span id=\"site-name-1\">EchoNet-Dynamic</span></a>\n            </div>\n            <div id=\"site-slogan\">\n              <a href=\"https://echonet.github.io/dynamic/index.html\"><span id=\"site_slogan\">A Large New Cardiac Motion Video Data Resource for Medical Machine Learning</span></a>\n            </div>\n          </div>\n        </div>\n      </div>\n    </div>\n  </div><!-- Menu -->\n  <div id=\"mainmenu\" class=\"clearfix\" role=\"navigation\">\n    <div class=\"container\">\n      <div class=\"navbar navbar-default\">\n        <!-- main navigation -->\n         <button type=\"button\" class=\"navbar-toggle btn-navbar\" data-toggle=\"collapse\" data-target=\".navbar-collapse\"> <span class=\"menu-text\">Menu</span></button> <!-- /nav-collapse -->\n        <div class=\"navbar-collapse collapse\">\n          <div role=\"navigation\">\n            <div id=\"primary-nav\">\n              <ul class=\"nav navbar-nav\" aria-label=\"primary navigation\">\n                <li id=\"nav-1\">\n                  <a href=\"index.html\">Home</a>\n                </li>\n                <li id=\"nav-2\">\n                  <a href=\"index.html#intro\">Introduction</a>\n                </li>\n                <li id=\"nav-3\">\n                  <a href=\"index.html#motivation\">Motivation</a>\n                </li>\n                <li id=\"nav-4\">\n                  <a href=\"index.html#dataset\">Dataset</a>\n                </li>\n                <li id=\"nav-5\">\n                  <a href=\"index.html#code\">Code</a>\n                </li>\n                <li id=\"nav-6\">\n                  <a href=\"index.html#access\">Accessing Dataset</a>\n                </li>\n                <li id=\"nav-7\">\n                  <a href=\"index.html#paper\">Paper</a>\n                </li>\n              </ul>\n            </div>\n          </div>\n        </div><!-- /nav-collapse -->\n      </div><!-- /navbar -->\n    </div><!-- /container -->\n  </div><!-- /mainmenu -->\n  <!--End Header-->\n  <div id=\"intro\" class=\"container\" role=\"introduction\" tabindex=\"0\">\n    <h2>Introduction</h2>\n    <p>Echocardiography, or cardiac ultrasound, is the most widely used and readily available imaging modality to assess cardiac function and structure. Combining portable instrumentation, rapid image acquisition, high temporal resolution, and without the risks of ionizing radiation, echocardiography is one of the most frequently utilized imaging studies in the United States and serves as the backbone of cardiovascular imaging. For diseases ranging from heart failure to valvular heart diseases, echocardiography is both necessary and sufﬁcient to diagnose many cardiovascular diseases. In addition to our deep learning model, we introduce a new large video dataset of echocardiograms for computer vision research. The EchoNet-Dynamic database includes 10,030 labeled echocardiogram videos and human expert annotations (measurements, tracings, and calculations) to provide a baseline to study cardiac motion and chamber sizes.</p><br>\n    <video class=\"center\" width=\"320\" height=\"320\" autoplay loop muted><source src=\"media/example_small.mp4\" type=\"video/mp4\"></video>\n    <br>\n    <p><h2 id=\"motivation\">Motivation</h2>\n    <p>Machine learning analysis of biomedical images has seen significant recent advances. In contrast, there has been much less work on medical videos, despite the fact that videos are routinely used in many clinical settings. A major bottleneck for this is the the lack of openly available and well annotated medical video data. Computer vision has benefited greatly from open databases which allow for collaboration, comparison, and creation of task specific architectures. We present the EchoNet-Dynamic dataset of 10,030 echocardiography videos, spanning the range of typical echocardiography lab imaging acquisition conditions, with corresponding labeled measurements including ejection fraction, left ventricular volume at end-systole and end-diastole, and human expert tracings of the left ventricle as an aid for studying machine learning approaches to evaluate cardiac function. We additionally present the performance of our model with 3-dimensional convolutional neural network architecture for video classification.\n    This model is used semantically segment the left ventricle and to assess ejection fraction to expert human performance and as a benchmark for further collaboration, comparison, and creation of task-specific architectures. To the best of our knowledge, this is the largest labeled medical video dataset made available publicly to researchers and medical professionals and first public report of video-based 3D convolutional architectures to assess cardiac function.</p><br>\n    <br>\n    <h2 id=\"dataset\">Dataset</h2><p><img class=\"center\" loading=\"lazy\" width=\"640\" src=\"media/Schematic.webp\">\n    <p><b>Echocardiogram Videos:</b> A standard full resting echocardiogram study consists of a series of videos and images visualizing the heart from different angles, positions, and image acquisition techniques. The dataset contains 10,030 apical-4-chamber echocardiography videos from individuals who underwent imaging between 2016 and 2018 as part of routine clinical care at Stanford University Hospital. Each video was cropped and masked to remove text and  information outside of the scanning sector. The resulting images were then downsampled by cubic interpolation into standardized 112x112 pixel videos.</p><br>\n    <p><img class=\"center\" loading=\"lazy\" width=\"500\" src=\"media/MetaDataVariables.webp\">\n    <p><b>Measurements:</b> In addition to the video itself, each study is linked to clinical measurements and calculations obtained by a registered sonographer and veriﬁed by a level 3 echocardiographer in the standard clinical workﬂow. A central metric of cardiac function is the left ventricular ejection fraction, which is used to diagnose cardiomyopathy, assess eligibility for certain chemotherapies, and determine indication for medical devices. The ejection fraction is expressed as a percentage and is the ratio of left ventricular end systolic volume (ESV) and left ventricular end diastolic volume (EDV) determined by (EDV - ESV) / EDV.</p><br>\n    <p><img class=\"center\" loading=\"lazy\" width=\"500\" src=\"media/Tracings.webp\">\n    <p><b>Tracings:</b> In our dataset, for each video, the left ventricle is traced at the endocardial border at two separate time points representing end-systole and end-diastole. Each tracing is used to estimate ventricular volume by integration of ventricular area over the length of the major axis of the ventricle. The expert tracings are represented by a collection of paired coordinates corresponding to each human tracing. The first pair of coordinates represent the length and direction of the long axis of the left ventricle, and subsequent coordinate pairs represent short axis linear distances starting from the apex of the heart to the mitral apparatus. Each coordinate pair is also listed with a video file name and frame number to identify the representative frame from which the tracings match.</p>\n    <p>Further description of the dataset is available in our <a href=\"NeuroIPS_2019_ML4H Workshop_Paper.pdf\">NeurIPS Machine Learning 4 Health workshop paper</a>.</p>\n    <br>\n    <h2 id=\"code\">Code</h2>\n    <p>Our code is available <a href=\"https://github.com/echonet/dynamic\">here</a>.</p>\n    <br>\n    <h2 id=\"access\">Accessing Dataset</h2>\n    <br>\n    <div class=\"well\" id=\"agreement\" tabindex=\"0\">\n      <h2>Stanford University School of Medicine EchoNet-Dynamic Dataset Research Use Agreement</h2>\n      <p>By registering for downloads from the EchoNet-Dynamic Dataset, you are agreeing to this Research Use Agreement, as well as to the Terms of Use of the Stanford University School of Medicine website as posted and updated periodically at http://www.stanford.edu/site/terms/.</p>\n      <p>1. Permission is granted to view and use the EchoNet-Dynamic Dataset without charge for personal, non-commercial research purposes only. Any commercial use, sale, or other monetization is prohibited.</p>\n      <p>2. Other than the rights granted herein, the Stanford University School of Medicine (“School of Medicine”) retains all rights, title, and interest in the EchoNet-Dynamic Dataset.</p>\n      <p>3. You may make a verbatim copy of the EchoNet-Dynamic Dataset for personal, non-commercial research use as permitted in this Research Use Agreement. If another user within your organization wishes to use the EchoNet-Dynamic Dataset, they must register as an individual user and comply with all the terms of this Research Use Agreement.</p>\n      <p>4. YOU MAY NOT DISTRIBUTE, PUBLISH, OR REPRODUCE A COPY of any portion or all of the EchoNet-Dynamic Dataset to others without specific prior written permission from the School of Medicine.</p>\n      <p>5. YOU MAY NOT SHARE THE DOWNLOAD LINK to the EchoNet-Dynamic dataset to others. If another user within your organization wishes to use the EchoNet-Dynamic Dataset, they must register as an individual user and comply with all the terms of this Research Use Agreement.</p>\n      <p>6. You must not modify, reverse engineer, decompile, or create derivative works from the EchoNet-Dynamic Dataset. You must not remove or alter any copyright or other proprietary notices in the EchoNet-Dynamic Dataset.</p>\n      <p>7. The EchoNet-Dynamic Dataset has not been reviewed or approved by the Food and Drug Administration, and is for non-clinical, Research Use Only. In no event shall data or images generated through the use of the EchoNet-Dynamic Dataset be used or relied upon in the diagnosis or provision of patient care.</p>\n      <p>8. THE ECHONET-DYNAMIC DATASET IS PROVIDED \"AS IS,\" AND STANFORD UNIVERSITY AND ITS COLLABORATORS DO NOT MAKE ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS ECHONET-DYNAMIC DATASET.</p>\n      <p>9. You will not make any attempt to re-identify any of the individual data subjects. Re-identification of individuals is strictly prohibited. Any re-identification of any individual data subject shall be immediately reported to the School of Medicine.</p>\n      <p>10. Any violation of this Research Use Agreement or other impermissible use shall be grounds for immediate termination of use of this EchoNet-Dynamic Dataset. In the event that the School of Medicine determines that the recipient has violated this Research Use Agreement or other impermissible use has been made, the School of Medicine may direct that the undersigned data recipient immediately return all copies of the EchoNet-Dynamic Dataset and retain no copies thereof even if you did not cause the violation or impermissible use.</p>\n      <p>In consideration for your agreement to the terms and conditions contained here, Stanford grants you permission to view and use the EchoNet-Dynamic Dataset for personal, non-commercial research. You may not otherwise copy, reproduce, retransmit, distribute, publish, commercially exploit or otherwise transfer any material.</p>\n      <h4>Limitation of Use</h4>\n      <p>You may use EchoNet-Dynamic Dataset for legal purposes only.</p>\n      <p>You agree to indemnify and hold Stanford harmless from any claims, losses or damages, including legal fees, arising out of or resulting from your use of the EchoNet-Dynamic Dataset or your violation or role in violation of these Terms. You agree to fully cooperate in Stanford’s defense against any such claims. These Terms shall be governed by and interpreted in accordance with the laws of California.</p>\n    </div>\n    \n    <div class=\"well\" id=\"access\" tabindex=\"0\">\n    <a href=\"https://stanfordaimi.azurewebsites.net/datasets/834e1cd1-92f7-4268-9daa-d359198b310a\">Access the dataset via the Stanford Artificial Intelligence in Medicine and Imaging (AIMI) Center Shared Datasets Portal.</a><br>\n    </div>\n    \n    <br>\n    <br>\n    <br>\n\n      <h2 id=\"paper\">Paper</h2>\n      <p><a href=\"https://doi.org/10.1038/s41586-020-2145-8\">Video-based AI for beat-to-beat assessment of cardiac function.</a><br>David Ouyang, Bryan He, Amirata Ghorbani, Neal Yuan, Joseph Ebinger, Curt P. Langlotz, Paul A. Heidenreich, Robert A. Harrington, David H. Liang, Euan A. Ashley, and James Y. Zou. <b>Nature</b> (2020) </p>\n      <p>For inquiries, contact us at <a href=\"mailto:%20ouyangd@stanford.edu\">ouyangd@stanford.edu</a>.</p>\n\n  </div>\n  <!--Lagunita Theme (TODO: Server side include)-->\n  <script src=\"//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js\"></script>\n  <script src=\"lagunita/js/modernizr.custom.17475.js\"></script>\n  <script src=\"lagunita/js/bootstrap.min.js\"></script>\n  <script src=\"lagunita/js/base.js?v=1.0\"></script>\n  <script src=\"lagunita/js/custom.js\"></script>\n  <!--End Lagunita Theme-->\n</body>\n</html>\n"
  },
  {
    "path": "docs/lagunita/css/custom.css",
    "content": ".center {\r\n  display: block;\r\n  margin-left: auto;\r\n  margin-right: auto;\r\n  max-width: 100%;\r\n}\r\n"
  },
  {
    "path": "docs/lagunita/js/base.js",
    "content": "// JavaScript Document\n\n/***\n    Utility functions available across the site\n***/\nvar LAGUNITA = {\n  size: function(){\n    // return window size based on visibility as calculated by CSS\n    var size = 'lg';\n  \n    // if we don't already have the size-detect div's, add them\n    if ( $('.size-detect-xs').length == 0 ) {\n      $('body')\n        .append('<div class=\"size-detect-xs\" />')\n        .append('<div class=\"size-detect-sm\" />')\n        .append('<div class=\"size-detect-md\" />')\n        .append('<div class=\"size-detect-lg\" />');\n    }\n  \n    $(['xs', 'sm', 'md', 'lg']).each(function(i, sz) {\n      if ($('.size-detect-'+sz).css('display') != 'none') {\n        size = sz;\n      }\n    });\n    return size;\n  },\n\n  stickFooter: function(){\n    // adjust css to make footer sticky\n    var h = $('#footer').height() + 'px';\n    $('#su-content').css('padding-bottom', h);\n    $('#footer').css('margin-top', '-'+h);\n  }\n};\n\n$(document).ready(function() {\n  LAGUNITA.stickFooter();\n\n  // note resize events and trigger resizeEnd event when resizing stops\n  $(window).resize(function() {\n    if(this.resizeTO) clearTimeout(this.resizeTO);\n    this.resizeTO = setTimeout(function() {\n      $(this).trigger('resizeEnd');\n    }, 200);\n  });\n  // Call responsive funtion when browser window resizing is complete\n  $(window).bind('resizeEnd', function() {\n    // show or hide the hamburger based on window size\n    var size = LAGUNITA.size(); // what size is our window (xs, sm, md or lg)\n    if (size == 'md' || size == 'lg') { // if size is md or lg, unhide search and gateway blocks\n      $('.navbar-collapse').collapse('hide');\n      // $('.navbar-collapse').removeClass('in').addClass('collapse');\n    }\n    // re-stick the footer in case its height has changed\n    LAGUNITA.stickFooter();\n  });\n  \n  $('.navbar-collapse').collapse({toggle: false}); // activate collapsibility without toggling state\n  \n  $('#skip > a').click(function(e){\n    var href = $(this).attr('href').substr(1); // remove the #\n    var target = $('a[name=\"' + href + '\"]');\n    target.focus();\n  });\n\n});\n"
  },
  {
    "path": "docs/lagunita/js/custom.js",
    "content": "// Custom Javascript for Lagunita HTML Theme\n// You can add custom JS functions to this file.\njQuery(document).ready(function($){\n  // Add \"active\" to nav element if matches body class of \"subnav-n\"\n  var bodyClass=\"\";\n  var matches = document.body.className.match(/(^|\\s+)(subnav-\\d+)(\\s|$)/);\n  if (matches) {\n    // found the bodyClass\n    bodyClass = matches[2];\n  }\n\n  if (bodyClass.length > 0){\n    var target = $( '#' + bodyClass );\n    target.addClass('active');\n  }\n});"
  },
  {
    "path": "docs/lagunita/js/modernizr.custom.17475.js",
    "content": "/* Modernizr 2.6.2 (Custom Build) | MIT & BSD\n * Build: http://modernizr.com/download/#-csstransforms-csstransitions-touch-shiv-cssclasses-prefixed-teststyles-testprop-testallprops-prefixes-domprefixes-load\n */\n;window.Modernizr=function(a,b,c){function z(a){j.cssText=a}function A(a,b){return z(m.join(a+\";\")+(b||\"\"))}function B(a,b){return typeof a===b}function C(a,b){return!!~(\"\"+a).indexOf(b)}function D(a,b){for(var d in a){var e=a[d];if(!C(e,\"-\")&&j[e]!==c)return b==\"pfx\"?e:!0}return!1}function E(a,b,d){for(var e in a){var f=b[a[e]];if(f!==c)return d===!1?a[e]:B(f,\"function\")?f.bind(d||b):f}return!1}function F(a,b,c){var d=a.charAt(0).toUpperCase()+a.slice(1),e=(a+\" \"+o.join(d+\" \")+d).split(\" \");return B(b,\"string\")||B(b,\"undefined\")?D(e,b):(e=(a+\" \"+p.join(d+\" \")+d).split(\" \"),E(e,b,c))}var d=\"2.6.2\",e={},f=!0,g=b.documentElement,h=\"modernizr\",i=b.createElement(h),j=i.style,k,l={}.toString,m=\" -webkit- -moz- -o- -ms- \".split(\" \"),n=\"Webkit Moz O ms\",o=n.split(\" \"),p=n.toLowerCase().split(\" \"),q={},r={},s={},t=[],u=t.slice,v,w=function(a,c,d,e){var f,i,j,k,l=b.createElement(\"div\"),m=b.body,n=m||b.createElement(\"body\");if(parseInt(d,10))while(d--)j=b.createElement(\"div\"),j.id=e?e[d]:h+(d+1),l.appendChild(j);return f=[\"&#173;\",'<style id=\"s',h,'\">',a,\"</style>\"].join(\"\"),l.id=h,(m?l:n).innerHTML+=f,n.appendChild(l),m||(n.style.background=\"\",n.style.overflow=\"hidden\",k=g.style.overflow,g.style.overflow=\"hidden\",g.appendChild(n)),i=c(l,a),m?l.parentNode.removeChild(l):(n.parentNode.removeChild(n),g.style.overflow=k),!!i},x={}.hasOwnProperty,y;!B(x,\"undefined\")&&!B(x.call,\"undefined\")?y=function(a,b){return x.call(a,b)}:y=function(a,b){return b in a&&B(a.constructor.prototype[b],\"undefined\")},Function.prototype.bind||(Function.prototype.bind=function(b){var c=this;if(typeof c!=\"function\")throw new TypeError;var d=u.call(arguments,1),e=function(){if(this instanceof e){var a=function(){};a.prototype=c.prototype;var f=new a,g=c.apply(f,d.concat(u.call(arguments)));return Object(g)===g?g:f}return c.apply(b,d.concat(u.call(arguments)))};return e}),q.touch=function(){var c;return\"ontouchstart\"in a||a.DocumentTouch&&b instanceof DocumentTouch?c=!0:w([\"@media (\",m.join(\"touch-enabled),(\"),h,\")\",\"{#modernizr{top:9px;position:absolute}}\"].join(\"\"),function(a){c=a.offsetTop===9}),c},q.csstransforms=function(){return!!F(\"transform\")},q.csstransitions=function(){return F(\"transition\")};for(var G in q)y(q,G)&&(v=G.toLowerCase(),e[v]=q[G](),t.push((e[v]?\"\":\"no-\")+v));return e.addTest=function(a,b){if(typeof a==\"object\")for(var d in a)y(a,d)&&e.addTest(d,a[d]);else{a=a.toLowerCase();if(e[a]!==c)return e;b=typeof b==\"function\"?b():b,typeof f!=\"undefined\"&&f&&(g.className+=\" \"+(b?\"\":\"no-\")+a),e[a]=b}return e},z(\"\"),i=k=null,function(a,b){function k(a,b){var c=a.createElement(\"p\"),d=a.getElementsByTagName(\"head\")[0]||a.documentElement;return c.innerHTML=\"x<style>\"+b+\"</style>\",d.insertBefore(c.lastChild,d.firstChild)}function l(){var a=r.elements;return typeof a==\"string\"?a.split(\" \"):a}function m(a){var b=i[a[g]];return b||(b={},h++,a[g]=h,i[h]=b),b}function n(a,c,f){c||(c=b);if(j)return c.createElement(a);f||(f=m(c));var g;return f.cache[a]?g=f.cache[a].cloneNode():e.test(a)?g=(f.cache[a]=f.createElem(a)).cloneNode():g=f.createElem(a),g.canHaveChildren&&!d.test(a)?f.frag.appendChild(g):g}function o(a,c){a||(a=b);if(j)return a.createDocumentFragment();c=c||m(a);var d=c.frag.cloneNode(),e=0,f=l(),g=f.length;for(;e<g;e++)d.createElement(f[e]);return d}function p(a,b){b.cache||(b.cache={},b.createElem=a.createElement,b.createFrag=a.createDocumentFragment,b.frag=b.createFrag()),a.createElement=function(c){return r.shivMethods?n(c,a,b):b.createElem(c)},a.createDocumentFragment=Function(\"h,f\",\"return function(){var n=f.cloneNode(),c=n.createElement;h.shivMethods&&(\"+l().join().replace(/\\w+/g,function(a){return b.createElem(a),b.frag.createElement(a),'c(\"'+a+'\")'})+\");return n}\")(r,b.frag)}function q(a){a||(a=b);var c=m(a);return r.shivCSS&&!f&&!c.hasCSS&&(c.hasCSS=!!k(a,\"article,aside,figcaption,figure,footer,header,hgroup,nav,section{display:block}mark{background:#FF0;color:#000}\")),j||p(a,c),a}var c=a.html5||{},d=/^<|^(?:button|map|select|textarea|object|iframe|option|optgroup)$/i,e=/^(?:a|b|code|div|fieldset|h1|h2|h3|h4|h5|h6|i|label|li|ol|p|q|span|strong|style|table|tbody|td|th|tr|ul)$/i,f,g=\"_html5shiv\",h=0,i={},j;(function(){try{var a=b.createElement(\"a\");a.innerHTML=\"<xyz></xyz>\",f=\"hidden\"in a,j=a.childNodes.length==1||function(){b.createElement(\"a\");var a=b.createDocumentFragment();return typeof a.cloneNode==\"undefined\"||typeof a.createDocumentFragment==\"undefined\"||typeof a.createElement==\"undefined\"}()}catch(c){f=!0,j=!0}})();var r={elements:c.elements||\"abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video\",shivCSS:c.shivCSS!==!1,supportsUnknownElements:j,shivMethods:c.shivMethods!==!1,type:\"default\",shivDocument:q,createElement:n,createDocumentFragment:o};a.html5=r,q(b)}(this,b),e._version=d,e._prefixes=m,e._domPrefixes=p,e._cssomPrefixes=o,e.testProp=function(a){return D([a])},e.testAllProps=F,e.testStyles=w,e.prefixed=function(a,b,c){return b?F(a,b,c):F(a,\"pfx\")},g.className=g.className.replace(/(^|\\s)no-js(\\s|$)/,\"$1$2\")+(f?\" js \"+t.join(\" \"):\"\"),e}(this,this.document),function(a,b,c){function d(a){return\"[object Function]\"==o.call(a)}function e(a){return\"string\"==typeof a}function f(){}function g(a){return!a||\"loaded\"==a||\"complete\"==a||\"uninitialized\"==a}function h(){var a=p.shift();q=1,a?a.t?m(function(){(\"c\"==a.t?B.injectCss:B.injectJs)(a.s,0,a.a,a.x,a.e,1)},0):(a(),h()):q=0}function i(a,c,d,e,f,i,j){function k(b){if(!o&&g(l.readyState)&&(u.r=o=1,!q&&h(),l.onload=l.onreadystatechange=null,b)){\"img\"!=a&&m(function(){t.removeChild(l)},50);for(var d in y[c])y[c].hasOwnProperty(d)&&y[c][d].onload()}}var j=j||B.errorTimeout,l=b.createElement(a),o=0,r=0,u={t:d,s:c,e:f,a:i,x:j};1===y[c]&&(r=1,y[c]=[]),\"object\"==a?l.data=c:(l.src=c,l.type=a),l.width=l.height=\"0\",l.onerror=l.onload=l.onreadystatechange=function(){k.call(this,r)},p.splice(e,0,u),\"img\"!=a&&(r||2===y[c]?(t.insertBefore(l,s?null:n),m(k,j)):y[c].push(l))}function j(a,b,c,d,f){return q=0,b=b||\"j\",e(a)?i(\"c\"==b?v:u,a,b,this.i++,c,d,f):(p.splice(this.i++,0,a),1==p.length&&h()),this}function k(){var a=B;return a.loader={load:j,i:0},a}var l=b.documentElement,m=a.setTimeout,n=b.getElementsByTagName(\"script\")[0],o={}.toString,p=[],q=0,r=\"MozAppearance\"in l.style,s=r&&!!b.createRange().compareNode,t=s?l:n.parentNode,l=a.opera&&\"[object Opera]\"==o.call(a.opera),l=!!b.attachEvent&&!l,u=r?\"object\":l?\"script\":\"img\",v=l?\"script\":u,w=Array.isArray||function(a){return\"[object Array]\"==o.call(a)},x=[],y={},z={timeout:function(a,b){return b.length&&(a.timeout=b[0]),a}},A,B;B=function(a){function b(a){var a=a.split(\"!\"),b=x.length,c=a.pop(),d=a.length,c={url:c,origUrl:c,prefixes:a},e,f,g;for(f=0;f<d;f++)g=a[f].split(\"=\"),(e=z[g.shift()])&&(c=e(c,g));for(f=0;f<b;f++)c=x[f](c);return c}function g(a,e,f,g,h){var i=b(a),j=i.autoCallback;i.url.split(\".\").pop().split(\"?\").shift(),i.bypass||(e&&(e=d(e)?e:e[a]||e[g]||e[a.split(\"/\").pop().split(\"?\")[0]]),i.instead?i.instead(a,e,f,g,h):(y[i.url]?i.noexec=!0:y[i.url]=1,f.load(i.url,i.forceCSS||!i.forceJS&&\"css\"==i.url.split(\".\").pop().split(\"?\").shift()?\"c\":c,i.noexec,i.attrs,i.timeout),(d(e)||d(j))&&f.load(function(){k(),e&&e(i.origUrl,h,g),j&&j(i.origUrl,h,g),y[i.url]=2})))}function h(a,b){function c(a,c){if(a){if(e(a))c||(j=function(){var a=[].slice.call(arguments);k.apply(this,a),l()}),g(a,j,b,0,h);else if(Object(a)===a)for(n in m=function(){var b=0,c;for(c in a)a.hasOwnProperty(c)&&b++;return b}(),a)a.hasOwnProperty(n)&&(!c&&!--m&&(d(j)?j=function(){var a=[].slice.call(arguments);k.apply(this,a),l()}:j[n]=function(a){return function(){var b=[].slice.call(arguments);a&&a.apply(this,b),l()}}(k[n])),g(a[n],j,b,n,h))}else!c&&l()}var h=!!a.test,i=a.load||a.both,j=a.callback||f,k=j,l=a.complete||f,m,n;c(h?a.yep:a.nope,!!i),i&&c(i)}var i,j,l=this.yepnope.loader;if(e(a))g(a,0,l,0);else if(w(a))for(i=0;i<a.length;i++)j=a[i],e(j)?g(j,0,l,0):w(j)?B(j):Object(j)===j&&h(j,l);else Object(a)===a&&h(a,l)},B.addPrefix=function(a,b){z[a]=b},B.addFilter=function(a){x.push(a)},B.errorTimeout=1e4,null==b.readyState&&b.addEventListener&&(b.readyState=\"loading\",b.addEventListener(\"DOMContentLoaded\",A=function(){b.removeEventListener(\"DOMContentLoaded\",A,0),b.readyState=\"complete\"},0)),a.yepnope=k(),a.yepnope.executeStack=h,a.yepnope.injectJs=function(a,c,d,e,i,j){var k=b.createElement(\"script\"),l,o,e=e||B.errorTimeout;k.src=a;for(o in d)k.setAttribute(o,d[o]);c=j?h:c||f,k.onreadystatechange=k.onload=function(){!l&&g(k.readyState)&&(l=1,c(),k.onload=k.onreadystatechange=null)},m(function(){l||(l=1,c(1))},e),i?k.onload():n.parentNode.insertBefore(k,n)},a.yepnope.injectCss=function(a,c,d,e,g,i){var e=b.createElement(\"link\"),j,c=i?h:c||f;e.href=a,e.rel=\"stylesheet\",e.type=\"text/css\";for(j in d)e.setAttribute(j,d[j]);g||(n.parentNode.insertBefore(e,n),m(c,0))}}(this,document),Modernizr.load=function(){yepnope.apply(window,[].slice.call(arguments,0))};"
  },
  {
    "path": "docs/lagunita.html",
    "content": "<meta charset=\"utf-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n\n<script src=\"plugins/main.js?0\"></script>\n\n<script src=\"//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js\"></script>\n<script src=\"lagunita/js/modernizr.custom.17475.js\"></script>\n<script src=\"lagunita/js/bootstrap.min.js\"></script>\n<script src=\"lagunita/js/base.js?v=1.0\"></script>\n<script src=\"lagunita/js/custom.js\"></script>\n<link rel=\"stylesheet\" href=\"lagunita/css/bootstrap.min.css\" type=\"text/css\" />\n<link rel=\"stylesheet\" href=\"//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css\" type=\"text/css\" />\n<link rel=\"stylesheet\" href=\"lagunita/css/base.min.css?v=0.1\" type=\"text/css\" />\n<link rel=\"stylesheet\" href=\"lagunita/css/custom.css?v=0.1\" type=\"text/css\"/>\n"
  },
  {
    "path": "echonet/__init__.py",
    "content": "\"\"\"\nThe echonet package contains code for loading echocardiogram videos, and\nfunctions for training and testing segmentation and ejection fraction\nprediction models.\n\"\"\"\n\nimport click\n\nfrom echonet.__version__ import __version__\nfrom echonet.config import CONFIG as config\nimport echonet.datasets as datasets\nimport echonet.utils as utils\n\n\n@click.group()\ndef main():\n    \"\"\"Entry point for command line interface.\"\"\"\n\n\ndel click\n\n\nmain.add_command(utils.segmentation.run)\nmain.add_command(utils.video.run)\n\n__all__ = [\"__version__\", \"config\", \"datasets\", \"main\", \"utils\"]\n"
  },
  {
    "path": "echonet/__main__.py",
    "content": "\"\"\"Entry point for command line.\"\"\"\n\nimport echonet\n\n\nif __name__ == '__main__':\n    echonet.main()\n"
  },
  {
    "path": "echonet/__version__.py",
    "content": "\"\"\"Version number for Echonet package.\"\"\"\n\n__version__ = \"1.0.0\"\n"
  },
  {
    "path": "echonet/config.py",
    "content": "\"\"\"Sets paths based on configuration files.\"\"\"\n\nimport configparser\nimport os\nimport types\n\n_FILENAME = None\n_PARAM = {}\nfor filename in [\"echonet.cfg\",\n                 \".echonet.cfg\",\n                 os.path.expanduser(\"~/echonet.cfg\"),\n                 os.path.expanduser(\"~/.echonet.cfg\"),\n                 ]:\n    if os.path.isfile(filename):\n        _FILENAME = filename\n        config = configparser.ConfigParser()\n        with open(filename, \"r\") as f:\n            config.read_string(\"[config]\\n\" + f.read())\n            _PARAM = config[\"config\"]\n        break\n\nCONFIG = types.SimpleNamespace(\n    FILENAME=_FILENAME,\n    DATA_DIR=_PARAM.get(\"data_dir\", \"a4c-video-dir/\"))\n"
  },
  {
    "path": "echonet/datasets/__init__.py",
    "content": "\"\"\"\nThe echonet.datasets submodule defines a Pytorch dataset for loading\nechocardiogram videos.\n\"\"\"\n\nfrom .echo import Echo\n\n__all__ = [\"Echo\"]\n"
  },
  {
    "path": "echonet/datasets/echo.py",
    "content": "\"\"\"EchoNet-Dynamic Dataset.\"\"\"\n\nimport os\nimport collections\nimport pandas\n\nimport numpy as np\nimport skimage.draw\nimport torchvision\nimport echonet\n\n\nclass Echo(torchvision.datasets.VisionDataset):\n    \"\"\"EchoNet-Dynamic Dataset.\n\n    Args:\n        root (string): Root directory of dataset (defaults to `echonet.config.DATA_DIR`)\n        split (string): One of {``train'', ``val'', ``test'', ``all'', or ``external_test''}\n        target_type (string or list, optional): Type of target to use,\n            ``Filename'', ``EF'', ``EDV'', ``ESV'', ``LargeIndex'',\n            ``SmallIndex'', ``LargeFrame'', ``SmallFrame'', ``LargeTrace'',\n            or ``SmallTrace''\n            Can also be a list to output a tuple with all specified target types.\n            The targets represent:\n                ``Filename'' (string): filename of video\n                ``EF'' (float): ejection fraction\n                ``EDV'' (float): end-diastolic volume\n                ``ESV'' (float): end-systolic volume\n                ``LargeIndex'' (int): index of large (diastolic) frame in video\n                ``SmallIndex'' (int): index of small (systolic) frame in video\n                ``LargeFrame'' (np.array shape=(3, height, width)): normalized large (diastolic) frame\n                ``SmallFrame'' (np.array shape=(3, height, width)): normalized small (systolic) frame\n                ``LargeTrace'' (np.array shape=(height, width)): left ventricle large (diastolic) segmentation\n                    value of 0 indicates pixel is outside left ventricle\n                             1 indicates pixel is inside left ventricle\n                ``SmallTrace'' (np.array shape=(height, width)): left ventricle small (systolic) segmentation\n                    value of 0 indicates pixel is outside left ventricle\n                             1 indicates pixel is inside left ventricle\n            Defaults to ``EF''.\n        mean (int, float, or np.array shape=(3,), optional): means for all (if scalar) or each (if np.array) channel.\n            Used for normalizing the video. Defaults to 0 (video is not shifted).\n        std (int, float, or np.array shape=(3,), optional): standard deviation for all (if scalar) or each (if np.array) channel.\n            Used for normalizing the video. Defaults to 0 (video is not scaled).\n        length (int or None, optional): Number of frames to clip from video. If ``None'', longest possible clip is returned.\n            Defaults to 16.\n        period (int, optional): Sampling period for taking a clip from the video (i.e. every ``period''-th frame is taken)\n            Defaults to 2.\n        max_length (int or None, optional): Maximum number of frames to clip from video (main use is for shortening excessively\n            long videos when ``length'' is set to None). If ``None'', shortening is not applied to any video.\n            Defaults to 250.\n        clips (int, optional): Number of clips to sample. Main use is for test-time augmentation with random clips.\n            Defaults to 1.\n        pad (int or None, optional): Number of pixels to pad all frames on each side (used as augmentation).\n            and a window of the original size is taken. If ``None'', no padding occurs.\n            Defaults to ``None''.\n        noise (float or None, optional): Fraction of pixels to black out as simulated noise. If ``None'', no simulated noise is added.\n            Defaults to ``None''.\n        target_transform (callable, optional): A function/transform that takes in the target and transforms it.\n        external_test_location (string): Path to videos to use for external testing.\n    \"\"\"\n\n    def __init__(self, root=None,\n                 split=\"train\", target_type=\"EF\",\n                 mean=0., std=1.,\n                 length=16, period=2,\n                 max_length=250,\n                 clips=1,\n                 pad=None,\n                 noise=None,\n                 target_transform=None,\n                 external_test_location=None):\n        if root is None:\n            root = echonet.config.DATA_DIR\n\n        super().__init__(root, target_transform=target_transform)\n\n        self.split = split.upper()\n        if not isinstance(target_type, list):\n            target_type = [target_type]\n        self.target_type = target_type\n        self.mean = mean\n        self.std = std\n        self.length = length\n        self.max_length = max_length\n        self.period = period\n        self.clips = clips\n        self.pad = pad\n        self.noise = noise\n        self.target_transform = target_transform\n        self.external_test_location = external_test_location\n\n        self.fnames, self.outcome = [], []\n\n        if self.split == \"EXTERNAL_TEST\":\n            self.fnames = sorted(os.listdir(self.external_test_location))\n        else:\n            # Load video-level labels\n            with open(os.path.join(self.root, \"FileList.csv\")) as f:\n                data = pandas.read_csv(f)\n            data[\"Split\"].map(lambda x: x.upper())\n\n            if self.split != \"ALL\":\n                data = data[data[\"Split\"] == self.split]\n\n            self.header = data.columns.tolist()\n            self.fnames = data[\"FileName\"].tolist()\n            self.fnames = [fn + \".avi\" for fn in self.fnames if os.path.splitext(fn)[1] == \"\"]  # Assume avi if no suffix\n            self.outcome = data.values.tolist()\n\n            # Check that files are present\n            missing = set(self.fnames) - set(os.listdir(os.path.join(self.root, \"Videos\")))\n            if len(missing) != 0:\n                print(\"{} videos could not be found in {}:\".format(len(missing), os.path.join(self.root, \"Videos\")))\n                for f in sorted(missing):\n                    print(\"\\t\", f)\n                raise FileNotFoundError(os.path.join(self.root, \"Videos\", sorted(missing)[0]))\n\n            # Load traces\n            self.frames = collections.defaultdict(list)\n            self.trace = collections.defaultdict(_defaultdict_of_lists)\n\n            with open(os.path.join(self.root, \"VolumeTracings.csv\")) as f:\n                header = f.readline().strip().split(\",\")\n                assert header == [\"FileName\", \"X1\", \"Y1\", \"X2\", \"Y2\", \"Frame\"]\n\n                for line in f:\n                    filename, x1, y1, x2, y2, frame = line.strip().split(',')\n                    x1 = float(x1)\n                    y1 = float(y1)\n                    x2 = float(x2)\n                    y2 = float(y2)\n                    frame = int(frame)\n                    if frame not in self.trace[filename]:\n                        self.frames[filename].append(frame)\n                    self.trace[filename][frame].append((x1, y1, x2, y2))\n            for filename in self.frames:\n                for frame in self.frames[filename]:\n                    self.trace[filename][frame] = np.array(self.trace[filename][frame])\n\n            # A small number of videos are missing traces; remove these videos\n            keep = [len(self.frames[f]) >= 2 for f in self.fnames]\n            self.fnames = [f for (f, k) in zip(self.fnames, keep) if k]\n            self.outcome = [f for (f, k) in zip(self.outcome, keep) if k]\n\n    def __getitem__(self, index):\n        # Find filename of video\n        if self.split == \"EXTERNAL_TEST\":\n            video = os.path.join(self.external_test_location, self.fnames[index])\n        elif self.split == \"CLINICAL_TEST\":\n            video = os.path.join(self.root, \"ProcessedStrainStudyA4c\", self.fnames[index])\n        else:\n            video = os.path.join(self.root, \"Videos\", self.fnames[index])\n\n        # Load video into np.array\n        video = echonet.utils.loadvideo(video).astype(np.float32)\n\n        # Add simulated noise (black out random pixels)\n        # 0 represents black at this point (video has not been normalized yet)\n        if self.noise is not None:\n            n = video.shape[1] * video.shape[2] * video.shape[3]\n            ind = np.random.choice(n, round(self.noise * n), replace=False)\n            f = ind % video.shape[1]\n            ind //= video.shape[1]\n            i = ind % video.shape[2]\n            ind //= video.shape[2]\n            j = ind\n            video[:, f, i, j] = 0\n\n        # Apply normalization\n        if isinstance(self.mean, (float, int)):\n            video -= self.mean\n        else:\n            video -= self.mean.reshape(3, 1, 1, 1)\n\n        if isinstance(self.std, (float, int)):\n            video /= self.std\n        else:\n            video /= self.std.reshape(3, 1, 1, 1)\n\n        # Set number of frames\n        c, f, h, w = video.shape\n        if self.length is None:\n            # Take as many frames as possible\n            length = f // self.period\n        else:\n            # Take specified number of frames\n            length = self.length\n\n        if self.max_length is not None:\n            # Shorten videos to max_length\n            length = min(length, self.max_length)\n\n        if f < length * self.period:\n            # Pad video with frames filled with zeros if too short\n            # 0 represents the mean color (dark grey), since this is after normalization\n            video = np.concatenate((video, np.zeros((c, length * self.period - f, h, w), video.dtype)), axis=1)\n            c, f, h, w = video.shape  # pylint: disable=E0633\n\n        if self.clips == \"all\":\n            # Take all possible clips of desired length\n            start = np.arange(f - (length - 1) * self.period)\n        else:\n            # Take random clips from video\n            start = np.random.choice(f - (length - 1) * self.period, self.clips)\n\n        # Gather targets\n        target = []\n        for t in self.target_type:\n            key = self.fnames[index]\n            if t == \"Filename\":\n                target.append(self.fnames[index])\n            elif t == \"LargeIndex\":\n                # Traces are sorted by cross-sectional area\n                # Largest (diastolic) frame is last\n                target.append(np.int(self.frames[key][-1]))\n            elif t == \"SmallIndex\":\n                # Largest (diastolic) frame is first\n                target.append(np.int(self.frames[key][0]))\n            elif t == \"LargeFrame\":\n                target.append(video[:, self.frames[key][-1], :, :])\n            elif t == \"SmallFrame\":\n                target.append(video[:, self.frames[key][0], :, :])\n            elif t in [\"LargeTrace\", \"SmallTrace\"]:\n                if t == \"LargeTrace\":\n                    t = self.trace[key][self.frames[key][-1]]\n                else:\n                    t = self.trace[key][self.frames[key][0]]\n                x1, y1, x2, y2 = t[:, 0], t[:, 1], t[:, 2], t[:, 3]\n                x = np.concatenate((x1[1:], np.flip(x2[1:])))\n                y = np.concatenate((y1[1:], np.flip(y2[1:])))\n\n                r, c = skimage.draw.polygon(np.rint(y).astype(np.int), np.rint(x).astype(np.int), (video.shape[2], video.shape[3]))\n                mask = np.zeros((video.shape[2], video.shape[3]), np.float32)\n                mask[r, c] = 1\n                target.append(mask)\n            else:\n                if self.split == \"CLINICAL_TEST\" or self.split == \"EXTERNAL_TEST\":\n                    target.append(np.float32(0))\n                else:\n                    target.append(np.float32(self.outcome[index][self.header.index(t)]))\n\n        if target != []:\n            target = tuple(target) if len(target) > 1 else target[0]\n            if self.target_transform is not None:\n                target = self.target_transform(target)\n\n        # Select clips from video\n        video = tuple(video[:, s + self.period * np.arange(length), :, :] for s in start)\n        if self.clips == 1:\n            video = video[0]\n        else:\n            video = np.stack(video)\n\n        if self.pad is not None:\n            # Add padding of zeros (mean color of videos)\n            # Crop of original size is taken out\n            # (Used as augmentation)\n            c, l, h, w = video.shape\n            temp = np.zeros((c, l, h + 2 * self.pad, w + 2 * self.pad), dtype=video.dtype)\n            temp[:, :, self.pad:-self.pad, self.pad:-self.pad] = video  # pylint: disable=E1130\n            i, j = np.random.randint(0, 2 * self.pad, 2)\n            video = temp[:, :, i:(i + h), j:(j + w)]\n\n        return video, target\n\n    def __len__(self):\n        return len(self.fnames)\n\n    def extra_repr(self) -> str:\n        \"\"\"Additional information to add at end of __repr__.\"\"\"\n        lines = [\"Target type: {target_type}\", \"Split: {split}\"]\n        return '\\n'.join(lines).format(**self.__dict__)\n\n\ndef _defaultdict_of_lists():\n    \"\"\"Returns a defaultdict of lists.\n\n    This is used to avoid issues with Windows (if this function is anonymous,\n    the Echo dataset cannot be used in a dataloader).\n    \"\"\"\n\n    return collections.defaultdict(list)\n"
  },
  {
    "path": "echonet/utils/__init__.py",
    "content": "\"\"\"Utility functions for videos, plotting and computing performance metrics.\"\"\"\n\nimport os\nimport typing\n\nimport cv2  # pytype: disable=attribute-error\nimport matplotlib\nimport numpy as np\nimport torch\nimport tqdm\n\nfrom . import video\nfrom . import segmentation\n\n\ndef loadvideo(filename: str) -> np.ndarray:\n    \"\"\"Loads a video from a file.\n\n    Args:\n        filename (str): filename of video\n\n    Returns:\n        A np.ndarray with dimensions (channels=3, frames, height, width). The\n        values will be uint8's ranging from 0 to 255.\n\n    Raises:\n        FileNotFoundError: Could not find `filename`\n        ValueError: An error occurred while reading the video\n    \"\"\"\n\n    if not os.path.exists(filename):\n        raise FileNotFoundError(filename)\n    capture = cv2.VideoCapture(filename)\n\n    frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))\n    frame_width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))\n    frame_height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))\n\n    v = np.zeros((frame_count, frame_height, frame_width, 3), np.uint8)\n\n    for count in range(frame_count):\n        ret, frame = capture.read()\n        if not ret:\n            raise ValueError(\"Failed to load frame #{} of {}.\".format(count, filename))\n\n        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n        v[count, :, :] = frame\n\n    v = v.transpose((3, 0, 1, 2))\n\n    return v\n\n\ndef savevideo(filename: str, array: np.ndarray, fps: typing.Union[float, int] = 1):\n    \"\"\"Saves a video to a file.\n\n    Args:\n        filename (str): filename of video\n        array (np.ndarray): video of uint8's with shape (channels=3, frames, height, width)\n        fps (float or int): frames per second\n\n    Returns:\n        None\n    \"\"\"\n\n    c, _, height, width = array.shape\n\n    if c != 3:\n        raise ValueError(\"savevideo expects array of shape (channels=3, frames, height, width), got shape ({})\".format(\", \".join(map(str, array.shape))))\n    fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')\n    out = cv2.VideoWriter(filename, fourcc, fps, (width, height))\n\n    for frame in array.transpose((1, 2, 3, 0)):\n        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)\n        out.write(frame)\n\n\ndef get_mean_and_std(dataset: torch.utils.data.Dataset,\n                     samples: int = 128,\n                     batch_size: int = 8,\n                     num_workers: int = 4):\n    \"\"\"Computes mean and std from samples from a Pytorch dataset.\n\n    Args:\n        dataset (torch.utils.data.Dataset): A Pytorch dataset.\n            ``dataset[i][0]'' is expected to be the i-th video in the dataset, which\n            should be a ``torch.Tensor'' of dimensions (channels=3, frames, height, width)\n        samples (int or None, optional): Number of samples to take from dataset. If ``None'', mean and\n            standard deviation are computed over all elements.\n            Defaults to 128.\n        batch_size (int, optional): how many samples per batch to load\n            Defaults to 8.\n        num_workers (int, optional): how many subprocesses to use for data\n            loading. If 0, the data will be loaded in the main process.\n            Defaults to 4.\n\n    Returns:\n       A tuple of the mean and standard deviation. Both are represented as np.array's of dimension (channels,).\n    \"\"\"\n\n    if samples is not None and len(dataset) > samples:\n        indices = np.random.choice(len(dataset), samples, replace=False)\n        dataset = torch.utils.data.Subset(dataset, indices)\n    dataloader = torch.utils.data.DataLoader(\n        dataset, batch_size=batch_size, num_workers=num_workers, shuffle=True)\n\n    n = 0  # number of elements taken (should be equal to samples by end of for loop)\n    s1 = 0.  # sum of elements along channels (ends up as np.array of dimension (channels,))\n    s2 = 0.  # sum of squares of elements along channels (ends up as np.array of dimension (channels,))\n    for (x, *_) in tqdm.tqdm(dataloader):\n        x = x.transpose(0, 1).contiguous().view(3, -1)\n        n += x.shape[1]\n        s1 += torch.sum(x, dim=1).numpy()\n        s2 += torch.sum(x ** 2, dim=1).numpy()\n    mean = s1 / n  # type: np.ndarray\n    std = np.sqrt(s2 / n - mean ** 2)  # type: np.ndarray\n\n    mean = mean.astype(np.float32)\n    std = std.astype(np.float32)\n\n    return mean, std\n\n\ndef bootstrap(a, b, func, samples=10000):\n    \"\"\"Computes a bootstrapped confidence intervals for ``func(a, b)''.\n\n    Args:\n        a (array_like): first argument to `func`.\n        b (array_like): second argument to `func`.\n        func (callable): Function to compute confidence intervals for.\n            ``dataset[i][0]'' is expected to be the i-th video in the dataset, which\n            should be a ``torch.Tensor'' of dimensions (channels=3, frames, height, width)\n        samples (int, optional): Number of samples to compute.\n            Defaults to 10000.\n\n    Returns:\n       A tuple of (`func(a, b)`, estimated 5-th percentile, estimated 95-th percentile).\n    \"\"\"\n    a = np.array(a)\n    b = np.array(b)\n\n    bootstraps = []\n    for _ in range(samples):\n        ind = np.random.choice(len(a), len(a))\n        bootstraps.append(func(a[ind], b[ind]))\n    bootstraps = sorted(bootstraps)\n\n    return func(a, b), bootstraps[round(0.05 * len(bootstraps))], bootstraps[round(0.95 * len(bootstraps))]\n\n\ndef latexify():\n    \"\"\"Sets matplotlib params to appear more like LaTeX.\n\n    Based on https://nipunbatra.github.io/blog/2014/latexify.html\n    \"\"\"\n    params = {'backend': 'pdf',\n              'axes.titlesize': 8,\n              'axes.labelsize': 8,\n              'font.size': 8,\n              'legend.fontsize': 8,\n              'xtick.labelsize': 8,\n              'ytick.labelsize': 8,\n              'font.family': 'DejaVu Serif',\n              'font.serif': 'Computer Modern',\n              }\n    matplotlib.rcParams.update(params)\n\n\ndef dice_similarity_coefficient(inter, union):\n    \"\"\"Computes the dice similarity coefficient.\n\n    Args:\n        inter (iterable): iterable of the intersections\n        union (iterable): iterable of the unions\n    \"\"\"\n    return 2 * sum(inter) / (sum(union) + sum(inter))\n\n\n__all__ = [\"video\", \"segmentation\", \"loadvideo\", \"savevideo\", \"get_mean_and_std\", \"bootstrap\", \"latexify\", \"dice_similarity_coefficient\"]\n"
  },
  {
    "path": "echonet/utils/segmentation.py",
    "content": "\"\"\"Functions for training and running segmentation.\"\"\"\n\nimport math\nimport os\nimport time\n\nimport click\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport scipy.signal\nimport skimage.draw\nimport torch\nimport torchvision\nimport tqdm\n\nimport echonet\n\n\n@click.command(\"segmentation\")\n@click.option(\"--data_dir\", type=click.Path(exists=True, file_okay=False), default=None)\n@click.option(\"--output\", type=click.Path(file_okay=False), default=None)\n@click.option(\"--model_name\", type=click.Choice(\n    sorted(name for name in torchvision.models.segmentation.__dict__\n           if name.islower() and not name.startswith(\"__\") and callable(torchvision.models.segmentation.__dict__[name]))),\n    default=\"deeplabv3_resnet50\")\n@click.option(\"--pretrained/--random\", default=False)\n@click.option(\"--weights\", type=click.Path(exists=True, dir_okay=False), default=None)\n@click.option(\"--run_test/--skip_test\", default=False)\n@click.option(\"--save_video/--skip_video\", default=False)\n@click.option(\"--num_epochs\", type=int, default=50)\n@click.option(\"--lr\", type=float, default=1e-5)\n@click.option(\"--weight_decay\", type=float, default=0)\n@click.option(\"--lr_step_period\", type=int, default=None)\n@click.option(\"--num_train_patients\", type=int, default=None)\n@click.option(\"--num_workers\", type=int, default=4)\n@click.option(\"--batch_size\", type=int, default=20)\n@click.option(\"--device\", type=str, default=None)\n@click.option(\"--seed\", type=int, default=0)\ndef run(\n    data_dir=None,\n    output=None,\n\n    model_name=\"deeplabv3_resnet50\",\n    pretrained=False,\n    weights=None,\n\n    run_test=False,\n    save_video=False,\n    num_epochs=50,\n    lr=1e-5,\n    weight_decay=1e-5,\n    lr_step_period=None,\n    num_train_patients=None,\n    num_workers=4,\n    batch_size=20,\n    device=None,\n    seed=0,\n):\n    \"\"\"Trains/tests segmentation model.\n\n    Args:\n        data_dir (str, optional): Directory containing dataset. Defaults to\n            `echonet.config.DATA_DIR`.\n        output (str, optional): Directory to place outputs. Defaults to\n            output/segmentation/<model_name>_<pretrained/random>/.\n        model_name (str, optional): Name of segmentation model. One of ``deeplabv3_resnet50'',\n            ``deeplabv3_resnet101'', ``fcn_resnet50'', or ``fcn_resnet101''\n            (options are torchvision.models.segmentation.<model_name>)\n            Defaults to ``deeplabv3_resnet50''.\n        pretrained (bool, optional): Whether to use pretrained weights for model\n            Defaults to False.\n        weights (str, optional): Path to checkpoint containing weights to\n            initialize model. Defaults to None.\n        run_test (bool, optional): Whether or not to run on test.\n            Defaults to False.\n        save_video (bool, optional): Whether to save videos with segmentations.\n            Defaults to False.\n        num_epochs (int, optional): Number of epochs during training\n            Defaults to 50.\n        lr (float, optional): Learning rate for SGD\n            Defaults to 1e-5.\n        weight_decay (float, optional): Weight decay for SGD\n            Defaults to 0.\n        lr_step_period (int or None, optional): Period of learning rate decay\n            (learning rate is decayed by a multiplicative factor of 0.1)\n            Defaults to math.inf (never decay learning rate).\n        num_train_patients (int or None, optional): Number of training patients\n            for ablations. Defaults to all patients.\n        num_workers (int, optional): Number of subprocesses to use for data\n            loading. If 0, the data will be loaded in the main process.\n            Defaults to 4.\n        device (str or None, optional): Name of device to run on. Options from\n            https://pytorch.org/docs/stable/tensor_attributes.html#torch.torch.device\n            Defaults to ``cuda'' if available, and ``cpu'' otherwise.\n        batch_size (int, optional): Number of samples to load per batch\n            Defaults to 20.\n        seed (int, optional): Seed for random number generator. Defaults to 0.\n    \"\"\"\n\n    # Seed RNGs\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n\n    # Set default output directory\n    if output is None:\n        output = os.path.join(\"output\", \"segmentation\", \"{}_{}\".format(model_name, \"pretrained\" if pretrained else \"random\"))\n    os.makedirs(output, exist_ok=True)\n\n    # Set device for computations\n    if device is None:\n        device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n    # Set up model\n    model = torchvision.models.segmentation.__dict__[model_name](pretrained=pretrained, aux_loss=False)\n\n    model.classifier[-1] = torch.nn.Conv2d(model.classifier[-1].in_channels, 1, kernel_size=model.classifier[-1].kernel_size)  # change number of outputs to 1\n    if device.type == \"cuda\":\n        model = torch.nn.DataParallel(model)\n    model.to(device)\n\n    if weights is not None:\n        checkpoint = torch.load(weights)\n        model.load_state_dict(checkpoint['state_dict'])\n\n    # Set up optimizer\n    optim = torch.optim.SGD(model.parameters(), lr=lr, momentum=0.9, weight_decay=weight_decay)\n    if lr_step_period is None:\n        lr_step_period = math.inf\n    scheduler = torch.optim.lr_scheduler.StepLR(optim, lr_step_period)\n\n    # Compute mean and std\n    mean, std = echonet.utils.get_mean_and_std(echonet.datasets.Echo(root=data_dir, split=\"train\"))\n    tasks = [\"LargeFrame\", \"SmallFrame\", \"LargeTrace\", \"SmallTrace\"]\n    kwargs = {\"target_type\": tasks,\n              \"mean\": mean,\n              \"std\": std\n              }\n\n    # Set up datasets and dataloaders\n    dataset = {}\n    dataset[\"train\"] = echonet.datasets.Echo(root=data_dir, split=\"train\", **kwargs)\n    if num_train_patients is not None and len(dataset[\"train\"]) > num_train_patients:\n        # Subsample patients (used for ablation experiment)\n        indices = np.random.choice(len(dataset[\"train\"]), num_train_patients, replace=False)\n        dataset[\"train\"] = torch.utils.data.Subset(dataset[\"train\"], indices)\n    dataset[\"val\"] = echonet.datasets.Echo(root=data_dir, split=\"val\", **kwargs)\n\n    # Run training and testing loops\n    with open(os.path.join(output, \"log.csv\"), \"a\") as f:\n        epoch_resume = 0\n        bestLoss = float(\"inf\")\n        try:\n            # Attempt to load checkpoint\n            checkpoint = torch.load(os.path.join(output, \"checkpoint.pt\"))\n            model.load_state_dict(checkpoint['state_dict'])\n            optim.load_state_dict(checkpoint['opt_dict'])\n            scheduler.load_state_dict(checkpoint['scheduler_dict'])\n            epoch_resume = checkpoint[\"epoch\"] + 1\n            bestLoss = checkpoint[\"best_loss\"]\n            f.write(\"Resuming from epoch {}\\n\".format(epoch_resume))\n        except FileNotFoundError:\n            f.write(\"Starting run from scratch\\n\")\n\n        for epoch in range(epoch_resume, num_epochs):\n            print(\"Epoch #{}\".format(epoch), flush=True)\n            for phase in ['train', 'val']:\n                start_time = time.time()\n                for i in range(torch.cuda.device_count()):\n                    torch.cuda.reset_peak_memory_stats(i)\n\n                ds = dataset[phase]\n                dataloader = torch.utils.data.DataLoader(\n                    ds, batch_size=batch_size, num_workers=num_workers, shuffle=True, pin_memory=(device.type == \"cuda\"), drop_last=(phase == \"train\"))\n\n                loss, large_inter, large_union, small_inter, small_union = echonet.utils.segmentation.run_epoch(model, dataloader, phase == \"train\", optim, device)\n                overall_dice = 2 * (large_inter.sum() + small_inter.sum()) / (large_union.sum() + large_inter.sum() + small_union.sum() + small_inter.sum())\n                large_dice = 2 * large_inter.sum() / (large_union.sum() + large_inter.sum())\n                small_dice = 2 * small_inter.sum() / (small_union.sum() + small_inter.sum())\n                f.write(\"{},{},{},{},{},{},{},{},{},{},{}\\n\".format(epoch,\n                                                                    phase,\n                                                                    loss,\n                                                                    overall_dice,\n                                                                    large_dice,\n                                                                    small_dice,\n                                                                    time.time() - start_time,\n                                                                    large_inter.size,\n                                                                    sum(torch.cuda.max_memory_allocated() for i in range(torch.cuda.device_count())),\n                                                                    sum(torch.cuda.max_memory_reserved() for i in range(torch.cuda.device_count())),\n                                                                    batch_size))\n                f.flush()\n            scheduler.step()\n\n            # Save checkpoint\n            save = {\n                'epoch': epoch,\n                'state_dict': model.state_dict(),\n                'best_loss': bestLoss,\n                'loss': loss,\n                'opt_dict': optim.state_dict(),\n                'scheduler_dict': scheduler.state_dict(),\n            }\n            torch.save(save, os.path.join(output, \"checkpoint.pt\"))\n            if loss < bestLoss:\n                torch.save(save, os.path.join(output, \"best.pt\"))\n                bestLoss = loss\n\n        # Load best weights\n        if num_epochs != 0:\n            checkpoint = torch.load(os.path.join(output, \"best.pt\"))\n            model.load_state_dict(checkpoint['state_dict'])\n            f.write(\"Best validation loss {} from epoch {}\\n\".format(checkpoint[\"loss\"], checkpoint[\"epoch\"]))\n\n        if run_test:\n            # Run on validation and test\n            for split in [\"val\", \"test\"]:\n                dataset = echonet.datasets.Echo(root=data_dir, split=split, **kwargs)\n                dataloader = torch.utils.data.DataLoader(dataset,\n                                                         batch_size=batch_size, num_workers=num_workers, shuffle=False, pin_memory=(device.type == \"cuda\"))\n                loss, large_inter, large_union, small_inter, small_union = echonet.utils.segmentation.run_epoch(model, dataloader, False, None, device)\n\n                overall_dice = 2 * (large_inter + small_inter) / (large_union + large_inter + small_union + small_inter)\n                large_dice = 2 * large_inter / (large_union + large_inter)\n                small_dice = 2 * small_inter / (small_union + small_inter)\n                with open(os.path.join(output, \"{}_dice.csv\".format(split)), \"w\") as g:\n                    g.write(\"Filename, Overall, Large, Small\\n\")\n                    for (filename, overall, large, small) in zip(dataset.fnames, overall_dice, large_dice, small_dice):\n                        g.write(\"{},{},{},{}\\n\".format(filename, overall, large, small))\n\n                f.write(\"{} dice (overall): {:.4f} ({:.4f} - {:.4f})\\n\".format(split, *echonet.utils.bootstrap(np.concatenate((large_inter, small_inter)), np.concatenate((large_union, small_union)), echonet.utils.dice_similarity_coefficient)))\n                f.write(\"{} dice (large):   {:.4f} ({:.4f} - {:.4f})\\n\".format(split, *echonet.utils.bootstrap(large_inter, large_union, echonet.utils.dice_similarity_coefficient)))\n                f.write(\"{} dice (small):   {:.4f} ({:.4f} - {:.4f})\\n\".format(split, *echonet.utils.bootstrap(small_inter, small_union, echonet.utils.dice_similarity_coefficient)))\n                f.flush()\n\n    # Saving videos with segmentations\n    dataset = echonet.datasets.Echo(root=data_dir, split=\"test\",\n                                    target_type=[\"Filename\", \"LargeIndex\", \"SmallIndex\"],  # Need filename for saving, and human-selected frames to annotate\n                                    mean=mean, std=std,  # Normalization\n                                    length=None, max_length=None, period=1  # Take all frames\n                                    )\n    dataloader = torch.utils.data.DataLoader(dataset, batch_size=10, num_workers=num_workers, shuffle=False, pin_memory=False, collate_fn=_video_collate_fn)\n\n    # Save videos with segmentation\n    if save_video and not all(os.path.isfile(os.path.join(output, \"videos\", f)) for f in dataloader.dataset.fnames):\n        # Only run if missing videos\n\n        model.eval()\n\n        os.makedirs(os.path.join(output, \"videos\"), exist_ok=True)\n        os.makedirs(os.path.join(output, \"size\"), exist_ok=True)\n        echonet.utils.latexify()\n\n        with torch.no_grad():\n            with open(os.path.join(output, \"size.csv\"), \"w\") as g:\n                g.write(\"Filename,Frame,Size,HumanLarge,HumanSmall,ComputerSmall\\n\")\n                for (x, (filenames, large_index, small_index), length) in tqdm.tqdm(dataloader):\n                    # Run segmentation model on blocks of frames one-by-one\n                    # The whole concatenated video may be too long to run together\n                    y = np.concatenate([model(x[i:(i + batch_size), :, :, :].to(device))[\"out\"].detach().cpu().numpy() for i in range(0, x.shape[0], batch_size)])\n\n                    start = 0\n                    x = x.numpy()\n                    for (i, (filename, offset)) in enumerate(zip(filenames, length)):\n                        # Extract one video and segmentation predictions\n                        video = x[start:(start + offset), ...]\n                        logit = y[start:(start + offset), 0, :, :]\n\n                        # Un-normalize video\n                        video *= std.reshape(1, 3, 1, 1)\n                        video += mean.reshape(1, 3, 1, 1)\n\n                        # Get frames, channels, height, and width\n                        f, c, h, w = video.shape  # pylint: disable=W0612\n                        assert c == 3\n\n                        # Put two copies of the video side by side\n                        video = np.concatenate((video, video), 3)\n\n                        # If a pixel is in the segmentation, saturate blue channel\n                        # Leave alone otherwise\n                        video[:, 0, :, w:] = np.maximum(255. * (logit > 0), video[:, 0, :, w:])  # pylint: disable=E1111\n\n                        # Add blank canvas under pair of videos\n                        video = np.concatenate((video, np.zeros_like(video)), 2)\n\n                        # Compute size of segmentation per frame\n                        size = (logit > 0).sum((1, 2))\n\n                        # Identify systole frames with peak detection\n                        trim_min = sorted(size)[round(len(size) ** 0.05)]\n                        trim_max = sorted(size)[round(len(size) ** 0.95)]\n                        trim_range = trim_max - trim_min\n                        systole = set(scipy.signal.find_peaks(-size, distance=20, prominence=(0.50 * trim_range))[0])\n\n                        # Write sizes and frames to file\n                        for (frame, s) in enumerate(size):\n                            g.write(\"{},{},{},{},{},{}\\n\".format(filename, frame, s, 1 if frame == large_index[i] else 0, 1 if frame == small_index[i] else 0, 1 if frame in systole else 0))\n\n                        # Plot sizes\n                        fig = plt.figure(figsize=(size.shape[0] / 50 * 1.5, 3))\n                        plt.scatter(np.arange(size.shape[0]) / 50, size, s=1)\n                        ylim = plt.ylim()\n                        for s in systole:\n                            plt.plot(np.array([s, s]) / 50, ylim, linewidth=1)\n                        plt.ylim(ylim)\n                        plt.title(os.path.splitext(filename)[0])\n                        plt.xlabel(\"Seconds\")\n                        plt.ylabel(\"Size (pixels)\")\n                        plt.tight_layout()\n                        plt.savefig(os.path.join(output, \"size\", os.path.splitext(filename)[0] + \".pdf\"))\n                        plt.close(fig)\n\n                        # Normalize size to [0, 1]\n                        size -= size.min()\n                        size = size / size.max()\n                        size = 1 - size\n\n                        # Iterate the frames in this video\n                        for (f, s) in enumerate(size):\n\n                            # On all frames, mark a pixel for the size of the frame\n                            video[:, :, int(round(115 + 100 * s)), int(round(f / len(size) * 200 + 10))] = 255.\n\n                            if f in systole:\n                                # If frame is computer-selected systole, mark with a line\n                                video[:, :, 115:224, int(round(f / len(size) * 200 + 10))] = 255.\n\n                            def dash(start, stop, on=10, off=10):\n                                buf = []\n                                x = start\n                                while x < stop:\n                                    buf.extend(range(x, x + on))\n                                    x += on\n                                    x += off\n                                buf = np.array(buf)\n                                buf = buf[buf < stop]\n                                return buf\n                            d = dash(115, 224)\n\n                            if f == large_index[i]:\n                                # If frame is human-selected diastole, mark with green dashed line on all frames\n                                video[:, :, d, int(round(f / len(size) * 200 + 10))] = np.array([0, 225, 0]).reshape((1, 3, 1))\n                            if f == small_index[i]:\n                                # If frame is human-selected systole, mark with red dashed line on all frames\n                                video[:, :, d, int(round(f / len(size) * 200 + 10))] = np.array([0, 0, 225]).reshape((1, 3, 1))\n\n                            # Get pixels for a circle centered on the pixel\n                            r, c = skimage.draw.disk((int(round(115 + 100 * s)), int(round(f / len(size) * 200 + 10))), 4.1)\n\n                            # On the frame that's being shown, put a circle over the pixel\n                            video[f, :, r, c] = 255.\n\n                        # Rearrange dimensions and save\n                        video = video.transpose(1, 0, 2, 3)\n                        video = video.astype(np.uint8)\n                        echonet.utils.savevideo(os.path.join(output, \"videos\", filename), video, 50)\n\n                        # Move to next video\n                        start += offset\n\n\ndef run_epoch(model, dataloader, train, optim, device):\n    \"\"\"Run one epoch of training/evaluation for segmentation.\n\n    Args:\n        model (torch.nn.Module): Model to train/evaulate.\n        dataloder (torch.utils.data.DataLoader): Dataloader for dataset.\n        train (bool): Whether or not to train model.\n        optim (torch.optim.Optimizer): Optimizer\n        device (torch.device): Device to run on\n    \"\"\"\n\n    total = 0.\n    n = 0\n\n    pos = 0\n    neg = 0\n    pos_pix = 0\n    neg_pix = 0\n\n    model.train(train)\n\n    large_inter = 0\n    large_union = 0\n    small_inter = 0\n    small_union = 0\n    large_inter_list = []\n    large_union_list = []\n    small_inter_list = []\n    small_union_list = []\n\n    with torch.set_grad_enabled(train):\n        with tqdm.tqdm(total=len(dataloader)) as pbar:\n            for (_, (large_frame, small_frame, large_trace, small_trace)) in dataloader:\n                # Count number of pixels in/out of human segmentation\n                pos += (large_trace == 1).sum().item()\n                pos += (small_trace == 1).sum().item()\n                neg += (large_trace == 0).sum().item()\n                neg += (small_trace == 0).sum().item()\n\n                # Count number of pixels in/out of computer segmentation\n                pos_pix += (large_trace == 1).sum(0).to(\"cpu\").detach().numpy()\n                pos_pix += (small_trace == 1).sum(0).to(\"cpu\").detach().numpy()\n                neg_pix += (large_trace == 0).sum(0).to(\"cpu\").detach().numpy()\n                neg_pix += (small_trace == 0).sum(0).to(\"cpu\").detach().numpy()\n\n                # Run prediction for diastolic frames and compute loss\n                large_frame = large_frame.to(device)\n                large_trace = large_trace.to(device)\n                y_large = model(large_frame)[\"out\"]\n                loss_large = torch.nn.functional.binary_cross_entropy_with_logits(y_large[:, 0, :, :], large_trace, reduction=\"sum\")\n                # Compute pixel intersection and union between human and computer segmentations\n                large_inter += np.logical_and(y_large[:, 0, :, :].detach().cpu().numpy() > 0., large_trace[:, :, :].detach().cpu().numpy() > 0.).sum()\n                large_union += np.logical_or(y_large[:, 0, :, :].detach().cpu().numpy() > 0., large_trace[:, :, :].detach().cpu().numpy() > 0.).sum()\n                large_inter_list.extend(np.logical_and(y_large[:, 0, :, :].detach().cpu().numpy() > 0., large_trace[:, :, :].detach().cpu().numpy() > 0.).sum((1, 2)))\n                large_union_list.extend(np.logical_or(y_large[:, 0, :, :].detach().cpu().numpy() > 0., large_trace[:, :, :].detach().cpu().numpy() > 0.).sum((1, 2)))\n\n                # Run prediction for systolic frames and compute loss\n                small_frame = small_frame.to(device)\n                small_trace = small_trace.to(device)\n                y_small = model(small_frame)[\"out\"]\n                loss_small = torch.nn.functional.binary_cross_entropy_with_logits(y_small[:, 0, :, :], small_trace, reduction=\"sum\")\n                # Compute pixel intersection and union between human and computer segmentations\n                small_inter += np.logical_and(y_small[:, 0, :, :].detach().cpu().numpy() > 0., small_trace[:, :, :].detach().cpu().numpy() > 0.).sum()\n                small_union += np.logical_or(y_small[:, 0, :, :].detach().cpu().numpy() > 0., small_trace[:, :, :].detach().cpu().numpy() > 0.).sum()\n                small_inter_list.extend(np.logical_and(y_small[:, 0, :, :].detach().cpu().numpy() > 0., small_trace[:, :, :].detach().cpu().numpy() > 0.).sum((1, 2)))\n                small_union_list.extend(np.logical_or(y_small[:, 0, :, :].detach().cpu().numpy() > 0., small_trace[:, :, :].detach().cpu().numpy() > 0.).sum((1, 2)))\n\n                # Take gradient step if training\n                loss = (loss_large + loss_small) / 2\n                if train:\n                    optim.zero_grad()\n                    loss.backward()\n                    optim.step()\n\n                # Accumulate losses and compute baselines\n                total += loss.item()\n                n += large_trace.size(0)\n                p = pos / (pos + neg)\n                p_pix = (pos_pix + 1) / (pos_pix + neg_pix + 2)\n\n                # Show info on process bar\n                pbar.set_postfix_str(\"{:.4f} ({:.4f}) / {:.4f} {:.4f}, {:.4f}, {:.4f}\".format(total / n / 112 / 112, loss.item() / large_trace.size(0) / 112 / 112, -p * math.log(p) - (1 - p) * math.log(1 - p), (-p_pix * np.log(p_pix) - (1 - p_pix) * np.log(1 - p_pix)).mean(), 2 * large_inter / (large_union + large_inter), 2 * small_inter / (small_union + small_inter)))\n                pbar.update()\n\n    large_inter_list = np.array(large_inter_list)\n    large_union_list = np.array(large_union_list)\n    small_inter_list = np.array(small_inter_list)\n    small_union_list = np.array(small_union_list)\n\n    return (total / n / 112 / 112,\n            large_inter_list,\n            large_union_list,\n            small_inter_list,\n            small_union_list,\n            )\n\n\ndef _video_collate_fn(x):\n    \"\"\"Collate function for Pytorch dataloader to merge multiple videos.\n\n    This function should be used in a dataloader for a dataset that returns\n    a video as the first element, along with some (non-zero) tuple of\n    targets. Then, the input x is a list of tuples:\n      - x[i][0] is the i-th video in the batch\n      - x[i][1] are the targets for the i-th video\n\n    This function returns a 3-tuple:\n      - The first element is the videos concatenated along the frames\n        dimension. This is done so that videos of different lengths can be\n        processed together (tensors cannot be \"jagged\", so we cannot have\n        a dimension for video, and another for frames).\n      - The second element is contains the targets with no modification.\n      - The third element is a list of the lengths of the videos in frames.\n    \"\"\"\n    video, target = zip(*x)  # Extract the videos and targets\n\n    # ``video'' is a tuple of length ``batch_size''\n    #   Each element has shape (channels=3, frames, height, width)\n    #   height and width are expected to be the same across videos, but\n    #   frames can be different.\n\n    # ``target'' is also a tuple of length ``batch_size''\n    # Each element is a tuple of the targets for the item.\n\n    i = list(map(lambda t: t.shape[1], video))  # Extract lengths of videos in frames\n\n    # This contatenates the videos along the the frames dimension (basically\n    # playing the videos one after another). The frames dimension is then\n    # moved to be first.\n    # Resulting shape is (total frames, channels=3, height, width)\n    video = torch.as_tensor(np.swapaxes(np.concatenate(video, 1), 0, 1))\n\n    # Swap dimensions (approximately a transpose)\n    # Before: target[i][j] is the j-th target of element i\n    # After:  target[i][j] is the i-th target of element j\n    target = zip(*target)\n\n    return video, target, i\n"
  },
  {
    "path": "echonet/utils/video.py",
    "content": "\"\"\"Functions for training and running EF prediction.\"\"\"\n\nimport math\nimport os\nimport time\n\nimport click\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport sklearn.metrics\nimport torch\nimport torchvision\nimport tqdm\n\nimport echonet\n\n\n@click.command(\"video\")\n@click.option(\"--data_dir\", type=click.Path(exists=True, file_okay=False), default=None)\n@click.option(\"--output\", type=click.Path(file_okay=False), default=None)\n@click.option(\"--task\", type=str, default=\"EF\")\n@click.option(\"--model_name\", type=click.Choice(\n    sorted(name for name in torchvision.models.video.__dict__\n           if name.islower() and not name.startswith(\"__\") and callable(torchvision.models.video.__dict__[name]))),\n    default=\"r2plus1d_18\")\n@click.option(\"--pretrained/--random\", default=True)\n@click.option(\"--weights\", type=click.Path(exists=True, dir_okay=False), default=None)\n@click.option(\"--run_test/--skip_test\", default=False)\n@click.option(\"--num_epochs\", type=int, default=45)\n@click.option(\"--lr\", type=float, default=1e-4)\n@click.option(\"--weight_decay\", type=float, default=1e-4)\n@click.option(\"--lr_step_period\", type=int, default=15)\n@click.option(\"--frames\", type=int, default=32)\n@click.option(\"--period\", type=int, default=2)\n@click.option(\"--num_train_patients\", type=int, default=None)\n@click.option(\"--num_workers\", type=int, default=4)\n@click.option(\"--batch_size\", type=int, default=20)\n@click.option(\"--device\", type=str, default=None)\n@click.option(\"--seed\", type=int, default=0)\ndef run(\n    data_dir=None,\n    output=None,\n    task=\"EF\",\n\n    model_name=\"r2plus1d_18\",\n    pretrained=True,\n    weights=None,\n\n    run_test=False,\n    num_epochs=45,\n    lr=1e-4,\n    weight_decay=1e-4,\n    lr_step_period=15,\n    frames=32,\n    period=2,\n    num_train_patients=None,\n    num_workers=4,\n    batch_size=20,\n    device=None,\n    seed=0,\n):\n    \"\"\"Trains/tests EF prediction model.\n\n    \\b\n    Args:\n        data_dir (str, optional): Directory containing dataset. Defaults to\n            `echonet.config.DATA_DIR`.\n        output (str, optional): Directory to place outputs. Defaults to\n            output/video/<model_name>_<pretrained/random>/.\n        task (str, optional): Name of task to predict. Options are the headers\n            of FileList.csv. Defaults to ``EF''.\n        model_name (str, optional): Name of model. One of ``mc3_18'',\n            ``r2plus1d_18'', or ``r3d_18''\n            (options are torchvision.models.video.<model_name>)\n            Defaults to ``r2plus1d_18''.\n        pretrained (bool, optional): Whether to use pretrained weights for model\n            Defaults to True.\n        weights (str, optional): Path to checkpoint containing weights to\n            initialize model. Defaults to None.\n        run_test (bool, optional): Whether or not to run on test.\n            Defaults to False.\n        num_epochs (int, optional): Number of epochs during training.\n            Defaults to 45.\n        lr (float, optional): Learning rate for SGD\n            Defaults to 1e-4.\n        weight_decay (float, optional): Weight decay for SGD\n            Defaults to 1e-4.\n        lr_step_period (int or None, optional): Period of learning rate decay\n            (learning rate is decayed by a multiplicative factor of 0.1)\n            Defaults to 15.\n        frames (int, optional): Number of frames to use in clip\n            Defaults to 32.\n        period (int, optional): Sampling period for frames\n            Defaults to 2.\n        n_train_patients (int or None, optional): Number of training patients\n            for ablations. Defaults to all patients.\n        num_workers (int, optional): Number of subprocesses to use for data\n            loading. If 0, the data will be loaded in the main process.\n            Defaults to 4.\n        device (str or None, optional): Name of device to run on. Options from\n            https://pytorch.org/docs/stable/tensor_attributes.html#torch.torch.device\n            Defaults to ``cuda'' if available, and ``cpu'' otherwise.\n        batch_size (int, optional): Number of samples to load per batch\n            Defaults to 20.\n        seed (int, optional): Seed for random number generator. Defaults to 0.\n    \"\"\"\n\n    # Seed RNGs\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n\n    # Set default output directory\n    if output is None:\n        output = os.path.join(\"output\", \"video\", \"{}_{}_{}_{}\".format(model_name, frames, period, \"pretrained\" if pretrained else \"random\"))\n    os.makedirs(output, exist_ok=True)\n\n    # Set device for computations\n    if device is None:\n        device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n    # Set up model\n    model = torchvision.models.video.__dict__[model_name](pretrained=pretrained)\n\n    model.fc = torch.nn.Linear(model.fc.in_features, 1)\n    model.fc.bias.data[0] = 55.6\n    if device.type == \"cuda\":\n        model = torch.nn.DataParallel(model)\n    model.to(device)\n\n    if weights is not None:\n        checkpoint = torch.load(weights)\n        model.load_state_dict(checkpoint['state_dict'])\n\n    # Set up optimizer\n    optim = torch.optim.SGD(model.parameters(), lr=lr, momentum=0.9, weight_decay=weight_decay)\n    if lr_step_period is None:\n        lr_step_period = math.inf\n    scheduler = torch.optim.lr_scheduler.StepLR(optim, lr_step_period)\n\n    # Compute mean and std\n    mean, std = echonet.utils.get_mean_and_std(echonet.datasets.Echo(root=data_dir, split=\"train\"))\n    kwargs = {\"target_type\": task,\n              \"mean\": mean,\n              \"std\": std,\n              \"length\": frames,\n              \"period\": period,\n              }\n\n    # Set up datasets and dataloaders\n    dataset = {}\n    dataset[\"train\"] = echonet.datasets.Echo(root=data_dir, split=\"train\", **kwargs, pad=12)\n    if num_train_patients is not None and len(dataset[\"train\"]) > num_train_patients:\n        # Subsample patients (used for ablation experiment)\n        indices = np.random.choice(len(dataset[\"train\"]), num_train_patients, replace=False)\n        dataset[\"train\"] = torch.utils.data.Subset(dataset[\"train\"], indices)\n    dataset[\"val\"] = echonet.datasets.Echo(root=data_dir, split=\"val\", **kwargs)\n\n    # Run training and testing loops\n    with open(os.path.join(output, \"log.csv\"), \"a\") as f:\n        epoch_resume = 0\n        bestLoss = float(\"inf\")\n        try:\n            # Attempt to load checkpoint\n            checkpoint = torch.load(os.path.join(output, \"checkpoint.pt\"))\n            model.load_state_dict(checkpoint['state_dict'])\n            optim.load_state_dict(checkpoint['opt_dict'])\n            scheduler.load_state_dict(checkpoint['scheduler_dict'])\n            epoch_resume = checkpoint[\"epoch\"] + 1\n            bestLoss = checkpoint[\"best_loss\"]\n            f.write(\"Resuming from epoch {}\\n\".format(epoch_resume))\n        except FileNotFoundError:\n            f.write(\"Starting run from scratch\\n\")\n\n        for epoch in range(epoch_resume, num_epochs):\n            print(\"Epoch #{}\".format(epoch), flush=True)\n            for phase in ['train', 'val']:\n                start_time = time.time()\n                for i in range(torch.cuda.device_count()):\n                    torch.cuda.reset_peak_memory_stats(i)\n\n                ds = dataset[phase]\n                dataloader = torch.utils.data.DataLoader(\n                    ds, batch_size=batch_size, num_workers=num_workers, shuffle=True, pin_memory=(device.type == \"cuda\"), drop_last=(phase == \"train\"))\n\n                loss, yhat, y = echonet.utils.video.run_epoch(model, dataloader, phase == \"train\", optim, device)\n                f.write(\"{},{},{},{},{},{},{},{},{}\\n\".format(epoch,\n                                                              phase,\n                                                              loss,\n                                                              sklearn.metrics.r2_score(y, yhat),\n                                                              time.time() - start_time,\n                                                              y.size,\n                                                              sum(torch.cuda.max_memory_allocated() for i in range(torch.cuda.device_count())),\n                                                              sum(torch.cuda.max_memory_reserved() for i in range(torch.cuda.device_count())),\n                                                              batch_size))\n                f.flush()\n            scheduler.step()\n\n            # Save checkpoint\n            save = {\n                'epoch': epoch,\n                'state_dict': model.state_dict(),\n                'period': period,\n                'frames': frames,\n                'best_loss': bestLoss,\n                'loss': loss,\n                'r2': sklearn.metrics.r2_score(y, yhat),\n                'opt_dict': optim.state_dict(),\n                'scheduler_dict': scheduler.state_dict(),\n            }\n            torch.save(save, os.path.join(output, \"checkpoint.pt\"))\n            if loss < bestLoss:\n                torch.save(save, os.path.join(output, \"best.pt\"))\n                bestLoss = loss\n\n        # Load best weights\n        if num_epochs != 0:\n            checkpoint = torch.load(os.path.join(output, \"best.pt\"))\n            model.load_state_dict(checkpoint['state_dict'])\n            f.write(\"Best validation loss {} from epoch {}\\n\".format(checkpoint[\"loss\"], checkpoint[\"epoch\"]))\n            f.flush()\n\n        if run_test:\n            for split in [\"val\", \"test\"]:\n                # Performance without test-time augmentation\n                dataloader = torch.utils.data.DataLoader(\n                    echonet.datasets.Echo(root=data_dir, split=split, **kwargs),\n                    batch_size=batch_size, num_workers=num_workers, shuffle=True, pin_memory=(device.type == \"cuda\"))\n                loss, yhat, y = echonet.utils.video.run_epoch(model, dataloader, False, None, device)\n                f.write(\"{} (one clip) R2:   {:.3f} ({:.3f} - {:.3f})\\n\".format(split, *echonet.utils.bootstrap(y, yhat, sklearn.metrics.r2_score)))\n                f.write(\"{} (one clip) MAE:  {:.2f} ({:.2f} - {:.2f})\\n\".format(split, *echonet.utils.bootstrap(y, yhat, sklearn.metrics.mean_absolute_error)))\n                f.write(\"{} (one clip) RMSE: {:.2f} ({:.2f} - {:.2f})\\n\".format(split, *tuple(map(math.sqrt, echonet.utils.bootstrap(y, yhat, sklearn.metrics.mean_squared_error)))))\n                f.flush()\n\n                # Performance with test-time augmentation\n                ds = echonet.datasets.Echo(root=data_dir, split=split, **kwargs, clips=\"all\")\n                dataloader = torch.utils.data.DataLoader(\n                    ds, batch_size=1, num_workers=num_workers, shuffle=False, pin_memory=(device.type == \"cuda\"))\n                loss, yhat, y = echonet.utils.video.run_epoch(model, dataloader, False, None, device, save_all=True, block_size=batch_size)\n                f.write(\"{} (all clips) R2:   {:.3f} ({:.3f} - {:.3f})\\n\".format(split, *echonet.utils.bootstrap(y, np.array(list(map(lambda x: x.mean(), yhat))), sklearn.metrics.r2_score)))\n                f.write(\"{} (all clips) MAE:  {:.2f} ({:.2f} - {:.2f})\\n\".format(split, *echonet.utils.bootstrap(y, np.array(list(map(lambda x: x.mean(), yhat))), sklearn.metrics.mean_absolute_error)))\n                f.write(\"{} (all clips) RMSE: {:.2f} ({:.2f} - {:.2f})\\n\".format(split, *tuple(map(math.sqrt, echonet.utils.bootstrap(y, np.array(list(map(lambda x: x.mean(), yhat))), sklearn.metrics.mean_squared_error)))))\n                f.flush()\n\n                # Write full performance to file\n                with open(os.path.join(output, \"{}_predictions.csv\".format(split)), \"w\") as g:\n                    for (filename, pred) in zip(ds.fnames, yhat):\n                        for (i, p) in enumerate(pred):\n                            g.write(\"{},{},{:.4f}\\n\".format(filename, i, p))\n                echonet.utils.latexify()\n                yhat = np.array(list(map(lambda x: x.mean(), yhat)))\n\n                # Plot actual and predicted EF\n                fig = plt.figure(figsize=(3, 3))\n                lower = min(y.min(), yhat.min())\n                upper = max(y.max(), yhat.max())\n                plt.scatter(y, yhat, color=\"k\", s=1, edgecolor=None, zorder=2)\n                plt.plot([0, 100], [0, 100], linewidth=1, zorder=3)\n                plt.axis([lower - 3, upper + 3, lower - 3, upper + 3])\n                plt.gca().set_aspect(\"equal\", \"box\")\n                plt.xlabel(\"Actual EF (%)\")\n                plt.ylabel(\"Predicted EF (%)\")\n                plt.xticks([10, 20, 30, 40, 50, 60, 70, 80])\n                plt.yticks([10, 20, 30, 40, 50, 60, 70, 80])\n                plt.grid(color=\"gainsboro\", linestyle=\"--\", linewidth=1, zorder=1)\n                plt.tight_layout()\n                plt.savefig(os.path.join(output, \"{}_scatter.pdf\".format(split)))\n                plt.close(fig)\n\n                # Plot AUROC\n                fig = plt.figure(figsize=(3, 3))\n                plt.plot([0, 1], [0, 1], linewidth=1, color=\"k\", linestyle=\"--\")\n                for thresh in [35, 40, 45, 50]:\n                    fpr, tpr, _ = sklearn.metrics.roc_curve(y > thresh, yhat)\n                    print(thresh, sklearn.metrics.roc_auc_score(y > thresh, yhat))\n                    plt.plot(fpr, tpr)\n\n                plt.axis([-0.01, 1.01, -0.01, 1.01])\n                plt.xlabel(\"False Positive Rate\")\n                plt.ylabel(\"True Positive Rate\")\n                plt.tight_layout()\n                plt.savefig(os.path.join(output, \"{}_roc.pdf\".format(split)))\n                plt.close(fig)\n\n\ndef run_epoch(model, dataloader, train, optim, device, save_all=False, block_size=None):\n    \"\"\"Run one epoch of training/evaluation for segmentation.\n\n    Args:\n        model (torch.nn.Module): Model to train/evaulate.\n        dataloder (torch.utils.data.DataLoader): Dataloader for dataset.\n        train (bool): Whether or not to train model.\n        optim (torch.optim.Optimizer): Optimizer\n        device (torch.device): Device to run on\n        save_all (bool, optional): If True, return predictions for all\n            test-time augmentations separately. If False, return only\n            the mean prediction.\n            Defaults to False.\n        block_size (int or None, optional): Maximum number of augmentations\n            to run on at the same time. Use to limit the amount of memory\n            used. If None, always run on all augmentations simultaneously.\n            Default is None.\n    \"\"\"\n\n    model.train(train)\n\n    total = 0  # total training loss\n    n = 0      # number of videos processed\n    s1 = 0     # sum of ground truth EF\n    s2 = 0     # Sum of ground truth EF squared\n\n    yhat = []\n    y = []\n\n    with torch.set_grad_enabled(train):\n        with tqdm.tqdm(total=len(dataloader)) as pbar:\n            for (X, outcome) in dataloader:\n\n                y.append(outcome.numpy())\n                X = X.to(device)\n                outcome = outcome.to(device)\n\n                average = (len(X.shape) == 6)\n                if average:\n                    batch, n_clips, c, f, h, w = X.shape\n                    X = X.view(-1, c, f, h, w)\n\n                s1 += outcome.sum()\n                s2 += (outcome ** 2).sum()\n\n                if block_size is None:\n                    outputs = model(X)\n                else:\n                    outputs = torch.cat([model(X[j:(j + block_size), ...]) for j in range(0, X.shape[0], block_size)])\n\n                if save_all:\n                    yhat.append(outputs.view(-1).to(\"cpu\").detach().numpy())\n\n                if average:\n                    outputs = outputs.view(batch, n_clips, -1).mean(1)\n\n                if not save_all:\n                    yhat.append(outputs.view(-1).to(\"cpu\").detach().numpy())\n\n                loss = torch.nn.functional.mse_loss(outputs.view(-1), outcome)\n\n                if train:\n                    optim.zero_grad()\n                    loss.backward()\n                    optim.step()\n\n                total += loss.item() * X.size(0)\n                n += X.size(0)\n\n                pbar.set_postfix_str(\"{:.2f} ({:.2f}) / {:.2f}\".format(total / n, loss.item(), s2 / n - (s1 / n) ** 2))\n                pbar.update()\n\n    if not save_all:\n        yhat = np.concatenate(yhat)\n    y = np.concatenate(y)\n\n    return total / n, yhat, y\n"
  },
  {
    "path": "example.cfg",
    "content": "DATA_DIR = a4c-video-dir/\n"
  },
  {
    "path": "requirements.txt",
    "content": "certifi==2020.12.5\ncycler==0.10.0\ndecorator==4.4.2\nechonet==1.0.0\nimageio==2.9.0\njoblib==1.0.1\nkiwisolver==1.3.1\nmatplotlib==3.3.4\nnetworkx==2.5\nnumpy==1.20.1\nopencv-python==4.5.1.48\npandas==1.2.3\nPillow==8.1.2\npyparsing==2.4.7\npython-dateutil==2.8.1\npytz==2021.1\nPyWavelets==1.1.1\nscikit-image==0.18.1\nscikit-learn==0.24.1\nscipy==1.6.1\nsix==1.15.0\nsklearn==0.0\nthreadpoolctl==2.1.0\ntifffile==2021.3.17\ntorch==1.8.0\ntorchvision==0.9.0\ntqdm==4.59.0\ntyping-extensions==3.7.4.3\n"
  },
  {
    "path": "scripts/ConvertDICOMToAVI.ipynb",
    "content": "{\n \"cells\": [\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 12,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"# David Ouyang 10/2/2019\\n\",\n    \"\\n\",\n    \"# Notebook which iterates through a folder, including subfolders, \\n\",\n    \"# and convert DICOM files to AVI files of a defined size (natively 112 x 112)\\n\",\n    \"\\n\",\n    \"import re\\n\",\n    \"import os, os.path\\n\",\n    \"from os.path import splitext\\n\",\n    \"import pydicom as dicom\\n\",\n    \"import numpy as np\\n\",\n    \"from pydicom.uid import UID, generate_uid\\n\",\n    \"import shutil\\n\",\n    \"from multiprocessing import dummy as multiprocessing\\n\",\n    \"import time\\n\",\n    \"import subprocess\\n\",\n    \"import datetime\\n\",\n    \"from datetime import date\\n\",\n    \"import sys\\n\",\n    \"import cv2\\n\",\n    \"#from scipy.misc import imread\\n\",\n    \"import matplotlib.pyplot as plt\\n\",\n    \"import sys\\n\",\n    \"from shutil import copy\\n\",\n    \"import math\\n\",\n    \"\\n\",\n    \"destinationFolder = \\\"Output Folder Name\\\"\\n\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 10,\n   \"metadata\": {},\n   \"outputs\": [\n    {\n     \"name\": \"stdout\",\n     \"output_type\": \"stream\",\n     \"text\": [\n      \"Requirement already satisfied: pillow in c:\\\\programdata\\\\anaconda3\\\\lib\\\\site-packages (6.2.0)\\n\",\n      \"Requirement already satisfied: scipy in c:\\\\programdata\\\\anaconda3\\\\lib\\\\site-packages (1.3.1)\\n\"\n     ]\n    }\n   ],\n   \"source\": [\n    \"# Dependencies you might need to run code\\n\",\n    \"# Commonly missing\\n\",\n    \"\\n\",\n    \"#!pip install pydicom\\n\",\n    \"#!pip install opencv-python\\n\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 2,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"def mask(output):\\n\",\n    \"    dimension = output.shape[0]\\n\",\n    \"    \\n\",\n    \"    # Mask pixels outside of scanning sector\\n\",\n    \"    m1, m2 = np.meshgrid(np.arange(dimension), np.arange(dimension))\\n\",\n    \"    \\n\",\n    \"\\n\",\n    \"    mask = ((m1+m2)>int(dimension/2) + int(dimension/10)) \\n\",\n    \"    mask *=  ((m1-m2)<int(dimension/2) + int(dimension/10))\\n\",\n    \"    mask = np.reshape(mask, (dimension, dimension)).astype(np.int8)\\n\",\n    \"    maskedImage = cv2.bitwise_and(output, output, mask = mask)\\n\",\n    \"    \\n\",\n    \"    #print(maskedImage.shape)\\n\",\n    \"    \\n\",\n    \"    return maskedImage\\n\",\n    \"\\n\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 3,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"def makeVideo(fileToProcess, destinationFolder):\\n\",\n    \"    try:\\n\",\n    \"        fileName = fileToProcess.split('\\\\\\\\')[-1] #\\\\\\\\ if windows, / if on mac or sherlock\\n\",\n    \"                                                 #hex(abs(hash(fileToProcess.split('/')[-1]))).upper()\\n\",\n    \"\\n\",\n    \"        if not os.path.isdir(os.path.join(destinationFolder,fileName)):\\n\",\n    \"\\n\",\n    \"            dataset = dicom.dcmread(fileToProcess, force=True)\\n\",\n    \"            testarray = dataset.pixel_array\\n\",\n    \"\\n\",\n    \"            frame0 = testarray[0]\\n\",\n    \"            mean = np.mean(frame0, axis=1)\\n\",\n    \"            mean = np.mean(mean, axis=1)\\n\",\n    \"            yCrop = np.where(mean<1)[0][0]\\n\",\n    \"            testarray = testarray[:, yCrop:, :, :]\\n\",\n    \"\\n\",\n    \"            bias = int(np.abs(testarray.shape[2] - testarray.shape[1])/2)\\n\",\n    \"            if bias>0:\\n\",\n    \"                if testarray.shape[1] < testarray.shape[2]:\\n\",\n    \"                    testarray = testarray[:, :, bias:-bias, :]\\n\",\n    \"                else:\\n\",\n    \"                    testarray = testarray[:, bias:-bias, :, :]\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"            print(testarray.shape)\\n\",\n    \"            frames,height,width,channels = testarray.shape\\n\",\n    \"\\n\",\n    \"            fps = 30\\n\",\n    \"\\n\",\n    \"            try:\\n\",\n    \"                fps = dataset[(0x18, 0x40)].value\\n\",\n    \"            except:\\n\",\n    \"                print(\\\"couldn't find frame rate, default to 30\\\")\\n\",\n    \"\\n\",\n    \"            fourcc = cv2.VideoWriter_fourcc('M','J','P','G')\\n\",\n    \"            video_filename = os.path.join(destinationFolder, fileName + '.avi')\\n\",\n    \"            out = cv2.VideoWriter(video_filename, fourcc, fps, cropSize)\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"            for i in range(frames):\\n\",\n    \"\\n\",\n    \"                outputA = testarray[i,:,:,0]\\n\",\n    \"                smallOutput = outputA[int(height/10):(height - int(height/10)), int(height/10):(height - int(height/10))]\\n\",\n    \"\\n\",\n    \"                # Resize image\\n\",\n    \"                output = cv2.resize(smallOutput, cropSize, interpolation = cv2.INTER_CUBIC)\\n\",\n    \"\\n\",\n    \"                finaloutput = mask(output)\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"                finaloutput = cv2.merge([finaloutput,finaloutput,finaloutput])\\n\",\n    \"                out.write(finaloutput)\\n\",\n    \"\\n\",\n    \"            out.release()\\n\",\n    \"\\n\",\n    \"        else:\\n\",\n    \"            print(fileName,\\\"hasAlreadyBeenProcessed\\\")\\n\",\n    \"    except:\\n\",\n    \"        print(\\\"something filed, not sure what, have to debug\\\", fileName)\\n\",\n    \"    return 0\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": null,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"AllA4cNames = \\\"Input Folder Name\\\"\\n\",\n    \"\\n\",\n    \"count = 0\\n\",\n    \"    \\n\",\n    \"cropSize = (112,112)\\n\",\n    \"subfolders = os.listdir(AllA4cNames)\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"for folder in subfolders:\\n\",\n    \"    print(folder)\\n\",\n    \"\\n\",\n    \"    for content in os.listdir(os.path.join(AllA4cNames, folder)):\\n\",\n    \"        for subcontent in os.listdir(os.path.join(AllA4cNames, folder, content)):\\n\",\n    \"            count += 1\\n\",\n    \"            \\n\",\n    \"\\n\",\n    \"            VideoPath = os.path.join(AllA4cNames, folder, content, subcontent)\\n\",\n    \"\\n\",\n    \"            print(count, folder, content, subcontent)\\n\",\n    \"\\n\",\n    \"            if not os.path.exists(os.path.join(destinationFolder,subcontent + \\\".avi\\\")):\\n\",\n    \"                makeVideo(VideoPath, destinationFolder)\\n\",\n    \"            else:\\n\",\n    \"                print(\\\"Already did this file\\\", VideoPath)\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"print(len(AllA4cFilenames))\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": null,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": []\n  }\n ],\n \"metadata\": {\n  \"kernelspec\": {\n   \"display_name\": \"Python 3\",\n   \"language\": \"python\",\n   \"name\": \"python3\"\n  },\n  \"language_info\": {\n   \"codemirror_mode\": {\n    \"name\": \"ipython\",\n    \"version\": 3\n   },\n   \"file_extension\": \".py\",\n   \"mimetype\": \"text/x-python\",\n   \"name\": \"python\",\n   \"nbconvert_exporter\": \"python\",\n   \"pygments_lexer\": \"ipython3\",\n   \"version\": \"3.7.4\"\n  }\n },\n \"nbformat\": 4,\n \"nbformat_minor\": 2\n}\n"
  },
  {
    "path": "scripts/InitializationNotebook.ipynb",
    "content": "{\n \"cells\": [\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 4,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"# David Ouyang 12/5/2019\\n\",\n    \"\\n\",\n    \"# Notebook which:\\n\",\n    \"# 1. Downloads weights\\n\",\n    \"# 2. Initializes model and imports weights\\n\",\n    \"# 3. Performs test time evaluation of videos (already preprocessed with ConvertDICOMToAVI.ipynb)\\n\",\n    \"\\n\",\n    \"import re\\n\",\n    \"import os, os.path\\n\",\n    \"from os.path import splitext\\n\",\n    \"import pydicom as dicom\\n\",\n    \"import numpy as np\\n\",\n    \"from pydicom.uid import UID, generate_uid\\n\",\n    \"import shutil\\n\",\n    \"from multiprocessing import dummy as multiprocessing\\n\",\n    \"import time\\n\",\n    \"import subprocess\\n\",\n    \"import datetime\\n\",\n    \"from datetime import date\\n\",\n    \"import sys\\n\",\n    \"import cv2\\n\",\n    \"import matplotlib.pyplot as plt\\n\",\n    \"import sys\\n\",\n    \"from shutil import copy\\n\",\n    \"import math\\n\",\n    \"import torch\\n\",\n    \"import torchvision\\n\",\n    \"\\n\",\n    \"sys.path.append(\\\"..\\\")\\n\",\n    \"import echonet\\n\",\n    \"\\n\",\n    \"import wget \\n\",\n    \"\\n\",\n    \"#destinationFolder = \\\"/Users/davidouyang/Dropbox/Echo Research/CodeBase/Output\\\"\\n\",\n    \"destinationFolder = \\\"C:\\\\\\\\Users\\\\\\\\Windows\\\\\\\\Dropbox\\\\\\\\Echo Research\\\\\\\\CodeBase\\\\\\\\Output\\\"\\n\",\n    \"#videosFolder = \\\"/Users/davidouyang/Dropbox/Echo Research/CodeBase/a4c-video-dir\\\"\\n\",\n    \"videosFolder = \\\"C:\\\\\\\\Users\\\\\\\\Windows\\\\\\\\Dropbox\\\\\\\\Echo Research\\\\\\\\CodeBase\\\\\\\\a4c-video-dir\\\"\\n\",\n    \"#DestinationForWeights = \\\"/Users/davidouyang/Dropbox/Echo Research/CodeBase/EchoNetDynamic-Weights\\\"\\n\",\n    \"DestinationForWeights = \\\"C:\\\\\\\\Users\\\\\\\\Windows\\\\\\\\Dropbox\\\\\\\\Echo Research\\\\\\\\CodeBase\\\\\\\\EchoNetDynamic-Weights\\\"\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 5,\n   \"metadata\": {},\n   \"outputs\": [\n    {\n     \"name\": \"stdout\",\n     \"output_type\": \"stream\",\n     \"text\": [\n      \"The weights are at C:\\\\Users\\\\Windows\\\\Dropbox\\\\Echo Research\\\\CodeBase\\\\EchoNetDynamic-Weights\\n\",\n      \"Segmentation Weights already present\\n\",\n      \"EF Weights already present\\n\"\n     ]\n    }\n   ],\n   \"source\": [\n    \"# Download model weights\\n\",\n    \"\\n\",\n    \"if os.path.exists(DestinationForWeights):\\n\",\n    \"    print(\\\"The weights are at\\\", DestinationForWeights)\\n\",\n    \"else:\\n\",\n    \"    print(\\\"Creating folder at \\\", DestinationForWeights, \\\" to store weights\\\")\\n\",\n    \"    os.mkdir(DestinationForWeights)\\n\",\n    \"    \\n\",\n    \"segmentationWeightsURL = 'https://github.com/douyang/EchoNetDynamic/releases/download/v1.0.0/deeplabv3_resnet50_random.pt'\\n\",\n    \"ejectionFractionWeightsURL = 'https://github.com/douyang/EchoNetDynamic/releases/download/v1.0.0/r2plus1d_18_32_2_pretrained.pt'\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"if not os.path.exists(os.path.join(DestinationForWeights, os.path.basename(segmentationWeightsURL))):\\n\",\n    \"    print(\\\"Downloading Segmentation Weights, \\\", segmentationWeightsURL,\\\" to \\\",os.path.join(DestinationForWeights,os.path.basename(segmentationWeightsURL)))\\n\",\n    \"    filename = wget.download(segmentationWeightsURL, out = DestinationForWeights)\\n\",\n    \"else:\\n\",\n    \"    print(\\\"Segmentation Weights already present\\\")\\n\",\n    \"    \\n\",\n    \"if not os.path.exists(os.path.join(DestinationForWeights, os.path.basename(ejectionFractionWeightsURL))):\\n\",\n    \"    print(\\\"Downloading EF Weights, \\\", ejectionFractionWeightsURL,\\\" to \\\",os.path.join(DestinationForWeights,os.path.basename(ejectionFractionWeightsURL)))\\n\",\n    \"    filename = wget.download(ejectionFractionWeightsURL, out = DestinationForWeights)\\n\",\n    \"else:\\n\",\n    \"    print(\\\"EF Weights already present\\\")\\n\",\n    \"        \\n\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 6,\n   \"metadata\": {},\n   \"outputs\": [\n    {\n     \"name\": \"stdout\",\n     \"output_type\": \"stream\",\n     \"text\": [\n      \"loading weights from  C:\\\\Users\\\\Windows\\\\Dropbox\\\\Echo Research\\\\CodeBase\\\\EchoNetDynamic-Weights\\\\r2plus1d_18_32_2_pretrained\\n\",\n      \"cuda is available, original weights\\n\",\n      \"external_test ['0X1A05DFFFCAFB253B.avi', '0X1A0A263B22CCD966.avi', '0X1A2A76BDB5B98BED.avi', '0X1A2C60147AF9FDAE.avi', '0X1A2E9496910EFF5B.avi', '0X1A3D565B371DC573.avi', '0X1A3E7BF1DFB132FB.avi', '0X1A5FAE3F9D37794E.avi', '0X1A6ACFE7B286DAFC.avi', '0X1A8D85542DBE8204.avi', '23_Apical_4_chamber_view.dcm.avi', '62_Apical_4_chamber_view.dcm.avi', '64_Apical_4_chamber_view.dcm.avi']\\n\"\n     ]\n    },\n    {\n     \"name\": \"stderr\",\n     \"output_type\": \"stream\",\n     \"text\": [\n      \"100%|██████████████████████████████████████████████████████████████████████████████████| 10/10 [00:10<00:00,  1.00s/it]\\n\",\n      \"100%|████████████████████████████████████████████████████████| 13/13 [00:29<00:00,  2.26s/it, 3122.29 (3440.26) / 0.00]\\n\"\n     ]\n    }\n   ],\n   \"source\": [\n    \"# Initialize and Run EF model\\n\",\n    \"\\n\",\n    \"frames = 32\\n\",\n    \"period = 1 #2\\n\",\n    \"batch_size = 20\\n\",\n    \"model = torchvision.models.video.r2plus1d_18(pretrained=False)\\n\",\n    \"model.fc = torch.nn.Linear(model.fc.in_features, 1)\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"print(\\\"loading weights from \\\", os.path.join(DestinationForWeights, \\\"r2plus1d_18_32_2_pretrained\\\"))\\n\",\n    \"\\n\",\n    \"if torch.cuda.is_available():\\n\",\n    \"    print(\\\"cuda is available, original weights\\\")\\n\",\n    \"    device = torch.device(\\\"cuda\\\")\\n\",\n    \"    model = torch.nn.DataParallel(model)\\n\",\n    \"    model.to(device)\\n\",\n    \"    checkpoint = torch.load(os.path.join(DestinationForWeights, os.path.basename(ejectionFractionWeightsURL)))\\n\",\n    \"    model.load_state_dict(checkpoint['state_dict'])\\n\",\n    \"else:\\n\",\n    \"    print(\\\"cuda is not available, cpu weights\\\")\\n\",\n    \"    device = torch.device(\\\"cpu\\\")\\n\",\n    \"    checkpoint = torch.load(os.path.join(DestinationForWeights, os.path.basename(ejectionFractionWeightsURL)), map_location = \\\"cpu\\\")\\n\",\n    \"    state_dict_cpu = {k[7:]: v for (k, v) in checkpoint['state_dict'].items()}\\n\",\n    \"    model.load_state_dict(state_dict_cpu)\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"# try some random weights: final_r2+1d_model_regression_EF_sgd_skip1_32frames.pth.tar\\n\",\n    \"# scp ouyangd@arthur2:~/Echo-Tracing-Analysis/final_r2+1d_model_regression_EF_sgd_skip1_32frames.pth.tar \\\"C:\\\\Users\\\\Windows\\\\Dropbox\\\\Echo Research\\\\CodeBase\\\\EchoNetDynamic-Weights\\\"\\n\",\n    \"#Weights = \\\"final_r2+1d_model_regression_EF_sgd_skip1_32frames.pth.tar\\\"\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"output = os.path.join(destinationFolder, \\\"cedars_ef_output.csv\\\")\\n\",\n    \"\\n\",\n    \"ds = echonet.datasets.Echo(split = \\\"external_test\\\", external_test_location = videosFolder, crops=\\\"all\\\")\\n\",\n    \"print(ds.split, ds.fnames)\\n\",\n    \"\\n\",\n    \"mean, std = echonet.utils.get_mean_and_std(ds)\\n\",\n    \"\\n\",\n    \"kwargs = {\\\"target_type\\\": \\\"EF\\\",\\n\",\n    \"          \\\"mean\\\": mean,\\n\",\n    \"          \\\"std\\\": std,\\n\",\n    \"          \\\"length\\\": frames,\\n\",\n    \"          \\\"period\\\": period,\\n\",\n    \"          }\\n\",\n    \"\\n\",\n    \"ds = echonet.datasets.Echo(split = \\\"external_test\\\", external_test_location = videosFolder, **kwargs, crops=\\\"all\\\")\\n\",\n    \"\\n\",\n    \"test_dataloader = torch.utils.data.DataLoader(ds, batch_size = 1, num_workers = 5, shuffle = True, pin_memory=(device.type == \\\"cuda\\\"))\\n\",\n    \"loss, yhat, y = echonet.utils.video.run_epoch(model, test_dataloader, \\\"test\\\", None, device, save_all=True, blocks=25)\\n\",\n    \"\\n\",\n    \"with open(output, \\\"w\\\") as g:\\n\",\n    \"    for (filename, pred) in zip(ds.fnames, yhat):\\n\",\n    \"        for (i,p) in enumerate(pred):\\n\",\n    \"            g.write(\\\"{},{},{:.4f}\\\\n\\\".format(filename, i, p))\\n\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": null,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"# Initialize and Run Segmentation model\\n\",\n    \"\\n\",\n    \"torch.cuda.empty_cache()\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"videosFolder = \\\"C:\\\\\\\\Users\\\\\\\\Windows\\\\\\\\Dropbox\\\\\\\\Echo Research\\\\\\\\CodeBase\\\\\\\\View Classification\\\\\\\\AppearsA4c\\\\\\\\Resized2\\\"\\n\",\n    \"\\n\",\n    \"def collate_fn(x):\\n\",\n    \"    x, f = zip(*x)\\n\",\n    \"    i = list(map(lambda t: t.shape[1], x))\\n\",\n    \"    x = torch.as_tensor(np.swapaxes(np.concatenate(x, 1), 0, 1))\\n\",\n    \"    return x, f, i\\n\",\n    \"\\n\",\n    \"dataloader = torch.utils.data.DataLoader(echonet.datasets.Echo(split=\\\"external_test\\\", external_test_location = videosFolder, target_type=[\\\"Filename\\\"], length=None, period=1, mean=mean, std=std),\\n\",\n    \"                                         batch_size=10, num_workers=0, shuffle=False, pin_memory=(device.type == \\\"cuda\\\"), collate_fn=collate_fn)\\n\",\n    \"if not all([os.path.isfile(os.path.join(destinationFolder, \\\"labels\\\", os.path.splitext(f)[0] + \\\".npy\\\")) for f in dataloader.dataset.fnames]):\\n\",\n    \"    # Save segmentations for all frames\\n\",\n    \"    # Only run if missing files\\n\",\n    \"\\n\",\n    \"    pathlib.Path(os.path.join(destinationFolder, \\\"labels\\\")).mkdir(parents=True, exist_ok=True)\\n\",\n    \"    block = 1024\\n\",\n    \"    model.eval()\\n\",\n    \"\\n\",\n    \"    with torch.no_grad():\\n\",\n    \"        for (x, f, i) in tqdm.tqdm(dataloader):\\n\",\n    \"            x = x.to(device)\\n\",\n    \"            y = np.concatenate([model(x[i:(i + block), :, :, :])[\\\"out\\\"].detach().cpu().numpy() for i in range(0, x.shape[0], block)]).astype(np.float16)\\n\",\n    \"            start = 0\\n\",\n    \"            for (filename, offset) in zip(f, i):\\n\",\n    \"                np.save(os.path.join(destinationFolder, \\\"labels\\\", os.path.splitext(filename)[0]), y[start:(start + offset), 0, :, :])\\n\",\n    \"                start += offset\\n\",\n    \"                \\n\",\n    \"dataloader = torch.utils.data.DataLoader(echonet.datasets.Echo(split=\\\"external_test\\\", external_test_location = videosFolder, target_type=[\\\"Filename\\\"], length=None, period=1, segmentation=os.path.join(destinationFolder, \\\"labels\\\")),\\n\",\n    \"                                         batch_size=1, num_workers=8, shuffle=False, pin_memory=False)\\n\",\n    \"if not all(os.path.isfile(os.path.join(destinationFolder, \\\"videos\\\", f)) for f in dataloader.dataset.fnames):\\n\",\n    \"    pathlib.Path(os.path.join(destinationFolder, \\\"videos\\\")).mkdir(parents=True, exist_ok=True)\\n\",\n    \"    pathlib.Path(os.path.join(destinationFolder, \\\"size\\\")).mkdir(parents=True, exist_ok=True)\\n\",\n    \"    echonet.utils.latexify()\\n\",\n    \"    with open(os.path.join(destinationFolder, \\\"size.csv\\\"), \\\"w\\\") as g:\\n\",\n    \"        g.write(\\\"Filename,Frame,Size,ComputerSmall\\\\n\\\")\\n\",\n    \"        for (x, filename) in tqdm.tqdm(dataloader):\\n\",\n    \"            x = x.numpy()\\n\",\n    \"            for i in range(len(filename)):\\n\",\n    \"                img = x[i, :, :, :, :].copy()\\n\",\n    \"                logit = img[2, :, :, :].copy()\\n\",\n    \"                img[1, :, :, :] = img[0, :, :, :]\\n\",\n    \"                img[2, :, :, :] = img[0, :, :, :]\\n\",\n    \"                img = np.concatenate((img, img), 3)\\n\",\n    \"                img[0, :, :, 112:] = np.maximum(255. * (logit > 0), img[0, :, :, 112:])\\n\",\n    \"\\n\",\n    \"                img = np.concatenate((img, np.zeros_like(img)), 2)\\n\",\n    \"                size = (logit > 0).sum(2).sum(1)\\n\",\n    \"                try:\\n\",\n    \"                    trim_min = sorted(size)[round(len(size) ** 0.05)]\\n\",\n    \"                except:\\n\",\n    \"                    import code; code.interact(local=dict(globals(), **locals()))\\n\",\n    \"                trim_max = sorted(size)[round(len(size) ** 0.95)]\\n\",\n    \"                trim_range = trim_max - trim_min\\n\",\n    \"                peaks = set(scipy.signal.find_peaks(-size, distance=20, prominence=(0.50 * trim_range))[0])\\n\",\n    \"                for (x, y) in enumerate(size):\\n\",\n    \"                    g.write(\\\"{},{},{},{}\\\\n\\\".format(filename[0], x, y, 1 if x in peaks else 0))\\n\",\n    \"                fig = plt.figure(figsize=(size.shape[0] / 50 * 1.5, 3))\\n\",\n    \"                plt.scatter(np.arange(size.shape[0]) / 50, size, s=1)\\n\",\n    \"                ylim = plt.ylim()\\n\",\n    \"                for p in peaks:\\n\",\n    \"                    plt.plot(np.array([p, p]) / 50, ylim, linewidth=1)\\n\",\n    \"                plt.ylim(ylim)\\n\",\n    \"                plt.title(os.path.splitext(filename[i])[0])\\n\",\n    \"                plt.xlabel(\\\"Seconds\\\")\\n\",\n    \"                plt.ylabel(\\\"Size (pixels)\\\")\\n\",\n    \"                plt.tight_layout()\\n\",\n    \"                plt.savefig(os.path.join(destinationFolder, \\\"size\\\", os.path.splitext(filename[i])[0] + \\\".pdf\\\"))\\n\",\n    \"                plt.close(fig)\\n\",\n    \"                size -= size.min()\\n\",\n    \"                size = size / size.max()\\n\",\n    \"                size = 1 - size\\n\",\n    \"                for (x, y) in enumerate(size):\\n\",\n    \"                    img[:, :, int(round(115 + 100 * y)), int(round(x / len(size) * 200 + 10))] = 255.\\n\",\n    \"                    interval = np.array([-3, -2, -1, 0, 1, 2, 3])\\n\",\n    \"                    for a in interval:\\n\",\n    \"                        for b in interval:\\n\",\n    \"                            img[:, x, a + int(round(115 + 100 * y)), b + int(round(x / len(size) * 200 + 10))] = 255.\\n\",\n    \"                    if x in peaks:\\n\",\n    \"                        img[:, :, 200:225, b + int(round(x / len(size) * 200 + 10))] = 255.\\n\",\n    \"                echonet.utils.savevideo(os.path.join(destinationFolder, \\\"videos\\\", filename[i]), img.astype(np.uint8), 50)                \"\n   ]\n  }\n ],\n \"metadata\": {\n  \"kernelspec\": {\n   \"display_name\": \"Python 3\",\n   \"language\": \"python\",\n   \"name\": \"python3\"\n  },\n  \"language_info\": {\n   \"codemirror_mode\": {\n    \"name\": \"ipython\",\n    \"version\": 3\n   },\n   \"file_extension\": \".py\",\n   \"mimetype\": \"text/x-python\",\n   \"name\": \"python\",\n   \"nbconvert_exporter\": \"python\",\n   \"pygments_lexer\": \"ipython3\",\n   \"version\": \"3.7.4\"\n  }\n },\n \"nbformat\": 4,\n \"nbformat_minor\": 2\n}\n"
  },
  {
    "path": "scripts/beat_by_beat_analysis.R",
    "content": "library(ggplot2)\nlibrary(stringr)\nlibrary(plyr)\nlibrary(dplyr)\nlibrary(lubridate)\nlibrary(reshape2)\nlibrary(scales)\nlibrary(ggthemes)\nlibrary(Metrics)\n\ndata <- read.csv(\"r2plus1d_18_32_2_pretrained_test_predictions.csv\", header = FALSE)\nstr(data)\n\n\ndataNoAugmentation <- data[data$V2 == 0,]\nstr(dataNoAugmentation)\n\n\ndataGlobalAugmentation <- data %>% group_by(V1) %>% summarize(meanPrediction = mean(V3), sdPred = sd(V3))\nstr(dataGlobalAugmentation)\n\n\nsizeData <- read.csv(\"size.csv\")\nsizeData <- sizeData[sizeData$ComputerSmall == 1,]\nstr(sizeData)\n\nsizeRelevantFrames <- sizeData[c(1,2)]\nsizeRelevantFrames$Frame <- sizeRelevantFrames$Frame - 32\nsizeRelevantFrames[sizeRelevantFrames$Frame < 0,]$Frame <- 0\n\n\nbeatByBeat <- merge(sizeRelevantFrames, data, by.x = c(\"Filename\", \"Frame\"), by.y = c(\"V1\", \"V2\"))\nbeatByBeat <- beatByBeat %>% group_by(Filename) %>% summarize(meanPrediction = mean(V3), sdPred = sd(V3))\nstr(beatByBeat)\n\n### For use, need to specify file directory\nfileLocation <- \"/Users/davidouyang/Local Medical Data/\"\nActualNumbers <- read.csv(paste0(fileLocation, \"FileList.csv\", sep = \"\"))\nActualNumbers <- ActualNumbers[c(1,2)]\nstr(ActualNumbers)\n\n\n\ndataNoAugmentation <- merge(dataNoAugmentation, ActualNumbers, by.x = \"V1\", by.y = \"Filename\", all.x = TRUE)\ndataNoAugmentation$AbsErr <- abs(dataNoAugmentation$V3 - dataNoAugmentation$EF)\nstr(dataNoAugmentation)\n\nsummary(abs(dataNoAugmentation$V3 - dataNoAugmentation$EF))\n# Mean of 4.216\n\nrmse(dataNoAugmentation$V3,dataNoAugmentation$EF) \n## 5.56\n\nmodelNoAugmentation <- lm(dataNoAugmentation$EF ~ dataNoAugmentation$V3)\nsummary(modelNoAugmentation)$r.squared\n# 0.79475\n\n\nbeatByBeat <- merge(beatByBeat, ActualNumbers, by.x = \"Filename\", by.y = \"Filename\", all.x = TRUE)\nsummary(abs(beatByBeat$meanPrediction - beatByBeat$EF))\n# Mean of 4.051697\n\nrmse(beatByBeat$meanPrediction, beatByBeat$EF) \n# 5.325237\n\nmodelBeatByBeat <- lm(beatByBeat$EF ~ beatByBeat$meanPrediction)\nsummary(modelBeatByBeat)$r.squared\n# 0.8093174\n\n\nbeatByBeatAnalysis <- merge(sizeRelevantFrames, data, by.x = c(\"Filename\", \"Frame\"), by.y = c(\"V1\", \"V2\"))\nstr(beatByBeatAnalysis)\n\n\nMAEdata <- data.frame(counter = 1:500)\nMAEdata$sample <- -9999\nMAEdata$error <- -9999\n\nstr(MAEdata)\n\nfor (i in 1:500){\n\n\nsamplingBeat <-  sample_n(beatByBeatAnalysis %>% group_by(Filename), 1 + floor((i-1)/100), replace = TRUE) %>% group_by(Filename) %>% dplyr::summarize(meanPred = mean(V3))\nsamplingBeat <- merge(samplingBeat, ActualNumbers, by.x = \"Filename\", by.y = \"Filename\", all.x = TRUE)\nsamplingBeat$error <- abs(samplingBeat$meanPred - samplingBeat$EF)\n\nMAEdata$sample[i] <-  1 + floor((i-1)/100)\nMAEdata$error[i] <- mean(samplingBeat$error )\n\n\n}\n\nstr(MAEdata)\n\nbeatBoxPlot <- ggplot(data = MAEdata) + geom_boxplot(aes(x = sample, y = error, group = sample), outlier.shape = NA\n) + theme_classic() + theme(legend.position = \"none\", axis.text.y = element_text( size=7)) + xlab(\"Number of Sampled Beats\") + ylab(\"Mean Absolute Error\") + scale_fill_brewer(palette = \"Set1\", direction = -1) \n\nbeatBoxPlot\n\n"
  },
  {
    "path": "scripts/plot_complexity.py",
    "content": "#!/usr/bin/env python3\n\n\"\"\"Code to generate plots for Extended Data Fig. 4.\"\"\"\n\nimport os\n\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nimport echonet\n\n\ndef main(root=os.path.join(\"timing\", \"video\"),\n         fig_root=os.path.join(\"figure\", \"complexity\"),\n         FRAMES=(1, 8, 16, 32, 64, 96),\n         pretrained=True):\n    \"\"\"Generate plots for Extended Data Fig. 4.\"\"\"\n\n    echonet.utils.latexify()\n\n    os.makedirs(fig_root, exist_ok=True)\n    fig = plt.figure(figsize=(6.50, 2.50))\n    gs = matplotlib.gridspec.GridSpec(1, 3, width_ratios=[2.5, 2.5, 1.50])\n    ax = (plt.subplot(gs[0]), plt.subplot(gs[1]), plt.subplot(gs[2]))\n\n    # Create legend\n    for (model, color) in zip([\"EchoNet-Dynamic (EF)\", \"R3D\", \"MC3\"], matplotlib.colors.TABLEAU_COLORS):\n        ax[2].plot([float(\"nan\")], [float(\"nan\")], \"-\", color=color, label=model)\n    ax[2].set_title(\"\")\n    ax[2].axis(\"off\")\n    ax[2].legend(loc=\"center\")\n\n    for (model, color) in zip([\"r2plus1d_18\", \"r3d_18\", \"mc3_18\"], matplotlib.colors.TABLEAU_COLORS):\n        for split in [\"val\"]:  # [\"val\", \"train\"]:\n            print(model, split)\n            data = [load(root, model, frames, 1, pretrained, split) for frames in FRAMES]\n            time = np.array(list(map(lambda x: x[0], data)))\n            n = np.array(list(map(lambda x: x[1], data)))\n            mem_allocated = np.array(list(map(lambda x: x[2], data)))\n            # mem_cached = np.array(list(map(lambda x: x[3], data)))\n            batch_size = np.array(list(map(lambda x: x[4], data)))\n\n            # Plot Time (panel a)\n            ax[0].plot(FRAMES, time / n, \"-\" if pretrained else \"--\", marker=\".\", color=color, linewidth=(1 if split == \"train\" else None))\n            print(\"Time:\\n\" + \"\\n\".join(map(lambda x: \"{:8d}: {:f}\".format(*x), zip(FRAMES, time / n))))\n\n            # Plot Memory (panel b)\n            ax[1].plot(FRAMES, mem_allocated / batch_size / 1e9, \"-\" if pretrained else \"--\", marker=\".\", color=color, linewidth=(1 if split == \"train\" else None))\n            print(\"Memory:\\n\" + \"\\n\".join(map(lambda x: \"{:8d}: {:f}\".format(*x), zip(FRAMES, mem_allocated / batch_size / 1e9))))\n            print()\n\n    # Labels for panel a\n    ax[0].set_xticks(FRAMES)\n    ax[0].text(-0.05, 1.10, \"(a)\", transform=ax[0].transAxes)\n    ax[0].set_xlabel(\"Clip length (frames)\")\n    ax[0].set_ylabel(\"Time Per Clip (seconds)\")\n\n    # Labels for panel b\n    ax[1].set_xticks(FRAMES)\n    ax[1].text(-0.05, 1.10, \"(b)\", transform=ax[1].transAxes)\n    ax[1].set_xlabel(\"Clip length (frames)\")\n    ax[1].set_ylabel(\"Memory Per Clip (GB)\")\n\n    # Save figure\n    plt.tight_layout()\n    plt.savefig(os.path.join(fig_root, \"complexity.pdf\"))\n    plt.savefig(os.path.join(fig_root, \"complexity.eps\"))\n    plt.close(fig)\n\n\ndef load(root, model, frames, period, pretrained, split):\n    \"\"\"Loads runtime and memory usage for specified hyperparameter choice.\"\"\"\n    with open(os.path.join(root, \"{}_{}_{}_{}\".format(model, frames, period, \"pretrained\" if pretrained else \"random\"), \"log.csv\"), \"r\") as f:\n        for line in f:\n            line = line.split(\",\")\n            if len(line) < 4:\n                # Skip lines that are not csv (these lines log information)\n                continue\n            if line[1] == split:\n                *_, time, n, mem_allocated, mem_cached, batch_size = line\n                time = float(time)\n                n = int(n)\n                mem_allocated = int(mem_allocated)\n                mem_cached = int(mem_cached)\n                batch_size = int(batch_size)\n                return time, n, mem_allocated, mem_cached, batch_size\n    raise ValueError(\"File missing information.\")\n\n\nif __name__ == \"__main__\":\n    main()\n"
  },
  {
    "path": "scripts/plot_hyperparameter_sweep.py",
    "content": "#!/usr/bin/env python3\n\n\"\"\"Code to generate plots for Extended Data Fig. 1.\"\"\"\n\nimport os\n\nimport matplotlib\nimport matplotlib.pyplot as plt\n\nimport echonet\n\n\ndef main(root=os.path.join(\"output\", \"video\"),\n         fig_root=os.path.join(\"figure\", \"hyperparameter\"),\n         FRAMES=(1, 8, 16, 32, 64, 96, None),\n         PERIOD=(1, 2, 4, 6, 8)\n         ):\n    \"\"\"Generate plots for Extended Data Fig. 1.\"\"\"\n\n    echonet.utils.latexify()\n    os.makedirs(fig_root, exist_ok=True)\n\n    # Parameters for plotting length sweep\n    MAX = FRAMES[-2]\n    START = 1    # Starting point for normal range\n    TERM0 = 104  # Ending point for normal range\n    BREAK = 112  # Location for break\n    TERM1 = 120  # Starting point for \"all\" section\n    ALL = 128    # Location of \"all\" point\n    END = 135    # Ending point for \"all\" section\n    RATIO = (BREAK - START) / (END - BREAK)\n\n    # Set up figure\n    fig = plt.figure(figsize=(3 + 2.5 + 1.5, 2.75))\n    outer = matplotlib.gridspec.GridSpec(1, 3, width_ratios=[3, 2.5, 1.50])\n    ax = plt.subplot(outer[2])   # Legend\n    ax2 = plt.subplot(outer[1])  # Period plot\n    gs = matplotlib.gridspec.GridSpecFromSubplotSpec(\n        1, 2, subplot_spec=outer[0], width_ratios=[RATIO, 1], wspace=0.020)  # Length plot\n\n    # Plot legend\n    for (model, color) in zip([\"EchoNet-Dynamic (EF)\", \"R3D\", \"MC3\"],\n                              matplotlib.colors.TABLEAU_COLORS):\n        ax.plot([float(\"nan\")], [float(\"nan\")], \"-\", color=color, label=model)\n    ax.plot([float(\"nan\")], [float(\"nan\")], \"-\", color=\"k\", label=\"Pretrained\")\n    ax.plot([float(\"nan\")], [float(\"nan\")], \"--\", color=\"k\", label=\"Random\")\n    ax.set_title(\"\")\n    ax.axis(\"off\")\n    ax.legend(loc=\"center\")\n\n    # Plot length sweep (panel a)\n    ax0 = plt.subplot(gs[0])\n    ax1 = plt.subplot(gs[1], sharey=ax0)\n    print(\"FRAMES\")\n    for (model, color) in zip([\"r2plus1d_18\", \"r3d_18\", \"mc3_18\"],\n                              matplotlib.colors.TABLEAU_COLORS):\n        for pretrained in [True, False]:\n            loss = [load(root, model, frames, 1, pretrained) for frames in FRAMES]\n            print(model, pretrained)\n            print(\"    \".join(list(map(lambda x: \"{:.1f}\".format(x) if x is not None else None, loss))))\n\n            l0 = loss[-2]\n            l1 = loss[-1]\n            ax0.plot(FRAMES[:-1] + (TERM0,),\n                     loss[:-1] + [l0 + (l1 - l0) * (TERM0 - MAX) / (ALL - MAX)],\n                     \"-\" if pretrained else \"--\", color=color)\n            ax1.plot([TERM1, ALL],\n                     [l0 + (l1 - l0) * (TERM1 - MAX) / (ALL - MAX)] + [loss[-1]],\n                     \"-\" if pretrained else \"--\", color=color)\n            ax0.scatter(list(map(lambda x: x if x is not None else ALL, FRAMES)), loss, color=color, s=4)\n            ax1.scatter(list(map(lambda x: x if x is not None else ALL, FRAMES)), loss, color=color, s=4)\n\n    ax0.set_xticks(list(map(lambda x: x if x is not None else ALL, FRAMES)))\n    ax1.set_xticks(list(map(lambda x: x if x is not None else ALL, FRAMES)))\n    ax0.set_xticklabels(list(map(lambda x: x if x is not None else \"All\", FRAMES)))\n    ax1.set_xticklabels(list(map(lambda x: x if x is not None else \"All\", FRAMES)))\n\n    # https://stackoverflow.com/questions/5656798/python-matplotlib-is-there-a-way-to-make-a-discontinuous-axis/43684155\n    # zoom-in / limit the view to different portions of the data\n    ax0.set_xlim(START, BREAK)  # most of the data\n    ax1.set_xlim(BREAK, END)\n\n    # hide the spines between ax and ax2\n    ax0.spines['right'].set_visible(False)\n    ax1.spines['left'].set_visible(False)\n\n    ax1.get_yaxis().set_visible(False)\n\n    d = 0.015  # how big to make the diagonal lines in axes coordinates\n    # arguments to pass plot, just so we don't keep repeating them\n    kwargs = dict(transform=ax0.transAxes, color='k', clip_on=False, linewidth=1)\n    x0, x1, y0, y1 = ax0.axis()\n    scale = (y1 - y0) / (x1 - x0) / 2\n    ax0.plot((1 - scale * d, 1 + scale * d), (-d, +d), **kwargs)  # top-left diagonal\n    ax0.plot((1 - scale * d, 1 + scale * d), (1 - d, 1 + d), **kwargs)  # bottom-left diagonal\n\n    kwargs.update(transform=ax1.transAxes)  # switch to the bottom 1xes\n    x0, x1, y0, y1 = ax1.axis()\n    scale = (y1 - y0) / (x1 - x0) / 2\n    ax1.plot((-scale * d, scale * d), (-d, +d), **kwargs)  # top-right diagonal\n    ax1.plot((-scale * d, scale * d), (1 - d, 1 + d), **kwargs)  # bottom-right diagonal\n\n    # ax0.xaxis.label.set_transform(matplotlib.transforms.blended_transform_factory(\n    #        matplotlib.transforms.IdentityTransform(), fig.transFigure # specify x, y transform\n    #        )) # changed from default blend (IdentityTransform(), a[0].transAxes)\n    ax0.xaxis.label.set_position((0.6, 0.0))\n    ax0.text(-0.05, 1.10, \"(a)\", transform=ax0.transAxes)\n    ax0.set_xlabel(\"Clip length (frames)\")\n    ax0.set_ylabel(\"Validation Loss\")\n\n    # Plot period sweep (panel b)\n    print(\"PERIOD\")\n    for (model, color) in zip([\"r2plus1d_18\", \"r3d_18\", \"mc3_18\"], matplotlib.colors.TABLEAU_COLORS):\n        for pretrained in [True, False]:\n            loss = [load(root, model, 64 // period, period, pretrained) for period in PERIOD]\n            print(model, pretrained)\n            print(\"    \".join(list(map(lambda x: \"{:.1f}\".format(x) if x is not None else None, loss))))\n\n            ax2.plot(PERIOD, loss, \"-\" if pretrained else \"--\", marker=\".\", color=color)\n    ax2.set_xticks(PERIOD)\n    ax2.text(-0.05, 1.10, \"(b)\", transform=ax2.transAxes)\n    ax2.set_xlabel(\"Sampling Period (frames)\")\n    ax2.set_ylabel(\"Validation Loss\")\n\n    # Save figure\n    plt.tight_layout()\n    plt.savefig(os.path.join(fig_root, \"hyperparameter.pdf\"))\n    plt.savefig(os.path.join(fig_root, \"hyperparameter.eps\"))\n    plt.savefig(os.path.join(fig_root, \"hyperparameter.png\"))\n    plt.close(fig)\n\n\ndef load(root, model, frames, period, pretrained):\n    \"\"\"Loads best validation loss for specified hyperparameter choice.\"\"\"\n    pretrained = (\"pretrained\" if pretrained else \"random\")\n    f = os.path.join(\n        root,\n        \"{}_{}_{}_{}\".format(model, frames, period, pretrained),\n        \"log.csv\")\n    with open(f, \"r\") as f:\n        for line in f:\n            if \"Best validation loss \" in line:\n                return float(line.split()[3])\n\n    raise ValueError(\"File missing information.\")\n\n\nif __name__ == \"__main__\":\n    main()\n"
  },
  {
    "path": "scripts/plot_loss.py",
    "content": "#!/usr/bin/env python3\n\n\"\"\"Code to generate plots for Extended Data Fig. 3.\"\"\"\n\nimport argparse\nimport os\nimport matplotlib\nimport matplotlib.pyplot as plt\n\nimport echonet\n\n\ndef main():\n    \"\"\"Generate plots for Extended Data Fig. 3.\"\"\"\n\n    # Select paths and hyperparameter to plot\n    parser = argparse.ArgumentParser()\n    parser.add_argument(\"dir\", nargs=\"?\", default=\"output\")\n    parser.add_argument(\"fig\", nargs=\"?\", default=os.path.join(\"figure\", \"loss\"))\n    parser.add_argument(\"--frames\", type=int, default=32)\n    parser.add_argument(\"--period\", type=int, default=2)\n    args = parser.parse_args()\n\n    # Set up figure\n    echonet.utils.latexify()\n    os.makedirs(args.fig, exist_ok=True)\n    fig = plt.figure(figsize=(7, 5))\n    gs = matplotlib.gridspec.GridSpec(ncols=3, nrows=2, figure=fig, width_ratios=[2.75, 2.75, 1.50])\n\n    # Plot EF loss curve\n    ax0 = fig.add_subplot(gs[0, 0])\n    ax1 = fig.add_subplot(gs[0, 1], sharey=ax0)\n    for pretrained in [True]:\n        for (model, color) in zip([\"r2plus1d_18\", \"r3d_18\", \"mc3_18\"], matplotlib.colors.TABLEAU_COLORS):\n            loss = load(os.path.join(args.dir, \"video\", \"{}_{}_{}_{}\".format(model, args.frames, args.period, \"pretrained\" if pretrained else \"random\"), \"log.csv\"))\n            ax0.plot(range(1, 1 + len(loss[\"train\"])), loss[\"train\"], \"-\" if pretrained else \"--\", color=color)\n            ax1.plot(range(1, 1 + len(loss[\"val\"])), loss[\"val\"], \"-\" if pretrained else \"--\", color=color)\n\n    plt.axis([0, max(len(loss[\"train\"]), len(loss[\"val\"])), 0, max(max(loss[\"train\"]), max(loss[\"val\"]))])\n    ax0.text(-0.25, 1.00, \"(a)\", transform=ax0.transAxes)\n    ax1.text(-0.25, 1.00, \"(b)\", transform=ax1.transAxes)\n    ax0.set_xlabel(\"Epochs\")\n    ax1.set_xlabel(\"Epochs\")\n    ax0.set_xticks([0, 15, 30, 45])\n    ax1.set_xticks([0, 15, 30, 45])\n    ax0.set_ylabel(\"Training MSE Loss\")\n    ax1.set_ylabel(\"Validation MSE Loss\")\n\n    # Plot segmentation loss curve\n    ax0 = fig.add_subplot(gs[1, 0])\n    ax1 = fig.add_subplot(gs[1, 1], sharey=ax0)\n    pretrained = False\n    for (model, color) in zip([\"deeplabv3_resnet50\"], list(matplotlib.colors.TABLEAU_COLORS)[3:]):\n        loss = load(os.path.join(args.dir, \"segmentation\", \"{}_{}\".format(model, \"pretrained\" if pretrained else \"random\"), \"log.csv\"))\n        ax0.plot(range(1, 1 + len(loss[\"train\"])), loss[\"train\"], \"--\", color=color)\n        ax1.plot(range(1, 1 + len(loss[\"val\"])), loss[\"val\"], \"--\", color=color)\n\n    ax0.text(-0.25, 1.00, \"(c)\", transform=ax0.transAxes)\n    ax1.text(-0.25, 1.00, \"(d)\", transform=ax1.transAxes)\n    ax0.set_ylim([0, 0.13])\n    ax0.set_xlabel(\"Epochs\")\n    ax1.set_xlabel(\"Epochs\")\n    ax0.set_xticks([0, 25, 50])\n    ax1.set_xticks([0, 25, 50])\n    ax0.set_ylabel(\"Training Cross Entropy Loss\")\n    ax1.set_ylabel(\"Validation Cross Entropy Loss\")\n\n    # Legend\n    ax = fig.add_subplot(gs[:, 2])\n    for (model, color) in zip([\"EchoNet-Dynamic (EF)\", \"R3D\", \"MC3\", \"EchoNet-Dynamic (Seg)\"], matplotlib.colors.TABLEAU_COLORS):\n        ax.plot([float(\"nan\")], [float(\"nan\")], \"-\", color=color, label=model)\n    ax.set_title(\"\")\n    ax.axis(\"off\")\n    ax.legend(loc=\"center\")\n\n    plt.tight_layout()\n    plt.savefig(os.path.join(args.fig, \"loss.pdf\"))\n    plt.savefig(os.path.join(args.fig, \"loss.eps\"))\n    plt.savefig(os.path.join(args.fig, \"loss.png\"))\n    plt.close(fig)\n\n\ndef load(filename):\n    \"\"\"Loads losses from specified file.\"\"\"\n\n    losses = {\"train\": [], \"val\": []}\n    with open(filename, \"r\") as f:\n        for line in f:\n            line = line.split(\",\")\n            if len(line) < 4:\n                continue\n            epoch, split, loss, *_ = line\n            epoch = int(epoch)\n            loss = float(loss)\n            assert(split in [\"train\", \"val\"])\n            if epoch == len(losses[split]):\n                losses[split].append(loss)\n            elif epoch == len(losses[split]) - 1:\n                losses[split][-1] = loss\n            else:\n                raise ValueError(\"File has uninterpretable formatting.\")\n    return losses\n\n\nif __name__ == \"__main__\":\n    main()\n"
  },
  {
    "path": "scripts/plot_simulated_noise.py",
    "content": "#!/usr/bin/env python3\n\n\"\"\"Code to generate plots for Extended Data Fig. 6.\"\"\"\n\nimport os\nimport pickle\n\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport PIL\nimport sklearn\nimport torch\nimport torchvision\n\nimport echonet\n\n\ndef main(fig_root=os.path.join(\"figure\", \"noise\"),\n         video_output=os.path.join(\"output\", \"video\", \"r2plus1d_18_32_2_pretrained\"),\n         seg_output=os.path.join(\"output\", \"segmentation\", \"deeplabv3_resnet50_random\"),\n         NOISE=(0, 0.1, 0.2, 0.3, 0.4, 0.5)):\n    \"\"\"Generate plots for Extended Data Fig. 6.\"\"\"\n\n    device = torch.device(\"cuda\")\n\n    filename = os.path.join(fig_root, \"data.pkl\")  # Cache of results\n    try:\n        # Attempt to load cache\n        with open(filename, \"rb\") as f:\n            Y, YHAT, INTER, UNION = pickle.load(f)\n    except FileNotFoundError:\n        # Generate results if no cache available\n        os.makedirs(fig_root, exist_ok=True)\n\n        # Load trained video model\n        model_v = torchvision.models.video.r2plus1d_18()\n        model_v.fc = torch.nn.Linear(model_v.fc.in_features, 1)\n        if device.type == \"cuda\":\n            model_v = torch.nn.DataParallel(model_v)\n        model_v.to(device)\n\n        checkpoint = torch.load(os.path.join(video_output, \"checkpoint.pt\"))\n        model_v.load_state_dict(checkpoint['state_dict'])\n\n        # Load trained segmentation model\n        model_s = torchvision.models.segmentation.deeplabv3_resnet50(aux_loss=False)\n        model_s.classifier[-1] = torch.nn.Conv2d(model_s.classifier[-1].in_channels, 1, kernel_size=model_s.classifier[-1].kernel_size)\n        if device.type == \"cuda\":\n            model_s = torch.nn.DataParallel(model_s)\n        model_s.to(device)\n\n        checkpoint = torch.load(os.path.join(seg_output, \"checkpoint.pt\"))\n        model_s.load_state_dict(checkpoint['state_dict'])\n\n        # Run simulation\n        dice = []\n        mse = []\n        r2 = []\n        Y = []\n        YHAT = []\n        INTER = []\n        UNION = []\n        for noise in NOISE:\n            Y.append([])\n            YHAT.append([])\n            INTER.append([])\n            UNION.append([])\n\n            dataset = echonet.datasets.Echo(split=\"test\", noise=noise)\n            PIL.Image.fromarray(dataset[0][0][:, 0, :, :].astype(np.uint8).transpose(1, 2, 0)).save(os.path.join(fig_root, \"noise_{}.tif\".format(round(100 * noise))))\n\n            mean, std = echonet.utils.get_mean_and_std(echonet.datasets.Echo(split=\"train\"))\n\n            tasks = [\"LargeFrame\", \"SmallFrame\", \"LargeTrace\", \"SmallTrace\"]\n            kwargs = {\n                \"target_type\": tasks,\n                \"mean\": mean,\n                \"std\": std,\n                \"noise\": noise\n            }\n            dataset = echonet.datasets.Echo(split=\"test\", **kwargs)\n\n            dataloader = torch.utils.data.DataLoader(dataset,\n                                                     batch_size=16, num_workers=5, shuffle=True, pin_memory=(device.type == \"cuda\"))\n\n            loss, large_inter, large_union, small_inter, small_union = echonet.utils.segmentation.run_epoch(model_s, dataloader, \"test\", None, device)\n            inter = np.concatenate((large_inter, small_inter)).sum()\n            union = np.concatenate((large_union, small_union)).sum()\n            dice.append(2 * inter / (union + inter))\n\n            INTER[-1].extend(large_inter.tolist() + small_inter.tolist())\n            UNION[-1].extend(large_union.tolist() + small_union.tolist())\n\n            kwargs = {\"target_type\": \"EF\",\n                      \"mean\": mean,\n                      \"std\": std,\n                      \"length\": 32,\n                      \"period\": 2,\n                      \"noise\": noise\n                      }\n\n            dataset = echonet.datasets.Echo(split=\"test\", **kwargs)\n\n            dataloader = torch.utils.data.DataLoader(dataset,\n                                                     batch_size=16, num_workers=5, shuffle=True, pin_memory=(device.type == \"cuda\"))\n            loss, yhat, y = echonet.utils.video.run_epoch(model_v, dataloader, \"test\", None, device)\n            mse.append(loss)\n            r2.append(sklearn.metrics.r2_score(y, yhat))\n            Y[-1].extend(y.tolist())\n            YHAT[-1].extend(yhat.tolist())\n\n        # Save results in cache\n        with open(filename, \"wb\") as f:\n            pickle.dump((Y, YHAT, INTER, UNION), f)\n\n    # Set up plot\n    echonet.utils.latexify()\n\n    NOISE = list(map(lambda x: round(100 * x), NOISE))\n    fig = plt.figure(figsize=(6.50, 4.75))\n    gs = matplotlib.gridspec.GridSpec(3, 1, height_ratios=[2.0, 2.0, 0.75])\n    ax = (plt.subplot(gs[0]), plt.subplot(gs[1]), plt.subplot(gs[2]))\n\n    # Plot EF prediction results (R^2)\n    r2 = [sklearn.metrics.r2_score(y, yhat) for (y, yhat) in zip(Y, YHAT)]\n    ax[0].plot(NOISE, r2, color=\"k\", linewidth=1, marker=\".\")\n    ax[0].set_xticks([])\n    ax[0].set_ylabel(\"R$^2$\")\n    l, h = min(r2), max(r2)\n    l, h = l - 0.1 * (h - l), h + 0.1 * (h - l)\n    ax[0].axis([min(NOISE) - 5, max(NOISE) + 5, 0, 1])\n\n    # Plot segmentation results (DSC)\n    dice = [echonet.utils.dice_similarity_coefficient(inter, union) for (inter, union) in zip(INTER, UNION)]\n    ax[1].plot(NOISE, dice, color=\"k\", linewidth=1, marker=\".\")\n    ax[1].set_xlabel(\"Pixels Removed (%)\")\n    ax[1].set_ylabel(\"DSC\")\n    l, h = min(dice), max(dice)\n    l, h = l - 0.1 * (h - l), h + 0.1 * (h - l)\n    ax[1].axis([min(NOISE) - 5, max(NOISE) + 5, 0, 1])\n\n    # Add example images below\n    for noise in NOISE:\n        image = matplotlib.image.imread(os.path.join(fig_root, \"noise_{}.tif\".format(noise)))\n        imagebox = matplotlib.offsetbox.OffsetImage(image, zoom=0.4)\n        ab = matplotlib.offsetbox.AnnotationBbox(imagebox, (noise, 0.0), frameon=False)\n        ax[2].add_artist(ab)\n        ax[2].axis(\"off\")\n    ax[2].axis([min(NOISE) - 5, max(NOISE) + 5, -1, 1])\n\n    fig.tight_layout()\n    plt.savefig(os.path.join(fig_root, \"noise.pdf\"), dpi=1200)\n    plt.savefig(os.path.join(fig_root, \"noise.eps\"), dpi=300)\n    plt.savefig(os.path.join(fig_root, \"noise.png\"), dpi=600)\n    plt.close(fig)\n\n\nif __name__ == \"__main__\":\n    main()\n"
  },
  {
    "path": "scripts/run_experiments.sh",
    "content": "#!/bin/bash\n\nfor pretrained in True False\ndo\n    for model in r2plus1d_18 r3d_18 mc3_18\n    do\n        for frames in 96 64 32 16 8 4 1\n        do\n            batch=$((256 / frames))\n            batch=$(( batch > 16 ? 16 : batch ))\n\n            cmd=\"import echonet; echonet.utils.video.run(modelname=\\\"${model}\\\", frames=${frames}, period=1, pretrained=${pretrained}, batch_size=${batch})\"\n            python3 -c \"${cmd}\"\n        done\n        for period in 2 4 6 8\n        do\n            batch=$((256 / 64 * period))\n            batch=$(( batch > 16 ? 16 : batch ))\n\n            cmd=\"import echonet; echonet.utils.video.run(modelname=\\\"${model}\\\", frames=(64 // ${period}), period=${period}, pretrained=${pretrained}, batch_size=${batch})\"\n            python3 -c \"${cmd}\"\n        done\n    done\ndone\n\nperiod=2\npretrained=True\nfor model in r2plus1d_18 r3d_18 mc3_18\ndo\n    cmd=\"import echonet; echonet.utils.video.run(modelname=\\\"${model}\\\", frames=(64 // ${period}), period=${period}, pretrained=${pretrained}, run_test=True)\"\n    python3 -c \"${cmd}\"\ndone\n\npython3 -c \"import echonet; echonet.utils.segmentation.run(modelname=\\\"deeplabv3_resnet50\\\",  save_segmentation=True, pretrained=False)\"\n\npretrained=True\nmodel=r2plus1d_18\nperiod=2\nbatch=$((256 / 64 * period))\nbatch=$(( batch > 16 ? 16 : batch ))\nfor patients in 16 32 64 128 256 512 1024 2048 4096 7460\ndo\n    cmd=\"import echonet; echonet.utils.video.run(modelname=\\\"${model}\\\", frames=(64 // ${period}), period=${period}, pretrained=${pretrained}, batch_size=${batch}, num_epochs=min(50 * (8192 // ${patients}), 200), output=\\\"output/training_size/video/${patients}\\\", n_train_patients=${patients})\"\n    python3 -c \"${cmd}\"\n    cmd=\"import echonet; echonet.utils.segmentation.run(modelname=\\\"deeplabv3_resnet50\\\", pretrained=False, num_epochs=min(50 * (8192 // ${patients}), 200), output=\\\"output/training_size/segmentation/${patients}\\\", n_train_patients=${patients})\"\n    python3 -c \"${cmd}\"\n\ndone\n\n"
  },
  {
    "path": "setup.py",
    "content": "#!/usr/bin/env python3\n\"\"\"Metadata for package to allow installation with pip.\"\"\"\n\nimport os\n\nimport setuptools\n\nwith open(\"README.md\", \"r\") as fh:\n    long_description = fh.read()\n\n# Use same version from code\n# See 3 from\n# https://packaging.python.org/guides/single-sourcing-package-version/\nversion = {}\nwith open(os.path.join(\"echonet\", \"__version__.py\")) as f:\n    exec(f.read(), version)  # pylint: disable=W0122\n\nsetuptools.setup(\n    name=\"echonet\",\n    description=\"Video-based AI for beat-to-beat cardiac function assessment.\",\n    version=version[\"__version__\"],\n    url=\"https://echonet.github.io/dynamic\",\n    packages=setuptools.find_packages(),\n    install_requires=[\n        \"click\",\n        \"numpy\",\n        \"pandas\",\n        \"torch\",\n        \"torchvision\",\n        \"opencv-python\",\n        \"scikit-image\",\n        \"tqdm\",\n        \"sklearn\"\n    ],\n    classifiers=[\n        \"Programming Language :: Python :: 3\",\n    ],\n    entry_points={\n        \"console_scripts\": [\n            \"echonet=echonet:main\",\n        ],\n    }\n\n)\n"
  }
]