text:
#! /usr/bin/env python
#
def hermite_ek_compute ( n ):

#*****************************************************************************80
#
## HERMITE_EK_COMPUTE computes a Gauss-Hermite quadrature rule.
#
#  Discussion:
#
#    The code uses an algorithm by Elhay and Kautsky.
#
#    The abscissas are the zeros of the N-th order Hermite polynomial.
#
#    The integral:
#
#      integral ( -oo < x < +oo ) exp ( - x * x ) * f(x) dx
#
#    The quadrature rule:
#
#      sum ( 1 <= i <= n ) w(i) * f ( x(i) )
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    15 June 2015
#
#  Author:
#
#    John Burkardt.
#
#  Reference:
#
#    Sylvan Elhay, Jaroslav Kautsky,
#    Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
#    Interpolatory Quadrature,
#    ACM Transactions on Mathematical Software,
#    Volume 13, Number 4, December 1987, pages 399-415.
#
#  Parameters:
#
#    Input, integer N, the number of abscissas.
#
#    Output, real X(N), the abscissas.
#
#    Output, real W(N), the weights.
#
  import numpy as np
  from imtqlx import imtqlx
  from r8_gamma import r8_gamma
#
#  Define the zero-th moment.
#
  zemu = r8_gamma ( 0.5 )
#
#  Define the Jacobi matrix.
#
  bj = np.zeros ( n )
  for i in range ( 0, n ):
    bj[i] = np.sqrt ( float ( i + 1 ) / 2.0 )

  x = np.zeros ( n )

  w = np.zeros ( n )
  w[0] = np.sqrt ( zemu )
#
#  Diagonalize the Jacobi matrix.
#
  x, w = imtqlx ( n, x, bj, w )
