import numpy as np
import os
import pandas as pd
import plotly.offline as opy
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from sklearn.preprocessing import MaxAbsScaler
from sklearn.decomposition import PCA
from sklearn.neighbors import NearestNeighbors
from typing import Tuple

N_JOBS = int(os.cpu_count() * 0.8)
FEATURE_COLUMNS = ["loudness", "tempo", "pitch_network_average_degree",
                   "pitch_network_entropy", "pitch_network_mean_clustering_coeff",
                   "timbre_00", "timbre_01", "timbre_02", "timbre_03", "timbre_04", "timbre_05",
                   "timbre_06", "timbre_07", "timbre_08", "timbre_09", "timbre_10", "timbre_11"]

class MSDModel():
    def __init__(self,
                 df: pd.DataFrame,
                 feature_columns: list[str] = FEATURE_COLUMNS,
                 n_pca_components: int = 3,
                 n_neighbors_default: int = 10,
                 n_jobs: int = N_JOBS,
                 popularity_threshold: float = 0.85) -> None:
        """
        Initialize the class

        :param df: Dataframe of data we are fitting to
        :param features: List of features to use in the dataframe
        :param n_pca_components: Number of PCA components
        """
        # Capture the feature columns
        self.feature_columns = feature_columns

        # Copy of the dataframe
        self.orig_df = df[df["song_hotttnesss"] > 0].copy()
        self.mucked_with_df = self.orig_df.copy()

        # Remove NaN from artist familiarity
        self.mucked_with_df["artist_familiarity"] = self.mucked_with_df["artist_familiarity"].fillna(0)
        # Remove NaN from artist hotttnesss
        self.mucked_with_df["artist_hotttnesss"] = self.mucked_with_df["artist_hotttnesss"].fillna(0)

        # Standardize the data
        self.scalar = MaxAbsScaler()
        self.mucked_with_df[self.feature_columns] = self.scalar.fit_transform(self.mucked_with_df[self.feature_columns])

        # Fit the PCA
        self.pca = PCA(n_components=n_pca_components)
        self.pca.fit(self.mucked_with_df[self.feature_columns])

        # Filter out popular songs
        self.orig_df = self.orig_df[self.orig_df["song_hotttnesss"] < popularity_threshold]
        self.mucked_with_df = self.mucked_with_df[self.mucked_with_df["song_hotttnesss"] < popularity_threshold]
        self.pca_features = self.pca.transform(self.mucked_with_df[self.feature_columns])

        # Declare the nearest neighbors
        self.n_neighbors_default = n_neighbors_default
        self.nn = NearestNeighbors(n_neighbors=n_neighbors_default, n_jobs=n_jobs)
        self.nn.fit(self.pca_features)

    def find_k_neighbors(self,
                         user_df: pd.DataFrame,
                         n_neighbors: int = None) -> Tuple[pd.DataFrame, np.array]:
        """
        Given a user dataframe record, find the n closest neighbors

        :param user_df: The user data to fit to
        :param n_neighbors: Number of neighbors to return
        """
        # Make sure all columns we need exist
        assert all([x in user_df.columns for x in self.feature_columns])

        # Make sure only one row was passed in
        assert len(user_df) == 1

        # Copy the df
        df_in = user_df.copy()

        # Setting the number of neighbors the users want
        if not n_neighbors:
            n_neighbors = self.n_neighbors_default

        # Transform the user_df to our PCA
        df_in[self.feature_columns] = self.scalar.transform(df_in[self.feature_columns])
        transformed_user_array = self.pca.transform(df_in[self.feature_columns])

        # Run the model and return the results
        distances, idx = self.nn.kneighbors(transformed_user_array, n_neighbors=n_neighbors+1)

        # Test for edge case where one of the songs we are passing in
        # is a "dumpster", so retreive more and pair down after checking...
        if distances.flatten()[0] < 1e-6:
            distances = distances[1:]
            idx = idx.flatten()[1:]
        else:
            distances = distances[0:n_neighbors]
            idx = idx.flatten()[0:n_neighbors]
        return self.orig_df.iloc[idx.flatten()], transformed_user_array

    def transform_df(self, df: pd.DataFrame) -> pd.DataFrame:
        """
        Given a dataframe with the features, add new columns with the features projected on
        each of the PCA axes

        :param df: DataFrame of info
        :return: New dataframe
        """
        # Perform the PCA
        df_out = df.copy()
        # df_out = df_out.sample(frac=.25)
        df_out[self.feature_columns] = self.scalar.transform(df_out[self.feature_columns])
        transformed_user_array = self.pca.transform(df_out[self.feature_columns])
        # Add the columns
        for j in range(transformed_user_array.shape[1]):
            df_out["PCA {}".format(j)] = transformed_user_array[:, j]

        return df_out

    def create_pca_plot(self,
                        user_dumpster_diver_features_df: pd.DataFrame,
                        msd_recs_dumpster_features_df: pd.DataFrame,
                        spotify_recs_dumpster_features_df: pd.DataFrame,
                        ) -> None:
        """
        Create the PCA plot with all the data
        :param user_dumpster_diver_features_df: User listening profile
        :param msd_recs_dumpster_features_df: MSD Dumpster Diver recos
        :param spotify_recs_dumpster_features_df: Spotify recos
        :returns:
        """
        # Transform all of the dfs to add the PCA columns
        background_pca_df = self.transform_df(self.orig_df)
        background_pca_df["Source"] = "MSD"

        user_dumpster_diver_features_df = self.transform_df(user_dumpster_diver_features_df)
        user_dumpster_diver_features_df["Source"] = "User"

        msd_recs_dumpster_features_df = self.transform_df(msd_recs_dumpster_features_df)
        msd_recs_dumpster_features_df["Source"] = "Dumpster Diver"

        spotify_recs_dumpster_features_df = self.transform_df(spotify_recs_dumpster_features_df)
        spotify_recs_dumpster_features_df["Source"] = "Spotify"

        user_dumpster_diver_features_df

        #
        # Create PCA Plot for PCA 0 and PCA 1
        #
        pca_p0_p1_background = go.Scattergl(
            name="MSD Data",
            x = background_pca_df["PCA 0"],
            y = background_pca_df["PCA 1"],
            # template="plotly_dark",
            mode='markers',
            marker=dict(
                color=background_pca_df["song_hotttnesss"],
                symbol="circle"
            ),
            opacity=0.2,
            hovertext=background_pca_df["song_title"]
        )
        pca_p0_p1_spotify = go.Scattergl(
            name="Spotify Recommendations",
            x = spotify_recs_dumpster_features_df["PCA 0"],
            y = spotify_recs_dumpster_features_df["PCA 1"],
            # template="plotly_dark",
            mode='markers',
            marker=dict(
                color='rgb(74,206,110)',
                symbol="circle",
                size=10
            ),
            opacity=1,
            hovertext=spotify_recs_dumpster_features_df["name"]
        )
        pca_p0_p1_dumpster = go.Scattergl(
            name="Dumpster Diver Recommendations",
            x = msd_recs_dumpster_features_df["PCA 0"],
            y = msd_recs_dumpster_features_df["PCA 1"],
            # template="plotly_dark",
            mode='markers',
            marker=dict(
                color='rgb(0,255,255)',
                symbol="x",
                size=10
            ),
            opacity=1,
            hovertext=msd_recs_dumpster_features_df["song_title"]
        )
        pca_p0_p1_user = go.Scattergl(
            name="User's Top Spotify Tracks",
            x = user_dumpster_diver_features_df["PCA 0"],
            y = user_dumpster_diver_features_df["PCA 1"],
            # template="plotly_dark",
            mode='markers',
            marker=dict(
                color='rgb(255,255,0)',
                symbol="x",
                size=10
            ),
            opacity=1,
            hovertext=user_dumpster_diver_features_df["name"]
        )
        pca_p0_p1 = go.Figure(data=[pca_p0_p1_background,pca_p0_p1_spotify,pca_p0_p1_dumpster, pca_p0_p1_user])
        pca_p0_p1.update_layout(template="plotly_dark", title="PCA Dimensions 0 and 1", title_x=0.5, height=500)

        #
        # Create PCA Plot for PCA 0 and PCA 1
        #
        pca_p1_p2_background = go.Scattergl(
            name="MSD Data",
            x = background_pca_df["PCA 1"],
            y = background_pca_df["PCA 2"],
            # template="plotly_dark",
            mode='markers',
            marker=dict(
                color=background_pca_df["song_hotttnesss"],
                symbol="circle"
            ),
            opacity=0.2,
            hovertext=background_pca_df["song_title"]
        )
        pca_p1_p2_spotify = go.Scattergl(
            name="Spotify Recommendations",
            x = spotify_recs_dumpster_features_df["PCA 1"],
            y = spotify_recs_dumpster_features_df["PCA 2"],
            # template="plotly_dark",
            mode='markers',
            marker=dict(
                color='rgb(74,206,110)',
                symbol="circle",
                size=10
            ),
            opacity=1,
            hovertext=spotify_recs_dumpster_features_df["name"]
        )
        pca_p1_p2_dumpster = go.Scattergl(
            name="Dumpster Diver Recommendations",
            x = msd_recs_dumpster_features_df["PCA 1"],
            y = msd_recs_dumpster_features_df["PCA 2"],
            # template="plotly_dark",
            mode='markers',
            marker=dict(
                color='rgb(0,255,255)',
                symbol="x",
                size=10
            ),
            opacity=1,
            hovertext=msd_recs_dumpster_features_df["song_title"]
        )
        pca_p1_p2_user = go.Scattergl(
            name="User's Top Spotify Tracks",
            x = user_dumpster_diver_features_df["PCA 1"],
            y = user_dumpster_diver_features_df["PCA 2"],
            # template="plotly_dark",
            mode='markers',
            marker=dict(
                color='rgb(255,255,0)',
                symbol="x",
                size=10
            ),
            opacity=1,
            hovertext=user_dumpster_diver_features_df["name"]
        )
        pca_p1_p2 = go.Figure(data=[pca_p1_p2_background,pca_p1_p2_spotify,pca_p1_p2_dumpster, pca_p1_p2_user])
        pca_p1_p2.update_layout(template="plotly_dark", title="PCA Dimensions 1 and 2", title_x=0.5, height=500)

        #
        # Create PCA Plot for PCA 0 and PCA 1
        #
        pca_p0_p2_background = go.Scattergl(
            name="MSD Data",
            x = background_pca_df["PCA 0"],
            y = background_pca_df["PCA 2"],
            # template="plotly_dark",
            mode='markers',
            marker=dict(
                color=background_pca_df["song_hotttnesss"],
                symbol="circle"
            ),
            opacity=0.2,
            hovertext=background_pca_df["song_title"]
        )
        pca_p0_p2_spotify = go.Scattergl(
            name="Spotify Recommendations",
            x = spotify_recs_dumpster_features_df["PCA 0"],
            y = spotify_recs_dumpster_features_df["PCA 2"],
            # template="plotly_dark",
            mode='markers',
            marker=dict(
                color='rgb(74,206,110)',
                symbol="circle",
                size=10
            ),
            opacity=1,
            hovertext=spotify_recs_dumpster_features_df["name"]
        )
        pca_p0_p2_dumpster = go.Scattergl(
            name="Dumpster Diver Recommendations",
            x = msd_recs_dumpster_features_df["PCA 0"],
            y = msd_recs_dumpster_features_df["PCA 2"],
            # template="plotly_dark",
            mode='markers',
            marker=dict(
                color='rgb(0,255,255)',
                symbol="x",
                size=10
            ),
            opacity=1,
            hovertext=msd_recs_dumpster_features_df["song_title"]
        )
        pca_p0_p2_user = go.Scattergl(
            name="User's Top Spotify Tracks",
            x = user_dumpster_diver_features_df["PCA 0"],
            y = user_dumpster_diver_features_df["PCA 2"],
            # template="plotly_dark",
            mode='markers',
            marker=dict(
                color='rgb(255,255,0)',
                symbol="x",
                size=10
            ),
            opacity=1,
            hovertext=user_dumpster_diver_features_df["name"]
        )
        pca_p0_p2 = go.Figure(data=[pca_p0_p2_background,pca_p0_p2_spotify,pca_p0_p2_dumpster, pca_p0_p2_user])
        pca_p0_p2.update_layout(template="plotly_dark", title="PCA Dimensions 0 and 2", title_x=0.5, height=500)

        # Create the divs
        div0 = opy.plot(pca_p0_p1, auto_open=False, output_type="div")
        div1 = opy.plot(pca_p1_p2, auto_open=False, output_type="div")
        div2 = opy.plot(pca_p0_p2, auto_open=False, output_type="div")

        # return div
        return div0, div1, div2


