#encoding: utf-8
from __future__ import print_function
import os
import matplotlib
matplotlib.use('Agg')
from config import config as cfg
from training import get_number_of_steps
from proc.preproc import (describe_data, load_from_text,
                          split_patients_by_patient_ID,
                          get_class_weight)
from proc.gennerator import (random_image_generator as train_gennerator,
                             ImageGeneratorFromPath)
from proc.image import ImageTransformer
from model import Xception_CBAM
from keras.callbacks import ModelCheckpoint, TensorBoard, ReduceLROnPlateau
from training.callback import (MultiGPUCheckpointCallback, HistoryLogger,
                               MultipleClassAUROC)
from utils import makedir
from keras.utils import multi_gpu_model
from keras import backend as K
import tensorflow as tf
import numpy as np
from keras.optimizers import Adam
from metrics import weight_binary_ce

def show_batch_sample(gen, path=None):
    from matplotlib import pyplot as plt
    from skimage.util.montage import montage2d
    batch_x, batch_y = next(gen)
    x = montage2d(np.squeeze(batch_x[:, :, :, 0]))
    fig = plt.figure(figsize=(15, 15))
    plt.imshow(x, cmap='bone')
    plt.axis('off')
    if path:
        plt.savefig(path)
    else:
        plt.show()

def get_callbacks(val_gen, xray14_labels, log_dir, base_model=None):
    auc_cb = MultipleClassAUROC(val_gen, xray14_labels, log_dir,
                                'best_auc_weight.h5', len(val_gen))
    his_cb = HistoryLogger('his', log_dir, plot_item='binary_accuracy')
    weights_path = os.path.join(log_dir, cfg.weights_name)
    if base_model:
        checkpoint = MultiGPUCheckpointCallback(filepath=weights_path, base_model=base_model)
    else:
        checkpoint = ModelCheckpoint(weights_path, monitor='val_loss',
                                     verbose=1, save_best_only=True,
                                     mode='min', save_weights_only=True)
    callbacks = [
        TensorBoard(log_dir=log_dir, batch_size=cfg.batch_size),
        ReduceLROnPlateau(monitor='val_loss', factor=0.1,
                          patience=cfg.patience, verbose=1,
                          mode="min", min_lr=1e-8),
        auc_cb, his_cb, checkpoint
    ]

    return callbacks

def train(exp_name='xray14_wce_06'):
    # train_val_df, test_df, xray14_labels = load_from_text(cfg.data_root)
    train_val_test_df_name = 'train_val_test_df.npy'
    # np.save(train_val_test_df_name, [train_val_df, test_df, xray14_labels])
    train_val_df, test_df, xray14_labels = np.load(train_val_test_df_name)
    train_df, val_df = split_patients_by_patient_ID(train_val_df, 4, cfg.random_seed)
    print('*'*40, 'tain data', '*'*40)
    describe_data(train_df, xray14_labels)
    print('*' * 40, 'val data', '*' * 40)
    describe_data(val_df, xray14_labels)
    train_transformer = ImageTransformer(samplewise_normalization=True,
                                         rotation_range=10,
                                         width_shift_range=0.05,
                                         height_shift_range=0.1,
                                         shear_range=0.1,
                                         zoom_range=[0.8, 1.2],
                                         horizontal_flip=True)
    val_transformer = ImageTransformer(samplewise_normalization=True)
    train_gen = train_gennerator(train_transformer,
                                 train_val_df,
                                 cfg.input_shape[:-1],
                                 xray14_labels,
                                 batch_size=cfg.batch_size,
                                 color_mode='rgb')
    val_gen = ImageGeneratorFromPath(val_transformer,
                                     test_df['path'],
                                     test_df['xray14_vec'],
                                     shuffle=False,
                                     target_size=cfg.input_shape[:-1],
                                     batch_size=cfg.batch_size,
                                     color_mode='rgb')
    cb_gen = ImageGeneratorFromPath(val_transformer,
                                    test_df['path'],
                                    test_df['xray14_vec'],
                                    shuffle=False,
                                    target_size=cfg.input_shape[:-1],
                                    batch_size=cfg.batch_size,
                                    color_mode='rgb')
    log_path = os.path.join(cfg.log_dir, exp_name)
    makedir(log_path)
    trainable = True
    if cfg.n_gpus > 1:
        with tf.device('/cpu:0'):
            base_model = Xception_CBAM(cfg.input_shape, include_top=True,
                                       classes=len(xray14_labels),
                                       pretrain_weights='imagenet',
                                       layer_trainable=trainable)
        parallel_model = multi_gpu_model(base_model, gpus=cfg.n_gpus)
        callbacks = get_callbacks(cb_gen, xray14_labels, log_path, base_model=base_model)
        model = parallel_model
    else:
        model = Xception_CBAM(cfg.input_shape, include_top=True,
                              classes=len(xray14_labels),
                              pretrain_weights='imagenet',
                              layer_trainable=trainable)
        callbacks = get_callbacks(cb_gen, xray14_labels, log_path)
        base_model = model
    class_weights = get_class_weight(train_val_df, xray14_labels)
    print('class weights ---', class_weights)
    model.compile(optimizer=Adam(1e-3), loss=weight_binary_ce(class_weights),
                  metrics=['binary_accuracy', 'mae'])
    train_steps = get_number_of_steps(len(train_val_df), cfg.batch_size)
    val_steps   = get_number_of_steps(len(test_df), cfg.batch_size)
    model.fit_generator(train_gen, epochs=cfg.epochs,
                        steps_per_epoch=train_steps,
                        callbacks=callbacks,
                        validation_data=val_gen,
                        workers=cfg.n_works,
                        max_queue_size=cfg.n_queue,
                        use_multiprocessing=True,
                        validation_steps=val_steps,
                        initial_epoch=0)
    base_model.save_weights(__file__.split('.'[0]) + '.hdf5')

if __name__ == "__main__":
    print(cfg)
    K.set_session(cfg.sess)
    train()


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt

# Import Mask RCNN
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize

# Root directory of the project
ROOT_DIR = os.getcwd()

# Import COCO config
sys.path.append(ROOT_DIR)
import coco

# Directory to save logs and trained model
MODEL_DIR = ROOT_DIR

# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)

class InferenceConfig(coco.CocoConfig):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1
    DETECTION_MIN_CONFIDENCE = 0.6
    #RPN_NMS_THRESHOLD = 0.5

config = InferenceConfig()
config.display()

# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

# Load weights trained on MS-COCO
model.load_weights(COCO_MODEL_PATH, by_name=True)

# COCO Class names
# Index of the class in the list is its ID. For example, to get ID of
# the teddy bear class, use: class_names.index('teddy bear')
class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
               'bus', 'train', 'truck', 'boat', 'traffic light',
               'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
               'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
               'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
               'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
               'kite', 'baseball bat', 'baseball glove', 'skateboard',
               'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
               'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
               'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
               'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
               'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
               'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
               'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
               'teddy bear', 'hair drier', 'toothbrush']
# car: 3
# bus: 6
# truck: 8

# Load a random image from the images folder
#image = skimage.io.imread(os.path.join(ROOT_DIR, 'zz.jpg'))

# Run detection
#results = model.detect([image], verbose=1)

# Visualize results
#r = results[0]
#visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])

import math

def drawImage(image, boxes, masks, class_ids, class_names, scores, show_bbox=False, show_label=False, show_seg=True, show_center=True):
    N = boxes.shape[0] # Number of instances
    if not N:
        print("\n*** No instances to display *** \n")
    else:
        assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0]

    masked_image = image.astype(np.uint32).copy()

    car_count = 0
    for i in range(N):
        class_id = class_ids[i]
        if class_id != 3 and class_id != 6 and class_id != 8:
            continue

        car_count = car_count + 1

        # Bounding Box.
        if not np.any(boxes[i]):
            # Skip this instance. Has no bbox. Likely lost in image cropping.
            continue
        y1, x1, y2, x2 = boxes[i]

        if show_bbox:
            cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 1, cv2.LINE_AA)
            #p = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2, alpha=0.7, linestyle="dashed", edgecolor=color, facecolor='none')

        if show_center:
            cv2.circle(image, (x1 + int(math.fabs(float(x2 - x1))/2), y1 + int(math.fabs(float(y2 - y1))/2)), 4, (0, 0, 255), -1)

        if show_label:
            score = scores[i] if scores is not None else None
            label = class_names[class_id]
            caption = "{} {:.3f}".format(label, score) if score else label

            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(image, caption, (x1, y1 + 8), font, 1, (255, 255, 255), 1, cv2.LINE_AA)
            #ax.text(x1, y1 + 8, caption, color='w', size=11, backgroundcolor="none")

        if show_seg:
            # Mask
            mask = masks[:, :, i]
            masked_image = visualize.apply_mask(masked_image, mask, (0, 0, 0))

            # Mask Polyline
            # Pad to ensure proper polygons for masks that touch image edges.
            padded_mask = np.zeros((mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8)
            padded_mask[1:-1, 1:-1] = mask
            contours = visualize.find_contours(padded_mask, 0.5)
            for verts in contours:
                # Subtract the padding and flip (y, x) to (x, y)
                verts = np.fliplr(verts) - 1
                cv2.polylines(image, np.int32([verts]), True, (0, 255, 255))
                #p = visualize.Polygon(verts, facecolor="none", edgecolor=())
                #ax.add_patch(p)

    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(image, 'CAR: {}'.format(car_count), (5, 32), font, 1, (0, 0, 255), 2, cv2.LINE_AA)
    pass

import numpy as np
import cv2

INPUT_VIDEO_PATH = os.path.join(ROOT_DIR, 'video1.mp4')

cap = cv2.VideoCapture(INPUT_VIDEO_PATH)
VIDEO_WIDTH = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
VIDEO_HEIGHT = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
VIDEO_FRAMES = cap.get(cv2.CAP_PROP_FRAME_COUNT)
VIDEO_FPS = cap.get(cv2.CAP_PROP_FPS)

print('Video Open: ', INPUT_VIDEO_PATH)
print('Video size: ', VIDEO_WIDTH, 'x', VIDEO_HEIGHT)
print('Video frames: ', VIDEO_FRAMES)
print('Video FPS: ', VIDEO_FPS)

FOURCC = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', FOURCC, float(VIDEO_FPS), (int(VIDEO_WIDTH), int(VIDEO_HEIGHT)))

frame_index = 0
SKIP_PREFIX_FRAMES = 0

show_video = True

while (cap.isOpened()):
    ret, frame = cap.read()

    if (frame_index < SKIP_PREFIX_FRAMES):
        frame_index = frame_index + 1
        continue

    results = model.detect([frame], verbose=1)
    print('Detect: ', frame_index, '/', VIDEO_FRAMES)
    frame_index = frame_index + 1

    r = results[0]
    drawImage(frame, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])

    if show_video:
        cv2.imshow('preview', frame)
    out.write(frame)

    key_code = cv2.waitKey(1)
    if key_code & 0xFF == ord('q'):
        break
    elif key_code & 0xFF == ord('s'):
        show_video = True
    elif key_code & 0xFF == ord('h'):
        show_video = False

cap.release()
out.release()
cv2.destroyAllWindows()


from styx_msgs.msg import TrafficLight
import tensorflow as tf
import numpy as np
import datetime

