import os, sys, json
import base64
from django.shortcuts import render, redirect
from django.http.response import JsonResponse
from django.views.decorators.csrf import csrf_exempt

# db
from accounts.models.user import User
from ShareShogi.models.note import Note
from ShareShogi.models.note_page import NotePage

# １つのノートの情報を取得

@csrf_exempt
def get_NoteInfo_request(request):

    payload = json.loads(request.body.decode("utf-8"))
    # payload = request.POST
    note_id = int(payload["note_id"])
    info = get_NoteInfo(note_id)

    return JsonResponse({"code" : 200, "result" : info})

def get_NoteInfo(note_id):

    record_Note = Note.objects.get(id=note_id)
    info = {
        "nickname" : record_Note.user.nickname,
        "id" : record_Note.id,
        "opening_sente" : record_Note.opening_sente,
        "opening_gote" : record_Note.opening_gote,
        "is_public" : record_Note.is_public,
        "pages" : [],
        "favo" : record_Note.favo,
        "hashtags" : ["三間飛車", "居飛車穴熊"]
    }

    queryset_NotePage = NotePage.objects.filter(note=record_Note)
    ordered_NotePage = sorted(list(queryset_NotePage), key=lambda x: x.order_in_parent)

    for record_NotePage in ordered_NotePage:
        info["pages"].append(
            {
                "NotePage_id" : record_NotePage.id,
                "image_url" : record_NotePage.image_url,
                "message" : record_NotePage.message
            }
        )

    return info

# ユーザーのノート一覧を取得

@csrf_exempt
def get_user_Note_request(request):

    payload = json.loads(request.body.decode("utf-8"))
    user_id = int(payload["user_id"])
    notes = get_user_Note(user_id)

    return JsonResponse({"code" : 200, "result" : notes})

def get_user_Note(user_id):

    record_User = User.objects.get(id=int(user_id))
    items = []

    for record_Note in Note.objects.filter(user=record_User)[::-1]:

        info = {
            "id" : record_Note.id,
            "title" : record_Note.title,
            "thumb" : record_Note.thumb_url,
            "opening_sente" : record_Note.opening_sente,
            "opening_gote" : record_Note.opening_gote,
            "is_public" : record_Note.is_public,
            "favo" : record_Note.favo,

            "hashtags" : ["三間飛車", "居飛車穴熊"]
        }
        items.append(info)

    return items

# ノート一覧の検索結果を取得

@csrf_exempt
def get_Note_request(request):

    '''
    params = {"is_public":True, "opening_sente":None, "opening_gote":None}
    {"params" : params}
    '''

    print("")
    print("====")

    # payload = request.POST
    payload = json.loads(request.body.decode("utf-8"))
    print(payload)
    params = payload["params"]

    items = get_Note(**params)

    resp = {
        "code" : 200,
        "result" : items
    }

    return JsonResponse(resp)

def get_Note(is_public=True, opening_sente=None, opening_gote=None):

    items = []

    # あとで実装
    if (opening_sente is not None) and (opening_gote is None):
        queryset_Note = Note.objects.filter(is_public=is_public)
    # あとで実装
    elif (opening_sente is None) and (opening_gote is not None):
        queryset_Note = Note.objects.filter(is_public=is_public)

    else:
        queryset_Note = Note.objects.filter(is_public=is_public)

    for record_Note in queryset_Note[::-1]:

        info = {
            "id" : record_Note.id,
            "title" : record_Note.title,
            "thumb" : record_Note.thumb_url,
            "opening_sente" : record_Note.opening_sente,
            "opening_gote" : record_Note.opening_gote,
            "is_public" : record_Note.is_public,
            "favo" : record_Note.favo,

            "nickname" : record_Note.user.nickname,
            "hashtags" : ["三間飛車", "居飛車穴熊"]
        }
        items.append(info)

    return items


import numpy as np
import trimesh
import pprint

data_home_directory = '../../Ops-robotics-rsimcon/content/raw/mesh' \
                      '/64907_Fanuc_430_Robot/'

the_model = 'armature_rigging_groups-binary-7.4-z-forward-y-up.fbx'

meshes = trimesh.load_meshes(data_home_directory + the_model)

for i in range(len(meshes)):
    mesh = meshes[i]

    # if there are multiple bodies in the mesh we can split the mesh by
    # connected components of face adjacency. If this example mesh
    # is a single watertight body we get a list of one mesh
    # mesh.split()

    pp = pprint.PrettyPrinter(indent = 2)

    pp.pprint({'mesh_number':  i+1
           ,'asset_dir': data_home_directory
           ,'file_name': the_model
           ,"n_vertices": len(mesh.vertices)
           ,"area": mesh.area
           ,"is_winding_consistent": mesh.is_winding_consistent
           ,"is_convex": mesh.is_convex
           ,"n_facets": len(mesh.facets)
           ,"bounding_box_extents": mesh.bounding_box_oriented.extents
           ,"extents": mesh.extents
           ,"sum_area_faces": np.sum(mesh.area_faces)
           ,"sum_area_facets": np.sum(mesh.facets_area)
           ,"watertight_q": mesh.is_watertight
           ,"euler_number": mesh.euler_number
           ,"volume_by_convex_hull_ratio": np.divide(mesh.volume,
                                                     mesh.convex_hull.volume)
           ,"center_of_mass": mesh.center_mass
           ,'centroid': mesh.centroid
           ,"geometric_moment_of_inertia": mesh.moment_inertia
           ,"number_of_facets": len(mesh.facets)
           ,'number_of_edges': len(mesh.edges)
           ,'number_of_unique_edges': len(mesh.edges_unique)
           ,'number_of_face_edges': len(mesh.edges_face)
           ,'number_of_sorted_edges': len(mesh.edges_sorted)
           ,'number_of_face_adjacency_edges': len(mesh.face_adjacency_edges)
           ,'number_of_unique_face_edges': len(mesh.faces_unique_edges)
           ,'number_of_faces': len(mesh.faces)
           ,'number_of_triangles': len(mesh.triangles)
           ,'principal_inertia_components': mesh.principal_inertia_components
           ,'principal_inertia_vectors': mesh.principal_inertia_vectors
           ,'volume': mesh.volume
            })

    for facet in mesh.facets:
        mesh.visual.face_colors[facet] = trimesh.visual.random_color()

    # mesh.export(data_home_directory +
    #             'armature_rigging_groups_submesh_%0.3d.dae' % (i+1),
    #             'collada')

    mesh.export(data_home_directory +
                'armature_rigging_groups_submesh_%0.3d.off' % (i+1),
                'off')

    mesh.show()


from medpy.io import load, save
import numpy as np
import nibabel as nib
import argparse

parser = argparse.ArgumentParser(description="Merge Mask with results file")
parser.add_argument('-mask_data', type=str, default='data/mask/')
parser.add_argument('-results_data', type=str, default='data/results/')
parser.add_argument('-save_path', type=str, default='data/merged_results/')
parser.add_argument('-label_data', type=str, default='data/segmentation/')
args = parser.parse_args()

def dice(result_array, label_array, empty_score=1.0):

    result_array = np.asarray(result_array).astype(np.bool)
    label_array = np.asarray(label_array).astype(np.bool)

    if result_array.shape != label_array.shape:
        raise ValueError("Shape mismatch! Results shape: " + result_array.shape + " Label shape: " + label_CT.shape)

    sum = result_array.sum() + label_array.sum()
    if sum == 0:
        return empty_score

    # Compute Dice coefficient
    intersection = np.logical_and(result_array, label_array)

    return 2. * intersection.sum() / (result_array.sum() + label_array.sum())

for idx in range(1):
    seg = nib.load(args.results_data + 'test-segmentation-' + str(idx) + '.nii').get_data()
    print("loading results: " + str(idx))
    mask = nib.load(args.mask_data + 'segmentation-' + str(idx) + '.nii').get_data()
    print("Loading mask: " + str(idx))

    mask_shape_array = np.zeros(mask.shape)
    print("mask shape: " + str(mask_shape_array.shape))

    seg_shape_array = np.zeros(mask.shape)
    print('results shape: ' + str(seg_shape_array.shape))

    seg_cords = np.where(seg==2)
    seg_shape_array[seg_cords] = 2

    mask_cords = np.where(mask==1)
    mask_shape_array[mask_cords] = 1

    merged_cords = np.logical_and(mask_shape_array,seg_shape_array)

    mask[merged_cords] = 2
    print('Saving Merged Results')
    save(mask, args.save_path + 'merged_results-' + str(idx) + '.nii')

    print('Loading segmentation file: ' + str(idx))
    label = nib.load(args.label_data + 'segmentation-' + str(idx) + '.nii').get_data()

    result_tumor = np.where(mask==2)
    label_tumor = np.where(label==2)

    tumor_dice = dice(result_tumor,label_tumor)

    results_kidney = np.where(mask==1) and np.where(mask==2)
    label_kidney = np.where(label==1) and np.where(label==2)

    kidney_dice = dice(results_kidney, label_kidney)

    print("Tumor Dice score: " + str(tumor_dice) + 'Kidney Dice score: ' + str(kidney_dice))


"""Draw maps using folium."""

from IPython.core.display import HTML
import json
import folium
import functools
import random

#########################
# Exposed Functionality #
#########################

def draw_map(obj=None, **kwargs):
    """Draw a map with center & zoom containing all points and regions that
    displays points as circles and regions as polygons.

    obj -- a coordinate for the center, a MapRegion, or a MapPoint
    see help(Map) for more parameters to pass to draw_map
    """
    return get_map(obj, **kwargs).to_html()

def get_map(obj=None, **kwargs):
    """Get a map object to manipulate before drawing it. Use to_html() to draw.

    obj -- a coordinate for the center, a MapRegion, or a MapPoint
    see help(Map) for more parameters to pass to draw_map
    """
    return Map(location=obj, **kwargs).map()

class MapData:
    """A geoJSON string, object, or file."""

    _data = None

    def __init__(self, path_or_json_or_string = None):
        """Loads geoJSON"""
        jsons = path = string = path_or_json_or_string
        if not string:
            return
        if isinstance(jsons, (dict, list)):
            self._data = jsons
        try:
            self._data = json.loads(string)
        except ValueError:
            pass
        try:
            self._data = json.loads(open(path, 'r').read())
        except FileNotFoundError:
            pass
        if not self._data:
            raise MapError('MapData accepts a valid geoJSON object'
            'geoJSON string, or path to a geoJSON file')
        self._process_data()

    def _process_data(self):
        """Processes the data, and makes each list coordinates accessible via
        the ID of the feature.
        """
        self._features = self._data['features']
        self._data = {}
        for i, feature in enumerate(self._features):
            key, val = feature.get('id', i), MapRegion(
                locations=feature['geometry']['coordinates'],
                geo_formatted=True)
            self._data[key] = val
            setattr(self, key, val)

    def to_dict(self):
        return self._data.copy()

    def to_list(self):
        return list(self._data.values())

##################
# Implementation #
##################