from lark import Lark, Transformer
from collections import namedtuple
import re
import gzip
from .packed_vectors import PackedVectors
from .logic_sim import LogicSim

Call = namedtuple('Call', ['name', 'parameters'])
ScanPattern = namedtuple('ScanPattern', ['load', 'launch', 'capture', 'unload'])

class StilFile:
    def __init__(self, version, signal_groups, scan_chains, calls):
        self.version = version
        self.signal_groups = signal_groups
        self.scan_chains = scan_chains
        self.si_ports = dict((v[0], k) for k, v in scan_chains.items())
        self.so_ports = dict((v[-1], k) for k, v in scan_chains.items())
        self.calls = calls
        self.patterns = []
        launch = {}
        capture = {}
        load = {}
        for call in self.calls:
            if call.name == 'load_unload':
                unload = {}
                for so_port in self.so_ports:
                    if so_port in call.parameters:
                        unload[so_port] = call.parameters[so_port].replace('\n', '')
                if len(capture) > 0:
                    self.patterns.append(ScanPattern(load, launch, capture, unload))
                    capture = {}
                    launch = {}
                load = {}
                for si_port in self.si_ports:
                    if si_port in call.parameters:
                        load[si_port] = call.parameters[si_port].replace('\n', '')
            if call.name.endswith('_launch') or call.name.endswith('_capture'):
                if len(launch) == 0:
                    launch = dict((k, v.replace('\n', '')) for k, v in call.parameters.items())
                else:
                    capture = dict((k, v.replace('\n', '')) for k, v in call.parameters.items())

    def _maps(self, c):
        interface = list(c.interface) + [n for n in c.nodes if 'DFF' in n.kind]
        intf_pos = dict([(n.name, i) for i, n in enumerate(interface)])
        pi_map = [intf_pos[n] for n in self.signal_groups['_pi']]
        po_map = [intf_pos[n] for n in self.signal_groups['_po']]
        scan_maps = {}
        scan_inversions = {}
        for chain_name, chain in self.scan_chains.items():
            scan_map = []
            scan_in_inversion = []
            scan_out_inversion = []
            inversion = False
            for n in chain[1:-1]:
                if n == '!':
                    inversion = not inversion
                else:
                    scan_in_inversion.append(inversion)
            scan_in_inversion = list(reversed(scan_in_inversion))
            inversion = False
            for n in reversed(chain[1:-1]):
                if n == '!':
                    inversion = not inversion
                else:
                    scan_map.append(intf_pos[n])
                    scan_out_inversion.append(inversion)
            scan_maps[chain[0]] = scan_map
            scan_maps[chain[-1]] = scan_map
            scan_inversions[chain[0]] = scan_in_inversion
            scan_inversions[chain[-1]] = scan_out_inversion
        return interface, pi_map, po_map, scan_maps, scan_inversions

    def tests(self, c):
        interface, pi_map, po_map, scan_maps, scan_inversions = self._maps(c)
        tests = PackedVectors(len(self.patterns), len(interface), 2)
        for i, p in enumerate(self.patterns):
            for si_port in self.si_ports.keys():
                tests.set_values(i, p.load[si_port], scan_maps[si_port], scan_inversions[si_port])
            tests.set_values(i, p.launch['_pi'], pi_map)
        return tests

    def tests8v(self, c):
        interface, pi_map, po_map, scan_maps, scan_inversions = self._maps(c)
        init = PackedVectors(len(self.patterns), len(interface), 2)
        for i, p in enumerate(self.patterns):
            # init.set_values(i, '0' * len(interface))
            for si_port in self.si_ports.keys():
                init.set_values(i, p.load[si_port], scan_maps[si_port], scan_inversions[si_port])
            init.set_values(i, p.launch['_pi'], pi_map)
        sim4v = LogicSim(c, len(init), 2)
        sim4v.assign(init)
        sim4v.propagate()
        launch = init.copy()
        sim4v.capture(launch)
        for i, p in enumerate(self.patterns):
            # if there was no launch clock, then init = launch
            if ('P' not in p.launch['_pi']) or ('P' not in p.capture['_pi']):
                for si_port in self.si_ports.keys():
                    launch.set_values(i, p.load[si_port], scan_maps[si_port], scan_inversions[si_port])
            if 'P' in p.capture['_pi']:
                launch.set_values(i, p.capture['_pi'], pi_map)

        return PackedVectors.from_pair(init, launch)

    def responses(self, c):
        interface, pi_map, po_map, scan_maps, scan_inversions = self._maps(c)
        resp = PackedVectors(len(self.patterns), len(interface), 2)
        for i, p in enumerate(self.patterns):
            resp.set_values(i, p.capture['_po'], po_map)
            for so_port in self.so_ports.keys():
                resp.set_values(i, p.unload[so_port], scan_maps[so_port], scan_inversions[so_port])
        return resp

class StilTransformer(Transformer):
    def __init__(self):
        super().__init__()
        self._signal_groups = None
        self._calls = None
        self._scan_chains = None

    @staticmethod
    def quoted(args): return args[0][1:-1]

    @staticmethod
    def call(args): return Call(args[0], dict(args[1:]))

    @staticmethod
    def call_parameter(args): return args[0], args[1].value

    @staticmethod
    def signal_group(args): return args[0], args[1:]

    @staticmethod
    def scan_chain(args):
        scan_in = None
        scan_cells = None
        scan_out = None
        for t in args[1:]:
            if t.data == 'scan_in':
                scan_in = t.children[0]
            elif t.data == 'scan_out':
                scan_out = t.children[0]
            if t.data == 'scan_cells':
                scan_cells = [n.replace('.SI', '') for n in t.children]
                scan_cells = [re.sub(r'.*\.', '', s) if '.' in s else s for s in scan_cells]
        return args[0], ([scan_in] + scan_cells + [scan_out])

    def signal_groups(self, args): self._signal_groups = dict(args)

    def pattern(self, args): self._calls = [c for c in args if isinstance(c, Call)]

    def scan_structures(self, args): self._scan_chains = dict(args)

    def start(self, args):
        return StilFile(float(args[0]), self._signal_groups, self._scan_chains, self._calls)

def parse(stil):
    grammar = r"""
    start: "STIL" FLOAT _ignore _block*
    _block: signal_groups | scan_structures | pattern
        | "Header" _ignore
        | "Signals" _ignore
        | "Timing" _ignore
        | "PatternBurst" quoted _ignore
        | "PatternExec" _ignore
        | "Procedures" _ignore
        | "MacroDefs" _ignore

    signal_groups: "SignalGroups" "{" signal_group* "}"
    signal_group: quoted "=" "'" quoted ( "+" quoted)* "'" _ignore? ";"?

    scan_structures: "ScanStructures" "{" scan_chain* "}"
    scan_chain: "ScanChain" quoted "{" ( scan_length
        | scan_in | scan_out | scan_inversion | scan_cells | scan_master_clock )* "}"
    scan_length: "ScanLength" /[0-9]+/ ";"
    scan_in: "ScanIn" quoted ";"
    scan_out: "ScanOut" quoted ";"
    scan_inversion: "ScanInversion" /[0-9]+/ ";"
    scan_cells: "ScanCells" (quoted | /!/)* ";"
    scan_master_clock: "ScanMasterClock" quoted ";"

    pattern: "Pattern" quoted "{" ( label | w | c | macro | ann | call )* "}"
    label: quoted ":"
    w: "W" quoted ";"
    c: "C" _ignore
    macro: "Macro" quoted ";"
    ann: "Ann" _ignore
    call: "Call" quoted "{" call_parameter* "}"
    call_parameter: quoted "=" /[^;]+/ ";"

    quoted: /"[^"]*"/
    FLOAT: /[-0-9.]+/
    _ignore: "{" _NOB? _ignore_inner* "}"
    _ignore_inner: "{" _NOB? _ignore_inner* "}" _NOB?
    _NOB: /[^{}]+/
    %ignore ( /\r?\n/ | "//" /[^\n]*/ | /[\t\f ]/ )+
    """
    if '\n' not in str(stil):  # One line?: Assuming it is a file name.
        if str(stil).endswith('.gz'):
            with gzip.open(stil, 'rt') as f:
                text = f.read()
        else:
            with open(stil, 'r') as f:
                text = f.read()
    else:
        text = str(stil)
    return Lark(grammar, parser="lalr", transformer=StilTransformer()).parse(text)

def extract_scan_pattens(stil_calls):
    pats = []
    pi = None
    scan_in = None
    for call in stil_calls:
        if call.name == 'load_unload':
            scan_out = call.parameters.get('Scan_Out')
            if scan_out is not None:
                scan_out = scan_out.replace('\n', '')
            if pi: pats.append(ScanPattern(scan_in, pi, None, scan_out))
            scan_in = call.parameters.get('Scan_In')
            if scan_in is not None:
                scan_in = scan_in.replace('\n', '')
        if call.name == 'allclock_capture':
            pi = call.parameters['_pi'].replace('\n', '')
    return pats

def match_patterns(stil_file, pats, interface):
    intf_pos = dict([(n.name, i) for i, n in enumerate(interface)])
    pi_map = [intf_pos[n] for n in stil_file.signal_groups['_pi']]
    scan_map = [intf_pos[re.sub(r'b..\.', '', n)] for n in reversed(stil_file.scan_chains['1'])]
    # print(scan_map)
    tests = PackedVectors(len(pats), len(interface), 2)
    for i, p in enumerate(pats):
        tests.set_values(i, p.scan_in, scan_map)
        tests.set_values(i, p.pi, pi_map)

    resp = PackedVectors(len(pats), len(interface), 2)
    for i, p in enumerate(pats):
        resp.set_values(i, p.pi, pi_map)
        resp.set_values(i, p.scan_out, scan_map)

    return tests, resp


from django.shortcuts import render

from django.http import HttpResponseRedirect
from .models import income, expense
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import os

# Create your views here.
def homepage(request):
	if request.session['username'] == 'logged out':

		return HttpResponseRedirect('/login/')
	else:
		if request.method == "POST":
			if request.POST.get('what_earned') and request.POST.get('amount_earned') and request.POST.get('date_earned'):
				castig = income()
				castig.username_earned = request.session['username']
				castig.what_earned = request.POST.get('what_earned').upper()
				castig.amount_earned = request.POST.get('amount_earned')
				castig.date_earned = request.POST.get('date_earned')
				castig.message_earned = request.POST.get('message_earned')

				castig.save()

				return HttpResponseRedirect('/home/')

			elif request.POST.get('what_expense') and request.POST.get('amount_expense') and request.POST.get('date_expense'):
				cheltuiala = expense()
				cheltuiala.username_expense = request.session['username']
				cheltuiala.what_expense = request.POST.get('what_expense').upper()
				cheltuiala.amount_expense = request.POST.get('amount_expense')
				cheltuiala.date_expense = request.POST.get('date_expense')
				cheltuiala.message_expense = request.POST.get('message_expense')

				cheltuiala.save()

				scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']

				basedir = os.path.abspath(os.path.dirname(__file__))
				data_json = basedir+'/Test-ec10a3a14e27.json'
				credentials = ServiceAccountCredentials.from_json_keyfile_name(data_json, scope)

				gc = gspread.authorize(credentials)

				wks = gc.open('Personal Finance').sheet1

				wks.append_row(['','','','',
					request.POST.get('date_expense'),
					request.POST.get('what_expense').upper(),
					request.POST.get('amount_expense')])

				return HttpResponseRedirect('/home/')
			else:
				return HttpResponseRedirect('/home/')
		else:
			total_income = sum(income.objects.filter(username_earned=request.session['username']).values_list('amount_earned', flat=True))
			total_expense = sum(expense.objects.filter(username_expense=request.session['username']).values_list('amount_expense', flat=True))
			balance = total_income - total_expense

			income_balance_expense = {
			'total_income':total_income,
			'total_expense':total_expense,
			'balance':balance
			}
			return render(request, 'homepage/dashboard.html', income_balance_expense)

def income_history(request):
	if request.session['username'] == 'logged out':

		return HttpResponseRedirect('/login/')
	else:
		income_list_db = income.objects.filter(username_earned=request.session['username']).values_list()
		length_income = len(income_list_db)

		what_earned_list = []
		for index in range(length_income):
			what_earned_list.append(income_list_db[index][2])

		date_earned_list = []
		for index in range(length_income):
			date_earned_list.append(income_list_db[index][4])

		amount_earned_list = []
		for index in range(length_income):
			amount_earned_list.append(income_list_db[index][3])

		message_earned_list = []
		for index in range(length_income):
			message_earned_list.append(income_list_db[index][5])

		fusion = zip(what_earned_list, date_earned_list, amount_earned_list, message_earned_list)
		income_dictionary = {
		"fusion":fusion
		}

		return render(request, 'homepage/income_history.html', income_dictionary)

