import os
import re
import string
import pandas as pd
from collections import deque

def load_data(data_dir='data', languages=None, dev_test_split=0.5, random_seed=1337):
    """
    Loads the data, splitting the test file into a dev and test set using stratified sampling of the languages.

    Parameters
    ----------
    data_dir : str
        Directory of the data to load
    languages : {None, list, int, float}, optional
        None : Default, load all languages from the data
        list : List of languages e.g ['English', 'German', ...] to load from the data
        int : Randomly sample 'int' number of languages for the train/dev/test sets
        float: Value between 0 and 1, the percentage of languages for the train/dev/test sets
    dev_test_split: float, optional
        Value between 0 and 1, setting the dev and test split ratio.
    random_seed: int
        Random seed to set the random state, making the sampling reproducible.

    Returns
    -------
    (DataFrame, DataFrame, DataFrame)
        train_df, dev_df, test_df
    """

    labels = pd.read_csv(os.path.join(data_dir, 'labels.csv'), sep=';', keep_default_na=False)
    label_to_lang = dict(zip(labels['Label'], labels['English']))
    with open(os.path.join(data_dir, 'x_train.txt')) as f:
        x_train = [s.strip() for s in f.readlines()]
    with open(os.path.join(data_dir, 'x_test.txt')) as f:
        x_test = [s.strip() for s in f.readlines()]
    with open(os.path.join(data_dir, 'y_train.txt')) as f:
        y_train = [s.strip() for s in f.readlines()]
    with open(os.path.join(data_dir, 'y_test.txt')) as f:
        y_test = [s.strip() for s in f.readlines()]

    train_df = pd.DataFrame(zip(x_train, y_train), columns=['X', 'y'])
    train_df['y'] = train_df['y'].map(label_to_lang)

    test_df = pd.DataFrame(zip(x_test, y_test), columns=['X', 'y'])
    test_df['y'] = test_df['y'].map(label_to_lang)

    if languages is not None:
        lang_list = languages

        if isinstance(languages, int):
            lang_list = labels['English'].sample(n=languages, random_state=random_seed).to_list()

        if isinstance(languages, float):
            lang_list = labels['English'].sample(frac=languages, random_state=random_seed).to_list()

        train_df = train_df.query('y in @lang_list')
        test_df = test_df.query('y in @lang_list')

    train_df = train_df[train_df['X'].apply(filter_)]
    test_df = test_df[test_df['X'].apply(filter_)]

    dev_df = test_df.groupby('y').sample(frac=dev_test_split, random_state=random_seed)
    test_df = test_df[~test_df.index.isin(dev_df.index)]

    return train_df, dev_df, test_df

def filter_(doc):
    """
    Returns true if doc is not an empty string after removing punctuation, digits and whitespaces

    Parameters
    ----------
    doc : str
        Document to check for emptiness.

    Returns
    -------
    bool
        True if the document is empty after preprocessing
    """
    prep_doc = preprocess(doc)
    prep_doc = re.sub('\s+', '', prep_doc)

    return len(prep_doc) > 0

def preprocess(doc):
    """
    Preprocess 'doc' i.e lowercase text, remove punctuation and digits.

    Parameters
    ----------
    doc : str
        Document to preprocess

    Returns
    -------
    str
        Preprocessed document

    """
    prep_doc = doc.lower()
    prep_doc = re.sub('[' + string.punctuation + ']', '', prep_doc)
    prep_doc = re.sub('[' + string.digits + ']', '', prep_doc)

    return prep_doc

def prep_word(doc):
    """
    Preprocess and split words on whitespaces, for word-based models

    Parameters
    ----------
    doc : str
        Document to process

    Returns
    -------
    list
        List of words in the document, preprocessed
    """
    prep_doc = preprocess(doc)
    prep_doc = re.sub('\s+', ' ', prep_doc)
    words = prep_doc.split()

    return words

def prep_ngram(doc, n):
    """
    Preprocess document, create list of character n-grams

    Parameters
    ----------
    doc : str
        Document to process
    n : int
        Number of characters for the n-grams

    Returns
    -------
    deque :
        List-like sequence of character n-grams
    """
    prep_doc = preprocess(doc)
    words = re.sub('\s+', '_', prep_doc)
    words = "_" + words + "_"
    ngrams = deque()
    for i in range(len(words)-n):
        ngrams.append(words[i:i+n])

    return ngrams

def prep_ngram_range(doc, min_n, max_n):
    """
    Preprocess document, create list of character n-grams ranging from min_n to max_n characters

    Parameters
    ----------
    doc : str
        Document to process
    min_n : int
        Minimum number of characters to include
    max_n : int
        Maximum number of characters to include

    Returns
    -------
    deque
        List-like sequence of character n-grams
    """
    prep_doc = preprocess(doc)
    words = re.sub('\s+', '_', prep_doc)
    words = "_" + words + "_"
    ngrams = deque()
    for k in range(min_n, max_n+1):
        for i in range(len(words)-k):
            ngrams.append(words[i:i+k])

    return ngrams


# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import logging.handlers as lh

def trim_hash(in_hash, hash_keys):
    """Filter hash/remove unwanted keys

    Return a new hash only with the keys that were specified in hash_keys param,
    remove the others.

    Args:
        in_hash: input hash
        hash_keys: keys that should stay

    Returns:
        Filtered hash
    """
    if hash_keys:
        return {k: in_hash[k] for k in hash_keys}
    else:
        return in_hash

def init_logging(verbosity=0, std_err=False, syslog_facility="LOG_USER"):
    """Init logging subsytem

    Sets up logging accordig to the call arguments.

    Args:
        verbosity: how verbose should the script be:
                   0 - silent most of the time
                   1 - moderate logging
                   2 - print everything
        std_err: log the data to STDERR as well (apart from syslog)
        syslog_facility: syslog facility to send data to
    """
    logger = logging.getLogger()

    if logger.handlers:
        for handler in logger.handlers:
            logger.removeHandler(handler)

    if verbosity == 0:
        # Print/log only important stuff:
        logger.setLevel(logging.WARN)
        fmt = logging.Formatter('%(message)s')
    elif verbosity == 1:
        # Print most of the stuff:
        logger.setLevel(logging.INFO)
        fmt = logging.Formatter('%(levelname)s: %(message)s')
    else:
        # Here be dragons:
        logger.setLevel(logging.DEBUG)
        fmt = logging.Formatter('%(filename)s[%(process)d] ' +
                                '%(levelname)s: %(message)s')

    syslog_h = lh.SysLogHandler(address='/dev/log',
                                facility=getattr(lh.SysLogHandler,
                                                 syslog_facility))
    syslog_h.setFormatter(fmt)
    logger.addHandler(syslog_h)

    if std_err:
        stdout_h = logging.StreamHandler()
        stdout_h.setFormatter(fmt)
        logger.addHandler(stdout_h)


#parallel calculation of statistics

import numpy as np
import sys
sys.path.append("/home/moehlc/idaf_library")
import libidaf.idafIO as io
import multiprocessing as mp
import os

def initMemMap(savepath,name,shape):
	try:
		os.makedirs(savepath)
	except:
		print(savepath+' already exists')

	savename = name + '.mmap'
	print('initialize new memmap ' + savename)

	mmap = np.memmap(savepath + savename,dtype = 'float64', mode = 'w+',\
 	shape = shape)
	return mmap

def loadMemMapSlice(path,name,shape,sliceNr):
	return np.array(np.memmap(path + name,dtype = 'float64', mode = 'r',\
 	shape = shape)[:,:,sliceNr])

def loadMemMapSlices(path,names,shape,sliceNr):
	print('importing slices nr. ' + str(sliceNr))
	nFiles = len(names) #nr of filenames
	vol = np.zeros((shape[0],shape[1],nFiles))

	for i in range(nFiles):
		vol[:,:,i] = loadMemMapSlice(path,names[i],shape,sliceNr)
	return vol

def calcNnSliceAndExport(inslices,sliceNr,outmap):
	#return number of values over z that are not nan
	outmap[:,:,sliceNr] = (~np.isnan(inslices)).sum(axis = 2)
	outmap.flush()

def calcMeanSliceAndExport(inslices,sliceNr,outmap):
	#return number of values over z that are not nan
	outmap[:,:,sliceNr] = np.nanmean(inslices,axis = 2)
	outmap.flush()

def calcStdSliceAndExport(inslices,sliceNr,outmap):
	#return number of values over z that are not nan
	outmap[:,:,sliceNr] = np.nanstd(inslices,axis = 2)
	outmap.flush()

def calcPercentileSliceAndExport(inslices,sliceNr,outmap,percentile):
	for i in range(inslices.shape[0]):
		for j in range(inslices.shape[1]):
			x = inslices[i,j,:].flatten()
			x = x[~np.isnan(x)] # remove nan
			if len(x) > 1:
				outmap[i,j,sliceNr] = np.percentile(x,percentile)
			else:
				outmap[i,j,sliceNr] = np.nan
	outmap.flush()

def calcStatisticsForSlice(sliceNr,path,fnames,shape,m):
	#load slices
	sl = loadMemMapSlices(path,fnames,shape,sliceNr)
	print('calculate mean nn and std of slice Nr. ' + str(sliceNr))
	calcNnSliceAndExport(sl,sliceNr,m['vol_nn'])
	calcMeanSliceAndExport(sl,sliceNr,m['vol_mean'])
	calcStdSliceAndExport(sl,sliceNr,m['vol_std'])
	print('calculate median of slice Nr. ' + str(sliceNr))
	calcPercentileSliceAndExport(sl,sliceNr,m['vol_median'],50)
	print('calculate percentiles of slice Nr. ' + str(sliceNr))
	calcPercentileSliceAndExport(sl,sliceNr,m['vol_percentile05'],5)
	calcPercentileSliceAndExport(sl,sliceNr,m['vol_percentile25'],25)
	calcPercentileSliceAndExport(sl,sliceNr,m['vol_percentile75'],75)
	calcPercentileSliceAndExport(sl,sliceNr,m['vol_percentile95'],95)

def calcStatisticsForPopulation(path,pattern,savepath,shape,namePrefix):
	fnames = io.getFilelistFromDir(path,pattern) #list of pickles to process

	memmaps = {}
	#initialization of memmaps
	memmaps['vol_nn'] = initMemMap(savepath,namePrefix + 'nn',shape)
	memmaps['vol_mean'] = initMemMap(savepath,namePrefix + 'mean',shape)
	memmaps['vol_std'] = initMemMap(savepath,namePrefix + 'std',shape)
	memmaps['vol_median'] = initMemMap(savepath,namePrefix + 'median',shape)
	memmaps['vol_percentile05'] = initMemMap(savepath,namePrefix + 'percentile05',shape)
	memmaps['vol_percentile25'] = initMemMap(savepath,namePrefix + 'percentile25',shape)
	memmaps['vol_percentile75'] = initMemMap(savepath,namePrefix + 'percentile75',shape)
	memmaps['vol_percentile95'] = initMemMap(savepath,namePrefix + 'percentile95',shape)

	#process slice by slice
	for sliceNr in range(shape[2]):
		#calcStatisticsForSlice(sliceNr,path,fnames,shape,memmaps)
		mp.Process(target = calcStatisticsForSlice, args =(sliceNr,path,fnames,shape,memmaps)).start()

if __name__ == '__main__':

	path_ad = '/facilities/idaf/IDAF_Projects/140327_raman_bloodvessel_mri/data/filteredMemMaps/angio_ad/'
	path_wt = '/facilities/idaf/IDAF_Projects/140327_raman_bloodvessel_mri/data/filteredMemMaps/angio_wt/'

	savepath_ad = '/facilities/idaf/IDAF_Projects/140327_raman_bloodvessel_mri/data/statisticsMemMaps/angio_ad/'
	savepath_wt = '/facilities/idaf/IDAF_Projects/140327_raman_bloodvessel_mri/data/statisticsMemMaps/angio_wt/'

	patternDist = 'filtered_Size_20distanceSkel' # gaussfiltered distance images
	patternFlow = 'filtered_Size_20flowSkel'

	shape = (320,320,272)

	#calcStatisticsForPopulation(path_ad,patternDist,savepath_ad,shape,'dist_')
	calcStatisticsForPopulation(path_wt,patternDist,savepath_wt,shape,'dist_')

	#calcStatisticsForPopulation(path_ad,patternFlow,savepath_ad,shape,'flow_')
	#calcStatisticsForPopulation(path_wt,patternFlow,savepath_wt,shape,'flow_')


