#!/usr/bin/env python
import sys, os
import itertools
import numpy as np
import matplotlib.pyplot as plt

from keras.utils import to_categorical
from keras.models import load_model
from sklearn.metrics import confusion_matrix

# Parameter
height = width = 48
num_classes = 7
model_name = sys.argv[2]
class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
option = sys.argv[3]  # draw: draw confusion mastrix
                      # pick: pick a image fitting the type II error

# Read the train data
print('Read start')
try:
  X = np.load('X.npy')
  Y = np.load('Y.npy')
except:
  with open(sys.argv[1], "r+") as f:
    line = f.read().strip().replace(',', ' ').split('\n')[1:]
    raw_data = ' '.join(line)
    length = width*height+1 #1 is for label
    data = np.array(raw_data.split()).astype('float').reshape(-1, length)
    X = data[:, 1:]
    Y = data[:, 0]
    # Change data into CNN format
    X = X.reshape(X.shape[0], height, width, 1)
    Y = Y.reshape(Y.shape[0], 1)
    print('Saving X.npy & Y.npy')
    np.save('X.npy', X) # (-1, height, width, 1)
    np.save('Y.npy', Y) # (-1, 1)

X /= 255
print('Read finished!')

# Split the data
valid_num = 3000
X_train, Y_train = X[:-valid_num], Y[:-valid_num].astype('int')
X_valid, Y_valid = X[-valid_num:], Y[-valid_num:].astype('int')
# print(X_train.shape)
# print(X_valid.shape)
# print(Y_train.shape)
# print(Y_valid.shape)

# Load model
model = load_model(model_name)
print('Predicting')
pred = model.predict(X_valid)
# print(pred.shape)
pred_label = np.argmax(pred, axis=1)
# print(pred_label.shape)
Y_valid = Y_valid.reshape(-1, )
# print(Y_valid.shape)
print('Predicting done!')

def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, '{:.3f}'.format(cm[i, j]),
                 horizontalalignment="center",
                 color="brown" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

# Compute confusion matrix
if option == 'draw':
  cnf_matrix = confusion_matrix(Y_valid, pred_label)
  np.set_printoptions(precision=3)

  # Plot non-normalized confusion matrix
  # plt.figure()
  # plot_confusion_matrix(cnf_matrix, classes=class_names,
  #                       title='Confusion matrix, without normalization')

  # Plot normalized confusion matrix
  plt.figure()
  plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
                        title='Normalized confusion matrix')

  plt.show()

# Pick image
elif option == 'pick':
  base_dir = './'
  img_dir = os.path.join(base_dir, 'cm_image')
  if not os.path.exists(img_dir):
    os.makedirs(img_dir)

  true_label = np.argwhere(Y_valid == 3).squeeze()
  # print(true_label)
  picked_label = np.argwhere(pred_label[true_label] == 3).squeeze()
  # print(picked_label)

  idx = true_label[picked_label[3]]
  print('Picking image number {}'.format(idx))
  see = X_valid[idx].reshape(height, width)
  # print(see.shape)
  ans = ['{:.3f}'.format(i) for i in list(pred[idx])]
  print('True label: {:d}; Predicted label: {}'.format(Y_valid[idx], pred_label[idx]))
  print('Its percentage: {}'.format(' , '.join(ans)))

  plt.figure()
  plt.imshow(see,cmap='gray')
  plt.colorbar()
  plt.tight_layout()
  fig = plt.gcf()
  plt.draw()
  fig.savefig(os.path.join(img_dir, '{}.png'.format(idx)), dpi=100)


import os
import sys
from flask import Flask, request, abort, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from models import Podcast, Speaker, Episode, setup_db, db
from tests.sample import reset_db_tables
from auth.auth import AuthError, requires_auth

def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__)
    setup_db(app)
    app.config['JSON_SORT_KEYS'] = False
    # if no data in table fill with sample data
    query = Podcast.query.all
    if not query:
        reset_db_tables(app)

    '''
    Set up CORS
    '''
    cors = CORS(app, resources={r"/*": {'origins': '*'}})

    '''
    set Access-Control-Allow
    '''
    @app.after_request
    def after_request(response):
        response.headers.add(
            'Access-Control-Allow-Headers',
            'Content-Type, Authorization'
        )
        response.headers.add(
            'Access-Control-Allow-Methods',
            'GET, POST, PATCH, DELETE, OPTIONS'
        )
        return response

    @app.route('/', methods={'GET'})
    def home():
        return jsonify({
            'success': True,
            'message': 'Healthy'
        })

    '''
    GET Endpoints
    '''

    def get_all(Model, value):
        query = Model.query.all()
        items = [items.format() for items in query]

        return jsonify({
            'success': True,
            '{}'.format(value): items
        })

    def get_id(Model, id, value):
        query = Model.query.filter(Model.id == id).one_or_none()

        if query:
            item = query.format()
            return jsonify({
                'success': True,
                '{}'.format(value): item
            })
        else:
            abort(404)

    @app.route('/podcasts', methods={'GET'})
    def get_podcasts():
        return get_all(Podcast, 'podcasts')

    @app.route('/podcasts/<int:podcast_id>', methods={'GET'})
    def get_podcast_id(podcast_id):
        return get_id(Podcast, podcast_id, 'podcast')

    @app.route('/speakers', methods={'GET'})
    def get_speakers():
        return get_all(Speaker, 'speakers')

    @app.route('/speakers/<int:speaker_id>', methods={'GET'})
    def get_speaker_id(speaker_id):
        return get_id(Speaker, speaker_id, 'speaker')

    @app.route('/episodes', methods={'GET'})
    def get_episodes():
        return get_all(Episode, 'episodes')

    @app.route('/episodes/<int:episode_id>', methods={'GET'})
    def get_episode_id(episode_id):
        return get_id(Episode, episode_id, 'episode')

    '''
    POST Endpoints
    '''

    @app.route('/podcasts', methods=['POST'])
    @requires_auth('post:data')
    def create_search_podcast(payload):
        body = request.get_json()
        new_author = body.get('author', None)
        new_name = body.get('name', None)
        new_image_link = body.get('image', None)
        new_podcast_link = body.get('podcast_link', None)
        search = body.get('search', None)

        try:
            if search:
                # Search in Podcasts for author and name
                query_author = (
                    Podcast.query
                    .filter(Podcast.author.ilike("%{}%".format(search)))
                )
                author_results = [podcast.format() for podcast in query_author]

                query_name = (
                    Podcast.query
                    .filter(Podcast.name.ilike("%{}%".format(search)))
                )
                name_results = [podcast.format() for podcast in query_name]

                search_count = len(name_results)+len(author_results)

                result = {
                    'success': True,
                    'author_search': author_results,
                    'name_search': name_results,
                    'results': search_count
                }
            else:
                # POST a new podcast
                podcast = Podcast(
                    author=new_author, name=new_name,
                    image=new_image_link, podcast_link=new_podcast_link
                )
                podcast.insert()
                result = {
                    'success': True,
                    'podcast': podcast.format()
                }

        except:
            db.session.rollback()
            print(sys.exc_info())
            abort(422)

        finally:
            db.session.close()

        return jsonify(result)

    @app.route('/speakers', methods=['POST'])
    @requires_auth('post:data')
    def create_search_speaker(payload):
        body = request.get_json()
        new_name = body.get('name', None)
        new_image_link = body.get('image_link', None)
        new_twitter_link = body.get('twitter_link', None)
        new_website_link = body.get('website_link', None)
        search = body.get('search', None)

        try:
            if search:
                # Search in Speakers for names

                query_name = (
                    Speaker.query
                    .filter(Speaker.name.ilike("%{}%".format(search)))
                )
                name_results = [speaker.format() for speaker in query_name]

                search_count = len(name_results)

                result = {
                    'success': True,
                    'name_search': name_results,
                    'results': search_count
                }
            else:
                # POST a new speaker
                speaker = Speaker(
                    name=new_name,
                    image_link=new_image_link,
                    twitter_link=new_twitter_link,
                    website_link=new_website_link
                )
                speaker.insert()
                result = {
                    'success': True,
                    'speaker': speaker.format()
                }

        except:
            db.session.rollback()
            print(sys.exc_info())
            abort(422)

        finally:
            db.session.close()

        return jsonify(result)

    @app.route('/episodes', methods=['POST'])
    @requires_auth('post:data')
    def create_search_episodes(payload):
        body = request.get_json()
        new_title = body.get('title', None)
        new_topics = body.get('topics', None)
        new_podcast_link = body.get('podcast_link', None)
        new_speaker_id = body.get('speaker_id', None)
        new_podcast_id = body.get('podcast_id', None)
        search = body.get('search', None)

        try:
            if search:
                # Search in Episodes for titles and topics

                query_title = (
                    Episode.query
                    .filter(Episode.title.ilike("%{}%".format(search)))
                )
                title_results = [episode.format() for episode in query_title]

                query_topics = (
                    Episode.query
                    .filter(Episode.topics.ilike("%{}%".format(search)))
                )
                topics_results = [episode.format() for episode in query_topics]

                search_count = len(title_results)+len(topics_results)

                result = {
                    'success': True,
                    'title_search': title_results,
                    'topics_search': topics_results,
                    'results': search_count
                }
            else:
                # POST a new episode
                episode = Episode(
                    title=new_title,
                    topics=new_topics,
                    podcast_link=new_podcast_link,
                    speaker_id=new_speaker_id,
                    podcast_id=new_podcast_id
                )
                episode.insert()
                result = {
                    'success': True,
                    'episode': episode.format()
                }

        except:
            db.session.rollback()
            print(sys.exc_info())
            abort(422)

        finally:
            db.session.close()

        return jsonify(result)

    '''
    DELETE Endpoints
    '''
    def delete_by_id(Model, id):
        try:
            query = Model.query.get(id)
            query.delete()
            result = {
              'success': True,
              'deleted_id': id
            }
        except:
            db.session.rollback()
            print(sys.exc_info())
            if query is None:
                abort(404)
            else:
                abort(422)

        finally:
            db.session.close()

        return jsonify(result)

    @app.route('/podcasts/<int:podcast_id>', methods=['DELETE'])
    @requires_auth('delete:data')
    def delete_podcast(payload, podcast_id):

        return delete_by_id(Podcast, podcast_id)

    @app.route('/speakers/<int:speaker_id>', methods=['DELETE'])
    @requires_auth('delete:data')
    def delete_speaker(payload, speaker_id):

        return delete_by_id(Speaker, speaker_id)

    @app.route('/episodes/<int:episode_id>', methods=['DELETE'])
    @requires_auth('delete:data')
    def delete_episode(payload, episode_id):

        return delete_by_id(Episode, episode_id)

    '''
    PATCH Endpoints
    '''
    @app.route('/podcasts/<int:podcast_id>', methods=['PATCH'])
    @requires_auth('patch:data')
    def update_podcast(payload, podcast_id):
        body = request.get_json()
        new_author = body.get('author', None)
        new_name = body.get('name', None)
        new_image = body.get('image', None)
        new_podcast_link = body.get('podcast_link', None)

        try:
            podcast = Podcast.query.get(podcast_id)
            podcast.author = new_author
            podcast.name = new_name
            podcast.image = new_image
            podcast.podcast_link = new_podcast_link

            result = {
              'success': True,
              'podcast': podcast.format()
            }
        except:
            db.session.rollback()
            print(sys.exc_info())
            if podcast is None:
                abort(404)
            else:
                abort(422)

        finally:
            db.session.close()

        return jsonify(result)

    @app.route('/speakers/<int:speaker_id>', methods=['PATCH'])
    @requires_auth('patch:data')
    def update_speaker(payload, speaker_id):
        body = request.get_json()
        new_name = body.get('name', None)
        new_image = body.get('image_link', None)
        new_twitter = body.get('twitter_link', None)
        new_website = body.get('website_link', None)

        try:
            speaker = Speaker.query.get(speaker_id)
            speaker.name = new_name
            speaker.image_link = new_image
            speaker.twitter_link = new_twitter
            speaker.website_link = new_website

            result = {
              'success': True,
              'speaker': speaker.format()
            }
        except:
            db.session.rollback()
            print(sys.exc_info())
            if speaker is None:
                abort(404)
            else:
                abort(422)

        finally:
            db.session.close()

        return jsonify(result)

    @app.route('/episodes/<int:episode_id>', methods=['PATCH'])
    @requires_auth('patch:data')
    def update_episode(payload, episode_id):
        body = request.get_json()
        new_title = body.get('title', None)
        new_topics = body.get('topics', None)
        new_podcast_link = body.get('podcast_link', None)
        new_speaker_id = body.get('speaker_id', None)
        new_podcast_id = body.get('podcast_id', None)

        try:
            episode = Episode.query.get(episode_id)
            episode.title = new_title
            episode.topics = new_topics
            episode.podcast_link = new_podcast_link
            episode.speaker_id = new_speaker_id
            episode.podcast_id = new_podcast_id
            result = {
              'success': True,
              'episode': episode.format()
            }
        except:
            db.session.rollback()
            print(sys.exc_info())
            if episode is None:
                abort(404)
            else:
                abort(422)

        finally:
            db.session.close()

        return jsonify(result)

    '''
    Error handlers for expected errors
    '''
    @app.errorhandler(400)
    def bad_request(error):
        return jsonify({
            "success": False,
            "error": 400,
            "message": "bad request"
        }), 400

    @app.errorhandler(401)
    def unauthorized(error):
        return jsonify({
            "success": False,
            "error": 401,
            "message": "unauthorized"
        }), 401

    @app.errorhandler(403)
    def forbidden(error):
        return jsonify({
            "success": False,
            "error": 403,
            "message": "forbidden"
        }), 403

    @app.errorhandler(404)
    def not_found(error):
        return jsonify({
            "success": False,
            "error": 404,
            "message": "resource not found"
        }), 404

    @app.errorhandler(422)
    def unprocessable(error):
        return jsonify({
            "success": False,
            "error": 422,
            "message": "unprocessable"
        }), 422

    @app.errorhandler(500)
    def server_error(error):
        return jsonify({
            "success": False,
            "error": 500,
            "message": "internal server error"
        }), 500

    @app.errorhandler(AuthError)
    def server_error(error):
        return jsonify({
            "success": False,
            "error": error.status_code,
            "message": error.description
        }), error.status_code

    return app

