# -*- coding: utf-8 -*-
from __future__ import unicode_literals
#Weinschenk
#3-15
from __future__ import division
import os
import numpy as np
import pandas as pd
import itertools
from pylab import *
import statsmodels.api as sm

from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})

#from ggplot import *
#from patsy import ModelDesc

# Location of experimental data files
data_dir = '../Experimental_Data/'

# Location of test description file
info_file = '../Experimental_Data/Gas_Cooling_Test_Matrix.csv'

# Location of figures
plot_dir = '../Figures/Gas_Cooling/'
# List of sensor groups for each plot
sensor_groups = [['TC_A1_'],['TC_A2_'],['TC_A3_']]

# Load exp. timings and description file
info = pd.read_csv(info_file, index_col=0)

# Skip files
skip_files = ['_reduced', 'description_','zero','bb','es_','fse','cafs','fsw','_attic','sc','gas_cooling','west_','east_','all_']
skip_channels = []#['_9','_10','_11']

#  ===============================
#  = Loop through all data files =
#  ===============================

# Load exp. data file
data1 = pd.read_csv(data_dir + 'GCSeries1.csv')
data2 = pd.read_csv(data_dir + 'GCSeries2.csv')
data3 = pd.read_csv(data_dir + 'GCSeries3.csv')
data4 = pd.read_csv(data_dir + 'GCSeries4.csv')
data5 = pd.read_csv(data_dir + 'GCSeries5.csv')

for entry in range(0,len(info)):
	if info['Replicates'][entry] == 0:
		continue
	else:
		print (info['Nozzle'][entry]+ ' ' + info['Position'][entry])
		num_rep = info['Replicates'][entry]
		del_TC_1_h2o = np.zeros((11,num_rep))
		del_TC_2_h2o = np.zeros((11,num_rep))
		del_TC_3_h2o = np.zeros((11,num_rep))
		del_TC_1_cafs = np.zeros((11,num_rep))
		del_TC_2_cafs = np.zeros((11,num_rep))
		del_TC_3_cafs = np.zeros((11,num_rep))

		for reps in range(0,num_rep):
			for channel in data1.columns[1:]:
                # Skip excluded channels
				if any([substring in channel for substring in skip_channels]):
					continue
				elif 'TC_A1_' in channel:
					water_test = 'data'+str(info['Test_Series_Water'][entry+reps])
					water_test = locals().get(water_test)
					water_data_interval = water_test[channel][info['Start_Time_Water'][entry+reps]:info['Stop_Time_Water'][entry+reps]]
					del_TC_1_h2o[int(channel[6:])-1,reps] = max(water_data_interval) - min(water_data_interval)
					#del_TC_1_h2o[int(channel[6:])-1,reps] = (max(water_data_interval) - min(water_data_interval))/max(water_data_interval)

					cafs_test = 'data'+str(info['Test_Series_CAFS'][entry+reps])
					cafs_test = locals().get(cafs_test)
					cafs_data_interval = cafs_test[channel][info['Start_Time_CAFS'][entry+reps]:info['Stop_Time_CAFS'][entry+reps]]
					del_TC_1_cafs[int(channel[6:])-1,reps] = max(cafs_data_interval) - min(cafs_data_interval)
					#del_TC_1_cafs[int(channel[6:])-1,reps] = (max(cafs_data_interval) - min(cafs_data_interval))/max(cafs_data_interval)
				elif 'TC_A3_' in channel:
					water_test = 'data'+str(info['Test_Series_Water'][entry+reps])
					water_test = locals().get(water_test)
					water_data_interval = water_test[channel][info['Start_Time_Water'][entry+reps]:info['Stop_Time_Water'][entry+reps]]
					del_TC_3_h2o[int(channel[6:])-1,reps] = max(water_data_interval) - min(water_data_interval)
					#del_TC_3_h2o[int(channel[6:])-1,reps] = (max(water_data_interval) - min(water_data_interval))/max(water_data_interval)
					# print (max(water_data_interval) , min(water_data_interval), max(water_data_interval) - min(water_data_interval), channel,reps,'water')
					cafs_test = 'data'+str(info['Test_Series_CAFS'][entry+reps])
					cafs_test = locals().get(cafs_test)
					cafs_data_interval = cafs_test[channel][info['Start_Time_CAFS'][entry+reps]:info['Stop_Time_CAFS'][entry+reps]]
					del_TC_3_cafs[int(channel[6:])-1,reps] = max(cafs_data_interval) - min(cafs_data_interval)
					#del_TC_3_cafs[int(channel[6:])-1,reps] = (max(cafs_data_interval) - min(cafs_data_interval))/max(cafs_data_interval)
					# print (max(cafs_data_interval) , min(cafs_data_interval),max(cafs_data_interval) - min(cafs_data_interval),channel,reps,'cafs')
					# print()
				if 'TC_A2_' in channel:
					water_test = 'data'+str(info['Test_Series_Water'][entry+reps])
					water_test = locals().get(water_test)
					water_data_interval = water_test[channel][info['Start_Time_Water'][entry+reps]:info['Stop_Time_Water'][entry+reps]]
					del_TC_2_h2o[int(channel[6:])-1,reps] = (max(water_data_interval) - min(water_data_interval))#/max(water_data_interval)

					cafs_test = 'data'+str(info['Test_Series_CAFS'][entry+reps])
					cafs_test = locals().get(cafs_test)
					cafs_data_interval = cafs_test[channel][info['Start_Time_CAFS'][entry+reps]:info['Stop_Time_CAFS'][entry+reps]]
					del_TC_2_cafs[int(channel[6:])-1,reps] = (max(cafs_data_interval) - min(cafs_data_interval))#/max(cafs_data_interval)
		del_water = concatenate([del_TC_1_h2o, del_TC_2_h2o, del_TC_3_h2o], axis=1)
		#del_water = del_TC_3_h2o
		del_water = del_water.reshape(-1)
		del_cafs  = concatenate([del_TC_1_cafs, del_TC_2_cafs, del_TC_3_cafs], axis=1)
		#del_cafs = del_TC_3_cafs
		del_cafs  = del_cafs.reshape(-1)
		dif_data  = []
		for i in range (0,len(del_water)):
			dif_data.append({'del_water' : del_water[i], 'del_cafs' : del_cafs[i]})
		del_temp = pd.DataFrame(dif_data,columns=['del_water','del_cafs'])

		#-----------------------
		# Stats Analysis OLS
		#-----------------------

		y = del_water.ravel()
		X = del_cafs.ravel()
		est = sm.OLS(y, X)
		est = est.fit()
		max_axis = max(X.max(),y.max())
		X_prime = np.linspace(0,X.max())
		y_hat = est.predict(X_prime)
		avg_rel_diff = sum((y-X)/((y+X)/2.))/len(X)

		# p = ggplot(aes('del_cafs', 'del_water'),data = del_temp) + geom_point() + stat_smooth(method='lm', color='blue',se=True) + \
		# geom_abline(intercept=0,slope=1,colour="black") + scale_x_continuous("$\Delta$T (deg C) CAFS", limits=(0,max_axis)) + \
		# scale_y_continuous("$\Delta$T (deg C) H$_2$O",limits=(0,max_axis))
		# ggsave(p,plot_dir + info['Nozzle'][entry] + '_' + info['Position'][entry] + '_scatter_ggplot.pdf',format='pdf' )

		fig = figure()
		plt.scatter(X,y,alpha=0.5)
		plt.plot(X_prime, y_hat, 'r--', alpha=0.9,linewidth=3)  # Add the regression line, colored in red
		plt.plot(X_prime,X_prime,'k')
		plt.fill_between(X_prime,0.85*X_prime,1.15*X_prime, facecolor='gray',alpha=0.5, interpolate=True,linewidth=3)
		axis([0, max_axis, 0, max_axis])
		plt.text(0.1*max_axis , 0.95*max_axis, 'R$^2$ = ' + str(around(est.rsquared,decimals=3)),
			 horizontalalignment='center',
		     verticalalignment='center', bbox=dict(facecolor='none', edgecolor='black'))
		xlabel('$\Delta$T ($^{\circ}$C) CAFS')
		ylabel('$\Delta$T ($^{\circ}$C) WATER')
		savefig(plot_dir + info['Nozzle'][entry] + '_' + info['Position'][entry] + '_scatter.pdf',format='pdf')

		# fig = figure()
		# plt.scatter(del_TC_2_h2o,del_TC_2_cafs,alpha=0.5)
		# plt.plot(X_prime,X_prime,'k')
		# axis([0, max_axis, 0, max_axis])
		# xlabel('$\Delta$T ($^{\circ}$C) CAFS')
		# ylabel('$\Delta$T ($^{\circ}$C) H$_2$O')
		# savefig(plot_dir + info['Nozzle'][entry] + '_' + info['Position'][entry] + '_A2_scatter.pdf',format='pdf')

		# fig=figure()
		# plt.hist(est.norm_resid())
		# plt.ylabel('Count')
		# plt.xlabel('Normalized residuals')
		# savefig(plot_dir + info['Nozzle'][entry] + '_' + info['Position'][entry] + '_residuals.pdf',format='pdf')


#!/usr/bin/env pypy3

from collections import Counter
from collections import deque
from itertools import count

EXPECTED_PART_1 = 110
EXPECTED_PART_2 = 20

def north(x, y):
    return x, y - 1

def northeast(x, y):
    return x + 1, y - 1

def east(x, y):
    return x + 1, y

def southeast(x, y):
    return x + 1, y + 1

def south(x, y):
    return x, y + 1

def southwest(x, y):
    return x - 1, y + 1

def west(x, y):
    return x - 1, y

def northwest(x, y):
    return x - 1, y - 1

DIRECTIONS = [
    ((northeast, north, northwest), north),
    ((southeast, south, southwest), south),
    ((northwest, west, southwest), west),
    ((northeast, east, southeast), east),
]

def neighbours(x, y):
    return [
        north(x, y),
        northeast(x, y),
        east(x, y),
        southeast(x, y),
        south(x, y),
        southwest(x, y),
        west(x, y),
        northwest(x, y),
    ]

def read_input(filename):
    with open(filename) as lines:
        return frozenset(
            (x, y)
            for y, line in enumerate(lines)
            for x, c in enumerate(line)
            if c == "#"
        )

def step(grove, directions):
    proposals = {}
    not_moved = set()

    for x, y in grove:
        neighbour_count = sum(neighbour in grove for neighbour in neighbours(x, y))
        if neighbour_count != 0:
            for look_directions, move_direction in directions:
                if all(direction(x, y) not in grove for direction in look_directions):
                    proposals[x, y] = move_direction(x, y)
                    break
            else:
                not_moved.add((x, y))
        else:
            not_moved.add((x, y))

    proposal_counts = Counter(proposals.values())
    grove = set()

    for elf, proposal in proposals.items():
        if proposal_counts[proposal] == 1:
            grove.add(proposal)
        else:
            grove.add(elf)

    grove |= not_moved
    directions.rotate(-1)

    return grove

def part_1(grove):
    directions = deque(DIRECTIONS)
    for _ in range(10):
        grove = step(grove, directions)

    min_x = min(x for x, _ in grove)
    max_x = max(x for x, _ in grove)
    min_y = min(y for _, y in grove)
    max_y = max(y for _, y in grove)
    return (max_x - min_x + 1) * (max_y - min_y + 1) - len(grove)

def part_2(grove):
    directions = deque(DIRECTIONS)

    for i in count(1):
        old_grove = grove
        grove = step(grove, directions)
        if grove == old_grove:
            return i

def test_part_1():
    assert part_1(read_input("input_test")) == EXPECTED_PART_1

def test_part_2():
    puzzle_input = read_input("input_test")
    assert part_2(puzzle_input) == EXPECTED_PART_2

def main():
    grove = read_input("input")
    print(part_1(grove))
    print(part_2(grove))

if __name__ == "__main__":
    main()


import random
from typing import List

import pygame

import pymunk
import pymunk.pygame_util