#
#  If N is odd, force the center X to be exactly 0.
#
  if ( ( n % 2 ) == 1 ):
    x[(n-1)//2] = 0.0

  for i in range ( 0, n ):
    w[i] = w[i] ** 2

  return x, w

def hermite_ek_compute_test ( ):

#*****************************************************************************80
#
## HERMITE_EK_COMPUTE_TEST tests HERMITE_EK_COMPUTE.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    15 June 2015
#
#  Author:
#
#    John Burkardt
#
  import platform

  print ( '' )
  print ( 'HERMITE_EK_COMPUTE_TEST' )
  print ( '  Python version: %s' % ( platform.python_version ( ) ) )
  print ( '  HERMITE_EK_COMPUTE computes a Hermite quadrature rule' )
  print ( '  using the Elhay-Kautsky algorithm.' )
  print ( '' )
  print ( '  Index       X             W' )

  for n in range ( 1, 11 ):

    x, w = hermite_ek_compute ( n )

    print ( '' )

    for i in range ( 0, n ):
      print ( '  %2d  %24.16g  %24.16g' % ( i, x[i], w[i] ) )
#
#  Terminate.
#
  print ( '' )
  print ( 'HERMITE_EK_COMPUTE_TEST:' )
  print ( '  Normal end of execution.' )

  return

if ( __name__ == '__main__' ):
  from timestamp import timestamp
  timestamp ( )
  hermite_ek_compute_test ( )
  timestamp ( )


text:
import os
import json
from io import BytesIO

import boto3
import glog as log
import numpy as np
import traceback
from PIL import Image

from multivitamin.module import PropertiesModule, Codes
from multivitamin.utils.work_handler import WorkerManager
from multivitamin.media import MediaRetriever
from multivitamin.data.response.utils import get_current_time
from multivitamin.data.response.dtypes import (
    Footprint,
    VideoAnn,
    Property,
)

def get_contents_file_s3_key(url, sample_rate=None):
    contents_file_name = "contents"
    contents_file_ext = "json"
    rel_path_format = "{video_id}/{filename}.{ext}"
    video_id = os.path.basename(url).rsplit(".", 1)[0]
    video_id = "".join([e for e in video_id if e.isalnum() or e in ["/", "."]])
    if video_id is not None:
        video_id += ".{}fps".format(sample_rate)
    contents_file_key = rel_path_format.format(
        video_id=video_id, filename=contents_file_name, ext=contents_file_ext
    )
    contents_file_key = "".join(
        [e for e in contents_file_key if e.isalnum() or e in ["/", "."]]
    )
    return contents_file_key

class FrameExtractor(PropertiesModule):
    def __init__(
        self,
        server_name,
        version,
        sample_rate=1.0,
        s3_bucket=None,
        local_dir=None,
        module_id_map=None,
        n_threads=100,
    ):
        super().__init__(server_name, version, module_id_map=module_id_map)

        self.n_threads = n_threads
        self._sample_rate = sample_rate
        self._local_dir = local_dir
        self._s3_bucket = s3_bucket
        self._list_file = "contents"
        self._img_name_format = "{tstamp:010d}"
        self._rel_path_format = "{video_id}/{filename}.{ext}"
        self._image_rel_path_format = "{video_id}/frames/{filename}.{ext}"
        self._s3_url_format = "https://s3.amazonaws.com/{bucket}/{s3_key}"

        self._sql_db = ""

        self._s3_client = boto3.client("s3")

        self._encoding = "JPEG"
        self._content_type = "image/jpeg"
        self._s3_upload_args = {"ContentType": self._content_type}

    @staticmethod
    def _mklocaldirs(directory):
        if not os.path.exists(directory):
            os.makedirs(directory)

    def _write_frame_helper(self, data):
        try:
            self._write_frame(**data)
        except:
            log.error("Local Write Failed")

    def _write_frame(self, frame, tstamp, video_id):
        filename = self._img_name_format.format(tstamp=tstamp)
        relative_path = self._image_rel_path_format.format(
            video_id=video_id, filename=filename, ext=self._encoding
        )
        full_path = "{}/{}".format(self._local_dir, relative_path)
        full_path = "".join([e for e in full_path if e.isalnum() or e in ["/", "."]])
        im_filelike = self._convert_frame_to_filelike(frame)
        with open(full_path, "wb") as f:
            f.write(im_filelike.read())

    def _add_contents_to_local(self, contents):
        filelike = BytesIO()
        contents_json = {"original_url": self.video_url, "frames": []}
        for video_id, tstamp in contents:
            filename = self._img_name_format.format(tstamp=tstamp)
            relative_path = self._image_rel_path_format.format(
                video_id=video_id, filename=filename, ext=self._encoding
            )
            full_path = "{}/{}".format(self._local_dir, relative_path)
            full_path = "".join(
                [e for e in full_path if e.isalnum() or e in ["/", "."]]
            )
            # line = "{}\t{}\n".format(tstamp, full_path)
            # filelike.write(line.encode())
            contents_json["frames"].append((tstamp, full_path))
        filelike.write(json.dumps(contents_json, indent=2).encode())
        filelike.seek(0)

        with open("{}/{}".format(self._local_dir, self.contents_file_key), "wb") as f:
            f.write(filelike.read())

    def _convert_frame_to_filelike(self, frame):
        im = Image.fromarray(frame)
        im_filelike = BytesIO()
        im.save(im_filelike, format=self._encoding)
        im_filelike.seek(0)
        return im_filelike

    def _upload_frame_helper(self, data):
        try:
            self._upload_frame(**data)
        except:
            log.error("Failed to write Frame to S3")

    def _upload_frame(self, frame, tstamp, video_id):
        filename = self._img_name_format.format(tstamp=tstamp)
        im_filelike = self._convert_frame_to_filelike(frame)
        s3_key = self._image_rel_path_format.format(
            video_id=video_id, filename=filename, ext=self._encoding
        )
        s3_key = "".join([e for e in s3_key if e.isalnum() or e in ["/", "."]])
        result = self._s3_client.upload_fileobj(
            im_filelike, self._s3_bucket, s3_key, ExtraArgs=self._s3_upload_args
        )
        self._append_to_sql(video_id, tstamp, s3_key, result)

    def _append_to_sql(self, video_id, tstamp, s3_key, result):
        pass

    def _add_contents_to_s3(self, contents):
        filelike = BytesIO()
        contents_json = {"original_url": self.video_url, "frames": []}
        for video_id, tstamp in contents:
            filename = self._img_name_format.format(tstamp=tstamp)
            s3_key = self._image_rel_path_format.format(
                video_id=video_id, filename=filename, ext=self._encoding
            )
            s3_key = "".join([e for e in s3_key if e.isalnum() or e in ["/", "."]])
            im_url = self._s3_url_format.format(bucket=self._s3_bucket, s3_key=s3_key)
            # line = "{}\t{}\n".format(tstamp, im_url)
            # filelike.write(line.encode())
            contents_json["frames"].append((tstamp, im_url))
        filelike.write(json.dumps(contents_json, indent=2).encode())
        filelike.seek(0)

        result = self._s3_client.upload_fileobj(
            filelike,
            self._s3_bucket,
            self.contents_file_key,
            ExtraArgs={"ContentType": "application/json"},
        )
        return result

    def process_properties(self):
        self._s3_write_manager = WorkerManager(
            func=self._upload_frame_helper,
            n=self.n_threads,
            max_queue_size=100,
            parallelization="thread",
        )

        self._local_write_manager = WorkerManager(
            func=self._write_frame_helper,
            n=self.n_threads,
            max_queue_size=100,
            parallelization="thread",
        )

        self.last_tstamp = 0.0
        log.info("Processing")
        # filelike = self.media_api.download(return_filelike=True)

        # if filelike.getbuffer().nbytes == 0:
        #     self.code = "ERROR_NO_IMAGES_LOADED"

        # log.info('Getting hash')
        # video_hash = hashfileobject(filelike, hexdigest=True)
        self.video_url = self.response.request.url
        try:
            log.info(f"Loading media from url: {self.response.request.url}")
            self.med_ret = MediaRetriever(self.video_url)
        except Exception as e:
            log.error(e)
            log.error(traceback.print_exc())
            self.code = Codes.ERROR_LOADING_MEDIA
            return

        self.contents_file_key = get_contents_file_s3_key(self.video_url,
                                                          self._sample_rate)
        video_id = self.contents_file_key.split("/")[0]
        if self._local_dir is not None:
            self._mklocaldirs("{}/{}".format(self._local_dir, video_id))
            self._mklocaldirs("{}/{}/frames".format(self._local_dir, video_id))
            if os.path.exists("{}/{}".format(self._local_dir, self.contents_file_key)):
                log.info("Local Video already exists")

        try:
            self._s3_client.head_object(
                Bucket=self._s3_bucket, Key=self.contents_file_key
            )
            new_url = self._s3_url_format.format(
                bucket=self._s3_bucket, s3_key=self.contents_file_key
            )
            log.info("Video already exists")
            p = Property(
                server=self.name,
                ver=self.version,
                value=new_url,
                property_type="extraction",
                property_id=1,
            )
            track = VideoAnn(t1=0.0, t2=float(self.last_tstamp), props=[p])
            self.response.append_track(track)
            self._s3_write_manager.kill_workers_on_completion()
            self._local_write_manager.kill_workers_on_completion()
            return
        except:
            pass

        contents = []
        log.info("Getting frames")
        for i, (frame, tstamp_secs) in enumerate(
            self.med_ret.get_frames_iterator(sample_rate=self._sample_rate)
        ):
            tstamp = int(tstamp_secs * 1000)
            # self._upload_frame(frame, tstamp, video_hash)
            if i % 100 == 0:
                log.info("...tstamp: " + str(tstamp))
            log.debug("tstamp: " + str(tstamp))
            if frame is None:
                continue
            frame = np.ascontiguousarray(frame[:, :, ::-1])  # RGB to BGR
            self.last_tstamp = tstamp
            data = {"frame": frame, "tstamp": tstamp, "video_id": video_id}
            contents.append((video_id, tstamp))

            if self._local_dir is not None:
                self._local_write_manager.queue.put(data)

            if self._s3_bucket is not None:
                self._s3_write_manager.queue.put(data)

        # self._s3_write_manager.kill_workers_on_completion()
        # self._local_write_manager.kill_workers_on_completion()
        if self._s3_bucket is not None:
            result = self._add_contents_to_s3(contents)
        if self._local_dir is not None:
            self._add_contents_to_local(contents)

        new_url = self._s3_url_format.format(
            bucket=self._s3_bucket, s3_key=self.contents_file_key
        )
        p = Property(
            server=self.name,
            ver=self.version,
            value=new_url,
            property_type="extraction",
            property_id=1,
        )
        track = VideoAnn(t1=0.0, t2=float(self.last_tstamp), props=[p])
        self.response.append_track(track)
        self._s3_write_manager.kill_workers_on_completion()
        self._local_write_manager.kill_workers_on_completion()

ext: py

max_stars_repo_name: keishinkickback/multivitamin

max_issues_repo_name: keishinkickback/multivitamin

max_forks_repo_name: keishinkickback/multivitamin

reason: import numpy


text:
#!/usr/bin/python

from tkinter import *
from tkinter.messagebox import showerror
from Layers import Strato
import Model
from Model import Chimera
import numpy as np
from os import listdir, getcwd, mkdir
from os.path import isfile, join, isdir
from DataManager import load_inputs, load_labels, prepare_data
from ModelManager import load_model, select_export_filepath

global globalPath
globalPath = join(getcwd(), "Chimera")

global color
color = {
    "Dense": '#2196F3',
    "Conv1D": '#4CAF50',
    "Conv2D": '#009688',
    "Dropout": '#F44336'}

def switch_freeze(ix):
    strato = nnet.layers[ix]
    if strato.layer.trainable:
        strato.freeze()
    else:
        strato.unfreeze()
    update_net()

def move_up(ix):
    nnet.move_up(ix)
    update_net()

def move_down(ix):
    nnet.move_down(ix)
    update_net()

def remove(ix):
    nnet.remove_layer(ix)
    update_net()

def update_net():
    global layer_repr

    for label in layer_repr:
        label.grid_forget()

    layer_repr = []

    n_row = 5
    ix = 0

    for l in nnet.layers:
        frame = Frame(master,
                      bg=color[l.config['layer_type']],
                      padx=32,
                      pady=4,
                      width=60)
        frame.grid(row=n_row, column=1, columnspan=4, pady=2)
        layer_repr.append(frame)

        # label for layer type
        label = Label(frame, text=l.config['layer_type'],
                      bg=frame['bg'],
                      padx=4,
                      width=15,
                      font="Arial 14")
        label.grid(row=n_row, column=1)

        # label for layer shape
        shape_l = Label(frame, text=l.label,
                        bg=frame['bg'],
                        padx=4,
                        width=6,
                        font="Arial 14")
        shape_l.grid(row=n_row, column=2)

        # freeze/unfreeze button
        freeze_b_text = StringVar()
        if l.layer.trainable:
            freeze_b_text = "Freeze"
        else:
            freeze_b_text = "Unfreeze"

        freeze_b = Button(frame,
                          bg='#0D47A1',
                          foreground='#FFFFFF',
                          borderwidth=0,
                          text=freeze_b_text,
                          font="Arial 12",
                          command=lambda ix=ix: switch_freeze(ix))
        freeze_b.grid(row=n_row, column=3)

        if ix < len(nnet.layers) - 1:
            # button to move up
            if ix > 0:
                up_b = Button(frame,
                              image=upPhoto,
                              text="up",
                              command=lambda ix=ix: move_up(ix))
                up_b.grid(row=n_row, column=4)

            if ix < len(nnet.layers) - 2:
                # button to move down
                down_b = Button(frame,
                                image=downPhoto,
                                text="down",
                                command=lambda ix=ix: move_down(ix))
                down_b.grid(row=n_row, column=5)

            # button to remove
            remove_b = Button(frame,
                              image=removePhoto,
                              text="delete",
                              command=lambda ix=ix: remove(ix))
            remove_b.grid(row=n_row, column=6)

        n_row += 1
        ix += 1

    # set the selected item in loss menu and optimizer menu
    loss_choice.set(nnet.loss_name)
    opt_choice.set(nnet.optimizer_name)

def import_model():
    raise NotImplementedError()

def export():
    filepath = select_export_filepath()

    try:
        nnet.export(filepath)
    except RuntimeError:
        showerror("Error", "Could not build model.")

def add_layer(layer_type):
    """
    function to add a layer to the network
    args:
        layer_type (string):
    """
    nnet.add_layer(layer_type)
    # update the view of the network with the new layer
    update_net()

def update_list_models():
    menu = menu_models["menu"]
    menu.delete(0, "end")
    mypath = "models"
    models = [f for f in listdir(mypath) if isfile(join(mypath, f))]
    for m in models:
        menu.add_command(label=m,
                         command=lambda value=m: model_choice.set(value))

def save_and_update():
    nnet.save(modelname_e.get())
    update_list_models()

def load_and_update(model_name):
    nnet.load(model_name)
    update_net()

def fit():
    try:
        train_examples, train_labels, test_examples, test_labels = prepare_data()
    except RuntimeError:
        print("data not selected")
        return

    history_obj = nnet.fit(train_examples, train_labels)
    accuracy = history_obj.history['accuracy'][-1]
    loss = history_obj.history['loss'][-1]
    accuracy_l['text'] = "Accuracy: " + str(accuracy)
    loss_l['text'] = "Loss: " + str(loss)
    update_net()

def create_window():
    global master, layer_repr

    # main window setup
    master = Tk()
    master.title("Chimera")
    master.minsize(1020, 620)
    master.geometry("1020x620")
    master['bg'] = '#fafafa'
    master.columnconfigure(0, weight=1)
    master.columnconfigure(1, weight=1)
    master.columnconfigure(2, weight=2)
    master.columnconfigure(3, weight=1)
    master.columnconfigure(4, weight=1)
    master.columnconfigure(5, weight=2)

    # all images
    global upPhoto, downPhoto, removePhoto
    imgDir = join(globalPath, "img")
    upPhoto = PhotoImage(file=join(imgDir, "arrow_up.png"))
    downPhoto = PhotoImage(file=join(imgDir, "arrow_down.png"))
    removePhoto = PhotoImage(file=join(imgDir, "delete.png"))
    savephoto = PhotoImage(file=join(imgDir, "save.png"))

    global input_l, labels_l, accuracy_l, loss_l
    input_l = Label(master,
                    text="No input data",
                    font="Arial 11",
                    bg=master['bg'],
                    padx=12)
    input_l.grid(row=0, column=5, sticky="W")
    labels_l = Label(master,
                     text="No labels",
                     font="Arial 11",
                     bg=master['bg'],
                     padx=12)
    labels_l.grid(row=1, column=5, sticky="W")
    accuracy_l = Label(master,
                       text="No accuracy data ",
                       font="Arial 11",
                       bg=master['bg'],
                       padx=12)
    accuracy_l.grid(row=2, column=5, sticky="W")
    loss_l = Label(master,
                   text="No loss data",
                   font="Arial 11",
                   bg=master['bg'],
                   padx=12)
    loss_l.grid(row=3, column=5, sticky="W")

    menubar = Menu(master)
    importMenu = Menu(menubar, tearoff=0)
    importMenu.add_command(label="Import Model", command=import_model)
    importMenu.add_command(label="Import Input", command=lambda value=input_l: load_inputs(value))
    importMenu.add_command(label="Import Labels", command=lambda value=labels_l: load_labels(value))

    menubar.add_cascade(label="Import", menu=importMenu)
    menubar.add_command(label="Export", command=export)
    menubar.add_command(label="Train", command=fit)
    master.config(menu=menubar)

    model_l = Label(master, text="Model name:",
                    bg=master['bg'])
    model_l.grid(row=0, column=1, sticky="E", padx=4)

    # entry for model name
    global modelname_e
    modelname_e = Entry(master)
    modelname_e.grid(row=0, column=2, sticky="ew")

    # save button
    save_b = Button(master,
                    image=savephoto,
                    text="save",
                    command=save_and_update)
    save_b.grid(row=0, column=3, sticky="W", padx=4)

    # model menu
    load_model_l = Label(master, text="Saved models:",
                         bg=master['bg'])
    load_model_l.grid(row=1, column=1, sticky="E", padx=4)

    models = [f for f in listdir("models") if isfile(join("models", f))]
    if models == []:
        models.append("")
    global menu_models, model_choice
    model_choice = StringVar(master)
    model_choice.set(models[0])
    menu_models = OptionMenu(master, model_choice, *models,
                             command=lambda model_name=model_choice.get(): load_and_update(model_name))
    menu_models.grid(row=1, column=2, sticky="ew",)

    # losses menu
    losses_l = Label(master, text="Losses:", bg=master['bg'])
    losses_l.grid(row=2, column=1, sticky="E", padx=4)

    global loss_choice
    losses = [i for i in Model.losses.keys()]
    loss_choice = StringVar(master)
    loss_choice.set('CategoricalCrossentropy')
    menu_losses = OptionMenu(master, loss_choice, *losses,
                             command=lambda loss_name=loss_choice.get(): nnet.set_loss(loss_name))
    menu_losses.grid(row=2, column=2, sticky="ew")

    # optimizers menu
    opt_l = Label(master, text="Optimizers:", bg=master['bg'])
    opt_l.grid(row=3, column=1, sticky="E", padx=4)

    global opt_choice
    optimizers = [i for i in Model.optimizers.keys()]
    opt_choice = StringVar(master)
    opt_choice.set('Adam')
    menu_opt = OptionMenu(master, opt_choice, *optimizers,
                          command=lambda opt_name=opt_choice.get(): nnet.set_optimizer(opt_name))
    menu_opt.grid(row=3, column=2, sticky="ew")

    # initialize empty list for layer repr
    layer_repr = []

    # buttons to add layers
    addDense_b = Button(master,
                        text="Add Dense",
                        width=15,
                        bg=color['Dense'],
                        command=lambda: add_layer("Dense"))
    addDense_b.grid(row=0, column=0)
    addConv1D_b = Button(master,
                         text="Add Conv1D",
                         width=15,
                         bg=color['Conv1D'],
                         command=lambda: add_layer("Conv1D"))
    addConv1D_b.grid(row=1, column=0)
    addConv2D_b = Button(master,
                         text="Add Conv2D",
                         width=15,
                         bg=color['Conv2D'],
                         command=lambda: add_layer("Conv2D"))
    addConv2D_b.grid(row=2, column=0)
    addDropout_b = Button(master,
                          text="Add Dropout",
                          width=15,
                          bg=color['Dropout'],
                          command=lambda: add_layer("Dropout"))
    addDropout_b.grid(row=3, column=0)

    update_net()
    mainloop()

def set_nnet():
    global nnet
    nnet = Chimera()
    nnet.add_layer("Dense")

def checkdir():
    if not isdir("models"):
        mkdir("models")
    if not isdir("layers"):
        mkdir("layers")

checkdir()
set_nnet()
create_window()


text:
import os
import glob
import io
import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
from pylab import rcParams
from matplotlib import cm
import joypy

# =============================================================================
# CONSTANTS
# =============================================================================

# Paths to input data.
pKa_SUBMISSIONS_DIR_PATH = '../relative_microstate_free_energy_predictions/'
USER_MAP_FILE_PATH = '../SAMPL8-pKa-user-map.csv'

# =============================================================================
# UTILITY CLASSES
# =============================================================================

class IgnoredSubmissionError(Exception):
    """Exception used to signal a submission that must be ignored."""
    pass

class BadFormatError(Exception):
    """Exception used to signal a submission with unexpected formatting."""
    pass

class SamplSubmission:
    """A generic SAMPL submission.
    Parameters
    ----------
    file_path : str
        The path to the submission file.
    Raises
    ------
    IgnoredSubmission
        If the submission ID is among the ignored submissions.
    """

    # Section of the submission file.
    SECTIONS = {}

    # Sections in CSV format with columns names.
    CSV_SECTIONS = {}

    def __init__(self, file_path, user_map):
        file_name = os.path.splitext(os.path.basename(file_path))[0]
        file_data = file_name.split('-')

        # Load predictions.
        sections = self._load_sections(file_path)  # From parent-class.
        self.data = sections['Predictions']  # This is a list
        self.data = pd.DataFrame(data=self.data) # Now a DataFrame
        #self.name = sections['Name'][0] #want this to take the place of the 5 letter code
        self.file_name = file_name

        self.method_name = sections['Name'][0] #want this to take the place of the 5 letter code

        # Check if this is a reference submission
        self.reference_submission = False
        #if self.method_name in self.REF_SUBMISSIONS:
        if "REF" in self.method_name or "NULL" in self.method_name:
            print("REF found: ", self.method_name)
            self.reference_submission = True

    @classmethod
    def _read_lines(cls, file_path):
        """Generator to read the file and discard blank lines and comments."""
        with open(file_path, 'r', encoding='utf-8-sig') as f:
            for line in f:
                # Strip whitespaces.
                line = line.strip()
                # Don't return blank lines and comments.
                if line != '' and line[0] != '#':
                    yield line

    @classmethod
    def _load_sections(cls, file_path):
        """Load the data in the file and separate it by sections."""
        #print("file_path",file_path)
        sections = {}
        current_section = None
        for line in cls._read_lines(file_path):
            # Check if this is a new section.
            if line[:-1] in cls.SECTIONS:
                current_section = line[:-1]
            else:
                if current_section is None:
                    import pdb
                    pdb.set_trace()
                try:
                    sections[current_section].append(line)
                except KeyError:
                    sections[current_section] = [line]

        # Check that all the sections have been loaded.
        found_sections = set(sections.keys())
        if found_sections != cls.SECTIONS:
            raise BadFormatError('Missing sections: {}.'.format(found_sections - cls.SECTIONS))

        # Create a Pandas dataframe from the CSV format.
        #for section_name in cls.CSV_SECTIONS:
        #    csv_str = io.StringIO('\n'.join(sections[section_name]))
        #    columns = cls.CSV_SECTIONS[section_name]
        #    id_column = columns[0]
        #    section = pd.read_csv(csv_str, index_col=id_column, names=columns, skipinitialspace=True)
        #    sections[section_name] = section

        # Create a Pandas dataframe from the CSV format.
        for section_name, pandas_kwargs in cls.CSV_SECTIONS.items():
            try:
                csv_str = io.StringIO('\n'.join(sections[section_name]))
                section = pd.read_csv(csv_str, skipinitialspace=True, **pandas_kwargs)
                sections[section_name] = section
            except:
                print(f"Error loading {section_name}, continuing without.") #If it's not there, don't save
        return sections

# =============================================================================
# pKa PREDICTION CHALLENGE
# =============================================================================

class pKaSubmission(SamplSubmission):
    """A submission for pKa challenge.
    Parameters
    ----------
    file_path : str
        The path to the submission file
    Raises
    ------
    IgnoredSubmission
        If the submission ID is among the ignored submissions.
    """

    # The D3R challenge IDs that are handled by this class.
    #CHALLANGE_IDS = {1559}

    # The IDs of the submissions that will be ignored in the analysis.
    #TEST_SUBMISSIONS = {}

    # Section of the submission file.
    '''SECTIONS = {"Predictions",
                "Participant name",
                "Participant organization",
                "Name",
                "Compute time",
                "Computing and hardware",
                "Software",
                "Category",
                "Method",
                "Ranked"}'''

    SECTIONS = {'Predictions', 'Participant name', 'Participant organization', 'Compute time',
        'Computing and hardware', 'Name', 'Software', 'Method', 'Category', 'Ranked'}

    # Sections in CSV format with columns names.
    #CSV_SECTIONS = {"Predictions": ("ID tag",
    #                                "Molecule ID",
    #                                "total charge",
    #                                "relative free energy",
    #                                "relative free energy SEM",
    #                                "relative free energy model uncertainty")}
    CSV_SECTIONS = {'Predictions': {'names': ('System ID', 'Alternate State','formal charge', '$\Delta\Delta $G',
        'SEM $\Delta \Delta $G', 'd$\Delta \Delta$G', 'SMILES'),'index_col': 'System ID'}}

    def __init__(self, file_path, user_map):
        super().__init__(file_path, user_map)

        file_name = os.path.splitext(os.path.basename(file_path))[0]
        file_data = file_name.split('-')

        # Load predictions.
        sections = self._load_sections(file_path)  # From parent-class.
        self.data = sections['Predictions']  # This is a pandas DataFrame.
        self.method_name = sections['Name'][0]
        self.category = sections['Category'][0] # New section for pKa challenge.
        self.participant = sections['Participant name'][0].strip()
        self.organization = sections['Participant organization'][0].strip()
        self.ranked = sections['Ranked'][0].strip() =='True'

# =============================================================================
# UTILITY FUNCTIONS
# =============================================================================

def load_submissions(directory_path, user_map):
    """Load submissions from a specified directory using a specified user map.
    Optional argument:
        ref_ids: List specifying submission IDs (alphanumeric, typically) of
        reference submissions which are to be ignored/analyzed separately.
    Returns: submissions
    """
    submissions = []
    for file_path in glob.glob(os.path.join(directory_path, '*.csv')):
        try:
            submission = pKaSubmission(file_path, user_map)

        except IgnoredSubmissionError:
            continue
        submissions.append(submission)
    return submissions

class pKaSubmissionCollection:
    """A collection of pKa submissions."""

    def __init__(self, submissions, output_directory_path, pKa_submission_collection_file_path, no_outliers = True):

        # Compute beta and other constants
        kB = 1.381 * 6.02214 / 1000.0  # [kJ/(mol K)]
        beta = 1. / (kB * 300)  # [mol/kJ]
        beta = beta * 4.186
        C_unit = 1 / beta * np.log(10)

        # Build collection dataframe from the beginning.
        # Build full pKa collection table.

        data = []

        # Submissions for pKa.
        for submission in submissions_RFE:
            #print(submission.file_name)
            if "RFE-NHLBI-TZVP-QM" in submission.method_name and no_outliers:
                continue
            #print(submission.method_name)
            #print(submission.data.keys())

            for mol_ID, series in submission.data.iterrows():
                ref_state = submission.data.loc[mol_ID, "Alternate State"]

                RFE_mean_pred_original = submission.data.loc[mol_ID, '$\Delta\Delta $G']
                #print(RFE_mean_pred_original)
                RFE_SEM_pred_original = submission.data.loc[mol_ID, 'SEM $\Delta \Delta $G']
                total_charge = submission.data.loc[mol_ID, 'formal charge']
                #print(total_charge)
                RFE_model_uncertainty_original =  submission.data.loc[mol_ID, 'd$\Delta \Delta$G']

                '''# correct sign + unit
                if submission.file_name in ["pKa-VA-2", "pKa_RodriguezPaluch_SMD_1", "pKa_RodriguezPaluch_SMD_2", "pKa_RodriguezPaluch_SMD_3"]:
                    sign_error = "yes"
                    RFE_mean_pred = RFE_mean_pred_original*-1

                    RFE_mean_pred = RFE_mean_pred*1.36 #convert submission to kcal/mol
                    RFE_SEM_pred = RFE_SEM_pred_original*1.36
                    RFE_model_uncertainty = RFE_model_uncertainty_original*1.36'''

                # Convert submissions to kcal/mol
                if submission.file_name in ["pKa-ECRISM-1", "pKa-VA-2-charge-correction", "pKa_RodriguezPaluch_SMD_1", "pKa_RodriguezPaluch_SMD_2", "pKa_RodriguezPaluch_SMD_3"]:
                    RFE_mean_pred = RFE_mean_pred_original*C_unit #convert submission to kcal/mol
                    RFE_SEM_pred = RFE_SEM_pred_original*C_unit
                    RFE_model_uncertainty = RFE_SEM_pred_original*C_unit

                # fix submission which seems to be in kJ/mol
                if submission.file_name in ["pka-nhlbi-1c"]:
                    # correct free energies into kcal/mol
                    # submission seemed to have used C_units = 5.69 for kJ/mol, so can divide by 4.186 to get kcal/mol

                    #sign_error = "yes"
                    #RFE_mean_pred = RFE_mean_pred_original*-1

                    RFE_mean_pred = RFE_mean_pred_original/4.186
                    RFE_SEM_pred = RFE_SEM_pred_original/4.186
                    RFE_model_uncertainty = RFE_model_uncertainty_original/4.186

                #If single transition states are opposite in sign from macro pKa, we assume they made a sign error
                if submission.file_name in [ "pKa-VA-2-charge-correction", "pka-nhlbi-1c", "pKa_RodriguezPaluch_SMD_1","pKa_RodriguezPaluch_SMD_2", "pKa_RodriguezPaluch_SMD_3"]:
                    RFE_mean_pred = RFE_mean_pred*-1 #fix sign error
                    submission.data.loc[mol_ID, "relative free energy"] = RFE_mean_pred
                    sign_error = "yes"

                    data.append({
                        'method name': submission.method_name,
                        'file name': submission.file_name,
                        'reference state': ref_state,
                        'ID tag': mol_ID,
                        'total charge': total_charge,
                        'sign correction?': sign_error,
                        'Relative microstate free energy prediction': RFE_mean_pred,
                        'Relative microstate free energy SEM': RFE_SEM_pred,
                        'model uncertainty': RFE_model_uncertainty,
                        'Relative microstate free energy prediction (original)': RFE_mean_pred_original,
                        'Relative microstate free energy SEM (original)': RFE_SEM_pred_original,
                        'model uncertainty (original)': RFE_model_uncertainty_original
                    })
                else:
                    sign_error = "no"
                    data.append({
                        'method name': submission.method_name,
                        'file name': submission.file_name,
                        'reference state': ref_state,
                        'ID tag': mol_ID ,
                        'total charge': total_charge,
                        'sign correction?': sign_error,
                        'Relative microstate free energy prediction': RFE_mean_pred_original,
                        'Relative microstate free energy SEM': RFE_SEM_pred_original,
                        'model uncertainty': RFE_model_uncertainty_original
                    })

        # Transform into Pandas DataFrame.
        self.data = pd.DataFrame(data=data)
        self.output_directory_path = output_directory_path

        print("\n SubmissionCollection: \n")
        print(self.data)

        # Create general output directory.
        os.makedirs(self.output_directory_path, exist_ok=True)

        # Save collection.data dataframe in a CSV file.
        self.data.to_csv(pKa_submission_collection_file_path)

# =============================================================================
# MAIN
# =============================================================================

if __name__ == '__main__':

    sns.set_style('whitegrid')
    sns.set_context('paper')

    # Import user map.
    with open(USER_MAP_FILE_PATH, 'r') as f:
        user_map = pd.read_csv(f)

    def read_collection_file(collection_file_path):
        """
        Function to read SAMPL6 collection CSV file that was created by logPubmissionCollection.
        :param collection_file_path
        :return: Pandas DataFrame
        """

        # Check if submission collection file already exists.
        if os.path.isfile(collection_file_path):
            print("Analysis will be done using the existing collection file: {}".format(collection_file_path))

            collection_df = pd.read_csv(collection_file_path, index_col=0)
            #print("\n SubmissionCollection: \n")
            #print(collection_df)
        else:
            raise Exception("Collection file doesn't exist: {}".format(collection_file_path))

        return collection_df

    def append_value(dict_obj, key, value):
        # Check if key exist in dict or not
        if key in dict_obj:
            # Key exist in dict.
            # Check if type of value of key is list or not
            if not isinstance(dict_obj[key], list):
                # If type is not list then make it list
                dict_obj[key] = [dict_obj[key]]
            # Append the value in list
            dict_obj[key].append(value)
        else:
            # As key is not in dict,
            # so, add key-value pair
            dict_obj[key] = value

    def ridge_plot(df, by, column, figsize, colormap, output_directory_path, fig_name):
        print("Making ridge plot")
        plt.close('all')
        plt.rcParams['axes.labelsize'] = 12
        plt.rcParams['xtick.labelsize'] = 12
        plt.rcParams['ytick.labelsize'] = 12
        plt.rcParams['axes.labelsize'] = 14
        #plt.rcParams['figure.autolayout'] = True
        #plt.tight_layout()

        # Make ridge plot
        #print(df)
        fig, axes = joypy.joyplot(data=df, by=by, column=column, figsize=figsize, colormap=colormap, linewidth=1)
        # Add x-axis label
        axes[-1].set_xlabel(column)

        plt.savefig(output_directory_path + "/" + fig_name+".pdf")

    def ridge_plot_wo_overlap(df, by, column, figsize, colormap, output_directory_path):
        plt.close('all')
        plt.rcParams['axes.labelsize'] = 14
        plt.rcParams['xtick.labelsize'] = 14
        plt.rcParams['figure.autolayout'] = True
        plt.tight_layout()

        # Make ridge plot
        fig, axes = joypy.joyplot(data=df, by=by, column=column, figsize=figsize, colormap=colormap, linewidth=1, overlap=0)
        # Add x-axis label
        axes[-1].set_xlabel(column)
        plt.savefig(output_directory_path + "/" + "ridgeplot.pdf")

    def violinplot(df, output_directory_path, width, fig_name):
        print("Making horizontal violin plot")

        plt.close('all')
        data_ordered_by_mol_ID = df.sort_values(["ID tag"], ascending=["True"])

        sns.set(rc={'figure.figsize': (8.27,11.7)})
        sns.violinplot(y="ID tag", x='Relative microstate free energy prediction',
                       data=data_ordered_by_mol_ID, inner='point',
                       linewidth=0.5, width=width)
        plt.tight_layout()
        #plt.savefig(output_directory_path + "/" + "violinplot.pdf")
        plt.savefig(output_directory_path + "/" + fig_name+"_horizontal.pdf")

        print("Making vertical violin plot")

        plt.close('all')
        data_ordered_by_mol_ID = df.sort_values(["ID tag"], ascending=["True"])

        sns.set(rc={'figure.figsize': (12,8)})
        v = sns.violinplot(x="ID tag",
                           y='Relative microstate free energy prediction',
                           data=data_ordered_by_mol_ID, inner='point',
                           linewidth=0.5, width=width)

        v.set_xticklabels(v.get_xticklabels(),rotation=90)
        plt.tight_layout()
        #plt.savefig(output_directory_path + "/" + "violinplot.pdf")
        plt.savefig(output_directory_path + "/" + fig_name+".pdf")

    def barplot(df, output_directory_path, figsize, fig_name):
        print("Making bar plot")
        plt.close('all')
        current_palette = sns.color_palette()
        sns_color = current_palette[2]

        plt.style.use(["seaborn-talk", "seaborn-whitegrid"])
        plt.rcParams['axes.labelsize'] = 20
        plt.rcParams['xtick.labelsize'] = 14
        plt.rcParams['ytick.labelsize'] = 18
        plt.rcParams['legend.fontsize'] = 16
        plt.rcParams['legend.handlelength']
        plt.rcParams['figure.autolayout'] = True

        plt.figure(figsize=figsize)

        x = range(len(df['average FE prediction']))
        y = df['average FE prediction']

        plt.bar(x, y)
        plt.xticks(x, df['ID tag'], rotation=90)#, horizontalalignment='right')
        #plt.errorbar(x, y, yerr=df['average SEM'], fmt="none", ecolor=sns_color, capsize=3, capthick=True, label='SEM', alpha=0.75)
        plt.errorbar(x, y, yerr=df['prediction STD'], fmt="none", ecolor=sns_color, capsize=3, capthick=True, label='STDEV', alpha=0.75)

        plt.xlabel("Microstates")
        plt.ylabel("Average relative microstate free energy")
        plt.savefig(output_directory_path + "/" + fig_name+".pdf")

    # ======================================================================================================================
    #
    # ======================================================================================================================

    output_directory_path = "./plots"
    pKa_submission_collection_file_path = "{}/relative_microstate_FE_submissions.csv".format(output_directory_path)
    os.makedirs(output_directory_path, exist_ok=True)

    # Load submissions data.
    submissions_RFE = load_submissions(pKa_SUBMISSIONS_DIR_PATH, user_map)

    collection_logP = pKaSubmissionCollection(submissions_RFE, output_directory_path, pKa_submission_collection_file_path, no_outliers = False)
    print(collection_logP.data)
    collection_data = read_collection_file(collection_file_path = pKa_submission_collection_file_path)

    # Ridge plot using all predictions
    #ridge_plot(df = collection_logP.data, by = "ID tag", column = "Relative microstate free energy prediction",
    #            figsize = (5, 8), colormap=cm.plasma,
    #            output_directory_path=output_directory_path, fig_name="ridgeplot_all_FE_predictions")

    # Violin plot using all predictions
    #violinplot(df = collection_data, output_directory_path=output_directory_path, width=5,fig_name="violinplot_all_FE_predictions" )

    #df = collection_logP.data
    #df2=df.groupby("ID tag")["Relative microstate free energy prediction"].agg(['count','mean', 'min', 'max','std'])
    #df3=df.groupby("ID tag")["Relative microstate free energy SEM"].agg(['mean'])
    #df4=pd.merge(df2, df3, left_index=True, right_index=True)
    #df4.columns = ['prediction count', 'average FE prediction', 'min prediction', 'max prediction', 'prediction STD', 'average SEM']
    #df4['ID tag'] = df4.index
    #df4.reset_index(level=0, inplace=True)
    #df4.to_csv(output_directory_path+"/numbers.csv")

    # Barplot of average FE predictions
    #barplot(df=df4, output_directory_path=output_directory_path, figsize=(28,10), fig_name="barplot_average_FE_predictions")

    '''# Repeat without outlier
    output_directory_path = "./plots"
    pKa_submission_collection_file_path = "{}/relative_microstate_FE_submissions_no_outlier.csv".format(output_directory_path)

    os.makedirs(output_directory_path, exist_ok=True)

    # Load submissions data.
    submissions_RFE = load_submissions(pKa_SUBMISSIONS_DIR_PATH, user_map)

    collection_logP_no_outllier = pKaSubmissionCollection(submissions_RFE, output_directory_path, pKa_submission_collection_file_path, no_outliers = True)
    collection_data_no_outlier = read_collection_file(collection_file_path = pKa_submission_collection_file_path)

    # Ridge plot using all predictions
    ridge_plot(df = collection_logP_no_outllier.data, by = "ID tag", column = "Relative microstate free energy prediction",
                figsize = (5, 8),
               colormap=cm.plasma, output_directory_path=output_directory_path, fig_name="ridgeplot_all_FE_predictions_no_outlier")

    # Violin plot using all predictions
    violinplot(df = collection_data_no_outlier, output_directory_path=output_directory_path, width=5, fig_name="violinplot_all_FE_predictions_no_outlier")

    df = collection_logP_no_outllier.data
    df2=df.groupby("ID tag")["Relative microstate free energy prediction"].agg(['count','mean', 'min', 'max','std'])
    df3=df.groupby("ID tag")["Relative microstate free energy SEM"].agg(['mean'])
    df4=pd.merge(df2, df3, left_index=True, right_index=True)
    df4.columns = ['prediction count', 'average FE prediction', 'min prediction', 'max prediction', 'prediction STD', 'average SEM']
    #df4['ID tag'] = df4.index
    df4.reset_index(level=0, inplace=True)
    df4.to_csv(output_directory_path+"/numbers_no_outlier.csv")

    # Barplot of average FE predictions
    barplot(df=df4, output_directory_path=output_directory_path, figsize=(28,10), fig_name="barplot_average_FE_predictions_no_outlier")
'''


text:
"""
This script will use the 2D box from the label rather than from YOLO,
but will still use the neural nets to get the 3D position and plot onto the
image. Press space for next image and escape to quit
"""
import os
import sys
import time
import argparse
import numpy as np
import cv2

import torch
from torch_lib.Dataset import Dataset
from torch_lib import Model, ClassAverages
from torchvision.models import vgg
from library.Math import calc_location
from library.Plotting import plot_2d_box, plot_3d_box

# to run car by car
SINGLECAR = False

PARSER = argparse.ArgumentParser()

PARSER.add_argument("--dataset-path", default="eval/",
                    help="Path to directory with dataset")

PARSER.add_argument("--calib-path", default="camera_cal/calib_cam_to_cam.txt",
                    help="Path file with calibrating data for camera")

PARSER.add_argument("--weights-path", default="weights/",
                    help="Path to folder, where weights will be saved. \
                        By default, this is weights/")

PARSER.add_argument("--imwrite", action="store_true",
                    help="Flag for running the code in the mode of saving images to a folder. \
                        If this flag is used, the files are saved in output_dir. \
                        By default, images are displayed using cv2.imshow.")

PARSER.add_argument("--output-dir", default="output_dir/",
                    help="If the imwrite flag is True, the images will be saved to this directory. \
                        By default, this is output_dir/")

PARSER.add_argument("--device", default="cuda",
                    help="PyTorch device: cuda/cpu")

def plot_regressed_3d_bbox(img, truth_img, cam_to_img, box_2d, dimensions, alpha, theta_ray):
    """ plot regressed 3d box """
    # the math! returns X, the corners used for constraint
    location, _ = calc_location(
        dimensions, cam_to_img, box_2d, alpha, theta_ray)

    orient = alpha + theta_ray

    plot_2d_box(truth_img, box_2d)
    plot_3d_box(img, cam_to_img, orient, dimensions, location)  # 3d boxes

    return location

def main():
    """ main """
    flags = PARSER.parse_args()

    device = flags.device

    if flags.imwrite:
        os.makedirs(flags.output_dir, exist_ok=True)

    weights_path = flags.weights_path
    model_lst = [x for x in sorted(os.listdir(weights_path)) if x.endswith('.pkl')]
    if len(model_lst) == 0:
        print('No previous model found, please train first!')
        sys.exit()
    else:
        print('Using previous model %s' % model_lst[-1])
        my_vgg = vgg.vgg19_bn(pretrained=True)
        model = Model.Model(features=my_vgg.features, bins=2).to(device)
        checkpoint = torch.load(os.path.join(weights_path, model_lst[-1]), map_location=torch.device(device))
        model.load_state_dict(checkpoint['model_state_dict'])
        model.eval()

    # defaults to /eval
    dataset = Dataset(flags.dataset_path, flags.calib_path)
    averages = ClassAverages.ClassAverages()

    all_images = dataset.all_objects()
    for key in sorted(all_images.keys()):

        start_time = time.time()

        data = all_images[key]

        truth_img = data['Image']
        img = np.copy(truth_img)
        objects = data['Objects']
        cam_to_img = data['Calib']

        for i, detected_obj in enumerate(objects):
            label = detected_obj.label
            theta_ray = detected_obj.theta_ray
            input_img = detected_obj.img

            input_tensor = torch.zeros([1, 3, 224, 224]).to(device)
            input_tensor[0, :, :, :] = input_img
            input_tensor.to(device)

            [orient, conf, dim] = model(input_tensor)
            orient = orient.cpu().data.numpy()[0, :, :]
            conf = conf.cpu().data.numpy()[0, :]
            dim = dim.cpu().data.numpy()[0, :]

            dim += averages.get_item(label['Class'])

            argmax = np.argmax(conf)
            orient = orient[argmax, :]
            cos = orient[0]
            sin = orient[1]
            alpha = np.arctan2(sin, cos)
            alpha += dataset.angle_bins[argmax]
            alpha -= np.pi

            location = plot_regressed_3d_bbox(\
                img, truth_img, cam_to_img, label['Box_2D'], dim, alpha, theta_ray)

            print('Estimated pose: %s' % location)
            print('Truth pose: %s' % label['Location'])
            print('-------------')

            # plot car by car
            if not SINGLECAR:
                continue
            numpy_vertical = np.concatenate((truth_img, img), axis=0)
            if not flags.imwrite:
                cv2.imshow('2D detection on top, 3D prediction on bottom', numpy_vertical)
                cv2.waitKey(1)
            else:
                cv2.imwrite(os.path.join(flags.output_dir, f'im_sc_{key}_{i}.jpg'), numpy_vertical)

        print('Got %s poses in %.3f seconds\n' % (len(objects), time.time() - start_time))

        # plot image by image
        if SINGLECAR:
            continue
        numpy_vertical = np.concatenate((truth_img, img), axis=0)
        if not flags.imwrite:
            cv2.imshow('2D detection on top, 3D prediction on bottom', numpy_vertical)
            cv2.waitKey(1)
        else:
            cv2.imwrite(os.path.join(flags.output_dir, f'im_sc_{key}.jpg'), numpy_vertical)

if __name__ == '__main__':
    main()


text:
# -*- coding: utf-8 -*-
"""
Beginner Script 4 - Imports, Pandas, and Matplotlib

In this script, we'll learn to import third party libraries, read an excel sheet,
plot values to a graph, and write to an excel sheet.

1.) We include other libraries that people have built by using the special
keyword "import".  At the beginning of the setup, you were asked to run the
command 'conda install pandas matplotlib ...'.  This command installed the
libraries on your computer so that they can be imported.
"""

import pandas as pd # here we are importing pandas and renaming it to 'pd'
import numpy as np

def plotDataFrame():
    """
    2.) A dictionary is a special python object that allows you to map a key to a
    list of values.  You can then later look up the list of values through this key.
    """
    my_dict = {
            "Numbers": [4, 1, 5, 2, 3],
            "Names": np.random.rand(5)  # just creating a list of 5 random numbers
        }

    """
    3.) A dictionary is a special python object that allows you to map a key to a
    list of values.  You can then later look up the list of values through this key.

    The DataFrame is like a excel sheet
    """
    my_df = pd.DataFrame(my_dict)

    """
    4.) Data Frames are great because they have useful functions you can use to
    interact with your dataset like 'sort' and 'plot'

    DataFrame.sort_by(['column_1', 'column_2'], ascending=[1,0])
    DataFrame.plot(style=['o','rx'], title = '...')
    """
    # sort ... (sort wasn't working, so we're going to skip this.)

    # then plot
    my_df.plot(style=['o','rx'], title = 'Karls Numbers')

plotDataFrame()

def plotExcel():
    """
    4.) DataFrames also have built in Excel functionality, like creating a file
    and reading from one.

    DataFrame.to_excel('file_name.xlsx', sheet_name='sheet_name', index=False)
    DataFrame.read_excel('file_name.xlsx')
    """
    # create a dictionary of numbers

    # create a DataFrame from the dictionary

    # write to the file

    # read from the file

    # plot the results

    """
    Your assignment is to update the function above to
        - accepts an Excel file name as a parameter,
        - reads the Excel file,
        - and plot a specific column.
        - Test it using an Excel file you use on a regular basis!
    """

max_stars_repo_name: havkarl/python-tutorial

max_issues_repo_name: havkarl/python-tutorial

max_forks_repo_name: havkarl/python-tutorial


text:
import os
import unittest
from assimulo.solvers.sundials import CVodeError
from sympy import Symbol, MutableDenseMatrix, Float
from means import TrajectoryCollection, SolverException
from means.core import ODEProblem, Moment, VarianceTerm
from means.inference import Inference
from means.inference.results import InferenceResult, NormalConvergenceStatus
from means.io.serialise import dump, load
from means.examples.sample_models import MODEL_P53, MODEL_MICHAELIS_MENTEN, MODEL_LOTKA_VOLTERRA, \
                                         MODEL_HES1, MODEL_DIMERISATION
from means.simulation import Trajectory, TrajectoryWithSensitivityData, SensitivityTerm, Simulation
import numpy as np
from StringIO import StringIO
from tempfile import mkstemp
import cPickle as pickle

def _sample_problem():
    lhs_terms = [Moment(np.array([1, 0, 0]), symbol='y_0'),
                 Moment(np.array([0, 1, 0]), symbol='y_1'),
                 Moment(np.array([0, 0, 1]), symbol='y_2'),
                 Moment(np.array([0, 0, 2]), symbol='yx1'),
                 Moment(np.array([0, 1, 1]), symbol='yx2'),
                 Moment(np.array([0, 2, 0]), symbol='yx3'),
                 Moment(np.array([1, 0, 1]), symbol='yx4'),
                 Moment(np.array([1, 1, 0]), symbol='yx5'),
                 Moment(np.array([2, 0, 0]), symbol='yx6')]
    constants = ['c_0', 'c_1', 'c_2', 'c_3', 'c_4', 'c_5', 'c_6']

    c_0 = Symbol('c_0')
    c_1 = Symbol('c_1')
    y_0 = Symbol('y_0')
    c_2 = Symbol('c_2')
    y_2 = Symbol('y_2')
    c_6 = Symbol('c_6')
    yx4 = Symbol('yx4')
    yx6 = Symbol('yx6')
    c_3 = Symbol('c_3')
    c_4 = Symbol('c_4')
    y_1 = Symbol('y_1')
    c_5 = Symbol('c_5')
    yx2 = Symbol('yx2')
    yx1 = Symbol('yx1')
    yx3 = Symbol('yx3')
    yx5 = Symbol('yx5')
    rhs = MutableDenseMatrix([[c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0) + yx4*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0)) + yx6*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)], [c_3*y_0 - c_4*y_1], [c_4*y_1 - c_5*y_2], [2*c_4*y_1*y_2 + c_4*y_1 + 2*c_4*yx2 - 2*c_5*y_2**2 + c_5*y_2 - 2*c_5*yx1 - 2*y_2*(c_4*y_1 - c_5*y_2)], [c_3*y_0*y_2 + c_3*yx4 + c_4*y_1**2 - c_4*y_1*y_2 - c_4*y_1 + c_4*yx3 - c_5*y_1*y_2 - y_1*(c_4*y_1 - c_5*y_2) - y_2*(c_3*y_0 - c_4*y_1) + yx2*(-c_4 - c_5)], [2*c_3*y_0*y_1 + c_3*y_0 + 2*c_3*yx5 - 2*c_4*y_1**2 + c_4*y_1 - 2*c_4*yx3 - 2*y_1*(c_3*y_0 - c_4*y_1)], [c_0*y_2 - c_1*y_0*y_2 - c_2*y_0*y_2**2/(c_6 + y_0) - c_2*y_0*yx1/(c_6 + y_0) + c_4*y_0*y_1 + c_4*yx5 - c_5*y_0*y_2 - y_0*(c_4*y_1 - c_5*y_2) - y_2*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + yx4*(-c_1 + 2*c_2*y_0*y_2/(c_6 + y_0)**2 - 2*c_2*y_2/(c_6 + y_0) - c_5 - y_2*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))) + yx6*(-c_2*y_0*y_2**2/(c_6 + y_0)**3 + c_2*y_2**2/(c_6 + y_0)**2 - y_2*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2))], [c_0*y_1 - c_1*y_0*y_1 - c_2*y_0*y_1*y_2/(c_6 + y_0) - c_2*y_0*yx2/(c_6 + y_0) + c_3*y_0**2 - c_4*y_0*y_1 - y_0*(c_3*y_0 - c_4*y_1) - y_1*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + yx4*(c_2*y_0*y_1/(c_6 + y_0)**2 - c_2*y_1/(c_6 + y_0) - y_1*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))) + yx5*(-c_1 + c_2*y_0*y_2/(c_6 + y_0)**2 - c_2*y_2/(c_6 + y_0) - c_4) + yx6*(-c_2*y_0*y_1*y_2/(c_6 + y_0)**3 + c_2*y_1*y_2/(c_6 + y_0)**2 + c_3 - y_1*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2))], [2*c_0*y_0 + c_0 - 2*c_1*y_0**2 + c_1*y_0 - 2*c_2*y_0**2*y_2/(c_6 + y_0) + c_2*y_0*y_2/(c_6 + y_0) - 2*y_0*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + yx4*(2*c_2*y_0**2/(c_6 + y_0)**2 - 4*c_2*y_0/(c_6 + y_0) - c_2*y_0/(c_6 + y_0)**2 + c_2/(c_6 + y_0) - 2*y_0*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))) + yx6*(-2*c_1 - 2*c_2*y_0**2*y_2/(c_6 + y_0)**3 + 4*c_2*y_0*y_2/(c_6 + y_0)**2 + c_2*y_0*y_2/(c_6 + y_0)**3 - 2*c_2*y_2/(c_6 + y_0) - c_2*y_2/(c_6 + y_0)**2 - 2*y_0*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2))]])

    problem = ODEProblem(method='MEA', left_hand_side_descriptors=lhs_terms, right_hand_side=rhs, parameters=constants)
    return problem

