import math
import random
import matplotlib.pyplot as plot
import numpy as np

class RBF(object):
    def __init__(self, learningRate=0.2, momentum=0.01, epochs=100, inputNodes=1, hiddenNodes=12, outputNodes=1, bias=1):
        self.bias = bias
        self.learningRate = learningRate
        self.momentum = momentum
        self.epochs = epochs
        self.inputNodes = inputNodes
        self.hiddenNodes = hiddenNodes
        self.outputNodes = outputNodes
        self.radials = np.zeros(self.hiddenNodes)
        self.r = np.zeros(self.hiddenNodes)
        self.radialOutput = np.zeros(self.hiddenNodes + self.bias)
        self.radialOutput[:] = 1
        self.outputWeights = np.zeros(self.hiddenNodes + self.bias)
        for i in range(self.hiddenNodes + self.bias):
            self.outputWeights[i] = random.uniform(-10, 10)
        self.previousWeights = np.array(self.outputWeights)
        self.x = []
        self.y = []
        self.epoch = []
        self.error = []
        self.testingError = []
        self.testError = 0
        self.outFromHiddenLayer = None

    def calculateGaussian(self, distance, r):
        return math.exp(-math.pow(distance, 2) / 2 * math.pow(r, 2))

    def query(self, input):
        self.radialOutput[:] = 1
        for i in range(self.hiddenNodes):
            self.radialOutput[i] = self.calculateGaussian(np.abs(input - self.radials[i]), self.r[i])

        # propagacja w przod warstwy outputu
        output = 0.0
        for i in range(self.hiddenNodes + self.bias):
            output += self.outputWeights[i] * self.radialOutput[i]
        return output

    def trainRadialLayer(self, data):
        # wybor centrow ze zbioru treningowego
        self.radials = data[np.random.choice(data.shape[0], self.hiddenNodes, replace=False)][:, 0]
        # ustawienie promieni dla neuronow radialnych
        for i in range(self.hiddenNodes):
            distances = np.zeros(self.hiddenNodes)
            for j in range(self.hiddenNodes):
                distances[j] = np.abs(self.radials[i] - self.radials[j])
            self.r[i] = max(distances) / math.sqrt(2 * self.hiddenNodes)
        # obliczenie outputu z warstwy ukrytej korzystajac z funkcji gaussa
        self.outFromHiddenLayer = np.zeros((data.shape[0], self.hiddenNodes))
        for d in range(data.shape[0]):
            for i in range(self.hiddenNodes):
                self.outFromHiddenLayer[d][i] = self.calculateGaussian(np.abs(data[d][0] - self.radials[i]), self.r[i])

    def trainLinearLayer(self, data, testData):
        for e in range(self.epochs):
            epochError = 0
            for d in range(data.shape[0]):
                target = data[d][1]
                self.radialOutput[:self.hiddenNodes] = self.outFromHiddenLayer[d]
                # propagacja w przod warstwy outputu
                output = 0.0
                for i in range(self.hiddenNodes + self.bias):
                    output += self.outputWeights[i] * self.radialOutput[i]

                deltaOutput = output - target
                # aktualizacja wag
                for i in range(self.hiddenNodes + self.bias):
                    self.outputWeights[i] = self.outputWeights[i] - (
                            deltaOutput * self.radialOutput[i] * self.learningRate) + \
                                            self.momentum * (self.outputWeights[i] - self.previousWeights[i])
                    self.previousWeights[i] = self.outputWeights[i]

                # obliczenie bledu
                epochError += deltaOutput * deltaOutput / 2
            self.epoch.append(e)
            self.error.append(epochError / data.shape[0])
            self.testingError.append(self.checkAproximateQuality(testData))

    def train(self, data, testData, repeats=5):
        error = float('inf')
        for i in range(repeats):
            self.x.clear()
            self.y.clear()
            self.epoch.clear()
            self.error.clear()
            self.testingError.clear()
            self.trainRadialLayer(data)
            self.trainLinearLayer(data, testData)
            if np.average(self.error) < error:
                error = np.average(self.error)
                err = self.error.copy()
                wei = self.outputWeights.copy()
                rad = self.radials.copy()
                sig = self.r.copy()
                tes = self.testingError.copy()
        self.error = err
        self.outputWeights = wei
        self.radials = rad
        self.r = sig
        self.testError = self.checkAproximateQuality(testData)
        self.testingError = tes
        # wykres aproksymacji z punktami treningowymi
        plot.plot(data[:, 0], data[:, 1], 'ro', label='Training Points')
        plot.title('Chart for '+self.hiddenNodes.__str__()+' radial neurons \n eta= '+self.learningRate.__str__()+ ' ,momentum= '+
                                        self.momentum.__str__())
        plot.xlabel('x')
        plot.ylabel('y')
        for i in range(-4050, 4050):
            self.x.append(i / 1000)
            self.y.append(float(self.query(i / 1000)))
        plot.plot(self.x, self.y, linewidth=7, label='Approximated function')
        plot.legend(loc='upper center', bbox_to_anchor=(0.5, 1.00), shadow=True, ncol=2)
        plot.xticks(np.arange(-5, 6, 1))
        plot.ylim(-8, 4)
        #plot.savefig(self.momentum.__str__()+'m2train' + self.hiddenNodes.__str__() + '.png')
        plot.show()

        # wykres bledu dla zbioru treningowego
        plot.plot(self.epoch, self.error)
        plot.title('Chart for '+self.hiddenNodes.__str__()+' neurons - errors from training input')
        plot.xlabel('Epochs')
        plot.ylabel('Error')
        plot.ylim(0, max(self.error)+0.05)
        #plot.savefig(self.momentum.__str__()+'m2etrain'+self.hiddenNodes.__str__()+'.png')
        plot.show()

        # wykres bledu dla zbioru testowego
        plot.plot(self.epoch, self.testingError)
        plot.title('Chart for '+self.hiddenNodes.__str__()+' neurons - errors from testing input')
        plot.xlabel('Epochs')
        plot.ylabel('Error')
        plot.ylim(0, max(self.testingError)+0.05)
        #plot.savefig('2etest'+self.hiddenNodes.__str__()+'.png')
        plot.show()

        # wykres aproksymacji z punktami testowymi
        plot.plot(testData[:, 0], testData[:, 1], 'yo', label='Testing Points')
        plot.title('Chart for ' + self.hiddenNodes.__str__() + ' radial neurons - error='+round(self.testError, 5).__str__()+
                   '\n eta= ' + self.learningRate.__str__() + ' ,momentum= ' + self.momentum.__str__())
        plot.xlabel('x')
        plot.ylabel('y')
        plot.plot(self.x, self.y, linewidth=7, label='Approximated function')
        plot.legend(loc='upper center', bbox_to_anchor=(0.5, 1.00), shadow=True, ncol=2)
        plot.xticks(np.arange(-5, 6, 1))
        plot.ylim(-8, 4)
        #plot.savefig('2test'+self.hiddenNodes.__str__()+'.png')
        plot.show()

    def checkAproximateQuality(self, testData):
        error = 0
        for d in testData:
            input = d[0]
            target = d[1]
            output = self.query(input)
            error += ((output - target) ** 2) / 2
        error /= testData.shape[0]
        return error

# odczytanie pliku approximation_train1
with open('approximation_train1.txt') as f:
    inputsTrain1 = []
    for line in f:
        inputsTrain1.append([float(x) for x in line.split()])
inputsTrain1 = np.array(inputsTrain1)
# odczytanie pliku approximation_train2
with open('approximation_train2.txt') as f:
    inputsTrain2 = []
    for line in f:
        inputsTrain2.append([float(x) for x in line.split()])
inputsTrain2 = np.array(inputsTrain2)
# odczytanie pliku approximation_test
with open('approximation_test.txt') as f:
    inputsTest = []
    for line in f:
        inputsTest.append([float(x) for x in line.split()])
inputsTest = np.array(inputsTest)

rbf = RBF()
rbf.train(inputsTrain1, inputsTest)

# rbf1 = RBF()
# rbf1.train(inputsTrain2, inputsTest)


from database import getCustomerDetails
from ApiCall import apicall_vehicle_loan, apicall_personal_loan, apicall_credit_card, apicall_housing_loan
from flask import Flask, request, render_template
import json
# import things
#from flask_table import Table, Col

app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template('result.html')
    #return render_template('result.html', Personal_Loan_Val="80%", Personal_Loan_Sts="Yes")

@app.route('/', methods=['POST'])
def my_form_post():
    text = request.form['search']
    variable=getCustomerDetails(text)
    pl=apicall_personal_loan(variable[1])
    vl=apicall_vehicle_loan(variable[0])
    cc=apicall_credit_card(variable[2])
    hl=apicall_housing_loan(variable[3])

    return render_template('result.html', Personal_Loan_Val=pl[0], Personal_Loan_Sts= str(int(float(pl[1])*100)) + '%', CC_Val= cc[0], CC_Sts = str(int(float(cc[1])*100)) + '%',HL_Val = hl[0], HL_Sts = str(int(float(hl[1])*100)) +'%' , CL_Val = vl[0], CL_Sts = str(int(float(vl[1])*100)) + '%', BL_Val = vl[0], BL_Sts = str(int(float(vl[1])*100)) + '%')

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

Citi - IBM Hackathon - Anonymous/Flask Application - Source Code/Flask.py


import h5py
import numpy as np
import pickle
import scipy
import time
import os
import process
from os import listdir
from os.path import isfile, join
import clustering as clr
import denoising as dns

# %%