class BouncyBalls(object):

    def __init__(self) -> None:

        self._space = pymunk.Space()
        self._space.gravity = (0.0, 900.0)

        self._dt = 1.0 / 60.0

        self._physics_steps_per_frame = 1

        pygame.init()
        self._screen = pygame.display.set_mode((600, 600))
        width, height = 600, 600
        self._clock = pygame.time.Clock()

        self._draw_options = pymunk.pygame_util.DrawOptions(self._screen)

        self._add_static_scenery()

        self._balls: List[pymunk.Circle] = []

        self._running = True
        self._ticks_to_next_ball = 10

    def run(self) -> None:

         while self._running:

            for x in range(self._physics_steps_per_frame):
                self._space.step(self._dt)

            self._process_events()
            self._update_balls()
            self._clear_screen()
            self._draw_objects()
            pygame.display.flip()

            self._clock.tick(50)
            pygame.display.set_caption("fps: " + str(self._clock.get_fps()))

    def _add_static_scenery(self) -> None:

        static_body = self._space.static_body
        static_lines = [
            pymunk.Segment(static_body, (600, 600), (0, 600-150), 70),
            pymunk.Segment(static_body, (600, 600), (600, 0), 25),
            pymunk.Segment(static_body, (0, 0), (0,600-100), 25)
        ]
        for line in static_lines:
            line.elasticity = 0.55
            line.friction = 0.9
        self._space.add(*static_lines)

    def _process_events(self) -> None:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self._running = False
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                self._running = False
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_p:
                pygame.image.save(self._screen, "bouncing_balls.png")

    def _update_balls(self) -> None:

        self._ticks_to_next_ball -= 1
        if self._ticks_to_next_ball <= 0:
            self._create_ball()
            self._ticks_to_next_ball = 100

        balls_to_remove = [ball for ball in self._balls if ball.body.position.y > 500]
        for ball in balls_to_remove:
            self._space.remove(ball, ball.body)
            self._balls.remove(ball)

    def _create_ball(self) -> None:

        mass = 10
        radius = 25
        inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
        body = pymunk.Body(mass, inertia)
        x = random.randint(115, 350)
        body.position = x, 200
        shape = pymunk.Circle(body, radius, (0, 0))
        shape.elasticity = 1.25
        shape.friction = 1.25
        self._space.add(body, shape)
        self._balls.append(shape)

    def _clear_screen(self) -> None:

        self._screen.fill(pygame.Color("white"))

    def _draw_objects(self) -> None:

        self._space.debug_draw(self._draw_options)

def main():
    game = BouncyBalls()
    game.run()

if __name__ == "__main__":
    main()


# unsweep script for the GPU
#
# Installation:
# * Download and install Fiji
# * Activate the update sites "clij", "clij2"
#   and "Haesleinhuepf". The third one must
#   be entered manually.
#
# Author: Robert Haase, rhaase@mpi-cbg.de
#		 November 2019
# -----------------------------------------

# enter input folders and file endings
inputFolder = "D:/_MPI-CBG -Data/Daniela/18102019/tiff_stacks/";
outputFolder = "V:/IMAGING/SCAPE/20191018/"

inputSuffix = ".tiff";

# set this value to 1 if you want to prevent scaling
isotropic_scale = 0.5;

# SCAPE config
unsweep_angle = 35; # degrees
translationX = 0;

#/ ------------ DO NO EDIT BELOW  --------------------------------------

import os;

from net.haesleinhuepf.clijx import CLIJx;
from net.haesleinhuepf.spimcat.unsweep import Unsweep
from ij import IJ;

# init GPU
clijx = CLIJx.getInstance();

# get list of files and subfolders in input path
for root, directories, filenames in os.walk(inputFolder):
	for directory in directories:
		print("directory: " + directory);
		for subroot, subdirectories, subfilenames in os.walk(inputFolder + directory):
			filenames.sort();
			for filename in subfilenames:
				print("filename: " + filename);
				# Check for file extension
				if not filename.endswith(inputSuffix):
					continue

				print (filename);

				# load image
				input = clijx.readImageFromDisc(inputFolder + directory + "/" + filename);

				if (isotropic_scale != 1):
					# downsample
					downsampled = clijx.create([input.getWidth() * isotropic_scale, input.getHeight() * isotropic_scale, input.getDepth() * isotropic_scale]);
					clijx.downsample(input, downsampled, isotropic_scale, isotropic_scale, isotropic_scale);

					# unsweep
					unsweeped = clijx.create(downsampled);
					Unsweep.unsweep(clijx.getClij(), downsampled, unsweeped, unsweep_angle, translationX);
					downsampled.close();
				else:
					# unsweep
					unsweeped = clijx.create(input);
					Unsweep.unsweep(clijx.getClij(), input, unsweeped, unsweep_angle, translationX);

				input.close();

				# save result stack
				clijx.saveAsTIF(unsweeped, outputFolder + "/" + filename + "_corr.tif");

				# draw max projection
				max_projected = clijx.create([unsweeped.getWidth(), unsweeped.getHeight()], unsweeped.getNativeType());
				clijx.maximumZProjection(unsweeped, max_projected)
				clijx.saveAsTIF(max_projected, outputFolder + "/thumbnails/" + filename + "_corr.tif");

				# cleanu
					# unsweepp
				unsweeped.close();
				max_projected.close();

				#break;

		#break;


import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# Import data (Make sure to parse dates. Consider setting index column to 'date'.)
df = pd.read_csv("fcc-forum-pageviews.csv", index_col = "date", parse_dates = ["date"])
# print(df.head())
# print(df.describe())

# Clean data
df = df[(df['value'] >= df['value'].quantile(0.025)) &
        (df['value'] <= df['value'].quantile(0.975))]
# print(df.head())
# print(df.describe())

def draw_line_plot():
    """
    Create a draw_line_plot function that uses Matplotlib to draw a line chart
    similar to "examples/Figure_1.png". The title should be "Daily freeCodeCamp
    Forum Page Views 5/2016-12/2019". The label on the x axis should be "Date"
    and the label on the y axis should be "Page Views".
    """
    fig = plt.figure(figsize=(10, 3))

    plt.plot(df['value'],color='r')
    plt.xlabel('Date')
    plt.ylabel('Page Views')
    plt.title('Daily freeCodeCamp Forum Page Views 5/2016-12/2019')
    # plt.show()

    # Save image and return fig (don't change this part)
    fig.savefig('line_plot.png')
    return fig
# draw_line_plot()

def draw_bar_plot():
    """
    Create a draw_bar_plot function that draws a bar chart similar to
    "examples/Figure_2.png". It should show average daily page views for each
    month grouped by year. The legend should show month labels and have a title
    of "Months". On the chart, the label on the x axis should be "Years" and
    the label on the y axis should be "Average Page Views".
    """
    # Copy and modify data for monthly bar plot
    df_bar = df.copy()
    df_bar["month"] = df_bar.index.month
    df_bar["year"] = df_bar.index.year

    df_bar = df_bar.groupby(["year", "month"])["value"].mean().unstack()
    # print(df_bar.head())
    # print(df_bar.describe())

    months=['January','February','March','April','May','June','July','August','September','October','November','December']

    # Draw bar plot
    fig = df_bar.plot(kind ='bar', figsize=(8, 8)).figure
    plt.xlabel('Years')
    plt.ylabel('Average Page Views')
    plt.legend(fontsize = 10, labels = months)
    plt.show()

    # Save image and return fig (don't change this part)
    fig.savefig('bar_plot.png')
    return fig
# draw_bar_plot()