app = create_app()

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=True)
    # app.run()


'''Unit tests'''

import os
import io
from collections import deque

import requests

from cert_session import Session, _create_certifi_temp

def get_lines_from_file(path):
    with open(path) as fh:
        return fh.readlines()

def get_line_count_path(path):
    with open(path) as fh:
        return get_line_count_fh(fh)

def get_line_count_fh(fh):
    lines = 0

    for _ in fh:
        lines += 1

    return lines

def test_get_line_count_fh():
    fh = io.StringIO('Line1\nLine2\nLine3\n')
    assert get_line_count_fh(fh) == 3

def test_get_line_count_fh_unterminated():
    fh = io.StringIO('Line1\nLine2\nLine3')
    assert get_line_count_fh(fh) == 3

def test_create_certifi_temp(cert_path):
    tmp_path = _create_certifi_temp(cert_path)
    assert tmp_path

    with open(tmp_path) as ftmp:
        tail_lines = list(deque(ftmp, maxlen=get_line_count_path(cert_path)))

    assert tail_lines == get_lines_from_file(cert_path)
    os.unlink(tmp_path)

def test_cert_session_exit(cert_path):
    sess = Session(cert_path)
    assert sess
    assert isinstance(sess, type(requests.session()))
    assert sess == sess.__enter__()

    path = sess.verify
    assert os.path.exists(path)

    sess.__exit__(None, None, None)
    assert not os.path.exists(path)

def test_cert_session_exit_with_exception(cert_path):
    sess = Session(cert_path)
    assert sess
    assert isinstance(sess, type(requests.session()))
    assert sess == sess.__enter__()

    path = sess.verify
    assert os.path.exists(path)

    sess.__exit__(Exception, None, None)
    assert os.path.exists(path)

    os.unlink(path)


import glob
import matplotlib.pyplot as plt
import numpy as np
from tools.paper.plotting.plot_utils import plot_save
from matplotlib import rc, rcParams

# activate latex text rendering
# rc('text', usetex=True)
# rc('axes', linewidth=2)
# rc('font', weight='bold')
# rcParams['text.latex.preamble'] = [r'\usepackage{sfmath} \boldmath']

DLMC_DATA = "/sdb/cache/workingset_size/dlmc"
SS_DATA = "/sdb/cache/workingset_size/ss"

plt.rcParams["font.family"] = "Times New Roman"
plt.rcParams.update({'font.size': 14})
plt.rcParams["figure.figsize"] = (7, 6)
plt.rcParams['axes.xmargin'] = 0
plt.rcParams['axes.ymargin'] = 0

SS_SPARSITY_RANGES = [0.7, 1.0]
DLMC_SPARSITY_RANGES = [0.7, 1.0]

np.random.seed(42)

fig, ax = plt.subplots()
ax.spines.right.set_visible(False)
ax.spines.top.set_visible(False)

def rand_jitter(arr):
    stdev = .005 * (max(arr) - min(arr))
    return arr + np.random.randn(len(arr)) * stdev

tile_sizes = np.load(f"{DLMC_DATA}/tile_sizes.npy")
dlmc_files = np.random.choice(glob.glob(DLMC_DATA + "/**/*_working_set.npz", recursive=True), 200)

tile_sizes = np.load(f"{SS_DATA}/tile_sizes.npy")
ss_files = np.random.choice(glob.glob(SS_DATA + "/**/*_working_set.npz", recursive=True), 200)

for file in ss_files:
    working_set_sizes = np.load(file)
    cov = working_set_sizes["cov"]
    if len(cov) == 0: continue
    sparsity = working_set_sizes["sparsity"]

    if sparsity < SS_SPARSITY_RANGES[0] or sparsity > SS_SPARSITY_RANGES[1]: continue
    last_ss = plt.scatter(rand_jitter(tile_sizes[:len(cov)]), cov, color="navy", label="SuiteSparse",
                          alpha=float(sparsity), s=1)

for file in dlmc_files:
    working_set_sizes = np.load(file)
    cov = working_set_sizes["cov"]
    sparsity = working_set_sizes["sparsity"]

    if sparsity < DLMC_SPARSITY_RANGES[0] or sparsity > DLMC_SPARSITY_RANGES[1]: continue
    last_dlmc = plt.scatter(rand_jitter(tile_sizes[:len(cov)]), cov, color="orangered", label="DLMC",
                            alpha=float(sparsity), s=1)

ticks = [f'{i}x{i}' for i in tile_sizes]
plt.xticks(tile_sizes[::6], ticks[::6], rotation=45)
plt.xlabel(r"Tile Size")
plt.ylabel(r"Coefficient of Variation")

#plt.tight_layout()

lgnd = plt.legend(handles=[last_ss, last_dlmc], loc='upper right')
lgnd.legendHandles[0]._sizes = [10]
lgnd.legendHandles[1]._sizes = [10]
#
plt.margins(x=0)
plt.tight_layout()

plot_save("working_set_size")


import os
import torch
from diffusers.models.cross_attention import LoRACrossAttnProcessor
from diffusers import StableDiffusionPipeline
import json

import argparse

parser = argparse.ArgumentParser("generate images from a unet lora checkpoint")
parser.add_argument("--unet_weight", default="", type=str)
parser.add_argument("--prompts", default="validation_prompts_500.json", type=str)
parser.add_argument("--folder", default="dump", type=str)
parser.add_argument("--negative_prompt", default="Weird image. ", type=str)
parser.add_argument("--world_size", default=-1, type=int)
parser.add_argument("--rank", default=-1, type=int)

