#! /usr/bin/env python3

__all__ = [
    'track_and_calc_colors'
]

from typing import List, Optional, Tuple

import cv2
import numpy as np

from corners import (CornerStorage, FrameCorners)
from data3d import CameraParameters, PointCloud, Pose
import frameseq
from _camtrack import (
    PointCloudBuilder,
    create_cli,
    calc_point_cloud_colors,
    to_opencv_camera_mat3x3,
    view_mat3x4_to_pose,
    triangulate_correspondences,
    TriangulationParameters,
    build_correspondences,
    pose_to_view_mat3x4,
    calc_inlier_indices,
    rodrigues_and_translation_to_view_mat3x4,
    eye3x4,
    check_baseline,
    get_baseline,
    _remove_correspondences_with_ids)

class TrackingError(Exception):
    pass

class CameraTracker:
    def __init__(self, intrinsic_mat, corner_storage, known_view_1, known_view_2):
        self.intrinsic_mat = intrinsic_mat
        self.corner_storage = corner_storage
        frame_num_1 = known_view_1[0]
        frame_num_2 = known_view_2[0]
        self.pc_builder = PointCloudBuilder()
        view_matr_1 = pose_to_view_mat3x4(known_view_1[1])
        view_matr_2 = pose_to_view_mat3x4(known_view_2[1])

        self.tracked_views = [eye3x4()] * len(self.corner_storage)
        self.tracked_views[frame_num_1] = view_matr_1
        self.tracked_views[frame_num_2] = view_matr_2
        self.initial_baseline = get_baseline(view_matr_1, view_matr_2)
        print("initial baseline =", self.initial_baseline)

        self._add_points_from_frame(frame_num_1, frame_num_2, initial_triangulation=True)
        self.min_init_frame = min(frame_num_1, frame_num_2)
        self.max_init_frame = max(frame_num_1, frame_num_2)
        self.outliers = set()

    def _get_pos(self, frame_number) -> np.ndarray:
        corners = self.corner_storage[frame_number]
        _, pts_ids, corners_ids = np.intersect1d(self.pc_builder.ids, corners.ids, assume_unique=True,
                                                 return_indices=True)

        common_pts_3d = self.pc_builder.points[pts_ids]
        common_ids = self.pc_builder.ids[pts_ids]
        common_corners = corners.points[corners_ids]
        inlier_mask = np.zeros_like(common_pts_3d, dtype=np.bool)
        inlier_counter = 0
        for common_id, mask_elem in zip(common_ids[:, 0], inlier_mask):
            if common_id not in self.outliers:
                mask_elem[:] = True
                inlier_counter += 1
        if inlier_counter > 7:
            common_pts_3d = common_pts_3d[inlier_mask].reshape(-1, 3)
            common_ids = common_ids[inlier_mask[:, :1]].reshape(-1, 1)
            common_corners = common_corners[inlier_mask[:, :2]].reshape(-1, 2)
        max_reproj_error = 3.0
        if len(common_pts_3d) < 4:
            raise TrackingError('Not enough points to solve RANSAC on frame ' + str(frame_number))
        _, rot_vec, tr_vec, inliers = cv2.solvePnPRansac(common_pts_3d,
                                                         common_corners,
                                                         self.intrinsic_mat,
                                                         None,
                                                         reprojectionError=max_reproj_error,
                                                         flags=cv2.SOLVEPNP_EPNP)
        extrinsic_mat = rodrigues_and_translation_to_view_mat3x4(rot_vec, tr_vec)
        proj_matr = self.intrinsic_mat @ extrinsic_mat
        if inliers is None:
            raise TrackingError('Failed to solve PnP on frame' + str(frame_number))

        while len(inliers) < 8 and max_reproj_error < 50:
            max_reproj_error *= 1.2
            inliers = calc_inlier_indices(common_pts_3d, common_corners, proj_matr, max_reproj_error)
        inlier_pts = common_pts_3d[inliers]
        inlier_corners = common_corners[inliers]
        outlier_ids = np.setdiff1d(common_ids, common_ids[inliers], assume_unique=True)
        self.outliers.update(outlier_ids)
        if len(inliers) < 4:
            inlier_pts = common_pts_3d
            inlier_corners = common_corners
        print('Found position on', len(inlier_corners), 'inliers')
        _, rot_vec, tr_vec, inliers = cv2.solvePnPRansac(inlier_pts, inlier_corners, self.intrinsic_mat, None,
                                                         rot_vec, tr_vec, useExtrinsicGuess=True)
        return rodrigues_and_translation_to_view_mat3x4(rot_vec, tr_vec)

    def _add_points_from_frame(self,
                               frame_num_1: int,
                               frame_num_2: int,
                               initial_triangulation: bool = False) -> int:
        corners_1 = self.corner_storage[frame_num_1]
        corners_2 = self.corner_storage[frame_num_2]
        correspondences = build_correspondences(corners_1, corners_2, ids_to_remove=self.pc_builder.ids)
        if len(correspondences.ids) > 0:
            max_reproj_error = 1.0
            min_angle = 1.0
            view_1 = self.tracked_views[frame_num_1]
            view_2 = self.tracked_views[frame_num_2]
            triangulation_params = TriangulationParameters(max_reproj_error, min_angle, 0)
            pts_3d, triangulated_ids, med_cos = triangulate_correspondences(correspondences, view_1,
                                                                            view_2, self.intrinsic_mat,
                                                                            triangulation_params)
            if initial_triangulation:
                num_iter = 0
                while len(pts_3d) < 8 and len(correspondences.ids) > 7 and num_iter < 100:
                    max_reproj_error *= 1.2
                    min_angle *= 0.8
                    triangulation_params = TriangulationParameters(max_reproj_error, min_angle, 0)
                    pts_3d, triangulated_ids, med_cos = triangulate_correspondences(correspondences, view_1,
                                                                                view_2, self.intrinsic_mat,
                                                                                triangulation_params)
                    num_iter += 1

                if num_iter >= 100 and len(pts_3d) < 4:
                    raise TrackingError('Failed to triangulate enough points')
            self.pc_builder.add_points(triangulated_ids, pts_3d)

            return len(pts_3d)
        elif initial_triangulation:
            raise TrackingError('Not found correspondences on image pair')
        else:
            return 0

    def track(self):
        curr_frame = self.min_init_frame + 1
        seq_size = len(self.corner_storage)
        for _ in range(2, seq_size):
            if curr_frame == self.max_init_frame:
                curr_frame += 1
            if curr_frame >= seq_size:
                curr_frame = self.min_init_frame - 1
                self.outliers = set()
            print('Frame', curr_frame)
            try:
                self.tracked_views[curr_frame] = self._get_pos(curr_frame)
            except TrackingError as error:
                print(error)
                print('Stopping tracking')
                break
            prev_curr_diff = 5
            num_pairs = 0
            num_added = 0
            while num_pairs < 5:
                prev_frame = \
                    curr_frame - prev_curr_diff if curr_frame > self.min_init_frame else curr_frame + prev_curr_diff
                prev_curr_diff += 1
                if prev_frame < seq_size and (self.min_init_frame <= prev_frame or curr_frame < self.min_init_frame):
                    if check_baseline(self.tracked_views[prev_frame], self.tracked_views[curr_frame],
                                      self.initial_baseline * 0.15):
                        num_added += self._add_points_from_frame(prev_frame, curr_frame)
                        num_pairs += 1
                else:
                    break
            print('Added', num_added, 'points')
            print('Point cloud size = ', len(self.pc_builder.ids))
            if curr_frame > self.min_init_frame:
                curr_frame += 1
            else:
                curr_frame -= 1

        return self.tracked_views, self.pc_builder

def _test_frame_pair(intrinsic_mat: np.ndarray,
                     corners_1: FrameCorners,
                     corners_2: FrameCorners,
                     param_koeff: float = 1) -> Tuple[int, Optional[Pose]]:
    correspondences = build_correspondences(corners_1, corners_2)
    if len(correspondences.ids) < 6:
        return 0, None
    points2d_1 = correspondences.points_1
    points2d_2 = correspondences.points_2

    essential, essential_inliers = cv2.findEssentialMat(points2d_1, points2d_2, intrinsic_mat, threshold=param_koeff)
    homography, homography_inliers = cv2.findHomography(points2d_1, points2d_2, method=cv2.RANSAC)
    if len(np.where(homography_inliers > 0)[0]) > len(np.where(essential_inliers > 0)[0]):
        return 0, None
    if essential.shape != (3, 3):
        return 0, None
    num_passed, rot, t, mask = cv2.recoverPose(essential, points2d_1, points2d_2,
                                               intrinsic_mat, mask=essential_inliers)

    outlier_ids = np.array(
        [pt_id for pt_id, mask_elem in zip(correspondences.ids, mask) if mask_elem[0] == 0],
        dtype=correspondences.ids.dtype)
    inlier_correspondences = _remove_correspondences_with_ids(correspondences, outlier_ids)
    if len(inlier_correspondences.ids) < 4:
        return 0, None
    view_matr_1 = eye3x4()
    view_matr_2 = np.hstack((rot, t))
    triangulation_params = TriangulationParameters(param_koeff, 1.0 / param_koeff, 0.0)
    pts_3d, trianulated_ids, med_cos = triangulate_correspondences(inlier_correspondences, view_matr_1, view_matr_2,
                                                                   intrinsic_mat, triangulation_params)
    if len(pts_3d) < 4:
        return 0, None
    print(len(inlier_correspondences.ids), len(pts_3d))
    return len(pts_3d), view_mat3x4_to_pose(view_matr_2)

def _find_pos_pair(intrinsic_mat: np.ndarray,
                   corner_storage: CornerStorage,
                   param_koeff: float = 1) -> Tuple[Tuple[int, Pose], Tuple[int, Pose]]:
    best_num_pts = 0
    best_frame = 1
    best_pose = None
    for frame_number in range(1, len(corner_storage)):
        print(frame_number)
        num_pts, pose = _test_frame_pair(intrinsic_mat, corner_storage[0], corner_storage[frame_number])
        if num_pts > best_num_pts:
            best_num_pts = num_pts
            best_pose = pose
            best_frame = frame_number
    if best_pose is None:
        if param_koeff < 4:
            return _find_pos_pair(intrinsic_mat, corner_storage, param_koeff * 2)
        raise TrackingError("Failed to initialize tracking")
    return (0, view_mat3x4_to_pose(eye3x4())), (best_frame, best_pose)

def track_and_calc_colors(camera_parameters: CameraParameters,
                          corner_storage: CornerStorage,
                          frame_sequence_path: str,
                          known_view_1: Optional[Tuple[int, Pose]] = None,
                          known_view_2: Optional[Tuple[int, Pose]] = None) \
        -> Tuple[List[Pose], PointCloud]:
    rgb_sequence = frameseq.read_rgb_f32(frame_sequence_path)
    intrinsic_mat = to_opencv_camera_mat3x3(
        camera_parameters,
        rgb_sequence[0].shape[0]
    )
    view_mats, point_cloud_builder = [eye3x4()] * len(corner_storage), PointCloudBuilder()

    try:
        known_view_1, known_view_2 = _find_pos_pair(intrinsic_mat, corner_storage)
        tracker = CameraTracker(intrinsic_mat, corner_storage, known_view_1, known_view_2)
        view_mats, point_cloud_builder = tracker.track()
    except TrackingError as error:
        print(error)
        print('Poses and point cloud are not calculated')

    calc_point_cloud_colors(
        point_cloud_builder,
        rgb_sequence,
        view_mats,
        intrinsic_mat,
        corner_storage,
        5.0
    )
    point_cloud = point_cloud_builder.build_point_cloud()
    poses = list(map(view_mat3x4_to_pose, view_mats))
    return poses, point_cloud

if __name__ == '__main__':
    # pylint:disable=no-value-for-parameter
    create_cli(track_and_calc_colors)()


#=========================================================================
# IntMulFL_test
#=========================================================================

import pytest
import random

random.seed(0xdeadbeef)

