import numpy as np
import tensorflow as tf
import gin
import os, pdb

VGG_MEAN = [103.939, 116.779, 123.68]

@gin.configurable
class Model:
    def __init__(self,
                 vgg16_npy_path,
                 fc_sizes,
                 num_points,
                 save_dir,
                 loss_type='l2',
                 learning_rate=0.001,
                 momentum=0.9,
                 use_vgg=True,
                 finetune_vgg=False):
        # For predicting nodes, use fc_sizes=[1024, 1024], num_points=128.
        # For predicting knots, use fc_sizes=[1024, 1024, 128, 128], num_points=4.
        self.data_dict = np.load(vgg16_npy_path)
        self.scope='vgg'
        self.fc_sizes = fc_sizes
        self.num_points = num_points
        self.loss_type = loss_type
        self.learning_rate = learning_rate
        self.momentum = momentum
        self.use_vgg = use_vgg
        self.finetune_vgg = finetune_vgg
        self.save_dir = save_dir

    def build(self, rgb=None):
        """
        load variable from npy to build the VGG
        :param rgb: rgb image [batch, height, width, 3] values scaled [0, 255]
        """
        with tf.variable_scope(self.scope):
            if rgb is not None:
                self.rgb = rgb
            else:
                self.rgb = tf.placeholder(dtype=tf.float32, shape=[None,224,224,3])
            self.training = tf.placeholder(tf.bool)
            # Convert RGB to BGR
            bgr = tf.reverse(self.rgb, axis=[-1]) - tf.constant(VGG_MEAN)
            bgr = bgr / 256.0
            #bgr = (self.rgb-100.0)/10.0
            trainable = self.finetune_vgg or (not self.use_vgg)
            self.conv1_1 = self.conv_layer(bgr, "conv1_1", 64, load_weight=self.use_vgg, trainable=trainable)
            self.conv1_2 = self.conv_layer(self.conv1_1, "conv1_2", 64, load_weight=self.use_vgg, trainable=trainable)
            self.pool1 = self.max_pool(self.conv1_2, 'pool1')

            self.conv2_1 = self.conv_layer(self.pool1, "conv2_1", 128, load_weight=self.use_vgg, trainable=trainable)
            self.conv2_2 = self.conv_layer(self.conv2_1, "conv2_2", 128, load_weight=self.use_vgg, trainable=trainable)
            self.pool2 = self.max_pool(self.conv2_2, 'pool2')

            self.conv3_1 = self.conv_layer(self.pool2, "conv3_1", 256, load_weight=self.use_vgg, trainable=trainable)
            self.conv3_2 = self.conv_layer(self.conv3_1, "conv3_2", 256, load_weight=self.use_vgg, trainable=trainable)
            self.conv3_3 = self.conv_layer(self.conv3_2, "conv3_3", 256, load_weight=self.use_vgg, trainable=trainable)
            self.pool3 = self.max_pool(self.conv3_3, 'pool3')

            self.conv4_1 = self.conv_layer(self.pool3, "conv4_1", 512, load_weight=self.use_vgg, trainable=trainable)
            self.conv4_2 = self.conv_layer(self.conv4_1, "conv4_2", 512, load_weight=self.use_vgg, trainable=trainable)
            self.conv4_3 = self.conv_layer(self.conv4_2, "conv4_3", 512, load_weight=self.use_vgg, trainable=trainable)
            self.pool4 = self.max_pool(self.conv4_3, 'pool4')

            self.conv5_1 = self.conv_layer(self.pool4, "conv5_1", 512, load_weight=self.use_vgg, trainable=trainable)
            self.conv5_2 = self.conv_layer(self.conv5_1, "conv5_2", 512, load_weight=self.use_vgg, trainable=trainable)
            self.conv5_3 = self.conv_layer(self.conv5_2, "conv5_3", 512, load_weight=self.use_vgg, trainable=trainable)
            self.pool5 = self.max_pool(self.conv5_3, 'pool5')

            net = tf.layers.flatten(self.pool5)
            self.fcs = []
            for i, h in enumerate(self.fc_sizes):
                net = self.dense(net, 'fc%d'%(6+i), h, tf.nn.relu)
                self.fcs.append(net)
            self.pred = self.dense(net, 'fc%d'%(6+len(self.fc_sizes)), 2*self.num_points, None)
            self.pred = tf.reshape(self.pred, [-1,self.num_points,2])

            self.last_feature = self.fcs[-1]
            saver_var_list = self.get_trainable_variables()
            if not self.use_vgg:
                vars = self.get_variables()
                vars = [v for v in vars if 'moving_mean' in v.name or 'moving_variance' in v.name]
                saver_var_list = saver_var_list + vars
            self.saver = tf.train.Saver(var_list=saver_var_list, max_to_keep=20)

    def max_pool(self, bottom, name):
        return tf.nn.max_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)

    def conv_layer(self, bottom, name, channels, kernel=3, stride=1, load_weight=True, trainable=False):
        with tf.variable_scope(name):
            if load_weight:
                k_init = tf.constant_initializer(self.data_dict[name+'_W'], verify_shape=True)
                b_init = tf.constant_initializer(self.data_dict[name+'_b'], verify_shape=True)
            else:
                k_init = tf.variance_scaling_initializer()
                b_init = tf.zeros_initializer()
            output = tf.layers.conv2d(bottom, channels, kernel_size=kernel, strides=stride, padding='SAME',
                                      activation=None, kernel_initializer=k_init, bias_initializer=b_init,
                                      trainable=trainable)
            if not load_weight:
                output = tf.layers.batch_normalization(output, momentum=0.9, epsilon=1e-6,
                                                       training=self.training, trainable=True)
            output = tf.nn.relu(output)
        return output

    def dense(self, bottom, name, channels, activation, scale=1.0, load_weight=False, trainable=True):
        with tf.variable_scope(name):
            if load_weight:
                k_init = tf.constant_initializer(self.data_dict[name+'_W'], verify_shape=True)
                b_init = tf.constant_initializer(self.data_dict[name+'_b'], verify_shape=True)
            else:
                k_init = tf.variance_scaling_initializer(scale)
                b_init = tf.zeros_initializer()
            output = tf.layers.dense(bottom, channels, activation=None,
                                     kernel_initializer=k_init, bias_initializer=b_init,
                                     trainable = trainable)
            #if not load_weight:    #
            #    output = tf.layers.batch_normalization(output, training=self.training, trainable=trainable)  #
            if activation is not None:
                output = activation(output)
        return output

    def predict_single(self, sess, input, training=False):
        pred, = sess.run([self.pred], feed_dict={self.rgb:input[None], self.training:training})
        return pred[0]

    def predict_batch(self, sess, inputs, training=False):
        pred, = sess.run([self.pred], feed_dict={self.rgb:inputs, self.training:training})
        return pred

    def get_variables(self):
        return tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, self.scope)
    def get_trainable_variables(self):
        return tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, self.scope)

    def setup_optimizer(self, GT_position=None):
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        if self.loss_type == "l2":
            if GT_position is not None:
                self.gt_pred = GT_position
            else:
                self.gt_pred = tf.placeholder(name="gt_pred", dtype=tf.float32, shape=[None, self.num_points, 2])
            self.loss = tf.nn.l2_loss(self.gt_pred-self.pred, "loss_knots")
            with tf.control_dependencies(update_ops):
                self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, beta1=self.momentum). \
                                          minimize(self.loss)
            tf.summary.scalar('loss', self.loss)
        elif self.loss_type == "gradient":
            self.pred_grad = tf.placeholder(name="pred_grad", dtype=tf.float32, shape=self.pred.shape)
            self.parameters = self.get_trainable_variables()
            self.gradients = tf.gradients(self.pred, self.parameters, self.pred_grad)
            with tf.control_dependencies(update_ops):
                self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, beta1=self.momentum). \
                                          apply_gradients(zip(self.gradients, self.parameters))
        else:
            raise ValueError("Unsupported loss type!")
        self.merged_summary = tf.summary.merge_all()

    def fit(self, sess, inputs, annos):
        if self.loss_type == "l2":
            _, loss = sess.run([self.optimizer, self.loss],
                               feed_dict={self.rgb:inputs,
                                          self.gt_pred:annos,
                                          self.training:True})
        elif self.loss_type == "gradient":
            _, = sess.run([self.optimizer], feed_dict={self.rgb:inputs,
                                                       self.pred_grad:annos,
                                                       self.training:True})
            loss = None
        return loss

    def save(self, sess, step):
        self.saver.save(sess, os.path.join(self.save_dir, 'model'), global_step=step)

    def load(self, sess, snapshot):
        self.saver.restore(sess, snapshot)

if __name__ == "__main__":
    model = Model('vgg16_weights.npz', [1024, 1024, 128, 128], 4, '../tmp', use_vgg=False)
    input = tf.placeholder(dtype=tf.float32, shape=(None, 224,224,3))
    model.build(input)
    output = tf.placeholder(dtype=tf.float32, shape=(None, 4, 2))
    model.setup_optimizer(output)


import openai
import pyttsx3
import speech_recognition as sr
from api_key import API_KEY
import playsound

openai.api_key = API_KEY

# initialize text-to-speech engine
engine = pyttsx3.init()

# write script to find device index
#mic = sr.Microphone()

# #pyttsx speech
# def speak(word):
#     engine.setProperty('rate', 135)
#     engine.setProperty('volume', 0.8)

#     voices = engine.getProperty('voices')
#     engine.setProperty('voice', voices[1].id)

#     engine.say(str(word))
#     engine.runAndWait()
#     engine.stop()

def speak(text):
    engine.say(text)
    engine.runAndWait()

conversation = ""
user_name = "Farnaz"
bot_name = "Tiny Helper"

r = sr.Recognizer()
mic = sr.Microphone(device_index=0)

while True:
    with mic as source:
        print("\nListening...")
        r.adjust_for_ambient_noise(source, duration=0.1) #background noise
        speak('Hello, How may I help you today?')
        try:
            audio = r.listen(source, timeout=5) #set timeout to 5 seconds
        except sr.WaitTimeoutError:
            print("Microphone is not picking up any sound. Please check your microphone and try again.")
            continue
    print("no longer listening.\n")

    try:
        user_input = r.recognize_google(audio)
    except Exception as e:
        print(f"\nException occurred!:\n{e}\n")
        continue

    prompt = user_name + ": " + user_input + "\n" + bot_name+ ": "

    conversation += prompt  # allows for context

    # fetch response from open AI api
    response = openai.Completion.create(engine='text-davinci-003', prompt=conversation, max_tokens=300)
    response_str = response["choices"][0]["text"].replace("\n", "")
    # response_str = response_str.split(user_name + ": ", 1)[0].split(bot_name + ": ", 1)[0]

    # conversation += response_str + "\n"
    # print(response_str)

    response_lines = response_str.split("\n")
    response_str = ""
    for line in response_lines:
        if user_name + ":" not in line and bot_name + ":" not in line:
            response_str += line.strip()

    conversation += bot_name + ": " + response_str + "\n"
    print(bot_name + ": " + response_str)

    engine.say(response_str)
    engine.runAndWait()