def _map_to_defaults(defaults, params, excludes=[], includes=[]):
    """Squishes together two dictionaries, with one containing defaults."""
    rval = defaults.copy()
    rval.update(params)
    return {
        k: v for k, v in rval.items()
        if k not in excludes
        and k in (includes or rval.keys())
    }

def _hook(f):
    """Decorator for obj method, checks for and invokes PREFIX_before and
    PREFIX_after a method is called
    """

    def call_hook(self, template, *args, **kwargs):
        fn = getattr(self, template % f.__name__, None)
        if callable(fn):
            fn(*args, **kwargs)

    @functools.wraps(f)
    def method(self, *args, **kwargs):
        call_hook(self, '_before_%s', *args, **kwargs)
        rval = f(self, *args, **kwargs)
        call_hook(self, '_after_%s', *args, **kwargs)
        return rval

    return method

class ProgrammerError(Exception):
    """Thrown when programmers make a mistake"""

    message = ''
    prefix = 'Please report this to your instructors'

    def get_message(self):
        return '%s: %s' % (self.prefix, self.message)

class MapError(Exception):
    """Thrown when users make a mistake"""
    pass

######################
# Folium Abstraction #
######################

class MapEntity:
    """A base utility for all entities related to map

    "Attributes" are arguments that will be passed to the object's associated
    Folium method (called, the "mapper")
    - get and set "attributes", using map[prop]
    - after initializing the object, all properties will either be filled with
     a specified default or be set to None, to accept Folium's default

    All other data are pieces of data that you may wish to associate with the
    object.
    - all other data get and set, using map.prop
    """

    _init = {}                                  # params for initial call
    _defaults = {}                              # defaults for attrs and data
    _attributes = None                          # only data passed to mapper
    _allowed_attributes = []                    # allowed attributes
    _mapper = None                              # default function to map obj
    _folium = None                              # access to the map object
    _allowed_collections = (list, set, tuple)   # for list of locations

    @_hook
    def __init__(self, **kwargs):
        """Saves attributes, with defaults"""
        self._init, self._attributes = kwargs, {}
        self._attributes = _map_to_defaults(
            self._defaults, kwargs, includes=self._allowed_attributes)
        self.set(**_map_to_defaults(self._defaults, kwargs))

    def __setitem__(self, k, v):
        self._attributes[k] = v

    def __getitem__(self, k):
        return self._attributes[k]

    def __delitem__(self, k):
        del self._attributes[k]

    def set(self, override=False, **kwargs):
        """Transform args into properties"""
        for k, v in kwargs.items():
            attr = getattr(self, k, None)
            if callable(attr) and not override:
                raise ProgrammerError('"%s" is an existing method' % k)
            setattr(self, k, v)
        return self

    def get(self, *args):
        """Get a set of variables - use pairs to set getattr default"""
        get = lambda val: getattr(self, val[0], val[1]) \
            if isinstance(val, tuple) else getattr(self, val)
        return (get(var) for var in args)

    @_hook
    def map(self, map=None):
        """Maps this object, with its own attributes"""
        if not callable(self._mapper):
            self._mapper = getattr(map, self._mapper)
        self._folium = self._mapper(**self._attributes)
        return self

class Map(MapEntity):
    """
    Represents a map, potentially with MapPoints and MapRegions

    location or center -- lat-long pair at the center of the map
    width -- pixel int or percentage string
    height -- pixel int or percentage string
    tiles -- map tileset, or "theme"
    API_key -- for Cloudmade or Mapbox tiles
    max_zoom -- maximum zoom, default 18
    zoom_start -- starting zoom level
    points -- a list of MapPoints
    regions -- a list of MapRegions
    *pass any other kwarg to save it with the object

    >>> map = Map().map()
    >>> map.to_html()
    <IPython.core.display.HTML object>
    """

    _mapper = folium.Map

    _allowed_attributes = [
        'location',
        'width',
        'height',
        'tiles',
        'API_key',
        'max_zoom',
        'zoom_start',
    ]

    _defaults = {
        'location': [45.5244, -122.6699],
        'tiles': 'Stamen Toner',
        'zoom_start': 17,
        'width': 960,
        'height': 500,
        'points': [],
        'regions': []
    }

    @_hook
    def to_html(self):
        """Outputs the HTML that IPython will display."""
        map = self._folium
        map._build_map()
        map_html = map.HTML.replace('"', '"')
        return HTML('<iframe srcdoc="%s" '
                    'style="width: %spx; height: %spx; '
                    'border: none"></iframe>'
                    % (map_html, map.width, map.height))

    def _before_to_html(self, *args, **kwargs):
        """Called before to_html; add points and regions before mapping."""
        for point in self.points:
            point.map(self._folium)

        for region in self.regions:
            region.map(self._folium)

        # uncomment the following with new Folium release
        # if 'zoom_start' not in self._init:
        #     self._autofit(self.bounds)

    def _before_map(self, *args, **kwargs):
        """Autozoom map where appropriate."""
        if self.location:
            self._dynamic_arg('location')

        self.bounds = self._autobounds()

        if not self.location:
            self._autocenter(self.bounds)

        # TODO(Alvin): remove with new Folium release
        if 'zoom_start' not in self._init:
            self._autofit(self.bounds)

    def _autobounds(self):
        """Simple calculation for bounds."""
        bounds = {}

        def check(prop, compare, extreme, val):
            opp = min if compare is max else max
            bounds.setdefault(prop, val)
            bounds[prop] = opp(compare(bounds[prop], val), extreme)

        def bound_check(coord):
            if not coord:
                return
            if isinstance(coord, self._allowed_collections):
                if isinstance(coord[0], (int, float)):
                    long, lat = coord
                    check('max_lat', max, 90, lat)
                    check('min_lat', min, -90, lat)
                    check('max_long', max, 180, long)
                    check('min_long', min, -180, long)
                else:
                    [bound_check(c) for c in coord]
            else:
                raise ProgrammerError('Ended up with %s.' % type(coord))

        bound_check([point.location for point in self.points])
        bound_check([region.to_polygon() for region in self.regions])
        return bounds

    def _autofit(self, bounds):
        """Automatically fit everything with the maximum zoom possible."""
        if not bounds:
            return

        # TODO(Alvin): uncomment with new Folium release
        # self._folium.fit_bounds(
        #     [bounds['min_long'], bounds['min_lat']],
        #     [bounds['max_long'], bounds['max_lat']]
        # )

        # remove the following with new Folium release
        # rough approximation, assuming max_zoom is 18
        import math
        try:
            factor = 1.2
            width = ((bounds['max_lat'] - bounds['min_lat']) or 2)
            height = ((bounds['max_long'] - bounds['min_long']) or 2)
            area, max_area = width*height, 180*360
            zoom = math.log(area/max_area)/-factor
            self['zoom_start'] = max(1, min(18, round(zoom)))
        except ValueError:
            raise MapError('Uh oh. Is the data formatted as long-lat '
            'pairs? (Check. If the smallest number is less than -90, '
            'those are long-lat pairs) If so, please add geo_formatted=True '
            'to create_map')

    def _autocenter(self, bounds):
        """Find the center."""
        if not bounds:
            return

        midpoint = lambda a, b: (a + b)/2
        self['location'] = (
            midpoint(bounds['min_lat'], bounds['max_lat']),
            midpoint(bounds['min_long'], bounds['max_long'])
        )

    def _dynamic_arg(self, key):
        """Check the first arg in check_map for other options."""
        val = getattr(self, key, None)
        if val:
            setattr(self, key, None)
            del self[key]
            if isinstance(val, MapRegion):
                self.regions.append(val)
            elif isinstance(val, MapPoint):
                self.points.append(val)
            else:
                setattr(self, key, val)
                self[key] = val

    @classmethod
    def reverse(cls, lst_or_coord_or_obj):
        """Reverse all coordinates in a (possibly nested) list.

        Effectively translates all EPSG-compliant coordinates into valid
        geoJSON coordinates.
        """
        lst = coord = obj = lst_or_coord_or_obj
        if not lst:
            return
        if isinstance(coord[0], (int, float)):
            return coord[::-1]
        if isinstance(lst, cls._allowed_collections):
            return [cls.reverse(item) for item in lst]
        return obj

class MapPoint(MapEntity):
    """
    A circle, wrapper for Folium's draw_circle. Draw by passing into draw_map.

    location -- lat-long pair at center of circle
    radius -- radius of circle on map
    popup -- text that pops up when circle is clicked
    line_color -- color of circle border
    fill_color -- color of circle within border
    fill_opacity -- opacity of circle fill
    """

    _mapper = 'circle_marker'

    _allowed_attributes = [
        'location',
        'radius',
        'popup',
        'line_color',
        'fill_color',
        'fill_opacity'
    ]

    _defaults = {
        'location': [],
        'radius': 10,
        'popup': '',
        'line_color': '#3186cc',
        'fill_color': '#3186cc',
        'fill_opacity': 0.6
    }

    def __init__(self, location_or_x, y=None, **kwargs):
        """
        Checks for coordinates passed in as two args, default is to pass as
        one arg
        """
        location = x = location_or_x
        if isinstance(x, (int, float)):
            location = (x, y)
        super().__init__(location=location, **kwargs)

    def __str__(self):
        return str(list(self.location))

    def _after___init__(self, *args, **kwargs):
        """
        Translates all lat-long pairs into long-lat pairs, from EPSG-compliant
        to GeoJSON format
        """
        if 'geo_formatted' not in kwargs \
                or not kwargs['geo_formatted']:
            self.location = Map.reverse(self.location)