from pymtl      import *
from pclib.test import mk_test_case_table, run_sim
from pclib.test import TestSource, TestSink

from lab1_imul.IntMulFL   import IntMulFL

#-------------------------------------------------------------------------
# TestHarness
#-------------------------------------------------------------------------

class TestHarness (Model):

  def __init__( s, imul, src_msgs, sink_msgs,
                src_delay, sink_delay,
                dump_vcd=False, test_verilog=False ):

    # Instantiate models

    s.src  = TestSource ( Bits(64), src_msgs,  src_delay  )
    s.imul = imul
    s.sink = TestSink   ( Bits(32), sink_msgs, sink_delay )

    # Dump VCD

    if dump_vcd:
      s.imul.vcd_file = dump_vcd

    # Translation

    if test_verilog:
      s.imul = TranslationTool( s.imul )

    # Connect

    s.connect( s.src.out,  s.imul.req  )
    s.connect( s.imul.resp, s.sink.in_ )

  def done( s ):
    return s.src.done and s.sink.done

  def line_trace( s ):
    return s.src.line_trace()  + " > " + \
           s.imul.line_trace()  + " > " + \
           s.sink.line_trace()

#-------------------------------------------------------------------------
# mk_req_msg
#-------------------------------------------------------------------------

def req( a, b ):
  msg = Bits( 64 )
  msg[32:64] = Bits( 32, a, trunc=True )
  msg[ 0:32] = Bits( 32, b, trunc=True )
  return msg

def resp( a ):
  return Bits( 32, a, trunc=True )

#----------------------------------------------------------------------
# Test Case: small positive * positive
#----------------------------------------------------------------------

small_pos_pos_msgs = [
  req(  2,  3 ), resp(   6 ),
  req(  4,  5 ), resp(  20 ),
  req(  3,  4 ), resp(  12 ),
  req( 10, 13 ), resp( 130 ),
  req(  8,  7 ), resp(  56 ),
  req( 12, 15 ), resp( 180 ),
]

# ''' LAB TASK '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Define additional lists of request/response messages to create
# additional directed and random test cases.
# ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

# Parsity_test with two parsity numbers
def random_index(rate):
  start = 0
  index = 0
  randnum = random.randint(1, sum(rate))
  for index, scope in enumerate(rate):
    start += scope
    if randnum <= start:
      break
  return index

arr = [0,1]
rate = [95,5]

def random_parsity(arr, rate):
  lst = []
  for i in range(32):
    k = arr[random_index(rate)]
    lst.append(k)
  mul = 31
  sum = 0
  for ele in lst:
    sum += ele * (2 ** mul)
    mul = mul -1
  return sum

random_paristy_msgs = []

for i in xrange(100):
  a = random_parsity(arr, rate)
  b = random_parsity(arr, rate)
  print(a, b)
  random_paristy_msgs.extend([ req( a, b ), resp( a * b ) ])

# Density_test with two density numbers
arr = [0,1]
rate = [5, 95]

random_density_msgs = []

for i in xrange(100):
  a = random_parsity(arr, rate)
  b = random_parsity(arr, rate)
  print(a, b)
  random_density_msgs.extend([ req( a, b ), resp( a * b ) ])

#-------------------------------------------------------------------------
# Test Case Table
#-------------------------------------------------------------------------

test_case_table = mk_test_case_table([
  (                      "msgs                 src_delay sink_delay"),
  [ "small_pos_pos",     small_pos_pos_msgs,   0,        0          ],
  [ "parsity number",    random_paristy_msgs,  0,        0          ],
  [ "density number",    random_density_msgs,  0,        0          ],

  # ''' LAB TASK '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  # Add more rows to the test case table to leverage the additional lists
  # of request/response messages defined above, but also to test
  # different source/sink random delays.
  # ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

])

#-------------------------------------------------------------------------
# Test cases
#-------------------------------------------------------------------------

@pytest.mark.parametrize( **test_case_table )
def test( test_params, dump_vcd ):
  run_sim( TestHarness( IntMulFL(),
                        test_params.msgs[::2], test_params.msgs[1::2],
                        test_params.src_delay, test_params.sink_delay ),
           dump_vcd )


import os, random

# Directories
MAIN_DIR       = os.path.dirname(os.path.realpath(__file__))
SCENES         = MAIN_DIR + '/scenes'
VREP_DIR       = MAIN_DIR + '/vrep'

sim_port       = random.randint(19000, 20000)
local_ip       = '127.0.0.1'

# Simulation parameters
fps = 30.               # Frames per second
simulation_steps = 5000
image_size = 800
map_size   = 40
steps_slam = 10 # Perform SLAM every n steps


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

from . import log
from flask import request,render_template
from ..models import Log_host_monitor
from flask.ext.login import login_required

#客户端心跳日志
@log.route('/hosts')
@log.route('/hosts/<int:num>')
@login_required
def heartbeat_log(num = 1):
    site_url = '/log/hosts'

    project_list = [u'id',u'客户端ip',u'时间',u'状态']
    search_select_list = {'all':u'全部',
                          "200":u'在线',
                          "403":u'拒绝',
                          "404":u'ip 不存在',
                          "500":u'离线'}
    search_value = ""
    select_value = "all"
    if 'search' in request.args  and \
            'select' in request.args and \
            request.args.get('select'):
        select_value = request.args.get('select')
        search_value = request.args.get('search')
        if select_value == 'all':
            pagination = Log_host_monitor.query.\
                order_by(-Log_host_monitor.id).\
                filter(Log_host_monitor.client_ip.ilike('%'+search_value+'%'))\
                .order_by(-Log_host_monitor.id)\
                .paginate(num, 20, True)
        else:
            pagination = Log_host_monitor.query.\
                order_by(-Log_host_monitor.id).\
                filter(Log_host_monitor.client_ip.ilike('%'+search_value+'%'),
                       Log_host_monitor.status.ilike('%'+select_value+'%'))\
                .order_by(-Log_host_monitor.id)\
                .paginate(num, 20, True)

    else:
        pagination = Log_host_monitor.query.\
            order_by(-Log_host_monitor.id).\
            paginate(num, 20, True)
    posts = pagination.items
    return render_template('log/hosts.html',
                            site_url=site_url,
                            project_list=project_list,
                            search_select_list=search_select_list,
                            posts=posts,
                            pagination=pagination,
                            search_value=search_value,
                            select_value=select_value)


# David Dewhurst

from __future__ import print_function
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

def moving_average(array, order):
	"""moving_average(array, order):
	calculates a moving average of array
	to the specified order

    Args:
        array (1-d array): array of data of which you want the moving average
        order (int): the order of which you want a moving average

    Returns:
        ret: The moving average of the array calculated to the correct order

    """
	ret = np.cumsum(array, dtype=float)
	ret[order:] = ret[order:] - ret[:-order]
	return ret[order - 1:] / order

def historical_volatility(array, num_days=252):
	"""historical volatility(array, num_days=252):
	calculates the historical volatility of a security

	Args:
		array (1-d array): an array of security prices
		num_days (int, optional): the number of days over which you want to
		calculuate the historical volatility. Default is 252 (252 trading
		days in a year) but 360 and 365 are also common

	Returns:
		the historical volatility of the security

	"""
	log_daily_returns = [np.log(array[i] / array[i-1]) for i in range(1,
		len(array))]
	return np.sqrt(num_days * np.var(log_daily_returns))

class time_series_viz(object):
	"""time_series_viz(object)
	A time series visualization class that provides limited methods by which
	to visualize multiple time series of similar time scale on the same axis

	Initialization:

		arrays (numpy array): should be a timesteps x number_of_series nd
		array. Use numpy.column_stack((array1, array2, array3)) to create

	Methods:

		standalone_display
		Displays visualization in a standalone manner without assuming
		other axes are present.

		Args:
			labels (array, optional): ['x-axis', 'y-axis']
			save (boolean, optional): True saves the figure, False does not
			save. Default is not to save the figure and instead show it on
			screen
			savename (string, optional): string to save the plot as.
			Should include the file type, e.g., 'picture.png'

		append_to_plot
		Appends the visualization to an already-created figure

		Args:
			axes (matplotlib.pyplot axes object): a set of axes to which to
			append the plot
			labels (array, optional): ['x-axis', 'y-axis']
	"""

	def __init__(self, arrays):
		self.ts = arrays

	def standalone_display(self, labels=None, save=False, savename=None):
		fig = plt.figure()
		ax = fig.add_subplot(1, 1, 1)
		try:
			ax.hold(True)
			[ax.plot(range(self.ts.shape[0]),
			 self.ts[:, i]) for i in range(self.ts.shape[-1])]
		except IndexError:
			ax.plot(range(self.ts.shape[0]), self.ts,
				'k-')

		ax.set_xticks([])

		if labels is not None:
			try:
				ax.set_xlabel(labels[0])
				ax.set_ylabel(labels[1])
			except:
				raise(IOError, 'labels specified incorrectly')

		if save is False:
			plt.show()
		elif save is True:
			assert(type(savename) is str)
			plt.savefig(savename)

	def append_to_plot(self, axes, labels=None):
		try:
			axes.hold(True)
			[axes.plot(range(self.ts.shape[0]),
			 self.ts[:, i]) for i in range(self.ts.shape[-1])]
		except IndexError:
			axes.plot(range(self.ts.shape[0]), self.ts,
				'k-')

		if labels is not None:
			try:
				axes.set_xlabel(labels[0])
				axes.set_ylabel(labels[1])
			except:
				raise(IOError, 'labels specified incorrectly')

def high_low_strike_prices(array, prop=0.1):
	""" calculates a "high" and "low" strike price for an option
	on an asset for heuristic use in genetic programming

	Args:
		array (np array): array of security price data
		prop (float, optional): proportion up (down) the high (low) price
		should be

	Returns:
		high (np array): array of high strike prices
		low (np array): array of low strike prices
	"""
	high = [(1. + prop) * price for price in array]
	low = [(1. - prop) * price for price in array]
	return np.asarray(high), np.asarray(low)

def jensen_shannon_divergence(p, q):
	""" calculates the jensen-shannon divergence of two distributions

	Args:
		p (array): probability distribution
		q (array): probability distribution

	Returns:
		jensen shannon divergence of the distributions
	"""
	p = np.asarray(p)
	q = np.asarray(q)
	m = 0.5 * (p + q)
	return 0.5 * (stats.entropy(p, m) + stats.entropy(q, m))


import ply.lex as lex

# list of token names
tokens = (
	'SUBJ',
	'CODE',
	'LPAREN',
	'RPAREN',
	'MIN',
	'NUMBER',
	'PERMISSION',
	'OR',
	'FROM',
	'OF',
	'CREDIT',
	'AND',
	'PERIOD',
	'PREREQ',
)

# regular expresion rules for simple tokens
t_SUBJ = r'[A-Z]{4}'
t_CODE = r'[0-9][FP][0-9][0-9]'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_OR = r'\sor\s'
t_FROM = r'\sfrom\s'
t_OF = r'\sof\s'
t_CREDIT = r'\scredits?'
t_AND = r'\sand\s'
t_PERIOD = r'\.'

def t_MIN(t):
	r'minimum\s[0-9][0-9]\spercent'
	t.value = int(t.value.split(' ')[1])
	return t

def t_PREREQ(t):
	r'Prerequisite\(s\)\: '

def t_PERMISSION(t):
	r'permission\sof\sthe\s[A-Za-z]+'
	t.value = t.value.split(' ')[3]
	return t

def t_NUMBER(t):
	r'one-half|one|two|three|four|five|six'
	t.value = ['one-half','one','two','three','four','five','six'].index(t.value)
	return t

# a string containing ignored characters
t_ignore = ','

def t_error(t):
	t.lexer.skip(1)

lexer = lex.lex()

#data = 'Prerequisite(s): MATH 1P12 or permission of the instructor.'
#print(data)

#lexer.input(data)

#for tok in lexer:
#	print(tok)


from django.conf.urls import patterns, include, url
#from django.contrib.auth.views import login, logout

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

