from django.shortcuts import render

# Create your views here.
# -*- coding: utf-8 -*-
"""
Views of Product app
"""

from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
import json
from django.urls import reverse_lazy
from django.views.generic import FormView
from obp.api import API, APIError
from .forms import CreateProductForm
from django.views.decorators.csrf import csrf_exempt
from base.utils import exception_handle

class IndexProductView(LoginRequiredMixin, FormView):
    """Index view for Product"""
    template_name = "products/index.html"
    form_class = CreateProductForm
    success_url = reverse_lazy('products-create')

    def dispatch(self, request, *args, **kwargs):
        self.api = API(request.session.get('obp'))
        return super(IndexProductView, self).dispatch(request, *args, **kwargs)

    def get_form(self, *args, **kwargs):
        form = super(IndexProductView, self).get_form(*args, **kwargs)
        # Cannot add api in constructor: super complains about unknown kwarg
        form.api = self.api
        fields = form.fields
        print(fields, "These are fields")
        try:
            fields['bank_id'].choices = self.api.get_bank_id_choices()
        except APIError as err:
            messages.error(self.request, err)
        except Exception as err:
            messages.error(self.request, err)
        return form

    def form_valid(self, form):
        try:
           data = form.cleaned_data
           print(data, "This is a data")
           urlpath = '/banks/{}/products'.format(bank_id)
           payload={
                "parent_product_code": data["parent_product_code"],
                "name": data["name"],
                "more_info_url": data["more_info_url"],
                "terms_and_conditions_url": data["terms_and_conditions_url"],
                "description": data["description"],
                "meta": {
                        "license": {
                        "id": "ODbL-1.0",
                        "name": data["meta_license_name"] if data["meta_license_name"]!="" else "license name"
                        }
                    },
           }
           result = self.api.put(urlpath, payload=payload)
        except APIError as err:
            messages.error(self.request, err)
            return super(IndexProductView, self).form_invalid(form)
        except Exception as err:
            messages.error(self.request, "Unknown Error")
            return super(IndexProductView, self).form_invalid(form)
        if 'code' in result and result['code']>=400:
            messages.error(self.request, result['message'])
            return super(IndexProductView, self).form_valid(form)
        msg = 'Product {} for Bank {} has been created successfully!'.format(result['product_code'], result['bank_id'])
        messages.success(self.request, msg)
        return super(IndexProductView, self).form_valid(form)

class UpdateProductView(LoginRequiredMixin, FormView):
    template_name = "products/update.html"
    success_url = '/products/list'
    form_class = CreateProductForm

    def dispatch(self, request, *args, **kwargs):
        self.api = API(request.session.get('obp'))
        return super(UpdateProductView, self).dispatch(request, *args, **kwargs)

    def get_form(self, *args, **kwargs):
        form = super(UpdateProductView, self).get_form(*args, **kwargs)
        # Cannot add api in constructor: super complains about unknown kwarg
        form.api = self.api
        fields = form.fields
        urlpath = "/banks/{}/products/{}".format(self.kwargs['bank_id'], self.kwargs['product_code'])
        try:
            fields['bank_id'].choices = self.api.get_bank_id_choices()
        except APIError as err:
            messages.error(self.request, err)
        except Exception as err:
            messages.error(self.request, err)
        try:
            result = self.api.get(urlpath)
            fields['parent_product_code'].initial = self.kwargs['parent_product_code']
            fields['name'].initial = result['name']
            fields['more_info_url'].initial = result['more_info_url']
            fields['terms_and_conditions_url'].initial = result['terms_and_conditions_url']
            fields['description'].initial = result['description']
            fields['meta_license_id'].initial = result['meta']['license']['id']
            fields['meta_license_name'].initial = result['meta']['license']['name']
        except APIError as err:
            messages.error(self.request, err)
        except Exception as err:
            messages.error(self.request, "Unknown Error {}".format(err))
        return form
    def form_valid(self, form):
        data = form.cleaned_data
        urlpath = '/banks/{}/products/{}'.format(data["bank_id"], data["product_code"])
        payload = {
            #"product_code": data["product_code"],
            "parent_product_code": data["parent_product_code"],
            #"bank_id": data["bank_id"],
            "name": data["name"],
            "more_info_url": data["more_info_url"],
            "terms_and_conditions_url": data["terms_and_conditions_url"],
            "description": data["description"],
            "meta": {
                "license": {
                    "id": "PDDL",
                    "name": data["meta_license_name"] if data["meta_license_name"]!="" else "license name"
                }
            },
        }
        try:
            result = self.api.put(urlpath, payload=payload)
            print(result, "This is result")
            if 'code' in result and result['code']>=400:
                error_once_only(self.request, result['message'])
                return super(UpdateProductView, self).form_invalid(form)
        except APIError as err:
            messages.error(self.request, err)
            return super(UpdateProductView, self).form_invalid(form)
        except Exception as err:
            messages.error(self.request, err)
            return super(UpdateProductView, self).form_invalid(form)
        msg = 'Product {} for Bank {} has been Update successfully!'.format(  # noqa
            data["product_code"], data["bank_id"])
        messages.success(self.request, msg)
        return super(UpdateProductView, self).form_valid(form)

    def get_context_data(self, **kwargs):
        context = super(UpdateProductView, self).get_context_data(**kwargs)
        self.bank_id = self.kwargs['bank_id']
        self.branch_id = self.kwargs['product_code']
        context.update({
            'product_code': self.product_code,
            'bank_id': self.bank_id
        })
        return context