def draw_box_plot():
    """
    Create a draw_box_plot function that uses Searborn to draw two adjacent
    box plots similar to "examples/Figure_3.png". These box plots should show
    how the values are distributed within a given year or month and how it
    compares over time. The title of the first chart should be "Year-wise Box
    Plot (Trend)" and the title of the second chart should be "Month-wise Box
    Plot (Seasonality)". Make sure the month labels on bottom start at "Jan"
    and the x and x axis are labeled correctly.
    """
    # Prepare data for box plots (this part is done!)
    df_box = df.copy()
    df_box.reset_index(inplace=True)
    df_box['Year'] = [d.year for d in df_box.date]
    df_box['Month'] = [d.strftime('%b') for d in df_box.date]
    # print(df_box.head())
    # print(df_box.describe())

    # Draw box plots (using Seaborn)
    fig, axes = plt.subplots(figsize=(12, 5), ncols=2, sharex=False)
    sns.despine(left=True)

    box_plot_year = sns.boxplot(x=df_box['Year'], y=df_box.value, ax=axes[0])
    box_plot_year.set_title("Year-wise Box Plot (Trend)")
    box_plot_year.set_xlabel('Year')
    box_plot_year.set_ylabel('Page Views')

    box_plot_month = sns.boxplot(x=df_box['Month'], y=df_box.value, ax=axes[1],
                                 order=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"])
    box_plot_month.set_title("Month-wise Box Plot (Seasonality)")
    box_plot_month.set_xlabel('Month')
    box_plot_month.set_ylabel('Page Views')

    # Save image and return fig (don't change this part)
    fig.savefig('box_plot.png')
    return fig
# draw_box_plot()

# Run all
print("\n\n\nLine Plot")
draw_line_plot()

print("\n\n\nBar Plot")
draw_bar_plot()

print("\n\n\nBox Plot")
draw_box_plot()

Data Analysis with Python/Projects/Page View Time Series Visualizer/page_view_time_series_visualizer.py


import allure

from src.pages.home_page import HomePage
from src.test_base import TestBase

class HomePageTest(TestBase):
    URL = "https://www.python.org/"

    @allure.description("""
    Opens the Python HomePage and checks if the python logo is displayed
    """)
    def test_home_page(self):
        self.driver.get(self.URL)
        self.assertEqual(self.driver.current_url, self.URL, "Current URL is different than it it supposed to")

        home_page = HomePage(self.driver)
        self.assertTrue(home_page.get_logo().is_displayed())


import pandas as pd
import numpy as np

def pay_average(pd_data):
    f_base = 0
    m_base = 0
    u_base = 0
    f_total = 0
    m_total = 0
    u_total = 0
    f_count = 0
    m_count = 0
    u_count = 0
    count = 0
    for index, row in pd_data.iterrows():
        if row['Job'] == 'SPECIAL NURSE':
            if row['Gender'] == "m":
                temp = row['Base Salary']
                if not pd.isna(temp):
                    m_base += temp
                else: print(temp)
                m_total += row['Total Pay']
                m_count += 1
            if row['Gender'] == "f":
                temp = row['Base Salary']
                if not pd.isna(temp):
                    f_base += temp
                f_total += row['Total Pay']
                f_count += 1
            else:
                temp = row['Base Salary']
                if not pd.isna(temp):
                    u_base += temp
                u_total += row['Total Pay']
                u_count += 1
    pay_avg = [['men', m_count, m_base/m_count, m_total/m_count],['women', f_count, f_base/f_count, f_total/f_count],
                ['other', u_count, u_base/u_count, u_total/u_count]]
    pd_pay_avg = pd.DataFrame(pay_avg, columns=['Gender', 'Number', 'Base Pay Avg', 'Total Pay Avg'])
    return pd_pay_avg

data = pd.read_csv('cleaned_data.csv', delimiter=',')
print(pay_average(data))


def main():
    n = int(input())
    for _ in range(n):
        x = int(input())
        tot = 0
        for _ in range(x):
            name, quantity, price = input().split()
            tot += int(quantity) * float(price)
        print("${:0.2f}".format(tot))

if __name__ == "__main__":
    main()


"""Compute finite-temperature thermodynamic quantities using materials project data."""
import logging
import os
import pickle
import time
from functools import lru_cache
from typing import List, Union

import numpy as np
from filelock import FileLock
from pymatgen.core import Composition as C
from pymatgen.core import Element
from pymatgen.ext.matproj import MPRester
from pymatgen.core.periodic_table import _pt_data
from pymatgen.entries.computed_entries import ComputedEntry
from pymatgen.ext.matproj import MPRestError

from s4.cache import CACHE_DIR
from s4.thermo.calc.finite_g import finite_dg_correction
from s4.thermo.constants import CO3_CORRECTION, MP_OXYGEN_CORRECTION
from s4.thermo.utils import convert_unit
from s4.types import Comp

__author__ = 'Haoyan Huo'
__email__ = 'haoyan.huo@lbl.gov'
__maintainer__ = 'Haoyan Huo'

__protocol_version__ = 20210316

__all__ = [
    'compute_corrected_dhf',
    'compute_corrected_dgf',
    'query_system',
    'MPThermoDatabase',
    'database',
]

rester = MPRester()

Carbon = Element('C')
Oxygen = Element('O')

def compute_corrected_dhf(mp_entry: ComputedEntry) -> float:
    """
    Compute corrected :math:`\\Delta H_f(0K)` in ev/atom for a material in
    the Materials Project.

    We apply two corrections as follows:

    1. Elemental contributions as used in the Materials Project.
    2. For carbonates, we apply a correction for CO32- anion (fitted by Haoyan).

    :param mp_entry: A computed entry retrieved from the Materials Project.
    :return: The corrected formation enthalpy at zero Kelvin.
    """
    comp = mp_entry.composition

    dhf = mp_entry.energy

    # Fix CO3
    if Carbon in comp and np.isclose(comp.get(Oxygen, 0) / comp[Carbon], 3):
        dhf -= MP_OXYGEN_CORRECTION * comp[Oxygen]
        dhf += CO3_CORRECTION * comp[Carbon]

    for element, fraction in comp.items():
        pure = query_system(element.symbol)[0]
        dhf -= fraction * pure.energy / sum(pure.composition.values())

    return dhf / sum(comp.values())

def compute_corrected_dgf(mp_entry: ComputedEntry, temperature: float = 300) -> float:
    """
    Compute corrected :math:`\\Delta G_f(T)` in ev/atom for a material in
    the Materials Project.

    We apply three corrections as follows:

    1. Elemental contributions as used in the Materials Project.
    2. For carbonates, we apply a correction for CO32- anion (fitted by Haoyan).
    3. Chris Bartels' finite :math:`dG_f(T)` correction.

    :param mp_entry: A computed entry retrieved from the Materials Project.
    :param temperature: Temperature for which :math:`dG_f(T)` will be computed.
    :return: The corrected gibbs energy of formation at the temperature specified.
    """
    if len(mp_entry.composition) == 1:
        return 0
    dhf = compute_corrected_dhf(mp_entry)
    dgf = finite_dg_correction(mp_entry, temperature, dhf)
    return dgf

def _cache_valid(content):
    if not isinstance(content, dict):
        return False
    if content.get('protocol_version', 0) < __protocol_version__:
        return False

    return True

@lru_cache(maxsize=128)
def _read_or_download_cache(system):
    cache_dir = os.path.join(
        CACHE_DIR,
        'mp_cache')

    if not os.path.exists(cache_dir):
        os.makedirs(cache_dir)

    cache_fn = os.path.join(cache_dir, f'{system}.pickle')

    def _return_cache():
        if os.path.exists(cache_fn):
            with open(cache_fn, 'rb') as cache_f:
                content = pickle.load(cache_f)
                if _cache_valid(content):
                    return content['data']
        raise ValueError('Cache missing or invalid.')

    try:
        return _return_cache()
    except (ValueError, pickle.UnpicklingError, EOFError):
        pass

    lock = FileLock(os.path.join(cache_dir, 'write.lock'))

    max_retry = 5
    for i in range(max_retry):
        with lock:
            # Someone else who locked the file may have already written this
            # file. So try to read again.
            try:
                return _return_cache()
            except (ValueError, pickle.UnpicklingError, EOFError):
                pass

        try:
            start = time.time()
            result = rester.get_entries(
                system,
                inc_structure='final',
                sort_by_e_above_hull=True,
                property_data=['volume', 'icsd_ids'])
            logging.debug('Querying MP for %s took %.3f seconds.', system, time.time() - start)

            with lock:
                with open(cache_fn, 'wb') as cache_write_f:
                    pickle.dump({
                        'data': result,
                        'protocol_version': __protocol_version__
                    }, cache_write_f)

            return result
        except MPRestError:
            if i == max_retry - 1:
                raise
            time.sleep(i + 1)

def query_system(system: Union[List[str], str]) -> List[ComputedEntry]:
    """
    Query the Materials Project to get a list of compositions within a system or a specific
    composition. There are multiple ways of specifying a system:

    1. List of chemical elements, e.g., `Ba-Ti-O`.
    2. Exact composition, e.g., `BaCO3`.

    :param system: Chemical system identifier.
    :returns: List of matched entries in the Materials Project.
    """
    if isinstance(system, list):
        assert len([Element(x) for x in system]) > 0
        system_s = '-'.join(sorted(system))
    elif system in _pt_data:
        system_s = system
    elif isinstance(system, str):
        composition = C(system)
        assert len(composition) > 0
        system_s = '-'.join(sorted(x.symbol for x in composition))
        data = _read_or_download_cache(system_s)

        target_comp = composition.reduced_formula
        entries = list(filter(
            lambda x: x.composition.reduced_formula == target_comp,
            data))
        return entries
    else:
        raise TypeError(f'Invalid material specifier, '
                        f'must be a list of elements or a '
                        f'chemical formula, got {system}')

    return _read_or_download_cache(system_s)

class MPThermoDatabase:
    """
    Thermo database by using MP data.
    Just to keep a same interface as ExpThermoDatabase.
    """

    def __str__(self):
        return '<Calculated thermo database using MP data>'

    __repr__ = __str__

    @staticmethod
    def dgf(composition: Comp, temperature: float, unit='ev') -> float:
        """Compute dGf formation gibbs free energy at finite temperature"""
        composition = C(composition)
        entry = query_system(composition.reduced_formula)[0]
        value = compute_corrected_dgf(entry, temperature=temperature)

        if unit == 'ev/atom':
            return value

        value *= sum(composition.values())
        return convert_unit(value, unit, fromunit='ev')

    @staticmethod
    def dhf(composition: Comp,
            # Keep compatible with other APIs
            temperature: float,  # pylint: disable=unused-argument
            unit='ev') -> float:
        """Compute dHf formation enthalpy at finite temperature"""
        composition = C(composition)
        entry = query_system(composition.reduced_formula)[0]
        value = compute_corrected_dhf(entry)

        if unit == 'ev/atom':
            return value

        value *= sum(composition.values())
        return convert_unit(value, unit, fromunit='ev')

database = MPThermoDatabase()


import argparse
import app.config as config

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-m", "--model", help="The histogram model to use. Choose from the following options: 'rgb', 'hsv' or 'gray'. Leave empty to train using all 3 histogram models.")
    parser.add_argument("--mode", required=True, help="The mode to run the code in. Choose from the following options: 'train', 'test' or 'segment'.")
    parser.add_argument("--showhists", action="store_true", help="Specify whether you want to display each generated histogram.")
    parser.add_argument("-d", "--debug", action="store_true", help="Specify whether you want to print additional logs for debugging purposes.")
    args = parser.parse_args()
    config.debug = args.debug
    config.mode = args.mode
    config.show_histograms = args.showhists
    config.model = args.model
    if config.mode == "train":
        off_line_colour_based_feature_extraction_phase()
    elif config.mode == "test":
        on_line_retrieval_phase()
    elif config.mode == "segment":
        segment_video()
    else:
        print("Wrong mode chosen. Choose from the following options: 'train', 'test' or 'segment'.")
        exit(0)


import sys
input = sys.stdin.readline

N = int(input())
weights = list(map(int, input().split()))

weights.sort()

beforeWeights = 0
for num in weights:
  if beforeWeights + 1 < num:
    break
  beforeWeights += num

print(beforeWeights + 1)


"""Pipeline compiler for bundling modern JavaScript with rollup.js.

Version Added:
    4.0
"""

import os
import re
from pathlib import Path
from typing import List, Optional, Tuple

from pipeline.conf import settings
from pipeline.compilers import SubProcessCompiler

from djblets.pipeline.compilers.mixins import SourceMapStaleCheckMixin

#: The default regular expression used for rollup.js index files.
#:
#: Version Added:
#:     4.0
DEFAULT_FILE_PATTERN = r'index\.(js|es6|es6\.js|ts)$'

_match_regex = re.compile(settings.get('ROLLUP_FILE_PATTERN') or
                          DEFAULT_FILE_PATTERN)

class RollupCompiler(SourceMapStaleCheckMixin, SubProcessCompiler):
    """A Pipeline compiler for interfacing with rollup.js.

    `rollup.js <https://rollupjs.org/>`_ is a module bundler for JavaScript
    that compiles down a JavaScript codebase, linked together through module
    imports (ES6, CommonJS etc.), into a single JavaScript file. It can manage
    compilation with Babel or TypeScript, and supports a wide variety of
    plugins.

    This compiler makes it easy to develop modern JavaScript while still
    taking advantage of the best of Pipeline, such as managing separate
    bundles, loading bundles into Django templates, and triggering automatic
    recompilation on demand if any files (including those imported via modules)
    in the bundle are stale.

    To use this compiler, you will need an ``index`` file in your Pipeline
    JavaScript bundle, which imports any other modules that should be part
    of that compiled rollup.js bundle. An ``index`` file can be in the
    following forms:

    * :file:`index.js`
    * :file:`index.es6`
    * :file:`index.es6.js`
    * :file:`index.ts`

    Custom filename patterns can be specified by setting
    ``settings.PIPELINE['ROLLUP_FILE_PATTERN']`` to a regular expression string
    (defaults to :py:data:`DEFAULT_FILE_PATTERN`).

    Your Pipeline bundle may consist of just this one ``index`` file, or it
    may include other JavaScript as well. Each ``index`` will be compiled as
    its own rollup.js bundle, and its compiled contents included as part of the
    Pipeline bundle.

    Sourcemaps are used to check if any parts of the bundle are stale,
    triggering automatic recompilation when loading pages. Please note that
    any modules that you want included but are not actively being used within
    that rollup.js bundle may be excluded from automatic recompilation
    detection if you have treeshaking enabled (which is enabled by default).

    You will need to set ``settings.PIPELINE['ROLLUP_BINARY']`` to the path
    of :file:`rollup` and then set any command line arguments needed
    (such as a path to your configuration file) in
    ``settings.PIPELINE['ROLLUP_ARGUMENTS']``.

    The arguments should *not* include ``-c`` / ``--config`` to specify the
    ``rollup.config.js`` path. This will be computed automatically, in order
    to ensure the right file is used based on whichever source tree may be
    hosting the input file (such as when a project is consuming another
    project's source files and compiling them).

    Version Added:
        4.0
    """

    output_extension = 'js'

    def match_file(
        self,
        path: str,
    ) -> bool:
        """Return whether this compiler matches a source file.

        This will look for the following filenames by default:

        * :file:`index.js`
        * :file:`index.es6`
        * :file:`index.es6.js`
        * :file:`index.ts`

        To customize this, set ``settings.PIPELINE['ROLLUP_FILE_PATTERN']``
        to a regular expression string.

        Args:
            path (str):
                The path to the source file being considered.

        Returns:
            bool:
            ``True`` if this compiler will invoke rollup.js on this file.
            ``False`` if it will not.
        """
        return _match_regex.match(os.path.basename(path)) is not None

    def compile_file(
        self,
        infile: str,
        outfile: str,
        outdated: bool = False,
        force: bool = False,
    ) -> None:
        """Compile a file using rollup.js.

        This will cause the file and anything it imports in the tree into
        a rollup.js bundle, for inclusion in the Pipeline bundle.

        Args:
            infile (str):
                The source file.

            outfile (str):
                The destination file.

            outdated (bool, optional):
                Whether the destination file is known to be outdated.

            force (bool, optional):
                Whether to force re-running rollup.js on this file.
        """
        if outdated or force:
            # Look for the root of a tree, containing any of a number of
            # build configuration files.
            tree_root, rollup_config_path = \
                self._find_tree_root(os.path.dirname(infile))

            args: List[str] = [
                settings.ROLLUP_BINARY,
            ]

            if rollup_config_path:
                # We found a rollup.config.js. Make sure we tell rollup.js to
                # use it explicitly.
                args += [
                    '--bundleConfigAsCjs',
                    '-c',
                    rollup_config_path,
                ]

            args += [
                settings.ROLLUP_ARGUMENTS,
                '--sourcemap',
                '-i',
                infile,
                '-o',
                outfile,
            ]

            self.execute_command(args, cwd=tree_root)

    def _find_tree_root(
        self,
        start_dir: str,
    ) -> Tuple[Optional[str], Optional[str]]:
        """Return the root of a source tree for an input file.

        This will scan up the tree, looking for a :file:`rollup.config.js` or
        :file:`.babelrc`. This is used to try to find the proper working
        directory needed to successfully compile a file.

        This is important when a project is consuming and building another
        project's static media, so that the consumed project's configuration
        is applied.

        Args:
            start_dir (str):
                The starting directory for the search.

        Returns:
            str:
            The root of the source tree, or ``None`` if it could not be found.
        """
        path = Path(start_dir)
        root = Path(path.root)

        while path != root:
            rollup_config_path = Path(path / 'rollup.config.js')

            if rollup_config_path.exists():
                # This is the ideal result. We found the top-level of the
                # tree and the rollup.config.js file.
                return str(path), str(rollup_config_path)
            elif Path(path / '.babelrc').exists():
                # We didn't find rollup.config.js, but we found .babelrc.
                # Consider this the top of the tree.
                return str(path), None

            path = path.parent

        return None, None


import pyaudio
import wave
import threading
import atexit
from AudioConverter import AudioConverter

class Player(object):
    def __init__(self, inFile, interactiveUI, rate=44100):
        self.rate = rate
        self.wf = wave.open(AudioConverter(inFile), 'rb')

        self.slider = interactiveUI.ui.horizontalSlider
        self.interactiveUI = interactiveUI
        self.slider.setMinimum(0)
        self.slider.setMaximum(self.wf.getnframes())
        self.slider.setValue(0)
        self.numFrames = 0

        self.p = pyaudio.PyAudio()
        self.stream = self.p.open(format=self.p.get_format_from_width(self.wf.getsampwidth()),
                                  channels=self.wf.getnchannels(),
                                  rate=self.wf.getframerate(),
                                  output=True,
                                  stream_callback=self.new_frame)
        self.stop = False
        self.pause = False
        atexit.register(self.close)

    def new_frame(self, data, frame_count, time_info, status):
        self.numFrames = self.numFrames + frame_count
        self.slider.setValue(self.numFrames)

        data = self.wf.readframes(frame_count)

        len_default = 2048
        if len(data) < len_default:
            self.stop = True
            self.interactiveUI.playerDone.done()

        return (data, pyaudio.paContinue)

    def close(self):

        self.stream.close()
        self.wf.close()
        self.p.terminate()


import argparse
import os.path
import pandas as pd

fio_input_names = [
    'bs', 'iodepth', 'threads', 'lat_min', 'lat_max', 'lat_avg', 'lat_stdev',
    'ops', 'lat_pctl_99.0', 'lat_pctl_99.9', 'lat_pctl_99.99',
    'lat_pctl_99.999', 'bw_avg', 'bw_min', 'bw_max',
    'iops_avg', 'iops_min', 'iops_max', 'cpuload']

fio_nsec_2_usec_names = [
    'lat_min', 'lat_max', 'lat_avg', 'lat_stdev', 'lat_pctl_99.0',
    'lat_pctl_99.9', 'lat_pctl_99.99', 'lat_pctl_99.999']

fio_KiBps_2_Gbps_names = ['bw_avg', 'bw_min', 'bw_max']

KiBpbs_2_Gbps = 1024 * 8 / 1000 / 1000 / 1000

fio_output_names = [
    'threads', 'iodepth', 'bs', 'ops', 'lat_min', 'lat_max', 'lat_avg',
    'lat_stdev', 'lat_pctl_99.0', 'lat_pctl_99.9', 'lat_pctl_99.99',
    'lat_pctl_99.999', 'bw_min', 'bw_max', 'bw_avg',
    'iops_min', 'iops_max', 'iops_avg', 'cpuload']

ib_lat_input_names = [
    'bs', 'ops', 'lat_min', 'lat_max', 'lat_mode', 'lat_avg', 'lat_stdev',
    'lat_pctl_99.0', 'lat_pctl_99.9']

ib_lat_output_names = [
    'bs', 'ops', 'lat_min', 'lat_max', 'lat_avg', 'lat_stdev', 'lat_pctl_99.0',
    'lat_pctl_99.9']

ib_bw_input_names = [
    'threads', 'iodepth', 'bs', 'ops', 'bw_peak', 'bw_avg', 'msg_rate']

ib_bw_output_names = [
    'threads', 'iodepth', 'bs', 'ops', 'bw_avg']

def main():
    parser = argparse.ArgumentParser(
        description='Standardize a CSV (EXPERIMENTAL)')
    parser.add_argument('csv_file', metavar='CSV_FILE',
        help='a CSV log file to process')
    parser.add_argument('--csv_type', metavar='CSV_TYPE', required=True,
        choices=['ib_lat', 'ib_bw', 'fio'], help='a type of the CSV file')
    parser.add_argument('--output_file', metavar='OUTPUT_FILE',
        default='output.csv', help='an output file')
    args = parser.parse_args()

    if args.csv_type == 'ib_lat':
        df = pd.read_csv(args.csv_file, header=0, names=ib_lat_input_names)
        df = df.reindex(columns=ib_lat_output_names)
    elif args.csv_type == 'ib_bw':
        df = pd.read_csv(args.csv_file, header=0, names=ib_bw_input_names)
        df = df.reindex(columns=ib_bw_output_names)
        df = df.apply(lambda x: round(x, 2) \
            if x.name == 'bw_avg' else x)
    else: # fio
        df = pd.read_csv(args.csv_file, header=0, names=fio_input_names)
        # convert nsec to usec
        df = df.apply(lambda x: round(x / 1000, 2) \
            if x.name in fio_nsec_2_usec_names else x)
        # convert KiB/s to Gb/s
        df = df.apply(lambda x: round(x * KiBpbs_2_Gbps, 2) \
            if x.name in fio_KiBps_2_Gbps_names else x)
        df = df.reindex(columns=fio_output_names)

    _, ext = os.path.splitext(args.output_file)
    if ext == '.csv':
        df.to_csv(args.output_file, index=False)
    elif ext == '.json':
        df.to_json(args.output_file, orient='records')
    else:
        raise Exception(f"Unsupported output file extension: {ext}")

if __name__ == "__main__":
    main()


#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Create Date    :  2017-08-10 09:43
# Git Repo       :  https://github.com/Jerry-Ma
# Email Address  :  jerry.ma.nk@gmail.com
"""
core.py

The core functionalities
"""

from __future__ import (absolute_import, division, print_function)
import re
import os
import stat
import logging
from datetime import datetime
import shutil
from shutilwhich import which
from astropy.io.misc import yaml
from astropy.io import fits
from astropy.table import Table, Column, unique
from multiprocessing import Pool, cpu_count
from functools import partial
from io import StringIO

from . import qa
from . import pipeline
from .apus import core as apuscore
from .apus.common import touch_file

def setup_workdir(workdir=".", overwrite_dir=False, backup_config=True):
    """
    Setup execution environment and dump default configuration file

    Parameters
    ----------
    workdir: str
        The directory to store the created dirs and files.

    require_empty: bool
        when set True, raise RuntimeError if the workdir is not empty

    backup_config: bool
        when not require_empty, this controls whether the files are
        get backed up when an overwriting is about to occur.
    """
    logger = logging.getLogger(name="setup")

    logdir = 'logs'  # stores logs for the apus pipelines
    externdir = 'extern'  # stores external dependencies
    tmpdir = 'tmp'  # stores temporary files
    configfile = 'postcalib.yaml'  # main configuration file

    # validate the workdir
    workdir_content = os.listdir(workdir)
    if workdir_content:
        if not overwrite_dir:
            # skip setup when all files are present
            if all(d in workdir_content for d in [
                    logdir, externdir, tmpdir, configfile]):
                raise RuntimeError(
                    "it seems that the workdir has been setup already. "
                    "Re-run with -f to proceed anyway")
            else:
                raise RuntimeError(
                    "the workdir is not empty, re-run with -f to proceed "
                    "anyway")
        else:
            logger.warning(
                    "the workdir is not empty but a forced setup is requested")
    logger.info("setup workdir {}".format(workdir))

    # external dependencies
    externdir = os.path.join(workdir, externdir)
    which_path = os.path.abspath(externdir) + ':' + os.environ['PATH']
    if os.path.isdir(externdir):
        logger.warning("use existing extern dir {}".format(externdir))
    else:
        os.makedirs(externdir)
        logger.info("create extern dir {}".format(externdir))
    logger.info("check external dependencies")
    astromatic_prefix = []
    for name, cmds, datadir in [
            ("SExtractor", ("sex", 'ldactoasc'), "sextractor"),
            ("SCAMP", ("scamp", ), "scamp"),
            ("SWarp", ("swarp", ), "swarp")]:
        cmds = [which(cmd, path=which_path) for cmd in cmds]
        if any(c is None for c in cmds):
            raise RuntimeError("not able to locate {}"
                               .format(name))
        prefix = os.path.normpath(
                os.path.join(os.path.dirname(cmds[0]), '../'))
        datadir = os.path.join(prefix, "share", datadir)
        if not os.path.exists(datadir):
            raise RuntimeError(
                "not able to locate data files for {0}. It is likely that {0} "
                "is compiled within the source directory but without proper "
                "installation. To resolve the issue, either run `make install`"
                " in the source directory, or manually link the data "
                "directory to {1}.".format(name, datadir))
        logger.info("{0:10s} ... OK".format(name))
        astromatic_prefix.append(prefix)
    if len(set(astromatic_prefix)) > 1:
        raise RuntimeError(
            "it seems that the SExtractor, SCAMP and SWarp are not installed "
            "into the same prefix. PostCalib for now does not deal with this "
            "situation. Try re-configure SExtractor, SCAMP and SWarp with "
            "--prefix=<prefixpath>")
    astromatic_prefix = os.path.normpath(astromatic_prefix[0])
    logger.info("use shared astromatic prefix {}".format(
        astromatic_prefix))
    stilts_cmd = which("stilts", path=which_path)
    if stilts_cmd is None:
        logger.warning("not able to find stilts. Get from internet")
        # retrieve stilts
        from astropy.utils.data import download_file
        stilts_jar_tmp = download_file(
                "http://www.star.bris.ac.uk/%7Embt/stilts/stilts.jar",
                cache=True)
        stilts_jar = os.path.join(externdir, 'stilts.jar')
        shutil.copyfile(stilts_jar_tmp, stilts_jar)
        stilts_cmd = os.path.join(externdir, 'stilts')
        with open(stilts_cmd, 'w') as fo:
            fo.write("""#!/bin/sh
java -Xmx4000M -classpath "{0}:$CLASSPATH" uk.ac.starlink.ttools.Stilts "$@"
""".format(os.path.abspath(stilts_jar)))
        os.chmod(stilts_cmd, os.stat(stilts_cmd).st_mode | stat.S_IEXEC)
    logger.info("{0:10s} ... OK".format("stilts"))
    logger.info("use stilts {}".format(stilts_cmd))
    funpack_cmd = which("funpack", path=which_path)
    if funpack_cmd is None:
        logger.warning("not able to find funpack. Get from internet")
        # retrieve stilts
        from astropy.utils.data import download_file
        funpack_tmp = download_file(
                "http://heasarc.gsfc.nasa.gov/FTP/software/fitsio/c/"
                "cfitsio_latest.tar.gz",
                cache=True)
        funpack_src = os.path.join(externdir, 'cfitsio')
        import tarfile
        with tarfile.open(funpack_tmp) as tar:
            tar.extractall(path=externdir)
        logger.warning("try compiling funpack")
        import subprocess
        try:
            for command in [
                    './configure', 'make', 'make fpack', 'make funpack']:
                subprocess.check_call(command.split(), cwd=funpack_src)
        except subprocess.CalledProcessError:
            raise RuntimeError("unable to compile funpack")
        funpack_cmd = os.path.join(externdir, 'funpack')
        shutil.copy(
                os.path.join(externdir, 'cfitsio', 'funpack'),
                funpack_cmd)
    logger.info("{0:10s} ... OK".format("funpack"))
    logger.info("use funpack {}".format(funpack_cmd))

    # create logging directory
    logdir = os.path.join(workdir, logdir)
    if os.path.isdir(logdir):
        logger.warning("use existing log dir {}".format(logdir))
    else:
        os.makedirs(logdir)
        logger.info("create log dir {}".format(logdir))

    # setup scratch space
    tmpdir = os.path.join(workdir, tmpdir)
    # def freespace_GiB(path):
    #     stat = os.statvfs(path)
    #     return stat.f_bfree * stat.f_frsize / 1024 ** 3

    # logger.info("{:.2f} GiB free in {}".format(
    #     freespace_GiB(scratch_dir), scratch_dir))
    if os.path.isdir(tmpdir):
        logger.warning("use existing tmp dir {}".format(tmpdir))
    else:
        os.makedirs(tmpdir)
        logger.info("create tmp dir {}".format(tmpdir))

    # dump default config
    time_fmt = "%b-%d-%Y_%H-%M-%S"
    base_fmt = ("{obsid}_{object}_odi_{band}")
    config = """## config file of PostCalib
## {time:s}

# setting
phot_model_flags: 'color,ota,cat'

# qa inputs
qa_headers: [
    'OBSID', 'CRVAL1', 'CRVAL2', 'OBJECT', 'EXPMEAS', 'AIRMASS',
    'SEEING', 'SKY_MEDI', 'SKY_STD',
    'FILTER', 'INSTRUME', 'MJD-OBS'
    ]

# naming
reg_ppa: '{reg_ppa:s}'  # regex to parse the default filenames from PPA
fmt_orig: 'orig_{base_fmt}.{{ext}}'
reg_orig: '{reg_orig:s}'  # regex to parse the filenames in job-in dir

reg_inputs: '{reg_inputs:s}'  # regex to parse the filenames in jobdir
fmt_inputs: 'orig_{base_fmt}.{{ext}}'  # format string for image in jobdir
sel_inputs: ['orig_*.fits', 'orig_*.fits.fz']

fmt_masked: 'masked_{base_fmt:s}.fits'  # format string of masked images
sel_masked: 'masked_*.fits'   # selection string of masked images

fmt_selected: '{{ppflag}}{{imflag}}_{base_fmt:s}.fits'
sel_fcomb: 'fcomb[0-9]*_masked_*.fits'
fmt_objcat: '{{ppflag}}objcat_{base_fmt:s}.cat'  # format of object catalog
fmt_objmask: '{{ppflag}}objmask_{base_fmt:s}.fits'  # format of object masks
sel_objmask: 'fcomb[0-9]*_objmask_*.fits'   # selection string of object masks
fmt_sky: '{{ppflag}}sky_{base_fmt:s}.fits'  # format string of sky images
fmt_fcomb: '{{ppflag}}combined.fits'
reg_fcomb: '{reg_fcomb:s}'  # regex to parse grouped master
fmt_fsmooth: '{{ppflag}}smoothed.fits'

sel_fsub: 'fsub[0-9]*_masked_*.fits'
fmt_fsub_fsmooth: 'fcomb{{grpid}}_smoothed.fits'  # fmt for subtract fcomb
fmt_fsub: 'fsub_{base_fmt:s}.fits'

sel_fsubed: 'fsub_*.fits'
sel_phot: 'phot[0-9]*_*_*.fits'
fmt_photcat: '{{ppflag}}{{imflag}}_{base_fmt:s}.cat'
fmt_photcat_cleaned: '{{ppflag}}{{imflag}}_{base_fmt:s}.cln.cat'
fmt_photcat_matched: '{{ppflag}}{{imflag}}_{base_fmt:s}.zp.cat'
fmt_phot: '{{ppflag}}odi_{{band}}.cat'
reg_phot: '{reg_phot:s}'  # regex to parse grouped phot master
phot_hdr_suffix: 'hdr_phot'
phot_hdr_glob: 'phot[0-9]*_*{{obsid}}*odi_{{band}}.hdr_phot'

sel_mosaic: 'mosaic[0-9]*_*_*.fits'
fmt_mosaic_orig: 'swarp{{grpid}}_{{imflag}}_odi_{{band}}.fits'
fmt_mosaic_hdr: 'coadd{{grpid}}.mosaic'
fmt_mosaic: 'coadd{{grpid}}_{{imflag}}_odi_{{band}}.fits'
fmt_mosaic_wht: 'coadd{{grpid}}_{{imflag}}_odi_{{band}}.wht.fits'
reg_mosaic: {reg_mosaic:s}
reg_mosaic_fits: {reg_mosaic_fits:s}
fmt_msccat_matched: 'coadd{{grpid}}{{imflag}}_odi_{{band}}.zp.cat'

reg_grp: '{reg_grp:s}'  # regex to parse grouped images
fmt_grp: '{{ppflag}}{{grpid}}_{{imflag}}_{base_fmt:s}.fits'

# environ
workdir: {workdir}
tmpdir: {tmpdir}
logdir: {logdir}
astromatic_prefix: {astromatic_prefix}
stilts_cmd: {stilts_cmd}
funpack_cmd: {funpack_cmd}
""".format(time=datetime.now().strftime(time_fmt),
           version="0.0",
           reg_ppa=r'(?P<obsid>20\d{6}T\d{6}\.\d)_(?P<object>.+?)'
                   r'_odi_(?P<band>[ugriz])\.(?P<procid>\d{4})\.'
                   r'(?P<ext>fits|fits\.fz)$',
           reg_orig=r'(?P<ppflag>[^_/]+_)?(?P<imflag>[^_/]+)_'
                    r'(?P<obsid>20\d{6}T\d{6}\.\d)_(?P<object>.+?)'
                    r'_odi_(?P<band>[ugriz])'
                    r'\.(?P<ext>fits|fits\.fz)$',
           reg_inputs=r'(?P<ppflag>[^_/]+_)?(?P<imflag>[^_/]+)_'
                    r'(?P<obsid>20\d{6}T\d{6}\.\d)_(?P<object>.+?)'
                    r'_odi_(?P<band>[ugriz])'
                    r'\.(?P<ext>[^/]+)$',
           reg_fcomb=r'(?P<ppflag>[^_/]+_)(?P<imflag>[^/]+)\.fits',
           reg_phot=r'(?P<ppflag>[^_/]+)_odi_(?P<band>[ugriz])'
                    r'\.(?P<ext>[^/]+)',
           reg_mosaic=r'(?P<ppflag>[a-z]+)(?P<grpid>\d+)_(?P<imflag>[^_/]+)'
                      r'_odi_(?P<band>[ugriz])\.(?P<ext>[^/]+)$',
           reg_mosaic_fits=r'(?P<ppflag>[a-z]+)(?P<grpid>\d+)'
                           r'_(?P<imflag>[^_/]+)'
                           r'_odi_(?P<band>[ugriz])\.fits$',
           reg_grp=r'(?P<ppflag>[a-z]+)(?P<grpid>\d+)_(?P<imflag>[^_/]+)_'
                    r'(?P<obsid>20\d{6}T\d{6}\.\d)_(?P<object>.+?)'
                    r'_odi_(?P<band>[ugriz])'
                    r'\.(?P<ext>[^/]+)$',
           # reg_inputs=r'(?P<ppflag>[^_/]+_)?(?P<imflag>[^_/]+)_'
           #            r'(?P<obsid>20\d{6}T\d{6}\.\d)_(?P<object>.+?)'
           #            r'_odi_(?P<band>[ugriz])'
           #            r'_(?P<featgrp>\d+)_(?P<photgrp>\d+)_(?P<mscgrp>\d+)'
           #            r'\.(?P<ext>fits|fits\.fz)$',
           **locals())
    configfile = os.path.join(workdir, configfile)
    if os.path.exists(configfile):
        if backup_config:
            timestamp = datetime.fromtimestamp(
                os.path.getmtime(configfile)).strftime(time_fmt)
            bakfile = os.path.join(
                workdir,
                "{1}_{0}{2}".format(timestamp, *os.path.splitext(
                    os.path.basename(configfile)))
                )
            logger.warning("backup existing config file to {}".format(
                bakfile))
            os.rename(configfile, bakfile)
        else:
            logger.warning(
                    "overwrite existing config file {}".format(configfile))
    with open(configfile, 'w') as fo:
        fo.write(config)

def _qa_worker(image, config):
    """Create a QA summary for given input image"""
    headers = config['qa_headers']
    values = []
    try:
        with fits.open(image, memmap=True) as hdulist:
            for key in headers:
                try:
                    val = hdulist[0].header[key]
                except KeyError:
                    val = hdulist[1].header[key]
                except KeyError:
                    val = ""
                if key == "OBJECT":
                    # escape name
                    val = re.sub(r'\s+', '_', val.strip())
                values.append(val)
            # QA image
            preview = qa.create_preview(hdulist=hdulist)
            preview.save()
            # mask guide ota
            values.insert(0, ','.join(map(str, preview.guide_otas)))
            # append filename
            values.append(image)
        keys = ['mask_otas', ] + headers + ['filename', ]
        return keys, values
    except Exception as e:
        raise e
        return None, None

def init_job(config_file, jobkey, images,
             overwrite_dir=False, backup_jobfile=True, dirs_only=False):
    logger = logging.getLogger(jobkey + ".init")

    # load config file
    with open(config_file, 'r') as fo:
        logger.info("use config file {}".format(config_file))
        logger.info("\n\n{}\n".format(fo.read()))
        fo.seek(0)
        config = yaml.load(fo)

    if dirs_only:
        logger.warning("init job {} dirs only".format(jobkey))

    # create or get input dir
    workdir = config['workdir']
    jindir = os.path.join(workdir, jobkey + ".in")
    logger.info("initialize inputs of job {}".format(jobkey))
    if os.path.exists(jindir):
        if overwrite_dir:
            logger.warning("overwrite existing job inputs dir {}".format(
                jindir))
        else:
            raise RuntimeError(
                "job inputs dir {} exists. Re-run with -f to overwrite".format(
                    jindir))
    else:
        os.makedirs(jindir)
        logger.info("create job inputs dir {}".format(jindir))

    if not dirs_only:
        # purge any existing good links that matches the linked name regex
        reg_orig = re.compile(config['reg_orig'])
        for filename in os.listdir(jindir):
            filename = os.path.join(jindir, filename)
            if os.path.islink(filename) and re.match(
                    reg_orig, os.path.basename(filename)):
                logger.warning("x {}".format(filename))
                os.unlink(filename)
                # remove preview files if any
                previewname = filename.rsplit(".fz", 1)[0].rsplit(
                        ".fits", 1)[0] + '.png'
                if os.path.exists(previewname) and not os.path.islink(
                        previewname):
                    logger.warning("x {}".format(previewname))
                    os.remove(previewname)

        # link over to job-in dir
        reg_ppa = re.compile(config['reg_ppa'])
        linked = []
        for i, image in enumerate(images):
            imagebase = os.path.basename(image)
            parsed_filename = re.match(reg_ppa, imagebase)
            if parsed_filename is None:
                logger.info("skip unmatched file {}".format(image))
                continue
            parsed_filename = parsed_filename.groupdict()
            linkname = config['fmt_orig'].format(**parsed_filename)
            linkname = linkname.replace('*', 'x')
            link = os.path.join(jindir, linkname)
            logger.info("{} -> {}".format(imagebase, link))
            os.symlink(os.path.abspath(image), link)
            linked.append(link)  # update to the link path
        images = linked
        logger.info("{} input images linked".format(len(images)))

        # run QA
        # pool = Pool(1)
        pool = Pool(cpu_count())
        qa_rets = pool.map_async(
                partial(_qa_worker,  config=config),
                images).get(9999999)
        # qa_rets = []
        # for image in qa_images:
        #     qa_rets.append(_qa_worker(image, config=config))

        qa_keys = []
        qa_vals = []
        for i, (k, v) in enumerate(qa_rets):
            if k is None:
                logger.warning("corrupted: {}".format(images[i]))
            else:
                qa_keys.append(k)
                qa_vals.append(v)
        # tabulate QA results
        qa_tbl = Table(
                rows=qa_vals, names=qa_keys[0],
                )
        # prepend grouping table
        qa_tbl.add_column(Column(range(len(qa_tbl)), name='numid'), index=0)
        for i, (group, group_key) in enumerate([
                ('fcomb', "FILTER"), ('fsub', 'FILTER'),
                ('phot', 'FILTER'),
                ('mosaic', 'OBJECT')]):
            colname = group + "_group"
            keys = qa_tbl[group_key]
            unique_keys = list(unique(qa_tbl[[group_key]])[group_key])
            col = Column([unique_keys.index(k) for k in keys])
            qa_tbl.add_column(col, index=i + 1, name=colname)  # after numid

        logger.info("summary of input images\n{}".format(
            "\n".join(qa_tbl.pformat(max_lines=-1, max_width=-1))))
        # save the table
        tblname = os.path.join(jindir, '{}.txt.in'.format(jobkey))

        def quote(x):
            x = str(x)
            if ' ' in x or x == "":
                return "\"{}\"".format(x)
            else:
                return x
        _fo = StringIO()
        qa_tbl.write(_fo, format='ascii.fixed_width', delimiter=" ",
                     formats={c: quote for c in qa_tbl.colnames})
        with open(tblname, 'w') as fo:
            fo.write('#' + _fo.getvalue()[1:])
        # copy table to work_dir for the next step
        jobfile = os.path.join(workdir, '{}.txt'.format(jobkey))

        if os.path.exists(jobfile):
            if backup_jobfile:
                time_fmt = "%b-%d-%Y_%H-%M-%S"
                timestamp = datetime.fromtimestamp(
                    os.path.getmtime(jobfile)).strftime(time_fmt)
                bakfile = os.path.join(
                    workdir,
                    "{1}_{0}{2}".format(timestamp, *os.path.splitext(
                        os.path.basename(jobfile)))
                    )
                logger.warning("backup existing job file to {}".format(
                    bakfile))
                os.rename(jobfile, bakfile)
            else:
                logger.warning(
                        "overwrite existing job file {}".format(jobfile))

        shutil.copy(tblname, jobfile)

    # create skymask dir
    skymask_dir = os.path.join(workdir, jobkey + ".skymask")
    if os.path.exists(skymask_dir):
        logger.warning("overwrite existing skymask dir {}".format(skymask_dir))
    else:
        os.makedirs(skymask_dir)
        logger.info("create sky mask dir {}".format(skymask_dir))
    with open(os.path.join(skymask_dir, 'README'), 'w') as fo:
        fo.write("""# Auto-generated README file, do not edit.

User can create and add DS9 region files to mask out
very bright stars that are hard to get rid of automatically.

The naming of the created DS9 region files could be
    1.  "skymask.reg"
        This file serves as the "global" mask, i.e., entries of this file
        are used to mask all images.

    2. *{obsid}*.reg
        This file is used when creating sky image from the image with "OBSID"
        keyword = obsid. The OBSID is a UTC time-like string that identifies
        uniquely the image, e.g. "20140925T201215.6".
        Example of valid region filenames:
            20140925T201215.6.reg
            20140925T201215.6_bright.reg
            gsc_20140925T201215.6.reg

The content of the region file should typically contain entries that are
specified in sky coordinates (RA, Dec). The file is parsed and processed with
python package "pyregion", which have good support of a variety of region
shapes. Please check https://pyregion.readthedocs.io/en/latest/ for more
details.""")
    touch_file(os.path.join(skymask_dir, 'skymask.reg'))

    # create job config
    # jobconfig =

def run_pipeline(config_file, jobfile, apus_args=None):
    jobkey = os.path.splitext(os.path.basename(jobfile))[0]
    logger = logging.getLogger(jobkey)
    logger.info("run pipeline {}".format(jobkey))

    # load config file
    with open(config_file, 'r') as fo:
        logger.info("use config file {}".format(config_file))
        # logger.info("\n\n{}\n".format(fo.read()))
        fo.seek(0)
        config = yaml.load(fo)

    # create jobdir
    workdir = config['workdir']
    jobdir = os.path.join(workdir, jobkey)
    if os.path.exists(jobdir):
        logger.warning("use existing jobdir {}".format(jobdir))
    else:
        os.makedirs(jobdir)
        logger.info("create job inputs dir {}".format(jobdir))

    # create job config if not exists
    job_configfile = os.path.join(workdir, f'{jobkey}.yaml')
    if not os.path.exists(job_configfile):
        touch_file(job_configfile)
    with open(job_configfile, 'r') as fo:
        jobconfig = yaml.load(fo)
        if jobconfig is None:
            jobconfig = dict()

    config['jobdir'] = jobdir
    config['jobfile'] = jobfile
    config['jobconfig'] = jobconfig
    config['jobkey'] = jobkey
    config['skymask_dir'] = jobdir + ".skymask"
    config['bpmask_dir'] = jobdir + ".bpmask"

    logger.info("arguments passed to Apus {}".format(apus_args))
    logger.propagate = False
    # call apus
    apuscore.bootstrap(
            dict(
                jobkey=jobkey,
                jobdir=jobdir,
                logdir=config['logdir'],
                env_overrides={
                    'path_prefix': config['astromatic_prefix'],
                    'tmpdir': config['tmpdir']},
                tlist=pipeline.get_tlist(config),
                ),
            apus_args
            )
    # # create or get input dir
    # workdir = config['workdir']
    # jobdir = os.path.join(workdir, jobkey)
    # if os.path.exists(jobdir):
    #     logger.warning("use existing jobdir {}".format(jobdir))
    # else:
    #     os.makedirs(jobdir)
    #     logger.info("create job inputs dir {}".format(jobdir))


"""11051 이항 계수 2"""

# 부동소수점때문에 직접 구현한 factorial은 숫자가 커질수록 오차가 커짐
# 그렇기에 math 모듈에 내장된 factorial함수를 가져옴
from math import factorial

num, k = map(int, input().split())  # 이항계수 (num, k)가 주어짐
result = factorial(num) // (factorial(k) * factorial(num - k))
print(result % 10007)

# --------------------------------------------------------------------------------------------------
# 파스칼의 삼각형을 이용하여 푼 방법
# 파스칼 삼각형은 맨 왼쪽과 오른쪽은 모두 1이고 나머지는 두 대각선 위에 있는 값을 더한 값을 갖는 삼각형
n, k = map(int, input().split())
d = []

for i in range(n + 1):
    d.append([1] * (i + 1))

for i in range(2, n + 1):
    for j in range(1, i):
        d[i][j] = (d[i - 1][j - 1] + d[i - 1][j]) % 10007

print(d[n][k])


from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.urls import reverse

from .models import Order
from .forms import OrderForm

@login_required
def homepage(request):
    context = {}
    return render(request, 'example/homepage.html', context)

@login_required
def orders_list(request):
    orders = Order.objects.order_by('pk')
    context = {'orders': orders}
    return render(request, 'example/orders_list.html', context)

@login_required
def edit_order(request, pk=None):
    if request.method == "POST":
        if pk:
            order = Order.objects.get(pk=pk)
            form = OrderForm(request.POST, instance=order)
        else:
            form = OrderForm(request.POST)
        if form.is_valid():
            form.save()
        return HttpResponseRedirect(reverse('orders_list'))
    else:
        order = None
        if pk is None:
            form = OrderForm()
        else:
            order = get_object_or_404(Order, pk=pk)
            form = OrderForm(instance=order)
        context = {'order': order, 'form': form}
        return render(request, 'example/edit_order.html', context)


import tensorflow as tf
import tf_helper_functions as tf_helper
import os

class SCHUFAModel:

    def __init__(self, config):
        self.config = config
        self.build_model()
        self.saver = tf.train.Saver()

    def build_model(self):

        self.x = tf.placeholder(dtype=tf.float32, shape=[None, self.config["inputs"]], name="x")

        with tf.name_scope("dnn_model"):
            #input layer(hidden 1)
            self.w1 = tf_helper.weights([self.config["inputs"], self.config["hidden1"]], name="w1")
            b1 = tf_helper.bias([self.config["hidden1"]])
            z1 = tf_helper.dense_layer(self.x, self.w1, b1, activation=tf.nn.relu)
            z1_drop = tf.nn.dropout(z1, self.config["dropout_prob"])

            #hidden layer 2
            self.w2 = tf_helper.weights([self.config["hidden1"], self.config["hidden2"]], name="w2")
            b2 = tf_helper.bias([self.config["hidden2"]])
            z2 = tf_helper.dense_layer(z1_drop, self.w2, b2, activation=tf.nn.relu)
            z2_drop = tf.nn.dropout(z2, self.config["dropout_prob"])

            #hidden layer 3
            self.w3 = tf_helper.weights([self.config["hidden2"], self.config["hidden3"]], name="w3")
            b3 = tf_helper.bias([self.config["hidden3"]])
            z3 = tf_helper.dense_layer(z2_drop, self.w3, b3, activation=tf.nn.relu)
            z3_drop = tf.nn.dropout(z3, self.config["dropout_prob"])

            #output layer
            self.w4 = tf_helper.weights([self.config["hidden3"], self.config["outputs"]], name="w4")
            b4 = tf_helper.bias([self.config["outputs"]])
            z4 = tf_helper.dense_layer(z3_drop, self.w4, b4, activation=None)

            '''
            self.logits = z3
            values, indices = tf.nn.top_k(tf.nn.softmax(self.logits))
            self.predictions = indices
            '''
        with tf.name_scope("loss"):
            if(self.config["loss"] == "log_loss"):
                self.y = tf.placeholder(dtype=tf.int32, shape=(None, self.config["outputs"]), name="y")
                self.logits = tf.nn.sigmoid(z4)

                #binarize logits for final predictions
                prediction_thresh = tf.constant(0.5, dtype=tf.float32)
                self.predictions=tf.cast(tf.greater_equal(self.logits, prediction_thresh), tf.float32)
                self.loss = tf.losses.log_loss(labels=self.y, predictions=self.logits)

            #Perform a softmax to a weights mean squared sum .
            elif(self.config["loss"] == "weighted_squares"):
                self.y = tf.placeholder(dtype=tf.int32, shape=(None), name="y")
                self.logits = tf.nn.softmax(z4)

                #prediction equals the largest value of the softmax
                self.predictions = self.logits[:, 0] < self.logits[:, 1]

                y = tf.one_hot(indices=tf.squeeze(self.y), depth=2)
                square_diff = tf.square(y - self.logits)
                good_cred, bad_cred = tf.split(value=square_diff, num_or_size_splits=2, axis=1)
                self.loss = self.config["false_neg_loss"]*tf.reduce_sum(good_cred) + self.config["false_pos_loss"]*tf.reduce_sum(bad_cred)

            #Perfrom a softmax to cross entropy
            else:
                self.y = tf.placeholder(dtype=tf.int32, shape=(None, self.config["outputs"]), name="y")
                y = tf.squeeze(self.y)
                self.logits = tf.nn.softmax(z4)
                _, self.predictions = tf.nn.top_k(self.logits)
                self.xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=z4)
                self.loss = tf.reduce_mean(self.xentropy, name="loss")

        with tf.name_scope("lipshitz_regularization"):
            #
            self.w1_projection, self.w1_norm = tf_helper.lipschitz_projection(self.w1, p_norm=self.config["p_norm"], lamda=self.config["lamda1"])
            self.w2_projection, self.w2_norm = tf_helper.lipschitz_projection(self.w2, p_norm=self.config["p_norm"], lamda=self.config["lamda2"])
            self.w3_projection, self.w3_norm = tf_helper.lipschitz_projection(self.w3, p_norm=self.config["p_norm"], lamda=self.config["lamda3"])

        with tf.name_scope("training_op"):
            self.learning_rate = tf.Variable(self.config["learning_rate"])
            #optimizer = tf.train.MomentumOptimizer(self.learning_rate, momentum=self.config["momentum"], use_nesterov=True)
            optimizer = tf.train.GradientDescentOptimizer(self.learning_rate)
            #computes gradients and applys gradients
            self.training_op = optimizer.minimize(self.loss)

        self._build_metrics()

    def _build_metrics(self):

        with tf.name_scope("accuracy"):
            self.train_acc, self.train_acc_op = tf.metrics.accuracy(labels=self.y, predictions=self.predictions, name="train_acc")
            self.test_acc, self.test_acc_op = tf.metrics.accuracy(labels=self.y, predictions=self.predictions, name="test_acc")

            #keeps a handle on local accuracy variables
            self.train_acc_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="accuracy/train_acc")
            self.test_acc_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="accuracy/test_acc")
            self.train_acc_vars_init = tf.variables_initializer(var_list=self.train_acc_vars)
            self.test_acc_vars_init = tf.variables_initializer(var_list=self.test_acc_vars)

        with tf.name_scope("precision"):
            self.train_prec, self.train_prec_op = tf.metrics.precision(labels=self.y, predictions=self.predictions, name="train_prec")
            self.test_prec, self.test_prec_op = tf.metrics.precision(labels=self.y, predictions=self.predictions, name="test_prec")

            #keeps a handle on local precision variables
            self.train_prec_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="precision/train_prec")
            self.test_prec_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="precision/test_prec")
            self.train_prec_vars_init = tf.variables_initializer(var_list=self.train_prec_vars)
            self.test_prec_vars_init = tf.variables_initializer(var_list=self.test_prec_vars)

        with tf.name_scope("recall"):
            self.train_rec, self.train_rec_op = tf.metrics.recall(labels=self.y, predictions=self.predictions, name="train_rec")
            self.test_rec, self.test_rec_op = tf.metrics.recall(labels=self.y, predictions=self.predictions, name="test_rec")

            #keeps a handle on local precision variables
            self.train_rec_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="recall/train_rec")
            self.test_rec_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="recall/test_rec")
            self.train_rec_vars_init = tf.variables_initializer(var_list=self.train_rec_vars)
            self.test_rec_vars_init = tf.variables_initializer(var_list=self.test_rec_vars)

        with tf.name_scope("cost_matrix"):
            self.cost_matrix = [tf.count_nonzero(self.y < tf.cast(self.predictions, tf.int32))]
    def save_model(self, session, save_dir, file_name="model.ckpt"):
        self.saver.save(session, os.path.join(save_dir, file_name))


