# Script by Carla Silva and Inês Dutra 2020 :: Graph Coloring Classical Version

""" Graph Coloring Problem

    Formulation of the problem for a graph G=(V,E) with a number of colors n.

"""

from pyqubo import Array, solve_qubo, Constraint
import matplotlib.pyplot as plt
import networkx as nx
import time
import sys
import random
import numpy as np
import seaborn as sns
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
import neal
import pandas as pd

def alpha(n,h):
  return(n+h)

def beta(n,alfa,h):
  return(((n**3+n**2+1)*alfa)+h)

def exp1(n, vc, B):
    exp = 0.0
    for i in range(n):
        for k in range(n):
            for kl in range(n):
                if (k != kl):
                    exp += Constraint(vc[i,k]*vc[i,kl], label="exp")
    exp = B*exp
    return(exp)

def exp2(n, vc, A):
    exp = 0.0
    for i in range(n):
      for k in range(n):
          exp += Constraint(1-vc[i,k], label="exp")
    exp = A*exp
    return(exp)

def exp3(n, vc, neigh, A):
    exp = 0.0
    for i in range(n):
        for il in range(n):
            if (i != il):
                for k in range(n):
                   exp += Constraint(vc[i,k]*vc[il,k]*neigh[i,il], label="exp")
    exp = A*exp
    return(exp)

def exp4(n, vc, c, A):
    exp = 0.0
    for i in range(n):
      for k in range(n):
        exp += Constraint(vc[i,k]*(1-c[k]), label="exp")
    exp = A*exp
    return(exp)

def exp5(n, c):
    exp = 0.0
    for k in range(n):
        exp += c[k]
    return(exp)

def col(n, decoded_solution):
    # Obtain colors of each vertex
    colors = [0 for i in range(n)]
    for i in range(n):
        for k in range(n):
          if decoded_solution['vc'][i][k] == 1:
            colors[i] = k
            break
    return(colors)

def graph(G, colors, n, A, B):
    # Plot graph after coloring
    f = plt.figure()
    colorlist = random.sample(list(sns.color_palette('hls')), n)
    nx.draw_networkx(G, node_color=[colorlist[colors[node]] for node in G.nodes], node_size=400, font_weight='bold', font_color='w')
    plt.axis("off")
    f.savefig('fig-n'+str(n)+'A'+str(A)+'B'+str(B)+'.pdf', bbox_inches='tight')
    dictionaryColors = dict(zip(list(G.nodes), [colorlist[colors[node]] for node in G.nodes]))
    return dictionaryColors

if __name__ == "__main__":

    n = int(sys.argv[1]) # Colors
    A = int(sys.argv[2]) # Alpha
    B = int(sys.argv[3]) # Beta

    h = 0.0000005 #small number

    #Test A and B equals alpha and beta
    #A = alpha(n,h)
    #B = beta(n,A,h)

    orig_stdout = sys.stdout

    f = open('graphColoringClassicalResults-n'+str(n)+'A'+str(A)+'B'+str(B)+'.txt', 'w')
    sys.stdout = f

    print("--------------------------------------------------------------------")
    print("\n# GRAPH COLORInG PROBLEM WITH n COLOURS On CLASSICAL SOLVER #\n")
    print("--------------------------------------------------------------------")

    G = nx.erdos_renyi_graph(n=n, p=0.5, seed=123, directed=False)

    print("Is graph connected?",nx.is_connected(G))

    E = G.edges

    neigh = nx.adjacency_matrix(G).todense()

    # Prepare a binary vector

    vc = Array.create('vc', (n, n), 'BINARY')

    c = Array.create('c', (n), 'BINARY')

    print("--------------------------------------------------------------------")
    print("1st expression:")
    print("--------------------------------------------------------------------")
    print(exp1(n, vc, B))

    print("--------------------------------------------------------------------")
    print("2nd expression:")
    print("--------------------------------------------------------------------")
    print(exp2(n, vc, A))

    print("--------------------------------------------------------------------")
    print("3rd expression:")
    print("--------------------------------------------------------------------")
    print(exp3(n, vc, neigh, A))

    print("--------------------------------------------------------------------")
    print("4th expression:")
    print("--------------------------------------------------------------------")
    print(exp4(n, vc, c, A))

    print("--------------------------------------------------------------------")
    print("5th expression:")
    print("--------------------------------------------------------------------")
    print(exp5(n, c))

    # Define hamiltonian H
    H = exp1(n, vc, B) + exp2(n, vc, A) + exp3(n, vc, neigh, A) + exp4(n, vc, c, A) + exp5(n, c)

    # Compile model
    model = H.compile()

    # Create QUBO
    qubo, offset = model.to_qubo()

    print("--------------------------------------------------------------------")
    print("\nQUBO:\n")
    print("--------------------------------------------------------------------")

    print(qubo)

    start_time = time.time()

    nr = 10000
    c = max(qubo.values())
    max_abs_value = float(max(abs(v) for v in qubo.values()))
    scale_qubo = {k: float(v) / max_abs_value for k, v in qubo.items()}
    sa = neal.SimulatedAnnealingSampler()
    sa_computation = sa.sample_qubo(scale_qubo, num_reads=nr, beta_range=(0.01,1), offset=offset, num_sweeps=5000, seed=123, chain_strength=c)

    elapsed_time = time.time() - start_time

    print("--------------------------------------------------------------------")
    print("\nCLASSICAL RESULTS:\n")
    print("--------------------------------------------------------------------")

    df = []
    for a,b,c in sa_computation.record:
        decoded_solution, broken, energy = model.decode_solution(a, vartype='BINARY')
        if not broken:
            d = 0
        else:
            d = broken["exp"]["penalty"]
        for key in range(0,n):
            decoded_solution['c']['c'+str(key)] = decoded_solution['c'].pop(key)
        for i in range(0,n):
            decoded_solution['vc']['vc'+str(i)] = decoded_solution['vc'].pop(i)
        for i in range(0,n):
            for j in range(0,n):
                decoded_solution['vc'].update( {'vc'+str(i)+str(j) : decoded_solution['vc']['vc'+str(i)][j]} )
            decoded_solution['vc'].pop('vc'+str(i))
        decoded_solution['c'].update(decoded_solution['vc'])
        df.append({"Sample": decoded_solution['c'], "Energy": energy, "Occurrences": c, "Broken chains": d})

    df = pd.DataFrame(df)
    df.to_csv('GC'+str(n)+'A'+str(A)+'B'+str(B)+'.csv',index=False)
    pd.set_option('display.float_format', lambda x: '%.20f' % x)
    pd.options.display.max_colwidth = 10000
    print(df.to_string(index=False))

    print("--------------------------------------------------------------------")
    print("\nSAMPLE WITH MINIMUM ENERGY:\n")
    print("--------------------------------------------------------------------")

    best = np.argmin(sa_computation.record.energy)
    best_solution = list(sa_computation.record.sample[best])

    print(dict(zip(sa_computation.variables, best_solution)))

    decoded_solution, broken, energy = model.decode_solution(best_solution, vartype="BINARY")
    print("number of broken constraint = {}".format(len(broken)))
    print(broken)
    print("energy = {}".format(energy))

    e = 99999
    for a,b,c in sa_computation.record:
        decoded_solution, broken, energy = model.decode_solution(a, vartype='BINARY')
        if energy < e:
            decoded_solution1 = decoded_solution
            e = energy

    # Match colors with each node
    colors = col(n, decoded_solution1)

    # Plot the colored graph
    nodesColor = graph(G, colors, n, A, B)

    print("--------------------------------------------------------------------")
    print("\nTIME (sec):\n")
    print("--------------------------------------------------------------------")
    print(elapsed_time,"All results",elapsed_time/nr,"One result")

    print("--------------------------------------------------------------------")
    print("\nList of graph colors:\n")
    print("--------------------------------------------------------------------")
    print(nodesColor)

    print("--------------------------------------------------------------------")
    print("\nNumber of nodes and number of colors:\n")
    print("--------------------------------------------------------------------")
    print(len(nodesColor)," nodes and ",len(set(list(nodesColor.values())))," colors")

    sys.stdout = orig_stdout
    f.close()


import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
import os
import sys
from netCDF4 import Dataset,num2date
from prueb_nombr1 import *
#from datetime import datetime, date, time, timedelta
#from pylab import *

name='SPREAD_1980_2009_pleased03.nc'
varsp = 'pcp'
tname='time'