class TLClassifier(object):
    def __init__(self, is_site):

        self.graph = tf.Graph()

        # Threshold about how confident we need to be to trust the classification
        self.threshold = 0.25

        # Choose the right model graph: simulation or site
        if is_site:
            model_graph = r'light_classification/trained_model/ssd_inception_v2_coco_2017_11_17/site/frozen_inference_graph.pb'
        else:
            model_graph = r'light_classification/trained_model/ssd_inception_v2_coco_2017_11_17/sim/frozen_inference_graph.pb'

        with self.graph.as_default():
            graph_def = tf.GraphDef()
            with tf.gfile.GFile(model_graph, 'rb') as file:
                graph_def.ParseFromString(file.read())
                tf.import_graph_def(graph_def, name='')

            self.image_tensor = self.graph.get_tensor_by_name('image_tensor:0')
            self.boxes = self.graph.get_tensor_by_name('detection_boxes:0')
            self.scores = self.graph.get_tensor_by_name('detection_scores:0')
            self.classes = self.graph.get_tensor_by_name('detection_classes:0')
            self.num_detections = self.graph.get_tensor_by_name(
                'num_detections:0')

        self.sess = tf.Session(graph=self.graph)

    def get_classification(self, image):
        """Determines the color of the traffic light in the image

        Args:
            image (cv::Mat): image containing the traffic light

        Returns:
            int: ID of traffic light color (specified in styx_msgs/TrafficLight)

        """
        with self.graph.as_default():
            img_conv = np.expand_dims(image, axis=0)
            start = datetime.datetime.now()
            (boxes, scores, classes, num_detections) = self.sess.run(
                [self.boxes, self.scores, self.classes, self.num_detections],
                feed_dict={self.image_tensor: img_conv})
            end = datetime.datetime.now()
            c = end - start
            # print(c.total_seconds())

        score = np.squeeze(scores)[0]
        clazz = np.squeeze(classes).astype(np.int32)[0]

        # print("scores:", scores)
        # print("classes:", classes)
        # print("num_detections:", num_detections)

        if score > self.threshold:
            if clazz == 1:
                return TrafficLight.GREEN
            elif clazz == 2:
                return TrafficLight.RED
            elif clazz == 3:
                return TrafficLight.YELLOW

        return TrafficLight.UNKNOWN


from typing import List, Tuple, Union, Iterator, Sequence, TextIO
from copy import copy
import contextlib
import math
import tempfile
import re
from pathlib import Path
import subprocess
import numpy as np
from scipy.spatial.distance import squareform, pdist, cdist
from Bio import SeqIO
from Bio.Seq import Seq
from .typed import PathLike
from .tensor import apc

class MSA:
    """Class that represents a multiple sequence alignment."""

    def __init__(
        self,
        sequences: List[Tuple[str, str]],
        seqid_cutoff: float = 0.2,
    ):
        self.headers = [header for header, _ in sequences]
        self.sequences = [seq for _, seq in sequences]
        self._seqlen = len(self.sequences[0])
        assert all(
            len(seq) == self._seqlen for seq in self.sequences
        ), "Seqlen Mismatch!"

        self._depth = len(self.sequences)
        self.seqid_cutoff = seqid_cutoff
        self.is_nucleotide = all(
            re.match(r"(A|C|T|G|U|-)*", seq) for seq in self.sequences
        )

    def __iter__(self) -> Iterator[Tuple[str, str]]:
        return zip(self.headers, self.sequences)

    def select(self, indices: Sequence[int], axis: str = "seqs") -> "MSA":
        assert axis in ("seqs", "positions")
        if axis == "seqs":
            data = [(self.headers[idx], self.sequences[idx]) for idx in indices]
            return self.__class__(data)
        else:
            data = [
                (header, "".join(seq[idx] for idx in indices)) for header, seq in self
            ]
            return self.__class__(data)

    def swap(self, index1: int, index2: int) -> "MSA":
        headers = copy(self.headers)
        sequences = copy(self.sequences)
        headers[index1], headers[index2] = headers[index2], headers[index1]
        sequences[index1], sequences[index2] = sequences[index2], sequences[index1]
        data = list(zip(headers, sequences))
        return self.__class__(data, seqid_cutoff=self.seqid_cutoff)

    def filter_coverage(self, threshold: float, axis: str = "seqs") -> "MSA":
        assert 0 <= threshold <= 1
        assert axis in ("seqs", "positions")
        notgap = self.array != self.gap
        match = notgap.mean(1 if axis == "seqs" else 0)
        indices = np.where(match >= threshold)[0]
        return self.select(indices, axis=axis)

    def hhfilter(
        self,
        seqid: int = 90,
        diff: int = 0,
        cov: int = 0,
        qid: int = 0,
        qsc: float = -20.0,
        binary: str = "hhfilter",
    ) -> "MSA":

        with tempfile.TemporaryDirectory(dir="/dev/shm") as tempdirname:
            tempdir = Path(tempdirname)
            fasta_file = tempdir / "input.fasta"
            fasta_file.write_text(
                "\n".join(f">{i}\n{seq}" for i, seq in enumerate(self.sequences))
            )
            output_file = tempdir / "output.fasta"
            command = " ".join(
                [
                    f"{binary}",
                    f"-i {fasta_file}",
                    "-M a3m",
                    f"-o {output_file}",
                    f"-id {seqid}",
                    f"-diff {diff}",
                    f"-cov {cov}",
                    f"-qid {qid}",
                    f"-qsc {qsc}",
                ]
            ).split(" ")
            result = subprocess.run(command, capture_output=True)
            result.check_returncode()
            with output_file.open() as f:
                indices = [int(line[1:].strip()) for line in f if line.startswith(">")]
            return self.select(indices, axis="seqs")

    def replace_(self, inp: str, rep: str) -> "MSA":
        dtype = self.dtype
        self.dtype = np.dtype("S1")  # type: ignore
        self.array[self.array == inp.encode()] = rep.encode()
        self.dtype = dtype
        return self

    @property
    def gap(self) -> Union[bytes, int]:
        return b"-" if self.dtype == np.dtype("S1") else ord("-")

    def __repr__(self) -> str:
        return f"MSA, L: {self.seqlen}, N: {self.depth}\n" f"{self.array}"

    def __getitem__(self, idx):
        return self.array[idx]

    def pdist(self) -> np.ndarray:
        dtype = self.dtype
        self.dtype = np.uint8
        dist = squareform(pdist(self.array, "hamming"))
        self.dtype = dtype
        return dist

    def greedy_select(self, num_seqs: int, mode: str = "max") -> "MSA":
        assert mode in ("max", "min")
        if self.depth <= num_seqs:
            return self
        dtype = self.dtype
        self.dtype = np.uint8

        optfunc = np.argmax if mode == "max" else np.argmin
        all_indices = np.arange(self.depth)
        indices = [0]
        pairwise_distances = np.zeros((0, self.depth))
        for _ in range(num_seqs - 1):
            dist = cdist(self.array[indices[-1:]], self.array, "hamming")
            pairwise_distances = np.concatenate([pairwise_distances, dist])
            shifted_distance = np.delete(pairwise_distances, indices, axis=1).mean(0)
            shifted_index = optfunc(shifted_distance)
            index = np.delete(all_indices, indices)[shifted_index]
            indices.append(index)
        indices = sorted(indices)
        self.dtype = dtype
        return self.select(indices, axis="seqs")

    def sample_weights(self, num_seqs: int) -> "MSA":
        if self.depth <= num_seqs:
            return self
        weights = self.weights[1:]
        weights = weights / weights.sum()
        indices = (
            np.random.choice(
                self.depth - 1, size=num_seqs - 1, replace=False, p=weights
            )
            + 1
        )
        indices = np.sort(indices)
        indices = np.append(0, indices)
        return self.select(indices, axis="seqs")

    def select_diverse(self, num_seqs: int, method: str = "hhfilter") -> "MSA":
        assert method in ("hhfilter", "sample-weights")
        if num_seqs >= self.depth:
            return self

        if method == "hhfilter":
            msa = self.hhfilter(diff=num_seqs)
            if num_seqs < msa.depth:
                msa = msa.select(np.arange(num_seqs))
        else:
            msa = self.sample_weights(num_seqs)
        return msa

    def invcov(self) -> np.ndarray:
        """given one-hot encoded MSA, return contacts"""
        from sklearn.preprocessing import OneHotEncoder
        dtype = self.dtype
        self.dtype = np.uint8
        Y = OneHotEncoder(drop=[self.gap]).fit_transform(self.array.reshape(-1, 1)).toarray().reshape(self.depth, self.seqlen, -1)
        K = Y.shape[-1]
        Y_flat = Y.reshape(self.depth, -1)
        c = np.cov(Y_flat.T)
        self.dtype = dtype
        return np.linalg.norm(c.reshape(self.seqlen, K, self.seqlen, K), ord=2, axis=(1, 3))
        # shrink = 4.5 / math.sqrt(self.depth) * np.eye(c.shape[0])
        # ic = np.linalg.inv(c + shrink)
        # ic = ic.reshape(self.seqlen, K, self.seqlen, K)
        # return apc(np.sqrt(np.square(ic).sum((1, 3))))

    @property
    def array(self) -> np.ndarray:
        if not hasattr(self, "_array"):
            self._array = np.array([list(seq) for seq in self.sequences], dtype="|S1")
        return self._array

    @property
    def dtype(self) -> type:
        return self.array.dtype

    @dtype.setter
    def dtype(self, value: type) -> None:
        assert value in (np.uint8, np.dtype("S1"))
        self._array = self.array.view(value)

    @property
    def seqlen(self) -> int:
        return self._seqlen

    @property
    def depth(self) -> int:
        return self._depth

    @property
    def seqid_cutoff(self) -> float:
        return self._seqid_cutoff

    @seqid_cutoff.setter
    def seqid_cutoff(self, value: float) -> None:
        assert 0 <= value <= 1
        if getattr(self, "_seqid_cutoff", None) != value:
            with contextlib.suppress(AttributeError):
                delattr(self, "_weights")
            with contextlib.suppress(AttributeError):
                delattr(self, "_neff")
        self._seqid_cutoff = value

    @property
    def is_covered(self) -> np.ndarray:
        if not hasattr(self, "_is_covered"):
            self._is_covered = (self[1:] != self.gap).any(0)
        return self._is_covered

    @property
    def coverage(self) -> float:
        if not hasattr(self, "_coverage"):
            notgap = self.array != self.gap
            self._coverage = notgap.mean(0)
        return self._coverage

    @property
    def weights(self) -> np.ndarray:
        if not hasattr(self, "_weights"):
            self._weights = 1 / (self.pdist() < self.seqid_cutoff).sum(1)
        return self._weights

    @property
    def is_protein(self) -> bool:
        return not self.is_nucleotide

    def neff(self, normalization: Union[float, str] = "none") -> float:
        if isinstance(normalization, str):
            assert normalization in ("none", "sqrt", "seqlen")
            normalization = {
                "none": 1,
                "sqrt": math.sqrt(self.seqlen),
                "seqlen": self.seqlen,
            }[normalization]
        if not hasattr(self, "_neff"):
            self._neff = self.weights.sum()
        return self._neff / normalization

    @classmethod
    def from_stockholm(
        cls,
        stofile: Union[PathLike, TextIO],
        keep_insertions: bool = False,
        **kwargs,
    ) -> "MSA":

        output = []
        valid_indices = None
        for record in SeqIO.parse(stofile, "stockholm"):
            description = record.description
            sequence = str(record.seq)
            if not keep_insertions:
                if valid_indices is None:
                    valid_indices = [i for i, aa in enumerate(sequence) if aa != "-"]
                sequence = "".join(sequence[idx] for idx in valid_indices)
            output.append((description, sequence))
        return cls(output, **kwargs)

    @classmethod
    def from_fasta(
        cls,
        fasfile: Union[PathLike, TextIO],
        keep_insertions: bool = False,
        uppercase: bool = False,
        remove_lowercase_cols: bool = False,
        **kwargs,
    ) -> "MSA":

        output = []
        valid_indices = None
        for record in SeqIO.parse(fasfile, "fasta"):
            description = record.description
            sequence = str(record.seq)
            if remove_lowercase_cols:
                if valid_indices is None:
                    valid_indices = [i for i, aa in enumerate(sequence) if aa.isupper()]
                sequence = "".join(sequence[i] for i in valid_indices)
            if not keep_insertions:
                sequence = re.sub(r"([a-z]|\.|\*)", "", sequence)
            if uppercase:
                sequence = sequence.upper()
            output.append((description, sequence))
        return cls(output, **kwargs)

    @classmethod
    def from_file(
        cls,
        alnfile: PathLike,
        keep_insertions: bool = False,
        **kwargs,
    ) -> "MSA":
        filename = Path(alnfile)
        if filename.suffix == ".sto":
            return cls.from_stockholm(filename, keep_insertions, **kwargs)
        elif filename.suffix in (".fas", ".fasta", ".a3m", ".a2m"):
            return cls.from_fasta(filename, keep_insertions, **kwargs)
        else:
            raise ValueError(f"Unknown file format {filename.suffix}")

    @classmethod
    def from_sequences(cls, sequences: Sequence[str]) -> "MSA":
        return cls([("", seq) for seq in sequences])

    def write(self, outfile: PathLike, form: str = "fasta") -> None:
        SeqIO.write(
            (SeqIO.SeqRecord(Seq(seq), id=header, description="") for header, seq in self),
            outfile,
            form,
        )