def can_transmute(element):
    '''Can this element be turned into gold?'''

    # Bug #172: make sure the input is actually an element.
    assert is_valid_element(element)

    # Gold is trivial.
    if element is Gold:
        return True

    # Trans-uranic metals and halogens are impossible.
    if (element.atomic_number > Uranium.atomic_number) or \
       (element in Halogens):
        return False

    # Look for a sequence of steps that leads to gold.
    steps = search_transmutations(element, Gold)
    if steps == []:
        return False
    else:
        # Bug #201: must be at least two elements in sequence.
        assert len(steps) >= 2
        return True


# 通过不断循环取余,对应的余数乘以10,然后再累加,就可以得到结果
# 只不过要注意对负数的处理,同时注意数值的取值范围

class Solution:
    def reverse(self, x):
        result = 0
        flag = -1 if x < 0 else 1
        x = abs(x)
        while x != 0:
            pop = x % 10
            x = x / 10
            result = result * 10 + pop
        result *= flag
        if result < - 2 ** 31 or result > 2 ** 31 -1:
            return 0
        return result


import tempfile
import senet.core.ecmwf_utils as eu
# snappy_utils should be imported last, as it modifies the system path
import senet.core.snappy_utils as su
import datetime

def prepare(elevation_map:str, ecmwf_data_file:str, date_time_utc:datetime.datetime, time_zone:float, output_file:str, elevation_band:str = "elevation",
    prepare_temperature:bool = True, prepare_vapour_pressure:bool = True, prepare_air_pressure:bool = True,
    prepare_wind_speed:bool = True, prepare_clear_sky_solar_radiation:bool = True, prepare_daily_solar_irradiance:bool = True):
    """Prepares ERA5 reanalysis surface meteorological data based on the ECMWF ERA5 reanalysis data and the high resolution DEM.

    Args:
        elevation_map (str): Path to high resolution DEM (output of elevation graph)
        ecmwf_data_file (str): Path to ECMWF NetCDF file
        date_time_utc (datetime.datetime): Date and time (UTC) for which to prepare meteorological data (YYYY-MM-DD HH:MM)
        output_file (str): Path to save file
        time_zone (float): Time zone of the center of area of interest
        elevation_band (str, optional): Name of elevation band. Defaults to "elevation"
        prepare_temperature (bool, optional): Prepare temperature. Defaults to True
        prepare_vapour_pressure (bool, optional): Prepare vapour pressure. Defaults to True
        prepare_air_pressure (bool, optional): Prepare air pressure. Defaults to True
        prepare_wind_speed (bool, optional): Prepare wind speed. Defaults to True
        prepare_clear_sky_solar_radiation (bool, optional): Prepare clear sky solar radiation. Defaults to True
        prepare_daily_solar_irradiance (bool, optional): Prepare daily solar irradiance. Defaults to True
    """
    # Save elevation to GeoTIFF because it will need to be read by GDAL later
    temp_file = tempfile.NamedTemporaryFile(suffix=".tif", delete=False)
    temp_elev_path = temp_file.name
    temp_file.close()
    su.copy_bands_to_file(elevation_map, temp_elev_path, [elevation_band])

    # Calculate required meteorological parameters
    bands = []
    if prepare_temperature:
        data = eu.get_ECMWF_data(ecmwf_data_file, 'air_temperature', date_time_utc, temp_elev_path,
                                 time_zone)
        bands.append({'band_data': data, 'band_name': 'air_temperature', 'description':
                      'Air temperature at 100 m above surface(K)'})
    if prepare_vapour_pressure:
        data = eu.get_ECMWF_data(ecmwf_data_file, 'vapour_pressure', date_time_utc, temp_elev_path,
                                 time_zone)
        bands.append({'band_data': data, 'band_name': 'vapour_pressure', 'description':
                      'Surface vapour pressure (mb)'})
    if prepare_air_pressure:
        data = eu.get_ECMWF_data(ecmwf_data_file, 'air_pressure', date_time_utc, temp_elev_path,
                                 time_zone)
        bands.append({'band_data': data, 'band_name': 'air_pressure', 'description':
                      'Surface air pressure (mb)'})
    if prepare_wind_speed:
        data = eu.get_ECMWF_data(ecmwf_data_file, 'wind_speed', date_time_utc, temp_elev_path,
                                 time_zone)
        bands.append({'band_data': data, 'band_name': 'wind_speed', 'description':
                      'Wind speed at 100 m above surface (m/s)'})
    if prepare_clear_sky_solar_radiation:
        data = eu.get_ECMWF_data(ecmwf_data_file, 'clear_sky_solar_radiation', date_time_utc,
                                 temp_elev_path, time_zone)
        bands.append({'band_data': data, 'band_name': 'clear_sky_solar_radiation', 'description':
                      'Instantenous clear sky surface solar irradiance (W/m^2)'})
    if prepare_daily_solar_irradiance:
        data = eu.get_ECMWF_data(ecmwf_data_file, 'average_daily_solar_irradiance', date_time_utc,
                                 temp_elev_path, time_zone)
        bands.append({'band_data': data, 'band_name': 'average_daily_solar_irradiance',
                      'description': 'Average daily solar irradiance (W/m^2)'})

    # Save the output file
    geo_coding = su.read_snappy_product(elevation_map, elevation_band)[1]
    su.write_snappy_product(output_file, bands, 'ecmwfData', geo_coding)