class MapRegion(MapEntity):
    """
    A polygon, wrapper for Folium's geo_json method, with a few customizations.
    Draw by passing into draw_map.

    regions -- list of MapRegions (if present, locations and points not used)
    locations -- list of list of lat-long pairs for locations with multiple
                 polygons (if present, points are not used)
    points -- list of lat-long pairs for a normal polygon
    geo_formatted -- boolean, True if the data is formatted as long-lat pairs
    *pass any other kwarg to save it with the object

    Note: GeoJSON requires long-lat-alt triplets, or just long-lat pairs,
    although EPSG:4326 states that the coordinate order should be lat-long. For
    this reason, MapRegion has to flip incoming lat-long pairs to become
    long-lat pairs. By default, MapRegion accepts lat-long pairs, to match
    MapPoint's behavior.
    """

    # composite options

    CHILDREN = 'children'  # invoke map() for children
    GROUP = 'group'  # invoke to_json() for children
    ONE = 'one'  # invoke to_polygon() for children

    _mapper = 'geo_json'

    _allowed_attributes = [
        'geo_path',
        'geo_str',
        'data_out',
        'data',
        'columns',
        'key_on',
        'threshold_scale',
        'fill_color',
        'fill_opacity',
        'line_color',
        'line_weight',
        'line_opacity',
        'legend_name',
        'topojson',
        'reset'
    ]

    _defaults = {
        'composite': GROUP,
        'regions': [],
        'locations': [[]],
        'points': [],
        'fill_color': 'blue',
        'fill_opacity': 0.6,
        'line_color': 'black',
        'line_weight': 1,
        'line_opacity': 1
    }

    CACHE_DIR = 'cache'
    TEMP_FILE = None

    def _after___init__(self, *args, **kwargs):
        """
        Translates all lat-long pairs into long-lat pairs, from EPSG-compliant
        to GeoJSON format
        """
        if 'geo_formatted' not in kwargs or not kwargs['geo_formatted']:
            if 'locations' in kwargs:
                self.locations = Map.reverse(kwargs['locations'])
            if 'points' in kwargs:
                self.points = Map.reverse(kwargs['points'])

    def _before_map(self, *args, **kwargs):
        """Prepares for mapping by passing JSON representation"""
        self._validate()
        json_str = json.dumps(self._to_json()).replace(']]]}}', ']]]}}\n')

        # self.attributes['geo_str'] = json_str

        # TODO(Alvin): find soln to temporary workaround
        if self.locations[0] or self.points or self.regions:
            import os
            os.makedirs(self.CACHE_DIR, exist_ok=True)
            self.TEMP_FILE = os.path.join(
                self.CACHE_DIR, str(round(random.random()*1000))+'temp.json')
            open(self.TEMP_FILE, 'w').write(json_str)
            self._attributes['geo_path'] = self.TEMP_FILE

    def map(self, map=None):
        """Takes action, depending on composite

        CHILDREN -- invoke map() for each child
        GROUP -- invoke parent map() normally
        ONE -- invoke parent map() normally
        """
        composite = self.composite
        if composite in (MapRegion.GROUP, MapRegion.ONE):
            super().map(map)
        elif composite == MapRegion.CHILDREN:
            for region in self.regions:
                region.map(map)

    def _validate(self):
        """Checks for validity of data"""
        locations, points = self.get('locations', 'points')

        if not isinstance(points, self._allowed_collections):
            raise MapError('"Points" must be a %s' %
                ' or '.join(self._allowed_collections))

    @staticmethod
    def to_json(features, **kwargs):
        """Converts any features list to FeatureCollection JSON"""
        return _map_to_defaults({
            'type': 'FeatureCollection',
            'features': features
        }, kwargs)

    def _to_json(self):
        """Converts MapRegion to folium-ready geoJSON, depending on composite.

        CHILDREN -- none
        GROUP -- construct jsons with children
        ONE -- construct polygons with children
        """
        composite = self.composite
        if composite == MapRegion.GROUP:
            return MapRegion.to_json(
                self._to_feature_json())
        elif composite == MapRegion.ONE:
            return MapRegion.to_json(
                MapRegion.to_feature_json(
                    self._to_polygon_json()))
        return None

    @staticmethod
    def to_feature_json(feature, **kwargs):
        """Converts single feature into feature JSON"""
        return _map_to_defaults({
            'type': 'Feature',
            'properties': {'featurecla': 'Map'},
            'geometry': feature
        }, kwargs)

    def _to_feature_json(self):
        """Converts to list of feature JSONs"""
        return [MapRegion.to_feature_json(feat)
            for feat in self.to_feature()]

    def to_feature(self):
        """Converts to list of poylgon JSONs"""
        regions = self.regions

        if regions:
            rval = []
            for region in regions:
                rval += (region.to_feature() or [])
            return rval

        return [self._to_polygon_json()]

    @staticmethod
    def to_polygon_json(polygon, **kwargs):
        """Converts single polygon into polygon JSON"""
        return _map_to_defaults({
            'type': 'Polygon',
            'coordinates': polygon
        }, kwargs)

    def _to_polygon_json(self):
        """Converts to one polygon JSON"""
        return MapRegion.to_polygon_json(self.to_polygon())

    def to_polygon(self):
        """Converts to list of polygons, including that of descendants"""
        regions, locations, points = self.get('regions', 'locations', 'points')

        if regions:
            rval = []
            for region in regions:
                rval += region.to_polygon()
            return rval

        return self._to_locations()

    def _to_locations(self):
        """Converts to list of polygons"""
        locations, points = self.get('locations', 'points')
        if not locations[0]:
            return [points]
        return locations

    def __str__(self):
        return json.dumps(self._to_json())


import os.path
import ast
import prediction
# import linear_regression
import cnn_tweet

from flask import Flask, flash, Response, request, render_template

app = Flask(__name__)
app.secret_key = 'some_secret'

@app.route('/')
def hello_world():
    return 'Hello from Flask!'

def root_dir():  # pragma: no cover
    return os.path.abspath(os.path.dirname(__file__))

def get_file(filename):  # pragma: no cover
    try:
        src = os.path.join(root_dir(), filename)
        return open(src).read()
    except IOError as exc:
        return str(exc)

@app.route('/prediction', methods=['GET'])
def getPrediction():  # pragma: no cover
    content = get_file('prediction.html')
    return Response(content, mimetype="text/html")

@app.route('/model', methods=['POST'])
def getModel():  # pragma: no cover
    print("asdfasdfasdf")
    model = request.form.get("optradio")
    content = get_file('prediction.html')
    if model == "linearregression":
        content = get_file('linearregression.html')
    elif model == "rfregression":
        content = get_file("rfregression.html")
    elif model == "nnregression":
        content = get_file("nnregression.html")
    elif model == "logisticregression":
        content = get_file("logisticregression.html")
    elif model == "rfclassification":
        content = get_file("rfclassification.html")
    elif model == "nnclassification":
        content = get_file("nnclassification.html")

    return Response(content, mimetype="text/html")

@app.route('/regression', methods=['GET'])
def metrics():  # pragma: no cover
    content = get_file('result.html')
    return Response(content, mimetype="text/html")

@app.route('/logisticregression', methods=['POST'])
def logisticregression():  # pragma: no cover
    curr_loan_delinquency_status = 0
    loan_age = ast.literal_eval(request.form.get("loan_age"))
    remaining_months_to_legal_maturity = ast.literal_eval(request.form.get("remaining_months_to_legal_maturity"))
    curr_interest_rate = ast.literal_eval(request.form.get("curr_interest_rate"))
    prediction.getLogisticRegression(curr_loan_delinquency_status,
                                     loan_age,
                                     remaining_months_to_legal_maturity,
                                     curr_interest_rate)
    content = get_file("result.html")
    return Response(content, mimetype="text/html")

@app.route('/rfclassification', methods=['POST'])
def rfclassification():  # pragma: no cover
    curr_loan_delinquency_status = 0
    loan_age = ast.literal_eval(request.form.get("loan_age"))
    remaining_months_to_legal_maturity = ast.literal_eval(request.form.get("remaining_months_to_legal_maturity"))
    curr_interest_rate = ast.literal_eval(request.form.get("curr_interest_rate"))
    prediction.getRFclassification(curr_loan_delinquency_status,
                                     loan_age,
                                     remaining_months_to_legal_maturity,
                                     curr_interest_rate)
    content = get_file("result.html")
    return Response(content, mimetype="text/html")

@app.route('/nnclassification', methods=['POST'])
def nnclassification():  # pragma: no cover
    curr_loan_delinquency_status = 0
    loan_age = ast.literal_eval(request.form.get("loan_age"))
    remaining_months_to_legal_maturity = ast.literal_eval(request.form.get("remaining_months_to_legal_maturity"))
    curr_interest_rate = ast.literal_eval(request.form.get("curr_interest_rate"))
    prediction.getNNclassification(curr_loan_delinquency_status,
                                     loan_age,
                                     remaining_months_to_legal_maturity,
                                     curr_interest_rate)
    content = get_file("result.html")
    return Response(content, mimetype="text/html")

@app.route('/linearregression', methods=['POST'])
def linearregression():  # pragma: no cover
    orig_loantovalue = ast.literal_eval(request.form.get("orig_loantovalue"))
    credit_score = ast.literal_eval(request.form.get("credit_score"))
    orig_upb = ast.literal_eval(request.form.get("orig_upb"))
    no_borrower = ast.literal_eval(request.form.get("no_borrower"))
    orig_debttoincome = ast.literal_eval(request.form.get("orig_debttoincome"))
    no_unit = ast.literal_eval(request.form.get("no_unit"))
    orig_combined_loantovalue = ast.literal_eval(request.form.get("orig_combined_loantovalue"))

    first_time_homebuyer_flag_Y = 0.0
    occupancy_status_I = 0.0
    occupancy_status_O = 0.0
    occupancy_status_S = 0.0
    channel_B = 0.0
    channel_C = 0.0
    channel_R = 0.0
    channel_T = 0.0
    prepayment_penalty_mortgage_flag_Y = 0.0
    loan_purpose_C = 0.0
    loan_purpose_N = 0.0
    loan_purpose_P = 0.0

    first_time_homebuyer_flag = request.form.get("first_time_homebuyer_flag")
    occupancy_status = request.form.get("occupancy_status")
    channel = request.form.get("channel")
    prepayment_penalty_mortgage_flag = request.form.get("prepayment_penalty_mortgage_flag")
    loan_purpose = request.form.get("loan_purpose")
    if first_time_homebuyer_flag == "Y":
        first_time_homebuyer_flag_Y = 1.0

    if occupancy_status == "I":
        occupancy_status_I = 1.0
    elif occupancy_status == "O":
        occupancy_status_O = 1.0
    elif occupancy_status == "S":
        occupancy_status_S = 1.0

    if channel == "B":
        channel_B = 1.0
    elif channel == "C":
        channel_C = 1.0
    elif channel == "R":
        channel_R = 1.0
    elif channel == "T":
        channel_T = 1.0

    if prepayment_penalty_mortgage_flag == "Y":
        prepayment_penalty_mortgage_flag_Y = 1.0

    if loan_purpose == "C":
        loan_purpose_C = 1.0
    elif loan_purpose == "N":
        loan_purpose_N = 1.0
    elif loan_purpose == "P":
        loan_purpose_P = 1.0

    prediction.getLinearRegression(orig_loantovalue,
                                    credit_score,
                                    orig_upb,
                                    no_borrower,
                                    orig_debttoincome,
                                    no_unit,
                                    orig_combined_loantovalue,
                                    first_time_homebuyer_flag_Y,
                                    occupancy_status_I,
                                    occupancy_status_O,
                                    occupancy_status_S,
                                    channel_B,
                                    channel_C,
                                    channel_R,
                                    channel_T,
                                    prepayment_penalty_mortgage_flag_Y,
                                    loan_purpose_C,
                                    loan_purpose_N,
                                    loan_purpose_P)
    content = get_file("result.html")
    return Response(content, mimetype="text/html")