def expenses_history(request):
	if request.session['username'] == 'logged out':

		return HttpResponseRedirect('/login/')
	else:

		expense_list_db = expense.objects.filter(username_expense=request.session['username']).values_list()
		length_expense = len(expense_list_db)

		what_expense_list = []
		for index in range(length_expense):
			what_expense_list.append(expense_list_db[index][2])

		date_expense_list = []
		for index in range(length_expense):
			date_expense_list.append(expense_list_db[index][4])

		amount_expense_list = []
		for index in range(length_expense):
			amount_expense_list.append(expense_list_db[index][3])

		message_expense_list = []
		for index in range(length_expense):
			message_expense_list.append(expense_list_db[index][5])

		fusion = zip(what_expense_list, date_expense_list, amount_expense_list, message_expense_list)
		expense_dictionary = {
		"fusion":fusion
		}
		return render(request, 'homepage/expenses_history.html', expense_dictionary)

def settings(request):
	if request.session['username'] == 'logged out':

		return HttpResponseRedirect('/login/')
	else:

		if request.method == "POST":
			currency_used = '$'
			if request.POST.get('currency') == 'dollar':
				currency_used == '$'
			elif request.POST.get('currency') == 'pound':
				currency_used == '₤'
			elif request.POST.get('currency') == 'euro':
				currency_used == '€'

		return render(request, 'homepage/settings.html')


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pytest
from pypengine.skeleton import fib

__author__ = "Francesco Strozzi"
__copyright__ = "Francesco Strozzi"
__license__ = "none"

def test_fib():
    assert fib(1) == 1
    assert fib(2) == 1
    assert fib(7) == 13
    with pytest.raises(AssertionError):
        fib(-10)


from typing import List
from fastapi import WebSocket
from app.schemas.chat import Chat

class ConnectionManager:
    def __init__(self) -> None:
        self.active_connections: List[(WebSocket, str)] = []
        self.neew_help_conn: List[(WebSocket, str)] = []

    async def connect(self, websocket: WebSocket, nickname: str) -> None:
        await websocket.accept()
        self.active_connections.append((websocket, nickname))
        self.neew_help_conn.append((websocket, nickname))

    def disconnect(self, websocket: WebSocket, nickname: str) -> None:
        self.active_connections.remove((websocket, nickname))

    async def send_personal_message(self, message: str, websocket: WebSocket) -> None:
        await websocket.send_text(message)

    async def broadcast(self, message: str) -> None:
        for connection in self.active_connections:
            await connection[0].send_text(message)

    async def get_chats_with_promlem(self):
        chats: List[Chat] = []
        for chat in self.neew_help_conn:
            chats.append(Chat(name=str(chat[1]), status=chat[0].client_state.name))
        return chats


import pickle

import warnings

warnings.filterwarnings("error")

import numpy as np
import copy

from em_fields.slurm_functions import get_script_evolution_slave_fenchel
from em_fields.default_settings import define_plasma_parameters
from em_fields.em_functions import get_thermal_velocity

evolution_slave_fenchel_script = get_script_evolution_slave_fenchel()

import matplotlib.pyplot as plt

plt.rcParams.update({'font.size': 12})
# plt.rcParams.update({'font.size': 10})
# plt.rcParams.update({'font.size': 8})
# plt.rcParams["figure.facecolor"] = 'white'
# plt.rcParams["axes.facecolor"] = 'green'

# plt.close('all')

save_dir = '/Users/talmiller/Downloads/single_particle/'
# save_dir += '/set26_B0_1T_l_3m_Post_Rm_3_first_cell_center_crossing/'
# save_dir += '/set27_B0_1T_l_3m_Post_Rm_3_first_cell_center_crossing/'
# save_dir += '/set28_B0_1T_l_10m_Post_Rm_3_first_cell_center_crossing/'
# save_dir += '/set29_B0_1T_l_3m_Post_Rm_2_first_cell_center_crossing/'
# save_dir += '/set30_B0_1T_l_3m_Post_Rm_3_first_cell_center_crossing/'
# save_dir += '/set31_B0_1T_l_3m_Post_Rm_3_intervals/'
# save_dir += '/set32_B0_1T_l_1m_Post_Rm_3_intervals/'
# save_dir += '/set33_B0_1T_l_3m_Post_Rm_3_intervals/'
# save_dir += '/set34_B0_1T_l_3m_Post_Rm_3_intervals/'
# save_dir += '/set36_B0_1T_l_1m_Post_Rm_3_intervals/'
# save_dir += '/set37_B0_1T_l_1m_Post_Rm_3_intervals/'
# save_dir += '/set38_B0_1T_l_1m_Post_Rm_3_intervals_D_T/'
save_dir += '/set39_B0_1T_l_1m_Post_Rm_3_intervals_D_T/'

RF_type = 'electric_transverse'
# E_RF_kVm = 1 # kV/m
# E_RF_kVm = 10  # kV/m
# E_RF_kVm = 25  # kV/m
E_RF_kVm = 50  # kV/m
# E_RF_kVm = 100  # kV/m

# RF_type = 'magnetic_transverse'
# B_RF = 0.01  # T
# B_RF = 0.02  # T
B_RF = 0.04  # T
# B_RF = 0.05  # T
# B_RF = 0.1  # T

# gas_name = 'deuterium'
# gas_name = 'DT_mix'
# gas_name = 'tritium'`

gas_name_list = []
gas_name_list += ['deuterium']
# gas_name_list += ['DT_mix']
# gas_name_list += ['tritium']

use_RF = True
# use_RF = False

absolute_velocity_sampling_type = 'maxwell'
# absolute_velocity_sampling_type = 'const_vth'
r_0 = 0
# r_0 = 1.5
# r_0 = 3.0

# alpha_loop_list = np.round(np.linspace(0.9, 1.1, 11), 2)  # set26
# beta_loop_list = np.round(np.linspace(0, 1, 11), 11)

# alpha_loop_list = np.round(np.linspace(0.9, 1.1, 21), 2)  # set27
# beta_loop_list = np.round(np.linspace(-1, 1, 21), 2)

# alpha_loop_list = np.round(np.linspace(0.6, 1.0, 21), 2)  # set28
# beta_loop_list = np.round(np.linspace(-5, 0, 21), 2)

# alpha_loop_list = np.round(np.linspace(0.8, 1.0, 21), 2)  # set29, set30
# beta_loop_list = np.round(np.linspace(-10, 0, 21), 2)

# alpha_loop_list = np.round(np.linspace(0.8, 1.0, 11), 2)  # set31, 32, 33
# beta_loop_list = np.round(np.linspace(-10, 0, 11), 2)

# alpha_loop_list = np.round(np.linspace(0.9, 1.1, 11), 2)  # set34
# beta_loop_list = np.round(np.linspace(-5, 5, 11), 2)

# alpha_loop_list = np.round(np.linspace(0.8, 1.2, 21), 2)  # set36
# beta_loop_list = np.round(np.linspace(-5, 5, 21), 2)

alpha_loop_list = np.round(np.linspace(0.5, 1.5, 21), 2)  # set37, 39
beta_loop_list = np.round(np.linspace(-10, 10, 21), 2)

# alpha_loop_list = np.round(np.linspace(0.7, 1.3, 21), 2)  # set38
# beta_loop_list = np.round(np.linspace(-5, 5, 21), 2)

# for ind_beta, beta_RF in enumerate(beta_loop_list):
#     for ind_alpha, alpha_RF in enumerate(alpha_loop_list):
# ind_alpha = 0
# ind_alpha = 1
# ind_alpha = 2
# ind_alpha = 4
# ind_alpha = 5
# ind_alpha = 7
# ind_beta = 1
# ind_beta = 2
# ind_beta = 5
# ind_beta = 4
# ind_beta = 3
# alpha = alpha_loop_list[ind_alpha]
# beta = beta_loop_list[ind_beta]

select_alpha_list = []
select_beta_list = []
set_name_list = []

# select_alpha_list += [1.3]
# select_beta_list += [0.0]
# set_name_list += ['1']
#
# select_alpha_list += [1.4]
# select_beta_list += [3.0]
# set_name_list += ['2']
#
# select_alpha_list += [0.8]
# select_beta_list += [0.0]
# set_name_list += ['3']
#
# select_alpha_list += [1.0]
# select_beta_list += [-3.0]
# set_name_list += ['4']
#
# select_alpha_list += [0.6]
# select_beta_list += [-2.0]
# set_name_list += ['5']

select_alpha_list += [1.4]
select_beta_list += [3.0]
set_name_list += ['1']

select_alpha_list += [1.0]
select_beta_list += [-3.0]
set_name_list += ['2']

select_alpha_list += [0.7]
select_beta_list += [-3.0]
set_name_list += ['3']

select_alpha_list += [0.55]
select_beta_list += [-7.0]
set_name_list += ['4']

# ind_set = 0
ind_set = 1
# ind_set = 2
# ind_set = 3

alpha = select_alpha_list[ind_set]
beta = select_beta_list[ind_set]
RF_set_name = set_name_list[ind_set]