import os
import pickle
from numpy.core.fromnumeric import ptp
import torch
import numpy as np
from math import ceil
from model_vc import Generator

from synthesis import build_model
from synthesis import wavegen
import soundfile

import matplotlib.pyplot as plt

def pad_seq(x, base=32):
    len_out = int(base * ceil(float(x.shape[0])/base))
    len_pad = len_out - x.shape[0]
    assert len_pad >= 0
    return np.pad(x, ((0,len_pad),(0,0)), 'constant'), len_pad

device = 'cuda:0'

# ckpt = './pretrained/autovc.ckpt'
ckpt = './ckpt/iter-100K-bs32.pth'

# Generator
G = Generator(32,256,512,32).eval().to(device)
g_checkpoint = torch.load(ckpt, map_location='cuda:0')
G.load_state_dict(g_checkpoint['model'])

# Wavegen
model = build_model().to(device)
checkpoint = torch.load('./pretrained/checkpoint_step001000000_ema.pth', map_location='cuda:0')
model.load_state_dict(checkpoint['state_dict'])

metadata = pickle.load(open('metadata.pkl', 'rb'))
spk_org, emb_org, x_org = metadata[0]
spk_trg, emb_trg, x_trg = metadata[1]

x_org, len_pad = pad_seq(x_org)
uttr_org = torch.from_numpy(x_org[np.newaxis, :, :]).to(device)
emb_org = torch.from_numpy(emb_org[np.newaxis, :]).to(device)
emb_trg = torch.from_numpy(emb_trg[np.newaxis, :]).to(device)
with torch.no_grad():
    _, x_identic_psnt, _ = G(uttr_org, emb_org, emb_trg)
    if len_pad == 0:
        uttr_trg = x_identic_psnt[0, 0, :, :].cpu().numpy()
    else:
        uttr_trg = x_identic_psnt[0, 0, :-len_pad, :].cpu().numpy()

    result = uttr_trg

# print(spkid, emb.shape, x_org.shape)
# uttr_org = uttr_org[0,:-len_pad,:].cpu().numpy()
# plt.imshow()
# plt.show()
# plt.imshow(np.abs(uttr_trg-uttr_org))
# plt.show()
# print(np.min(uttr_org), np.min(uttr_trg))

# uttr_trg = uttr_trg/np.max(uttr_trg)*0.9

# exit()
waveform = wavegen(model, c=uttr_trg)
soundfile.write('./converted/{}_{}@100K_bs32.wav'.format(spk_org, spk_trg), waveform, samplerate=16000)


# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
"""
Train and eval functions used in main.py
"""
import math
import os
import sys
from typing import Iterable

import torch

from util.box_ops import *
from util.plot_utils import *
import util.misc as utils
from datasets.coco_eval import CocoEvaluator
from datasets.panoptic_eval import PanopticEvaluator

from PIL import Image

def train_one_epoch(model: torch.nn.Module, criterion: torch.nn.Module,
                    data_loader: Iterable, optimizer: torch.optim.Optimizer,
                    device: torch.device, epoch: int, max_norm: float = 0):
    model.train()
    criterion.train()
    metric_logger = utils.MetricLogger(delimiter="  ")
    metric_logger.add_meter('lr', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
    metric_logger.add_meter('class_error', utils.SmoothedValue(window_size=1, fmt='{value:.2f}'))
    header = 'Epoch: [{}]'.format(epoch)
    print_freq = 10

    for samples, targets in metric_logger.log_every(data_loader, print_freq, header):
        samples = samples.to(device)
        targets = [{k: v.to(device) for k, v in t.items()} for t in targets]

        outputs = model(samples) # pred_logits, pred_bboxs, aux_outputs, pred_masks
        loss_dict = criterion(outputs, targets)
        weight_dict = criterion.weight_dict
        losses = sum(loss_dict[k] * weight_dict[k] for k in loss_dict.keys() if k in weight_dict)

        # reduce losses over all GPUs for logging purposes
        loss_dict_reduced = utils.reduce_dict(loss_dict)
        loss_dict_reduced_unscaled = {f'{k}_unscaled': v
                                      for k, v in loss_dict_reduced.items()}
        loss_dict_reduced_scaled = {k: v * weight_dict[k]
                                    for k, v in loss_dict_reduced.items() if k in weight_dict}
        losses_reduced_scaled = sum(loss_dict_reduced_scaled.values())

        loss_value = losses_reduced_scaled.item()

        if not math.isfinite(loss_value):
            print("Loss is {}, stopping training".format(loss_value))
            print(loss_dict_reduced)
            sys.exit(1)

        optimizer.zero_grad()
        losses.backward()
        if max_norm > 0:
            torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm)
        optimizer.step()

        metric_logger.update(loss=loss_value, **loss_dict_reduced_scaled, **loss_dict_reduced_unscaled)
        metric_logger.update(class_error=loss_dict_reduced['class_error'])
        metric_logger.update(lr=optimizer.param_groups[0]["lr"])
    # gather the stats from all processes
    metric_logger.synchronize_between_processes()
    print("Averaged stats:", metric_logger)
    return {k: meter.global_avg for k, meter in metric_logger.meters.items()}

@torch.no_grad()
def evaluate(model, criterion, postprocessors, data_loader, base_ds, device, output_dir):
    model.eval()
    criterion.eval()

    metric_logger = utils.MetricLogger(delimiter="  ")
    metric_logger.add_meter('class_error', utils.SmoothedValue(window_size=1, fmt='{value:.2f}'))
    header = 'Test:'

    iou_types = tuple(k for k in ('segm', 'bbox') if k in postprocessors.keys())
    coco_evaluator = CocoEvaluator(base_ds, iou_types)
    # coco_evaluator.coco_eval[iou_types[0]].params.iouThrs = [0, 0.1, 0.5, 0.75]

    panoptic_evaluator = None
    if 'panoptic' in postprocessors.keys():
        panoptic_evaluator = PanopticEvaluator(
            data_loader.dataset.ann_file,
            data_loader.dataset.ann_folder,
            output_dir=os.path.join(output_dir, "panoptic_eval"),
        )

    for samples, targets in metric_logger.log_every(data_loader, 10, header):
        samples = samples.to(device)
        targets = [{k: v.to(device) for k, v in t.items()} for t in targets]

        outputs = model(samples)
        loss_dict = criterion(outputs, targets)
        weight_dict = criterion.weight_dict

        # reduce losses over all GPUs for logging purposes
        loss_dict_reduced = utils.reduce_dict(loss_dict)
        loss_dict_reduced_scaled = {k: v * weight_dict[k]
                                    for k, v in loss_dict_reduced.items() if k in weight_dict}
        loss_dict_reduced_unscaled = {f'{k}_unscaled': v
                                      for k, v in loss_dict_reduced.items()}
        metric_logger.update(loss=sum(loss_dict_reduced_scaled.values()),
                             **loss_dict_reduced_scaled,
                             **loss_dict_reduced_unscaled)
        metric_logger.update(class_error=loss_dict_reduced['class_error'])

        orig_target_sizes = torch.stack([t["orig_size"] for t in targets], dim=0)
        results = postprocessors['bbox'](outputs, orig_target_sizes)
        if 'segm' in postprocessors.keys():
            target_sizes = torch.stack([t["size"] for t in targets], dim=0)
            results = postprocessors['segm'](results, outputs, orig_target_sizes, target_sizes)
        res = {target['image_id'].item(): output for target, output in zip(targets, results)}
        if coco_evaluator is not None:
            coco_evaluator.update(res)

        if panoptic_evaluator is not None:
            res_pano = postprocessors["panoptic"](outputs, target_sizes, orig_target_sizes)
            for i, target in enumerate(targets):
                image_id = target["image_id"].item()
                file_name = f"{image_id:012d}.png"
                res_pano[i]["image_id"] = image_id
                res_pano[i]["file_name"] = file_name

            panoptic_evaluator.update(res_pano)

    # gather the stats from all processes
    metric_logger.synchronize_between_processes()
    print("Averaged stats:", metric_logger)
    if coco_evaluator is not None:
        coco_evaluator.synchronize_between_processes()
    if panoptic_evaluator is not None:
        panoptic_evaluator.synchronize_between_processes()

    # accumulate predictions from all images
    if coco_evaluator is not None:
        coco_evaluator.accumulate()
        coco_evaluator.summarize()
    panoptic_res = None
    if panoptic_evaluator is not None:
        panoptic_res = panoptic_evaluator.summarize()
    stats = {k: meter.global_avg for k, meter in metric_logger.meters.items()}
    if coco_evaluator is not None:
        if 'bbox' in postprocessors.keys():
            stats['coco_eval_bbox'] = coco_evaluator.coco_eval['bbox'].stats.tolist()
        if 'segm' in postprocessors.keys():
            stats['coco_eval_masks'] = coco_evaluator.coco_eval['segm'].stats.tolist()
    if panoptic_res is not None:
        stats['PQ_all'] = panoptic_res["All"]
        stats['PQ_th'] = panoptic_res["Things"]
        stats['PQ_st'] = panoptic_res["Stuff"]
    return stats, coco_evaluator

