import re
import json
import os
import csv
import pandas

def get_files_in_dir(basepath, ext=[], debug=False):
    basepath = os.path.abspath(basepath)

    ext = [str.lower(x) for x in ext]

    try:
        if os.path.exists(basepath) and os.path.isdir(basepath):
            result = [os.path.join(basepath, x) for x in os.listdir(basepath)]
            files = [[os.path.split(x)[0], *os.path.splitext(os.path.basename(x))] for x in result if os.path.isfile(x)]
            if len(ext) == 0:
                # print('无类型筛选,返回所有文件')
                if debug: print(files)
                return files

            if len(ext) > 0:
                # print('返回{}类型的文件'.format(ext))
                files = [[os.path.split(x)[0], *os.path.splitext(os.path.basename(x))] for x in result if
                         str.lower(re.split(r'\.', x)[-1]) in ext]
                if debug: print(files)
                return files
        else:
            return False
    except Exception as e:
        print('发生错误{}'.format(e))

def read_file(filepath, codec='utf-8'):
    with open(filepath, 'r', encoding='utf-8') as f:
        s = f.read()
    return s

def write_to_json(filepath, json_str):
    with open(filepath, 'w') as json_file:
        json.dump(json_str, json_file, indent=4)

def write_csv(filepath, line: list,keys:list):
    if os.path.exists(filepath):
        with open(filepath, 'a', newline='', encoding='utf-8-sig') as csv_file:
            data = csv.writer(csv_file, delimiter=',')
            data.writerow(line)
    else:
        with open(filepath, 'w', newline='', encoding='utf-8-sig') as csv_file:
            data = csv.writer(csv_file, delimiter=',')
            data.writerow(keys)
            data.writerow(line)

def save_json(filepath, data):
    with open(filepath, 'w', encoding='utf-8') as f:
        f.write(json.dumps(data, indent=4, ensure_ascii=False))

def read_txt(filepath):
    with open(filepath, 'r', encoding='utf-8-') as f:
        return f.readlines()

def write_txt(filepath, txt):
    with open(filepath, 'a', encoding='utf-8') as f:
        f.write(txt)

def load_json(filepath):
    with open(filepath, 'r', encoding='utf-8') as f:
        return json.load(f)

def clean_char_in_filename(s):
    return re.sub('[?、_()\\\/*、<>|]', '-', s)

def parse(filepath,no):

    data=load_json(filepath)

    keys=['name', 'id', 'type','citycode', 'start_stop', 'end_stop', 'start_time', 'end_time' , 'status', 'direc', 'company', 'distance', 'basic_price', 'total_price', 'busstops']

    bus_stops=[]

    bus_type=data['type']
    print(bus_type)

    filename=None
    if data['name'][0] in ['K','k'] and filename==None:
        filename = 'K'
    elif  bus_type == [] and filename == None:
        filename='普通公交'
    elif '地铁' in bus_type and filename == None:
        filename='地铁'
    elif '有轨电车' in bus_type and filename == None:
        filename='有轨电车'
    else:
        filename='普通公交'

    bus_type=str(bus_type)

    businfo=[]
    for key in keys[:-1]:
        value=data[key]
        if not value:
            value=''
        if key=='type':
            value=bus_type
        businfo.append(value)

    count=0
    busstops=[]
    for index in  range(0,len(data['busstops'])):
        busstop=data['busstops'][index]
        id=busstop['id']
        location=busstop['location']
        name=busstop['name']
        if index==0:
            distance=0
            duration=0
            p2p_distance=0
        else:
            search_id=str(data['busstops'][index-1]['id']),data['busstops'][index-1]['name'],str(id),name

            if search_id in distances.keys():
                distance,duration,p2p_distance=distances[search_id]
            else:
                print([str(data['busstops'][index - 1]['id']), data['busstops'][index - 1]['location'],
                       data['busstops'][index - 1]['name'], str(id), location, name])

                write_csv('站与站含距离.csv',[str(data['busstops'][index-1]['id']),data['busstops'][index-1]['location'],data['busstops'][index-1]['name'],str(id),location,name],[])

                distance,duration,p2p_distance='无数据','无数据','无数据'

        if id in no:
            write_txt('删除的部分车站.txt',data['name']+'--'+str(index)+'--'+name+'\n')
            # print(filepath,index,name)
            count+=1
            continue

        # record.append(name+f'({location})'+f'({distance } M)'+f'({duration} S)'+f'({p2p_distance} M)')
        # record.append(name + f'\n({location})' + f'\n({distance} M)' + f'\n({duration} S)' + f'\n({p2p_distance} M)')
        busstops.append([name ,f'({location})' , f'({distance} M)' , f'({duration} S)' , f'({p2p_distance} M)'])
        # bus_stops.append([str(id),str(location),str(name)])

    # if count==len(data['busstops']):
    #     print(f'丢弃{filepath}')
    #     write_txt('不符合条件的路线.txt',filepath+'\n')
    #     return False

    # bus_stop_to_stop=[]

    # for index in  range(1,len(data['busstops'])):
    #     start=[str(data['busstops'][index-1]['id']),
    #     str(data['busstops'][index-1]['location']),
    #     str(data['busstops'][index-1]['name'])]
    #
    #     stop=[str(data['busstops'][index]['id']),
    #     str(data['busstops'][index]['location']),
    #     str(data['busstops'][index]['name'])]
    #
    #     bus_stop_to_stop.append(*[start+stop])

    return filename,keys,businfo,busstops#bus_stops,bus_stop_to_stop

def check_dir(d):
    d = os.path.abspath(d)
    if not os.path.exists(d):
        os.makedirs(d)
    return d

sourcedir = check_dir('./result')
outputdir = check_dir('./output')
all_stops=[]
all_stop_to_stop=[]

no=pandas.read_csv('不要的车站.csv',encoding='utf-8-sig')
no=list(no['id'])
# print(no)
# no=[]

distances={}
distance_df=pandas.read_csv('站与站含距离-完整.csv',encoding='utf-8-sig')
distance_df.fillna('0',inplace=True)
for index,record in distance_df.iterrows():
    sid=record['sid']
    slocation=record['slocation']
    sname=record['sname']
    eid=record['eid']
    elocation=record['elocation']
    ename=record['ename']
    distance=record['distance']
    duration=record['duration']
    p2p_distance=record['p2p_distance']
    distances[sid,sname,eid,ename]=[distance,duration,p2p_distance]
#
# print(distances)

for basepath, filename, extname in get_files_in_dir(sourcedir, ['json']):
    # print(basepath, filename, extname)
    source_file_path = os.path.join(basepath, filename + extname)
    busline=parse(source_file_path,no)
    if busline:
        name,keys,businfo,busstops=busline
        output_all_file_path = os.path.join(outputdir, name + '_all.csv')
        output_bus_distance_file_path = os.path.join(outputdir, name + '_bus_distance.csv')
        output_bus_duration_file_path = os.path.join(outputdir, name + '_bus_duration.csv')
        output_linear_distance_file_path = os.path.join(outputdir, name + '_linear_distance.csv')

        # for i in busstops:
        #     print(i)

        a=['\n'.join(x) for x in busstops]
        write_csv(output_all_file_path,businfo+a,keys)

        b = [busstop[0]+'\n'+busstop[1] +'\n'+busstop[2]  for busstop in busstops]
        write_csv(output_bus_distance_file_path, businfo+b, keys)

        c = [busstop[0] + '\n' + busstop[1]  + '\n' + busstop[3]  for busstop in busstops]
        write_csv(output_bus_duration_file_path, businfo+c, keys)

        d = [busstop[0] + '\n' + busstop[1]  + '\n' + busstop[4]  for busstop in busstops]
        write_csv(output_linear_distance_file_path, businfo+d, keys)

#         all_stops+=stops
#         all_stop_to_stop+=stop_to_stop
#
#
# all_stops=pandas.DataFrame(all_stops)
# all_stops.columns=['id','location','name']
# # all_stops.drop_duplicates(inplace=True)
# all_stops.drop_duplicates(subset=['id','name'],inplace=True)
#
# all_stop_to_stop=pandas.DataFrame(all_stop_to_stop)
# all_stop_to_stop.columns=['sid','slocation','sname','eid','elocation','ename']
# all_stop_to_stop.insert(6,column='distance',value=None)
# all_stop_to_stop.insert(7,column='duration',value=None)
# # all_stop_to_stop.drop_duplicates(inplace=True)
# all_stop_to_stop.drop_duplicates(subset=['sid','sname','eid','ename'],inplace=True)
#
# # all_stops.to_csv('全部车站.csv',index=None,header=True,encoding='utf-8-sig')
# all_stop_to_stop.to_csv('站与站.csv',index=None,header=True,encoding='utf-8-sig')

爬虫/Ahhhhhh-高德地图api爬取城市公交信息/3.根据json统计出路线和车站.py

3.根据json统计出路线和车站.py


"""openapi"""
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi

from stac_api.config import ApiExtensions, ApiSettings

def config_openapi(app: FastAPI, settings: ApiSettings):
    """config openapi"""

    def custom_openapi():
        """config openapi"""
        if app.openapi_schema:
            return app.openapi_schema

        openapi_schema = get_openapi(
            title="Sherlock STAC API", version="0.1", routes=app.routes
        )

        if settings.api_extension_is_enabled(ApiExtensions.fields):
            openapi_schema["paths"]["/search"]["get"]["responses"]["200"]["content"][
                "application/json"
            ]["schema"] = {"$ref": "#/components/schemas/ItemCollection"}
            openapi_schema["paths"]["/search"]["post"]["responses"]["200"]["content"][
                "application/json"
            ]["schema"] = {"$ref": "#/components/schemas/ItemCollection"}

        app.openapi_schema = openapi_schema
        return app.openapi_schema

    app.openapi = custom_openapi