def means_vars(CODE, PATH_IN, PATH_OUT):
    """ Performs the entire preprocessing pipeline over a single hdf5 file.
    WARNING: When file dimension is huge, memory error occurs.
    """
    # IMPORTING FILE
    hdf = h5py.File(PATH_IN+'target'+CODE+'.hdf5','r')

    x = np.array(hdf['radar'])
    n_frame = x.shape[0]

    r = process.range_doppler(x[0])
    dim = r.shape

    rda = np.empty((n_frame, dim[0], dim[1]))

    for ind in range(n_frame):
        rd = process.range_doppler(x[ind])
        rda[ind, :, :] = np.array(20 * np.log10(rd))
        rda -= np.amax(rda)
        #rd[rd < -45] = -45

    print('RDA:\t\t DONE')

    # DYNAMIC THRESHOLDING
    rda_thresh_g = np.empty((n_frame, dim[0], dim[1]))

    for i in range(n_frame):
        X = rda[0,:,:].flatten()
        m = np.mean(X) + 2*np.std(X)
        rda_thresh_g[i, :, :] = (rda[i,:,:]>m)*1

    print('Thresholding:\t DONE')

    # LINE REMOVING
    rda_bool = np.empty((n_frame, dim[0], dim[1]))

    for i in range(n_frame):
        rda_bool[i,:,:] = dns.remove_line(rda_thresh_g[i,:,:], tickness=0)
        #rda_bool[i,:,:] = remove_line(rda_thresh_g[i,:,:], axis=1, tickness=0)

    print('Line Removing:\t DONE')

    # CLUSTERING
    means_true = np.empty((n_frame, 3))
    # means_noise = np.empty((n_frame, 3))
    vars_true = np.empty((n_frame, 2))

    for ind in range(n_frame):

        data = dns.select_points(rda_bool[ind], rda[ind])
        labels, m = clr.gaussian_mixtures(data, n_cluster_=2, Verbose=False, Bayesian=False)

        i_true = np.argmax(np.array(m)[:, 2])
        means_true[ind, :] = np.array(m)[i_true, :]
        primo = np.std(data[labels==i_true][:,1])
        secondo = np.std(data[labels==i_true][:,2])
        vars_true[ind, :] = np.array([primo, secondo])

    print('Clustering:\t DONE')

    with open(PATH_OUT+'means_vars'+CODE+'.p', 'wb') as outfile:
        pickle.dump((means_true, vars_true), outfile)

    print('Saving Data:\t DONE')
    print('')

# %%

# For each file in PATH_IN directory, means_vars() is applied
# every file name needs to be formatted as target+CODE+.hdf5
PATH_IN = '/Users/orientamento/Desktop/idrad/train copy/'
PATH_OUT = '/Users/orientamento/Desktop/idrad/means_vars_finale/'

# os.system('ls '+ PATH_IN + ' > list.txt')
# f = open("list.txt", "r")
#files = [(f, os.path.getsize(PATH_IN+f)) for f in listdir(PATH_IN) if isfile(join(PATH_IN, f))]
#files = [f for f, size in files if size <= 47236128]

files = listdir(PATH_IN)
codes = []
total_time = 0

for n, line in enumerate(files):
    print('Processing file', line)
    CODE = line[6:-5]
    if line[:6] == 'target' and line[-5:] == '.hdf5':
        print('Code:', CODE)

        start_time = time.time()
        means_vars(CODE, PATH_IN, PATH_OUT)
        end_time = time.time()
        print('Time Required: %.2f' % (end_time-start_time),'sec')
	#total_time += (end_time - start_time)
    else:
        print('Not able to process this file!')

print(n, 'files processed')
print('Average Time Required: %.2f' % total_time/n)
print('Start merging the files')

# creates two files containing all data retrieved so far

PATH_IN = PATH_OUT
PATH_OUT = ''

files = listdir(PATH_IN)
n_files = len(files)

means_array = np.empty((n_files, n_frame, 3))
vars_array = np.empty((n_files, n_frame, 2))

for ind in range(n_files):
    with open(PATH_IN+files[ind], 'rb') as infile:
        means_true, vars_true = pickle.load(infile)

        means_array[ind,:,:] = means_true
        vars_array[ind,:,:] = vars_true

with open(PATH_OUT+'means_array.p', 'wb') as outfile:
        pickle.dump(means_array, outfile)

with open(PATH_OUT+'vars_array.p', 'wb') as outfile:
        pickle.dump(vars_array, outfile)

print('THE END!')


#!/usr/bin/python
"""
Localizer Node

-Purpose is to take in a bounding box from yolo and publish the pose of that object.
-The plan is that this will be done by opencv's solvePnP, so classical techniques will be needed in order
to get some points that we know the 3D mappiing of.
-Input will be darknet ros bounding box msg and output will be our custom detection msg with pose
in local frame, and camera frame
 -Transformation and averaging will be done by the mapper node in global control

Detection.object_type (string)
Detection.camera_frame (string)
Detection.location (pose)

"""
import rospy
import tf
import numpy as np

from darknet_ros_msgs.msg import BoundingBoxes
from darknet_ros_msgs.msg import BoundingBox
from geometry_msgs.msg import PoseArray, Pose
from localizer.msg import Detection

from sensor_msgs.msg import Image

from classical_cv import gate_localizer, dice_localizer

class Localizer():
    def __init__(self):
        # this will be the new one
        self.darknet_sub = rospy.Subscriber('/darknet_ros/bounding_boxes', BoundingBoxes, self.boxes_received)
        self.pose_pub = rospy.Publisher('/Global_State/task_poses', Detection, queue_size=10)

        # old
        # self.darknet_sub = rospy.Subscriber('/darknet_ros/bounding_boxes', BoundingBoxes, self.box_to_pose)
        # self.pose_pub = rospy.Publisher('/Global_State/task_poses', PoseArray, queue_size=10)

        self.gate_localizer = gate_localizer.GateLocalizer()
        self.dice_localizer = dice_localizer.DiceLocalizer()

        # print("Localizer inited")

    # main callback
    # msg is darknet bounding boxes msg
    def boxes_received(self,msg):
        # pass box and image to classical scripts to be cropped and used
        # example: r, t = self.gate_localizer.localize(image, box)

        start_gate_boxes = [box for box in msg.bounding_boxes if "start_gate" in box.Class]
        dice_boxes = [box for box in msg.bounding_boxes if "dice" in box.Class]

        if start_gate_boxes is not None:
            print('Attempt gate localization')
            vecs = self.gate_localizer.localize(msg.image, start_gate_boxes)
            if vecs is not None:
                rot, trans = vecs
                self.publish_pose('start_gate', 'occam0_frame', msg.image_header, rot, trans)

        if dice_boxes is not None:
            # for now we only have the 6
            # TODO: make dynamic
            rot, trans = self.dice_localizer.localize(msg.image, dice_boxes)
            self.publish_pose('dice6', 'occam0_frame', msg.image_header, rot, trans)

    # publish the Detection msg for mapper to take in and transform
    def publish_pose(self, object_type, camera_frame, image_header, rotation, translation):
        pose = Pose()

        # opencv does x to right, y down, and z out from camera
        pose.position.x = translation[2]
        pose.position.y = -1*translation[0] # opencv coord system differs from ours
        pose.position.z = translation[1]

        # TODO: double check opencv rotation
        roll = rotation[0]
        pitch = rotation[0]
        yaw = rotation[0]

        euler_rotation = tf.transformations.quaternion_from_euler(roll, pitch, yaw)
        pose.orientation.x = euler_rotation[0]
        pose.orientation.y = euler_rotation[1]
        pose.orientation.z = euler_rotation[2]
        pose.orientation.w = euler_rotation[3]

        detection = Detection()

        detection.location = pose
        detection.object_type = object_type
        detection.camera_frame = camera_frame
        detection.image_header = image_header

        self.pose_pub.publish(detection)

def main():
    rospy.init_node('localizer')
    l = Localizer()
    try:
        rospy.spin()
    except rospy.ROSInterruptException:
        sys.exit()

if __name__=="__main__":
    main()


from bpy.types import (
        PropertyGroup,
        )

from bpy.props import (
        StringProperty,
        BoolProperty,
        IntProperty,
        IntVectorProperty,
        FloatProperty,
        FloatVectorProperty,
        EnumProperty,
        PointerProperty,
        CollectionProperty,
        )

from . import enums

