text:
import os.path as osp
import math
import json
from PIL import Image

import torch
import numpy as np
import cv2
import albumentations as A
from torch.utils.data import Dataset
from shapely.geometry import Polygon

def cal_distance(x1, y1, x2, y2):
    '''calculate the Euclidean distance'''
    return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)

def move_points(vertices, index1, index2, r, coef):
    '''move the two points to shrink edge
    Input:
        vertices: vertices of text region <numpy.ndarray, (8,)>
        index1  : offset of point1
        index2  : offset of point2
        r       : [r1, r2, r3, r4] in paper
        coef    : shrink ratio in paper
    Output:
        vertices: vertices where one edge has been shinked
    '''
    index1 = index1 % 4
    index2 = index2 % 4
    x1_index = index1 * 2 + 0
    y1_index = index1 * 2 + 1
    x2_index = index2 * 2 + 0
    y2_index = index2 * 2 + 1

    r1 = r[index1]
    r2 = r[index2]
    length_x = vertices[x1_index] - vertices[x2_index]
    length_y = vertices[y1_index] - vertices[y2_index]
    length = cal_distance(vertices[x1_index], vertices[y1_index], vertices[x2_index], vertices[y2_index])
    if length > 1:
        ratio = (r1 * coef) / length
        vertices[x1_index] += ratio * (-length_x)
        vertices[y1_index] += ratio * (-length_y)
        ratio = (r2 * coef) / length
        vertices[x2_index] += ratio * length_x
        vertices[y2_index] += ratio * length_y
    return vertices

def shrink_poly(vertices, coef=0.3):
    '''shrink the text region
    Input:
        vertices: vertices of text region <numpy.ndarray, (8,)>
        coef    : shrink ratio in paper
    Output:
        v       : vertices of shrinked text region <numpy.ndarray, (8,)>
    '''
    x1, y1, x2, y2, x3, y3, x4, y4 = vertices
    r1 = min(cal_distance(x1,y1,x2,y2), cal_distance(x1,y1,x4,y4))
    r2 = min(cal_distance(x2,y2,x1,y1), cal_distance(x2,y2,x3,y3))
    r3 = min(cal_distance(x3,y3,x2,y2), cal_distance(x3,y3,x4,y4))
    r4 = min(cal_distance(x4,y4,x1,y1), cal_distance(x4,y4,x3,y3))
    r = [r1, r2, r3, r4]

    # obtain offset to perform move_points() automatically
    if cal_distance(x1,y1,x2,y2) + cal_distance(x3,y3,x4,y4) > \
       cal_distance(x2,y2,x3,y3) + cal_distance(x1,y1,x4,y4):
        offset = 0 # two longer edges are (x1y1-x2y2) & (x3y3-x4y4)
    else:
        offset = 1 # two longer edges are (x2y2-x3y3) & (x4y4-x1y1)

    v = vertices.copy()
    v = move_points(v, 0 + offset, 1 + offset, r, coef)
    v = move_points(v, 2 + offset, 3 + offset, r, coef)
    v = move_points(v, 1 + offset, 2 + offset, r, coef)
    v = move_points(v, 3 + offset, 4 + offset, r, coef)
    return v

def get_rotate_mat(theta):
    '''positive theta value means rotate clockwise'''
    return np.array([[math.cos(theta), -math.sin(theta)], [math.sin(theta), math.cos(theta)]])

def rotate_vertices(vertices, theta, anchor=None):
    '''rotate vertices around anchor
    Input:
        vertices: vertices of text region <numpy.ndarray, (8,)>
        theta   : angle in radian measure
        anchor  : fixed position during rotation
    Output:
        rotated vertices <numpy.ndarray, (8,)>
    '''
    v = vertices.reshape((4,2)).T
    if anchor is None:
        anchor = v[:,:1]
    rotate_mat = get_rotate_mat(theta)
    res = np.dot(rotate_mat, v - anchor)
    return (res + anchor).T.reshape(-1)

def get_boundary(vertices):
    '''get the tight boundary around given vertices
    Input:
        vertices: vertices of text region <numpy.ndarray, (8,)>
    Output:
        the boundary
    '''
    x1, y1, x2, y2, x3, y3, x4, y4 = vertices
    x_min = min(x1, x2, x3, x4)
    x_max = max(x1, x2, x3, x4)
    y_min = min(y1, y2, y3, y4)
    y_max = max(y1, y2, y3, y4)
    return x_min, x_max, y_min, y_max

def cal_error(vertices):
    '''default orientation is x1y1 : left-top, x2y2 : right-top, x3y3 : right-bot, x4y4 : left-bot
    calculate the difference between the vertices orientation and default orientation
    Input:
        vertices: vertices of text region <numpy.ndarray, (8,)>
    Output:
        err     : difference measure
    '''
    x_min, x_max, y_min, y_max = get_boundary(vertices)
    x1, y1, x2, y2, x3, y3, x4, y4 = vertices
    err = cal_distance(x1, y1, x_min, y_min) + cal_distance(x2, y2, x_max, y_min) + \
          cal_distance(x3, y3, x_max, y_max) + cal_distance(x4, y4, x_min, y_max)
    return err

def find_min_rect_angle(vertices):
    '''find the best angle to rotate poly and obtain min rectangle
    Input:
        vertices: vertices of text region <numpy.ndarray, (8,)>
    Output:
        the best angle <radian measure>
    '''
    angle_interval = 1
    angle_list = list(range(-90, 90, angle_interval))
    area_list = []
    for theta in angle_list:
        rotated = rotate_vertices(vertices, theta / 180 * math.pi)
        x1, y1, x2, y2, x3, y3, x4, y4 = rotated
        temp_area = (max(x1, x2, x3, x4) - min(x1, x2, x3, x4)) * \
                    (max(y1, y2, y3, y4) - min(y1, y2, y3, y4))
        area_list.append(temp_area)

    sorted_area_index = sorted(list(range(len(area_list))), key=lambda k: area_list[k])
    min_error = float('inf')
    best_index = -1
    rank_num = 10
    # find the best angle with correct orientation
    for index in sorted_area_index[:rank_num]:
        rotated = rotate_vertices(vertices, angle_list[index] / 180 * math.pi)
        temp_error = cal_error(rotated)
        if temp_error < min_error:
            min_error = temp_error
            best_index = index
    return angle_list[best_index] / 180 * math.pi

def is_cross_text(start_loc, length, vertices):
    '''check if the crop image crosses text regions
    Input:
        start_loc: left-top position
        length   : length of crop image
        vertices : vertices of text regions <numpy.ndarray, (n,8)>
    Output:
        True if crop image crosses text region
    '''
    if vertices.size == 0:
        return False
    start_w, start_h = start_loc
    a = np.array([start_w, start_h, start_w + length, start_h, start_w + length, start_h + length,
                  start_w, start_h + length]).reshape((4, 2))
    p1 = Polygon(a).convex_hull
    for vertice in vertices:
        p2 = Polygon(vertice.reshape((4, 2))).convex_hull
        inter = p1.intersection(p2).area
        if 0.01 <= inter / p2.area <= 0.99:
            return True
    return False

def crop_img(img, vertices, labels, length):
    '''crop img patches to obtain batch and augment
    Input:
        img         : PIL Image
        vertices    : vertices of text regions <numpy.ndarray, (n,8)>
        labels      : 1->valid, 0->ignore, <numpy.ndarray, (n,)>
        length      : length of cropped image region
    Output:
        region      : cropped image region
        new_vertices: new vertices in cropped region
    '''
    h, w = img.height, img.width
    # confirm the shortest side of image >= length
    if h >= w and w < length:
        img = img.resize((length, int(h * length / w)), Image.BILINEAR)
    elif h < w and h < length:
        img = img.resize((int(w * length / h), length), Image.BILINEAR)
    ratio_w = img.width / w
    ratio_h = img.height / h
    assert(ratio_w >= 1 and ratio_h >= 1)

    new_vertices = np.zeros(vertices.shape)
    if vertices.size > 0:
        new_vertices[:,[0,2,4,6]] = vertices[:,[0,2,4,6]] * ratio_w
        new_vertices[:,[1,3,5,7]] = vertices[:,[1,3,5,7]] * ratio_h

    # find random position
    remain_h = img.height - length
    remain_w = img.width - length
    flag = True
    cnt = 0
    while flag and cnt < 1000:
        cnt += 1
        start_w = int(np.random.rand() * remain_w)
        start_h = int(np.random.rand() * remain_h)
        flag = is_cross_text([start_w, start_h], length, new_vertices[labels==1,:])
    box = (start_w, start_h, start_w + length, start_h + length)
    region = img.crop(box)
    if new_vertices.size == 0:
        return region, new_vertices

    new_vertices[:,[0,2,4,6]] -= start_w
    new_vertices[:,[1,3,5,7]] -= start_h
    return region, new_vertices

def rotate_all_pixels(rotate_mat, anchor_x, anchor_y, length):
    '''get rotated locations of all pixels for next stages
    Input:
        rotate_mat: rotatation matrix
        anchor_x  : fixed x position
        anchor_y  : fixed y position
        length    : length of image
    Output:
        rotated_x : rotated x positions <numpy.ndarray, (length,length)>
        rotated_y : rotated y positions <numpy.ndarray, (length,length)>
    '''
    x = np.arange(length)
    y = np.arange(length)
    x, y = np.meshgrid(x, y)
    x_lin = x.reshape((1, x.size))
    y_lin = y.reshape((1, x.size))
    coord_mat = np.concatenate((x_lin, y_lin), 0)
    rotated_coord = np.dot(rotate_mat, coord_mat - np.array([[anchor_x], [anchor_y]])) + \
                                                   np.array([[anchor_x], [anchor_y]])
    rotated_x = rotated_coord[0, :].reshape(x.shape)
    rotated_y = rotated_coord[1, :].reshape(y.shape)
    return rotated_x, rotated_y

def resize_img(img, vertices, size):
    h, w = img.height, img.width
    ratio = size / max(h, w)
    if w > h:
        img = img.resize((size, int(h * ratio)), Image.BILINEAR)
    else:
        img = img.resize((int(w * ratio), size), Image.BILINEAR)
    new_vertices = vertices * ratio
    return img, new_vertices

def adjust_height(img, vertices, ratio=0.2):
    '''adjust height of image to aug data
    Input:
        img         : PIL Image
        vertices    : vertices of text regions <numpy.ndarray, (n,8)>
        ratio       : height changes in [0.8, 1.2]
    Output:
        img         : adjusted PIL Image
        new_vertices: adjusted vertices
    '''
    ratio_h = 1 + ratio * (np.random.rand() * 2 - 1)
    old_h = img.height
    new_h = int(np.around(old_h * ratio_h))
    img = img.resize((img.width, new_h), Image.BILINEAR)

    new_vertices = vertices.copy()
    if vertices.size > 0:
        new_vertices[:,[1,3,5,7]] = vertices[:,[1,3,5,7]] * (new_h / old_h)
    return img, new_vertices

def rotate_img(img, vertices, angle_range=10):
    '''rotate image [-10, 10] degree to aug data
    Input:
        img         : PIL Image
        vertices    : vertices of text regions <numpy.ndarray, (n,8)>
        angle_range : rotate range
    Output:
        img         : rotated PIL Image
        new_vertices: rotated vertices
    '''
    center_x = (img.width - 1) / 2
    center_y = (img.height - 1) / 2
    angle = angle_range * (np.random.rand() * 2 - 1)
    img = img.rotate(angle, Image.BILINEAR)
    new_vertices = np.zeros(vertices.shape)
    for i, vertice in enumerate(vertices):
        new_vertices[i,:] = rotate_vertices(vertice, -angle / 180 * math.pi, np.array([[center_x],[center_y]]))
    return img, new_vertices