#!/usr/bin/env python
'''
SQL Introducción [Python]
Ejemplos de clase
---------------------------
Autor: Inove Coding School
Version: 1.1

Descripcion:
Programa creado para mostrar ejemplos prácticos de los visto durante la clase
'''

__author__ = "Inove Coding School"
__email__ = "alumnos@inove.com.ar"
__version__ = "1.1"

import os
import csv
import sqlite3

import numpy as np

# https://extendsclass.com/sqlite-browser.html

def create_schema():
    conn = sqlite3.connect('heart.db')
    c = conn.cursor()

    # Ejecutar una query
    c.execute("""
                DROP TABLE IF EXISTS sensor;
            """)

    # Ejecutar una query
    c.execute("""
            CREATE TABLE sensor(
                [id] INTEGER PRIMARY KEY,
                [pulso] INTEGER NOT NULL
            );
            """)

    conn.commit()
    conn.close()

def show_estadistica():
    # establece conexion a Data Base
    conn = sqlite3.connect('heart.db')
    c = conn.cursor()

    # Recupera
    pulsos = c.execute("SELECT pulso FROM sensor").fetchall()

    pulsos = np.asanyarray(pulsos)
    if (len(pulsos) > 0):
        print('Pulso Medio ',pulsos.mean())
        print('Pulso Maximo ',pulsos.max())
        print('Pulso Minimo ',pulsos.min())
        print('Pulso Standard ',pulsos.std())

def carga_tabla_desde_csv():
    # Establece conexion a Data Base
     conn = sqlite3.connect('heart.db')
     c = conn.cursor()

    # Define CSV
     path = os.path.dirname(__file__)
     sensor_path = os.path.join(path, 'sensor.csv')

    # Lee CSV
     with open(sensor_path) as fi:
         data = csv.DictReader(fi)

         for row in data:
             pulso = int(row['pulso'])
             if pulso <= 50:
                 continue

            # Carga tabla sensor
             c.execute("INSERT INTO sensor (pulso) VALUES (?);", [pulso])

    # Cierra Data Base
     conn.commit()
     conn.close()

if __name__ == '__main__':
    print("Bienvenidos a otra clase de Inove con Python - Data Base - CSV")

    # Crea Base de datos
    create_schema()
    # Carga datos en tabla desde CSV
    carga_tabla_desde_csv()
    # Muestra estadistica
    show_estadistica()

    print("Fin programa")


import backtrader as bt
import message as message
import datetime

'''双均线'''
class SmaCross(bt.Strategy):
    # 定义参数
    params = dict(
        fast_period=10,  # 快速移动平均期数
        slow_period = 60,)  # 慢速移动平均期数

    def __init__(self):
        # 定义快速移动平均线指标
        self.fastMA = bt.indicators.SMA(period=self.params.fast_period)

        # 定义慢速移动平均线指标
        self.slowMA = bt.indicators.SMA(period=self.params.slow_period)

        # 定义移动均线交叉信号指标
        self.crossover = bt.ind.CrossOver(self.fastMA, self.slowMA)

        self.order = None    # 设置订单引用，用于取消以往发出的尚未执行的订单

        self.message_able = False
        self.message = "";

    def next(self): # 每个新bar结束时触发调用一次，相当于其他框架的 on_bar()方法

        self.cancel(self.order) # 取消以往未执行订单

        # if not self.position:  # 还没有仓位，才可以买
        #     if self.crossover > 0:  # 金叉
        #         # self.order = self.buy(size=100) # 创建市价买单，该单会在次日开盘以开盘价成交
        #         self.order = self.order_target_percent(target=0.8)
        #
        #
        # # 已有仓位，才可以卖
        # elif self.crossover < 0:  # 死叉
        #     # self.order = self.sell() # 创建市价卖单，该单会在次日开盘以开盘价成交
        #     self.order = self.order_target_value(target=0)

        # print(self.data0.datetime.date(0))
        # print(self.fastMA.get(), self.slowMA.get())

        # current_day
        if self.crossover < 0 and self.position:  # 死叉且有仓位
            self.order = self.order_target_value(target=0)
            self.message = "全部卖出"
            self.message_process()

        # 金叉且没有仓位
        if self.crossover > 0 and not self.position:
            self.order = self.order_target_percent(target=0.8)
            self.message = "全仓买入"
            self.message_process()

    def message_process(self):
        trade_day = self.data0.datetime.date(0)
        cur_date = datetime.datetime.now().date()
        # cur_date = datetime.datetime(2021, 11, 16).date()
        pre_date = cur_date + datetime.timedelta(days=-1)

        cur_time = datetime.datetime.now().time()
        time_16 = datetime.time(16, 0)
        time_9 = datetime.time(9, 30)

        compare_date = cur_date
        if cur_time > time_16 :
            compare_date = cur_date
        if cur_time < time_9:
            compare_date = pre_date

        # 提醒信息
        if trade_day == compare_date:
            print(cur_date, self.message)
            if self.message_able:
                message.send_message(self.message)


from collections import defaultdict
row, col = map(int,input().split())

matrix = []

for r in range(row):
    x = input()

    matrix.append(x)
rows =[]
for r in range(row):
    compare = defaultdict(int)
    for c in range(col):
        if compare[matrix[r][c]]>0:
            rows.append((r,matrix[r][c]))
        else:
            compare[matrix[r][c]] +=1
column = []
for c in range(col):
    compare = defaultdict(int)
    for r in range(row):
        if compare[matrix[r][c]]>0:
            column.append((c,matrix[r][c]))
        else:
            compare[matrix[r][c]] +=1

for r , val in rows:
    matrix[r]=matrix[r].replace(val,"0")

for r in range(row):
    for c in range(col):
        if (c , matrix[r][c]) in column:
            matrix[r]=matrix[r].replace(matrix[r][c],"0",1)

ans = ""

for r in range(row):
    for c in range(col):
        if matrix[r][c]!= "0":
            ans += matrix[r][c]
print(ans)


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

# file: plm.py
# notice: based on https://github.com/huggingface/transformers/blob/v3.4.0/src/transformers/modeling_roberta.py
#         and https://github.com/huggingface/transformers/blob/v3.4.0/src/transformers/modeling_bert.py
# may need to change when using transformers!=3.4.0

import torch.nn as nn
from torch import Tensor
from transformers import BertModel, BertPreTrainedModel
from transformers.modeling_roberta import RobertaModel, RobertaPreTrainedModel

from models.classifier import truncated_normal_
from models.model_config import BertForSequenceClassificationConfig, RobertaForSequenceClassificationConfig

class BertForSequenceClassification(BertPreTrainedModel):
    """Fine-tune BERT model for text classification."""
    def __init__(self, config: BertForSequenceClassificationConfig,):
        super(BertForSequenceClassification, self).__init__(config)
        self.bert_config = config
        self.bert = BertModel(config,)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.cls_classifier = nn.Linear(config.hidden_size, config.num_labels)
        self.cls_classifier.weight = truncated_normal_(self.cls_classifier.weight, mean=0, std=0.02)
        self.init_weights()

    def forward(self, input_ids: Tensor, token_type_ids: Tensor, attention_mask: Tensor, output_hidden_states=None, return_dict=None):
        """
        Args:
            inputs_ids: input tokens, tensor of shape [batch_size, seq_len].
            token_type_ids: 1 for text_b tokens and 0 for text_a tokens. tensor of shape [batch_size, seq_len].
            attention_mask: 1 for non-[PAD] tokens and 0 for [PAD] tokens. tensor of shape [batch_size, seq_len].
            output_hidden_states: (default None)
            return_dict: (default None)
        Returns:
            cls_outputs: output logits for the [CLS] token. tensor of shape [batch_size, num_labels].
        """
        bert_outputs = self.bert(input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask,
                                 output_hidden_states=output_hidden_states, return_dict=return_dict)
        if not return_dict:
            bert_cls_output = bert_outputs[1]
        else:
            bert_cls_output = bert_outputs.pooler_output
        # bert_cls_output is in the shape of (batch_size, number_of_classes)
        bert_cls_output = self.dropout(bert_cls_output)
        cls_logits = self.cls_classifier(bert_cls_output)

        if not output_hidden_states:
            return cls_logits
        else:
            all_layer_hidden_states = bert_outputs.hidden_states
            # bert_embedding + bert_encoder_layers
            # <number of layers> and every tensor is in the shape of (batch_size, input_seq_len, model_hidden_size)
            return cls_logits, all_layer_hidden_states

class RobertaForSequenceClassification(RobertaPreTrainedModel):
    """Fine-tune RoBERTa model for text classification."""
    def __init__(self, config: RobertaForSequenceClassificationConfig,):
        super(RobertaForSequenceClassification, self).__init__(config)
        self.roberta_config = config
        self.roberta = RobertaModel(config,)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.cls_classifier = nn.Linear(config.hidden_size, config.num_labels)
        self.cls_classifier.weight = truncated_normal_(self.cls_classifier.weight, mean=0, std=0.02)
        self.init_weights()

    def forward(self, input_ids: Tensor, token_type_ids: Tensor, attention_mask: Tensor, output_hidden_states=None, return_dict=None):
        """
        Args:
            inputs_ids: input tokens, tensor of shape [batch_size, seq_len].
            token_type_ids: 1 for text_b tokens and 0 for text_a tokens. tensor of shape [batch_size, seq_len].
            attention_mask: 1 for non-[PAD] tokens and 0 for [PAD] tokens. tensor of shape [batch_size, seq_len].
        Returns:
            cls_outputs: output logits for the [CLS] token. tensor of shape [batch_size, num_labels].
        """
        roberta_outputs = self.roberta(input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask,
                                       output_hidden_states=output_hidden_states, return_dict=return_dict,)

        if not return_dict:
            roberta_cls_output = roberta_outputs[1]
        else:
            roberta_cls_output = roberta_outputs.pooler_output
        # roberta_cls_output is in the shape of (batch_size, number_of_classes)

        roberta_cls_output = self.dropout(roberta_cls_output)
        cls_logits = self.cls_classifier(roberta_cls_output)

        if not output_hidden_states:
            return cls_logits
        else:
            all_layer_hidden_states = roberta_outputs.hidden_states
            # roberta_embedding + roberta_encoder_layers
            # <number of layers> and every tensor is in the shape of (batch_size, input_seq_len, model_hidden_size)
            return cls_logits, all_layer_hidden_states


#! /usr/bin/env python3
from datetime import datetime, timedelta
from urllib.request import urlopen, urlretrieve
from numpy import array, radians
from common import is_number
from astropy.io import fits
import sys, os, argparse, re

# adapted from nirc2_reduce/get_ephem.py
def naif_lookup(target):
  '''looks up NAIF ID for target in list at naif_id_table.txt'''
  target = target.upper().strip(', \n')
  with open('../naif_id_table.txt','r') as f:
    for line in f:
      l = line.split(',')
      if is_number(target):
        #print(l[0].strip(', \'\n') == target)
        if l[0].strip(', \'\n') == target:
          code = target
      else:
        if l[1].strip(', \'\n') == target:
          code = l[0].strip(', \n')
    try:
      code
    except:
      msg = 'WARN (naif_lookup): NAIF code not in lookup table.'
      msg += 'If code fails, ensure target can be queried in Horizons.'
      print(msg)
      code = target

  if len(code) == 7: #minor body
    if code[0] == '2': #asteroid
      return code[1:]+';'
    elif code[0] =='1': #comet
      msg = 'ERROR (naif_lookup): Comets cannot be looked up by NAIF ID; '
      msg += 'Horizons generates multiple codes for every comet. '
      msg += 'Try your search in the Horizons Web tool, '
      msg += 'select the target body you want, '
      msg += 'and then copy the exact string into this code and it *may* work.'
      sys.exit(msg)
    return code
  return code