@app.route('/rfregression', methods=['POST'])
def rfregression():  # pragma: no cover
    credit_score = ast.literal_eval(request.form.get("credit_score"))
    no_unit = ast.literal_eval(request.form.get("no_unit"))
    orig_debttoincome = ast.literal_eval(request.form.get("orig_debttoincome"))
    orig_upb = ast.literal_eval(request.form.get("orig_upb"))
    orig_loantovalue = ast.literal_eval(request.form.get("orig_loantovalue"))
    orig_interest_rate = 0
    no_borrower = ast.literal_eval(request.form.get("no_borrower"))

    first_time_homebuyer_flag = request.form.get("first_time_homebuyer_flag")
    occupancy_status = request.form.get("occupancy_status")
    channel = request.form.get("channel")
    prepayment_penalty_mortgage_flag = request.form.get("prepayment_penalty_mortgage_flag")
    loan_purpose = request.form.get("loan_purpose")
    prediction.getRFregression(credit_score,
                    first_time_homebuyer_flag,
                    no_unit,
                    occupancy_status,
                    orig_debttoincome,
                    orig_upb,
                    orig_loantovalue,
                    orig_interest_rate,
                    channel,
                    prepayment_penalty_mortgage_flag,
                    loan_purpose,
                    no_borrower)
    content = get_file("result.html")
    return Response(content, mimetype="text/html")

@app.route('/nnregression', methods=['POST'])
def nnregression():  # pragma: no cover
    credit_score = ast.literal_eval(request.form.get("credit_score"))
    no_unit = ast.literal_eval(request.form.get("no_unit"))
    orig_debttoincome = ast.literal_eval(request.form.get("orig_debttoincome"))
    orig_upb = ast.literal_eval(request.form.get("orig_upb"))
    orig_loantovalue = ast.literal_eval(request.form.get("orig_loantovalue"))
    orig_interest_rate = 0
    no_borrower = ast.literal_eval(request.form.get("no_borrower"))

    first_time_homebuyer_flag = request.form.get("first_time_homebuyer_flag")
    occupancy_status = request.form.get("occupancy_status")
    channel = request.form.get("channel")
    prepayment_penalty_mortgage_flag = request.form.get("prepayment_penalty_mortgage_flag")
    loan_purpose = request.form.get("loan_purpose")
    prediction.getNNregression(credit_score,
                    first_time_homebuyer_flag,
                    no_unit,
                    occupancy_status,
                    orig_debttoincome,
                    orig_upb,
                    orig_loantovalue,
                    orig_interest_rate,
                    channel,
                    prepayment_penalty_mortgage_flag,
                    loan_purpose,
                    no_borrower)
    content = get_file("result.html")
    return Response(content, mimetype="text/html")

@app.route('/cnn-tweet', methods=['GET'])
def getCnnTweet():  # pragma: no cover
    return render_template('cnn_tweet.html')

@app.route('/cnn-tweet-label', methods=['POST'])
def postCnnTweet():  # pragma: no cover
    sentence = request.form["sentence"]
    label = cnn_tweet.getClassfication(sentence)

    flash("Result: {:d}".format(label))
    return render_template('cnn_tweet.html', sentence=sentence)

@app.route('/cnn-tweet-file', methods=['GET'])
def getCnnTweetFile():  # pragma: no cover
    return render_template('cnn_tweet_file.html')

@app.route('/cnn-tweet-file-label', methods=['POST'])
def postCnnTweetFile():  # pragma: no cover
    cnn_tweet.getTestDataOutput()

    # flash("Result: {:d}".format(label))
    return render_template('cnn_tweet_file.html')

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


import discord
import asyncio
client = discord.Client()
from commands import STATICS
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

@asyncio.coroutine
def ex(args, message, client, invoke):
    if message.content[8:]:

        search = message.content[8:]
        driver = webdriver.Firefox('/usr/local/bin/geckodriver')
        #driver = webdriver.Chromium()
        url = "http://www.google.com"
        driver.get(url)
        inputElement = driver.find_element_by_name("q")
        inputElement.send_keys(search)
        inputElement.submit()
        try:
            #resultsstats = driver.find_element_by_id().text
            #resultsstats = driver.find_element_by_id("resultStats").text

            #q = driver.find_element_by_id("search").text #res reo

            time.sleep(5)
            #listl = driver.find_elements_by_class_name("iUh30 bc") #links
            lists = driver.find_elements_by_class_name("LC20lb")
            yield from client.send_message(message.channel, "Found "+str(len(lists))+" searches:")
            i = 0
            for listiems in lists:
                yield from client.send_message(message.channel,listiems.text)

                i += 1
                if(i > 4):
                    driver.close()
                    break

                    #p = re.findall(r'<h3>()</h3>', str(q))
                    #print(q)
                    #print(resultsstats)
        except:
            yield from client.send_message(message.channel, "Es gibt einen fehler!")
            time.sleep(5)
            driver.close()
    else:
        embed = discord.Embed(
            title="Commands",
            color=600,  # blue
            description="~search (suche mit 4 Ergebnisse)"
        )
        embed.set_author(
            name="Command list",
            icon_url="http://img1.wikia.nocookie.net/__cb20070302203617/beamer/images/7/71/Zahnrad.png",
            url="https://discordapp.com/developers/applications "
        )
        embed.set_footer(
            text="Ein bot von Brainkackwitz"
        )
        embed.set_thumbnail(
            url="http://img1.wikia.nocookie.net/__cb20070302203617/beamer/images/7/71/Zahnrad.png"
        )
        yield from client.send_message(message.channel, embed=embed)
        time.sleep(5)
        driver.close()


#!/usr/bin/python3

import os
import json
import argparse

import base64
from io import BytesIO
import cv2
from PIL import Image
import numpy as np

def _load_b64_string_to_img(b64_byte_string):
    image_bytes = base64.b64decode(b64_byte_string)
    image_data = BytesIO(image_bytes)
    img = Image.open(image_data)
    return img

def _load_json_as_image(filename, output, type='rgb'):
    # with open(filename, 'r') as f:
    #     result_list = json.load(f)

    # for result in result_list:
    #     # image_byte_string = result['image']
    #     # image_bytes = base64.b64decode(image_byte_string)
    #     # image_data = BytesIO(image_bytes)
    #     # img = Image.open(image_data)

    #     #img = _load_b64_string_to_img(result['image'])
    #     output_name = output + '_' + result['key'] + '.jpg'
    #     img.save(output_name)

    with open(filename, 'r') as f:
        result = json.load(f)

    #img = _load_b64_string_to_img(result['predictions'])
    img_array = np.array(result['predictions'])
    img = Image.fromarray(img_array, 'RGB')
    output_name = output + '_' + '.jpg'
    img.save(output_name)

    print('saved:', output)

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--input', '-i', type=str, required=True)
parser.add_argument('--output', '-o', type=str, required=True)

if __name__ == '__main__':
    ARGS = parser.parse_args()
    INPUT = ARGS.input
    OUTPUT = ARGS.output
    _load_json_as_image(INPUT, OUTPUT)


from matplotlib import pyplot
import random

#x_values = [0, 2, 4, 8, 16, 32]
#y_values = [random.randint(0, 30) for elt in x_values]

data = open ("world_population.txt", "r").readlines()
dates = []
populations = []

for point in data:
    date, population = point.split()
    dates.append(date)
    populations.append(population)

line_format = "o-"
pyplot.plot(dates, populations, line_format)

pyplot.ylabel("Population")
pyplot.xlabel("Year")
pyplot.title("World population by year")

pyplot.show()


import sys
import numpy
import kernel_tuner

def assert_noodles_works():
    if sys.version_info[0] < 3 or (sys.version_info[0] == 3 and sys.version_info[1] < 5):
        raise RuntimeError("Noodles runner test requires Python 3.5 or newer")

    import importlib.util
    noodles_installed = importlib.util.find_spec("noodles") is not None

    if not noodles_installed:
        raise RuntimeError("Noodles runner test requires Noodles")

def test_noodles_opencl():
    kernel_string = """
    __kernel void vector_add(global float *c, global float *a, global float *b, int n) {
        int i = get_group_id(0) * block_size_x + get_local_id(0);
        if (i<n) {
            c[i] = a[i] + b[i];
        }
    }
    """

    size = 1000000
    a = numpy.random.randn(size).astype(numpy.float32)
    b = numpy.random.randn(size).astype(numpy.float32)
    c = numpy.zeros_like(b)
    n = numpy.int32(size)

    args = [c, a, b, n]
    tune_params = {"block_size_x": [16+16*i for i in range(32)]}

    result, _ = kernel_tuner.tune_kernel(
        "vector_add", kernel_string, size, args, tune_params, quiet=True)
    #    use_noodles=True, num_threads=4, quiet=True)

    print("# {par1:20} | {time:20} ".format(par1='block-size-x', time='time'))
    print("#{bar}|{bar}".format(bar='-'*22))
    for r in sorted(result, key=lambda r: r['block_size_x']):
        print("  {par1:20}   {time:20.6} ".format(par1=r['block_size_x'], time=r['time']))

if __name__ == "__main__":
    assert_noodles_works()
    test_noodles_opencl()


from PySide2.QtCore import QFileSystemWatcher
import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from time import time
import datetime

class FileWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        log_file_name = r"C:\Program Files (x86)\Hearthstone\Logs\Power.log"

        self.filewatcher = QFileSystemWatcher()
        is_add = self.filewatcher.addPath(log_file_name)
        print("ADD: ", is_add)
        self.filewatcher.fileChanged.connect(self.log_file_changed)

        self.show()

    def log_file_changed(self):
        print(f"{datetime.datetime.now():%Y-%m-%d %H:%M:%S} log_file_changed")

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = FileWindow()
    sys.exit(app.exec_())


import pip
import pkg_resources
# *** Add required packages to this list ***
packages = ['pyyaml', 'numpy', 'matplotlib']
# *** Add required packages to this list ***

def main(packages, upgrade = False):
    # Install gslab_tools if it's not installed yet or it has the wrong version
    pip.main(['install', '--upgrade', 'git+http://git@github.com/gslab-econ/gslab_python.git@v4.1.0'])

    # Install required packages using pip
    installed_packages = [pkg.key for pkg in pip.get_installed_distributions()]
    for pkg in packages:
        if upgrade:
            pip.main(['install', '--upgrade', pkg])
        elif pkg not in installed_packages:
            pip.main(['install', pkg])

# upgrade = TRUE will update all packages to the most current version
# upgrade = FALSE will skip packages that are already installed
main(packages, upgrade = False)


import os
import sys
from collections import defaultdict
from typing import List, Callable
from numbers import Number
from pathlib import Path

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import cvxpy as cp
from cvxpylayers.torch.cvxpylayer import CvxpyLayer
import pytorch_lightning as pl

DIR_PATH = Path(__file__).parent
sys.path.append(str(DIR_PATH))
import utils
import _deep_perm_net_v2 as deep_perm_net_v2
import permuted_mnist_dataset as pmnist
import lit_mnist_classifier

