import os
from robustness import datasets
from robustness.tools.breeds_helpers import make_living17
from robustness.tools.breeds_helpers import setup_breeds

# returns both the source(pretrain) and target(finetuning)
def get_dataloaders(cfg, source=True):
    if not hasattr(cfg.datasets.living17, 'info_dir'):
        cfg.datasets.living17.info_dir = "./datasets/data/imagenet_class_hierarchy/modified"
    if not (os.path.exists(cfg.datasets.living17.info_dir) and len(os.listdir(cfg.info_dir))):
        print("Downloading class hierarchy information into `info_dir`")
        setup_breeds(cfg.datasets.living17.info_dir)

    ret = make_living17(cfg.datasets.living17.info_dir, split="rand")
    superclasses, subclass_split, label_map = ret
    train_subclasses, test_subclasses = subclass_split
    dataloaders = dict()

    if source == True:
        dataset_source = datasets.CustomImageNet(cfg.datasets.living17.data_dir, train_subclasses)
        loaders_source = dataset_source.make_loaders(cfg.num_workers, cfg.batch_size)
        train_loader_source, val_loader_source = loaders_source
        dataloaders = {'train': train_loader_source, 'eval':val_loader_source}
    else:
        dataset_target = datasets.CustomImageNet(cfg.datasets.living17.data_dir, test_subclasses)
        loaders_target = dataset_target.make_loaders(cfg.train.num_workers, cfg.train.batch_size)
        train_loader_target, val_loader_target = loaders_target
        dataloaders = {'train': train_loader_target, 'eval':val_loader_target}
    return dataloaders