@torch.no_grad()
def visualize(model, criterion, postprocessors, data_loader, base_ds, device, output_dir):
    import numpy as np
    import itertools
    import seaborn as sns

    from models.segmentation import PostProcessPanoptic
    import panopticapi
    from panopticapi.utils import id2rgb, rgb2id
    import matplotlib.pyplot as plt
    from pathlib import Path
    import io
    model.eval()
    criterion.eval()

    metric_logger = utils.MetricLogger(delimiter="  ")
    metric_logger.add_meter('class_error', utils.SmoothedValue(window_size=1, fmt='{value:.2f}'))

    coco_id2name, coco_name2id = data_loader.dataset.get_classes_dict()
    is_thing_map = {i: i <= 90 for i in range(250)}
    postprocessor = PostProcessPanoptic(is_thing_map, threshold=0.85)

    def get_origin_image(dataset, idx):
        # def get_origin_image(self, idx):
        ann_info = dataset.coco['annotations'][idx] if "annotations" in dataset.coco else dataset.coco['images'][idx]
        img_path = Path(dataset.img_folder) / ann_info['file_name'].replace('.png', '.jpg')
        img = Image.open(img_path).convert('RGB')
        return img

    # for samples, targets in metric_logger.log_every(data_loader, 10, header):
    for i, (samples, targets) in enumerate(data_loader):
        samples = samples.to(device)
        targets = [{k: v.to(device) for k, v in t.items()} for t in targets]

        outputs = model(samples)

        im = get_origin_image(data_loader.dataset, targets[0]['item_id'][0].item())

        # keep only predictions with 0.7+ confidence
        probas = outputs['pred_logits'].softmax(-1)[0, :, :-1]
        keep = probas.max(-1).values > 0.7

        ''' bbox visualization'''
        # convert boxes from [0; 1] to image scales
        bboxes_scaled = rescale_bboxes(outputs['pred_boxes'][0, keep].cpu(), im.size)

        scores_keep, boxes_keep = probas[keep].cpu(), bboxes_scaled

        plot_bbox_results(im, scores_keep, boxes_keep, id2name=coco_id2name)

        ''' mask visualization'''
        # Plot all the remaining masks
        ncols = 5
        fig, axs = plt.subplots(ncols=ncols, nrows=math.ceil(keep.sum().item() / ncols), figsize=(18, 10), squeeze=False)
        for line in axs:
            for a in line:
                a.axis('off')
        for i, mask in enumerate(outputs["pred_masks"][0][keep]):
            ax = axs[i // ncols, i % ncols]
            ax.imshow(mask.cpu().numpy(), cmap="cividis")
            ax.axis('off')
        fig.tight_layout()
        plt.show()

        # the post-processor expects as input the target size of the predictions (which we set here to the image size)
        result = postprocessor(outputs, torch.as_tensor(im.size).unsqueeze(0).to(outputs['pred_boxes'].device))[0]
        palette = itertools.cycle(sns.color_palette())
        # The segmentation is stored in a special-format png
        panoptic_seg = Image.open(io.BytesIO(result['png_string']))
        panoptic_seg = np.array(panoptic_seg, dtype=np.uint8).copy()
        # We retrieve the ids corresponding to each mask
        panoptic_seg_id = rgb2id(panoptic_seg)

        # Finally we color each mask individually
        panoptic_seg[:, :, :] = 0
        for id in range(panoptic_seg_id.max() + 1):
            panoptic_seg[panoptic_seg_id == id] = np.asarray(next(palette)) * 255
        plt.figure(figsize=(15,15))
        plt.imshow(panoptic_seg)
        plt.axis('off')
        plt.show()

    return


#!/usr/bin/env python3

import numpy as np

import tensorflow as tf

from baselines import logger
from baselines.acktr.acktr_disc import learn
from baselines.acktr.policies import CnnPolicy
from baselines.common.cmd_util import make_atari_env, atari_arg_parser
from baselines.common.vec_env.vec_frame_stack import VecFrameStack

import constants as cnst

def train(params):
    policy_fn = CnnPolicy

    dataflow_config = {
        'future_rewards': True,             # Should return future discounted rewards?
        'exclude_zero_actions': False,      # Should exclude zero actions
        'remap_actions': False,             # Should remap to smaller action set?
        'clip_rewards': True,               # Clip rewards to [-1, 1]
        'monte-specific-blackout': True,    # Cover up score and lives indicators
        'pong-specific-blackout': False,    # Cover up scores in pong
        'gamma': params.gamma,              # reward discount factor
        'frame_history': 4,                 # What is minimum number of expert frames since beginning of episode?
        'frameskip': 4,                     # frameskip
        'preload_images': True,             # Preload images from hard drive or keep reloading ?
        'gdrive_data_id': cnst.MONTE_DATA_GDRIVE_ID,
        'data_dir': cnst.DATA_DIR,
        'img_dir': cnst.MIKE_IMG_DIR,
        'traj_dir': cnst.MIKE_TRAJECTORIES_DIR,
        'stat_dir': cnst.MIKE_STATES_DIR,
        'batch_size': params.expert_nbatch,
        'max_score_cutoff': params.exp_max_score,  # What is maximum expert score we can show? Used to cut expert data
        'min_score_cutoff': 20000,                 # What is minimum score to count trajectory as expert
        'process_lost_lifes': True,                # Should loss of life zero future discounted reward?
        'use_n_trajectories': params.use_n_trajectories if 'use_n_trajectories' in params else None,
        'demos_path': params.demos_path
    }

    the_seed = np.random.randint(10000)
    print(80 * "SEED")
    print("Today's lucky seed is {}".format(the_seed))
    print(80 * "SEED")

    env = VecFrameStack(
        make_atari_env(
            env_id=params.env,
            num_env=params.num_env,
            seed=the_seed,
            limit_len=params.limit_len,
            limit_penalty=params.limit_penalty,
            death_penalty=params.death_penalty,
            step_penalty=params.step_penalty,
            random_state_reset=params.random_state_reset,
            dataflow_config=dataflow_config
        ),
        params.frame_stack
    )

    learn(
        policy=policy_fn,
        env=env,
        seed=the_seed,
        params=params,
        dataflow_config=dataflow_config,
        expert_nbatch=params.expert_nbatch,
        exp_adv_est=params.exp_adv_est,
        load_model=params.load_model,
        gamma=params.gamma,
        nprocs=params.num_env,
        nsteps=params.nsteps,
        ent_coef=params.ent_coef,
        expert_coeff=params.exp_coeff,
        lr=params.lr,
        lrschedule=params.lrschedule,
    )

    env.close()

def main():
    # os.environ["CUDA_VISIBLE_DEVICES"] = "0"
    parser = atari_arg_parser()
    parser.add_argument("--demos_path", type=str, required=True)
    params = parser.parse_args()
    logger.configure(dir=cnst.openai_logdir())
    train(params)

if __name__ == '__main__':
    main()


# (C) 2018-present Klebert Engineering

import os
import sys
import pickle
import argparse
import math
import random
from collections import defaultdict
from scipy.spatial import cKDTree

sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/modules")

from deepspell.corpus import DSCorpus
from deepspell.grammar import DSGrammar
from deepspell.baseline.symspell import DSSymSpellBaseline
from deepspell.models.encoder import DSVariationalLstmAutoEncoder

arg_parser = argparse.ArgumentParser("NDS AutoCompletion Quality Evaluator")
arg_parser.add_argument(
    "--grammar",
    default="corpora/grammar-encoder.json",
    help="Path to the JSON descriptor for the grammar that should be used for sample gen.")
arg_parser.add_argument(
    "--corpus",
    default="corpora/deepspell_data_north_america_cities.tsv",
    help="Path to the kdtree/token list from which correct matches should be drawn.")
arg_parser.add_argument(
    "--model",
    default="models/deepsp_spell-v1_na-lower_lr003_dec50_bat3072_emb8_fw128-128_bw128_de128-128_drop80.json",
    help="Path to the model JSON descriptor that should be used for token encoding.")
arg_parser.add_argument(
    "--model-data",
    default="corpora/deepspell_data_north_america_cities.0",
    help="Path to the model JSON descriptor that should be used for token encoding.",
    dest="model_data")
arg_parser.add_argument(
    "--baseline",
    default=False,
    action="store_true",
    help="Use this flag in place of --model if you wish to use the baseline matcher.")
arg_parser.add_argument(
    "-s", "--test-split-percentage",
    default=100,
    type=int,
    dest="test_split",
    choices=range(100),
    help="Per-token-class percentage of the given corpus data that should be used for evaluation.")
args = arg_parser.parse_args()

print("Benchmarking FTS Spellchecker... ")
print("  ... model:      "+("SYMSPELL-BASELINE" if args.baseline else args.model))
print("  ... model_data: "+args.model_data)
print("  ... corpus:     "+args.corpus)
print("  ... grammar:    "+args.grammar)
print("=======================================================================")
print("")

if args.baseline:
    encoder_model = DSSymSpellBaseline(args.model_data)
    spell_tokens = None
    spell_kdtree = None
else:
    encoder_model = DSVariationalLstmAutoEncoder(args.model)
    spell_tokens = [token.strip() for token in open(args.model_data+".tokens", "r")]
    spell_kdtree = pickle.load(open(args.model_data+".kdtree", "rb"))

training_corpus = DSCorpus(args.corpus, "na", lowercase=True)
training_grammar = DSGrammar(args.grammar, training_corpus.featureset)
featureset = training_corpus.featureset

def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=10):
    """
    Call in a loop to create terminal progress bar
    @params:
        iteration   - Required  : current iteration (Int)
        total       - Required  : total iterations (Int)
        prefix      - Optional  : prefix string (Str)
        suffix      - Optional  : suffix string (Str)
        decimals    - Optional  : positive number of decimals in percent complete (Int)
        bar_length  - Optional  : character length of bar (Int)
    """
    str_format = "{0:." + str(decimals) + "f}"
    percents = str_format.format(100 * (iteration / float(total)))
    filled_length = int(round(bar_length * iteration / float(total)))
    bar = '#' * filled_length + '-' * (bar_length - filled_length)
    sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),
    if iteration == total:
        sys.stdout.write('\n')
    sys.stdout.flush()

class TokenClassBenchmark:
    def __init__(self):
        self.corrected = 0
        self.not_corrected = 0

    def correction_precision(self):
        total, correct = self.corrected+self.not_corrected, self.corrected
        if total == 0:
            return .0
        return float(correct)/float(total)

def test_split_len(token_list):
    return math.ceil(len(token_list) * float(args.test_split)/100.)

# This dictionary contains a TokenClassBenchmark object for every
# token class in the given corpus.
benchmark_stats = defaultdict(lambda: TokenClassBenchmark())

# These variables serve to observe the evaluation progress
total_tokens = sum(test_split_len(tokens) for _, tokens in training_corpus.data.items())
completed_tokens = 0

for class_id, tokens in training_corpus.data.items():

    random.shuffle(tokens)

    for test_token in tokens[:test_split_len(tokens)]:

        completed_tokens += 1
        print_progress(completed_tokens, total_tokens, suffix="({}/{}) ('{}')".format(
            completed_tokens,
            total_tokens,
            test_token.string))
        test_string = training_grammar.random_phrase_with_token(test_token, corrupt=True)[0].string  # Get sample phrase

        # -- Evaluate corrector
        if args.baseline:
            suggestions = {suggestion[0] for suggestion in encoder_model.match(test_string, silent=True)[:3]}
        else:
            lookup_vector = encoder_model.encode(test_string)
            suggestions = {spell_tokens[i] for i in spell_kdtree.query(lookup_vector, k=3)[1]}

        if test_token.string in suggestions:
            benchmark_stats[class_id].corrected += 1
        else:
            benchmark_stats[class_id].not_corrected += 1

print("\n\nDone.")
print("Results:")
for class_id, benchmark in benchmark_stats.items():
    print(" * {}: corr={}%".format(
        featureset.class_name_for_id(class_id),
        benchmark.correction_precision()))


from django.shortcuts import render
from django.core.files.storage import FileSystemStorage
from keras.models import load_model
from keras.preprocessing import image
import tensorflow as tf
import json
from tensorflow import Graph

# Define o tamanho padrão da imagem a ser carregada.
# Define o arquivo json com as classes de imagens.
# Adicionado o parâmetro encoding que resolve problemas de acentuação quando retornando dados do json.

img_height, img_width = 224,224
with open('./models/imagenet_classes.json','r', encoding='utf8') as f:
    labelInfo = f.read()
labelInfo = json.loads(labelInfo)

# model = load_model('./models/MobileNetModelImagenet.h5') # Carrega o modelo treinado do keras, desse modo funciona apenas nos notebooks

# Carrega o modelo.
model_graph = Graph()
with model_graph.as_default():
    tf_session = tf.compat.v1.Session() # Usa o Session do tensorflow anterior a versão 2.0
    with tf_session.as_default():
        model=load_model('./models/MobileNetModelImagenet.h5')