"""
Compare SV2P and SVG. [SV2P is trained elsewhere, but the predictions file we use
will be in a standard pickle file that we can load anywhere.]

For SV2P prediction format, see:
https://github.com/ryanhoque/cloth-visual-mpc/blob/sv2p/vismpc/scripts/predict.py

NOTE: https://stackoverflow.com/questions/55178229/importerror-cannot-import-name-structural-similarity-error
we are using the older way of doing ssim_metric, so I don't know if documentation is up to date.
Probably easier to do each channel separately, then average, as Dr. Denton does it.

Update Jan 24: iterate through different model snapshots for SVG.
"""
import os
import cv2
import sys
import pickle
import random
import argparse
from svg import utils
import numpy as np
from os.path import join
from collections import defaultdict
from skimage.measure import compare_psnr as psnr_metric
from skimage.measure import compare_ssim as ssim_metric
import PIL
from PIL import (Image, ImageDraw, ImageFont)
np.set_printoptions(precision=3)

pp = argparse.ArgumentParser()
pp.add_argument('--datatype', default='')
pp.add_argument('--svg_model', default='model_0050')
pp.add_argument('--results_tail', default='results_svg_3', help='see predict_svg.sh')
args = pp.parse_args()
assert args.datatype in ['fabric-random', 'fabric-01-2021'], args.datatype
assert args.results_tail in ['results_svg_1', 'results_svg_2', 'results_svg_3'], args.results_tail
# NOTE(daniel): we hard-code saving into frand_3 and fnew_3.

# ----------------------------------------------------------------------------- #
# ------------------------------- DATA FILES ---------------------------------- #
# ----------------------------------------------------------------------------- #
HORIZON = 5
HEAD_SV2P = '/home/seita/cloth-visual-mpc/logs/'
HEAD_SVG  = join('/home/seita/svg/', args.results_tail)

# fr = fabric_random. Always load even though we might be using the other data.
TAIL_SV2P_01_fr = 'demos-fabric-random-epis_400_COMBINED_PREDS_SV2P_01.pkl'
TAIL_SV2P_10_fr = 'demos-fabric-random-epis_400_COMBINED_PREDS_SV2P_10.pkl'
TAIL_SVG_fr    = f'demos-fabric-random-epis_400_COMBINED_PREDS_SVG-LP_{args.svg_model}.pkl'
SV2P_01_fr_pth  = join(HEAD_SV2P, TAIL_SV2P_01_fr)
SV2P_10_fr_pth  = join(HEAD_SV2P, TAIL_SV2P_10_fr)
SVG_fr_pth      = join(HEAD_SVG,  TAIL_SVG_fr)

# fnew = fabric_01-2021 (new data). Always load even though we might be using the other data.
TAIL_SV2P_10_fnew = 'demos-fabric-01-2021-epis_400_COMBINED_PREDS_SV2P_10.pkl'
TAIL_SVG_fnew    = f'demos-fabric-01-2021-epis_400_COMBINED_PREDS_SVG-LP_{args.svg_model}.pkl'
SV2P_10_fnew_pth  = join(HEAD_SV2P, TAIL_SV2P_10_fnew)
SVG_fnew_pth      = join(HEAD_SVG,  TAIL_SVG_fnew)

# These are _lists_, one dict per episode.
with open(SV2P_01_fr_pth, 'rb') as fh:
    SV2P_01_fr = pickle.load(fh)
with open(SV2P_10_fr_pth, 'rb') as fh:
    SV2P_10_fr = pickle.load(fh)
with open(SVG_fr_pth, 'rb') as fh:
    SVG_fr     = pickle.load(fh)

# fnew data
with open(SV2P_10_fnew_pth, 'rb') as fh:
    SV2P_10_fnew = pickle.load(fh)
with open(SVG_fnew_pth, 'rb') as fh:
    SVG_fnew     = pickle.load(fh)

assert len(SV2P_01_fr) == len(SV2P_10_fr) == len(SVG_fr) == len(SV2P_10_fnew) == len(SVG_fnew)
# ----------------------------------------------------------------------------- #
# ------------------------------- DATA FILES ---------------------------------- #
# ----------------------------------------------------------------------------- #

def depth_to_3ch(img, cutoff):
    """Useful to turn the background into black into the depth images.
    """
    w,h = img.shape
    new_img = np.zeros([w,h,3])
    img = img.flatten()
    img[img>cutoff] = 0.0
    img = img.reshape([w,h])
    for i in range(3):
        new_img[:,:,i] = img
    return new_img

def depth_scaled_to_255(img):
    assert np.max(img) > 0.0
    img = 255.0/np.max(img)*img
    img = np.array(img,dtype=np.uint8)
    for i in range(3):
        img[:,:,i] = cv2.equalizeHist(img[:,:,i])
    return img

def make_depth_img(img, cutoff=1000):
    # NOTE! Depth will look noisy due to domain randomization.
    img = np.squeeze(img)
    img = depth_to_3ch(img, cutoff)
    img = depth_scaled_to_255(img)
    return img

def draw_ssim(draw, IM_WIDTH, ws, hoff, groupname, ssims):
    # Draw some structural similarity values as a sanity check.
    fontsize = 10
    ssim_width = IM_WIDTH - (ws + 56)  # Make this LAST column
    text_0 = f'ssim {ssims[0]:0.2f}'
    text_1 = f'ssim {ssims[1]:0.2f}'
    text_2 = f'ssim {ssims[2]:0.2f}'
    text_3 = f'ssim {ssims[3]:0.2f}'
    text_4 = f'ssim {ssims[4]:0.2f}'
    off = 9
    if 'SVG' in groupname:
        groupname += args.svg_model
        groupname = groupname.replace('model_', '')
    draw.text((ssim_width, hoff+0*off), groupname, fill=(0,0,0,0))
    draw.text((ssim_width, hoff+1*off), text_0, fill=(0,0,0,0))
    draw.text((ssim_width, hoff+2*off), text_1, fill=(0,0,0,0))
    draw.text((ssim_width, hoff+3*off), text_2, fill=(0,0,0,0))
    draw.text((ssim_width, hoff+4*off), text_3, fill=(0,0,0,0))
    draw.text((ssim_width, hoff+5*off), text_4, fill=(0,0,0,0))

def eval_ssim(true_imgs, pred_imgs, channel='both'):
    # By default, sum SSIMs across all channels, and average.
    assert channel in ['color', 'depth', 'both'], channel
    assert true_imgs.shape == pred_imgs.shape == (HORIZON, 56, 56, 4), true_imgs.shape
    if channel == 'both':
        start, end = 0, 4
    elif channel == 'color':
        start, end = 0, 3
    elif channel == 'depth':
        start, end = 3, 4

    ssims = []
    for t in range(HORIZON):
        true_img = true_imgs[t]
        pred_img = pred_imgs[t]
        ssim = 0
        for c in range(start, end):
            X = true_img[:,:,c]
            Y = pred_img[:,:,c]
            if channel == 'depth':
                # Makes it 3 channel, but we only want the 1st (it's replicated).
                X = (make_depth_img(X))[:,:,0]
                Y = (make_depth_img(Y))[:,:,0]
            ssim += ssim_metric(X=X, Y=Y)
        ssim /= (end-start)
        assert -1 <= ssim <= 1, ssim
        ssims.append(ssim)
    return ssims