def generate_roi_mask(image, vertices, labels):
    mask = np.ones(image.shape[:2], dtype=np.float32)
    ignored_polys = []
    for vertice, label in zip(vertices, labels):
        if label == 0:
            ignored_polys.append(np.around(vertice.reshape((4, 2))).astype(np.int32))
    cv2.fillPoly(mask, ignored_polys, 0)
    return mask

def filter_vertices(vertices, labels, ignore_under=0, drop_under=0):
    if drop_under == 0 and ignore_under == 0:
        return vertices, labels

    new_vertices, new_labels = vertices.copy(), labels.copy()

    areas = np.array([Polygon(v.reshape((4, 2))).convex_hull.area for v in vertices])
    labels[areas < ignore_under] = 0

    if drop_under > 0:
        passed = areas >= drop_under
        new_vertices, new_labels = new_vertices[passed], new_labels[passed]

    return new_vertices, new_labels

class SceneTextDataset(Dataset):
    def __init__(self, root_dir, split='train', image_size=1024, crop_size=512, color_jitter=True,
                 normalize=True):
        with open(osp.join(root_dir, 'ufo/{}.json'.format(split)), 'r') as f:
            anno = json.load(f)

        self.anno = anno
        self.image_fnames = sorted(anno['images'].keys())
        self.image_dir = osp.join(root_dir, 'images')

        self.image_size, self.crop_size = image_size, crop_size
        self.color_jitter, self.normalize = color_jitter, normalize

    def __len__(self):
        return len(self.image_fnames)

    def __getitem__(self, idx):
        image_fname = self.image_fnames[idx]
        image_fpath = osp.join(self.image_dir, image_fname)

        vertices, labels = [], []
        for word_info in self.anno['images'][image_fname]['words'].values():
            vertices.append(np.array(word_info['points']).flatten())
            labels.append(int(not word_info['illegibility']))
        vertices, labels = np.array(vertices, dtype=np.float32), np.array(labels, dtype=np.int64)

        vertices, labels = filter_vertices(vertices, labels, ignore_under=10, drop_under=1)

        image = Image.open(image_fpath)
        image, vertices = resize_img(image, vertices, self.image_size)
        image, vertices = adjust_height(image, vertices)
        image, vertices = rotate_img(image, vertices)
        image, vertices = crop_img(image, vertices, labels, self.crop_size)

        if image.mode != 'RGB':
            image = image.convert('RGB')
        image = np.array(image)

        funcs = []
        if self.color_jitter:
            funcs.append(A.ColorJitter(0.5, 0.5, 0.5, 0.25))
        if self.normalize:
            funcs.append(A.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)))
        transform = A.Compose(funcs)

        image = transform(image=image)['image']
        word_bboxes = np.reshape(vertices, (-1, 4, 2))
        roi_mask = generate_roi_mask(image, vertices, labels)

        return image, word_bboxes, roi_mask

max_stars_repo_name: jongho-park/EAST

max_issues_repo_name: jongho-park/EAST

max_forks_repo_name: jongho-park/EAST


text:
#!/usr/bin/env python3

from os.path import abspath
from sys import exit
from csv import writer as CSVWriter
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter

import numpy as np

from skimage import io

# from skimage.viewer.plugins import LabelPainter
# Unfortunately LabelPainter is currently broken, let's implement a basic matplotlib solution

import matplotlib.pyplot as plt
import matplotlib.patches as mpatch

def main():
    args = mkArguments()

    image = io.imread(args.image)

    plt.imshow(image)

    points = []

    def onButton(event):
        x, y = int(event.xdata), int(event.ydata)
        points.append((x, y))
        print('Rectangle[{}]: at {},{}'.format('start' if len(points) % 2 != 0 else 'end', x, y))

    def onKey(event):
        if event.key == 'q' and points:
            points.pop()
        elif event.key == 'w':
            exit('Error: discarding rectangles, label again')
        elif event.key == 'e':
            plt.close()

    plt.connect('button_press_event', onButton)
    plt.connect('key_press_event', onKey)

    plt.title('mouse press: add point, q: pop point, w: discard, e: next')
    plt.axis('off')
    plt.show()

    if not points or len(points) % 2 != 0:
        exit('Error: unable to make up rectangles from no or odd number of points')

    with open(args.labelFile, 'a') as handle:
        writer = CSVWriter(handle)
        for i in range(0, len(points) - 1, 2):
            p0, p1 = points[i], points[i+1]
            (x0, y0) = np.min([p0, p1], axis=0)
            (x1, y1) = np.max([p0, p1], axis=0)

            # Region is made up of: path, x0, y0, x1, y1 (bottom left, top right)
            writer.writerow([abspath(args.image), x0, y0, x1, y1])