ncinsp = Dataset('/Users/fredm/Desktop/TFG_DEFINITIVO/Bases_de_datos/Datos_Reales/%s'%(name), 'r')
prsp = ncinsp.variables[varsp][:]
lonssp = ncinsp.variables['lon'][:]
latssp = ncinsp.variables['lat'][:]
nctime=ncinsp.variables['time'][:]
t_unit = ncinsp.variables[tname].units
t_cal = ncinsp.variables[tname].calendar

tvalue = num2date(nctime,units = t_unit,calendar = t_cal)
ncinsp.close()

agnos = [i.year for i in tvalue]
meses = [i.month for i in tvalue]
dias = [i.day for i in tvalue]

coord=np.loadtxt('/Users/fredm/Desktop/TFG_DEFINITIVO/6_comps/KMEANS/regiones.txt',dtype=int)
#print(np.shape(coord))
n_class=int(len(coord[0,:])/2)
prsp=prsp[:,:,:]
print(np.shape(prsp))
rain=np.ones((np.size(prsp,axis=0),n_class))

for i in range(0,n_class):
    y=coord[:,2*i]!=0
    prz1=prsp[:,coord[:,2*i+1][y],coord[:,2*i][y]]
    rain[:,i]=np.sum((prz1),axis=1)/sum(y)

ARR=np.zeros((len(rain[1:,0]),n_class+3))

#print(rain[-2])
ARR[:,3:]=rain[1:]
ARR[:,0]=dias[:-1]
ARR[:,1]=meses[:-1]
ARR[:,2]=agnos[:-1]
#print(ARR[0:2,:])
np.savetxt('/Users/fredm/Desktop/TFG_DEFINITIVO/Ficheros/Datos_Reales/precxreg_med_%s.txt'%(new_name(name)),ARR,delimiter=' , ',fmt='%.1f')


from github import Github, RateLimitExceededException
import os
import logging
import pickle
import traceback

from time import sleep
import datetime

from repo_stats import get_commit_activities, get_feature_files, get_languages, get_commits_with_given, date_of_last_commit, contains_cucumber, contains_given
from decorators import retry

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()

g = Github(os.environ['GITHUBTOKEN'])

state = {}

def load_state():
    with open('data/repo_stats.pickle', 'rb') as file:
        state = pickle.load(file)
    return state

def save_to_file(name, state):
    with open(f'data/{name}.pickle', 'wb') as file:
        pickle.dump(state, file)

@retry
def update_repo_info(repo_data):
    repo = g.get_repo(repo_data['full_name'])
    # repo_data["name"] = repo.name
    # repo_data["full_name"] = repo.full_name
    # repo_data["url"] = repo.html_url
    # repo_data["fork"] = repo.fork
    # repo_data["num_forks"] = repo.forks_count
    # repo_data["num_contributors"] = retry(
    #     lambda repo: repo.get_contributors().totalCount
    # )(repo)
    # repo_data["num_commits"] = retry(
    #     lambda repo: repo.get_commits().totalCount
    # )(repo)
    # repo_data["num_stars"] = repo.stargazers_count
    # repo_data["num_watchers"] = repo.subscribers_count
    # repo_data["commit_activities"] = get_commit_activities(repo)
    # repo_data["issues_closed"] = retry(
    #     lambda repo: repo.get_issues(state="closed").totalCount
    # )(repo)
    # repo_data["issues_all"] = retry(
    #     lambda repo: repo.get_issues(state="all").totalCount
    # )(repo)
    # repo_data["pull_requests_closed"] = retry(
    #     lambda repo: repo.get_pulls(state="closed").totalCount
    # )(repo)
    # repo_data["pull_requests_all"] = retry(
    #     lambda repo: repo.get_pulls(state="all").totalCount
    # )(repo)
    # repo_data["comments"] = retry(
    #     lambda repo: repo.get_comments().totalCount
    # )(repo)
    # repo_data["languages"] = get_languages(repo)
    repo_data["date_of_last_commit"] = date_of_last_commit(repo)
    repo_data["commits_with_given"] = get_commits_with_given(repo, g)
    repo_data["file_cucumber"] = contains_cucumber(repo, g)
    repo_data["file_given"] = contains_given(repo, g)
    repo_data["files_feature"] = get_feature_files(repo, g)

def main():
    state = load_state()
    print(state)
    counter = 0
    for repo in state['repos']:
        logger.info(repo['full_name'])
        update_repo_info(repo)
        counter += 1
        if counter >= 10:
            counter = 0
            save_to_file('auto_save_update', state)

if __name__ == "__main__":
    try:
        main()
    except:
        try:
            with open('data/crash/update_repo-crash.pickle', 'wb') as file:
                pickle.dump(state, file)
        except:
            with open('crash.pickle', 'wb') as file:
                pickle.dump(state, file)
        logger.info('state saved in data')
        raise
    else:
        save_to_file('updated_repo_stats', state)


from astropy.io import fits
from pypeit.display import display
import numpy as np
from photutils import RectangularAperture, aperture_photometry
from astropy.stats import sigma_clipped_stats
from scipy.ndimage import gaussian_filter
from photutils import DAOStarFinder
from pypeit.images import buildimage
from pypeit.spectrographs.util import load_spectrograph

# The following code is from MOSFIRE.CSU
def mm_to_pix(millimeters):
    demagnification = 7.24254
    return millimeters/demagnification/0.018

# following code comes from old trunk of MOSFIRE code tree:
# http://code.google.com/p/mosfire/source/browse/MOSFIRE/CSU.py?r=823640967a0497bd7bafbb0f8147228c3d3993bd

def csu_mm_to_pix(x_mm, slitno):
    """
    Convert a slit's position into a pixel value. This is a linear approximation to a sixth order polynomial fit by ccs from cooldown 8 data.
    """

    center_pix = (1042.986, 1035.879)
    numslits = 46
    rotation = np.radians(0.2282)
    tempscale = 0.99646
    mm = 1

    # _kfp is keck focal plane, the x_kfp has a "fudge factor"
    x_kfp = x_mm * tempscale - 5.0 * 1.2 * mm
    y_kfp = 5.8*mm*tempscale*(numslits - slitno + 0.70)
    # _mfp is Mosfire focal plane, convert the mms into pixels
    x_mfp = 2047 - mm_to_pix(x_kfp)
    y_mfp = mm_to_pix(y_kfp)

    # Rotate around the center
    x_mfp -= center_pix[0]
    y_mfp -= center_pix[1]

    x_mfp = np.cos(rotation)*x_mfp - np.sin(rotation)*y_mfp
    y_mfp = np.sin(rotation)*x_mfp + np.cos(rotation)*y_mfp
    x_mfp += center_pix[0]
    y_mfp += center_pix[1]

    return np.array([x_mfp, y_mfp])

ZP = {
    'Y':28.13,
    'J':27.89,
    'H':27.54,
    'Ks':26.93,
    'J2':26.93,
    'J3':26.88,
    'H1':26.88,
    'H2':26.85
}
'''
qso_obj_file = '/Users/joe/Downloads/MF.20200528.40601.fits'
qso_sky_file = '/Users/joe/Downloads/MF.20200528.40505.fits'
star_obj_file = '/Users/joe/Downloads/MF.20200528.40413.fits'
star_sky_file = '/Users/joe/Downloads/MF.20200528.40360.fits'
'''
qso_obj_file = '/home/riccardo/Downloads/MF.20200528.40601.fits'
qso_sky_file = '/home/riccardo/Downloads/MF.20200528.40505.fits'
star_obj_file = '/home/riccardo/Downloads/MF.20200528.40413.fits'
star_sky_file = '/home/riccardo/Downloads/MF.20200528.40360.fits'

# Build Science image
spectrograph = load_spectrograph('keck_mosfire')
det = 1
parset = spectrograph.default_pypeit_par()
parset['scienceframe']['process']['use_illumflat'] = False
parset['scienceframe']['process']['use_pixelflat'] = False

skyImg = buildimage.buildimage_fromlist(
    spectrograph, det, parset['scienceframe'], [qso_sky_file], ignore_saturation=False)
sys.exit(-1)
# Read in data
#star_obj = fits.getdata(star_obj_file)
#star_hdr = fits.getheader(star_obj_file)
#star_sky = fits.getdata(star_sky_file)
#star_sky_hdr = fits.getheader(star_sky_file)
#star_diff = star_obj - star_sky
#qso_obj = fits.getdata(qso_obj_file)
#qso_hdr =fits.getheader(qso_obj_file)
#qso_sky = fits.getdata(qso_sky_file)
qso_sky_hdr =fits.getheader(qso_sky_file)
qso_diff = qso_obj - qso_sky