def save_images_get_ssim(datatype):
    """Collect a bunch of images of ground truth sequences followed by predictions.
    Also, might as well compute SSIM since everything is already nicely loaded.

    Ideally we can then paste these together for a figure.
    BTW the ground truths should be the same among the different models, since we
    tested on the same data, so we can do another sanity check. Same for actions.
    All of these should be with context of 1, and predicting the next 5 frames.

    BTW: to make a figure we can just take screenshots of appropriate segments?
    Might make it easier since I'm grouping everything anyway.
    """
    assert datatype in ['fabric-random', 'fabric-01-2021'], datatype
    nb_eps = len(SV2P_10_fr)

    # SSIM: dict, maps model type (e.g., 'svg') --> ssim matrix of dim (num_eps, HORIZON)
    # We can then average across columns to get SSIM for 1-ahead predictions, 2-ahead, etc.
    # Imagine these as a grid of (episodes) x (horizon). The item in the 'top left' is for
    # 1st episode, 1-ahead prediction. In that episode, we have `N` sets of predictions, so
    # these store the average of the k-ahead predictions, k in {1,2,3,4,5=HORIZON}. BUT,
    # because some episodes are too short, it's easier to append a bunch of HORIZON-length
    # lists and then call np.array() on it later.
    SSIM_C = {'sv2p_01': [], 'sv2p_10': [], 'svg': [],}
    SSIM_D = {'sv2p_01': [], 'sv2p_10': [], 'svg': [],}

    for ep in range(nb_eps):
        if ep % 25 == 0:
            print(f'checking predictions on episode {ep}')

        # Predictions have shape (N, H=HORIZON, 56, 56, 4), N is number of predictions.
        # I load a bunch of un-necessary stuff, just make sure 'pred' is correct!
        if datatype == 'fabric-random':
            gt_obs          = SV2P_01_fr[ep]['gt_obs']
            gt_obs_0        = SV2P_10_fr[ep]['gt_obs']
            gt_obs_1        = SVG_fr[ep]['gt_obs']
            gt_context      = SV2P_01_fr[ep]['contexts']
            gt_context_0    = SV2P_10_fr[ep]['contexts']
            gt_context_1    = SVG_fr[ep]['contexts']
            gt_act          = SV2P_01_fr[ep]['act']
            gt_act_0        = SV2P_10_fr[ep]['act']
            gt_act_1        = SVG_fr[ep]['act']
            pred_sv2p_01_fr = SV2P_01_fr[ep]['pred']
            pred_sv2p_10_fr = SV2P_10_fr[ep]['pred']
            pred_svg_fr     = SVG_fr[ep]['pred']
            # We load multiple versions partly as an extra sanity check.
            assert gt_obs.shape     == gt_obs_0.shape     == gt_obs_1.shape
            assert gt_context.shape == gt_context_0.shape == gt_context_1.shape
            assert gt_act.shape     == gt_act_0.shape     == gt_act_1.shape
            assert np.allclose(gt_act, gt_act_0)
            assert np.allclose(gt_act, gt_act_1)
        elif datatype == 'fabric-01-2021':
            gt_obs_0          = SV2P_10_fnew[ep]['gt_obs']
            gt_obs_1          = SVG_fnew[ep]['gt_obs']
            gt_context_0      = SV2P_10_fnew[ep]['contexts']
            gt_context_1      = SVG_fnew[ep]['contexts']
            gt_act_0          = SV2P_10_fnew[ep]['act']
            gt_act_1          = SVG_fnew[ep]['act']
            pred_sv2p_10_fnew = SV2P_10_fnew[ep]['pred']
            pred_svg_fnew     = SVG_fnew[ep]['pred']
            # We load multiple versions partly as an extra sanity check.
            assert gt_obs_0.shape     == gt_obs_1.shape
            assert gt_context_0.shape == gt_context_1.shape
            assert gt_act_0.shape     == gt_act_1.shape
            assert np.allclose(gt_act_0, gt_act_1)

        # Actually cannot use len(gt_act), that's not the number of actions in an episode
        # Oh, the N can be negative so if that's the case shape of gt_context is (0,)
        num_obs = len(gt_obs_0)
        N = num_obs - HORIZON
        assert N <= gt_context_0.shape[0], f'{N} vs {gt_context_0.shape}'

        # New image for each episode? Actually maybe consider splitting depth vs color?
        # num_steps is number of actions but also need to add padding
        ws = 5                          # whitespace padding
        IM_WIDTH  = (num_obs) * 56 + (num_obs+1) * ws

        # Actually add another column for some SSIM measurements.
        IM_WIDTH += (ws + 56)

        # What do we want in the image? 2 rows for GT color and depth, then 2 for each
        # prediction of SV2P (1 and 10 masks) on the RGB image? N = number of predictions.
        # Then do 1 more row after that just to give empty breathing room to make sure we
        # did this right ... (sanity checks). Update: adding 2 more for depth, and we also
        # have to add the SVG predictions! But don't have 1 mask case for one case.
        if datatype == 'fabric-random':
            nrows = 2 + (N * 6) + 1
        elif datatype == 'fabric-01-2021':
            nrows = 2 + (N * 4) + 1
        nrows = max(3, nrows)  # handle negative N
        IM_HEIGHT = (nrows+1)*ws + (nrows*56)

        # Finally make this PIL Image.
        t_img = Image.new(mode='RGB', size=(IM_WIDTH,IM_HEIGHT), color=(255,255,255))
        draw = ImageDraw.Draw(t_img)

        # Ground truth sequence at the top
        for t in range(num_obs):
            _w = (t * 56) + ((t+1) * ws)
            _h = ws
            frame_rgb = gt_obs_0[t, :, :, :3]
            frame_d   = gt_obs_0[t, :, :, 3:]
            frame_d   = make_depth_img(frame_d)
            t_img.paste(PIL.Image.fromarray(frame_rgb), (_w, _h)          )
            t_img.paste(PIL.Image.fromarray(frame_d),   (_w, _h + 56 + ws))

        # Iterate through all PREDICTIONS, adding them sequentially. Update: actually
        # we have to check to see if the episode has enough length at all. If an episode
        # has under 5 actions then we could not do prediction on it.
        row = 2
        hoff = 0

        # SSIM FOR THIS EPISODE ONLY.
        ep_ssims_sv2p_01_c = []
        ep_ssims_sv2p_10_c = []
        ep_ssims_svg_c     = []
        ep_ssims_sv2p_01_d = []
        ep_ssims_sv2p_10_d = []
        ep_ssims_svg_d     = []

        for t in range(0, N):
            # NOTE: we can reorder the way we lay out the rows (e.g., I do SV2P then SVG)
            # if we want, just make sure `row` gets incremented / decremented as needed.
            if datatype == 'fabric-random':
                sv2p_01   = pred_sv2p_01_fr[t,:,:,:,:]  # (H,56,56,4)
                sv2p_10   = pred_sv2p_10_fr[t,:,:,:,:]  # (H,56,56,4)
                svg       = pred_svg_fr[t,:,:,:,:]      # (H,56,56,4)
                context   = gt_context[t,:,:,:]
                context_0 = gt_context_0[t,:,:,:]
                context_1 = gt_context_1[t,:,:,:]
            elif datatype == 'fabric-01-2021':
                sv2p_10   = pred_sv2p_10_fnew[t,:,:,:,:]  # (H,56,56,4)
                svg       = pred_svg_fnew[t,:,:,:,:]      # (H,56,56,4)
                context_0 = gt_context_0[t,:,:,:]
                context_1 = gt_context_1[t,:,:,:]
            # Now we know we can use `sv2p_10` and `svg`, and `sv2p_01` for the old data.
            # Here is also the ground truth for this full set of predictions.
            true_imgs = gt_obs_0[t:t+HORIZON, :, :, :]
            assert true_imgs.shape == sv2p_10.shape == svg.shape, true_imgs.shape

            # Starting width for this group, gets shifted for every batch of predictions.
            _w_start = ws + hoff

            # SV2P ONE MASK. Context, then predicted images.
            if datatype == 'fabric-random':
                img = PIL.Image.fromarray(context[:,:,:3])
                _h = (row+1)*ws + row*56
                t_img.paste(img, (_w_start, _h))
                for h in range(HORIZON):
                    img = PIL.Image.fromarray(sv2p_01[h, :, :, :3])
                    _w = (h+2)*ws + (h+1)*56 + hoff
                    t_img.paste(img, (_w, _h))
                ssims = eval_ssim(true_imgs=true_imgs, pred_imgs=sv2p_01, channel='color')
                draw_ssim(draw, IM_WIDTH, ws, hoff=_h, groupname='SV2P 01', ssims=ssims)
                ep_ssims_sv2p_01_c.append(ssims)
                row += 1

            # Next row, SV2P 10 MASK (Edit: putting row += 1 up earlier)
            img = PIL.Image.fromarray(context_0[:,:,:3])
            _h = (row+1)*ws + row*56
            t_img.paste(img, (_w_start, _h))
            for h in range(HORIZON):
                img = PIL.Image.fromarray(sv2p_10[h, :, :, :3])
                _w = (h+2)*ws + (h+1)*56 + hoff
                t_img.paste(img, (_w, _h))
            ssims = eval_ssim(true_imgs=true_imgs, pred_imgs=sv2p_10, channel='color')
            draw_ssim(draw, IM_WIDTH, ws, hoff=_h, groupname='SV2P 10', ssims=ssims)
            ep_ssims_sv2p_10_c.append(ssims)

            # Next row, SVG
            row += 1
            img = PIL.Image.fromarray(context_1[:,:,:3])
            _h = (row+1)*ws + row*56
            t_img.paste(img, (_w_start, _h))
            for h in range(HORIZON):
                img = PIL.Image.fromarray(svg[h, :, :, :3])
                _w = (h+2)*ws + (h+1)*56 + hoff
                t_img.paste(img, (_w, _h))
            ssims = eval_ssim(true_imgs=true_imgs, pred_imgs=svg, channel='color')
            draw_ssim(draw, IM_WIDTH, ws, hoff=_h, groupname='SVG', ssims=ssims)
            ep_ssims_svg_c.append(ssims)

            # SV2P ONE MASK, DEPTH.
            if datatype == 'fabric-random':
                row += 1
                img = PIL.Image.fromarray( make_depth_img(context[:,:,3:]) )
                _h = (row+1)*ws + row*56
                t_img.paste(img, (_w_start, _h))
                for h in range(HORIZON):
                    img = PIL.Image.fromarray( make_depth_img(sv2p_01[h, :, :, 3:]) )
                    _w = (h+2)*ws + (h+1)*56 + hoff
                    t_img.paste(img, (_w, _h))
                ssims = eval_ssim(true_imgs=true_imgs, pred_imgs=sv2p_01, channel='depth')
                draw_ssim(draw, IM_WIDTH, ws, hoff=_h, groupname='SV2P 01', ssims=ssims)
                ep_ssims_sv2p_01_d.append(ssims)

            # Next row, SV2P 10 MASK, DEPTH
            row += 1
            img = PIL.Image.fromarray( make_depth_img(context_0[:,:,3:]) )
            _h = (row+1)*ws + row*56
            t_img.paste(img, (_w_start, _h))
            for h in range(HORIZON):
                img = PIL.Image.fromarray( make_depth_img(sv2p_10[h, :, :, 3:]) )
                _w = (h+2)*ws + (h+1)*56 + hoff
                t_img.paste(img, (_w, _h))
            ssims = eval_ssim(true_imgs=true_imgs, pred_imgs=sv2p_10, channel='depth')
            draw_ssim(draw, IM_WIDTH, ws, hoff=_h, groupname='SV2P 10', ssims=ssims)
            ep_ssims_sv2p_10_d.append(ssims)

            # Next row, SVG, DEPTH
            row += 1
            img = PIL.Image.fromarray( make_depth_img(context_1[:,:,3:]) )
            _h = (row+1)*ws + row*56
            t_img.paste(img, (_w_start, _h))
            for h in range(HORIZON):
                img = PIL.Image.fromarray( make_depth_img(svg[h, :, :, 3:]) )
                _w = (h+2)*ws + (h+1)*56 + hoff
                t_img.paste(img, (_w, _h))
            ssims = eval_ssim(true_imgs=true_imgs, pred_imgs=svg, channel='depth')
            draw_ssim(draw, IM_WIDTH, ws, hoff=_h, groupname='SVG', ssims=ssims)
            ep_ssims_svg_d.append(ssims)

            # Prep for next row in next for loop.
            row += 1
            hoff += (ws + 56)

        # SSIMs if at least 1 prediction. Computed {1-ahead, ..., H-ahead} SSIMs for this episode.
        if N >= 1:
            ep_ssims_sv2p_01_c = np.array( ep_ssims_sv2p_01_c )
            ep_ssims_sv2p_10_c = np.array( ep_ssims_sv2p_10_c )
            ep_ssims_svg_c     = np.array( ep_ssims_svg_c     )
            ep_ssims_sv2p_01_d = np.array( ep_ssims_sv2p_01_d )
            ep_ssims_sv2p_10_d = np.array( ep_ssims_sv2p_10_d )
            ep_ssims_svg_d     = np.array( ep_ssims_svg_d     )
            assert ep_ssims_svg_c.shape == (num_obs - HORIZON, HORIZON), \
                    f'{num_obs}, {ep_ssims_svg_c.shape}'

            if datatype == 'fabric-random':
                ep_ssims_sv2p_01_c = np.mean(ep_ssims_sv2p_01_c, axis=0)
                ep_ssims_sv2p_01_d = np.mean(ep_ssims_sv2p_01_d, axis=0)
            ep_ssims_sv2p_10_c = np.mean(ep_ssims_sv2p_10_c, axis=0)
            ep_ssims_svg_c     = np.mean(ep_ssims_svg_c    , axis=0)
            ep_ssims_sv2p_10_d = np.mean(ep_ssims_sv2p_10_d, axis=0)
            ep_ssims_svg_d     = np.mean(ep_ssims_svg_d    , axis=0)

            SSIM_C['sv2p_01'].append( ep_ssims_sv2p_01_c )
            SSIM_C['sv2p_10'].append( ep_ssims_sv2p_10_c )
            SSIM_C['svg'    ].append( ep_ssims_svg_c     )
            SSIM_D['sv2p_01'].append( ep_ssims_sv2p_01_d )
            SSIM_D['sv2p_10'].append( ep_ssims_sv2p_10_d )
            SSIM_D['svg'    ].append( ep_ssims_svg_d     )

        # Save in BGR format, which means saving with OpenCV.
        estr = str(ep).zfill(3)
        sstr = str(num_obs).zfill(2)
        img_path = f'preds_{datatype}_ep_{estr}_obs_{sstr}_rgbd.png'
        if datatype == 'fabric-random':
            head_dir = join('results_frand_3', args.svg_model)
            os.makedirs(head_dir, exist_ok=True)
            img_path = join(head_dir, img_path)
        else:
            head_dir = join('results_fnew_3', args.svg_model)
            os.makedirs(head_dir, exist_ok=True)
            img_path = join(head_dir, img_path)
        #t_img.save(img_path)  # RGB, but we want BGR, hence do these two:
        t_img_np = np.array(t_img)
        cv2.imwrite(img_path, t_img_np)

    # Report SSIM metrics
    print(f'\nSome SSIM metrics for: {args}:\n')
    print('Length of lists: {} <= episodes {}'.format(len(SSIM_C['sv2p_10']), nb_eps))

    # Form numpy arrays.
    if datatype == 'fabric-random':
        SSIM_C['sv2p_01'] = np.array(SSIM_C['sv2p_01'])
        SSIM_D['sv2p_01'] = np.array(SSIM_D['sv2p_01'])
    SSIM_C['sv2p_10'] = np.array(SSIM_C['sv2p_10'])
    SSIM_C['svg'    ] = np.array(SSIM_C['svg'    ])
    SSIM_D['sv2p_10'] = np.array(SSIM_D['sv2p_10'])
    SSIM_D['svg'    ] = np.array(SSIM_D['svg'    ])

    # Print values. To make things easier to copy and paste, do color then depth.
    if datatype == 'fabric-random':
        print('SV2P01, C. {}'.format( np.mean(SSIM_C['sv2p_01'], axis=0)) )
    print('SV2P10, C. {}'.format( np.mean(SSIM_C['sv2p_10'], axis=0)) )
    print('SVG, C.    {}'.format( np.mean(SSIM_C['svg'],     axis=0)) )
    if datatype == 'fabric-random':
        print('SV2P01, D. {}'.format( np.mean(SSIM_D['sv2p_01'], axis=0)) )
    print('SV2P10, D. {}'.format( np.mean(SSIM_D['sv2p_10'], axis=0)) )
    print('SVG, D.    {}'.format( np.mean(SSIM_D['svg'],     axis=0)) )
    print('\nNote, shape SV2P, SVG: {}, {}'.format(SSIM_C['sv2p_10'].shape, SSIM_C['svg'].shape))

    print('\nNow standard deviations among the episodes:\n')
    if datatype == 'fabric-random':
        print('SV2P01, C. {}'.format( np.std(SSIM_C['sv2p_01'], axis=0)) )
    print('SV2P10, C. {}'.format( np.std(SSIM_C['sv2p_10'], axis=0)) )
    print('SVG, C.    {}'.format( np.std(SSIM_C['svg'],     axis=0)) )
    if datatype == 'fabric-random':
        print('SV2P01, D. {}'.format( np.std(SSIM_D['sv2p_01'], axis=0)) )
    print('SV2P10, D. {}'.format( np.std(SSIM_D['sv2p_10'], axis=0)) )
    print('SVG, D.    {}'.format( np.std(SSIM_D['svg'],     axis=0)) )
    print()