def index(request):
    context = {'a':1}

    return render(request,'index.html', context)

# Função de detecção.
def predictImage(request):
    print(request)
    print(request.POST.dict())
    fileObj = request.FILES['filePath']
    # Define o banco de dados
    fs = FileSystemStorage()
    # Salva o arquivo enviado com o mesmo nome do original e define o caminho pra imagem.
    filePathName = fs.save(fileObj.name,fileObj)
    filePathName = fs.url(filePathName)
    testimage = '.'+ filePathName
    # Carrega a imagem e lhe atribui o tamanho definido anteriormente.
    img = image.load_img(testimage, target_size=(img_height, img_width))
    # Transforma a imagem em uma array.
    x = image.img_to_array(img)
    x = x/255
    x = x.reshape(1,img_height, img_width, 3)
    # Executa a classificação.
    with model_graph.as_default():
       with tf_session.as_default():
           predi=model.predict(x)

    import numpy as np
    # axis=-1 é o id do label.
    # predi[0] é a array com as probabilides de todas as classes do modelo.
    # Pega o label da maior probabilidade dentro da array de probabilidades.
    predictedLabel = labelInfo[str(np.argmax(predi[0]))]
    # Retorna a probabilidade de a Imagem pertencer a classe detectada(a com maior probabilidade) e transforma em porcentagem.
    score = f'{(max(predi[0]) * 100):.2f}'
    # Normaliza as probabilidades das classes selecionadas, arredondando em 2 casas decimais,
    # assim quando a chance de ser uma das duas classes principais for menor que 0,1% serão igualhadas em 0 e a condição retorna o else.
    scoreDobraw = round(predi[0][236],3)
    scoreGroraw = round(predi[0][224],3)
    scoreDob = scoreDobraw*100
    scoreGro = scoreGroraw*100
    # Analisa a classe com maior grau de compatibilidade e normaliza em porcentagem com 1 casa decimal.
    myclass = 0
    if scoreDob > scoreGro:
        myclass = scoreDob
    else:
        myclass = scoreGro

    if myclass == scoreDob and myclass > 1.5:
        retScore = f'A imagem analisada tem {scoreDob:.1f}% de chances de pertencer a classe: Doberman' # Probabilidade de Pertencer a Classe Doberman
    elif myclass == scoreGro and myclass > 1.5:
        retScore = f'A imagem analisada tem {scoreGro:.1f}% de chances de pertencer a classe: Pastor Belga Groenendael' # Probabilidade de Pertencer a classe Groenendael
    # Se as duas classes tiverem chance menor que 0.05%, retorna a maior probabilidade de todas as classes do modelo traduzida para português.
    else:
        from googletrans import Translator
        maisProvavel = str((predictedLabel[1]).replace('_',' '))
        translator = Translator()
        traduz = translator.translate(maisProvavel,dest='pt')
        maisProvavelOut = traduz.text
        retScore = f'Não parece ser um Doberman, muito menos um Pastor Belga Groenendael, seria um(a) {maisProvavelOut}?'

    context = {'filePathName':filePathName, 'predictedLabel':predictedLabel[1], 'retScore':retScore}
    return render(request,'index.html', context)

# Define view da galeria
def viewDataBase(request):
    import os
    # Cria uma lista com os arquivos dentro do diretório ./media/
    listOfImages = os.listdir('./media/')
    # Pega o caminho de cada arquivo e adiciona em uma lista
    listOfImagesPath = ['./media/'+ i for i in listOfImages]
    context = {'listOfImagesPath':listOfImagesPath}

    return render(request,'viewDB.html',context) #envia context a viewDB.html


import signal
import numpy
import btceapi

key_file = 'key.txt'
pair = "btc_usd"
handler = btceapi.KeyHandler(key_file)
key = handler.getKeys()[0]
print "Trading with key %s" % key

def _attemptBuy(price):
    conn = btceapi.BTCEConnection()
    try:
        info = api.getInfo(conn)
        # Limit order to what we can afford to buy.
        available = float(getattr(info, "balance_usd"))
        buy_amount = available / price
        print "attempting to buy %s bitcoin at %s for %s" % (
            buy_amount, price, buy_amount*price
        )
        r = api.trade(pair, "buy", price, buy_amount, conn)
        # If the order didn't fill completely, cancel the remaining order
        if r.order_id != 0:
            print "\tCanceling unfilled portion of order"
            api.cancelOrder(r.order_id, conn)
            conn.close()
            raise Exception('buy failure')
    except:
        print "error buying"
        conn.close()
        raise Exception('buy failure')
        pass

    print "\tReceived %s bits" % (r.received)
    conn.close()

def _attemptSell(price):
    conn = btceapi.BTCEConnection()
    try:
        info = api.getInfo(conn)
        # Limit order to what we have available to sell.
        available = float(getattr(info, "balance_btc"))
        r = api.trade(pair, "sell", price, available, conn)
        # If the order didn't fill completely, cancel the remaining order
        if r.order_id != 0:
            print "\tCanceling unfilled portion of order"
            api.cancelOrder(r.order_id, conn)
            conn.close()
            raise Exception('sell failure')
    except:
        "print sell failed"
        conn.close()
        raise Exception('sell failure')
    print "\tSold %s at %s" % (r.received, price)
    conn.close()
# This overrides the onNewDepth method in the TraderBase class, so the
# framework will automatically pick it up and send updates to it.

class GracefulKiller:
    kill_now = False

    def __init__(self):
        signal.signal(signal.SIGINT, self.exit_gracefully)
        signal.signal(signal.SIGTERM, self.exit_gracefully)

    def exit_gracefully(self, signum, frame):
        self.kill_now = True

import pudb
pudb.set_trace()
conn = btceapi.BTCEConnection()
api = btceapi.TradeAPI(key, handler)
available = getattr(api.getInfo(conn), "balance_usd")
killer = GracefulKiller()
mean = 600
counter = 0
sample_rate = 30
DeltaList = [0]*sample_rate
buy_flag = False
delta = 0
adjust = 0.99999
conn.close()
while True:
    ask_list = []
    price_list = []
    if counter == sample_rate:
        counter = 0
        print "counter has zeroed"
        print "delta list is: %s" % DeltaList
    try:
        conn = btceapi.BTCEConnection()
        tradeHistory = btceapi.getTradeHistory(pair, conn)
        available = getattr(api.getInfo(conn), "balance_usd")
        asks, bids = btceapi.getDepth("btc_usd", conn)
        for ask in asks:
            ask_list.append(ask[0])
        for i in range(int(1.5*sample_rate)):
            price_list.append(tradeHistory[i].price)
        last_mean = mean
        mean = numpy.mean(ask_list)
    except:
        print "error getDepth"
        pass
    conn.close()
    try:
        DeltaList[counter] = float(mean-last_mean)
        filtered_delta = filter(lambda x: x != 0, DeltaList)
        delta = numpy.mean(filtered_delta)
    except:
        print "error"
        pass
    if delta > 0.03 and available > 100:
        try:
            price = float(ask_list[0])
            print "attempting buying at delta: %s for price %s and list %s" % (
                delta, price, DeltaList
            )
            _attemptBuy(price)
            buy_flag = True
        except:
            try:
                price = price*(2-adjust)
                print "attempting buying at delta: %s for price %s and list %s" % (
                    delta, price, DeltaList
                )
                _attemptBuy(price)
                buy_flag = True
            except:
                try:
                    price = price*(2-adjust)
                    print "attempting buying at delta: %s for price %s and list %s" % (
                        delta, price, DeltaList
                    )
                    _attemptBuy(price)
                    buy_flag = True
                except:
                    print "buy failed"
                    pass
    elif available < 100 and delta < -0.01:
        try:
            DeltaList[counter] = DeltaList[counter]*(2-adjust)
            if bids[0][0] > 500:
                price = float(ask_list[0])
                print "attempting selling at delta: %s for price %s" % (
                    DeltaList, price
                )
                _attemptSell(price)
                buy_flag = False
        except:
            try:
                price = price*adjust
                print "attempting selling at delta: %s for price %s" % (
                    delta, price
                )
                _attemptSell(price)
                buy_flag = False
            except:
                try:
                    price = price*adjust
                    print "attempting selling at delta: %s for price %s" % (
                        delta, price
                    )
                    _attemptSell(price)
                    buy_flag = False
                except:
                    print "sell failed"
                    pass
    counter += 1
    if killer.kill_now:
        break
print "End of the program. I was killed gracefully :)"


"""
This creates the plot that is used as Fig. 8 in Lyke et al. 2020. This spectrum
file and the catalog file is needed to make this plot. This will not work (and
has no error-trapping) for records that are missing any data from:
UKIDSS, ROSAT, FIRST, GALEX, or WISE. The spectrum file must have a positive
value for flux and ivar at every point. Spectra with bad pixels are NOT supported.

As UKIDSS and 2MASS overlap in wavelength coverage, only one should be used. 2MASS
data conversion is not supported here. This pretty much leaves UKIDSS only (for
this script).

There will be NO updates to this script to include 2MASS conversions.

Gaia data is not converted or plotted as the large time gap between SDSS optical and
Gaia optical observations cannot be plotted together in a meaningful way due to
known quasar flux variability.

The terms 'flux' and 'flux density' are used interchangeably throughout. Context
is key.

Dependencies
----------
spec file : requires the SDSS full spectrum file in ../data/
github repository : https://github.com/bradlyke/utilities
Note: To get these plots, LaTeX must be installed and
      matplotlib must be able to compile LaTeX commands

Input file
----------
The most recent version of the DR16Q quasar-only catalog.

Parameters
----------
input_file : the file name for the DR16Q FITS file in the
             ../data folder. No path needed.
pt,mt,ft : Plate, MJD, and FiberID for a record that has all multiwavelength data
           The values used to make the paper plot are hardcoded in __main__
           No guarantees are made about script viability or plot viability
           for any spectrum other than the hardcoded one.
output_plot_name : the name of the plot written out
plot_fontsize : The fontsize for everything in the plot
               For a 10x4 plot spanning twocolumn, 11 works best.
save_check : 0 - don't save, just plot
             1 - don't plot, just save into ../plots/

Output
----------
If selected, an EPS file of the plot.

"""

from astropy.io import fits
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
matplotlib.rc('text',usetex=True)
import progressBar_fancy as pbf
import cat_tools as ct
from sciCon import mks
import sys

#This function will smooth the SDSS spectrum using a boxcar window of a
#given pixel size (smooth_pct).
#This is a copy of the smoothing function from another of my repositories:
#  https://github.com/bradlyke/ica_5010/rolling_boxcar.ipynb
#Copied over so users don't have to clone another repository for one function.
def boxcar(flux_arr,flux_var,smooth_pct,weighted=False):
    spc = smooth_pct
    num_dpoints = len(flux_arr) #how many data points
    box_flux = np.zeros(num_dpoints,dtype='f8') #This is the smoothed flux data
    box_err = np.zeros(num_dpoints,dtype='f8') #This is the smoother flux error

    #This does the box. 10 pixels wide, so choose 5 on each side of current pixel
    for i in range(num_dpoints):
        #Define range of values to smooth
        if i < int(spc/2):
            lower = 0
        else:
            lower = -int(spc/2) + i
        if ((int(spc/2) + i) > num_dpoints):
            upper = num_dpoints
        else:
            upper = int(spc/2) + i
        #Smooth the values depending on smoothing type
        #Weighted takes each pixels error into account and the pixel flux is inversely
        #proportional to the pixel uncertainty
        if weighted==True:
            noise_temp = np.sqrt(flux_var[lower:upper])
            signal_temp = flux_arr[lower:upper]
            flux_temp = np.average(signal_temp,weights=noise_temp)
            ivar_temp = (np.average((signal_temp-flux_temp)**2, weights=noise_temp))**(-1.0)
        #Unweighted version. Why would you want to use this?
        else:
            flux_temp = np.median(flux_arr[lower:upper])
            ivar_temp = flux_var[i]

        #Save the NEW smoothed flux value
        box_flux[i] = flux_temp
        box_err[i] = ivar_temp

    return box_flux,box_err