def _sample_inference():

    r = Inference(problem=_sample_problem(),
                      starting_parameters=[1, 2, 3, 4, 5, 6, 7],
                      starting_conditions=[1,2,3],
                      variable_parameters=['c_0', 'c_1'],
                      observed_trajectories=[Trajectory([1,2], [2,3], Moment([1, 0, 0], 'x'))],
                      distance_function_type='gamma',
                      maxh=0.01) # Some simulation kwargs
    return r

class TestSerialisation(unittest.TestCase):

    def _roundtrip(self, object_):
        # Check dump/load works
        self.assertEquals(load(dump(object_)), object_)

    def test_model_serialisation_works(self):
        """
        Given a model object, the serialisation routine should be able to dump that model and recover it when
        the dumped data is loaded
        :return:
        """

        self._roundtrip(MODEL_P53)
        self._roundtrip(MODEL_MICHAELIS_MENTEN)
        self._roundtrip(MODEL_LOTKA_VOLTERRA)
        self._roundtrip(MODEL_HES1)
        self._roundtrip(MODEL_DIMERISATION)

    def test_odeproblem_serialisation_works(self):
        problem = _sample_problem()
        self._roundtrip(problem)
        # Now make sure to access problem.right_hand_side_as_function as this sometimes breaks pickle
        f = problem.right_hand_side_as_function
        # Do roundtrip again
        self._roundtrip(problem)

    def test_ode_problem_lna_serialisation_works(self):

        c_0 = Symbol('c_0')
        c_1 = Symbol('c_1')
        y_0 = Symbol('y_0')
        c_2 = Symbol('c_2')
        y_2 = Symbol('y_2')
        c_6 = Symbol('c_6')
        c_3 = Symbol('c_3')
        c_4 = Symbol('c_4')
        y_1 = Symbol('y_1')
        c_5 = Symbol('c_5')
        V_00 = Symbol('V_00')
        V_02 = Symbol('V_02')
        V_20 = Symbol('V_20')
        V_01 = Symbol('V_01')
        V_21 = Symbol('V_21')
        V_22 = Symbol('V_22')
        V_10 = Symbol('V_10')
        V_12 = Symbol('V_12')
        V_11 = Symbol('V_11')
        right_hand_side = MutableDenseMatrix([[c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)], [c_3*y_0 - c_4*y_1], [c_4*y_1 - c_5*y_2], [2*V_00*(-c_1 + c_2*y_0*y_2/(c_6 + y_0)**2 - c_2*y_2/(c_6 + y_0)) - V_02*c_2*y_0/(c_6 + y_0) - V_20*c_2*y_0/(c_6 + y_0) + c_0**Float('1.0', prec=15) + (c_1*y_0)**Float('1.0', prec=15) + (c_2*y_0*y_2/(c_6 + y_0))**Float('1.0', prec=15)], [V_00*c_3 - V_01*c_4 + V_01*(-c_1 + c_2*y_0*y_2/(c_6 + y_0)**2 - c_2*y_2/(c_6 + y_0)) - V_21*c_2*y_0/(c_6 + y_0)], [V_01*c_4 - V_02*c_5 + V_02*(-c_1 + c_2*y_0*y_2/(c_6 + y_0)**2 - c_2*y_2/(c_6 + y_0)) - V_22*c_2*y_0/(c_6 + y_0)], [V_00*c_3 - V_10*c_4 + V_10*(-c_1 + c_2*y_0*y_2/(c_6 + y_0)**2 - c_2*y_2/(c_6 + y_0)) - V_12*c_2*y_0/(c_6 + y_0)], [V_01*c_3 + V_10*c_3 - 2*V_11*c_4 + (c_3*y_0)**Float('1.0', prec=15) + (c_4*y_1)**Float('1.0', prec=15)], [V_02*c_3 + V_11*c_4 - V_12*c_4 - V_12*c_5 - (c_4*y_1)**Float('1.0', prec=15)], [V_10*c_4 - V_20*c_5 + V_20*(-c_1 + c_2*y_0*y_2/(c_6 + y_0)**2 - c_2*y_2/(c_6 + y_0)) - V_22*c_2*y_0/(c_6 + y_0)], [V_11*c_4 + V_20*c_3 - V_21*c_4 - V_21*c_5 - (c_4*y_1)**Float('1.0', prec=15)], [V_12*c_4 + V_21*c_4 - 2*V_22*c_5 + (c_4*y_1)**Float('1.0', prec=15) + (c_5*y_2)**Float('1.0', prec=15)]])

        ode_lhs_terms = [Moment(np.array([1, 0, 0]), symbol=y_0),
                         Moment(np.array([0, 1, 0]), symbol=y_1),
                         Moment(np.array([0, 0, 1]), symbol=y_2),
                         VarianceTerm((0, 0), V_00),
                         VarianceTerm((0, 1), V_01),
                         VarianceTerm((0, 2), V_02),
                         VarianceTerm((1, 0), V_10),
                         VarianceTerm((1, 1), V_11),
                         VarianceTerm((1, 2), V_12),
                         VarianceTerm((2, 0), V_20),
                         VarianceTerm((2, 1), V_21),
                         VarianceTerm((2, 2), V_22)]

        constants = ['c_0', 'c_1', 'c_2', 'c_3', 'c_4', 'c_5', 'c_6']

        problem = ODEProblem('LNA', ode_lhs_terms, right_hand_side, constants)
        self._roundtrip(problem)

        # Now make sure to access problem.right_hand_side_as_function as this sometimes breaks pickle
        f = problem.right_hand_side_as_function
        # Do roundtrip again
        self._roundtrip(problem)

    def test_trajectory_serialisation(self):
        t = Trajectory([1, 2, 3], [3, 2, 1], Moment([1,2,3], 'x'))
        self._roundtrip(t)

    def test_trajectory_collection_serialisation(self):

        t1 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1,2,3], 'x'))
        t2 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1,0,0], 'y'))
        tc = TrajectoryCollection([t1, t2])
        self._roundtrip(tc)

    def test_trajectory_with_sensitivities_serialisation(self):
        term = Moment([1, 0, 0], 'x')
        x = Trajectory([1, 2, 3], [3, 2, 1], SensitivityTerm(term, 'x'))
        y = Trajectory([1, 2, 3], [7, 8, 9], SensitivityTerm(term, 'y'))
        t = TrajectoryWithSensitivityData([1, 2, 3], [-1, -2, -3], term, sensitivity_data=[x, y])
        self._roundtrip(t)

    def test_inference_result_serialisation(self):
        r = InferenceResult(inference=_sample_inference(),
                            optimal_parameters=[15, 16, -17, 18, 19, 20],
                            optimal_initial_conditions=[-15, -16, -18],
                            distance_at_minimum=15.8,
                            convergence_status=NormalConvergenceStatus(None, 100, 180),
                            solutions=[[([1, 2, 3, 4, 5, 6], [3, 2, 1]), ([3, 2, 1, 0, -1, -2], [1, 2, 3])]],
                            distance_landscape=[([1, 2, 3, 4, 5, 6], [3, 2, 1], 15.4), ([3, 2, 1, 0, -1, -2], [1, 2, 3], 10),
                                                 ([3, 2, 1, 0, -1, -2], [1, 2, 3], 3.4)]
                            )
        self._roundtrip(r)

    def test_inference_serialisation(self):

        r = _sample_inference()
        self._roundtrip(r)