if __name__ == "__main__":
    save_images_get_ssim(datatype=args.datatype)


__author__ = 'fcaramia'
import optparse
from readinputs import *
from utils import *
from writeresults import *
import networkx as nx

def main():

    parser = optparse.OptionParser()

    parser.add_option('-i', '--interactome', type='string', dest="interactome", help="interactome file")
    parser.add_option('-c', '--config', type='string', dest="config_file", help="config file")
    parser.add_option('-s', '--siRNA', type='string', dest="siRNA_file", help="siRNA candidates file")
    parser.add_option('-o', '--outfile', dest='out', help="output file", default='out.txt', type='string')
    parser.add_option('-m', '--miRNA', dest='miRNA_file', help="miRNA file", type='string')
    (options, args) = parser.parse_args()

    if not options.config_file:
        parser.error("config file is needed")

    if not options.siRNA_file:
        parser.error("siRNA file is needed")

    # Read Config
    config = {}
    config = read_config(options.config_file, config)

    # Print config
    for k in config:
        print(k, config[k])

    if not validate_config(config):
        parser.error("Invalid config file")

    # Read Candidates
    candidates = {}
    candidates = read_candidates(options.siRNA_file, candidates)

    miRNAs = {}
    # Read miRNA
    if options.miRNA_file:
        miRNAs = read_candidates(options.miRNA_file, miRNAs)
        # Check sign of miRNAs
        miRNAs = check_sign_of_candidates(miRNAs, parser)

    interactome = {}
    # Read interactome
    if options.interactome:
        interactome = read_interactome(options.interactome, interactome)

    # Check sign of candidates
    candidates = check_sign_of_candidates(candidates, parser)
    candidates_raw = candidates.copy()
    # set graph
    graph = nx.DiGraph()
    mir_graph = nx.DiGraph()

    # Read DBs
    marks = {}

    if config["mirTarBase"] != '':
        print("Loading mirTarBase")
        mir_graph = read_db(config["gene_db_dir"] + config['mirTarBase'], mir_graph)

    for db in config['gene_dbs']:

        graph = read_db(config["gene_db_dir"] + db, graph, config['directed'], float(config["min_score"]))

    # Mark genes that are connected to each other
    if config["expand"] == 0:
        # Check only for direct regulation
        marks = check_reg_graph(list(candidates.keys()), graph, marks)
    else:
        if config['directed'] == 'no':
            print("Warning: creating a non directed network could add false positives")
        print("Expanding Search")
        print(len(candidates), "siRNA candidates")
        print(len(graph.nodes()), "nodes")
        print(len(graph.edges()), "edges")

        marks = check_graph(list(candidates.keys()), graph, marks, config['expand'])

    i = 0
    for r in marks:
        i += len(marks[r])

    not_in_db = 0
    nodes = list(graph.nodes())
    for c in candidates:
        if c not in nodes:
            not_in_db += 1

    print("Candidate genes with no evidence for selected score: " + str(not_in_db))

    # Generate Network score for marked genes
    norm_network_scores_neg = {}
    norm_network_scores_pos = {}
    paths_for_scoring = marks
    network_scores_neg = {}
    network_scores_pos = {}

    if len(marks) > 0:

        if config['filter_by_si']:
            paths_for_scoring = mark_for_scoring(candidates, marks, config['std_dev_val'])
        # Generate network scores
        get_network_scores(paths_for_scoring, graph, config['score_reduce_fun'], network_scores_neg, network_scores_pos)

        if config['scoring'] in ["neg", "pos_neg"]:
            norm_network_scores_neg = normalize_scores(network_scores_neg, 0.0, float(config["network"]), 0.0)

        if config['scoring'] in ["pos", "pos_neg"]:
            norm_network_scores_pos = normalize_scores(network_scores_pos, 0.0, float(config["network"]), 0.0)

    # Generate miRNA scores
    norm_mir_scores = {}
    if len(mir_graph) > 0 and len(miRNAs) > 0:
        # Search predecessors of genes and compute mir scores
        mir_scores = get_mir_scores(candidates, mir_graph, miRNAs, config['mir_score_select'])
        norm_mir_scores = normalize_scores(mir_scores, 0.0, float(config["miRNA"]), 0.0)

    norm_si_scores = normalize_scores(candidates, 0.0, float(config["siRNA"]), float(config["std_capping"]))

    total_scores = process_total_scores(norm_si_scores, norm_network_scores_neg, norm_network_scores_pos,
                                        norm_mir_scores)

    print_results(options.out, graph, paths_for_scoring, mir_graph, total_scores, norm_si_scores,
                  norm_network_scores_neg, norm_network_scores_pos, norm_mir_scores, config, candidates_raw,
                  network_scores_neg, network_scores_pos, mir_scores, miRNAs, interactome)

    print_stats(norm_si_scores, norm_network_scores_neg, norm_network_scores_pos, norm_mir_scores, total_scores, options.out)

if __name__ == '__main__':
    main()


#!/usr/bin/env python
import os
import re
from typing import Any, Dict

from setuptools import find_namespace_packages, setup

this_directory = os.path.abspath(os.path.dirname(__file__))

with open(os.path.join(this_directory, "README.md"), encoding="utf-8") as f:
    long_description = f.read()

package_name = "dbt-athena-community"

# get version from a separate file
def _get_plugin_version_dict() -> Dict[str, Any]:
    _version_path = os.path.join(this_directory, "dbt", "adapters", "athena", "__version__.py")
    _semver = r"""(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)"""
    _pre = r"""((?P<prekind>a|b|rc)(?P<pre>\d+))?"""
    _version_pattern = rf"""version\s*=\s*["']{_semver}{_pre}["']"""
    with open(_version_path) as f:
        match = re.search(_version_pattern, f.read().strip())
        if match is None:
            raise ValueError(f"invalid version at {_version_path}")
        return match.groupdict()

def _get_package_version() -> str:
    parts = _get_plugin_version_dict()
    return f'{parts["major"]}.{parts["minor"]}.{parts["patch"]}'

dbt_version = "1.6"
package_version = _get_package_version()
description = "The athena adapter plugin for dbt (data build tool)"

if not package_version.startswith(dbt_version):
    raise ValueError(f"Invalid setup.py: package_version={package_version} must start with dbt_version={dbt_version}")

setup(
    name=package_name,
    version=package_version,
    description=description,
    long_description=long_description,
    long_description_content_type="text/markdown",
    platforms="any",
    license="Apache License 2.0",
    license_files=("LICENSE.txt",),
    url="https://github.com/dbt-athena/dbt-athena",
    packages=find_namespace_packages(include=["dbt", "dbt.*"]),
    include_package_data=True,
    install_requires=[
        # In order to control dbt-core version and package version
        "boto3~=1.26",
        "boto3-stubs[athena,glue,lakeformation,sts]~=1.26",
        "dbt-core~=1.6.0",
        "pyathena>=2.25,<4.0",
        "pydantic>=1.10,<3.0",
        "tenacity~=8.2",
    ],
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "License :: OSI Approved :: Apache Software License",
        "Operating System :: Microsoft :: Windows",
        "Operating System :: MacOS :: MacOS X",
        "Operating System :: POSIX :: Linux",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Programming Language :: Python :: 3.12",
    ],
    python_requires=">=3.8",
)


import json

import cv2
import numpy as np
import pandas as pd
from pathlib import Path
import matplotlib.pyplot as plt

class HeatMap:
    def __init__(self, plates_path):
        self.plates_path = Path(plates_path)

    def get_max_plate_shape(self):
        plate_shapes_y = []
        plate_shapes_x = []
        for folder_path in self.plates_path.iterdir():
            with open(str(folder_path / 'plate_info.json'), 'r') as f:
                plate_info = json.load(f)
            plate_shape = [int(plate_info['info']['plate_info']['plate_shape'][0] -
                               plate_info['info']['plate_info']['margin_boundaries']['upper_boundary'] -
                               plate_info['info']['plate_info']['margin_boundaries']['bottom_boundaries']),
                           int(plate_info['info']['plate_info']['plate_shape'][1] -
                               plate_info['info']['plate_info']['margin_boundaries']['left_boundary'] -
                               plate_info['info']['plate_info']['margin_boundaries']['right_boundary'])]
            plate_shapes_y.append(plate_shape[0])
            plate_shapes_x.append(plate_shape[1])
        return [max(plate_shapes_y), max(plate_shapes_x)]

    @staticmethod
    def get_plate_info(df, plate_number):
        results_dict = {}
        temp_dict = {}
        for _, row in df.iterrows():
            temp_dict[row['crop_id_plate']] = {'x1': row['bounding_box_x'], 'y1': row['bounding_box_y'],
                                               'w': row['defect_width'], 'h': row['defect_height'],
                                               'area': row['defect_width'] * row['defect_height'],
                                               'defect': row['is_defect'], 'type': row['defect_type'],
                                               'b_nb': row['buffer_number'], 'c_nb': row['crop_id_buffer']}
        results_dict[plate_number] = temp_dict
        return results_dict

    @staticmethod
    def draw_defects_per_plate(info_plate, temp):
        for plate_number, val in info_plate.items():
            for id in val.keys():
                x1 = info_plate[plate_number][id]['x1']
                x2 = x1 + info_plate[plate_number][id]['w']
                y1 = info_plate[plate_number][id]['y1']
                y2 = y1 + info_plate[plate_number][id]['h']
                temp = cv2.rectangle(temp, (x1, y1), (x2, y2), 1, -1)
        return temp

    def _sum_defects_of_plates(self, path_list, background):
        drawn_defects = background.copy()
        temp = background.copy()
        for folder_path in path_list:
            defects = pd.read_csv(folder_path / 'defects_in_plate.csv')
            plate_info = self.get_plate_info(defects, folder_path.name)
            print(folder_path.name)
            temp = self.draw_defects_per_plate(plate_info, temp)
            drawn_defects = drawn_defects + temp
        return drawn_defects

    def draw_plate_defects(self, max_shape):
        background = np.zeros(max_shape, dtype='int32')
        path_list = [p for p in self.plates_path.iterdir()]
        results = self._sum_defects_of_plates(path_list, background)
        return results

    @staticmethod
    def draw_heatmap(plates_defects):
        plt.imshow(plates_defects * 1000, cmap='YlOrRd', interpolation='bilinear')
        plt.axis('off')
        plt.show()