# adapted from nirc2_reduce/get_ephem.py
def get_ephemerides(code, obs_code, tstart, tend, stepsize, quantities = None) :
  """
  input NAIF target code, e.g. 501 for Io, and date in the format:
  'YYYY-MM-DD HH:MM'
  For example: data=get_ephem.get_ephemerides('501','2017-06-09 08:24')
  Returns a list containing (in string format):
  UTdate and time,sun,moon,RA (J2000),DEC (J2000),dra,ddec,azimuth,elevation,
  Airmass,Extinction,APmag,s-brt,Ang-Diam("),ang-sep("),visibility,Ob-lon,Ob-lat,NP.ang,NP.dist
  """
  if quantities == None:
    quantities = '1,3,4,8,9,12,13,14,15,17,19,20'

  tstart_obj = datetime.strptime(tstart,'%Y-%m-%d %H:%M')
  tend_obj = datetime.strptime(tend,'%Y-%m-%d %H:%M')
  if tend_obj - tstart_obj <= timedelta(minutes = 1):
    print('End time before start time. Setting end time to one minute after start time.')
    tend_obj = tstart_obj + timedelta(minutes = 1)

  tstart_UT = datetime.strftime(tstart_obj,"'%Y-%m-%d %H:%M'")
  tend_UT = datetime.strftime(tend_obj,"'%Y-%m-%d %H:%M'")

  http = "https://ssd.jpl.nasa.gov/horizons_batch.cgi?batch=1"
  make_ephem = "&MAKE_EPHEM='YES'&TABLE_TYPE='OBSERVER'"
  command    = "&COMMAND=" + str(code)
  center     = "&CENTER="+str(obs_code)  #568 is Mauna Kea, 662 is Lick, etc
  t_start    = "&START_TIME=" + tstart_UT
  t_stop     = "&STOP_TIME=" + tend_UT
  t_step     = "&STEP_SIZE='" + stepsize + "'"
  quantities = "&QUANTITIES='%s'"%quantities
  csv        = "&CSV_FORMAT='YES'"

  url = http+make_ephem+command+center+t_start+t_stop+t_step+quantities+csv
  url = url.replace(" ", "%20")
  url = url.replace("'", "%27")
  try:
    with urlopen(url) as response:
      ephem = response.readlines()
  except:
    msg = 'ERROR (get_ephem): Could not retrieve query from Horizons. '
    msg += 'Check Internet connection and input URL'
    sys.exit(msg)

  inephem = False
  data = []
  for i in range(len(ephem)) :
    if type(ephem[i]) != str:
      ephem[i] = ephem[i].decode('UTF-8')
    if inephem == False:
      #get observatory lat, lon, alt for later
      if ephem[i].startswith('Center geodetic :'):
        l = ephem[i].split(':')[1]
        latlonalt = l.split()[0]
        [lon,lat,alt] = [float(s.strip(', \n')) for s in latlonalt.split(',')]
        observatory_coords = [lat,lon,alt]
      if ephem[i].startswith('$$SOE') :
        inephem=True
        #data = [ephem[i+1].decode('UTF-8').split(',')]
    elif inephem == True:
      if ephem[i].startswith("$$EOE") :
        inephem=False
      else:
        data.append(ephem[i].split(','))
  if len(data) == 0:
    msg = 'ERROR (get_ephem): Ephemeris data not found. '
    msg += 'Check that the target has valid ephemeris data for the specified time range.'
    raise ValueError(msg)
  else:
    out = array(data)[:,:-1]
  return out, observatory_coords

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument('-d', '--dir',
    default = '.',
    help = 'directory of the fits file'
    )
  parser.add_argument('-f', '--fits',
    default = 'tbd',
    help = 'base name of the fits file without extension'
    )
  parser.add_argument('-b', '--obs',
    default = 'tbd',
    help = 'observation facility'
    )
  parser.add_argument('-s', '--shift',
    default = '0',
    help = 'shift oblat by'
    )
  args = vars(parser.parse_args())

  print('Running write_ephemerides ...')

  hdu = fits.open('%s/%s.fits' % (args['dir'], args['fits']))[0]
  date = hdu.header['DATE-OBS']
  planet = hdu.header['OBJECT']
  del1 = hdu.header['CDELT1']
  del2 = hdu.header['CDELT2']

  naif = naif_lookup(planet)

  if args['obs'] == 'vla':
    obscode = -5
  elif args['obs'] == 'alma':
    obscode = -7
  elif args['obs'] == 'keck':
    obscode = 568
  elif args['obs'] == 'lick':
    obscode = 662
  else:
    raise ValueError('Observation Code Not Found.')
  tstart = datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%f')
  tend = tstart + timedelta(minutes = 60)

  tstart = datetime.strftime(tstart, '%Y-%m-%d %H:%M')
  tend = datetime.strftime(tend, '%Y-%m-%d %H:%M')
  ephem = get_ephemerides(naif, obscode, tstart, tend, '1 minutes')[0][0]
  ephem = [val.strip(' ') for val in ephem]
  #print(ephem)

  time = ephem[0]
  ra, dec = ephem[3], ephem[4]
  dra, ddec = float(ephem[5]), float(ephem[6])
  ob_lon, ob_lat = float(ephem[16]), float(ephem[17])
  print(ob_lat)
  ob_lat += float(args['shift'])
  print(ob_lat)
  np_ang = float(ephem[20])
  dist = float(ephem[24])*1.496e8 # au -> km
  pix1 = dist*abs(radians(del1))
  pix2 = dist*abs(radians(del2))

  os.system('mkdir -p ../dat')
  with open('../dat/%s.info' % args['fits'], 'w') as file:
    file.write('# EPHEMERIDES\n')
    file.write('FITS_FILE         = %s/%s.fits\n' % (args['dir'],args['fits']))
    file.write('PLANET            = %s\n' % planet)
    file.write('NAIF_ID_CODE      = %s\n' % naif)
    file.write('FACILITY          = %s\n' % args['obs'])
    file.write('FACILITY_CODE     = %d\n' % obscode)
    file.write('TIME_START        = %s\n' % tstart.replace(' ', ','))
    file.write('TIME_END          = %s\n' % tend.replace(' ', ','))
    file.write('RA                = %s\n' % ra.replace(' ', ','))
    file.write('DEC               = %s\n' % dec.replace(' ' , ','))
    file.write('DRA               = %s\n' % dra)
    file.write('DDEC              = %s\n' % ddec)
    file.write('OBLON (W)         = %s\n' % ob_lon)
    file.write('OBLAT (GRAPHIC)   = %s\n' % ob_lat)
    file.write('NPANG (DEG)       = %s\n' % np_ang)
    file.write('DIST (KM)         = %s\n' % dist)
    file.write('DEL1 (DEG)        = %s\n' % del1)
    file.write('DEL2 (DEG)        = %s\n' % del2)
    file.write('PIXSCALE1 (KM)    = %s\n' % pix1)
    file.write('PIXSCALE2 (KM)    = %s\n' % pix2)


import jinja2
import os
import webapp2
import logging
import time
from google.appengine.api import memcache
from google.appengine.ext import db

jinja_environment = jinja2.Environment(
    loader = jinja2.FileSystemLoader(os.path.dirname(__file__)),
    autoescape = True)

class Blog(db.Model):
    subject = db.StringProperty(required = True)
    content = db.TextProperty(required = True)
    created = db.DateTimeProperty(auto_now_add = True)

ALL_BLOGS_CACHE_KEY = 'all_blogs'
LAST_QUERIED_CACHE_KEY = 'last_queried'

class ProblemSet3(webapp2.RequestHandler):
    template = jinja_environment.get_template('ProblemSet3.html')
    def get(self):
        blogs = memcache.get(ALL_BLOGS_CACHE_KEY)
        last_queried = memcache.get(LAST_QUERIED_CACHE_KEY)
        if blogs is None:
            logging.error('DB QUERY')
            blogs = db.GqlQuery('SELECT * FROM Blog ORDER BY created DESC')
            blogs = list(blogs)
            memcache.set(ALL_BLOGS_CACHE_KEY, blogs)
            last_queried = time.time()
            memcache.set(LAST_QUERIED_CACHE_KEY, last_queried)
        template_values = {
            'blogs': blogs,
            'time_past': str(int(time.time() - float(last_queried)))
            }
        self.response.out.write(self.template.render(template_values))

class ProblemSet3_NewPost(webapp2.RequestHandler):
    template = jinja_environment.get_template('ProblemSet3_NewPost.html')
    def get(self):
        self.response.out.write(self.template.render())
    def post(self):
        blog_subject = self.request.get('subject')
        blog_content = self.request.get('content')
        if blog_subject and blog_content:
            blog = Blog(subject = blog_subject, content = blog_content)
            blog.put()
            blog_id = str(blog.key().id())
            memcache.set(ALL_BLOGS_CACHE_KEY, None)
            self.redirect('/ps3/' + blog_id)
        else:
            template_values = {
                'subject': blog_subject,
                'content': blog_content,
                'error': 'Both subject and content are required.',
                }
            self.response.out.write(self.template.render(template_values))

class ProblemSet3_Post(webapp2.RequestHandler):
    template = jinja_environment.get_template('ProblemSet3_Post.html')
    def get(self, blog_id):
        blog_cache_key = 'permalink_' + blog_id
        last_queried_cache_key = 'last_queried_' + blog_id
        blog = memcache.get(blog_cache_key)
        if blog is None:
            blog = Blog.get_by_id(int(blog_id))
            memcache.set(blog_cache_key, blog)
            last_queried = time.time()
            memcache.set(last_queried_cache_key, last_queried)
        else:
            last_queried = memcache.get(last_queried_cache_key)
        template_values = {
            'subject': blog.subject,
            'created': blog.created,
            'content': blog.content,
            'time_past': str(int(time.time() - float(last_queried)))
            }
        self.response.out.write(self.template.render(template_values))


from typing import Dict, List, TextIO, Optional, Set, Tuple

from overrides import overrides
import torch
from torch.nn.modules import Linear, Dropout
from torch.autograd import Variable
import torch.nn.functional as F

from allennlp.common import Params
from allennlp.common.checks import ConfigurationError
from allennlp.data import Vocabulary
from allennlp.modules import Seq2SeqEncoder, TimeDistributed, TextFieldEmbedder
from allennlp.modules.token_embedders import Embedding
from allennlp.models.model import Model
from allennlp.nn import InitializerApplicator, RegularizerApplicator
from allennlp.nn.util import get_text_field_mask, sequence_cross_entropy_with_logits
from allennlp.nn.util import get_lengths_from_binary_sequence_mask, viterbi_decode
from allennlp.nn.util import batched_index_select
from allennlp.training.metrics import SpanBasedF1Measure

from qasrl.modules.sentence_encoder import SentenceEncoder
from qasrl.modules.slot_sequence_generator import SlotSequenceGenerator
from qasrl.metrics.question_metric import QuestionMetric