args = parser.parse_args()

use_command_line_rank = args.world_size > 0 and args.rank >= 0

unet_weight = args.unet_weight
negative_prompt = args.negative_prompt

local_rank = 0
for v in ('LOCAL_RANK', 'MPI_LOCALRANKID', 'SLURM_LOCALID', 'OMPI_COMM_WORLD_LOCAL_RANK'):
    if v in os.environ:
        local_rank = int(os.environ[v])
        if use_command_line_rank:
            # make sure that only use one GPU when directly specify world size and rank from command line
            assert local_rank == 0
        break

if use_command_line_rank:
    rank = args.rank
else:
    rank = 0
    for v in ('RANK', 'PMI_RANK', 'SLURM_PROCID', 'OMPI_COMM_WORLD_RANK'):
        if v in os.environ:
            rank = int(os.environ[v])
            break

if use_command_line_rank:
    world_size = args.world_size
else:
    world_size = 1
    for v in ('WORLD_SIZE', 'PMI_SIZE', 'SLURM_NTASKS', 'OMPI_COMM_WORLD_SIZE'):
        if v in os.environ:
            world_size = int(os.environ[v])
            break

with open(args.prompts) as f:
    pairs = json.load(f)

pipeline = StableDiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4",
    torch_dtype=torch.float16,
).to(local_rank)

if unet_weight:
    model_weight = torch.load(unet_weight, map_location='cpu')
    unet = pipeline.unet
    lora_attn_procs = {}
    lora_rank = list(set([v.size(0) for k, v in model_weight.items() if k.endswith("down.weight")]))
    assert len(lora_rank) == 1
    lora_rank = lora_rank[0]
    for name in unet.attn_processors.keys():
        cross_attention_dim = None if name.endswith("attn1.processor") else unet.config.cross_attention_dim
        if name.startswith("mid_block"):
            hidden_size = unet.config.block_out_channels[-1]
        elif name.startswith("up_blocks"):
            block_id = int(name[len("up_blocks.")])
            hidden_size = list(reversed(unet.config.block_out_channels))[block_id]
        elif name.startswith("down_blocks"):
            block_id = int(name[len("down_blocks.")])
            hidden_size = unet.config.block_out_channels[block_id]

        lora_attn_procs[name] = LoRACrossAttnProcessor(
            hidden_size=hidden_size, cross_attention_dim=cross_attention_dim, rank=lora_rank
        ).to(local_rank)
    unet.set_attn_processor(lora_attn_procs)
    unet.load_state_dict(model_weight, strict=False)

if not os.path.exists(args.folder):
    os.makedirs(args.folder)

generator = torch.Generator(device='cuda').manual_seed(rank + 1)

for i, pair in enumerate(pairs):
    if i % world_size != rank:
        continue
    if os.path.exists(f"{args.folder}/{i}.jpg"):
        continue
    with torch.no_grad():
        raw_images = pipeline([pair], num_inference_steps=50, generator=generator, negative_prompt=[negative_prompt]).images
    for j, image in enumerate(raw_images):
        image.save(f"{args.folder}/{i}.jpg", quality=90)


import cv2
import numpy as np
from threading import *
import time

face_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

Video = cv2.VideoCapture(0)
time.sleep(3)

def face(faces):
    dimension=len(faces)
    if (dimension!=0):
        for (x,y,w,h) in faces:
            cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
    else:
        pass

while True:
    check,frame=Video.read()

    faces=face_cascade.detectMultiScale(frame)
    face(faces)
    cv2.imshow('Captures',frame)
    key=cv2.waitKey(1)
    if key == ord('q'):
        break
Video.release()
cv2.destroyAllWindows()


import paddle
import paddle.nn as nn
import paddle.nn.functional as F

class PointwiseMatching(nn.Layer):
    def __init__(self, pretrained_model, dropout=None):
        super().__init__()
        self.ptm = pretrained_model
        self.dropout = nn.Dropout(dropout if dropout is not None else 0.1)
        self.classifier = nn.Linear(self.ptm.config["hidden_size"], 2)

    def forward(
        self, input_ids, token_type_ids=None, position_ids=None, attention_mask=None
    ):
        _, cls_embedding = self.ptm(
            input_ids, token_type_ids, position_ids, attention_mask
        )
        cls_embedding = self.dropout(cls_embedding)
        logits = self.classifier(cls_embedding)
        return logits

class PairwiseMatching(nn.Layer):
    def __init__(self, pretrained_model, dropout=None, margin=0.1):
        super().__init__()
        self.ptm = pretrained_model
        self.dropout = nn.Dropout(dropout if dropout is not None else 0.1)
        self.margin = margin
        self.similarity = nn.Linear(self.ptm.config["hidden_size"], 1)

    def predict(
        self, input_ids, token_type_ids=None, position_ids=None, attention_mask=None
    ):
        _, cls_embedding = self.ptm(
            input_ids, token_type_ids, position_ids, attention_mask
        )
        cls_embedding = self.dropout(cls_embedding)
        sim_score = self.similarity(cls_embedding)
        sim_score = F.sigmoid(sim_score)
        return sim_score

    def forward(
        self,
        pos_input_ids,
        neg_input_ids,
        pos_token_type_ids=None,
        neg_token_type_ids=None,
        pos_position_ids=None,
        neg_position_ids=None,
        pos_attention_mask=None,
        neg_attention_mask=None,
    ):
        _, pos_cls_embedding = self.ptm(
            pos_input_ids, pos_token_type_ids, pos_position_ids, pos_attention_mask
        )
        _, neg_cls_embedding = self.ptm(
            neg_input_ids, neg_token_type_ids, neg_position_ids, neg_attention_mask
        )
        pos_embedding = self.dropout(pos_cls_embedding)
        neg_embedding = self.dropout(neg_cls_embedding)
        pos_sim = self.similarity(pos_embedding)
        neg_sim = self.similarity(neg_embedding)
        pos_sim = F.sigmoid(pos_sim)
        neg_sim = F.sigmoid(neg_sim)
        labels = paddle.full(
            shape=[pos_cls_embedding.shape[0]], fill_value=1.0, dtype="float32"
        )
        loss = F.margin_ranking_loss(pos_sim, neg_sim, labels, margin=self.margin)
        return loss


from scipy.spatial import distance as dist
from collections import OrderedDict
import numpy as np
import cv2
import enum

class Colors(enum.IntEnum):
   Green = 0
   Red = 1
   Blue = 2
   Yellow = 3
   Cyan = 4
   Purple = 5

class ColorDetector:
   def __init__(self, colorSettings):
      self.initColors(colorSettings)

   def detectColor(self, frame, contour):
      # construct a mask for the contour, then compute the
      # average L*a*b* value for the masked region
      mask = np.zeros(frame.shape[:2], dtype="uint8")
      cv2.drawContours(mask, [contour], -1, 255, -1)
      mask = cv2.erode(mask, None, iterations=2)
      mean = cv2.mean(frame, mask=mask)[:3]
      # initialize the minimum distance found thus far
      minDist = (np.inf, None)
      # loop over the known L*a*b* color values
      for (i, color) in enumerate(self.colors):
          # compute the distance between the current L*a*b*
          # color value and the mean of the image
          distance = dist.euclidean(color, mean)
          # if the distance is smaller than the current distance,
          # then update the bookkeeping variable
          if distance < minDist[0]:
              minDist = (distance, i)
          # return the name of the color with the smallest distance
      return self.colorCodes[minDist[1]]

   def initColors(self, colorSettings):
      colors = OrderedDict()
      colors[Colors.Red] = colorSettings.baseRedColor
      colors[Colors.Purple] = colorSettings.basePurpleColor
      colors[Colors.Green] = colorSettings.baseGreenColor
      colors[Colors.Cyan] = colorSettings.baseCyanColor
      colors[Colors.Blue] = colorSettings.baseBlueColor
      colors[Colors.Yellow] = colorSettings.baseYellowColor

      # allocate memory for the L*a*b* image, then initialize
      # the color names list
      self.colors = np.zeros((len(colors), 1, 3), dtype="uint8")
      self.colorCodes = []
      # loop over the colors dictionary
      for (i, (colorCode, value)) in enumerate(colors.items()):
          self.colors[i] = value
          self.colorCodes.append(colorCode)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import datetime

class Migration(migrations.Migration):

    dependencies = [
        ('PsirtPageAnalyzer', '0004_auto_20150727_1658'),
    ]

    operations = [
        migrations.AlterField(
            model_name='vulnerability',
            name='firstPublishedDate',
            field=models.DateTimeField(verbose_name='first published', default=datetime.datetime(2015, 8, 28, 9, 48, 25, 52340)),
        ),
        migrations.AlterField(
            model_name='vulnerability',
            name='lastUpdatedDate',
            field=models.DateTimeField(verbose_name='last updated', default=datetime.datetime(2015, 8, 28, 9, 48, 25, 52340)),
        ),
    ]


from __future__ import print_function

import grpc
import numpy as np
import requests
import sys
import tensorflow.compat.v1 as tf_v1
import tensorflow as tf
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2_grpc

from util import preprocess_image

# The image URL is the location of the image we should send to the server
IMAGE_URL = 'https://tensorflow.org/images/blogs/serving/cat.jpg'

tf_v1.app.flags.DEFINE_string('server', 'localhost:8500', 'PredictionService host:port')
tf_v1.app.flags.DEFINE_string('image', '', 'path to image in JPEG format')
tf_v1.app.flags.DEFINE_string('model', 'resnet50', 'Name of model (resnet50 or Inceptionv3).')
FLAGS = tf_v1.app.flags.FLAGS