class TestSerialisationStringIO(TestSerialisation):
    def _roundtrip(self, object_):
        s = StringIO()
        try:
            object_.to_file(s)

            # Reset the file pointer so we can read from it
            s.seek(0)

            new_object = object_.__class__.from_file(s)
            self.assertEqual(object_, new_object)
        finally:
            s.close()

    def test_deserialisation_of_wrong_class_fails(self):
        """
        Given a file where a model object has been serialised, assert that
        deserialisation of that file into a trajectory object fails with ValueError
        :return:
        """

        s = StringIO()
        try:
            MODEL_P53.to_file(s)
            self.assertRaises(ValueError, Trajectory.from_file, s)
        finally:
            s.close()

class TestSerialisationFileIO(TestSerialisation):
    def _roundtrip(self, object_):
        __, tmp_file = mkstemp()
        try:
            object_.to_file(tmp_file)
            new_object = object_.__class__.from_file(tmp_file)
            self.assertEqual(object_, new_object)
        finally:
            os.unlink(tmp_file)

    def test_deserialisation_of_wrong_class_fails(self):
        """
        Given a file where a model object has been serialised, assert that
        deserialisation of that file into a trajectory object fails with ValueError
        :return:
        """

        __, tmp_file = mkstemp()
        try:
            MODEL_P53.to_file(tmp_file)
            self.assertRaises(ValueError, Trajectory.from_file, tmp_file)
        finally:
            os.unlink(tmp_file)