@exception_handle
@csrf_exempt
def create_list(request):
    print(request.POST, "createProductList listt")
    return HttpResponse("
View 1
")


import logging
from pathlib import Path

import pandas as pd

from foodscope.mappings import expand_greeks

_log = logging.getLogger(__name__)
_csv_dir = Path(Path(__file__).parent.parent, "v2020_04_07")

def lazy_property(fn):
    attr = f"_lazy_{fn.__name__}"

    @property
    def _lazy(self):
        if not hasattr(self, attr):
            setattr(self, attr, fn(self))
        return getattr(self, attr)

    return _lazy

class FooDb:
    def __init__(self) -> None:
        self.food = Food()
        self.compound = Compound()
        self.nutrient = Nutrient()
        self.health_effect = HealthEffect()
        self.compounds_health_effect = CompoundsHealthEffect()
        self._content = None

    @lazy_property
    def content(self):
        return Content()

    def foods_by_compound(self, compound: str):
        _log.warn("Use Food.by_compound instead")
        return self.food.by_compound(compound)

    def health_effects(self, food: str):
        df = filter_content(source_type="Compound")
        result = pd.merge(self.food.select(food), df, on="food_id")
        result = pd.merge(
            result, self.compound, left_on="source_id", right_on="compound_id"
        )
        result = pd.merge(result, self.compounds_health_effect, on="compound_id")
        return pd.merge(result, self.health_effect, on="health_effect_id")

    def composition(self, food: str, source=None):
        if source is None:
            source = ("Compound", "Nutrient")

        df = Content(cols=["source_id", "source_type", "food_id"])
        df = pd.merge(df, self.food.select(food), on="food_id")
        result = pd.DataFrame()
        if "Compound" in source:
            result = result.append(
                pd.merge(
                    filter_content(df, "Compound"),
                    self.compound,
                    left_on="source_id",
                    right_on="compound_id",
                )
            )
        if "Nutrient" in source:
            result = result.append(
                pd.merge(
                    filter_content(df, "Nutrient"),
                    self.nutrient,
                    left_on="source_id",
                    right_on="nutrient_id",
                )
            )
        return result

class Table(pd.DataFrame):
    _filename = None
    _cols = None
    _rename = None

    def __init__(self, cols=None):
        if cols is None:
            cols = self._cols
        elif cols == "all":
            cols = None
        df = load(self._filename, cols)
        if self._rename is not None:
            df.rename(columns=self._rename, inplace=True)
        super().__init__(df)

    def select(self, name):
        matches = expand_greeks(name)
        return self.equiv(matches)

    def equiv(self, group):
        if isinstance(group, str):
            _log.warn("Use .select() instead")
            group = [group]
        if not isinstance(group, (list, tuple, set)):
            raise TypeError("group should be iterable")
        return self.loc[self.name.str.contains("|".join(group), case=False)]

class EntityTable(Table):
    def by_id(self, row_id):
        return self.loc[self.get(self._id) == row_id, :]

class Nutrient(EntityTable):
    _filename = "Nutrient.csv"
    _cols = ["id", "name"]
    _rename = {"id": "nutrient_id"}
    _id = "nutrient_id"

class Compound(EntityTable):
    _filename = "Compound.csv"
    _cols = ["id", "name"]
    _rename = {"id": "compound_id"}
    _id = "compound_id"

    def health_effects(self, group):
        result = self.equiv(group)
        result = pd.merge(result, CompoundsHealthEffect(), on="compound_id")
        return pd.merge(result, HealthEffect(), on="health_effect_id")

class Food(EntityTable):
    _filename = "Food.csv"
    _cols = ["id", "name"]
    _rename = {"id": "food_id"}
    _id = "food_id"

    def by_compound(self, name):
        compounds = DB.compound.select(name)
        ids = compounds.compound_id
        df = filter_content(DB.content, source_type="Compound")
        mask = df["source_id"].isin(ids)
        df = df.loc[mask, :]
        df.rename(columns={"source_id": "compound_id"}, inplace=True)

        merged = pd.merge(df, self, on="food_id")
        return pd.merge(merged, compounds, on="compound_id")

    def subtract(self, name):
        compounds = DB.compound.select(name)
        ids = compounds.compound_id
        df = filter_content(DB.content, source_type="Compound")
        mask = df["source_id"].isin(ids)
        filtered_ids = df.loc[~mask, :].food_id
        return self.loc[self.food_id.isin(filtered_ids)]

class HealthEffect(EntityTable):
    _filename = "HealthEffect.csv"
    _cols = ["id", "name", "description"]
    _rename = {"id": "health_effect_id"}
    _id = "health_effect_id"

class CompoundsHealthEffect(Table):
    _filename = "CompoundsHealthEffect.csv"
    _cols = ["id", "compound_id", "health_effect_id"]

class Content(Table):
    _filename = "Content.csv"
    _cols = [
        "id",
        "food_id",
        "source_id",
        "source_type",
        "orig_content",
        "orig_min",
        "orig_max",
    ]

def load(filename, cols=None):
    return pd.read_csv(Path(_csv_dir, filename), usecols=cols)

def filter_content(df=None, source_type=None, cols=None):
    df = Content(cols) if df is None else df
    if source_type not in ("Nutrient", "Compound"):
        raise ValueError(f"Invalid source_type={source_type}")
    return df.loc[df.source_type == source_type, :]

def foods_by_compound(compound: str):
    return DB.foods_by_compound(compound)

def health_effects(food: str):
    return DB.health_effects(food)

# probably need outer join if source is both
def composition(food: str, source=None):
    return DB.composition(food, source)

DB = FooDb()


from flask import Flask, render_template, Response, make_response, send_file
import audio_recognizer
import json
import video_recognizer
import os
import numpy as np

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'

app = Flask(__name__, static_folder='static')

global audio_emotion
global multi_emotion

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/video_feed')
def video_feed():
    return Response(video_recognizer.gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/multimodal1')
def multimodal1():
    return render_template('question.html')

@app.route('/multimodal')
def multimodal():
    return render_template('multimodal.html')

@app.route('/question2html')
def question2html():
    return render_template('question2.html')

@app.route('/questio2')
def questio2():
    return render_template('multimodal2.html')

@app.route('/live-data_multi')
def live_data_multi():
    """ This function is called from graph_multi.js that updates the multimodal diagram.
    It fetches predictions, updates global variables and returns a multimodal prediction. """
    global multi_emotion
    f = open('video_prediction.json')
    video_data = json.load(f)
    audio_data = audio_recognizer.analyze_audio()
    newdict = [{"name": "Video",
                "data": [{"name": "Angry", "value": round(video_data[0] * 100, 2)},
                         {"name": "Fearful", "value": round(video_data[1] * 100, 2)},
                         {"name": "Happy", "value": round(video_data[2] * 100, 2)},
                         {"name": "Sad", "value": round(video_data[3] * 100, 2)}]},
               {"name": "Audio",
                "data": [{"name": "Angry", "value": round(audio_data[0] * 100, 2)},
                         {"name": "Fearful", "value": round(audio_data[1] * 100, 2)},
                         {"name": "Happy", "value": round(audio_data[2] * 100, 2)},
                         {"name": "Sad", "value": round(audio_data[3] * 100, 2)}]}]
    multimodal_data = []
    video_data = np.array(video_data)
    for i in range(len(audio_data)):
        multimodal_data.append(0.7 * video_data[i] + 0.3 * audio_data[i])
    multi_emotion = multimodal_data

    response = make_response(json.dumps(newdict))
    response.content_type = 'application/json'
    return response

@app.route('/multi_emotion')
def multi_emotion():
    try:
        data = multi_emotion
    except NameError:
        data = [0.0, 0.0, 0.0, 0.0]
    print('multi array: ', data)
    if np.argmax(data) == 0:
        return send_file('static\\images\\angry.jpg', mimetype='image/jpg')
    elif np.argmax(data) == 1:
        return send_file('static\\images\\fear.jpg', mimetype='image/jpg')
    elif np.argmax(data) == 2:
        return send_file('static\\images\\happy.jpg', mimetype='image/jpg')
    elif np.argmax(data) == 3:
        return send_file('static\\images\\sad.jpg', mimetype='image/jpg')
    else:
        return send_file('static\\images\\neutral.jpg', mimetype='image/jpg')

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


# main.py
#/usr/bin/python3

import sys
import argparse
import datetime
import jsonpickle
import os
#from pathlib import Path

class StatusReport():
    def __init__(self):
        self.monday_date : datetime
        self.messages = [ "", "", "", "", ""]
        self.next_week_goal = ""
        self.need_help = ""

    def overwrite(self, day) -> bool:
        n : str = ""
        if self.messages[day] != "":
            n = input("Already filled: " + self.messages[day] + "\nErase? (y/n)")
        else: return True

        n = n.lower().strip()
        if n == "y" or n == "yes":
            return True
        return False

    def setMondayDate(self, date) -> None:
        self.monday_date = date

    def recordMessage(self, day):
        assert(day < 8 and day > -1)

        if self.overwrite(day):
            self.messages[day] = input("enter reflection: ")
        else:
            return

    def recordMessage(self, day, message = "") -> None:
        assert(day < 8 and day > -1)
        print (message)
        if self.overwrite(day):
            if message != "":
                self.messages[day] = message
            else:
                self.messages[day] = input("enter reflection: ")
            print("message written" + message)
        else:
            print("message not written")
            return

    def isFinished(self) -> bool:
        return self.next_week_goal != "" and self.need_help != ""

    def setNextWeekGoal(self) -> None:
        n : str = ""
        if self.next_week_goal != "":
            n = input("Already filled! Erase? (y/n)")
        if n == "y" or n == "Y" or n == "":
            self.next_week_goal = input("enter next step: ")
        else:
            return

    def setNeedHelp(self) -> None:
        n : str = ""
        if self.need_help != "":
            n = input("Already filled! Erase need help? (y/n)")
        if n == "y" or n == "Y" or n == "":
            self.need_help = input("enter need help: ")
        else:
            return

    def generate(self) -> str:
            return """Launchie Weekly Report
Name: Eric Sims
Date of Monday: """ + self.monday_date.strftime("%d %B %Y") + """

What I've learned from specific activities and accomplishments throughout the week
Monday: """ + self.messages[0] + """
Tuesday: """ + self.messages[1] + """
Wednesday: """ + self.messages[2] + """
Thursday: """ + self.messages[3] + """
Friday: """ + self.messages[4] + """

Planned accomplishments for next week: """ + self.next_week_goal + """

Requested assistance: """ + self.need_help

# Command line arguments
arg_parser = argparse.ArgumentParser(description="Filling out the Weekly Status Report")
arg_parser.add_argument("-m", "--message", type=str, help="Reflect on the day")
arg_parser.add_argument("-e", "--email", action= 'store_true', help="Email to supervisor")
arg_parser.add_argument("-g", "--goal", help="Goal for next week")
arg_parser.add_argument("-n", "--need", help="Requesting help")
arg_parser.add_argument("-d", "--display", action= 'store_true', help="Display report")

group = arg_parser.add_mutually_exclusive_group()
group.add_argument("-v", "--verbose", action="store_true")
group.add_argument("-q", "--quiet", action="store_true")

args = arg_parser.parse_args()

def prompt():
    print( """************** Launchie Report Filler ****************
    w - Reflections for today's work
    p - Reflections for another weekday
    f - Finish the document
    e - Email report to supervisor
    h - help
    q - quit (auto saves)
******************************************************""")

verbose : bool = args.verbose
quiet : bool  = args.quiet
cmd : str = ""

currentDate = datetime.datetime.now()
weekday = currentDate.weekday()
mondayDate = datetime.datetime.now() - datetime.timedelta(days=currentDate.weekday())
filePathJSON = "data/report_" + mondayDate.strftime("%m_%d_%Y") + ".json"
filePathTXT = "reports/report_" + mondayDate.strftime("%m_%d_%Y") + ".txt"

print("filePath: " + filePathJSON) if verbose else None

# Check if report is already created
if (os.path.isfile(filePathJSON)):
    with open(filePathJSON, "r") as f:
        report = jsonpickle.decode(f.read())
else:
    # no previous report made, make a new one
    report = StatusReport()
    report.setMondayDate(mondayDate)

if args.message != None:
    report.recordMessage(weekday, str(args.message))
    print("Recorded message for " + currentDate.strftime("%A"))
elif args.display:
    print(report.generate())
elif args.goal != None:
    report.nextweek_goal = args.goal
    print("Set next week goal: " + report.next_week_goal) if verbose else None
elif args.need != None:
    report.need_help = args.need
    print("Set need help: " + report.need_help) if verbose else None
else:
    # INPUT EVENT LOOP
    prompt() if not quiet else None
    while (1):
        cmd = input(">>> ")
        if cmd == 'q': break

        elif cmd == 'w':
            print ("response for today") if verbose else None
            report.recordMessage(weekday)
            print(report.messages) if verbose else None

        elif cmd == 'p':
            print("response for previous days") if verbose else None
            custom_day = int(input("0 - Monday\t1 - Tuesday\t2 - Wednesday\t3 - Thursday\t4 - Friday\nday: "))
            report.recordMessage(custom_day)
            print(report.messages) if verbose else None

        elif cmd == 'e':
            print("emailing supervisor") if verbose else None
            txt = report.generate()
            with open(filePathTXT, "w") as f:
                f.write(txt)

        elif cmd == 'f':
            print("finishing documentation") if verbose else None
            report.setNextWeekGoal()
            report.setNeedHelp()

        elif cmd == 'h' or cmd == 'help':
            prompt()

        else:
            print("Invalid command")

# Save report
frozen = jsonpickle.encode(report)

with open(filePathJSON, "w") as f:
    f.write(frozen)


import os
import sys
import argparse
import glob
import pprint
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

import screed
import sourmash
from sourmash.sourmash_args import load_file_as_signatures
import sourmash.fig

def try_reading_csv(groups_file):
    # autodetect format
    if '.tsv' in groups_file or '.csv' in groups_file:
        separator = '\t'
        if '.csv' in groups_file:
            separator = ','
        try:
            samples = pd.read_csv(groups_file, dtype=str, sep=separator)
        except Exception as e:
            sys.stderr.write(f"\n\tError: {groups_file} file is not properly formatted. Please fix.\n\n")
            print(e)
    elif '.xls' in groups_file:
        try:
            samples = pd.read_excel(groups_file, dtype=str, sep=separator)
        except Exception as e:
            sys.stderr.write(f"\n\tError: {groups_file} file is not properly formatted. Please fix.\n\n")
            print(e)
    return samples

def get_anchor_containment(acclist, siglist):
    # return accession, anchor containment as list of tuples
    contain_tuples = []
    anchor_sig = siglist[0]
    for acc, sig in zip (acclist, siglist):
        containment = anchor_sig.contained_by(sig)
        contain_tuples.append((acc, containment))
    return contain_tuples

def get_anchor_jaccard(acclist, siglist):
    # return accession, anchor containment as list of tuples
    jaccard_tuples = []
    anchor_sig = siglist[0]
    for acc, sig in zip (acclist, siglist):
        jaccard_dist = anchor_sig.jaccard(sig)
        jaccard_tuples.append((acc, jaccard_dist))
    return jaccard_tuples

def assess_jaccard_and_containment(groupD, acc2sig, ksize, alphabet, sigdir="", abund=False): # provide options for choosing to use abund or not? (compute either one OR both, as is done now)
    anchorJaccard,anchorContainment = {},{}
    for group, accInfo in groupD.items():
        steps_to_common_ancestor = {"species": 0, "genus": 1, "family": 2, "order": 3, "class": 4, "phylum": 5, "superkingdom": 6}
        siglist = [""]*7
        acclist = [""]*7
        for rank, acc in accInfo.items():
            idx = steps_to_common_ancestor[rank]
            # load signatures
            sigfile = os.path.join(sigdir, f"{acc}.sig")
            sig =  list(load_file_as_signatures(sigfile, ksize=int(ksize), select_moltype=alphabet))
            siglist[idx] = sig[0]
            acclist[idx] = acc
        # assess jaccard and containment from anchor
        anchorContainment[group] = get_anchor_containment(acclist,siglist)
        anchorJaccard[group] = get_anchor_jaccard(acclist,siglist)
    return anchorContainment, anchorJaccard

def plot_all_distances(anchorDist, dist_csv, dist_long_csv, dist_plot=None, containment=False):
    # given all the distances generated from each path, boxplot the distances!
    # groupDF['steps_to_common_ancestor'] = groupDF["rank"].map(steps_to_common_ancestor)
    # for now, just turn into pandas DF and box plot
    # first, build distance dataframe as before (to keep plotting the same for now)
    distance_only = {}
    for key, val in anchorDist.items():
        #key = path number; val = list of tuples [(acc, anchordist), (acc,anchordist)]
        distance_only[key] = [v[1] for v in val]
    rank_order= ["anchor_species", "genus", "family", "order", "class", "phylum", "superkingdom"]
    # this imports with path as the rownames.
    distDF = pd.DataFrame.from_dict(distance_only, orient="index", columns= rank_order)

    distDF.to_csv(dist_csv, index_label="evolpath")

    if dist_plot:
        sns.set_style("white")
        plt.figure(figsize=(11,7))
        plt.xticks(fontsize=14)
        plt.yticks(fontsize=12)
        distDF.drop(columns=["anchor_species"], inplace=True)
        g=sns.boxenplot(data=distDF, palette="GnBu_d")
        g.set(yscale="log")
        #sns.stripplot(data=distDF,color='black',alpha=0.7) #, size=8)
        plt.xlabel("Common Ancestor", size=18, labelpad=20)
        if containment:
            plt.ylabel("Containment", size=20, labelpad=15)
        else:
            plt.ylabel("Jaccard", size=20, labelpad=15)
        plt.savefig(dist_plot)
        sys.stderr.write(f"wrote plot to {dist_plot}\n")

    ## now, build long form data
    distanceTest = pd.DataFrame.from_dict(anchorDist, orient="index", columns= rank_order)
    distanceTest = distanceTest.reset_index() # make index (path name) a column
    distanceTest = distanceTest.rename(columns={"index": "path"})
    distanceMelt = pd.melt(distanceTest, id_vars=["path"], value_vars=["anchor_species", "genus", "family", "order", "class", "phylum", "superkingdom"], var_name='rank')
    if containment:
        distanceMelt[['accession', 'containment']] = pd.DataFrame(distanceMelt['value'].tolist(), index=distanceMelt.index)
    else:
        distanceMelt[['accession', 'jaccard']] = pd.DataFrame(distanceMelt['value'].tolist(), index=distanceMelt.index)
    distanceMelt.drop("value", axis=1, inplace=True)
    distanceMelt['rank'] = pd.Categorical(distanceMelt['rank'], rank_order)
    distanceMelt.sort_values(by=["path","rank"], inplace=True)
    distanceMelt.to_csv(dist_long_csv, index=False)
    sys.stderr.write(f"wrote long distance csv to {dist_long_csv}\n")

def main(args):
    # from query csv, build dictionary of group:: filenames
    lineages = pd.read_csv(args.lineages_csv, dtype=str, sep=",", header=0)
    pathinfo = pd.read_csv(args.paths_csv, dtype=str, sep="\t", header=0)
    pathinfo = pathinfo.merge(lineages, on="accession")
    group2acc = (pathinfo.groupby('path').apply(lambda x: dict(zip(x['rank'],x["accession"]))).to_dict())

    #load signames from file at once
    #all_sigfiles = sourmash.sourmash_args.load_file_list_of_signatures(args.from_file)
    #acc2sig = {}
    #for fn in all_sigfiles:
    #    acc = os.path.basename(fn).rsplit(".sig")[0]
    #    sig = load_file_as_signatures(fn, ksize=args.ksize, select_moltype=args.alphabet)
    #    acc2sig[acc] = sig

    # assess similarity
    contain, jaccard = assess_jaccard_and_containment(group2acc, pathinfo, args.ksize, args.alphabet, sigdir=args.sigdir, abund=False)
    # build dataframe and plot
    plot_all_distances(jaccard, args.anchor_jaccard_csv, args.anchor_jaccard_long_csv, dist_plot=args.anchor_jaccard_plot)
    plot_all_distances(contain, args.anchor_containment_csv, args.anchor_containment_long_csv, dist_plot=args.anchor_containment_plot, containment=True)

def cmdline(sys_args):
    "Command line entry point w/argparse action."
    p = argparse.ArgumentParser()
    p.add_argument("paths_csv")
    p.add_argument("--from-file", required=True)
    p.add_argument("--sigdir", default="")
    p.add_argument("--alphabet", default="protein")
    p.add_argument("--ksize", default=11)
    p.add_argument("--lineages-csv", required=True)
    p.add_argument("--signature-name-column", default="filename", help="column with signature names in the sbt. By default, this should be the fasta file basename")
    p.add_argument("--anchor-jaccard-csv")
    p.add_argument("--anchor-jaccard-long-csv")
    p.add_argument("--anchor-containment-csv")
    p.add_argument("--anchor-containment-long-csv")
    p.add_argument("--anchor-jaccard-plot")
    p.add_argument("--anchor-containment-plot")
    args = p.parse_args()
    return main(args)

if __name__ == '__main__':
    returncode = cmdline(sys.argv[1:])
    sys.exit(returncode)


from time import sleep

import uiautomator2 as u2

d = u2.connect()
# d.app_start('com.ba
sess = d.session('com.taobao.idlefish')  # 开始网易云音乐
sess.close()  # 停止网易云音乐
sess.restart()  # 冷启动网易云音乐
# 启动应用程序（如果未运行），如果已运行(跳过启动)
sess = d.session('com.taobao.idlefish', attach=True)  # 则跳过启动
# 如果应用崩溃或退出
sess(text='Music').click()  # 引发SessionBrokenError
# 会话下的其他函数调用也会引发SessionBrokenError


import requests
import process_prov_map

import plotly.express as px
import pandas as pd
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
from datetime import datetime, date
from conf import app
from conf import server

# Section 4. dropdown provinsi
section4 = html.Div([
    html.Br(),
    html.H3('Informasi Per-Provinsi'),
    dcc.Dropdown(
        id='list-provinsi',
        options=process_prov_map.list_provinsi,
        placeholder='Masukkan provinsi',
        value='Jambi',
        multi=False
    ),

    html.Div(id='output')
])

@app.callback(
    Output('output', 'children'),
    Input('list-provinsi', 'value')
)
def update_info(value):

    # fungsi card
    def card(angka, status, penambahan):
        '''
        Untuk card status kasus aktif, sembuh, meninggal dan dirawat
        '''
        card_content = [
            dbc.CardBody(
                [
                    html.H2(angka, className="card-title"),
                    html.H5(status),
                    html.P(
                        penambahan,
                        className='card-text',
                    ),
                ]
            ),
        ]
        return card_content

    # fungsi bar chart
    def get_bar(dataframe, title):
        '''
        Generate barchart data covid indonesia
        '''
        chart = px.bar(
            dataframe,
            x='key',
            y='doc_count',
            title=title,
            labels={'doc_count': '(Persen (%))'},
            color='key',
            )
        chart.update_layout(
                xaxis=dict(
                    showgrid=False
                ),
                yaxis=dict(
                    showgrid=True,
                    gridcolor='lightgrey',
                ),
                plot_bgcolor='white',
                title_x=0.5,
                showlegend=False,

                hoverlabel= dict(
                    bgcolor='white'
                )
            )
        chart.update_yaxes(fixedrange=True)
        chart.update_xaxes(fixedrange=True, title_text='')
        return chart

    def get_pie(dataframe, title):
        '''
        Generate pie chart untuk kelompok usia dan jenis kelamin
        '''
        chart = px.pie(
            dataframe,
            title=title,
            values='doc_count',
            names='key',
            hover_name='key',
            labels={
                'key': 'Key',
                'doc_count': 'Persen'
            }
        )
        chart.update_layout(
            title_x=0.5,
            hoverlabel = dict(
                bgcolor='white'
            )
        )
        return chart

    def generate_chart(kondisi, mode, title):
        '''
        Fungsi untuk generate chart
        kondisi: nama kolom pada dataframe
        mode: bar/pie
        title: chart title
        '''
        jumlah_data = data['data']['kasus'][kondisi]['current_data']
        dataframe = pd.DataFrame(data['data']['kasus'][kondisi]['list_data'])

        if mode == 'bar':
            out = get_bar(
                dataframe,
                title='%s Provinsi %s (%s data)' % (title, provinsi.title(), jumlah_data),
            )
        elif mode == 'pie':
            out = get_pie(
                dataframe,
                title='%s Provinsi %s (%s data)' % (title, provinsi.title(), jumlah_data),
            )
        else:
            print('Periksa kembali parameter mode (pie/bar)')

        return out

    def get_chart(df, x, y, title, legend_title):
        chart = px.line(
            df,
            x=x,
            y=y,
            title=title,
            labels={
                'KASUS':'Kasus Baru',
                'tanggal':'Tanggal',
                'MENINGGAL': 'Meninggal',
                'SEMBUH': 'Sembuh'
                },
            )
        chart.update_yaxes(title_text='', fixedrange=True)
        chart.update_xaxes(title_text='')
        chart.update_layout(
            xaxis=dict(
                showgrid=True,
                gridcolor='lightgrey',
                rangeslider=dict(
                    visible=True
                ),
                type='date'
            ),
            yaxis=dict(
                showgrid=True,
                gridcolor='lightgrey'
            ),
            plot_bgcolor='white',
            title_x=0.5,
            height=600,
            legend_title_text=legend_title,
            hovermode='x unified'
        )

        return chart

    if value != None:
        prov = value.upper().replace(' ', '_')
        link = 'https://data.covid19.go.id/public/api/prov_detail_' + prov + '.json'
        a = requests.get(link)
        data = a.json()

        # ubah format tanggal
        tgl = datetime.strptime(data['last_date'], '%Y-%m-%d')
        tanggal_update = tgl.strftime('%d %B %Y')

        # membuat dataframe untuk plot
        df_perprov = pd.DataFrame(data['list_perkembangan'])

        # merubah timestamp ke datetime object
        df_perprov['tanggal'] = df_perprov['tanggal'].apply(lambda x: date.fromtimestamp(x/1000))

        # jumlah data
        akhir = df_perprov.shape[0]

        # Nama provinsi
        provinsi = data['provinsi']

        # total kasus
        total_kasus = df_perprov['AKUMULASI_KASUS'][akhir-1]
        total_sembuh = df_perprov['AKUMULASI_SEMBUH'][akhir-1]
        total_meninggal = df_perprov['AKUMULASI_MENINGGAL'][akhir-1]
        total_dirawat = df_perprov['AKUMULASI_DIRAWAT_OR_ISOLASI'][akhir-1]

        # statistik provinsi
        gabungan = get_chart(
            df_perprov,
            x='tanggal',
            y=['KASUS', 'MENINGGAL', 'SEMBUH'],
            title='Perkembangan Kasus Per-Hari (Grafik Gabungan) Provinsi %s' % (provinsi),
            legend_title=''
        )

        # Gejala positif covid
        gejala_positif = generate_chart(
            kondisi='gejala',
            mode='bar',
            title='Gejala Positif Covid-19'
        )

        # Kondisi penyerta positif covid-19
        kondisi_penyerta = generate_chart(
            kondisi='kondisi_penyerta',
            mode='bar',
            title='Kondisi Penyerta Positif Covid-19'
        )

        # Kelompok umur
        kelompok_umur = generate_chart(
            kondisi='kelompok_umur',
            mode='pie',
            title='Kelompok Umur Positif Covid-19'
        )

        # Kelompok jenis kelamin
        kelompok_jk = generate_chart(
            kondisi='jenis_kelamin',
            mode='pie',
            title='Jenis Kelamin Positif Covid-19'
        )

        # Variable yang akan di return
        out = html.Div([
            html.Br(),

            # Row nama provinsi
            dbc.Row([
                html.H2(provinsi),
            ], justify='center'),

            # Row statistik provinsi
            dbc.Row([
                dbc.Col([
                    dbc.Card(
                        card(
                            format(total_kasus, ','),
                            'TERKONFIRMASI',
                            '+%s Kasus' % (format(df_perprov['KASUS'][akhir-1], ','))
                        ), color='primary')
                ]),

                dbc.Col([
                    dbc.Card(
                        card(
                            format(total_dirawat, ','),
                            'KASUS AKTIF',
                            '+%s Kasus Aktif' % (format(df_perprov['DIRAWAT_OR_ISOLASI'][akhir-1], ','))
                        ), color='warning')
                ]),

                dbc.Col([
                    dbc.Card(
                        card(
                            format(total_sembuh, ','),
                            'SEMBUH',
                            '+%s Kasus Sembuh' % (format(df_perprov['SEMBUH'][akhir-1], ','))
                        ), color='success')
                ]),

                dbc.Col([
                    dbc.Card(
                        card(
                            format(total_meninggal, ','),
                            'MENINGGAL',
                            '+%s Kasus Meninggal' % (format(df_perprov['MENINGGAL'][akhir-1], ','))
                        ), color='danger')
                ]),

            ], style={'color': 'white'}),

            # Row chart provinsi
            dbc.Row([
                dbc.Col([
                    dcc.Graph(figure=gabungan, config={'displayModeBar': False})
                ])
            ]),

            # Row statistik gejala
            dbc.Row([
                dbc.Col([
                    dcc.Graph(
                        figure=gejala_positif,
                        config={'displayModeBar': False})
                ]),

                dbc.Col([
                    dcc.Graph(
                        figure=kondisi_penyerta,
                        config={'displayModeBar': False})
                ]),

            ]),

            # Row kelompok usia dan jenis kelamin
            dbc.Row([
                dbc.Col([
                    dcc.Graph(
                        figure=kelompok_umur,
                        config={'displayModeBar': False}
                    )
                ]),

                dbc.Col([
                    dcc.Graph(
                        figure=kelompok_jk,
                        config={'displayModeBar': False}
                    )
                ])

            ])

        ])

        return out


import utils
from utils import *
from utils import _compressed
from extractor import *

def main():
    has_wordlist()
    try:
        position_first_flag(compressed_file())
    except IndexError:
        print("usage: python decompress.py [file] [-w wordlist]")
        exit()

    _content = shell_cmd_output_lines("ls")
    content = _content

    while True:
        if content != _content:
            position_new_flag(content, _content)

        compressed_type = type(_compressed)

        redefine_type_and_extract(compressed_type)
        content = shell_cmd_output_lines("ls")
        utils._it += 1

if __name__ == '__main__':
    main()


import pickle
import pickletools
import base64
import os
from pdb import set_trace
from pickle import PROTO, GLOBAL, NONE, MARK, UNICODE, NEWTRUE, DICT, TUPLE2, BUILD, EMPTY_TUPLE, REDUCE, NEWFALSE, POP, STOP

class User:
  def __init__(self):
    pass

code = PROTO + b'\x04' + \
  GLOBAL + b'__main__\nUser\n' + \
  NONE + \
  MARK + \
    UNICODE + b'admin\n' + \
    NEWTRUE + \
    DICT + \
  TUPLE2 + BUILD + \
  EMPTY_TUPLE + REDUCE + \
  MARK + \
    UNICODE + b'admin\n' + \
    NEWFALSE + \
    DICT + \
  BUILD + \
  GLOBAL + b'__main__\nUser\n' + \
  NONE + \
  MARK + \
    UNICODE + b'__setstate__\n' + \
    GLOBAL + b'__main__\nUser\n' + \
    DICT + \
  TUPLE2 + BUILD + \
  POP + \
  STOP

def add(a, b):
    return a + b

class RCE:
  def __reduce__(self):
    #return os.system, ('ls -la',) # or whatever command here
    return add, (1, 2,)

class Foo:
  def __init__(self):
    self.x = 1

  def __setstate__(self):
    self.x = 2

if __name__ == '__main__':
    set_trace()

    aa = pickle._loads(code)

    set_trace()
    pickled = pickle.dumps(RCE())
    print(base64.urlsafe_b64encode(pickled))


from pygame import *
from Niveau import *
from Constantes import *
import pickle

class Session ():
    def __init__(self,niveau,difficulte,mode_affichage,mode_minimap,nb_niv_max):
        try:
            fichier = open(sauvegarde,'rb')
            f = pickle.Unpickler(fichier)
            parametres = f.load()
            self.niv_courant = Niveau(parametres[0],parametres[1],parametres[2],parametres[3],None,parametres[4],parametres[5],parametres[6],parametres[7],parametres[8],parametres[9])
            #paramètres pour la réinitialisation
            self.difficulte=self.niv_courant.difficulte
            self.mode_affichage=self.niv_courant.mode_affichage
            self.mode_minimap=self.niv_courant.mode_minimap
            self.recupere = True
        except IOError:
            self.niv_courant = Niveau(niveau,difficulte,mode_affichage,mode_minimap)
            #paramètres pour la réinitialisation
            self.difficulte=difficulte
            self.mode_affichage=mode_affichage
            self.mode_minimap=mode_minimap
            self.recupere = False
        self.joueur = None
        self.tuto_courant = 1
        #paramètres pourle transfert de niveau
        self.nb_niv_courant=niveau
        self.nb_niv_max=nb_niv_max
    def reset_niveau(self):
        """
        Fonction qui recommence le niveau courant
        """
        self.niv_courant = Niveau(self.nb_niv_courant,self.difficulte,self.mode_affichage,self.mode_minimap,None,True,self.joueur)
    def reset_niveau_tuto(self):
        """
        Fonction qui recommence le niveau courant
        """
        self.niv_courant = Niveau("tuto"+str(self.tuto_courant),self.difficulte,self.mode_affichage,self.mode_minimap,None,True,self.joueur)
    def transfert_niveau(self,joueur):
        """
        Fonction qui transfert les informations d'un niveau a un autre
        Entrées:
            -le niveau
        """
        self.niv_courant = Niveau(self.nb_niv_courant,self.difficulte,self.mode_affichage,self.mode_minimap,None,True,joueur)
    def transfert_niveau_tuto(self,joueur):
        """
        Fonction qui transfert les informations d'un niveau a un autre
        Entrées:
            -le niveau
        """
        self.niv_courant = Niveau("tuto"+str(self.tuto_courant),self.difficulte,self.mode_affichage,self.mode_minimap,None,True,joueur)
    def run(self):
        """
        Fonction qui prend en charge la boucle principale de la session
        """
        pause = False
        while (self.nb_niv_courant == "demo" or self.nb_niv_courant<=self.nb_niv_max) and not pause:
            res,win,joueur = self.niv_courant.run()
            fichier = open(sauvegarde,'wb')
            f = pickle.Pickler(fichier)
            f.clear_memo()
            f.dump(self.niv_courant.sauve())
            pause = (res==-1)
            if res == 0:
                if self.niv_courant.greater_teleportation:
                    self.teleporte(joueur,self.niv_courant.destination)
                    pause = True
            if self.nb_niv_courant == "demo":
                pygame.time.wait(res)
                pause = True
            elif res != -1:
                pygame.time.wait(res)
                if win:
                    self.nb_niv_courant+=1
                    if self.nb_niv_courant>self.nb_niv_max:
                        self.nb_niv_courant=1
                    self.joueur = joueur
                    self.transfert_niveau(joueur)
                else:
                    self.reset_niveau()

    def runtuto(self):
        """
        Fonction qui parcours les niveaux du tutoriels
        """
        pause = False
        fintuto = False
        while not (pause or fintuto):
            res,win,joueur = self.niv_courant.run()
            pause = (res==-1)
            if res == 0:
                if self.niv_courant.greater_teleportation:
                    self.teleporte(joueur,self.niv_courant.destination)
                    pause = True
            if res != -1:
                pygame.time.wait(res)
                if win:
                    self.tuto_courant+=1
                    self.joueur = joueur
                    try:
                        self.transfert_niveau_tuto(joueur)
                    except:
                        self.nb_niv_courant=1
                        fintuto = True
                else:
                    self.reset_niveau_tuto()

    def teleporte(self,joueur,destination):
        """
        Fonction qui téléporte le joueur entre les niveaux
        """
        self.niv_courant = Niveau(destination,self.difficulte,self.mode_affichage,self.mode_minimap,destination,True,joueur)
        self.joueur = joueur
        if isinstance(destination,str) and destination[:4] == "tuto":
            self.tuto_courant = int(destination[4:])
            self.runtuto()
        else:
            self.run()


import nose
from nose.case import (
    FunctionTestCase,
    try_run
)
from nose.loader import TestLoader
from nose.plugins import Plugin
from nose.pyversion import ismethod
from nose.util import (
    isclass,
    isgenerator
)
from mock import patch, MagicMock

class NewFunctionTestCase(FunctionTestCase):
    """
    Rewrite part of class FunctionTestCase, to get same format of output
    test results for generators, like other tests of class.
    """

    def __init__(
            self, cls, test, test_name, setUp=None, tearDown=None, arg=tuple(),
            descriptor=None
    ):
        self.test_name = test_name
        self.cls = cls
        self.inst = self.cls()
        FunctionTestCase.__init__(self, test, setUp, tearDown, arg, descriptor)

    def setUp(self):
        """
        Run setup function attached to the test function.
        Added setup_mocks which must be run when call mock from inherited class.
        """
        # import pdb; pdb.set_trace()
        try_run(self.inst, ('setup_mocks', 'setup', 'setUp', 'setUpFunc'))

    def tearDown(self):
        """
        Added teardown_mocks which must be run to stop all mocks.
        """
        try_run(self.inst, ('teardown_mocks', 'tearDown', 'setUp', 'setUpFunc'))

    def __str__(self):
        """
        Added self.test_name to display in output also "check_" part of test
        in full path to actual processing test.
        """
        func, _ = self._descriptors()
        name = func.__name__
        name = "%s.%s.%s.%s" % (
            self.cls.__module__,
            self.cls.__name__,
            self.test_name,
            name
        )
        return name
    __repr__ = __str__

class CustomLoaderForGenerators(TestLoader):
    """
    Based on nose function: loadTestsFromGenerator from loader.py,
    which Lazy-load tests from a generator function.
    """

    def load_tests_from_generator_method_with_set_up(
            self, generator, cls, mock_patch
    ):
        """
        Create instance of TestCase to call setUp, which is need to
        prevent error: (No api proxy found for service "datastore_v3)".
        """
        test_class_instance = cls()
        test_class_instance.setUp()
        test_class_instance.setup_mocks()

        def generate():
            """
            Rewrite internal function 'generate' from
            loadTestsFromGeneratorMethod to send 'test_name' of "check_"
            and limit operations to have only those which are required.
            """
            if mock_patch is None:
                for test in generator(test_class_instance):

                    test_check, arguments = self.parseGeneratedTest(test)
                    yield NewFunctionTestCase(
                        test=test_check,
                        arg=arguments,
                        cls=cls,
                        test_name=generator.im_func.__name__
                    )
            else:
                # Temp to prevent pep8 errors
                decorator_patch = MagicMock()
                decorator_patch.return_value = ''

                with patch(mock_patch) as decorator_patch:
                    for test in generator(test_class_instance):

                        test_check, arguments = self.parseGeneratedTest(test)
                        yield NewFunctionTestCase(
                            test=test_check,
                            arg=arguments,
                            cls=cls,
                            test_name=generator.im_func.__name__
                        )
        # Back to built-in nose method
        return self.suiteClass(generate, context=generator, can_split=False)

class YieldWithSetUp(Plugin):
    """
    Main plugin yield class definition. Features: Run setUp on generators
    and display full path in output during test.
    """
    name = 'yield'

    def __init__(self):
        super(YieldWithSetUp, self).__init__()

    def makeTest(self, obj, cls):
        """
        Only this function from nose API give test (cls) and testCase (cls).
        """
        mock_patch = None

        # If not found generators, then back to nose
        if not callable(cls):
            return None
        else:
            found_generator_test = test_check(obj, cls)

            # If generator is found, override loader from nose
            if found_generator_test:

                # Get mock.patch from decorator
                if hasattr(obj, 'patchings'):
                    mock_patch = get_mock_patch_of_test(obj)
                # 'return' is required on x when we have generators.
                # This will allow in test function, change to next
                # check function (internal function)
                x = CustomLoaderForGenerators()
                return x.load_tests_from_generator_method_with_set_up(
                    obj, cls, mock_patch
                )
            else:
                return None

def test_check(obj, cls):
    test_class_instance = cls()
    test_class_instance.setUp()
    test_class_instance.setup_mocks()
    generator = obj
    generator_test = generator(test_class_instance)
    return generator_test

def get_mock_patch_of_test(obj):
    mock_patch = obj.patchings[0].getter().__name__
    mock_patch += '.' + obj.patchings[0].attribute
    return mock_patch

if __name__ == '__main__':
    nose.main()


#!/usr/bin/python3
import os
import math
import argparse

from pathlib import Path

from pykinetic.classes import Energy,SimulationParameters,ConvergenceParameters
from pykinetic.writers import CplusplusWriter
import pykinetic.writers as writers
from pykinetic.utils import (BiasedChemicalSystem, write_indexfile,
                              calc_standard_state_correction)
from pykinetic.userinput import populate_chemicalsystem_fromfiles

__version__ = "0.1.0"

WRITERS = {'python':writers.python.Batch,
           'c++':CplusplusWriter}

def create_parser():
    """ Create a Command line argument parser """
    description =""" Creates a python script that contains a
    system of differential equations that represent a Chemical System
    and the tools to solve it numerically"""
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("compounds",type=Path,
                        help="File with the compounds and Energies")
    parser.add_argument("reactions",type=Path,
                        help="File with the reactions, energies and/or TSs")
    parser.add_argument("outfile", type=Path, help="Output script")
    parser.add_argument("--relative",
                        action='store_true',
                        default=False,
                        help="""Whether the energy in the reactions file is
                        relative to reactants. Otherwise the absolute value of
                        the TS structure is assumed to correspond to the
                        provided energy""")
    parser.add_argument("-I","--IndexFile",
                        action="store_true",
                        default=False,
                        help="""If Enabled creates a txt file named
                        'OutFile'.index with the indices used for reactions,
                        compounds and TSs""",
                        )
    parser.add_argument("-Ir","--indexfile-relative",
                        action="store_true",
                        dest="indexfile_relative",
                        default=False,
                        help="""Enforces relative barriers in the specified
                        default energy unit in the indexfile.""")
    parser.add_argument("-T","--Temperature",
                        nargs=2,
                        metavar=("Temp","Unit"),
                        default=(25.0,'C'),
                        help="Temperature and unit ('C'=Celsius and 'K'=Kelvin)")
    parser.add_argument("--unit",
                        default='kcal/mol',
                        help="""Energy unit used in all the energy values whose
                        unit is not specified""",
                        choices=Energy._units)
    parser.add_argument("--bias",
                        nargs='?',
                        default=0.0,
                        const='Standard State',
                        help="""Applies a bias to all energies. If no argument
                        is provided it applies Standard State correction.
                        Otherwise, uses the specified value """)
    parser.add_argument("--convergence",
                        type=Path,
                        default=None,
                        help="Path to the file with the Convergence parameters")
    parser.add_argument("--simulation",
                        type=Path,
                        default=None,
                        help="Path to the file with the Simulation parameters")
    parser.add_argument("--tail",
                        type=Path,
                        default=None,
                        help="""Path to the non-standard Template for the ending
                         part of the generated script""")
    parser.add_argument("--header",
                        type=Path,
                        default=None,
                        help=""" Path to the non-standard Template for the head
                        of the generated script""")
    parser.add_argument("--writer",
                        choices=['python','c++'],
                        default='python',
                        help="format for the output model")

    return parser
def parse_arguments(parser):
    """ Check the arguments of the parser and prepare them for their use """
    args = parser.parse_args()

    # Read the header if it has been specified
    if args.header is not None:
        with open(args.header) as F:
            txt = F.read()
        args.header = txt
    # Read the tail if it has been specified
    if args.tail is not None:
        with open(args.tail) as F:
            txt = F.read()
        args.tail = txt

    # Handle Temperature
    if args.Temperature[1] == 'C':
        T = float(args.Temperature[0]) + 273.15
    elif args.Temperature[1] == 'K':
        T = float(args.Temperature[0])
    else:
        msg = "Temperature in {} not implemented".format(args.Temperature[1])
        raise NotImplementedError(msg)
    args.Temperature = T

    # Handle bias
    if args.bias == 'Standard State':
        bias = calc_standard_state_correction(T)
        bias.to_unit(args.unit)
    else:
        bias = Energy(args.bias,args.unit)
    args.bias = bias

    # Handle simulation parameters
    if args.simulation is None:
        sim_params = SimulationParameters()
    else:
        sim_params = SimulationParameters.read_from(args.simulation)
    args.simulation = sim_params

    # Handle convergence parameters
    if args.convergence is None:
        conv_params = ConvergenceParameters()
    else:
        conv_params = ConvergenceParameters.read_from(args.convergence)
    args.convergence = conv_params

    if not args.IndexFile and args.indexfile_relative:
        args.IndexFile = True

    return args

def main():
    parser = create_parser()
    args = parse_arguments(parser)
    unit = args.unit
    T = args.Temperature
    # Initialize the Writer
    writer_cls = WRITERS[args.writer]
    writer = writer_cls(conc_var='x',mb_var='dxdt',fun_var='model',
                        jac_var='Jac',jac_fun_var='jacobian',
                        header=args.header,tail=args.tail)
    writer.set_parameters(simulation=args.simulation,
                          convergence=args.convergence)
    # Initialize the ChemicalSystem
    chemsys = BiasedChemicalSystem(bias=args.bias,T=args.Temperature,unit=unit)
    populate_chemicalsystem_fromfiles(chemsys,
                                      file_c=args.compounds,
                                      file_r=args.reactions,
                                      energy_unit=unit,
                                      relativeE=args.relative)
    chemsys.apply_bias()

    data_file_name = f'{args.outfile.stem}.data'
    writer.parameters['out_filename'] = data_file_name
    writer.write(chemsys,args.outfile)

    if args.IndexFile:
        stem = args.outfile.stem
        index_file = args.outfile.parent / f'{stem}.index'
        isrelative = args.indexfile_relative or args.relative
        write_indexfile(chemsys,index_file, isrelative=isrelative)

if __name__ == '__main__':
    main()


import sqlite3
connection=sqlite3.connect("hospital.db")
getDPhno=input("Enter Doctor's Phone number: ")

getDName=input("Enter Doctor's Name: ")
getDQual=input("Enter Doctor's Qualification: ")
getDAdd=input("Enter Doctor's Address: ")
getDEmail=input("Enter Doctor's Email ID: ")
connection.execute("UPDATE DOCTOR\
                   SET DOCTOR_NAME='"+getDName+"',DOCTOR_QUALIFICATION='"+getDQual+"',DOCTOR_ADDRESS='"+getDAdd+"',DOCTOR_EMAILID='"+getDEmail+"'\
                    WHERE DOCTOR_PHNO="+getDPhno)
connection.commit()
print(f'Updated Doctor {getDName} details successfully')
res = connection.execute(" SELECT * FROM DOCTOR ")
for i in res:
    print("Id",i[0])
    print("Doctors Name: ", i[1])
    print("Doctors Qualification: ", i[2])
    print("Doctors Address: ", i[3])
    print(" Doctors Phone number: ", i[4])
    print("Doctors Email ID: ", i[5])
    print("*********$$$$$$***********")


# Imports
import os
from plt import PLT
from data_utils import Data

# Functions
def plt_multiclass(logger, **args):
    result_dir = '../results/'
    logger.info(f'Results directory: {result_dir}')
    if not os.path.exists(result_dir):
        os.makedirs(result_dir)
        logger.debug(f'Results directory does not exist. Creating the directory')
    log_fname = logger.handlers[0].baseFilename.split('/')[-1]
    result_fname = log_fname.replace('_log_', '_results_').replace('.log', '.results')
    logger.debug(f'Results filepath: {result_dir}{result_fname}')
    result_fh = open(f'{result_dir}{result_fname}', 'w')

    print('---PROGRESSIVE ELM - MULTI-CLASS---')
    print('---PROGRESSIVE ELM - MULTI-CLASS---', file=result_fh)

    filename = args['filename']
    header = args['header']
    label_location = args['label']
    scale_type = args['scale']
    test_ratio = args['testratio']

    print(f'Dataset file: {filename}', file=result_fh)
    logger.debug(f'Dataset File: {filename}, Scaling Type: {scale_type}, Test ratio: {test_ratio}')
    logger.debug(f'Header attribute in pandas read_csv: {header}, label location in csv: {label_location}')

    # Hyper-parameters
    hidden_dim = args['neurons']
    N0 = args['initial']
    batch_size = args['batch']

    logger.debug(f'Hidden layer neurons: {hidden_dim}, Number of samples in initial block: {N0}, '
                f'Batch size for training: {batch_size}')

    # Load and Pre-process Data
    logger.info('Loading and preprocessing data...')
    data = Data()
    data.set_logger(logger)
    print(f'Loading data from file: {filename}')
    data.load_csv(fname=filename, header=header)
    data.get_feats_labels(label_column=label_location)
    data.scale_features(scale_type=scale_type)
    data.split_train_test(test_ratio=test_ratio)
    logger.info('Loading and preprocessing data done')

    # PLT-ELM Model
    logger.info('Creating PLT ELM model')
    try:
        plt= PLT(data.num_feats, hidden_dim)
    except:
        logger.error('---> !!! Error in creating PLT class object !!!')
        raise ValueError

    plt.set_logger(logger)

    data.set_initial_batch_size(N0)
    data.set_training_batch_size(batch_size)

    print('Begin Training...')
    logger.info('Begin Training...')
    for batch_id, X, y in data.fetch_train_data():
        if batch_id == 0:
            plt.initial_batch(X, y)
            continue
        plt.train(X, y)

    print('Training Complete')
    logger.info('Training Complete')

    print('Begin Testing...')
    logger.info('Begin Testing...')
    report, accuracy = plt.test(data.test_data['X'], data.test_data['y'])
    print('Testing Complete')
    logger.info('Testing Complete')

    print(f'Classification Report: \n{report}\nAccuracy: {accuracy}')
    print(f'Classification Report: \n{report}\nAccuracy: {accuracy}', file=result_fh)
    logger.info(f'\nClassification Report: \n{report}')
    logger.info(f'\nAccuracy: {accuracy}')
    pass

# Main
if __name__ == '__main__':
    pass


from __future__ import print_function, division
import os
from torchvision.datasets.vision import VisionDataset
from PIL import Image
from collections import defaultdict

# Ignore warnings
import warnings
warnings.filterwarnings("ignore")

LABEL_TO_STRING = {
    11: "Fixed-wing Aircraft",
    12: "Small Aircraft",
    13: "Passenger/Cargo Plane",
    15: "Helicopter",
    17: "Passenger Vehicle",
    18: "Small Car",
    19: "Bus",
    20: "Pickup Truck",
    21: "Utility Truck",
    23: "Truck",
    24: "Cargo Truck",
    25: "Truck Tractor w/ Box Trailer",
    26: "Truck Tractor",
    27: "Trailer",
    28: "Truck Tractor w/ Flatbed Trailer",
    29: "Truck Tractor w/ Liquid Tank",
    32: "Crane Truck",
    33: "Railway Vehicle",
    34: "Passenger Car",
    35: "Cargo/Container Car",
    36: "Flat Car",
    37: "Tank car",
    38: "Locomotive",
    40: "Maritime Vessel",
    41: "Motorboat",
    42: "Sailboat",
    44: "Tugboat",
    45: "Barge",
    47: "Fishing Vessel",
    49: "Ferry",
    50: "Yacht",
    51: "Container Ship",
    52: "Oil Tanker",
    53: "Engineering Vehicle",
    54: "Tower crane",
    55: "Container Crane",
    56: "Reach Stacker",
    57: "Straddle Carrier",
    59: "Mobile Crane",
    60: "Dump Truck",
    61: "Haul Truck",
    62: "Scraper/Tractor",
    63: "Front loader/Bulldozer",
    64: "Excavator",
    65: "Cement Mixer",
    66: "Ground Grader",
    71: "Hut/Tent",
    72: "Shed",
    73: "Building",
    74: "Aircraft Hangar",
    76: "Damaged Building",
    77: "Facility",
    79: "Construction Site",
    83: "Vehicle Lot",
    84: "Helipad",
    86: "Storage Tank",
    89: "Shipping container lot",
    91: "Shipping Container",
    93: "Pylon",
    94: "Tower",
}

class XviewDataset(VisionDataset):
    """xView object detection dataset.

    Args:
        root_dir (string): Directory with all the images.
        annFile (string): Path to json annotation file.
        transform (callable, optional): A function/transform that  takes in an PIL image
            and returns a transformed version. E.g, ``transforms.ToTensor``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        transforms (callable, optional): A function/transform that takes input sample and its target as entry
            and returns a transformed version.
    """

    def __init__(self, root, annFile, transform=None, target_transform=None, transforms=None):
        super(XviewDataset, self).__init__(root, transforms, transform, target_transform)
        image_ids = [int(f.replace(".tif", "")) for f in os.listdir(root)]

        if not os.path.isdir(root):
            raise RuntimeError('Dataset not found or corrupted.' +
                               ' Go to <http://xviewdataset.org/> to download it.')

        if not os.path.isfile(annFile):
            raise RuntimeError('Annotation file not found or corrupted.' +
                               ' See <http://xviewdataset.org/> to download it.')

        # === load annotations ===
        import json
        import time
        with open(annFile, "r") as f:
            print("loading annotations into memory...")
            t0 = time.time()
            raw_anns = json.load(f)
            raw_anns = raw_anns["features"]
            t1 = time.time()
            print(f"Done (t={t1-t0:.2f}s)")

        # === create index ===
        print("creating index...")
        image_id_to_object_ids = defaultdict(list)
        objects = []
        for i in range(len(raw_anns)): # for all objects
            image_id = int(raw_anns[i]["properties"]["image_id"].replace(".tif", ""))
            xmin, ymin, xmax, ymax = map(int, raw_anns[i]["properties"]["bounds_imcoords"].split(","))
            x = xmin
            y = ymin
            w = xmax - xmin
            h = ymax - ymin
            type_id = raw_anns[i]["properties"]["type_id"]
            image_id_to_object_ids[image_id].append(i)
            objects.append({
                "image_id": image_id,
                "bbox": [x, y, w, h],
                "category_id": type_id
            })
        print("index created!")
        self.image_ids = image_ids
        self.image_id_to_object_ids = image_id_to_object_ids
        self.objects = objects

    def __getitem__(self, index):
        """
        Args:
            index (int): Index

        Returns:
            tuple: Tuple (image, target) where target is a dictionary.
        """
        img_id = self.image_ids[index]
        ann_ids = self.image_id_to_object_ids[img_id]
        target = []
        for ann_id in ann_ids:
            target.append(self.objects[ann_id])

        fname = os.path.join(self.root, str(img_id) + ".tif")

        img = Image.open(os.path.join(self.root, fname)).convert("RGB")
        if self.transforms is not None:
            img, target = self.transforms(img, target)
        return img, target

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

if __name__ == "__main__":
    ds = XviewDataset(root="/home/nitta/data/xview/train_images",
                      annFile="/home/nitta/data/xview/xView_train.geojson")
    img, target = ds[0]
    print(img)
    print(target)


import pandas as pd
import math
import time
import requests
import json

class InvalidResponseError(Exception):
    """Base class for exceptions in this module."""
    pass

session = requests.Session()

def upload_incidents(data):
    # initialized = getattr(SESSION_THREAD_LOCAL, 's', None)
    # if initialized is None:
    #     SESSION_THREAD_LOCAL.s = requests.Session()
    start = time.time()
    domain = "sf"
    staging_url = "https://statecraft-api-staging.herokuapp.com/api"
    production_url = "https://api.statecrafthq.com/api"
    local_url = "http://localhost:9000/api"
    local_docker_url = "http://docker.for.mac.localhost:9000/api"

    # if SERVER == "prod":
    #     url = production_url
    # elif SERVER == "staging":
    #     url = staging_url
    #     domain = "sfhousing"
    # elif SERVER == "docker":
    #     url = local_docker_url
    # else:
    url = production_url

    headers = {
        'x-statecraft-domain': domain,
        'Content-Type': 'application/json'
    }
    container = {
        "query":
        "mutation($data: [IncidentInput!]!) { updateIncidents(area: \"sf\", incidents: $data) }",
        "variables": {
            "data": data
        }
    }
    data = json.dumps(container)
    for i in range(3):
        response = session.post(url, data=data, headers=headers, stream=False)
        try:
            rdata = json.loads(response.text)
            if rdata['data']['updateIncidents'] != 'ok':
                raise InvalidResponseError("Wrong response!")
        except BaseException as e:
            print("Wrong Response!")
            print("Sent:")
            print(data)
            print("Got:")
            print(response.text)
            raise e

    #    print(r.text)
    end = time.time()
    return end - start

print("Loading Crime Dataset...")
start = time.time()
CRIMES = pd.read_csv(
    "downloads/SF_Crime.csv",
    sep=',',
    dtype={
        'PdId': str,
        'IncidntNum': str,
        'Date': str,
        'Time': str,
        # 'X': str,
        # 'Y': str
    })
end = time.time()
print(end - start)

category_map = {}
category_map['NON-CRIMINAL'] = 'NON_CRIMINAL'
category_map['ROBBERY'] = 'ROBBERY'
category_map['ASSAULT'] = 'ASSAULT'
category_map['SECONDARY CODES'] = 'SECONDARY_CODES'
category_map['VANDALISM'] = 'VANDALISM'
category_map['BURGLARY'] = 'BURGLARY'
category_map['LARCENY/THEFT'] = 'LARCENY'
category_map['DRUG/NARCOTIC'] = 'DRUG'
category_map['WARRANTS'] = 'WARRANTS'
category_map['VEHICLE THEFT'] = 'VEHICLE_THEFT'
category_map['ARSON'] = 'ARSON'
category_map['MISSING PERSON'] = 'MISSING_PERSON'

category_map['DRIVING UNDER THE INFLUENCE'] = 'DRIVING_UNDER_THE_INFLUENCE'
category_map['SUSPICIOUS OCC'] = 'SUSPICIOUS_OCC'
category_map['RECOVERED VEHICLE'] = 'RECOVERED_VEHICLE'
category_map['DRUNKENNESS'] = 'DRUNKENNESS'
category_map['TRESPASS'] = 'TRESPASS'

category_map['FRAUD'] = 'FRAUD'
category_map['DISORDERLY CONDUCT'] = 'DISORDERLY_CONDUCT'
category_map['SEX OFFENSES, FORCIBLE'] = 'SEX_OFFENSES_FORCIBLE'
category_map['FORGERY/COUNTERFEITING'] = 'COUNTERFEITING'
category_map['KIDNAPPING'] = 'KIDNAPPING'
category_map['EMBEZZLEMENT'] = 'EMBEZZLEMENT'
category_map['STOLEN PROPERTY'] = 'STOLEN_PROPERTY'
category_map['LIQUOR LAWS'] = 'LIQUOR_LAWS'
category_map['FAMILY OFFENSES'] = 'FAMILY_OFFENSES'
category_map['LOITERING'] = 'LOITERING'
category_map['BAD CHECKS'] = 'BAD_CHECKS'
category_map['TREA'] = 'TREA'
category_map['GAMBLING'] = 'GAMBLING'
category_map['RUNAWAY'] = 'RUNAWAY'
category_map['BRIBERY'] = 'BRIBERY'
category_map['PROSTITUTION'] = 'PROSTITUTION'
category_map['PORNOGRAPHY/OBSCENE MAT'] = 'PORNOGRAPHY'
category_map['SEX OFFENSES, NON FORCIBLE'] = 'SEX_OFFENSES_NON_FORCIBLE'
category_map['SUICIDE'] = 'SUICIDE'
category_map['EXTORTION'] = 'EXTORTION'
category_map['OTHER OFFENSES'] = 'OTHER_OFFENSES'
category_map['WEAPON LAWS'] = 'WEAPON_LAWS'

# 'NON-CRIMINAL' 'ROBBERY' 'ASSAULT' 'SECONDARY CODES' 'VANDALISM'
#  'BURGLARY' 'LARCENY/THEFT' 'DRUG/NARCOTIC' 'WARRANTS' 'VEHICLE THEFT'
#  'OTHER OFFENSES' 'WEAPON LAWS' 'ARSON' 'MISSING PERSON'
#  'DRIVING UNDER THE INFLUENCE' 'SUSPICIOUS OCC' 'RECOVERED VEHICLE'
#  'DRUNKENNESS' 'TRESPASS' 'FRAUD' 'DISORDERLY CONDUCT'
#  'SEX OFFENSES, FORCIBLE' 'FORGERY/COUNTERFEITING' 'KIDNAPPING'
#  'EMBEZZLEMENT' 'STOLEN PROPERTY' 'LIQUOR LAWS' 'FAMILY OFFENSES'
#  'LOITERING' 'BAD CHECKS' 'TREA' 'GAMBLING' 'RUNAWAY' 'BRIBERY'
#  'PROSTITUTION' 'PORNOGRAPHY/OBSCENE MAT' 'SEX OFFENSES, NON FORCIBLE'
#  'SUICIDE' 'EXTORTION'

data = []
for i in range(len(CRIMES)):
    d = CRIMES.iloc[i]
    data.append({
        'id': d['PdId'],
        'incidentNumber': d['IncidntNum'],
        'category': category_map[d['Category']],
        'location': {
            'latitude': d['Y'],
            'longitude': d['X']
        },
        'date': pd.to_datetime(d['Date'] + ' ' + d['Time']).strftime('%Y-%m-%d %H:%M:%S'),
        'description': d['Descript'],
        'resolution': d['Resolution'],
        'address': d['Address']
    })

    if len(data) > 200:
        start = time.time()
        upload_incidents(data)
        data = []
        end = time.time()
        print(end - start)

if len(data) > 200:
        upload_incidents(data)

# def converter(s):

#     if isinstance(s, str):
#         return s.split('|')[0].split(',')[0]
#     return 'None'

# PARCELS["districtna_converted"] = PARCELS["districtna"].apply(converter)
# PARCELS["zoning_sim_converted"] = PARCELS["zoning_sim"].apply(converter)

# print(PARCELS.groupby("districtna_converted").size())
# print(PARCELS["zoning_sim_converted"].unique())
# print(PARCELS.groupby("zoning_sim_converted").size())
# print(PARCELS[PARCELS["zoning_sim"] == "C-2|P"])


from utils.openbis_utils import create_sample_identifier_string, create_project_identifier_string, create_experiment_identifier_string
from .update_parsers import CreationToUpdateParserFactory
from ..definition_to_creation import SampleDefinitionToCreationType, ScriptDefinitionToCreationType, \
    ProjectDefinitionToCreationType, ExperimentDefinitionToCreationType

class CreationToUpdateParser(object):

    @staticmethod
    def parse(creations_that_exist, existing_elements):
        updates = {}
        for creation_type, creations in creations_that_exist.items():
            parsers = CreationToUpdateParserFactory.get_parsers(creation_type)
            for creation in creations:
                existing_element = get_existing_element_based_on(creation_type, creation,
                                                                 existing_elements[creation_type])
                if existing_element:
                    for parser in parsers:
                        update = parser.parse(creation, existing_element)
                        update_type = parser.get_type()
                        if update_type not in updates:
                            updates[update_type] = []
                        updates[update_type].extend(update if type(update) == list else [update])

        return updates

def get_existing_element_based_on(creation_type, creation, existing_elements):
    if creation_type == SampleDefinitionToCreationType:
        existing_element = list(filter(
            lambda existing_element: create_sample_identifier_string(creation) == existing_element.identifier.identifier,
            existing_elements))
    elif creation_type == ProjectDefinitionToCreationType:
        existing_element = list(filter(
            lambda existing_element: create_project_identifier_string(creation) == str(existing_element.identifier),
            existing_elements))
    elif creation_type == ExperimentDefinitionToCreationType:
        existing_element = list(filter(
            lambda existing_element: create_experiment_identifier_string(creation) == str(existing_element.identifier),
            existing_elements))
    else:
        if creation_type == ScriptDefinitionToCreationType:
            attr = 'name'
        else:
            attr = 'code'
        existing_element = list(
            filter(lambda existing_element: getattr(creation, attr) == getattr(existing_element, attr),
                   existing_elements))
    return None if len(existing_element) == 0 else existing_element[0]


import functools
import logging

from lightsc import requests

from .. import grids
from ..types import Dimensions, Position

from . import actions
from .elements import (
    Button,
    PowerButton,
    UILayer,
    groups,
)

logger = logging.getLogger("library.python.monolight.ui")

class _ToggleUI(actions.Action):

    def __init__(self, grid: grids.MonomeGrid) -> None:
        actions.Action.__init__(self)
        self._grid = grid

    async def _run(self) -> None:
        self._grid.show_ui = not self._grid.show_ui

def root(grid: grids.MonomeGrid) -> UILayer:
    foreground_layer = UILayer("root", grid.size)

    foreground_layer.insert(Button("toggle ui", Position(15, 7), actions={
        Button.ActionEnum.UP: _ToggleUI(grid),
    }))

    # some shortcuts:
    foreground_layer.insert(Button("off *", Position(0, 7), actions={
        Button.ActionEnum.UP: actions.Lightsd(
            requests=[requests.PowerOff], targets=["*"]
        )
    }))
    foreground_layer.insert(Button("on *", Position(1, 7), actions={
        Button.ActionEnum.UP: actions.Lightsd(
            requests=[requests.PowerOn], targets=["*"]
        )
    }))
    foreground_layer.insert(
        Button("4000k kitchen", Position(2, 7), actions={
            Button.ActionEnum.UP: actions.Lightsd(requests=[
                functools.partial(
                    requests.SetLightFromHSBK,
                    ["neko"], 0.0, 0.0, 1.0, 4000, 1000,
                ),
                functools.partial(requests.PowerOn, ["neko"]),
            ])
        })
    )
    foreground_layer.insert(Button("orange", Position(3, 7), actions={
        Button.ActionEnum.UP: actions.Lightsd(requests=[
            functools.partial(
                requests.SetLightFromHSBK,
                ["#tower"], 37.469443, 1.0, 0.25, 3500, 600,
            ),
            functools.partial(
                requests.SetLightFromHSBK,
                ["fugu"], 47.469443, 0.2, 0.1, 3500, 600,
            ),
            functools.partial(
                requests.SetLightFromHSBK,
                ["candle"], 47.469443, 0.2, 0.15, 3500, 600,
            ),
            functools.partial(requests.PowerOn, ["#br"])
        ]),
    }))
    foreground_layer.insert(Button("blue", Position(4, 7), actions={
        Button.ActionEnum.UP: actions.Lightsd(requests=[
            functools.partial(
                requests.SetLightFromHSBK,
                ["#tower"], 163.237354, 0.923552, 0.25, 3500, 600,
            ),
            functools.partial(
                requests.SetLightFromHSBK,
                ["fugu"], 47.469443, 0.2, 0.1, 3500, 600,
            ),
            functools.partial(
                requests.SetLightFromHSBK,
                ["candle"], 47.469443, 0.2, 0.15, 3500, 600,
            ),
            functools.partial(requests.PowerOn, ["#br"])
        ]),
    }))
    foreground_layer.insert(PowerButton(
        "#br", Position(5, 7), targets=["#br"]
    ))
    foreground_layer.insert(PowerButton(
        "white", Position(6, 7), targets=["white"]
    ))

    # some control blocks:
    BulbControlPad = functools.partial(
        groups.BulbControlPad, sliders_size=Dimensions(width=1, height=6),
    )
    foreground_layer.insert(BulbControlPad(
        "kitchen control", Position(0, 0), targets=["neko"],
    ))
    foreground_layer.insert(BulbControlPad(
        "#tower control", Position(4, 0), targets=["#tower"],
    ))
    foreground_layer.insert(BulbControlPad(
        "fugu control", Position(8, 0), targets=["fugu"],
    ))
#   foreground_layer.insert(BulbControlPad(
#       "candle control", Position(12, 0), targets=["candle"],
#   ))

    return foreground_layer


'''
Author: Nam Tran
This file contains code for the data analytics module. To run: python data_analytics.py [questions.csv] [answers.csv] [output_path_for_csv_file - optional]
'''

from textblob import TextBlob
import matplotlib.pyplot as plt
import numpy as np
import statistics
import socket
import csv
import sys
import math

plt.rcParams.update({'figure.max_open_warning': 0})

def create_histogram(question_num, distribution_list):
# Input: two lists of student and question distributions
# Return type: N/A
# Function: creates histograms
    answer_list = []
    for index, val in enumerate(distribution_list):
        for i in range(val):
            answer_list.append(index+1)

    num_bins = num_choices
    lower_bound, upper_bound = 1, num_choices+1

    y = np.asarray(answer_list)
    hist, bin_edges = np.histogram(y, num_bins, range=(lower_bound, upper_bound))
    width = bin_edges[1] - bin_edges[0]
    fig = plt.figure()
    plt.bar(bin_edges[:-1], hist, align='center', width=width*0.6, edgecolor='k', facecolor='blue', alpha=0.5)
    plt.xticks(range(num_bins+1))
    plt.yticks(range(max(distribution_list)+1))
    plt.xlim([lower_bound-width/2, upper_bound-width/2])
    plt.title("Question {}".format(question_num))
    plt.xlabel("Choice")
    plt.ylabel("Number of Students Choosing")
    plt.savefig(output_path + "histogram_question_{}".format(question_num)) # saves the figure as a file

def calc_mean(distribution_list):
# Input: a list of student and question distribution
# Return type: float
# Function: returns a mean value
    summation = 0
    for choice, num_responses in enumerate(distribution_list):
        summation+=(choice+1)*num_responses
    return summation/len(distribution_list)

def calc_standard_deviation(distribution_list):
# Input: a list of student and question distribution
# Return type: float
# Function: returns std value

    list_of_responses = []
    for choice, num_responses in enumerate(distribution_list):
        for i in range(num_responses):
            list_of_responses.append(choice+1)

    return round(statistics.stdev(list_of_responses),1)

def calc_median(distribution_list):
# Input: a list of student and question distribution
# Return type: int
# Function: returns median value
    list_of_responses = []
    for choice, num_responses in enumerate(distribution_list):
        for i in range(num_responses):
            list_of_responses.append(choice+1)
    return statistics.median(list_of_responses)

def calc_mode(distribution_list):
# Input: a list of student and question distribution
# Return type: int
# Function: returns mode value
    list_of_responses = []
    for choice, num_responses in enumerate(distribution_list):
        for i in range(num_responses):
            list_of_responses.append(choice+1)

    most = max(list(map(list_of_responses.count, list_of_responses)))
    return list(set(filter(lambda val: list_of_responses.count(val) == most, list_of_responses)))

def write_csv():
# Input: N/A
# Return type: N/A
# Function: writes generated analytical values to a csv file.
    with open(output_path + "analytics.csv", mode = "w", newline = '') as out_file:
        file_writer = csv.writer(out_file, delimiter = ",", quotechar = '"', quoting = csv.QUOTE_MINIMAL)
        file_writer.writerow(["MULTIPLE_CHOICE QUESTION:"])
        file_writer.writerow(["is_multiple_choice","question_number","most_popular_choice","mean","mode","median","standard_deviation"])
        file_writer.writerow(["OPEN_ENDED QUESTION:"])
        file_writer.writerow(["is_open_ended","question_number","percentage_positive_all_students", "percentage_negative_all_students", "percentage_subjective_all_students", "percentage_objective_all_students", "percentage_neutral_all_students"])
        for row_number, row_content in analytics_dict.items():
            file_writer.writerow(row_content)

def numerical_metrics():
# Input: N/A
# Return type: N/A
# Function: generates numerical values for various numerical metrics
    for key, distribution_list in numeric_question_dict.items():
        num_students_most_popular_choice = max(distribution_list)
        most_popular_choice = distribution_list.index(num_students_most_popular_choice) + 1 # the first answer choice starts at 1
        print("For question {}:".format(key)) # included in csv file
        print("- Number of students choosing each numeric choice: {}".format(distribution_list))
        print("- Most chosen numeric choice is {}".format(most_popular_choice)) # included in csv file
        print("- Mean value of numeric response is {}".format(calc_mean(distribution_list))) # included in csv file
        print("- Mode value(s) of numeric response is/are {}".format(calc_mode(distribution_list))) # included in csv file
        print("- Median value of numeric response is {}".format(calc_median(distribution_list))) # included in csv file
        print("- Standard deviation of numeric response is {}".format(calc_standard_deviation(distribution_list))) # included in csv file
        print("------------------------------")
        create_histogram(key, distribution_list)

        analytics_dict[key] = []
        new_row = ["multiple_choice",key, most_popular_choice, calc_mean(distribution_list), calc_mode(distribution_list), calc_median(distribution_list), calc_standard_deviation(distribution_list)]
        analytics_dict[key].extend(new_row)

def sentiment_analysis():
# Input: N/A
# Return type: N/A
# Function: performs sentiment analysis and generates relevant pie charts

    num_positive = 0
    num_negative = 0
    num_objective = 0
    num_subjective = 0
    num_neutral = 0

    num_aggregate_responses = 0

    for key, response_list in student_dict.items():
        print("Performing sentiment analysis on textual responses from student {}".format(key))
        for index, response in enumerate(response_list):
            if not response.isnumeric(): # only perform sentiment analysis on non-numeric responses

                analytics_dict[index+1] = []
                obj = TextBlob(response)
                polarity = round(obj.sentiment.polarity,1) # a value within the range [-1.0,1.0]
                subjectivity = round(obj.sentiment.subjectivity,1) # a value between [0.0,1.0]
                status = []
                num_aggregate_responses+=1

                if polarity == 0 or subjectivity == 0.5:
                    status.append("Neutral")
                    overall_sentiment = "Neutral"
                    num_neutral+=1
                    print("Question {}: {} response".format(index+1, overall_sentiment))
                    textual_question_dict[index+1].append([status[0]])
                else:

                    if polarity > 0:
                        status.append('Positive')
                        num_positive+=1
                    elif polarity < 0:
                        status.append('Negative')
                        num_negative+=1

                    if subjectivity < 0.5:
                        status.append("Objective")
                        num_objective+=1
                    elif subjectivity > 0.5:
                        status.append("Subjective")
                        num_subjective+=1

                    overall_sentiment = "{} and {}".format(status[0], status[1])
                    print("Question {}: {} response".format(index+1, overall_sentiment))
                    textual_question_dict[index+1].append([status[0],status[1]])

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

    for question_id, response_sentiment_list in textual_question_dict.items():
        analytics_dict[question_id] = []
        flattened_list = [item for sublist in response_sentiment_list for item in sublist]
        percentage_positive = round((flattened_list.count("Positive")/len(response_sentiment_list))*100, 1)
        percentage_negative = round((flattened_list.count("Negative")/len(response_sentiment_list))*100, 1)
        percentage_objective = round((flattened_list.count("Objective")/len(response_sentiment_list))*100, 1)
        percentage_subjective = round((flattened_list.count("Subjective")/len(response_sentiment_list))*100, 1)
        percentage_neutral = round((flattened_list.count("Neutral")/len(response_sentiment_list))*100, 1)
        new_row = ["open_ended",question_id, percentage_positive, percentage_negative, percentage_subjective, percentage_objective, percentage_neutral]
        analytics_dict[question_id].extend(new_row)

        # pie charts
        labels = 'Subjective', 'Objective'
        sizes = [(flattened_list.count("Subjective")/len(response_sentiment_list))*360, (flattened_list.count("Objective")/len(response_sentiment_list))*360]
        colors = ['gold', 'yellowgreen']
        explode = (0, 0.1)
        subjectivity_chart_per_question = plt.figure()
        plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)
        plt.axis('equal')
        plt.title("Question {}: Subjectivity Chart for Student Responses".format(question_id))
        plt.savefig(output_path + "subjectivity_chart_question_{}".format(question_id)) # saves the figure as a file

        labels = 'Positive', 'Negative'
        sizes = [(flattened_list.count("Positive")/len(response_sentiment_list))*360, (flattened_list.count("Negative")/len(response_sentiment_list))*360]
        colors = ['gold', 'yellowgreen']
        explode = (0, 0.1)
        polarity_chart_per_question = plt.figure()
        plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)
        plt.title("Question {}: Polarity Chart for Student Responses".format(question_id))
        plt.axis('equal')
        plt.savefig(output_path + "polarity_chart_question_{}".format(question_id)) # saves the figure as a file

    print("Percentage of positive responses out of textual responses among all students: {:.1f}%".format((num_positive/num_aggregate_responses)*100))
    print("Percentage of negative responses out of textual responses among all students: {:.1f}%".format((num_negative/num_aggregate_responses)*100))
    print("Percentage of objective responses out of textual responses among all students: {:.1f}%".format((num_objective/num_aggregate_responses)*100))
    print("Percentage of subjective responses out of textual responses among all students: {:.1f}%".format((num_subjective/num_aggregate_responses)*100))
    print("Percentage of neutral responses out of textual responses among all students: {:.1f}%".format((num_neutral/num_aggregate_responses)*100))

    # pie charts
    labels = 'Subjective', 'Objective'
    sizes = [(num_subjective/num_aggregate_responses)*360, (num_objective/num_aggregate_responses)*360]
    colors = ['gold', 'yellowgreen']
    explode = (0, 0.1)
    subjectivity_chart = plt.figure()
    plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)
    plt.axis('equal')
    plt.title("Subjectivity Chart for All Student Textual Responses")
    plt.savefig(output_path + "subjectivity_chart") # saves the figure as a file

    labels = 'Positive', 'Negative'
    sizes = [(num_positive/num_aggregate_responses)*360, (num_negative/num_aggregate_responses)*360]
    colors = ['gold', 'yellowgreen']
    explode = (0, 0.1)
    polarity_chart = plt.figure()
    plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)
    plt.title('Polarity Chart for All Student Textual Responses')
    plt.axis('equal')
    plt.savefig(output_path + "polarity_chart") # saves the figure as a file