class LitDeepPermNet_v2(deep_perm_net_v2.DeepPermNet_v2, pl.LightningModule):
    def __init__(
        self
        , feature_extractor: nn.Module
        , feature_size: int
        , head_size: int
        , loss_func: str
        , batch_size: int
        , lr: float
        , mnist_path: str
        , download: bool
        , duplicates_multiplier: int
        , seed: int
        , move_data_to_cuda: bool
        , use_binary_optimization_accuracy_metric: bool
        , transform: Callable = transforms.Compose([
            transforms.ToTensor()
            , transforms.Normalize((0.1307,), (0.3081,))
        ])
        , **kwargs
    ):

        super().__init__(feature_extractor, feature_size, head_size, **kwargs)

        if loss_func.lower() == 'l1':
            self.loss_func = nn.L1Loss()
        elif loss_func.lower() == 'l2':
            self.loss_func = nn.MSELoss()
        else:
            raise NotImplementedError()

        self.use_binary_optimization_accuracy_metric = use_binary_optimization_accuracy_metric
        self.transform = transform
        self.move_data_to_cuda = move_data_to_cuda
        self.batch_size = batch_size
        self.download = download
        self.feature_size = feature_size
        self.head_size = head_size
        self.lr = lr
        self.duplicates_multiplier = duplicates_multiplier
        self.seed = seed
        self.mnist_path = mnist_path
        self.save_hyperparameters(
            {
                'loss_func': loss_func.lower()
                , 'batch_size': batch_size
                , 'feature_size': feature_size
                , 'head_size': head_size
                , 'lr': lr
                , 'duplicates_multiplier': duplicates_multiplier
                , 'seed': seed
                , **kwargs
            }
        )

    def training_step(self, batch, batch_idx):
        x, y = batch
        y_hat = self(x)
        loss = self.loss_func(y_hat.flatten(1), y.flatten(1)).type(torch.float32)
        self.log('train_loss', loss)
        return loss

    def training_epoch_end(self, outputs):
        losses = torch.as_tensor([o['loss'] for o in outputs])
        self.log('avg_train_loss', losses.mean(), prog_bar=True)

    def validation_step(self, batch, batch_idx):
        x, y = batch
        y_hat = self(x)

        loss = self.loss_func(y_hat.flatten(1), y.flatten(1)).type(torch.float32)
        output = {'val_loss': loss}

        if self.use_binary_optimization_accuracy_metric:
            acc = utils.get_mean_accuracy(y_hat, y)
            output['val_acc'] = acc

        self.log_dict(output, prog_bar=True)

        return output

    def validation_epoch_end(self, outputs):
        losses = []
        accs = []

        for o in outputs:
            losses.append(o['val_loss'])
            if o.get('val_acc', None) is not None:
                accs.append(o['val_acc'])

        log = {'avg_val_loss': torch.as_tensor(losses).mean()}
        if len(accs) > 0:
            log['avg_val_acc'] = torch.as_tensor(accs).mean()

        self.log_dict(log, prog_bar=True)

    def configure_optimizers(self):
        return torch.optim.Adam(filter(lambda p: p.requires_grad, self.parameters()), lr=self.lr)

    def train_dataloader(self):
        train_dataset = pmnist.PermutedMnistDataset(
            self.mnist_path
            , download=self.download
            , train=True
            , transform=self.transform
            , duplicates_multiplier=self.duplicates_multiplier
            , head_size=self.head_size
            , seed=self.seed
            , move_data_to_cuda=self.move_data_to_cuda
        )

        train_loader = torch.utils.data.DataLoader(
            train_dataset
            , batch_size=self.batch_size
            , shuffle=True
        )

        return train_loader

    def val_dataloader(self):
        val_dataset = pmnist.PermutedMnistDataset(
            self.mnist_path
            , download=self.download
            , train=False
            , transform=self.transform
            , duplicates_multiplier=self.duplicates_multiplier
            , head_size=self.head_size
            , seed=self.seed
            , move_data_to_cuda=self.move_data_to_cuda
        )

        val_loader = torch.utils.data.DataLoader(
            val_dataset
            , batch_size=self.batch_size
        )

        return val_loader

if __name__ == "__main__":
    feature_extractor = lit_mnist_classifier.LitMNISTClassifier.load_from_checkpoint(
        checkpoint_path='logs_mnist_clf/version_1/checkpoints/epoch=14-avg_val_loss=0.0394-avg_val_acc=0.9876.ckpt'
    )
    feature_extractor = feature_extractor.float()

    model = LitDeepPermNet_v2(
        feature_extractor=feature_extractor
        , feature_size=10
        , head_size=10
        , loss_func='l1'
        , batch_size=16
        , lr=0.0003
        , mnist_path='mnist'
        , download=False
        , duplicates_multiplier=2
        , seed=42
        , disable_feature_extractor_training=True
        , use_binary_optimization_accuracy_metric=False
        , permutation_extractor_version='v5'
        , bottleneck_features_num=64
        , move_data_to_cuda=False
        , entropy_reg=1e-1
        , max_iter=1e2
        , tol=1e-6
        , log=False
        , verbose=False
        , log_interval=10
        , tau=1e3
    )
    # model.on_validation_model_eval()

    logger = pl.loggers.TensorBoardLogger(save_dir='./logs_deep_perm_net_v2_1', name='')
    checkpoint_callback = pl.callbacks.ModelCheckpoint(
        monitor='avg_val_loss'
        , save_top_k=1
        , mode='min'
        , filepath=str(Path(logger.log_dir, 'checkpoints', '{epoch}-{avg_val_loss:.4f}'))
    )
    trainer = pl.Trainer(
        max_epochs=100
        , checkpoint_callback=checkpoint_callback
        , logger=logger
        # , gradient_clip_val=0.5
        # , precision=16  # On GPU can be beneficial
        , track_grad_norm=2
        # , gpus=[0]
        # , fast_dev_run=True
    )
    trainer.fit(model)


from app import app, events_topic
from random import randint
from faust.cli import option

@app.command(option('--max-events', type=int, required=True))
async def send_events(self, max_events:int):
    print(f'Sending {max_events} events...')
    half_events = max_events // 2
    for i in range(max_events):
        await events_topic.send(key=str(randint(1, 1500000)), value=randint(0, 1000))
        if i == half_events:
            print(f'Half amount of events sent: {i}')


#
import urllib
import flask
from flask import request
from apps.dsiv.controller.c_bmy import CBmy
from apps.dsiv.controller.flask_web import FlaskWeb

class CDsiv(object):
    bmy_id_to_img_files = None
    bmy_id_to_img_file_idx = None

    @staticmethod
    def get_bmy_id_example_img_file():
        bmy_id = int(urllib.parse.unquote(request.args.get('bmyId')))
        img_idx = int(urllib.parse.unquote(request.args.get('imgIdx')))
        print('bmyId={0}'.format(bmy_id))
        print('CDsiv.bmy_id_to_img_files={0};'.format(len(CDsiv.bmy_id_to_img_files)))
        print('bmy_id_to_img_file_idx={0};'.format(len(CDsiv.bmy_id_to_img_file_idx)))
        # CDsiv.bmy_id_to_img_file_idx[bmy_id]
        img_file = CDsiv.bmy_id_to_img_files[bmy_id][img_idx]
        resp = FlaskWeb.get_resp_param()
        resp['data'] ={'imgFile': img_file[0]}
        return FlaskWeb.generate_response(resp)

    @staticmethod
    def display_image():
        img_file = urllib.parse.unquote(request.args.get('imgFile'))
        with open(img_file, 'rb') as img_fd:
            image_data = img_fd.read()
        response = flask.make_response(image_data)
        postfix = img_file[-4:]
        response.headers['Content-Type'] = 'image/{0}'.format(postfix)
        return response

    @staticmethod
    def initialize():
        CDsiv.bmy_id_to_img_files = CDsiv.read_bmy_id_to_img_files()
        #CDsiv.initialize_bmy_id_to_img_file_idx()
        CDsiv.bmy_id_to_img_file_idx = CDsiv.read_bmy_id_to_img_file_idx()
        #bmy_id = 188
        #bmy_id_to_img_file_idx[bmy_id] = 518
        #CDsiv.save_bmy_id_to_img_file_idx(bmy_id_to_img_file_idx)
        #print('idx={0};'.format(bmy_id_to_img_file_idx[bmy_id]))
        #print('image_file: {0};'.format(bmy_id_to_img_files[bmy_id][bmy_id_to_img_file_idx[bmy_id]]))
        pass

    bmy_sim_org_dict = {}
    @staticmethod
    def get_org_bmy_id_of_sim(sim_bmy_id):
        if len(CDsiv.bmy_sim_org_dict)<2:
            with open('/media/ps/0A9AD66165F33762/yantao/dcl/support/bmy_sim_org_dict.txt', 'r', encoding='utf-8') as fd:
                for line in fd:
                    line = line.strip()
                    arrs = line.split(':')
                    CDsiv.bmy_sim_org_dict[int(arrs[0])] = int(arrs[1])
        return CDsiv.bmy_sim_org_dict[sim_bmy_id]

    @staticmethod
    def read_bmy_id_to_img_files():
        num = 0
        bmy_id_to_img_files = {}
        with open('/media/ps/0A9AD66165F33762/yantao/dcl/datasets/'
                    'CUB_200_2011/anno/sfds_train_ds_20201020.txt', 'r', encoding='utf-8') as fd:
            for line in fd:
                line = line.strip()
                arrs = line.split('*')
                img_file = arrs[0]
                sim_bmy_id = int(arrs[1])
                bmy_id = CDsiv.get_org_bmy_id_of_sim(sim_bmy_id)
                if bmy_id not in bmy_id_to_img_files:
                    bmy_id_to_img_files[bmy_id] = []
                bmy_id_to_img_files[bmy_id].append([img_file])
                num += 1
                if num % 1000000 == 0:
                    print('已经处理{0}条记录...'.format(num))
        return bmy_id_to_img_files

    @staticmethod
    def save_bmy_id_to_img_file_idx(bmy_id_to_img_file_idx):
        with open('./bmy_id_to_img_file_idx.txt', 'w', encoding='utf-8') as fd:
            for k, v in bmy_id_to_img_file_idx.items():
                fd.write('{0}:{1}\r\n'.format(k, v))

    @staticmethod
    def read_bmy_id_to_img_file_idx():
        bmy_id_to_img_file_idx = {}
        with open('./bmy_id_to_img_file_idx.txt', 'r', encoding='utf-8') as fd:
            for line in fd:
                line = line.strip()
                arrs = line.split(':')
                bmy_id_to_img_file_idx[int(arrs[0])] = int(arrs[1])
        return bmy_id_to_img_file_idx

    @staticmethod
    def initialize_bmy_id_to_img_file_idx():
        '''
        '''
        bmy_ids = CBmy.get_bmy_ids()
        with open('./bmy_id_to_img_file_idx.txt', 'w+', encoding='utf-8') as fd:
            for bmy_id in bmy_ids:
                print('bmy_id: {0};'.format(bmy_id))
                fd.write('{0}:{1}\r\n'.format(bmy_id['bmy_id'], 0))


from selenium import webdriver
from PIL import Image, ImageEnhance
import tesserocr
import time

url = 'http://bn023.test.s2ps.cn/'
driver = webdriver.Chrome()
driver.maximize_window()  # 将浏览器最大化
driver.get(url)
time.sleep(1)