class PearRayMaterialProperties(PropertyGroup):
    bsdf = EnumProperty(
        name="BSDF",
        description="BSDF type",
        items=enums.enum_material_bsdf,
        default='COOK_TORRANCE'
    )

    cast_shadows = BoolProperty(
        name="Cast Shadows",
        description="Cast Shadows",
        default=True
    )

    cast_self_shadows = BoolProperty(
        name="Cast Self Shadows",
        description="Cast shadows on himself",
        default=True
    )

    is_camera_visible = BoolProperty(
        name="Camera Visible",
        description="Is visible through the camera",
        default=True
    )

    is_shadeable = BoolProperty(
        name="Shadeable",
        description="Will be shaded",
        default=True
    )

    # Emission
    emission_color_type = EnumProperty(
        name="Emission Color Type",
        description="Emission Color Type",
        items=enums.enum_color_type,
        default='COLOR'
    )
    emission_color = FloatVectorProperty(
        name="Emission Color",
        description="Emission Color",
        default=(0,0,0),
        subtype="COLOR",
        soft_max=1,
    )
    emission_color_temp = FloatProperty(
        name="Emission Color Temperature",
        description="Emission Blackbody Color Temperature",
        min=0, soft_max=100000.00, default=5500, step=100
    )
    emission_color_temp_type = EnumProperty(
        name="Emission Color Temperature Type",
        description="Emission Blackbody Color Temperature Type",
        items=enums.enum_temp_type,
        default='LUM'
    )
    emission_color_temp_factor = FloatProperty(
        name="Emission Color Temperature Normalization Factor",
        description="Emission Blackbody Color Temperature Normalization Factor",
        min=0, soft_max=100000.00, default=1, step=100
    )
    emission_color_tex_slot = IntProperty(
        name="Texture Slot",
        description="Used Texture Slot",
        min=0, soft_max=100000, default=0
    )

    # Diffuse
    diffuse_color_type = EnumProperty(
        name="Diffuse Color Type",
        description="Diffuse Color Type",
        items=enums.enum_color_type,
        default='COLOR'
    )
    diffuse_color_temp = FloatProperty(
        name="Diffuse Color Temperature",
        description="Diffuse Blackbody Color Temperature",
        min=0, soft_max=100000.00, default=1000, step=100
    )
    diffuse_color_temp_type = EnumProperty(
        name="Diffuse Color Temperature Type",
        description="Diffuse Blackbody Color Temperature Type",
        items=enums.enum_temp_type,
        default='LUM'
    )
    diffuse_color_temp_factor = FloatProperty(
        name="Diffuse Color Temperature Normalization Factor",
        description="Diffuse Blackbody Color Temperature Normalization Factor",
        min=0, soft_max=100000.00, default=1, step=100
    )
    diffuse_color_tex_slot = IntProperty(
        name="Texture Slot",
        description="Used Texture Slot",
        min=0, soft_max=100000, default=0
    )

    # Specular
    specular_color_type = EnumProperty(
        name="Specular Color Type",
        description="Specular Color Type",
        items=enums.enum_color_type,
        default='COLOR'
    )
    specular_color_temp = FloatProperty(
        name="Specular Color Temperature",
        description="Specular Blackbody Color Temperature",
        min=0, soft_max=100000.00, default=1000, step=100
    )
    specular_color_temp_type = EnumProperty(
        name="Specular Color Temperature Type",
        description="Specular Blackbody Color Temperature Type",
        items=enums.enum_temp_type,
        default='LUM'
    )
    specular_color_temp_factor = FloatProperty(
        name="Specular Color Temperature Normalization Factor",
        description="Specular Blackbody Color Temperature Normalization Factor",
        min=0, soft_max=100000.00, default=1, step=100
    )
    specular_color_tex_slot = IntProperty(
        name="Texture Slot",
        description="Used Texture Slot",
        min=0, soft_max=100000, default=0
    )
    specular_ior_color = FloatVectorProperty(
        name="Specular Index of Refraction",
        description="Specular Index of Refraction",
        default=(1.55,1.55,1.55),
        subtype="COLOR",
        soft_min=1,
        soft_max=3,
    )
    specular_ior_value = FloatProperty(
        name="Specular Index of Refraction",
        description="Specular Index of Refraction",
        default=1.55,
        soft_min=1,
        soft_max=3,
    )
    specular_ior_type = EnumProperty(
        name="Specular Index of Refraction Type",
        description="Specular Index of Refraction Type",
        items=enums.enum_ior_type,
        default='VALUE'
    )

    # Ward
    spec_roughness_x = FloatProperty(
        name="Roughness X",
        description="Roughness to tangent direction",
        min=0, soft_max=1.00, default=0.50
    )
    spec_roughness_y = FloatProperty(
        name="Roughness Y",
        description="Roughness to binormal direction",
        min=0, soft_max=1.00, default=0.50
    )
    reflectivity = FloatProperty(
        name="Reflectivity",
        description="Reflectivity of material",
        min=0, soft_max=1.00, default=0.50
    )

    # CookTorrance
    ct_fresnel_mode = EnumProperty(
        name="Fresnel Mode",
        description="Fresnel Mode",
        items=enums.enum_material_ct_fresnel_mode,
        default='DIELECTRIC'
    )

    ct_distribution_mode = EnumProperty(
        name="Distribution Mode",
        description="Distribution Mode",
        items=enums.enum_material_ct_distribution_mode,
        default='GGX'
    )

    ct_geometry_mode = EnumProperty(
        name="Geometry Mode",
        description="Geometry Mode",
        items=enums.enum_material_ct_geometry_mode,
        default='COOK_TORRANCE'
    )

    # Grid
    grid_first_material = None
    grid_second_material = None
    grid_count = IntProperty(
        name="Grid Count",
        description="Grid Count",
        min=1, soft_max=100000, default=10
    )
    grid_tile_uv = BoolProperty(
        name="Tile UV",
        description="Tile the UV coordinates",
        default=True
    )

    # Glass
    glass_is_thin = BoolProperty(
        name="Thin",
        description="Disables total reflections",
        default=False
    )


import gdal
import numpy as np
import pandas as pd

def image_to_df(filename, bands, nodata=-9999):
    """Reads the specified bands from the specified image and imports
    them to a pandas data frame.

    Parameters:

    * filename: Image filename to read (must be a GDAL-supported format) bands:
    * A list of bands to process. Band IDs are 1-based. For example [1, 2, 3]
    * for a standard Landsat image will process the Blue, Green and Red bands
    * nodata: A No Data value to ignore, defaults to -9999

    Returns: A pandas data frame with a column per band read, with all the data
    in it, with no data values removed. Columns will be named "Bx" where x is
    the band ID.

    """
    im = gdal.Open(filename)

    data = {}

    for band_id in bands:
        band = im.GetRasterBand(band_id).ReadAsArray().astype(float)

        band[band == nodata] = np.nan
        data['B%d' % band_id] = band.ravel()

    df = pd.DataFrame(data)

    df.dropna()

    return df


#!/usr/bin/env python3
# script to plot and compare first order RK4 results

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# purely cosmetic
import seaborn as sns
sns.set()

def main():
    # create dataframe from C++ outfile
    df = pd.read_csv("ARK1.csv", header=0);
    x = df.iloc[:, 0].tolist();
    y2 = df.iloc[:, 1].tolist();

    # create numpy arrays to compare to
    y = np.exp(x);

    # create canvas
    fig = plt.figure();
    ax = plt.axes();

    # plot lines
    line = ax.plot(x, y, label=r"Analytic Solution $e^x$", color='r')
    line2 = ax.scatter(x, y2, label=r"RK4 Solution", s=10, alpha = 0.7, color='b');

    # set preferences
    plt.xlabel("x");
    plt.ylabel("y");
    plt.title("Runge-Kutta Solution of $y' = y$");
    plt.legend();

    # show
    plt.grid(True);
    plt.show();
    # plt.savefig("exp.svg")
    return 0;

main();


import numpy as np
import pandas as pd
 #utf编码格式的csv文件中的中文一般会是乱码，这时需要把文件格式另存为gbk格式
def csv_utf_2_gbk(srcPath):
    try:
        data=pd.DataFrame(pd.read_csv(srcPath,encoding='utf8',low_memory=False))
        data.to_csv(srcPath, index=False, sep=',', encoding='gbk')
    except:
        print(srcPath,"文件处理出错")

def csv_gbk_2_utf(srcPath):
    try:
        data=pd.DataFrame(pd.read_csv(srcPath,encoding='gbk',low_memory=False))
        data.to_csv(srcPath, index=False, sep=',', encoding='utf8')
    except:
        print(srcPath, "文件处理出错")
csv_utf_2_gbk('4s.csv')

# 数据提取
df=pd.read_csv('4s.csv',index_col=u'纳税人编号',encoding='gbk')
# print(data.isnull().sum()/len(data))
# print(len(data))

#数据预处理（将销售类型与销售模式以及输出转换成虚拟变量）
type_dummies=pd.get_dummies(df[u'销售类型'],prefix='type')
model_dummies=pd.get_dummies(df[u'销售模式'],prefix='model')
result_dummies=pd.get_dummies(df[u'输出'],prefix='result')
df=pd.concat([df,type_dummies,model_dummies,result_dummies],axis=1)
df.drop([u'销售类型',u'销售模式',u'输出'],axis=1,inplace=True)
#正常列去除，异常列作为结果
df.drop([u'result_正常'],axis=1,inplace=True)
df.rename(columns={u'result_异常':'result'},inplace=True)
#数据划分(80%作为训练数据，20%作为测试数据)
# data=df.as_matrix()
# from random import shuffle
# shuffle(data)
# data_train=data[:int(len(data)*0.8),:]
# data_test=data[int(len(data)*0.8):,:]
def cm_plot(y, yp):
  from sklearn.metrics import confusion_matrix #导入混淆矩阵函数
  cm = confusion_matrix(y, yp) #混淆矩阵
  import matplotlib.pyplot as plt #导入作图库
  plt.matshow(cm, cmap=plt.cm.Greens) #画混淆矩阵图，配色风格使用cm.Greens，更多风格请参考官网。
  plt.colorbar() #颜色标签
  for x in range(len(cm)): #数据标签
    for y in range(len(cm)):
      plt.annotate(cm[x,y], xy=(x, y), horizontalalignment='center', verticalalignment='center')
  plt.ylabel('True label') #坐标轴标签
  plt.xlabel('Predicted label') #坐标轴标签
  return plt
# cm_plot(train_y,predict_result).show()#混淆矩阵显示

##用于3D可视化
from mpl_toolkits.mplot3d import Axes3D
##用于可视化图表
import matplotlib.pyplot as plt
##用于做科学计算
import numpy as np
##用于做数据分析
import pandas as pd
##用于加载数据或生成数据等
from sklearn import datasets
##导入PCA库
from sklearn.decomposition import PCA
# from sklearn.decomposition import LDA
data=df.as_matrix()
y=data[:,-1]
x=data[:,1:-1]
model_pca = PCA(n_components=3)
X_pca = model_pca.fit(x).transform(x)
print("降维后各主成分方向：\n",model_pca.components_)
print("降维后各主成分的方差值：",model_pca.explained_variance_)
print("降维后各主成分的方差值与总方差之比：",model_pca.explained_variance_ratio_)
print("奇异值分解后得到的特征值：",model_pca.singular_values_)
print("降维后主成分数：",model_pca.n_components_)
fig = plt.figure(figsize=(10,8))
# In[]
ax = Axes3D(fig,rect=[0, 0, 1, 1], elev=30, azim=20)
ax.scatter(X_pca[:, 0], X_pca[:, 1], X_pca[:, 2], marker='o',c=y)

# X_new = pca.transform(x)
# print("original shape: {}".format(str(data.shape)))
# print("reduced shape: {}".format(str(X_new.shape)))
# # from mpl_toolkits.mplot3d import Axes3D
# import matplotlib.pyplot as plt
# plt.scatter(X_new[:, 0], X_new[:, 1],c=y)
# plt.show()