"""
To make a Spectral Energy Distribution (SED), all of the different sources
of data have to be on the same flux scale (y-axis). These following functions
will convert the different surveys' data in DR16Q to the standard SDSS flux
units of 10^(-17) erg/s/cm^2/Angstrom.
uk_convert(): UKIDSS data
jy_convert(): Anything in Janskys.
nano_convert(): Anything in nanomaggies (GALEX or WISE data)
xray_convert(): Anything in flux, F (erg/s/cm^2), but NOT flux density, F_lambda (erg/s/cm^2/Ang)
                ROSAT and XMM both use F.

"""
#Remember that the ouput on all of these needs to be:
# 10^(-17) erg/s/cm^2/Ang
def uk_convert(influx,inlam):
    #I will convert W/m^2/Hz from UKIDSS here
    flux_temp = influx*1000 #converts W/m^2/Hz to erg/s/cm^2/Hz
    flux_temp = flux_temp * (mks.c*10**(10)) * inlam**(-2.0) #converts /Hz to /Ang
    flux_temp = flux_temp * 1e17 #convert to SDSS version
    return flux_temp

def jy_convert(fjy,inlam):
    #I will convert Jy or mJy here
    flux_temp = fjy / 1000 #converts mJy to Jy
    flux_temp = flux_temp * 1e-26 #converts Jy to W/m^2/Hz
    flux_temp = uk_convert(flux_temp,inlam) #converts W/m^2/Hz to erg/s/cm^2/Ang
    #uk_convert() already reports in SDSS version
    return flux_temp

def nano_convert(influx,inlam):
    #I will convert nanomaggies from GALEX and WISE here
    #flux_source will be 'GALEX' or 'WISE' as they may be on separate systems
    flux_temp = influx * 3.631e-6 #converts to Jy
    flux_temp = flux_temp * 1e-26 #converts to W/m^2/Hz
    flux_temp = uk_convert(flux_temp,inlam) #converts W/m^2/Hz to erg/s/cm^2/Ang
    #uk_convert() already reports in SDSS version

    return flux_temp

def xray_convert(influx,inlam):
    #I will convert ROSAT/XMM here. They are in Flux, not flux density
    #ROSAT(2RXS) will have one data point. XMM will have 2 (soft/hard) or
    #3 (soft/hard/total). The inlam should be the center wavelength for the range
    #and must be input in Angstroms. Conversion from keV to Ang in sed_plotter()
    flux_temp = influx / inlam
    flux_temp = flux_temp * 1e17 #convert to SDSS scaling

    return flux_temp

#def tmass_convert(influx,inlam):
    #Convert 2MASS magnitudes here. An online program does this, check there:
    # http://ssc.spitzer.caltech.edu/warmmission/propkit/pet/magtojy/
    #2MASS conversions are not supported for DR16Q. The spectrum in the paper
    #used UKIDSS data for the wavelenghts in question.

def sed_plotter(infile,pt,mt,ft,fname,fntsize,write_check):
    dr = fits.open(infile)[1].data #Load the catalog file
    matplotlib.rc('font',size=fntsize)
    matplotlib.rcParams['text.latex.preamble'] = [r'\boldmath']

    drloc = ct.find_rec(dr,pt,mt,ft) #Find record for the specified quasar

    #Load the SDSS spectrum
    spec_iname = '../data/spec-{0}-{1}-{2:04d}.fits'.format(pt,mt,ft)
    spec_data = fits.open(spec_iname)[1].data
    spec_lam = 10**spec_data['loglam'] #wavelength stored as log10(lambda) in file.
    spec_err = spec_data['ivar'] #error stored as inverse variance per pixel
    wspec = np.where(spec_lam <= 10000)[0] #We don't want UKIDSS to overlap SDSS
    #Note that SDSS spectra above lambda=10,000 Ang are typically dominated by sky emission
    spec_flux = spec_data['flux']
    spec_fluxS, spec_errS = boxcar(spec_flux,spec_err,10,weighted=True) #10 pixel window size

    #Converting the GALEX data
    gal_data = np.zeros(2,dtype=[('LAM','float64'),('FLUX_RAW','float64'),('FLUX_CONV','float64'),
                                            ('FLUX_ERR_RAW','float64'),('FLUX_ERR_CONV','float64')])
    gal_data['LAM'][0] = 1550 #Angstroms, FUV bandpass = 1350 - 1750 Ang
    gal_data['LAM'][1] = 2275 #Angstroms, NUV bandpass = 1750 - 2800 Ang
    gal_data['FLUX_RAW'][0] = dr['FUV'][drloc]
    gal_data['FLUX_RAW'][1] = dr['NUV'][drloc]
    #Convert inverse variance to a 1-sigma uncertainty for error bars later
    gal_data['FLUX_ERR_RAW'][0] = 1/np.sqrt(dr['FUV_IVAR'][drloc])
    gal_data['FLUX_ERR_RAW'][1] = 1/np.sqrt(dr['NUV_IVAR'][drloc])
    #Convert GALEX flux and uncertainty to SDSS units
    gal_data['FLUX_CONV'] = nano_convert(gal_data['FLUX_RAW'],gal_data['LAM'])
    gal_data['FLUX_ERR_CONV'] = nano_convert(gal_data['FLUX_ERR_RAW'],gal_data['LAM'])

    #Converting the UKIDSS data
    ukid_data = np.zeros(4,dtype=[('LAM','float64'),('FLUX_RAW','float64'),('FLUX_CONV','float64'),
                                        ('FLUX_ERR_RAW','float64'),('FLUX_ERR_CONV','float64')])
    #Get passband centers in Angstroms (comments are in microns)
    ukid_data['LAM'][0] = 10200 #Y-band, Angstroms, 50% cuton/cutoff = 0.97/1.07 microns Y
    ukid_data['LAM'][1] = 12500 #J 1.17/1.33
    ukid_data['LAM'][2] = 16350 #H 1.49/1.78
    ukid_data['LAM'][3] = 22000 #K 2.03/2.37
    ukid_data['FLUX_RAW'][0] = dr['YFLUX'][drloc]
    ukid_data['FLUX_RAW'][1] = dr['JFLUX'][drloc]
    ukid_data['FLUX_RAW'][2] = dr['HFLUX'][drloc]
    ukid_data['FLUX_RAW'][3] = dr['KFLUX'][drloc]
    ukid_data['FLUX_ERR_RAW'][0] = dr['YFLUX_ERR'][drloc]
    ukid_data['FLUX_ERR_RAW'][1] = dr['JFLUX_ERR'][drloc]
    ukid_data['FLUX_ERR_RAW'][2] = dr['HFLUX_ERR'][drloc]
    ukid_data['FLUX_ERR_RAW'][3] = dr['KFLUX_ERR'][drloc]
    #Convert UKIDSS flux and error to SDSS units
    ukid_data['FLUX_CONV'] = uk_convert(ukid_data['FLUX_RAW'],ukid_data['LAM'])
    ukid_data['FLUX_ERR_CONV'] = uk_convert(ukid_data['FLUX_ERR_RAW'],ukid_data['LAM'])

    #Radio, X-ray, and WISE (infrared) data. Radio/X-ray does not appear on the plot
    #as these are so far out in wavelength space they are difficult to plot even on
    #a log scale. The flux ranges for these are also of significantly
    #lesser (radio)/greater (X-ray) magnitude to SDSS, that the y-axis also won't
    #work well. Converted here for instructional purposes, but not used in the plot.
    radxw_data = np.zeros(4,dtype=[('LAM','float64'),('FLUX_RAW','float64'),('FLUX_CONV','float64'),
                                    ('FLUX_ERR_RAW','float64'),('FLUX_ERR_CONV','float64')])
    #Convert ROSAT data (not used)
    radxw_data['LAM'][0] = ((mks.h * mks.c)/(1250*mks.eV))*(10**10) #Angstroms, ROSAT is 0.5 - 2.0 keV
    radxw_data['FLUX_RAW'][0] = dr['2RXS_SRC_FLUX'][drloc]
    radxw_data['FLUX_CONV'][0] = xray_convert(radxw_data['FLUX_RAW'][0],radxw_data['LAM'][0])
    radxw_data['FLUX_ERR_RAW'][0] = dr['2RXS_SRC_FLUX_ERR'][drloc]
    radxw_data['FLUX_ERR_CONV'][0] = xray_convert(radxw_data['FLUX_ERR_RAW'][0],radxw_data['LAM'][0])
    #Convert WISE infrared data (USED in plot)
    radxw_data['LAM'][1] = 34000 #Angstroms, W1 band center
    radxw_data['LAM'][2] = 46000 #Angstroms, W2 band center
    radxw_data['FLUX_RAW'][1] = dr['W1_FLUX'][drloc]
    radxw_data['FLUX_RAW'][2] = dr['W2_FLUX'][drloc]
    radxw_data['FLUX_CONV'][1:3] = nano_convert(radxw_data['FLUX_RAW'][1:3],radxw_data['LAM'][1:3])
    radxw_data['FLUX_ERR_RAW'][1] = 1/np.sqrt(dr['W1_FLUX_IVAR'][drloc])
    radxw_data['FLUX_ERR_RAW'][2] = 1/np.sqrt(dr['W2_FLUX_IVAR'][drloc])
    radxw_data['FLUX_ERR_CONV'][1:3] = nano_convert(radxw_data['FLUX_ERR_RAW'][1:3],radxw_data['LAM'][1:3])
    #Convert FIRST radio data (not used)
    radxw_data['LAM'][3] = 20e8 #Angstroms, 20cm for FIRST
    radxw_data['FLUX_RAW'][3] = dr['FIRST_FLUX'][drloc]
    radxw_data['FLUX_CONV'][3] = jy_convert(radxw_data['FLUX_RAW'][3],radxw_data['LAM'][3])
    radxw_data['FLUX_ERR_RAW'][3] = (dr['FIRST_FLUX'][drloc]-0.25)/dr['FIRST_SNR'][drloc]
    radxw_data['FLUX_ERR_CONV'][3] = jy_convert(radxw_data['FLUX_ERR_RAW'][3],radxw_data['LAM'][3])

    #Print the converted flux values for each band pass. Using the default spectrum
    #in the paper, this will show why FIRST and ROSAT were not plotted.
    print('\n')
    print('LAMBDA: FUV | FLUX: {:.3e} | ERR: {:.3e}'.format(gal_data['FLUX_CONV'][0],gal_data['FLUX_ERR_CONV'][0]))
    print('LAMBDA: NUV | FLUX: {:.3e} | ERR: {:.3e}'.format(gal_data['FLUX_CONV'][1],gal_data['FLUX_ERR_CONV'][1]))

    print('LAMBDA: Y | FLUX: {:.3e}'.format(ukid_data['FLUX_CONV'][0]))
    print('LAMBDA: J | FLUX: {:.3e}'.format(ukid_data['FLUX_CONV'][1]))
    print('LAMBDA: H | FLUX: {:.3e}'.format(ukid_data['FLUX_CONV'][2]))
    print('LAMBDA: K | FLUX: {:.3e}'.format(ukid_data['FLUX_CONV'][3]))

    print('LAMBDA: ROSAT | FLUX: {:.3e}'.format(radxw_data['FLUX_CONV'][0]))
    print('LAMBDA: W1 | FLUX: {:.3e}'.format(radxw_data['FLUX_CONV'][1]))
    print('LAMBDA: W2 | FLUX: {:.3e}'.format(radxw_data['FLUX_CONV'][2]))
    print('LAMBDA: FIRST | FLUX: {:.3e}'.format(radxw_data['FLUX_CONV'][3]))

    print('\nFIRST RAW: {:.3e}'.format(radxw_data['FLUX_RAW'][3]))

    #The bottom x-axis will be in the observed frame, the top will have the
    #wavelengths in the rest frame. These need different tick labels.
    xtick_obsLabs = np.array([1400,2000,4000,10000,20000,40000])
    xtick_restLabs = np.array([1000,2000,4000,10000,20000])

    #The SDSS name of the quasar and redshift to include in a text box.
    name_str = r'\textbf{SDSS J002912.35+022549.5, z = 0.576}'

    #Now we plot. Error bars for multiwave data may not display as they are
    #very small compared to the flux values (1 to 2 orders of magnitude smaller)
    fig,ax = plt.subplots(figsize=(10,4))
    axT = ax.twiny() #This makes a set of tick labels along the top (twin ALONG y-axis)
    #ax.errorbar(radxw_data['LAM'][0],radxw_data['FLUX_CONV'][0],yerr=radxw_data['FLUX_ERR_CONV'][0],label='ROSAT') #ROSAT DATA
    ax.errorbar(gal_data['LAM'],gal_data['FLUX_CONV'],yerr=gal_data['FLUX_ERR_CONV'],color='blue',alpha=0.7,fmt='d',label=r'\textbf{GALEX}') #GALEX DATA with ERROR
    ax.plot(spec_lam[wspec],spec_flux[wspec],linewidth=0.8,color='0.70',alpha=1.0,label=r'\textbf{SDSS Raw}') #SDSS DATA
    ax.plot(spec_lam[wspec],spec_fluxS[wspec],linewidth=0.6,color='black',alpha=1.0,label=r'\textbf{SDSS Smoothed}') #SDSS DATA
    ax.errorbar(ukid_data['LAM'],ukid_data['FLUX_CONV'],yerr=ukid_data['FLUX_ERR_CONV'],color='darkorange',alpha=1.0,fmt='^',label=r'\textbf{UKIDSS}') #UKIDSS DATA with ERROR
    ax.errorbar(radxw_data['LAM'][1:3],radxw_data['FLUX_CONV'][1:3],yerr=radxw_data['FLUX_ERR_CONV'][1:3],color='red',fmt='o',label=r'\textbf{WISE}') #WISE DATA with ERROR
    #ax.errorbar(radxw_data['LAM'][3],radxw_data['FLUX_CONV'][3],yerr=radxw_data['FLUX_ERR_CONV'][3],label='FIRST') #FIRST DATA

    ax.set_xscale('log') #Lower x-axis scale
    #Convert observed wavelengths to rest by lam_obs / (1+z)
    top_lim = np.array(ax.get_xlim())/1.576
    print('Rest Limits: ',top_lim) #Print these for verification by user
    #Top x-axis labels and scale
    axT.set_xlim(top_lim)
    axT.set_xscale('log')
    #Everything done to the bottom x-axis ticks needs to be done to the top (axT)
    ax.set_xticks(xtick_obsLabs)
    ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
    axT.set_xticks(xtick_restLabs)
    axT.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
    ax.set_xlabel(r'\textbf{Observed Frame Wavelength (\AA)}')
    axT.set_xlabel(r'\textbf{Rest Frame Wavelength (\AA)}')
    ax.set_ylabel(r'$f_{\lambda}$ ($10^{-17}$ \textbf{erg s}$^{-1}$\, \textbf{cm}$^{-2}$\, \textbf{\AA}$^{-1}$)')

    ##Bottom X-axis and Left/Right Y-axis minor ticks
    ax.tick_params(axis='both',direction='in')
    ax.tick_params(axis='both',which='minor',direction='in')

    #Right-side ticks
    ax.tick_params(right=True)
    ax.tick_params(which='minor',right=True)

    #Top minor ticks
    axT.tick_params(direction='in')
    axT.tick_params(which='minor',direction='in')
    ax.yaxis.set_minor_locator(ticker.MultipleLocator(10)) #Y-axis minor ticks locations
    ax.legend(loc='upper right',facecolor='white',edgecolor='white')

    #This calls the name_str from earlier. Puts it in the bottom left
    ax.text(0.01,0.1,name_str,transform=ax.transAxes, verticalalignment='top',bbox=dict(boxstyle='square,pad=0.2',fc='magenta',alpha=0.0))

    #Write out the plot, or just view it
    if write_check == 1:
        fig.savefig(fname,bbox_inches='tight',pad_inches=0.03,format='eps')
        plt.close()
    else:
        plt.tight_layout()
        plt.show()