# -*- coding: utf-8 -*-
from django.conf.urls import patterns, include, url

urlpatterns = patterns('djack',
    url(r'^create/$', 'views.create'),
    url(r'^$', 'views.list_tweets'),
    url(r'^(?P<tweet_id>\d+)/?$', 'views.detail_tweet'),
    url(r'^like/(?P<tweet_id>\d+)/?', 'views.like_process'),
    url(r'^bye/', 'views.bye_user'),
    url(r'^likers/(?P<tweet_id>\d+)/?', 'views.likers'),
    url(r'^(?P<tweet_id>\d+)/comment/?', 'views.create_comment'),
)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import accounts.models

class Migration(migrations.Migration):

    dependencies = [
        ('accounts', '0004_remove_userdetail_birthday'),
    ]

    operations = [
        migrations.AddField(
            model_name='userdetail',
            name='profile_image',
            field=models.ImageField(null=True, blank=True, upload_to=accounts.models.get_image_path),
            preserve_default=True,
        ),
    ]


#!/usr/bin/env python
import tkinter as tk
from PIL import Image, ImageTk
import os
import pyocr
import pyocr.builders
import myougiden_api
import threading
tool = pyocr.get_available_tools()[0]

def best_fit(width, height, image):
	(x, y) = image.size
	scale = width / x
	if y * scale > height:
		scale = height / y
	#print(scale)
	return image.resize((int(x * scale), int(y * scale)), Image.BILINEAR)

class LookupThread(threading.Thread):
	def run(self):
		self.dirty = False
		image, app = (self._args)
		try:
			string = app.image_to_dict(image)
			if not self.dirty:
				app.draw_dict(string)

		finally:
			# Avoid a refcycle if the thread is running a function with
			# an argument that has a member that points to the thread.
			del self._target, self._args, self._kwargs

class Application(tk.Frame):
	def __init__(self, master=None):
		tk.Frame.__init__(self, master)
		if os.path.isfile("last_page"):
			last_page = open("last_page", "r")
			try:
				self.current_page = int(last_page.read())
			except:
				self.current_page = 0
			last_page.close()
		else:
			self.current_page = 0
		self.pack(fill=tk.BOTH, expand=1)
		self.createWidgets()
		self.current_page_oid = 0
		self.current_page_image = None
		self.drawing_box = False
		self.box_oid = 0
		self.box_coords = (0, 0, 0, 0)
		self.lookup = None
		self.tkimage = None
		self.rotation = 0
		self.fullscreen = False

	def createWidgets(self):
		#self.quitButton = tk.Button(self, text='Quit',
		#	command=self.quit)
		#self.quitButton.grid()
		self.update()
		(width, height) = (self.winfo_width(), self.winfo_height())
		self.frame = tk.Canvas(self, width=width, height=height, cursor="tcross")
		self.frame.pack(fill=tk.BOTH)
		self.frame.bind('<Left>', self.next_image)
		self.frame.bind('<Right>', self.prev_image)
		#self.frame.bind('<r>', self.rotate)
		self.frame.bind('<Configure>', self.resize_event)
		self.frame.focus_set()
		self.frame.bind('<Button-1>', self.start_drawing_box)
		self.frame.bind('<ButtonRelease-1>', self.stop_drawing_box)
		self.frame.bind('<Double-Button-1>', self.clear_box)
		self.frame.bind('<Button-2>', self.side_tap)
		self.frame.bind('<Motion>', self.draw_box)
		self.frame.bind('<F11>', self.toggle_fullscreen)

	def toggle_fullscreen(self, event=None):
		self.fullscreen = not self.fullscreen  # Just toggling the boolean
		self.master.attributes("-fullscreen", self.fullscreen)
		self.update_screen()

	def side_tap(self, event):
		if event.x < (self.frame.winfo_width() / 2):
			self.change_image(1)
		else:
			self.change_image(-1)

	def resize_event(self, event):
		self.frame.width = event.width   #>>>854
		self.frame.height = event.height #>>>404
		self.frame.config(width=self.frame.width, height=self.frame.height)
		self.update_screen()

	def update_screen(self):
		self.change_image(0)

	def rotate(self, event):
		self.rotation = (self.rotation + 1) % 4
		self.update_screen()

	def clear_box(self, event):
		self.drawing_box = False
		if self.box_oid != 0:
			self.frame.delete(self.box_oid)
		self.frame.delete("text")
		self.frame.delete("selection")
	def start_drawing_box(self, event):
		textbox = self.frame.bbox("text")
		selectionbox = self.frame.bbox(self.box_oid)
		for bbox in textbox, selectionbox:
			if bbox is not None:
				(x, y, x2, y2) = bbox
				mx = event.x
				my = event.y
				if mx > x and mx < x2 and my > y and my < y2:
					self.clear_box(None)
					return

		self.drawing_box = True
		x = event.x
		y = event.y
		self.box_coords = (x, y, x, y)
		if self.box_oid != 0:
			self.frame.delete(self.box_oid)
		self.box_oid = self.frame.create_rectangle(x, y, x, y, fill="black", stipple="gray50")
		self.frame.addtag_withtag("selection", self.box_oid)

	def stop_drawing_box(self, event):
		try:
			self.drawing_box = False
			(ix, iy, ix2, iy2) = self.frame.bbox(self.current_page_oid)
			(bx, by, bx2, by2) = self.frame.bbox(self.box_oid)
			px = (bx - ix) / (ix2 - ix)
			py = (by - iy) / (iy2 - iy)
			px2 = (bx2 - ix) / (ix2 - ix)
			py2 = (by2 - iy)/ (iy2 - iy)
			#print("%f, %f, %f, %f" % (px, py, px2, py2))

			(width, height) = self.current_page_image.size
			cx = int(px * width)
			cx2 = int(px2 * width)
			cy = int(py * height)
			cy2 = int(py2 * height)
			#print("%d, %d, %d, %d" % (cx, cy, cx2, cy2))
			ocr_image = self.current_page_image.crop((cx, cy, cx2, cy2))
			#draw = ImageDraw.Draw(self.current_page_image)
			#draw.rectangle([cx, cy, cx2, cy2], outline="black")
			#ocr_image = image
			if self.lookup is not None:
				self.lookup.dirty = True
			self.lookup = LookupThread(args=(ocr_image,self))
			self.lookup.start()
			#self.image_to_dict(ocr_image)
		except:
			pass

	def draw_box(self, event):
		if not self.drawing_box:
			return
		(x, y, x2, y2) = self.box_coords
		x2 = event.x
		y2 = event.y
		self.box_coords = (x, y, x2, y2)
		self.frame.delete(self.box_oid)
		self.box_oid = self.frame.create_rectangle(x, y, x2, y2, outline="#00AA00", fill="#00AA00", stipple="gray50")

	def change_image(self, amount):
		self.clear_box(None)
		new_page = self.current_page + amount
		if new_page < 0 or new_page > len(images) - 2:
			return
		self.current_page = new_page
		image = Image.open(images[self.current_page])
		if self.rotation != 0:
			image = image.rotate(-90 * self.rotation)
		(width, height) = (self.frame.winfo_width(), self.frame.winfo_height())
		image = best_fit(width, height, image)
		self.tkimage = ImageTk.PhotoImage(image)
		self.frame.delete(self.current_page_oid)
		self.current_page_oid = self.frame.create_image(int(width/2), 0, image=self.tkimage, anchor=tk.N)
		self.current_page_image = image
		last_page = open("last_page", "w")
		last_page.write(str(self.current_page))
		last_page.close()

	def prev_image(self, event):
		self.change_image(-1)

	def next_image(self, event):
		self.change_image(1)

	def image_to_dict(self, image):
		size = image.size
		image = image.resize((size[0] * 2, size[1] * 2), Image.BILINEAR)
		string = tool.image_to_string(image, lang="jpn", builder=pyocr.builders.TextBuilder(5))
		string = string.strip()
		if string != "":
			dict_entry = myougiden_api.run([string])
		else:
			dict_entry = None
			string = "No character recognized"
		#image.save("/tmp/export.png")
		if dict_entry is not None and string != "No character recognized":
			string = "\n".join(dict_entry)
		else:
			dict_entry = myougiden_api.run(string[:-1])
			dict_entry2 = myougiden_api.run(string[1:])
			dict_entry3 = myougiden_api.run(string[1:])
			result = ""
			result2 = ""
			result3 = ""
			if dict_entry is not None:
				result = "\n".join(dict_entry)
			if dict_entry2 is not None:
				result2 = "\n".join(dict_entry2)
			if dict_entry2 is not None:
				result3 = "\n".join(dict_entry3)
			string = result + result2 + result3
		print(string)
		return string

	def draw_dict(self, string):
		self.text = self.frame.create_text(5, 5, width=500, anchor=tk.NW, text=string)
		self.frame.addtag_withtag("text", self.text)
		(x, y, x2, y2) = self.frame.bbox(self.text)
		self.textbox = self.frame.create_rectangle(x, y, x2, y2, fill="white", outline="white")
		self.frame.addtag_withtag("text", self.textbox)
		self.frame.tag_lower(self.textbox, self.text)

directory = '/home/klaxa/Images/manga/'

images = sorted([os.path.join(directory, filename) for filename in os.listdir('/home/klaxa/Images/manga/')])
app = Application()
app.master.title('Yurimon reader')
app.update_screen()
app.mainloop()