#
# created by
# Antonio Garcia-Uceda Juarez
# PhD student
# Medical Informatics
#
# created on 09/02/2018
# Last update: 09/02/2018
########################################################################################

from CommonUtil.ErrorMessages import *
from CommonUtil.FunctionsUtil import *
import SimpleITK as sitk
import pydicom
from pydicom.dataset import Dataset, FileDataset
import nibabel as nib
import numpy as np
import h5py
import datetime, time
import gzip

class GZIPmanager(object):

    @staticmethod
    def getReadFile(filename):
        return gzip.GzipFile(filename, 'r')

    @staticmethod
    def getWriteFile(filename):
        return gzip.GzipFile(filename, 'w')

    @staticmethod
    def closeFile(fileobj):
        fileobj.close()

class FileReader(object):

    @staticmethod
    def getImageSize(filename):

        basename, extension = ospath_splitext_recurse(filename)

        if (extension == '.dcm'):
            return DICOMreader.getImageSize(filename)
        if (extension == '.dcm.gz'):
            print("Not implemented for extension '.dcm.gz'...")
            return False
            # fileobj = GZIPmanager.getReadFile(filename)
            # out_arrsize = DICOMreader.getImageSize(fileobj)
            # GZIPmanager.closeFile(fileobj)
            # return out_arrsize
        elif (extension == '.nii'):
            return NIFTIreader.getImageSize(filename)
        elif (extension == '.nii.gz'):
            return NIFTIreader.getImageSize(filename)
        elif (extension == '.npy'):
            return NUMPYreader.getImageSize(filename)
        elif (extension == '.npz'):
            return NUMPYZreader.getImageSize(filename)
        elif (extension == '.npy.gz'):
            fileobj = GZIPmanager.getReadFile(filename)
            out_arrsize = NUMPYreader.getImageSize(fileobj)
            GZIPmanager.closeFile(fileobj)
            return out_arrsize
        elif (extension == '.hdf5'):
            return HDF5reader.getImageSize(filename)
        else:
            message = "No valid file extension: %s..." %(extension)
            CatchErrorException(message)

    @staticmethod
    def getImageArray(filename):

        basename, extension = ospath_splitext_recurse(filename)

        if (extension == '.dcm'):
            return DICOMreader.getImageArray(filename)
        if (extension == '.dcm.gz'):
            print("Not implemented for extension '.dcm.gz'...")
            return False
            # fileobj = GZIPmanager.getReadFile(filename)
            # out_array = DICOMreader.getImageArray(fileobj)
            # GZIPmanager.closeFile(fileobj)
            # return out_array
        elif (extension == '.nii'):
            return NIFTIreader.getImageArray(filename)
        elif (extension == '.nii.gz'):
            return NIFTIreader.getImageArray(filename)
        elif (extension == '.npy'):
            return NUMPYreader.getImageArray(filename)
        elif (extension == '.npz'):
            return NUMPYZreader.getImageArray(filename)
        elif (extension == '.npy.gz'):
            fileobj = GZIPmanager.getReadFile(filename)
            out_array = NUMPYreader.getImageArray(fileobj)
            GZIPmanager.closeFile(fileobj)
            return out_array
        elif (extension == '.hdf5'):
            return HDF5reader.getImageArray(filename)
        else:
            message = "No valid file extension: %s..." %(extension)
            CatchErrorException(message)

    @staticmethod
    def writeImageArray(filename, image_array):

        basename, extension = ospath_splitext_recurse(filename)

        if (extension == '.dcm'):
            DICOMreader.writeImageArray(filename, image_array)
        if (extension == '.dcm.gz'):
            print("Not implemented for extension '.dcm.gz'...")
            return False
            # fileobj = GZIPmanager.getWriteFile(filename)
            # DICOMreader.writeImageArray(fileobj, image_array)
            # GZIPmanager.closeFile(fileobj)
        elif (extension == '.nii'):
            NIFTIreader.writeImageArray(filename, image_array)
        elif (extension == '.nii.gz'):
            NIFTIreader.writeImageArray(filename, image_array)
        elif (extension == '.npy'):
            NUMPYreader.writeImageArray(filename, image_array)
        elif (extension == '.npz'):
            NUMPYZreader.writeImageArray(filename, image_array)
        elif (extension == '.npy.gz'):
            fileobj = GZIPmanager.getWriteFile(filename)
            NUMPYreader.writeImageArray(fileobj, image_array)
            GZIPmanager.closeFile(fileobj)
        elif (extension == '.hdf5'):
            HDF5reader.writeImageArray(filename, image_array)
        else:
            message = "No valid file extension: %s..." %(extension)
            CatchErrorException(message)

class HDF5reader(FileReader):

    # get h5py image size:
    @staticmethod
    def getImageSize(filename):
        data_file = h5py.File(filename, 'r')
        return data_file['data'].shape

    # get h5py image array:
    @staticmethod
    def getImageArray(filename):
        data_file = h5py.File(filename, 'r')
        return data_file['data'][:]

    # write h5py file array:
    @staticmethod
    def writeImageArray(filename, image_array):
        data_file = h5py.File(filename, 'w')
        data_file.create_dataset('data', data=image_array)
        data_file.close()

class NUMPYreader(FileReader):

    # get numpy image size:
    @staticmethod
    def getImageSize(filename):
        return np.load(filename).shape

    # get numpy image array:
    @staticmethod
    def getImageArray(filename):
        return np.load(filename)

    # write numpy file array:
    @staticmethod
    def writeImageArray(filename, image_array):
        np.save(filename, image_array)

class NUMPYZreader(FileReader):

    # get numpy image size:
    @staticmethod
    def getImageSize(filename):
        return np.load(filename)['arr_0'].shape

    # get numpy image array:
    @staticmethod
    def getImageArray(filename):
        return np.load(filename)['arr_0']

    # write numpy file array:
    @staticmethod
    def writeImageArray(filename, image_array):
        np.savez_compressed(filename, image_array)

class NIFTIreader(FileReader):
    # In nifty format, the axes are reversed.
    # Need to swap axis and set depth_Z first dim

    # get nifti image size:
    @staticmethod
    def getImageSize(filename):
        nib_im = nib.load(filename)
        return nib_im.get_data().shape[::-1]

    # get nifti image array:
    @staticmethod
    def getImageArray(filename):
        nib_im = nib.load(filename)
        return np.swapaxes(nib_im.get_data(), 0, 2)

    # write nifti file array:
    @staticmethod
    def writeImageArray(filename, image_array):
        nib_im = nib.Nifti1Image(np.swapaxes(image_array, 0, 2), np.eye(4))
        nib.save(nib_im, filename)

class DICOMreader(FileReader):

    # get dcm image dims:
    @staticmethod
    def getImageSize(filename):
        ds = sitk.ReadImage(filename)
        #np.swapaxes(ds.GetSize(), 0, 2)
        return sitk.GetArrayFromImage(ds).shape

    # get dcm voxel size:
    @staticmethod
    def getImageVoxelSize(filename):
        ds = pydicom.read_file(filename)
        voxel_size = (float(ds.PixelSpacing[0]),
                      float(ds.PixelSpacing[1]),
                      float(ds.SpacingBetweenSlices))
        return voxel_size

    # load dcm file array:
    @staticmethod
    def getImageArray(filename):
        ds = sitk.ReadImage(filename)
        return sitk.GetArrayFromImage(ds)

    # write dcm file array:
    @staticmethod
    def writeImageArray(filename, image_array):
        ds = sitk.GetImageFromArray(image_array)
        sitk.WriteImage(ds, filename)

    @staticmethod
    def writeDICOMimage(filename, image_array):

        ## This code block was taken from the output of a MATLAB secondary
        ## capture.  I do not know what the long dotted UIDs mean, but
        ## this code works.
        file_meta = Dataset()
        file_meta.MediaStorageSOPClassUID = 'Secondary Capture Image Storage'
        file_meta.MediaStorageSOPInstanceUID = '1.3.6.1.4.1.9590.100.1.1.111165684411017669021768385720736873780'
        file_meta.ImplementationClassUID = '1.3.6.1.4.1.9590.100.1.0.100.4.0'

        ds = FileDataset(filename, {}, file_meta=file_meta, preamble="\0" * 128)
        ds.Modality = 'WSD'
        ds.ContentDate = str(datetime.date.today()).replace('-', '')
        ds.ContentTime = str(time.time())  # milliseconds since the epoch
        ds.StudyInstanceUID = '1.3.6.1.4.1.9590.100.1.1.124313977412360175234271287472804872093'
        ds.SeriesInstanceUID = '1.3.6.1.4.1.9590.100.1.1.369231118011061003403421859172643143649'
        ds.SOPInstanceUID = '1.3.6.1.4.1.9590.100.1.1.111165684411017669021768385720736873780'
        ds.SOPClassUID = 'Secondary Capture Image Storage'
        ds.SecondaryCaptureDeviceManufctur = 'Python 2.7.3'

        ## These are the necessary imaging components of the FileDataset object.
        ds.SamplesPerPixel = 1
        ds.PhotometricInterpretation = "MONOCHROME2"
        ds.PixelRepresentation = 0
        ds.HighBit = 15
        ds.BitsStored = 16
        ds.BitsAllocated = 16
        ds.SmallestImagePixelValue = '\\x00\\x00'
        ds.LargestImagePixelValue = '\\xff\\xff'
        ds.Rows = image_array.shape[0]
        ds.Columns = image_array.shape[1]
        if image_array.dtype != np.uint16:
            image_array = image_array.astype(np.uint16)
        ds.PixelData = image_array.tostring()

        ds.save_as(filename)

    # get dcm header info:
    @staticmethod
    def loadPatientInformation(filename):

        ds = pydicom.read_file(filename)

        information = {}
        information['PatientID'] = ds.PatientID
        information['PatientName'] = ds.PatientName
        information['PatientBirthDate'] = ds.PatientBirthDate
        information['PatientSex'] = ds.PatientSex
        information['StudyID'] = ds.StudyID
        # information['StudyTime'] = ds.Studytime
        information['InstitutionName'] = ds.InstitutionName
        information['Manufacturer'] = ds.Manufacturer
        information['NumberOfFrames'] = ds.NumberOfFrames
        return information

    # copy PixelData info and save image
    @staticmethod
    def copyPixelDataAndSaveImage(origfilename, newfilename):

        orig_ds = pydicom.read_file(origfilename)
        new_ds  = pydicom.read_file(newfilename)
        orig_ds.PixelData = new_ds.PixelData
        orig_ds.save_as(origfilename)