"""move job table to postgres, change joblock relationship

Revision ID: 65b83c197420
Revises: 9478bbaf8010
Create Date: 2019-12-02 15:53:23.937129

"""
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision = "65b83c197420"
down_revision = "9478bbaf8010"
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "job",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column(
            "status",
            sa.Enum("UNKNOWN", "SCHEDULED", "RUNNING", "FINISHED", "EXCEPTION", name="jobstatus"),
            nullable=True,
        ),
        sa.Column("scheduled_time", sa.DateTime(), nullable=True),
        sa.Column("start_time", sa.DateTime(), nullable=True),
        sa.Column("finish_time", sa.DateTime(), nullable=True),
        sa.Column("function_name", sa.Unicode(length=255), nullable=True),
        sa.Column("scheduled_by", sa.Unicode(length=255), nullable=True),
        sa.Column("comment", sa.Unicode(length=255), nullable=True),
        sa.Column("ticket_ref", sa.Unicode(length=32), nullable=True),
        sa.Column("next_job_id", sa.Integer(), nullable=True),
        sa.Column("result", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("exception", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("finished_devices", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.ForeignKeyConstraint(
            ["next_job_id"],
            ["job.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.add_column("joblock", sa.Column("job_id", sa.Integer(), nullable=False))
    op.drop_constraint("joblock_jobid_key", "joblock", type_="unique")
    op.create_unique_constraint(None, "joblock", ["job_id"])
    op.create_foreign_key(None, "joblock", "job", ["job_id"], ["id"])
    op.drop_column("joblock", "jobid")
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("joblock", sa.Column("jobid", sa.VARCHAR(length=24), autoincrement=False, nullable=False))
    op.drop_constraint(None, "joblock", type_="foreignkey")
    op.drop_constraint(None, "joblock", type_="unique")
    op.create_unique_constraint("joblock_jobid_key", "joblock", ["jobid"])
    op.drop_column("joblock", "job_id")
    op.drop_table("job")
    # ### end Alembic commands ###


from django.shortcuts import render
from .models import Post
import plotly.graph_objs as go

def post_list(request):
    posts = Post.objects.all()
    return render(request, 'jsonplaceholder_app/post_list.html', {'posts': posts})

def post_chart(request):
    # Retrieve data from the database
    posts = Post.objects.all()

    # Extract user IDs and count the number of posts for each user
    user_ids = [post.userId for post in posts]
    post_counts = {user_id: user_ids.count(user_id) for user_id in user_ids}

    # Create a bar chart using Plotly
    fig = go.Figure()

    # Add a bar trace to the chart
    fig.add_trace(
        go.Bar(
            x=list(post_counts.keys()),  # User IDs
            y=list(post_counts.values()),  # Number of posts
            marker_color='rgb(26, 118, 255)',  # Bar color
            text=list(post_counts.values()),  # Display post count as text on bars
            textposition='auto',  # Position of the text on bars
            hoverinfo='x+y',  # Display both user ID and post count in hover info
        )
    )

    # Customize chart layout
    fig.update_layout(
        title='Number of Posts by User',
        xaxis_title='User ID',
        yaxis_title='Number of Posts',
        xaxis=dict(type='category'),  # Use a categorical x-axis
        bargap=0.1,  # Gap between bars
        showlegend=False,  # Hide legend
        paper_bgcolor='rgb(243, 243, 243)',  # Background color
        plot_bgcolor='rgb(243, 243, 243)',  # Plot area color
    )

    # Convert the chart to HTML
    chart_div = fig.to_html(full_html=False)

    return render(request, 'jsonplaceholder_app/post_chart.html', {'chart_div': chart_div})


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time    : 2020/5/29 16:30
@File    : utils.py
@author  : dfkai
@Software: PyCharm
"""
import logging
from hashlib import md5

import mistune
from pygments import highlight
from pygments.formatters import html
from pygments.lexers import get_lexer_by_name

logger = logging.getLogger(__name__)

def get_md5(str):
    m = md5(str.encode('utf-8'))
    return m.hexdigest()

def send_email(emailto, title, content):
    from blog_co.blog_signals import send_email_signal
    send_email_signal.send(
        send_email.__class__,
        emailto=emailto,
        title=title,
        content=content)

def block_code(text, lang, inlinestyles=False, linenos=False):
    '''
    markdown代码高亮
    :param text:
    :param lang:
    :param inlinestyles:
    :param linenos:
    :return:
    '''
    if not lang:
        text = text.strip()
        return u'
%s
\n' % mistune.escape(text)

    try:
        lexer = get_lexer_by_name(lang, stripall=True)
        formatter = html.HtmlFormatter(
            noclasses=inlinestyles, linenos=linenos
        )
        code = highlight(text, lexer, formatter)
        if linenos:
            return '
%s
\n' % code
        return code
    except BaseException:
        return '
%s
\n' % (
            lang, mistune.escape(text)
        )

class HighlightRenderer(mistune.HTMLRenderer):
    def block_code(self, code, lang=None):
        if lang:
            lexer = get_lexer_by_name(lang, stripall=True)
            formatter = html.HtmlFormatter()
            return highlight(code, lexer, formatter)
        return '
' + mistune.escape(code) + '
'

class CommonMarkdown():
    @staticmethod
    def get_markdown(value):
        markdown = mistune.create_markdown(renderer=HighlightRenderer())
        return markdown(value)


import os
import shutil
import numpy as np

def copy_model(out_dir, step):
    """
    Copy model file to outer dir
    """
    src_file1 = out_dir + '/checkpoints/model-' + str(step) + '.data-00000-of-00001'
    src_file2 = out_dir + '/checkpoints/model-' + str(step) + '.index'
    src_file3 = out_dir + '/checkpoints/model-' + str(step) + '.meta'

    dest_file1 = out_dir + '/model-best.data-00000-of-00001'
    dest_file2 = out_dir + '/model-best.index'
    dest_file3 = out_dir + '/model-best.meta'

    copy_file(src_file1, dest_file1)
    copy_file(src_file2, dest_file2)
    copy_file(src_file3, dest_file3)

def copy_file(src, dest):
    if not os.path.isfile(src):
        print('{} not exists!'.format(src))
    else:
        # Split path and filename
        fpath,fname=os.path.split(dest)
        if not os.path.exists(fpath):
            os.makedirs(fpath)
        # Copy file
        shutil.copyfile(src,dest)

def store_pr(directory, value):
    file = directory + '/pr.npy'
    if not os.path.isfile(file):
        pr_list = []
    else:
        pr_list = list(np.load(file))
    pr_list.append(value)
    np.save(file, np.array(pr_list))

def read_pr(directory):
    file = directory + '/pr.npy'
    if not os.path.isfile(file):
        return 0.0
    else:
        pr_list = np.load(file)
        return pr_list[-1]

def calculate_progress(epoch, pattern_num):
    total = 6 * 10
    current = ((pattern_num//5)-1)*10 + epoch + 1
    return '{:.2f}'.format(current/total)


import os

import pandas as pd

def path_check(path: str = '') -> bool:
    if os.path.exists:
        return True
    else:
        raise ValueError(f'path: "{path}" doesn''t exist.')

def parse_csv(
        parse_from_: str = '',
        kwargs_: dict[str, any] = {},
        columns_: tuple[any] = None,
        dt_column_: str = 'Date',
        datetime_format_: str = '%Y-%m-%d'
):
    """Парсит и предобрабатывает файл котировок (фьючерса, опциона и др.)."""
    path_check(parse_from_)
    df = pd.read_csv(parse_from_, **kwargs_)
    if columns_:
        df.columns = columns_
    df[dt_column_] = pd.to_datetime(df[dt_column_], format=datetime_format_)
    df = df.sort_values(by=dt_column_)
    df.set_index(dt_column_, inplace=True)
    return df


#importing libraries
import streamlit as st
import numpy as np
import pandas as pd
import os
from PIL import Image
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import spacy
from spacy.matcher import PhraseMatcher
import skillNer
import plotly
import plotly.graph_objs as go
from streamlit_option_menu import option_menu
import matplotlib.pyplot as plt
plt.style.use("seaborn-whitegrid")
import seaborn as sns
from collections import Counter
import warnings
warnings.filterwarnings("ignore")
from plotly import tools
import matplotlib.pyplot as plt

im = Image.open("Forward-MENA-logo.png")

st.set_page_config(
    page_title="ForasTech Skilling Tools",
    page_icon=im,
    layout="centered",
    initial_sidebar_state="expanded",
    menu_items={
        'Get Help': 'https://www.extremelycoolapp.com/help',
        'Report a bug': "https://www.extremelycoolapp.com/bug",
        'About': "# This is a header. This is an *extremely* cool app!"
    }
)

#Navigation Bar
Menu = option_menu(None, ["Home", "Scraping","Skills Extraction","Score Matching"],icons=['house','pen','search',"code"],menu_icon="cast", default_index=0, orientation="horizontal", styles={"container": {"padding": "0!important", "background-color": "#B0C4DE"},"icon": {"color": "black", "font-size": "25px"}, "nav-link": {"font-size": "15px", "text-align": "left", "margin":"0px", "--hover-color": "#eee"},"nav-link-selected": {"background-color": "#4F6272"},})

#Setting Conditions
video_file = open('FMQ_Digital-World_Music-only-2.mp4', 'rb')
video_bytes = video_file.read()
if Menu == "Home": st.video(video_bytes)
if Menu == "Home": st.title("FIND OUT ABOUT THE TOP SKILLS NEEDED IN THE MARKET TODAY!")
if Menu == "Home":st.write("77% believe there is a gap between the job requirementsand the university graduate qualifications. As the world moves at a rapid pace towards digitalization, the gap  is exponentially growing. Accordingly, we have taken on the role of filling this gap.")
if Menu == "Home":st.write("We will help you learn about the most in-demand careers, assess your skills, get certified and find the right job. To do so, please surf our offered tools to go through a career aspiration path.")

if Menu== "Scraping":
    import time
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from webdriver_manager.chrome import ChromeDriverManager

    job_descriptions, job_titles, Positions = [], [], []

    # function to run code based on input
    def scrapeWeb(query, website):
        driver = webdriver.Chrome(ChromeDriverManager().install())
        if dropdown_value == "Bayt.com":
            # code A logic
            base_url = "https://www.bayt.com/en/"
            # web page url
            driver.get(base_url)
            time.sleep(1)
            # input search query
            ## look for search bar location
            search_bar = driver.find_element(By.ID, "text_search")
            ## enter text
            search_bar.send_keys(query)
            time.sleep(1)
            ## look for search button loacation
            search_button = driver.find_element(By.ID, "search_icon_submit")
            ## click button
            search_button.click()
            time.sleep(1)
            # get job urls
            job_urls = driver.find_elements(By.XPATH, "//a[@data-js-aid='jobID']")
            job_urls = [elem.get_attribute("href") for elem in job_urls]

            # loop over urls

            for url in job_urls:
                # open url
                driver.get(url)

                try:
                    # get job description
                    job_description = driver.find_element(By.XPATH, "//div[@class='card-content is-spaced']")
                    job_description = job_description.text
                except:
                    break

                # get job title
                job_title = driver.find_element(By.ID, "job_title")
                job_title = job_title.text

                # append to list
                job_descriptions.append(job_description)

                # append to list
                job_titles.append(job_title)

                # append to list
                Positions.append(query)

                # sleep
                time.sleep(1)

            output_data = pd.DataFrame(data={"Positions":Positions, "job title":job_titles, "job description":job_descriptions})

            return output_data

        elif dropdown_value == "DaleelMadani.com":
                # code B logic
            base_url = "https://daleel-madani.org/jobs/"

            driver.get(base_url)
            time.sleep(1)

            # input search query
            ## look for search bar location
            search_bar = driver.find_element(By.ID, "edit-search-api-views-fulltext")
            ## enter text
            search_bar.send_keys(query)
            time.sleep(1)
            ## look for search button loacation
            search_button = driver.find_element(By.ID, "edit-submit-jobs-index-dm-jobs")
            ## click button
            search_button.click()
            time.sleep(1)

            # get job urls
            job_urls = driver.find_elements(By.XPATH, "//div[@class='field-item even']//h4//a")
            job_urls = [elem.get_attribute("href") for elem in job_urls]

            # loop over all jobs

            for url in job_urls:
                # open url
                driver.get(url)

                # get job description
                job_description = driver.find_element(By.XPATH, "//div[@class='field field-name-body field-type-text-with-summary field-label-above']")
                job_description = job_description.text

                # get job title
                job_title = driver.find_element(By.XPATH, "//div[@class='field-item even']//h1")
                job_title = job_title.text

                # append to list
                job_descriptions.append(job_description)

                # append to list
                job_titles.append(job_title)

                # append to list
                Positions.append(query)

                # sleep
                time.sleep(1)

            output_data = pd.DataFrame(data={"Positions":Positions, "job title":job_titles, "job description":job_descriptions})
            return output_data

        elif dropdown_value == "jobsforlebanon.com":
            base_url = "https://www.jobsforlebanon.com"
            driver.get(base_url)
            time.sleep(1)

            # input search query
            ## look for search bar location
            search_bar = driver.find_element(By.ID, "ui-search-terms")
            ## enter text
            search_bar.send_keys(query)
            time.sleep(1)
            ## look for search button loacation
            search_button = driver.find_element(By.XPATH, "//input[@type='submit']")
            ## click button
            search_button.click()
            time.sleep(1)

            # get job urls
            job_urls = driver.find_elements(By.XPATH, "//div[@class='catalogue-job']")
            job_urls = [elem.get_attribute("data-href") for elem in job_urls]

            for url in job_urls:
                # open url
                driver.get(url)

                # get job description
                job_description = driver.find_element(By.XPATH, "//div[@itemprop='description']")
                job_description = job_description.text

                # get job title
                job_title = driver.find_element(By.XPATH, "//main[@class='jobad-main job']//h1")
                job_title = job_title.text

                # append to list
                job_descriptions.append(job_description)

                # append to list
                job_titles.append(job_title)

                # append to list
                Positions.append(query)

                # sleep
                time.sleep(1)

            output_data = pd.DataFrame(data={"Positions":Positions, "job title":job_titles, "job description":job_descriptions})

            return output_data

        else:
            return None

    # set up streamlit app
    st.title('ForasTech Job Scraping Tool:')

    # dropdown for user to choose code
    dropdown_value = st.selectbox('Select a website to scrape:', ['Bayt.com', 'DaleelMadani.com', 'jobsforlebanon.com'])

    # input text box for user to enter input
    input_text = st.text_input('Enter Job Position:')

    # Create a button to download the output file
    from io import BytesIO
    def download_button(df):
        output = BytesIO()
        excel_writer = pd.ExcelWriter(output, engine='xlsxwriter')
        df.to_excel(excel_writer, sheet_name='Sheet1', index=False)
        excel_writer.save()
        processed_data = output.getvalue()
        return processed_data

    # search button to run code
    if st.button('Search'):
        # run code and display output
        output_data = scrapeWeb(input_text, dropdown_value)
        if output_data is not None:
            st.dataframe(output_data)
            processed_data = download_button(output_data)
            st.download_button(label='Download Results',
                        data=processed_data,
                        file_name='output.xlsx',
                        mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

if Menu == "Skills Extraction":

    # load default skills data base
    from skillNer.general_params import SKILL_DB
    # import skill extractor
    from skillNer.skill_extractor_class import SkillExtractor
    # init params of skill extractor
    nlp = spacy.load("en_core_web_lg")
    # init skill extractor
    skill_extractor = SkillExtractor(nlp, SKILL_DB, PhraseMatcher)

    st.title("ForasTech Skills Extraction Tool:")
    #enter the job description
    job_description = st.text_area('Enter the job description:')

    if st.button('Run'):
        annotations = skill_extractor.annotate(job_description)
        doc_node_values = [match['doc_node_value'] for match in annotations['results']['ngram_scored']]
        unique_skills = list(set(doc_node_values))
        st.write(f"Skills Extracted: {unique_skills}")

        # Create a WordCloud object
        wordcloud = WordCloud(width=800, height=800, background_color='white', colormap='inferno', max_words=50).generate_from_text(' '.join(unique_skills))

        # Plot the WordCloud image
        st.set_option('deprecation.showPyplotGlobalUse', False)
        plt.figure(figsize=(8,8))
        plt.imshow(wordcloud, interpolation='bilinear')
        plt.axis("off")
        st.pyplot()

if Menu == "Score Matching":
    # Load data from CSV file
    df = pd.read_excel('ExtractedSkills.xlsx')

    # Create list of unique job names
    job_names = df["Original JT"].unique().tolist()
    job_names.sort()

    # Sidebar widgets for selecting job name
    selected_job = st.sidebar.selectbox("Select a job", [job.title() for job in job_names])

    # Filter DataFrame based on selected job
    job_df = df[df["Original JT"] == selected_job.lower()]

    # Get the top 5 SS and top 5 HS repeated or counted skills in the DataFrame of the selected job
    ss_skills = job_df[job_df["type"] == "ss"]["OrgSkill"].value_counts().sort_values(ascending=False).index.tolist()
    hs_skills = job_df[job_df["type"] == "hs"]["OrgSkill"].value_counts().sort_values(ascending=False).index.tolist()

    # Dictionary to map proficiency level to multiplication factor
    proficiency_mapping = {"Beginner": 1/3, "Intermediate": 2/3, "Proficient": 1}

    # Sidebar widgets for selecting SS and HS skills with proficiency level
    select_ss_skills = st.sidebar.multiselect("Select the Soft Skills you acquire:", ss_skills, format_func=lambda x: x.title())
    ss_proficiency = {}
    for skill in select_ss_skills:
        proficiency = st.sidebar.radio(f"Select your proficiency level in  '{skill.title()}':", ["Beginner", "Intermediate", "Proficient"])
        ss_proficiency[skill] = proficiency

    select_hs_skills = st.sidebar.multiselect("Select the Hard Skills you acquire:", hs_skills, format_func=lambda x: x.title())
    hs_proficiency = {}
    for skill in select_hs_skills:
        proficiency = st.sidebar.radio(f"Select your proficiency level in  '{skill.title()}':", ["Beginner", "Intermediate", "Proficient"])
        hs_proficiency[skill] = proficiency

    # Calculate matching score
    total_ss_skills = len(ss_skills)
    total_hs_skills = len(hs_skills)
    total_skills = total_ss_skills + total_hs_skills
    matching_score = 0

    # Calculate score for selected SS skills
    for skill in select_ss_skills:
        proficiency = ss_proficiency[skill]
        if skill in ss_skills[:5]:
            matching_score += 0.08 * proficiency_mapping[proficiency]
        else:
            matching_score += (20 / (total_skills - 10) / 100) * proficiency_mapping[proficiency]

    # Calculate score for selected HS skills
    for skill in select_hs_skills:
        proficiency = hs_proficiency[skill]
        if skill in hs_skills[:5]:
            matching_score += 0.08 * proficiency_mapping[proficiency]
        else:
            matching_score += (20 / (total_skills - 10) / 100) * proficiency_mapping[proficiency]

    # Round matching score to 2 decimal places
    matching_score = round(matching_score * 100, 2)

    # Display job name as title and matching score in the center of the screen
    st.title("ForasTech Matching Score Calculation Tool:")
    st.header(f"Matching score for {selected_job} is: {matching_score}%")

    if matching_score <= 65:
        st.write("To enhance your skills in this field, you can start a learning path course or training on our website and get certified!")
    else:
        st.write("Continue learning more skills to gain competitive edge! The sky is the limit.")


import boto3
import os
from transform_conf import CONF
import json

ONE_JOB_LAMBDA = os.environ['ONE_JOB_LAMBDA']

def main(event, context):
    lambda_client = boto3.client('lambda')
    for c in CONF:
        lambda_client.invoke(FunctionName=ONE_JOB_LAMBDA,
                            InvocationType='Event',
                            Payload=json.dumps(c))

    response = {
        "statusCode": 200,
        "body": json.dumps('success')
    }

    return response


# https://scrapingclub.com

from bs4 import BeautifulSoup
import requests
urlbasic = url = 'https://scrapingclub.com/exercise/list_basic/'
url = 'https://scrapingclub.com/exercise/list_basic/?page=1'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
items = soup.find_all('div', class_='col-lg-4 col-md-6 mb-4')
for item in items:
    itemName = item.find('h4', class_='card-title').text.strip('\n')
    itemPrice = item.find('h5').text
    print('Name:%s Price: %s' % (itemName, itemPrice))

pages = soup.find('ul', class_='pagination')
urls = []
links = pages.find_all('a', class_='page-link')
for link in links:
    pageNum = int(link.text) if link.text.isdigit() else None
    if pageNum != None:
        x = link.get('href')
        urls.append(x)
print(urls)

for i in urls:
    newUrl = urlbasic + i
    items = soup.find_all('div', class_='col-lg-4 col-md-6 mb-4')
    print('Page No:%s' % i)
    for item in items:
        itemName = item.find('h4', class_='card-title').text.strip('\n')
        itemPrice = item.find('h5').text
        print('Name:%s Price: %s' % (itemName, itemPrice))


######################################################
#  Ben Palmer University of Birmingham 2020
#  Free to use
######################################################

import numpy

class es_calc:

  def run():

    print("Energies: Surface, Vacany etc")

    # Setup ES
    es.init()
    potential.es_add_potentials()
    es_calc.es_add()

    #es.energy()
    #for i in range(es.cc):
    #  print(es.config_energy[i,:])
    es.calculate_es()

  @staticmethod
  def es_add():

    print(g.es)
    for item in g.es:
      es_id = es.add_es_config(item['rcut'], item['alat'], item['label_id'], item['type'])

    """
    # Test values

    rcut = 6.5
    alat = 4.04
    label = 1
    type = 3

    # Add Config
    es_id = es.add_es_config(rcut, alat, label, type)
    #print(es_id)

    rcut = 6.5
    alat = 3.5
    label = 1
    type = 2
    es_id = es.add_es_config(rcut, alat, label, type)
    #print(es_id)
    """

  @staticmethod
  def load():
    if('es' not in g.inp.keys()):
      return None
    if('es_file' not in g.inp['es'].keys()):
      return None
    try:
      dir = g.inp['es']['dir'].strip()
      es_file = std.path(g.inp['es']['dir'], g.inp['es']['es_file'])
    except:
      dir = ""
      es_file = g.inp['es']['es_file']

    # Read BP data file
    es_inp = read_config.read_file(es_file)

    # Make list
    g.es = []

    # READ IN UNITS
    try:
      es_pressure = es_inp['units']['pressure']
    except:
      es_pressure = 'GPA'
    try:
      es_length = es_inp['units']['length']
    except:
      es_length = 'ang'
    try:
      es_energy = es_inp['units']['energy']
    except:
      es_energy = 'ev'

    for k in es_inp.keys():
      if('potlabel' in es_inp[k].keys() and 'alat' in es_inp[k].keys()):
        potlabel = es_inp[k]['potlabel']
        label_str, label_id = labels.add(potlabel)

        new_es = es_calc.make(label_id, label_str)

        # ALAT
        # There must be an alat value set
        new_es['alat'] = units.convert(es_length, 'ang', float(es_inp[k]['alat']))

        # UNIT VECTOR
        new_es['uv'][:,:] = 0.0
        new_es['uv'][0,0] = 1.0
        new_es['uv'][1,1] = 1.0
        new_es['uv'][2,2] = 1.0
        try:
          if('uv' in es_inp[k].keys()):
            # CUBIC
            if(len(es_inp[k]['uv']) == 1):
              new_es['uv'][0,0] = float(es_inp[k]['uv'][0])
              new_es['uv'][1,1] = float(es_inp[k]['uv'][0])
              new_es['uv'][2,2] = float(es_inp[k]['uv'][0])
            # CUBIC
            if(len(es_inp[k]['uv']) == 3):
              new_es['uv'][0,0] = float(es_inp[k]['uv'][0])
              new_es['uv'][1,1] = float(es_inp[k]['uv'][1])
              new_es['uv'][2,2] = float(es_inp[k]['uv'][2])
        except:
          pass

        # TYPE
        try:
          if(es_inp[k]['type'].lower() == 'sc'):
            new_es['type'] = 1
          elif(es_inp[k]['type'].lower() == 'bcc'):
            new_es['type'] = 2
          elif(es_inp[k]['type'].lower() == 'fcc'):
            new_es['type'] = 3
          elif(es_inp[k]['type'].lower() == 'zb'):
            new_es['type'] = 4
        except:
          pass

        # EXPANSION
        try:
          new_es['expansion'] = float(es_inp[k]['expansion'])
        except:
          pass

        # RCUT
        try:
          new_es['rcut'] = units.convert(es_length, 'ang', float(es_inp[k]['rcut']))
        except:
          pass

        # RCUT
        try:
          new_es['surface_energy'] = units.convert(es_length, 'ang', float(es_inp[k]['surface_energy']))
        except:
          pass

        # Add to list
        g.es.append(new_es)

    #

  def make(label_id, label_str):
    # b0 bulk modulus
    # e0 cohesive energy  (maybe change to ecoh)
    # ec elastic constants
    #

    es_d ={
           'label_id': label_id,
           'label_str': label_str,
           'alat': None,
           'uv': numpy.zeros((3,3,),),
           'type': 1,
           'expansion': 4,
           'rcut': 6.5,
           'surface_energy': None,
          }
    es_d['uv'][:,:] = 0.0
    es_d['uv'][0,0] = 1.0
    es_d['uv'][1,1] = 1.0
    es_d['uv'][2,2] = 1.0
    return es_d


import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler

# making a custom dataset class
class ImpuritiesDataset(Dataset):
    def __init__(self, target, conditions):
        self.target = target
        self.conditions = conditions
    def __len__(self):
        return len(self.conditions)
    def __getitem__(self, idx):
        condition = self.conditions[idx]
        target = self.target[idx]
        return target, condition

def split_target_condition(data, condition: str):
    '''split given dataframe into a target dataframe
        and condition dataframe
        input: all of the data and a single column that is the condition
        output: df of conditions data and df of target data'''

    conditions = data.loc[:,condition]
    targets = data.loc[:, ~data.columns.isin(['Class'])]

    print(f"shape of targets (all features without operating conditions): {targets.shape}")
    print(f"shape of conditions (operating conditions): {conditions.shape}")

    return targets, conditions

def minmax_scaler(target_data,condition_data, min: float, max: float):
    '''Scales data and transforms target and condition data to tensors
        input: target data, condition data, min for scaling, max for scaling
        output: a tensor of the target data (scaled) and tensor of conditions data (unscaled)'''
    scaler = MinMaxScaler(feature_range=(min,max))

    # fit & transform scaler data
    targets = scaler.fit_transform(target_data)

    column_names = list(target_data.columns.values)
    target_data_scaled = pd.DataFrame(targets, columns=column_names)

    target_tensor = torch.tensor(target_data_scaled.values, dtype=torch.float32)
    conditions_tensor = torch.tensor(condition_data, dtype=torch.float32).unsqueeze(dim=1)

    return conditions_tensor, target_tensor, scaler

def data_batch(target_tensor,conditions_tensor,n_datapoints: int, batch_size:int):
    '''batching data of conditions and target tensors
        put into dataloader'''
    n_samples = len(target_tensor)//n_datapoints

    target_tensor_list = []
    conditions_tensor_list = []
    invalid_batch_indexes = []

    #Iterating through all of the data and dividing it up
    for i in np.arange(0, n_samples, 1):

        #Taking a batch of the target data
        temp_target_batch = torch.split(target_tensor, n_datapoints, dim=0)[i]

        #Checking for any NaNs in that tensor
        # if its true, not saving it and the condition tensor to the list
        if torch.isnan(temp_target_batch).any() == True:
            invalid_batch_indexes.append(i)
            continue
        else:
            target_tensor_list.append(temp_target_batch)

        #Taking the corresponding batch of conditions
        temp_condition_batch = torch.split(conditions_tensor, n_datapoints, dim=0)[i]
        conditions_tensor_list.append(temp_condition_batch)

    print(f'length of data: {target_tensor.shape[0]}')
    print(f'We have {len(target_tensor_list)} samples with {n_datapoints} datapoints in each.')
    print(f'There are {len(invalid_batch_indexes)} invalid batches.\n')

    #Inputting data into ImpuritiesDataset class
    train_data = ImpuritiesDataset(target=target_tensor_list,
                               conditions=conditions_tensor_list)

    train_dataloader = DataLoader(
    dataset=train_data,
    batch_size=batch_size,
    shuffle=False,
    )

    print(f"dataloader: {train_dataloader}")
    print(f"length of train_dataloader: {len(train_dataloader)} batches of {batch_size} (batch_size) examples")
    print(f"number of total examples: {len(train_data)} \neach example containing... \nTARGET shape (number of timepoints): {train_data[0][0].shape} \nCONDITION shape (class 1 or 2): {train_data[0][1].shape}")

    return train_dataloader

def data_batch_clean(target_tensor,conditions_tensor,n_datapoints: int, batch_size:int):
    '''ASSUMES NO NAN VALUES. Batching data of conditions and target tensors
        put into dataloader'''
    n_samples = len(target_tensor)//n_datapoints

    target_tensor_list = []
    conditions_tensor_list = []

    #Iterating through all of the data and dividing it up
    for i in np.arange(0, n_samples, 1):

        #Taking a batch of the target data
        temp_target_batch = torch.split(target_tensor, n_datapoints, dim=0)[i]
        target_tensor_list.append(temp_target_batch)

        #Taking the corresponding batch of conditions
        temp_condition_batch = torch.split(conditions_tensor, n_datapoints, dim=0)[i]
        conditions_tensor_list.append(temp_condition_batch)

    print(f'length of data: {target_tensor.shape[0]}')
    print(f'We have {len(target_tensor_list)} samples with {n_datapoints} datapoints in each.')

    #Inputting data into ImpuritiesDataset class
    train_data = ImpuritiesDataset(target=target_tensor_list,
                               conditions=conditions_tensor_list)

    train_dataloader = DataLoader(
    dataset=train_data,
    batch_size=batch_size,
    shuffle=False,
    )

    print(f"dataloader: {train_dataloader}")
    print(f"length of train_dataloader: {len(train_dataloader)} batches of {batch_size} (batch_size) examples")
    print(f"number of total examples: {len(train_data)} \neach example containing... \nTARGET shape (number of timepoints): {train_data[0][0].shape} \nCONDITION shape (class 1 or 2): {train_data[0][1].shape}")

    return train_dataloader

def read_csv_drop(file_path: str,columns_to_drop: []):
    '''reads time-series data, drops the date column and any rows with NaNs.
        input: file path (str)'''
    df = pd.read_csv(file_path, sep=",")
    df = df.drop(columns=columns_to_drop)
    print(f'dataframe shape after dropping time column: {df.shape}')
    df = df.dropna()
    print(f'dataframe shape after dropping nan rows: {df.shape}')

    return df


import numpy as np

from abc import ABC, abstractmethod
from datetime import datetime
from dataclasses import dataclass
from numba import njit

from laika import AstroDog
from laika.gps_time import GPSTime
from navtools.conversions import ecef2lla
from navtools.constants import SPEED_OF_LIGHT

# Ionosphere
@dataclass
class IonosphereModelParameters:
    time: datetime
    rx_pos: np.array
    emitter_pos: np.array
    az: float
    el: float
    fcarrier: float
    alpha: np.array = np.array([2.6768e-08, 4.4914e-09, -3.2658e-07, -5.2153e-07])
    beta: np.array = np.array([1.3058e05, -1.1203e05, -7.0416e05, -6.4865e06])

class IonosphereModel(ABC):
    @abstractmethod
    def __init__(self) -> None:
        super().__init__()

    @abstractmethod
    def get_delay(self, params: IonosphereModelParameters):
        pass

class TotalElectronCountMapModel(IonosphereModel):
    def __init__(self) -> None:
        super().__init__()

    def get_delay(self, params: IonosphereModelParameters):
        gps_time = GPSTime.from_datetime(datetime=params.time)

        if not hasattr(self, "dog"):
            self.dog = AstroDog()
            self.ionex = self.dog.get_ionex(time=gps_time)

        delay = self.ionex.get_delay(
            time=gps_time,
            freq=params.fcarrier,
            rcv_pos=params.rx_pos,
            sat_pos=params.emitter_pos,
            az=params.az,
            el=params.el,
        )

        return delay

class KlobucharModel(IonosphereModel):
    def __init__(self) -> None:
        super().__init__()

    def get_delay(self, params: IonosphereModelParameters):
        gps_time = GPSTime.from_datetime(datetime=params.time)
        lla = ecef2lla(x=params.rx_pos[0], y=params.rx_pos[1], z=params.rx_pos[2])

        delay = compute_klobuchar_delay(
            lat=lla.lat,
            lon=lla.lon,
            az=params.az,
            el=params.el,
            tow=gps_time.tow,
            alpha=params.alpha,
            beta=params.beta,
        )

        return delay

@njit(cache=True)
def compute_klobuchar_delay(
    lat: float,
    lon: float,
    az: float,
    el: float,
    tow: float,
    alpha: np.array,
    beta: np.array,
):
    psi = 0.0137 / (el / np.pi + 0.11) - 0.022
    phi = lat / np.pi + psi * np.cos(az)

    if phi > 0.416:
        phi = 0.416
    elif phi < -0.416:
        phi = -0.416
    lam = lon / np.pi + psi * np.sin(az) / np.cos(phi * np.pi)

    local_tod = 43200.0 * lam + tow
    local_tod -= np.floor(local_tod / 86400.0) * 86400.0  # [s]

    slant_factor = 1.0 + 16.0 * np.power(0.53 - el / np.pi, 3.0)

    amplitude = alpha[0] + phi * (alpha[1] + phi * (alpha[2] + phi * alpha[3]))
    period = beta[0] + phi * (beta[1] + phi * (beta[2] + phi * beta[3]))
    if amplitude < 0.0:
        amplitude = 0.0
    if period < 72000.0:
        period = 72000.0

    phase = 2.0 * np.pi * (local_tod - 50400.0) / period

    multiple = 5e-9
    if np.abs(phase) < 1.57:
        multiple += amplitude * (1.0 + phase * phase * (-0.5 + phase * phase / 24.0))

    delay = SPEED_OF_LIGHT * slant_factor * multiple

    return delay

# Troposphere
@dataclass
class TroposphereModelParameters:
    rx_pos: np.array
    el: float

class TroposphereModel(ABC):
    @abstractmethod
    def __init__(self) -> None:
        super().__init__()

    @abstractmethod
    def get_delay(self, params: TroposphereModelParameters):
        pass

class SaastamoinenModel(TroposphereModel):
    def __init__(self) -> None:
        super().__init__()

    def get_delay(self, params: TroposphereModelParameters):
        delay = compute_saastamoinen_delay(rx_pos=params.rx_pos, el=params.el)

        return delay

@njit(cache=True)
def compute_saastamoinen_delay(
    rx_pos, el, humidity=0.75, temperature_at_sea_level=15.0
):
    """function from RTKlib: https://github.com/tomojitakasu/RTKLIB/blob/master/src/rtkcmn.c#L3362-3362
        with no changes by way of laika: https://github.com/commaai/laika

    Parameters
    ----------
    rx_pos : _type_
        receiver ECEF position
    el : _type_
        elevation to emitter [rad]
    humidity : float, optional
        relative humidity, by default 0.75
    temperature_at_sea_level : float, optional
        temperature at sea level [C], by default 15.0

    Returns
    -------
    _type_
        sum of wet and dry tropospheric delay [m]
    """
    # TODO: clean this up
    rx_pos_lla = ecef2lla(x=rx_pos[0], y=rx_pos[1], z=rx_pos[2])
    if rx_pos_lla[2] < -1e3 or 1e4 < rx_pos_lla[2] or el <= 0:
        return 0.0

    hgt = 0.0 if rx_pos_lla.alt < 0.0 else rx_pos_lla.alt  # standard atmosphere

    pres = 1013.25 * pow(1.0 - 2.2557e-5 * hgt, 5.2568)
    temp = temperature_at_sea_level - 6.5e-3 * hgt + 273.16
    e = 6.108 * humidity * np.exp((17.15 * temp - 4684.0) / (temp - 38.45))

    # /* saastamoninen model */
    z = np.pi / 2.0 - el
    trph = (
        0.0022768
        * pres
        / (1.0 - 0.00266 * np.cos(2.0 * rx_pos_lla.lat) - 0.00028 * hgt / 1e3)
        / np.cos(z)
    )
    trpw = 0.002277 * (1255.0 / temp + 0.05) * e / np.cos(z)
    return trph + trpw

# Factories
def get_ionosphere_model(model_name: str):
    """factory function that retrieves requested ionosphere model

    Parameters
    ----------
    model_name : str
        name of ionosphere model

    Returns
    -------
    IonosphereModel
        ionosphere model
    """
    IONOSPHERE_MODELS = {
        "tecmap": TotalElectronCountMapModel(),
        "klobuchar": KlobucharModel(),
    }

    model_name = "".join([i for i in model_name if i.isalnum()]).casefold()
    model = IONOSPHERE_MODELS.get(
        model_name.casefold(),
        TotalElectronCountMapModel(),  # defaults to tec map
    )

    return model

def get_troposphere_model(model_name: str):
    """factory function that retrieves requested troposphere model

    Parameters
    ----------
    model_name : str
        name of troposphere model

    Returns
    -------
    TroposphereModel
        troposphere model
    """
    TROPOSPHERE_MODELS = {"saastamoinen": SaastamoinenModel()}

    model_name = "".join([i for i in model_name if i.isalnum()]).casefold()
    model = TROPOSPHERE_MODELS.get(
        model_name.casefold(), SaastamoinenModel()
    )  # defaults to saastamoinen

    return model


import numpy as np

# 设置
cpus = 7  # 使用的CPU个数（等于提交任务的个数）
parameter_array_all = np.arange(0, 10, 0.1) # 需要计算的参数

# 通过.sh脚本文件修改的任务指标。job_index从0开始，最大值为cpus-1
job_index = -1

# 预处理
len_of_parameter_all = len(parameter_array_all)  # 需要计算参数的个数
if len_of_parameter_all%cpus == 0:
	len_parameter = int(len_of_parameter_all/cpus) # 一个CPU/任务需要计算参数的个数
	parameter_array = parameter_array_all[job_index*len_parameter:(job_index+1)*len_parameter]
else:
	len_parameter = int(len_of_parameter_all/(cpus-1)) # 一个CPU/任务需要计算参数的个数
	if job_index != cpus-1:
		parameter_array = parameter_array_all[job_index*len_parameter:(job_index+1)*len_parameter]
	else:
		parameter_array = parameter_array_all[job_index*len_parameter:len_of_parameter_all]

# 任务
with open('a'+str(job_index)+'.txt', 'w') as f:
	for parameter in parameter_array:
		result = parameter**2
		f.write(str(parameter)+'            '+str(result)+'\n')


import scipy
import numpy as np

from art import *

def create_design_matrix(y_eeg,n_predictors,n_samples_window,timelimits,sr):
    #building design matrix from scratch
    #n_predictors = 3
    #n_samples_window = 307
    #timelimits = [-.2,.4] #307  samples per predictor * 4
    #sr = 512

    expanded_params = (1+n_predictors)*n_samples_window
    signal_longitud_in_samples = len(y_eeg)

    X = np.zeros([signal_longitud_in_samples, expanded_params])

    x         = np.linspace(timelimits[0],timelimits[1],n_samples_window)

    zero_idx=closest_indices(x,0)
    evt_lat = df_conditions.iloc[:,-1].values

    for beta in range(n_predictors+1):

        j_idx = np.arange(beta*n_samples_window,beta*n_samples_window+n_samples_window)
        for j in j_idx:
            for i in range(len(evt_lat)):
                X[evt_lat[i]+j-beta*n_samples_window-zero_idx,j] = df_conditions.iloc[:,beta].values[i]
    return X

import mne
import pandas as pd
import numpy as np
import pathlib
import os
import logging
import datetime

class exp_info:
    """
    Class containing the experiment information.

    Attributes
    -------
    _path: str
        Path to the EEG data.
    subjects_ids: list
        List of subject's id.

    subjects_groups: list
        List of subject's group
    """

    def __init__(self):
    # Define set and fdt path and et data path
        self.eeg_path = paths().eeg_analysis_path()
        self.raw_path = paths().eeg_raw_path()
        self.results_path = paths().results_path()
        self.log_path = paths().log_path()

        # Select subject
        self.subjects_ids = ['S101','S102','S103','S104','S105','S106','S107','S108','S109','S110','S111',
                             'S112','S113','S114','S115','S116','S117','S118','S119']

        # Subjects interpolated channels
        self.subjects_int_channels = {'S101': [], 'S102': [], 'S102': [], 'S103': [], 'S104': [], 'S105': [],
                                      'S106': [], 'S107': [], 'S108': [], 'S109': [], 'S110': [], 'S111': [],
                                      'S112': [], 'S113': [], 'S114': [], 'S115': [], 'S116': [], 'S117': [],
                                      'S118': [], 'S119': []}

        # Subject rejected ICA components based on plochl criteria and visual inspection
        self.subjects_rej_ic = {'S101': [], 'S102': [], 'S102': [], 'S103': [], 'S104': [], 'S105': [],
                                'S106': [], 'S107': [], 'S108': [], 'S109': [], 'S110': [], 'S111': [],
                                'S112': [], 'S113': [], 'S114': [], 'S115': [], 'S116': [], 'S117': [],
                                'S118': [], 'S119': []}

        # Missing et data
        self.no_trig_subjects = []

        # Get et channels by name [Gaze x, Gaze y, Pupils]
        self.et_channel_names = ['R-GAZE-X', 'R-GAZE-Y', 'R-AREA']

        # Trigger channel name
        self.trig_ch = ''

        self.line_noise_freqs = (50)

        self.screen_size = (1920,1080)

    def initialize_logging(self):
    # Initialize logging configuration
        log_format = '%(asctime)s - %(levelname)s - %(message)s'
        log_level = logging.INFO
        log_filename = 'analysis_' + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + '.log'

        logger = logging.getLogger()
        logger.setLevel(log_level)
        # Create a new log file for each run
        handler = logging.FileHandler( os.path.join(self.log_path, log_filename), mode='w')
        handler.setFormatter(logging.Formatter(log_format))
        logger.addHandler(handler)
        #logging.basicConfig(filename=os.path.join(self.log_path, log_filename), level=log_level, format=log_format)
        logger.info("-----------------------------------------------------")
        logger.info("------------LOG INITIALIZATION-----------------------")
        logger.info("-----------------------------------------------------")

import os

class paths:
    """
    Paths to participants data.

    Attributes
    -------
    name: str
        Runing computers name result of os.popen('whoami').read().
    """

    def __init__(self):
        self.name = os.popen('whoami').read()

        if self.name == 'dac\n':
            if os.path.exists('/media/dac/SSD-curie'):
                self.main_path = '/media/dac/SSD-curie/Hybrid/'
            elif os.path.exists('/Volumes/DAC1T/'):
                self.main_path = '/Volumes/DAC1T/Hybrid/'
            else:
                print('HDD not connected\n')

        elif self.name == '':
            self.main_path = ''

    def eeg_analysis_path(self):
        """
        Paths to participants EEG data to analyze.

        Returns
        -------
        eeg_path: str
            Path in str format to the .set and .fdt EEG data.
        """

        eeg_path = self.main_path + 'Hybrid_preana_out/8.data_analysis/'

        return eeg_path

    def eeg_raw_path(self):
        """
        Paths to participants raw EEG data.

        Returns
        -------
        eeg_path: str
            Path in str format to the  EEG bdf data and the
            bh data tables.
        """

        raw_path = self.main_path + 'HybridSearch/'

        return raw_path

    def evts_path(self):
        """
        Paths to participants event eeglab struct data

        (MATLAB).

        Returns
        -------
        et_path: str
            Path in str format to the ET folder containing the Eye-Tracker data.
        """

        evts_path = self.main_path + 'Hybrid_preana_out/9.event_structs/'

        return evts_path

    def results_path(self):
        """
        Paths to the results folder.

        Returns
        -------
        results_path: str
            Path in str format to the folder to store the results.
        """

        results_path = self.main_path + 'Hybrid_ana_out/'

        # Create directory if it doesn't exist
        os.makedirs(results_path, exist_ok=True)

        return results_path

    def plots_path(self):
        """
        Paths to the plots folder.

        Returns
        -------
        plots_path: str
            Path in str format to the folder to store the results.
        """

        plots_path = self.main_path + 'Hybrid_ana_out/plots/'

        # Create directory if it doesn't exist
        os.makedirs(plots_path, exist_ok=True)

        return plots_path

    def item_pos_path(self):
        """
        Paths to the search items positions file.

        Returns
        -------
        item_pos_path: str
            Path in str format to the items positions file.
        """

        item_pos_path = self.main_path + 'HybridSearch/posiciones_estimulos_completa_2022.csv'

        return item_pos_path

    def experiment_path(self):
        """
        Paths to the Psychopy experiment directory.

        Returns
        -------
        exp_path: str
            Path in str format to the items positions file.
        """

        exp_path = self.main_path + 'HybridSearch/Psychopy experimento/'

        return exp_path

    def log_path(self):
        """
        Paths to the logs folder

        Returns
        -------
        log_path: str
            Path in str format to the folder to store the log files.
        """

        log_path = self.main_path + 'Hybrid_ana_out/logs/'

        # Create directory if it doesn't exist
        os.makedirs(log_path, exist_ok=True)

        return log_path

    def full_metadata_path(self):
        """
        Paths to the logs folder

        Returns
        -------
        log_path: str
            Path in str format to the folder to store the log files.
        """

        full_metadata_path = self.main_path + 'Hybrid_ana_out/metadata/'

        # Create directory if it doesn't exist
        os.makedirs(full_metadata_path, exist_ok=True)

        return full_metadata_path

class subject:
    """
    Class containing methods to get eeg,et and bh
    data for a subject.

    Attributes
    -------
    _path: str
        Path to the EEG data.
    subjects_ids: list
        List of subject's id.

    subjects_groups: list
        List of subject's group
    """
    def __init__(self,exp_info,subject_code=None):
        if subject_code == None:
            subject_id = exp_info.subjects_ids[0]
        elif type(subject_code) == int:
            subject_id = exp_info.subjects_ids[subject_code]
        elif type(subject_code) == str and (subject_code in exp_info.subjects_ids):
            subject_id = subject_code
        else:
            print('Subject not found.')

        self.subject_id   = subject_id
        logger = logging.getLogger()
        logger.info("Class containing subject %s information was loaded",self.subject_id)
        logger.info("-----------------------------------------------------")

    def load_bh_csv(self):
        bh_csv_path = paths().eeg_raw_path() + self.subject_id + '/'
        files = os.listdir(bh_csv_path)
        # Filter the list to include only CSV files
        csv_files = [f for f in files if f.endswith('.csv') and not f.startswith('.')]
        if len(csv_files) == 1:
            # Read the CSV file into a DataFrame
            tprint(f'''\nLoading behavioural data.....
            from   {csv_files}\n''',font="fancy67")
            csv_path = os.path.join(bh_csv_path, csv_files[0])
            df = pd.read_csv(csv_path)
            logger = logging.getLogger()
            logger.info("Behaviour csv from subject %s was loaded",self.subject_id)
            logger.info("-----------------------------------------------------")

            return df
        else:
            print('Error: There is not exactly one CSV file in the folder.\n')
            print(csv_files)

    def load_analysis_eeg(self):
        tprint(f'''\nLoading EEG data.....
        subject   {self.subject_id}\n''',font="fancy67")
        # get subject path
        set_path =  paths().eeg_analysis_path()
        set_file =  os.path.join(set_path,f'{self.subject_id}_analysis.set')

        # Load sesions
        try:
            raw     = mne.io.read_raw_eeglab( set_file, preload=True)
            return raw
        # Missing data
        except FileNotFoundError:
            print('No .ds files found in directory: {}'.format(set_path))

    def load_event_struct(self):
        tprint('''\nLoading events data.....
        from\n''',font="fancy67")        # get subject path
        evts_path =  paths().evts_path()
        evts_file =  os.path.join(evts_path,f'{self.subject_id}_events.csv')
        tprint(evts_file +'\n',font="fancy67")
        # Load sesions
        try:
            df = pd.read_csv(evts_file)
            logger = logging.getLogger()
            logger.info("Events csv from subject %s was loaded",self.subject_id)
            logger.info("-----------------------------------------------------")
            return df
        # Missing data
        except FileNotFoundError:
            print('No event files found in directory: {}'.format(evts_path))

    def get_et_data(self,raw,sample_limits,plot=0):
        '''
        sample_limits = [start_sample, stop_sample] list
        plot 1/0
        '''
        et_chans = setup.exp_info().et_channel_names
        et       = raw[et_chans,sample_limits[0]:sample_limits[1]]
        x = et[0].T[:,0]
        y = et[0].T[:,1]

        if plot:
            plt.plot(x,y)
            plt.show()
        return x , y

    def load_electrode_positions(self,raw):
        montage = mne.channels.make_standard_montage('biosemi128')
        raw.set_montage(montage, on_missing='ignore')
        return raw

    def load_metadata(self):
        tprint('''\nLoading events data.....
        from\n''',font="fancy67")        # get subject path
        sub_id = self.subject_id
        metadata_path =  paths().full_metadata_path()
        evts_file =  os.path.join(metadata_path,f'{sub_id}_full_metadata.csv')
        tprint(evts_file +'\n',font="fancy67")
        # Load sesions
        try:
            df = pd.read_csv(evts_file)
            logger = logging.getLogger()
            logger.info("Events csv from subject %s was loaded",self.subject_id)
            logger.info("-----------------------------------------------------")
            return df
        # Missing data
        except FileNotFoundError:
            print('No event files found in directory: {}'.format(evts_path))


"""
    Drug Sensitivity in Cancer database process
"""

input_path  = "senseDR.txt"
output_path = "senseDR.txt.xls"

import pandas as pd

df = pd.read_table(input_path)

target = df["targets"]

geneDict = {}
n = 0
for g in target:
    gene = g.split(",")
    for j in gene:
        n += 1
        geneDict[j.lstrip(" ")] = ""

for index,row in df.iterrows():

    for i in row[4].split(","):
        if i in geneDict:
            geneDict[i] += "%s : %s : %s ; " % (row[1], row[2], row[3])

with open(output_path, "w") as outf:

    outf.write("Gene Symbol\tDrug Name ; Synonyms ; Pathway Name\n")
    for key in geneDict:

        outl = key + "\t" + geneDict[key].strip(" ;") + "\n"
        outf.write(outl)

"""
    NovoDriver Genes process
"""

input_path  = "NovoDriverGene.txt"
output_path = "NovoDriverGene.xls"

import pandas as pd

df = pd.read_table(input_path)

geneList   = df["Gene Symbol"]
sourceList = df["Source"]

uniqueGene   = set(geneList)
uniqueSource = set(sourceList)

geneDict = {}
for i in uniqueGene:
    geneDict[i] = ""

outf = open(output_path, "w")

for index,row in df.iterrows():

    if "Gene Symbol" == row[0]:
        continue

    gene   = row[0]
    source = row[1]

    if gene in geneDict:
        geneDict[gene] += source + ","

outf.write("Gene Symbol\tSource\n")
for i in geneDict:

    outl = i + "\t" + geneDict[i].strip(",") + "\n"
    outf.write(outl)

outf.close()

"""
    CGC database process
"""

input_path  = "CGC.csv"
output_path = "CGC.xls"

with open(input_path, "r") as inf:

    outf = open(output_path, "w")

    title = "Gene\tTumor\n"
    outf.write(title)
    # print(title)

    next(inf)
    for l in inf:
        print(l)

        l_split = l.strip().split("\t")

        outl = l_split[0] + "\t"
        for i in l_split[1:]:
            outl += i + ","
        outl = outl.strip() + "\n"
        outf.write(outl)

    outf.close()


from django.shortcuts import get_object_or_404, render, redirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth.decorators import permission_required
from datetime import datetime, timedelta

from django.http import JsonResponse

from statistics.models.journal import Journal
# from statistics.helpers import date2req

from statistics.forms import RecordForm

def records(request, journal_id):
    journal = get_object_or_404(Journal, pk=journal_id)
    record_list = journal.record_set.order_by('-date').all()
    paginator = Paginator(record_list, 25)
    page = request.GET.get('page')
    try:
        records = paginator.page(page)
    except PageNotAnInteger:
        records = paginator.page(1)
    except EmptyPage:
        records = paginator.page(paginator.num_pages)
    return render(
        request,
        'statistics/records.html',
        {'records': records, 'journal': journal})

@permission_required('statistics.create_journal_record',
                     login_url='iplant_login')
def record_create(request, journal_id):
    journal = get_object_or_404(Journal, pk=journal_id)
    # Подготовить форму либо на основе данных POST, либо
    # если этот словарь пустой, инициализировать новую форму
    form = RecordForm(request.POST or None, extended_stat=journal.extended_stat)
    # Рендерим форму если словарь POST пустой (пришли по GET),
    # проверяем и записываем данные, если пришли по POST
    if request.POST and form.is_valid():
        journal.set_record_data(form.cleaned_data)
        # если нажали кнопку Применить и выйти, то переходим к журналу
        if request.POST['submit'] == 'af':
            return redirect('statistics:show', journal_id=journal_id)
        # если Применить и меняется день:
        else:
            days_cnt = int(request.POST['submit'])
            new_date = datetime.strptime(request.POST['date'], '%d.%m.%Y') + timedelta(days_cnt)
            rec = journal.rec_on_date(new_date)
            if rec:
                return redirect(
                    'statistics:record_edit',
                    journal_id=journal.id,
                    record_id=rec.id,
                )
            else:
                form = RecordForm(
                    None,
                    extended_stat=journal.extended_stat,
                    initial={'date': new_date.strftime('%d.%m.%Y')}
                )
                return render(
                    request,
                    'statistics/record_form.html',
                    {'form': form, 'journal': journal, 'record_id': None})

    return render(request,
                  'statistics/record_form.html',
                  {'form': form, 'journal': journal, 'record_id': None})

@permission_required('statistics.edit_journal_record',
                     login_url='iplant_login')
def record_update(request, journal_id, record_id):
    journal = get_object_or_404(Journal, pk=journal_id)
    form = RecordForm(request.POST or journal.get_record_data(record_id),
                      extended_stat=journal.extended_stat)
    if request.POST and form.is_valid():
        journal.set_record_data(form.cleaned_data, record_id)
        # если нажали кнопку Применить и выйти, то переходим к журналу
        if request.POST['submit'] == 'af':
            return redirect('statistics:show', journal_id=journal_id)
        # если Применить и меняется день:
        else:
            days_cnt = int(request.POST['submit'])
            new_date = datetime.strptime(
                request.POST['date'], '%d.%m.%Y') + timedelta(days_cnt)
            rec = journal.rec_on_date(new_date)
            if rec:
                return redirect(
                    'statistics:record_edit',
                    journal_id=journal.id,
                    record_id=rec.id,
                )
            else:
                form = RecordForm(
                    None,
                    extended_stat=journal.extended_stat,
                    initial={'date': new_date.strftime('%d.%m.%Y')}
                )
                return render(request,
                              'statistics/record_form.html',
                              {'form': form, 'journal': journal, 'record_id': None})
    return render(request,
                  'statistics/record_form.html',
                  {'form': form, 'journal': journal, 'record_id': record_id})

@permission_required('statistics.delete_journal_record',
                     login_url='iplant_login')
def record_delete(request, journal_id, record_id):
    template_name = 'statistics/confirm_record_delete.html'
    journal = get_object_or_404(Journal, pk=journal_id)
    if request.method == 'POST':
        journal.delete_record(record_id)
        return redirect('statistics:show', journal_id=journal_id)
    return render(request, template_name)

@permission_required('statistics.create_journal_record',
                     login_url='iplant_login')
def simple_record_create(request, journal_id):
    journal = get_object_or_404(Journal, pk=journal_id)
    if request.is_ajax():
        form = RecordForm(request.POST, extended_stat=journal.extended_stat)
        if form.is_valid():
            rec = journal.set_record_data(form.cleaned_data)
            response = {'journal_id': journal.id,
                        'work': rec.work,
                        'rec_id': rec.id,
                        }
        else:
            response = "bad Record data"
    else:
        response = "not AJAX!"
    return JsonResponse(response)


import pdb
from pyhocon import ConfigFactory
import os
"""
Loads configurations set in a .hocon file to a Python object
Refer to the hocon file for description of variables
"""

class BaseConfig:
    def __init__(self, conf_path):
        super().__init__()
        self.conf = ConfigFactory.parse_file(conf_path)
        # ------------- general options ----------------------------------------

        self.save_path = os.path.expanduser(self.conf['save_path'])
        self.gpu = self.conf['gpu']
        self.print_freq = self.conf['print_freq']
        self.num_workers = self.conf['num_workers']

        # ------------- experiment options -------------------------------------
        self.test_mode = self.conf['test_mode']
        self.mode = self.conf['mode']

        # ------------- data options -------------------------------------------
        self.split_dir = os.path.expanduser(self.conf['split_dir'])
        self.shuffle = self.conf['shuffle']
        self.pin_memory = self.conf['pin_memory']
        self.rand_seed = self.conf['rand_seed']
        # ------------- common optimization options ----------------------------
        self.batch_size = self.conf['batch_size']
        self.desired_bs = self.conf['desired_bs']
        self.momentum = self.conf['momentum']
        self.weight_decay = self.conf['weight_decay']
        self.lr = self.conf['lr']
        self.epochs = self.conf['epochs']
        self.scheduler = self.conf['scheduler']

        # ------------- logging options ----------------------------------------
        self.visdom = self.conf['visdom']

        # ---------- resume or retrain options ---------------------------------
        self.retrain = None if len(self.conf['retrain']) == 0 else os.path.expanduser(self.conf['retrain'])
        self.resume = None if len(self.conf['resume']) == 0 else os.path.expanduser(self.conf['resume'])
        self.notes = self.conf['notes']

        # load from remote configs if remote is True
        self.remote_conf = ConfigFactory.parse_file("configs/remote.hocon")
        if self.remote_conf["remote"]:
            self.remote = self.remote_conf["remote"]
            self.test_mode = self.remote_conf["test_mode"]
            self.num_workers = self.remote_conf["num_workers"]
            self.print_freq = self.remote_conf["print_freq"]
            self.batch_size = self.remote_conf["batch_size"]

class DatasetConfig(BaseConfig):
    def __init__(self, conf_path):
        super().__init__("configs/base.hocon")
        self.datasets = dict()

    def add_dataset(self, dataset_path, name):
        data_conf_obj = dict()
        data_conf = ConfigFactory.parse_file(dataset_path)
        data_conf_obj['data_path'] = os.path.expanduser(data_conf['data_path'])
        data_conf_obj['label1'] = os.path.expanduser(data_conf['label1'])
        data_conf_obj['label2'] = os.path.expanduser(data_conf['label2'])
        data_conf_obj['meta_path'] = os.path.expanduser(data_conf['meta_path'])
        self.datasets[name] = data_conf_obj

    def add_both_datasets(self):
        self.both_data = dict(
            adni = None,
            aud = None
        )

        def create_data_conf(conf_path):
            data_conf_obj = dict()
            data_conf = ConfigFactory.parse_file(conf_path)
            data_conf_obj["data_path"] = os.path.expanduser(data_conf['data_path'])
            data_conf_obj["split1_path"] = os.path.expanduser(data_conf['split1_path'])
            data_conf_obj["split2_path"] = os.path.expanduser(data_conf['split2_path'])
            data_conf_obj["label1"] = data_conf['label1']
            data_conf_obj["label2"] = data_conf['label2']
            data_conf_obj["meta_path"] = os.path.expanduser(data_conf['meta_path'])
            data_conf_obj["normalize_mean_std"] = data_conf['normalize_mean_std']
            return data_conf_obj
        adni_conf_obj = create_data_conf(adni_conf_path)
        aud_conf_obj = create_data_conf(aud_conf_path)
        self.both_data["adni"] = adni_conf_obj
        self.both_data["aud"] = aud_conf_obj

class ExperimentConfig(DatasetConfig):
    def __init__(self, conf_path):
        super().__init__(conf_path)
        self.exp_conf = ConfigFactory.parse_file(conf_path)
        self.eval = EvalConfig()
        self.dataset = self.exp_conf['dataset']

        # add dataset confs
        adni_conf_path = "configs/adni.hocon"
        aud_conf_path = "configs/aud.hocon"
        self.add_dataset(adni_conf_path, "adni")
        self.add_dataset(aud_conf_path, "aud")
        self.data_path = self.datasets[self.dataset]["data_path"]
        # add experiment confs
        self.len_z = self.exp_conf["len_z"]
        self.exp_id = self.exp_conf['exp_id']
        self.test_mode = self.exp_conf['test_mode']
        self.mode = self.exp_conf['mode']
        self.retrain = None if len(self.exp_conf['retrain']) == 0 else os.path.expanduser(self.exp_conf['retrain'])
        self.resume = None if len(self.exp_conf['resume']) == 0 else os.path.expanduser(self.exp_conf['resume'])
        self.notes = self.conf['notes']

class LoCAConfig(ExperimentConfig):
    def __init__(self, conf_path):
        super().__init__(conf_path)
        self.len_k = self.exp_conf["len_k"]

class BetaVAEConfig(ExperimentConfig):
    def __init__(self, conf_path):
        super().__init__(conf_path)
        self.beta = self.exp_conf["beta"]

class EvalConfig:
    def __init__(self, conf_path="configs/eval.hocon"):
        self.eval_conf = ConfigFactory.parse_file(conf_path)
        self.plot = self.eval_conf['plot']
        self.num_epochs = self.eval_conf['num_epochs']
        self.lr = self.eval_conf['lr']
        self.weight_decay = self.eval_conf['weight_decay']
        self.batch_size = self.eval_conf['batch_size']
        self.show_plots = self.eval_conf['show_plots']


from osrs.models.skills import Skills
from osrs.models.skills_summary import SkillsSummary
from osrs.models.minigames import Minigames
from osrs.models.bosses import Bosses
from osrs.enums import AccountType
from osrs.config import CONFIG, ENV

from sqlalchemy.ext.mutable import MutableDict
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import Column, DateTime, String, Integer, func
from sqlalchemy.dialects.sqlite import JSON

import sqlalchemy
import os

DB_URI = CONFIG[ENV].get("DB_URI")
URL = os.environ.get("DATABASE_URL", DB_URI)

engine = sqlalchemy.create_engine(URL, pool_pre_ping=True)
Base = declarative_base()
session_factory = sessionmaker(bind=engine)
session = scoped_session(session_factory)

class Highscores(Base):
    __tablename__ = "highscores"

    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(256), nullable=False)
    account_type = Column(String(256), nullable=False)
    skills_summary = Column(MutableDict.as_mutable(JSON), nullable=False)
    skills = Column(MutableDict.as_mutable(JSON), nullable=False)
    minigames = Column(MutableDict.as_mutable(JSON), nullable=False)
    bosses = Column(MutableDict.as_mutable(JSON), nullable=False)
    updated_date = Column(DateTime, server_default=func.now(), onupdate=func.now())
    created_date = Column(DateTime, server_default=func.now())

    def from_data(
        self,
        username: str,
        account_type: AccountType,
        current_time: int,
        skills_summary: SkillsSummary,
        skills: Skills,
        minigames: Minigames,
        bosses: Bosses,
    ):
        self.username = username
        self.account_type = account_type.value
        self.skills_summary = skills_summary.to_json()
        self.skills = skills.to_json()
        self.minigames = minigames.to_json()
        self.bosses = bosses.to_json()

def init_db():
    Base.metadata.create_all(engine)
    print("Database created")

def get_db_session():
    db = session
    try:
        yield db
    finally:
        db.close()


from ..utils import _file_handle, location_normalization
from ..datatypes.arraycgh import ArrayCGH

# filter valid return only valid probes
def ucsc_mapping(ucsc_file, filter_valid=False):
    """Reads an input file describing a chip and returns a dictionary mapping the probes included in the specified chip to their relative base pairs.

    The input file must contain rows organized in 5 tab-separated columns, representing:

    #. Indexing field to speed chromosome range queries.
    #. Reference sequence chromosome or scaffold
    #. Start position in chromosome
    #. End position in chromosome
    #. Name of the probe

    Example: http://hgdownload.cse.ucsc.edu/goldenpath/hg19/database/agilentCgh4x44k.txt.gz

    Parameters
    ----------

    ucsc_file : str
        The path to the file containing the chip specification.

    filter_valid : bool, optional (default: False)
        Whether to include or not lines with possibly missing values. If set to False (default), returns a list of 4-tuples (chromosome number, start base, end base, a flag indicating if the probe has been correctly read), if set to True in each tuple the last value is omitted.

    Returns
    -------

    ucsc_mapping : list
        The list of tuples representing the probes on the chip.

    """

    ucsc_mapping = dict()
    for line in _file_handle(ucsc_file):
        _, chr, sb, eb, id = line.split()
        value = location_normalization(chr, int(sb)+1, int(eb))
        if not filter_valid:
            ucsc_mapping[id] = value
        elif not value[3]: # Only If valid
            ucsc_mapping[id] = value[:3]

    return ucsc_mapping


import wx

from FilePropertiesPane import FilePropertiesPane

class ImagePropertiesPane(FilePropertiesPane):
	imageList = None
	imageListItems = {}
	imageListItemNames = {
		'root': 'page_white_picture',
		'imageSize': 'shape_handles',
		'width': 'tag_blue',
		'height': 'tag_blue',
		}

	def __init__(self, parent, document, *args, **kwargs):
		FilePropertiesPane.__init__(self, parent, document, rootLabel = "Image document", *args, **kwargs)

		image = wx.Image( document.GetFileName() )
		if image.IsOk():
			width_value = str( image.GetWidth() )
			height_value = str( image.GetHeight() )

			imageSize_item = self.tree.AppendItem(self.root, "Image size", image = self.TreeImages()['imageSize'])

			width_text = wx.TextCtrl(self.tree, value = width_value, size = (50, -1), style = wx.TE_READONLY)
			width_item = self.tree.AppendItem(imageSize_item, "Width", image = self.TreeImages()['width'], wnd = width_text)

			height_text = wx.TextCtrl(self.tree, value = height_value, size = (50, -1), style = wx.TE_READONLY)
			height_item = self.tree.AppendItem(imageSize_item, "Height", image = self.TreeImages()['height'], wnd = height_text)

			self.tree.Expand(imageSize_item)

	def SetImageList(self, imageList):
		ImagePropertiesPane.imageList = imageList

	def GetImageList(self):
		return ImagePropertiesPane.imageList

	def TreeImages(self):
		return ImagePropertiesPane.imageListItems

	def ImageListItemNames(self):
		return self.imageListItemNames


# -!- coding: utf-8 -!-
# 排布的模块
str1 = {
    "model1": [
        [
            39.500001,
            36.390000000000015
        ],
        [
            4,
            7.100000999999999
        ],
        [
            2,
            3.390000000000015
        ],
        [
            8,
            368.20503639000054
        ]
    ],
    "model2": [
        [
            7.100000999999999,
            33
        ],
        [
            1,
            1.5000009999999993
        ],
        [
            2,
            0
        ],
        [
            2,
            49.50003299999997
        ]
    ]
}
# 大矩形坐标
str2 = [
    [
        -274.920461,
        -208.824746
    ],
    [
        -274.920461,
        -172.434746
    ],
    [
        -235.42046,
        -172.434746
    ],
    [
        -235.42046,
        -208.824746
    ]
]

# 排布开始位置 左下:leftDown 左上:leftUp 右上:rightUp 右下:rightDown
site = 'leftUp'

# 模块参数
M = {
    "model1": [
        # 长
        16.5,
        # 宽
        8.1
    ],
    "model2": [
        16.5,
        5.6
    ]
}

def stall_car(stall, car):
    if car.__len__() == 0:
        return "排布模块不能为空"
    if stall.__len__() == 0:
        return "大矩形坐标不能为空"
    # print(list(car.keys()))
    # 总返回参数
    let = []
    # 上一个排布的模块的key
    last_key = ""
    # 沿x轴的起始坐标
    last_x = []
    # y轴的起始坐标
    last_y = []
    for key in list(car.keys()):
        if site == "leftUp":
            la = {"modelKey": key}
            cood = []
            if last_x.__len__() == 0 or last_y.__len__() == 0 or last_key == "":
                start_x_0 = stall[1][0]
                start_y_0 = stall[1][1]
            elif M[key][0] == M[last_key][0]:
                start_x_0 = last_x[0]
                start_y_0 = last_x[1]
            else:
                start_x_0 = last_y[0]
                start_y_0 = last_y[1]
            for model1_num_y in range(car[key][2][0]):
                start_x = start_x_0
                start_y = start_y_0 - model1_num_y*M[key][0]
                # x方向排布
                for model1_num_x in range(car[key][1][0]):
                    # 坐标数组
                    coord = []
                    # 左下角坐标
                    leftDown = [start_x, start_y - M[key][0]]
                    # 左上角坐标
                    leftUp = [start_x, start_y]
                    # 右上角
                    rightUp = [start_x + M[key][1], start_y]
                    # 右下角
                    rightDown = [start_x + M[key][1], start_y - M[key][0]]
                    coord.append(leftDown)
                    coord.append(leftUp)
                    coord.append(rightUp)
                    coord.append(rightDown)
                    cood.append(coord)
            la["cood"] = cood
            let.append(la)
            last_x = [cood[cood.__len__()-1][3][0], cood[0][1][1]]
            last_y = [cood[0][1][0], cood[cood.__len__()-1][3][1]]
            last_key = key
    return let

print(stall_car(str2, str1))


import numpy as np

class RoadNet:
    def __init__(self):
        pass

class Intersection:
    def __init__(self, instreets, outstreets):
        self.instreets = instreets
        self.traficlights = [False] * len(instreets)
        self.outstreets = outstreets

    def set_green(self, street_name):
        pass

    def set_schedule(self, schedules):
        self.green_index = 0
        instreet_names = [street.name for street in self.instreets]
        green_street_indexs = [instreet_names.index(
            name) for name, _ in schedules]
        self.schedules = list(
            zip(green_street_indexs, [s[1] for s in schedules]))
        self.green_instreet_index = schedules[0][0]
        self.time_until_light_change = schedules[0][1]
        self.schedule = schedules

    def step(self):
        # set green light
        if self.time_until_light_change > 0:
            self.time_until_light_change -= 1
        else:
            self.green_index += 1
            next_schedule = self.schedules[self.green_index % len(
                self.schedules)]
            self.green_instreet_index = next_schedule[0]
            self.time_until_light_change = next_schedule[1]

        for i, street in enumerate(self.instreets):
            street.step(i == self.green_instreet_index)

class Street:
    def __init__(self, start: int, end: int, name: str, length: int):
        self.start_ind = start
        self.end_ind = end
        self.name = name
        self.start = start
        self.end = end
        self.length = length
        self.cars = []

    def add_car(self):
        self.cars.append(car)
        # car.arrives_at = current_time + self.length
        car.time_until_traffic_light = self.length

    def step(self, green: bool):
        for car in self.cars:
            car.step(green)

class Car:
    def __init__(self, streets):
        self.streets = streets
        self.current_street = 0
        self.time_until_traffic_light = 0
        self.finished_at = None
        self.framme_snart = False

    def step(self, green: bool):
        if self.time_until_traffic_light > 0:
            self.time_until_traffic_light -= 1
        elif green:
            current_street = car.streets[car.current_street]
            self.current_street.cars.remove(self)
            self.current_street += 1
            next_street = car.streets[car.current_street]
            next_street.add_car(self)

    def get_score(self, D):
        if self.finished_at is None:
            return 0
        else:
            return self.value + (D - self.finished_at)

def calculate_score(text, D, cars, intersections):
    lines = text.split("\n")
    nbr_intersections_with_schedule = int(lines[0])
    line_id = 1
    for _ in range(nbr_intersections_with_schedule):
        intersection_id = int(lines[line_id])
        line_id += 1
        nbr_streets = int(lines[line_id])
        line_id += 1
        schedule = []
        for _ in range(nbr_streets):
            name, duration = lines[line_id].split(" ")
            schedule.append((name, int(duration)))
            line_id += 1

        intersections[intersection_id].set_schedule(schedule)

        # setup intersection schedules based on text
    for t in range(D):
        # update lights
        for intersection in intersections:
            intersection.step()

    score = 0
    for car in cars:
        score += car.get_score(D)

    return score

def read_file(path):
    with open(path) as text_file:
        D, I, S, V, F = list(map(int, text_file.readline().split(" ")))
        intersections = [([], []) for i in range(I)]
        streets = {}
        for i in range(S):
            B, E, name, L = text_file.readline().split(" ")
            street = Street(int(B), int(E), name.strip(), int(L))
            streets[street.name] = street
            intersections[int(B)][1].append(street)
            intersections[int(E)][0].append(street)

        intersections = [Intersection(instreets, outstreets)
                         for instreets, outstreets in intersections]

        cars = []
        for i in range(V):
            tmp = text_file.readline().split(" ")
            street_track = [streets[name.strip()] for name in tmp[1:]]
            cars.append(Car(street_track))

    return D, I, F, intersections, list(streets.values()), cars

def write_file(path, schedule):
    with open(path, 'w') as out_file:
        out_file.write("{}\n".format(len(schedule)))
        for intersection in schedule:
            intersection_id = intersection[0]
            traffic_lights_with_schedule = intersection[1]
            nbr_traffic_lights = len(traffic_lights_with_schedule)

            if nbr_traffic_lights > 0:
                out_file.write("{}\n{}\n".format(
                    intersection_id, nbr_traffic_lights))
                out_file.write("\n".join(["{} {}".format(road, time)
                                          for road, time in traffic_lights_with_schedule]))
                out_file.write("\n")

    with open(path) as submission:
        return submission.read()

def simple(intersections):
    schedule = []
    for i, intersection in enumerate(intersections):
        street_schedule = list(
            zip([street.name for street in intersection.instreets], [1] * len(intersection.instreets)))
        schedule.append((i, street_schedule))
    return schedule

def better(cars, streets, intersections):
    schedule = []
    streets_counts = [0] * len(streets)
    for car in cars:
        pass
    for i, intersection in enumerate(intersections):
        street_schedule = list(
            zip([street.name for street in intersection.instreets], [1] * len(intersection.instreets)))
        schedule.append((i, street_schedule))
    return schedule

def adrian_better(intersections, streets, cars, D):
    street_count = {street.name: 0 for street in streets}
    for car in cars:
        for street in car.streets:
            street_count[street.name] += 1

    scale = 5
    schedule = []
    for i, intersection in enumerate(intersections):
        instreet_names = [street.name for street in intersection.instreets]
        instreet_count = np.array([street_count[name]
                                   for name in instreet_names])
        the_sum = np.min(instreet_count)
        if the_sum > 0:
            normed_counts = instreet_count / the_sum
            normed_counts[normed_counts < 0.1] = np.inf
            min_reasonable_val = np.min(normed_counts)
            if min_reasonable_val > 0:
                scaled_counts = (
                    normed_counts / min_reasonable_val).astype(int)
                scaled_counts[scaled_counts >= D] = D
            else:
                scaled_counts = normed_counts.astype(int)
            new_sched = []
            for name, time in zip(instreet_names, scaled_counts):
                if time > 0:
                    new_sched.append([name, time])
            schedule.append([i, new_sched])
    return schedule

def fastest_cars(intersections, streets, cars, D):
    distances = np.zeros(len(cars))
    for i, car in enumerate(cars):
        distance = 0
        for street in car.streets:
            distance += street.length
        distances[i] = distance

    fastest = np.argsort(distances)[::-1]

    schedule = {idx: [] for idx, intersection in enumerate(intersections)}
    for car_idx in fastest:
        car = cars[car_idx]
        for street in car.streets:
            if len(schedule[street.end_ind]) > 0:
                pass
            else:
                schedule[street.end_ind].append([street.name, 1])
    return list(schedule.items())

def main():
    paths = [
        "a",
        "b",
        "c",
        "d",
        "e",
        "f",
    ]
    for path in paths:
        D, I, F, intersections, streets, cars = read_file(path + ".txt")
        # Create classes
        # result = adrian_better(intersections, streets, cars, D)
        result = fastest_cars(intersections, streets, cars, D)
        text_result = write_file(path + ".out", result)
        print(path)
        # score = calculate_score(text_result, D, cars, intersections)
        # print(score)

if __name__ == "__main__":
    main()


import random

# print multiple lines
print("Winning Rules of Rock,Paper and scissor: \n" + "Rock vs Paper => Paper Wins \n"
        + "Rock vs Scissor => Rock wings \n" + "Paper vs Scissor => Scissor Wings")

# We have to use here while loop beacause here is many type of conditon
while True:
    print("Enter your choice: \n" + "1. Rock \n" + "2. Paper \n" + "3. Scissor")
    # take the input using integer
    choice = int(input("User Turn: "))
    # if any one currect then it shows true vlue
    while choice > 3 or choice < 1:
        choice = int(input("Invallid Input"))

    # Initialize value of choice which is processing here
    if choice == 1:
        choice_name = 'Rock'
    elif choice == 2:
        choice_name = 'Paper'
    else:
        choice_name = 'Scissor'

    print("User Input: ", choice_name)
    print("Now It's Computer Turn.................")

    # Computer Turn ramdomly number
    comp_choice = random.randint(1,3)

    # Looping util comp_choice
    while comp_choice == choice:
        comp_choice = random.randint(1,3)

    # Initialize the value of comp_choice name
    if comp_choice == 1:
        comp_choice = 'Rock'
    elif comp_choice == 2:
        comp_choice = 'Paper'
    else:
        comp_choice = 'Scissor'

    print("Computer choice: ", comp_choice)
    print(choice_name + " vs " + comp_choice)

    # Main condition for winning
    if((choice == 1 and comp_choice == 3) or (choice == 3 and comp_choice == 1)):
        print("Rock Wins ==>", end = "")
        result = "Rock"
    elif((choice == 1 and comp_choice == 2) or (choice == 2 and comp_choice == 1)):
        print("Paper Wins ==>", end = "")
        result = "Paper"
    else:
        print("Scissor Wins ==>", end = "")
        result = "Scissor"

    # print aither User or computer wins than print
    if(result == choice_name):
        print("*******************USER WINS***************")
    else:
        print("******************COMPUTER WINS****************")

    print("Do you want to play again(y/N): ")
    ans = input()
    if (ans == "n" or ans == "N"):
        break

print("\n ...Thanks for playing...")


import logging
import logging.config
from pathlib import Path

logger = logging.getLogger(__name__)

def logging_test_child():
    """ Testing the logging functionality with child module, with different settings"""
    print("Testing the logging module")
    # logger.setLevel("WARNING")
    logger.info(f"This is name: {__name__}")
    logger.warning('This is a warning')
    logger.debug('This message should go to the log file')
    logger.info('So should this')
    logger.warning('And this, too')
    logger.error('And non-ASCII stuff, too, like Øresund and Malmö')

    print(f"Testing after log level is set to DEBUG")
    logger.setLevel("DEBUG")
    logger.warning('This is a warning')
    logger.debug('This message should go to the log file')
    logger.info('So should this')
    logger.warning('And this, too')
    logger.error('And non-ASCII stuff, too, like Øresund and Malmö')

if __name__ == "__main__":
    print("Logging")
    # read_json_config()


import os, sys
sys.path.append('/home/yang/open-convnet-black-box/VGGImagenet/')
sys.path.append('/home/yang/open-convnet-black-box/VGGImagenet/Tools/')
import numpy as np
import tensorflow as tf
from Prepare_Model import prepare_cnn3
from Prepare_Data import list_load
from Plot import simple_plot

images = [
    'Dog_1.JPEG',
    'Dog_2.JPEG',
    'Dog_3.JPEG',
    'Dog_4.JPEG',
    'Dog_5.JPEG',
    'tabby.png'
]

sal_type = [
    'PlainSaliency',
    'Deconv',
    'GuidedBackprop'
]

def super_saliency(tensor, inputs, num_to_viz):
    result = []
    shape = int(np.prod(tensor.get_shape()[1:]))
    tensor_flat = tf.reshape(tensor, [-1, shape])
    pick_indices = np.random.choice(shape, num_to_viz)
    for idx in pick_indices:
        result.append(tf.gradients(tensor_flat[:, idx], inputs)[0])
    return tf.stack(result)

def job(cnn3, sal_type, sess, if_pool, batch_img, fns):

    # first: pick one layer
    # second: pick num_to_viz neurons from this layer
    # third: calculate the saliency map w.r.t self.imgs for each picked neuron

    num_to_viz = 10
    layer_name = 'FC'

    # shape = (num_to_viz, num_input_images, 224, 224, 3)
    # TF Graph
    saliencies = super_saliency(cnn3.layers_dic[layer_name], cnn3.layers_dic['Input'], num_to_viz)

    # shape = (num_input_images, num_to_viz, 224, 224, 3)
    saliencies_val = sess.run(saliencies, feed_dict={cnn3.images: batch_img})
    saliencies_val_trans = np.transpose(saliencies_val, (1, 0, 2, 3, 4))

    for idx, name in enumerate(fns):
        save_dir = "01272018/{}/{}/Pooling_{}/".format(name, sal_type, if_pool)
        for index, image in enumerate(saliencies_val_trans[idx]):
            simple_plot(image, name + '_' + sal_type + '_' + str(if_pool) + '_' + str(index), save_dir)

def main():

    for sal in sal_type:

        for pool in [True, False]: # with and without pooling

            tf.reset_default_graph()
            sess = tf.Session()
            cnn3 = prepare_cnn3(sal, sess, pool=pool)

            batch_img, fns = list_load("./../data_imagenet", images)

            for idx, image in enumerate(batch_img):

                job(cnn3, sal, sess, pool, np.expand_dims(image, axis=0), [fns[idx]])

            sess.close()

if __name__ == '__main__':
    # setup the GPUs to use
    os.environ['CUDA_VISIBLE_DEVICES'] = '1'
    main()


# -*- coding: utf-8 -*-

"""byte - collection module."""

from __future__ import absolute_import, division, print_function

from byte.core.compat import PY26
from byte.core.plugin.manager import PluginManager
from byte.executors.core.base import Executor, FormatExecutor
from byte.model import Model
from byte.queries import DeleteQuery, InsertQuery, SelectQuery, UpdateQuery

from six import string_types
from six.moves.urllib.parse import ParseResult, parse_qsl, urlparse
import inspect
import logging

log = logging.getLogger(__name__)

class CollectionError(Exception):
    """Generic collection error."""

class CollectionLoadError(CollectionError):
    """Collection couldn't be loaded."""

class CollectionModelError(CollectionError):
    """Collection model violation."""

class CollectionParseError(CollectionError):
    """Collection parse error."""

class CollectionValidationError(CollectionError):
    """Collection item violation error."""

class Collection(object):
    """Collection for the storage of keyed items."""

    def __init__(self, model_or_uri=None, uri=None, model=None, executor=None, plugins=None, **kwargs):
        """
        Create keyed item collection.

        :param uri: Data Source URI
        :type uri: str

        :param model: Collection data model
        :type model: class

        :param executor: Collection executor
        :type executor: byte.executors.core.base.Executor or type

        :param plugins: List of plugins to enable for the collection (if :code:`None` is provided
                        all plugins will be enabled)
        :type plugins: list
        """
        self.model = None

        self.uri = None
        self.parameters = {}

        self._executor = None
        self._executor_cls = None

        self.plugins = None

        # Parse `model_or_uri` parameter
        model, uri = self._resolve_model_or_uri(model_or_uri, model, uri)

        # Parse keyword parameters
        if model:
            self.model = model

        # Construct plugin manager
        self.plugins = PluginManager(plugins)

        # Parse URI
        self._parse_uri(uri)

        # Retrieve executor
        if executor:
            self.executor = executor
        elif uri:
            self.executor = self.plugins.get_executor_by_scheme(self.uri.scheme)

        # Set plugin configuration
        self._configure_plugins(**kwargs)

    @property
    def executor(self):
        """Retrieve collection executor.

        :return: Executor
        :rtype: byte.executors.core.base.Executor
        """
        if not self._executor:
            # Ensure executor class is available
            if not self._executor_cls:
                raise CollectionLoadError('No executor available')

            # Construct executor instance
            self._executor = self._executor_cls(
                self,
                self.model
            )

        # Return current executor instance
        return self._executor

    @executor.setter
    def executor(self, value):
        """Set the current collection executor.

        :param value: Executor (class or instance)
        :type value: byte.executors.core.base.Executor or class
        """
        if not value:
            self._executor = None
            self._executor_cls = None
            return

        # Class
        if inspect.isclass(value) and issubclass(value, Executor):
            self._executor = None
            self._executor_cls = value
            return

        # Instance
        if isinstance(value, Executor):
            self._executor = value
            self._executor_cls = None

        # Unknown value
        raise ValueError('Unknown value provided (expected `Executor` class or instance)')

    @property
    def format(self):
        """Retrieve collection executor format."""
        if not isinstance(self.executor, FormatExecutor):
            raise ValueError('Executor doesn\'t support formats')

        return self.executor.format

    @property
    def internal(self):
        """Retrieve internal model metadata."""
        if not self.model:
            return None

        return self.model.Internal

    @property
    def properties(self):
        """Retrieve model properties."""
        if not self.model:
            return None

        return self.model.Properties

    def bind(self, model):
        """Bind collection to data model.

        :param model: Data model
        :type model: byte.model.Model
        """
        if not model or not issubclass(model, Model):
            raise CollectionModelError('Invalid value provided for the "model" parameter (expected Model subclass)')

        self.model = model

    def connect(self, prop, collection):
        """Connect collection to relation property.

        :param prop: Relation property
        :type prop: byte.property.RelationProperty

        :param collection: Collection
        :type collection: Collection
        """
        if not prop:
            raise ValueError('Invalid property')

        prop.connect(collection)

    def transaction(self):
        """Create transaction.

        :return: Transaction
        :rtype: byte.executors.core.models.database.transaction.DatabaseTransaction
        """
        if not self.executor:
            raise Exception('No executor available')

        transaction = self.executor.transaction()

        if transaction.operations > 0:
            raise Exception('Transaction is already active')

        return transaction

    def execute(self, query):
        """Execute query.

        :param query: Query
        :type query: byte.queries.Query
        """
        if not self.executor:
            raise Exception('No executor available')

        return self.executor.execute(query)

    def all(self):
        """Retrieve all items from collection."""
        return self.select()

    def delete(self):
        """Create delete query."""
        return DeleteQuery(self, self.model)

    def select(self, *properties):
        """Create select query."""
        return SelectQuery(
            self, self.model,
            properties=properties or None
        )

    def update(self, args, **kwargs):
        """Create update query."""
        data = kwargs

        for value in args:
            data.update(value)

        return UpdateQuery(
            self, self.model,
            data=data
        )

    def create(self, **kwargs):
        """Create item."""
        if not self.model:
            raise Exception('Collection has no model bound')

        return self.model.create(
            _collection=self,
            **kwargs
        )

    def create_or_get(self):
        """Create (or retrieve the existing) item."""
        raise NotImplementedError

    # TODO Better handling of primary key queries (string values are currently parsed as expressions)
    def get(self, *expressions, **kwargs):
        """Retrieve item."""
        query = self.select().limit(1)

        if expressions:
            query = query.where(*expressions)

        if kwargs:
            query = query.filter(**kwargs)

        return query.first()

    def get_or_create(self):
        """Retrieve existing (or create) item."""
        raise NotImplementedError

    def insert(self, *args):
        """Create insert query."""
        return InsertQuery(
            self, self.model,
            properties=args or None
        )

    def insert_from(self, query, properties):
        """Create insert from query."""
        return InsertQuery(
            self, self.model,
            query=query,
            properties=properties
        )

    def insert_many(self, items):
        """Create insert many query."""
        return InsertQuery(
            self, self.model,
            items=items
        )

    def _resolve_model_or_uri(self, model_or_uri, model, uri):
        if not model_or_uri:
            return model, uri

        if inspect.isclass(model_or_uri) and issubclass(model_or_uri, Model):
            model = model_or_uri
        elif isinstance(model_or_uri, string_types):
            uri = model_or_uri
        else:
            raise ValueError('Unknown initialization parameter value (expected subclass of Model, or string)')

        return model, uri

    def _parse_uri(self, uri):
        if not uri:
            return

        if PY26:
            # Retrieve scheme from `uri`
            scheme_end = uri.index('://')
            scheme = uri[0:scheme_end]

            # Replace scheme in `uri` with "http" (to avoid parsing bugs)
            uri = 'http' + uri[scheme_end:]

            # Parse URI
            parsed = urlparse(uri)

            # Build parse result with original scheme
            self.uri = ParseResult(scheme, *parsed[1:])
        else:
            self.uri = urlparse(uri)

        if self.uri.query:
            self.parameters = dict(parse_qsl(self.uri.query))

    def _configure_plugins(self, **kwargs):
        for key, value in kwargs.items():
            kind, key = tuple(key.split('_', 1))

            # Ensure plugin exists
            if not hasattr(self, kind):
                raise ValueError('Unknown plugin: %s' % (kind,))

            # Ensure attribute exists
            plugin = getattr(self, kind)

            if not hasattr(plugin, key):
                raise ValueError('Unknown plugin attribute: %s.%s' % (kind, key))

            # Set attribute value
            setattr(plugin, key, value)

# noinspection PyAbstractClass
class CollectionMixin(Collection):
    """Base class for collection mixins."""