# Get bar information from header
xpix = np.zeros(92)
ypix = np.zeros(92)
for islit in range(1, 93):
    slit = int(islit + 1) / 2
    pos = qso_sky_hdr["B%0.2iPOS" % islit]
    xpix[islit-1], ypix[islit-1] = csu_mm_to_pix(pos, slit)

display.connect_to_ginga(raise_err=True, allow_new=True)
viewer, ch_sky = display.show_image(qso_diff,'QSO_SKY') #, cuts = (-5.0*qso_sigma, 5.0*qso_sigma))
display.show_points(viewer, ch_sky, ypix, xpix)

sys.exit(-1)

plate_scale = 0.18 # Put in correct value
centroid=[star_hdr['CRPIX1']+15,star_hdr['CRPIX2']+20]
lengthx=round(2/plate_scale/2)
lengthy=round(3.5/plate_scale/2)

# Determine acquisition window location # Find the centroid of the image
sky_mean, sky_med, sky_sigma = sigma_clipped_stats(qso_sky[centroid[1]-lengthy:centroid[1]+lengthy, centroid[0]-lengthx:centroid[0]+lengthx],sigma_lower=5.0, sigma_upper=5.0)
acq_mask = (qso_sky > (sky_med -2.0*sky_sigma)) & (qso_sky < (sky_med + 2.0*sky_sigma))
# qso_mean, qso_med, qso_sigma = sigma_clipped_stats(qso_diff[centroid[0]-lengthx:centroid[0

#acq_mask = np.zeros_like(star_sky, dtype=bool)

'''
fwhm = 3.0
star_smooth = gaussian_filter(star_diff, sigma=fwhm)
qso_smooth = gaussian_filter(qso_diff, sigma=fwhm)

# Find the centroid of the image
#star_mean, star_med, star_sigma = sigma_clipped_stats(star_diff[centroid[0]-lengthx:centroid[0]+lengthx, centroid[1]-lengthy:centroid[1]+lengthy],
#                                                      sigma_lower=5.0, sigma_upper=5.0)
#qso_mean, qso_med, qso_sigma = sigma_clipped_stats(qso_diff[centroid[0]-lengthx:centroid[0]+lengthx, centroid[1]-lengthy:centroid[1]+lengthy],
#                                                   sigma_lower=5.0, sigma_upper=5.0)
daofind = DAOStarFinder(fwhm=5.0, threshold=5.*sky_sigma)
sources = daofind(star_diff)

# Define coordinates and apertures
aperture = RectangularAperture([sources['xcentroid'], sources['ycentroid']], lengthx*2, lengthy)
box_mask = aperture.to_mask(method='center')
back_aperture = RectangularAperture([sources['xcentroid'], sources['ycentroid']], lengthx*2, lengthy*2)

# Estimate background and aperture photometry for the star
phot_star = aperture_photometry(star_diff, aperture)
phot_back_star = aperture_photometry(star_diff, back_aperture)
counts_star=phot_star['aperture_sum']
counts_back_star=(phot_back_star['aperture_sum']-phot_star['aperture_sum'])/(back_aperture.area-aperture.area)*aperture.area
f_star=(counts_star-counts_back_star)/star_hdr['ELAPTIME']
m_star=-2.5 * np.log10(f_star) + ZP[star_hdr['FILTER']]
print(f_star,m_star)

# Estimate background and aperture photometry for the QSO
phot_qso = aperture_photometry(qso_diff, aperture)
phot_back_qso = aperture_photometry(qso_diff, back_aperture)
counts_qso=phot_qso['aperture_sum']
counts_back_qso=(phot_back_qso['aperture_sum']-phot_qso['aperture_sum'])/(back_aperture.area-aperture.area)*aperture.area
f_qso=(counts_qso-counts_back_qso)/qso_hdr['ELAPTIME']
m_qso=-2.5 * np.log10(f_qso) + ZP[qso_hdr['FILTER']]
print(f_qso,m_qso)

# Display images
display.connect_to_ginga(raise_err=True, allow_new=True)
viewer, ch_star = display.show_image(star_diff,'Star', clear=True, cuts = (-5.0*star_sigma, 5.0*star_sigma))
viewer, ch_qso = display.show_image(qso_diff,'QSO', cuts = (-5.0*qso_sigma, 5.0*qso_sigma))
out_star = ch_star.cut_levels((star_mean -5.0*star_sigma, star_mean + 10.0*star_sigma))
out_qso = ch_qso.cut_levels((qso_mean -5.0*qso_sigma, qso_mean + 7.0*qso_sigma))

# After displaying all the images sync up the images with WCS_MATCH
shell = viewer.shell()
shell.start_global_plugin('WCSMatch')
shell.call_global_plugin_method('WCSMatch', 'set_reference_channel', [ch_star], {})
'''


import json
from datetime import datetime
import requests
import pytz
#test for merging branch

class ApiError(Exception):

    def __init__(self, url, status):
        self.url = url
        self.status = status

    def __str__(self):
        return "<{}> {}".format(self.status, self.url)

class Api:

    BASE_URL = 'https://api.genability.com/rest'
    HEADER = {
        'Content-Type': 'application/json'
    }

    DURATIONS = {
        'HOUR': 60*60*1000,
        'QTR_HOUR': 15*60*1000
    }

    UNITS = {
        'consumption': 'kWh',
        'demand': 'kW'
    }

    def __init__(self, app_id, app_key):
        self.app_id = app_id
        self.app_key = app_key
        self.now = datetime.utcnow().replace(tzinfo=pytz.utc)

    def get_calc_request_template_for(self, mtid, property_data, profile, profile_duration, profile_type, from_datetime, to_datetime):
        """
        mtid: Master Tariff Id to use for the calc
        property_data: An array of additional properties to set for the calc with each element
                       being at 2-item tuple or array of [key, value].
        profile: Annual profile. Currently `duration` set to 15m interval data. To use hourly (8760)
                 instead, change `duration` to 60*60*1000.
        profile_duration: HOUR or QTR_HOUR. The interval between points in the profile.
        profile_type: 'consumption' or 'demand'.
        from_datetime: Start datetime of the calc. Should align with the start of the profile.
        to_datetime: End datetime of the calc.
        """

        request_template = {
            'masterTariffId': mtid,
            'detailLevel': 'CHARGE_TYPE',
            'groupBy': 'MONTH',
            'fromDateTime': from_datetime,
            'toDateTime': to_datetime,
            'propertyInputs': [
                {
                    'keyName': profile_type,
                    'unit': Api.UNITS[profile_type],
                    'dataSeries': profile,
                    'duration': Api.DURATIONS[profile_duration],
                    'fromDateTime': from_datetime
                }
            ]
        }

        for pd in property_data:
            request_template['propertyInputs'].append(
                {
                    'keyName': pd[0],
                    'dataValue': pd[1]
                }
            )

        return request_template

    def calc(self, request):
        resp = requests.post(
            '{}/v1/ondemand/calculate'.format(Api.BASE_URL),
            headers=Api.HEADER,
            json=request,
            auth=(self.app_id, self.app_key)
        )

        if resp.status_code != 200:
            with open('bad_request.json', 'w') as fp:
                json.dump(request, fp)
            with open('bad_response.json', 'w') as fp:
                json.dump(resp.json(), fp)
            # raise ApiError(resp.request.url, resp.status_code)

        return resp.json(), resp

    def tariff(self, mtid, **kwargs):
        resp = requests.get(
            '{}/public/tariffs/{}'.format(Api.BASE_URL, mtid),
            headers=Api.HEADER,
            params=kwargs,
            auth=(self.app_id, self.app_key)
        )

        # if resp.status_code != 200:
            # raise ApiError(resp.request.url, resp.status_code)

        return resp.json()['results'][0], resp


import caffe
import lmdb

lmdb_env = lmdb.open('/home/davidbutra/Escritorio/caffe/examples/mnist/mnist_train_lmdb')
lmdb_txn = lmdb_env.begin()
lmdb_cursor = lmdb_txn.cursor()
datum = caffe.proto.caffe_pb2.Datum()
n = 0
i = 0
nImage = 1
loop = 0

width = 28
height = 28
ftype = 'P2'