def parse():
# Input: N/A
# Return type: N/A
# Function: parses the raw csv data file.
    global student_count

    with open(questions_file) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter = ',', quotechar ='"', quoting = csv.QUOTE_ALL)
        next(csv_reader) # skip a row
        line_count = 0
        for index, row in enumerate(csv_reader):
            question_type_dict[index+1] = int(row[len(row)-1])

    with open(answers_file) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter = ',', quotechar ='"', quoting = csv.QUOTE_ALL)
        next(csv_reader) # skip a row
        line_count = 0

        for row in csv_reader:
            if line_count % num_questions == 0:
                student_count+=1
                student_dict[student_count] = [] # create an empty list to hold the student's responses
            if question_type_dict[int(row[4])] == 1 or question_type_dict[int(row[4])] == 2:
                student_dict[student_count].append(row[1])
                numeric_question_dict[int(row[4])] = [0]*num_choices
            elif question_type_dict[int(row[4])] == 3: # open-ended question
                student_dict[student_count].append(row[2])
            line_count+=1

        for key, response_list in student_dict.items(): # tally the responses for each numeric question
            for index, response in enumerate(response_list):
                if question_type_dict[index+1] == 1 or question_type_dict[index+1] == 2:
                    numeric_question_dict[index+1][int(response)-1]+=1
                elif question_type_dict[index+1] == 3:
                    textual_question_dict[index+1] = []