# 截取当前网页并命名为printscreen，该网页有我们需要的验证码
driver.save_screenshot('printscreen.png')
imgelement = driver.find_element_by_id('txtcheckcode')  # 定位验证码
location = imgelement.location  # 获取验证码x,y轴坐标
print(location)
size = imgelement.size  # 获取验证码的width,height的坐标
print(size)

# 写成我们需要截取的位置坐标（通过增减确定目标位置，每台电脑的分辨率不一样，请根据自己的电脑找合适的位置）
rangle = (int(location['x']+310), int(location['y']+160), int(location['x'] + size['width']+218),
          int(location['y'] + size['height']+175))

i = Image.open("printscreen.png")  # 打开截图
frame4 = i.crop(rangle)  # 使用Image的crop函数，从截图中再次截取我们需要的区域
frame4=frame4.convert('RGB')
frame4.save('printscreen.jpg') # 保存我们接下来的验证码图片 进行打码

image = Image.open('printscreen.jpg')
image = image.convert('L')
threshold = 127
table = []
for i in range(256):
    if i < threshold:
        table.append(1)
    else:
        table.append(0)

image = image.point(table, '1')
#image.show()
result = tesserocr.image_to_text(image)
print(result)

time.sleep(2)
driver.close()


"""
Als gemeente wil ik dat de aanvraag tbh een straatoptreden als zaak wordt
gecreëerd zodat mijn dossiervorming op orde is en de voortgang transparant is.

Ref: https://github.com/VNG-Realisatie/gemma-zaken/issues/163

Zie ook: test_userstory_39.py, test_userstory_169.py
"""
import datetime

from django.test import override_settings

from rest_framework import status
from rest_framework.test import APITestCase
from zds_schema.constants import RolOmschrijving, RolTypes
from zds_schema.tests import get_operation_url

# aanvraag aangemaakt in extern systeem, leeft buiten ZRC
from zrc.datamodel.constants import ZaakobjectTypes
from zrc.datamodel.models import Zaak
from zrc.datamodel.tests.factories import ZaakFactory
from zrc.tests.utils import parse_isodatetime

CATALOGUS = 'https://example.com/ztc/api/v1/catalogus/878a3318-5950-4642-8715-189745f91b04'
ZAAKTYPE = f'{CATALOGUS}/zaaktypen/283ffaf5-8470-457b-8064-90e5728f413f'
STATUS_TYPE = f'{ZAAKTYPE}/statustypen/1'

VERANTWOORDELIJKE_ORGANISATIE = 'https://www.example.com/orc/api/v1/rsgb/nietnatuurlijkepersonen/1234'
AVG_INZAGE_VERZOEK = 'https://www.example.com/orc/api/v1/avg/inzageverzoeken/1234'
BEHANDELAAR = 'https://www.example.com/orc/api/v1/brp/natuurlijkepersonen/1234'

@override_settings(LINK_FETCHER='zds_schema.mocks.link_fetcher_200')
class US153TestCase(APITestCase):

    def test_create_zaak_with_kenmerken(self):
        zaak_create_url = get_operation_url('zaak_create')
        data = {
            'zaaktype': ZAAKTYPE,
            'bronorganisatie': '517439943',
            'verantwoordelijkeOrganisatie': VERANTWOORDELIJKE_ORGANISATIE,
            'identificatie': 'AVG-inzageverzoek-1',
            'omschrijving': 'Dagontheffing - Station Haarlem',
            'toelichting': 'Het betreft een clown met grote trom, mondharmonica en cymbalen.',
            'startdatum': '2018-08-15',
            'kenmerken': [{
                'kenmerk': 'kenmerk 1',
                'bron': 'bron 1',
            }, {
                'kenmerk': 'kenmerk 2',
                'bron': 'bron 2',
            }]
        }

        response = self.client.post(zaak_create_url, data, HTTP_ACCEPT_CRS='EPSG:4326')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)

        zaak = Zaak.objects.get(identificatie=data['identificatie'])
        self.assertEqual(zaak.zaakkenmerk_set.count(), 2)

    def test_read_zaak_with_kenmerken(self):
        zaak = ZaakFactory.create()
        zaak.zaakkenmerk_set.create(kenmerk='kenmerk 1', bron='bron 1')
        self.assertEqual(zaak.zaakkenmerk_set.count(), 1)

        zaak_read_url = get_operation_url('zaak_read', uuid=zaak.uuid)

        response = self.client.get(zaak_read_url, HTTP_ACCEPT_CRS='EPSG:4326')
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)

        data = response.json()

        self.assertTrue('kenmerken' in data)
        self.assertEqual(len(data['kenmerken']), 1)
        self.assertDictEqual(
            data['kenmerken'][0],
            {
                'kenmerk': 'kenmerk 1',
                'bron': 'bron 1',
            }
        )

    def test_update_zaak_with_kenmerken(self):
        zaak = ZaakFactory.create()
        kenmerk_1 = zaak.zaakkenmerk_set.create(kenmerk='kenmerk 1', bron='bron 1')
        self.assertEqual(zaak.zaakkenmerk_set.count(), 1)

        zaak_read_url = get_operation_url('zaak_read', uuid=zaak.uuid)
        response = self.client.get(zaak_read_url, HTTP_ACCEPT_CRS='EPSG:4326')

        data = response.json()

        zaak_update_url = get_operation_url('zaak_update', uuid=zaak.uuid)
        data['kenmerken'].append({
            'kenmerk': 'kenmerk 2',
            'bron': 'bron 2',
        })

        response = self.client.put(zaak_update_url, data, HTTP_ACCEPT_CRS='EPSG:4326')
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)

        zaak = Zaak.objects.get(identificatie=zaak.identificatie)
        self.assertEqual(zaak.zaakkenmerk_set.count(), 2)

        # All objects are deleted, and (re)created.
        self.assertFalse(kenmerk_1.pk in zaak.zaakkenmerk_set.values_list('pk', flat=True))

    def test_full_flow(self):
        zaak_create_url = get_operation_url('zaak_create')
        zaakobject_create_url = get_operation_url('zaakobject_create')
        status_create_url = get_operation_url('status_create')
        rol_create_url = get_operation_url('rol_create')

        # Creeer InzageVerzoek
        # self.client.post(...)

        # Creeer Zaak
        data = {
            'zaaktype': ZAAKTYPE,
            'bronorganisatie': '517439943',
            'verantwoordelijkeOrganisatie': VERANTWOORDELIJKE_ORGANISATIE,
            'identificatie': 'AVG-inzageverzoek-1',
            'omschrijving': 'Melding binnengekomen via website.',
            'toelichting': 'Vanuit melding: Beste,\n\nGraag zou ik ...',
            'startdatum': '2018-08-22',
            'kenmerken': [{
                'kenmerk': 'kenmerk 1',
                'bron': 'bron 1',
            }, {
                'kenmerk': 'kenmerk 2',
                'bron': 'bron 2',
            }]
        }
        response = self.client.post(zaak_create_url, data, HTTP_ACCEPT_CRS='EPSG:4326')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)

        zaak = response.json()

        # Koppel Zaak aan InzageVerzoek
        data = {
            'zaak': zaak['url'],
            'object': AVG_INZAGE_VERZOEK,
            'relatieomschrijving': 'Inzage verzoek horend bij deze zaak.',
            'type': ZaakobjectTypes.avg_inzage_verzoek
        }
        response = self.client.post(zaakobject_create_url, data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)

        # Geef de Zaak een initiele Status
        data = {
            'zaak': zaak['url'],
            'statusType': STATUS_TYPE,
            'datumStatusGezet': datetime.datetime.now().isoformat(),
        }
        response = self.client.post(status_create_url, data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)

        # Haal mogelijke rollen op uit ZTC...
        # self.client.get(...)

        # Voeg een behandelaar toe.
        data = {
            'zaak': zaak['url'],
            'betrokkene': BEHANDELAAR,
            'betrokkeneType': RolTypes.natuurlijk_persoon,
            'rolomschrijving': RolOmschrijving.behandelaar,
            'roltoelichting': 'Initiele behandelaar die meerdere (deel)behandelaren kan aanwijzen.'
        }
        response = self.client.post(rol_create_url, data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)

        # Status wijzigingen...

        # Update Zaak met nieuwe behandeltermijn, uitstel van 2 weken.
        zaak_update_url = get_operation_url('zaak_update', uuid=zaak['url'].rsplit('/', 1)[1])

        if zaak['einddatumGepland']:
            end_date_planned = parse_isodatetime(zaak['einddatumGepland'])
        else:
            end_date_planned = datetime.datetime.now()

        data = zaak.copy()
        data['einddatumGepland'] = (end_date_planned + datetime.timedelta(days=14)).strftime('%Y-%m-%d')

        response = self.client.put(zaak_update_url, data, HTTP_ACCEPT_CRS='EPSG:4326')
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)

        # Voeg documenten toe...
        # self.client.post(...)
        # Koppel documenten aan Zaak
        # self.client.post(...)


import json

from .....execution import Execution
from .mutation_type import mutation

def from_string(value):
    if value is None:
        return None

    if value.startswith("{") and value.endswith("}"):
        return json.loads(value)

    if value.startswith("int:"):
        return int(value[4:])

    return value

@mutation.field("execute")
async def resolve_execute(
    obj,
    info,
    experiment,
    storage=None,
    engine=None,
    index=None,
    project=None,
    seed=None,
):
    execution = Execution(
        experiment=from_string(experiment),
        storage=from_string(storage),
        engine=from_string(engine),
        index=from_string(index),
        project=from_string(project),
        seed=from_string(seed),
    )
    execution.submit()
    return execution


"""
Unittests for: pyin.core
"""

import sys

import pytest

import pyin.core
import tests._test_module

def test_single_expr():
    result = list(pyin.core.pmap("20 <= line <= 80", range(100)))
    assert len(result) == len(list(range(20, 81)))
    for item in result:
        assert 20 <= item <= 80

def test_with_map():
    result = list(pyin.core.pmap(
        "list(map(int, line.split('-')))", ['2015-01-01']))
    assert result == [[2015, 1, 1]]

@pytest.mark.parametrize("obj", [
    'map', 'reduce', 'op', 'it'
])
def test_scope(obj):
    """Make sure specific objects aren't removed from the scope."""
    for res in pyin.core.pmap(obj, 'word'):
        pass

def test_floating_point_division():
    result = next(pyin.core.pmap('5 / 3', ['']))
    assert isinstance(result, float)
    assert 1 < result < 2

@pytest.mark.parametrize("expression", [
    'return',  # Evaluates as a statement, not string.
    ''
])
def test_invalid_expression(expression):
    with pytest.raises(SyntaxError):
        next(pyin.core.pmap(expression, []))


from django.forms import ModelForm
from .models import Question, Choice
from django.db import models

class QuestionForm(ModelForm):
    #question_text = forms.CharField(label='Название', max_length=200)

    class Meta:
        model = Question
        fields = ('question_text', 'pub_date')

class ChoiceForm(ModelForm):
    class Meta:
        model = Choice
        fields = ('question', 'choice_text')