for key, value in lmdb_cursor:

    n = 0
    i = 0

    if loop < 30:

        datum.ParseFromString(value)
        label = datum.label
        data = caffe.io.datum_to_array(datum)
        #print label
        #print data

        pgmfile=open('data' + str(nImage) + '.pgm', 'w')
        pgmfile.write("%s\n" % (ftype))
        pgmfile.write("%d %d\n" % (width,height))
        pgmfile.write("255\n")

        txtfile=open('data' + str(nImage) + '.txt', 'w')
        txtfile.write("%s\n" % (ftype))
        txtfile.write("%d %d\n" % (width,height))
        txtfile.write("255\n")

        nImage = nImage + 1
        loop = loop + 1

        while i < height:
            if n == width - 1:
                pgmfile.write("%s\n" % (data[0][i][n]))
                txtfile.write("%s\n" % (data[0][i][n]))
                i = i + 1
                n = 0
            elif data[0][i][n + 1] < 10:
                pgmfile.write("%s   " % (data[0][i][n]))
                txtfile.write("%s   " % (data[0][i][n]))
                n = n + 1
            elif data[0][i][n + 1] < 100:
                pgmfile.write("%s  " % (data[0][i][n]))
                txtfile.write("%s  " % (data[0][i][n]))
                n = n + 1
            else:
                pgmfile.write("%s " % (data[0][i][n]))
                txtfile.write("%s " % (data[0][i][n]))
                n = n + 1

        pgmfile.close()


import logging
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import datetime
from common import dir_config
from util.logs.logg import Mylogger

my_logger = Mylogger()
# 封装基本函数 - 执行日志，异常处理，失败截图
import os

class BasePage:
    def __init__(self, driver):
        self.driver = driver

    # 等待元素可见
    def wait_eleVisible(self, locator, time=30, poll_frequency=0.5, doc=""):
        """

        :param locator:元素定位类型
        :param time:元素定位方式
        :param poll_frequency:
        :param doc: 块名_页面名称_操作名称_时间
        :return: None
        """
        my_logger.info("等待元素{0}可见".format(locator))
        try:
            # 开始等待的时间
            start = datetime.datetime.now()
            WebDriverWait(self.driver, time, poll_frequency).until(EC.visibility_of_element_located(locator))
            # 结束等待的时间点
            end = datetime.datetime.now()
            # 求一个差值，写在日志中，等待了多久
            wait_time = (end - start).seconds
            my_logger.info("{0}:元素{1}亦可见，等待起始时间：{2}，等待结束时间为：{3}，等待时长".format(locator, start, end, wait_time))
        except:
            logging.exception("等待元素可见失败！！！！")
            # 截图
            self.save_srceenshot(doc)
            raise

    # 等待元素存在
    def wait_elePresence(self, locator, doc=""):
        my_logger.info("等待元素出现：{}".format(locator))
        try:
            EC.invisibility_of_element_located(locator)
        except:
            logging.exception("等待元素可见失败！！！！")
            # 截图
            self.save_srceenshot(doc)

            raise

    # 查找元素
    def get_element(self, locator, doc=""):
        my_logger.info("查找元素：{}".format(locator))
        try:
            return self.driver.find_element(*locator)
        except:
            logging.exception("等待元素可见失败！！！！")
            # 截图
            self.save_srceenshot(doc)
            raise

    # 点击操作
    def click_element(self, locator, doc=""):
        # 找元素
        ele = self.get_element(locator, doc)
        # 元素操作
        my_logger.info("{0}点击元素：{1}".format(doc, locator))
        try:
            ele.click()
        except:
            logging.exception("点击元素失败！！！！")
            # 截图
            self.save_srceenshot(doc)
            raise

    # 输入操作
    def input_text(self, locator, text, doc=""):

        # 找元素
        # 元素操作输入
        try:
            ele = self.get_element(locator, doc)
            ele.send_keys(text)
        except:
            logging.exception("输入元素失败！！！！")
            # 截图
            self.save_srceenshot(doc)

            raise

    # 获取元素的文本内容
    def get_text(self, locator, doc=""):
        # 找元素
        ele = self.get_element(locator, doc)
        # 元素操作输入
        try:
            return ele.text
        except:
            logging.exception("获取元素文本失败！！！！")
            # 截图
            self.save_srceenshot(doc)
            raise

    # 获取元素的属性
    def get_element_attribute(self, locator, attr, doc=""):
        # 找元素
        ele = self.get_element(locator, doc)
        # 元素操作输入
        try:
            return ele.get_attribute(attr)
        except:
            logging.exception("获取元素的属性失败！！！！")
            # 截图
            self.save_srceenshot(doc)
            raise

    # alret处理
    def alret_action(self, action="accrpt"):
        pass

    # iframe切换
    def switch_iframe(self, iframe_reference):
        pass

    # 上传操作
    def upload_file(self):
        pass

    # j截图
    def save_srceenshot(self, doc=""):
        # 图片名称：名字对应那个用例：模块名_页面名称_操作名称_时间。pm
        file_name = dir_config.screenshot_dir + "/{0}_{1}.png".format(doc,
                                                                      time.strftime("%Y-%m-%H-%M-%S", time.localtime()))
        self.driver.save_screenshot(file_name)
        my_logger.info('截取网页成功，文件路径为：{0}'.format(file_name))
        try:
            self.driver.save_screenshot(file_name)
            my_logger.info("截图成功，图片路径为{}".format(file_name))
            assert (0)
        except:
            logging.exception("截图失败")
            assert (0)
        # start_time=time.time()
        # file_name='{}.png'.format(start_time)
        # file_path=os.path.join(dir_config.screenshot_dir,file_name)
        # self.driver.save_screenshot(file_path,doc=doc)
        # my_logger.info("错误页面截图保存路径为:{}".format(file_path))


import random
from Bot import *
from notBot import *
import time
import datetime
import multiprocessing

global_time = time.time()
print ("start time : " + time.strftime('%d-%b-%Y:%H:%M:%S',time.localtime(global_time)))

def generate_mrp(bot):
    method_list = ['GET','HEAD','POST']
    botmasterResource = '/twitter/login/botmaster'
    notBotmasterResource = '/twitter/login/notbotmaster'
    parameterList = ['index','scm','acm','admin','guest']
    if (bot):
        method = method_list[random.randint(0, 1)]
        resource = '/%s/?%s=%s' % (botmasterResource,parameterList[random.randint(0, 4)],random.randint(0,3))
        return '\"%s %s HTTP/1.1\"' % (method,resource)
    else:
        method = method_list[random.randint(0, 2)]
        resource = '/%s/?%s=%s' % (notBotmasterResource,parameterList[random.randint(0, 4)],random.randint(0,3))
        return '\"%s %s HTTP/1.1\"' % (method,resource)

def generate_ip():
    ip = ''
    temp = []
    for i in range(4):
        id = random.randint(1, 255)
        temp.append(str(id))
    ip = '.'.join(temp)
    return ip

def generate_bot():
    Time = time.strftime('%d-%m-%Y:%H:%M:%S',time.localtime(globalTime))
    bot = Bot(Time)
    self.machineList.append(bot)

def generate_notBot():
    Time = time.strftime('%d-%m-%Y:%H:%M:%S',time.localtime(globalTime))
    notbot = notBot(Time)
    machineList.append(notbot)

def bot_loop(filename,botList,global_time):
    global number
    #print (number)
    for bot in botList:
        number = bot.activity(global_time,filename,number)

def notBot_loop(filename,notBotList,global_time):
    global number
    #print (number)
    for notBot in notBotList:
        number = notBot.activity(global_time,filename,number)

#start here
print ('please enter the number of log to generate')
while True:
    try:
        number = int(input())
    except NameError:
        print  ('please enter an number')
        continue
    if number <= 0:
        print ('please enter an possitive number')
        continue
    break
print ('please enter the number of bots')
botNumber = int(input())
print ('please enter the number of notbots')
notBotNumber = int(input())
print ('please enter a filename to output the apache log data')
filename = input()
botList = []
notBotList = []
for i in range(botNumber):
    bot = Bot(global_time)
    botList.append(bot)
for i in range(botNumber):
    notbot = notBot(global_time)
    notBotList.append(notbot)

while number:
    if (random.randint(0,botNumber+notBotNumber)<botNumber):
        for bot in botList:
            number = bot.activity(global_time,filename,number)
    if (random.randint(0,botNumber+notBotNumber)<notBotNumber):
        for notbot in notBotList:
            number = notbot.activity(global_time,filename,number)
    global_time = time.strftime('%d-%b-%Y:%H:%M:%S',time.localtime(global_time))
    date_time = datetime.datetime.strptime(global_time,'%d-%b-%Y:%H:%M:%S')
    date_time = date_time + datetime.timedelta(seconds = random.randint(1,60))
    global_time = time.mktime(date_time.timetuple())