def compress(s):
    result = ""

    i = 0
    j = 0

    while i < len(s) and j < len(s):
        count = 0
        if s[i] == s[j]:
            j += 1
            if j == len(s):
                count = j - i
                if count == 1:
                    result += f"{s[i]}"
                else:
                    result += f"{count}{s[i]}"
        else:
            count = j - i
            if count == 1:
                result += f"{s[i]}"
            else:
                result += f"{count}{s[i]}"
            i = j
    return result

if __name__ == "__main__":
    print(compress("ccaaatsss"))
    print(compress('nnneeeeeeeeeeeezz'))
    print(compress('yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'))


import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__)))
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
from models.trainNhppNm import *
from datasets.synthetic.nhppnm import *
import matplotlib.pyplot as plt
import torch
from lib.animation import *
from examples.utilities import *

# Definition of the models
examples = dict()
# t=? gamma = 1.25
examples[3] = np.asarray( [ [[-15, 0], [15, 0], [0, 8], [0, -8]],
                            [[23, 0], [-23, 0], [0, -6], [0, 6]],
                            [[-18, 0], [18, 0], [0, 2], [0, -2]],
                            [[6, 0], [-6, 0], [0, 0], [0, 0]] ] )

########################################
########## Parameter Settings ##########
########################################
# Choose the model