for gas_name in gas_name_list:

    title = 'set ' + RF_set_name + ': '
    if RF_type == 'electric_transverse':
        title += '$E_{RF}$=' + str(E_RF_kVm) + 'kV/m, $\\alpha$=' + str(alpha) + ', $\\beta$=' + str(beta)
    elif RF_type == 'magnetic_transverse':
        title += '$B_{RF}$=' + str(B_RF) + 'T, $\\alpha$=' + str(alpha) + ', $\\beta$=' + str(beta)
    title += ', ' + gas_name
    print(title)

    set_name = ''
    if use_RF is False:
        set_name += 'without_RF'
    else:
        if RF_type == 'electric_transverse':
            set_name += 'ERF_' + str(E_RF_kVm)
        elif RF_type == 'magnetic_transverse':
            set_name += 'BRF_' + str(B_RF)
        set_name += '_alpha_' + str(alpha)
        set_name += '_beta_' + str(beta)
    if absolute_velocity_sampling_type == 'const_vth':
        set_name = 'const_vth_' + set_name
    if r_0 > 0:
        set_name += '_r0_' + str(r_0) + '_' + set_name
    # set_name += '_antiresonant'
    set_name += '_' + gas_name

    save_dir_curr = save_dir + set_name

    # load runs data
    data_dict_file = save_dir_curr + '.pickle'
    with open(data_dict_file, 'rb') as fid:
        data_dict = pickle.load(fid)
    settings_file = save_dir + 'settings.pickle'
    with open(settings_file, 'rb') as fid:
        settings = pickle.load(fid)
    field_dict_file = save_dir + 'field_dict.pickle'
    with open(field_dict_file, 'rb') as fid:
        field_dict = pickle.load(fid)

    for key in data_dict.keys():
        data_dict[key] = np.array(data_dict[key])

    # divide the phase space by the angle
    theta_LC = 360 / (2 * np.pi) * np.arcsin(1 / np.sqrt(field_dict['Rm']))
    N_theta_LC = 1
    N_theta_T = 1
    N_theta = 2 * N_theta_LC + N_theta_T
    dtheta_LC = theta_LC / N_theta_LC
    dtheta_T = (180 - 2 * theta_LC) / N_theta_T
    theta_bins_max_list = [dtheta_LC]
    for i in range(N_theta_LC - 1):
        theta_bins_max_list += [theta_bins_max_list[-1] + dtheta_LC]
    for i in range(N_theta_T):
        theta_bins_max_list += [theta_bins_max_list[-1] + dtheta_T]
    for i in range(N_theta_LC):
        theta_bins_max_list += [theta_bins_max_list[-1] + dtheta_LC]
    theta_bins_min_list = [0] + theta_bins_max_list[:-1]

    number_of_time_intervals = data_dict['t'].shape[1]

    particles_counter_mat_3d = np.zeros([N_theta, N_theta, number_of_time_intervals])
    particles_counter_mat2_3d = np.zeros([N_theta, N_theta, number_of_time_intervals])

    from matplotlib import cm

    colors = cm.rainbow(np.linspace(0, 1, number_of_time_intervals))
    # colors = ['b', 'g', 'r']
    # colors = ['r', 'g', 'b']

    for ind_t in range(number_of_time_intervals):
        # for ind_t in [0, 10]:
        # for ind_t in [0, 1]:
        # for ind_t in [0, 10, 20]:
        #     print(ind_t)

        inds_particles = range(data_dict['t'].shape[0])
        # inds_particles = [0, 1, 2]
        # inds_particles = range(1001)
        if ind_t == 0:
            print('num particles = ' + str(len(inds_particles)))

        v = data_dict['v'][inds_particles, ind_t]
        v0 = data_dict['v'][inds_particles, 0]
        vt = data_dict['v_transverse'][inds_particles, ind_t]
        vt0 = data_dict['v_transverse'][inds_particles, 0]
        vz = data_dict['v_axial'][inds_particles, ind_t]
        vz0 = data_dict['v_axial'][inds_particles, 0]
        theta = np.mod(360 / (2 * np.pi) * np.arctan(vt / vz), 180)
        Bz = data_dict['Bz'][inds_particles, ind_t]
        Bz0 = data_dict['Bz'][inds_particles, 0]
        vt_adjusted = vt * np.sqrt(
            Bz0 / Bz)  # no need to adjust v to B_min because energy is conserved (assuming no RF)

        det = vz ** 2.0 + vt ** 2.0 * (1 - Bz0 / Bz)
        inds_positive = np.where(det > 0)[0]
        vz_adjusted = np.zeros(len(inds_particles))
        vz_adjusted[inds_positive] = np.sign(vz0[inds_positive]) * np.sqrt(det[inds_positive])

        theta_adjusted = 90.0 * np.ones(len(inds_particles))
        theta_adjusted[inds_positive] = np.mod(
            360 / (2 * np.pi) * np.arctan(vt_adjusted[inds_positive] / vz_adjusted[inds_positive]), 180)

        color = colors[ind_t]

        # track if a particle left the population, and then cancel counting it for the following times
        if ind_t == 0:
            particles_counter_mat = np.zeros([N_theta, N_theta])
            inds_bins_ini = []
            cancelled_particles = np.zeros(len(inds_particles))
        particles_counter_mat2 = np.zeros([N_theta, N_theta])

        for ind_p in inds_particles:
            # if not cancelled_particles[ind_p]:
            theta_curr = theta_adjusted[ind_p]
            ind_bin_fin = [k for k, (t1, t2) in enumerate(zip(theta_bins_min_list, theta_bins_max_list))
                           if theta_curr > t1 and theta_curr <= t2][0]
            if ind_t == 0:
                inds_bins_ini += [ind_bin_fin]
                particles_counter_mat[ind_bin_fin, ind_bin_fin] += 1

            ind_bin_ini = inds_bins_ini[ind_p]

            particles_counter_mat2[ind_bin_ini, ind_bin_fin] += 1
            if ind_bin_fin != ind_bin_ini:
                if not cancelled_particles[ind_p]:
                    particles_counter_mat[ind_bin_ini, ind_bin_ini] -= 1
                    particles_counter_mat[ind_bin_ini, ind_bin_fin] += 1
                    cancelled_particles[ind_p] = 1

        if ind_t == 0:
            N0 = copy.deepcopy(np.diag(particles_counter_mat))

        particles_counter_mat_3d[:, :, ind_t] = particles_counter_mat
        particles_counter_mat2_3d[:, :, ind_t] = particles_counter_mat2

    # divide all densities by the parent initial density
    for ind_t in range(number_of_time_intervals):
        for ind_bin in range(N_theta):
            particles_counter_mat_3d[ind_bin, :, ind_t] /= (1.0 * N0[ind_bin])
            particles_counter_mat2_3d[ind_bin, :, ind_t] /= (1.0 * N0[ind_bin])
    particles_counter_mat2_for_fit_3d = copy.deepcopy(particles_counter_mat2_3d)

    t_array = data_dict['t'][0]
    # t_array /= settings['l'] / settings['v_th']
    # t_array /= settings['l'] / settings['v_th_for_cyc']
    # define v_th ref
    _, _, mi, _, Z_ion = define_plasma_parameters(gas_name='tritium')
    v_th_ref = get_thermal_velocity(settings['T_keV'] * 1e3, mi, settings['kB_eV'])
    t_array /= settings['l'] / v_th_ref

    colors = cm.rainbow(np.linspace(0, 1, N_theta))
    nu_decay_list = []
    nu_mat = np.zeros([N_theta, N_theta])

    do_fit = True
    # do_fit = False

    inds_t_array = range(len(t_array))
    fig, ax = plt.subplots(1, 1,
                           figsize=(6, 6),
                           )
    # fig.suptitle(title)

    ## calculate the saturation value to estimate the rate
    # inds_t_saturation = range(7, 21)
    # inds_t_saturation = range(2, 3)
    inds_t_saturation = range(15, 31)
    # inds_t_saturation = range(len(t_array))

    N_curr = particles_counter_mat2_3d[0, 1, :]
    saturation_value = np.mean(N_curr[inds_t_saturation])
    label = '$\\bar{N}_{rc}$'
    label += '=' + '{:.3f}'.format(saturation_value)
    ax.plot(t_array, N_curr, color='b', linestyle='-', label=label)
    ax.hlines(saturation_value, t_array[inds_t_saturation[0]], t_array[inds_t_saturation[-1]],
              color='b', linewidth=2, linestyle='--')

    N_curr = particles_counter_mat2_3d[2, 1, :]
    saturation_value = np.mean(N_curr[inds_t_saturation])
    label = '$\\bar{N}_{lc}$'
    label += '=' + '{:.3f}'.format(saturation_value)
    ax.plot(t_array, N_curr, color='g', linestyle='-', label=label)
    ax.hlines(saturation_value, t_array[inds_t_saturation[0]], t_array[inds_t_saturation[-1]],
              color='g', linewidth=2, linestyle='--')

    N_curr = particles_counter_mat2_3d[1, 0, :]
    saturation_value = np.mean(N_curr[inds_t_saturation])
    label = '$\\bar{N}_{cr}$'
    label += '=' + '{:.3f}'.format(saturation_value)
    ax.plot(t_array, N_curr, color='r', linestyle='-', label=label)
    ax.hlines(saturation_value, t_array[inds_t_saturation[0]], t_array[inds_t_saturation[-1]],
              color='r', linewidth=2, linestyle='--')

    N_curr = particles_counter_mat2_3d[1, 2, :]
    saturation_value = np.mean(N_curr[inds_t_saturation])
    label = '$\\bar{N}_{cl}$'
    label += '=' + '{:.3f}'.format(saturation_value)
    ax.plot(t_array, N_curr, color='orange', linestyle='-', label=label)
    ax.hlines(saturation_value, t_array[inds_t_saturation[0]], t_array[inds_t_saturation[-1]],
              color='orange', linewidth=2, linestyle='--')

    # ax.plot(t_array, particles_counter_mat2_3d[2, 1, :], color='g', linestyle='-', label='$\\bar{N}_{lc}$')
    # ax.plot(t_array, particles_counter_mat2_3d[1, 0, :], color='r', linestyle='-', label='$\\bar{N}_{cr}$')
    # ax.plot(t_array, particles_counter_mat2_3d[1, 2, :], color='orange', linestyle='-', label='$\\bar{N}_{cl}$')

    # ax.set_xlabel('$t \\cdot v_{th} / l$')
    # ax.set_xlabel('$t \\cdot v_{th,T} / l$')
    # ax.set_xlabel('$t / \\tau_{th}$',
    #               fontsize=20)
    # ax.set_ylim([0, 0.9])
    ax.set_ylim([0, 1.0])
    ax.set_xlim([0, 1.75])
    # ax.set_title(gas_name)
    # ax.set_title(title)
    ax.legend(loc='upper left',
              fontsize=20)
    # ax.legend(loc='center right', fontsize=16)
    # ax.legend(fontsize=16)
    ax.grid(True)
    # text = '(a)'
    if gas_name == 'deuterium':
        gas_name_shorthand = 'D'
    if gas_name == 'tritium':
        gas_name_shorthand = 'T'
    # text = '(' + gas_name_shorthand + ',' + RF_set_name + ')'
    text = RF_set_name + ' (' + gas_name_shorthand + ')'
    plt.text(0.99, 0.98, text, fontdict={'fontname': 'times new roman', 'weight': 'bold', 'size': 30},
             horizontalalignment='right', verticalalignment='top', color='k',
             transform=fig.axes[0].transAxes)
    # fig.set_tight_layout({'pad': 0.5, 'rect': (0, 0, 1, 0.95)})
    fig.set_layout_engine(layout='tight')

    ## save plots to file
    # save_fig_dir = '../../../Papers/texts/paper2022/pics/'
    # file_name = 'saturation_set_' + gas_name_shorthand + '_' + RF_set_name
    # if RF_type == 'magnetic_transverse':
    #     file_name = 'BRF_' + file_name
    # # file_name += '_mod'
    # beingsaved = plt.gcf()
    # beingsaved.savefig(save_fig_dir + file_name + '.eps', format='eps')


import scipy.io
import os
import Utils.OneHot
import pandas as pd

'''
    TFName represents name of TFs
    DataSetName represents name of Datasets
'''
def seq2kmer(seq):
    """
    Convert original sequence to kmers

    Arguments:
    seq -- str, original sequence.
    k -- int, kmer of length k specified.

    Returns:
    kmers -- str, kmers separated by space

    """
    kmer = [seq[x:x + 3] for x in range(len(seq) + 1 - 3)]
    kmers = " ".join(kmer)
    return kmers

def DataReader(TFName, DataSetName, type):
    FeatureMatrix = []
    DenseLabels = []
    if DataSetName == 'ChIP-exo':
        '''
            AR -> labels
            GR -> label
        '''
        DenseLabels = scipy.io.loadmat(os.path.dirname(__file__) + '/ChIP-exo/' + TFName + '/label.mat')['labels']
        with open(os.path.dirname(__file__) + '/ChIP-exo/' + TFName + '/sequence.txt', 'r') as SReader:
            for line in SReader.readlines():
                FeatureMatrix.append(list(map(str, line.rstrip('\n'))))
        FeatureMatrix = Utils.OneHot.OneHot(sequence=FeatureMatrix, number=len(FeatureMatrix), nucleotide=4,
                                            length=DenseLabels.shape[1])
    else:
        with open(os.path.dirname(__file__) + '/ChIP-seq/' + TFName + '/baseline/seq_' + type + '.txt', 'r') as SReader, open(
                  os.path.dirname(__file__) + '/ChIP-seq/' + TFName + '/baseline/lab_' + type + '.txt', 'r') as LReader:
            for SRLine, LRLine in zip(SReader.readlines(), LReader.readlines()):
                FeatureMatrix.append(SRLine.rstrip('\n'))
                DenseLabels.append(list(map(int, [label for label in LRLine.rstrip('\n')])))
        FeatureMatrix = Utils.OneHot.OneHot(sequence=FeatureMatrix, number=len(FeatureMatrix), nucleotide=4,
                                            length=len(DenseLabels[1]))
    return FeatureMatrix, DenseLabels

def DataReaderPrecit(path):

    Dataset = pd.read_csv(path, header=None)
    Sequence = Dataset[0].tolist()
    FeatureMatrix = Utils.OneHot.OneHot(sequence=Sequence, number=len(Sequence), nucleotide=4,
                                        length=100)
    return FeatureMatrix

def DataReaderBERT(TFName, DataSetName, KMER):

    trainDataset = pd.read_csv('../Dataset/' + DataSetName + '/' + TFName + '/' + str(KMER) + '-mer/train.txt', sep='\t')

    TrainSequence = trainDataset['sequence'].tolist()
    TrainDenseLabels = []
    TrainLabels = trainDataset['label'].tolist()

    for row in trainDataset['denseLabel']:
        TrainDenseLabels.append(list(map(int, [label for label in row])))

    testDataset = pd.read_csv('../Dataset/' + DataSetName + '/' + TFName + '/' + str(KMER) + '-mer/test.txt', sep='\t')

    TestSequence = testDataset['sequence'].tolist()
    TestDenseLabels = []
    TestLabels = testDataset['label'].tolist()

    for row in testDataset['denseLabel']:
        TestDenseLabels.append(list(map(int, [label for label in row])))

    return TrainSequence, TrainDenseLabels, TrainLabels, TestSequence, TestDenseLabels, TestLabels

def DataReaderSequence(TFName, DataSetName, KMER):

    trainDataset = pd.read_csv('../Dataset/' + DataSetName + '/' + TFName + '/' + str(KMER) + '-mer/train.txt', sep='\t')

    TrainSequence = trainDataset['sequence'].tolist()
    TrainLabels = trainDataset['label'].tolist()

    testDataset = pd.read_csv('../Dataset/' + DataSetName + '/' + TFName + '/' + str(KMER) + '-mer/test.txt', sep='\t')

    TestSequence = testDataset['sequence'].tolist()
    TestLabels = testDataset['label'].tolist()

    return TrainSequence, TrainLabels, TestSequence, TestLabels