"""Support for the Vallox ventilation unit fan."""
from __future__ import annotations

from collections.abc import Mapping
import logging
from typing import Any, NamedTuple

from vallox_websocket_api import Vallox
from vallox_websocket_api.exceptions import ValloxApiException

from homeassistant.components.fan import (
    FanEntity,
    FanEntityFeature,
    NotValidPresetModeError,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType

from . import ValloxDataUpdateCoordinator, ValloxEntity
from .const import (
    DOMAIN,
    METRIC_KEY_MODE,
    METRIC_KEY_PROFILE_FAN_SPEED_AWAY,
    METRIC_KEY_PROFILE_FAN_SPEED_BOOST,
    METRIC_KEY_PROFILE_FAN_SPEED_HOME,
    MODE_OFF,
    MODE_ON,
    STR_TO_VALLOX_PROFILE_SETTABLE,
    VALLOX_PROFILE_TO_STR_SETTABLE,
)

_LOGGER = logging.getLogger(__name__)

class ExtraStateAttributeDetails(NamedTuple):
    """Extra state attribute details."""

    description: str
    metric_key: str

EXTRA_STATE_ATTRIBUTES = (
    ExtraStateAttributeDetails(
        description="fan_speed_home", metric_key=METRIC_KEY_PROFILE_FAN_SPEED_HOME
    ),
    ExtraStateAttributeDetails(
        description="fan_speed_away", metric_key=METRIC_KEY_PROFILE_FAN_SPEED_AWAY
    ),
    ExtraStateAttributeDetails(
        description="fan_speed_boost", metric_key=METRIC_KEY_PROFILE_FAN_SPEED_BOOST
    ),
)

def _convert_fan_speed_value(value: StateType) -> int | None:
    if isinstance(value, (int, float)):
        return int(value)

    return None

async def async_setup_entry(
    hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
    """Set up the fan device."""
    data = hass.data[DOMAIN][entry.entry_id]

    client = data["client"]
    client.set_settable_address(METRIC_KEY_MODE, int)

    device = ValloxFanEntity(
        data["name"],
        client,
        data["coordinator"],
    )

    async_add_entities([device])

class ValloxFanEntity(ValloxEntity, FanEntity):
    """Representation of the fan."""

    _attr_supported_features = FanEntityFeature.PRESET_MODE
    _attr_has_entity_name = True

    def __init__(
        self,
        name: str,
        client: Vallox,
        coordinator: ValloxDataUpdateCoordinator,
    ) -> None:
        """Initialize the fan."""
        super().__init__(name, coordinator)

        self._client = client

        self._attr_unique_id = str(self._device_uuid)

    @property
    def preset_modes(self) -> list[str]:
        """Return a list of available preset modes."""
        # Use the Vallox profile names for the preset names.
        return list(STR_TO_VALLOX_PROFILE_SETTABLE.keys())

    @property
    def is_on(self) -> bool:
        """Return if device is on."""
        return self.coordinator.data.get_metric(METRIC_KEY_MODE) == MODE_ON

    @property
    def preset_mode(self) -> str | None:
        """Return the current preset mode."""
        vallox_profile = self.coordinator.data.profile
        return VALLOX_PROFILE_TO_STR_SETTABLE.get(vallox_profile)

    @property
    def extra_state_attributes(self) -> Mapping[str, int | None]:
        """Return device specific state attributes."""
        data = self.coordinator.data

        return {
            attr.description: _convert_fan_speed_value(data.get_metric(attr.metric_key))
            for attr in EXTRA_STATE_ATTRIBUTES
        }

    async def _async_set_preset_mode_internal(self, preset_mode: str) -> bool:
        """
        Set new preset mode.

        Returns true if the mode has been changed, false otherwise.
        """
        try:
            self._valid_preset_mode_or_raise(preset_mode)

        except NotValidPresetModeError as err:
            _LOGGER.error(err)
            return False

        if preset_mode == self.preset_mode:
            return False

        try:
            await self._client.set_profile(STR_TO_VALLOX_PROFILE_SETTABLE[preset_mode])

        except (OSError, ValloxApiException) as err:
            _LOGGER.error("Error setting preset: %s", err)
            return False

        return True

    async def async_set_preset_mode(self, preset_mode: str) -> None:
        """Set new preset mode."""
        update_needed = await self._async_set_preset_mode_internal(preset_mode)

        if update_needed:
            # This state change affects other entities like sensors. Force an immediate update that
            # can be observed by all parties involved.
            await self.coordinator.async_request_refresh()

    async def async_turn_on(
        self,
        percentage: int | None = None,
        preset_mode: str | None = None,
        **kwargs: Any,
    ) -> None:
        """Turn the device on."""
        _LOGGER.debug("Turn on")

        update_needed = False

        if preset_mode:
            update_needed = await self._async_set_preset_mode_internal(preset_mode)

        if not self.is_on:
            try:
                await self._client.set_values({METRIC_KEY_MODE: MODE_ON})

            except OSError as err:
                _LOGGER.error("Error turning on: %s", err)

            else:
                update_needed = True

        if update_needed:
            # This state change affects other entities like sensors. Force an immediate update that
            # can be observed by all parties involved.
            await self.coordinator.async_request_refresh()

    async def async_turn_off(self, **kwargs: Any) -> None:
        """Turn the device off."""
        if not self.is_on:
            return

        try:
            await self._client.set_values({METRIC_KEY_MODE: MODE_OFF})

        except OSError as err:
            _LOGGER.error("Error turning off: %s", err)
            return

        # Same as for turn_on method.
        await self.coordinator.async_request_refresh()


""" Find, detect, and make available timeseries drivers implementations

Timeseries drivers must inherit from the Abstract Base Class
"AbstractTimeSeriesDriver" to be detected.
"""
import importlib
import os
import pkgutil
import sys

from ..logger import logger

class BrokenModule(object):
    """ Timeseries driver module "%s" is broken.

    Reason:
    %s

    Please see TSTools Github repository for more information or to report an
    issue:

    TSTools
    """
    def __init__(self, module, message):
        self.__doc__ = self.__doc__ % (module, message)

class TSManager(object):
    """ Timeseries Manager

    Finds and stores references to available timeseries
    """
    # Loaded timeseries
    ts = None

    def __init__(self, location=None):
        # Location of timeseires modules
        self.plugin_dir = []
        # All available timeseries
        self.ts_drivers = []

        if location and os.path.isdir(location):
            self.plugin_dir.append(location)

        file_location = os.path.join(os.path.dirname(__file__), 'drivers')
        self.plugin_dir.append('./' if file_location == '' else file_location)

        self.find_timeseries()

    def find_timeseries(self):
        """ Try to find timeseries classes """
        try:
            from . import timeseries
        except ImportError:
            logger.critical('Could not import "timeseries". Check your path')
            raise
        else:
            logger.debug('Found "timeseries" module')

        broken = []

        # Use pkgutil to search for timeseries
        logger.debug('Module name: {n}'.format(n=__name__))
        for loader, modname, ispkg in pkgutil.walk_packages(self.plugin_dir):
            full_path = '%s.drivers.%s' % (__name__.rsplit('.', 1)[0], modname)
            try:
                importlib.import_module(full_path)
            except ImportError as e:
                logger.error('Cannot import %s: %s' % (modname, e.message))
                broken_module = BrokenModule(modname,
                                             e.args[0] if e.args else
                                             'Unknown import error')
                broken_module.description = 'Broken: %s' % modname
                broken.append(broken_module)
            except:
                logger.error('Cannot import %s: %s' %
                             (modname, sys.exc_info()[0]))
                raise

        self.ts_drivers = timeseries.AbstractTimeSeriesDriver.__subclasses__()
        for tsd in self.ts_drivers:
            logger.info('Found driver: {tsd}'.format(tsd=tsd))

        # Find even more descendents
        for subclass in self.ts_drivers:
            self.recursive_find_subclass(subclass)

        self.ts_drivers.extend(broken)

    def recursive_find_subclass(self, subclass):
        """ Search subclass for descendents """

        sub_subclasses = subclass.__subclasses__()

        for sub_subclass in sub_subclasses:
            if sub_subclass not in self.ts_drivers:
                self.ts_drivers.append(sub_subclass)
                logger.info('Found driver: {tsd}'.format(tsd=sub_subclass))
            self.recursive_find_subclass(sub_subclass)

# Store timeseries manager
tsm = TSManager()
logger.debug('Found {i} TS data models'.format(i=len(tsm.ts_drivers)))


from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader

# Create your views here.
def index1(request):
    alist=[1,2,3]
    template = loader.get_template('home/index.html')
    context = {
        'alist':alist,
    }
    return HttpResponse(template.render(context,request))
def index(request):
    template = loader.get_template('home/index-1.htm')
    context = {
    }
    return HttpResponse(template.render(request))


import tensorflow as tf
import numpy as np
import pickle
import cv2
from PIL import Image
import imutils

class Data(object):
    def __init__(self, data_dir, height, width, batch_size):
        self.data_dir = data_dir
        self.height = height
        self.width = width
        self.batch_size = batch_size

    def get_rot_data_iterator(self, images, labels):
        images = tf.cast(images, dtype=tf.float32)
        dataset = tf.data.Dataset.from_tensor_slices((images, labels))
        dataset = dataset.shuffle(buffer_size=10000, reshuffle_each_iteration=True)
        dataset = dataset.batch(self.batch_size)
        dataset = dataset.repeat()
        #dataset = dataset.map(self.preprocess, num_parallel_calls=2)
        dataset = dataset.prefetch(self.batch_size)
        iterator = dataset.make_initializable_iterator()
        print(tf.compat.v1.data.get_output_shapes(dataset))
        print(tf.compat.v1.data.get_output_types(dataset))
        return iterator

    def get_training_data(self):
        images = []
        labels = []
        for i in range(1, 6):
            data = self._get_next_batch_from_file(i)
            images.extend(list(self.convert_images(data[b"data"])))
            labels.extend(list(data[b"labels"]))
        return np.array(images), tf.keras.utils.to_categorical(np.array(labels))

    def convert_images(self, raw_images):
        images = raw_images / 255.0
        images = raw_images.reshape([-1, 3, self.height, self.width])
        images = images.transpose([0, 2, 3, 1])
        return images

    def _get_next_batch_from_file(self, batch_number):
        data = self._unpickle_data(self.data_dir + self._get_batch_name(batch_number))
        return data

    def _get_batch_name(self, number):
        return "data_batch_{0}".format(number)

    def _unpickle_data(self, filepath):
        with open(filepath, 'rb') as data:
            dict = pickle.load(data, encoding='bytes')
        return dict

    def get_test_data(self):
         data = self._unpickle_data(self.data_dir + "test_batch")
         return data[b"data"], tf.keras.utils.to_categorical(data[b"labels"])

    def preprocess(self, images, labels):
        rot_labels = []
        rot_images = []
        rotations = [90, 180, 270]
        images = tf.cast(images, dtype=tf.float32)
        for image in images:
            rot_labels.append(0)
            rot_images.append(image)
            for i, angle in enumerate(rotations, 1):
                rotated = imutils.rotate_bound(image, angle)
                rot_images.append(rotated)
                rot_labels.append(i)

        return np.array(rot_images), tf.keras.utils.to_categorical(np.array(rot_labels))

    @staticmethod
    def print_image_to_screen(data):
        """
        Used for debugging purposes.
        """
        img = Image.fromarray(data, 'RGB')
        img.show()

    @staticmethod
    def get_image(image_path):
        return

if __name__ == "__main__":
    DATA_DIR = "./data/cifar-10-batches-py/"
    data_obj = Data(DATA_DIR, 32, 32, 5000)
    x, y = data_obj.get_training_data()
    xr, yr = data_obj.preprocess(x, y)


"""Test add_text bot module."""
#
# (C) Pywikibot team, 2016-2021
#
# Distributed under the terms of the MIT license.
#
import unittest

import pywikibot

from scripts.add_text import add_text, get_text

from tests.aspects import TestCase

class TestAdding(TestCase):

    """Test adding text."""

    family = 'wikipedia'
    code = 'en'

    dry = True

    def setUp(self):
        """Setup test."""
        super().setUp()
        self.page = pywikibot.Page(self.site, 'foo')

    def test_basic(self):
        """Test adding text."""
        (_, newtext, _) = add_text(
            self.page, 'bar', putText=False,
            oldTextGiven='foo\n{{linkfa}}')
        self.assertEqual(
            'foo\n{{linkfa}}\nbar',
            newtext)

    def test_with_category(self):
        """Test adding text before categories."""
        (_, newtext, _) = add_text(
            self.page, 'bar', putText=False,
            oldTextGiven='foo\n[[Category:Foo]]')
        self.assertEqual(
            'foo\nbar\n\n[[Category:Foo]]',
            newtext)

    def test_get_text(self):
        """Test get_text with given text."""
        self.assertEqual(get_text(self.page, 'foo', False), 'foo')

if __name__ == '__main__':  # pragma: no cover
    unittest.main()


# encoding: UTF-8
import json
import traceback
from datetime import datetime, date
import pandas as pd
from pandas import DataFrame
from vnpy.app.cta_strategy.backtesting import BacktestingEngine
from vnpy.app.cta_strategy.strategies.boll_channel_strategy import BollChannelStrategy
from vnpy.app.cta_strategy.strategies.turtle_signal_strategy import TurtleSignalStrategy
from vnpy.app.cta_strategy.strategies.double_ma_strategy import DoubleMaStrategy

class BatchCTABackTest:
	"""
	提供批量CTA策略回测，输出结果到excel或pdf，和CTA策略批量优化，输出结果到excel或pdf，
	"""

	def __init__(self, vtSymbolconfig="vtSymbol.json", exportpath=".\\"):
		"""
		加载配置路径
		"""
		config = open(vtSymbolconfig)
		self.setting = json.load(config)
		self.exportpath = exportpath

	def addParameters(self, engine, vt_symbol: str, startDate, endDate, interval="1m", capital=1_000_000):
		"""
		从vtSymbol.json文档读取品种的交易属性，比如费率，交易每跳，比率，滑点
		"""
		if vt_symbol in self.setting:
			engine.set_parameters(
				vt_symbol=vt_symbol,
				interval=interval,
				start=startDate,
				end=endDate,
				rate=self.setting[vt_symbol]["rate"],
				slippage=self.setting[vt_symbol]["slippage"],
				size=self.setting[vt_symbol]["size"],
				pricetick=self.setting[vt_symbol]["pricetick"],
				capital=capital
			)
		else:
			print("symbol %s hasn't be maintained in config file" % vt_symbol)
		return engine

	def runBatchTest(self, strategy_setting, startDate, endDate, portfolio):
		"""
		进行回测
		"""
		resultDf = DataFrame()
		dfportfolio = None
		for strategy_name, strategy_config in strategy_setting.items():
			engine = BacktestingEngine()
			vt_symbol = strategy_config["vt_symbol"]
			engine = self.addParameters(engine, vt_symbol, startDate, endDate)
			if type(strategy_config["setting"]) is str:
				print(strategy_config["setting"])
				engine.add_strategy(
					eval(strategy_config["class_name"]),
					json.loads(strategy_config["setting"], )
				)
			else:
				engine.add_strategy(
					eval(strategy_config["class_name"]),
					strategy_config["setting"]
				)
			engine.load_data()
			engine.run_backtesting()
			df = engine.calculate_result()
			if portfolio == True:
				if dfportfolio is None:
					dfportfolio = df
				else:
					dfportfolio = dfportfolio + df
			resultDict = engine.calculate_statistics(df, False)
			resultDict["class_name"] = strategy_config["class_name"]
			resultDict["setting"] = strategy_config["setting"]
			resultDict["vt_symbol"] = strategy_config["vt_symbol"]
			resultDf = resultDf.append(resultDict, ignore_index=True)

		if portfolio == True:
			# dfportfolio = dfportfolio.dropna()
			engine = BacktestingEngine()
			engine.calculate_statistics(dfportfolio)
			engine.show_chart(dfportfolio)
		return resultDf

	def runBatchTestJson(self, jsonpath="ctaStrategy.json", startDate=datetime(2019, 7, 1),
	                     endDate=datetime(2020, 1, 1), exporpath=None, portfolio=True):
		"""
		从ctaStrategy.json去读交易策略和参数，进行回测
		"""
		with open(jsonpath, mode="r", encoding="UTF-8") as f:
			strategy_setting = json.load(f)
		resultDf = self.runBatchTest(strategy_setting, startDate, endDate, portfolio)
		self.ResultExcel(resultDf, exporpath)
		return strategy_setting

	def runBatchTestExcecl(self, path="ctaStrategy.xls", startDate=datetime(2019, 7, 1),
	                       endDate=datetime(2020, 1, 1), exporpath=None, portfolio=False):
		"""
		从ctaStrategy.excel去读交易策略和参数，进行回测
		"""
		df = pd.read_excel(path)
		strategy_setting = df.to_dict(orient='index')
		resultDf = self.runBatchTest(strategy_setting, startDate, endDate, portfolio)
		self.ResultExcel(resultDf, exporpath)
		return strategy_setting

	def ResultExcel(self, result, export=None):
		"""
		输出交易结果到excel
		"""
		if export != None:
			exportpath = export
		else:
			exportpath = self.exportpath
		try:
			path = exportpath + "CTABatch" + str(date.today()) + "v0.xls"
			result.to_excel(path, index=False)
			print("CTA Batch result is export to %s" % path)
		except:
			print(traceback.format_exc())

		return None

if __name__ == '__main__':
	bts = BatchCTABackTest()
	bts.runBatchTestJson()


from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from django.shortcuts import render, get_list_or_404, get_object_or_404
from .models import Movie, Review, Genre
from django.contrib.auth import get_user_model
import jwt
from django.conf import settings
from .serializers import MovieListSerializer, MovieSerializer, ReviewListSerializer, ReviewSerializer
from rest_framework.decorators import authentication_classes, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework_jwt.authentication import JSONWebTokenAuthentication

# Create your views here.
@api_view(['GET'])
def movies(request):
    movie_list = get_list_or_404(Movie)
    serializer = MovieSerializer(movie_list, many=True)
    return Response(serializer.data)

@api_view(['GET'])
def movie_detail(request, movie_pk):
    movie = get_object_or_404(Movie, pk=movie_pk)
    serializer = MovieSerializer(movie)
    return Response(serializer.data)

@api_view(['GET', 'POST'])
@authentication_classes([JSONWebTokenAuthentication])
@permission_classes([IsAuthenticated])
def reviews(request, movie_pk):
    if request.method == 'GET':
        review_list = Review.objects.all().filter(movie_id=movie_pk)
        serializer = ReviewListSerializer(review_list, many=True)
        return Response(serializer.data)

    else:
        serializer = ReviewListSerializer(data=request.data)
        if serializer.is_valid(raise_exception=True):
            movie = get_object_or_404(Movie, pk=request.data.get('movie'))

            pre_point = movie.vote_average * movie.vote_count

            point = pre_point+int(request.data.get('rank'))
            count = movie.vote_count + 1
            new_vote_average = round(point/count, 2)

            movie.vote_average = new_vote_average
            movie.vote_count = count
            movie.save()
            # print(request.user)
            serializer.save(user=request.user)
            return Response(serializer.data, status=status.HTTP_201_CREATED)

@api_view(['GET'])
@authentication_classes([JSONWebTokenAuthentication])
@permission_classes([IsAuthenticated])
def my_review(request, my_pk):
    if request.method == 'GET':
        review_list = Review.objects.all().filter(user_id=my_pk)
        serializer = ReviewListSerializer(review_list, many=True)
        return Response(serializer.data)

# authentication_classes 붙여줘야함!
@api_view(['PUT', 'DELETE'])
@authentication_classes([JSONWebTokenAuthentication])
@permission_classes([IsAuthenticated])
def review_update_delete(request, review_pk):
  review = get_object_or_404(Review, pk=review_pk)
  if not request.user.reviews.filter(pk=review_pk).exists():
    return Response({'message': '권한이 없습니다.'})

  if request.method == 'PUT':
    serializer = ReviewListSerializer(review, data=request.data)

    # if serializer.is_valid(raise_exception=True):
    if serializer.is_valid(raise_exception=True):
      movie = get_object_or_404(Movie, pk=request.data.get('movie'))
      pre_point = movie.vote_average * (movie.vote_count - 1)
      point = pre_point+int(request.data.get('rank'))
      count = movie.vote_count
      new_vote_average = round(point/count, 2)
      movie.vote_average = new_vote_average
      movie.vote_count = count
      movie.save()
      serializer.save(user=request.user)
      return Response(serializer.data)

  else:
    review = get_object_or_404(Review, pk=review_pk)
    # token = request.headers['Authorization'].split()[1]
    # SECRET_KEY = settings.SECRET_KEY
    # print(token)
    # 디코드하려면 재료 3개
    # payload = jwt.decode(token,SECRET_KEY,algorithms=['HS256'])
    # print(payload['user_id'])
    movie = get_object_or_404(Movie, pk=review.movie_id)
    pre_point = movie.vote_average * (movie.vote_count)
    # pre_count = movie.vote_count
    point = pre_point - review.rank
    count = movie.vote_count-1
    new_vote_average = round(point/count, 2)
    movie.vote_average = new_vote_average
    movie.vote_count = count
    movie.save()
    # print(request.user) 익명으로 안받아와짐 그래서 토큰으로 받아줌
    review.delete()
    return Response({ 'id': review_pk })

@api_view(['POST'])
def recommend(request):
    me_like = request.data.get('me_like')

    # 인기순
    favorite_movies = Movie.objects.all().order_by('-vote_average')[:10]
    favorite_serialize = MovieSerializer(favorite_movies, many=True)
    # 리뷰 기반 장르기반 추천
    user_movies_review = []
    # 배우기반 추천
    user_movies_actor = []
    # 감독기반 추천
    user_movies_director = []
    # 개봉년도
    # 제작 국가
    # 리뷰 기반 장름별
    reviews = Review.objects.all()
    for review in reviews:
        movie = Movie.objects.get(pk=review.movie_id)
        if not movie in user_movies_review:
            user_movies_review.append(movie)

    # 좋아요 기반추천
    my_user_like_movies = []
    user_like_movies = []
    # 좋아요 기반 추천
    like_movies = request.data.get('like_movies')
    for like_movie in like_movies:
        movie = get_object_or_404(Movie, pk=like_movie)
        if not movie in my_user_like_movies:
            my_user_like_movies.append(movie)
    # 내가 좋아요 한 것 제거
    for like_movie in my_user_like_movies:
        # print(me_like)
        if like_movie.id not in me_like:
            user_like_movies.append(like_movie)
    # print(user_like_movies)

    # user_genre_serialize = MovieSerializer(user_movies_review, many=True)
    user_like_serialize = MovieSerializer(user_like_movies, many=True)

    # 연령대
    user_movies_age = []
    # 연령별 기반 추천
    me_age = request.data.get('me_age')
    for age in me_age:
        movie = get_object_or_404(Movie, pk=age)
        if not movie in user_movies_age:
            user_movies_age.append(movie)

    user_movies_age_serializer = MovieSerializer(user_movies_age, many=True)
    return Response([favorite_serialize.data, user_like_serialize.data, user_movies_age_serializer.data])

@api_view(['POST'])
# @authentication_classes([JSONWebTokenAuthentication])
# @permission_classes([IsAuthenticated])
def my_movie_like(request, my_pk):
    me = get_object_or_404(get_user_model(), pk=my_pk)
    # print(me)
    data = []
    movies = request.data
    for movie_pk in movies:
        movie = get_object_or_404(Movie, pk=movie_pk)
        serializer = MovieSerializer(movie)
        data.append(serializer.data)

    return Response(data)

@api_view(['POST'])
def my_movie_dislike(request, my_pk):
    me = get_object_or_404(get_user_model(), pk=my_pk)
    # print(me)
    data = []
    movies = request.data
    for movie_pk in movies:
        movie = get_object_or_404(Movie, pk=movie_pk)
        serializer = MovieSerializer(movie)
        data.append(serializer.data)

    return Response(data)

@api_view(['POST'])
def my_movie_wish(request, my_pk):
    me = get_object_or_404(get_user_model(), pk=my_pk)
    # print(me)
    data = []
    movies = request.data
    for movie_pk in movies:
        movie = get_object_or_404(Movie, pk=movie_pk)
        serializer = MovieSerializer(movie)
        data.append(serializer.data)

    return Response(data)

@api_view(['POST'])
def movie_like(request, my_pk, movie_title):
  movie = get_object_or_404(Movie, title=movie_title)
  me = get_object_or_404(get_user_model(), pk=my_pk)
  if me.like_movies.filter(pk=movie.pk).exists():
      me.like_movies.remove(movie.pk)
      liking = False

  else:
      me.like_movies.add(movie.pk)
      liking = True

  return Response(liking)

@api_view(['POST'])
def movie_dislike(request, my_pk, movie_title):
  movie = get_object_or_404(Movie, title=movie_title)
  me = get_object_or_404(get_user_model(), pk=my_pk)
  if me.dislike_movies.filter(pk=movie.pk).exists():
      me.dislike_movies.remove(movie.pk)
      disliking = False

  else:
      me.dislike_movies.add(movie.pk)
      disliking = True

  return Response(disliking)

@api_view(['POST'])
def movie_wish(request, my_pk, movie_title):
  movie = get_object_or_404(Movie, title=movie_title)
  me = get_object_or_404(get_user_model(), pk=my_pk)
  if me.wish_movies.filter(pk=movie.pk).exists():
      me.wish_movies.remove(movie.pk)
      wishing = False

  else:
      me.wish_movies.add(movie.pk)
      wishing = True

  return Response(wishing)

@api_view(['POST'])
def like_movie_users(request, my_pk):
  # print(request.data)
  users = []
  movies = request.data.get('movies')

  for movie in movies:
    movie = get_object_or_404(Movie, pk=movie)
    serializer = MovieSerializer(movie)

    for user in serializer.data.get('like_users'):
      if user not in users:
        users.append(user)

  return Response(users)

@api_view(['POST'])
def dislike_movie_users(request, my_pk):
  # print(request.data)
  users = []
  movies = request.data.get('movies')

  for movie in movies:
    movie = get_object_or_404(Movie, pk=movie)
    serializer = MovieSerializer(movie)

    for user in serializer.data.get('dislike_users'):
      if user not in users:
        users.append(user)

  return Response(users)

@api_view(['POST'])
def wish_movie_users(request, my_pk):
  # print(request.data)
  users = []
  movies = request.data.get('movies')

  for movie in movies:
    movie = get_object_or_404(Movie, pk=movie)
    serializer = MovieSerializer(movie)

    for user in serializer.data.get('wish_users'):
      if user not in users:
        users.append(user)

  return Response(users)


import requests
from bs4 import BeautifulSoup
import sqlite3
import os
from os import path

url_bundesliga = 'https://www.theguardian.com/football/laligafootball/table'
r_bundesliga = requests.get(url_bundesliga)

soup=BeautifulSoup(r_bundesliga.text,'html.parser')

teams_bundesliga_dict = {};
#grabs the html table (there is only one table)
theTable = soup.find('table',class_='table table--football table--league-table table--responsive-font table--striped')

def print_dict(teams_epl_dict):
    for team in teams_epl_dict.keys():
            print(team,end=" ")
            for stat in teams_epl_dict[team]:
                print(str(stat),end=" ")
            print()

#this for loop scrapes the data from the standings table
def get_data(teams_epl_dict, theTable):
    #initializes the dictionary with stats from the standings located on the guardian sports website
    for epl_team in (theTable.find_all('tr',)[1:]):
        team_name=epl_team.find('td',class_='table-column--main').text.strip()

        #the stats in order: position, games played, wins, draws, losses, goals for
        #goals against, goals differential, points
        position = epl_team.find('td',class_='table-column--sub').text.strip()
        games_played = epl_team.find_all('td')[2].text.strip()
        wins = epl_team.find_all('td',class_='table-column--importance-1')[0].text.strip()
        draws = epl_team.find_all('td',class_='table-column--importance-1')[1].text.strip()
        losses = epl_team.find_all('td',class_='table-column--importance-1')[2].text.strip()
        goal_differential = epl_team.find('td',class_='table-column--importance-3').text.strip()
        points = epl_team.find_all('td')[9].text.strip()

        teams_epl_dict[team_name]=[int(position),int(games_played),int(wins),int(draws),int(losses),int(goal_differential),int(points)]

get_data(teams_bundesliga_dict,theTable)
#print_dict(teams_epl_dict)

#creating a database of the standings
def create_and_initialize_db(tableName,teams_epl_dict):
    print('la liga (bundesliga) creating')
    connection = sqlite3.connect(tableName)
    cursor = connection.cursor()

    # table already created
    cursor.execute("""CREATE TABLE IF NOT EXISTS laliga_table (
                position INTEGER,
                team_name TEXT UNIQUE,
                games_played INTEGER,
                wins INTEGER,
                draws INTEGER,
                losses INTEGER,
                goal_differential INTEGER,
                points INTEGER
                )""")

    for t in teams_epl_dict.keys():
        cursor.execute("INSERT OR IGNORE INTO laliga_table (position, team_name, games_played, wins, draws, losses, goal_differential, points) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
        (teams_epl_dict[t][0],t,teams_epl_dict[t][1],teams_epl_dict[t][2],teams_epl_dict[t][3],teams_epl_dict[t][4],teams_epl_dict[t][5],teams_epl_dict[t][6]))
        connection.commit()

    cursor.close()
    connection.close()

def delete_db(tableName):
    if(path.isfile(tableName)):
        print('la liga (bundesliga) updating db')
        os.remove(tableName)
    else:
        print('la liga (bundesliga) nothing to delete')

delete_db('laliga_table.db')
create_and_initialize_db('laliga_table.db',teams_bundesliga_dict)


from keras.layers import Input, Dense, TimeDistributed, concatenate, LSTM, Conv2D, MaxPooling2D, Flatten
from keras.models import Model
from keras.utils import plot_model
import numpy as np

# functions

def timedistributed_vgg_block(layer_in, num_filters, num_conv):
    for _ in range(num_conv):
        layer_in = TimeDistributed(
            Conv2D(num_filters, (3, 3), padding='same',
                   activation='relu'))(layer_in)
    layer_in = TimeDistributed(MaxPooling2D(pool_size=(2, 2),
                                            strides=(2, 2)))(layer_in)
    return layer_in

# loading data

"""
# Main Inputs
"""
bgra_input_1 = Input(shape=(130, 130, 4),
                     dtype='float32',
                     name='bgra_input_1')
bgra_input_2 = Input(shape=(130, 130, 4),
                     dtype='float32',
                     name='bgra_input_2')
depth_input_1 = Input(shape=(130, 130, 1),
                      dtype='float32',
                      name='depth_input_1')
depth_input_2 = Input(shape=(130, 130, 1),
                      dtype='float32',
                      name='depth_input_2')
#skeletxt_input_1 = Input(shape=(250, 75),
#                         dtype='float32',
#                         name='skeletxt_input_1')
#skeletxt_input_2 = Input(shape=(250, 75),
#                         dtype='float32',
#                         name='skeletxt_input_2')
"""
# Convolution Layers for BGRA
# 5 VGG Blocks with 2 Layers each
"""
# First Convolution Layer
bgra_input_1_conv = TimeDistributed(
    Conv2D(64, (3, 3),
           input_shape=(130, 130, 4),
           activation='relu',
           padding='same'))(bgra_input_1)
bgra_input_2_conv = TimeDistributed(
    Conv2D(64, (3, 3),
           input_shape=(130, 130, 4),
           activation='relu',
           padding='same'))(bgra_input_2)

# bgra_input_1_conv = timedistributed_vgg_block(bgra_input_1_conv, 64, 1)
bgra_input_1_conv = timedistributed_vgg_block(bgra_input_1_conv, 128, 1)
bgra_input_1_conv = timedistributed_vgg_block(bgra_input_1_conv, 256, 2)
bgra_input_1_conv = timedistributed_vgg_block(bgra_input_1_conv, 512, 2)
bgra_input_1_conv = timedistributed_vgg_block(bgra_input_1_conv, 512, 2)
bgra_input_1_conv = TimeDistributed(Flatten())(bgra_input_1_conv)

# bgra_input_2_conv = timedistributed_vgg_block(bgra_input_2_conv, 64, 1)
bgra_input_2_conv = timedistributed_vgg_block(bgra_input_2_conv, 128, 1)
bgra_input_2_conv = timedistributed_vgg_block(bgra_input_2_conv, 256, 2)
bgra_input_2_conv = timedistributed_vgg_block(bgra_input_2_conv, 512, 2)
bgra_input_2_conv = timedistributed_vgg_block(bgra_input_2_conv, 512, 2)
bgra_input_2_conv = TimeDistributed(Flatten())(bgra_input_2_conv)
"""
# Convolution Layers for Depth
# 2 VGG Blocks with 2 Conv2D Layers each
"""
# First Convolution Layer
depth_input_1_conv = TimeDistributed(
    Conv2D(32, (3, 3),
           input_shape=(130, 130, 1),
           activation='relu',
           padding='same'))(depth_input_1)
depth_input_2_conv = TimeDistributed(
    Conv2D(32, (3, 3),
           input_shape=(130, 130, 1),
           activation='relu',
           padding='same'))(depth_input_2)

# depth_input_1_conv = timedistributed_vgg_block(depth_input_1_conv, 32, 1)
depth_input_1_conv = timedistributed_vgg_block(depth_input_1_conv, 64, 1)
depth_input_1_conv = timedistributed_vgg_block(depth_input_1_conv, 128, 2)
depth_input_1_conv = TimeDistributed(Flatten())(depth_input_1_conv)

# depth_input_2_conv = timedistributed_vgg_block(depth_input_2_conv, 32, 1)
depth_input_2_conv = timedistributed_vgg_block(depth_input_2_conv, 64, 1)
depth_input_2_conv = timedistributed_vgg_block(depth_input_2_conv, 128, 2)
depth_input_2_conv = TimeDistributed(Flatten())(depth_input_2_conv)

# Concatenate
conc_out = concatenate([
    bgra_input_1_conv, bgra_input_2_conv, depth_input_1_conv,
    depth_input_2_conv
    ])

# LSTM
lstm_out = LSTM(8192)(conc_out)
lstm_out = Dense(2048, activation='relu')(lstm_out)

# Output
main_output = Dense(2, activation='softmax', name='main_output')(lstm_out)

# Model Definition
model = Model(inputs=[
    bgra_input_1, depth_input_1, bgra_input_2, depth_input_2,
],
              outputs=[main_output])

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

print(model.summary())
plot_model(model,
           show_shapes=True,
           to_file='multiple_vgg_blocks_final_convlayer.png')

# model.fit(
#     {
#         'bgra_input_1': bgra_input_1_data,
#         'bgra_input_2': bgra_input_2_data,
#         'depth_input_1': depth_input_1_data,
#         'depth_input_2': depth_input_2_data,
#         'skeletxt_input_1': skeletxt_input_1_data,
#         'skeletxt_input_2': skeletxt_input_2_data,
#         'main_output': labels
#     },
#     epochs=epoch_placeholder,
#     batch_size=batch_size_placeholder)


from collections import defaultdict
N = int(input())

trades = []
for i in range(0,N):
    data = defaultdict(str)
    data['ID'], data['Type'], data['Name'], data['Price'],data['Quantity'] = input().split(' ')
    trades.append(data)

sell =defaultdict(list)
buy = defaultdict(list)

for i in range(0,len(trades)):
    if(trades[i]['Type']=='Sell'):
        sell[trades[i]['Name']] = [int(trades[i]['Price']),int(trades[i]['Quantity'])]
    elif(trades[i]['Type']=='Buy'):
        buy[trades[i]['Name']].append([int(trades[i]['Price']),int(trades[i]['Quantity'])])

flag = False
for key,value_sell in sell.items():
    for value_buy in buy[key]:
        if(value_buy[0]>=value_sell[0]):
            flag = True
            print(str(key)+":"+str(value_sell[0]))
            break
if(not flag):
    print("Stocks not traded")


import time,sys,os
# LIB is the parent directory of the directory where program resides.
LIB = os.path.join(os.path.dirname(__file__), '..')
DAT = os.path.join(os.path.dirname(__file__), '..', 'dataset', 'dataset1')
sys.path.insert(0, LIB)
from sklearn import tree
from DecisionTree import *
import numpy as np
import pandas as pd

trainData = pd.read_table(os.path.join(DAT,'train.txt'), header=None, encoding='gb2312', delim_whitespace=True)
testData = pd.read_table(os.path.join(DAT,'test.txt'), header=None, encoding='gb2312', delim_whitespace=True)
trainLabel = np.array(trainData.pop(3))
trainData = np.array(trainData)
testLabel = np.array(testData.pop(3))
testData = np.array(testData)

time_start1 = time.time()
clf1 = DecisionTreeClassifier()
clf1.train(trainData, trainLabel)
clf1.predict(testData)
score1 = clf1.accuracy(testLabel)
time_end1 = time.time()
print("Accuracy of self-DecisionTree: %f" % score1)
print("Runtime of self-DecisionTree:", time_end1-time_start1)

time_start = time.time()
clf = tree.DecisionTreeClassifier()
clf.fit(trainData, trainLabel)
clf.predict(testData)
score = clf.score(testData, testLabel, sample_weight=None)
time_end = time.time()
print("Accuracy of sklearn-DecisionTree: %f" % score)
print("Runtime of sklearn-DecisionTree:", time_end-time_start)


import numpy as np
import cv2
from shapeDetector import Shape
from shapeDetector import ShapeType
from imageProcessing import ImageProcessing
import rtmidi
import mido
import winsound

#contoursImage = np.ones(shape=(height,width,3), dtype=np.uint8) * 255

def sendNoteOn(note):
    message = mido.Message('note_on', note=note, velocity=127)
    sentOnNotes.append(note)
    midiOutput.send(message)

def sendNoteOff(note):
    message = mido.Message('note_off', note=note, velocity=127)
    midiOutput.send(message)

def sendControlChange(control, value):
    message = mido.Message('control_change', control=control, value=value)
    midiOutput.send(message)

def sendProgramChange(program, channel):
    message = mido.Message('program_change', program=program, channel=channel)
    midiOutput.send(message)

sentOnNotes = []

minFrequency = 500
maxFrequency = 7000
duration = 30

timePerLoop = 200
loopsPerImageProcessing = 5

imageHeight = 400

originalImage = cv2.imread("test4.jpg")
originalSize = originalImage.shape[:2]
ip = ImageProcessing()

loopsSinceImageProcessed = loopsPerImageProcessing

print("MIDI output ports: ", mido.get_output_names())

outputName = mido.get_output_names()[0]
midiOutput = mido.open_output("LoopBe Internal MIDI 1")

#exit()

resizedImage = cv2.resize(originalImage, (round(imageHeight / originalSize[0] * originalSize[1]), imageHeight))
ip.processImage(resizedImage)

drawing = False # true if mouse is pressed
erasing = False
ix,iy = -1,-1

cv2.namedWindow('image')

def on_trackbar(val):
    ip.xSegments = val
    ip.processImage(ip.image)

def on_speed_trackbar(val):
    global timePerLoop
    if val < 50:
        val = 50
    timePerLoop = val

cv2.createTrackbar("segments", "image" , 5, 30, on_trackbar)
cv2.createTrackbar("delay", "image" , 200, 500, on_speed_trackbar)

def freeDraw(event,x,y,flags,param):
    global drawing,erasing

    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        sendNoteOff(0)

    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing == True:
            cv2.circle(ip.image,(x,y),4,(0,0,0),-1)
        elif erasing == True:
            cv2.circle(ip.image,(x,y),15,(255,255,255),-1)

    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        ip.processImage(ip.image)

    elif event == cv2.EVENT_RBUTTONDOWN:
        erasing = True
        sendNoteOff(0)

    elif event == cv2.EVENT_RBUTTONUP:
        erasing = False
        ip.processImage(ip.image)

cv2.setMouseCallback('image',freeDraw)
while True:

    if loopsSinceImageProcessed == loopsPerImageProcessing:
        ()
        cv2.imwrite('output.jpg', ip.outputImage, [cv2.IMWRITE_JPEG_QUALITY, 90])
        #resizedImage = cv2.resize(originalImage, (round(imageHeight / originalSize[0] * originalSize[1]), imageHeight))
        #ip.processImage(resizedImage)
        #loopsSinceImageProcessed = 0
    else:
        ()
        #loopsSinceImageProcessed += 1

    ip.resetOutputImage()

    if not drawing and not erasing:

        if cv2.waitKey(timePerLoop) != -1:
            break

        ip.nextSegment()

        for note in sentOnNotes:
            sendNoteOff(note)
            sentOnNotes = []

        for shape in ip.getActiveShapes():
            ()
            if shape.shapeType == ShapeType.NONE or shape.shapeType == ShapeType.LINE:
                sendControlChange(1,3)
            elif shape.shapeType == ShapeType.CIRCLE:
                sendControlChange(1,2)
            elif shape.shapeType == ShapeType.TRIANGLE:
                sendControlChange(1,4)
            elif shape.shapeType == ShapeType.RECTANGLE:
                sendControlChange(1,1)

            #note = int(ip.getRelativeShapePosition(shape) * (72-48)) + 48
            note = int(ip.getRelativeShapePosition(shape) * 128)
            if shape.shapeType != ShapeType.TRIANGLE:
                sendNoteOn(note)
        # winsound.Beep(int(minFrequency + ip.getRelativeShapePosition(shape) * (maxFrequency - minFrequency)), duration)

    else:
        cv2.waitKey(10)

    ip.drawGrid()

    cv2.imshow("image", ip.outputImage)
    cv2.imshow("threshold", ip.thresholdImage)
    cv2.imshow("red", ip.redMask)
    cv2.imshow("green", ip.greenMask)
    cv2.imshow("black", ip.blackMask)
    #cv2.imshow("grayscale", grayscaleImage)

cv2.destroyAllWindows()


import pyautogui, time

time.sleep(2)

pyautogui.keyDown('ctrl')   #1
pyautogui.hotkey('win','d') #2
pyautogui.keyUp('ctrl')     #3 for opening a new window

pyautogui.press('win')
pyautogui.write('chrome') # "chrome" can be changed to any browser you like that you have installed
pyautogui.press('enter')
time.sleep(2)
f= open ("sites", 'r')
pyautogui.hotkey('ctrl', ' n') #for a new window in chrome
for word in f:
    pyautogui.write(word)
    pyautogui.press("enter")
    pyautogui.hotkey('ctrl', 't') #for new tab
    #pyautogui.hotkey('ctrl', ' n') #for new window in chrome


import collections
import numpy as np
import os.path
import re
import sys
from keras.models import model_from_json
import tensorflow as tf
import keras
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

class Classifier_cntk:
    def __init__(self,model_filename: str):
        import cntk
        self.model = None
        try:
          self.load_model(model_filename)
        except:
            raise Exception("Model didn't load successfully")

    def load_model(self,model_filename):

        self.model_filename = model_filename

        cntk_model = cntk.load_model(model_filename)

        #  First try and find output by name
        model_output = cntk_model.find_by_name('ScaledLogLikelihood')

        #  Fall back to first defined output
        if model_output is None:
            model_output = cntk_model.outputs[0]

        #  Create an object restricted to the desired output.
        cntk_model = cntk.combine(model_output)

        #  Optimized RNN models won't run on CPU without conversion.
        if 0 == cntk.use_default_device().type():
            cntk_model = cntk.misc.convert_optimized_rnnstack(cntk_model)

        self.model = cntk_model
        return self

    def stack_features(self, feature_vectors,context_frames=11):
        return np.column_stack([
            feature_vectors[np.minimum(len(feature_vectors) - 1, np.maximum(
                0, np.arange(len(feature_vectors), dtype=np.int) + d))]
            for d in range(-context_frames, context_frames + 1)
        ])

    def eval(self, feature_vectors,do_stack_features = False):
        if(do_stack_features):
            feature_vectors = self.stack_features(feature_vectors)

        if(self.model):
            return self.model.eval(feature_vectors.astype('f'))[0]
        else:
            raise  Exception("Model isn't loaded yet")

class Classifier_keras:
  def __init__(self,model_arch: str,model_weight: str, prior_file: str):
    self.model = None
    self.priori_logproba = None
    self.session=tf.Session()
    try:
        with self.session.as_default():
            with self.session.graph.as_default():

                with open(model_arch, 'r') as json_file:
                    json_savedModel= json_file.read()
                    self.model = model_from_json(json_savedModel)
                    # print(self.model.summary())
                self.model.compile(loss='categorical_crossentropy',
                        optimizer='adam',metrics=["categorical_accuracy"])
                self.model.load_weights(model_weight)
                self.model._make_predict_function()

                with open(prior_file,"r") as f:
                    priori = [line.split()[1] for line in f.readlines()]
                    self.priori_logproba = np.log(np.array(list(map(float,priori))))
    except:
        raise Exception("Model didn't load successfully")

  def eval(self, features):
    if(self.model):
        with self.session.as_default():
            with self.session.graph.as_default():
                sample=features.reshape(1,features.shape[0],features.shape[1])
                # print(sample.shape)
                pred=self.model.predict(sample)

                return np.log(pred[0]) - self.priori_logproba
    else:
            raise  Exception("Model isn't loaded yet")


from pygame.sprite import Sprite
import pygame
from abc import ABCMeta
from threading import Timer
from random import choices
from models import *
import time

class Equipment(Sprite, metaclass=ABCMeta):
    pass

class DefenderEquipment(Equipment, metaclass=ABCMeta):
    pass

class AttackerEquipment(Equipment, metaclass=ABCMeta):
    pass

class AmbulanceEquipment(DefenderEquipment):
    count = 1
    def __init__(self, defender, game_controller):
        super().__init__()

class ListEquipment(DefenderEquipment):
    count = 1
    def __init__(self, game_controller, last_cds):
        super().__init__()
        ListEquipment.count -= 1
        if time.time() - CivilianDefender.last_created_time < 15:
            CivilianDefender.last_created_time += 5
        if time.time() - FattyDefender.last_created_time < 60:
            FattyDefender.last_created_time += 5
        if time.time() - KamikazeDefender.last_created_time < 10:
            KamikazeDefender.last_created_time += 5
        if time.time() - PharmacistDefender.last_created_time < 25:
            PharmacistDefender.last_created_time += 5
        if time.time() - AuraDefender.last_created_time < 80:
            AuraDefender.last_created_time += 5
        for i in range(len(last_cds)):
            last_cds[i] -= 5000

class CanonEquipment(DefenderEquipment):
    count = 1
    def __init__(self, game_controller):
        super().__init__()
        CanonEquipment.count -= 1
        chosen = choices(Attacker.attackers, k=2)
        for attacker in chosen:
            attacker.attacked(2000, EquipmentAttacker())

class IndifferentEquipment(AttackerEquipment):
    count = 1
    def __init__(self, game_controller):
        super().__init__()
        IndifferentEquipment.count -= 1
        for attacker in Attacker.attackers:
            attacker.attacked(800, EquipmentAttacker())
        for defender in Defender.defenders:
            defender.attacked(800, EquipmentAttacker())

class DopingEquipment(AttackerEquipment):
    count = 1
    doped = set()
    def __init__(self, game_controller):
        super().__init__()
        DopingEquipment.count -= 1
        for attacker in Attacker.attackers:
            if attacker not in self.doped:
                attacker.speed = attacker.speed * 1.25
                DopingEquipment.doped.add(attacker)

class SignalEquipment(AttackerEquipment):
    count = 1

    on = False

    @staticmethod
    def turn_off():
        SignalEquipment.on = False

    def __init__(self, game_controller):
        super().__init__()
        SignalEquipment.count -= 1
        SignalEquipment.on = True
        timer = Timer(5, SignalEquipment.turn_off)
        timer.start()


######## Video Object Detection Using Tensorflow-trained Classifier #########
#
# Author: Evan Juras
# Date: 1/16/18
# Description:
# This program uses a TensorFlow-trained classifier to perform object detection.
# It loads the classifier uses it to perform object detection on a video.
# It draws boxes and scores around the objects of interest in each frame
# of the video.

## Some of the code is copied from Google's example at
## https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb

## and some is copied from Dat Tran's example at
## https://github.com/datitran/object_detector_app/blob/master/object_detection_app.py

## but I changed it to make it more understandable to me.

# Import packages
import cv2
import os
import numpy as np
import tensorflow as tf
import sys
import matplotlib.pyplot as plt
from filestack import Client
import datetime
import requests
import json
import pandas as pd
##from openpyxl import load_workbook
##file="./excel/a.xlsx"
##df = pd.read_excel(file, header=None)
##writer = pd.ExcelWriter(file, engine='openpyxl')
client = Client("AwCnfiZESWaA14iEjm189z")
data=[]
data2=[]
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")

# Import utilites
from utils import label_map_util
from utils import visualization_utils as vis_util

# Name of the directory containing the object detection module we're using
MODEL_NAME = 'faster_rcnn_inception_v2_99k_result'

# Grab path to current working directory
CWD_PATH = os.getcwd()

# Path to frozen detection graph .pb file, which contains the model that is used
# for object detection.
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

# Path to label map file
PATH_TO_LABELS = os.path.join('data', 'labelmap2.pbtxt')

# Number of classes the object detector can identify
NUM_CLASSES = 5

# Load the label map.
# Label maps map indices to category names, so that when our convolution
# network predicts `5`, we know that this corresponds to `king`.
# Here we use internal utility functions, but anything that returns a
# dictionary mapping integers to appropriate string labels would be fine
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)

# Load the Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')

    sess = tf.Session(graph=detection_graph)

# Define input and output tensors (i.e. data) for the object detection classifier

# Input tensor is the image
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

# Output tensors are the detection boxes, scores, and classes
# Each box represents a part of the image where a particular object was detected
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

# Each score represents level of confidence for each of the objects.
# The score is shown on the result image, together with the class label.
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')

# Number of objects detected
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Open video file
video = cv2.VideoCapture(CWD_PATH + "/video/IMG_0439_31s_green.mp4")
fps = int(video.get(cv2.CAP_PROP_FPS))
count = 0
rowCount=0
success = True
while success:

    success, image = video.read()
    name = './Capture/image' + str(count) + '.jpg'
    def length_of_bounding_box(ymin,ymax):
            return (ymax - ymin)/2 + ymin
    if count%(5*fps) == 0 :

        print ('Creating...' + name)
        height, width, channels = image.shape

        image_expanded = np.expand_dims(image, axis=0)

        # Perform the actual detection by running the model with the image as input
        (boxes, scores, classes, num) = sess.run(
            [detection_boxes, detection_scores, detection_classes, num_detections],
            feed_dict={image_tensor: image_expanded})
        threshold = 0.5
        objects = []

        for index, value in enumerate(classes[0]):
          object_dict = {}
          box=[]
          if scores[0, index] > threshold:
            #print(len(boxes[0,index]))
            ymin = boxes[0,index][0]* height
            xmin = boxes[0,index][1]* width
            ymax = boxes[0,index][2]* height
            xmax = boxes[0,index][3]* width
            object_dict['type'] =(category_index.get(value)).get('name')
            object_dict['yValue'] =length_of_bounding_box(ymin , ymax)
            object_dict['xValue'] =length_of_bounding_box(xmin , xmax)
            objects.append(object_dict)

        motorCount=0
        carCount=0

        for obj in objects:
            keys = iter(obj.values())
            typeVe,yValue,xValue=next(keys),next(keys),next(keys)
            print(typeVe)
##            print(typeVe ,"=",yValue,"=",xValue)
            motorLabel="motor"

            if(yValue >=560 and yValue <=650 and xValue>=550 and xValue<=1350):
                if(typeVe.strip() == motorLabel):
                    motorCount +=1
                else:
                    carCount+=1
            else:
                print("No vehicle in area detection")
        print(motorCount)
        print(carCount)
##        print(carCount)
        image[560:700 , 550:1350 , 2]=255
        plt.figure( figsize = (10,10))
        cv2.imwrite(name,image)
##        data.append(motorCount)
##        data2.append(carCount)

##        new_filelink = client.upload(filepath=name, multipart=False)
##        print('filestack url: ' + new_filelink.url)
##        print(datetime.datetime.now().time())
##        vehicles ={
##	"CameraId":1,
##	"result":[
##		{
##		"typeId":1,
##		"countVeh":motorCount
##		},
##		{
##		"typeId":2,
##		"countVeh":carCount
##		}
##	]
##}
##        headers = {'Content-type': 'application/json'}
##        url='http://localhost:8080/api/detection'
##        params={'detectResultString':json.dumps(vehicles)}
##        r=requests.post(url, params=params, headers=headers)
##        print(r.status_code, r.reason, r.text)
    count+=1

    #cv2.imshow('Object detector', image)
    # All the results have been drawn on the frame, so it's time to display it.
    ##cv2.imshow('Object detector', frame)

    # Press 'q' to quit
    if cv2.waitKey(0) == ord('q'):
        break

def save_Excel():
    df2 = pd.DataFrame({'Data': data})
    df1 = pd.DataFrame({'Data': data2})
    book=load_workbook(file)
    writer.book = book
    writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
    df2.to_excel(writer, sheet_name='Sheet1', header=None, index=False,
             startcol=1,startrow=1)
    df1.to_excel(writer, sheet_name='Sheet1', header=None, index=False,
             startcol=2,startrow=1)
    writer.save()
##save_Excel()
# Clean up
video.release()
cv2.destroyAllWindows()


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 25 08:47:40 2018

@author: ben
"""

import numpy as np
from nipy import load_image
import matplotlib.pyplot as plt
import datetime
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from skimage.transform import resize
import os
import cv2

myimgLR = load_image('DATA/NotProcessedData/tfMRI_WM_LR.nii')
print(myimgLR.shape)
LR = myimgLR.get_data()[:,:,:,350]

def normalize(arr):
    arr_min = np.min(arr)
    return (arr-arr_min)/(np.max(arr)-arr_min)

def explode(data):
    shape_arr = np.array(data.shape)
    size = shape_arr[:3]*2 - 1
    exploded = np.zeros(np.concatenate([size, shape_arr[3:]]), dtype=data.dtype)
    exploded[::2, ::2, ::2] = data
    return exploded

def expand_coordinates(indices):
    x, y, z = indices
    x[1::2, :, :] += 1
    y[:, 1::2, :] += 1
    z[:, :, 1::2] += 1
    return x, y, z

def plot_cube(cube, angle=320):
    cube = normalize(cube)

    facecolors = cm.viridis(cube)
    facecolors[:,:,:,-1] = cube
    facecolors = explode(facecolors)

    filled = facecolors[:,:,:,-1] != 0
    x, y, z = expand_coordinates(np.indices(np.array(filled.shape) + 1))

    fig = plt.figure(figsize=(30/2.54, 30/2.54))
    ax = fig.gca(projection='3d')
    ax.view_init(30, angle)
    ax.set_xlim(right=IMG_DIM*2)
    ax.set_ylim(top=IMG_DIM*2)
    ax.set_zlim(top=IMG_DIM*2)

    ax.voxels(x, y, z, filled, facecolors=facecolors)
    plt.savefig('/Users/kws/Desktop/FMRI_IMAGES/Subject_1/image_' + str(i), bbox_inches = 'tight')
    plt.show()

#Generate Images for each time step of a given 4D NiFTI file
myimg = load_image('tfMRI_WM_LR.nii')
time_steps = myimg.shape[3]
IMG_DIM = 50

for i in range(1,time_steps):
    LR = myimg.get_data()[:,:,:,i]
    IMG_DIM = 50
    resized = resize(normalize(LR), (IMG_DIM, IMG_DIM, IMG_DIM), mode='constant')
    plot_cube(resized)

#Generate Movie from generated images
image_folder = '/Users/kws/Desktop/FMRI_IMAGES/Subject_1'
video_name = 'Subject_1.avi'

images = [img for img in os.listdir(image_folder) if img.endswith(".png")]
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape

video = cv2.VideoWriter(video_name, -1, 1, (width,height))

for image in images:
    video.write(cv2.imread(os.path.join(image_folder, image)))

cv2.destroyAllWindows()
video.release()