# Set the time and other parameters
modelNumber = 3
seed = 0
gamma = 1.75
maxTime = 7  #5.5
trainingsetTime = 5
timeInterval = 0.01
windowSize = 1.0  #1.0
stepSize = 1.0 #1.0
# Set the number of epochs
numOfEpochs = 300
# Set the learning rate
lr = 0.1

# Extract the model parameters
z0 = examples[modelNumber]
order = z0.shape[0]
numOfNodes = z0.shape[1]
dim = z0.shape[2]
# Set the seed value for torch
torch.random.manual_seed(seed=seed)

# Animation
basedir = os.path.dirname(os.path.abspath(__file__))
filename = "model{}_seed={}_gamma={}_maxT={}_timeInter={}_windowSize={}_stepSize={}_numOfEpochs={}_lr={}"\
            .format(modelNumber, seed, gamma, maxTime, timeInterval, windowSize, stepSize, numOfEpochs, lr)

# Sample the event times
nm = NHPPNM(gamma=gamma, z0=z0, maxTime=maxTime, timeInterval=timeInterval, seed=seed)
networkEvents = nm.constructNetwork()

# Construct the training and testing sets
dataset = contructDataset(networkEvents, numOfNodes, windowSize, stepSize, maxTime)
trainSet, testSet = splitDataset(dataset=dataset, trainingsetTime=trainingsetTime)