def main(_):
    if FLAGS.model == 'resnet50':
        image_size = 224
    elif FLAGS.model == 'inceptionv3':
        image_size = 299
    else:
        print('Please specify model as either resnet50 or Inceptionv3.')
        sys.exit(-1)

    if FLAGS.image:
        with open(FLAGS.image, 'rb') as f:
            data = f.read()
    else:
        # Download the image URL if a path is not provided as input
        dl_request = requests.get(IMAGE_URL, stream=True)
        dl_request.raise_for_status()
        data = dl_request.content

    channel = grpc.insecure_channel(FLAGS.server)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    request = predict_pb2.PredictRequest()
    request.model_spec.name = FLAGS.model
    request.model_spec.signature_name = 'serving_default'
    image_data = tf.reshape(preprocess_image(data, FLAGS.model, image_size), [1, image_size, image_size, 3])

    # Run the graph
    with tf_v1.Session() as sess:
        sess.run(tf_v1.global_variables_initializer())
        image_data = (sess.run(image_data))

    request.inputs['input'].CopyFrom(
        tf.make_tensor_proto(image_data, shape=[1, image_size, image_size, 3]))
    result = stub.Predict(request)
    print('Predicted class: ', str(np.argmax(result.outputs['predict'].float_val)))

if __name__ == '__main__':
    tf_v1.disable_eager_execution()
    tf_v1.app.run()


from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.action_chains import ActionChains
from datetime import datetime
from PIL import Image
from io import BytesIO
import re
import shutil
import requests
import time
import os
import base64

directory = "src"
query = input("What would you like to search? ")
query.replace(" ", "+")
url = 'https://www.google.com/search?q='+str(query)
url = url + '&source=lnms&tbm=isch&sa=X&ved=2ahUKEwie44_AnqLpAhUhBWMBHUFGD90Q_'
url = url + 'AUoAXoECBUQAw&biw=1920&bih=947'
qty = int(input("How many pictures? "))
# Timestring so queries can be run more than once without conflict
runtime = datetime.now().strftime("%Y%m%d%H%M%S")
# run headless
options = FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox(options=options)

def save_img(inp, img, i):
    try:
        filename = runtime + "_" + inp + "_" + str(i)+".jpg"
        image_path = os.path.join(os.getcwd(), directory, filename)
        if (img.startswith("http")):
            image = requests.get(img, stream=True)

            with open(image_path, "wb") as file:
                shutil.copyfileobj(image.raw, file)
        else:
            # Google sometimes stores images in base64 strings, this deals with
            # that
            print("Base64 image (lower res, Sorry!)")
            img = re.sub('^data:image/.+;base64,', '', img)
            byte_data = base64.b64decode(img)
            img_data = BytesIO(byte_data)
            imgfile = Image.open(img_data)
            imgfile.save(image_path, "jpeg")
    except Exception:
        print("Image failed to save!")
        pass

def scrape_images(input, url, driver, quantity):
    try:
        driver.get(url)
        time.sleep(3)
        for j in range(1, quantity+1):
            print("Downloading image: %d" % (j))
            imgurl = driver.find_element_by_xpath('//div//div//div//div//div//div//div//div//div//div['+str(j)+']//a[1]//div[1]//img[1]')
            imgurl.click()
            time.sleep(5)
            # n3VNCb
            # //body/div[2]/c-wiz/div[3]/div[2]/div[3]/div/div/div[3]/div[2]/c-wiz/div[1]/div[1]/div/div[2]/a/img
            try:
                img = driver.find_element_by_xpath("//body/div[2]/c-wiz/.//div[contains(@class, 'tvh9oe BIB1wf')]/.//img[contains(@class, 'n3VNCb')]").get_attribute("src")
            except Exception as b:
                print("Could not find HQ image Error: " % (str(b)))
                img = driver.find_element_by_xpath("//body/div[2]/c-wiz/.//img[contains(@class, 'n3VNCb')]").get_attribute("src")
            save_img(input, img, j)
    except Exception as e:
        print("Could not scrape")
        print(str(e))

scrape_images(query, url, driver, qty)
driver.close()


#! /u/sgupta45/conda/bin/python3
import itertools
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import re

hatch = {'FCFS': '*', 'LEDF': '.', 'GEDF': 'xx', 'GLAX': '/'}

def geo_mean(iterable):
    a = np.array(iterable)
    return a.prod()**(1.0/len(a))

def add_plot(offset, policy, label):
    if policy in hatch:
        plt.bar([i+offset for i in x], occupancy[policy], edgecolor='k',
                width=width, label=label, fc='w', hatch=hatch[policy])
    else:
        plt.bar([i+offset for i in x], occupancy[policy], edgecolor='k',
                width=width, label=label, fc='k')

policies = ['FCFS', 'LEDF', 'GEDF', 'GLAX', 'APRX3']
applications = ['canny', 'deblur', 'gru', 'harris', 'lstm']
app_mixes = sorted(list(itertools.combinations(applications, 3)))

def get_stat(app_mix, interconnect):
    values = []

    base_dir_name = '../image_4_parallel_dma_' + interconnect + '/'
    for app in applications:
        base_dir_name += app + '_'
        if app in app_mix: base_dir_name += '4_'
        else:              base_dir_name += '0_'

    for policy in policies:
        for line in open(base_dir_name + policy + '_pipeline/stats.txt'):
            if 'system.acctest.local_bus.utilization' in line:
                values.append(float(line.split()[1]))
                break

    return values

occupancy = {p:[] for p in policies}

for app_mix in app_mixes:
    bus_util = get_stat(app_mix, 'bus')
    xbar_util = get_stat(app_mix, 'xbar')
    for i, policy in enumerate(policies):
        occupancy[policy].append(xbar_util[i] / bus_util[i])

# calculate geo-mean for each policy
for policy in policies:
    occupancy[policy].append(geo_mean(occupancy[policy]))

# plot parameters
x = np.arange(len(app_mixes) + 1)

# add the bars
plt.figure(figsize=(24, 8), dpi=600)
plt.rc('axes', axisbelow=True)

width = 0.16
add_plot(-(width*2), 'FCFS',  'FCFS')
add_plot(-width,     'LEDF',  'GEDF-D')
add_plot(0,          'GEDF',  'GEDF-N')
add_plot(width,      'GLAX',  'LAX')
add_plot((width*2),  'APRX3', 'ELF')

x_labels = ['Mix ' + str(i) for i in range(len(app_mixes))] + ['Gmean']
plt.xlabel('Application mix', fontsize=35)
plt.xticks(x, x_labels, fontsize=35, rotation='vertical')

plt.ylabel('Interconnect occupancy time\n(xbar / util)', fontsize=35)
plt.yticks(fontsize=35)
#plt.ylim([0, 120])
#plt.gca().yaxis.set_major_locator(plt.MultipleLocator(0.2))

plt.legend(loc="upper left", ncol=5, fontsize=35)
plt.grid(color='silver', linestyle='-', linewidth=1)

# save the image
plt.savefig('plots/interconnect_occupancy.pdf', bbox_inches='tight')


import cv2
import numpy as np
## define main colors
WHITE = (255,255,255)
BLACK = (0,0,0)
BLUE  = (255,0,0)
GREEN = (0,255,0)
RED   = (0,0,255)
CYAN  = (255,255,0)
MAGENTA = (255,0,255)
YELLOW = (0,255,255)
## define points
p0, p1 = (100, 150), (200, 200)
p2 = (50, 250)
redX, greenX, maxX = 2, 2, 30

def REDtrackbar(x):
    x = max(1, x)
    global greenX, redX
    redX = x
    img[:] = WHITE
    cv2.line(img, p0, p1, RED, redX)
    cv2.line(img, p2, p1, GREEN, greenX)
    cv2.imshow('', img)

def GREENtrackbar(x):
    x = max(1, x)
    global greenX, redX
    greenX = x
    img[:] = WHITE
    cv2.line(img, p0, p1, RED, redX)
    cv2.line(img, p2, p1, GREEN, greenX)
    cv2.imshow('', img)

img = np.ones((300, 600, 3), np.uint8)
img[:] = WHITE
cv2.line(img, p0, p1, RED, redX)
cv2.line(img, p2, p1, GREEN, greenX)
cv2.imshow('', img)
cv2.createTrackbar('RED-thickness', '', redX, maxX,  REDtrackbar)
cv2.createTrackbar('GREEN-thickness', '', greenX, maxX, GREENtrackbar)


from aiogram import types
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery
from aiogram.utils.callback_data import CallbackData

from loader import dp
from utils.db_api.dataset import getUser, updateSubs

edit_call = CallbackData("shows_item", "items")

@dp.message_handler(text="Инфо")
async def get_regs(message: types.Message):
    user = getUser(message.from_user.id)
    await message.answer(f"Ваш ник : {message.from_user.username}\n"
                         f"Знак зодиака : {user['zodiak']}\n"
                         f"Подписка на утренний гороскоп :  {user['subs']}")

@dp.message_handler(text="Подписка")
async def get_pod(message: types.Message):
    markup = InlineKeyboardMarkup()
    user = getUser(message.from_user.id)
    sbs = int(user["subs"])
    if sbs == 0:
        tae = "не активна"
        markup.insert(
            InlineKeyboardButton(
                text="Активировать",
                callback_data=edit_call.new(items=0)
            )
        )
    else:
        tae = "активна"
        markup.insert(
            InlineKeyboardButton(
                text="Деактивирвть",
                callback_data=edit_call.new(items=1)
            )
        )

    await message.answer(f"Ваша утренняя подписка на гороскоп  {tae}",
                         reply_markup=markup)

@dp.callback_query_handler(edit_call.filter())
async def show_edit(call: CallbackQuery, callback_data: dict):
    # print(callback_data)
    dto = int(callback_data['items'])
    if dto == 1:
        updateSubs(0, call.from_user.id)
        await call.message.answer("Подписка отключена")
    else:
        updateSubs(1, call.from_user.id)
        await call.message.answer("Подписка включена")


from unittest import TestCase, skipIf
import time
import re
import sys
import multiprocessing as mp
from queue import Empty
from unittest.mock import patch