@Model.register("qasrl_question")
class QuestionModel(Model):
    def __init__(self, vocab: Vocabulary,
                 sentence_encoder: SentenceEncoder,
                 question_generator: SlotSequenceGenerator,
                 initializer: InitializerApplicator = InitializerApplicator(),
                 regularizer: Optional[RegularizerApplicator] = None):
        super(QuestionModel, self).__init__(vocab, regularizer)
        self._sentence_encoder = sentence_encoder
        self._question_generator = question_generator
        if self._sentence_encoder.get_output_dim() != self._question_generator.get_input_dim():
            raise ConfigurationError(
                ("Input dimension of question generator (%s) must be " % self._question_generator.get_input_dim()) + \
                ("equal to the output dimension of the sentence encoder (%s)." % self._sentence_encoder.get_output_dim()))
        self.metric = QuestionMetric(vocab, self._question_generator.get_slot_names())

    def get_slot_names(self):
        return self._question_generator.get_slot_names()

    @overrides
    def forward(self,
                text: Dict[str, torch.LongTensor],
                predicate_indicator: torch.LongTensor,
                predicate_index: torch.LongTensor,
                **kwargs):
        # slot_name -> Shape: batch_size, 1
        gold_slot_labels = self._get_gold_slot_labels(kwargs)
        if gold_slot_labels is None:
            raise ConfigurationError("QuestionModel requires gold labels for teacher forcing when running forward. "
                                     "You may wish to run beam_decode instead.")
        # Shape: batch_size, num_tokens, self._sentence_encoder.get_output_dim()
        encoded_text, text_mask = self._sentence_encoder(text, predicate_indicator)
        # Shape: batch_size, self._sentence_encoder.get_output_dim()
        pred_rep = batched_index_select(encoded_text, predicate_index).squeeze(1)
        # slot_name -> Shape: batch_size, slot_name_vocab_size
        slot_logits = self._question_generator(pred_rep, **gold_slot_labels)

        batch_size, _ = pred_rep.size()

        # Shape: <scalar>
        slot_nlls, neg_log_likelihood = self._get_cross_entropy(slot_logits, gold_slot_labels)
        self.metric(slot_logits, gold_slot_labels, torch.ones([batch_size]), slot_nlls, neg_log_likelihood)
        return {**slot_logits, "loss": neg_log_likelihood}

    def beam_decode(self,
                    text: Dict[str, torch.LongTensor],
                    predicate_indicator: torch.LongTensor,
                    predicate_index: torch.LongTensor,
                    max_beam_size: int,
                    min_beam_probability: float,
                    clause_mode: bool = False):
        # Shape: batch_size, num_tokens, self._sentence_encoder.get_output_dim()
        encoded_text, text_mask = self._sentence_encoder(text, predicate_indicator)
        # Shape: batch_size, self._sentence_encoder.get_output_dim()
        pred_rep = batched_index_select(encoded_text, predicate_index).squeeze(1)
        return self._question_generator.beam_decode(pred_rep, max_beam_size, min_beam_probability, clause_mode)

    def get_metrics(self, reset: bool = False):
        return self.metric.get_metric(reset=reset)

    def _get_cross_entropy(self, slot_logits, gold_slot_labels):
        slot_xes = {}
        xe = None
        for slot_name in self.get_slot_names():
            slot_xe = F.cross_entropy(slot_logits[slot_name], gold_slot_labels[slot_name].squeeze(-1), reduction = "sum")
            slot_xes[slot_name] = slot_xe
            if xe is None:
                xe = slot_xe
            else:
                xe = xe + slot_xe
        return slot_xes, xe

    def _get_gold_slot_labels(self, instance_slot_labels_dict):
        # each of gold_slot_labels[slot_name] is of
        # Shape: batch_size
        gold_slot_labels = {}
        for slot_name in self.get_slot_names():
            if slot_name in instance_slot_labels_dict and instance_slot_labels_dict[slot_name] is not None:
                gold_slot_labels[slot_name] = instance_slot_labels_dict[slot_name].unsqueeze(-1)
        for slot_name in self.get_slot_names():
            if slot_name not in instance_slot_labels_dict or instance_slot_labels_dict[slot_name] is None:
                gold_slot_labels = None
        return gold_slot_labels


import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
from scipy.linalg import *
import random
from sklearn.cluster import spectral_clustering
import time

years = {"2016":12}
user_id_mapping = {}
id_user_mapping = {}

def get_adjacency_matrix(Graph):
    '''
    Build adjacency matrix for weighted graph
    '''
    n = Graph.number_of_nodes()
    A = np.zeros((n, n))
    weights = nx.get_edge_attributes(Graph,'weight')
    for edge, weight in weights.items():
        (s, e) = edge
        s_id = user_id_mapping[str(s)]
        e_id = user_id_mapping[str(e)]
        A[s_id][e_id] = weight
        A[e_id][s_id] = weight
    return A

def get_sparse_degree_matrix(Graph, A):
    '''
    Build degree matrix of weighted graph
    '''
    number_nodes = Graph.number_of_nodes()
    D = np.zeros((number_nodes, number_nodes))
    for node in Graph:
        node_id = user_id_mapping[node]
        D[node_id][node_id] = sum(A[node_id,:])
    return D

def modularity(Graph, A, D, assignments, num_clusters):
    '''
    Computes and returns modularity of cut assignments into num_clusters
    '''
    D_diag = np.diagonal(D)
    D_ = np.matmul(D_diag, D_diag.T)
    S = np.zeros((Graph.number_of_nodes(), num_clusters))
    S[np.arange(Graph.number_of_nodes()),assignments] = 1
    denominator = float(sum(sum(A)))
    B = A - (D/denominator)
    mod = np.trace(np.matmul(np.matmul(S.T, B),S))/denominator
    return mod

def plot_modularity_maximization(X, modularities):
    '''
    Plot graphs of modularity for different cluster sizes
    '''
    fig = plt.figure()
    plot2, = plt.plot(X, modularities[0], label = 'Feb. 2016')
    plot6, = plt.plot(X, modularities[1], label = 'June 2016')
    i = 0
    for mods in modularities:
        print(i)
        i += 1
        print(mods)
        print("max modularity")
        print(max(mods))
        print("argmax modularities")
        print(np.argmax(mods))
    plt.title("Users: Modularities for Different Cluster Sizes")
    plt.xlabel("Number of Clusters")
    plt.ylabel("Modularity")
    plt.legend()
    plt.savefig("user_comments_modularities.png")
    plt.show()

def main():
    directory = "NewUserGraphEdgeLists/"
    all_modularities = []

    for year in years.keys():

        months = ["0"+str(6), str(12)]
        for month in months:
            modularities = []
            filename = directory + year + "-" + month + "_user_comments.gml"
            a = time.time()

            # Read in graph
            print("reading in graph")
            Go = nx.read_gml(filename)
            b = time.time()
            print("Done reading in graph")
            print(b-a)

            # Get largets connected component
            print("getting largest connected component")
            G = max(nx.connected_component_subgraphs(Go), key=len)
            c = time.time()
            print("done getting largest connected component")
            print(c-b)

            # Create user <-> id mapping
            print("creating user-id mapping")
            i = 0
            for node in G:
                user_id_mapping[str(node)] = i
                i += 1
            id_user_mapping = dict(zip(user_id_mapping.values(), user_id_mapping.keys()))
            d = time.time()
            print("Done creating user-id mapping")
            print(d-b)

            # Write largest connected component of graph to gml file
            print("Graph has {0} nodes".format(G.number_of_nodes()))
            print("writing to gml file")
            nx.write_gml(G, "usergraph_connected"+month+"-"+year+".gml")
            print("Done writing to gml file")
            e = time.time()
            print(e-d)

            # Compute adjacency matrix A and degree matrix D
            print("Getting A")
            A = get_adjacency_matrix(G)
            f = time.time()
            print("Done getting A")
            print(f-e)
            print("Getting D")
            D = get_sparse_degree_matrix(G, A)
            print("Done getting D")
            g = time.time()
            print(g-f)

            # Find number of clusters that maximizes modularity
            for num_clusters in range(3, 20):
                o = time.time()
                print("Spectral clustering")
                assignments = spectral_clustering(A, n_clusters=num_clusters)
                h = time.time()
                print("Done with spectral clustering")
                print(h-o)
                print("Modularity")
                mod = modularity(G, A, D, assignments, num_clusters)
                k = time.time()
                print("Done getting modularity")
                print(k-h)
                print("Clusters: {0}".format(num_clusters))
                modularities.append(mod)
                print(modularities)
                assignmentMappings = {}
            all_modularities.append(modularities)

    l = time.time()
    print("Plot modularity")
    plot_modularity_maximization(range(3, 20), all_modularities)
    print("Done plotting modularity")
    m = time.time()
    print(m-l)

if __name__ == "__main__":
	main()


import grpc

from google.protobuf import empty_pb2
from .external_pb2 import *
from .blockchain_pb2 import *
from .quorum_set_pb2 import *
from .mobilecoind_api_pb2 import *
from .mobilecoind_api_pb2_grpc import *

import time, datetime
from typing import Tuple, Optional

MONITOR_SYNC_INTERVAL_SECONDS = 0.5
LEDGER_SYNC_INTERVAL_SECONDS = 0.5

DEFAULT_SUBADDRESS_INDEX = 0

# see transaction/core/src/constants.rs
MINIMUM_FEE = 10_000_000_000

def parse_tx_status(status) -> str:
    if status == TxStatus.Unknown:
        return "Unknown"
    elif status == TxStatus.Verified:
        return "Verified"
    elif status == TxStatus.TombstoneBlockExceeded:
        return "Tombstone Block Exceeded"
    elif status == TxStatus.InvalidConfirmationNumber:
        return "Invalid Confirmation Number"
    else:
        return "Error: Unexpected TxStatus ({})".format(tx_status)

class MonitorNotFound(Exception):
    """ When a Monitor is not Found"""
    pass