# All Available File Readers
DICTAVAILFILEREADERS = {"numpy": NUMPYreader,
                        "dicom": DICOMreader,
                        "nifti": NIFTIreader }


'''

A video capturing tool.

You can choose to capture frames either from live cctvs
or from local video files.

To capture from live streaming:
python3 cctv_to_img.py -ch 3 -m l -n 5
-> You will get 5 consecutive frames for the third screen of your cctv.

To capture from local video files:
python3 cctv_to_img.py -ch 3 -m r -n 10
-> You will get 10 copies of capture where two images have the interval of
   1/10 of the total duration.

To get captures from all local video channels:
python3 cctv_to_img.py -ch 0 -m r -n 15

'''

import cv2
import argparse
import os

RTSP_URL = '' # The rtsp url of your cctv.
FIRST_PORT_NUM = 0 # The first port number that refers to your first channel.
W, H = 0, 0 # The width and height of a image that you would like to save.
INPUT_PATH = 'records/' # The input path where your local videos located in.
OUTPUT_PATH = 'capture/' # The output path where you save your captures.

def cctv_to_img(channel, mode, n):

    port_num = FIRST_PORT_NUM + (channel - 1)
    if mode == 'l':
        cap = cv2.VideoCapture(RTSP_URL + ':{}'.format(port_num))

        # if there is no directory, mkdir and save imgs.
        if not(os.path.exists(OUTPUT_PATH + 'live_cap/channel_{}'.format(channel))):
            os.mkdir(OUTPUT_PATH + 'live_cap/channel_{}'.format(channel))

        count = 0
        while(count < n):
            _, frame = cap.read()
            cv2.imwrite(OUTPUT_PATH + 'live_cap/channel_{}/frame_{}.jpg'.format(channel, count + 1), frame)
            count += 1

        cap.release()

    elif mode == 'r':
        cap = cv2.VideoCapture(INPUT_PATH + 'channel_{}.mp4'.format(channel))
        fps = cap.get(cv2.CAP_PROP_FPS)
        total_f_num = cap.get(cv2.CAP_PROP_FRAME_COUNT)
        f_interval = int(total_f_num/n)

        # if there is no directory, mkdir and save imgs.
        if not(os.path.exists(OUTPUT_PATH + 'record_cap/channel_{}'.format(channel))):
            os.mkdir(OUTPUT_PATH + 'record_cap/channel_{}'.format(channel))

        for idx in range(n):
            cap.set(cv2.CAP_PROP_POS_FRAMES, f_interval * idx)
            _, frame = cap.read()
            # capture imgs
            cv2.imwrite(OUTPUT_PATH + 'record_cap/channel_{}/frame_{}.jpg'.format(channel, idx + 1), frame)

        cap.release()

if __name__ == '__main__':
    # create a parser object
    parser = argparse.ArgumentParser("A video capturing tool")
    # add arguments
    parser.add_argument('-ch', nargs=1, type=int, required=True, choices={0,1,2,3,4,5,6,7}, \
        help='channel number(0: capture all videos)')
    parser.add_argument('-m', nargs=1, type=str, choices={'l', 'r'}, required=True, \
        help='l: live streaming capture, r: recorded video capture')
    parser.add_argument('-n', nargs=1, default=[10], type=int, \
        help='the number of captures you want')
    # parse the arguments from standard input
    args = parser.parse_args()

    # if ch 0 is given, capture all channels
    if(args.ch[0] == 0):
        for i in range(1,8):
            cctv_to_img(i,args.m[0], args.n[0])
    else:
        cctv_to_img(args.ch[0], args.m[0], args.n[0])


from aiida.engine import ExitCode
from aiida.common.exceptions import NotExistent
from aiida.parsers.parser import Parser
from aiida_phonopy.common.raw_parsers import (
    parse_thermal_properties, parse_FORCE_CONSTANTS, parse_projected_dos,
    parse_total_dos, parse_band_structure)

class PhonopyParser(Parser):
    """
    Parser the DATA files from phonopy.
    """

    def __init__(self, calc):
        """
        Initialize the instance of PhonopyParser
        """
        super(PhonopyParser, self).__init__(calc)

    def parse(self, **kwargs):
        """
        Parses the datafolder, stores results.
        """
        self.logger.info("Parsing start.")

        # select the folder object
        # Check that the retrieved folder is there
        try:
            output_folder = self.retrieved
        except NotExistent:
            return self.exit_codes.ERROR_NO_RETRIEVED_FOLDER

        # check what is inside the folder
        list_of_files = output_folder.list_object_names()

        # OUTPUT file should exist
        # if not self._calc._OUTPUT_FILE_NAME in list_of_files:
        #    successful = False
        #    self.logger.error("Output file not found")
        #    return successful, ()

        # Get files and do the parsing

        fc_filename = self.node.inputs.force_constants_filename.value
        if fc_filename in list_of_files:
            with output_folder.open(fc_filename) as f:
                fname = f.name
            self.out('force_constants', parse_FORCE_CONSTANTS(fname))

        projected_dos_filename = self.node.inputs.projected_dos_filename.value
        if projected_dos_filename in list_of_files:
            with output_folder.open(projected_dos_filename) as f:
                fname = f.name
            self.out('pdos', parse_projected_dos(fname))

        total_dos_filename = self.node.inputs.projected_dos_filename.value
        if total_dos_filename in list_of_files:
            with output_folder.open(total_dos_filename) as f:
                fname = f.name
            self.out('dos', parse_total_dos(fname))

        tp_filename = self.node.inputs.thermal_properties_filename.value
        if tp_filename in list_of_files:
            with output_folder.open(tp_filename) as f:
                fname = f.name
            self.out('thermal_properties', parse_thermal_properties(fname))

        band_filename = self.node.inputs.band_structure_filename.value
        if band_filename in list_of_files:
            if 'symmetry' in self.node.inputs.settings.attributes:
                sym_dataset = self.node.inputs.settings['symmetry']
                label = "%s (%d)" % (sym_dataset['international'],
                                     sym_dataset['number'])
            else:
                label = None
            with output_folder.open(band_filename) as f:
                fname = f.name
            self.out('band_structure',
                     parse_band_structure(fname, label=label))

        self.logger.info("Parsing done.")
        return ExitCode(0)


from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import os
import numpy as np
import dicom
from scipy import misc
from dicom.tag import Tag
from math import ceil

class Ui_MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super(Ui_MainWindow, self).__init__()
        self.setupUi()
        self.zooming = False
        self.adjust_window = False
        self.cropping = False
        self.lengthing = False
        self.angling = False
        self.angle_point_loaded = False
        self.default_zoom_percentage = 100
        self.ImageViewWidth = 600
        self.ImageViewHeight = 600

        self.setAcceptDrops(True)
        self.show()
    def setupUi(self):
        self.resize(1060, 680)
        self.setStyleSheet("background-color: rgb(229, 255, 240);")

        self.centralWidget = QtWidgets.QWidge

        font = QtGui.QFont()
        font.setPointSize(11)
        #Top Left Label_Graphics
        self.Label_TL = QtWidgets.QLabel()
        self.Label_TL.setGeometry(QtCore.QRect(0, 20, 150, 100))
        self.Label_TL.setFont(font)
        self.Label_TL.setStyleSheet('color: red; background-color: none')

        #Top Right Label_Graphics
        self.Label_TR = QtWidgets.QLabel()
        self.Label_TR.setGeometry(QtCore.QRect(500, 40, 100, 60))
        self.Label_TR.setFont(font)
        self.Label_TR.setStyleSheet('color: red; background-color: red')

        #Bottom Left Label_Graphics
        self.Label_BL = QtWidgets.QLabel()
        self.Label_BL.setGeometry(QtCore.QRect(0, 580, 100, 60))
        self.Label_BL.setFont(font)
        self.Label_BL.setStyleSheet('color: red; background-color: none')

        #Bottom Right Label_Graphics
        self.Label_BR = QtWidgets.QLabel()
        self.Label_BR.setGeometry(QtCore.QRect(500, 580, 100, 60))
        self.Label_BR.setFont(font)
        self.Label_BR.setStyleSheet('color: red; background-color: none')

        #Label Length Label
        self.Label_Length = QtWidgets.QLabel()
        self.Label_Length.setStyleSheet('color: red; background-color: none')
        self.Label_Length.setGeometry(QtCore.QRect(250, 580, 80, 30))
        self.Label_Length.setVisible(False)

        #Angle Length Label
        self.Label_Angle = QtWidgets.QLabel()
        self.Label_Angle.setStyleSheet('color: red; background-color: none')
        self.Label_Angle.setGeometry(QtCore.QRect(250, 0, 80, 30))
        self.Label_Angle.setVisible(False)

        #Graphic Label
        self.Label_Graphics = QtWidgets.QLabel()
        self.Label_Graphics.setGeometry(QtCore.QRect(0, 40, 600, 600))
        self.Label_Graphics.setStyleSheet("border:none;\n"
                                          "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(229, 255, 240, 255), stop:1 rgba(255, 255, 255, 255));\n"
                                          "")

def run():
    app = QtWidgets.QApplication(sys.argv)
    ui = Ui_MainWindow()

    sys.exit(app.exec_())

if __name__ == "__main__":
    run()


# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt

"""Tests of coverage/python.py"""

import os
import sys

import pytest

from coverage import env
from coverage.python import get_zip_bytes, source_for_file