def main():
# Input: N/A
# Return type: N/A
# Function: the main function that drives the entire module.
    parse()
    print("Number of multiple choice questions: \n {}".format(list(question_type_dict.values()).count(1) + list(question_type_dict.values()).count(2))) # count number of multiple-choice questions
    print("------------------------------")
    print("Number of open-ended questions: \n {}".format(list(question_type_dict.values()).count(3))) # count number of open-ended questions
    print("------------------------------")
    print("Student count is {}".format(student_count))
    print("------------------------------")
    print("Number of answer choices possible is {}".format(num_choices))
    print("------------------------------")
    numerical_metrics()
    sentiment_analysis()
    write_csv()

question_type_dict = {}
analytics_dict = {} # processed results in the form of a dictionary for output to csv
numeric_question_dict = {} # containing responses for each numeric question
textual_question_dict = {} # containing question ID used as dummy holder for each textual question
student_dict = {} # dictionary containing lists of student responses

num_questions = 20
num_choices = 5
student_count = 0
output_path = ""

questions_file = sys.argv[1]
answers_file = sys.argv[2]
if len(sys.argv) > 3:
    output_path = sys.argv[3]

#hard coded values indicating if this is text or numeric

if __name__ == '__main__':
    main()
    plt.show() # show all the figures


import keyword
import datetime