print('Fin')


from snakemake.shell import shell

def run_bb(command,*args,log=None,**kwargs):
    for a in args:
        command+=' '+a

    for k in kwargs:
        if k=='input':
            input=kwargs[k]
            if len(input)>1 and not  isinstance(input,str):
                command+=f" in={','.join(kwargs[k])} "
            else:
                command+=f" in={kwargs[k]} "
        elif k=='mem':
            mem= int(float(kwargs[k])*0.85)
            command+=f" -Xmx{mem}g "
        else:
            if type(kwargs[k])==bool:
                kwargs[k]= 't' if kwargs[k] else 'f'
            command+=f" {k}={kwargs[k]} "

    if log is not None:
        if kwargs.get('append',False):
            command += f' 2>>{log}'
        else:
            command += f' 2>{log}'

    #print(f"{command}")
    shell(command)

if __name__ == '__main__':

    if len(snakemake.log)>0:
        log=snakemake.log[0]
    else:
        log=None

    run_bb(log = log,
           **snakemake.input,
           **snakemake.params,
           **snakemake.output)


from django.contrib.auth.mixins import AccessMixin
from django.shortcuts import redirect

class OwnerAndLoginMixin(AccessMixin):

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated or not request.user.is_owner:
            return redirect("leads:condor")
        return super().dispatch(request, *args, **kwargs)


import io
import random
from flask import Flask, Response
import numpy as np
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
app = Flask(__name__)

@app.route('/plot.png')
def plot_png():
    fig = create_figure()
    output = io.BytesIO()
    FigureCanvas(fig).print_png(output)
    return Response(output.getvalue(), mimetype='image/png')

def moving_average(x, w):
    return np.convolve(x, np.ones(w), 'valid') / w

def create_figure():
    fig = Figure()
    ax = fig.add_subplot(1, 1, 1)
    xs = range(31)
    ys = [random.randint(-5, 5) for x in xs]
    ys = moving_average(ys, 4)
    ax.plot(xs[:28], ys)
    ax.set_title('Mood Tracker Feburary 2021')
    ax.set_xlabel('Day')
    ax.set_ylabel('overall mood (pos/neg thinking)')
    ax.set_xticks(np.arange(28))
    ax.set_xticklabels(np.arange(1,29))
    ax.grid(True)
    return fig

app.run()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 14 16:28:24 2020

@author: sahanaasokan
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plot
import seaborn as sns
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score

df = pd.read_excel('PCCI_Data.xlsx')
y = df['readmit30']
x = df.drop(columns=['readmit30'], axis=1)

x=pd.get_dummies(x, columns=['sex','weekday','month','ethnic_group_c'
 ,'marital_status_c','insurance_provider', 'tobacco_user','Condition','care_plan_following_discharge'])

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.25, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)

import xgboost as xgb
regressor = xgb.XGBRegressor(
                 colsample_bytree=0.2,
                 gamma=0.0,
                 learning_rate=0.01,
                 max_depth=4,
                 min_child_weight=1.5,
                 n_estimators=7200,
                 reg_alpha=0.9,
                 reg_lambda=0.6,
                 subsample=0.2,
                 seed=42,
                 silent=1)

regressor.fit(x_train, y_train)
y_pred_xgb = regressor.predict(x_test)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm_KNN = confusion_matrix(y_test, y_pred_xgb)

# accuracy: (tp + tn) / (p + n)
accuracy = accuracy_score(y_test, y_pred_xgb)
print('Accuracy: %f' % accuracy)
# precision tp / (tp + fp)
precision = precision_score(y_test, y_pred_xgb)
print('Precision: %f' % precision)
# recall: tp / (tp + fn)
recall = recall_score(y_test, y_pred_xgb)
print('Recall: %f' % recall)
# f1: 2 tp / (2 tp + fp + fn)
f1 = f1_score(y_test, y_pred_xgb)
print('F1 score: %f' % f1)


import os
import os.path

import lxml.etree

img_dir = '/tmp/cat'
img_result_dir = '/tmp/cat_result'
xml_save_to = 'train.xml'

def gen_data():
    for entry in os.scandir(img_result_dir):
        img_path = os.path.join(img_dir, entry.name[:-4])

        with open(entry.path) as f:
            content = f.read()

        points = []
        seps = content.split('|')
        for sep in seps:
            x, y = sep.split(',')
            x = int(float(x))
            y = int(float(y))
            points.append((x, y))

        yield (img_path, points)

dataset = lxml.etree.Element('dataset')
images = lxml.etree.SubElement(dataset, 'images')

for entry in gen_data():
    path = entry[0]
    points = entry[1]

    image = lxml.etree.SubElement(images, 'image', attrib={'file': path})

    top = min([p[1] for p in points])
    left = min([p[0] for p in points])
    width = max([p[0] for p in points]) - left
    height = max([p[1] for p in points]) - top

    lxml.etree.SubElement(image, 'box', attrib={'top': str(
        top), 'left': str(left), 'width': str(width), 'height': str(height)})

tree = lxml.etree.ElementTree(dataset)
tree.write(xml_save_to, pretty_print=True, xml_declaration=True, encoding='utf-8')


import sys
sys.stdin = open("17822_input.txt")

from collections import deque

# 원판 회전 함수
def rotate_plate(X, D, K):
    global N, M, A
    for x in range(X-1, N, X):  # 회전하는 원판 X의 배수
        if D:  # 시계방향
            A[x].rotate(-K)
        else:  # 반시계방향
            A[x].rotate(K)
    return

# flag가 0일 때 호출됨
def control(A):
    global N, M
    plus = 0
    cnt = 0
    for i in range(N):
        for j in range(M):
            if A[i][j]:
                plus += A[i][j]
                cnt += 1
    if cnt:
        avg = plus / cnt
    else:
        avg = 0
    # 이걸로 써도 무관함!
    # try:
    #     avg = plus / cnt
    # except:
    #     avg = 0
    for i in range(N):
        for j in range(M):
            if A[i][j] and avg != 0:
                if A[i][j] > avg:
                    A[i][j] -= 1
                elif A[i][j] < avg:
                    A[i][j] += 1
    return

# 인접한 같은 수 지우기(dfs로 구현)
dr = (-1, 1, 0, 0)
dc = (0, 0, -1, 1)

def remove(sr, sc):
    global A, flag
    flag2 = 0
    S = [(sr, sc)]
    key = A[sr][sc]
    A[sr][sc] = 0
    pr, pc = sr, sc
    while S:
        r, c = S.pop()
        for i in range(4):
            nr = r + dr[i]
            nc = (c + dc[i]) % M
            if not (0 <= nr < N):
                continue
            if not A[nr][nc]:
                continue
            if A[nr][nc] != key:
                continue
            flag = 1
            flag2 = 1
            A[nr][nc] = 0
            pr, pc = nr, nc
            S.append((nr, nc))
    if flag2 == 0:
        A[pr][pc] = key
    return

N, M, T = map(int, input().split())
A = [deque(map(int, input().split())) for _ in range(N)]
# print(A)
for _ in range(T):
    x, d, k = map(int, input().split())
    rotate_plate(x, d, k)
    flag = 0
    for i in range(N):
        for j in range(M):
            if A[i][j]:
                remove(i, j)
    # 인접하면서 수가 같은 것이 없다.
    if flag == 0:
        control(A)

# print(A)
# 최종 배열 A의 모든 원소 합
total = 0
for i in range(N):
    for j in range(M):
        if A[i][j]:
            total += A[i][j]
print(total)


#!/usr/bin/python3.5
import os
import csv
import datetime
import time
import json

print("""
-----------------------------
Starting automated-speedtests
-----------------------------
""")

ping = False
down = False
up = False
img = False
tests = 0

with open('/home/pi/automated-speedtests/config.json', 'r') as f:
    config = json.load(f)

def pingTest():
    hostname = config['hostname']
    response = os.system("ping -c 1 " + hostname)
    if response == 0:
        pingResult = True
    else:
        pingResult = False

    return pingResult