#Call from the command line with:
#    python sed_plot.py <DR16Q file name> <plot name> <font size> <save flag>
# Example:
#    python sed_plot.py DR16Q_v3.fits sed.eps 11 1
if __name__=='__main__':
    input_file = '../data/{}'.format(sys.argv[1])
    output_plot_name = '../plots/{}'.format(sys.argv[2])
    plot_fontsize = int(sys.argv[3])
    save_check = int(sys.argv[4])
    pt,mt,ft = 7855,57011,530
    sed_plotter(input_file,pt,mt,ft,output_plot_name,plot_fontsize,save_check)


from urllib.request import urlopen
from datetime import datetime
from operator import itemgetter

import tensorflow as tf
from PIL import Image, ImageDraw
from object_detection import ObjectDetection
from azure.storage.blob import BlobServiceClient, BlobClient
import numpy as np
import sys
import uuid
import io
import time

MODEL_FILENAME = 'model.pb'
LABELS_FILENAME = 'labels.txt'
CONTAINER_NAME = "forkliftimages"

od_model = None
last_upload_time = None
latest_blob_image = ''
send_to_blob_interval = 5
labels = []

class TFObjectDetection(ObjectDetection):
    """Object Detection class for TensorFlow"""

    def __init__(self, graph_def, labels):
        super(TFObjectDetection, self).__init__(labels)
        self.graph = tf.compat.v1.Graph()
        with self.graph.as_default():
            input_data = tf.compat.v1.placeholder(tf.float32, [1, None, None, 3], name='Placeholder')
            tf.import_graph_def(graph_def, input_map={"Placeholder:0": input_data}, name="")

    def predict(self, preprocessed_image):
        inputs = np.array(preprocessed_image, dtype=np.float)[:, :, (2, 1, 0)]  # RGB -> BGR

        with tf.compat.v1.Session(graph=self.graph) as sess:
            output_tensor = sess.graph.get_tensor_by_name('model_outputs:0')
            outputs = sess.run(output_tensor, {'Placeholder:0': inputs[np.newaxis, ...]})
            return outputs[0]

def initialize():
    global od_model

    # Load a TensorFlow model
    graph_def = tf.compat.v1.GraphDef()
    with tf.io.gfile.GFile(MODEL_FILENAME, 'rb') as f:
        graph_def.ParseFromString(f.read())

    # Load labels
    with open(LABELS_FILENAME, 'r') as f:
        labels = [l.strip() for l in f.readlines()]

    od_model = TFObjectDetection(graph_def, labels)

def predict_image(image, blob_service_client):
    global last_upload_time
    global latest_blob_image
    try:
        if image.mode != "RGB":
            print("Converting to RGB")
            image = image.convert("RGB")

        predictions = od_model.predict_image(image)

        response = { 'created': datetime.utcnow().isoformat(), 'forklift': 0, 'bloburl' : latest_blob_image }

        if predictions:
            highest_prediction = max(predictions, key=itemgetter('probability'))
            print(highest_prediction)
            if highest_prediction['probability'] > 0.6:
                response['forklift'] = 1
                if last_upload_time is None or time.time() - last_upload_time >= send_to_blob_interval:

                    bounding_box = highest_prediction['boundingBox']

                    x0 = image.width * bounding_box['left']
                    y0 = image.height * bounding_box['top']
                    x1 = image.width * (bounding_box['left'] + bounding_box['width'])
                    y1 = image.height * (bounding_box['top'] + bounding_box['height'])

                    draw = ImageDraw.Draw(image)
                    draw.rectangle(((x0, y0), (x1, y1)), outline="red", width=3)

                    stream = io.BytesIO()
                    image.save(stream, format='PNG')
                    img_byte_array = stream.getvalue()

                    file_name = str(uuid.uuid4()) + ".png"
                    blob_client = blob_service_client.get_blob_client(container=CONTAINER_NAME, blob=file_name)
                    blob_client.upload_blob(img_byte_array)

                    last_upload_time = time.time()
                    latest_blob_image = blob_client.url

                    response['bloburl'] = latest_blob_image

                    print("sent image to blob:" + str(latest_blob_image))
        else:
            latest_blob_image = ''
            response['bloburl'] = latest_blob_image

        print("Results: " + str(response))
        return response

    except Exception as e:
        print(str(e))
        return 'Error: Could not preprocess image for prediction. ' + str(e)


import wrappy
import numpy as np
import pandas as pd
import robin_stocks.robinhood as rh
from tqdm import tqdm
from datetime import datetime, timezone
from pprint import pformat
from tr4d3r.core.market import PseudoTimeMarket, RealTimeMarket
from tr4d3r.utils.misc import autodatetime, decimal_floor

@wrappy.memoize(
    cache_limit=100,
    return_copy=False,
    persist_path="robin-history.pkl",
    persist_batch_size=1,
)
def rh_historicals(symbol, interval, span):
    """
    Helper function for the RobinhoodPseudoTime class.
    """
    rh.login()
    dictl = rh.get_stock_historicals(symbol, interval=interval, span=span)
    assert (
        isinstance(dictl, list) and dictl
    ), f"Expected non-empty list of dicts, got {dictl}"
    assert isinstance(dictl[0], dict), f"Expected non-empty list of dicts, got {dictl}"
    return dictl

def rh_str_to_datetime(utc_str):
    raw_dt = datetime.strptime(utc_str, "%Y-%m-%dT%H:%M:%SZ")
    utc_dt = raw_dt.astimezone(timezone.utc)
    return utc_dt

class RobinhoodPseudoMarket(PseudoTimeMarket):
    """
    Robinhood pseudo-time market where orders can take place in the past.
    This is for simulation purposes.
    """

    CURRENCY = "USD"

    def __init__(self, symbols, interval="day", span="5year"):
        assert len(symbols) > 0, "Expecting at least one symbol"
        self.span = span
        self.interval = interval

        self.symbol_to_table = {}
        for _symbol in tqdm(symbols, desc="Loading stock historicals"):
            _dictl = rh_historicals(_symbol, interval, span)
            _table = pd.DataFrame(_dictl)
            _table["begins_at"] = _table["begins_at"].apply(rh_str_to_datetime)
            _table["begins_at_timestamp"] = _table["begins_at"].apply(
                lambda x: x.timestamp()
            )
            self.symbol_to_table[_symbol] = _table

        first_table = self.symbol_to_table[symbols[0]]
        last_idx = first_table.shape[0] - 1
        self.start_time = first_table.loc[0, "begins_at"]
        self.end_time = first_table.loc[last_idx, "begins_at"]

    def __repr__(self):
        info = {
            "listing": list(self.symbol_to_table.keys()),
            "timespan": self.span,
            "interval": self.interval,
        }
        return f"===={self.__class__.__name__}====\n{pformat(info)}"

    def time_sequence(self, offset):
        offset_list = super().time_sequence(offset)
        return [self.start_time + _off for _off in offset_list]

    def get_price(self, symbol, time=None):
        table = self.symbol_to_table[symbol]
        time = autodatetime(time)

        # find the most recent recorded time before the supplied time
        interval_idx = (
            np.searchsorted(
                table["begins_at_timestamp"].values, time.timestamp() + 1e-6
            )
            - 1
        )
        assert interval_idx >= 0, f"Time out of range: {time}"

        return float(table.loc[interval_idx, "open_price"])