class Client(object):
    """ Manages the MobileCoin Wallet Client.
    """
    def __init__(self, daemon, ssl):
        """ Initializes the client and connects to a mobilecoind service.
          daemon -- address and port of mobilecoind daemon
          ssl    -- use SSL to connect
        """

        if ssl:
            credentials = grpc.ssl_channel_credentials()
            self.channel = grpc.secure_channel(daemon, credentials)
        else:
            self.channel = grpc.insecure_channel(daemon)
        self.stub = MobilecoindAPIStub(self.channel)

    def __del__(self):
        """ Close the gRPC connection upon deletion."""
        try:
            self.channel.close()
        except Exception:
            pass

    def add_monitor(self,
                    account_key: bytes,
                    first_subaddress: int = DEFAULT_SUBADDRESS_INDEX,
                    num_subaddresses: int = 10000, # requires ~ 1.5 seconds
                    first_block: int = 0):
        """ Create a process that watches the ledger for tx outputs belonging to a
        set of subaddresses, each specified by account_key and an index.
        """
        request = AddMonitorRequest(account_key=account_key,
                                    first_subaddress=first_subaddress,
                                    num_subaddresses=num_subaddresses,
                                    first_block=first_block)
        return self.stub.AddMonitor(request)

    def remove_monitor(self, monitor_id: bytes):
        """ Remove an existing monitor and delete any data it has stored.
        """
        request = RemoveMonitorRequest(monitor_id=monitor_id)
        return self.stub.RemoveMonitor(request)

    def get_monitor_list(self):
        """ Returns a list of all active monitors.
        """
        return self.stub.GetMonitorList(empty_pb2.Empty())

    def get_monitor_status(self, monitor_id: bytes):
        """ Returns a status report for a monitor process.
        """
        request = GetMonitorStatusRequest(monitor_id=monitor_id)
        return self.stub.GetMonitorStatus(request)

    def get_unspent_tx_output_list(self,
                                   monitor_id: bytes,
                                   subaddress_index: int = DEFAULT_SUBADDRESS_INDEX):
        """ Returns the list of tx outputs collected for a subaddress.
        """
        request = GetUnspentTxOutListRequest(monitor_id=monitor_id,
                                             subaddress_index=subaddress_index)
        return self.stub.GetUnspentTxOutList(request)

    def get_balance(self,
                    monitor_id: bytes,
                    subaddress_index: int = DEFAULT_SUBADDRESS_INDEX):
        """ Returns the sum of unspent tx outputs collected for a subaddress.
        """
        request = GetBalanceRequest(monitor_id=monitor_id,
                                    subaddress_index=subaddress_index)
        return self.stub.GetBalance(request)

    def get_monitor_id(self,
                       account_key: bytes,
                       subaddress_index: int = DEFAULT_SUBADDRESS_INDEX):
        """ Returns the monitor for a given subaddress, if one exists.
        """
        monitor_id_list = self.get_monitor_list().monitor_id_list
        target_vpk = account_key.view_private_key
        target_spk = account_key.spend_private_key

        best_monitor_id = b''
        best_next_block = 0
        for monitor_id in monitor_id_list:
            status = self.get_monitor_status(monitor_id).status
            if target_vpk == status.account_key.view_private_key and target_spk == status.account_key.spend_private_key:
                if subaddress_index >= status.first_subaddress and subaddress_index <= status.first_subaddress + status.num_subaddresses:
                    if status.next_block >= best_next_block:
                        best_next_block = status.next_block
                        best_monitor_id = monitor_id
        if best_monitor_id:
            return best_monitor_id
        else:
            raise MonitorNotFound

    #
    # Utilities
    #

    def generate_entropy(self) -> bytes:
        """ Generate 32 bytes of entropy using a cryptographically secure RNG.
        """
        response = self.stub.GenerateRootEntropy(empty_pb2.Empty())
        return response.root_entropy

    def generate_mnemonic(self) -> str:
        """ Generate entropy mnemonic using a cryptographically secure RNG.
        """
        response = self.stub.GenerateMnemonic(empty_pb2.Empty())
        return response.mnemonic

    def get_account_key(self, entropy: bytes):
        """ Get the private keys from entropy.
        """
        request = GetAccountKeyFromRootEntropyRequest(root_entropy=entropy)
        return self.stub.GetAccountKeyFromRootEntropy(request)

    def get_account_key_from_mnemonic(self, mnemonic: str):
        """ Get the private keys from the entropy mnemonic.
        """
        request = GetAccountKeyFromMnemonicRequest(mnemonic=mnemonic)
        return self.stub.GetAccountKeyFromMnemonic(request)

    def get_public_address(self,
                           monitor_id: bytes,
                           subaddress_index: int = DEFAULT_SUBADDRESS_INDEX):
        """ Returns the public address for a given monitor and index
        """
        request = GetPublicAddressRequest(
            monitor_id=monitor_id, subaddress_index=subaddress_index)
        return self.stub.GetPublicAddress(request)

    def parse_address_code(self, b58_code: str):
        """ Parse a b58 encoded public address
        """
        request = ParseAddressCodeRequest(b58_code=b58_code)
        return self.stub.ParseAddressCode(request)

    def create_address_code(self, receiver):
        """ Create a b58 encoding for a public address
        """
        request = CreateAddressCodeRequest(receiver=receiver)
        return self.stub.CreateAddressCode(request)

    def parse_request_code(self, b58_code: str):
        """ Parse a b58 request code to recover content.
        """
        request = ParseRequestCodeRequest(b58_code=b58_code)
        return self.stub.ParseRequestCode(request)

    def create_request_code(self, receiver, value: int = 0, memo: str = ""):
        """ Create a "request code" used to generate a QR code for wallet apps.
        """
        request = CreateRequestCodeRequest(receiver=receiver,
                                           value=value,
                                           memo=memo)
        return self.stub.CreateRequestCode(request)

    def parse_transfer_code(self, b58_code: str):
        """ Parse a b58 transfer code to recover content.
        """
        request = ParseTransferCodeRequest(b58_code=b58_code)
        return self.stub.ParseTransferCode(request)

    def create_transfer_code(self, entropy: bytes, tx_public_key, memo: str = ""):
        """ Create a "transfer code" used to generate a QR code for wallet apps.
        """
        request = CreateTransferCodeRequest(entropy=entropy,
                                            tx_public_key=tx_public_key,
                                            memo=memo)
        return self.stub.CreateTransferCode(request)

    #
    # Transactions
    #

    def generate_tx(self,
                    sender_monitor_id: bytes,
                    change_subaddress: int,
                    input_list,
                    outlay_dict,
                    fee: int = 0,
                    tombstone: int = 0):
        """ Prepares a transaction. If the fee is zero, we use the default minimum fee. Mix-ins and other
        complexities of the MobileCoin protocol are handled automatically.
        """
        outlay_list = [
            Outlay(value=r['value'], receiver=r['receiver'])
            for r in outlay_dict
        ]
        request = GenerateTxRequest(sender_monitor_id=sender_monitor_id,
                                    change_subaddress=change_subaddress,
                                    input_list=input_list,
                                    outlay_list=outlay_list,
                                    fee=fee,
                                    tombstone=tombstone)
        return self.stub.GenerateTx(request)

    def generate_optimization_tx(self,
                                 monitor_id: bytes,
                                 subaddress: int = DEFAULT_SUBADDRESS_INDEX):
        """ Due to limits on the number of inputs allowed for a transaction, a wallet can contain
        more value than is spendable in a single transaction. This generates a self-payment
        that combines small value tx outputs together.
        """
        request = GenerateOptimizationTxRequest(monitor_id=monitor_id,
                                                subaddress=subaddress)
        return self.stub.GenerateOptimizationTx(request)

    def generate_transfer_code_tx(self,
                                  sender_monitor_id: bytes,
                                  change_subaddress: int,
                                  input_list,
                                  value: int,
                                  fee: int,
                                  tombstone: int,
                                  memo: str):
        """ Prepares a transaction that can be submitted to fund a transfer code for a new
        one time account.
        """
        request = GenerateTransferCodeTxRequest(
            sender_monitor_id=sender_monitor_id,
            change_subaddress=change_subaddress,
            input_list=input_list,
            value=value,
            fee=fee,
            tombstone=tombstone,
            memo=memo)
        return self.stub.GenerateTransferCodeTx(request)

    def generate_tx_from_tx_out_list(self,
                                     account_key: bytes,
                                     input_list,
                                     receiver,
                                     fee: int):
        request = GenerateTxFromTxOutListRequest(
            account_key=account_key,
            input_list=input_list,
            receiver=receiver,
            fee=fee,
        )
        return self.stub.GenerateTxFromTxOutList(request)

    def submit_tx(self, tx_proposal):
        """ Submit a prepared transaction proposal.
        """
        request = SubmitTxRequest(tx_proposal=tx_proposal)
        return self.stub.SubmitTx(request)

    #
    # Databases
    #

    def get_ledger_info(self):
        """ Returns a status report for mobilecoind's ledger maintenance.
        """
        return self.stub.GetLedgerInfo(empty_pb2.Empty())

    def get_block_info(self, block: int):
        """ Returns a status report for a ledger block.
        """
        request = GetBlockInfoRequest(block=block)
        return self.stub.GetBlockInfo(request)

    def get_block(self, block: int):
        """ Returns detailed information for a ledger block.
        """
        request = GetBlockRequest(block=block)
        return self.stub.GetBlock(request)

    def get_tx_status_as_sender(self, submit_response):
        """ Check if a key image appears in the ledger.
        """
        return self.stub.GetTxStatusAsSender(submit_response)

    def get_tx_status_as_receiver(self, receiver_tx_receipt):
        """ Check if a transaction public key appears in the ledger.
        """
        request = GetTxStatusAsReceiverRequest(receipt=receiver_tx_receipt)
        return self.stub.GetTxStatusAsReceiver(request)

    #
    # Blockchain and network info
    #

    def get_network_status(self):
        """ Returns the total network height and our current sync height
        """
        return self.stub.GetNetworkStatus(empty_pb2.Empty())

    #
    # Convenience functions using the mobilecoind API
    #

    def wait_for_ledger(self,
                        max_blocks_to_sync: int = 100,
                        timeout_seconds: int = 10) -> Tuple[bool, int, int, Optional[float]]:
        """ Check if the local copy of the ledger is in sync

        If we are behind, wait until the ledger downloads up to max_blocks_to_sync
        If we are still behind, return (True, local blocks, remote blocks, rate in blocks/sec)
        If we are in sync, return (False, local blocks, remote blocks, None)

        """
        network_status_response = self.get_network_status()
        remote_count = network_status_response.network_highest_block_index
        local_count = network_status_response.local_block_index
        ledger_is_behind = network_status_response.is_behind

        if not ledger_is_behind:
            return (ledger_is_behind, local_count, remote_count, None)

        start = datetime.datetime.now()
        initial_local_count = local_count
        while ledger_is_behind:
            time.sleep(LEDGER_SYNC_INTERVAL_SECONDS)

            network_status_response = self.get_network_status()
            remote_count = network_status_response.network_highest_block_index
            local_count = network_status_response.local_block_index
            ledger_is_behind = network_status_response.is_behind

            delta = datetime.datetime.now() - start
            total_blocks_synced = local_count - initial_local_count

            if total_blocks_synced > max_blocks_to_sync:
                break

            if delta.total_seconds() > timeout_seconds:
                break

        blocks_per_second = total_blocks_synced / delta.total_seconds()
        return (ledger_is_behind, local_count, remote_count, blocks_per_second)

    def wait_for_monitor(self,
                         monitor_id: bytes,
                         max_blocks_to_sync: int = 100,
                         timeout_seconds: int = 10) -> Tuple[bool, int, int, Optional[float]]:
        """ Check if a monitor is in sync

        If we are behind, wait until the monitor processes up to max_blocks_to_sync
        If we are still behind, return (True, monitor next block, remote blocks, rate in blocks/sec)
        If we are in sync, return (False, monitor next block, remote blocks, None)

        """

        # check the ledger and monitor
        network_status_response = self.get_network_status()
        remote_count = network_status_response.network_highest_block_index
        local_count = network_status_response.local_block_index
        ledger_is_behind = network_status_response.is_behind

        next_block = self.get_monitor_status(monitor_id).status.next_block
        monitor_is_behind = ledger_is_behind or (next_block <= local_count)

        if not monitor_is_behind:
            return (monitor_is_behind, next_block, remote_count, None)

        start = datetime.datetime.now()
        initial_next_block = next_block
        while monitor_is_behind:
            time.sleep(MONITOR_SYNC_INTERVAL_SECONDS)

            network_status_response = self.get_network_status()
            remote_count = network_status_response.network_highest_block_index
            local_count = network_status_response.local_block_index
            ledger_is_behind = network_status_response.is_behind

            next_block = self.get_monitor_status(monitor_id).status.next_block
            monitor_is_behind = ledger_is_behind or (next_block <= local_count)

            delta = datetime.datetime.now() - start
            total_blocks_synced = next_block - initial_next_block

            if total_blocks_synced > max_blocks_to_sync:
                break

            if delta.total_seconds() > timeout_seconds:
                break

        blocks_per_second = total_blocks_synced / delta.total_seconds()
        return (monitor_is_behind, next_block, remote_count, blocks_per_second)


from bs4 import BeautifulSoup
import re
import json
import numpy as np

def handle_file(file_name):
    items = list()
    with open(file_name, encoding='utf-8') as file:
        text = ''
        for row in file.readlines():
            text += row

        site = BeautifulSoup(text, 'html.parser')
        products = site.find_all('div', attrs={'class': 'product-item'})

        for product in products:
            item = dict()
            item['id'] = product.a['data-id']
            item['link'] = product.find_all('a')[1]['href']
            item['img_url'] = product.find_all('img')[0]['src']
            item['title'] = product.find_all('span')[0].get_text().strip()
            item['price'] = int(product.price.get_text().replace('₽', '').replace(' ', '').strip())
            item['bonus'] = int(product.strong.get_text().replace('+ начислим ', '').replace(' бонусов','').strip())
            props = product.ul.find_all('li')
            for prop in props:
                item[prop['type']] = prop.get_text().strip()
            items.append(item)

            # print(item)

    return items

items = []

for i in range(1, 45):
    file_name = f"zip_var_27_2/{i}.html"
    items += handle_file(file_name)

items = sorted(items, key=lambda x: x['id'], reverse=True)

with open('result_filtered_id.json', 'w', encoding='utf-8') as f:
    f.write(json.dumps(items))

filt_items = list()
for item in items:
    if(item['bonus'] >= 4000):
        filt_items.append(item)

with open('result_filtered_bonus.json', 'w', encoding='utf-8') as f:
    f.write(json.dumps(filt_items))

prices = list()

for item in items:
    prices.append(int(item['price']))

result_num = {}

result_num['max'] = str(np.max(prices))
result_num['min'] = str(np.min(prices))
result_num['avg'] = str(np.average(prices))
result_num['sum'] = str(np.sum(prices))
result_num['std'] = str(np.std(prices))

result_text = {}