# Learning the model
init_time = time.time()
nhppNmmModel = TrainNhppNm(trainSet=trainSet, testSet=testSet, numOfepochs=numOfEpochs, lr=lr,
                           numOfNodes=numOfNodes, dim=dim, order=order, numOfSamples=10, timeInterval=0.01, seed=seed)
train_loss, test_loss = nhppNmmModel.learn()
print("-> Total Running time: {}".format(time.time() - init_time))

# Define the node pairs
i, j = ( 0, 1 )
# Define the time points for the figures
timePoints = np.arange(0, maxTime, 0.1)

# Create a folder
basefolder = os.path.join(basedir, "../figures/", "example"+str(modelNumber))
if not os.path.isdir(basefolder):
    os.makedirs(name=basefolder)

# Animate the latent positions
plt.figure()
z_true = np.zeros(shape=(len(timePoints), numOfNodes, dim), dtype=np.float)
z_est = np.zeros(shape=(len(timePoints), numOfNodes, dim), dtype=np.float)
for inx in range(len(timePoints)):
    z_true[inx, :, :] = nm.getAllPositions(t=timePoints[inx])
    z_est[inx, :, :] = nhppNmmModel.getAllPositions(t=timePoints[inx]).detach().numpy()
anim = Animation(timePoints=timePoints,
                 r=1, c=2, figSize=(12, 6), bgColor='white',
                 color=['k', 'r', 'b', 'g'], marker='.', markerSize=10, delay=250, margin=[0.1, 0.1])
anim.addData(x=z_true, index=0, title="Ground-truth")
anim.addData(x=z_est, index=1, title="Estimation")
anim.plot(filePath=os.path.join(basefolder, "latentPositions_{}.gif".format(filename)))

# Plot the training and testing loss
plt.figure()
plt.title("Training Losses")
plt.plot(train_loss, 'r', label="Training")
plt.xlabel("Epochs")
plt.ylabel("Negative Log-likelihood")
plt.legend()
plt.savefig(os.path.join(basefolder, "trainLoss_{}.png".format(filename)))

# Plot the testing loss
plt.figure()
plt.title("Testing Losses")
plt.plot(test_loss, 'b', label="Testing")
plt.xlabel("Epochs")
plt.ylabel("Negative Log-likelihood")
plt.legend()
plt.savefig(os.path.join(basefolder, "testLoss_{}.png".format(filename)))