from django_test.forms import ContactForm1, ContactForm2, ContactForm3
from django_test.views import ContactWizard

import settings

urlpatterns = patterns('',
    (r'^articles/', include('article.urls')),
    (r'^accounts/', include('userprofile.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
    url(r'^facebook/', include('django_facebook.urls')),

    # user auth urls
    url(r'^accounts/login/$',  'django_test.views.login'),
    url(r'^accounts/auth/$',  'django_test.views.auth_view'),
    url(r'^accounts/logout/$', 'django_test.views.logout'),
    url(r'^accounts/loggedin/$', 'django_test.views.loggedin'),
    url(r'^accounts/invalid/$', 'django_test.views.invalid_login'),
    url(r'^accounts/register/$', 'django_test.views.register_user'),
    url(r'^accounts/register_success/$', 'django_test.views.register_success'),
    url(r'^contact/$', ContactWizard.as_view([ContactForm1, ContactForm2, ContactForm3])),
    url(r'^search/', include('haystack.urls')),
    url(r'^celery_test/', 'django_test.views.start_celery_task'),
    url(r'^celery_progress/', 'django_test.views.monitor_celery_task'),
)

if not settings.DEBUG:
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns

    urlpatterns += staticfiles_urlpatterns()


# Creating Pokemon class
class Pokemon:

    # Initialization
    def __init__(self, name, level, type, base_damage, max_health):
        self.name = name
        self.level = level
        type = type.lower()
        if type == "fire":
            self.type = 0
        elif type == "water":
            self.type = 1
        elif type == "grass":
            self.type = 2
        else:
            self.type = 3
        self.health = max_health
        self.base_damage = base_damage
        self.max_health = max_health
        self.is_knocked_out = False
        for i in range(2, level):
            self.max_health += 25
            self.base_damage += 10
        self.health = self.max_health
        self.owned = 0

        print(self.name + " now has " + str(self.max_health) + " HP")

    # Knock Down Pokemon
    def knock_out(self):
        if self.health <= 0:
            self.is_knocked_out = True
            print(self.name + " has been knocked out")

    # Damage Pokemon
    def lose_health(self, health):
        if self.health <= health:
            if not self.is_knocked_out:
                print(self.name + " has lost " + str(self.health) + " HP")
                self.health = 0
                self.knock_out()
            else:
                print(self.name + " is already knocked out")
        else:
            if not self.is_knocked_out:
                self.health -= health
                print(self.name + " has lost " + str(health) + " HP")
                print(self.name + " now has " + str(self.health) + " HP")
            else:
                print(self.name + "is already knocked out")

    # Heal Pokemon
    def gain_health(self, health):
        if self.health + health <= self.max_health:
            if not self.is_knocked_out:
                self.health += health
                print(self.name + " has gained " + str(health) + " HP")
                print(self.name + " now has " + str(self.health) + " HP")
            else:
                print(self.name + " is knocked out, revive him first")
        elif self.health == self.max_health:
            if not self.is_knocked_out:
                print(self.name + " is at max HP")
            else:
                print(self.name + " is knocked out, revive him first")
        elif self.health + health >= self.max_health:
            if not self.is_knocked_out:
                print(self.name + " has gained " + str(self.max_health - self.health) + " HP")
                self.health = self.max_health
                print(self.name + " now has " + str(self.health) + " HP")
            else:
                print(self.name + " is knocked out, revive him first")

    # Revive Pokemon
    def pokemon_revive(self):
        if not self.is_knocked_out:
            print(self.name + " is already alive")
        else:
            self.health = self.max_health
            self.is_knocked_out = False
            print(self.name + " has been revived")
            print(self.name + " now has " + str(self.health) + " HP")

    # Attack Another Pokemon
    def pokemon_attack(self, pokemon):
        if not self.is_knocked_out and not pokemon.is_knocked_out:
            print(self.name + " has attacked " + pokemon.name)
            if self.type == 0 and pokemon.type == 2:
                print("Bonus damage gained")
                pokemon.lose_health(2 * self.base_damage)
            elif self.type == 1 and pokemon.type == 0:
                print("Bonus damage gained")
                pokemon.lose_health(2 * self.base_damage)
            else:
                pokemon.lose_health(self.base_damage)
        elif self.is_knocked_out:
            print(self.name + " is knocked out")
        elif pokemon.is_knocked_out:
            print(pokemon.name + " is already knocked out")

# Creating Trainer Class
class Trainer:

    # Initialization
    def __init__(self, name, potions, revive_potions):
        self.pokemon_set = []
        self.name = name
        self.potions = potions
        self.active_pokemon = None
        self.revive_potions = revive_potions

    # Add Pokemon to trainer's set
    def add_pokemon(self, pokemon):
        if len(self.pokemon_set) <= 2 and pokemon not in self.pokemon_set and pokemon.owned == 0:
            self.pokemon_set.append(pokemon)
            pokemon.owned = 1
        elif len(self.pokemon_set) <= 2 and pokemon in self.pokemon_set:
            print("{} already has {}".format(self.name, pokemon.name))
        elif not len(self.pokemon_set) <= 2:
            print(self.name + " has max number of Pokemons")
        elif pokemon.owned != 0:
            print(pokemon.name + " is owned by another trainer")

    # Heals Pokemon
    def heal_pokemon(self):
        if self.active_pokemon is not None:
            if self.potions >= 1 and self.active_pokemon.health != self.active_pokemon.max_health:
                print("{} has healed his Pokemon".format(self.name))
                self.active_pokemon.gain_health(100)
                self.potions -= 1
            elif self.potions != 0 and not self.active_pokemon.health != self.active_pokemon.max_health:
                self.active_pokemon.gain_health(100)
            else:
                print(self.name + " has no more healing potions")
        else:
            print(self.name + " has no active pokemon")

    # Shows Pokemons
    def show_pokemons(self):
        if len(self.pokemon_set) == 1:
            print("{} currently has {}".format(self.name, self.pokemon_set[0].name))
        elif len(self.pokemon_set) == 2:
            print("{} currently has {} and {}".format(self.name, self.pokemon_set[0].name, self.pokemon_set[1].name))
        elif len(self.pokemon_set) == 3:
            print("{} currently has {}, {} and {}".format(self.name, self.pokemon_set[0].name, self.pokemon_set[1].name,
                                                          self.pokemon_set[2].name))
        else:
            print("{} currently has no pokemons".format(self.name))

    # Sets the active pokemon
    def set_active_pokemon(self, pokemon):
        if pokemon in self.pokemon_set:
            self.active_pokemon = pokemon
            print(self.name + " has set " + self.active_pokemon.name + " as an active pokemon")
        else:
            print(self.name + " doesn't have this pokemon")

    # Attacks another trainer's pokemon
    def attack(self, trainer):
        if self.active_pokemon is not None and trainer.active_pokemon is not None:
            print(self.name + " is attempting to attack " + trainer.name)
            self.active_pokemon.pokemon_attack(trainer.active_pokemon)
        elif self.active_pokemon is None:
            print(self.name + " has no active Pokemon")
        elif trainer.active_pokemon is None:
            print(trainer.name + " has no active Pokemon")

    # Revive a knocked out pokemon
    def revive_pokemon(self):
        if self.revive_potions >= 1:
            if self.active_pokemon is not None:
                print(self.name + " is attempting to revive " + self.active_pokemon.name)
                self.active_pokemon.pokemon_revive()
                self.revive_potions -= 1
            else:
                print(self.name + " is attempting to revive " + self.active_pokemon.name)
                print(self.name + " has no active pokemon")
        else:
            print(self.name + " has no revive potions")

# Creating Pokemon Object
charmander = Pokemon("Charmander", 12, "Fire", 20, 108)
bulbasaur = Pokemon("Bulbasaur", 13, "Grass", 25, 110)
wartortle = Pokemon("Wartortle", 14, "Water", 19, 115)
pikachu = Pokemon("Pikachu", 17, "Electric", 21, 112)
mewtwo = Pokemon("Mewtwo", 15, "Psychic", 18, 109)
suicune = Pokemon("Suicune", 16, "Suicune", 20, 114)

# Creating Trainer Objects
ethan = Trainer("Ethan", 2, 3)
nate = Trainer("Nate", 2, 2)

############################ PROJECT DONE ############################
# Deigned completely by Mohamed Emad


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

# File    : file.py
# Exercise: Sudoku solver
# Author  : Jonas Solsvik
# Email   : jonas.solsvik@gmail.com
# Date    : 16.02.17
# License : Free as in pizza

import pickle

toggle_bits = 0x1ff
top_bit     = 0x200

single_bits = {
    0 : 0x0,
    1 : 0x1,
    2 : 0x2,
    3 : 0x4,
    4 : 0x8,
    5 : 0x10,
    6 : 0x20,
    7 : 0x40,
    8 : 0x80,
    9 : 0x100,
}

#
# Filepaths
#
filepath = "bit.hash"
filepath_intmap = "int.map"
filepath_bitmap = "bit.bmap"
filepath_bitgrid = "bit.grid"
filepath_solution = "solution.map"

#
# Bithash
#

def dump_bithash(bithash):
    with open(filepath, 'wb') as file:
        pickle.dump(bithash, file, protocol=2)

def load_bithash():
    with open(filepath, 'rb') as file:
        bithash = pickle.load(file)
    return bithash

#
# Bitmap
#

def load_bitmap(path=""):
    with open(filepath_bitmap, 'rb') as bitfile:
        bitmap = pickle.load(bitfile)

    return bitmap

def dump_bitmap(bitmap):
    with open(filepath_bitmap, 'wb') as bitfile:
        pickle.dump(bitmap, bitfile, protocol=2)

def save_solution(bitmap):

    bithash = load_bithash()
    with open(filepath_solution, 'a') as savefile:
        for r in range(9):
            for c in range(9):
                savefile.write(str(bithash[bitmap[r][c]][0])+ " ")

            savefile.write("\n")
        savefile.write("\n")

def convert_intmap_to_bitmap(file=False,path=filepath_intmap):

    bitmap = []
    intmap = []

    with open(path) as intfile:
        file_contents = intfile.readlines()

    for i in range(9):
        line = file_contents[i].rstrip()
        line = line.split(" ")
        intmap.append([int(line[i]) for i in range(9)])

    bitmap = [[((single_bits[intmap[i][j]] | top_bit) ^ toggle_bits) if single_bits[intmap[i][j]] > 0 else (0x0 | top_bit) for j in range(9)] for i in range(9) ]

    if file:
        with open(filepath_bitmap, 'wb') as bitfile:
            pickle.dump(bitmap, bitfile, protocol=2)

    return bitmap

def load_all_grids():

    with open(filepath_bitgrid, "r") as gridfile:
        box = gridfile.readline().rstrip()
        col = gridfile.readline().rstrip()
        row = gridfile.readline().rstrip()

    box = box.split(" ")
    col = col.split(" ")
    row = row.split(" ")

    box = [int(box[i]) for i in range(9)]
    col = [int(col[i]) for i in range(9)]
    row = [int(row[i]) for i in range(9)]

    return box, col, row


def partition(arr,s,e):
    pivot=arr[e]
    i=s-1
    for j in range(s,e-1):
        if arr[j]<pivot:
            i+=1
            arr[i],arr[j]=arr[j],arr[i]
    arr[i+1],arr[e]=arr[e],arr[i+1]
    return i+1
def quicksort(arr,s,e):
    if s<e:
        pivot=partition(arr,s,e)
        quicksort(arr,s,pivot-1)
        quicksort(arr,pivot+1,e)
arr=list(map(int,input("Enter array elements : ").split()))
quicksort(arr,0,len(arr)-1)
print(arr)


import OpenSSL
import base64
import json
import hashlib

from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding

key = OpenSSL.crypto.PKey()
key.generate_key(type=OpenSSL.crypto.TYPE_RSA,bits=2048)
priv = key.to_cryptography_key()
pub = priv.public_key()

header = {
    "alg":"RS256",
    "jwk":  {
            "kty":"RSA",
            "kid":"bilbo.baggins@hobbiton.example",
            "use":"sig"#,
            # "n":"n4EPtAOCc9Alke...........rNP5zw",
            # "e":"AQAB"
            }
}

e = pub.public_numbers().e
n = pub.public_numbers().n

header["jwk"]["e"] = base64.urlsafe_b64encode((e).to_bytes((e).bit_length()//8+1,byteorder='big')).decode('utf8').rstrip("=")
header["jwk"]["n"] = base64.urlsafe_b64encode((n).to_bytes((n).bit_length()//8+1,byteorder='big')).decode('utf8').rstrip("=")
payload = "YWRtaW4"

str = base64.urlsafe_b64encode(bytes(json.dumps(header), encoding='utf8')).decode('utf8').rstrip("=")+"."+payload

sig = priv.sign(bytes(str, encoding='utf8'), algorithm=hashes.SHA256(), padding=padding.PKCS1v15())

print(str+"."+base64.urlsafe_b64encode(sig).decode('utf8').rstrip("="))


from numpy.lib.recfunctions import unstructured_to_structured as struct
from numpy.lib.recfunctions import structured_to_unstructured as unstruct
import numpy as np
import pricer
import utils
import query

np.seterr(divide='ignore')
np.warnings.filterwarnings('ignore')
base = 253

def surface(symbol,qdate,qtime=None,errors=False,post=True,loc=0,scale=None):
    q = f"select * from dbo.{symbol} where Date = convert(datetime,'{qdate}',103) order by Time,Tenor,Strike"
    df = query.get("Vols",q)
    if qtime is not None: df = df[df["Time"]==qtime]
    st = utils.pack(df)
    del df;

    #----------------------------------------------------------------------------------
    f = lambda LST: pricer.norm_weights(LST,loc,scale)
    wgt = utils.apply(f,1,st,["Time","Tenor"],["LogStrike"])

    sv = pricer.SSVI(errors)
    f = lambda LST,VAR,TNR,VOL: sv.calibrate(LST,VAR,TNR,VOL,wgt)

    if errors == True:
        prm = utils.apply(f,6,st,["Time"],["LogStrike","TotAtmfVar","Tenor","SmtVol"],diff=True,fill=False)
        errors = prm[-1]
        prm = np.asarray(prm[:-1]).T
    else:
        prm = utils.apply(f,5,st,["Time"],["LogStrike","TotAtmfVar","Tenor","SmtVol"],fill=False)

    #----------------------------------------------------------------------------------
    reduced = ["Time","Tenor","SpotMid","Forward","CumDivDays","Div","ImpBor",
               "Rate","CallH0","CallH1","CallKh","PutH0","PutH1","PutKh","TotAtmfVar"]
    st = utils.unique_nosort(st[reduced])

    prm = utils.restack(prm,st["TotAtmfVar"],tile=False)

    rho = sv.correlation(st["TotAtmfVar"], prm[:,0], prm[:,1], prm[:,2])
    phi = sv.power_law(st["TotAtmfVar"], prm[:,3], prm[:,4])
    atm,skw,krt = sv.raw2jw(st["Tenor"]/base,st["TotAtmfVar"],phi,rho)

    arr = unstruct(st)
    arr = np.column_stack([arr,atm,skw,krt,phi,rho])
    df = utils.unpack(arr,qdate,False,symbol)

    if post == True:
        query.post(df,"Params","main","replace")

    return df

# symbol = "JPM"
# qdate = "16/10/2020"
# qtime = 2040
# errors = True
# df = surface(symbol,qdate,qtime=None,errors=False,post=False)


#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 yakumo <yakumo@yakumo-pc>
#
# Distributed under terms of the MIT license.

import json
import sys

from flask import Flask, request, g
from github_webhook import Webhook
import zmq

from consumer import zmq_address, Consumer
from store import store

def create_app():

    app = Flask(__name__)
    webhook = Webhook(app)
    context = zmq.Context()
    zmq_sock = context.socket(zmq.PUSH)
    zmq_sock.bind(zmq_address)

    @webhook.hook('release')
    def on_release(data):
        guid = request.headers['X-Github-Delivery']
        zmq_sock.send_pyobj((guid, data))

    @webhook.hook('status')
    def on_status(data):
        guid = request.headers['X-Github-Delivery']
        zmq_sock.send_pyobj((guid, data))

    @app.route('/')
    def hello_world():
        return 'Hello, World!'

    @app.route('/deployments/<guid>')
    def get_deployment(guid):
        return json.dumps(store.get(guid) or {'op': 'null'})

    @app.route('/ops', methods=['POST'])
    def ops():
        print(json.loads(request.form['payload']))
        payload = json.loads(request.form['payload'])
        guid = payload['callback_id']
        action = payload['actions'][0]
        user = payload['user']['name']
        if action['value'] == 'deploy':
            store.set(guid, {'op': 'deploy', 'user': user})
        elif action['value'] == 'abort':
            store.set(guid, {'op': 'abort', 'user': user})
        # return json.dumps(payload['original_message'])
        return '', 204

    return app

def create_consumer():
    instance = Consumer()
    instance.run()

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print('Usage: python main.py [webhook|consumer]')
    if sys.argv[1] == 'webhook':
        create_app().run(host="0.0.0.0", port=8080)
    if sys.argv[1] == 'consumer':
        create_consumer()


from __future__ import absolute_import

from rest_framework import serializers

from .list import ListField

class NoteSerializer(serializers.Serializer):
    text = serializers.CharField()
    mentions = ListField(required=False)

    def validate_mentions(self, attrs, source):
        if source in attrs and 'group' in self.context:
            mentions = attrs[source]
            project = self.context['group'].project
            member_ids = set(
                project.member_set.filter(user_id__in=mentions).values_list('user_id', flat=True)
            )
            invalid_user_ids = [m for m in mentions if int(m) not in member_ids]
            if invalid_user_ids:
                raise serializers.ValidationError('Cannot mention a non-team member')

        return attrs


"""
This module is starting point for task 4.1 from Coding Campus 2018 Python course
(Dungeon Game)
"""

import logging
import threading
from .dungeon_map import DungeonMap
from .player import Player
import dungeon_game.utils as utils
import dungeon_game.log as log
from dungeon_game.decorators import log_decorator, debug_log_decorator
from dungeon_game.exceptions import InputError, MapInitError
from dungeon_game.enemy import ThreadEnemyWrapper
import dungeon_game.config as config

SAVE_PATH = "save.dat"
logger = logging.getLogger(log.LOGGER_NAME)

active_map = None
active_player = None
thread_enemy = None

def validator_map_size(string):
    """
    Validator for map size input
    Raises InputError with error description if string is not valid
    :param string: String to check
    :return: Bool, if success
    """
    result = False

    if string.isdigit():

        size = int(string)
        if 5 <= size <= 100:
            result = True
        else:
            raise InputError("Unacceptable map size! Try again")
    else:
        raise InputError("Input is not integer! Try again")

    return result

def validator_movement_string(string):
    """
    Validator for player move direction input
    Raises InputError with error description if string is not valid
    :param string: String to check
    :return: Bool, if success
    """
    result = False

    if string.lower() == "u":
        result = True
    elif string.lower() == "l":
        result = True
    elif string.lower() == "r":
        result = True
    elif string.lower() == "d":
        result = True
    else:
        raise InputError("Incorrect movement direction!\nPlayer can move up [U], left [L], right [R] or down [D]\n")

    return result

def validator_response(string):
    """
    Validator for yes/no response input
    Raises InputError with error description if string is not valid
    :param string: String to check
    :return: Bool, if success
    """
    result = False

    if string.lower() == "n" or string.lower() == "y":
        result = True
    else:
        raise InputError("Incorrect answer!")

    return result

def validator_command(string):
    """
    Validator for command input
    Raises InputError with error description if string is not valid
    :param string: String to check
    :return: Bool, if success
    """
    result = False

    if string.lower() == "move" or string.lower() == "save":
        result = True
    else:
        raise InputError("Incorrect command!")

    return result

@log_decorator
@debug_log_decorator
def play_game():
    """
    Plays single game of Dungeon Game
    :return: None
    """

    while True:

        player_hitpoints = active_player.get_hitpoints()
        is_trap_nearby, is_treasure_nearby = active_map.check_nearby_tiles(active_player.position)

        print("\n\n")

        if active_player.is_dead():

            logger.info("Player is killed")
            print("You grow weak and fall to the cold floor, dropping your bag beside you. You feel you can't go anymore further.")
            print("Dungeon consumes you!")
            break

        if is_trap_nearby:
            print("Your senses detect a trap in nearby tile. Watch your step!")
        if is_treasure_nearby:
            print("Radiant glow gives away treasure's location in nearby tile. Target is close!")

        logger.info(f"Player position {active_player.position}")
        print(f"Your position is ({active_player.position[1]}, {active_player.position[0]})")
        print(f"Your health is {player_hitpoints} HP")
        print(f"Your bag has space for {config.PLAYER_BAG_SIZE - active_player.bag } more chests.\n")

        command = utils.get_input(validator_command, "Input command [Save] or [Move]: ")
        if command.lower() == "save":

            if utils.save_game(SAVE_PATH, active_map, active_player):
                print("Game saved!")
            else:
                print("Save failed!")

            continue

        while True:

            move_direction = utils.get_input(validator_movement_string, "Input direction to move in: ")
            if active_player.move(move_direction.lower(), active_map):

                logger.info(f"Player moves {active_player.position[1]}, {active_player.position[0]}")
                break
            else:
                print("You can't move there! Try again\n")

        is_trap, is_treasure = active_map.check_current_tile(active_player.position)

        if is_trap:

            logger.info(f"Player is damaged by 1 hitpoint. Hitpoints left: {player_hitpoints - 1}")
            print("You spring the trap and it firmly clenches around your leg.")
            active_player.decrease_hitpoints()

            if active_player.is_dead():

                logger.info("Player is killed")
                print("You loose too much blood trying to remove it and give in to darkness.")
                print("Dungeon consumes you!")
                break

            else:

                print("You manage to remove the trap suffering minor wounds.")
                active_map.game_map[active_player.position[0]][active_player.position[1]] = DungeonMap.SYMBOL_PLAYER

        if is_treasure:

            logger.info(f"Player found the treasure. Current bag size: {active_player.bag + 1}")
            print("Shimmering glow and warmth leads you to open chest filled with gold. You fill your bag with contents of the chest.")
            active_player.bag += 1

            if active_player.is_bag_full():

                logger.info("Player filled the bag")
                print("Feeling you can't carry anymore, you backtrack to dungeon exit with the bag full of gold.")
                print("You escaped the dungeon!")
                break

            else:

                print("Your bag feels a little heavier, but there is still space for more. You continue your journey.")
                active_map.game_map[active_player.position[0]][active_player.position[1]] = DungeonMap.SYMBOL_PLAYER

    active_player.mark_last_pos(active_map)
    logger.info("Game over")

@log_decorator
@debug_log_decorator
def init_game():
    """
    Initializes game map and player position
    :return: None
    """

    global active_map
    global active_player

    while True:

        map_size = utils.get_input(validator_map_size, "Input map size [5 - 100]: ")

        try:
            active_map = DungeonMap(int(map_size))
        except MapInitError as exc:
            print(f"Failed to create map! Error: f{str(exc)}")
        else:
            break

    active_player = Player(active_map)

@log_decorator
@debug_log_decorator
def start_game():
    """
    Start game dialog
    :return: None
    """

    global active_map
    global active_player
    global thread_enemy

    active_map = DungeonMap(0, False)
    active_player = Player(active_map)

    response = utils.get_input(validator_response, "Do you wish to load last saved game? [Y\\N]: ")

    if response.lower() == 'y':

        if not utils.load_game(SAVE_PATH, active_map, active_player):

            print("Failed to load saved game! Starting new game")
            init_game()

    else:

        logger.info("Starting new game.")
        init_game()

    thread_enemy = ThreadEnemyWrapper(active_map, active_player)
    thread_enemy.start()

def run():
    """
    Runs game
    Package link: https://test.pypi.org/project/dungeon-game-yeremenko/
    :return: None
    """

    print("Welcome to Dungeon Game!")
    log.init_logger()

    while True:

        start_game()
        play_game()

        print("\nGAME OVER\n")
        active_map.print_map()

        thread_enemy.join()

        response = utils.get_input(validator_response, "Try again? [Y\\N]: ")

        if response.lower() == 'n':
            break
        else:
            logger.info("User is restarting the game")

    logger.info("User quit the game")
    print("\nQuitting game...")


import concurrent.futures
from typing import List

import numpy as np
from rasterio import features
from rasterio.transform import Affine, from_bounds
from shapely.geometry import Polygon

from raster_analysis.data_environment import DataEnvironment, SourceLayer
from raster_analysis.geodesy import get_area
from raster_analysis.globals import LOGGER, BasePolygon
from raster_analysis.query import Query
from raster_analysis.window import DerivedWindow, SourceWindow

class DataCube:
    def __init__(self, geom: BasePolygon, tile: Polygon, query: Query):
        self.grid = query.get_minimum_grid()
        self.mean_area = get_area(tile.centroid.y, self.grid.get_pixel_width()) / 10000

        source_layers = query.get_source_layers()
        layer_names = query.get_layer_names()
        self.windows = self._get_windows(source_layers, tile, query.data_environment)

        for layer in query.get_derived_layers():
            self.windows[layer.name] = DerivedWindow(
                layer, self.windows[layer.source_layer], self.mean_area
            )

        for layer in source_layers:
            if layer.name not in layer_names and layer.name in self.windows:
                # free up memory, since this means the source layer was only required
                # for a derived layer
                del self.windows[layer.name]

        tile_width = self.grid.get_tile_width()
        self.shifted_affine: Affine = from_bounds(*tile.bounds, tile_width, tile_width)

        self.mask = self._mask_geom_on_raster(
            np.ones((tile_width, tile_width)), self.shifted_affine, geom
        )

    def _get_windows(
        self,
        layers: List[SourceLayer],
        tile: BasePolygon,
        data_environment: DataEnvironment,
    ):
        with concurrent.futures.ThreadPoolExecutor() as executor:
            # Start the load operations and mark each future with its URL
            futures = {
                executor.submit(
                    SourceWindow, layer, tile, self.grid, data_environment
                ): layer
                for layer in layers
            }

            return self._get_window_results(futures)

    @staticmethod
    def _get_window_results(futures):
        windows = {}
        for future in concurrent.futures.as_completed(futures):
            layer = futures[future]

            try:
                windows[layer.name] = future.result()
            except Exception as e:
                LOGGER.exception(f"Exception while reading window for layer {layer}")
                raise e

        return windows

    @staticmethod
    def _mask_geom_on_raster(
        raster_data: np.ndarray, shifted_affine: Affine, geom: BasePolygon
    ) -> np.ndarray:
        """For a given polygon, returns a numpy masked array with the
        intersecting values of the raster_data, all non- intersecting cells are
        masked.  This assumes that the input geometry is in the same SRS as the
        raster.

        Args:
            raster_data (ndarray): ndarray of raster data to be masked onto
            shifted_affine (Affine): the transform used to map the vector geometry to the raster
            geom (Shapely Geometry): A polygon in the same SRS as `raster_path`
                which will define the area of the raster to mask.

        Returns
           Numpy masked array of source raster, cropped to the extent of the
           input geometry, with any modifications applied. Areas where the
           supplied geometry does not intersect are masked.
        """

        if raster_data.size > 0:
            # Create a numpy array to mask cells which don't intersect with the
            # polygon. Cells that intersect will have value of 1 (unmasked), the
            # rest are filled with 0s (masked)
            geom_mask = features.geometry_mask(
                [geom],
                out_shape=raster_data.shape,
                transform=shifted_affine,
                invert=True,
            )

            # Mask the data array, with modifications applied, by the query polygon
            return geom_mask
        else:
            return np.array([])


import pandas
import itertools
from math import log
import math
import numpy as np

df_trigram = pandas.read_csv('count3l.txt',index_col=None, delim_whitespace=True, names=('trigram', 'prob'))
#print(df_trigram)

new_sum_3 = df_trigram.sum(axis = 0, skipna = False)  # sums all prob of bigram
total_prob_3 = (new_sum_3[0])
#print("The log value total occurence from the trigram data is: ",np.log2(total_prob_3))           # total prob of all bigram
#print()

avg_prob_3 = []

df1_3 = (df_trigram.iloc[:,1]) / (total_prob_3) #first row of data frame
avg_prob_3 = np.log2(df1_3)
#print(avg_prob_3)
#print(total_prob)

trigram_prob = []
tprob= df_trigram.iloc[:,0]
trigram_prob = tprob                                                                 ##########
#print(bigram_prob)

max_prob = max(avg_prob_3)
#print("The log value of max prob is: ",max_prob)

dict_prob_3 = dict((trigram_prob[index], avg_prob_3[index]) for index in range(len(trigram_prob)))
#print("The bigram:",dict_prob_3, sep = " ")

### testing purpose
#print(list(dict_prob_3.keys())[2])   # output >>> Key2
#print(list(dict_prob_3.values())[2])  # output >>> Value2

############ Cryptanalysis of Caesar cipher #########################################

alphabets = 'abcdefghijklmnopqrstuvwxyz'   # Alphabets in english language
text = input('Enter the string :')        # Input message
print()
#text = "hello"
shift = 12

def encrypt(n, plaintext):                 # Function used for encryption to get cipher text
    result = ''

# Since lower case alphabets is used as the plain text.
# Here index of the input string will be considered and shift value will be added,
# and performs modulo operation for encryption
    for l in plaintext.lower():
        try:
            i = (alphabets.index(l) + shift) % 26
            result += alphabets[i]
        except ValueError:
            result += l

    return result.lower()

encrypted = encrypt(shift, text)         #Encrypts the text message using shift value
print('The encrypted text is:', encrypted)
#print()

#==================below is to calculate prob of encrypted text===================================

string = encrypted
string = string.replace(" ", "")
n = 3
temp = []
out = [(string[i:i+n]) for i in range(len(string)+1- n)]
temp = out
#print("The encrypted text is split as trigram: ",temp)

prob_list_3 = []
for i in range(len(temp)):
    prob_3 = dict_prob_3.get(temp[i])
    prob_list_3 = prob_3
    #print(prob_list)
    prob_max_3 = np.max(prob_list_3)

#print("the candidate with max prob is '{}' and the candidate is '{}' ".format(prob_max_3, temp[i]))
#print()

add = 0
for i in range(len(temp)):
    res = float(dict_prob_3.get(temp[i]))
    add = res+add

#print("The total prob of encrypted text is: ",add)
print()

##### Need to get the last element from add and print in output ??????????????##############
#######################################################################################################

def decrypt(n, encrypted):                 # Function used for decryption to get back plain text
    result = ''

    for l in encrypted:
        try:
            i = (alphabets.index(l) - shift) % 26
            result += alphabets[i]
        except ValueError:
            result += l

    return result

decrypted = decrypt(shift, encrypted)    #Decrypts the encrypted message to get back plain text

print('The text decrypted from cipher text is:', decrypted)
#print()
#==================below is to calculate prob of decrypted text===================================

string1_list = []
string1 = decrypted

string1 = string1.split()
#print(string1)
#string1 = string1.replace(" ", "")
#string1 = string1.split()
string1_list = string1
print("The candidates of decrypted text are: ",string1_list)
#print()
#print(len(string1_list[1]))
#add_list_dec_prob = []

n = 3
out1_add = []

for x in range(len(string1_list)):
    #print(len(string1_list))
    out1 = [(string1_list[x][i:i+n]) for i in range(len(string1_list[x])+1 -n)]
    #print("Candidates split as bigram for prob calculation: ",out1)
    res1 = [dict_prob_3.get(out1[i]) for i in range(len(out1))]
    #print(res1)
    res2 = np.sum(res1)
    #print(res2)
    out1_add = np.append(out1_add,res2)

out1_sum_list = []
out1_sum = np.sum(out1_add)
out1_sum_list = out1_sum
#print(out1_sum_list)

dictionary = dict(zip(string1_list, out1_add))
print()
print("The candidate and prob values are: ",dictionary)
print()

maximum = max(dictionary, key=dictionary.get)
print("the candidate with max prob is '{}' and the candidate is '{}' ".format(dictionary[maximum], maximum))

###################### Brute force attack ###################################################

print()
print("The brute force attack tells that the text was encrypted at key: ",shift)
print()

for shift in range(len(alphabets)):
    brute_force = ' '
    for symbol in encrypted:
        if symbol in alphabets:
            num = alphabets.find(symbol)
            num = num - shift
            if num < 0:
                num = num + len(alphabets)
            brute_force = brute_force + alphabets[num]
        else:
                brute_force = brute_force + symbol

    print("<Brute force attack for Shift> #%s: is %s"%(shift,brute_force))

    for i in range(len(brute_force)):
        bf_string_3 = brute_force
        bf_string_3 = bf_string_3.replace(" ", "")
        n = 3
        bf_temp_3 = []
        bf_out_3 = [(bf_string_3[i:i+n]) for i in range(len(bf_string_3)+1- n)]
        bf_temp_3 = bf_out_3

    #print("The decrypted text is split as bigram: ",bf_temp)
    #print()

    add = 0
    for i in range(len(bf_temp_3)):
         #res = float(dict_prob.get(temp[i]))
         res = dict_prob_3.get(bf_temp_3[i])
         add = res+add

    #print("The total prob of open text candidate is: ",add)
    #print()

print()
print("The total probability of open text candidate is %s  "%out1_sum_list)
print()
        #print(brute_force)


import time
from urllib.request import urlretrieve
import subprocess
from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get(
    "https://www.amazon.com/Programming-Rust-Fast-Systems-Development/dp/1491927283")

time.sleep(2)
#push the button of preview of the book

driver.find_element_by_id("sitbLogoImg").click()
#driver.find_element_by_id("sitbLogoImg").send_keys(Keys.RETURN)

imageList = set()

#wait for loading the page
time.sleep(5)
#turn the page as long as right arrow can be clicked on

while "pointer" in driver.find_element_by_id("sitbReaderRightPageTurner"
                                            ).get_attribute("style"):
    driver.find_element_by_id("sitbReaderRightPageTurner").click()
    time.sleep(2)
    # get a loaded new page(multiple pages can be loaded together but dont add
    # duplicated pages to the set)
    pages = driver.find_elements_by_xpath("//div[@class='pageImage']/div/img")
    for page in pages:
        image = page.get_attribute("src")
        imageList.add(image)

driver.quit()

#start processing image with the URL by tesseract
for image in sorted(imageList):
    urlretrieve(image, "page.jpg")
    p = subprocess.Popen(["tesseract", "page.jpg", "page"],
                        stdout = subprocess.PIPE, stderr=subprocess.PIPE)
    p.wait()
    f = open("page.txt", "r", encoding='UTF-8')
    print(f.read())


import imp
import os

def load_class(full_class_string):
    """
    Dynamically load a class from a string

    >>> klass = load_class("module.submodule.ClassName")
    >>> klass2 = load_class("myfile.Class2")

    Author: https://gist.github.com/tsileo/4354068
    """
    class_data = full_class_string.split(".")

    module_str = class_data[0]
    class_str = class_data[-1]
    submodules_list = []

    if len(class_data) > 2:
        submodules_list = class_data[1:-1]

    f, filename, description = imp.find_module(module_str)
    module = imp.load_module(module_str, f, filename, description)

    # Find each submodule
    for smod in submodules_list:
        path = os.path.dirname(filename) if os.path.isfile(
            filename) else filename

        f, filename, description = imp.find_module(smod, [path])

        # Now we can load the module
        try:
            module = imp.load_module(
                " ".join(class_data[:-1]), f, filename, description)
        finally:
            if f:
                f.close()

    # Finally, we retrieve the Class
    return getattr(module, class_str)


import sys
import numpy as np

import matplotlib.pyplot as plt
import scipy.io as sio

from fealpy.mesh.TriangleMesh import TriangleMesh
from fealpy.functionspace.tools import function_space

inputName = sys.argv[1]
degree = int(sys.argv[2])
outputName = sys.argv[3]

data = sio.loadmat(inputName)

point = data['point']
cell = data['cell'] - 1

mesh = TriangleMesh(point, cell)
edge = mesh.ds.edge
cell2edge = mesh.ds.cell_to_edge()
cell2edgeSign = mesh.ds.cell_to_edge_sign()
V = function_space(mesh, 'Lagrange', degree)

ipoint = V.interpolation_points()
edge2dof = V.edge_to_dof()
cell2dof = V.cell_to_dof()

data = {
        'point':point,
        'cell':cell + 1,
        'edge':edge + 1,
        'cell2edge':cell2edge + 1,
        'cell2edgeSign':cell2edgeSign,
        'dofPoint':ipoint,
        'edge2dof':edge2dof + 1,
        'cell2dof':cell2dof + 1
        }
sio.matlab.savemat(outputName, data)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import itertools
import numpy as np
from atlas_db.inputs.nc4 import AtlasNc4Input
from atlas_db.ingestors.mongodb import AtlasMongoIngestor
from atlas_db.constants import SCALE

class AtlasPsims(AtlasNc4Input):
    """Ingestion object for PSIMS.

    """
    def __init__(self, backend, *args, **kwargs):
        super(AtlasPsims, self).__init__(*args, **kwargs)
        self.name = ''.join(self.nc_file.split('/')[-1].split('.')[:-1])
        params = ['agricultural_model', 'climate_model', None, 'harms',
                  'irrigation', 'variable', 'crop']
        self.parameters = {
            params[i]: v for i, v in enumerate(self.name.split('_'))
            if i in [0, 1, 2, 3, 4, 5, 6]}
        # self.name = '_'.join(self.name.split('_')[3:5])
        self.human_name = 'pSIMS: {0} {1} {2}'.format(
            self.parameters['agricultural_model'],
            self.parameters['climate_model'],
            self.parameters['irrigation'])
        self.backend = backend

    def ingest(self):
        self.backend.ingest_metadata(self.metadata)
        for variable in self.variables:
            self.ingest_variable(variable)

    def ingest_variable(self, variable):

        print(variable)

        values = self.nc_dataset[variable][:]

        lons_lats = itertools.product(
            enumerate(self.lats), enumerate(self.lons))

        values = np.swapaxes(
            values, self.nc_dataset.variables[variable].dimensions.index(
                self.lat_var), 0)

        values = np.swapaxes(
            values, self.nc_dataset.variables[variable].dimensions.index(
                self.lon_var), 1)

        self.backend.ingest(values, lons_lats, self.metadata['name'], variable)

if __name__ == '__main__':
    import os
    from atlas_db.constants import BASE_DIR
    _filename = '{}_{}_{}_{}_{}_{}_{}_{}_{}.nc4'.format(
        'papsim', 'wfdei.cru', 'hist', 'default', 'firr', 'whe',
        'annual', 1979, 2012)
    NC_FILE = os.path.join(BASE_DIR, 'data', 'netcdf', 'psims', _filename)
    p = AtlasPsims(AtlasMongoIngestor(SCALE), NC_FILE, SCALE)
    p.ingest()


import math
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
import sys

import os
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parent_dir)
import linegraph
import scatterplot

def main(figureSaveLocation):
    """Create an image that demonstrates the difference between the step boundary of a decision tree and the boundary induced by an SVM.
    """

    # Define the image size.
    maxX = 20
    minX = 0
    maxY = 20
    minY = 0

    # Define the good hyperplanes.
    goodShift = 1.75
    goodDividingPlaneMinY = goodHyperlpaneDividing(x=minX)
    goodDividingPlaneMaxY = goodHyperlpaneDividing(x=maxX)
    goodDividingPlaneXCoords = [minX, maxX]
    goodDividingPlaneYCoords = [goodDividingPlaneMinY, goodDividingPlaneMaxY]

    # Determine support vectors.
    class1SupportVectorsX = np.array([8.5])
    class1SupportVectorsY = np.array([goodHyperlpaneDividing(x=class1SupportVectorsX[0] + goodShift)])
    class2SupportVectorsX = np.array([6.5, 13.0])
    class2SupportVectorsY = np.array([goodHyperlpaneDividing(x=class2SupportVectorsX[0] - goodShift), goodHyperlpaneDividing(x=class2SupportVectorsX[1] - goodShift)])

    # Define plane appearances.
    planeStyles = ['-', '--', '--']
    planeWidths = [2.0, 1.0, 1.0]

    # Class 1 data and control variables.
    class1X = np.concatenate([class1SupportVectorsX, np.array([
        1.0, 1.3, 1.5, 1.7,
        2.0, 2.2, 2.5, 2.9,
        3.0, 3.5, 3.7,
        4.0, 4.3, 4.8,
        5.1, 5.4, 5.8,
        6.5, 6.9,
        7.3, 7.6, 7.9,
        8.2, 8.5,
        9.1, 9.5,
        10.6, 10.9,
        13.3, 13.6,
        14.0])])
    class1Y = np.concatenate([class1SupportVectorsY, np.array([
        4.0, 7.9, 12.5, 16.3,
        6.7, 8.1, 17.9, 10.3,
        5.8, 13.2, 19.0,
        9.3, 17.6, 12.0,
        14.5, 8.3, 12.3,
        10.1, 17.1,
        11.7, 16.2, 19.1,
        13.7, 18.3,
        12.3, 16.7,
        15.7, 18.2,
        19.8, 17.9,
        19.0])])
    class1Color = '#0000FF'  # Blue
    class1Size = 30

    # Class 2 data and control variables.
    class2X = np.concatenate([class2SupportVectorsX, np.array([
        4.0,
        5.3, 5.7,
        6.2, 6.6, 6.9,
        7.5,
        8.1, 8.3, 8.7,
        9.2, 9.6,
        10.1, 10.5, 10.9,
        11.4, 11.7,
        12.2, 12.5, 12.9,
        13.1, 13.7,
        14.1, 14.5, 14.6,
        15.0, 15.3, 15.7, 15.9,
        16.1, 16.3, 16.5, 16.9,
        17.0, 17.5, 17.7,
        18.2, 18.3, 18.7,
        19.0, 19.3, 19.7])])
    class2Y = np.concatenate([class2SupportVectorsY, np.array([
        0.6,
        2.2, 1.2,
        0.3, 3.2, 1.7,
        2.4,
        2.3, 5.4, 3.1,
        0.7, 4.3,
        3.2, 5.6, 1.3,
        6.2, 3.4,
        7.8, 1.2, 4.3,
        2.3, 10.0,
        6.8, 11.5, 8.9,
        4.5, 7.5, 13.2, 8.3,
        2.1, 12.0, 5.7, 9.3,
        1.2, 14.4, 4.5,
        3.4, 6.7, 12.3,
        14.3, 10.1, 15.8])])
    class2Color = '#FF0000'  # Red
    class2Size = 30

    # Aggregate all class information.
    scatterXValues = [class1X, class2X]  # The X values for the classes.
    scatterXAllValues = [j for i in scatterXValues for j in i]  # A list of the X values for all the datapoints.
    scatterYValues = [class1Y, class2Y]  # The Y values for the classes.
    scatterYAllValues = [j for i in scatterYValues for j in i]  # A list of the Y values for all the datapoints.
    colors = [class1Color, class2Color]  # The colors for the classes.
    sizes = [class1Size, class2Size]  # The size of the markers for the scatterplots.

    # Define the decision tree boundary.
    decisionBoundarySegmentsXCoords = [[0.0, 0.0], [0.0, 6.0], [6.0, 6.0], [6.0, 11.0], [11.0, 11.0], [11.0, 17.0], [17.0, 17.0], [17.0, 20.0]]
    decisionBoundarySegmentsYCoords = [[0.0, 3.0], [3.0, 3.0], [3.0, 7.0], [7.0, 7.0], [7.0, 15.0], [15.0, 15.0], [15.0, 20.0], [20.0, 20.0]]

    # Create the plot.
    currentFigure = plt.figure()
    gs = gridspec.GridSpec(10, 10)
    gs.update(left=0, right=1, bottom=0, top=1, wspace=0.05)#, hspace=0.05)
    plot = plt.subplot(gs[1:-1, 1:-1])
    plot.set_xlim(left=minX, right=maxX)
    plot.set_ylim(bottom=minY, top=maxY)
    scatterplot.graphGeneration(scatterXValues, scatterYValues, currentFigure=currentFigure, colors=colors, sizes=sizes)
    linegraph.graphGeneration([goodDividingPlaneXCoords], [goodDividingPlaneYCoords], currentFigure=currentFigure, markerSizes=[0], lineWidths=[1], lineStyles=['--'])

    # Create the decision tree boundary.
    for i, j in zip(decisionBoundarySegmentsXCoords, decisionBoundarySegmentsYCoords):
        linegraph.graphGeneration([i], [j], currentFigure=currentFigure, markerSizes=[0], lineWidths=[2])
        if i[0] == i[1]:
            plt.fill_betweenx(j, i, [goodHyperlpaneDividing(y=j[0]), goodHyperlpaneDividing(y=j[1])], color='green', alpha=0.2)
        else:
            plt.fill_between(i, j, [goodHyperlpaneDividing(x=i[0]), goodHyperlpaneDividing(x=i[1])], color='green', alpha=0.2)

    # Make all the tick marks invisible.
    for ax in currentFigure.get_axes():
        removeTickMarks(ax, xAxis=True, yAxis=True)

    plt.savefig(figureSaveLocation, bbox_inches='tight', transparent=True)
    plt.show()

def goodHyperlpaneDividing(x=None, y=None):
    if x != None:
        return x
    if y != None:
        return y

def removeTickMarks(axes, xAxis=False, yAxis=False):
    """Removes all tick marks.
    """

    if xAxis:
        axes.set_xticks([])
    if yAxis:
        axes.set_yticks([])

if __name__ == '__main__':
    main(sys.argv[1])


# -*- coding: utf-8 -*-
"""
@author: Meziane AITE, meziane.aite@inria.fr
Description:
genere a dic of association id_orgin id_ref using:
    metaNetX
    xref and common_name from metacyc

usage:
    memap_like.py --sbml=FILE --tgdbRef=FILE --metaNetX_dir=DIR --output=FILE --originDB=DB [-v]
    memap_like.py --fileOfId=FILE --tgdbRef=FILE --metaNetX_dir=DIR --output=FILE --originDB=DB [-v]

options:
    -h --help     Show help.
    --sbml=FILE    sbml file to convert
    --tgdbRef=FILE    tgdb file representing to the database of reference (ex: metacyc_18.5.tgdb)
    --metaNetX_dir=DIR    directory containg chem_xref.tsv and reac_xref.tsv from metaNetX.
    --originDB=DB    the name of the db ref, {kegg, bigg}.
    --output=FILE   file of assoc.
    -v   print info

#TODO
#TODO : to del, use samifier isntead
- try to check specie/reaction's name vs all common-name from metacyc
- Better report
- optionnal: add possibility to change the refDB => not using tgdbRef
"""
from lib.tinygraphdb import Tinygraphdb
from lib.sbmlPlugin import decode_bigg
from re import search
try:
    from libsbml import *
except:
    print("package libsbml needed, use this cmd:\n pip install "
          + "python-libsbml")
    exit()
try:
    import docopt
except ImportError:
    print("package docopt needed, use this cmd:\n pip install "
          + "docopt")
    exit()

#test
#"../dyliss_tools/data/MetacycFlatFiles/19.5/PADMET/metacyc_19.5.padmet"
#

def main():
    list_of_db = ["kegg","bigg"]
    refDB = "metacyc"
    #recovering args
    args = docopt.docopt(__doc__)
    sbml_file = args["--sbml"]
    fileOfId = args["--fileOfId"]
    tgdbRef = Tinygraphdb(args["--tgdbRef"])
    metaNetX_dir = args["--metaNetX_dir"]
    chem_xrefFile = metaNetX_dir+"chem_xref.tsv"
    reac_xrefFile = metaNetX_dir+"reac_xref.tsv"
    output = args["--output"]
    verbose = args["-v"]
    originDB = args["--originDB"]
    if originDB not in list_of_db:
        raise KeyError("the originDB must be in:",list_of_db)

    #METABOLITES dictionnary of reference
    dicOfChem_xref = extract_assoc_id_from_metaNetX(chem_xrefFile, originDB, refDB)
    #REACTIONS dictionnary of refenrece
    dicOfReac_xref = extract_assoc_id_from_metaNetX(reac_xrefFile, originDB, refDB)
    #[0]: dictionnary of all metacyc common name. [1] all xref of metacyc
    allCommon_name, allXref_dbRef = extract_assoc_from_tgdbRef(tgdbRef)

    if sbml_file is not None:
        #Retriving species and reactions from the sbml
        reader = SBMLReader()
        document = reader.readSBML(sbml_file)
        model = document.getModel()
        listOfSpecies = model.getListOfSpecies()
        listOfReactions = model.getListOfReactions()
        if originDB == "kegg":
            bigg_dict = {}
            all_id_to_check = set()
            for specie in listOfSpecies:
                encoded_id = specie.getId()
                decoded_id = decode_bigg(encoded_id)
                all_id_to_check.add(decoded_id)
                bigg_dict[decoded_id] = encoded_id
            nb_unique_species = len(all_id_to_check)
            for reaction in listOfReactions:
                encoded_id = reaction.getId()
                decoded_id = decode_bigg(encoded_id)
                all_id_to_check.add(decoded_id)
                bigg_dict[decoded_id] = encoded_id
            nb_unique_reactions = len(all_id_to_check) - nb_unique_species
        else:
            unique_species = set([specie.getId() for specie in listOfSpecies])
            nb_unique_species = len(unique_species)
            unique_reactions = set([reaction.getId() for reaction in listOfReactions])
            nb_unique_reactions = len(unique_reactions)
            all_id_to_check = unique_species.union(unique_reactions)
            all_id_to_check = set(all_id_to_check)

    elif fileOfId is not None:
        with open(fileOfId, 'r') as f:
            all_id_to_check = set([line for line in f.read().splitlines() if "-" not in line])

    result_dict = {}
    nb_id = len(all_id_to_check)
    nbReac_match = 0
    nbChem_match = 0
    for _id in all_id_to_check:
        chem_assoc_found = get_refID_from_metaNetx(_id, originDB, refDB, dicOfChem_xref)
        reac_assoc_found = get_refID_from_metaNetx(_id, originDB, refDB, dicOfReac_xref)

        if chem_assoc_found is not None:
            nbChem_match += 1
            try:
                result_dict[_id] = result_dict[_id].union(chem_assoc_found)
            except KeyError:
                result_dict[_id] = chem_assoc_found
        if reac_assoc_found is not None:
            nbReac_match += 1
            try:
                result_dict[_id] = result_dict[_id].union(reac_assoc_found)
            except KeyError:
                result_dict[_id] = reac_assoc_found

    nb_match_xref = 0
    for _id in all_id_to_check:
        xref_assoc_found = get_refID_from_tgdbRef(_id, allXref_dbRef)
        if xref_assoc_found is not None:
            nb_match_xref += 1
            try:
                result_dict[_id] = result_dict[_id].union(xref_assoc_found)
            except KeyError:
                result_dict[_id] = xref_assoc_found
    all_ids_not_mapped = all_id_to_check.difference(set(result_dict.keys()))

    with open(output, 'w') as f:
        header = ["#originID", "refID"]
        header = "\t".join(header)+"\n"
        f.write(header)

        if originDB == "kegg":
            for k, v in result_dict.iteritems():
                k_encoded = bigg_dict[k]
                line = [k_encoded, ";".join(v)]
                line = "\t".join(line)+"\n"
                f.write(line)
        else:
            for k, v in result_dict.iteritems():
                line = [k, ";".join(v)]
                line = "\t".join(line)+"\n"
                f.write(line)
    if len(all_ids_not_mapped) != 0:
        """with open("not_mapped.txt", 'w') as f:
            f.write("\n".join(all_ids_not_mapped))"""

    if verbose:
        if sbml_file is not None:
            print("sbml file info:")
            print("\tNb of species: "+str(listOfSpecies))
            print("\tNb of reactions: "+str(listOfReactions))
            print("\tNb of unique species: "+str(nb_unique_species))
            print("\tNb of unique reactions: "+str(nb_unique_reactions))
        print("Memap_like report:")
        print("\tNb total ids to check: "+str(nb_id))
        print("MetaNetx results:")
        print("\tNb ids mapped with chemxref of MetaNetx: "+str(nbChem_match))
        print("\tNb ids mapped with reacxref of MetaNetx: "+str(nbReac_match))
        print("TGDBRef report:")
        print("\tNb of ids mapped based on xref of TGDBRef: "+str(nb_match_xref))
        print("Bilan: "+str(len(result_dict.keys()))+"/"+str(nb_id)+" ids mapped")

def extract_assoc_id_from_metaNetX(metaNetx_file, originDB, refDB):
    dicOfAssoc_xref = {}
    with open(metaNetx_file,'r') as f:
        #comment' section start with '#', the associations begin just after
        full_xref_assoc = [tuple(line.split("\t")[:2])
        for line in f.read().splitlines()
        if not line.startswith("#")]
        #ex of tuple in full_xref: ('bigg:10FTHF5GLUtl', 'MNXR1')

        #recovering only association containg id from the originDB of refDB
        xref_assoc = (assoc for assoc in full_xref_assoc
        if originDB in assoc[0] or refDB in assoc[0])
        #ex of tuple in generator: ('kegg:R00001', 'MNXR73598')

        for (xref, mnx) in xref_assoc:
            #in xref, there is necesseraly 'refDB:' or 'originDB:'
            #cant just split by ':' because some id have ':' inside ... GG
            if search(refDB, xref):
                db = refDB
                db_id = xref.replace(refDB+":","")
            elif search(originDB, xref):
                db = originDB
                db_id = xref.replace(originDB+":","")
            else:
                raise ValueError("Interne error, impossible to happen, to check: "+refDB,+xref)

            #creating a dictionnary with: key = mnx_id, value = dict{originDB:set(ids), refDB:set(ids)}
            try:
                dicOfAssoc_xref[mnx]
                try:
                    dicOfAssoc_xref[mnx][db].add(db_id)
                except KeyError:
                    dicOfAssoc_xref[mnx].update({db:set([db_id])})
            except KeyError:
                dicOfAssoc_xref[mnx] = {db:set([db_id])}
        #ex of item in dicOfAssoc_xref
        #dicOfAssoc_xref["MNXR73598"]:
        #{'kegg': ['R00001'], 'metacyc': ['ENDOPOLYPHOSPHATASE-RXN']}

        #delete all dictionnary with less than 2 key, (need at least 2 keys to
        #make an association between origin and ref)
        for k, v in dicOfAssoc_xref.items():
            if len(v.keys()) != 2:
                dicOfAssoc_xref.pop(k)
    return dicOfAssoc_xref

def extract_assoc_from_tgdbRef(tgdbRef):
    #dict of common_name from tgdbRef / key = tgdbref_id, value = common_name
    allCommon_name = dict([(node.getId(),node.getMisc()["COMMON-NAME"]) for node in tgdbRef.getDicOfNode().values()
     if "COMMON-NAME" in node.getMisc().keys()])
    allXref_node = ((node.getId(), node.getMisc()["ID"][0]) for node in tgdbRef.getDicOfNode().itervalues()
    if node.getClass() == "xref")

    #dict of xref with key = X_DB_id, value = [all tgdbRef_id associated with]
    allXref_dbRef = {}
    for (k, v) in allXref_node:
        allrefDB_id = set([rlt.getIdIn() for rlt in tgdbRef.getDicOfRelationOut().get(k, "None")])
        allXref_dbRef.update({v:allrefDB_id})
    return(allCommon_name, allXref_dbRef)

def get_refID_from_metaNetx(_id, originDB, refDB, dicOfAssoc_xref):
    result = None
    for assoc in dicOfAssoc_xref.itervalues():
        if _id in assoc[originDB]:
            result = assoc[refDB]
    return result

def get_refID_from_tgdbRef(_id, allXref_dbRef):
    try:
        result = allXref_dbRef[_id]
        return result
    except KeyError:
        return None

if __name__ == "__main__":
    main()


a, r, n = map(int, input().split())
count = 0

while True:
    count += 1
    if count == n:
        print(a)
        break
    a *= r

# 등비수열의 합 공식을 이용한 풀이
a, r, n = map(int, input().split())
print(a * (r ** (n - 1)))


# 聚合之前在服务器中进行在训练(baseline number = 100)
import os
import copy
import random
from tqdm import tqdm
import numpy as np
import torch, torchvision
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data.dataset import Dataset
from torchvision import transforms
from torchvision.transforms import Compose
torch.backends.cudnn.benchmark=True
from configuration import config
from CustomImageDataset import CustomImageDataset     # 图像增强模块
from VGGModule import VGG                   # 神经网络模块
from AlexNetModule import AlexNet
from LeNetModel import LeNet
from dataallocationrandom import dataallocationrandom

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

class FLlearningNonIID_1(object):
    def __init__(self):
        self.dataallocation = config.get("DataAllocation_14")     # 每个主机的分配的数据list
        self.classes_pc = 1                             # 每个主机分配的class的大小
        self.num_clients = len(self.dataallocation)          # 环境中主机的个数
        self.num_selected = self.num_clients            # 进行训练的主机的个数
        self.num_rounds = 20                           # 主机和服务器之间进行通信的轮数
        self.epochs = 5                                 # client本地训练的轮数
        self.batch_size = 32                            # 将数据批量加载到数据加载器中
        self.baseline_num = 100                         # 服务器重新训练的基准线
        self.retrain_epochs = 20                        # 服务器重新训练的轮数
        self.del_()                                     # 消除数据为0的节点client

    def del_(self):
        dataallocation_i_temp = [] # 存放元素不为0的元素
        for i in range(len(self.dataallocation)):
            if self.dataallocation[i] != 0:
                dataallocation_i_temp.append(self.dataallocation[i])
            else:
                pass
        self.dataallocation = dataallocation_i_temp
        # 更新self.num_clients和num_selected
        self.num_clients = len(self.dataallocation)
        if self.num_selected > self.num_clients:
            # self.num_selected = random.randint(1,self.num_clients)
            self.num_selected = self.num_clients
        else:
            pass

    #### get cifar dataset in x and y form
    # 得到cifar数据集的数据和标签
    def get_cifar10(self):
        '''Return CIFAR10 train/test data and labels as numpy arrays'''
        data_train = torchvision.datasets.CIFAR10('./data', train=True, download=True)
        data_test = torchvision.datasets.CIFAR10('./data', train=False, download=True)

        x_train, y_train = data_train.data.transpose((0,3,1,2)), np.array(data_train.targets)
        x_test, y_test = data_test.data.transpose((0,3,1,2)), np.array(data_test.targets)

        return x_train, y_train, x_test, y_test

    def print_image_data_stats(self,data_train, labels_train, data_test, labels_test):
        print("\nData: ")
        print(" - Train Set: ({},{}), Range: [{:.3f}, {:.3f}], Labels: {},..,{}".format(
            data_train.shape, labels_train.shape, np.min(data_train), np.max(data_train),
            np.min(labels_train), np.max(labels_train)))
        print(" - Test Set: ({},{}), Range: [{:.3f}, {:.3f}], Labels: {},..,{}".format(
            data_test.shape, labels_test.shape, np.min(data_train), np.max(data_train),
            np.min(labels_test), np.max(labels_test)))

    def clients_rand(self,train_len, nclients):
        '''
        train_len: size of the train data
        nclients: number of clients

        Returns: to_ret

        This function creates a random distribution
        for the clients, i.e. number of images each client
        possess.
        # '''
        # # 随机给client数据比例random
        # client_tmp=[]
        # sum_=0
        # #### creating random values for each client ####
        # for i in range(nclients-1):                     # 把剩余的数据量给最后一个client
        #     tmp=random.randint(10,100)
        #     sum_+=tmp
        #     client_tmp.append(tmp)

        # client_tmp= np.array(client_tmp)
        # #### using those random values as weights ####
        # clients_dist= ((client_tmp/sum_)*train_len).astype(int)
        # num  = train_len - clients_dist.sum()             # 计算剩余的数据量
        # to_ret = list(clients_dist)
        # to_ret.append(num)                          # 将剩余的数据量加入list中
        # return to_ret

        # client的数据比例给定dataallocation
        print("test_1")
        client_dist = []
        for i in range(len(self.dataallocation) -1):
            dtmp = int(train_len * self.dataallocation[i])
            client_dist.append(dtmp)
        client_dist_array = np.array(client_dist)       # 将client_dist转化为np.array类型,使用下面的sum函数计算
        num = train_len - client_dist_array.sum()       # 计算剩余的数据量
        client_dist.append(num)           # 将剩余的数据量加入list中
        to_ret = client_dist
        print("test_2")
        return to_ret

    def split_image_data(self,data, labels, n_clients=100, classes_per_client=10, shuffle=True, verbose=True):
        '''
        Splits (data, labels) among 'n_clients s.t. every client can holds 'classes_per_client' number of classes
        Input:
            data : [n_data x shape]
            labels : [n_data (x 1)] from 0 to n_labels
            n_clients : number of clients
            classes_per_client : number of classes per client
            shuffle : True/False => True for shuffling the dataset, False otherwise
            verbose : True/False => True for printing some info, False otherwise
        Output:
            clients_split : client data into desired format
        '''

        # 判断self.dataallocation中的元素是否都为平均值，即每个client中的数据量为平均分配
        # flag = 1: 平均分配，flag = 0 :非平均分配
        print("self.dataallocation:",self.dataallocation)
        print("ave:",1/self.num_clients)

        flag = 1      # 初始化flag = 1
        for i in self.dataallocation:
            if i != (1 / self.num_clients):
                flag = 0
            else:
                pass

        if flag == 1:
            print("flag = 1,数据平均分配")
        else:
            print("flag = 0，数据非平均分配")

        #### constants ####
        n_data = data.shape[0]                  # 总的数据量
        n_labels = np.max(labels) + 1           # 求序列的最值，即为n_labels的数量

        ### client distribution ####
        data_per_client = self.clients_rand(len(data), n_clients)
        print("data_per_client,环境中主机分配到的数据量大小为:",data_per_client)
        data_per_client_per_class = [np.maximum(1,nd // classes_per_client) for nd in data_per_client]
        print("data_per_client_per_class，环境中每个client中的每个类最多能分配到的数据大小:",data_per_client_per_class)

        data_per_client_temp = copy.deepcopy(data_per_client)

        # data temp
        if flag == 1:
            data_per_client_temp[(len(self.dataallocation) - 1)] -= 1
        else:
            pass
        print("data_per_client_temp:",data_per_client_temp)

        # sort for labels
        data_idcs = [[] for i in range(n_labels)]
        for j, label in enumerate(labels):
            data_idcs[label] += [j]
        if shuffle:
            for idcs in data_idcs:
                np.random.shuffle(idcs)

        # split data among clients
        clients_split = []
        c = 0
        for i in range(n_clients):
            # c = 0
            client_idcs = []

            budget = data_per_client_temp[i]                 # 每个client的数据量

            c = np.random.randint(n_labels)             # 随机获得class
            while budget > 0:
                take = min(data_per_client_per_class[i], len(data_idcs[c]), budget)  # 得到参数最小值

                client_idcs += data_idcs[c][:take]
                data_idcs[c] = data_idcs[c][take:]

                budget -= take

                c = (c + 1) % n_labels                  # 获取新的class
                # c = c + 1

            clients_split += [(data[client_idcs], labels[client_idcs])]
        print("data_per_client:1:",data_per_client)
        print("data_per_client_temp:1:",data_per_client_temp)
        # 内嵌函数，外部无法访问，打印信息
        def print_split(clients_split):
            print("Data split:")
            for i, client in enumerate(clients_split):
                split = np.sum(client[1].reshape(1,-1)==np.arange(n_labels).reshape(-1,1), axis=1)
                print(" - Client {}: {}".format(i,split))
            print()

            if verbose:
                print_split(clients_split)

        clients_split = np.array(clients_split,dtype=object)

        return clients_split

    def shuffle_list(self,data):
        '''
        This function returns the shuffled data
        '''
        # print("len(data)",len(data))
        for i in range(len(data)):
            tmp_len = len(data[i][0])
            index = [i for i in range(tmp_len)]
            random.shuffle(index)
            data[i][0],data[i][1] = self.shuffle_list_data(data[i][0],data[i][1])
        return data

    def shuffle_list_data(self,x, y):
        '''
        This function is a helper function, shuffles an
        array while maintaining the mapping between x and y
        '''
        inds = list(range(len(x)))
        random.shuffle(inds)
        return x[inds],y[inds]

    def get_default_data_transforms(self,train=True, verbose=True):
        transforms_train = {
        'cifar10' : transforms.Compose([
            transforms.ToPILImage(),
            transforms.RandomCrop(32, padding=4),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))]),#(0.24703223, 0.24348513, 0.26158784)
        }
        transforms_eval = {
        'cifar10' : transforms.Compose([
            transforms.ToPILImage(),
            transforms.ToTensor(),
            transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
        }
        if verbose:
            print("\nData preprocessing: ")
            for transformation in transforms_train['cifar10'].transforms:
                print(' -', transformation)
            print()

        return (transforms_train['cifar10'], transforms_eval['cifar10'])

    def get_data_loaders(self,nclients,batch_size,classes_pc=10 ,verbose=True ):

        x_train, y_train, x_test, y_test = self.get_cifar10()

        if verbose:
            self.print_image_data_stats(x_train, y_train, x_test, y_test)

        transforms_train, transforms_eval = self.get_default_data_transforms(verbose=False)

        split = self.split_image_data(x_train, y_train, n_clients=nclients,
                classes_per_client=self.classes_pc, verbose=verbose)
        # print("n_client test:",nclients)
        split_tmp = self.shuffle_list(split)

        client_loaders = [torch.utils.data.DataLoader(CustomImageDataset(x, y, transforms_train),
                                                                        batch_size=batch_size, shuffle=True) for x, y in split_tmp]

        test_loader = torch.utils.data.DataLoader(CustomImageDataset(x_test, y_test, transforms_eval), batch_size=100, shuffle=False)

        return client_loaders, test_loader

    def baseline_data(self,num):
        '''
        Returns baseline data loader to be used on retraining on global server
        Input:
                num : size of baseline data
        Output:
                loader: baseline data loader
        '''
        xtrain, ytrain, xtmp, ytmp = self.get_cifar10()
        x , y = self.shuffle_list_data(xtrain, ytrain)

        x, y = x[:num], y[:num]
        transform, _ = self.get_default_data_transforms(train=True, verbose=False)
        loader = torch.utils.data.DataLoader(CustomImageDataset(x, y, transform), batch_size=16, shuffle=True)
        return loader

    def client_update(self, client_model, optimizer, train_loader, epoch, model):
        """
        This function updates/trains client model on client data
        """
        model.train()
        for e in range(epoch):
            for batch_idx, (data, target) in enumerate(train_loader):
                data, target = data.cuda(), target.cuda()
                # data, target = data.to(device), target.to(device)
                optimizer.zero_grad()
                output = client_model(data)
                loss = F.nll_loss(output, target)
                loss.backward()
                optimizer.step()
        return loss.item()

    def client_syn(self, client_model, global_model):
        '''
        This function synchronizes the client model with global model
        '''
        client_model.load_state_dict(global_model.state_dict())

    def server_aggregate(self, global_model, client_models, client_lens):
        """
        This function has aggregation method 'wmean'
        wmean takes the weighted mean of the weights of models
        """
        total = sum(client_lens)
        n = len(client_models)
        global_dict = global_model.state_dict()
        for k in global_dict.keys():
            global_dict[k] = torch.stack([client_models[i].state_dict()[k].float()*(n*client_lens[i]/total) for i in range(len(client_models))], 0).mean(0)
        global_model.load_state_dict(global_dict)
        for model in client_models:
            model.load_state_dict(global_model.state_dict())

    def test(self, global_model, test_loader, model):
        """
        This function test the global model on test
        data and returns test loss and test accuracy
        """
        model.eval()
        test_loss = 0
        correct = 0
        with torch.no_grad():
            for data, target in test_loader:
                data, target = data.cuda(), target.cuda()
                # data, target = data.to(device), target.to(device)
                output = global_model(data)
                test_loss += F.nll_loss(output, target, reduction='sum').item()  # sum up batch loss
                pred = output.argmax(dim=1, keepdim=True)  # get the index of the max log-probability
                correct += pred.eq(target.view_as(pred)).sum().item()

        test_loss /= len(test_loader.dataset)
        acc = correct / len(test_loader.dataset)

        return test_loss, acc

    def main(self):
        print("test")
        ############################################
        #### Initializing models and optimizer  ####
        ############################################

        #### global model ##########
        global_model =  VGG("VGG19").cuda()

        ############# client models ###############################
        client_models = [ VGG("VGG19").cuda() for _ in range(self.num_selected)]
        for model in client_models:
            model.load_state_dict(global_model.state_dict()) ### initial synchronizing with global modle

        ###### optimizers ################
        opt = [optim.SGD(model.parameters(), lr = 0.01) for model in client_models]

        ####### baseline data ############
        loader_fixed = self.baseline_data(self.baseline_num)

        ###### Loading the data using the above function ######
        train_loader, test_loader = self.get_data_loaders(classes_pc=self.classes_pc, nclients= self.num_clients,
                                                            batch_size=self.batch_size,verbose=True)

        losses_train = []
        losses_test = []
        acc_test = []
        losses_retrain=[]

        # Runnining FL
        for r in range(self.num_rounds):    #Communication round
            # select random clients
            client_idx = np.random.permutation(self.num_clients)[:self.num_selected]
            client_lens = [len(train_loader[idx]) for idx in client_idx]

            # client update
            loss = 0
            for i in tqdm(range(self.num_selected)):
                self.client_syn(client_models[i], global_model)
                loss += self.client_update(client_models[i], opt[i], train_loader[client_idx[i]], self.epochs,model)
            losses_train.append(loss)

            # server aggregate
            #### retraining on the global server
            # loss_retrain = 0
            # for i in tqdm(range(self.num_selected)):
            #     loss_retrain+= self.client_update(client_models[i], opt[i], loader_fixed, self.retrain_epochs,model)
            # losses_retrain.append(loss_retrain)

            ### Aggregating the models
            self.server_aggregate(global_model, client_models,client_lens)
            test_loss, acc = self.test(global_model, test_loader,model)
            losses_test.append(test_loss)
            acc_test.append(acc)
            print('%d-th round' % r)
            print('average train loss %0.3g | test loss %0.3g | test acc: %0.3f' % (loss_retrain / self.num_selected, test_loss, acc))
        return acc_test, losses_test

    # 类的销毁
    def __del__(self):
        print("FLLearningNon-IID的对象被收回----\n")

if __name__ == "__main__":
    dataallocation = config.get("DataAllocation_14")
    # dataallocation = dataallocationrandom(10)

    FL = FLlearningNonIID_1()

    FL.num_rounds = 100
    acc_test, losses_test = FL.main()