def mkArguments():
    parser = ArgumentParser(description='Label images with rectangle regions of interest',
                            formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument('image', type=str, help='image to label with rectangles')
    parser.add_argument('labelFile', type=str, help='.csv file to append labeled regions for image to')
    return parser.parse_args()

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        exit('\nBye')

ext: py

max_stars_repo_name: daniel-j-h/exit-signs

max_issues_repo_name: daniel-j-h/exit-signs

max_forks_repo_name: daniel-j-h/exit-signs

reason: import numpy


text:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

# Plot 1-D gaussian
n = 1         # n number of independent 1-D gaussian
m= 1000       # m points in 1-D gaussian
f_random = np.random.normal(size=(n, m))
# more information about 'size': https://www.sharpsightlabs.com/blog/numpy-random-normal/
#print(f_random.shape)

for i in range(n):
    #sns.distplot(f_random[i], hist=True, rug=True, vertical=True, color="orange")
    sns.distplot(f_random[i], hist=True, rug=True)

plt.title('1 random samples from a 1-D Gaussian distribution')
plt.xlabel('x')
plt.ylabel('P(x)')
plt.savefig('fig1.png')


text:
# Imports from 3rd party libraries
import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_daq as daq
import dash_html_components as html
from dash.dependencies import Input, Output
from joblib import load
import pandas as pd
import numpy as np

# Imports from this application
from app import app

#Load pipeline
pipeline = load('pipeline.joblib')

# 2 column layout. 1st column width = 4/12
# https://dash-bootstrap-components.opensource.faculty.ai/l/components/layout
column1 = dbc.Col(
    [
        dcc.Markdown(
            """

            ## Decision Predictor

            Select or enter the following inputs to find out which party will win.

            Follow this [link](heroku.com) to find out what values to input for text fields.

            """,
            className='mb-5'
        ),

        dcc.Markdown('#### [Petitioner (Person or Entity)](https://ds15-unit2-build.herokuapp.com/features)', className='mt-1'),
        dcc.Input(
            id='petitioner',
            placeholder=' Enter a number',
            type='number',
            value=''
            ),

        dcc.Markdown('#### [Respondent (Person or Entity)](https://ds15-unit2-build.herokuapp.com/features)', className='mt-3'),
        dcc.Input(
            id='respondent',
            placeholder=' Enter a number',
            type='number',
            value=''
            ),

        dcc.Markdown('#### [Case Source](https://ds15-unit2-build.herokuapp.com/features)', className='mt-3'),
        dcc.Input(
            id='caseSource',
            placeholder=' Enter a number',
            type='number',
            value=''
            ),

        dcc.Markdown('#### [Case Origin](https://ds15-unit2-build.herokuapp.com/features)', className='mt-3'),
        dcc.Input(
            id='caseOrigin',
            placeholder=' Enter a number',
            type='number',
            value=''
            ),

        dcc.Markdown('#### Reason for Certiorari', className='mt-3'),
        dcc.Dropdown(
            id='certReason',
            options=[
            {'label': '1 - Case did not arise on cert or cert not granted', 'value': '1.0'},
            {'label': '2 - Federal court conflict', 'value': '2.0'},
            {'label': '3 - Federal court conflict and to resolve important or significant question', 'value': '3.0'},
            {'label': '4 - Putative conflict', 'value': '4.0'},
            {'label': '5 - Conflict between federal court and state court', 'value': '5.0'},
            {'label': '6 - State court conflict', 'value': '6.0'},
            {'label': '7 - Federal court confusion or uncertainty', 'value': '7.0'},
            {'label': '8 - State court confusion or uncertainty', 'value': '8.0'},
            {'label': '9 - Federal court and state court confusion or uncertainty', 'value': '9.0'},
            {'label': '10 - To resolve important or significant question', 'value': '10.0'},
            {'label': '11 - To resolve question presented', 'value': '11.0'},
            {'label': '12 - No reason given', 'value': '12.0'},
            {'label': '13 - Other reason', 'value': '13.0'},
            {'label': '12 - No reason given', 'value': 'nan'}
            ],
        value=''
        ),

        dcc.Markdown('#### Lower Court Disposition', className='mt-3'),
        dcc.Dropdown(
            id='lcDisposition',
            options=[
            {'label': '1 - Stay, petition, or motion granted', 'value': '1.0'},
            {'label': '2 - Affirmed', 'value': '2.0'},
            {'label': '3 - Reversed', 'value': '3.0'},
            {'label': '4 - Reversed and remanded', 'value': '4.0'},
            {'label': '5 - Vacated and remanded', 'value': '5.0'},
            {'label': '6 - Affirmed and reversed (or vacated) in part', 'value': '6.0'},
            {'label': '7 - Affirmed and reversed (or vacated) in part and remanded', 'value': '7.0'},
            {'label': '8 - Vacated', 'value': '8.0'},
            {'label': '9 - Petition denied or appeal dismissed', 'value': '9.0'},
            {'label': '10 - Modify', 'value': '10.0'},
            {'label': '11 - Remand', 'value': '11.0'},
            {'label': '12 - Unusual disposition', 'value': '12.0'},
            {'label': '13 - Unspecified', 'value': 'nan'}
            ],
        value=''
        ),

        dcc.Markdown('#### Issue Area', className='mt-3'),
        dcc.Dropdown(
            id='issueArea',
            options=[
            {'label': '1 - Criminal Procedure', 'value': '1.0'},
            {'label': '2 - Civil Rights', 'value': '2.0'},
            {'label': '3 - First Amendment', 'value': '3.0'},
            {'label': '4 - Due Process', 'value': '4.0'},
            {'label': '5 - Privacy', 'value': '5.0'},
            {'label': '6 - Attorneys', 'value': '6.0'},
            {'label': '7 - Unions', 'value': '7.0'},
            {'label': '8 - Economic Activity', 'value': '8.0'},
            {'label': '9 - Judicial Power', 'value': '9.0'},
            {'label': '10 - Federalism', 'value': '10.0'},
            {'label': '11 - Interstate Relations', 'value': '11.0'},
            {'label': '12 - Federal Taxation', 'value': '12.0'},
            {'label': '13 - Miscellaneous', 'value': '13.0'},
            {'label': '14 - Private Action', 'value': '14.0'},
            {'label': '13 - Miscellaneous', 'value': 'nan'}
            ],
        value=''
        ),
        dcc.Markdown("",id='out1'),

    dcc.Markdown('#### [Specific Issue](https://ds15-unit2-build.herokuapp.com/features)', className='mt-3'),
    dcc.Input(
        id='issue',
        placeholder=' Enter a number',
        type='number',
        value=''
        ),

    dcc.Markdown('#### Legal Provision', className='mt-3'),
    dcc.Dropdown(
        id='lawType',
        options=[
            {'label': '1 - Constitution', 'value': 1.0},
            {'label': '2 - Constitutional Amendment', 'value': '2.0'},
            {'label': '3 - Federal statute', 'value': '3.0'},
            {'label': '4 - Court rules', 'value': '4.0'},
            {'label': '5 - Other', 'value': '5.0'},
            {'label': '6 - Infrequently litigated statutes', 'value': '6.0'},
            {'label': '8 - State or local law or regulation', 'value': '8.0'},
            {'label': '9 - No legal provision', 'value': '9.0'},
            {'label': '10 - Unspecified', 'value': 'nan'}
            ],
        value=''
        ),

    dcc.Markdown('#### [Specific Legal Provision](https://ds15-unit2-build.herokuapp.com/features)', className='mt-3'),
    dcc.Input(
        id='lawSupp',
        placeholder=' Enter a number',
        type='number',
        value=''
        ),

    ],
    md=6,
)

column2 = dbc.Col(
    [
        html.H2('Predicted Prevailing Party', className='mb-3'),
        html.Div(id='prediction-content', className='lead'),

        # daq.Gauge(
        #     id='my-daq-gauge',
        #     max=100,
        #     value=50,
        #     min=0
        # ),

    ]
)

@app.callback(
    Output(component_id='prediction-content', component_property='children'),
    [
        Input(component_id='petitioner', component_property='value'),
        Input(component_id='respondent', component_property='value'),
        Input(component_id='caseSource', component_property='value'),
        Input(component_id='caseOrigin', component_property='value'),
        Input(component_id='certReason', component_property='value'),
        Input(component_id='lcDisposition', component_property='value'),
        Input(component_id='issueArea', component_property='value'),
        Input(component_id='issue', component_property='value'),
        Input(component_id='lawType', component_property='value'),
        Input(component_id='lawSupp', component_property='value')

        ]
)

def predict(petitioner, respondent, caseSource, caseOrigin, certReason, lcDisposition, issueArea, issue, lawType, lawSupp):
    df = pd.DataFrame(
        columns=['petitioner','respondent','caseSource','caseOrigin','certReason','lcDisposition','issueArea','issue','lawType','lawSupp'],
        data=[[petitioner, respondent, caseSource, caseOrigin, certReason, lcDisposition, issueArea, issue, lawType, lawSupp]]
    )

    y_pred = pipeline.predict(df)
    # return f'{y_pred*100:.0f}% Probability'

    if y_pred == '1.0':
        return html.Img(src='assets/Petitioner.png',className='img-fluid', style = {'height': '400px'})
    else:
        return html.Img(src='assets/Respondent.png',className='img-fluid', style = {'height': '400px'})

def update_output_div(petitioner, respondent, caseSource, caseOrigin, certReason, lcDisposition, issueArea, issue, lawType, lawSupp):
    return f'{petitioner} {respondent} {caseSource} {caseOrigin} {certReason} {lcDisposition} {issueArea} {issue} {lawType} {lawSupp}'
layout = dbc.Row([column1, column2])


text:
import os
import numpy as np
from utide.utilities import loadbunch

datadir = os.path.join(os.path.dirname(__file__))

fname = 'ut_constants'
data = loadbunch(
    os.path.join(datadir, '{}.mat'.format(fname)), masked=False)
np.savez('{}.npz'.format(fname), **data)

fname = 'FUV0'
data = loadbunch(
    os.path.join(datadir, '{}.mat'.format(fname)), masked=False)
np.savez('{}.npz'.format(fname), **data)

max_stars_repo_name: PanhaiDongPhD/UTide

max_issues_repo_name: berglindp/UTide

max_forks_repo_name: berglindp/UTide


text:
import logging

import numpy as np
import theano.tensor as tt
import pymc3 as pm

from bambi import version
from bambi.priors import Prior

from .base import BackEnd
from .utils import probit, cloglog

_log = logging.getLogger("bambi")

class PyMC3BackEnd(BackEnd):
    """PyMC3 model-fitting backend."""

    # Available link functions
    links = {
        "identity": lambda x: x,
        "logit": tt.nnet.sigmoid,
        "probit": probit,
        "cloglog": cloglog,
        "inverse": tt.inv,
        "inverse_squared": lambda x: tt.inv(tt.sqrt(x)),
        "log": tt.exp,
    }

    dists = {"HalfFlat": pm.Bound(pm.Flat, lower=0)}

    def __init__(self):
        self.name = pm.__name__
        self.version = pm.__version__

        # Attributes defined elsewhere
        self.model = None
        self.mu = None  # build()
        self.spec = None  # build()
        self.trace = None  # build()
        self.advi_params = None  # build()
        self.fit = False  # run()

    def build(self, spec):  # pylint: disable=arguments-differ
        """Compile the PyMC3 model from an abstract model specification.

        Parameters
        ----------
        spec : Bambi model
            A Bambi ``Model`` instance containing the abstract specification of the model
            to compile.
        """

        coords = spec._get_pymc_coords()  # pylint: disable=protected-access
        self.model = pm.Model(coords=coords)
        noncentered = spec.noncentered

        ## Add common effects
        # Common effects have at most ONE coord.
        # If ndim > 1 we're certain there's more than one column (see squeeze)
        with self.model:
            self.mu = 0.0
            for term in spec.common_terms.values():
                data = term.data.squeeze()
                label = term.name
                dist = term.prior.name
                args = term.prior.args
                if term.pymc_coords:
                    dims = list(term.pymc_coords.keys())
                    coef = self.build_common_distribution(dist, label, dims=dims, **args)
                else:
                    shape = () if data.ndim == 1 else data.shape[1]
                    coef = self.build_common_distribution(dist, label, shape=shape, **args)

                if data.ndim == 1:
                    self.mu += data * coef
                else:
                    self.mu += pm.math.dot(data, coef)

        ## Add group-specific effects
        # Group-specific effects always have pymc_coords. At least for the group.
        # The loop through predictor columns is not the most beautiful alternative.
        # But it's the fastest. Doing matrix multiplication, pm.math.dot(data, coef), is slower.

        # Add group specific terms that have prior for their correlation matrix
        with self.model:
            for group, eta in spec.priors_cor.items():
                # pylint: disable=protected-access
                terms = [spec.terms[name] for name in spec._get_group_specific_groups()[group]]
                self.mu += add_lkj(terms, eta)

        # Add group specific terms that don't have a prior for their correlation matrix
        terms = [
            term
            for term in spec.group_specific_terms.values()
            if term.name.split("|")[1] not in spec.priors_cor
        ]
        with self.model:
            for term in terms:
                label = term.name
                dist = term.prior.name
                args = term.prior.args
                predictor = term.predictor.squeeze()
                dims = list(term.pymc_coords.keys())
                coef = self.build_group_specific_distribution(
                    dist, label, noncentered, dims=dims, **args
                )
                coef = coef[term.group_index]

                if predictor.ndim > 1:
                    for col in range(predictor.shape[1]):
                        self.mu += coef[:, col] * predictor[:, col]
                else:
                    self.mu += coef * predictor

        # Build response distribution
        with self.model:
            self.build_response(spec)

        self.spec = spec

    # pylint: disable=arguments-differ, inconsistent-return-statements
    def run(
        self, start=None, method="mcmc", init="auto", n_init=50000, omit_offsets=True, **kwargs
    ):
        """Run the PyMC3 MCMC sampler.

        Parameters
        ----------
        start: dict, or array of dict
            Starting parameter values to pass to sampler; see ``pm.sample()`` for details.
        method: str
            The method to use for fitting the model. By default, ``'mcmc'``, in which case the
            PyMC3 sampler will be used. Alternatively, ``'advi'``, in which case the model will be
            fitted using  automatic differentiation variational inference as implemented in PyMC3.
            Finally, ``'laplace'``, in which case a laplace approximation is used, ``'laplace'`` is
            not recommended other than for pedagogical use.
        init: str
            Initialization method (see PyMC3 sampler documentation). Currently, this is
            ``'jitter+adapt_diag'``, but this can change in the future.
        n_init: int
            Number of initialization iterations if ``init = 'advi'`` or '``init = 'nuts'``.
            Default is kind of in PyMC3 for the kinds of models we expect to see run with Bambi,
            so we lower it considerably.
        omit_offsets: bool
            Omits offset terms in the ``InferenceData`` object when the model includes
            group specific effects. Defaults to ``True``.

        Returns
        -------
        An ArviZ ``InferenceData`` instance.
        """
        model = self.model

        if method.lower() == "mcmc":
            draws = kwargs.pop("draws", 1000)
            with model:
                idata = pm.sample(
                    draws,
                    start=start,
                    init=init,
                    n_init=n_init,
                    return_inferencedata=True,
                    **kwargs,
                )

            if omit_offsets:
                offset_vars = [var for var in idata.posterior.var() if var.endswith("_offset")]
                idata.posterior = idata.posterior.drop_vars(offset_vars)

            for group in idata.groups():
                getattr(idata, group).attrs["modeling_interface"] = "bambi"
                getattr(idata, group).attrs["modeling_interface_version"] = version.__version__

            # Drop variables and dimensions associated with LKJ prior
            vars_to_drop = [var for var in idata.posterior.var() if var.startswith("_LKJ")]
            dims_to_drop = [dim for dim in idata.posterior.dims if dim.startswith("_LKJ")]

            idata.posterior = idata.posterior.drop_vars(vars_to_drop)
            idata.posterior = idata.posterior.drop_dims(dims_to_drop)

            # Reorder coords
            # pylint: disable=protected-access
            coords_original = list(self.spec._get_pymc_coords().keys())
            coords_group = [c for c in coords_original if c.endswith("_coord_group_factor")]
            for coord in coords_group:
                coords_original.remove(coord)
            coords_new = ["chain", "draw"] + coords_original + coords_group

            idata.posterior = idata.posterior.transpose(*coords_new)

            self.fit = True
            return idata

        elif method.lower() == "advi":
            with model:
                self.advi_params = pm.variational.ADVI(start, **kwargs)
            # this should return an InferenceData object (once arviz adds support for VI)
            return self.advi_params

        elif method.lower() == "laplace":
            return _laplace(model)

    def build_common_distribution(self, dist, label, **kwargs):
        """Build and return a PyMC3 Distribution for a common term."""
        # We are sure prior arguments aren't hyperpriors because it is already checked in the model
        distribution = self.get_distribution(dist)
        return distribution(label, **kwargs)

    def build_group_specific_distribution(self, dist, label, noncentered, **kwargs):
        """Build and return a PyMC3 Distribution for a group specific term."""
        dist = self.get_distribution(dist)
        if "dims" in kwargs:
            group_dim = [dim for dim in kwargs["dims"] if dim.endswith("_group_expr")]
            kwargs = {
                k: self.expand_prior_args(k, v, label, noncentered, dims=group_dim)
                for (k, v) in kwargs.items()
            }
        else:
            kwargs = {
                k: self.expand_prior_args(k, v, label, noncentered) for (k, v) in kwargs.items()
            }
        # Non-centered parameterization for hyperpriors
        if noncentered and has_hyperprior(kwargs):
            old_sigma = kwargs["sigma"]
            _offset = pm.Normal(label + "_offset", mu=0, sigma=1, dims=kwargs["dims"])
            return pm.Deterministic(label, _offset * old_sigma, dims=kwargs["dims"])
        return dist(label, **kwargs)

    def build_response(self, spec):
        """Build and return a response distribution."""
        data = spec.response.data.squeeze()
        name = spec.response.name
        link = spec.family.link
        if isinstance(link, str):
            link = self.links[link]

        likelihood = spec.family.likelihood
        dist = self.get_distribution(likelihood.name)
        kwargs = {likelihood.parent: link(self.mu), "observed": data}
        if likelihood.priors:
            kwargs.update(
                {
                    k: self.expand_prior_args(k, v, name, False)
                    for (k, v) in likelihood.priors.items()
                }
            )

        if spec.family.name == "gamma":
            # Gamma distribution is specified using mu and sigma, but we request prior for alpha.
            # We need to build sigma from mu and alpha.
            # kwargs["mu"] ** 2 / kwargs["alpha"] would also work
            beta = kwargs["alpha"] / kwargs["mu"]
            sigma = (kwargs["mu"] / beta) ** 0.5
            return dist(name, mu=kwargs["mu"], sigma=sigma, observed=kwargs["observed"])

        if spec.family.name == "beta":
            # Beta distribution is specified using alpha and beta, but we have mu and kappa.
            # alpha = mu * kappa and beta = (1 - mu) * kappa
            alpha = kwargs["mu"] * kwargs["kappa"]
            beta = (1 - kwargs["mu"]) * kwargs["kappa"]
            return dist(name, alpha=alpha, beta=beta, observed=kwargs["observed"])

        return dist(name, **kwargs)

    def get_distribution(self, dist):
        """Return a PyMC3 distribution."""
        if isinstance(dist, str):
            if hasattr(pm, dist):
                dist = getattr(pm, dist)
            elif dist in self.dists:
                dist = self.dists[dist]
            else:
                raise ValueError(
                    f"The Distribution {dist} was not found in PyMC3 or the PyMC3BackEnd."
                )
        return dist

    def expand_prior_args(self, key, value, label, noncentered, **kwargs):
        # Inspect all args in case we have hyperparameters.
        # kwargs are used to pass 'dims' for group specific terms.
        if isinstance(value, Prior):
            return self.build_group_specific_distribution(
                value.name, f"{label}_{key}", noncentered, **value.args, **kwargs
            )
        return value

def _laplace(model):
    """Fit a model using a Laplace approximation.

    Mainly for pedagogical use. ``mcmc`` and ``advi`` are better approximations.

    Parameters
    ----------
    model: PyMC3 model

    Returns
    -------
    Dictionary, the keys are the names of the variables and the values tuples of modes and standard
    deviations.
    """
    with model:
        varis = [v for v in model.unobserved_RVs if not pm.util.is_transformed_name(v.name)]
        maps = pm.find_MAP(start=model.test_point, vars=varis)
        hessian = pm.find_hessian(maps, vars=varis)
        if np.linalg.det(hessian) == 0:
            raise np.linalg.LinAlgError("Singular matrix. Use mcmc or advi method")
        stds = np.diag(np.linalg.inv(hessian) ** 0.5)
        maps = [v for (k, v) in maps.items() if not pm.util.is_transformed_name(k)]
        modes = [v.item() if v.size == 1 else v for v in maps]
        names = [v.name for v in varis]
        shapes = [np.atleast_1d(mode).shape for mode in modes]
        stds_reshaped = []
        idx0 = 0
        for shape in shapes:
            idx1 = idx0 + sum(shape)
            stds_reshaped.append(np.reshape(stds[idx0:idx1], shape))
            idx0 = idx1
    return dict(zip(names, zip(modes, stds_reshaped)))

def has_hyperprior(kwargs):
    """Determines if a Prior has an hyperprior"""
    return (
        "sigma" in kwargs
        and "observed" not in kwargs
        and isinstance(kwargs["sigma"], pm.model.TransformedRV)
    )

def add_lkj(terms, eta=1):
    """Add correlated prior for group-specific effects.

    This function receives a list of group-specific terms that share their `grouper`, constructs
    a multivariate Normal prior with LKJ prior on the correlation matrix, and adds the necessary
    variables to the model. It uses a non-centered parametrization.

    Parameters
    ----------
    terms: list
        A list of terms that share a common grouper (i.e. ``1|Group`` and ``Variable|Group`` in
        formula notation).
    eta: num
        The value for the eta parameter in the LKJ distribution.

    Parameters
    ----------
    mu:
        The contribution to the linear predictor of the roup-specific terms in ``terms``.
    """

    # Parameters
    # grouper: The name of the grouper.
    # rows: Sum of the number of columns in all the "Xi" matrices for a given grouper.
    #       Same than the order of L
    # cols: Number of groups in the grouper variable
    mu = 0
    grouper = terms[0].name.split("|")[1]
    rows = int(np.sum([term.predictor.shape[1] for term in terms]))
    cols = int(terms[0].grouper.shape[1])  # not the most beautiful, but works

    # Construct sigma
    # Horizontally stack the sigma values for all the hyperpriors
    sigma = np.hstack([term.prior.args["sigma"].args["sigma"] for term in terms])

    # Reconstruct the hyperprior for the standard deviations, using one variable
    sigma = pm.HalfNormal.dist(sigma=sigma, shape=rows)

    # Obtain Cholesky factor for the covariance
    lkj_decomp, corr, sigma = pm.LKJCholeskyCov(  # pylint: disable=unused-variable
        "_LKJCholeskyCov_" + grouper,
        n=rows,
        eta=eta,
        sd_dist=sigma,
        compute_corr=True,
        store_in_trace=False,
    )

    coefs_offset = pm.Normal("_LKJ_" + grouper + "_offset", mu=0, sigma=1, shape=(rows, cols))
    coefs = tt.dot(lkj_decomp, coefs_offset).T

    ## Separate group-specific terms
    start = 0
    for term in terms:
        label = term.name
        dims = list(term.pymc_coords.keys())
        predictor = term.predictor.squeeze()
        delta = term.predictor.shape[1]

        if delta == 1:
            idx = start
        else:
            idx = slice(start, start + delta)

        # Add prior for the parameter
        coef = pm.Deterministic(label, coefs[:, idx], dims=dims)
        coef = coef[term.group_index]

        # Add standard deviation of the hyperprior distribution
        group_dim = [dim for dim in dims if dim.endswith("_group_expr")]
        pm.Deterministic(label + "_sigma", sigma[idx], dims=group_dim)

        # Account for the contribution of the term to the linear predictor
        if predictor.ndim > 1:
            for col in range(predictor.shape[1]):
                mu += coef[:, col] * predictor[:, col]
        else:
            mu += coef * predictor
        start += delta

    # TO DO: Add correlations
    return mu

max_stars_repo_name: vishalbelsare/bambi

max_issues_repo_name: vishalbelsare/bambi

max_forks_repo_name: vishalbelsare/bambi

reason: import numpy,import theano,import pymc3


text:
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""Plot energy dispersion example.
"""
import numpy as np
import matplotlib.pyplot as plt
#from astropy.units import Quantity
from gammapy import irf

ebounds = np.logspace(-1, 2, 100)
pdf_matrix = irf.gauss_energy_dispersion_matrix(ebounds, sigma=0.2)
energy_dispersion = irf.EnergyDispersion(pdf_matrix, ebounds)

energy_dispersion.plot(type='matrix')
plt.show()

max_stars_repo_name: zblz/gammapy

max_issues_repo_name: zblz/gammapy

max_forks_repo_name: zblz/gammapy

reason: import numpy,from astropy


text:
import os
import h5py
import numpy as np

def get_data_files(files_list):
    with open(files_list) as f:
        data_files = f.read().splitlines()

    dir_path = os.path.dirname(files_list)
    data_files = [os.path.join(dir_path, file) for file in data_files]

    return data_files

def load_h5(filename):
    f = h5py.File(filename, 'r')
    src = f['source'][:]
    temp = f['template'][:]
    t_matrix = f['transform_matrix'][:]

    return src, temp, t_matrix

def shuffle_data(src, temp, t_matrix):
    idx = np.arange(len(src))
    np.random.shuffle(idx)

    return src[idx, ...], temp[idx, ...], t_matrix[idx, ...], idx

max_stars_repo_name: katka-juhasova/DP

max_issues_repo_name: katka-juhasova/DP

max_forks_repo_name: katka-juhasova/DP


text:
'''
Created on Jan 7, 2014

@author: jbq
'''

import numpy
from channel import Channel
from dsf import Dsf
from logger import tr

class ChannelGroup(object):
  '''
  This class implements a group of channels
  representing a group of dynamic structure factors
  '''

  def __init__(self):
    '''
    Attributes:
      fseries: list of external parameter values
      channels: series of channels, one for each point of the dynamical domain.
    '''
    self.fseries = None
    self.channels = None
    self.shape = None

  def Nchannels(self):
    if self.shape is None:
      return 0
    return numpy.prod(self.shape)
  nchannels = property( fget = Nchannels )

  def InitFromDsfGroup(self, dsfg ):
    ''' Load a group of dynamic structure factors into a channel group '''
    self.fseries = dsfg.fseries
    self.shape = dsfg.shape
    # Instantiate the channels
    self.channels = numpy.empty( self.nchannels, dtype = object )
    # fill the channels.
    for i in range( self.nchannels ):
      self.channels[i] = Channel()
      self.channels[i].SetSignalSeries( dsfg.ExtractSignalSeries(i) )
      self.channels[i].SetErrorSeries( dsfg.ExtractErrorSeries(i) )

  def InitializeInterpolator(self, running_regr_type = 'linear', windowlength=3):
    ''' Create the spline interpolator for each channel '''
    def initInterp(channel):
      channel.InitializeInterpolator( self.fseries, running_regr_type = running_regr_type, windowlength=windowlength)
      return channel
    vinitInterp = numpy.vectorize( initInterp ) # faster than the classic "for" loop
    self.channels[:] = vinitInterp( self.channels )

  def __getitem__(self,index):
    ''' Return channel at appropriate index '''
    return self.channels[index]

  def __call__(self,fvalue):
    ''' Return a dsf object invoking the interpolators for the channels '''
    signalseries=[]
    errorseries=[]
    for ichannel in range(self.nchannels):
      signal,error = self[ichannel](fvalue)
      signalseries.append(signal)
      errorseries.append(error)
    dsf = Dsf()
    dsf.SetIntensities(numpy.array(signalseries).reshape(self.shape))
    dsf.SetErrors(numpy.array(errorseries).reshape(self.shape))
    dsf.SetFvalue(fvalue)
    return dsf

max_stars_repo_name: camm-sns/dsfinterp

max_issues_repo_name: camm-sns/dsfinterp

max_forks_repo_name: camm-sns/dsfinterp


text:
from __future__ import absolute_import, division, print_function

import base64
import imageio
import imageio_ffmpeg
import IPython
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import PIL.Image
#import pyvirtualdisplay
import datetime

import tensorflow as tf

from tf_agents.agents.dqn import dqn_agent
from tf_agents.environments import suite_gym
from tf_agents.environments import tf_py_environment
from tf_agents.eval import metric_utils
from tf_agents.metrics import tf_metrics
from tf_agents.networks import sequential
from tf_agents.policies import random_tf_policy
from tf_agents.replay_buffers import tf_uniform_replay_buffer
from tf_agents.trajectories import trajectory
from tf_agents.specs import tensor_spec
from tf_agents.utils import common

print(tf.version.VERSION)
print()
print("----- Checking GPU devices -----")
print()
print(tf.config.list_physical_devices('GPU'))

#num_iterations = 20000 # @param {type:"integer"}
num_iterations = 1000 # @param {type:"integer"}

initial_collect_steps = 100  # @param {type:"integer"}
collect_steps_per_iteration = 1  # @param {type:"integer"}
replay_buffer_max_length = 100000  # @param {type:"integer"}

batch_size = 64  # @param {type:"integer"}
learning_rate = 1e-3  # @param {type:"number"}
log_interval = 200  # @param {type:"integer"}

num_eval_episodes = 10  # @param {type:"integer"}
eval_interval = 1000  # @param {type:"integer"}

env_name = 'CartPole-v0'
env = suite_gym.load(env_name)

#@test {"skip": true}
env.reset()
#PIL.Image.fromarray(env.render())

print('Observation Spec:')
print(env.time_step_spec().observation)

print('Reward Spec:')
print(env.time_step_spec().reward)

print('Action Spec:')
print(env.action_spec())

time_step = env.reset()
print('Time step:')
print(time_step)

action = np.array(1, dtype=np.int32)

next_time_step = env.step(action)
print('Next time step:')
print(next_time_step)

train_py_env = suite_gym.load(env_name)
eval_py_env = suite_gym.load(env_name)

train_env = tf_py_environment.TFPyEnvironment(train_py_env)
eval_env = tf_py_environment.TFPyEnvironment(eval_py_env)

fc_layer_params = (100, 50)
action_tensor_spec = tensor_spec.from_spec(env.action_spec())
num_actions = action_tensor_spec.maximum - action_tensor_spec.minimum + 1

# Define a helper function to create Dense layers configured with the right
# activation and kernel initializer.
def dense_layer(num_units):
  return tf.keras.layers.Dense(
      num_units,
      activation=tf.keras.activations.relu,
      kernel_initializer=tf.keras.initializers.VarianceScaling(
          scale=2.0, mode='fan_in', distribution='truncated_normal'))

# QNetwork consists of a sequence of Dense layers followed by a dense layer
# with `num_actions` units to generate one q_value per available action as
# it's output.
dense_layers = [dense_layer(num_units) for num_units in fc_layer_params]
q_values_layer = tf.keras.layers.Dense(
    num_actions,
    activation=None,
    kernel_initializer=tf.keras.initializers.RandomUniform(
        minval=-0.03, maxval=0.03),
    bias_initializer=tf.keras.initializers.Constant(-0.2))
q_net = sequential.Sequential(dense_layers + [q_values_layer])

optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)

train_step_counter = tf.Variable(0)

agent = dqn_agent.DqnAgent(
    train_env.time_step_spec(),
    train_env.action_spec(),
    q_network=q_net,
    optimizer=optimizer,
    td_errors_loss_fn=common.element_wise_squared_loss,
    train_step_counter=train_step_counter)

agent.initialize()

eval_policy = agent.policy
collect_policy = agent.collect_policy

random_policy = random_tf_policy.RandomTFPolicy(train_env.time_step_spec(),
                                                train_env.action_spec())

example_environment = tf_py_environment.TFPyEnvironment(
    suite_gym.load('CartPole-v0'))

time_step = example_environment.reset()

random_policy.action(time_step)

#@test {"skip": true}
def compute_avg_return(environment, policy, num_episodes=10):

  total_return = 0.0
  for _ in range(num_episodes):

    time_step = environment.reset()
    episode_return = 0.0

    while not time_step.is_last():
      action_step = policy.action(time_step)
      time_step = environment.step(action_step.action)
      episode_return += time_step.reward
    total_return += episode_return

  avg_return = total_return / num_episodes
  return avg_return.numpy()[0]

# See also the metrics module for standard implementations of different metrics.
# https://github.com/tensorflow/agents/tree/master/tf_agents/metrics

print()
print("----- Computing Average Return on Randon Policy -----")
print()

avg_return=compute_avg_return(eval_env, random_policy, num_eval_episodes)
print("- Average return is "+str(avg_return))

print()
print("----- Setting up replay buffer -----")
print()

replay_buffer = tf_uniform_replay_buffer.TFUniformReplayBuffer(
    data_spec=agent.collect_data_spec,
    batch_size=train_env.batch_size,
    max_length=replay_buffer_max_length)

print("- data spec: ")
print(agent.collect_data_spec)
print("- data spec fields: ")
print(agent.collect_data_spec._fields)

print()
print("----- Collection data from random policy and recording in replay buffer -----")
print()

#@test {"skip": true}
def collect_step(environment, policy, buffer):
  time_step = environment.current_time_step()
  action_step = policy.action(time_step)
  next_time_step = environment.step(action_step.action)
  traj = trajectory.from_transition(time_step, action_step, next_time_step)

  # Add trajectory to the replay buffer
  buffer.add_batch(traj)

def collect_data(env, policy, buffer, steps):
  for _ in range(steps):
    collect_step(env, policy, buffer)

collect_data(train_env, random_policy, replay_buffer, initial_collect_steps)

# This loop is so common in RL, that we provide standard implementations.
# For more details see tutorial 4 or the drivers module.
# https://github.com/tensorflow/agents/blob/master/docs/tutorials/4_drivers_tutorial.ipynb
# https://www.tensorflow.org/agents/api_docs/python/tf_agents/drivers

# For the curious:
# Uncomment to peel one of these off and inspect it.
# iter(replay_buffer.as_dataset()).next()

print()
print("----- Creating data pipeline from replay buffer to agent -----")
print()

# Dataset generates trajectories with shape [Bx2x...]
dataset = replay_buffer.as_dataset(
    num_parallel_calls=3,
    sample_batch_size=batch_size,
    num_steps=2).prefetch(3)

print("- dataset : "+str(dataset))
dataset

iterator = iter(dataset)
print("- iterator : "+str(iterator))

# For the curious:
# Uncomment to see what the dataset iterator is feeding to the agent.
# Compare this representation of replay data
# to the collection of individual trajectories shown earlier.

# iterator.next()

print()
print("----- Training the agent -----")
print()

#@test {"skip": true}
#try:
#  %%time
#except:
#  pass

# (Optional) Optimize by wrapping some of the code in a graph using TF function.
agent.train = common.function(agent.train)

# Reset the train step
agent.train_step_counter.assign(0)

# Evaluate the agent's policy once before training.
avg_return = compute_avg_return(eval_env, agent.policy, num_eval_episodes)
returns = [avg_return]

for _ in range(num_iterations):

  # Collect a few steps using collect_policy and save to the replay buffer.
  collect_data(train_env, agent.collect_policy, replay_buffer, collect_steps_per_iteration)

  # Sample a batch of data from the buffer and update the agent's network.
  experience, unused_info = next(iterator)
  train_loss = agent.train(experience).loss

  step = agent.train_step_counter.numpy()

  if step % log_interval == 0:
    print('step = {0}: loss = {1}'.format(step, train_loss))

  if step % eval_interval == 0:
    avg_return = compute_avg_return(eval_env, agent.policy, num_eval_episodes)
    print('step = {0}: Average Return = {1}'.format(step, avg_return))
    returns.append(avg_return)

print()
print("----- plot analytics -----")
print()

#@test {"skip": true}

iterations = range(0, num_iterations + 1, eval_interval)
plt.plot(iterations, returns)
plt.ylabel('Average Return')
plt.xlabel('Iterations')
plt.ylim(top=250)
plt.savefig("GRAPHS\\cartploe_avg_returns.pdf", facecolor='w', edgecolor='b')

print()
print("----- Saving trained model -----")
print()

saved_models_path="SAVED_agents\\RL_cartpole_agents\\agent_RL_cartpole_v0"
tf.saved_model.save(agent, saved_models_path)

print()
print("----- Generate video of agent performing -----")
print()

def embed_mp4(filename):
  """Embeds an mp4 file in the notebook."""
  video = open(filename,'rb').read()
  b64 = base64.b64encode(video)
  tag = '''
  <video width="640" height="480" controls>
    <source src="data:video/mp4;base64,{0}" type="video/mp4">
  Your browser does not support the video tag.
  </video>'''.format(b64.decode())

  return IPython.display.HTML(tag)

def create_policy_eval_video(policy, filename, num_episodes=5, fps=30):
  filename = filename + ".mp4"
  with imageio.get_writer(filename, fps=fps) as video:
    for _ in range(num_episodes):
      time_step = eval_env.reset()
      video.append_data(eval_py_env.render())
      while not time_step.is_last():
        action_step = policy.action(time_step)
        time_step = eval_env.step(action_step.action)
        video.append_data(eval_py_env.render())
  return embed_mp4(filename)

print("vid nb 1")
create_policy_eval_video(agent.policy, "GRAPHS\\trained-agent")
print("vid nb 2")
create_policy_eval_video(random_policy, "GRAPHS\\random-agent")

print()
print("----- Done -----")
print()


text:
"""JaxFit

Some interesting text
"""
from jax.config import config as _config

from ._version import version as __version__

# TODO: fine-grain control
# Really the only place float32 falls apart is poisson logpmf for very large N
_config.update("jax_enable_x64", True)

__all__ = ["__version__"]

max_stars_repo_name: nsmith-/jaxfit

max_issues_repo_name: nsmith-/jaxfit

max_forks_repo_name: nsmith-/jaxfit

reason: from jax


text:
import random
import numpy as np
from functools import reduce
from utils import calc_iou_fast

def calc_gt_rpn(img_data, config):
    """ calc ground truth labels for alle possible anchors
    Args:
        config
        img_data
    Returns:
        y_class: array which marks objects for anchors
            shape = (1, feature_height, feature_width, num_anchors, 2)
        y_regr: normalized regrission scores for positive anchors
            shape = (1, feature_height, feature_width, num_anchors, 4)
    """

    downscale = config.base_network_downscale
    num_anchors = config.num_anchors
    anchor_sizes = config.anchor_box_scales
    anchor_ratios = config.anchor_box_ratios

    # to calculate offset value
    num_bboxes = len(img_data['bboxes'])

    # calculate the output map size based on the network architecture
    feature_width, feature_height = img_data['width'] // downscale, img_data['height'] // downscale

    # calculated all anchor coordinates
    anchor_cord = np.zeros((4, feature_height, feature_width, num_anchors), dtype='float')

    curr_layer = -1
    for a_size in anchor_sizes:
        for a_ratio in anchor_ratios:
            curr_layer += 1

            anchor_w = a_size * a_ratio[0]
            anchor_h = a_size * a_ratio[1]

            # For every point in x, there are all the y points and vice versa
            X, Y = np.meshgrid(np.arange(feature_width), np.arange(feature_height))
            anchor_cord[0, :, :, curr_layer] = X + .5
            anchor_cord[1, :, :, curr_layer] = Y + .5
            anchor_cord[0, :, :, curr_layer] *= downscale
            anchor_cord[1, :, :, curr_layer] *= downscale
            anchor_cord[0, :, :, curr_layer] -= anchor_w / 2
            anchor_cord[1, :, :, curr_layer] -= anchor_h / 2
            anchor_cord[2, :, :, curr_layer] = anchor_w
            anchor_cord[3, :, :, curr_layer] = anchor_h

    # set unallowed anchors to 0
    ai, bi = anchor_cord[0] < 0, anchor_cord[1] < 0
    ci = anchor_cord[0] + anchor_cord[2] > img_data['width']
    di = anchor_cord[1] + anchor_cord[3] > img_data['height']
    idx = np.where(reduce(np.logical_or, (ai, bi, ci, di)))
    anchor_cord[:, idx[0], idx[1], idx[2]] = 1e-5

    anchor_center = anchor_cord[:2] + anchor_cord[2:] / 2

    # initialise empty output objectives
    y_class = np.zeros((feature_height, feature_width, num_anchors, 2), dtype='uint8')
    y_regr = np.zeros((feature_height, feature_width, num_anchors, 4))

    # Initialize with 'negative'
    y_class[:, :, :] = [1, 0]

    best_iou_for_loc = np.zeros((feature_height, feature_width, num_anchors), dtype='float')

    for bbox_num in range(num_bboxes):
        # get IOU of the current GT box and the current anchor box
        bbox = img_data['bboxes'][bbox_num]
        a = (bbox['x'], bbox['y'], bbox['w'], bbox['h'])
        ious = calc_iou_fast(a, anchor_cord)

        x_center = bbox['x'] + .5 * bbox['w']
        y_center = bbox['y'] + .5 * bbox['h']
        # normalisierte abweichungen/ziele berechnen
        tx = (x_center - anchor_center[0]) / (anchor_cord[2])
        ty = (y_center - anchor_center[1]) / (anchor_cord[3])
        tw = np.log(bbox['w'] / anchor_cord[2])
        th = np.log(bbox['h'] / anchor_cord[3])
        loc_regr = np.stack((tx, ty, tw, th)).transpose((1, 2, 3, 0))

        # set neutral, where not already positive and iou < rpn_max_overlap
        neutral = np.logical_and(config.rpn_max_overlap > ious, ious >= config.rpn_min_overlap)
        negative = (y_class[:, :, :, 1] == 0)
        idx = np.where(np.logical_and(neutral, negative))
        y_class[idx] = [0, 0]

        idx = np.logical_and(ious >= config.rpn_max_overlap, ious > best_iou_for_loc)
        idx = np.where(idx)
        y_class[idx] = [1, 1]
        y_regr[idx] = loc_regr[idx]
        best_iou_for_loc[idx] = ious[idx]

        if idx[0].shape[0] == 0 and np.max(ious) > 0.:
            best = np.where(ious == np.max(ious))
            y_class[best] = [1, 1]
            y_regr[best] = loc_regr[best]
            # take this box for sure
            best_iou_for_loc[best] = 1.1

    pos_locs = np.where(np.logical_and(y_class[:, :, :, 0] == 1, y_class[:, :, :, 1] == 1))
    neg_locs = np.where(np.logical_and(y_class[:, :, :, 0] == 1, y_class[:, :, :, 1] == 0))
    num_pos = len(pos_locs[0])
    num_neg = len(neg_locs[0])

    # one issue is that the RPN has many more negative than positive regions,
    # so we turn off some of the negative regions
    num_regions = config.max_num_regions

    if num_pos > num_regions / 2:
        val_locs = random.sample(range(num_pos), num_pos - num_regions // 2)
        y_class[pos_locs[0][val_locs], pos_locs[1][val_locs], pos_locs[2][val_locs]] = [0, 0]
        num_pos = num_regions // 2

    # only so many negatives as positives
    if num_neg + num_pos > num_regions:
        val_locs = random.sample(range(num_neg), num_neg - num_pos)
        y_class[neg_locs[0][val_locs], neg_locs[1][val_locs], neg_locs[2][val_locs]] = [0, 0]

    # scale regression values
    y_regr *= config.std_scaling
    # set regression to -1, when there isn't a object
    y_regr[np.where(y_class[:, :, :, 1] == 0)] = -100.

    # adapt shapes to match RPN output
    y_class = np.concatenate((y_class[:, :, :, 0], y_class[:, :, :, 1]), axis=2)
    y_regr = np.reshape(y_regr, y_regr.shape[:-2] + (-1,))
    y_class = np.expand_dims(y_class, axis=0)
    y_regr = np.expand_dims(y_regr, axis=0)

    return y_class, y_regr, num_pos


text:
"""
User Interface for Focus Monitor
"""
import glob
import string, time, datetime
from math import *
from matplotlib import dates
from matplotlib.dates import MinuteLocator, DateFormatter
from Tkinter import *
import tkMessageBox
import numpy as N
import pylab as P

def modelled(camera, date, startTime, stopTime):
    """
    Date in form of a string: month day  year. Times as 15:33 - 24 hour clock
    """
    print 'Modelled', camera, date, startTime, stopTime
    thermal = "/Users/niemi/Desktop/temp/hst/OTA/thermal/"

    # Focus model offsets
    camConst = {'PC': 261.1, 'HRC': 261.0, 'WFC1': 259.7, 'WFC2': 260.35}
    secMove = {'2004.12.22': 4.16, '2006.07.31': 5.34}

    # Define data lists
    julian = []
    temp1 = []
    temp2 = []
    temp3 = []
    temp4 = []
    temp5 = []
    temp6 = []
    hours = []
    focusDate = time.strptime(date, '%m/%d/%Y')
    timeAxis = []
    year = focusDate[0]
    month = focusDate[1]
    day = focusDate[2]

    # Get date-dependent focus adjustment
    focusShift = 0.0
    dateStamp = '%4d.%02d.%02d' % (year, month, day)
    for k in secMove.keys():
        if dateStamp > k:
            focusShift = focusShift + secMove[k]
    print 'Secondary mirror move ', focusShift

    dayOfYear = focusDate[7]
    dayString = "%03d" % dayOfYear
    yearString = str(year)
    start = string.split(startTime, ':')
    stop = string.split(stopTime, ':')
    startHour = int(start[0])
    startMinute = int(start[1])
    stopHour = int(stop[0])
    stopMinute = int(stop[1])
    jday = toJulian(year, month, day)
    jstart = jday + (startHour + startMinute / 60.0) / 24.0 - 40.0 / (60.0 * 24.0)  # 40 minute backtrack
    jstop = jday + (stopHour + stopMinute / 60.0) / 24.0
    fileName = 'thermalData' + yearString + '.dat'
    #if not(os.access(thermal + fileName, os.F_OK)): # then check Chris Long's file
    f = open(thermal + fileName, 'r')
    while f:
        line = f.readline()
        if line == '': break
        columns = string.split(line)
        timeStamp = columns[0]
        jul = float(columns[1])

        if jstart <= jul <= jstop:
            julian.append(jul)
            tup = fromJulian(jul)
            hr = tup[3] + (tup[4] + tup[5] / 60.0) / 60.0 # Extract hours
            hours.append(hr)
            tobj = datetime.datetime(tup[0], tup[1], tup[2], tup[3], tup[4], tup[5])
            timeAxis.append(tobj)
            num = dates.date2num(tobj)
            temp1.append(float(columns[2]))
            temp2.append(float(columns[3]))
            temp3.append(float(columns[4]))
            temp4.append(float(columns[5]))
            temp5.append(float(columns[6]))
            temp6.append(float(columns[7]))
            if day > dayOfYear: break
    f.close()

    if  len(temp1) == 0: # No temperature data in time range - Check Chris Long file
        longFile = glob.glob(thermal + '/breathing2009/BreathingData' + '*') # will produce a list
        if  len(longFile) > 0:
            longFile.sort()
            print 'Using ', longFile[-1] # Use latest version
            f = open(longFile[-1], 'r')
            while f:
                line = f.readline()
                if line == '': break
                columns = string.split(line)
                timeStamp = columns[0]
                jul = float(columns[1])

                if jstart <= jul <= jstop:
                    julian.append(jul)
                    tup = fromJulian(jul)
                    hr = tup[3] + (tup[4] + tup[5] / 60.0) / 60.0 # Extract hours
                    hours.append(hr)
                    tobj = datetime.datetime(tup[0], tup[1], tup[2], tup[3], tup[4], tup[5])
                    timeAxis.append(tobj)
                    num = dates.date2num(tobj)
                    temp1.append(float(columns[2]))
                    temp2.append(float(columns[3]))
                    temp3.append(float(columns[4]))
                    temp4.append(float(columns[5]))
                    temp5.append(float(columns[6]))
                    temp6.append(float(columns[7]))
                    if day > dayOfYear: break
            f.close()
        else:
            print 'Did not find Chris Long file'
            print 'No matching thermal data file'
            gui.statusBar.config(text='No matching thermal data file')
            return

    jtime = N.array(julian)
    aftLS = N.array(temp1)
    trussAxial = N.array(temp2)
    trussDiam = N.array(temp3)
    aftShroud = N.array(temp4)
    fwdShell = N.array(temp5)
    lightShield = N.array(temp6)
    #tBreath is value of light shield temp  minus average of previous eight values
    tBreath = lightShield.copy() # Make a real copy
    l = N.size(tBreath)
    if l < 10:
        print 'No temperature data'
        gui.statusBar.config(text='No temperature data')
        return

    r1 = range(8)
    tBreath[r1] = 0.0
    r2 = range(8, l)
    for r in r2:
        tBreath[r] = 0.7 * (lightShield[r] - sum(lightShield[r - 8:r]) / 8.0)

    focusModel = camConst[camera] + focusShift\
                 - 0.0052 * jtime + 0.48 * aftLS + 0.81 * trussAxial - 0.28 * aftShroud + 0.18 * fwdShell + 0.55 * tBreath
    print 'Average model%10.2f' % (N.mean(focusModel[8:]))
    # Just the Bely term
    Bely = 0.55 * tBreath
    bShift = N.mean(focusModel) - N.mean(Bely)
    Bely = Bely + bShift

    # Time independent Focus model with mean zero offset
    flatModel = camConst[camera] + focusShift\
                + 0.48 * aftLS + 0.81 * trussAxial - 0.28 * aftShroud + 0.18 * fwdShell + 0.55 * tBreath - 281.64
    print 'Flat model%10.2f' % (N.mean(flatModel[8:]))

    # Make up an output file

    op = open('plotdata' + dateStamp + '.txt', 'w')
    op.write('Julian Date     Date       Time    Model  Flat Model\n')
    for r in range(8, l):
        dataString1 = '%12.6f' % jtime[r]
        dataString2 = timeAxis[r].strftime(' %b %d %Y %H:%M:%S')
        dataString3 = '%8.4f %8.4f \n' %\
                      (focusModel[r], flatModel[r])
        op.write(dataString1 + dataString2 + dataString3)
        t = timeAxis[r]
        #print t.strftime('%b %d %Y %H:%M:%S')
    op.close()

    # Set up for plots
    P.figure(1)
    P.clf()
    P.subplot(211)
    P.ylabel('Degrees C')
    P.title('Temperatures ' + date)
    P.plot(dates.date2num(timeAxis), temp3)
    P.plot(dates.date2num(timeAxis), temp4)
    P.plot(dates.date2num(timeAxis), temp5)
    ax = P.gca()
    ax.xaxis.set_major_locator(MinuteLocator((0, 20, 40)))
    ax.xaxis.set_major_formatter((DateFormatter('%H:%M')))
    P.legend(('Truss Dia', 'Aft Shr', 'Fwd Sh'), loc='upper left')
    P.grid(True)
    P.subplot(212)
    P.plot(dates.date2num(timeAxis), temp1)
    P.plot(dates.date2num(timeAxis), temp6)
    P.legend(('Aft LS', 'Light Sh'))
    P.xlabel('Time')
    P.ylabel('Degrees C')
    ax2 = P.gca()
    ax2.xaxis.set_major_locator(MinuteLocator((0, 20, 40)))
    ax2.xaxis.set_major_formatter((DateFormatter('%H:%M')))
    P.grid(True)

    P.figure(2)
    #P.clf()
    P.plot(hours[8:], focusModel[8:], '-ro')
    #P.plot(hours[8:], Bely[8:], '-g+')
    P.title('Model ' + date)
    P.xlabel('Time')
    P.ylabel('microns')
    #print gui.display
    if gui.display == 'Comparison': P.legend(('Measured', 'Model'), loc='upper right')
    P.grid(True)
    P.draw()
    return

def measured(camera, testDate):
    """
    Extract focus measurements
    """
    print 'MEASURED'
    fDate = []
    fJulian = []
    fActual = []
    dateList = []

    measure = open(camera + 'FocusHistory.txt', 'r')
    focusData = measure.readlines()
    measure.close()
    for k in range(10): print focusData[k] #Temporary test
    count = len(focusData)
    print count, 'lines in History file'
    for l in range(2, count):
        pieces = string.split(focusData[l])
        if len(pieces) > 0:
            dataSet = pieces[0]
            if dataSet != '0':
                fDate.append(pieces[2])
                fJulian.append(pieces[3])
                fActual.append(pieces[4])
    entries = len(fDate)
    print entries, ' entries'

    plotJulian = []
    plotTime = []
    plotFocus = []
    for e in range(entries):
        if fDate[e] == testDate:
            t = float(fJulian[e])
            h = 24.0 * (t - floor(t))
            plotJulian.append(t)
            plotTime.append(h)
            plotFocus.append(float(fActual[e]))
    meanF = sum(plotFocus) / len(plotFocus)
    print 'Average measurement%10.2f' % (meanF)
    # Reformat date
    pDate = time.strptime(testDate, '%m/%d/%y')
    dateText = time.asctime(pDate)
    datePieces = string.split(dateText)
    plotDate = datePieces[1] + ' ' + datePieces[2] + ' ' + datePieces[4]

    P.figure(2)
    P.clf()
    P.plot(plotTime, plotFocus, '-bo')
    P.grid(True)
    P.title(camera + ' Focus measurement ' + plotDate)
    P.xlabel('Time')
    P.ylabel('Microns')
    P.draw()
    return (plotJulian[0], plotJulian[-1])

def toJulian(year, month, day):
    """
    Use time functions
    """
    dateString = str(year) + ' ' + str(month) + ' ' + str(day) + ' UTC'
    tup = time.strptime(dateString, '%Y %m %d %Z')
    sec = time.mktime(tup)
    days = (sec - time.timezone) / 86400.0  # Cancel time zone correction
    jday = days + 40587 # Julian date of Jan 1 1900
    return jday

def fromJulian(j):
    days = j - 40587 # From Jan 1 1900
    sec = days * 86400.0
    tup = time.gmtime(sec)
    return tup

def comparison(camera, date):
    monthName = ['blank', 'January', 'February', 'March', 'April', 'May', 'June',
                 'July', 'August', 'September', 'October', 'November', 'December']
    times = measured(camera, date) # times now contains start and end Julian times
    t0 = times[0]
    tp0 = fromJulian(t0)
    h0 = 24.0 * (t0 - int(t0))
    ih0 = int(h0)
    m0 = int(60 * (h0 - int(h0)))
    t1 = times[1]
    tp1 = fromJulian(t1)
    h1 = 24.0 * (t1 - int(t1))
    ih1 = int(h1)
    m1 = int(60 * (h1 - int(h1)))
    dateString = '%2d/%2d/%4d' % (tp1[1], tp1[2], tp1[0])
    dateString = string.lstrip(dateString) # clean up leading blanks
    startTime = '%02d:%02d' % (h0, m0)
    stopTime = '%02d:%02d' % (h1, m1)
    modelled(camera, dateString, startTime, stopTime)

class FocusMenu(object):
    def __init__(self):
        self.root = None

    def CreateForm(self):
        self.root = Tk()
        self.root.title('HST Focus Monitor')
        self.disp = StringVar()
        self.disp.set('Modelled')
        self.cam = StringVar()
        self.cam.set('PC')
        self.year = IntVar()

        Label(self.root, text='Display').grid(row=1, column=2)
        Label(self.root, text='Camera').grid(row=1, column=0, sticky=W)

        # Camera choice
        self.rc1 = Radiobutton(self.root, text='PC', variable=self.cam, value='PC')
        self.rc1.grid(row=3, column=0, sticky=W)
        self.rc2 = Radiobutton(self.root, text='HRC', variable=self.cam, value='HRC')
        self.rc2.grid(row=4, column=0, sticky=W)

        #Display choice
        self.rd1 = Radiobutton(self.root, text='Modelled', variable=self.disp, value='Modelled')
        self.rd1.grid(row=3, column=2, sticky=W)
        self.rd2 = Radiobutton(self.root, text='Measured', variable=self.disp, value='Measured')
        self.rd2.grid(row=4, column=2, sticky=W)
        self.rd3 = Radiobutton(self.root, text='Comparison', variable=self.disp, value='Comparison')
        self.rd3.grid(row=5, column=2, sticky=W)

        # Year of model or measurement
        Label(self.root, text='Year to display').grid(row=6, column=0, sticky=W)
        self.yearEntry = Entry(self.root)
        self.yearEntry.grid(row=6, column=1)

        self.b1 = Button(self.root, text='Proceed', command=self.GetDate)
        self.b1.grid(row=13, column=1)

        self.b2 = Button(self.root, text='Exit', command=self.Finish)
        self.b2.grid(row=13, column=2)

        self.statusBar = Label(self.root, text='', foreground='blue')
        self.statusBar.grid(row=11, sticky=S)

    def Show(self):
        self.root.mainloop()

    def GetDate(self):
    # Validate year and date input
        firstYear = 2003
        lastYear = time.gmtime()[0] # Get current year
        self.display = self.disp.get()
        self.camera = self.cam.get()
        self.year = self.yearEntry.get()
        goodyear = False
        try:
            iy = int(self.year)
            if iy < firstYear or iy > lastYear:
                self.statusBar.config(text='Year must be between 2003 and current year')
            else:
                goodyear = True
        except ValueError:
            self.statusBar.config(text='Bad format for Year')
            msg.grid(row=7, column=0)

        if goodyear: # Show selection for exact date
            if self.display == 'Measured' or self.display == 'Comparison':
                #print 'Display', self.display
                focusFile = self.camera + 'FocusHistory.txt'
                print 'Focus File', focusFile # Temporary
                measure = open(focusFile, 'r')
                focusData = measure.readlines()
                lf = len(focusData)
                measure.close()

                self.dateList = [] # Prepare to collect measurement dates from one year
                for l in range(1, lf): #Skip first  line
                    pieces = string.split(focusData[l])
                    if len(pieces) > 1 and pieces[0] != "0": # Skip blank and meaningless lines
                        [m, d, y] = string.split(pieces[2], '/')
                        if int(y) == iy % 100: # Match last two digits of year
                            dateStamp = pieces[2] # If this is different from previous date, add to list
                            if len(self.dateList) == 0 or self.dateList[-1] != dateStamp: self.dateList.append(
                                dateStamp)

                # Now make up listbox with dateList
                ly = len(self.dateList)
                if ly == 0:
                    nodata = 'No data for ' + self.camera + ' in ' + self.year
                    self.statusBar.config(text=nodata)
                    goodyear = False
                    return

                Label(self.root, text='Choose date of measurement').grid(row=7, column=0, sticky=NW)
                self.measureDates = Listbox(self.root, height=ly, selectmode=SINGLE)
                self.measureDates.grid(row=7, column=1)
                self.dateChoice = None # until chosen
                for i in range(ly): self.measureDates.insert(END, self.dateList[i])
                self.measureDates.bind("<Button1-ButtonRelease>", self.ChooseDate)
                self.b1.grid_forget()  # Remove PROCEED button
                self.rc1.config(state=DISABLED) # Turn off camera and display choices
                self.rc2.config(state=DISABLED)
                self.rd1.config(state=DISABLED)
                self.rd2.config(state=DISABLED)
                self.rd3.config(state=DISABLED)
                self.yearEntry.config(state=DISABLED)
                self.statusBar.config(text='')

                Button(self.root, text='Display', command=self.StartGraph).grid(row=8, column=1)

            elif self.display == 'Modelled': # Select arbitrary dates and times
                #print 'Display',self.display
                self.b1.grid_forget()  # Remove PROCEED button
                self.rc1.config(state=DISABLED) # Turn off camera and display choices
                self.rc2.config(state=DISABLED)
                self.rd1.config(state=DISABLED)
                self.rd2.config(state=DISABLED)
                self.rd3.config(state=DISABLED)
                self.yearEntry.config(state=DISABLED)
                self.statusBar.config(text='')

                Label(self.root, text='Date e.g. 11/23').grid(row=8, column=0, sticky=W)
                self.dayEntry = Entry(self.root)
                self.dayEntry.grid(row=8, column=1)
                Label(self.root, text='Start time in form 12:23').grid(row=9, column=0, sticky=W)
                self.time1Entry = Entry(self.root)
                self.time1Entry.grid(row=9, column=1)
                self.time2Entry = Entry(self.root)
                self.time2Entry.grid(row=10, column=1)
                Label(self.root, text='Use 24 hour clock').grid(row=9, column=2)
                Label(self.root, text='Stop time').grid(row=10, column=0, sticky=W)
                Button(self.root, text='Display', command=self.GetTimes).grid(row=11, column=1)

    def GetTimes(self):
        self.day = self.dayEntry.get()
        try:
            (m, d) = string.split(self.day, '/')
            if (1 <= int(m) <= 12) and (1 <= int(d) <= 31):
                goodday = True
            else:
                self.statusBar.config(text='Numerical error in date')
                goodday = False
        except ValueError:
            self.statusBar.config(text='Date to be in form mm/dd')
            goodday = False

        self.time1 = self.time1Entry.get()
        try:
            (hour1, minute1) = string.split(self.time1, ':')
            if (0 <= int(hour1) <= 23) and (0 <= int(minute1) <= 59):
                goodstart = True
            else:
                self.statusBar.config(text='Numerical error in start time')
                goodstart = False
        except ValueError:
            self.statusBar.config(text='Start time to be in form hh:mm')
            goodstart = False

        if goodstart: print 'h1m1', hour1, minute1

        self.time2 = self.time2Entry.get()
        try:
            (hour2, minute2) = string.split(self.time2, ':')
            if (0 <= int(hour2) <= 23) and (0 <= int(minute2) <= 59):
                goodstop = True
            else:
                self.statusBar.config(text='Numerical error in stop time')
                goodstop = True
        except ValueError:
            self.statusBar.config(text='Stop time to be in form hh:mm')
            goodstop = False

        if goodstop: print 'h2m2', hour2, minute2

        # Final check
        if goodstart and goodstop:
            t1 = int(hour1) + int(minute1) / 60.0
            t2 = int(hour2) + int(minute2) / 60.0
            if t1 > t2:
                goodstop = False
                self.statusBar.config(text='Start time is later than stop time').grid(row=11, column=0)
            else:
                goodstop = True

        if goodday and goodstart and goodstop:
            self.statusBar.config(text='Generating graphs')
            graph() # Start display

        return

    def ChooseDate(self, event):
        item = self.measureDates.curselection()
        self.dateChoice = self.dateList[int(item[0])]

    def StartGraph(self):
        if self.dateChoice: # Proceed only if date choice has been made
            self.statusBar.config(text='Generating graphs')
            graph()
            #self.statusBar.config(text = 'Finished')
        else: self.statusBar.config(text='Choose a date')

    def Finish(self):
        self.root.destroy()
        P.close('all')

def graph():
    monthName = ['blank', 'January', 'February', 'March', 'April', 'May', 'June',
                 'July', 'August', 'September', 'October', 'November', 'December']
    print 'Create display'
    print 'Camera', gui.camera
    print 'Display ', gui.display
    if gui.display == 'Modelled':
        #print gui.day, gui.year, gui.time1, gui.time2
        fulldate = "%5s/%4s" % (gui.day, gui.year)
        fulldate = string.lstrip(fulldate)
        modelled(gui.camera, fulldate, gui.time1, gui.time2)

    elif gui.display == 'Measured':
        #print 'Date ', gui.dateChoice
        times = measured(gui.camera, gui.dateChoice)

    elif gui.display == "Comparison":
        #print gui.camera, gui.dateChoice
        comparison(gui.camera, gui.dateChoice)

    print 'Finished'
    #gui.statusBar.config(text = 'Finished')
    return

if __name__ == '__main__':
    gui = FocusMenu()   # Creates an instance of FocusMenu
    gui.CreateForm()    # Builds the form
    gui.Show()          # Starts the loop

max_stars_repo_name: sniemi/SamPy

max_issues_repo_name: sniemi/SamPy

max_forks_repo_name: sniemi/SamPy


text:
from typing import Dict
from typing import List
from typing import Optional
from typing import Sequence
from typing import Union

import numpy as np
import pandas as pd

from evidently.pipeline.column_mapping import ColumnMapping

class DatasetUtilityColumns:
    def __init__(
            self,
            date: Optional[str],
            id_column: Optional[str],
            target: Optional[str],
            prediction: Optional[Union[str, Sequence[str]]],
    ) -> None:
        self.date = date
        self.id_column = id_column
        self.target = target
        self.prediction = prediction

    def as_dict(self) -> Dict[str, Union[Optional[str], Optional[Union[str, Sequence[str]]]]]:
        return {
            'date': self.date,
            'id': self.id_column,
            'target': self.target,
            'prediction': self.prediction,
        }

class DatasetColumns:
    def __init__(
            self,
            utility_columns: DatasetUtilityColumns,
            num_feature_names,
            cat_feature_names,
            target_names: Optional[List[str]],
    ) -> None:
        self.utility_columns = utility_columns
        self.num_feature_names = num_feature_names
        self.cat_feature_names = cat_feature_names
        self.target_names = target_names

    def as_dict(self) -> Dict[str, Union[Optional[List[str]], Dict]]:
        return {
            'utility_columns': self.utility_columns.as_dict(),
            'cat_feature_names': self.cat_feature_names,
            'num_feature_names': self.num_feature_names,
            'target_names': self.target_names,
        }

    def get_all_features_list(self, cat_before_num: bool = True) -> List[str]:
        """List all features names"""
        if cat_before_num:
            return self.cat_feature_names + self.num_feature_names

        else:
            return self.num_feature_names + self.cat_feature_names

    def get_features_len(self) -> int:
        """How mane feature do we have. It is useful for pagination in widgets."""
        return len(self.num_feature_names) + len(self.cat_feature_names)

def process_columns(dataset: pd.DataFrame, column_mapping: ColumnMapping):
    date_column = column_mapping.datetime if column_mapping.datetime in dataset else None
    id_column = column_mapping.id
    target_column = column_mapping.target if column_mapping.target in dataset else None
    prediction_column = column_mapping.prediction
    num_feature_names = column_mapping.numerical_features
    target_names = column_mapping.target_names
    utility_columns = [date_column, id_column, target_column]

    if isinstance(prediction_column, str):
        if prediction_column in dataset:
            prediction_column = prediction_column

        else:
            prediction_column = None

        utility_columns.append(prediction_column)

    elif prediction_column is None:
        pass

    else:
        prediction_column = dataset[prediction_column].columns.tolist()

        if prediction_column:
            utility_columns += prediction_column

    if num_feature_names is None:
        num_feature_names = list(set(dataset.select_dtypes([np.number]).columns) - set(utility_columns))

    else:
        num_feature_names = dataset[num_feature_names].select_dtypes([np.number]).columns.tolist()

    cat_feature_names = column_mapping.categorical_features

    if cat_feature_names is None:
        cat_feature_names = list(set(dataset.select_dtypes(exclude=[np.number]).columns) - set(utility_columns))

    else:
        cat_feature_names = dataset[cat_feature_names].columns.tolist()

    return DatasetColumns(
        DatasetUtilityColumns(date_column, id_column, target_column, prediction_column),
        num_feature_names,
        cat_feature_names,
        target_names,
    )

def calculate_confusion_by_classes(confusion_matrix: pd.DataFrame, class_names: List[str]) -> Dict[str, Dict[str, int]]:
    """Calculate metrics
        TP (true positive)
        TN (true negative)
        FP (false positive)
        FN (false negative)
    for each class from confusion matrix.

    Returns a dict like:
    {
        'class_1_name': {
            'tp': 1,
            'tn': 5,
            'fp': 0,
            'fn': 3,
        },
        ...
    }
    """
    true_positive = np.diag(confusion_matrix)
    false_positive = confusion_matrix.sum(axis=0) - np.diag(confusion_matrix)
    false_negative = confusion_matrix.sum(axis=1) - np.diag(confusion_matrix)
    true_negative = confusion_matrix.sum() - (false_positive + false_negative + true_positive)
    confusion_by_classes = {}

    for idx, class_name in enumerate(class_names):
        confusion_by_classes[str(class_name)] = {
            'tp': true_positive[idx],
            'tn': true_negative[idx],
            'fp': false_positive[idx],
            'fn': false_negative[idx],
        }

    return confusion_by_classes

max_stars_repo_name: burkovae/evidently

max_issues_repo_name: burkovae/evidently

max_forks_repo_name: burkovae/evidently


text:
from flask import Flask, render_template, request
import jsonify
import requests
import pickle
import numpy as np
import sklearn
import matplotlib
from sklearn.preprocessing import StandardScaler
app = Flask(__name__)
model = pickle.load(open('Customer_Churn_Prediction.pkl', 'rb'))
@app.route('/', methods=['GET'])
def Home():
    return render_template('index.html')

standard_to = StandardScaler()
@app.route('/predict',methods=['POST'])
def predict():
    if request.method == 'POST':
        CreditScore = int(request.form['CreditScore'])
        Age = int(request.form['Age'])
        Tenure = int(request.form['Tenure'])
        Balance = float(request.form['Balance'])
        NumOfProducts = int(request.form['NumOfProducts'])
        HasCrCard = int(request.form['HasCrCard'])
        IsActiveMember = int(request.form['IsActiveMember'])
        EstimatedSalary = float(request.form['EstimatedSalary'])
        Geography_Germany = request.form['Geography_Germany']
        if(Geography_Germany == 'Germany'):
            Geography_Germany = 1
            Geography_Spain= 0
            Geography_France = 0

        elif(Geography_Germany == 'Spain'):
            Geography_Germany = 0
            Geography_Spain= 1
            Geography_France = 0

        else:
            Geography_Germany = 0
            Geography_Spain= 0
            Geography_France = 1
        Gender_Male = request.form['Gender_Male']
        if(Gender_Male == 'Male'):
            Gender_Male = 1
            Gender_Female = 0
        else:
            Gender_Male = 0
            Gender_Female = 1
        prediction = model.predict([[CreditScore,Age,Tenure,Balance,NumOfProducts,HasCrCard,IsActiveMember,EstimatedSalary,Geography_Germany,Geography_Spain,Gender_Male]])
        if prediction==1:
             return render_template('index.html',prediction_text="The Customer will leave the bank")
        else:
             return render_template('index.html',prediction_text="The Customer will not leave the bank")

if __name__=="__main__":
    app.run(debug=True)


text:
import json
import numpy as np

import torch
from torch.utils.data import Dataset, DataLoader
import sentencepiece as spm
from .import FairseqDataset
from .fairseq_dataset import TAG_DICT
from .indexed_dataset import IndexedRawTextDataset
from .collaters import Seq2SeqCollater

class TaggedDataset(IndexedRawTextDataset):
    def __init__(self, path, dictionary, append_eos=True, reverse_order=False):
        self.src_tokens = []
        self.src_sizes = []
        self.tgt_tokens = []
        self.tgt_sizes = []
        self.src = []
        self.tgt = []
        self.sizes = []
        self.append_eos = append_eos
        self.reverse_order = reverse_order
        self.speakers = []
        self.ids = []
        self.dictionary = dictionary
        self.read_data(path)
        self.size = len(self.ids)

    def read_data(self, path):
        with open(path, 'r', encoding='utf-8') as f:
            chat_dict = json.load(f)

        for chat in chat_dict.values():
            for turn in chat:
                src = turn['source']
                self.src.append(src)
                src_tokens = torch.Tensor(self.dictionary.encode(src))
                self.src_tokens.append(src_tokens)
                self.src_sizes.append(len(src_tokens))

                tgt = turn['target']
                self.tgt.append(tgt)
                tgt_tokens = torch.Tensor(self.dictionary.encode(tgt)).long()
                self.tgt_tokens.append(tgt_tokens)
                self.tgt_sizes.append(len(tgt_tokens))

                self.speakers.append(turn['speaker'])
                self.ids.append(turn['utteranceID'])
        self.src_sizes = np.array(self.src_sizes)
        self.tgt_sizes = np.array(self.tgt_sizes)
        self.sizes = self.src_sizes

    def __getitem__(self, idx):
        src_speaker = torch.Tensor([self.dictionary.model.piece_to_id(TAG_DICT[self.speakers[idx]])])
        source, target =  torch.cat((torch.Tensor([self.dictionary.bos()]).long(), src_speaker.long(), self.src_tokens[idx].long(), torch.Tensor([self.dictionary.eos()]).long())) , torch.cat((torch.Tensor([self.dictionary.bos()]).long(), self.tgt_tokens[idx].long(), torch.Tensor([self.dictionary.eos()]).long()))
        return {"id": idx, "source": source, "target": target}

    def collater(self, samples):
        collate_fn = Seq2SeqCollater(pad_index=self.dictionary.model.piece_to_id("<pad>"), eos_index=self.dictionary.model.piece_to_id("</s>"))
        samples = collate_fn.collate(samples)
        return samples

if __name__ == "__main__":
    model = spm.SentencePieceProcessor(model_file="../../../data/wmtchat2020/spm.model")
    dataset = TwoToOneDataset("../../../data/wmtchat2020/valid.json", model)
    sample = dataset[5]
    print(dataset.src[5])
    print(spm.decode(sample["source"]))
    print(dataset.tgt[5])
    print(spm.decode(sample["target"]))

max_stars_repo_name: kayoyin/DialogueMT

max_issues_repo_name: kayoyin/DialogueMT

max_forks_repo_name: kayoyin/DialogueMT


text:
#!/usr/bin/env python3
import numpy as np
from momentum_env import MomentumEnv
from baselines.common.cmd_util import mujoco_arg_parser
from baselines import bench, logger

# import os
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import pydart2 as pydart

def train(env_id, num_timesteps, seed):
    from baselines.common import set_global_seeds
    from baselines.common.vec_env.vec_normalize import VecNormalize
    from baselines.ppo2 import ppo2
    from baselines.ppo2.policies import MlpPolicy
    import gym
    import tensorflow as tf
    from baselines.common.vec_env.dummy_vec_env import DummyVecEnv
    ncpu = 2
    config = tf.ConfigProto(allow_soft_placement=True,
                            intra_op_parallelism_threads=ncpu,
                            inter_op_parallelism_threads=ncpu)
    tf.Session(config=config).__enter__()

    def make_env():
        env = MomentumEnv()
        env.model.set_deep_ext_force(fm=0., fv=np.array([0., 0., 1.]), ts=5., te=5.1)
        env = bench.Monitor(env, logger.get_dir(), allow_early_resets=True)
        return env

    env = DummyVecEnv([make_env])
    env = VecNormalize(env)

    set_global_seeds(seed)
    policy = MlpPolicy
    model = ppo2.learn(policy=policy, env=env, nsteps=2048, nminibatches=32,
                       lam=0.95, gamma=0.99, noptepochs=10, log_interval=1,
                       ent_coef=0.0, vf_coef=0.5, max_grad_norm=0.5,
                       lr=3e-4,
                       cliprange=0.2, save_interval=128,
                       total_timesteps=num_timesteps)

    load_path = logger.get_dir() + '../log/checkpoints/04096'

    # model = ppo2.learn(policy=policy, env=env, nsteps=2048, nminibatches=32,
    #                    lam=0.95, gamma=0.99, noptepochs=10, log_interval=1,
    #                    ent_coef=0.0,
    #                    lr=3e-4,
    #                    cliprange=0.2, save_interval=512,
    #                    total_timesteps=num_timesteps, load_path=load_path)

    return model, env

def main():
    from time import strftime
    pydart.init()
    args = mujoco_arg_parser().parse_args()
    logger.configure(dir='./log'+strftime("%Y%m%d%H%M")+'/')
    model, env = train(args.env, num_timesteps=10000000, seed=args.seed)

    logger.log("Running trained model")
    obs = np.zeros((env.num_envs,) + env.observation_space.shape)
    obs[:] = env.reset()
    while True:
        actions = model.step(obs)
        res = env.step(actions)
        obs[:] = res[0]
        done = res[2]
        if done[0]:
            break

        # env.render()

if __name__ == '__main__':
    main()

max_stars_repo_name: hpgit/HumanFoot

max_issues_repo_name: hpgit/HumanFoot

max_forks_repo_name: hpgit/HumanFoot