for item in items:
    elem = item.get('matrix')
    if(elem != None):
        if(elem in result_text):
            result_text[elem] += 1
        else:
            result_text[elem] = 1

result_num['text'] = result_text
with open('result_math.json', 'w', encoding='utf-8') as f:
    json.dump(result_num, f, ensure_ascii=False)

handle_file("zip_var_27_2/27.html")


#!/usr/bin/env python3

r"""
This file contains functions which are useful for processing BMC dumps.
"""

import imp
import os
import sys

import bmc_ssh_utils as bsu
import gen_misc as gm
import gen_print as gp
import gen_robot_keyword as grk
from robot.libraries.BuiltIn import BuiltIn

base_path = (
    os.path.dirname(os.path.dirname(imp.find_module("gen_robot_print")[1]))
    + os.sep
)
sys.path.append(base_path + "data/")
import variables as var  # NOQA

def get_dump_dict(quiet=None):
    r"""
    Get dump information and return as an ordered dictionary where the keys
    are the dump IDs and the values are the full path names of the dumps.

    Example robot program call:

    ${dump_dict}=  Get Dump Dict
    Rprint Vars  dump_dict

    Example output:

    dump_dict:
      [1]:
      /var/lib/phosphor-debug-collector/dumps/1/obmcdump_1_1508255216.tar.xz
      [2]:
      /var/lib/phosphor-debug-collector/dumps/2/obmcdump_2_1508255245.tar.xz
      [3]:
      /var/lib/phosphor-debug-collector/dumps/3/obmcdump_3_1508255267.tar.xz
      [4]:
      /var/lib/phosphor-debug-collector/dumps/4/obmcdump_4_1508255283.tar.xz

    Description of argument(s):
    quiet                           If quiet is set to 1, this function will
                                    NOT write status messages to stdout.
    """

    quiet = int(gp.get_var_value(quiet, 1))
    cmd_buf = "find /var/lib/phosphor-debug-collector/ -maxdepth 4 -type f"
    output, stderr, rc = bsu.bmc_execute_command(cmd_buf, quiet=quiet)

    BuiltIn().log_to_console(output)
    return output.split("\n")

def valid_dump(dump_id, dump_dict=None, quiet=None):
    r"""
    Verify that dump_id is a valid.  If it is not valid, issue robot failure
    message.

    A dump is valid if the indicated dump_id refers to an existing dump with a
    valid associated dump file.

    Description of argument(s):
    dump_id                         A dump ID (e.g. "1", "2", etc.)
    dump_dict                       A dump dictionary such as the one returned
                                    by get_dump_dict.  If this value is None,
                                    this function will call get_dump_dict on
                                    the caller's behalf.
    quiet                           If quiet is set to 1, this function will
                                    NOT write status messages to stdout.
    """

    if dump_dict is None:
        dump_dict = get_dump_dict(quiet=quiet)

    if dump_id not in dump_dict:
        message = (
            "The specified dump ID was not found among the existing"
            + " dumps:\n"
        )
        message += gp.sprint_var(dump_id)
        message += gp.sprint_var(dump_dict)
        BuiltIn().fail(gp.sprint_error(message))

    if not dump_dict[dump_id].endswith("tar.xz"):
        message = (
            'There is no "tar.xz" file associated with the given'
            + " dump_id:\n"
        )
        message += gp.sprint_var(dump_id)
        dump_file_path = dump_dict[dump_id]
        message += gp.sprint_var(dump_file_path)
        BuiltIn().fail(gp.sprint_error(message))

def scp_dumps(targ_dir_path, targ_file_prefix="", dump_dict=None, quiet=None):
    r"""
    SCP all dumps from the BMC to the indicated directory on the local system
    and return a list of the new files.

    Description of argument(s):
    targ_dir_path                   The path of the directory to receive the
                                    dump files.
    targ_file_prefix                Prefix which will be prepended to each
                                    target file's name.
    dump_dict                       A dump dictionary such as the one returned
                                    by get_dump_dict.  If this value is None,
                                    this function will call get_dump_dict on
                                    the caller's behalf.
    quiet                           If quiet is set to 1, this function will
                                    NOT write status messages to stdout.
    """

    targ_dir_path = gm.add_trailing_slash(targ_dir_path)

    if dump_dict is None:
        dump_list = get_dump_dict(quiet=quiet)

    status, ret_values = grk.run_key("Open Connection for SCP", quiet=quiet)

    dump_file_list = []
    for file_path in dump_list:
        targ_file_path = (
            targ_dir_path + targ_file_prefix + os.path.basename(file_path)
        )
        status, ret_values = grk.run_key(
            "scp.Get File  " + file_path + "  " + targ_file_path, quiet=quiet
        )
        dump_file_list.append(targ_file_path)

    return dump_file_list


import arcade
import random

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

SPRITE_SCALING_COIN = 2
SPRITE_SCALING_PLAYER = 0.2
COIN_COUNT = 50

coin = arcade.Sprite("coin_01.png", SPRITE_SCALING_COIN)

class MyGame(arcade.Window):
    """ Main application class. """

    def __init__(self, width, height):
        super().__init__(width, height)

        arcade.set_background_color(arcade.color.AMAZON)

    def setup(self):
        """ Set up the game and initialize the variables. """

        # Create the sprite lists
        self.player_list = arcade.SpriteList()
        self.coin_list = arcade.SpriteList()

        # Score
        self.score = 0

        # Set up the player
        # Character image from kenney.nl
        self.player_sprite = arcade.Sprite("character.png", SPRITE_SCALING_PLAYER)
        self.player_sprite.center_x = 50 # Starting position
        self.player_sprite.center_y = 50
        self.player_list.append(self.player_sprite)

        # Create the coins
        for i in range(COIN_COUNT):

            # Create the coin instance
            # Coin image from kenney.nl
            coin = arcade.Sprite("coin_01.png", SPRITE_SCALING_COIN)

            coin.height = 200
            coin.width = 200

            # Position the coin
            # coin.center_x = random.randrange(1, SCREEN_WIDTH / 2)
            # coin.center_y = random.randrange(1, SCREEN_HEIGHT / 2)

            # Add the coin to the lists
            self.coin_list.append(coin)

    def on_draw(self):
        """ Render the screen. """
        arcade.start_render()
        # Your drawing code goes here
        self.coin_list.draw()
        self.player_list.draw()

    def update(self, delta_time):
        """ All the logic to move, and the game logic goes here. """
        pass

def main():
    game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT)
    game.setup()
    arcade.run()

if __name__ == "__main__":
    main()


from project.libs.response import ItemResp, ItemsResp
from project.libs.validators.request import validate_request
from project.product.entities import Category

from . import schemas
from .interfaces import ICategoryRepo, ICategoryUseCase
from .requests import (
    CreateCategoryRequest,
    DeleteCategoryRequest,
    FindCategoriesRequest,
    GetCategoryByIdRequest,
    UpdateCategoryRequest,
)

class CategoryUseCase(ICategoryUseCase):
    def __init__(self, repo: ICategoryRepo):
        self.repo = repo

    @validate_request(schema=schemas.FindCategoriesSchema)
    def find(self, req: FindCategoriesRequest) -> ItemsResp:
        return self.repo.find(req.search)

    @validate_request(schema=schemas.CreateCategorySchema)
    def create(self, req: CreateCategoryRequest):

        category = Category(
            name=req.name,
        )

        return self.repo.create(category)

    @validate_request(schema=schemas.UpdateCategorySchema)
    def update(self, req: UpdateCategoryRequest) -> ItemResp:

        resp = self.repo.get_by_id(
            id=req.id,
        )
        if not resp.ok:
            return resp

        category = resp.item
        category.name = req.name

        return self.repo.update(category)

    @validate_request(schema=schemas.GetCategoryByIdSchema)
    def get_by_id(self, req: GetCategoryByIdRequest) -> ItemResp:
        return self.repo.get_by_id(req.id)

    @validate_request(schema=schemas.GetCategoryByIdSchema)
    def delete(self, req: DeleteCategoryRequest) -> ItemResp:
        return self.repo.get_by_id(req.id)


from cv2 import *
import numpy as np
from PIL import Image
import numpy
from matplotlib import pyplot as plt
from resizeimage import resizeimage

utezi = [1, 2, 4, 8, 16, 32, 64, 128]#za bite npr 00000101 pomnozis z 1 in z 4
uniformni = [0, 1, 2, 3, 4, 6, 7, 8, 12, 14, 15, 16, 24, 28, 30, 31, 32, 48, 56, 60, 62, 63, 64, 96, 112, 120, 124, 126, 127, 128, 129, 131, 135, 143, 159, 191, 192, 193, 195, 199, 207, 223, 224, 225, 227, 231, 239, 240, 241, 243, 247, 248, 249, 251, 252, 253, 254, 255]
UNI = []
histogramuniformni = []
LBPuniform = []
q=False
print(len(uniformni))

for a in range(0, 256): # priredis vrednosti 59 v UNI polje
    for x in range(0, len(uniformni)):
        if uniformni[x] == a:
            UNI.append(a)
            q = True
            break

    if q != True:
        UNI.append(59)
    else:
        q = False

print(UNI)

img = cv2.imread("na.jpg")

grayImg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
LBPosnovna = grayImg.copy()
LBPusmerjenih = grayImg.copy()

height, width, channels = img.shape

def izracunajLBP():
    global grayImg, LBPosnovna

    for y in range(1, height):
        for x in range(1, width):
            if(y < height-1 and x < width-1):

                sredina = grayImg[y, x]
                zgorajLevo = grayImg[y-1, x-1]
                zgoraj = grayImg[y-1, x]
                zgorajDesno = grayImg[y-1, x+1]
                levo= grayImg[y, x-1]
                desno = grayImg[y, x+1]
                spodajLevo = grayImg[y+1, x-1]
                spodaj= grayImg[y+1, x]
                spodajDesno= grayImg[y+1, x+1]

                byte = []

                for i in [zgorajLevo, zgoraj, zgorajDesno, levo, desno, spodajLevo, spodaj, spodajDesno]:
                    if i >= sredina:
                        byte.append(1)
                    else:
                        byte.append(0)

                rezultat = 0

                for i in range(0, 8):
                    rezultat += utezi[i]*byte[i]

                LBPosnovna[y, x] = rezultat

def izracunajLBPuniform():
    global LBPosnovna, grayImg, UNI, histogramuniformni, LBPuniform
    Hst = [0] * 256

    LBPuniform = LBPosnovna.copy()

    for y in range(0, height):
        for x in range(0, width):
                Hst[UNI[LBPosnovna[y, x]]]+=1
                LBPuniform[y,x] = UNI[LBPosnovna[y,x]]

    for x in range(0, len(Hst)):
        if Hst[x] != 0:
            histogramuniformni.append(Hst[x])

def izracunajLBPusmerjenihGrad(d):
    global grayImg, LBPusmerjenih

    for y in range(1, height):
        for x in range(1, width):
            if (y < height - 1 and x < width - 1):

                zgorajLevo = grayImg[y - 1, x - 1]
                zgoraj = grayImg[y - 1, x]
                zgorajDesno = grayImg[y - 1, x + 1]
                levo = grayImg[y, x - 1]
                desno = grayImg[y, x + 1]
                spodajLevo = grayImg[y + 1, x - 1]
                spodaj = grayImg[y + 1, x]
                spodajDesno = grayImg[y + 1, x + 1]

                byte = []
                polja = [zgorajLevo, zgoraj, zgorajDesno, levo, desno, spodajLevo, spodaj, spodajDesno]

                stevec = 0

                for i in polja:
                    primerjaj = stevec+d

                    #print(primerjaj)
                    if primerjaj > 7:
                        primerjaj = (primerjaj - 7) - 1
                        #print("ta je index potem:")
                        #print(primerjaj)

                    if i >= polja[primerjaj]:
                        byte.append(1)
                    else:
                        byte.append(0)
                    stevec += 1

                rezultat = 0

                for i in range(0, 8):
                    rezultat += utezi[i] * byte[i]

                LBPusmerjenih[y, x] = rezultat