def speedTest(ping, down, up, img, tests):
    print("\nConducting speed test...")
    speedTestOutput = os.popen("python " + config['speedtestPath'] + " --simple --share").read()
    print("Speed test complete")

    lines = speedTestOutput.split("\n")
    ts = time.time()
    date = datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S")

    print("\n------------------------------------------------------")
    print(date + "\n" + speedTestOutput + "------------------------------------------------------\n")
    #Set speeds to 0 if speedtest-cli couldn't connect
    if "Cannot" in speedTestOutput:
        ping = 100
        down = 0
        up = 0
        img = "noimg"

    #Extract values for ping, download and upload
    else:
        ping = lines[0].split(' ')[1]
        down = lines[1].split(' ')[1]
        up = lines[2].split(' ')[1]
        img = lines[3].split(' ')[2]

    #Save the data to file for local network plotting
    fileSpeedResults = open(config['webserverPath'] + "/speedresults.csv", 'a')
    writer = csv.writer(fileSpeedResults)
    writer.writerow((date, ping, down, up))
    fileSpeedResults.close()

    #save just latest value for status display
    with open('/home/pi/automated-speedtests/latest.csv', 'w') as latest:
        latest.write(date + ',' + ping + ',' + down + ',' + up)

    ping = float(ping)
    down = float(down)
    up = float(up)

    tests += 1

    return(ping, down, up, img, tests)

downtime = 0
start_time = time.time()
pingResult = pingTest()

while pingResult == False:
    downtime = time.time() - start_time
    print("No network connection detected")
    pingResult = pingTest()

if pingResult == True:
    if downtime > 0:
        print("Network connection re-established. Downtime: ", downtime)
    else:
        print("Network connection detected")
        ping, down, up, img, tests = speedTest(ping, down, up, img, tests)


from flask import Flask, request, render_template

import base64
import numpy
# import cv2
import os

UPLOAD_FOLDER = "media"

app = Flask(__name__)
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)
app.config["imgdir"] = UPLOAD_FOLDER

def load_model():
    class tmp:
        def predict(x):
            print('Predicting for', x)

    return tmp()

MODEL = load_model()

def predict(image):
    return MODEL.predict(image)

@app.route('/upload', methods=["POST"])
def process_image():
    data = request.get_data().decode()
    image_b64 = data["image"]
    image = base64.decodebytes(image_b64)
    result = predict(image)
    return {"result": result}

@app.route('/', methods=["GET", "POST"])
def upload_image():
    if request.method == "GET":
        return render_template("home.html")
    elif request.method == "POST":
        data = request.files['file']
        # filename = secure_filename(file.filename) # save file
        filename = data.filename
        filepath = os.path.join(app.config['imgdir'], filename)
        data.save(filepath)
        # img = cv2.imread(filepath)
        result = 'non-cataract' # predict(img)
        return render_template("result.html", **{"result": result})

@app.route('/home',methods=["GET","POST"])
def homepage():
    if request.method == "GET":
        return render_template("home.html")
    elif request.method == "POST":
        data = request.files['file']
        # filename = secure_filename(file.filename) # save file
        filename = data.filename
        filepath = os.path.join(app.config['imgdir'], filename)
        data.save(filepath)
        # img = cv2.imread(filepath)
        result = 'non-cataract' # predict(img)
        return render_template("result.html", **{"result": result})


import json

def genera_sottoalbero(fnome,x,fout):
    ''' Funzione che legge file, controlla se e' presente albero con nodo x
        scrive in fout il sottoalbero, se x non c'e' restituisce []
    '''
    alb = leggiFile(fnome)              # leggo flie

    ris = {}                            # dizionario risultato
    # controllo se nodo x e' presente
    if x not in alb:
        salvaFile(ris, fout)            # scrivo su file
        return None                     # interrompo

    ris[x]=alb[x]                   # primo elemento di dizionario
    ls = alb[x].copy()              # creo una copia per non modificare diz
    calcola(alb, ls, ris)           # elementi da inserire
    salvaFile(ris, fout)

def cancella_sottoalbero(fnome,x,fout):
    ''' Funzione che controlla se nodo x e' presente in albero in caso elimina
        tutti x e i suoi figli altrimenti non fa modifiche
    '''
    alb = leggiFile(fnome)              # leggo file

    # se nodo e' presente
    if x in alb:
        ris = {}                        # dizionario nodi da eliminare
        ris[x]=alb[x]                   # primo elemento di dizionario
        ls = alb[x].copy()              # creo una copia per non modificare diz
        calcola(alb, ls, ris)           # elementi da inserire

        # per ogni nodo da eliminare
        for n in ris:
            del alb[n]                  # elimino nodo
        # per ongi chiave in albero
        for k in list(alb.keys()):
            # se x contenuta in nodo
            if alb[k].count(x) > 0:
                alb[k].remove(x)        # elimono figlio x da nodo
                break
    salvaFile(alb, fout)

def dizionario_livelli(fnome,fout):
    ''' Funzione che associa livello dell'albero a tutti i nodi che contiene '''
    alb = leggiFile(fnome)
    ris = {}
    # inzializzo livelli
    for n in range(0, 50):
        ris[n]=[]
    c = 0               # contatore
    rad = radice(alb)   # ricavo radice
    livello(alb, ris, rad.pop(), c)

    elimina = []
    # per ogni livello
    for liv in ris:
        # se e' vuoto
        if ris[liv] == []:
            elimina.append(liv)         # aggiungo a lista da eliminare
        ris[liv].sort()                 # riordino valori
    # per ogni chiave da eliminare
    for liv in elimina:
        del ris[liv]                    # la elimino

    salvaFile(ris, fout)

def dizionario_gradi_antenati(fnome,y,fout):
    ''' Funzione che associa nodo a numero di antenati di grado y '''
    alb = leggiFile(fnome)
    grado = {}
    ris = {}

    # per ogni chiave
    for k in alb:
        grado[k]=len(alb[k])            # associo grado a chiave

    c = 0               # contatore
    rad = radice(alb)   # ricavo radice
    antenati(alb, grado, ris, y, rad.pop(), c)

    salvaFile(ris, fout)

def radice(alb):
    ''' metodo che trova radice dell'albero '''
    figli = set()
    for nodo in alb:
        for f in alb[nodo]:
            figli.add(f)
    return set(alb) - figli

def antenati(alb, grado, ris, y, nodo, c):
    ''' funzione che associa ad ogni chiave il numero di antenati di grado y '''
    ris[nodo]=c         # associo nodo a numero di antenati di grado y
    # se nodo ha figli
    if alb[nodo] != []:
        # per ogni figlio
        for n in alb[nodo]:
            # se e' di grado y
            if grado[nodo] == y:
                antenati(alb, grado, ris, y, n, c+1)
            else:
                antenati(alb, grado, ris, y, n, c)

def livello(alb, ris, nodo, c):
    ''' funzione che associa nodi a livello rispettivo '''
    # associo nodo a livello
    ris[c].append(nodo)

    # se nodo ha figli
    if alb[nodo] != []:
        # ricorsione
        for nodo in alb[nodo]:
            livello(alb, ris, nodo, c+1)

def calcola(alb, ls, ris):
    ''' funzione che inserisce nel dizionario ris nodo x e i suoi figli '''
    if ls == []:                    # nel caso la lista fosse vuota
        return None
    # se nodo non ha figli
    if alb[ls[0]] == []:
        ris[ls[0]]=[]               # salvo nodo con lista vuota
    else:
        ris[ls[0]]=alb[ls[0]]       # salvo nodo con figli
        # per ogni figlio
        for n in alb[ls[0]]:
            ls.append(n)            # aggiungo nodo da visionare a lista
    ls.remove(ls[0])                # elimino nodo visto
    # finche' lista non e' vuota
    if ls != []:
        calcola(alb, ls, ris)

def salvaFile(ris, file):
    ''' Funzione che salva dizionario in formato json '''
    with open(file, 'w') as f:
        json.dump(ris, f)

def leggiFile(file):
    ''' Funzine che legge file e ritorna la sua rappresentazione '''
    with open(file) as j:
        f = json.load(j)
    return f


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

# (The MIT License)
#
# Copyright (c) 2015 Kura
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import re
from setuptools import (find_packages, setup)
import sys

desc = "Scrape a website"
long_desc = open('README.rst').read() + "\n\n" + open('CHANGES.rst').read()
long_desc = re.sub(r":[a-z]*:`", "`", long_desc)

entry_points = {
    'console_scripts': [
        'orbweaver = orbweaver:crawl',
    ]
}