from tests.coveragetest import CoverageTest

class GetZipBytesTest(CoverageTest):
    """Tests of `get_zip_bytes`."""

    run_in_temp_dir = False

    def test_get_encoded_zip_files(self):
        # See igor.py, do_zipmods, for the text of these files.
        zip_file = "tests/zipmods.zip"
        sys.path.append(zip_file)       # So we can import the files.
        for encoding in ["utf8", "gb2312", "hebrew", "shift_jis", "cp1252"]:
            filename = zip_file + "/encoded_" + encoding + ".py"
            filename = filename.replace("/", os.sep)
            zip_data = get_zip_bytes(filename)
            zip_text = zip_data.decode(encoding)
            self.assertIn('All OK', zip_text)
            # Run the code to see that we really got it encoded properly.
            __import__("encoded_"+encoding)

def test_source_for_file(tmpdir):
    path = tmpdir.join("a.py")
    src = str(path)
    assert source_for_file(src) == src
    assert source_for_file(src + 'c') == src
    assert source_for_file(src + 'o') == src
    unknown = src + 'FOO'
    assert source_for_file(unknown) == unknown

@pytest.mark.skipif(not env.WINDOWS, reason="not windows")
def test_source_for_file_windows(tmpdir):
    path = tmpdir.join("a.py")
    src = str(path)

    # On windows if a pyw exists, it is an acceptable source
    path_windows = tmpdir.ensure("a.pyw")
    assert str(path_windows) == source_for_file(src + 'c')

    # If both pyw and py exist, py is preferred
    path.ensure(file=True)
    assert source_for_file(src + 'c') == src

def test_source_for_file_jython():
    assert source_for_file("a$py.class") == "a.py"


#!/usr/bin/env python3
# Copyright (c) 2004-present Facebook All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

import unittest

from pyinventory.api.customer import add_customer
from pyinventory.api.equipment import add_equipment
from pyinventory.api.equipment_type import add_equipment_type
from pyinventory.api.link import add_link, get_port
from pyinventory.api.location import add_location
from pyinventory.api.location_type import add_location_type
from pyinventory.api.port_type import add_equipment_port_type
from pyinventory.api.service import (
    add_service,
    add_service_endpoint,
    add_service_link,
    get_service,
)
from pyinventory.api.service_type import add_service_type
from pyinventory.common.data_class import PropertyDefinition
from pyinventory.graphql.enum.property_kind import PropertyKind
from pysymphony import SymphonyClient

from ..utils.base_test import BaseTest
from ..utils.grpc.rpc_pb2_grpc import TenantServiceStub

class TestService(BaseTest):
    def __init__(
        self, testName: str, client: SymphonyClient, stub: TenantServiceStub
    ) -> None:
        super().__init__(testName, client, stub)

    def setUp(self) -> None:
        super().setUp()
        self.service_type = add_service_type(
            client=self.client,
            name="Internet Access",
            has_customer=True,
            properties=[
                PropertyDefinition(
                    property_name="Service Package",
                    property_kind=PropertyKind.string,
                    default_value="Public 5G",
                    is_fixed=False,
                ),
                PropertyDefinition(
                    property_name="Address Family",
                    property_kind=PropertyKind.string,
                    default_value=None,
                    is_fixed=False,
                ),
            ],
        )
        self.service = add_service(
            client=self.client,
            name="Room 201 Internet Access",
            external_id="S3232",
            service_type=self.service_type.name,
            customer=None,
            properties_dict={"Address Family": "v4"},
        )
        self.customer = add_customer(
            client=self.client, name="Donald", external_id="S322"
        )
        self.service_with_customer = add_service(
            client=self.client,
            name="Room 202 Internet Access",
            external_id="S32325",
            service_type=self.service_type.name,
            customer=self.customer,
            properties_dict={"Address Family": "v4"},
        )

    def test_service_created(self) -> None:
        fetched_service = get_service(client=self.client, id=self.service.id)
        self.assertEqual(fetched_service, self.service)

    def test_service_with_customer_created(self) -> None:
        fetched_service = get_service(
            client=self.client, id=self.service_with_customer.id
        )
        self.assertEqual(fetched_service, self.service_with_customer)
        fetched_customer = fetched_service.customer
        self.assertNotEqual(fetched_customer, None)
        self.assertEqual(fetched_customer, self.customer)

    @unittest.skip("Will be restored once new endpoint schema is finalized")
    def test_service_with_topology_created(self) -> None:
        add_equipment_port_type(
            self.client,
            name="port type 1",
            properties=[
                PropertyDefinition(
                    property_name="port property",
                    property_kind=PropertyKind.string,
                    default_value="port property value",
                    is_fixed=False,
                )
            ],
            link_properties=[
                PropertyDefinition(
                    property_name="link property",
                    property_kind=PropertyKind.string,
                    default_value="link property value",
                    is_fixed=False,
                )
            ],
        )
        add_location_type(
            client=self.client,
            name="Room",
            properties=[("Contact", "email", None, True)],
        )
        location = add_location(
            client=self.client,
            location_hirerchy=[("Room", "Room 201")],
            properties_dict={"Contact": "user@google.com"},
            lat=10,
            long=20,
        )
        add_equipment_type(
            client=self.client,
            name="Tp-Link T1600G",
            category="Router",
            properties=[("IP", "string", None, True)],
            ports_dict={"Port 1": "port type 1", "Port 2": "port type 1"},
            position_list=[],
        )
        router1 = add_equipment(
            client=self.client,
            name="TPLinkRouter1",
            equipment_type="Tp-Link T1600G",
            location=location,
            properties_dict={"IP": "192.688.0.1"},
        )
        router2 = add_equipment(
            client=self.client,
            name="TPLinkRouter2",
            equipment_type="Tp-Link T1600G",
            location=location,
            properties_dict={"IP": "192.688.0.2"},
        )
        router3 = add_equipment(
            client=self.client,
            name="TPLinkRouter3",
            equipment_type="Tp-Link T1600G",
            location=location,
            properties_dict={"IP": "192.688.0.3"},
        )
        link1 = add_link(
            client=self.client,
            equipment_a=router1,
            port_name_a="Port 1",
            equipment_b=router2,
            port_name_b="Port 1",
        )
        link2 = add_link(
            client=self.client,
            equipment_a=router2,
            port_name_a="Port 2",
            equipment_b=router3,
            port_name_b="Port 1",
        )

        endpoint_port = get_port(
            client=self.client, equipment=router1, port_name="Port 2"
        )

        for link in [link1, link2]:
            add_service_link(
                client=self.client, service_id=self.service.id, link_id=link.id
            )
        # TODO add service_endpoint_defintion api
        add_service_endpoint(
            client=self.client,
            service_id=self.service.id,
            equipment_id="1",
            endpoint_definition_id="1",
        )

        ports = [e.port for e in self.service.endpoints]
        self.assertEqual(
            [endpoint_port.id], [p.id if p is not None else None for p in ports]
        )
        self.assertEqual([link1.id, link2.id], [s.id for s in self.service.links])


# mi_aplicacion/views.py

from django.shortcuts import render, HttpResponse, redirect
from django.http import HttpResponseBadRequest
from .forms import *
from .models import Autor, Libro, Prestamo

# Create your views here.

def index(request):
    context = {}
    return render(request,'base.html', context)

def test_template(request):
    context = {}   # Aquí van la las variables para la plantilla
    return render(request,'test.html', context)

def lista_autores(request):
    context = {'autores':Autor.objects.all()}
    return render(request,'listado_autores.html', context)

def anadir_autor(request):
    form = AutorForm(request.POST)

    if request.method == 'POST':

        if form.is_valid():
            nuevo_autor = Autor(nombre=form.cleaned_data.get('nombre'))

            nuevo_autor.save()

            return redirect('/lista_autores')
    else:
        return render(request, 'formulario_crispy.html', {'form': AutorForm})

def borrar_autor(request):
    form = request.POST.dict()

    if request.method == 'POST':
        try:
            autor = Autor.objects.get(id=form.get('autor_id'))
            autor.delete()
        except:
            return HttpResponseBadRequest()

    return redirect('/lista_autores')

def modificar_autor(request, id_autor):

    if request.method == 'POST':
        form = request.POST.dict()
        #Si venimos de la lista
        if form.get('autor_id'):
            try:
                autor_modif = Autor.objects.get(id=form.get('autor_id'))

                formulario_modificar = AutorForm(instance=autor_modif)

                return render(request, 'formulario_crispy.html', {'form': formulario_modificar})
            except:
                return HttpResponseBadRequest()
        #si venimos del formulario de modificacion
        else:
            form = AutorForm(request.POST)
            if form.is_valid():
                try:
                    autor_modif = Autor.objects.get(id=id_autor)
                    autor_modif.nombre = form.cleaned_data.get('nombre')
                    autor_modif.save()
                except:
                    return HttpResponseBadRequest()
            else:
                return HttpResponseBadRequest()

    return redirect('/lista_autores')

def lista_libros(request):
    context = {'libros':Libro.objects.all()}
    return render(request,'listado_libros.html', context)

def anadir_libro(request):
    form = LibroForm(request.POST, request.FILES)

    if request.method == 'POST':

        if form.is_valid():

            nuevo_libro = form.save(commit=False)
            autor = form.cleaned_data['autores']
            nuevo_libro.save()

            nuevo_libro.autores.set(autor)

            return redirect('/lista_libros')
        else:
            return HttpResponse('formulario invalido')
    else:
        return render(request, 'formulario_crispy.html', {'form': LibroForm})

def borrar_libro(request):
    form = request.POST.dict()

    if request.method == 'POST':
        if form.get('libro_id') != '':
            libro = Libro.objects.get(id=form.get('libro_id'))
            libro.delete()

    return redirect('/lista_libros')