import qcodes
from qcodes.process.helpers import set_mp_method, kill_queue
from qcodes.process.qcodes_process import QcodesProcess
from qcodes.process.stream_queue import get_stream_queue, _SQWriter
from qcodes.process.server import ServerManager, RESPONSE_OK, RESPONSE_ERROR
import qcodes.process.helpers as qcmp
from qcodes.utils.helpers import in_notebook, LogCapture
from qcodes.utils.timing import calibrate

BREAK_SIGNAL = '~~BREAK~~'

class sqtest_echo:
    def __init__(self, name, delay=0.01, has_q=True):
        self.q_out = mp.Queue()
        self.q_err = mp.Queue()
        self.p = QcodesProcess(target=sqtest_echo_f,
                               args=(name, delay, self.q_out, self.q_err,
                                     has_q),
                               name=name)
        self.p.start()
        self.delay = delay
        self.resp_delay = delay * 2 + 0.03

    def send_out(self, msg):
        self.q_out.put(msg)
        time.sleep(self.resp_delay)

    def send_err(self, msg):
        self.q_err.put(msg)
        time.sleep(self.resp_delay)

    def halt(self):
        if not (hasattr(self, 'p') and self.p.is_alive()):
            return
        self.q_out.put(BREAK_SIGNAL)
        self.p.join()
        time.sleep(self.resp_delay)
        for q in ['q_out', 'q_err']:
            if hasattr(self, q):
                queue = getattr(self, q)
                kill_queue(queue)
                kill_queue(queue)  # repeat just to make sure it doesn't error

    def __del__(self):
        self.halt()

def sqtest_echo_f(name, delay, q_out, q_err, has_q):
    while True:
        time.sleep(delay)

        if not q_out.empty():
            out = q_out.get()

            if out == BREAK_SIGNAL:
                # now test that disconnect works, and reverts to
                # regular stdout and stderr
                if has_q:
                    try:
                        get_stream_queue().disconnect()
                    except RuntimeError:
                        pass
                    print('stdout ', end='', flush=True)
                    print('stderr ', file=sys.stderr, end='', flush=True)
                break

            print(out, end='', flush=True)

        if not q_err.empty():
            print(q_err.get(), file=sys.stderr, end='', flush=True)

def sqtest_exception():
    raise RuntimeError('Boo!')

class TestMpMethod(TestCase):
    def test_set_mp_method(self):
        start_method = mp.get_start_method()
        self.assertIn(start_method, ('fork', 'spawn', 'forkserver'))

        # multiprocessing's set_start_method is NOT idempotent
        with self.assertRaises(RuntimeError):
            mp.set_start_method(start_method)

        # but ours is
        set_mp_method(start_method)

        # it will still error on gibberish, but different errors depending
        # on whether you force or not
        with self.assertRaises(RuntimeError):
            set_mp_method('spoon')
        with self.assertRaises(ValueError):
            set_mp_method('spoon', force=True)

        # change the error we look for to test strange error handling
        mp_err_normal = qcmp.MP_ERR
        qcmp.MP_ERR = 'who cares?'
        with self.assertRaises(RuntimeError):
            set_mp_method('start_method')
        qcmp.MP_ERR = mp_err_normal

class TestQcodesProcess(TestCase):
    def setUp(self):
        mp_stats = calibrate(quiet=True)
        self.MP_START_DELAY = mp_stats['mp_start_delay']
        self.MP_FINISH_DELAY = mp_stats['mp_finish_delay']
        self.SLEEP_DELAY = mp_stats['sleep_delay']
        self.BLOCKING_TIME = mp_stats['blocking_time']
        self.sq = get_stream_queue()

    @skipIf(getattr(qcodes, '_IN_NOTEBOOK', False),
            'called from notebook')
    def test_not_in_notebook(self):
        # below we'll patch this to True, but make sure that it's False
        # in the normal test runner.
        self.assertEqual(in_notebook(), False)

        # and make sure that processes run this way do not use the queue
        with self.sq.lock:
            p = sqtest_echo('hidden', has_q=False)
            time.sleep(self.MP_START_DELAY)
            p.send_out('should go to stdout;')
            p.send_err('should go to stderr;')
            p.halt()

            self.assertEqual(self.sq.get(), '')

    @patch('qcodes.process.qcodes_process.in_notebook')
    def test_qcodes_process_exception(self, in_nb_patch):
        in_nb_patch.return_value = True

        with self.sq.lock:
            name = 'Hamlet'
            p = QcodesProcess(target=sqtest_exception, name=name)

            initial_outs = (sys.stdout, sys.stderr)

            # normally you call p.start(), but for this test we want
            # the function to actually run in the main process
            # it will run the actual target, but will print the exception
            # (to the queue) rather than raising it.
            p.run()

            # output streams are back to how they started
            self.assertEqual((sys.stdout, sys.stderr), initial_outs)
            time.sleep(0.01)
            exc_text = self.sq.get()
            # but we have the exception in the queue
            self.maxDiff = None
            self.assertGreaterEqual(exc_text.count(name + ' ERR'), 5)
            self.assertEqual(exc_text.count('Traceback'), 1, exc_text)
            self.assertEqual(exc_text.count('RuntimeError'), 2)
            self.assertEqual(exc_text.count('Boo!'), 2)

    @patch('qcodes.process.qcodes_process.in_notebook')
    def test_qcodes_process(self, in_nb_patch):
        in_nb_patch.return_value = True

        queue_format = re.compile(
            '^\[\d\d:\d\d:\d\d\.\d\d\d p\d( ERR)?\] [^\[\]]*$')

        with self.sq.lock:
            p1 = sqtest_echo('p1')
            p2 = sqtest_echo('p2')
            time.sleep(self.MP_START_DELAY + p1.delay + p2.delay)

            self.assertEqual(self.sq.get(), '')

            procNames = ['<{}>'.format(name)
                         for name in ('p1', 'p2')]

            reprs = [repr(p) for p in mp.active_children()]
            for name in procNames:
                self.assertIn(name, reprs)

            # test each individual stream to send several messages on same
            # and different lines

            for sender, label, term in ([[p1.send_out, 'p1] ', ''],
                                         [p1.send_err, 'p1 ERR] ', '\n'],
                                         [p2.send_out, 'p2] ', '\n'],
                                         [p2.send_err, 'p2 ERR] ', '']]):
                sender('row row ')
                sender('row your boat\n')
                sender('gently down ')
                time.sleep(0.01)
                data = [line for line in self.sq.get().split('\n') if line]
                expected = [
                    label + 'row row row your boat',
                    label + 'gently down '
                ]
                # TODO - intermittent error here
                self.assertEqual(len(data), len(expected), data)
                for line, expected_line in zip(data, expected):
                    self.assertIsNotNone(queue_format.match(line), data)
                    self.assertEqual(line[14:], expected_line, data)

                sender(' the stream' + term)
                # no label/header as we're continuing  the previous line
                self.assertEqual(self.sq.get(), ' the stream' + term)

            p1.send_out('marco')
            p2.send_out('polo\n')  # we don't see these single terminators
            p1.send_out('marco\n')  # when we change streams
            p2.send_out('polo')
            time.sleep(0.01)

            data = self.sq.get().split('\n')
            for line in data:
                if line:
                    self.assertIsNotNone(queue_format.match(line))

            data_msgs = [line[14:] for line in data]
            expected = [
                '',
                'p1] marco',
                'p2] polo',
                'p1] marco',
                'p2] polo'
            ]
            self.assertEqual(data_msgs, expected)

            # Some OS's start more processes just for fun... so don't test
            # that p1 and p2 are the only ones.
            # self.assertEqual(len(reprs), 2, reprs)

            p1.halt()
            p2.halt()
            # both p1 and p2 should have finished now, and ended.
            reprs = [repr(p) for p in mp.active_children()]
            for name in procNames:
                self.assertNotIn(name, reprs)

class TestStreamQueue(TestCase):
    def test_connection(self):
        sq = get_stream_queue()
        sq.connect('')
        # TODO: do we really want double-connect to raise? or maybe
        # only raise if the process name changes?
        with self.assertRaises(RuntimeError):
            sq.connect('')
        sq.disconnect()
        with self.assertRaises(RuntimeError):
            sq.disconnect()

    def test_del(self):
        sq = get_stream_queue()
        self.assertTrue(hasattr(sq, 'queue'))
        self.assertTrue(hasattr(sq, 'lock'))
        self.assertIsNotNone(sq.instance)

        sq.__del__()

        self.assertFalse(hasattr(sq, 'queue'))
        self.assertFalse(hasattr(sq, 'lock'))
        self.assertIsNone(sq.instance)

        sq.__del__()  # just to make sure it doesn't error

    # this is basically tested in TestQcodesProcess, but the test happens
    # in a subprocess so coverage doesn't know about it. Anyway, there are
    # a few edge cases left that we have to test locally.
    def test_sq_writer(self):
        sq = get_stream_queue()
        with sq.lock:
            sq_clearer = _SQWriter(sq, 'Someone else')
            sq_clearer.write('Boo!\n')

            # apparently we need a little delay to make sure the queue
            # properly appears populated.
            time.sleep(0.01)

            sq.get()
            sq_name = 'A Queue'
            sqw = _SQWriter(sq, sq_name)

            # flush should exist, but does nothing
            sqw.flush()

            lines = [
                'Knock knock.\nWho\'s there?\n',
                'Interrupting cow.\n',
                'Interr-',
                'MOO!\n',
                ''
            ]

            for line in lines:
                sqw.write(line)

            time.sleep(0.01)

            # _SQWriter doesn't do any transformations to the messages, beyond
            # adding a time string and the stream name
            for line in lines:
                if not line:
                    self.assertEqual(sq.queue.empty(), True)
                    continue

                self.assertEqual(sq.queue.empty(), False)
                timestr, stream_name, msg = sq.queue.get()
                self.assertEqual(msg, line)
                self.assertEqual(stream_name, sq_name)

            # now test that even if the queue is unwatched the messages still
            # go there. If we're feeling adventurous maybe we can test if
            # something was actually printed.
            sqw.MIN_READ_TIME = -1
            new_message = 'should get printed\n'
            sqw.write(new_message)

            time.sleep(0.01)

            self.assertEqual(sq.queue.empty(), False)
            self.assertEqual(sq.queue.get()[2], new_message)

            # test that an error in writing resets stdout and stderr
            # nose uses its own stdout and stderr... so keep them (as we will
            # force stdout and stderr to several other things) so we can put
            # them back at the end
            nose_stdout = sys.stdout
            nose_stderr = sys.stderr
            sys.stdout = _SQWriter(sq, 'mock_stdout')
            sys.stderr = _SQWriter(sq, 'mock_stderr')
            self.assertNotIn(sys.stdout, (sys.__stdout__, nose_stdout))
            self.assertNotIn(sys.stderr, (sys.__stderr__, nose_stderr))

            sqw.MIN_READ_TIME = 'not a number'
            with self.assertRaises(TypeError):
                sqw.write('trigger an error')

            self.assertEqual(sys.stdout, sys.__stdout__)
            self.assertEqual(sys.stderr, sys.__stderr__)

            sys.stdout = nose_stdout
            sys.stderr = nose_stderr
            time.sleep(0.01)
            sq.get()