setup(name='orbweaver',
      version='0.0.0',
      url='',
      author='Kura',
      author_email='kura@kura.io',
      maintainer='Kura',
      maintainer_email='kura@kura.io',
      description=desc,
      long_description=long_desc,
      license='MIT',
      platforms=['linux'],
      packages=find_packages(exclude=["*.tests"]),
      install_requires=['requests', 'blessings', 'beautifulsoup4', ],
      requires=['requests', 'blessings', 'beautifulsoup4', ],
      provides=['orbweaver', ],
      keywords=['spider', ],
      entry_points=entry_points,
      classifiers=[
        'Natural Language :: English',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.7',
      ],
      zip_safe=True,
      )


import config
from panda3d.bullet import BulletBoxShape, BulletRigidBodyNode
from panda3d.core import Vec3, Vec4, TransformState, Point3, BitMask32, MeshDrawer, TextureStage, PointLight, VBase4, OmniBoundingVolume
from direct.showbase.InputStateGlobal import inputState
from math import sin, cos, tan, pi
from weapons import fireWeapon
from entities import delete_entity

def init_ship():
    #CREATE OUR SHIP
    shape1 = BulletBoxShape(Vec3(4, 2, 1))
    shape2 = BulletBoxShape(Vec3(2, 4, 1))
    ship = config.WORLD_NP.attachNewNode(BulletRigidBodyNode(str(config.ENTITY_ID)))
    ship.node().setMass(1.0)
    ship.node().addShape(shape1, TransformState.makePos(Point3(0, -0.5, 0)))
    ship.node().addShape(shape2, TransformState.makePos(Point3(0, 1, 0)))
    ship.node().set_angular_damping(0.8)
    ship.setPos(0, 0, 7)
    ship.setCollideMask(BitMask32.bit(0))
    config.BULLET_WORLD.attachRigidBody(ship.node())
    shipModel = loader.loadModel('assets/models/ourShip.egg')
    shipModel.clearModelNodes()
    shipModel.setHpr(180,0,0)
    shipModel.setPos(0,0,-2)
    shipModel.reparentTo(ship)
    shipModel.hide()
    ship.node().notifyCollisions(True)
    config.SHIP = ship

    config.PANDA3D.camera.setPos(0,4,0)
    config.PANDA3D.camera.setHpr(0,0,0)
    config.PANDA3D.camera.reparentTo(ship)

    config.ENTITIES[config.ENTITY_ID] = {'CATEGORY':'ship', 'NODE':ship, 'MODEL':shipModel}
    config.ENTITY_ID += 1

    config.ENTITIES[config.ENTITY_ID] = {'CATEGORY':'weapon', 'WEAPON_ID':0,'LAST_SHOT':0,'BULLETS':[], 'FIREING':0, 'OWNER_ID':(config.ENTITY_ID-1)}
    config.ENTITIES[config.ENTITY_ID-1]['WEAPON_IDS'] = config.ENTITY_ID
    config.PILOT_WEAPON = config.ENTITY_ID
    config.ENTITY_ID += 1

def processInput(dt):
    force = Vec3(0, 0, 0)
    torque = Vec3(0, 0, 0)

    if inputState.isSet('up'):  force.setZ( 1.0)
    if inputState.isSet('down'): force.setZ(-1.0)
    if inputState.isSet('increase_thrust'): force.setY( 1.0)
    if inputState.isSet('decrease_thrust'): force.setY(-1.0)
    if inputState.isSet('left'):    force.setX(-1.0)
    if inputState.isSet('right'):   force.setX( 1.0)
    if inputState.isSet('yawLeft'):  torque.setZ( 1.0)
    if inputState.isSet('yawRight'): torque.setZ(-1.0)
    if inputState.isSet('rollLeft'):  torque.setY( -1.0)
    if inputState.isSet('rollRight'): torque.setY(1.0)
    if inputState.isSet('tiltUp'):  torque.setX( 1.0)
    if inputState.isSet('tiltDown'): torque.setX(-1.0)
    if inputState.isSet('fire'): fireWeapon(config.PILOT_WEAPON)

    force *= 30.0
    torque *= 10.0

    force = render.getRelativeVector(config.SHIP, force)
    torque = render.getRelativeVector(config.SHIP, torque)

    config.SHIP.node().setActive(True)
    config.SHIP.node().applyCentralForce(force)
    config.SHIP.node().applyTorque(torque)

    velocity = config.SHIP.node().getLinearVelocity()

    if velocity.length() > 10:
        velocity *= 10/velocity.length()
        config.SHIP.node().setLinearVelocity(velocity)

    velocity = config.SHIP.node().getAngularVelocity()

    if velocity.length() > 1.5:
        velocity *= 1.5/velocity.length()
        config.SHIP.node().setAngularVelocity(velocity)

SpaceShipSimulator/Panda3D - old version/code/ship.py


# © 2018 Danimar Ribeiro, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import base64
from lxml import objectify
from odoo import api, fields, models

class BomImportWizard(models.TransientModel):
    _name = "bom.import.wizard"

    xml_file = fields.Binary(string="Arquivo XML", required=True)
    create_product = fields.Boolean(string='Criar Produto?')

    @api.multi
    def action_import(self):
        xml_file = base64.decodestring(self.xml_file)
        xml = objectify.fromstring(xml_file)

        context = dict(self._context or {})
        active_ids = context.get('active_ids', []) or []
        order = self.env['sale.order'].browse(active_ids)

        if order.order_line:
            order.order_line.mapped('mrp_bom_id').unlink()
            order.order_line.unlink()

        line_env = self.env['sale.order.line']
        prod_env = self.env['product.product']
        bom_env = self.env['mrp.bom']
        bomline_env = self.env['mrp.bom.line']
        route_manuf_id = self.env.ref('mrp.route_warehouse0_manufacture').id
        route_mto_id = self.env.ref('stock.route_warehouse0_mto').id

        for tipologia in xml.TIPOLOGIAS.TIPOLOGIA:
            product_id = prod_env.search(
                [('default_code', '=', str(tipologia.CODESQD))], limit=1)
            vals = {
                'name': str(tipologia.DESCR),
                'standard_price': float(tipologia.PRECO_UNIT),
                'list_price': float(tipologia.PRECO_UNIT),
                'default_code': str(tipologia.CODESQD),
                'weight': float(tipologia.PESO_UNIT),
                'type': 'product',
                'route_ids': [(6, None, [route_manuf_id, route_mto_id])],
            }
            if not product_id:
                product_id = prod_env.create(vals)
            else:
                product_id.write(vals)
            bom_id = bom_env.create({
                'product_tmpl_id': product_id.product_tmpl_id.id,
                'product_qty': 1.0,
                'code': str(tipologia.TIPO),
                # projetista ta string...se pa, tem que mudar dps
                'projetista': str(tipologia.PROJETISTA),
                'width': float(tipologia.LARGURA),
                'height': float(tipologia.ALTURA),
                'trat_perf': str(tipologia.TRAT_PERF),
            })

            # O XML possui componentes, perfis e vidros
            if "COMPONENTE" in dir(tipologia.COMPONENTES):
                for comp in tipologia.COMPONENTES.COMPONENTE:
                    raw_id = prod_env.search(
                        [('default_code', '=', str(comp.CODIGO))], limit=1)
                    vals = {
                        'name': str(comp.DESCRICAO),
                        'standard_price': float(comp.CUSTO),
                        'list_price': float(comp.CUSTO),
                        'default_code': str(comp.CODIGO),
                        'type': 'product',
                        'sale_ok': False,
                    }
                    if not raw_id:
                        raw_id = prod_env.create(vals)
                    else:
                        raw_id.write(vals)
                    bomline_env.create({
                        'bom_id': bom_id.id,
                        'product_id': raw_id.id,
                        'product_qty': float(comp.QTDE),
                        'ref': str(comp.REF),
                        'color_code': str(comp.CODIGOCOR),
                        'size': str(comp.TAM),
                        'is_component': True,
                    })

            # Perfis
            if "PERFIL" in dir(tipologia.PERFIS):
                for perfil in tipologia.PERFIS.PERFIL:
                    raw_id = prod_env.search(
                        [('default_code', '=', str(perfil.CODIGO))], limit=1)
                    vals = {
                        'name': str(perfil.DESCRICAO),
                        'standard_price': float(perfil.CUSTO),
                        'list_price': float(perfil.CUSTO),
                        'default_code': str(perfil.CODIGO),
                        'weight': float(perfil.PESO),
                        'type': 'product',
                        'sale_ok': False,
                    }
                    if not raw_id:
                        raw_id = prod_env.create(vals)
                    else:
                        raw_id.write(vals)
                    bomline_env.create({
                        'bom_id': bom_id.id,
                        'product_id': raw_id.id,
                        'product_qty': float(perfil.QTDE),
                        'ref': str(perfil.REF),
                        'trat': str(perfil.TRAT),
                        'left_angle': str(perfil.ANG_ESQ),
                        'right_angle': str(perfil.ANG_DIR),
                        'size': str(perfil.TAM),
                        'is_profile': True,
                    })

            # Vidros
            if "VIDRO" in dir(tipologia.VIDROS):
                for vidro in tipologia.VIDROS.VIDRO:
                    raw_id = prod_env.search(
                        [('default_code', '=', str(vidro.CODIGO))], limit=1)
                    vals = {
                        'name': str(vidro.DESCRICAO),
                        'standard_price': float(vidro.CUSTO),
                        'list_price': float(vidro.CUSTO),
                        'default_code': str(vidro.CODIGO),
                        'type': 'product',
                        'sale_ok': False,
                    }
                    if not raw_id:
                        raw_id = prod_env.create(vals)
                    else:
                        raw_id.write(vals)
                    bomline_env.create({
                        'bom_id': bom_id.id,
                        'product_id': raw_id.id,
                        'product_qty': float(vidro.QTDE),
                        'ref': str(vidro.REF),
                        'height': float(vidro.ALTURA),
                        'width': float(vidro.LARGURA),
                        'surface': str(vidro.SUPERFICIE),
                        'is_glass': True,
                    })

            line_env.create({
                'order_id': order.id,
                'product_id': product_id.id,
                'product_uom_qty': float(tipologia.QTDE),
                'price_unit': float(tipologia.PRECO_UNIT),
                'mrp_bom_id': bom_id.id,
            })

        return {'type': 'ir.actions.act_window_close'}