def DataReaderPrecitBERT(path):

    Dataset = pd.read_csv(path, header=None)
    Sequence = Dataset[0].tolist()
    Sequence = list(map(seq2kmer, Sequence))

    return Sequence


from base.method import *
import unittest
from page.openS import *
from utils.excel_data import excel_data as e
class test_role(unittest.TestCase):
    def setUp(self):
        self.obj=method()
        self.excel_data =e()
        #获取实际结果列数
        self.r_col=self.excel_data.get_result_col()
        #获取响应时间列数
        self.t_col=self.excel_data.get_time_col()
        self.f = "public_url.json"
        self.f1="requestData_role.json"
        self.f2="data_role.xls"
    def write2excel(self,r,col,f):
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.json()["status"], 200)
        writeResult(col, self.r_col, "pass",f)
        writeResult(col, self.t_col, r.elapsed.total_seconds(),f)
    def tearDown(self):
        pass

    # 登陆接口
    def test_001(self):
        r = self.obj.post(1, self.f, self.f2)
        writeToken(r.json()["data"]["token"])
        # r=self.obj.post_token(2)
        self.write2excel(r, 1, self.f2)
    def test_002(self):
        #查询角色列表
        r=self.obj.get_t(2,self.f1,self.f2)
        self.write2excel(r,2,self.f2)
        #添加角色
        r=self.obj.post_t(3,self.f1,self.f2)
        self.write2excel(r,3,self.f2)
        #编辑角色
        level=r.json()["data"]["level"]
        name=r.json()["data"]["name"]
        tenantId=r.json()["data"]["tenantId"]
        id=r.json()["data"]["id"]
        l=[]
        li=[]
        l.append("level")
        li.append(level)
        l.append("name")
        li.append(name)
        l.append("tenantId")
        li.append(tenantId)
        l.append("id")
        li.append(id)
        r=self.obj.post_c(9,self.f1,self.f2,l,li)
        self.write2excel(r,9,self.f2)
        #角色授权加载自定义菜单树
        r=self.obj.get_v(4,id,self.f1,self.f2)
        self.write2excel(r,4,self.f2)
        #角色授权菜单保存
        l=[]
        li=[]
        l.append("roleId")
        li.append(id)
        r=self.obj.post_c(5,self.f1,self.f2,l,li)
        self.write2excel(r,5,self.f2)
        #通过角色id查询数据看板范围树
        r=self.obj.get_v(6,id,self.f1,self.f2)
        self.write2excel(r,6,self.f2)
        #角色授权数据看板保存
        l=[]
        li=[]
        l.append("roleId")
        li.append(id)
        r=self.obj.post_c(7,self.f1,self.f2,l,li)
        self.write2excel(r,7,self.f2)
        #删除角色
        r=self.obj.get_v(8,id,self.f1,self.f2)
        self.write2excel(r,8,self.f2)
if __name__ == '__main__':
    unittest.main()


# from PIL import Image
from functions import load_data, get_df, get_month, get_day, get_percentage, get_df_pv

import streamlit as st
import plotly.express as px

def main():

    # load data
    df = load_data()
    # obtain objects from data
    cc_names, df_minmax, col_pv = get_df(df)

    # Streamlit web
    st.set_page_config(layout="wide")

    # # Cryptocurrency image
    # row_0_spacer_1, row_0, row_0_spacer_2 = st.columns((0.1,1,0.1))
    # cc_image = Image.open('bitcoin.jpg')
    # with row_0:
    #     st.image(cc_image)
    # #''' image here'''

    # ROW 1: introduction
    row_1_spacer_1, row_1, row_1_spacer_2 = st.columns((0.1, 1, 0.1))

    row_1.title(
        ":currency_exchange:Data Analysis: Historical Price of 23 Crypto-currencies"
    )

    row_1.subheader(
        """This App aims to provide various form of visualization, charts, graphs, and tables for you to analyze 23 crypto-currencies on the market. Some are well known to all, such as Bitcoin and Dogecoin, some are only popular among enthusiasts. Whether you are familiar with crypto-currencies or not, have fun with this simple dashboard!"""
    )

    row_1.markdown(
        """:star2: For all charts, you are able to zoom in/out, and focus on a range of dates of your interest:star2:"""
    )

    row_1.write(
        """:chart: Check the data source [here](https://www.kaggle.com/sudalairajkumar/cryptocurrencypricehistory):chart:"""
    )
    row_1.write(
        """:notebook_with_decorative_cover: Check Data Wrangling Notebook [here](https://nbviewer.org/github/honghusldl/cryptocurrency-dashboard/blob/main/preprocessing/data_wrangling.ipynb):notebook_with_decorative_cover:"""
    )
    # divider
    row_1.markdown("---")

    # ROW 2: Life span table
    row_2_spacer_1, row_2_1, row_2_2, row_2_spacer_2 = st.columns((0.3, 1.7, 1.3, 0.3))

    with row_2_1:
        st.subheader("Life Span")

        st.write(
            ":bar_chart:Each crypto-currency has distinct life span. Here you can discover the life span for each currency"
        )

        fig_bar_lifespan = st.plotly_chart(
            px.bar(
                data_frame=df_minmax,
                x=df_minmax.index,
                y="Duration",
                color=df_minmax.index,
                title="Duration on Market by Currency",
                hover_data=["First Day", "Last Day"],
                height=650,
                width=850,
                labels={"Duration": "Duration on Market in Days"},
                text="Duration",
            )
            .update_traces(textposition="outside")
            .update_layout(showlegend=False)
        )

    with row_2_2:
        st.write("")
        st.dataframe(data=df_minmax, height=1000)
        st.write(":warning:The data source was update on 2021-7-6")

    st.write("---")

    # ROW 3: overview line chart market cap

    row_3_spacer_1, row_3_1, row_3_2, row_3_spacer_2 = st.columns((0.5, 4, 1, 0.5))
    with row_3_2:
        st.subheader(":book:Capitalpedia")
        st.markdown(
            ":ledger:***Market Capitalization*** is the total market value of the shares outstanding of a publicly traded cryptocurrency."
        )
        st.markdown(
            ":ledger:It is equal to **share price** times **number of shares outstanding**."
        )
        st.markdown(
            ":ledger:Market capitalization is one of the metrics to reflect the value of a cryptocurrency. It is even more useful for investors to understand the whole picture of a specific cryptocurrency since it leverages both volume and price. "
        )
        st.markdown(
            ":ledger:Cryptocurrencies are categorized into three classes based on their market capitalization"
        )
        st.markdown(
            ":one:Large-cap cryptocurrencies: with over $10 billion market cap. Lower risks for investors."
        )
        st.markdown(
            ":two:Mid-cap cryptocurrencies: with $1 to $10 billion market cap. Great growth potential but high risks."
        )
        st.markdown(
            ":three:Small-cap cryptocurrencies: with less than $1 billion market cap. High risks due to massive volatility.([Reference](https://www.coinbase.com/learn/crypto-basics/what-is-market-cap))"
        )

    with row_3_1:
        st.subheader("Overview: Market Capitalization")
        fig_line_cap = st.plotly_chart(
            px.line(
                data_frame=df,
                x="Date",
                y="Market_Capitalization",
                color="Name",
                width=1100,
                height=600,
                labels={
                    "Market_Capitalization": "Market Cap in USD",
                    "Volume": "Traded Volume",
                },
                template="seaborn",
            )
            .add_vline(x="2017-01-01", line_width=3, line_dash="dot", line_color="red")
            .add_annotation(
                x="2017-01-01",
                y=max(df["Market_Capitalization"]) * 0.85,
                text="'Cryptocurrency Fever' Began",
            )
            .update_layout(showlegend=False)
        )

        st.markdown(""":bookmark:**PC Component Market**:bookmark:""")
        st.markdown(
            """After cryptocurrency went in vogue in the middle of 2017, well-prepared mining companies and the enthusiasm among the general public causes destructive fluctuation on market.
      People have been forced to pay more than twice amount of money to hunt for a GPU. Majority of PC components rises in price due to shortage in supply. Used mining GPU are placed on market with a unresonable price.
      There are more and more disturbing the PC component market, especially GPU. """
        )

    st.markdown("---")
    row_4_spacer_1, row_4_1, row_4_2, row_4_3, row_4_spacer_2 = st.columns(
        (0.5, 1, 3, 1, 0.5)
    )
    with row_4_1:
        st.markdown(":point_down:Choose Your Day of Interest")
        # get year, month, day for pie chart
        y = list(range(2017, 2022))
        year = st.selectbox("Select Year", options=y)
        m = get_month(year)
        month = st.selectbox("Select Month", options=m)
        d = get_day(year, month)
        day = st.selectbox("Select Day", options=d)

        st.write(":bookmark:Dates range from 2017-1-1 to 2021-7-6.")

    with row_4_2:
        st.write(
            "Due to limit of space, full information about percentages of market cap are showed via table :point_right: "
        )
        st.write(
            "Percentage of Market Capitalization by Currency on a specific date after 2017 when the 'Fever' begun"
        )
        target_date = f"{year}-{month}-{day}"
        fig_pie_cap = st.plotly_chart(
            px.pie(
                data_frame=df.query(f"Date == '{target_date}'"),
                values="Market_Capitalization",
                names="Name",
                height=550,
            ).update_traces(textposition="inside", textinfo="percent+label")
        )

        st.markdown(
            "Higher percentage indicates that a cryptocurrency has higher total market value and stand strong in the crypto market. **However, as the line graph shown, market capitalization of a cryptocurrency could evaporate drastically due to market sentiment, societal responses and even governmental intervention.**"
        )

    with row_4_3:
        df_percentage = df.query(f"Date == '{target_date}'")
        df_cap_percentage = get_percentage(df_percentage)
        st.dataframe(data=df_cap_percentage, height=1000)

    st.write("---")

    row_5_spacer_1, row_5_1, row_5_spacer_2 = st.columns((0.2, 1, 0.2))
    # get df for analyzing price and volume
    df_pv = get_df_pv(df)

    with row_5_1:
        st.subheader("Price & Volume")
        st.write(
            "Price and volume are two important metrics reflecting the value of a cryptocurrency. Compared to market capitalization, they depict a cryptocurrency from different angles."
        )
        st.markdown(
            ":chart_with_upwards_trend:**Explore Opening price, Closing Price, Daily Highest and Lowest Price by selecting name of your interest.**"
        )

    row_6_spacer_1, box1, box2, box3, row_6_spacer_2 = st.columns((0.6, 1, 1, 1, 0.6))
    with box1:
        currency_name = st.selectbox("Select Cryptocurrency", options=cc_names)
    with box2:
        price_type_line = st.selectbox("Select type of price (Line)", options=col_pv)
    with box3:
        price_type_bar = st.selectbox("Select type of price (Bar)", options=col_pv)

    row_7_spacer_1, row_7_1, row_7_spacer_2 = st.columns((0.2, 1, 0.2))
    with row_7_1:
        df_query = df_pv.query("Name == @currency_name")
        fig_line_price = st.plotly_chart(
            px.line(
                data_frame=df_pv.query("Name == @currency_name"),
                x="Date",
                y=price_type_line,
                labels={
                    "High": "Highest Price in USD",
                    "Low": "Lowest Price in USD",
                    "Open": "Opening Price in USD",
                    "Close": "Closing Price in USD",
                },
                height=500,
                width=1100,
            )
            .add_bar(
                x=df_query["Date"], y=df_query[price_type_bar], name=price_type_bar
            )
            .update_layout(showlegend=False)
        )
        st.markdown(
            ":chart_with_upwards_trend:**Explore Volume by specifiying the date.**"
        )

    row_8_spacer_1, box4, box5, box6, row_8_spacer_2 = st.columns((0.6, 1, 1, 1, 0.6))
    with box4:
        y2 = list(range(2017, 2022))
        year2 = st.selectbox("Select Year:", options=y2)
    with box5:
        m2 = get_month(year2)
        month2 = st.selectbox("Select Month:", options=m2)
    with box6:
        d2 = get_day(year2, month2)
        day2 = st.selectbox("Select Day:", options=d2)
    row_8_spacer_1, row_8_1, row_8_2, row_8_spacer_2 = st.columns((0.6, 2, 1, 0.6))
    with row_8_1:
        target_date2 = f"{year2}-{month2}-{day2}"
        fig_bubble_volume = st.plotly_chart(
            px.scatter(
                data_frame=df_pv.query(f"Date == '{target_date2}'"),
                x="Close",
                y="Market_Capitalization",
                labels={
                    "Close": "Closing Price",
                    "Market_Capitalization": "Market Capitalization",
                },
                size="Volume",
                color="Name",
                log_x=True,
                log_y=True,
                width=750,
                height=500,
            )
        )
    with row_8_2:
        st.markdown(
            ":warning:Note: The chart is applied with log transformation on both X and Y axis."
        )
        st.markdown(
            ":books:Volume reflects activity or intensity of a cryptocurrency. In general, large volume indicates large number of participants in trading."
        )
        st.markdown(
            ":books:Since volume reflects activity, it might reveal the characteristics of a cryptocurrency. For instance, Bitcoin requires high-end PC specification to solve math problems in blockchain, which makes Bitcoin finite in amount and time-comsuming. Thus, Bitcoin has higher value and attracts more participants on the market, resulting in larger volume."
        )
        st.markdown(
            ":books:Volume fluctuates at various stages, and it is not necessarily correlated with price."
        )
        st.markdown(
            ":books:As mentioned earlier, volume and price together are able to reveal the market's trend, hotspot and so on. Let's discover more about volume and price below!"
        )

    row_9_spacer_1, row_9_1, row_9_2, row_9_3, row_9_spacer_2 = st.columns(
        (0.6, 1, 1, 1, 0.6)
    )
    with row_9_1:
        st.markdown(
            ":chart_with_upwards_trend:**Explore Closing price and Volume Trend.**"
        )
        currency_name_pv = st.selectbox("Select Cryptocurrency:", options=cc_names)

    row_10_spacer_1, row_10_1, row_10_2, row_10_spacer_2 = st.columns((0.8, 3, 1, 0.8))
    with row_10_1:
        df_query2 = df_pv.query("Name == @currency_name_pv")
        fig_line_volume_price = st.plotly_chart(
            px.line(
                data_frame=df_query2.melt(
                    id_vars=["Name", "Date", "High", "Open", "Low"]
                ),
                x="Date",
                y="value",
                color="variable",
                height=500,
                width=1000,
                log_y=True,
            )
        )