class ServerManagerTest(ServerManager):
    def _start_server(self):
        # don't really start the server - we'll test its pieces separately,
        # in the main process
        pass

class EmptyServer:
    def __init__(self, query_queue, response_queue, extras):
        query_queue.put('why?')
        response_queue.put(extras)

class CustomError(Exception):
    pass

def delayed_put(queue, val, delay):
    time.sleep(delay)
    queue.put(val)

class TestServerManager(TestCase):
    def check_error(self, manager, error_str, error_class):
        manager._response_queue.put(RESPONSE_ERROR, error_str)
        with self.assertRaises(error_class):
            manager.ask('which way does the wind blow?')

    def test_mechanics(self):
        extras = 'super secret don\'t tell anyone'

        sm = ServerManagerTest(name='test', server_class=EmptyServer,
                               shared_attrs=extras)
        sm._run_server()

        self.assertEqual(sm._query_queue.get(timeout=1), 'why?')
        self.assertEqual(sm._response_queue.get(timeout=1), extras)

        # builtin errors we propagate to the server
        builtin_error_str = ('traceback\n  lines\n and then\n'
                             '  OSError: your hard disk went floppy.')
        sm._response_queue.put((RESPONSE_ERROR, builtin_error_str))
        with self.assertRaises(OSError):
            sm.ask('which way does the wind blow?')

        # non-built-in errors we fall back on RuntimeError
        custom_error_str = ('traceback\nlines\nand then\n'
                            'CustomError: the Balrog is loose!')
        extra_resp1 = 'should get tossed by the error checker'
        extra_resp2 = 'so should this.'
        sm._response_queue.put((RESPONSE_OK, extra_resp1))
        sm._response_queue.put((RESPONSE_OK, extra_resp2))
        sm._response_queue.put((RESPONSE_ERROR, custom_error_str))

        # TODO: we have an intermittent failure below, but only when running
        # the full test suite (including pyqt and matplotlib?), not if we
        # run just this module, or at least not nearly as frequently.
        time.sleep(0.2)

        with LogCapture() as logs:
            with self.assertRaises(RuntimeError):
                sm.ask('something benign')
            self.assertTrue(sm._response_queue.empty())
        self.assertIn(extra_resp1, logs.value)
        self.assertIn(extra_resp2, logs.value)

        # extra responses to a query, only the last should be taken
        extra_resp1 = 'boo!'
        extra_resp2 = 'a barrel of monkeys!'
        sm._response_queue.put((RESPONSE_OK, extra_resp1))
        sm._response_queue.put((RESPONSE_OK, extra_resp2))
        time.sleep(0.05)
        p = mp.Process(target=delayed_put,
                       args=(sm._response_queue, (RESPONSE_OK, 42), 0.05))
        p.start()

        with LogCapture() as logs:
            self.assertEqual(sm.ask('what is the answer'), 42)
        self.assertIn(extra_resp1, logs.value)
        self.assertIn(extra_resp2, logs.value)

        # no response to a query
        with self.assertRaises(Empty):
            sm.ask('A sphincter says what?', timeout=0.05)

        # test halting an unresponsive server
        sm._server = mp.Process(target=time.sleep, args=(1000,))
        sm._server.start()

        self.assertIn(sm._server, mp.active_children())

        with LogCapture() as logs:
            sm.halt(0.01)
        self.assertIn('ServerManager did not respond '
                      'to halt signal, terminated', logs.value)

        self.assertNotIn(sm._server, mp.active_children())

    def test_pathological_edge_cases(self):
        # kill_queue should never fail
        kill_queue(None)

        # and halt should ignore AssertionErrors, which arise in
        # subprocesses when trying to kill a different subprocess
        sm = ServerManagerTest(name='test', server_class=None)

        class HorribleProcess:
            def is_alive(self):
                raise AssertionError

            def write(self):
                raise AssertionError

            def join(self):
                raise AssertionError

        sm._server = HorribleProcess()

        sm.halt()


from django.shortcuts import render

from django.http import HttpResponse
from .models import *

def index(request):

	data = {}
	# get posts in descending order by date
	data["recents"] = Article.objects.all().order_by('-posted')[:10]
	data["most_read"] = MostRead.objects.get()

	# now get the list of all sections
	sections = Section.objects.all()

	#for asection in sections:
		#section_name = str(asection.name)
		#article_in_section = Article.objects.where(Section = asection).latest('posted')
		#data['sections'][section_name] = article_in_section

	template_name = 'index.html'
	return render(request, template_name, data)

def section(request, section):

	data = {}
	# print Article.objects.filter(Section__slug__iexact=section).order_by('-posted')[:10]
	# get posts in descending order by date
	data["recents"] = Article.objects.filter(Section__slug__iexact=section).order_by('-posted')[:10]
	data["most_read"] = MostRead.objects.get()

	template_name = 'section.html'
	return render(request, template_name, data)

def article(request, slug):

	data = {}

	data["article"] = Article.objects.get(slug__iexact=slug)
	data["most_read"] = MostRead.objects.get()

	template_name = 'article.html'
	return render(request, template_name, data)

def advertising(request):
  template_name = 'extra/advertising.html'
  return render(request,template_name)

def subscribe(request):
  template_name = 'extra/subscribe.html'
  return render(request,template_name)

def about(request):

	template_name = 'extra/about.html'
	return render(request,template_name)

def tag(request, tag):

	data = {}
	data["recents"] = Article.objects.filter(tags__slug=tag).order_by('-posted')[:10]
	data["tag"] = Tag.objects.get(slug__iexact=tag)

	template_name = 'tag.html'
	return render(request, template_name, data)


"""
Output the eventalign values given the following input:
Raw Fast5 folder, match_fasta_ref file, outdir

Usage:

python get_eventalign.py fast5_dir match_fasta_ref.txt outdir hpc/local
"""

import sys
import os
import subprocess
import pathlib

fast5_dir = sys.argv[1]
match_fasta_ref_file = sys.argv[2]
outdir    = sys.argv[3]
executor  = sys.argv[4]

if not executor == "hpc":
    executor = "local"

with open(match_fasta_ref_file) as f:
    for line in f:
        _5mer, fasta, ref = line.split()

nanopolish_index = "nanopolish index -d " + fast5_dir + " " + fasta
minimap2         = "minimap2 -ax map-ont " + ref + " " + fasta + " | samtools sort -o " + outdir + "/" + _5mer + ".sorted.bam -T " + _5mer + ".tmp"
samtools_index   = "samtools index " + outdir + "/" + _5mer + ".sorted.bam"
nanopolish_align = "nanopolish eventalign --reads " + fasta + " --bam " + outdir + "/" + _5mer + ".sorted.bam --genome " + ref + " --scale-events > " + outdir + "/" +_5mer + ".eventalign.txt"

commands = ["module load minimap2/2.17", "module load samtools/1.4.1", "module load nanopolish/v0.13.2", nanopolish_index, minimap2, samtools_index, nanopolish_align]
pathlib.Path(outdir).mkdir(parents=True, exist_ok=True)
command  = " && ".join(commands)
if executor == "hpc":
    command = 'bsub -q short -n 1 -W 4:00 -R select[rh=6] -R rusage[mem=4000] -o ' + outdir + "/" + _5mer + ".log " + '"' + command  + '"'

print(_5mer, " running ...")
subprocess.call(command, shell = True)
if (executor == "hpc"):
    print(_5mer, " submitted to hpc!")
else:
    print(_5mer, " done!")


import numpy as np
import pandas as pd
from scipy import io, stats
import seaborn as sns
from matplotlib import pyplot as plt

# Convenience functions to handle data import
def import_expression_data(mtx_gene_exp_fil, barcodes_fil, features_fil):

    gene_expression = io.mmread(mtx_gene_exp_fil)
    barcodes = pd.read_csv(barcodes_fil, header=None)[0]
    features = pd.read_csv(features_fil, header=None)[0]

    gene_expression = pd.DataFrame(gene_expression.toarray(), index=barcodes, columns=features)

    return gene_expression

# Insert your code into the function shells below
def task_1_a(relative_path_to_data = '../exercise_1_data/'):

    gene_expression = import_expression_data(relative_path_to_data+'expression_data_1.mtx',
                                             relative_path_to_data+'expression_data_1_barcodes.tsv',
                                             relative_path_to_data+'expression_data_1_genes.tsv')
    cell_annotations = pd.read_csv(relative_path_to_data+'expression_data_1_metadata.tsv', sep='\t', index_col=0)

    organ_identity = {'A': '', 'B': '', 'C': '', 'D': '', 'E': '', 'F': ''}
    cell_type_identity = {'T1': '', 'T2': '', 'T3': '', 'T4': '', 'T5': ''}

    '''Insert your code here and update the dictionaries with your identification.
    Take care to use the same annotations are used in the paper.'''

    organ_identity = pd.Series(organ_identity).to_frame()
    cell_type_identity = pd.Series(cell_type_identity).to_frame()

    return organ_identity, cell_type_identity