if __name__ == '__main__':
    run_path = '/home/schneian/data/fis_knb/08sep20/daimler-mfa-2-brush-3w177-0001-2b45'
    heatmap = HeatMap(run_path)
    shape = heatmap.get_max_plate_shape()
    plates_sum = heatmap.draw_plate_defects(shape)
    heatmap.draw_heatmap(plates_sum)


import os

from sqlalchemy import text

from application import db

class Topic(db.Model):
    __tablename__ = "topic"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    created = db.Column(db.DateTime, default=db.func.current_timestamp())

    account_id = db.Column(db.Integer, db.ForeignKey('account.id'), nullable=False)
    account = db.relationship("User", back_populates="topics", lazy=True)

    area_id = db.Column(db.Integer, db.ForeignKey('area.id'), nullable=False)
    area = db.relationship("Area", back_populates="topics", lazy=True)

    messages = db.relationship("Message", back_populates='topic', lazy=True)

    def __init__(self, name, area_id):
        self.name = name
        self.area_id = area_id

    def last_post(self):
        return self.messages[-1]

    def last_post_by(self):
        return self.last_post().account.name

    def last_post_created(self):
        return self.last_post().created

    @staticmethod
    def find_topic_count():
        stmt = text("SELECT COUNT(*) as c FROM topic LIMIT 1")
        res = db.engine.execute(stmt)
        for row in res:
            return row[0]

    @staticmethod
    def find_hot_topic():
        stmt = None
        if os.environ.get("HEROKU"):
            stmt = text("SELECT topic.name, COUNT(*) as c FROM message"
                        " JOIN topic on message.topic_id = topic.id"
                        " WHERE message.created > now() - INTERVAL '1 WEEK'"
                        " GROUP BY topic.id"
                        " ORDER BY c DESC"
                        " LIMIT 1"
                        )

        else:
            stmt = text("SELECT topic.name, COUNT(*) as c FROM message"
                        " JOIN topic on message.topic_id = topic.id"
                        " WHERE message.created > datetime('now', '-7 day')"
                        " GROUP BY topic.id"
                        " ORDER BY c DESC"
                        " LIMIT 1"
                        )
        res = db.engine.execute(stmt)
        for row in res:
            return row[0]


from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from django.shortcuts import render
from django.http import Http404
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from el_pagination.views import AjaxListView
from django.contrib.auth.decorators import login_required, user_passes_test

from .models import News
from .forms import NewsForm

def index(request):
    context = {'news_list_1': News.objects.order_by('-pub_date')[1:3],
               'news_list_2': News.objects.order_by('-pub_date')[3:5],
    		   'main_news': News.objects.order_by('-pub_date')[0]}
    return render(request, 'home/index.html', context)

def news_feed(request,
    template='home/news-feed-list.html',
    page_template='home/news-feed-page.html'):
    context = {
        'news_list': News.objects.order_by('-pub_date'),
        'page_template': page_template,
    }
    if request.is_ajax():
        template = page_template
    return render(request, template, context)

def news(request):
    id_parameter = request.GET['id']
    context = {'news': News.objects.filter(id=id_parameter)[0]}
    return render(request, 'home/news.html', context)

@login_required
@user_passes_test(lambda u: u.groups.filter(name='CASD').count() == 1, login_url='/')
def news_manager(request):
    context = {
        'news_list': News.objects.order_by('-pub_date')
    }
    return render(request, 'home/manager/news-manager.html', context)

def news_create(request):
    form = NewsForm(request.POST or None)

    if form.is_valid():
        form.save()
    else:
        form = NewsForm()

    context = {
        'form': form,
    }
    return render(request, 'home/manager/news-add.html', context)

def news_remove(request, pk):
    news = get_object_or_404(News, pk=pk)
    news.delete()

    context = {
        'news_list': News.objects.order_by('-pub_date')
    }
    return render(request, 'home/manager/news-manager.html', context)

def news_update(request, pk):
    news = get_object_or_404(News, pk=pk)
    if request.method == 'POST':
        form = NewsForm(request.POST, instance=news)
        if form.is_valid():
            form.save()
    else:
        form = NewsForm(instance=news)

    context = {
        'form': form,
        'news': news,
    }
    return render(request, 'home/manager/news-edit.html', context)


import os
import json
import shutil
import numpy as np
from jobmanager.psi4_utils.run import run_general_hfx

psi4_config = {'bashrc': '/home/crduan/.bashrc',
               'conda_env': '/home/crduan/miniconda/envs/mols_py36'}
with open("psi4_config.json", "r") as f:
    psi4_config.update(json.load(f))
alphalist = [20, 15, 10, 5, 2]
rescued = 0
success_count = 0
basedir = os.getcwd()
for ii, functional in enumerate(psi4_config["functional"]):
    failed = False
    print("===%d: %s===" % (ii, functional))
    with open(functional + "/output.dat", "r") as fo:
        txt = "".join(fo.readlines())
    if 'PsiException: Could not converge SCF iterations' in txt or txt == "":
        success = False
        e_dict = {}
        e0 = np.nan
        for jj, alpha in enumerate(alphalist):
            print(alpha)
            if jj == 0:
                wfn = "../b3lyp/wfn.180.npy"
                wfn_b = "b3lyp/wfn.180.npy"
            else:
                wfn = "../%s/wfn.180.npy" % (functional + "-%d" %
                                             alphalist[jj-1])
                wfn_b = "%s/wfn.180.npy" % (functional + "-%d" %
                                            alphalist[jj-1])
            if not os.path.isdir(functional + "-%d" % alpha) and not failed:
                os.makedirs(functional + "-%d" % alpha)
                shutil.copyfile(psi4_config['xyzfile'], functional + "-%d" %
                                alpha + '/' + psi4_config['xyzfile'])
                success = run_general_hfx(psi4_config, functional, hfx=alpha, wfn=wfn)
                print("success: ", success)
                if success:
                    success_count += 1
                else:
                    failed = True
            else:
                print("attempted rescue: ", functional)
                resubed = False
                if not os.path.isfile(functional + "-%d" % alpha + "/output.dat"):
                    resubed = True
                else:
                    with open(functional + "-%d" % alpha + "/output.dat", "r") as fo:
                        txt = "".join(fo.readlines())
                    if "==> Iterations <==" not in txt:
                        resubed = True
                if resubed and os.path.isfile(wfn_b):
                    print("previously errored out. resubmitting...")
                    success = run_general_hfx(psi4_config, functional, hfx=alpha, wfn=wfn)
                    print("success: ", success)
                    if success:
                        success_count += 1
                    else:
                        failed = True
                elif ('PsiException: Could not converge SCF iterations') not in txt and (not os.path.isfile(functional + "-%d" % alpha + "/wfn.180.npy")):
                    _functional = functional + "-%d" % alpha
                    if not os.path.isfile(_functional + "/output-timeout.dat"):
                        shutil.copy(_functional + "/output.dat",
                                    _functional + "/output-timeout.dat")
                        print("Time out, direct resub...")
                        success = run_general_hfx(psi4_config, functional, hfx=alpha, wfn=wfn)
                        print("success: ", success)
                        if success:
                            success_count += 1
                        else:
                            failed = True
                    else:
                        failed = True
                        print("Already submit once for timeout.")
                else:
                    print("give up resubmission.")
        if not failed:
            rescued += 1
            print("rescued: ", functional)
    else:
        rescued += 1
        print("%s has already succeeded!" % functional)
print("all finished!")


import control
import random
import adventure
import settings
import menu
import character
import cast
from move_types import ATK, DEF, FIN
from control import takeover
from core import term