def pridobiKote(x, y):
    if x == 0:
        x=1
    kot = np.round(np.arctan2(y, x) * (180/np.pi))

    return kot

pridobiKote = np.vectorize(pridobiKote)

def gradientX(G, h, w):
    Gradient = G.copy()

    for x in range(h):
        for y in range(w):
            if (x == h-1 or y == w-1):
                Gradient[x][y] = 0
            else:
                Gradient[x][y] = abs(int(G[x][y -1]*(-1)) + int(G[x][y+1])) #[-1,0,1] pomnozis pa sestejes

    return Gradient

def gradientY(G, h, w):
    Gradient = G.copy()

    for x in range(h):
        for y in range(w):
            if (x == h-1 or y == w-1):
                Gradient[x][y] = 0
            else:
                Gradient[x][y] = abs(int(G[x-1][y] * (-1)) + int(G[x+1][y]))  # [[0,-1,0],[0,0,0],[0,1,0]] pomnozis pa sestejes

    return Gradient

def HOG(bins, cellsize, regionsize):
    global grayImg
    widthR = width
    heightR = height

    while(True): #izracunam da je sirina slike deljiva z cellsize
        if widthR % cellsize == 0:
            break;
        else:
            widthR+=1

    while (True):
        if heightR % cellsize == 0:
            break;
        else:
            heightR += 1

    print(heightR,widthR)

    imgResized = cv2.resize(grayImg, (widthR, heightR))

    Gx = np.asarray(imgResized).copy()
    Gy = np.asarray(imgResized).copy()

    Gx = gradientX(Gx, heightR, widthR)
    Gy = gradientY(Gy, heightR, widthR)

    Gradient = np.round(np.sqrt(np.multiply(Gx, Gx) + np.multiply(Gy, Gy)))

    A = pridobiKote(Gx, Gy)

    razmakBina = 180/bins

    '''for Y in range(heightR):
        for X in range(widthR):
            if A[Y][X] > 80:
                print(A[Y][X])
    '''
    stevec = 0
    koncneVrenosti = []

    for regijaY in range(0, heightR-cellsize, cellsize):
        for regijaX in range(0, widthR-cellsize, cellsize):
            stevec+=1
            Hist = [[0.0] * bins for i in range(regionsize * regionsize)]
            index1Hisotgrama = 0
            vsotaVrednosti = 0

            zactekRegijeY = regijaY
            zactekRegijeX = regijaX

            konecRegijeX = regijaX+ (cellsize * regionsize)
            konecRegijeY = regijaY+ (cellsize * regionsize)

            for celY in range(zactekRegijeY, konecRegijeY, cellsize):
                for celX in range(zactekRegijeX, konecRegijeX, cellsize):

                    zacetekCeliceY = celY
                    zacetekCeliceX = celX

                    konecCeliceY = celY + cellsize
                    konecCeliceX = celX + cellsize

                    for Y in range(zacetekCeliceY, konecCeliceY):
                        for X in range(zacetekCeliceX, konecCeliceX):
                            kot = A[Y, X]
                            gradient = Gradient[Y, X]

                            if (kot > razmakBina):
                                index = np.ceil((kot / razmakBina))
                                ostanek1 = index - (kot / razmakBina)
                                trenutni = (1 - ostanek1) * gradient
                                prejsni = ostanek1 * gradient
                            else:
                                index = np.ceil((kot / razmakBina))
                                ostanek1 = (kot / razmakBina)
                                prejsni = (1 - ostanek1) * gradient
                                trenutni = ostanek1 * gradient

                            Hist[index1Hisotgrama][int(index)] += trenutni
                            Hist[index1Hisotgrama][int(index - 1)] += prejsni

                    index1Hisotgrama += 1

            for histogram in range(0, regionsize * regionsize):
                for steviloHistggrama in range(0, bins):
                    vsotaVrednosti += np.multiply(Hist[histogram][steviloHistggrama],Hist[histogram][steviloHistggrama])

                    koncneVrenosti.append(Hist[histogram][steviloHistggrama])
            print(Hist)
            L = np.sqrt(vsotaVrednosti)

            print(L)

    print(koncneVrenosti)

    print(len(koncneVrenosti))
    print(stevec)

izracunajLBP()
izracunajLBPuniform()
izracunajLBPusmerjenihGrad(2)

HOG(9, 8, 2)

cv2.imshow("LBP osnovna", LBPosnovna)
cv2.imshow("LBP unfirom", LBPuniform)
cv2.imshow("LBP usmerjenih", LBPusmerjenih)

histogram = cv2.calcHist([LBPosnovna],[0],None,[256],[0,256])
sum = histogram.cumsum()#komulativno sesteje vse vrednosti histograma
normaliziranHistogram = (sum * histogram.max()) / sum.max()

plt.plot(normaliziranHistogram, color = 'g')
plt.hist(LBPosnovna.flatten(),256,[0,256])#speremnis 256 v 59 za uniform
plt.title("LBP osnovno")
plt.show()

cv2.waitKey(0)
cv2.destroyAllWindows()


import json
import logging
from datetime import datetime

import redis
from django.contrib.auth import login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.postgres.search import SearchVector
from django.db.models import Q
from django.db.models.functions import Length
from django.http import JsonResponse, HttpResponseForbidden
from django.shortcuts import get_object_or_404
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_exempt
from django_ratelimit.decorators import ratelimit
from .forms import CustomUserCreationForm, CustomAuthenticationForm

from .forms import StartConversationForm
from .models import Conversation, CustomUser

logger = logging.getLogger(__name__)

# ---------------------------- Register ----------------------------

@csrf_exempt
@ratelimit(key='ip', rate='5/h', method='POST', block=True)
def register(request):
    if request.method == 'POST':
        form = CustomUserCreationForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            logger.info(f"User '{user.email}' registered successfully.")
            return redirect('chat:conversations_list')
        else:
            logger.error("Form submission is not valid. Errors:")
            for field, errors in form.errors.items():
                for error in errors:
                    logger.error(f"{field}: {error}")
    else:
        form = CustomUserCreationForm()

    return render(request, 'users/authentication/register.html', {'form': form})

# ---------------------------- Login ----------------------------

@csrf_exempt
@ratelimit(key='ip', rate='10/m', method='POST', block=True)
def user_login(request):
    if request.method == 'POST':
        form = CustomAuthenticationForm(data=request.POST)
        if form.is_valid():
            user = form.get_user()
            login(request, user)
            logger.info(f"User '{user.email}' logged in successfully.")
            return redirect('chat:conversations_list')
        else:
            logger.error("Invalid login credentials. Errors:")
            for field, errors in form.errors.items():
                for error in errors:
                    logger.error(f"{field}: {error}")
            return render(request, 'users/authentication/login.html', {'form': form})
    else:
        form = CustomAuthenticationForm()

    return render(request, 'users/authentication/login.html', {'form': form})

# ---------------------------- Logout ----------------------------

def user_logout(request):
    logout(request)
    logger.info("User logged out.")
    return redirect('chat:login')

# ---------------------------- Conversation List ----------------------------

@login_required
@csrf_exempt
@ratelimit(key='user', rate='10/h', method='GET', block=True)
def conversations_list(request):
    try:
        conversations = Conversation.objects.filter(Q(user1=request.user) | Q(user2=request.user))

        conversation_data = []
        for conversation in conversations:
            if conversation.user1 == request.user:
                participant = conversation.user2
            else:
                participant = conversation.user1
            conversation_data.append({
                'id': conversation.id,
                'participant': participant,
            })
        logger.info(f"User '{request.user.email}' fetched conversation list successfully.")

    except Exception as e:
        conversation_data = []
        logger.error(f"An error occurred while fetching conversation list: {str(e)}")

    return render(request, 'chat/conversation/conversations_list.html', {'conversations': conversation_data})

# ---------------------------- Fetch messages from Redis ----------------------------

def fetch_messages_from_redis(conversation):
    try:
        redis_client = redis.StrictRedis(host="localhost", port=6379, db=0)
        conversation_key = f"conversation_{conversation.id}"
        message_strings = redis_client.lrange(conversation_key, 0, -1)
        messages = []
        for message_string in message_strings:
            message_data = json.loads(message_string.decode("utf-8"))
            message_data['formatted_timestamp'] = datetime.strptime(message_data['timestamp'],
                                                                    "%Y-%m-%d %H:%M:%S.%f").strftime("%m/%d/%Y %H:%M")
            messages.append(message_data)
        logger.info(f"Fetched messages for conversation {conversation.id}.")

    except redis.ConnectionError as e:
        messages = []
        logger.error(f"Redis connection error: {str(e)}")

    except Exception as e:
        messages = []
        logger.error(f"An error occurred while fetching messages from Redis: {str(e)}")

    finally:
        if 'redis_client' in locals():
            redis_client.close()

    return messages

# ---------------------------- Conversation View ----------------------------

@login_required
@csrf_exempt
@ratelimit(key='user', rate='5/m', method='GET', block=True)
def conversation_view(request, conversation_id):
    conversation = get_object_or_404(Conversation, id=conversation_id)

    if request.user not in [conversation.user1, conversation.user2]:
        return HttpResponseForbidden("You do not have permission to access this conversation.")

    participant = conversation.user2 if conversation.user1 == request.user else conversation.user1

    messages = fetch_messages_from_redis(conversation)

    return render(request, 'chat/conversation/conversation_view.html', {
        'conversation': conversation,
        'participant': participant,
        'messages': messages,
        'conversation_id': conversation_id,
    })

# ---------------------------- Start New Conversation ----------------------------

@login_required
@csrf_exempt
def start_conversation(request):
    if request.method == 'POST':
        form = StartConversationForm(request.POST)
        if form.is_valid():
            participants = form.cleaned_data['participants']
            existing_conversation = Conversation.objects.filter(
                Q(user1=request.user, user2=participants) | Q(user1=participants, user2=request.user)
            )

            if existing_conversation.exists():
                return redirect('chat:view_conversation', conversation_id=existing_conversation.first().id)
            else:
                conversation = Conversation.objects.create(user1=request.user, user2=participants)
                logger.info(f"Started a new conversation {conversation.id}.")
                return redirect('chat:view_conversation', conversation_id=conversation.id)
    else:
        form = StartConversationForm()

    return render(request, 'chat/conversation/conversation_start.html', {'form': form})

# ---------------------------- Search users by email ----------------------------

@login_required
@csrf_exempt
def search_users(request):
    try:
        if 'q' in request.GET:
            search_term = request.GET['q'].strip()
            if search_term:
                search_query = Q(email__icontains=search_term)
                users = CustomUser.objects.filter(search_query).exclude(id=request.user.id)[:10]
                user_data = [{'id': user.id, 'email': user.email} for user in users]
                return JsonResponse(user_data, safe=False)
        return JsonResponse([], safe=False)
    except Exception as e:
        return JsonResponse({'error': 'An error occurred while processing the request'}, status=500)


#encoding=utf-8
from selenium import  webdriver
import  time

driver=webdriver.Firefox()
web_url="https://www.baidu.com"
driver.get(web_url)
time.sleep(3)
driver.maximize_window()

el_set=driver.find_element_by_link_text("设置")
el_set.click()

#进入搜索设置并点击
el_search=driver.find_element_by_css_selector(".setpref")
el_search.click()

#定位保存设置按钮
el_save=driver.find_element_by_css_selector(".prefpanelgo")
el_save.click()
time.sleep(3)
#进入警告框中并且点击确定接受
# driver.switch_to.alert.accept()

#进入警告框中并直接关闭警告框
driver.switch_to.alert.dismiss()


####################################################
# for each element in the list, return its distance
# from the 'next less element' (NLE). If an
# element does not have a NLE, return -1.
#
# uses a monotonous stack
###################################################
from typing import List