class TestPickleSerialisation(TestSerialisation):
    """
    We need make sure our classes are serialisable using pickle as well, as this is python default and much more
    compressed of a file format, than the human readable yaml format
    """

    def _roundtrip(self, object_):
        pickle_dump = pickle.dumps(object_, pickle.HIGHEST_PROTOCOL)
        new_object = pickle.loads(pickle_dump)

        self.assertEqual(object_, new_object)

    def test_solver_exception(self):

        e = SolverException(None, CVodeError('Test', 14.9))
        self._roundtrip(e)

max_stars_repo_name: nicktimko/means

max_issues_repo_name: nicktimko/means

max_forks_repo_name: nicktimko/means

reason: import numpy,from sympy


text:
from neurospyke.query import Query
from neurospyke.response import Response
from neurospyke.sweep import Sweep
from neurospyke.utils import load_cells
import numpy as np
import pandas as pd

# load cells
data_dir_path = "tests/data/initial_examples/*.mat"
cells = load_cells(data_dir_path)

# run query, select cells to exampe
query1 = Query(cells, response_criteria = [('curr_duration',.3), ('sweep_time', '>0')])
query1.run()
cell1 = query1.cells[0]; cell2 = query1.cells[1]

# response with 5 APs in response to a current injection
# cell_name = 010417-1, sweep_idx = 16
sweep_obj_5AP = Sweep(cell1.sweep_df(16), cell=cell1)
response_obj_5AP = Response(sweep_obj_5AP.current_inj_waveforms()[0], sweep_obj_5AP)