def task_1_b(relative_path_to_data = '../exercise_1_data/'):

    gene_expression = import_expression_data(relative_path_to_data+'expression_data_2.mtx',
                                             relative_path_to_data+'expression_data_2_barcodes.tsv',
                                             relative_path_to_data+'expression_data_2_genes.tsv')

    cell_annotations = pd.DataFrame(index=gene_expression.index.values, columns=['phenotype'])

    '''Insert code here and update the cell_annotations dataframe with the phenotypes.
       Take care to use the same annotations are used in the paper.'''

    return cell_annotations

def task_2_a(relative_path_to_data = '../exercise_1_data/'):

    cell_sequences = np.load(relative_path_to_data+'tex_sampling.npy')
    cell_annotations = pd.read_csv(relative_path_to_data+'expression_data_3_metadata.txt', sep='\t', index_col=0)

    upper_trajectory_louvain_ordering = []
    lower_trajectory_louvain_ordering = []

    '''Insert code here and append each list with the sequence of louvain clusters.'''

    return upper_trajectory_louvain_ordering, lower_trajectory_louvain_ordering

def task_2_b(relative_path_to_data = '../exercise_1_data/'):

    gene_expression = import_expression_data(relative_path_to_data+'expression_data_3.mtx',
                                             relative_path_to_data+'expression_data_3_barcodes.tsv',
                                             relative_path_to_data+'expression_data_3_genes.tsv')

    cell_annotations = pd.read_csv(relative_path_to_data+'expression_data_3_metadata.txt', sep='\t', index_col=0)

    gene_correlations = pd.DataFrame(index=gene_expression.columns.values, columns=['correlation', 'p-values'])

    '''Insert your code here.'''

    return gene_correlations

# Run the code after updating all the functions
def main(relative_path_to_data = '../exercise_1_data/', output_path=''):

    #Task 1A output
    t1_organ_identity, t1_cell_type_identity = task_1_a(relative_path_to_data)
    t1_organ_identity.to_csv(output_path+'t1_organ_identity.txt', header=False)
    t1_cell_type_identity.to_csv(output_path+'t1_celltype_identity.txt', header=False)

    t1b_cell_annotations = task_1_b(relative_path_to_data)
    t1b_cell_annotations.to_csv(output_path+'t1b_cell_annotations.txt', sep='\t')

    t2a_upper_trajectory_louvain_ordering, t2a_lower_trajectory_louvain_ordering = task_2_a(relative_path_to_data)
    pd.DataFrame(t2a_upper_trajectory_louvain_ordering).to_csv(output_path+'t2a_upper_louvain_sequence.txt', header=False)
    pd.DataFrame(t2a_lower_trajectory_louvain_ordering).to_csv(output_path+'t2a_lower_louvain_sequence.txt', header=False)

    t2b_gene_correlations = task_2_b(relative_path_to_data)
    t2b_gene_correlations.sort_values('correlation').iloc[:10].to_csv(output_path+'t2b_gene_correlations.txt', sep='\t')

    sns.scatterplot(t2b_gene_correlations.correlation, -np.log(t2b_gene_correlations['p-values']))
    plt.savefig('t2b_volcano_plot.png')

    return t1_cell_type_identity, t1_cell_type_identity, t1b_cell_annotations, t2a_upper_trajectory_louvain_ordering, t2a_lower_trajectory_louvain_ordering, t2b_gene_correlations

if __name__ == "__main__":

    # Update paths if required
    t1_cell_type_identity, t1_cell_type_identity, t1b_cell_annotations, t2a_upper_trajectory_louvain_ordering,
    t2a_lower_trajectory_louvain_ordering, t2b_gene_correlations = main('../exercise_1_data/', './')


# !/usr/bin/python3.8
# -*- coding: utf-8 -*-
# @Author: qixitan
# @Email: qixitan@qq.com
# @FileName: ViT.py
# @Time: 2022/4/20 16:22

import torch
from torch import nn

from einops import rearrange
from einops.layers.torch import Reduce
from torch.nn import functional as F