class RobinhoodRealMarket(RealTimeMarket):
    """
    Robinhood real-time market.
    """

    DECIMALS_AMOUNT = 2
    DECIMALS_SHARES = 6
    MIN_TRANS_AMOUNT = 1e-2
    MIN_TRANS_SHARES = 1e-6

    def __init__(self):
        pass

    @classmethod
    def round_shares(cls, shares, floor=False):
        if floor:
            return decimal_floor(shares, cls.DECIMALS_SHARES)
        else:
            return round(shares, cls.DECIMALS_SHARES)

    @classmethod
    def round_amount(cls, amount, floor=False):
        if floor:
            return decimal_floor(amount, cls.DECIMALS_AMOUNT)
        else:
            return round(shares, cls.DECIMALS_AMOUNT)

    def validate_order(self, symbol, shares, amount):
        # check symbol exists in the market
        price = self.get_price(symbol)

        # consistency between shares and amount
        if shares is not None:
            assert amount is None, "Expected either shares or amount, got both"
            assert isinstance(shares, float), f"Invalid shares: {shares}"
            amount = shares * price
        else:
            assert amount is not None, "Expected either shares or amount, got neither"

        amount = self.__class__.round_amount(amount, floor=True)
        shares = self.__class__.round_shares(amount / price, floor=True)

        if (
            shares < self.__class__.MIN_TRANS_SHARES
            or amount < self.__class__.MIN_TRANS_AMOUNT
        ):
            return dict(symbol=symbol, shares=0.0, amount=None, valid=False)
        else:
            return dict(symbol=symbol, shares=shares, amount=None, valid=True)

    def market_buy(self, symbol, shares=None, amount=None, **kwargs):
        validate_dict = self.validate_order(symbol, shares, amount)
        if validate_dict["valid"]:
            shares = validate_dict["shares"]
            self._warn(f"market buy {symbol} {shares} shares")
            order = rh.orders.order_buy_fractional_by_quantity(symbol, shares, **kwargs)
        else:
            self._warn(f"invalid order {validate_dict}")
            order = None
        return order

    def market_sell(self, symbol, shares=None, amount=None, **kwargs):
        validate_dict = self.validate_order(symbol, shares, amount)
        if validate_dict["valid"]:
            shares = validate_dict["shares"]
            self._warn(f"market sell {symbol} {shares} shares")
            order = rh.orders.order_sell_fractional_by_quantity(
                symbol, shares, **kwargs
            )
        else:
            self._warn(f"invalid order {validate_dict}")
            order = None
        return order

    def get_price(self, symbol):
        price_list = rh.stocks.get_latest_price(symbol, includeExtendedHours=False)
        return list(map(float, price_list))[0]

    def get_bid_price(self, symbol):
        price_list = rh.stocks.get_latest_price(
            symbol, "bid_price", includeExtendedHours=False
        )
        return list(map(float, price_list))[0]

    def get_ask_price(self, symbol, time=None):
        price_list = rh.stocks.get_latest_price(
            symbol, "ask_price", includeExtendedHours=False
        )
        return list(map(float, price_list))[0]


import sys
import numpy
from struct import pack
from constants import bit_depth, NUMPY_DTYPE, SAMPLE_RATE
from utils import percent_to_db, dbfs_as_percent

import pyaudio
import wave

CHUNK_SIZE = 1024
NUM_CHANNELS = 2
FORMAT = pyaudio.paInt16 if bit_depth == 16 else pyaudio.paInt24

if not sys.platform == "win32":
    GO_UP = "\033[F"
    ERASE = "\033[2K"
else:
    GO_UP = "\n"
    ERASE = ""

def is_silent(snd_data, threshold):
    maxval = max(
        abs(numpy.amax(snd_data)),
        abs(numpy.amin(snd_data))
    ) / float(2 ** (bit_depth - 1))
    return maxval < threshold

def get_input_device_names(py_audio, info):
    input_interface_names = {}
    for i in range(0, info.get('deviceCount')):
        device_info = py_audio.get_device_info_by_host_api_device_index(0, i)
        if device_info.get('maxInputChannels') > 0:
            input_interface_names[i] = device_info.get('name')
    return input_interface_names

def get_input_device_index(py_audio, audio_interface_name=None):
    info = py_audio.get_host_api_info_by_index(0)
    input_interface_names = get_input_device_names(py_audio, info)

    if audio_interface_name:
        for index, name in input_interface_names.iteritems():
            if audio_interface_name.lower() in name.lower():
                return index
        else:
            raise Exception(
                "Could not find audio input '%s' in inputs:\n%s" % (
                    audio_interface_name,
                    list_input_devices(input_interface_names)))

def get_input_device_name_by_index(audio_interface_index):
    py_audio = pyaudio.PyAudio()
    info = py_audio.get_host_api_info_by_index(0)
    input_interface_names = get_input_device_names(py_audio, info)

    for index, name in input_interface_names.iteritems():
        if index == audio_interface_index:
            return name
    else:
        raise Exception(
            "Could not find audio input index %s in inputs:\n%s" % (
                audio_interface_index,
                list_input_devices(input_interface_names)))

def list_input_devices(device_names):
    lines = []
    for index, name in sorted(device_names.iteritems()):
        lines.append(u"{:3d}. {}".format(index, name))
    return u"\n".join(lines).encode("ascii", "ignore")

def record(
    limit=None,
    after_start=None,
    on_time_up=None,
    threshold=0.00025,
    print_progress=True,
    allow_empty_return=False,
    audio_interface_name=None,
    sample_rate=SAMPLE_RATE,
):
    p = pyaudio.PyAudio()
    input_device_index = get_input_device_index(p, audio_interface_name)

    stream = p.open(
        format=FORMAT,
        channels=NUM_CHANNELS,
        rate=sample_rate,
        input=True,
        output=False,
        frames_per_buffer=CHUNK_SIZE,
        input_device_index=input_device_index
    )

    num_silent = 0
    silence_timeout = sample_rate * 2.0
    snd_started = False
    in_tail = False
    release_time = None

    if print_progress:
        sys.stderr.write("\n")

    peak_value = None
    peak_index = None

    data = []
    total_length = 0

    while 1:
        if total_length > 0 and after_start is not None:
            after_start()
            after_start = None  # don't call back again
        array = stream.read(CHUNK_SIZE)
        snd_data = numpy.fromstring(array, dtype=NUMPY_DTYPE)
        snd_data = numpy.reshape(snd_data, (2, -1), 'F')

        peak_in_buffer = numpy.amax(numpy.absolute(snd_data), 1)
        peak_in_buffer_idx = numpy.argmax(numpy.absolute(snd_data))
        mono_peak_in_buffer = max(peak_in_buffer)

        if peak_value is None or peak_value < mono_peak_in_buffer:
            peak_value = mono_peak_in_buffer
            peak_index = total_length + peak_in_buffer_idx

        data.append(snd_data)
        total_length += len(snd_data[0])
        total_duration_seconds = float(total_length) / sample_rate

        time_since_peak = total_length - peak_index
        peak_pct = mono_peak_in_buffer / peak_value
        if time_since_peak:
            estimated_remaining_duration = peak_pct / time_since_peak
        else:
            estimated_remaining_duration = 1

        if print_progress:
            raw_percentages = (
                peak_in_buffer.astype(numpy.float) /
                float(2 ** (bit_depth - 1))
            )
            dbfs = [percent_to_db(x) for x in raw_percentages]
            pct_loudness = [dbfs_as_percent(db) for db in dbfs]
            sys.stderr.write(ERASE)
            sys.stderr.write("\t%2.2f secs\t" % total_duration_seconds)
            sys.stderr.write("% 7.2f dBFS\t\t|%s%s|\n" % (
                dbfs[0],
                int(40 * pct_loudness[0]) * '=',
                int(40 * (1 - pct_loudness[0])) * ' ',
            ))
            sys.stderr.write(ERASE)
            sys.stderr.write("\t\t\t% 7.2f dBFS\t\t|%s%s|\n" % (
                dbfs[1],
                int(40 * pct_loudness[1]) * '=',
                int(40 * (1 - pct_loudness[1])) * ' ',
            ))
            pct_silence_end = float(num_silent) / silence_timeout
            estimated_remaining_duration_string = \
                "est. remaining duration: %2.2f secs" % (
                    estimated_remaining_duration
                )
            if in_tail:
                sys.stderr.write(ERASE)
                sys.stderr.write("\t\treleasing\t\tsilence:|%s%s| %s" % (
                    int(40 * pct_silence_end) * '=',
                    int(40 * (1 - pct_silence_end)) * ' ',
                    estimated_remaining_duration_string,
                ))
            else:
                sys.stderr.write(ERASE)
                sys.stderr.write("\t\t\t\t\tsilence:|%s%s| %s" % (
                    int(40 * pct_silence_end) * '=',
                    int(40 * (1 - pct_silence_end)) * ' ',
                    estimated_remaining_duration_string,
                ))
            sys.stderr.write(GO_UP)
            sys.stderr.write(GO_UP)

        silent = is_silent(snd_data, threshold)

        if silent:
            num_silent += CHUNK_SIZE
        elif not snd_started:
            snd_started = True
        else:
            num_silent = 0

        if num_silent > silence_timeout:
            if on_time_up is not None:
                on_time_up()
            break
        elif not in_tail \
                and limit is not None \
                and total_duration_seconds >= limit:
            if on_time_up is not None:
                if on_time_up():
                    num_silent = 0
                    in_tail = True
                    release_time = total_duration_seconds
                else:
                    break
            else:
                break

    if print_progress:
        sys.stderr.write("\n\n\n")

    # TODO this is inefficient, should preallocate a huge
    # array up front and then just copy into it maybe?
    # but not in the tight loop, what if that causes the clicks?
    r = numpy.empty([NUM_CHANNELS, 0], dtype=NUMPY_DTYPE)
    for chunk in data:
        r = numpy.concatenate((r, chunk), axis=1)

    sample_width = p.get_sample_size(FORMAT)
    stream.stop_stream()
    stream.close()
    p.terminate()

    if snd_started or allow_empty_return:
        return sample_width, r, release_time
    else:
        return sample_width, None, release_time

def record_to_file(
    path,
    limit,
    after_start=None,
    on_time_up=None,
    sample_rate=SAMPLE_RATE
):
    sample_width, data, release_time = record(
        limit,
        after_start,
        on_time_up,
        sample_rate,
    )
    if data is not None:
        save_to_file(path, sample_width, data, sample_rate)
        return path
    else:
        return None

def save_to_file(path, sample_width, data, sample_rate=SAMPLE_RATE):
    wf = wave.open(path, 'wb')
    wf.setnchannels(NUM_CHANNELS)
    wf.setsampwidth(sample_width)
    wf.setframerate(sample_rate)

    flattened = numpy.asarray(data.flatten('F'), dtype=NUMPY_DTYPE)

    write_chunk_size = 512
    for chunk_start in xrange(0, len(flattened), write_chunk_size):
        chunk = flattened[chunk_start:chunk_start + write_chunk_size]
        packstring = '<' + ('h' * len(chunk))
        wf.writeframes(pack(packstring, *chunk))
    wf.close()

if __name__ == '__main__':
    print record_to_file('./demo.wav', sys.argv[1] if sys.argv[1] else None)
    print("done - result written to demo.wav")