# response with -400 pA current injection for sag/rebound
# cell_name = 010417-1, sweep_idx = 23
sweep_obj_rebound = Sweep(cell1.sweep_df(23), cell=cell1)
response_obj_rebound = Response(sweep_obj_rebound.current_inj_waveforms()[0], sweep_obj_rebound)

# response with both positive and negative current injections
# cell_name = 041015A, sweep_idx = 0
sweep_obj_2inj = Sweep(cell2.sweep_df(0), cell=cell2)
response_obj_2inj = Response(sweep_obj_2inj.current_inj_waveforms()[0], sweep_obj_2inj)

def test_criteria_priority():
    print(f"\n\n\t\tSTART TESTS")
    criteria = response_obj_5AP.criteria_priority()
    assert 'sweep_time' in criteria[0]

def test_meets_criterion():
    assert response_obj_5AP.meets_criterion(('curr_amplitude', '>0'))
    assert not response_obj_5AP.meets_criterion(('curr_amplitude', -400))
    try:
        response_obj_2inj.calc_or_read_from_cache('invalid_name')
        assert False
    except Exception as e:
        assert "calc_invalid_name" in str(e)

def test_calc_properties():
    assert isinstance(response_obj_2inj.calc_properties(['num_spikes']), dict)
    assert response_obj_2inj.calc_properties(['num_spikes'])['num_spikes'] == 4