if __name__ == "__main__":
    main()


import pickle

from PyDictionary import PyDictionary
from gensim.models import KeyedVectors
from nltk.stem import WordNetLemmatizer
from nltk.corpus import wordnet
from pyinflect import getInflection
from pattern.text.en import superlative, pluralize, comparative
from complexity_prediction.complexity_prediction import predict

def create_model(path, limit):
    """
    Store a gensim object of the current word2vec embeddings using pickle; the file will be named word2vec_model.sav

    Parameters:
    path (str): path to the word2vec pre-trained model
    limit (int): the number of embeddings to be saved in the gensim model
    """
    model = KeyedVectors.load_word2vec_format(path, limit=limit)
    pickle.dump(model, open("word2vec_model.sav", "wb"))

def tagConversion(tag):
    """
    Convert a complex nltk POS tag to a simpler wornet POS tag

    Parameters:
    tag (str): a nltk POS tag

    Returns:
    str: a corresponding wordnet POS tag
    """
    if tag.startswith('J'):
        return wordnet.ADJ
    elif tag.startswith('V'):
        return wordnet.VERB
    elif tag.startswith('N'):
        return wordnet.NOUN
    elif tag.startswith('R'):
        return wordnet.ADV

def debug_print(msg, debug=False):
    """
    Prints a debug message

    Parameters:
    msg (str): a message to be displayed
    debug (boolean): if the debug mode is enabled or not
    """
    if debug:
        print("[SYNONYMS GENERATION] " + msg)

def get_synonyms(word, nltk_pos, path, debug=False):
    """
    Get a list of synonyms for a given word and its corresponding POS

    The function selects only those synonyms that have a cosine similarity above the average similarity of all
    found synonyms; also, the function performs morphological changes to the words to preserve gramatical corectness.

    Parameters:
    word (str): the word for which the synonyms must be found
    nltk_pos (str): the nltk POS tag for the word given
    path (str): path to the word2vec saved model
    debug (boolean) (default:False): if the debug mode is enabled or not

    Returns:
    list: a list of touples formed by the synonym and its cosine similarity to the original word
    """
    dictionary = PyDictionary()
    model = pickle.load(open(path, "rb"))

    lemmatizer = WordNetLemmatizer()
    word = lemmatizer.lemmatize(word, pos=tagConversion(nltk_pos))

    synonyms = dictionary.synonym(word)

    if synonyms == None:
        return []

    # loop through the synonyms and compute the cosine similarities
    selected_synonyms = []
    average_similarity = 0
    for syn in synonyms:
        if syn in model.key_to_index and word in model.key_to_index:
            similarity = model.similarity(word, syn)
            debug_print("Similarity between " + word + " and " + syn + " is " + str(similarity), debug=debug)
            selected_synonyms.append((syn, similarity))
            average_similarity += similarity
        else:
            debug_print("Similarity between " + word + " and " + syn + " is unknown", debug=debug)

    if word not in model.key_to_index:
        for syn in synonyms:
            selected_synonyms.append((syn, 0))

    if len(selected_synonyms) == 0:
        return []

    average_similarity /= len(selected_synonyms)
    selected_synonyms = list(filter(lambda x: x[1] >= average_similarity, selected_synonyms))

    # do morphological changes to the synonyms
    for i in range(len(selected_synonyms)):
        if nltk_pos in ["VBD", "VBP", "VBZ", "VBG", "VBN"]:
            inflection = getInflection(selected_synonyms[i][0], nltk_pos)
            if inflection != None:
                selected_synonyms[i] = (inflection[0], selected_synonyms[i][1])
        elif nltk_pos in ["JJS", "RBS"]:
            selected_synonyms[i] = (superlative(selected_synonyms[i][0]), selected_synonyms[i][1])
        elif nltk_pos in ["NNS"]:
            selected_synonyms[i] = (pluralize(selected_synonyms[i][0]), selected_synonyms[i][1])
        elif nltk_pos in ["JJR", "RBR"]:
            selected_synonyms[i] = (comparative(selected_synonyms[i][0]), selected_synonyms[i][1])

    return selected_synonyms

def get_synonyms_transformers(word, nltk_pos, model, dictionary_unigrams, total, debug=False):
    """
    Get a list of synonyms for a given word and its corresponding POS

    This function shall be used in the transformer pipeline to determine which synonyms remain as candidates
    for the sentence ranking stage. This function will select only those synonyms that are predicted by the
    complexity prediction model as being easy words

    Parameters:
    word (str): the word for which the synonyms must be found
    nltk_pos (str): the nltk POS tag for the word given
    model (scikit-learn): the complexity prediction model to be used
    dictionary_unigrams (dict): a dictionary of unigrams from a language model
    total (int): the total number of words in the language model
    debug (bool): weather or not to print informations to stdout

    Returns:
    list: a list of touples formed by the synonym and the value 0 (the second value is for compatibility purposes and shall be ignored)
    """
    dictionary = PyDictionary()

    lemmatizer = WordNetLemmatizer()
    word = lemmatizer.lemmatize(word, pos=tagConversion(nltk_pos))

    synonyms = dictionary.synonym(word)

    if synonyms == None:
        return []

    selected_synonyms = []
    for syn in synonyms:
        tokens = syn.split(" ")
        simple = True
        for token in tokens:
            if predict(model, dictionary_unigrams, total, token) == 1:
                simple = False
                break
        if simple:
            debug_print("Synonym " + syn + " is a simple word", debug=debug)
            selected_synonyms.append((syn, 0))
        else:
            debug_print("Synonym " + syn + " is not a simple word", debug=debug)

    for i in range(len(selected_synonyms)):
        if nltk_pos in ["VBD", "VBP", "VBZ", "VBG", "VBN"]:
            inflection = getInflection(selected_synonyms[i][0], nltk_pos)
            if inflection != None:
                selected_synonyms[i] = (inflection[0], selected_synonyms[i][1])
        elif nltk_pos in ["JJS", "RBS"]:
            selected_synonyms[i] = (superlative(selected_synonyms[i][0]), selected_synonyms[i][1])
        elif nltk_pos in ["NNS"]:
            selected_synonyms[i] = (pluralize(selected_synonyms[i][0]), selected_synonyms[i][1])
        elif nltk_pos in ["JJR", "RBR"]:
            selected_synonyms[i] = (comparative(selected_synonyms[i][0]), selected_synonyms[i][1])

    return selected_synonyms


#!/usr/bin/env python
"""
Assignments from ROS for Beginners:
Author: Anis Koubaa
Modifications: Pranav.S (spranav1911@gmail.com)
Date of modification: 05-06-2022

"""
import numpy as np
import cv2

def filter_color(rgb_image, lower_bound_color, upper_bound_color):
    #convert the image into the HSV color space
    hsv_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2HSV)
    #cv2.imshow("hsv image",hsv_image)

    #find the upper and lower bounds of the yellow color (tennis ball)
    yellowLower =(30, 150, 100)
    yellowUpper = (50, 255, 255)

    #define a mask using the lower and upper bounds of the yellow color
    mask = cv2.inRange(hsv_image, lower_bound_color, upper_bound_color)

    return mask