VIEW_UPPER = (0,0,settings.BAT_WIDTH, settings.VIEW_HEIGHT//2)
VIEW_LOWER = (0,0+settings.VIEW_HEIGHT//2,
        settings.BAT_WIDTH, settings.VIEW_HEIGHT//2)
VIEW_LEFT = (0,0,settings.BAT_WIDTH//2, settings.VIEW_HEIGHT)
VIEW_RIGHT = (0+settings.BAT_WIDTH//2,0,settings.BAT_WIDTH//2,
        settings.VIEW_HEIGHT)

class BattleRecord:
    def __init__(self):
        self.koed = False
        self.num_kos = 0
        self.lost = False
        self.forefeit = False
        self.damage_dealt = 0
        self.damage_taken = 0
        self.participated = True

def calc_damage(mon, atk, defns, state):
    damage = 2
    if state.mon_type in atk.strong_vs:
        damage += 2
    if state.mon_type in atk.resisted_by:
        damage -= 2
    first_def = True
    if defns:
        for def_type in defns.strong_vs:
            if atk.type1 == def_type or atk.type2 == def_type:
                if first_def:
                    damage -= 2
                    first_def = False
                else:
                    damage -= 1
    if damage > 6:
        damage = 6
    if damage < 0:
        damage = 0
    return damage
damage_descriptors = [
        ("nothing",),
        ("decent",),
        ("strong",),
        ("powerful",),
        ("mighty",),
        ("devastating",),
        ("fearsome",),
        ]
class Battle:
    def __init__(self, opposing_team):
        self.opp = opposing_team
        self.current_opp = opposing_team[0]
        available_mons = [m for m in adventure.current.party if m.can_battle]
        self.current_mon = available_mons[0]
        self.player_actions = []
        self.opp_actions = []
        self.player_tag = None
        self.opp_tag = None
        for mon in adventure.current.party + self.opp:
            mon.battle_record = BattleRecord()
    def show_message(self, lines):
        msgbox = menu.MessageBox(lines, bg = self)
        control.takeover(msgbox, clear = False)
    def update(self, key):
        self.player_actions = []
        self.opp_actions = []
        actions_picked = 0
        action_menu = menu.FloatingMenu(0,settings.VIEW_HEIGHT-4,[
            ("Attack", "atk"),
            ("Defend", "def"),
            ("Finish", "fin"),
            ("Tag-out","tag"),
            ("Forfeit","for"),
            ], bg = self, title = "Action 1")
        cancel_option = [("Cancel", "x")]
        mon = self.current_mon
        while actions_picked < 2:
            action_menu.title = f"Action {1+actions_picked}"
            action = takeover(action_menu)
            if action == "atk":
                options =  list([(move.name, move) for move in mon.attacks])
                options += cancel_option
                info = []
                for move in mon.attacks:
                    t1 = move.type1.name
                    t2 = move.type2.name
                    strong = list(set([typ.name for typ in move.strong_vs]))
                    resist = list(set([typ.name for typ in move.resisted_by]))
                    move_info = [f"{t1} / {t2}",
                            f"Strong vs: {', '.join(strong)}",
                            f"Resisted by: {', '.join(resist)}"]
                    info.append(move_info)
                info.append("")
                move_menu = menu.FloatingMenu(0, settings.VIEW_HEIGHT,
                        options, bg = self, item_info = info)
                move = takeover(move_menu)
                if move != "x":
                    self.player_actions.append(move)
                    actions_picked += 1
                    action_menu.remove(("Attack", "atk"))
            elif action == "def":
                options =  list([(move.name, move) for move in mon.defences])
                options += cancel_option
                info = []
                for move in mon.defences:
                    t1 = move.type1.name
                    t2 = move.type2.name
                    strong = list(set([typ.name for typ in move.strong_vs]))
                    resist = list(set([typ.name for typ in move.resisted_by]))
                    move_info = [f"{t1} / {t2}",
                            f"Strong vs: {', '.join(strong)}"]
                    info.append(move_info)
                info.append("")
                move_menu = menu.FloatingMenu(0, settings.VIEW_HEIGHT,
                        options, bg = self, item_info = info)
                move = takeover(move_menu)
                if move != "x":
                    self.player_actions.append(move)
                    actions_picked += 1
                    action_menu.remove(("Defend", "def"))
            elif action == "fin":
                options =  list([(move.name, move) for move in mon.finishers])
                options += cancel_option
                info = []
                for move in mon.finishers:
                    t1 = move.type1.name
                    t2 = move.type2.name
                    strong = list(set([typ.name for typ in move.strong_vs]))
                    resist = list(set([typ.name for typ in move.resisted_by]))
                    move_info = [f"{t1} / {t2}",
                            f"Strong vs: {', '.join(strong)}",
                            f"Resisted by: {', '.join(resist)}"]
                    info.append(move_info)
                info.append("")
                move_menu = menu.FloatingMenu(0, settings.VIEW_HEIGHT,
                        options, bg = self, item_info = info)
                move = takeover(move_menu)
                if move != "x":
                    self.player_actions.append(move)
                    actions_picked += 1
                    action_menu.remove(("Finish", "fin"))
            elif action == "tag":
                options =  list([(teammate.name, teammate)
                    for teammate in adventure.current.party
                    if teammate != self.current_mon and teammate.can_battle])
                options += cancel_option
                move_menu = menu.FloatingMenu(0, settings.VIEW_HEIGHT-1,
                        options, bg = self)
                move = takeover(move_menu)
                if move != "x":
                    self.player_tag = takeover(move_menu)
                    actions_picked += 1
                    action_menu.remove(("Tag-out", "tag"))
            elif action == "for":
                options = [
                        ("Forfeit", "yes"),
                        ("Actually, maybe not", "x")
                        ]
                move_menu = menu.FloatingMenu(0, settings.VIEW_HEIGHT-1,
                        options, bg = self)
                move = takeover(move_menu)
                if move != "x":
                    self.show_message("You forfeit the battle!")
                    for mon in adventure.current.party:
                        mon.battle_record.loss = True
                        mon.battle_record.forfeit = True
                        mon.handle_battle_result()
                    return "LOSS"
        #OPP decision making (lol)
        tag_options =  list([mon
            for mon in self.opp
            if mon != self.current_opp and mon.can_battle])
        if random.random() < 0.05 and len(tag_options) > 0:
            self.opp_actions.append(random.choice(self.current_opp.attacks))
            self.opp_tag = random.choice(tag_options)
        elif self.current_mon.body.current_state <= 2 and random.random()< 0.3:
            self.opp_actions.append(random.choice(self.current_opp.finishers))
            self.opp_actions.append(random.choice(self.current_opp.attacks))
        elif self.current_mon.body.current_state <= 2:
            self.opp_actions.append(random.choice(self.current_opp.defences))
            self.opp_actions.append(random.choice(self.current_opp.finishers))
        else:
            self.opp_actions.append(random.choice(self.current_opp.attacks))
            self.opp_actions.append(random.choice(self.current_opp.defences))
        # ATK/FIN phase
        if self.current_mon.speed >= self.current_opp.speed:
            self.do_player_attack()
            self.do_opp_attack()
            self.do_fin(self.current_mon, self.player_actions,
                    self.current_opp, self.opp_actions)
            self.do_fin(self.current_opp, self.opp_actions,
                    self.current_mon, self.player_actions)
        else:
            self.do_opp_attack()
            self.do_player_attack()
            self.do_fin(self.current_opp, self.opp_actions,
                    self.current_mon, self.player_actions)
            self.do_fin(self.current_mon, self.player_actions,
                    self.current_opp, self.opp_actions)
        # TAG phase
        if self.player_tag:
            self.show_message(f"{self.current_mon.name} taggged out for "
                    f"{self.player_tag.name}")
            self.current_mon = self.player_tag
            self.player_tag = None
        if self.opp_tag:
            self.show_message(f"{self.current_opp.name} taggged out for "
                    f"{self.opp_tag.name}")
            self.current_opp = self.opp_tag
            self.opp_tag = None
        # KO swap and victory/loss phase
        if not self.current_mon.can_battle:
            self.show_message(f"{self.current_mon.name} can no longer battle.")
            options =  list([(teammate.name, teammate)
                for teammate in adventure.current.party
                if teammate != self.current_mon and teammate.can_battle])
            if len(options) == 0:
                self.show_message("The last member fell. You lost the fight.")
                return "LOSS"
            else:
                move_menu = menu.FloatingMenu(0, settings.VIEW_HEIGHT-1,
                        options, bg = self)
                self.player_tag = takeover(move_menu)
                self.show_message(f"{self.current_mon.name} taggged out for "
                        f"{self.player_tag.name}")
                self.current_mon = self.player_tag
                self.player_tag = None
        if not self.current_opp.can_battle:
            self.show_message(f"{self.current_opp.name} can no longer battle.")
            options =  list([teammate for teammate in self.opp
                if teammate != self.current_opp and teammate.can_battle])
            if len(options) == 0:
                self.show_message("The dust clears, and you are victorious.")
                for mon in adventure.current.party:
                    mon.battle_result()
                return "WIN"
            else:
                self.current_opp = random.choice(options)
                self.show_message(f"{self.current_opp.name} stood up to fight.")
    def do_fin(self, actor, actions, defender, defns):
        if not actor.can_battle:
            return
        def_move = None
        for action in defns:
            if action.move_type.action == DEF:
                def_move = action
        for action in actions:
            if action.move_type.action == ATK:
                def_move = None
        fin_move = None
        for action in actions:
            if action.move_type.action == FIN:
                fin_move = action
        if fin_move == None:
            return
        self.show_message(f"{actor.name} used {fin_move.name}")
        hit_state = None
        for state in defender.states:
            if state.mon_type in fin_move.strong_vs:
                self.show_message(f"{fin_move.name} pierces "
                    f"{defender.name}'s {state.name}!")
            elif state.current_state != 1:
                hit_state = state
                break
        if hit_state == None:
            hit_state = defender.body
        else:
            self.show_message(f"{defender.name}'s {hit_state.name}"
                    " protects it.")
            return
        damage = calc_damage(defender, fin_move, def_move, hit_state)
        if damage > hit_state.current_state:
            ko_verb = random.choice(fin_move.move_type.verbs)
            self.show_message(f"{actor.name} {ko_verb} {defender.name}")
            defender.can_battle = False
            defender.battle_record.koed = True
            actor.battle_record.num_kos += 1
        else:
            self.show_message("The attack is not enough to finish "+
                    defender.name)
    def do_player_attack(self):
        opp_def = None
        for action in self.opp_actions:
            if action.move_type.action == DEF:
                opp_def = action
        player_atk = None
        for action in self.player_actions:
            if action.move_type.action == ATK:
                player_atk = action
        if player_atk == None:
            return
        self.do_attack(self.current_mon, player_atk, self.current_opp, opp_def)
    def do_attack(self, attacker, attack, defender, defence):
        if not attacker.can_battle:
            return
        hit_state = None
        for state in defender.states:
            if state.current_state != 1:
                hit_state = state
                break
        if hit_state == None:
            hit_state = defender.body
        damage = calc_damage(defender, attack, defence, hit_state)
        self.show_message(f"{attacker.name} "
                f"{random.choice(attack.move_type.verbs)} "
                f"{defender.name} with {attack.name}.")
        if damage > 0:
            if defence:
                self.show_message(f"{defender.name}"
                        f" tried to defend with {defence.name}.")
            self.show_message("It's a "
                    f"{random.choice(damage_descriptors[damage])} hit!")
            hit_state.do_damage(damage)
            self.show_message(f"{defender.name}'s {hit_state.name} "
                    f"is {hit_state.state_descriptor()}.")
            attacker.battle_record.damage_dealt += damage
            defender.battle_record.damage_taken += damage
        elif defence == None:
            self.show_message(f"{defender.name}'s {hit_state.name}"
                    " resisted the attack")
        else:
            self.show_message(f"{defender.name}'s {defence.name} "
                    f"{random.choice(defence.move_type.verbs)} the attack.")
    def do_opp_attack(self):
        if not self.current_opp.can_battle:
            return
        player_def = None
        for action in self.player_actions:
            if action.move_type.action == DEF:
                player_def = action
        opp_atk = None
        for action in self.opp_actions:
            if action.move_type.action == ATK:
                opp_atk = action
        if opp_atk == None:
            return
        self.do_attack(self.current_opp, opp_atk, self.current_mon, player_def)
    def render(self):
        player_info = [
                self.current_mon.name,
                self.current_mon.species.description,
                f"{self.current_mon.type1.name}  {self.current_mon.type2.name}",
                "",
                self.current_mon.body.description()
        ] + list([state.description() for state in self.current_mon.states])
        menu.draw_textbox(VIEW_LEFT, player_info)
        opp_info = [
                self.current_opp.name,
                self.current_opp.species.description,
                f"{self.current_opp.type1.name} {self.current_opp.type2.name}",
                "",
                self.current_opp.body.description()
        ] + list([state.description() for state in self.current_opp.states])
        menu.draw_textbox(VIEW_RIGHT, opp_info)

class BattleTeam:
    def __init__(self):
        self.members = []

class BattleLeader(character.Character):
    def __init__(self, x, y, mon, team):
        super().__init__(x, y)
        self.mon = mon
        self.name = self.mon.name
        self.symbol = self.name[0]
        self.color = "red"
        self.team = team
        self.message = (f"{self.mon.name} :"
                f"{self.team.members[0].mon.name}! Let's rock!")
        self.team.members.append(self)
        self.battled = False
    def interact(self, scene):
        if not self.battled:
            scene.show_message("Up for a battle?")
            ques = menu.FloatingMenu(
                    0, settings.VIEW_HEIGHT -4,
                    [
                        ("Yeah", True),
                        ("Nah", False),
                        ("Yeah, nah.", False)],
                    bg = scene)
            up_for_it = takeover(ques)
            if up_for_it:
                scene.show_message(self.message)
                mons = list([teammate.mon for teammate in self.team.members])
                result = takeover(Battle(mons))
                if result != "LOSS":
                    scene.show_message("Nice fighting!")
                    self.battled = True
                else:
                    handle_loss(scene)
            else:
                scene.show_message("Maybe another time.")
        else:
            scene.show_message("You're pretty strong!")

def handle_loss(scene):
    revive_scene, revive_position = adventure.current.revive_point
    x,y = revive_position
    for mon in adventure.current.party:
        mon.revive()
        if mon.body.current_state == 1:
            mon.body.current_state = 2
    revive_scene.hero = scene.hero
    revive_scene.hero.x = x
    revive_scene.hero.y = y
    adventure.current.scene = revive_scene
    revive_scene.show_message("You don't remember much after the loss, "
            "but somehow you find yourself back in a familiar place.")

class BattleMember(character.Character):
    def __init__(self, x, y, mon, team):
        super().__init__(x, y)
        self.mon = mon
        self.name = self.mon.name
        self.symbol = self.name[0]
        self.color = "green"
        self.team = team
        openings = ["I'm proud of my ",
                "Can you get past my ",
                "My ultimate technique is ",
                "I've mastered ",
                "My teammates respect me for my "]
        move = random.choice(self.mon.defences+self.mon.attacks+
                self.mon.finishers)
        self.message = (f"{self.mon.name} : {random.choice(openings)}"
            f"{move.name}!")
        self.team.members.append(self)
    def interact(self, scene):
        scene.show_message(self.message)

class Wanderer(character.Character):
    def __init__(self, x, y, mon):
        super().__init__(x, y)
        self.mon = mon
        self.name = self.mon.name
        self.symbol = self.name[0]
        self.color = "orange"
        self.message = f"{self.mon.name}: Let's see what you've got!"
        self.battled = False
    def interact(self, scene):
        if not self.battled:
            scene.show_message("Up for a battle?")
            ques = menu.FloatingMenu(
                    0, settings.VIEW_HEIGHT -4,
                    [
                        ("Yeah", True),
                        ("Nah", False),
                        ("Yeah, nah.", False)],
                    bg = scene)
            up_for_it = takeover(ques)
            if up_for_it:
                scene.show_message(self.message)
                result = takeover(Battle([self.mon]))
                if result != "LOSS":
                    scene.show_message("Nice fighting!")
                    self.battled = True
                    if len(adventure.current.party) < adventure.party_limit:
                        scene.show_message("How about joining forces?")
                        ques = menu.FloatingMenu(
                                0, settings.VIEW_HEIGHT -4,
                                [
                                    ("Definitely.", True),
                                    ("Hell yeah.", True),
                                    ("I dunno...", False),
                                    ("I'll think about it.", False)],
                                bg = scene)
                        up_for_it = takeover(ques)
                        if up_for_it:
                            scene.show_message("Right on!")
                            self.mon.body.heal(2)
                            scene.foreground.remove(self)
                            m = cast.PartyMember(self.x, self.y, self.mon)
                            scene.foreground.append(m)
                            adventure.current.map_party.append(m)
                            adventure.current.party.append(self.mon)
                else:
                    handle_loss(scene)
            else:
                scene.show_message("Maybe another time.")
        else:
            pass

if __name__ == "__main__":
    import mon_species
    import mons
    from core import term
    opponents = []
    for i in range(6):
        opponents.append(mons.Mon(mon_species.Species()))
    battle = Battle(opponents)
    with term.fullscreen(), term.cbreak(), term.hidden_cursor(), term.keypad():
        takeover(battle)


from __future__ import print_function
import numpy as np
from astropy.io import fits
import matplotlib.pyplot as plt
from math import sqrt, pi

w1sgk = fits.open('w1rot.fits')[0].data
w3sgk = fits.open('w3rot.fits')[0].data
w5sgk = fits.open('w5rot.fits')[0].data
rsgk = fits.open('rad.fits')[0].data

w1antia = np.loadtxt('rotgongs414.1d23')[:,1]
w3antia = np.loadtxt('rotgongs414.3d23n')[:,1]
w5antia = np.loadtxt('rotgongs414.5d23n')[:,1]
rantia = np.loadtxt('rotgongs414.5d23n')[:,0]

maxratio = abs(w3antia).max()/(abs(w3sgk)/rsgk).max()
print("mag ratio, s3 = %10.5e" %(maxratio))
plt.plot(rsgk, abs(w3sgk)/rsgk, 'r')
plt.plot(rsgk, abs(w3sgk)/rsgk*maxratio, 'g')
plt.plot(rantia, abs(w3antia), 'b')
plt.show()
plt.close()

maxratio = abs(w5antia).max()/(abs(w5sgk)/rsgk).max()
print("mag ratio, s5 %10.5e" %(maxratio))
plt.plot(rsgk, abs(w5sgk)/rsgk, 'r')
plt.plot(rsgk, abs(w5sgk)/rsgk*maxratio, 'g')
plt.plot(rantia, abs(w5antia), 'b')
plt.show()
plt.close()

maxratio = abs(w1antia).max()/(abs(w1sgk)/rsgk).max()
print("mag ratio, s1 = %10.5e" %(maxratio))
plt.plot(rsgk, abs(w1sgk)/rsgk, 'r')
plt.plot(rsgk, abs(w1sgk)/rsgk*maxratio, 'g')
plt.plot(rantia, abs(w1antia), 'b')
plt.show()


import sys
sys.path.append('../')
from ex05.mylinearregression import MyLinearRegression as MyLR
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def add_polynomial_features(x, power):
    try:
        if type(x) != np.ndarray or x.shape[1] != 1 or not isinstance(power, (int, float)):
            return None
        return np.vander(x.reshape(-1), power + 1 , increasing=True)[:,1:]
    except:
        return None

def perform_regression(X, Y, theta):
    myLR =  MyLR(theta, alpha = 1.5e-10, max_iter = 1500000)
    y_pred = myLR.predict_(X)
    myLR.fit_(X, Y)
    y_pred = myLR.predict_(X)
    print(myLR.mse_(y_pred,Y))
    return y_pred

def output_graph(x, y, y_preds):
    plt.scatter(x, y, label="Pills Score")
    for i, y_pred in zip(range(len(y_preds)), y_preds):
        plt.plot(x, y_pred, label=f"degree{i + 1} polynomial expression")
    plt.xlabel("Micrograms")
    plt.ylabel("Scores")
    plt.grid()
    plt.legend()
    plt.show()

def polynomial_regression(data):
    XMicrograms = np.array(data[["Micrograms"]])[:,0].reshape(-1,1)
    Y = np.array(data[["Score"]])[:,0].reshape(-1,1)
    y_preds = []
    for i in range(1, 7):
        poly_X = add_polynomial_features(XMicrograms, i)
        theta = np.ones((poly_X.shape[1] + 1, 1)).reshape(-1, 1)
        if i == 1:
            theta = np.array([[88.67822162],[-8.92106849]]).reshape(-1,1)
        elif i == 2:
            theta = np.array([[65.92627185],[ 3.63987692],[-1.47092799]]).reshape(-1, 1)
        elif i == 3:
          theta = np.array([[ 52.01405471], [ 33.19711719], [-12.9595629 ], [  1.15407571]]).reshape(-1, 1)
        if i == 4:
            theta = np.array([[-20],[ 160],[ -80],[ 10],[ -1]]).reshape(-1,1)
        elif i == 5:
            theta = np.array([[1140],[ -1850],[ 1110],[ -305],[ 40],[ -2]]).reshape(-1,1)
        elif i == 6:
            theta = np.array([[9110],[ -18015],[ 13400],[ -4935],[ 966],[ -96.4],[ 3.86]]).reshape(-1,1)
        y_preds.append(perform_regression(poly_X, Y, theta))
    output_graph(XMicrograms, Y, y_preds)

def main():
    data = pd.read_csv("are_blue_pills_magics.csv")
    polynomial_regression(data)
    return

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
# Felipe G. Foschiera
# URI 2018 - Olimpíadas de Natal
from collections import defaultdict
medalhas = defaultdict(list)
while True:
    try:
        categoria, ouro, prata, bronze = input(), input(), input(), input()
        if ouro not in medalhas:
            medalhas[ouro] = [1, 0, 0]
        else:
            medalhas[ouro][0] += 1
        if prata not in medalhas:
            medalhas[prata] = [0, 1, 0]
        else:
            medalhas[prata][1] += 1
        if bronze not in medalhas:
            medalhas[bronze] = [0, 0, 1]
        else:
            medalhas[bronze][2] += 1
    except EOFError:
        break
print("Quadro de Medalhas")
for m in sorted(medalhas.items(), key = lambda x: (-x[1][0], -x[1][1], -x[1][2], x[0])):
    print(m[0], m[1][0], m[1][1], m[1][2])

URI Online Judge/Estruturas e Bibliotecas/2018 - Olimpíadas de Natal/natal.py


import matplotlib.pyplot as plt
import numpy as np
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

def f(item):
    try:
        return f'{item:,.2f}'
    except Exception as e:
        return str(item)
# li = list(zip(range(1, 14), range(14, 27)))

def chart(*pairs, **kwargs):
    for num, pair in enumerate(pairs):
        axes = []
        axes.append([x[0] for x in pair])
        axes.append([x[1] for x in pair])
        # plt.plot(*[np.array(i) for i in axes], ls=":", label=f"{f(pair.initial)} @ {f(pair.perc)}% {'Compounded' if pair.compounding else ''}")
        plt.plot(*[np.array(i) for i in axes], ls="-",  label=f"{f(pair.initial)} @ {f(pair.perc)}% {'Compd' if pair.compounding else ''}")

    plt.legend()
    # plt.figure(figsize=(8, 6), dpi=300)
    plt.xticks(range(0, 21))
    # plt.ticklabel_format(useOffset=False)
    plt.ylim(ymin=0)
    plt.xlim(xmin=0)
    plt.ticklabel_format(style='plain', useLocale=True)
    plt.grid(color='green', linestyle='--', linewidth = 0.5)
    return plt

    """
    Introducing interactivity
    fig = plt.figure()

    with plt.ion():
        # interactive mode will be on
        # figures will automatically be shown
        fig2 = plt.figure()
        fig2.show()
    """

def deposit_chart(*args, **kwargs):
    plot = chart(*args, **kwargs)
    if not kwargs['times']:
        plot.yticks(range(0, 32000000*kwargs['multiplier'], 2500000*kwargs['multiplier']))
    plot.xlabel("Time in years")
    plot.ylabel("Deposits")
    plot.title('Accumulated Deposit')
    plot.savefig('deposits.png', dpi=300)
    plt.tight_layout()
    plot.show()

def monthly_return_chart(*args, **kwargs):
    plot = chart(*args, **kwargs)
    if not kwargs['times']:
        plot.yticks(range(0, 450000*kwargs['multiplier'], 25000*kwargs['multiplier']))
    plot.xlabel("Time in years")
    plot.ylabel("Monthly Return")
    plot.title('Monthly Return Chart')
    plot.savefig('monthly_return.png', dpi=300)
    plt.tight_layout()
    plot.show()


fileOutput = open('list_US_Tickers', 'w')

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen

def getUSTickers():
    numStocks = 8700
    lastIndex = 1

    for i in range(1, numStocks, 20):

        #urlToScrape = 'https://finviz.com/screener.ashx?v=111&r=' + str(i)
        urlToScrape = 'https://finviz.com/screener.ashx?v=121&f=sec_industrials&o=pb&r=' + str(i)

        req = Request(urlToScrape, headers={'User-Agent': 'Mozilla/5.0'})
        webpage = urlopen(req).read()
        soup = BeautifulSoup(webpage, "html.parser")
        tBody = soup.find('div', {"id": "screener-content"})

        for tr in tBody.find_all('tr', {'valign': 'top'}):
            index = tr.find_all('a')[0].get_text(strip=True)
            ticker = tr.find_all('a')[1].get_text(strip=True)
            name = tr.find_all('a')[2].get_text(strip=True)
            sector = tr.find_all('a')[3].get_text(strip=True)
            industry = tr.find_all('a')[4].get_text(strip=True)
            country = tr.find_all('a')[5].get_text(strip=True)
            cap = tr.find_all('a')[6].get_text(strip=True)
            pe = tr.find_all('a')[7].get_text(strip=True)
            price = tr.find_all('a')[8].get_text(strip=True)

            if index == lastIndex:
                break
            print(index, ticker, name.replace(" ", "_"), sector.replace(" ", "_")
                  , industry.replace(" ", "_"), country.replace(" ", "_"), cap, pe, price)

            fileOutput.write(ticker + " " + name.replace(" ", "_") + " "
                             + sector.replace(" ", "_") + " "
                             + industry.replace(" ", "_") + " "
                             + country.replace(" ", "_") + " "
                             + cap + " " + price + '\n')
            fileOutput.flush()
            lastIndex = index
        else:
            continue
        break

getUSTickers()


import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import  MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os

def sendMail(columnames,prop):
    fromaddr = "contacto@bullestate.cl"
    toaddr = "joaquin@bullestate.cl, sergei@bullestate.cl"

    msg = MIMEMultipart()

    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = "Oportunidad BullEstate"

    body = "Estimados" + ":\n\nOportunidad Encontrada."
    for i,x in enumerate(columnames):
        body+=(str(columnames[i]))
        body+=": "
        try:
            body+=(str(prop[i]))
        except:
            body+=""
        body+='\n'

    msg.attach(MIMEText(body, 'plain'))

    server = smtplib.SMTP_SSL('smtp.zoho.com', 465)
    #server.starttls()
    server.login(fromaddr, "Bullestate.123")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()


import json

typeAliases = {str.__name__: "std::string", int.__name__: "std::int",
               "list: string": "std::vector<std::string>", "list: int": "std::vector<int>"}

def getCppType(parsedValue: str):
    variableType = "auto"
    if (parsedValue[0] == "[" and parsedValue[len(parsedValue) - 1] == "]"):
        typeOfArray = type(json.loads(parsedValue)[0])
        if (typeOfArray == str):
            typeOfArray = "list: string"
        if (typeOfArray == int):
            typeOfArray = "list: int"
        parsedValue = parsedValue.replace("[", "{", 1)
        parsedValue = (parsedValue[::1].replace("]", "}"))[::1]
        variableType = typeAliases[typeOfArray]
    return variableType


import numpy as np
from scipy.ndimage import label
from sklearn.mixture import GaussianMixture
import math
import nd2
import cv2
from tqdm import tqdm

def create_circular_mask(h, w, center, radius):
    '''
    Helper function to mask out cirular region of 2D array
    '''
    Y, X = np.ogrid[:h, :w]
    dist_from_center = np.sqrt((X - center[0])**2 + (Y-center[1])**2)

    mask = dist_from_center <= radius
    return mask

def return_overlapping_set(vertex, adj_matrix):
    '''
    Helper function to return set of all circles overlapping with a given circle by searching the
    overlapping graphs adjacency matrix in a BFS manner
    '''
    # Put initial adjacent vertecies in set and populate queue
    queue = list(np.nonzero(adj_matrix[vertex]))[0]
    ol_set = [vertex]

    # while queue is not empty
    while len(queue) > 0:
        # pop vertex
        vert = queue[0]
        queue = queue[1:]

        # Ad vertex to visited set
        ol_set.append(vert)

        # Get adjacent to new vertes
        vert_adj = list(np.nonzero(adj_matrix[vert]))[0]
    return ol_set

def circle_rect(rectangle):
    '''
    Enscribe a circle around a rectangle
    '''
    a = abs(rectangle[0][0] - rectangle[0][1])
    b = abs(rectangle[1][0] - rectangle[1][1])
    r = 0.5 * math.sqrt(a**2 + b **2)
    c = (rectangle[0][1] - a//2, rectangle[1][1] - b//2)
    return [c, r]

def circle_weight(circle, points):
    '''
    Compute the prportion of a circle (in an integer grid) that a point cloud covers.
    Used as a metric of circlularity
    '''
    # Get how many points should be in the circle

    # we could easily approximate this
    denom = round(circle[1]**2 * np.pi) // 1
    # Return fraction represented in points
    return len(points) / denom

def clean_satellites(image, verbose=True, brightness_threshold_percentile=99, elimination_threshold=50, overlap_padding=20, circle_mask_padding=10):
    '''
    Apply satellite dropplet cleaning technique to image (grayscale array).
    Techinque should be applied to the large images and will not work properly with individual droplets

    verbose                         <--- Report progress
    brightness_threshold_percentile <--- 100 - percetnage of brightest points to consider
    elimination_threshold           <--- minimum size of contiguous bright region to be considered when checking for sattelites,
                                         smaller regions will be automatically discardeds
    overlap_padding                 <--- padd circles when checking for overlap, in theory this should be large enough to account
                                         for dark bands cutting through sattelite dropplets
    circle_mask_padding             <--- enlarge border of circle used to mask out a sattelite dropplet. Should be thickness of dark rim of sattelite
    '''
    #TODO: Sanity checks

    # Brightness threshold percentile of 99 percent means top 1% of brightest points will be considered
    if verbose:
        print('1/6 | Determining brightness threshold and masking brightest points')
    sample = np.random.choice(a=image.flatten(), size=1000000)
    brightness_threshold = np.percentile(sample, brightness_threshold_percentile)

    # Create mask of brightest points in image
    mask = np.zeros(image.shape)
    mask[image > brightness_threshold] = 1

    # Get contiguous objects (regions of 1's) in mask
    if verbose:
        print('2/6 | Computing objects')
    labeled_features, num_features = label(mask)

    # Enscribe features with boxes
    if verbose:
        print('3/6 | Determining object bounding boxes and circles')
    feature_squares = []
    feature_points = []

    # X and Y naming is a bit wierd here
    for feat in tqdm(range(1, num_features + 1)):
        feat_coords = np.argwhere(labeled_features == feat)
        #TODO: Do something to remove features that are only a couple of pixels
        if len(feat_coords) > elimination_threshold:
            feature_points.append(feat_coords)
            x_coords = feat_coords[:,0]
            y_coords = feat_coords[:,1]
            x_axis = [min(x_coords), max(x_coords)]
            y_axis = [min(y_coords), max(y_coords)]
            feature_squares.append([y_axis, x_axis])

    # Enscribe circles around identified rectangular regions
    feature_circles = []

    for rect in feature_squares:
        feature_circles.append(circle_rect(rect))

    # Determine threshold of circle-similarity to seperate sattelites from dropplet edge segments
    if verbose:
        print('4/6 | Computing circle similarity threshold and circle similarity')
    scores = []
    for i in tqdm(range(len(feature_circles))):
        scores.append(circle_weight(feature_circles[i], feature_points[i]))

    gm = GaussianMixture(n_components=2, random_state=0)

    gm.fit(np.array(scores).reshape(-1, 1))

    x_den = np.linspace(0, 1, num=100)
    y_lab = gm.predict(x_den.reshape(-1, 1))

    decision_boundary = np.where(y_lab[:-1] != y_lab[1:])[0][0] + 2

    circle_weight_threshold = decision_boundary / 100

    mask_cw = []
    fil_circles = []
    fil_points = []

    for i in range(len(feature_circles)):
        score = scores[i]
        if score >= circle_weight_threshold:
            fil_circles.append(feature_circles[i])
            fil_points.append(feature_points[i])

    # Merge overlapping segments into larger based on padded circle collision
    # Determine threshold of circle-similarity to seperate sattelites from dropplet edge segments
    if verbose:
        print('5/6 | Merging overlapping sattelites')
    intersect_matrix = np.zeros((len(fil_circles), len(fil_circles)))

    for x in tqdm(range(len(fil_circles))):
        #only need to compute half and no diagonals
        for y in range(len(fil_circles)):
            if x == y:
                continue
            # Two ifcles intersect if distance between center points is less than sum of radii
            dR1R2 = np.linalg.norm(np.subtract(fil_circles[x][0], fil_circles[y][0]))
            intersect_matrix[x][y] = (dR1R2 <= fil_circles[x][1] + fil_circles[y][1] + overlap_padding)
            intersect_matrix[y][x] = (dR1R2 <= fil_circles[x][1] + fil_circles[y][1]+ overlap_padding)

    # Get sets of overlapping circles
    ol_circles = []
    ol_points = []
    ol_set = []
    considered_verticies = []

    # Copy intersect matrx
    ol_intersect = intersect_matrix

    for circ_index in tqdm(range(len(fil_circles))):
        # If circle not overlapping with anyone
        if sum(ol_intersect[circ_index]) == 0:
            # Just put it in the output array
            ol_circles.append(fil_circles[circ_index])
            ol_points.append(fil_points[circ_index])
        else:
            # Theres some overlapping afoot!

            # If vertex already considered, ignore
            if circ_index in considered_verticies:
                continue
            # Get indecies of all overlapping circles
            new_ol_set = return_overlapping_set(circ_index, ol_intersect)

            ol_set.append(new_ol_set)
            considered_verticies = considered_verticies + new_ol_set

            # Merge point clouds in new_ol_set
            new_points = fil_points[new_ol_set[0]]
            for i in new_ol_set[1:]:
                new_points = list(new_points) + list(fil_points[i])
            ol_points.append(new_points)

            # Compute new boxes
            x_coords = np.array(new_points)[:,0]
            y_coords = np.array(new_points)[:,1]
            x_axis = [min(x_coords), max(x_coords)]
            y_axis = [min(y_coords), max(y_coords)]
            new_box = [y_axis, x_axis]

            ol_circles.append(circle_rect(new_box))

    gs_image = image

    ip_mask = np.zeros(gs_image.shape)

    # For each circle, paint it in the mask
    if verbose:
        print('6/6 | Masking sattelites')

    for circle in tqdm(ol_circles):
        cv2.circle(ip_mask, circle[0], math.ceil(circle[1]) + circle_mask_padding ,255,-1)

    ip_mask =  np.array(ip_mask, dtype = np.uint8)

    #mask_content = cv2.bitwise_and(gs_image, gs_image, mask = ip_mask)

    masked_image = cv2.bitwise_and(gs_image, gs_image, mask = cv2.bitwise_not(ip_mask))

    return masked_image

if __name__ == "__main__":
    # Run test
    # Import test

    #path = "notebooks/data/20220722PM_ATTO2_T1_Split1.nd2"
    path = "notebooks/data/20220722PM_Calcein2_T1_Split1.nd2"
    #path = "notebooks/data/20220722PM_SulfoB1_T1_Split2.nd2"
    #path = "notebooks/data/20220722PM_CF405S_2_t1.nd2"
    #path = "notebooks/data/20220722PM_Calcein1_T1_Split2.nd2"

    test_image = nd2.imread(path)[-1]

    masked_image = clean_satellites(test_image)

    from matplotlib import pyplot as plt
    plt.imshow(test_image)
    plt.show()

    plt.imshow(masked_image)
    plt.show()