def test_run():
    results_df = response_obj_5AP.run()
    assert isinstance(results_df, pd.DataFrame)
    assert results_df.shape[0] == 1

def test_calc_or_read_from_cache():
    criteria = ['num_spikes']
    for criterion in criteria:
        response_obj_2inj.calc_or_read_from_cache(criterion)
        assert criterion in response_obj_2inj._cache

def test_window():
    window_df = response_obj_rebound.window(left_window=100, right_window=130)
    assert isinstance(window_df, pd.DataFrame)
    assert 'time', 'data'  in window_df.columns
    assert 'commands', 'sweep_time'  in window_df.columns
    assert 'sweep_index' in window_df.columns
    assert window_df.index[0] == 0
    assert window_df['time'][0] == 0

def test_too_big_left_window():
    try:
        response_obj_rebound.window(left_window=10000, right_window=0)
        assert False
    except Exception as e:
        assert 'Left window too big' in str(e)

def test_too_big_right_window():
    try:
        response_obj_rebound.window(left_window=0, right_window=10000)
        assert False
    except Exception as e:
        assert 'Right window too big' in str(e)
    print(f"\n\n\t\t BASIC PROPERTIES")

##################################################################################
###############################   BASIC PROPERTIES    ############################
##################################################################################

def test_calc_curr_duration():
    curr_duration = response_obj_5AP.calc_curr_duration()
    assert np.isclose(curr_duration, .3)

def test_calc_points_per_ms():
    points_per_ms = response_obj_2inj.calc_points_per_ms()
    assert points_per_ms == 20
    print(f"\n\n\t\tSPIKE PROPERTIES")

##################################################################################
###############################   SPIKE PROPERTIES    ############################
##################################################################################

def test_calc_spike_points():
    spike_points = response_obj_2inj.calc_spike_points()
    assert {2968,3465,5174,7308}.issubset(spike_points)

def test_calc_num_spikes():
    assert response_obj_5AP.calc_num_spikes() == 5
    assert response_obj_rebound.calc_num_spikes() == 0

# AP max
def test_calc_APmax_idx_and_val():
    results = response_obj_2inj.calc_APmax_idx_and_val()
    assert {2975, 3473, 5182, 7316}.issubset(results[0])
    assert {47, 42, 44, 43}.issubset(np.around(results[1]))
    results = response_obj_5AP.calc_APmax_idx_and_val()
    assert len(results[0]) == 5

# AHP
def test_calc_AHP_idx_and_val():
    calc_idx, calc_vals = response_obj_5AP.calc_AHP_idx_and_vals()
    known_vals = np.array([-47.7533, -47.8533,-50.1000, -51.2667, np.nan])
    assert np.allclose(calc_vals, known_vals, equal_nan=True)
    known_idx = np.array([2863, 3528, 4342, 5810, np.nan])
    assert np.allclose(calc_idx, known_idx, equal_nan=True)

# Threshold
def test_calc_threshold_and_vals():
    calc_idxs, calc_vals = response_obj_5AP.calc_threshold_idx_and_vals()
    known_idxs = np.array([2796, 3091, 4050, 5328, 6828])
    known_vals = np.array([-43.5533, -41.2133, -42.3800, -42.7733, -42.3800])
    assert np.allclose(calc_idxs, known_idxs)
    assert np.allclose(calc_vals, known_vals)

# AHP compared to threshold
def test_AHP_vs_threshold():
    threshold_vals = response_obj_5AP.calc_threshold_vals()
    AHP_vals = response_obj_5AP.calc_AHP_vals()
    known_vals = AHP_vals - threshold_vals
    calc_vals = response_obj_5AP.calc_AHP_vs_threshold()
    assert np.allclose(known_vals, calc_vals, equal_nan=True)

# AP amplitude
def test_AP_amplitudes():
    calc_vals = response_obj_5AP.calc_AP_amplitudes()
    known_vals = np.array([91.7933, 85.3533, 89.0600, 89.0600, 88.3733])
    assert np.allclose(calc_vals, known_vals)

def test_calc_val_pct_APamp():
    calc_vals = response_obj_5AP.calc_val_pct_APamp(percent=20)
    known_vals = np.array([-25.1947, -24.1427, -24.5680, -24.9613, -24.7053])
    assert np.allclose(calc_vals, known_vals)