print('Hello World')

def is_adult(age):
    if age >= 16:
        return True
    else:
        return False
result1 = is_adult(45)

def is_adult(age):
    return age >= 16
result1 = is_adult(15)
print(result1)

print(type(()))
print(type([]))

print(keyword.kwlist)

number1 = 10
if number1  > 0:
    print('positive')
else:
    print(') or positive')

message ='positive' if number1 > 0 else '0 or negative'
print(message)

# list is arrays []
number2 = [2,1,3,5,4,6,8,7,0,9]
#number2.sort()
#number2.reverse()
#number2.remove(item)
# del numbers [index
#numbers2.pop()
#number2.append(1000)
value1 = number2.__len__()
value2 = number2[2]
print(value2)

# sets {}
# dupilcates not allowed
numberList = [1,1]
numberSet = {1,1}
print(numberList)
print(numberSet)

# Dictionaries - Key, Value pair data structure

person = {
    'name': 'Jamal',
    'age': 20,
    'address': 'USA'
}
print(person['address'])

# BREAK AND CONTINUE

"""
number3 =0
while number3 < 10:
    if number3 ==5:
        break
#        print('number is 5 hooray')
    number3 += 1
    print(number3)
"""
number3 =0
while number3 < 10:
    number3 += 1
    if number3 < 5:
        continue
    print(number3)

# CLASSES AND OBJECTS

class Phone:
    def __init__(self, brand, price):
        self.brand = brand
        self.price = price

    def call(self, phoneNumber):
        print(f'{self.brand} is calling {phoneNumber}')

iphone = Phone("Iphone 7", 300)
samsung = Phone("S20", 1400)

print(iphone.brand)
print(iphone.price)
iphone.call('999')

# DATE TIME
print(datetime.datetime.now())
print(datetime.datetime.now().time())

# WORKING WITH FILES

'''
file = open('./data.csv', 'w')
file.write('id,name,email\n')
file.write('1,Jamilah,test1\n')
file.write('2,Wendy,test2\n')
file.write('3,Louise,test3\n')
file.close()
'''

file = open('./data.csv', 'r')
# print(file.read())
#for line in file:
#    print(line)
print(file.readlines())
file.close()