class NextLessElement:
    def calculate(self, nums: List[int]) -> List[int]:
        if not nums:
            return []

        right, monostack = [-1] * len(nums), []

        for i in range(len(nums)):
            """ check if the element pointed by the stack top
                is greater than the current element.

                if YES, then the current element is NLE of the
                element pointed by stack. Remove element from
                the stack and set its NLE

                how it differs from PLE?
                ------------------------
                PLE -> PLE of current element is set in the loop

                NLE -> NLE of element pointed by the stack is set in the loop
            """
            while monostack and nums[monostack[-1]] > nums[i]:
                x = monostack.pop()
                right[x] = i

            """ append current index to the stack """
            monostack.append(i)

        """ calculate the distance of each element from its NLE """
        for i in range(len(right)):
            right[i] = right[i] - i if right[i] != -1 else right[i]

        return right


#!/usr/bin/python3
"""Start link class to table in database """
from sys import argv
from sqlalchemy.orm import sessionmaker
from model_state import State, Base
from model_city import City
from sqlalchemy import (create_engine)

if __name__ == "__main__":
    engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format(
            argv[1],
            argv[2],
            argv[3]
        ),
        pool_pre_ping=True)
    Base.metadata.create_all(engine)

    Session = sessionmaker(bind=engine)
    session = Session()
    for city in session.query(City).join(State).order_by(City.id).all():
        state = session.query(State).filter(State.id == city.state_id).first()
        print("{}: ({}) {}".format(state.name, city.id, city.name))
    session.close()


#base tensorflow, openmm

import numpy as np
import mdtraj as md
import matplotlib.pyplot as plt
# import tensorflow as tf
import os

import torch
import torch.optim as optim
from torch.autograd import Variable

def SGD(diff_vec, eigenvecs, Print = False, PrintRound = 1000, Iter = 100000):
        """
        do SGD for given diff_vec and eigenvectors to find to optimal collective motion

        :param diff_vec: the differential vector of two conformations, shape N
        :param eigenvecs: the tICs that are used to compose the OCM, shape N * m
        :param Print: whether to print loss during fitting
        :param PrintRound: the interval of each two print
        :param Iter: iterations of the SGD
        :return vec_combine: the normalized OCM we get based on given tICs
        :return alpha: the weight on each tICs to compose the OCM
        :return loss: the final loss, aka the cosine distance of the diffvec and the OCM
        """
        N = eigenvecs.shape[1]
        diff_vec_var = Variable(torch.DoubleTensor(diff_vec), requires_grad = False)
        diff_vec_var = diff_vec_var / torch.linalg.norm(diff_vec_var)
        eigenvec_var = Variable(torch.DoubleTensor(eigenvecs), requires_grad = False)
        alpha_var = Variable(torch.rand(N, 1, dtype = torch.double), requires_grad = True)

        optimizer = optim.Adam([alpha_var], lr=10, betas=(0.9, 0.999), eps=1e-08, weight_decay=0, amsgrad=False)

        for i in range(Iter):
            optimizer.zero_grad()
            vec_combine = torch.matmul(eigenvec_var, alpha_var)
            loss = -torch.matmul(vec_combine.T, diff_vec_var) / torch.linalg.norm(vec_combine)
            if i % PrintRound == 0 and Print:
                print(loss.data)
            loss.backward()
            optimizer.step()
        alpha_var = alpha_var / torch.linalg.norm(vec_combine)
        alpha = alpha_var.detach().numpy()
        vec_combine = eigenvecs.dot(alpha)
        np.savetxt('./OCM.txt', vec_combine)
        print("loss=",loss.data.detach().numpy()[0])
        return vec_combine, alpha, loss.data.detach().numpy()[0]

'''
def SGD():
	data = np.loadtxt('./tICs.txt')
	f_pc1 = np.loadtxt('./PC1.txt')
	l = []
	f_tic = data[0:1500]
	n = len(f_tic)

	config = tf.ConfigProto()
	config.gpu_options.allow_growth = True
	config.allow_soft_placement = True
	config.log_device_placement = False
	sess = tf.Session(config=config)

	w0 = tf.Variable(tf.expand_dims(np.random.randn(n),0))
	tic = tf.constant(f_tic)
	pc1 = tf.expand_dims(f_pc1,0)
	vec = tf.matmul(w0,tic)

	LOSS = -(tf.reduce_sum(tf.multiply(vec,pc1))/tf.sqrt(tf.reduce_sum(tf.multiply(vec,vec))))

	X = tf.placeholder(tf.float32, shape=[None,None])
	#X2 = tf.placeholder(tf.float32,shape=[None])
	Y = tf.placeholder(tf.float32, shape=[None])
	tf.summary.scalar('loss_linear', LOSS)

	#Minimize
	learning_rate = tf.train.exponential_decay(0.2, 100000, 25000, 0.9, staircase=True)
	optimizer = tf.train.GradientDescentOptimizer(learning_rate)
	train = optimizer.minimize(LOSS)
	merged = tf.summary.merge_all()

	sess.run(tf.global_variables_initializer())
	#write=tf.summary.FileWriter('log',sess.graph)

	for step in range(100001):
	    loss_val, merg, W_val,_ = sess.run([LOSS,merged,w0,train],
	                                         feed_dict={X:f_tic,
	                                                    Y:f_pc1})

	l.append([n,loss_val, W_val])
	LOSS = loss_val
	assert LOSS < 0, 'Illegal combined vector with obtuse angle'
	weighted_tics = np.dot(W_val, f_tic)
	np.savetxt('./OCM.txt', weighted_tics)
'''

def proj(parm):
    data = np.loadtxt('./eq.txt')
    OCM = np.loadtxt('./OCM.txt')
    data = data - data[0]
    proj = np.dot(data, OCM)

    gap_low = np.percentile(proj, 5)
    gap_high = np.percentile(proj, 95)
    print(gap_low)
    print(gap_high)
    print(gap_high-gap_low)

    plt.scatter(range(len(data)), proj, s=10)
    plt.axhline(gap_high)
    plt.axhline(gap_low)
    plt.xlabel('frame')
    plt.ylabel('projection')
    plt.savefig('./proj_eq.png')
    plt.close()

    proj = proj.tolist()
    print(max(proj))
    print(min(proj))
    print(proj.index(max(proj)))
    print(proj.index(min(proj)))
    max_index = proj.index(max(proj))+1

    _load = md.load('./eq.dcd', top=parm)
    #_load.autoimage()
    _load.image_molecules()
    _load[max_index].save_amberrst7('./restart.rst7')

    return max(proj)-min(proj)
    #return gap_high-gap_low

parm1 = 'fixed.close.prmtop'
parm2 = 'fixed.open.prmtop'

'''
os.chdir('./close/')
SGD()
close_p = proj(parm=parm1)
print('projection of close:', close_p)
os.chdir('../open/')
SGD()
open_p = proj(parm=parm2)
print('projection of open', open_p)
'''

os.chdir('./close/')
diff_vec = np.loadtxt('./PC1.txt')
eigenvecs = np.loadtxt('./tICs.txt').T
SGD(diff_vec, eigenvecs)
close_p = proj(parm=parm1)
print('projection of close:', close_p)
os.chdir('../open/')
diff_vec = np.loadtxt('./PC1.txt')
eigenvecs = np.loadtxt('./tICs.txt').T
SGD(diff_vec, eigenvecs)
open_p = proj(parm=parm2)
print('projection of open', open_p)

os.chdir('../')
close_r  = md.load('./close/eq.rst7', top=parm1)
close_load_ca = md.load('./close/eq.rst7', top=parm1, atom_indices=close_r.topology.select('protein and name CA'))

open_r = md.load('./open/eq.rst7', top=parm2)
open_load_ca = md.load('./open/eq.rst7', top=parm2, atom_indices=open_r.topology.select('protein and name CA'))
close_load_ca.superpose(reference=open_load_ca, atom_indices=close_load_ca.topology.select('(resid 15 to 23 or resid 249 to 279 or resid 289 to 292 or resid 501 to 518 or resid 528 to 541 or resid 549 to 553) and name CA'))

RMSD = md.rmsd(close_load_ca, open_load_ca, 0)*10   #unit in Angstrom
print('RMSD: ',RMSD)

scale_s = 0.05*RMSD**2
scale_l = 0.09*RMSD**2
ss_close = 0.1*RMSD**2*close_p
ss_open = 0.1*RMSD**2*open_p
print('scaling factor: ', scale_s, scale_l)
print('step size: ', ss_close, ss_open)

import sampling
from sampling import run_bias
from sampling import run_eq

os.chdir('./close/')
run_bias(parm=parm1, crd='./restart.rst7', OCM_file='./OCM.txt', scaling=float(scale_s), ss=ss_close)

os.chdir('../open/')
run_bias(parm=parm2, crd='./restart.rst7', OCM_file='./OCM.txt', scaling=float(scale_s), ss=ss_open)

def load_bias1(parm, ss):
	ref = md.load('restart.rst7', top=parm)
	ca = ref.topology.select('protein and name CA')
	ref = md.load('restart.rst7', top=parm, atom_indices=ca)
	load = md.load('bias.dcd', top=parm, atom_indices=ca)
	load.superpose(reference=ref, atom_indices=ref.topology.select('(resid 15 to 23 or resid 249 to 279 or resid 289 to 292 or resid 501 to 518 or resid 528 to 541 or resid 549 to 553) and name CA'))
	data = load.xyz
	x,y,z = data.shape
	data = data.reshape(x, y*z)

	vec = np.loadtxt('./OCM.txt')
	xyz = ref.xyz[0].reshape(len(ca)*3,)
	target = xyz + ss*vec
	print(target.shape)
	data = data - target
	data = np.sqrt(np.dot(data, np.transpose(data)))
	dis = [data[i,i] for i in range(len(data))]
	sel = dis.index(min(dis))
	print('bias select: ', sel)

	_load = md.load('bias.dcd', top=parm)
	_load[sel].save_amberrst7('./bias_select.rst7')

'''
def load_bias2(parm, ss):
	ref = md.load('restart.rst7', top=parm)
	ca = ref.topology.select('protein and name CA')
	ref = md.load('restart.rst7', top=parm, atom_indices=ca)
	load = md.load('bias.dcd', top=parm, atom_indices=ca)
	load.superpose(reference=ref)
	data = load.xyz
	x,y,z = data.shape
	data = data.reshape(x, y*z)

	vec = np.loadtxt('./OCM.txt')
	xyz = ref.xyz[0].reshape(len(ca)*3,)
	data = data[:,i] - data[0,i]
	proj = np.dot(data, vec)

	dis = proj.tolist()
	sel = dis.index(min(dis))

	_load = md.load('bias.dcd', top=parm)
	_load[sel].save_amberrst7('./bias_select.rst7')
'''

def load_bias3(parm):
	_load = md.load('bias.dcd', top=parm)
	_load[-1].save_amberrst7('./bias_select.rst7')

os.chdir('../close/')
#_load = md.load('bias.dcd', top=parm1)
#_load[-1].save_amberrst7('./bias_select.rst7')
#load_bias1(parm=parm1, ss=close_p)
load_bias3(parm=parm1)

os.chdir('../open/')
#_load = md.load('bias.dcd', top=parm2)
#_load[-1].save_amberrst7('./bias_select.rst7')
#load_bias1(parm=parm2, ss=open_p)
load_bias3(parm=parm2)


from sqlalchemy import engine_from_config
from sqlalchemy.orm import sessionmaker, scoped_session

from audit_sugar_utm.settings_private import config_sqlalchemy_crm

engine_crm = engine_from_config(
    config_sqlalchemy_crm,
    prefix='db.',
    echo=True,
    pool_recycle=600,
    pool_pre_ping=True
)

# У нас только права на чтение, так что нет смысла заморачиваться с commit и
# rollback, потому используем опцию autocommit=True
session_factory_crm = sessionmaker(bind=engine_crm, autocommit=True)
Session_crm = scoped_session(session_factory_crm)
session_crm = Session_crm()


"""Unit tests for the main CLI."""
import io
import unittest

from tltp.cli import _main

class TestGetArgs(unittest.TestCase):

    def test_windows_flag_file(self):
        flags = io.StringIO('-L 20\r\n--noshow\r\n-x\r\n')
        args = _main.get_arguments(['foo'], flags)
        self.assertEqual(args.show, False)
        self.assertEqual(args.size, 20)

if __name__ == '__main__':
    unittest.main()