class PatchEmbedding(nn.Module):
    def __init__(self, inplanes: int = 3, patch_size: int = 16, emb_size: int = 768, image_size:int=224):
        super(PatchEmbedding, self).__init__()
        self.patch_size = patch_size
        self.projection = nn.Conv2d(inplanes, emb_size, kernel_size=patch_size, stride=patch_size)
        self.cls_token = nn.Parameter(torch.randn(1, 1, emb_size))
        self.positions = nn.Parameter(torch.randn((image_size//patch_size)**2+1, emb_size))

    def forward(self, x):
        out = self.projection(x)
        c, b, w, h = out.shape
        out = out.view(c, b, w*h).permute(0, 2, 1)
        cls_token = self.cls_token.repeat(c, 1, 1)
        out = torch.cat([cls_token, out], dim=1)
        out += self.positions
        return out

class MultiHeadAttention(nn.Module):
    def __init__(self, emb_size: int = 768, num_heads: int = 12, dropout: float = 0):
        super(MultiHeadAttention, self).__init__()
        self.emb_size = emb_size
        self.num_heads = num_heads
        # To speed up the calculation, we can use a single matrix to calculate Queries, keys, values.
        self.qkv = nn.Linear(emb_size, emb_size*3)
        self.att_drop = nn.Dropout(dropout)
        self.projection = nn.Linear(emb_size, emb_size)

    def forward(self, x: torch.Tensor, mask: torch.Tensor = None):
        # split keys, queries and values in num_heads
        qkv = rearrange(self.qkv(x), "b n (h d qkv) -> (qkv) b h n d", h=self.num_heads, qkv=3)
        queries, keys, values = qkv[0], qkv[1], qkv[2]
        # sum up over the last axis
        energy = torch.einsum('bhqd, bhkd -> bhqk', queries, keys)  # batch, num_heads, query_len, key_len
        if mask is not None:
            fill_value = torch.finfo(torch.float32).min
            energy.mask_fill(~mask, fill_value)
        scaling = self.emb_size ** (1 / 2)
        att = F.softmax(energy, dim=-1) / scaling
        att = self.att_drop(att)
        # sum up over the third axis
        out = torch.einsum('bhal, bhlv -> bhav ', att, values)
        out = rearrange(out, "b h n d -> b n (h d)")
        out = self.projection(out)
        return out

class ResidualAdd(nn.Module):
    def __init__(self, fn):
        super().__init__()
        self.fn = fn

    def forward(self, x, **kwargs):
        res = x
        x = self.fn(x, **kwargs)
        x += res
        return x

class FeedForwardBlock(nn.Sequential):  # 直接继承nn.Sequential 避免重复写forward
    def __init__(self, emb_size: int, expansion: int = 4, drop_p: float = 0.):
        super().__init__(
            nn.Linear(emb_size, expansion * emb_size),
            nn.GELU(),
            nn.Dropout(drop_p),
            nn.Linear(expansion * emb_size, emb_size),
        )

class TransformerEncoderBlock(nn.Sequential):
    def __init__(self,
                 emb_size: int = 768,
                 num_heads: int = 12,
                 drop_p: float = 0.,
                 forward_expansion: int = 4,
                 forward_drop_p: float = 0.,
                 ** kwargs):
        super().__init__(
            ResidualAdd(nn.Sequential(
                nn.LayerNorm(emb_size),
                MultiHeadAttention(emb_size, num_heads, **kwargs),
                nn.Dropout(drop_p)
            )),
            ResidualAdd(nn.Sequential(
                nn.LayerNorm(emb_size),
                FeedForwardBlock(
                    emb_size, expansion=forward_expansion, drop_p=forward_drop_p),
                nn.Dropout(drop_p)
            )
            ))

class TransformerEncoder(nn.Sequential):
    def __init__(self, depth: int = 12, num_heads: int = 8, **kwargs):
        super().__init__(*[TransformerEncoderBlock(num_heads=num_heads, **kwargs) for _ in range(depth)])

class ClassificationHead(nn.Sequential):
    def __init__(self, emb_size: int = 768, n_classes: int = 1000):
        super().__init__(
            Reduce('b n e -> b e', reduction='mean'),
            nn.LayerNorm(emb_size),
            nn.Linear(emb_size, n_classes))

class ViT_Ti(nn.Sequential):
    def __init__(self,
                 inplanes: int = 3,
                 emb_size: int = 192,
                 num_heads: int = 3,
                 img_size: int = 224,
                 depth: int = 12,
                 patch_size: int = 16,
                 num_classes: int = 1000,
                 **kwargs):
        super().__init__(
            PatchEmbedding(inplanes, patch_size, emb_size, img_size),
            TransformerEncoder(depth, emb_size=emb_size, num_heads=num_heads, **kwargs),
            ClassificationHead(emb_size, num_classes)
        )

class ViT_S(nn.Sequential):
    def __init__(self,
                 inplanes: int = 3,
                 emb_size: int = 384,
                 num_heads: int = 6,
                 img_size: int = 224,
                 depth: int = 12,
                 patch_size: int = 16,
                 num_classes: int = 1000,
                 **kwargs):
        super().__init__(
            PatchEmbedding(inplanes, patch_size, emb_size, img_size),
            TransformerEncoder(depth, emb_size=emb_size, num_heads=num_heads, **kwargs),
            ClassificationHead(emb_size, num_classes)
        )

class ViT_B(nn.Sequential):
    def __init__(self,
                 inplanes: int = 3,
                 emb_size: int = 768,
                 num_heads: int = 12,
                 img_size: int = 224,
                 depth: int = 12,
                 patch_size: int = 16,
                 num_classes: int = 1000,
                 **kwargs):
        super().__init__(
            PatchEmbedding(inplanes, patch_size, emb_size, img_size),
            TransformerEncoder(depth, emb_size=emb_size, num_heads=num_heads, **kwargs),
            ClassificationHead(emb_size, num_classes)
        )

class ViT_L(nn.Sequential):
    def __init__(self,
                 inplanes: int = 3,
                 emb_size: int = 1024,
                 num_heads: int = 16,
                 img_size: int = 224,
                 depth: int = 24,
                 patch_size: int = 16,
                 num_classes: int = 1000,
                 **kwargs):
        super().__init__(
            PatchEmbedding(inplanes, patch_size, emb_size, img_size),
            TransformerEncoder(depth, emb_size=emb_size, num_heads=num_heads, **kwargs),
            ClassificationHead(emb_size, num_classes)
        )


import logging
import sys
sys.path.append('.')

import tests.test_nodeEndpoints.add_work_items as awi

#logging utility
logging.getLogger("Nodes").setLevel(logging.WARNING)
logging.getLogger("endpoints").setLevel(logging.WARNING)
logging.basicConfig(format='%(name)-12s:%(lineno)-3s | %(levelname)-8s | %(message)s'
                , stream=sys.stdout, level=logging.DEBUG)
logger = logging.getLogger(__name__)

if __name__ == '__main__':
    logger.debug('--- sendingTasks.py ---')
    logger.debug('- connecting 1 executor (user) and sending 12 tasks')
    awi.SAMPLES = 1_000_000
    ip = sys.argv[1]
    port = int(sys.argv[2])
    awi.submit_helper(1, 30, ip=ip, port=port)
    logger.debug('- tasks sent to the head node')


from setuptools import setup, find_packages

with open('viz_server/version.py') as f:
    exec(f.read())

with open('requirements.txt') as f:
    install_requires = f.readlines()

setup(
    name='viz_server',
    version=VERSION,
    description='Visualizing data dependent workflows in Python',
    long_description='Visualizing parallel workflows system for Python',
    url='https://github.com/Parsl/viz_server',
    author='The Parsl Team',
    author_email='parsl@googlegroups.com',
    license='Apache 2.0',
    download_url='https://github.com/Parsl/parsl/archive/{}.tar.gz'.format(VERSION),
    include_package_data=True,
    packages=find_packages(),
    install_requires=install_requires,
    classifiers = [
        # Maturity
        'Development Status :: 3 - Alpha',
        # Intended audience
        'Intended Audience :: Developers',
        # Licence, must match with licence above
        'License :: OSI Approved :: Apache Software License',
        # Python versions supported
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
    ],
    keywords=['Visualization', 'Workflows', 'Scientific computing'],
    entry_points={'console_scripts':
      ['parsl-visualize=viz_server.viz_app:cli_run',
      ]}
)


import json

from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect, get_object_or_404
from django.template.defaulttags import url

from chat.forms import ContactForm
from chat.models import Message

def home(req):
    return render(req, "basemain.html")

def about(req):
    return render(req, "about.html")

def contact(req):
    if req.method == "POST":
        form = ContactForm(req.POST)
        if form.is_valid():
            form.save()
            return redirect("thanks")
    else:
        form = ContactForm()
    return render(req, "contact.html", context={'form': form})

def register(req):
    if req.method == "POST":
        form = UserCreationForm(req.POST)
        if form.is_valid():
            form.save()
            return redirect(req.GET.get("next") or url("home"))
    else:
        form = UserCreationForm()
    return render(req, "registration/register.html", context={"form": form})

# def profile(req, id):
#     prof = get_object_or_404(Profile, id=id)
#     return render(req, "profile.html", context={"form": prof})

def thanks(req):
    return render(req, "thanks.html")

# @login_required
# def profile_edit(req):
#     if req.method == "POST":
#         form = ProfileForm(req.POST)
#         if form.is_valid():
#             form.save()
#             return redirect("contact thanks")
#     else:
#         form = ProfileForm()
#     return render(req, "profile_edit.html", context={"form": form})

@login_required
def chat(req):
    return render(req, "chatindex.html")

@login_required
def api_log(req):
    log = Message.objects.filter(index=0, deleted=False)[:100]
    for item_no in range(log):
        if log[item_no].edited:
            log[item_no] = Message.objects.filter(msg_id=log[item_no].msg_id).order_by("-index")[0]
    return render(req, "api_log.html", context={"messages": log})

@login_required
def api_new(req):
    validation_errors = []

    if not req.POST.get("query"):
        validation_errors += ["No json processed"]

    jso = json.loads(req.POST.get("query"))

    if not type(jso["body"]) == "String":
        validation_errors += ["Incorrect message type"]

    if not type(jso["profile_id"]) == "Integer":
        validation_errors += ["Incorrect profile id format"]

    if validation_errors:
        return render(req, "errors.html", context={"validation_errors": validation_errors})

    msg_id = Message.objects.all().order_by("-msg_id")[0].msg_id + 1

    msg = Message(msg_id=msg_id, body=jso["body"], profile_id=jso["profile_id"])
    msg.save()

    return render(req, "jsonsuccess.html")

@login_required
def api_edit(req):
    validation_errors = []

    if not req.POST.get("query"):
        validation_errors += ["No json processed"]
        return render(req, "errors.html", context={"validation_errors": validation_errors})

    jso = json.loads(req.POST.get("query"))
    old_msg = Message.objects.filter(msg_id=req.POST.get("query")).order_by("-index")[0]

    if not old_msg.exists():
        validation_errors += ["No message to update with that id"]
        return render(req, "errors.html", context={"validation_errors": validation_errors})

    if jso["delete"] == True:
        old_msg.deleted = True
        old_msg.save()
        return render(req, "jsonsuccess.html")

    if not jso["body"]:
        validation_errors += ["No new message body"]
        return render(req, "errors.html", context={"validation_errors": validation_errors})

    msg = Message(index=old_msg.index+1, msg_id=old_msg.msg_id, body=jso["body"])
    msg.save()

    return render(req, "jsonsuccess.html")


"""
https://contest.yandex.ru/contest/22450/run-report/70514253/

Timothy is looking for a place to build a house for himself. The street he wants to live on has length n, that is,
it consists of n identical consecutive sections. Each plot is either empty or a house has already been built on it.

Sociable Timothy does not want to live far from other people on this street. Therefore, it is important for him to
know for each site the distance to the nearest empty site. If the plot is empty, this value will be equal to zero
- the distance to itself.

Help Timofey calculate the required distances. For this you have a street map. Houses in the city of Timothy were
numbered in the order in which they were built, so their numbers on the map are not ordered in any way.
Empty areas are marked with zeros.

Input example:
5
0 1 4 9 0
"""

from typing import List, Tuple

def closest_zero(n: int, sections: List[int]) -> List[int]:
    distances = []
    previous_zero_position = -1

    for index, section in enumerate(sections):

        if section != 0 and index + 1 == n:
            distances += list(range(1, n - previous_zero_position))
        elif section == 0 and index != 0 and previous_zero_position < 0:
            distances += reversed(list(range(1, index + 1)))
            distances.append(0)
            previous_zero_position = index
        elif section == 0 and index - 1 != previous_zero_position >= 0:
            segment = index - previous_zero_position - 1
            segment_module = segment % 2
            half_list = list(range(1, segment // 2 + 1 + segment_module))
            distances += half_list
            distances += half_list[len(half_list) - 1 - segment_module::-1] + [0] if segment > 1 else [0]
            previous_zero_position = index
        elif section == 0:
            distances.append(0)
            previous_zero_position = index

    return distances

def read_input() -> Tuple[int, List[int]]:
    n = int(input())
    arr = list(map(int, input().strip().split()))
    return n, arr

if __name__ == "__main__":
    n, sections = read_input()
    print(' '.join(map(str, closest_zero(n, sections))))


from typing import List, Tuple
from math import ceil

class OutOfOre(Exception):
	pass

class Reaction:
	def __init__(self, amt: int, reagents: List[Tuple["Chemical", int]]) -> None:
		self.total_reactions = 0
		self.amt_created = amt
		self.reagents = reagents

	def react(self, to_create: int) -> int:
		num_reactions = ceil(to_create / self.amt_created)
		for c, q in self.reagents:
			c.request(q * num_reactions)
		self.total_reactions += num_reactions
		return self.amt_created * num_reactions

class Chemical:
	def __init__(self, name: str) -> None:
		self.name = name
		self.unused = 0
		self.reaction = None
		self.total_requested = 0

	def set_reaction(self, reaction: Reaction) -> None:
		self.reaction = reaction

	def request(self, amount: int) -> None:
		if self.unused < amount:
			self.unused += self.reaction.react(amount - self.unused)
		self.total_requested += amount
		self.unused -= amount

class Ore(Chemical):
	START_AMOUNT: int = 1000000000000
	unused: int = 0
	name: str = "ORE"

	def __init__(self) -> None:
		self.total_requested = 0

	def request(self, amount: int) -> None:
		if self.total_requested + amount > Ore.START_AMOUNT:
			raise OutOfOre()
		self.total_requested += amount

def run(input_data: List[str], **kwargs) -> int:
	ore = Ore()
	chemicals_by_name = {ore.name: ore}
	for datum in input_data:
		reagents_string, chemical_str = datum.split(" => ")
		reaction_reagents = [
			(
				chemicals_by_name.setdefault(
					ingredient.split(" ")[1], Chemical(ingredient.split(" ")[1])
				),
				int(ingredient.split(" ")[0])
			)
			for ingredient in reagents_string.split(", ")
		]
		amt, chem = chemical_str.split(" ")
		chemicals_by_name.setdefault(chem, Chemical(chem)).set_reaction(Reaction(int(amt), reaction_reagents))

	fuel = chemicals_by_name.get("FUEL")
	fuel.request(1)
	max_ore_per_fuel = ore.total_requested

	while True:
		fuel_to_request = int((ore.START_AMOUNT - ore.total_requested) / max_ore_per_fuel) or 1
		try: fuel.request(fuel_to_request)
		except OutOfOre: return fuel.total_requested