def modificar_libro(request, id_libro):

    if request.method == 'POST':
        form = request.POST.dict()
        #Si venimos de la lista
        if form.get('libro_id'):
            try:
                libro_modif = Libro.objects.get(id=form.get('libro_id'))

                formulario_modificar = LibroForm(instance=libro_modif)

                return render(request, 'formulario_crispy.html', {'form': formulario_modificar})
            except:
                return HttpResponseBadRequest()
        #si venimos del formulario de modificacion
        else:
            try:
                libro_modif = Libro.objects.get(id=id_libro)
            except:
                return HttpResponseBadRequest()

            form = LibroForm(request.POST, request.FILES, instance=libro_modif)
            if form.is_valid():
                libro_modif.titulo = form.cleaned_data.get('titulo')
                libro_modif.portada = form.cleaned_data.get('portada')
                autor = form.cleaned_data['autores']

                libro_modif.save()

                libro_modif.autores.set(autor)
            else:
                return HttpResponse('formulario invalido')

    return redirect('/lista_libros')

def lista_prestamos(request):
    context = {'prestamos':Prestamo.objects.all()}
    return render(request,'listado_prestamos.html', context)

def anadir_prestamo(request):
    form = PrestamoForm(request.POST, request.FILES)

    if request.method == 'POST':

        if form.is_valid():
            nuevo_prestamo = form.save(commit=False)
            libro = form.cleaned_data['libro']
            nuevo_prestamo.libro = libro
            nuevo_prestamo.save()

            return redirect('/lista_prestamos')
        else:
            return HttpResponse('formulario invalido')
    else:
        return render(request, 'formulario_crispy.html', {'form': PrestamoForm})

def borrar_prestamo(request):
    form = request.POST.dict()

    if request.method == 'POST':
        if form.get('prestamo_id') != '':
            prest = Prestamo.objects.get(id=form.get('prestamo_id'))
            prest.delete()

    return redirect('/lista_prestamos')

def modificar_prestamo(request, id_prestamo):

    if request.method == 'POST':
        form = request.POST.dict()
        #Si venimos de la lista
        if form.get('prestamo_id'):
            try:
                prest_modif = Prestamo.objects.get(id=form.get('prestamo_id'))

                formulario_modificar = PrestamoForm(instance=prest_modif)

                return render(request, 'formulario_crispy.html', {'form': formulario_modificar})
            except:
                return HttpResponseBadRequest()
        #si venimos del formulario de modificacion
        else:
            form = PrestamoForm(request.POST)
            if form.is_valid():
                try:
                    prest_modif = Prestamo.objects.get(id=id_prestamo)
                    prest_modif.libro = form.cleaned_data.get('libro')
                    prest_modif.fecha = form.cleaned_data.get('fecha')
                    prest_modif.usuario = form.cleaned_data.get('usuario')

                    prest_modif.save()

                except:
                    return HttpResponseBadRequest()

    return redirect('/lista_prestamos')


from django.views.decorators.cache import cache_control
from django.views.decorators.csrf import csrf_protect
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import redirect
from redis7 import in_defenders

@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
@csrf_protect
def redirect_to_content(request):
	"""
	Helper function for return_to_content()
	"""
	orig = request.POST.get("orig",None)
	topic = request.POST.get("topic",None)
	obid = request.POST.get("obid",None)
	oun = request.POST.get("oun",None)
	lid = request.POST.get("lid",None)
	if topic:
		request.session["origin_topic"] = topic
	return return_to_content(request=request,origin=orig,obj_id=obid,link_id=lid,target_uname=oun)

def return_to_content(request,origin,obj_id=None,link_id=None,target_uname=None,source_origin=None):
	"""
	Decides where to redirect user to

	This is merely a redirect view and needs no url pattern (request is passed from other views, e.g. redirect_to_content())
	"""
	if origin in ('1','20'):
		# originated from fresh photos page
		if origin == '20':
			# single notification on fresh photos
			return redirect("photo",list_type='fresh-list')
		else:
			return redirect(reverse_lazy("redirect_to_photo",kwargs={'list_type': 'fresh-list','pk':obj_id}))
	elif origin in ('2','21'):
		# originated from best photos
		if origin == '21':
			# single notification on best photos
			return redirect("photo",list_type='best-list')
		else:
			return redirect(reverse_lazy("redirect_to_photo",kwargs={'list_type': 'best-list','pk':obj_id}))
	elif origin in ('3','19'):
		if origin == '19':
			# single notification on 'fresh_text'
			return redirect("fresh_text")
		# originated from home
		else:
			request.session["home_hash_id"] = link_id
			request.modified = True
			return redirect("redirect_to_home")
	elif origin == '4':
		# originated from user profile
		request.session["photograph_id"] = obj_id
		request.modified = True
		return redirect("profile_photos_redirect",target_uname,'fotos')
		# return redirect("profile", target_uname, 'fotos')
	elif origin == '5':
		# originated from photo detail
		return redirect("photo_detail", obj_id)
	elif origin == '6':
		# originated from 'cull_content' (a defender view)
		if in_defenders(request.user.id):
			return redirect("cull_content")
		else:
			return redirect("photo",list_type='fresh-list')
	elif origin == '7':
		# originated from fresh photos page
		return redirect("photo",list_type='fresh-list')
	elif origin == '8':
		# originated from home history
		if target_uname:
			return redirect("user_activity", target_uname)
		else:
			return redirect("fresh_text")
	elif origin == '9':
		# originated from a publicreply
		if source_origin:
			url = reverse_lazy("publicreply_view",kwargs={'parent_id':obj_id, 'origin':source_origin})+"#reply"
		else:
			url = reverse_lazy("publicreply_view",kwargs={'parent_id':obj_id})+"#reply"
		return redirect(url)
	elif origin == '10':
		# originated from user profile (About page)
		return redirect("user_profile", target_uname)
	elif origin == '11':
		# originated from the comments page
		if source_origin:
			url = reverse_lazy("comment",kwargs={'pk':obj_id, 'origin':source_origin})+"#reply"
		else:
			url = reverse_lazy("comment",kwargs={'pk':obj_id})+"#reply"
		return redirect(url)
	elif origin == '12' or origin == '13':
		# originated from 'best home'
		if origin == '12':
			# best home
			request.session["home_hash_id"] = link_id
			request.modified = True
			return redirect("best_home_redirect")
		else:
			# best home (from "direct-reply" notification)
			return redirect("best_home_page")
	elif origin == '14':
		# originated from a user's trending posts list
		return redirect("display_trending_history",target_uname=target_uname)
	elif origin == '15':
		# originated from a private group
		request.session["unique_id"] = obj_id#obj_id contains group_uuid, otherwise it won't work
		url = reverse_lazy("private_group_reply")+"#sectionJ"
		return redirect(url)
	elif origin == '16':
		# originated from a public group
		url = reverse_lazy("public_group",kwargs={'slug': obj_id})#obj_id contains group_uuid, otherwise it won't work
		return redirect(url)
	elif origin == '17':
		# originated from private chat list
		return redirect("personal_group_user_listing")
	elif origin == '18':
		# originated from received invites' list
		return redirect("show_personal_group_invite_list",'received')
	elif origin == '22':
		# originated from a topic page
		topic_url = request.session.pop("origin_topic",'')
		url = reverse_lazy("topic_redirect",kwargs={'topic_url':topic_url, 'obj_hash':link_id}) if link_id else reverse_lazy("topic_redirect",kwargs={'topic_url':topic_url})
		return redirect(url)
	elif origin == '23':
		# originated from online kon
		url = reverse_lazy("online_kon")+"#top"
		return redirect(url)
	elif origin == '24':
		# originated from direct response page
		return redirect(reverse_lazy("retrieve_direct_responses"))
	elif origin == '25':
		# originated from 'upvoting' history page
		return redirect('user_vote_history')
	elif origin in ('26','38'):
		# originated from single notif on 'for_me'
		if origin == '38':
			return redirect("custom_feed_redirect")
		# originated from 'my home'
		else:
			if link_id:
				return redirect(reverse_lazy("custom_feed_redirect",kwargs={'obj_hash':link_id}))
			else:
				return redirect("custom_feed_redirect")
	elif origin == '27':
		# originated from 'followers list'
		var = request.session.pop('page_num',None)
		if var:
			url = reverse_lazy("show_follower_list",kwargs={})+"?page="+var
		else:
			url = reverse_lazy("show_follower_list",kwargs={})
		return redirect(url)
	elif origin == '28':
		# originated from 'followers list'
		var = request.session.pop('page_num',None)
		if var:
			url = reverse_lazy("show_following_list",kwargs={})+"?page="+str(var)
		else:
			url = reverse_lazy("show_following_list",kwargs={})
		return redirect(url)
	elif origin == '29':
		# originated from 'publicly shared item history'
		var = request.session.pop('page_num',None)
		if var:
			url = reverse_lazy("display_user_public_feed_history",kwargs={'target_uname':target_uname})+"?page="+str(var)
		else:
			url = reverse_lazy("display_user_public_feed_history",kwargs={'target_uname':target_uname})
		# return redirect("display_user_public_feed_history", target_uname)
		return redirect(url)
	elif origin == '30':
		# originated from 'publicly shared follower history'
		var = request.session.pop('page_num',None)
		if var:
			url = reverse_lazy("display_user_follower_feed_history",kwargs={'target_uname':target_uname})+"?page="+str(var)
		else:
			url = reverse_lazy("display_user_follower_feed_history",kwargs={'target_uname':target_uname})
		# return redirect("display_user_public_feed_history", target_uname)
		return redirect(url)
	elif origin == '31':
		# originated from 'privately shared follower history'
		var = request.session.pop('page_num',None)
		if var:
			url = reverse_lazy("display_user_private_feed_history",kwargs={'target_uname':target_uname})+"?page="+str(var)
		else:
			url = reverse_lazy("display_user_private_feed_history",kwargs={'target_uname':target_uname})
		# return redirect("display_user_public_feed_history", target_uname)
		return redirect(url)
	elif origin == '32':
		# originated from 'new followers list'
		url = reverse_lazy("show_new_followers",kwargs={})
		return redirect(url)
	elif origin == '33':
		# originated from 'upvoting' history page (admin view)
		return redirect('vote_history_admin_view', user_id=obj_id)
	elif origin == '34':
		# originated from content detail (specifically - an image, i.e. obj_type is 'g')
		return redirect("content_detail_view", pk=obj_id, obj_type='g')
	elif origin == '35':
		# originated from 'reply' history page
		return redirect('retrieve_direct_response_activity')
	elif origin == '36':
		# originated from 'popular mehfil' list
		# DEPRECATED
		return redirect('get_ranked_groups')
	elif origin == '37':
		# originated from 'Red Stars' list
		return redirect('top_photo')
	############
	# origin '38' is also used up
	############
	else:
		# when no origin, redirect to 'for_me'
		if link_id:
			return redirect(reverse_lazy("custom_feed_redirect",kwargs={'obj_hash':link_id}))
		else:
			return redirect("custom_feed_redirect")