form_question = QuestionForm()

form_choice = ChoiceForm()


#!/usr/bin/env python
# -*- coding:utf-8 _*-
# Author: Wengs
# Time  : 2/24/2019 10:30 AM
# File  : sideshot_bullet.py
# IDE   : PyCharm

# 12-5

import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
    """一个对飞船发射的子弹进行管理的类"""

    def __init__(self, ss_settings, screen, ship):
        """在飞船所处的位置创建一个子弹对象"""
        super().__init__()
        self.screen = screen

        # 在(0, 0)处创建一个表示子弹的矩阵，再设置正确的位置
        self.rect = pygame.Rect(0, 0, ss_settings.bullet_width,
                                ss_settings.bullet_height)
        self.rect.centery = ship.rect.centery
        self.rect.right = ship.rect.right

        # 存储用小数表示的子弹设置
        self.x = float(self.rect.x)

        self.color = ss_settings.bullet_color
        self.speed_factor = ss_settings.bullet_speed_factor

    def update(self):
        """向上移动子弹"""
        # 更新表示子弹位置的小数值
        self.x += self.speed_factor
        # 更新表示子弹的rect的位置
        self.rect.x = self.x

    def draw_bullet(self):
        """在屏幕上绘制子弹"""
        pygame.draw.rect(self.screen, self.color, self.rect)


from database_component import DataBase
from clustering_kmeans import ClusteringKMeans
from classifier_random_forest import ClassifierRandomForest
from logistic_regression import Logistic_Regression
from utils import select_option, show_select_project
import glob, os
import pandas as pd

'''
*   @author Evandro Luis da Rosa Fensterseifer Filho
'''

def read_csv():
    data = pd.read_csv("./data-all.csv", index_col=0)
    return data

# ARRUMAR PRA EXIBIR COLUNAS, LINHAS - CONJUNTOS E VALORES ESPECIFICOS
def show_csv(row=0, col=0):
    print(data)

def import_data_for_database(db):
    my_list = []
    os.chdir("./dataB")
    my_list = glob.glob("*.bson")

    for aux in my_list:
        name = aux.split(".")[0]
        db.insertIntoCollection(aux, name)
    os.chdir("../")

def select_project(model, data, db):
    my_data = show_select_project(data)
    model.get_similars(my_data, db)

if __name__ == '__main__':
    data = read_csv()
    db = DataBase()
    db.setDataBase("trabalho")

    import_data_for_database(db)

    choose = -1
    print("Selecione metodo <opcao numerica>")
    print("0 - KMeans (PCA)")
    print("1 - Random Forest")
    print("2 - Logistic Regression")
    print("3 - Show dataset")
    while choose > 3 or choose < 0:
        choose = int(input("Opcao => "))

    if(choose == 0):
        kmeans = ClusteringKMeans()
        option = select_option(choose)
        kmeans.executeKM(data, option)
        select_project(kmeans, data, db)
    elif(choose == 1):
        randomForest = ClassifierRandomForest()
        option = select_option(choose)
        randomForest.executeRF(data, option)
        select_project(randomForest, data, db)
    elif(choose == 2):
        logiReg = Logistic_Regression()
        option = select_option(choose)
        logiReg.executeLogReg(data)
        select_project(logiReg, data, db)
    elif(choose == 3):
        show_csv(data)

    print("Operacao finalizada")


import requests
import json

key = 'apikey=plzBanMe' # deve ir ao site http://www.omdbapi.com/ e pegar uma nova key
r = '\033[31m'  # fonte vermelha (red)
n = '\033[m'  # Cor 'normal'

def existefilmes(titulo):
    quant = 0

    try:
        req = requests.get(f"http://www.omdbapi.com/?s={titulo}&type=movie&{key}")
        resposta = json.loads(req.text)
        if resposta['Response'] == 'True':
            quant = resposta['totalResults']
    except:
        print('Conexao falhou')
    return quant

def lista_filmes(titulo):
    lista = []
    for i in range(1, 101):
        print('Pesquisando na pagina', i)
        try:
            req = requests.get(f"http://www.omdbapi.com/?s={titulo}&type=movie&page={str(i)}&{key}")
            resposta = json.loads(req.text)

            if resposta['Response'] == 'False':
                break
            else:
                for filme in resposta['Search']:
                    title = filme['Title']
                    ano = filme['Year']
                    string = f"{title} ({ano})"
                    lista.append(string)
        except:
            print('Conexao falhou')
    return lista

sair = False
while not sair:
    op = input('Pesquise por um filme ou digite SAIR: ')
    if op.lower() == 'sair':
        sair = True
    else:
        lista_de_filmes = lista_filmes(op)
        print(f'Filmes encontrados: {len(lista_de_filmes)}')
        for filme in lista_de_filmes:
            print(filme)

Curso Python Básico - SOLYD/Aula 13 - API, JSON e consultando listas de filmes/exercicio13.py


#!/usr/bin/env python3
# coding: utf-8
#
#   File = infinite_poker_pif.py
#
#      Copyright 2020 Rob Curtis
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
############################################################################
import logging
import random

from pifs.base_pif import BasePIF
from utils import poker_util
from utils.reddit_helper import get_comment

instructionTemplate = """
Welcome to {}'s Infinite Poker PIF (managed by LatherBot).

I will deal five cards from a fresh deck to each qualified entry.
In order to qualify, you must have at least {} karma on the sub in the last 90 days.

To enter, simply add a top-level comment on the PIF post that includes (on a line by itself) the command:

 `LatherBot in`

I will check your karma and deal your cards if you qualify.

This PIF will close in {} hour(s).
At that time, I will determine the winner and notify the PIF's creator.

LatherBot documentation can be found in [the wiki](https://www.reddit.com/r/Wetshaving/wiki/latherbot)

If you see something, say something: [Report PIF Abuse](https://docs.google.com/forms/d/e/1FAIpQLScLVbYclUvKMbhrrz0WhfOKPQyr56_jH-4q8oOJf_emgAew7w/viewform?usp=sf_link)

Good luck!
"""

entry_template = """
{} drew {} {} {} {} {}

{}
"""

winner_template = """
The PIF is over!

u/{} has won with {}
"""

class InfinitePoker(BasePIF):

    def __init__(self, postId, authorName, minKarma, durationHours, endTime, pifOptions={}, pifEntries={}, karmaFail={}):
        logging.debug('Building poker PIF [%s]', postId)

        BasePIF.__init__(self, postId, authorName, 'infinite-poker', minKarma, durationHours, endTime, pifOptions, pifEntries, karmaFail)

    def pif_instructions(self):
        logging.info('Printing instructions for PIF [%s]', self.postId)
        return instructionTemplate.format(self.authorName,
                                          self.minKarma,
                                          self.durationHours)

    def handle_entry(self, comment, user, command_parts):
        logging.info('User [%s] entered to PIF [%s]', user, self.postId)

        deck = poker_util.new_deck()
        user_hand = list()

        for i in range(5):
            card = poker_util.deal_card(deck)
            user_hand.append(card)

        user_hand = poker_util.order_cards(user_hand)

        entry_details = dict()
        entry_details['CommentId'] = comment.id
        entry_details['UserHand'] = user_hand
        entry_details['HandScore'] = poker_util.hand_score(user_hand)

        self.pifEntries[user.name] = entry_details

        comment.reply(entry_template.format(
            user.name,
            poker_util.format_card(user_hand[0]),
            poker_util.format_card(user_hand[1]),
            poker_util.format_card(user_hand[2]),
            poker_util.format_card(user_hand[3]),
            poker_util.format_card(user_hand[4]),
            poker_util.determine_hand(user_hand)))
        comment.save()

    def determine_winner(self):
        curr_max_score = 0
        tied_winners = list()

        for entrant in self.pifEntries.keys():
            if self.pifEntries[entrant]['HandScore'] > curr_max_score:
                if self.postId != get_comment(self.pifEntries[entrant]['CommentId']).submission.id:
                    continue
                tied_winners = list()
                tied_winners.append(entrant)
                curr_max_score = self.pifEntries[entrant]['HandScore']
            elif self.pifEntries[entrant]['HandScore'] == curr_max_score:
                tied_winners.append(entrant)

        if len(tied_winners) == 1:
            self.pifWinner = tied_winners[0]
            logging.info('User [%s] has won PIF [%s]', self.pifWinner, self.postId)
        else:
            logging.info("{} players tied for first", len(tied_winners))
            self.pifWinner = random.choice(tied_winners)
        logging.info('User [%s] has won PIF [%s]', self.pifWinner, self.postId)

    def generate_winner_comment(self):
        return winner_template.format(self.pifWinner,
                                      poker_util.determine_hand(self.pifEntries[self.pifWinner]['UserHand']))


import cv2
import numpy as np
import os
import glob

import pandas as pd
import random
import matplotlib.pyplot as plt

'''
Code picked up from the
http://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/
'''

def bb_intersection_over_union(boxA, boxB):
    # determine the (x, y)-coordinates of the intersection rectangle
    xA = max(boxA[0], boxB[0])
    yA = max(boxA[1], boxB[1])
    xB = min(boxA[2], boxB[2])
    yB = min(boxA[3], boxB[3])

    # compute the area of intersection rectangle
    interArea = (xB - xA + 1) * (yB - yA + 1)

    # compute the area of both the prediction and ground-truth
    # rectangles
    boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
    boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)

    # compute the intersection over union by taking the intersection
    # area and dividing it by the sum of prediction + ground-truth
    # areas - the interesection area
    iou = interArea / float(boxAArea + boxBArea - interArea)

    # return the intersection over union value
    return iou

'''
Calculate the Tau for a given video and ten values
bins = 20
bin size = 1/20

'''
def calculate_t(truth, prediction):
    iou_list = []
    for original,predicted in zip(truth,prediction):
        iou = bb_intersection_over_union(original,predicted)
        iou_list.append(iou)
    bins = np.linspace(0,1,20)
    freq, bins = np.histogram(iou_list,bins)
    print(freq)

feature_params = dict( maxCorners = 100,
                       qualityLevel = 0.3,
                       minDistance = 7,
                       blockSize = 7 )