# Plot the node pair distances
plt.figure()
true_dist, est_dist = [], []
for t in timePoints:
    true_dist.append(nm.getDistanceBetween(i=i, j=j, t=t))
    est_dist.append(nhppNmmModel.getDistanceBetween(i=i, j=j, t=t).detach().numpy())

fig, ax = plt.subplots(1, 2, figsize=(12, 6))
ax[0].title.set_text("True Distances for ({} {}) pair".format(i, j))
ax[0].plot(timePoints, true_dist, 'k-')
ax[1].title.set_text("Estimated Distances".format(i, j))
ax[1].plot(timePoints, true_dist, 'r-')
ax[0].set_xlabel("Time")
ax[1].set_xlabel("Time")
plt.savefig(os.path.join(basefolder, "nodePairDistances_{}.png".format(filename)))

plt.figure()
plt.title("Squared Error for Intensity Function for ({} {}) pair")
intensityError = []
for t in timePoints:
    intensityError.append( (nm.computeIntensityForPair(i=i, j=j, t=t) - nhppNmmModel.computeIntensityForPair(i=i, j=j, t=t))**2 )
plt.plot(timePoints, intensityError, 'r-')
plt.xlabel("Time")
plt.savefig(os.path.join(basefolder, "intensityError_{}.png".format(filename)))

# Save the model parameters
torch.save(nhppNmmModel.state_dict(), os.path.join(basefolder, "modelParams_{}.pkl".format(filename)))

print("-> Train Set Ground-truth negative log-likelihood: {}".format(nm.getNLL(data=trainSet, numOfSamples=10)))
print("-> Train Set Estimated negative log-likelihood: {}".format(nhppNmmModel.forward(data=trainSet).detach().numpy()))
print("-> Test Set Ground-truth negative log-likelihood: {}".format(nm.getNLL(data=testSet, numOfSamples=10)))
print("-> Test Set Estimated negative log-likelihood: {}".format(nhppNmmModel.forward(data=testSet)))


def main() -> None:
    x1, y1, x2, y2 = map(int, input().split())

    diff_x = x2 - x1
    diff_y = y2 - y1
    x3 = x2 - diff_y
    y3 = y2 + diff_x
    x4 = x3 - diff_x
    y4 = y3 - diff_y

    print(f'{x3} {y3} {x4} {y4}')

if __name__ == '__main__':
    main()


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import sys

import mozinfo
import yaml
from marionette_driver.errors import JavascriptException, ScriptTimeoutException
from mozproxy import get_playback

AWSY_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if AWSY_PATH not in sys.path:
    sys.path.append(AWSY_PATH)

from awsy import process_perf_data, webservers
from awsy.awsy_test_case import AwsyTestCase

class TestMemoryUsage(AwsyTestCase):
    """Provides a test that collects memory usage at various checkpoints:
    - "Start" - Just after startup
    - "StartSettled" - After an additional wait time
    - "TabsOpen" - After opening all provided URLs
    - "TabsOpenSettled" - After an additional wait time
    - "TabsOpenForceGC" - After forcibly invoking garbage collection
    - "TabsClosed" - After closing all tabs
    - "TabsClosedSettled" - After an additional wait time
    - "TabsClosedForceGC" - After forcibly invoking garbage collection
    """

    def urls(self):
        return self._urls

    def perf_suites(self):
        return process_perf_data.PERF_SUITES

    def perf_checkpoints(self):
        return process_perf_data.CHECKPOINTS

    def perf_extra_opts(self):
        return self._extra_opts

    def setupTp5(self):
        urls = None
        default_tp5n_manifest = os.path.join(
            self._webroot_dir, "page_load_test", "tp5n", "tp5n.manifest"
        )
        tp5n_manifest = self.testvars.get("pageManifest", default_tp5n_manifest)
        with open(tp5n_manifest) as fp:
            urls = fp.readlines()
        # pylint --py3k: W1636
        urls = list(map(lambda x: x.replace("localhost", "localhost:{}"), urls))

        # We haven't set self._urls yet, so this value might be zero if
        # 'entities' wasn't specified.
        to_load = self.pages_to_load()
        if not to_load:
            to_load = len(urls)
        self._webservers = webservers.WebServers(
            "localhost", 8001, self._webroot_dir, to_load
        )
        self._webservers.start()
        for url, server in zip(urls, self._webservers.servers):
            self._urls.append(url.strip().format(server.port))

    def setupTp6(self):
        # tp5n stores its manifest in the zip file that gets extracted, tp6
        # doesn't so we just keep one in our project dir for now.
        default_tp6_pages_manifest = os.path.join(AWSY_PATH, "conf", "tp6-pages.yml")
        tp6_pages_manifest = self.testvars.get(
            "pageManifest", default_tp6_pages_manifest
        )
        urls = []
        with open(tp6_pages_manifest) as f:
            d = yaml.safe_load(f)
            for r in d:
                url = r["url"]
                if isinstance(url, list):
                    urls.extend(url)
                else:
                    urls.append(url)

        self._urls = urls

        # Indicate that we're using tp6 in the perf data.
        self._extra_opts = ["tp6"]

        if self.marionette.get_pref("fission.autostart"):
            self._extra_opts.append("fission")

        # Now we setup the mitm proxy with our tp6 pageset.
        tp6_pageset_manifest = os.path.join(AWSY_PATH, "tp6-pageset.manifest")
        config = {
            "playback_tool": "mitmproxy",
            "playback_version": "5.1.1",
            "playback_files": [tp6_pageset_manifest],
            "platform": mozinfo.os,
            "obj_path": self._webroot_dir,
            "binary": self._binary,
            "run_local": self._run_local,
            "app": "firefox",
            "host": "127.0.0.1",
            "ignore_mitmdump_exit_failure": True,
        }

        self._playback = get_playback(config)
        self._playback.start()

        # We need to reload after the mitmproxy cert is installed
        self.marionette.restart(in_app=False, clean=False)

        # Setup WebDriver capabilities that we need
        self.marionette.delete_session()
        caps = {
            "unhandledPromptBehavior": "dismiss",  # Ignore page navigation warnings
        }
        self.marionette.start_session(caps)
        self.marionette.set_context("chrome")

    def setUp(self):
        AwsyTestCase.setUp(self)
        self.logger.info("setting up")
        self._webroot_dir = self.testvars["webRootDir"]
        self._urls = []
        self._extra_opts = None

        if self.testvars.get("tp6", False):
            self.setupTp6()
        else:
            self.setupTp5()

        self.logger.info(
            "areweslimyet run by %d pages, %d iterations,"
            " %d perTabPause, %d settleWaitTime"
            % (
                self._pages_to_load,
                self._iterations,
                self._perTabPause,
                self._settleWaitTime,
            )
        )
        self.logger.info("done setting up!")

    def tearDown(self):
        self.logger.info("tearing down!")

        self.logger.info("tearing down webservers!")

        if self.testvars.get("tp6", False):
            self._playback.stop()
        else:
            self._webservers.stop()

        AwsyTestCase.tearDown(self)

        self.logger.info("done tearing down!")

    def clear_preloaded_browser(self):
        """
        Clears out the preloaded browser.
        """
        self.logger.info("closing preloaded browser")
        script = """
            if (window.NewTabPagePreloading) {
                return NewTabPagePreloading.removePreloadedBrowser(window);
            }
            return "NewTabPagePreloading.removePreloadedBrowser not available";
            """
        try:
            result = self.marionette.execute_script(script, script_timeout=180000)
        except JavascriptException as e:
            self.logger.error("removePreloadedBrowser() JavaScript error: %s" % e)
        except ScriptTimeoutException:
            self.logger.error("removePreloadedBrowser() timed out")
        except Exception:
            self.logger.error(
                "removePreloadedBrowser() Unexpected error: %s" % sys.exc_info()[0]
            )
        else:
            if result:
                self.logger.info(result)

    def test_open_tabs(self):
        """Marionette test entry that returns an array of checkpoint arrays.

        This will generate a set of checkpoints for each iteration requested.
        Upon successful completion the results will be stored in
        |self.testvars["results"]| and accessible to the test runner via the
        |testvars| object it passed in.
        """
        # setup the results array
        results = [[] for _ in range(self.iterations())]

        def create_checkpoint(name, iteration, minimize=False):
            checkpoint = self.do_memory_report(name, iteration, minimize)
            self.assertIsNotNone(checkpoint, "Checkpoint was recorded")
            results[iteration].append(checkpoint)

        # The first iteration gets Start and StartSettled entries before
        # opening tabs
        create_checkpoint("Start", 0)
        self.settle()
        create_checkpoint("StartSettled", 0)

        for itr in range(self.iterations()):
            self.open_pages()

            create_checkpoint("TabsOpen", itr)
            self.settle()
            create_checkpoint("TabsOpenSettled", itr)
            create_checkpoint("TabsOpenForceGC", itr, minimize=True)

            # Close all tabs
            self.reset_state()

            with self.marionette.using_context("content"):
                self.logger.info("navigating to about:blank")
                self.marionette.navigate("about:blank")
                self.logger.info("navigated to about:blank")
            self.signal_user_active()

            # Create checkpoint that may contain retained processes that will
            # be reused.
            create_checkpoint("TabsClosedExtraProcesses", itr)

            # Clear out the retained processes and measure again.
            self.clear_preloaded_browser()

            create_checkpoint("TabsClosed", itr)
            self.settle()
            create_checkpoint("TabsClosedSettled", itr)
            create_checkpoint("TabsClosedForceGC", itr, minimize=True)

        # TODO(ER): Temporary hack until bug 1121139 lands
        self.logger.info("setting results")
        self.testvars["results"] = results


import responses
from django.test import TestCase

from wagtail_toolbox.wordpress.api_client import Client

class TestClient(TestCase):
    """Test the Client class."""

    def setUp(self):
        self.test_url = "https://example.com/wp-json/wp/v2/posts"
        self.header_total_pages = "X-WP-TotalPages"
        self.header_total_results = "X-WP-Total"

    @responses.activate
    def test_get(self):
        """Test that the get method returns the correct JSON."""
        responses.add(
            responses.GET,
            self.test_url,
            status=200,
            json=[{"foo": "bar"}],
        )
        client = Client(self.test_url)
        self.assertEqual(client.get(self.test_url), [{"foo": "bar"}])

    @responses.activate
    def test_failed_get(self):
        """Test that the get method raises an exception if the request fails."""
        responses.add(
            responses.GET, self.test_url, body=Exception("Error fetching endpoint")
        )
        client = Client(self.test_url)
        with self.assertRaises(Exception):
            client.get(self.test_url)

    @responses.activate
    def test_get_total_pages(self):
        """Test that the get_total_pages property returns the correct number of pages."""
        responses.add(
            responses.GET,
            self.test_url,
            status=200,
            headers={self.header_total_pages: "2"},
        )
        client = Client(self.test_url)
        self.assertEqual(client.get_total_pages, 2)

    @responses.activate
    def test_get_total_results(self):
        """Test that the get_total_results property returns the correct number of results."""
        responses.add(
            responses.GET,
            self.test_url,
            status=200,
            headers={self.header_total_results: "2"},
        )
        client = Client(self.test_url)
        self.assertEqual(client.get_total_results, 2)

    @responses.activate
    def test_is_paged(self):
        """Test that the is_paged property returns True if the endpoint is paged."""
        responses.add(
            responses.GET,
            self.test_url,
            status=200,
            headers={self.header_total_pages: "2"},
        )
        client = Client(self.test_url)
        self.assertTrue(client.is_paged)

    @responses.activate
    def test_is_not_paged(self):
        """Test that the is_paged property returns False if the endpoint is not paged."""
        responses.add(
            responses.GET,
            self.test_url,
            status=200,
        )
        client = Client(self.test_url)
        self.assertFalse(client.is_paged)

    @responses.activate
    def test_paged_endpoints(self):
        """Test that the paged_endpoints property returns a list of URLs."""
        responses.add(
            responses.GET,
            self.test_url,
            status=200,
            headers={self.header_total_pages: "2"},
        )
        client = Client(self.test_url)
        for _ in client.paged_endpoints:
            self.assertEqual(
                client.paged_endpoints,
                [
                    f"{self.test_url}?page=1",
                    f"{self.test_url}?page=2",
                ],
            )