def main_navbar(request):
	"""
	Handles navigation of the bottom menu
	"""
	decision = request.GET.get('dec', None)
	if decision == '1':
		# redirect to 'for me'
		return redirect("for_me")
	elif decision == '2a':
		# redirect to inbox replies
		return redirect("retrieve_direct_responses")
	elif decision == '2b':
		# redirect to inbox activity
		return redirect("retrieve_direct_response_activity")
	elif decision == '3':
		# redirect to content sharing page
		return redirect("share_content")
	elif decision == '4':
		# redirect to best photos
		return redirect("photo",list_type='best-list')
	elif decision == '5':
		# redirect to best text
		return redirect("best_home_page")
	elif decision == '6':
		# redirect to sign up
		return redirect("unauth_home_new")
	elif decision == '7':
		# redirect to login
		return redirect("login")
	elif decision == '8':
		# redirect to user's profile
		username = request.user.username
		if username:
			return redirect("user_profile",request.user.username)
		else:
			# in case the user was logged out in the special circumstance of having changed their password
			return redirect("login")
	elif decision == '9':
		# redirect to top stars
		return redirect("top_photo")
	elif decision == '10':
		# redirect to privacy_policy
		return redirect("privacy_policy")
	elif decision == '11':
		# redirect to mehfils list
		return redirect("group_page")
	elif decision == '12':
		# redirect to online
		return redirect("online_kon")
	elif decision == '13':
		# redirect to topics
		return redirect("topic_listing")
	elif decision == '14':
		# redirect to more
		return redirect("more_options")
	elif decision == '15a':
		# redirect to 1on1 list
		return redirect("personal_group_user_listing")
	elif decision == '15b':
		# redirect to 1on1 invites received
		return redirect("show_personal_group_invite_list",'received')
	elif decision == '16':
		# redirect to help
		return redirect("help")
	elif decision == '17':
		# redirect to about
		return redirect("about")
	elif decision == '18':
		# redirect to logout
		return redirect("bahirniklo")
	elif decision == '19':
		# redirect to user_verification
		return redirect("verify_user_mobile_unpaid")
	elif decision == '20':
		# redirect to search
		return redirect("search_username")
	else:
		# default redirect
		return redirect("home")


from socket import socket, AF_INET, SOCK_STREAM
from threading import Thread
from config.configs import color

# classe para manipular o socket
class Send:
    def __init__(self):
        self.__msg = ''
        self.new = True
        self.con = None

    def put(self, msg):
        self.__msg = msg
        if self.con != None:
            # envia um mensagem atravez de uma conexão socket
            self.con.send(str.encode(self.__msg))

    def get(self):
        return self.__msg

    def loop(self):
        return self.new

# função esperar - Thread
def esperar(tcp, send, host='localhost', port=5000):
    destino = (host, port)
    # conecta ao servidor
    tcp.connect(destino)

    while send.loop():
        print(color.YELLOW + 'Conectado a ', host, '.' + color.END)
        # atribui a conexão ao manipulador
        send.con = tcp
        while send.loop():
            # aceita uma mensagem
            msg = tcp.recv(1024)
            if not msg: break
            print(str(msg, 'utf-8'))

if __name__ == '__main__':
    print(color.GREEN + 'Digite o seu nome: ' + color.END)
    ClienteName = input()

    print(color.YELLOW + 'Olá, ' + ClienteName + '. Meu nome é Luna, sou a I.A do Chat, estarei aqui para lhe auxiliar no que for preciso.' + color.END)

    print(color.GREEN + '\nPor favor, digite o nome ou IP do servidor(localhost): ' + color.END)
    host = input()

    if host == '':
        host = '127.0.0.1'

    # cria um socket
    tcp = socket(AF_INET, SOCK_STREAM)
    send = Send()
    # cria um Thread e usa a função esperar com dois argumentos
    processo = Thread(target=esperar, args=(tcp, send, host))
    processo.start()
    print('')

    msg = color.DARKCYAN + input() + color.END
    while True:
        send.put(color.BOLD + ClienteName + ': ' + msg + color.END + '\n')
        print('-----------------------------------------------------------------------------------')
        print(color.BOLD + "Digite sua mensagem: " + color.END)
        msg = color.DARKCYAN + input() + color.END

    processo.join()
    tcp.close()
    exit()


r"""Convert raw PASCAL dataset to TFRecord for object_detection.

Example usage:
    python X2_object_detection/dataset_tools/create_tfrecord_test.py
    --data_dir=/home/dm/my_data_generation/  \
    --set=train  \
    --output_path=/home/dm/my_data_generation/tfrecord/train.record
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import hashlib
import io
import os

import PIL.Image
import tensorflow as tf
import sys
sys.path.append('.')

from object_detection.utils import dataset_util

import json

flags = tf.app.flags
flags.DEFINE_string('data_dir', '', 'Root directory to raw PASCAL VOC dataset.')
flags.DEFINE_string('set', '', 'Convert training set, validation set or merged set.')
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
flags.DEFINE_boolean('ignore_difficult_instances', False, 'Whether to ignore difficult instances')
FLAGS = flags.FLAGS

CLASSES = ('__background__',  # always index 0
           'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'zero',
           'add', 'minus', 'multiple1', 'multiple2', 'division1', 'division2',
           'left_bracket', 'right_bracket', 'left_middle_bracket', 'right_middle_bracket',
           'a', 'b', 'c', 'n', 'x', 'y', 'z', 'equal'
           )

class DataProcessor:
    def __init__(self, data_root,
                 image_subdirectory='img', annotation_subdirectory='anno'):
        self.data_root = data_root
        self.image_subdirectory = image_subdirectory
        self.annotation_subdirectory = annotation_subdirectory
        print("Construct DataProcessor for " + self.data_root)

    def dict_to_tf_example(self, data, fileID, label_map_dict):
        # Load Image
        # img_ext = '.jpg'
        path = os.path.join(self.data_root, self.image_subdirectory, fileID.replace('.json', '.jpg'))

        with tf.gfile.GFile(path, 'rb') as fid:
            encoded_jpg = fid.read()
        encoded_jpg_io = io.BytesIO(encoded_jpg)
        image = PIL.Image.open(encoded_jpg_io)
        if image.format != 'JPEG':
            raise ValueError('Image format not JPEG')
        key = hashlib.sha256(encoded_jpg).hexdigest()

        width = int(data['image_info']['width'])
        height = int(data['image_info']['height'])

        xmin = []
        ymin = []
        xmax = []
        ymax = []
        classes = []
        classes_text = []
        truncated = []
        poses = []
        difficult_obj = []

        objects = data['bbox']

        # object include {x1, y1, x2, y2, class_id}
        for idx, obj in enumerate(objects):
            xmin.append(float(obj[0]) / width)
            ymin.append(float(obj[1]) / height)
            xmax.append(float(obj[2]) / width)
            ymax.append(float(obj[3]) / height)

            class_id = int(obj[4])
            classes.append(class_id)
            classes_text.append(CLASSES[class_id].encode('utf8'))

            truncated.append(int(0))
            poses.append('Unspecified'.encode('utf8'))

        example = tf.train.Example(features=tf.train.Features(feature={
            'image/height': dataset_util.int64_feature(height),
            'image/width': dataset_util.int64_feature(width),
            'image/filename': dataset_util.bytes_feature((fileID.replace('.json', '.jpg')).encode('utf8')),
            'image/source_id': dataset_util.bytes_feature((fileID.replace('.json', '.jpg')).encode('utf8')),
            'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')),
            'image/encoded': dataset_util.bytes_feature(encoded_jpg),
            'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),
            'image/object/bbox/xmin': dataset_util.float_list_feature(xmin),
            'image/object/bbox/xmax': dataset_util.float_list_feature(xmax),
            'image/object/bbox/ymin': dataset_util.float_list_feature(ymin),
            'image/object/bbox/ymax': dataset_util.float_list_feature(ymax),
            'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
            'image/object/class/label': dataset_util.int64_list_feature(classes),
            'image/object/difficult': dataset_util.int64_list_feature(difficult_obj),
            'image/object/truncated': dataset_util.int64_list_feature(truncated),
            'image/object/view': dataset_util.bytes_list_feature(poses),
        }))

        return example

def main(_):
    data_dir = FLAGS.data_dir
    set = FLAGS.set
    writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
    # label_map_dict = label_map_util.get_label_map_dict(FLAGS.label_map_path)

    dp = DataProcessor(data_root=os.path.join(data_dir, set),
                       image_subdirectory='img',
                       annotation_subdirectory='anno'
                       )
    annotations_dir = os.path.join(data_dir, set, 'anno')
    examples_list = os.listdir(annotations_dir)
    for idx, fileID in enumerate(examples_list):
        if idx % 100 == 0:
            print('On image %d of %d' % (idx, len(examples_list)))
        path = os.path.join(annotations_dir, fileID)
        # Read Annofiles
        with open(path, 'r') as fid:
            data = json.load(fid)
        tf_example = dp.dict_to_tf_example(data, fileID, None)
        writer.write(tf_example.SerializeToString())

    writer.close()

if __name__ == '__main__':
    tf.app.run()