# AP width
def test_calc_AP_width():
    calc_vals = response_obj_5AP.calc_AP_width(percent=50)
    known_vals = [0.85, 1.4, 1.2, 1.1, 1.1]
    assert np.allclose(calc_vals, known_vals)

# dVdt rising
def test_calc_dVdt_rising_pct_APamplitude():
    calc_vals = response_obj_5AP.calc_dVdt_pct_APamp(percent='20', direction='rising')
    known_vals = [263.6667,265.6000,291.0000,228.5333,243.1333]
    assert np.allclose(calc_vals, known_vals)
    calc_vals = response_obj_5AP.calc_dVdt_pct_APamp(percent='20', direction='falling')
    known_vals = [-54.6667,-28.3333,-33.2000,-38.0667,-38.0667]
    assert np.allclose(calc_vals, known_vals)
    calc_vals = response_obj_5AP.calc_dVdt_pct_APamp(percent='max', direction='rising')
    known_vals = [433.6, 345.73, 372.0667, 379.8667, 377.9333]
    assert np.allclose(calc_vals, known_vals)
    calc_vals = response_obj_5AP.calc_dVdt_pct_APamp(percent='max', direction='falling')
    known_vals = [-81.0667, -52.7333, -56.6667, -59.60000, -59.5333]
    assert np.allclose(calc_vals, known_vals)

def test_calc_dVdt_pct_APamp_last_spike():
    calc_val = response_obj_5AP.calc_dVdt_pct_APamp_last_spike(
            percent=20, direction='rising', num_spikes=5)
    known_val = 243.1333
    assert np.allclose(calc_val, known_val, atol=1e-4)

# delta threshold
def test_calc_delta_thresh():
    calc_vals = response_obj_5AP.calc_delta_thresh()
    known_vals = np.array([0, 2.3400, 1.1733, 0.7800, 1.1733])
    assert np.allclose(calc_vals, known_vals, atol=1e-4)

def test_calc_delta_thresh_last_spike():
    calc_val = response_obj_5AP.calc_delta_thresh_last_spike(5)
    known_val = 1.1733
    assert np.isclose(calc_val, known_val, atol=1e-4)

# ISI
def test_calc_ISI():
    ISIs = response_obj_2inj.calc_ISIs()
    assert {25,85,107}.issubset([round(ISI) for ISI in ISIs])
    assert not {30}.issubset([round(ISI) for ISI in ISIs])
    assert len(ISIs) == 3
    ISIs = response_obj_5AP.calc_ISIs()
    assert len(ISIs) == 4

max_stars_repo_name: RebeccaClarkson/NeuroSpyke

max_issues_repo_name: RebeccaClarkson/NeuroSpyke

max_forks_repo_name: RebeccaClarkson/NeuroSpyke


text:
# AUTOGENERATED! DO NOT EDIT! File to edit: densityRatioSugiyama.ipynb (unless otherwise specified).

__all__ = ['r', 'ulsif', 'clsif', 'eval_basis', 'estimate']

# Cell
import rpy2

import rpy2.robjects as robjects

import os

import numpy as np
import scipy.stats as ss
import matplotlib.pyplot as plt

r = robjects.r

import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()

for f in os.listdir("R/densityratiosugiyama/R"):
    r.source("R/densityratiosugiyama/R/{}".format(f))

ulsif = robjects.globalenv["ulsif"]

clsif = robjects.globalenv["clsif"]

eval_basis = robjects.globalenv["eval.basis"]

# Cell
def estimate(x,y,lambda_=1):
    x0 = x[~y]
    x1 = x[y]
    m0 = r.matrix(x0.T,nrow=x0.shape[1],ncol=x0.shape[0])
    m1 = r.matrix(x1.T,nrow=x1.shape[1],ncol=x1.shape[0])
    results = ulsif(m1,m0,lambda_)
    ratios = np.array(eval_basis(results,
                          r.matrix(x.T,
                                   nrow=x.shape[1],
                                   ncol=x.shape[0])))
    return ratios

max_stars_repo_name: Dzeiberg/multiinstance

max_issues_repo_name: Dzeiberg/multiinstance

max_forks_repo_name: Dzeiberg/multiinstance

reason: import numpy,import scipy


text:
import numpy as np
import argparse
import cv2

# fetching the arguments and save in dictionary
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Enter path to the image")
args = vars(ap.parse_args())

# loading and converting the image into numpy array
image = cv2.imread(args["image"])
#cv2.imshow("Original", image)
#cv2.waitKey(0)

#split the image based on channels
(B, G, R) = cv2.split(image)

cv2.imshow("Red", R)
cv2.imshow("Green", G)
cv2.imshow("Blue", B)
cv2.waitKey(0)

#alternate method
zeros = np.zeros(image.shape[:2], dtype = "uint8")
cv2.imshow("Red", cv2.merge([zeros, zeros, R]))
cv2.imshow("Green", cv2.merge([zeros, G, zeros]))
cv2.imshow("Blue", cv2.merge([B, zeros, zeros]))
cv2.waitKey(0)

#merge back the channels
merged = cv2.merge([B, G, R])
cv2.imshow("merged", merged)
cv2.waitKey(0)


text:
#!/usr/bin/env python
###############################################################################
#  - FILE: filter_results.py
#  - DESC:  Filter results
###############################################################################

from __future__ import print_function
import sys
import numpy as np
import cv2 as cv
from PIL import Image
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches

# write to stderr

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# load hmmer

def load_hmmer_time(filename):
    # line counter
    l_cnt = 0
    # field counter
    f_cnt = 0
    # output data
    data = []

    with open(filename, "r") as fp:
        for line in fp:
            # check for comment lines or empty lines
            if (line.startswith("##")):
                continue

            fields = line.split()
            # check that there are a proper number of fields
            if (len(fields) != 7):
                continue

            total_cells = (int(fields[0])+1) * (int(fields[2])+1)
            fields.append(total_cells)

            data.append(fields)
        return data

# append result_id, target_id, query_id and write out

def append_hmmer_time(filename, res_lookup, t_lookup, q_lookup):
    # line counter
    l_cnt = 0
    # field counter
    f_cnt = 0

    with open(filename, "r") as fp:
        for line in fp:
            # check for comment lines or empty lines
            if (line.startswith("##")):
                continue
            # break up line into space/tab separated components
            line = line.rstrip()
            fields = line.split()
            # check that there are a proper number of fields
            if (len(fields) != 7):
                continue

            try:
                t_name = fields[1]
                q_name = fields[3]

                t_id = t_lookup[t_name]
                q_id = q_lookup[q_name]

                # only report hmmer result if it is also in mmseqs results
                if (t_id, q_id) in res_lookup.keys():
                    res_id = res_lookup[(t_id, q_id)]

                    print(f'{res_id} {t_id} {q_id} {line}')

                l_cnt += 1
                if (l_cnt % 100000 == 0):
                    eprint(f'# {l_cnt} entries processed...')

            except:
                print(f'# ERROR occurred on line {l_cnt}: "{line}" ')
                eprint(f'# ERROR occurred on line {l_cnt}: "{line}" ')
            continue
    return

# load lookup dict
def load_lookup(filename):
    # line counter
    l_cnt = 0
    # field counter
    f_cnt = 0
    # output lookup dict
    name_lookup = {}
    id_lookup = {}

    # read file line-by-line
    with open(filename, "r") as fp:
        for line in fp:
            # check for comment or improper format lines
            if (line.startswith("#")):
                continue
            fields = line.split()
            if (len(fields) != 3):
                continue

            id_ = fields[0]
            offset = fields[1]
            name = fields[2]
            name_lookup[id_] = name
            id_lookup[name] = id_
            pass
        pass
    pass
    return name_lookup, id_lookup

# m8+ results lookup dict

def load_result_lookup(filename):
    # line counter
    l_cnt = 0
    # field counter
    f_cnt = 0
    # output lookup dict
    res_lookup = {}

    # read file line-by-line
    with open(filename, "r") as fp:
        for line in fp:
            # check for comment or improper format lines
            if (line.startswith("#")):
                continue
            fields = line.split()
            if (len(fields) != 17):
                continue

            res_id = fields[0]
            t_id = fields[1]
            q_id = fields[2]
            res_lookup[(t_id, q_id)] = res_id
        pass
    pass
    return res_lookup

##############################################################################
###########################         MAIN         #############################
##############################################################################

# get args
if ((len(sys.argv) == 2) and (sys.argv[1] == "--defaults")):
    hmmer_time_fname = "/home/devreckas/Data/hmmer-test-stdout.txt"
    target_lookup_fname = "/home/devreckas/Data/profmark-benchmark/temp/cloud/pmark.hmm.idx"
    query_lookup_fname = "/home/devreckas/Data/profmark-benchmark/temp/cloud/pmark.fa.idx"
    results_m8_fname = "/home/devreckas/Data/profmark-benchmark/temp/cloud/mmseqs_res.m8+"
elif (len(sys.argv) != 5):
    print("Usage: <hmmer_times> <target_lookup> <query_lookup> <results_m8+>")
    exit(0)
else:
    hmmer_time_fname = sys.argv[1]
    target_lookup_fname = sys.argv[2]
    query_lookup_fname = sys.argv[3]
    results_m8_fname = sys.argv[4]
pass

# test print
# print("This println goes to stdout.")
# eprint("This print goes to stderr.")
# exit(0)

# load data
target_name_lookup, target_id_lookup = load_lookup(target_lookup_fname)
query_name_lookup, query_id_lookup = load_lookup(query_lookup_fname)
res_id_lookup = load_result_lookup(results_m8_fname)
# hmmer_data = load_hmmer_time(hmmer_time_fname)

append_hmmer_time(hmmer_time_fname, res_id_lookup,
                  target_id_lookup, query_id_lookup)

# # linear spacing
# x = range( len(mmseqs_plus_data) )

# # sort data by size
# hmmer_data.sort( key=lambda r:int(r[7]) )

# # plot time data against problem size
# fwdbck_time = []
# size = []
# for data in hmmer_data:
# 	fwdbck_time.append( float(data[4]) )
# 	size.append( int(data[7]) )

# plt.scatter( size, fwdbck_time, marker='o', s=2 )
# plt.show()

max_stars_repo_name: TravisWheelerLab/MMOREseqs

max_issues_repo_name: TravisWheelerLab/MMOREseqs

max_forks_repo_name: TravisWheelerLab/MMOREseqs