def getContours(binary_image):
    #_, contours, hierarchy = cv2.findContours(binary_image,
    #                                          cv2.RETR_TREE,
    #                                           cv2.CHAIN_APPROX_SIMPLE)
    contours, hierarchy = cv2.findContours(binary_image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    return contours

def draw_ball_contour(binary_image, rgb_image, contours):
    black_image = np.zeros([binary_image.shape[0], binary_image.shape[1],3],'uint8')

    for c in contours:
        area = cv2.contourArea(c)
        perimeter= cv2.arcLength(c, True)
        ((x, y), radius) = cv2.minEnclosingCircle(c)
        if (area>100):
            cv2.drawContours(rgb_image, [c], -1, (150,250,150), 1)
            cv2.drawContours(black_image, [c], -1, (150,250,150), 1)
            cx, cy = get_contour_center(c)
            cv2.circle(rgb_image, (cx,cy),(int)(radius),(0,0,255),1)
            cv2.circle(black_image, (cx,cy),(int)(radius),(0,0,255),1)
            cv2.circle(black_image, (cx,cy),5,(150,150,255),-1)
            print ("Area: {}, Perimeter: {}".format(area, perimeter))
    print ("number of contours: {}".format(len(contours)))
    #cv2.imshow("RGB Image Contours",rgb_image)
    cv2.imshow("Black Image Contours",black_image)
    return black_image

def get_contour_center(contour):
    M = cv2.moments(contour)
    cx=-1
    cy=-1
    if (M['m00']!=0):
        cx= int(M['m10']/M['m00'])
        cy= int(M['m01']/M['m00'])
    return cx, cy

def main(image):
    #read_video.capture()
    #image_name = "images/copy/tennis_ball_detection"+str(i)+".jpg"
    yellowLower =(30, 150, 100)
    yellowUpper = (50, 255, 255)
    #rgb_image = read_rgb_image(image_name, False)
    binary_image_mask = filter_color(image, yellowLower, yellowUpper)
    contours = getContours(binary_image_mask)
    b_image = draw_ball_contour(binary_image_mask, image,contours)
    #cv2.imwrite("images/black/tennis_ball_detection"+str(i)+".jpg",b_image)
    cv2.waitKey()
    cv2.destroyAllWindows()


from django.shortcuts import render
from loudness.forms import LoudnessForm
from loudness.models import Loudness

def loudness(request):
    if request.method == 'GET':

        loudnesss = Loudness.objects.all()

        form = LoudnessForm()

        context = {
            'loudnesss' : loudnesss,
            'form' : form,
        }

        return render(request, 'loudness.html', context=context)
    elif request.method == 'POST':
        form = LoudnessForm(request.POST)

        if form.is_valid():

            loudnessv = form.save()
            form = LoudnessForm()

        context = {
            'form' : form
        }

        return render(request, 'loudness.html', context=context)


import pickle as pkl
from utils.data_statistic import get_statistic
from data_preprocess.cail_reader import read_cails

def get_label_index(index2num_list, num_threshold_low, num_threshold_high):
    index_list = []
    pair_list = list(zip(index2num_list[0].tolist(), index2num_list[1].tolist()))
    for index, num in pair_list:
        if num_threshold_low <= num <= num_threshold_high:
            index_list.append(index)
    return index_list

if __name__ == '__main__':
    data_path = '/home/nxu/Ladan_tnnls/processed_dataset/CAIL'
    train_dir = data_path + '/normal/small/train_processed_thulac.pkl'
    dataset_fold = read_cails(dir_path=data_path,
                              data_format='normal',
                              version='small')

    train_set, valid_set, test_set = next(dataset_fold)

    law_statisitc, accu_statistic, time_statistic = get_statistic(train_set, law_num=103, accu_num=119,
                                                                  time_num=11)

    print(law_statisitc)

    print(accu_statistic)

    print(time_statistic)

    print(get_label_index(law_statisitc, 100, 200))

    print(get_label_index(accu_statistic, 100, 200))

    tail_law_list = [37, 89, 74, 66, 25, 82, 32, 36, 5, 81, 7, 43, 17, 14, 48, 76, 96, 15, 72, 70, 54, 69, 49, 53, 85, 56, 44]
    tail_charge_list = [37, 85, 96, 114, 99, 86, 106, 36, 71, 104, 72, 95, 34, 39, 51, 23, 109, 62, 54, 31, 59, 60, 81, 110, 76, 26, 19, 64]

    tail_100to200_law = [81, 7, 43, 17, 14, 48, 76, 96, 15, 72, 70, 54, 69, 49, 53, 85, 56, 44]
    tail_100to200_charge = [114, 99, 86, 106, 36, 71, 104, 72, 95, 34, 39, 51, 23, 109, 62, 54, 31, 59, 60, 81, 110, 76, 26, 19, 64]


#!/usr/bin/python3
import pandas as pd
import sys
import os
import json
import matplotlib.pyplot as plt
from texttable import Texttable

def toDf(item_path, dropCols=None):
    pf_list = []
    counter = 0
    for chunk in chunks(item_path):
        if dropCols is not None:
            chunk = chunk.drop(labels=dropCols, axis='columns')
        pf_list.append(chunk)
        counter += len(chunk.index)
        print('read %d of %s' % (counter, item_path))
    return pd.concat(pf_list)

def chunks(item_path):
    chunkSize = 1000000
    return pd.read_csv(item_path, header=0, chunksize=chunkSize)

def min_watch_of_topN(dataset_path, ratings):
    ur = ratings.pivot_table(index=['userId'], values=['movieId'], aggfunc='count').rename(columns={'movieId': 'movies'})
    ur = ur.sort_values(('movies'), ascending=False)
    total_users = len(ur.index)
    percentage_list = []
    min_watched = []
    for i in range(1, 101):
        percent_i_idx = int(round((i * total_users)/100))-1
        if percent_i_idx < 0:
            percent_i_idx = 0
        if percent_i_idx > (total_users-1):
            percent_i_idx = total_users-1
        percent_i_record = ur.iloc[percent_i_idx]
        # print('index percent %d is %d' % (i, percent_i_idx))
        percentage_list.append(i)
        min_watched.append(percent_i_record['movies'].item())
    top_watchs = pd.DataFrame({'top N % of user': percentage_list, 'Minimal watched': min_watched})
    # print(top_watchs)
    top_watchs.plot(x='top N % of user', y='Minimal watched', title='Minimal movies watched / top N percent users', grid=True)
    plt.show()

def min_watched_of_topN(dataset_path, ratings):
    ur = ratings.pivot_table(index=['movieId'], values=['userId'], aggfunc='count').rename(columns={'userId': 'users'})
    ur = ur.sort_values(('users'), ascending=False)
    total_users = len(ur.index)
    percentage_list = []
    min_watched = []
    for i in range(1, 101):
        if (i % 5) != 0 or (i < 5):
            continue
        percent_i_idx = int(round((i * total_users)/100))-1
        if percent_i_idx < 0:
            percent_i_idx = 0
        if percent_i_idx > (total_users-1):
            percent_i_idx = total_users-1
        percent_i_record = ur.iloc[percent_i_idx]
        # print('index percent %d is %d' % (i, percent_i_idx))
        percentage_list.append(i)
        min_watched.append(percent_i_record['users'].item())
    top_watchs = pd.DataFrame({'top N % of movies': percentage_list, 'Minimal watchers': min_watched})
    print(top_watchs.head())
    # print(top_watchs)
    top_watchs.plot(x='top N % of movies', y='Minimal watchers', title='Minimal watchers / top N percent movies', grid=True)
    plt.show()

def group_by_hour(dataset_path, ratings):
    rating_times = pd.to_datetime(ratings['timestamp'], unit='s')
    print(rating_times.min())
    print(rating_times.max())
    ratings['rating_hour'] = rating_times.dt.hour
    ratings['rating_time'] = rating_times
    start = pd.to_datetime('2000/01/01', format='%Y/%m/%d')
    end = pd.to_datetime('2018/01/01', format='%Y/%m/%d')
    period = (end - start).days
    print('period = %d days' % period)
    filtered = ratings[(ratings['rating_time'] >= start) & (ratings['rating_time'] < end)]
    ur = filtered.pivot_table(index=['rating_hour'], values=['userId'], aggfunc='count').rename(columns={'userId': 'watch_times'})
    ur['average watch in hour'] = ur['watch_times']/period
    ur.plot(y='average watch in hour', title='Watched movies time-distributed', grid=True)
    plt.show()
    # print(ur.head(n=24))
    # times = pd.DatetimeIndex(ratings.timestamp)
    # grp_by_hr = ratings.groupby([times.hour])
    # print(grp_by_hr.head())

def top_100_rated_movies(dataset_path):
    ratings = toDf(os.path.join(dataset_path, 'ratings.csv'), dropCols=['timestamp', 'userId'])
    movies = toDf(os.path.join(dataset_path, 'movies.csv'), dropCols=['genres'])
    average_rating = ratings.groupby(by='movieId').mean()
    rating_count = ratings.groupby(by='movieId').count().rename(columns={'rating': 'rating_count'})
    movie_stat = average_rating.merge(rating_count, left_index=True, right_index=True).sort_values(by='rating_count', ascending=False).reset_index()
    movie_stat = movie_stat.merge(movies, left_on='movieId', right_on='movieId')
    total_movies_rated = len(movie_stat.index)
    print(total_movies_rated)
    # print(average_rating.head())
    # print(rating_count.head())
    theMiddle = int(total_movies_rated * 0.2)
    middle100 = movie_stat.iloc[theMiddle-100:theMiddle].drop(columns=['movieId'])
    print('From %d to %d, the mean rate is %f' % (theMiddle-100, theMiddle, middle100['rating'].mean()))
    middle100.plot(y=['rating'], title='Rating of Not so hot movies', grid=True)
    plt.show()
    hot100 = movie_stat.iloc[0:100].drop(columns=['movieId'])
    print('From 0 to 99, the mean rate is %f' % hot100['rating'].mean())
    # print(movie_stat.head())
    tb = Texttable()
    tb.set_cols_align(['l', 'r', 'r'])
    tb.set_cols_dtype(['f', 'i', 't'])
    tb.header(hot100.columns.get_values())
    tb.add_rows(hot100.values, header=False)
    with open('hot_100.txt', 'w') as ofile:
        ofile.write(tb.draw().encode('utf8'))
    hot100.plot(y=['rating'], title='Rating of Hot 100 movies', grid=True)
    hot_sucks = hot100[hot100['rating'] < 3.2]
    tb.reset()
    tb.header(hot_sucks.columns.get_values())
    tb.add_rows(hot_sucks.values, header=False)
    with open('hot_sucks.txt', 'w') as ofile:
        ofile.write(tb.draw().encode('utf8'))
    # print(hot100.mean())
    # print(cool100.mean())
    plt.show()

def test(dataset_path):
    tags = toDf(os.path.join(dataset_path, 'tags.csv'), dropCols=['timestamp', 'userId'])
    toy_story_tags = tags[tags['movieId'] == 1]
    print(toy_story_tags.head())

def main():
    dataset_path = sys.argv[1]
    test(dataset_path)
    # ratings = toDf(os.path.join(dataset_path, 'ratings.csv'), dropCols=['rating'])
    # min_watch_of_topN(dataset_path, ratings)
    # min_watched_of_topN(dataset_path, ratings)
    # group_by_hour(dataset_path, ratings)
    # top_100_rated_movies(dataset_path)

if __name__ == '__main__':
    main()


import time
import json
from pyfiglet import figlet_format
import pyautogui as pyg
import os
import sys
import subprocess

subprocess.call(["pip3", "install" ,"-r" ,"requirements.txt"])

# save urls.json do a variable
f = open("urls.json")
global urls
urls = json.load(f)
f.close()

# save data.json to a variable
f = open("data.json")
global data
data = json.load(f)
f.close()

# save all data

def save():
    f = open("data.json", 'w')
    json.dump(data, f)
    f.close()
    f = open("urls.json", 'w')
    json.dump(urls, f)
    f.close()
    print("Data saved")

# wait x seconds and restart

def wait_and_reset(amount=data["waittime"]):
    time.sleep(amount)
    clear()
    start()

def params(command, *args):
    print("Invalid params for:", command)
    for value in args:
        print("[{}]".format(value))
    wait_and_reset()

def clear():
    os.system("clear")

if data["clear_on_start"] == "True":
    clear()

def error():
    print("Someting went wrong")
    wait_and_reset()

# operation select function

def operation_select():
    operation = (input("Input operation (open or run)> ")).split(" ")
    # prime opening function
    if operation[0] == "open":
        run("open", "N/A", "N/A")
    # prime spam function
    elif operation[0] == "run":
        operation = "run"
    # extra commands
    # change loadtime
    elif operation[0] == "lt":
        try:
            data["loadtime"] = int(operation[1])
        except ValueError:
            print("Value must be an integer")
            wait_and_reset()
        except IndexError:
            params("lt(load time)", "length")
        save()
        wait_and_reset()
    # change wait time
    elif operation[0] == "wt":
        try:
            data["waittime"] = int(operation[1])
        except ValueError:
            print("Value must be an integer")
            wait_and_reset()
        except IndexError:
            params("wt(wait time)", "length")
        save()
        wait_and_reset()
    # change 'clear on start' status
    elif operation[0] == "cos":
        try:
            if operation[1] == "t":
                data["clear_on_start"] = "True"
            if operation[1] == "f":
                data["clear_on_start"] = "False"
            else:
                params("cos(clear on start)", "t(True) or f(False")
        except IndexError:
            params("cos(clear on start)", "t(True) or f(False")
        save()
        wait_and_reset()

    # add url fucntion
    elif operation[0] == "url":
        try:
            if operation[1] == "r":
                for key, values in urls.items():
                    print("{}: {}".format(key, values))
                try:
                    inp = int(input("Select url to remove> "))
                    del urls[str(inp)]
                    save()
                    wait_and_reset()
                except ValueError:
                    print("Value must be an integer")
                    wait_and_reset()

            elif operation[1] == "a":
                try:
                    i = len(urls.keys())
                    urls[str(i+1)] = operation[2]
                    print("Url added")
                    save()
                    wait_and_reset()
                except IndexError:
                    params("url", "r(remove)", "a(add) [url]")
            else:
                params("url", "r(remove)", "a(add) [url]")
        except IndexError:
            params("url", "r(remove)", "a(add) [url]")
    # save data
    elif operation[0] == "s":
        save()
        print("All data saved")
        wait_and_reset()
    # else
    else:
        print("Invalid operation")
        wait_and_reset()
    return operation

# runtime select function

def length_select():
    try:
        length = int(input("Input desired runtime (seconds)> "))
    except ValueError:
        print("Invalid length")
        time.sleep(data["waittime"])
        length_select()
    return length

# message select function

def message_select():
    message = input("Input message> ")
    return message

# countdown function

def countdown(amount):
    for i in range(0, amount):
        print(amount-i)
        time.sleep(1)

# main function (opens sites / or run spam)

def run(operation, message, runtime):
    # open urls
    if operation == 'open':
        if input("Enter 'r' when ready> ") == "r":
            print("Have browser open in:")
            countdown(data["loadtime"])
            for key, value in urls.items():
                pyg.keyDown("alt")
                pyg.press("d")
                pyg.keyUp("alt")
                pyg.typewrite(value)
                time.sleep(0.5)
                pyg.press("enter")
                time.sleep(1)
                pyg.keyDown("ctrl")
                pyg.press("t")
                pyg.keyUp("ctrl")
            pyg.keyDown("ctrl")
            pyg.press("w")
            pyg.keyUp("ctrl")
            inp = input("Would you like to run program> ")
            if inp == "yes":
                run("run", message_select(), length_select())
            elif inp == "no":
                print("Ok")
                save()
                exit()
            else:
                print("Invalid input")
                save()
                save()
                exit()
        else:
            print("Invalid input")
            time.sleep(data["waittime"])
            run(open, message, runtime)

    # start spam
    if operation == 'run':
        print("Open browser in:")
        countdown(data["loadtime"])
        t_end = time.time() + 60 * runtime
        while time.time() < t_end:
            time.sleep(1)
            pyg.typewrite(message)
            time.sleep(0.1)
            pyg.press("ENTER")
            pyg.keyDown("CTRL")
            pyg.press("TAB")
            pyg.keyUp("CTRL")
        print("Process finished")
        save()
        exit()

# define main loop

def start():
    print(figlet_format("DarkSGC", font="graffiti") + "Version 1.1\n")
    operation = operation_select()
    message = message_select()
    length = length_select()
    run(operation, message, length)

# start main loop
start()


import copy
import unittest

from diffusers import AutoencoderKL, Transformer2DModel
from mmengine.registry import MODELS
from mmengine.testing import RunnerTestCase
from mmengine.testing.runner_test_case import ToyModel
from torch import nn
from transformers import T5EncoderModel

from diffengine.engine.hooks import FastNormHook
from diffengine.models.editors import (
    SDDataPreprocessor,
    SDXLControlNetDataPreprocessor,
    SDXLDataPreprocessor,
    StableDiffusion,
    StableDiffusionXL,
    StableDiffusionXLControlNet,
)
from diffengine.models.losses import L2Loss
from diffengine.models.utils import TimeSteps, WhiteNoise

try:
    import apex
except ImportError:
    apex = None

class ToyModelPixArt(ToyModel):

    def __init__(self) -> None:
        super().__init__()
        height = 12
        width = 12

        model_kwargs = {
            "attention_bias": True,
            "cross_attention_dim": 32,
            "attention_head_dim": height * width,
            "num_attention_heads": 1,
            "num_vector_embeds": 12,
            "num_embeds_ada_norm": 12,
            "norm_num_groups": 32,
            "sample_size": width,
            "activation_fn": "geglu-approximate",
        }

        self.transformer = Transformer2DModel(**model_kwargs)
        self.text_encoder = T5EncoderModel.from_pretrained(
            "hf-internal-testing/tiny-random-t5")
        self.vae = AutoencoderKL(
            in_channels=4,
            out_channels=4,
            down_block_types=("DownEncoderBlock2D",),
            up_block_types=("UpDecoderBlock2D",),
            block_out_channels=(32,),
            layers_per_block=1,
            act_fn="silu",
            latent_channels=4,
            norm_num_groups=16,
            sample_size=16,
        )
        self.finetune_text_encoder = True

    @property
    def device(self):
        return next(self.parameters()).device

    def forward(self, *args, **kwargs):
        return super().forward(*args, **kwargs)

class TestFastNormHook(RunnerTestCase):

    def setUp(self) -> None:
        MODELS.register_module(name="StableDiffusion", module=StableDiffusion)
        MODELS.register_module(
            name="StableDiffusionXL", module=StableDiffusionXL)
        MODELS.register_module(
            name="SDDataPreprocessor", module=SDDataPreprocessor)
        MODELS.register_module(
            name="SDXLDataPreprocessor", module=SDXLDataPreprocessor)
        MODELS.register_module(
            name="StableDiffusionXLControlNet",
            module=StableDiffusionXLControlNet)
        MODELS.register_module(
            name="SDXLControlNetDataPreprocessor",
            module=SDXLControlNetDataPreprocessor)
        MODELS.register_module(name="L2Loss", module=L2Loss)
        MODELS.register_module(name="WhiteNoise", module=WhiteNoise)
        MODELS.register_module(name="TimeSteps", module=TimeSteps)
        MODELS.register_module(name="ToyModelPixArt", module=ToyModelPixArt)
        return super().setUp()

    def tearDown(self) -> None:
        MODELS.module_dict.pop("StableDiffusion")
        MODELS.module_dict.pop("StableDiffusionXL")
        MODELS.module_dict.pop("SDDataPreprocessor")
        MODELS.module_dict.pop("SDXLDataPreprocessor")
        MODELS.module_dict.pop("StableDiffusionXLControlNet")
        MODELS.module_dict.pop("SDXLControlNetDataPreprocessor")
        MODELS.module_dict.pop("L2Loss")
        MODELS.module_dict.pop("WhiteNoise")
        MODELS.module_dict.pop("TimeSteps")
        MODELS.module_dict.pop("ToyModelPixArt")
        return super().tearDown()

    @unittest.skipIf(apex is None, "apex is not installed")
    def test_init(self) -> None:
        FastNormHook()

    @unittest.skipIf(apex is None, "apex is not installed")
    def test_before_train(self) -> None:  # noqa
        from apex.contrib.group_norm import GroupNorm
        from apex.normalization import FusedLayerNorm

        cfg = copy.deepcopy(self.epoch_based_cfg)
        cfg.model.type = "StableDiffusion"
        cfg.model.model = "diffusers/tiny-stable-diffusion-torch"
        runner = self.build_runner(cfg)
        hook = FastNormHook(fuse_text_encoder_ln=True, fuse_gn=True)
        assert isinstance(
            runner.model.unet.down_blocks[
                1].attentions[0].transformer_blocks[0].norm1, nn.LayerNorm)
        assert isinstance(
            runner.model.unet.down_blocks[0].resnets[0].norm2, nn.GroupNorm)
        assert isinstance(
            runner.model.text_encoder.text_model.encoder.layers[
                0].layer_norm1, nn.LayerNorm)
        # replace norm
        hook.before_train(runner)
        assert isinstance(
            runner.model.unet.down_blocks[
                1].attentions[0].transformer_blocks[0].norm1, FusedLayerNorm)
        assert isinstance(
            runner.model.unet.down_blocks[0].resnets[0].norm2, GroupNorm)
        assert isinstance(
            runner.model.text_encoder.text_model.encoder.layers[
                0].layer_norm1, FusedLayerNorm)

        # Test StableDiffusionXL
        cfg = copy.deepcopy(self.epoch_based_cfg)
        cfg.model.type = "StableDiffusionXL"
        cfg.model.model = "hf-internal-testing/tiny-stable-diffusion-xl-pipe"
        runner = self.build_runner(cfg)
        hook = FastNormHook(fuse_text_encoder_ln=True, fuse_gn=True)
        assert isinstance(
            runner.model.unet.down_blocks[
                1].attentions[0].transformer_blocks[0].norm1, nn.LayerNorm)
        assert isinstance(
            runner.model.unet.down_blocks[0].resnets[0].norm2, nn.GroupNorm)
        assert isinstance(
            runner.model.text_encoder_one.text_model.encoder.layers[
                0].layer_norm1, nn.LayerNorm)
        assert isinstance(
            runner.model.text_encoder_two.text_model.encoder.layers[
                0].layer_norm1, nn.LayerNorm)
        # replace norm
        hook.before_train(runner)
        assert isinstance(
            runner.model.unet.down_blocks[
                1].attentions[0].transformer_blocks[0].norm1, FusedLayerNorm)
        assert isinstance(
            runner.model.unet.down_blocks[0].resnets[0].norm2, GroupNorm)
        assert isinstance(
            runner.model.text_encoder_one.text_model.encoder.layers[
                0].layer_norm1, FusedLayerNorm)
        assert isinstance(
            runner.model.text_encoder_two.text_model.encoder.layers[
                0].layer_norm1, FusedLayerNorm)

        # Test StableDiffusionXLControlNet
        cfg = copy.deepcopy(self.epoch_based_cfg)
        cfg.model.type = "StableDiffusionXLControlNet"
        cfg.model.model = "hf-internal-testing/tiny-stable-diffusion-xl-pipe"
        cfg.model.controlnet_model = "hf-internal-testing/tiny-controlnet-sdxl"
        runner = self.build_runner(cfg)
        hook = FastNormHook(fuse_text_encoder_ln=True, fuse_gn=True)
        assert isinstance(
            runner.model.unet.down_blocks[
                1].attentions[0].transformer_blocks[0].norm1, nn.LayerNorm)
        assert isinstance(
            runner.model.controlnet.down_blocks[
                1].attentions[0].transformer_blocks[0].norm1, nn.LayerNorm)
        assert isinstance(
            runner.model.unet.down_blocks[0].resnets[0].norm2, nn.GroupNorm)
        assert isinstance(
            runner.model.controlnet.down_blocks[0].resnets[0].norm2,
            nn.GroupNorm)
        assert isinstance(
            runner.model.text_encoder_one.text_model.encoder.layers[
                0].layer_norm1, nn.LayerNorm)
        assert isinstance(
            runner.model.text_encoder_two.text_model.encoder.layers[
                0].layer_norm1, nn.LayerNorm)
        # replace norm
        hook.before_train(runner)
        assert isinstance(
            runner.model.unet.down_blocks[
                1].attentions[0].transformer_blocks[0].norm1, FusedLayerNorm)
        assert isinstance(
            runner.model.controlnet.down_blocks[
                1].attentions[0].transformer_blocks[0].norm1, FusedLayerNorm)
        assert isinstance(
            runner.model.unet.down_blocks[0].resnets[0].norm2, GroupNorm)
        assert isinstance(
            runner.model.controlnet.down_blocks[0].resnets[0].norm2,
            GroupNorm)
        assert isinstance(
            runner.model.text_encoder_one.text_model.encoder.layers[
                0].layer_norm1, FusedLayerNorm)
        assert isinstance(
            runner.model.text_encoder_two.text_model.encoder.layers[
                0].layer_norm1, FusedLayerNorm)

        # test PixArt
        cfg = copy.deepcopy(self.epoch_based_cfg)
        cfg.model.type = "ToyModelPixArt"
        runner = self.build_runner(cfg)
        hook = FastNormHook(fuse_text_encoder_ln=True, fuse_gn=True)
        assert isinstance(
            runner.model.transformer.transformer_blocks[
                0].norm1.norm, nn.LayerNorm)
        # replace norm
        hook.before_train(runner)
        assert isinstance(
            runner.model.transformer.transformer_blocks[
                0].norm1.norm, FusedLayerNorm)


import redis

from flask import Flask, session, request, abort, jsonify

app = Flask(__name__)

app.config.from_object('config')

@app.route('/<key>')
def index(key):
    session.permament = True
    if 'value' not in request.args:
        value = app.config['CACHE'].get(key)
        if not value:
            abort(404)
        return jsonify({'value': value })

    app.config['CACHE'].set(key, request.args.get('value'))
    return jsonify({'success': True})


import cv2 # importing cv2 library
# import ImageFirebase
import time
import staticRec
import testguirandom

def Capture(name):

    cam = cv2.VideoCapture(0)

    ImageTaken = 0

    while True:
        if ImageTaken == 0 :

            count = 0

            ret, img = cam.read()

            cv2.imshow("Test", img)

            if not ret:
                break

            k = cv2.waitKey(1)

            if k % 256 == 27:
                #For Esc key
                print("Close")
                break

            elif k % 256 == 32:
                #For Space key

                file = '/Users/admin/VScode/Mini-Project-III-Python/Final-Code/ResFaceRecog/' + name + '.jpg'
                cv2.imwrite(file, img)

                print("Image " + str(count) + "saved")

                # time.sleep(5)

                # ImageFirebase.SendtoFirebase(name)

                count += 1
                ImageTaken += 1

        elif ImageTaken == 1:

            break

    cam.release
    cv2.destroyAllWindows

    NewtempText = staticRec.FaceRec(name + ".jpg")

    testguirandom.finalScreenDropOff(NewtempText)

# name = "Apurv Vidhate"
# Capture(name) # Only Uncomment this line when you are using the code independently


import random
import gym
import numpy as np
import cv2
from tensorflow.python.client import device_lib
from Agent import Agent
print(device_lib.list_local_devices())

def updateImage(state):
    state = cv2.cvtColor(cv2.resize(state,(84,110)),cv2.COLOR_BGR2GRAY)
    #remove the first 26 layers
    state = state[26:110,:]
    _,state = cv2.threshold(state, 1,255,cv2.THRESH_BINARY)
    #print(state.shape)
    state = np.reshape(state,(84,84,1))
    return state#.reshape(1,state.shape[0],state.shape[1],state.shape[2])

Episodes = 1000
Trials = 500
env = gym.make('SpaceInvaders-v0')

states = env.observation_space.shape

actions = env.action_space.n
#print(actions)
agent = Agent(states,actions)
done = False
batchSize = 32

for i in range (Episodes):
    state = env.reset()
    state = updateImage(state)
    #print ("State shape")
    #print (state.shape)

    while not done:
        #print (state.shape)
        env.render()
        action = agent.act(state)
        nextState,reward,done,inf   = env.step(action)
        nextState = updateImage(nextState)
        #print("next state")
        #print(len(state))
        #reward = reward if not done else 0.01
        #nextState = np.reshape(nextState,[1,states])
        agent.addToMemory(state,action,reward,nextState,done)
        state = nextState
        if done:
            print("episode {}/{}, score : {}, e: {:.2}".format(i,
                      Episodes,reward,agent.epsilon))
            break
        if len(agent.memory) > batchSize:
                agent.replay(batchSize)
    done = False