# -*- coding: utf-8 -*-
"""
Created on Mon Mar  6 10:02:59 2017

@author: c1625914
"""

#Clean up PSFs for Alex

import numpy as np
import argparse
from astropy.io import fits
from scipy.ndimage import rotate, filters

#Pixel Scale Keywords

PIXSCL_KEY_DEG = ['CD1_1', 'CD2_2', 'CDELT1', 'CDELT2']
PIXSCL_KEY_ARCSEC = ['PIXSCALE', 'SECPIX', 'PIXSCALX', 'PIXSCALY']
PIXSCL_KEYS = PIXSCL_KEY_DEG + PIXSCL_KEY_ARCSEC

parser = argparse.ArgumentParser()
parser.add_argument("source", help="Input source filename")
parser.add_argument("sourcefwhm", help="Input source beam FWHM")
args = parser.parse_args()

source_psf_file = args.source
source_fwhm = np.float(args.sourcefwhm)

def get_pixscale(header):

    #Get the pixel scale (in ") of a PSF. If it's in degrees, convert to "

    pixel_keys = has_pixelscale(header)

    pixel_key = pixel_keys.pop()
    pixel_scale = abs(header[pixel_key])

    if pixel_key in PIXSCL_KEY_DEG:
        pixel_scale *= 3600

    return round(pixel_scale, 6)

def has_pixelscale(header):

    #Find any pixel scale data in the FITS header

    return [key
            for key in PIXSCL_KEYS
            if key in list(header)]

def interpolate(psf,num_iter=5):

    psf_nans = np.zeros(psf.shape)

    x_range = psf_nans.shape[0]
    y_range = psf_nans.shape[1]

    for i in range(x_range):
            for j in range(y_range):
                if np.isnan(psf[i,j]) == 1:
                    psf_nans[i,j] = 1

    psf_copy = np.nan_to_num(psf)

    #smooth the PSF num_iter times

    for n in range(5):
        psf_smooth = filters.gaussian_filter(psf_copy,2)
        for i in range(x_range):
                for j in range(y_range):
                    if np.isnan(psf[i,j]) == 1:
                        psf_copy[i,j] = psf_smooth[i,j]

    for i in range(x_range):
        for j in range(y_range):
            if np.isnan(psf[i,j]) == 1:
                psf[i,j] = psf_copy[i,j]

    return psf

def centroid(psf):

    x_range = psf.shape[0]
    y_range = psf.shape[1]

    psf_smooth = filters.gaussian_filter(psf,5)

    #Assume the centre of the PSF is somewhere in the central half of the data

    psf_cutout = psf_smooth[int(x_range/4):int(3*x_range/4),int(y_range/4):int(3*y_range/4)]
    psf_max = np.amax(psf_cutout)

    x_centroid = 0
    y_centroid = 0
    n = 0

    for i in range(int(x_range/4),int(3*x_range/4),1):
        for j in range(int(y_range/4),int(3*y_range/4),1):
            if (psf_max-psf_smooth[i,j])/psf_max <= 5e-3:
                x_centroid += i
                y_centroid += j
                n += 1

    x_centroid /= n
    y_centroid /= n

    #Make sure centroid is integer-valued (just in case)

    x_centroid = int(x_centroid)
    y_centroid = int(y_centroid)

    #Shift the PSF to centre it

    x_shift = int(x_range/2)-x_centroid
    y_shift = int(x_range/2)-y_centroid
    psf = np.roll(psf,x_shift,axis=1)
    psf = np.roll(psf,y_shift,axis=0)

    return psf

def circularise(psf,rotations=14):

    #Rotate the PSF a total of 14 times, taking an iterative average each time.
    #This serves to make the PSF rotationally invariant

    for n in range(rotations,0,-1):
        psf_rotate = rotate(psf,360/(2**n), order=3, reshape=False)
        psf = 0.5*(psf+psf_rotate)

    #Set anything outside the maximum radius contained within the whole square to be 0

    radius = min(psf.shape)/2
    x_range = psf.shape[0]
    y_range = psf.shape[1]
    x_centre = int(x_range/2)
    y_centre = int(y_range/2)

    for i in range(x_range):
        for j in range(y_range):
            if (i - x_centre)**2+(j-y_centre)**2 > radius**2:
                psf[i,j] = 0

    #Round anything within machine uncertainty to 0

    psf[abs(psf) < np.finfo(float).eps] = 0

    return psf

def high_freq_filter(psf,fwhm,pscale=0.1):

    k_b = 8*np.pi/(fwhm*pscale)
    k_a = 0.9*k_b
    x_range = psf.shape[0]
    y_range = psf.shape[1]
    x_centre = int(x_range/2)
    y_centre = int(y_range/2)

    psf_filtered = np.zeros(psf.shape)

    #Apply the filter

    for i in range(x_range):
        for j in range(y_range):
            r = np.sqrt((i - x_centre)**2+(j-y_centre)**2)
            r =  r*pscale
            if r <= k_a:
                psf_filtered[i,j] = 1.
            elif r > k_a and r <= k_b:
                psf_filtered[i,j] = np.exp(-(1.8249*(r-k_a)/(k_b-k_a))**4)
    psf = psf_filtered*psf

    return psf

source_psf, source_header = fits.getdata('/home/daedalusdata/c1625914/ConvolutionKernels/PSFs/'+str(source_psf_file)+'.fits', header=True)

source_psf = interpolate(source_psf)
print('Boop')
source_pixscale = get_pixscale(source_header)
print('Boop')
source_psf_normalised = source_psf/source_psf.sum()
source_psf_centroid = centroid(source_psf_normalised)
print('Boop')
source_psf_circularised = circularise(source_psf_centroid)
print('Boop')
source_fourier = np.real(np.fft.fft2(np.fft.ifftshift(source_psf_circularised)))
source_fourier_shift = np.fft.fftshift(source_fourier)
print('Boop')
source_fourier_circularised = circularise(source_fourier_shift)
print('Boop')
source_fourier_filter = high_freq_filter(source_fourier,source_fwhm)
print('Boop')
source_fourier_filter_shift =np.fft.ifftshift(source_fourier_filter)
source_psf_filtered = np.fft.fftshift(np.real(np.fft.ifft2(source_fourier_filter_shift)))
print('Boop')
source_psf_filtered_circularised = circularise(source_psf)
print('Done')

fits.writeto('/home//daedalusdata/c1625914/ConvolutionKernels/PSFs/ForAlex/'+str(source_psf_file)+'_cleaned.fits', source_psf_filtered_circularised, source_header, clobber=True)