# Parameters for lucas kanade optical flow
lk_params = dict( winSize  = (15,15),
                  maxLevel = 2,
                  criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

"""
Read the ground truth.

"""

file = open('3/groundtruth_rect.txt', 'r')
# print(file.readline())
labels = file.readlines()
label = []
for l in labels:
    l = l.split(",")
    x = int(l[0])
    y = int(l[1])
    width = int(l[2])
    height = int(l[3])
    label.append([x,y,x+width,y+height])

"""
Read the images
"""

images = []
for filename in glob.iglob('3/**/*.jpg' , recursive = True):
    img = cv2.imread(filename)
    images.append(img)

"""
Take the images in sequence

"""

old_frame = images[0]
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
# p0 = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params)
p0 = cv2.cornerHarris(old_gray,blockSize = 1, ksize = 3, k = 0.02)

mask = np.zeros_like(old_frame)
X1 = label[0][0]
Y1 = label[0][1]
X2 = label[0][2]
Y2 = label[0][3]
pt1 = (X1,Y1)
pt2 = (X2,Y2)

(a,b) = np.where(p0)

p0 = np.stack((a, b), axis=-1)
print(type(p0))

p0 = p0.reshape(-1,1,2)
p0 =  np.array(p0)
p0 = p0.astype(np.float32)
prediction = []

for img in images:
    frame_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)

    # Compute the previous and new points
    good_new = p1[st==1]
    good_old = p0[st==1]

    # Application of affine transformation
    gn = np.float32(good_new)[0:3]
    go = np.float32(good_old)[0:3]

    M = cv2.getAffineTransform(go,gn)

    pt1 = np.concatenate((pt1,[1]))
    pt2 = np.concatenate((pt2,[1]))
    new_pt1 = M.dot(pt1)
    new_pt2 = M.dot(pt2)

    img = cv2.rectangle(img,(np.int32(new_pt1[0]),np.int32(new_pt1[1])),(np.int32(new_pt2[0]),np.int32(new_pt2[1])),(0,255,0),50)
    # cv2.imshow('frame',img)
    # cv2.waitKey(0)
    k=0
    if k == 27:
        break
    old_gray = frame_gray.copy()
    p0 = good_new.reshape(-1,1,2)
    pt1 = new_pt1
    pt2 = new_pt2
    prediction.append(np.concatenate((new_pt1,new_pt2)))

cv2.destroyAllWindows()

calculate_t(label,prediction)


import socket
import base64
import zipfile
import os

from p2p.connection_manager_4edge import ConnectionManager4Edge
from p2p.my_protocol_message_handler import MyProtocolMessageHandler
from p2p.message_manager import (
    MessageManager,
    RSP_FULL_CHAIN,
    MSG_ENHANCED,
)
from signature.generate_sigunature import DigitalSignature
STATE_INIT = 0
STATE_ACTIVE = 1
STATE_SHUTTING_DOWN = 2

dirname = os.path.dirname(__file__).replace("core", "")

class ClientCore:

    def __init__(self, my_port=50082, core_host=None, core_port=None):
        self.client_state = STATE_INIT
        print('Initializing ClientCore...')
        self.my_ip = self.__get_myip()
        print('Server IP address is set to ... ', self.my_ip)
        self.my_port = my_port
        self.my_core_host = core_host
        self.my_core_port = core_port
        self.cm = ConnectionManager4Edge(self.my_ip, self.my_port, core_host, core_port, self.__handle_message)
        self.mpm = MyProtocolMessageHandler()
        self.gs = DigitalSignature()
        self.my_protocol_message_store = []

    def start(self):
        self.client_state = STATE_ACTIVE
        self.cm.start()
        self.cm.connect_to_core_node()

    def shutdown(self):
        self.client_state = STATE_SHUTTING_DOWN
        print('Shutdown edge node ...')
        self.cm.connection_close()

    def get_my_current_state(self):
        return self.client_state

    def send_message_to_my_core_node(self, msg_type, msg):
        print("dirname:", dirname)
        msg_pub = self.gs.add_public_key(msg)
        print("msg_pub:", msg_pub)

        #txt化
        file_name = "msg_pub"
        f = open(dirname+"ZIP_busdata/send_busdata/" + str(file_name) + ".txt", 'w')
        f.write(msg_pub)
        f.close()
        print("txt")
        #zipを作成
        with zipfile.ZipFile(dirname+"ZIP_busdata/send_busdata/"+str(file_name) + ".zip" , "w", zipfile.ZIP_DEFLATED) as zf:
            zf.write(dirname+"ZIP_busdata/send_busdata/" + str(file_name) + ".txt", str(file_name) + ".txt")
        print("zip")
        #file_open

        f = open(dirname+"ZIP_busdata/send_busdata/"+str(file_name) + ".zip","rb")
        readdata = f.read()

        d = {}
        d["bin"] = base64.b64encode(readdata).decode("utf-8") #バイナリ化
        f.close
        new_msg = d

        msg_txt = self.cm.get_message_text(msg_type, new_msg)

        self.cm.send_msg((self.my_core_host, self.my_core_port), msg_txt)
        print("============ send_msg =============================")

    def __client_api(self, request, message):

        if request == 'pass_message_to_client_application':
            self.my_protocol_message_store.append(message)
        elif request == 'api_type':
            return 'client_core_api'
        else:
            print('not implemented api was used')

    def get_my_protocol_messages(self):

        if self.my_protocol_message_store != []:
            return self.my_protocol_message_store
        else:
            return None

    def __handle_message(self, msg):
        if msg[2] == RSP_FULL_CHAIN:
            # TODO: ブロックチェーン送信要求に応じて返却されたブロックチェーンを検証する処理を呼び出す
            pass
        elif msg[2] == MSG_ENHANCED:
            # P2P Network を単なるトランスポートして使っているアプリケーションが独自拡張したメッセージはここで処理する。SimpleBitcoin としてはこの種別は使わない
            # self.mpm.handle_message(msg[4], self.__client_api, True)
            print("MSE_ENHANCED")

    def __get_myip(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        return s.getsockname()[0]


from django.db import transaction

class TransactionMiddleware(object):
    """
    Transaction middleware. If this is enabled, each view function will be run
    with commit_on_response activated - that way a save() doesn't do a direct
    commit, the commit is done when a successful response is created. If an
    exception happens, the database is rolled back.
    """
    def process_request(self, request):
        """Enters transaction management"""
        transaction.enter_transaction_management()
        transaction.managed(True)

    def process_exception(self, request, exception):
        """Rolls back the database and leaves transaction management"""
        if transaction.is_dirty():
            transaction.rollback()
        transaction.leave_transaction_management()

    def process_response(self, request, response):
        """Commits and leaves transaction management."""
        if transaction.is_managed():
            if transaction.is_dirty():
                transaction.commit()
            transaction.leave_transaction_management()
        return response


from collections import namedtuple
import subprocess

#given a diff output (list of lines), return the changed files and changed lines
def get_diff_detail(diff):
    result = namedtuple('result','oldlines newlines')
    filelines = {}
    curfile = ''
    oldline = []
    newline = []
    flag = False
    for line in diff:
        if line.startswith('--- a/'):
            #store the previous file & reset the curfile and curline
            if len(curfile) > 0:
                filelines[curfile] = result(oldline,newline)
            curfile = line[6:]
            oldline = []
            newline = []
        if line.startswith('--- /dev/null'):
            flag = True
        if flag and line.startswith('+++ b/'):
            if len(curfile) > 0:
                filelines[curfile] = result(oldline,newline)
            curfile = line[6:]
            oldline = []
            newline = []
            flag = False
        if line.startswith('@@ '):
            lines = get_line_number(line)
            #oldline/newline might be empty (), but we don't check this to ensure they are in pairs
            oldline.append(lines.oldline)
            newline.append(lines.newline)
    #the last file
    filelines[curfile] = result(oldline,newline)
    return filelines

#given a line starts with @@, get its diff line numbers
def get_line_number(line):
        rawline = ''
        if line.find(' @@ ') > -1:
            rawline = line[3:line.find(' @@ ')]
        else:
            rawline = line[3:line.find(' @@')]
        half1 = rawline[1:rawline.find(' +')]
        half2 = rawline[rawline.find(' +')+2:]

        #tuple (start-line, end-line)
        oldline = ()
        if ',' in half1:
            count = int(half1[half1.find(',')+1:])
            if not count == 0:
                startline = int(half1[:half1.find(',')])
                oldline = (startline, startline + count -1)
        else:
            oldline = (int(half1), int(half1))

        newline = ()
        if ',' in half2:
            count = int(half2[half2.find(',')+1:])
            if not count == 0:
                startline = int(half2[:half2.find(',')])
                newline = (startline,startline + count -1)
        else:
            newline = (int(half2), int(half2))

        result = namedtuple("result", "oldline newline")
        return result(oldline, newline)

#get all the changed lines in the given file in the given commit
def get_file_change_lines(commit_hash, file, context):
    #list of range tuple (startline, endline)
    old_line_range = []
    new_line_range = []
    #TODO not sure we should switch branch here (maybe not, cuz now it's in the right branch). Also this call should belong to crawler instead of util
    cmd = 'git show -w --unified=' + str(context) + ' --pretty=format:b% ' + commit_hash + ' -- ' + file
    output = subprocess.check_output(cmd, shell=True)
    diff = []
    try:
        diff = output.decode().split('\n')
    except:
        pass
    for line in diff:
        if line.startswith('@@ '):
            lines = get_line_number(line)
            old_line_range.append(lines.oldline)
            new_line_range.append(lines.newline)

    result = namedtuple('result', 'oldlines newlines')
    return result(old_line_range,new_line_range)

#if the target lines are changed in the given commit/diff
def is_overlap(target_line, diff):
    # the target lines are the same as the oldlines in text diff
    for l in diff.oldlines:
        if len(l) == 0:
            continue
        for lc in target_line:
            if l[0] <= lc <= l[1]:
                return True
    return False

#check if target lines are changed in the given commit; also, return its corresponding lines after this commit
def get_lines_after_commit(target_lines, diff):
    hunkcount = len(diff.oldlines)
    for i in range(hunkcount):
        oldline = diff.oldlines[i]
        newline = diff.newlines[i]
        # only new lines are added
        if len(oldline) == 0:
            target_lines = refresh_target_lines(newline[0], newline[1]-newline[0]+1, target_lines, 'case1')
        # only lines are deleted
        elif len(newline) == 0:
            target_lines = refresh_target_lines(oldline[0], oldline[1]-oldline[0]+1, target_lines, 'case2')
        else:
            # update the subsequent target lines (if oldrange = newrange, then no impact)
            oldrange = oldline[1] - oldline[0] + 1
            newrange = newline[1] - newline[0] + 1
            if newrange > oldrange:
                # equal to from old endline+1 add the differnce of the range
                target_lines = refresh_target_lines(newline[0] + oldrange, newrange - oldrange, target_lines, 'case1')
            elif oldrange > newrange:
                # equal to from new endline+1 delete the difference of the range
                target_lines = refresh_target_lines(newline[1]+1, oldrange - newrange, target_lines, 'case2')

    return target_lines

def refresh_target_lines(start, range, target_lines, case):
    new_target_lines = []
    if case == "case1":
        for l in target_lines:
            # all lines after increase by range
            if l >= start:
                new_target_lines.append(l+range)
            else:
                new_target_lines.append(l)
    if case == "case2":
        for l in target_lines:
            if l > start + range - 1:
                new_target_lines.append(l-range)
            elif l < start:
                new_target_lines.append(l)
            # if l inbetween, then it's simply deleted

    return new_target_lines

def flatten(lines):
    flat_lines = []
    for l in lines:
        if len(l) == 0:
            continue
        flat_lines = flat_lines + [i for i in range(l[0],l[1]+1)]
    return flat_lines

def write_to_file(file, content):
    f = open(file, 'w')
    f.write(content)
    f.close()