from __future__ import annotations

from collections.abc import Collection
from typing import Any, Literal

import pandas as pd
import plotly.graph_objects as go
from pandas.core.common import flatten

from retentioneering.backend.tracker import (
    collect_data_performance,
    time_performance,
    track,
)
from retentioneering.eventstream.types import EventstreamType

FunnelTypes = Literal["open", "closed", "hybrid"]

class Funnel:
    """
    A class for the calculation and visualization of a conversion funnel.

    Parameters
    ----------
    eventstream : EventstreamType

    See Also
    --------
    .Eventstream.funnel : Call Funnel tool as an eventstream method.

    Notes
    -----
    See :doc:`Funnel user guide</user_guides/funnel>` for the details.

    """

    __default_layout = dict(
        margin={"l": 180, "r": 0, "t": 30, "b": 0, "pad": 0},
        funnelmode="stack",
        showlegend=True,
        hovermode="closest",
        legend=dict(orientation="v", bgcolor="#E2E2E2", xanchor="left", font=dict(size=12)),
    )
    __eventstream: EventstreamType
    stages: list[str]
    stage_names: list[str] | None
    funnel_type: FunnelTypes
    segments: Collection[Collection[int]] | None
    segment_names: list[str] | None
    __res_dict: dict[str, dict]

    @time_performance(
        scope="funnel",
        event_name="init",
    )
    def __init__(self, eventstream: EventstreamType) -> None:
        self.__eventstream = eventstream
        self.user_col = self.__eventstream.schema.user_id
        self.event_col = self.__eventstream.schema.event_name
        self.time_col = self.__eventstream.schema.event_timestamp
        self.__res_dict = {}

    def __validate_input(
        self,
        stages: list[str],
        stage_names: list[str] | None = None,
        funnel_type: FunnelTypes = "closed",
        segments: Collection[Collection[int]] | None = None,
        segment_names: list[str] | None = None,
    ) -> tuple[pd.DataFrame, list[str], list[str], FunnelTypes, Collection[Collection[int]], list[str]]:
        data = self.__eventstream.to_dataframe(copy=True)
        data = data[data[self.event_col].isin([i for i in flatten(stages)])]  # type: ignore

        if stages and stage_names and len(stages) != len(stage_names):
            raise ValueError("stages and stage_names must be the same length!")

        if segments is None:
            segments = [data[self.user_col].unique().tolist()]
            segment_names = ["all users"]
        else:
            sets = [set(segment) for segment in segments]
            if len(set.intersection(*sets)) > 0:
                raise ValueError("Check intersections of users in segments!")

        if segment_names is None:
            segment_names = [f"group {i}" for i in range(len(segments))]  # type: ignore

        if segments and segment_names and len(segments) != len(segment_names):  # type: ignore
            raise ValueError("segments and segment_names must be the same length!")

        # IDK why but pyright thinks this is Funnel!!!

        if funnel_type not in ["open", "closed", "hybrid"]:
            raise ValueError("funnel_type should be 'open', 'closed' or 'hybrid'!")

        for idx, stage in enumerate(stages):
            if type(stage) is not list:
                stages[idx] = [stage]  # type: ignore

        if stage_names is None:
            stage_names = []
            for t in stages:
                # get name
                stage_names.append(" | ".join(t).strip(" | "))

        return data, stages, stage_names, funnel_type, segments, segment_names

    def _plot_stacked_funnel(self, data: list[go.Funnel]) -> go.Figure:
        layout = go.Layout(**self.__default_layout)
        fig = go.Figure(data, layout)
        return fig

    @staticmethod
    def _calculate_plot_data(plot_params: dict[str, Any]) -> list[go.Funnel]:
        data = []
        for t in plot_params.keys():
            trace = go.Funnel(
                name=t,
                y=plot_params[t]["stages"],
                x=plot_params[t]["values"],
                textinfo="value+percent initial+percent previous",
            )
            data.append(trace)

        return data

    def _prepare_data_for_closed_and_hybrid_funnel(
        self,
        data: pd.DataFrame,
        stages: list[str],
        stage_names: list[str],
        segments: Collection[Collection[int]],
        segment_names: list[str],
    ) -> dict[str, dict]:
        min_time_0stage = (
            data[data[self.event_col].isin(stages[0])].groupby(self.user_col)[[self.time_col]].min().reset_index()
        )
        data = data.merge(min_time_0stage, "left", on=self.user_col, suffixes=("", "_min"))
        data.rename(columns={data.columns[-1]: "min_date"}, inplace=True)

        # filtered NA and only events that occurred after the user entered the first funnel event remain
        data = data[(~data["min_date"].isna()) & (data["min_date"] <= data[self.time_col])]
        data.drop(columns="min_date", inplace=True)

        __res_dict = {}
        for segment, name in zip(segments, segment_names):
            vals, _df = self._crop_df(data, stages, segment)
            __res_dict[name] = {"stages": stage_names, "values": vals}
        return __res_dict

    def _prepare_data_for_open_funnel(
        self,
        data: pd.DataFrame,
        stages: list[str],
        stage_names: list[str],
        segments: Collection[Collection[int]],
        segment_names: list[str],
    ) -> dict[str, dict]:
        __res_dict = {}
        for segment, name in zip(segments, segment_names):
            # isolate users from group
            group_data = data[data[self.user_col].isin(segment)]
            vals = [group_data[group_data[self.event_col].isin(stage)][self.user_col].nunique() for stage in stages]
            __res_dict[name] = {"stages": stage_names, "values": vals}
        return __res_dict

    def _crop_df(self, df: pd.DataFrame, stages: list[str], segment: Collection[int]) -> tuple[list[int], pd.DataFrame]:
        first_stage = stages[0]
        next_stages = stages[1:]

        first_stage_users = set(
            (df[(df[self.event_col].isin(first_stage)) & (df[self.user_col].isin(segment))][self.user_col])
        )
        df = df.drop(
            df[(~df[self.user_col].isin(first_stage_users)) | (df[self.event_col].isin(first_stage))].index.tolist()
        )

        prev_users_stage = first_stage_users
        vals = [len(first_stage_users)]
        for stage in next_stages:
            user_stage = set(
                df[(df[self.event_col].isin(stage)) & (df[self.user_col].isin(first_stage_users))][self.user_col]
            )
            user_stage = user_stage - (user_stage - prev_users_stage)
            prev_users_stage = user_stage

            vals.append(len(user_stage))

            if self.funnel_type == "closed":
                stage_min_df = (
                    df[df[self.event_col].isin(stage)].groupby(self.user_col)[[self.time_col]].min().reset_index()
                )
                df = df.merge(stage_min_df, "left", on=self.user_col, suffixes=("", "_min"))
                df.rename(columns={df.columns[-1]: "min_date"}, inplace=True)

                df.drop(
                    df[
                        (df["min_date"].isna())
                        | (df["min_date"] >= df[self.time_col])
                        | (~df[self.user_col].isin(user_stage))
                    ].index.tolist(),
                    inplace=True,
                )
                df.drop(columns="min_date", inplace=True)
            else:
                df = df.drop(df[~df[self.user_col].isin(user_stage)].index.tolist())

        return vals, df

    @time_performance(
        scope="funnel",
        event_name="fit",
    )
    def fit(
        self,
        stages: list[str],
        stage_names: list[str] | None = None,
        funnel_type: FunnelTypes = "closed",
        segments: Collection[Collection[int]] | None = None,
        segment_names: list[str] | None = None,
    ) -> None:
        """
        Calculate the funnel internal values with the defined parameters.
        Applying ``fit`` method is necessary for the following usage
        of any visualization or descriptive ``Funnel`` methods.

        Parameters
        ----------
        stages : list of str
            List of events used as stages for the funnel. Absolute and relative
            number of users who reached specified events at least once will be
            plotted. Multiple events can be grouped together as an individual state
            by combining them as a sub list.
        stage_names : list of str, optional
            List of stage names, this is necessary for stages that include several events.
        funnel_type : 'open', 'closed' or 'hybrid', default 'closed'

            - if ``open`` - all users will be counted on each stage;
            - if ``closed`` - each stage will include only users, that were present on all previous stages;
            - if ``hybrid`` - combination of 2 previous types. The first stage is required
              to go further. And for the second and subsequent stages it is important to have
              all previous stages in their path, but the order of these events is not taken
              into account.

        segments : Collection[Collection[int]], optional
            List of user_ids collections. Funnel for each user_id collection will be plotted.
            If ``None`` - all users from the dataset will be plotted. A user can only belong to one segment at a time.
        segment_names : list of str, optional
            Names of segments. Should be a list from unique values of the ``segment_col``.
            If ``None`` and ``segment_col`` is given - all values from ``segment_col`` will be used.
        """
        called_params = {
            "stages": stages,
            "stage_names": stage_names,
            "funnel_type": funnel_type,
            "segments": segments,
            "segment_names": segment_names,
        }
        not_hash_values = ["funnel_type"]

        (
            data,
            self.stages,
            self.stage_names,
            self.funnel_type,
            self.segments,
            self.segment_names,
        ) = self.__validate_input(stages, stage_names, funnel_type, segments, segment_names)

        if self.funnel_type in ["closed", "hybrid"]:
            self.__res_dict = self._prepare_data_for_closed_and_hybrid_funnel(
                data=data,
                stages=self.stages,
                stage_names=self.stage_names,
                segments=self.segments,
                segment_names=self.segment_names,
            )

        elif self.funnel_type == "open":
            self.__res_dict = self._prepare_data_for_open_funnel(
                data=data,
                stages=self.stages,
                segments=self.segments,
                segment_names=self.segment_names,
                stage_names=self.stage_names,
            )
        collect_data_performance(
            scope="funnel",
            event_name="metadata",
            called_params=called_params,
            not_hash_values=not_hash_values,
            performance_data={},
            eventstream_index=self.__eventstream._eventstream_index,
        )

    @time_performance(
        scope="funnel",
        event_name="plot",
    )
    def plot(self) -> go.Figure:
        """
        Create a funnel plot based on the calculated funnel values.
        Should be used after :py:func:`fit`.

        Returns
        -------
        go.Figure

        """
        result_dict = self.__res_dict
        data = self._calculate_plot_data(plot_params=result_dict)
        figure = self._plot_stacked_funnel(data=data)
        return figure

    @property
    @time_performance(
        scope="funnel",
        event_name="values",
    )
    def values(self) -> pd.DataFrame:
        """
        Returns a pd.DataFrame representing the calculated funnel values.
        Should be used after :py:func:`fit`.

        Returns
        -------
        pd.DataFrame

            +------------------+-------------+-----------------+-------------------+--------------------+
            | **segment_name** |  **stages** | **unique_users**|  **%_of_initial** |  **%_of_previous** |
            +------------------+-------------+-----------------+-------------------+--------------------+
            | segment_1        |  stage_1    |            2000 |            100.00 |          100.00    |
            +------------------+-------------+-----------------+-------------------+--------------------+

        """

        result_dict = self.__res_dict
        result_list = []
        for key in result_dict:
            result_ = pd.DataFrame(result_dict[key])
            result_.columns = ["stages", "unique_users"]  # type: ignore
            result_["segment_name"] = key
            result_ = result_[["segment_name", "stages", "unique_users"]]
            result_["shift"] = result_["unique_users"].shift(periods=1, fill_value=result_["unique_users"][0])
            result_["%_of_previous"] = (result_["unique_users"] / result_["shift"] * 100).round(2)
            result_["%_of_initial"] = (result_["unique_users"] / result_["unique_users"][0] * 100).round(2)
            result_.drop(columns="shift", inplace=True)
            result_list.append(result_)

        result_df = pd.concat(result_list).set_index(["segment_name", "stages"])

        return result_df

    @property
    @time_performance(
        scope="funnel",
        event_name="params",
    )
    def params(self) -> dict:
        """
        Returns the parameters used for the last fitting.

        """

        return {
            "stages": self.stages,
            "stage_names": self.stage_names,
            "funnel_type": self.funnel_type,
            "segments": self.segments,
            "segment_names": self.segment_names,
        }


# utilities
import re
import numpy as np
import pandas as pd
import nltk
# plotting
import seaborn as sns
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# nltk
from nltk.stem import WordNetLemmatizer
# sklearn
from sklearn.svm import LinearSVC
from sklearn.naive_bayes import BernoulliNB
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import confusion_matrix, classification_report
from nltk.tokenize import RegexpTokenizer
from sklearn import preprocessing

#Sentiment finder
from textblob import TextBlob
# Importing the dataset
##DATASET_COLUMNS=['ids','date','user','text']
#DATASET_ENCODING = "ISO-8859-1"
df = pd.read_csv('cleaned_superbowl_halftime_tweets.csv')

df.tweet = df.tweet.astype(str)
df.drop(columns=['Unnamed: 0.1', 'Unnamed: 0'], inplace=True)

df['polarity'] = df.apply(lambda x: TextBlob(x['tweet']).sentiment.polarity, axis=1)
df['tweet']=df['tweet'].str.lower()

tokenizer = RegexpTokenizer(r'\w+')
df['tweet'] = df['tweet'].apply(tokenizer.tokenize)
#print(df['tweet'].head())

st = nltk.PorterStemmer()
def stemming_on_text(data):
    text = [st.stem(word) for word in data]
    return data

df['tweet']= df['tweet'].apply(lambda x: stemming_on_text(x))
print(df['tweet'].head())

lm = nltk.WordNetLemmatizer()
def lemmatizer_on_text(data):
    text = [lm.lemmatize(word) for word in data]
    return data

df['tweet'] = df['tweet'].apply(lambda x: lemmatizer_on_text(x))
print(df['tweet'].head())

tweet_list = df['tweet']
df["tweet"] = df["tweet"].astype(str)

X=df.tweet
y=df.polarity

X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.05, random_state =26105111)
print(X_train.dtype)
vectoriser = TfidfVectorizer(ngram_range=(1,2), max_features=500000)

vectoriser.fit(X_train)
print('No. of feature_words: ', len(vectoriser.get_feature_names()))

X_train = vectoriser.transform(X_train)
X_test  = vectoriser.transform(X_test)

def model_Evaluate(model):
    # Predict values for Test dataset
    y_pred = model.predict(X_test)
    # Print the evaluation metrics for the dataset.
    print(classification_report(y_test, y_pred))
    # Compute and plot the Confusion matrix
    cf_matrix = confusion_matrix(y_test, y_pred)
    categories = ['Negative','Positive']
    group_names = ['True Neg','False Pos', 'False Neg','True Pos']
    group_percentages = ['{0:.2%}'.format(value) for value in cf_matrix.flatten() / np.sum(cf_matrix)]
    labels = [f'{v1}n{v2}' for v1, v2 in zip(group_names,group_percentages)]
    labels = np.asarray(labels).reshape(2,2)
    sns.heatmap(cf_matrix, annot = labels, cmap = 'Blues',fmt = '',
    xticklabels = categories, yticklabels = categories)
    plt.xlabel("Predicted values", fontdict = {'size':14}, labelpad = 10)
    plt.ylabel("Actual values" , fontdict = {'size':14}, labelpad = 10)
    plt.title ("Confusion Matrix", fontdict = {'size':18}, pad = 20)

print("X TRAIN DTYPE",X_train.dtype)
print("Y TRAIN DTYPE",y_train.dtype)
lab_enc = preprocessing.LabelEncoder()
training_scores_encoded = lab_enc.fit_transform(y_train)
LRmodel = LogisticRegression(C = 2, max_iter = 1000, n_jobs=-1)
LRmodel.fit(X_train, training_scores_encoded)
model_Evaluate(LRmodel)
y_pred3 = LRmodel.predict(X_test)


from typing import Dict

from django import template
from lis.models import NormalRange,LaboratoryTest,LaboratoryTestResultType,NormalRange,LaboratoryTestType
from lis.forms import ResultEntryForm,EditSpecimenForm,EditLabTestTypeForm,EditLabTestResultTypeForm
from inpatient_app.models import Bed,WardDischargeSummary,IPDTreatmentPlan,PerformPlan
from core.models import Patient,Resource,PatientResource,PatientConsultation,PatientDemoValues,MedicalCertificate
from billing_app.forms import EditItemForm,EditItemPriceForm
from billing_app.models import Item
from pharmacy_app.forms import EditPrescriptionInfoForm,EditPrescriptionInfoForm2
from core.forms import EditPatientTreatmentForm,EditPatientResourceForm,EditMedicalCertificateForm
from datetime import timedelta
from datetime import datetime
from django import forms

register = template.Library()

@register.simple_tag(takes_context=True)
def get_dict_elt(context, dict:Dict, key:str):
    # a template tag that will return str representation of normal range
    try:
        nrange:NormalRange = dict.get(key, None)
        if nrange:
            return f' {nrange.min_value} - {nrange.max_value}  {nrange.m_unit}'
        return 'not available'
    except:
        return ''

@register.simple_tag(takes_context=True)
def get_something(context,thirty_days_ago, bed_id):
    print('day isssss',thirty_days_ago,bed_id)
    bed = Bed.objects.get(id=bed_id)
    result = bed.is_admitted(thirty_days_ago,bed.id)
    context.update({'result':result})
    return ""

@register.simple_tag(takes_context=True)
def is_admitted(context, patient_id):
    patient = Patient.objects.get(id=patient_id)
    result = Bed.objects.filter().first().is_inpatient(patient.id)
    context.update({'result':result})
    return ""

@register.simple_tag(takes_context=True)
def return_info_form(context, plan_id):
    plan = IPDTreatmentPlan.objects.get(id=plan_id)
    print('plan id',plan.id)
    edit_info_form = EditPrescriptionInfoForm(planid=plan_id)
    context.update({'info_form5':edit_info_form})
    return ""

@register.simple_tag(takes_context=True)
def return_treatment_form(context, plan_id):
    plan = IPDTreatmentPlan.objects.get(id=plan_id)
    print('plan id',plan.id)
    edit_treatment_form = EditPatientTreatmentForm(planid=plan_id)

    context.update({'edit_treatment_form':edit_treatment_form})
    return ""

@register.simple_tag(takes_context=True)
def can_be_discharged(context, patient_id):
    patient = Patient.objects.get(id=patient_id)
    print('patient:', patient,'\n')
    result = WardDischargeSummary.objects.filter().first().has_summary(patient_id)
    print('result:', result,'\n')

    context.update({'can_be_discharged':result})
    return ""

@register.simple_tag(takes_context=True)
def is_scheduled(context,day, resource_id):
    #print('day isssss',day,resource_id)
    resource = Resource.objects.get(id=resource_id)
    result = resource.is_scheduled(day,resource_id)
    schedule = PatientResource.objects.filter(resource=resource).first()
    #resource.scheduled_resource.filter(start_time__lte=day, end_time__gte=day).exists()
    context.update({'result':result})
    context.update({'schedule':schedule})
    return ""

@register.simple_tag(takes_context=True)
def return_edit_resource_form(context, resource_id):
    resource = PatientResource.objects.get(id=resource_id)
    print('resource id',resource_id)
    edit_resource_form = EditPatientResourceForm(resource_id=resource.id)
    context.update({'edit_resource_form':edit_resource_form})
    return ""

@register.simple_tag(takes_context=True)
def return_consultation(context, consultation_id):
    consultation = PatientConsultation.objects.get(id=consultation_id)
    print('consultation',consultation,'\n')
    context.update({'consultation_object':consultation})
    return ""

@register.simple_tag(takes_context=True)
def return_demo_value(context, value_id):
    demo_value = PatientDemoValues.objects.get(id=value_id)
    context.update({'demo_value_object':demo_value})
    return ""

@register.simple_tag(takes_context=True)
def is_performed(context,day, plan_id):
    plan = IPDTreatmentPlan.objects.get(id=plan_id)

    try:
        performed_plan = PerformPlan.objects.filter().first()
    except Exception as e:
        return ""
    performed = performed_plan.is_performed(day,plan_id)
    #resource.scheduled_resource.filter(start_time__lte=day, end_time__gte=day).exists()
    print('\n','performed: ',performed,'\n')
    context.update({'performed':performed})
    if performed==True:
        after_date = day + timedelta(hours=12)
        before_date = day - timedelta(hours=12)
        performance = PerformPlan.objects.filter(registered_on__range=[before_date,after_date], plan=plan).last()
        context.update({'performance':performance})
    return ""

@register.simple_tag(takes_context=True)
def return_edit_certificate_form(context, certificate_id):
    certificate = MedicalCertificate.objects.get(id=certificate_id)
    edit_certificate_form = EditMedicalCertificateForm(certificateid=certificate.id)
    context.update({'edit_certificate_form':edit_certificate_form})
    return ""

@register.simple_tag(takes_context=True)
def return_edit_prescription_form(context, info_id):
    edit_prescription_form = EditPrescriptionInfoForm2(info_id=info_id)
    context.update({'edit_prescription_form':edit_prescription_form})
    return ""

@register.simple_tag(takes_context=True)
def is_in_date_array(context,day,date_array):
    #print('dateeee: ',date_array, 'day',str(day.month))
    #print('dayDDD: ', date_array)
    day1 = str(day.year) + "-" + str(day.month) + "-" + str(day.day)
    #print(day1)
    if day1 in date_array:
        #print('already')
        is_in_array = True
        context.update({'is_in_array':is_in_array})

    else:
        date_array.append(day1)
        #for d in date_array:
            #print(d,'kdlsdksld')
        is_in_array = False
        context.update({'is_in_array':is_in_array})

    return ""

@register.simple_tag(takes_context=True)
def get_result_form(context,test_id):
    result_entry_form = ResultEntryForm()

    test = LaboratoryTest.objects.get(id=test_id)
    form_fields = {}
    normal_range = {}
    for result_type in LaboratoryTestResultType.objects.filter(test_type=test.test_type,active=True):
        if result_type.input_type == 'NUMBER':
            form_field = forms.FloatField(widget=forms.NumberInput(
                attrs={
                    'min': 1,
                    'class': 'form-control',
                }
            ))
            normal_range = NormalRange.get_range(result_type, 33, 'M') #TO-DO pass patient age and sex
            #normal_range.update({
            #    f'id_{result_type.name}' : normal_range,
            #    })
            form_fields.update({result_type.name : form_field})
        elif result_type.input_type == "CHOICE":
            choice_set = [(x.choice, x.choice) for x in result_type.choice_set.all()]
            form_fields.update({result_type.name : forms.TypedChoiceField(choices=choice_set, widget=forms.Select(
                attrs={
                    'class': 'form-control',
                }
            ))})
        elif result_type.input_type == 'BOOL':
            form_fields.update({result_type.name : forms.BooleanField(required=False, widget=forms.CheckboxInput(
                attrs={
                    'id': 'checkboxbg1'
                }
            ))})
        elif result_type.input_type == 'TEXT':
            form_fields.update({result_type.name : forms.CharField(widget=forms.Input(
                attrs={
                    'class': 'form-control',
                }
            ))})
    result_entry_form.fields.update(form_fields)
    print('test:', test,'\n')
    print('result form:', result_entry_form,'\n')

    context.update({'result_entry_form':result_entry_form})
    return ""

@register.simple_tag(takes_context=True)
def return_edit_specimen_form(context, specimen_id):
    edit_specimen_form = EditSpecimenForm(specimen_id=specimen_id)
    context.update({'edit_specimen_form':edit_specimen_form})
    return ""

@register.simple_tag(takes_context=True)
def return_edit_test_type_form(context, test_type_id):
    edit_test_type_form = EditLabTestTypeForm(test_type_id=test_type_id)
    context.update({'edit_test_type_form':edit_test_type_form})
    return ""

@register.simple_tag(takes_context=True)
def return_edit_result_type_form(context, result_type_id):
    edit_result_type_form = EditLabTestResultTypeForm(result_type_id=result_type_id)
    context.update({'edit_result_type_form':edit_result_type_form})
    return ""

@register.simple_tag(takes_context=True)
def return_edit_item_form(context, item_id):
    edit_item_form = EditItemForm(item_id=item_id)
    item = Item.objects.get(id=item_id)
    edit_price_form = EditItemPriceForm(price_id=item.price_info.id)

    context.update({'edit_item_form':edit_item_form},

        )
    context.update(
                    {'edit_price_form':edit_price_form}
        )

    return ""

@register.simple_tag(takes_context=True)
def return_lab_item(context, test_type_id):
    test_type = LaboratoryTestType.objects.get(id=test_type_id)
    lab_item = Item.objects.filter(lab_test=test_type).last()
    context.update({'lab_item':lab_item},
        )

    return ""


from datetime import datetime

from flask import Flask, request
from flask_sqlalchemy import SQLAlchemy

import raw_data
import json

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] ='sqlite:///:memory:'

db = SQLAlchemy(app)

def get_response(data):
    return json.dumps(data), 200, {'Content-Type': 'application/json; charset=utf-8'}

class User(db.Model):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(100))
    last_name = db.Column(db.String(100))
    age = db.Column(db.Integer)
    email = db.Column(db.String(100))
    role = db.Column(db.String(100))
    phone = db.Column(db.String(100))

    def to_dict(self):
        return {col.name: getattr(self, col.name) for col in self.__table__.columns}

class Order(db.Model):
    __tablename__ = 'order'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    description = db.Column(db.String())
    start_date = db.Column(db.Date)
    end_date = db.Column(db.Date)
    address = db.Column(db.String(100))
    price = db.Column(db.Integer)
    customer_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    executor_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    def to_dict(self):
        return {col.name: getattr(self, col.name) for col in self.__table__.columns}

class Offer(db.Model):
    __tablename__ = 'offer'

    id = db.Column(db.Integer, primary_key=True)
    order_id = db.Column(db.Integer, db.ForeignKey('order.id'))
    executor_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    def to_dict(self):
        return {col.name: getattr(self, col.name) for col in self.__table__.columns}

with app.app_context():
    db.create_all()

    for user_data in raw_data.users:
        new_user = User(**user_data)
        db.session.add(new_user)
        db.session.commit()

    for order_data in raw_data.orders:
        order_data['start_date'] = datetime.strptime(order_data['start_date'], '%m/%d/%Y').date()
        order_data['end_date'] = datetime.strptime(order_data['end_date'], '%m/%d/%Y').date()
        new_order = Order(**order_data)
        db.session.add(new_order)
        db.session.commit()

    for offer_data in raw_data.offers:
        new_offer = Offer(**offer_data)
        db.session.add(new_offer)
        db.session.commit()

@app.route('/users', methods=['GET', 'POST'])
def users_page():
    if request.method == 'GET':
        users = User.query.all()
        res = [user.to_dict() for user in users]
        return get_response(res)
    elif request.method == 'POST':
        usr_data = json.loads(request.data)
        db.session.add(User(**usr_data))
        db.session.commit()
        return '', 201

@app.route('/users/<int:uid>', methods=['GET', 'PUT', 'DELETE'])
def user_page(uid):
    if request.method == 'GET':
        user = User.query.get(uid).to_dict()
        return get_response(user)
    if request.method == 'DELETE':
        user = User.query.get(uid)
        db.session.delete(user)
        db.session.commit()
        return '', 204
    if request.method == 'PUT':
        usr_data = json.loads(request.data)
        user = User.query.get(uid)
        user.first_name = usr_data['first_name']
        user.last_name = usr_data['last_name']
        user.role = usr_data['role']
        user.phone = usr_data['phone']
        user.email = usr_data['email']
        user.age = usr_data['age']

        db.session.add(user)
        db.session.commit()
        return '', 204

@app.route('/orders', methods=['GET', 'POST'])
def orders_page():
    if request.method == 'GET':
        orders = Order.query.all()
        res = []
        for order in orders:
            ord_dict = order.to_dict()
            ord_dict['start_date'] = str(ord_dict['start_date'])
            ord_dict['end_date'] = str(ord_dict['end_date'])
            res.append(ord_dict)
        return get_response(res)
    elif request.method == 'POST':
        ord_data = json.loads(request.data)
        db.session.add(Order(**ord_data))
        db.session.commit()
        return '', 201

@app.route('/orders/<int:oid>', methods=['GET', 'PUT', 'DELETE'])
def order_page(oid):
    order = Order.query.get(oid)
    if request.method == 'GET':
        ord_dict = order.to_dict()
        ord_dict['start_date'] = str(ord_dict['start_date'])
        ord_dict['end_date'] = str(ord_dict['end_date'])
        return get_response(ord_dict)
    if request.method == 'DELETE':
        db.session.delete(order)
        db.session.commit()
        return '', 204
    if request.method == 'PUT':
        ord_data = json.loads(request.data)
        ord_data['start_date'] = datetime.strptime(ord_data['start_date'], '%Y-%m-%d').date()
        ord_data['end_date'] = datetime.strptime(ord_data['start_date'], '%Y-%m-%d').date()

        order.name = ord_data['name']
        order.description = ord_data['description']
        order.start_date = ord_data['start_date']
        order.end_date = ord_data['end_date']
        order.price = ord_data['price']
        order.customer_id = ord_data['customer_id']
        order.executor_id = ord_data['executor_id']
        db.session.add(order)
        db.session.commit()
        return '', 204

@app.route('/offers', methods=['GET', 'POST'])
def offers_page():
    if request.method == 'GET':
        offers = Offer.query.all()
        res = [offer.to_dict() for offer in offers]
        return get_response(res)
    elif request.method == 'POST':
        ofr_data = json.loads(request.data)
        db.session.add(Offer(**ofr_data))
        db.session.commit()
        return '', 201

@app.route('/offers/<int:ofid>', methods=['GET', 'PUT', 'DELETE'])
def offer_page(ofid):
    if request.method == 'GET':
        offer = Offer.query.get(ofid).to_dict()
        return get_response(offer)
    if request.method == 'DELETE':
        offer = Offer.query.get(ofid)
        db.session.delete(offer)
        db.session.commit()
        return '', 204
    if request.method == 'PUT':
        ofr_data = json.loads(request.data)
        offer = Offer.query.get(ofid)
        offer.order_id = ofr_data['order_id']
        offer.executor_id = ofr_data['executor_id']
        db.session.add(offer)
        db.session.commit()
        return '', 204

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


import numpy as np
import cv2
import glob

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('animacion_reflexion_total.avi' ,fourcc ,15 ,(1200,1200))

lista = sorted(glob.glob('*'))

for imag in lista:
	img = cv2.imread(imag)
	# print(imag)
	# img = cv2.resize(img,(1000,1000)) #redimensionar
	out.write(img)

out.release()


import os

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

from pdas.data_utils import load_unified_helper, calc_mesh_bounds
from pdas.prom_utils import load_pod_basis

# mpl.rc("font", family="serif", size="10")
mpl.rc("font", family="sans", size="10")
mpl.rc("axes", labelsize="x-large")
mpl.rc("figure", facecolor="w")
mpl.rc("text", usetex=False)

def plot_contours(
    varplot,
    meshlist=None,
    datalist=None,
    meshdirs=None,
    datadirs=None,
    nvars=None,
    dataroot=None,
    merge_decomp=True,
    savefigs=False,
    outdir=None,
    plotlabels=None,
    nlevels=20,
    skiplevels=1,
    contourbounds=[None,None],
    draw_colorbar=True,
    plottime=10,
    plotskip=1,
    stopiter=-1,
    varlabel=None,
    plotbounds=False,
    bound_colors=None,
    figdim_base=[6.4, 4.8],
    vertical=True,
):

    # TODO: check dimension, slice if 3D

    # some input checking
    if stopiter != -1:
        assert stopiter >= 0
    if savefigs:
        assert outdir is not None
        if not os.path.isdir(outdir):
            os.mkdir(outdir)

    meshlist, datalist = load_unified_helper(
        meshlist,
        datalist,
        meshdirs,
        datadirs,
        nvars,
        dataroot,
        merge_decomp=merge_decomp,
    )
    ndata = len(datalist)

    if plotbounds:
        assert meshdirs is not None
        assert len(meshdirs) == ndata
        dom_bounds = calc_mesh_bounds(meshdirs)

    # prepping some lists
    if (plotlabels is None) or isinstance(plotlabels, str):
        plotlabels = [plotlabels] * ndata
    if not isinstance(plotskip, list):
        plotskip = [plotskip] * ndata
    assert len(plotskip) == ndata

    # set up time plot progression
    nt_min = np.infty
    for data_idx, data in enumerate(datalist):
        nt_min = min(nt_min, int(data.shape[-2] / plotskip[data_idx]))
    pause_time = plottime / nt_min

    # set up axes
    if vertical:
        fig, ax = plt.subplots(nrows=ndata, ncols=1)
        fig.set_figwidth(figdim_base[0])
        fig.set_figheight(figdim_base[1] * ndata)
    else:
        fig, ax = plt.subplots(nrows=1, ncols=ndata)
        fig.set_figwidth(figdim_base[0] * ndata)
        fig.set_figheight(figdim_base[1])
    if not isinstance(ax, np.ndarray):
         ax = np.array([ax])

    # set up contour parameters
    # assume first plot provides reasonable bounds, TODO: could improve
    if any([bound is None for bound in contourbounds]):
        contourbounds[0] = np.amin(datalist[0][:, :, :, varplot])
        contourbounds[1] = np.amax(datalist[0][:, :, :, varplot])
    levels = np.linspace(contourbounds[0], contourbounds[1], nlevels)
    ticks = levels[::skiplevels]

    itercounter = 0
    for t in range(0, nt_min):

        for plotnum in range(ndata):
            ax[plotnum].cla()

            # plot monolithic or merged decomposed solution
            if True:
                cf = ax[plotnum].contourf(
                    meshlist[plotnum][:, :, 0],
                    meshlist[plotnum][:, :, 1],
                    datalist[plotnum][:, :, t * plotskip[plotnum], varplot],
                    levels=levels,
                    extend="both",
                )

            # plot non-combined decomposed solution
            else:
                raise ValueError("Decomposed solution plotting not implemented")

            # plot domain boundaries
            if plotbounds:
                if dom_bounds[plotnum] is not None:
                    for dom_idx, bounds in enumerate(dom_bounds[plotnum]):
                        ax[plotnum].plot(
                            [bounds[0][0], bounds[0][1], bounds[0][1], bounds[0][0], bounds[0][0]],
                            [bounds[1][0], bounds[1][0], bounds[1][1], bounds[1][1], bounds[1][0]],
                            color=bound_colors[dom_idx],
                            linestyle="--",
                        )

            ax[plotnum].set_title(plotlabels[plotnum], fontsize=18)
            ax[plotnum].tick_params(axis='both', which='major', labelsize=14)

        fig.supxlabel('x', fontsize=16)
        fig.supylabel('y', fontsize=16)

        if (t == 0) and draw_colorbar:
            plt.tight_layout()
            if vertical:
                fig.subplots_adjust(right=0.8)
                cbar_ax = fig.add_axes([0.85, 0.1, 0.025, 0.8])
                cbar = fig.colorbar(cf, cax=cbar_ax, orientation="vertical")
            else:
                # fig.subplots_adjust(bottom=0.2)
                # cbar_ax = fig.add_axes([0.1, 0.1, 0.8, 0.025])
                fig.subplots_adjust(top=0.8)
                cbar_ax = fig.add_axes([0.1, 0.9, 0.8, 0.025])
                cbar = fig.colorbar(cf, cax=cbar_ax, orientation="horizontal")
                cbar.ax.xaxis.set_label_position('top')
            cbar.set_ticks(ticks)
            if varlabel is not None:
                cbar.set_label(varlabel)

        plt.pause(pause_time)
        if (stopiter != -1) and (itercounter >= stopiter):
            breakpoint()

        if savefigs:
            plt.savefig(os.path.join(outdir, f'fig_{t}.png'))

        itercounter += 1

    plt.show()
    print("Finished")

def plot_lines(
    ylist,
    outdir,
    outname,
    linecolors,
    xlist=None,
    varplot=None,
    linestyles="-",
    xlabel=None,
    ylabel=None,
    legendlabels=None,
    legendloc="best",
    xbounds=[None,None],
    ybounds=[None,None],
    figdim=[6.4, 4.8],
):
    """
    Inputs:
        - xlist: a (list of) 1D array(s)
        - ylist: a (list of) 1D/2D arrays. If 2D, must provide varplot, and
            assumed that trailing dimension is variable dimension
    """

    if not os.path.isdir(outdir):
        os.mkdir(outdir)

    if not isinstance(ylist, list):
        ylist = [ylist]
    nlines = len(ylist)

    if legendlabels is not None:
        assert len(legendlabels) == nlines

    # must have unique colors for plots
    if not isinstance(linecolors, list):
        linecolors = [linecolors]
    assert len(linecolors) >= nlines

    # non-unique linestyles fine
    if not isinstance(linestyles, list):
        linestyles = [linestyles] * nlines
    assert len(linestyles) >= nlines

    # get 1D arrays
    for yidx, yarr in enumerate(ylist):
        assert yarr.ndim <= 2

        if yarr.ndim == 2:
            assert varplot is not None
            ylist[yidx] = yarr[:, varplot]

    # check dimensions
    nvals_list = [y.shape[0] for y in ylist]
    allsame = all([val == nvals_list[0] for val in nvals_list])

    # handling certain edge cases for xlist
    # WARNING: may be incorrect if same number of samples but over different range
    #   Better to just be explicit
    if xlist is None:
        assert allsame
        xlist = [np.arange(ylist[0].shape[0])] * nlines
    else:
        if not isinstance(xlist, list):
            assert allsame
            xlist = [xlist] * nlines
    assert len(xlist) == nlines

    # plot
    fig, ax = plt.subplots(1, 1, figsize=figdim)
    for line_idx in range(nlines):

        ax.plot(
            xlist[line_idx], ylist[line_idx],
            color=linecolors[line_idx], linestyle=linestyles[line_idx]
        )

    if legendlabels is not None:
        ax.legend(legendlabels, loc=legendloc)
    ax.set_xlim(xbounds)
    ax.set_ylim(ybounds)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)

    plt.tight_layout()
    outfile = os.path.join(outdir, outname + ".png")
    print(f"Saving image to {outfile}")
    plt.savefig(outfile)

def plot_pod_res_energy(
    outdir,
    basis_dirs=None,
    svals=None,
    xlim=[None, None],
    plotcolors=["k", "r", "b"],
    linestyles=["-", "--", ":"],
    legend_labels=None,
    outsuff="",
):

    assert os.path.isdir(outdir)
    assert (basis_dirs is not None) != (svals is not None)

    if svals is None:
        if not isinstance(basis_dirs, list):
            basis_dirs = [basis_dirs]
        svals = []
        for basis_dir in basis_dirs:
            svals_in, = load_pod_basis(basis_dir, return_basis=False, return_svals=True)
            if isinstance(svals_in, list):
                svals += svals_in
            else:
                svals.append(svals_in.copy())
    else:
        if not isinstance(svals, list):
            assert isinstance(svals, np.ndarray)
            svals = [svals]

    nlines = len(svals)
    assert len(plotcolors) >= nlines
    assert len(linestyles) >= nlines
    if legend_labels is not None:
        assert len(legend_labels) == nlines

    fig, ax = plt.subplots(1, 1)
    for line_idx, sval_arr in enumerate(svals):

        # compute residual energy
        svals_sq = np.square(sval_arr)
        energy = 100* (1.0 - np.cumsum(svals_sq) / np.sum(svals_sq))

        nmodes = np.arange(1, energy.shape[0]+1)

        ax.semilogy(nmodes, energy, color=plotcolors[line_idx], linestyle=linestyles[line_idx])

    ax.set_xlim(xlim)
    ax.set_ylim([1e-2, 100])
    ax.set_xlabel("# POD modes")
    ax.tick_params(axis="both", which="major", labelsize=12)
    ax.set_ylabel("Residual POD energy, %")
    if legend_labels is not None:
        ax.legend(legend_labels, loc="upper right", fontsize=12)
    ax.set_yticks(
        [1e-2, 1e-1, 1, 10, 100],
        ["0.01", "0.1", "1", "10", "100"],
    )

    plt.tight_layout()
    outfile = os.path.join(outdir, f"S{outsuff}.png")
    print(f"Saving image to {outfile}")
    plt.savefig(outfile)


from django.shortcuts import render
import requests

# Create your views here.
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from nameko.standalone.rpc import ClusterRpcProxy

from .models import Character

from django.http import JsonResponse
from django.forms.models import model_to_dict

import json

CONFIG = {'AMQP_URI': "amqp://guest:guest@rabbitmq"}

class ListCharacters(APIView):
    """Returns all the Rick and Morty Character names and their IDs"""

    def get(self, request):
        characterList = []
        for character in Character.objects.all():
            characterList.append(model_to_dict(character))

        return Response(characterList, status=status.HTTP_200_OK)

class UpdateDatabase(APIView):

    def get(self, request):

        pages = self.getNumberOfPages()

        for page in range(1, pages+1):
            with ClusterRpcProxy(CONFIG) as rpc:
                characterList = rpc.rickService.listCharacters(page)

            for character in characterList['results']:
                entry = Character(
                    name = character['name'],
                    status = character['status'],
                    species = character['species'],
                    gender = character['gender'],
                    id = character['id'],
                    image = character['image']
                )
                entry.save()

        return Response(status=status.HTTP_200_OK)

    def getNumberOfPages(self):
        with ClusterRpcProxy(CONFIG) as rpc:
            characterList = rpc.rickService.listCharacters()

        return characterList['info']['pages']

class DetailCharacter(APIView):
    """Returns data from a single character"""

    def get(self, request, id):

        character = {}

        if (Character.objects.filter(pk=id).exists()):
            characterObject = Character.objects.get(pk=id)
            character = model_to_dict(characterObject)
        else:
            with ClusterRpcProxy(CONFIG) as rpc:
                character = rpc.rickService.getCharacterData(id)

        return Response(character, status=status.HTTP_200_OK)

class CharacterEpisodes(APIView):
    """Returns a list of every episode the chararter appears in"""
    def get(self, request, id):

        with ClusterRpcProxy(CONFIG) as rpc:
            episodes = rpc.rickService.getChapters(id)

        return Response(episodes, status=status.HTTP_200_OK)


"""
Code from lucidrains https://github.com/lucidrains/phenaki-pytorch/blob/main/phenaki_pytorch/phenaki_pytorch.py and modified
"""

import math
import torch
import torch.nn.functional as F
from torch import nn, einsum
from einops import rearrange, repeat

def exists(val):
    return val is not None

def default(val, d):
    return val if exists(val) else d

def log(t, eps=1e-20):
    return torch.log(t + eps)

def gumbel_noise(t):
    noise = torch.zeros_like(t).uniform_(0, 1)
    return -log(-log(noise))

def gumbel_sample(t, temperature=1., dim=-1):
    return ((t / max(temperature, 1e-10)) + gumbel_noise(t)).argmax(dim=dim)

def top_k(logits, thres=0.5):
    num_logits = logits.shape[-1]
    k = max(int((1 - thres) * num_logits), 1)
    val, ind = torch.topk(logits, k)
    probs = torch.full_like(logits, float('-inf'))
    probs.scatter_(1, ind, val)
    return probs

def FeedForward(dim, mult=4):
    inner_dim = int(mult * dim)
    return nn.Sequential(
        nn.LayerNorm(dim),
        nn.Linear(dim, inner_dim, bias=False),
        nn.GELU(),
        nn.Linear(inner_dim, dim, bias=False)
    )

class Attention(nn.Module):
    def __init__(
            self,
            dim,
            dim_context=None,
            dim_head=64,
            heads=8,
            causal=False,
            num_null_kv=2
    ):
        super().__init__()
        self.heads = heads
        self.causal = causal
        self.scale = dim_head ** -0.5
        inner_dim = dim_head * heads
        dim_context = default(dim_context, dim)

        self.norm = nn.LayerNorm(dim)

        self.null_kv = nn.Parameter(torch.randn(heads, 2 * num_null_kv, dim_head))

        self.to_q = nn.Linear(dim, inner_dim, bias=False)
        self.to_kv = nn.Linear(dim_context, inner_dim * 2, bias=False)
        self.to_out = nn.Linear(inner_dim, dim, bias=False)

    def forward(
            self,
            x,
            mask=None,
            context=None
    ):
        batch, device, dtype = x.shape[0], x.device, x.dtype

        kv_input = default(context, x)

        x = self.norm(x)

        q, k, v = self.to_q(x), *self.to_kv(kv_input).chunk(2, dim=-1)

        q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h=self.heads), (q, k, v))

        q = q * self.scale

        nk, nv = repeat(self.null_kv, 'h (n r) d -> b h n r d', b=batch, r=2).unbind(dim=-2)

        k = torch.cat((nk, k), dim=-2)
        v = torch.cat((nv, v), dim=-2)

        sim = einsum('b h i d, b h j d -> b h i j', q, k)

        i, j = sim.shape[-2:]

        if exists(mask):
            mask = F.pad(mask, (j - mask.shape[-1], 0), value=True)
            mask = rearrange(mask, 'b j -> b 1 1 j')
            sim = sim.masked_fill(~mask, -torch.finfo(sim.dtype).max)

        if self.causal:
            causal_mask = torch.ones((i, j), device=device, dtype=torch.bool).triu(j - i + 1)
            sim = sim.masked_fill(causal_mask, -torch.finfo(sim.dtype).max)

        attn = sim.softmax(dim=-1)

        out = einsum('b h i j, b h j d -> b h i d', attn, v)

        out = rearrange(out, 'b h n d -> b n (h d)')
        return self.to_out(out)

class Transformer(nn.Module):
    def __init__(
            self,
            dim,
            *,
            depth,
            dim_context=None,
            causal=False,
            dim_head=64,
            heads=8,
            ff_mult=4,
            attn_num_null_kv=2,
            has_cross_attn=False
    ):
        super().__init__()
        self.layers = nn.ModuleList([])

        for _ in range(depth):
            self.layers.append(nn.ModuleList([
                Attention(dim=dim, dim_head=dim_head, heads=heads, causal=causal),
                Attention(dim=dim, dim_head=dim_head, dim_context=dim_context, heads=heads, causal=False,
                          num_null_kv=attn_num_null_kv) if has_cross_attn else None,
                FeedForward(dim=dim, mult=ff_mult)
            ]))

        self.norm_out = nn.LayerNorm(dim)

    def forward(self, x, context=None, mask=None):

        for self_attn, cross_attn, ff in self.layers:
            x = self_attn(x) + x

            if exists(cross_attn) and exists(context):
                x = cross_attn(x, context=context, mask=None) + x

            x = ff(x) + x

        return self.norm_out(x)

class MaskGit(nn.Module):
    def __init__(
            self,
            *,
            dim,
            num_tokens,
            max_seq_len,
            **kwargs
    ):
        super().__init__()
        self.mask_id = num_tokens

        self.token_emb = nn.Embedding(num_tokens + 1, dim)  # last token is used as mask_id
        self.pos_emb = nn.Embedding(max_seq_len, dim)

        self.transformer = Transformer(
            dim=dim,
            attn_num_null_kv=2,
            has_cross_attn=True,
            **kwargs
        )

        self.to_logits = nn.Linear(dim, num_tokens)

        self.loss_fn = nn.CrossEntropyLoss(label_smoothing=0.1)

    def gamma(self, r):
        return (r * torch.pi / 2).cos()

    def add_noise(self, x, r):
        r = self.gamma(r)[:, None]
        mask = torch.bernoulli(r * torch.ones_like(x))
        mask = mask.round().bool()
        x = x * (~mask) + self.mask_id * mask
        return x, mask

    def forward(self, x, text_cond=None, **kwargs):
        b, n = x.shape

        x = self.token_emb(x)
        x = self.pos_emb(torch.arange(n, device=x.device)) + x

        x = self.transformer(x, text_cond, **kwargs)

        return self.to_logits(x)

    def loss(self, pred, video_indices, mask):
        acc = (pred.permute(0, 2, 1).argmax(1) == video_indices).float().mean()
        video_indices = video_indices[mask]  # 839
        # video_indices = video_indices.flatten()
        mask = mask.flatten()  # 1536
        pred = pred.view(-1, pred.shape[-1])[mask]  # 839x1024
        # pred = pred.view(-1, pred.shape[-1])  # 839x1024
        return self.loss_fn(pred, video_indices), acc

class MaskGitTrainWrapper(nn.Module):
    def __init__(
            self,
            maskgit,
            *,
            steps
    ):
        super().__init__()
        self.maskgit = maskgit
        self.mask_id = maskgit.mask_id

        self.steps = steps

    def forward(self, x, **kwargs):
        batch, seq, device = *x.shape, x.device

        self.maskgit.train()

        rand_step = torch.randint(0, self.steps, (1,), device=device)
        num_tokens_mask = (seq * torch.cos(rand_step * math.pi * 0.5 / self.steps)).round().long().clamp(
            min=1)  # cosine schedule was best

        _, indices = torch.randn((batch, seq), device=device).topk(num_tokens_mask.item(), dim=-1)
        mask = torch.zeros((batch, seq), device=device).scatter(1, indices, 1.).bool()

        masked_input = torch.where(mask, self.mask_id, x)

        logits = self.maskgit(masked_input, **kwargs)

        loss = F.cross_entropy(logits[mask], x[mask])
        return loss

class Phenaki(nn.Module):
    def __init__(
            self,
            *,
            maskgit: MaskGit,
            steps=18,  # 18 is the ideal steps with token critic
            sample_temperature=0.,
            text_embed_dim=None,
            cond_drop_prob=0.25,
            max_text_len=128
    ):
        super().__init__()

        self.maskgit = maskgit
        self.mask_id = maskgit.mask_id
        self.maskgit_trainer = MaskGitTrainWrapper(maskgit, steps=steps)

        # sampling

        self.steps = steps
        self.sample_temperature = sample_temperature

        assert cond_drop_prob > 0.
        self.cond_drop_prob = cond_drop_prob  # classifier free guidance for transformers - @crowsonkb

    @torch.no_grad()
    def sample( self, *, text, num_frames, cond_scale=3., starting_temperature=0.9, noise_K=1.):
        device = next(self.parameters()).device
        num_tokens = self.cvivit.num_tokens_per_frames(num_frames)

        with torch.no_grad():
            text_embeds = self.encode_texts([text], output_device=device)
            text_mask = torch.any(text_embeds != 0, dim=-1)

        shape = (1, num_tokens)

        video_token_ids = torch.full(shape, self.mask_id, device=device)
        mask = torch.ones(shape, device=device, dtype=torch.bool)

        scores = None

        for step in range(self.steps):
            is_first_step = step == 0
            is_last_step = step == (self.steps - 1)

            steps_til_x0 = self.steps - (step + 1)

            if not is_first_step and exists(scores):
                time = torch.full((1,), step / self.steps, device=device)
                num_tokens_mask = (num_tokens * torch.cos(time * math.pi * 0.5)).round().long().clamp(min=1)

                _, indices = scores.topk(num_tokens_mask.item(), dim=-1)
                mask = torch.zeros(shape, device=device).scatter(1, indices, 1).bool()

            video_token_ids = torch.where(mask, self.mask_id, video_token_ids)

            logits = self.maskgit.forward_with_cond_scale(
                video_token_ids,
                context=text_embeds,
                mask=text_mask,
                cond_scale=cond_scale
            )

            temperature = starting_temperature * (step / steps_til_x0)
            pred_video_ids = gumbel_sample(logits, temperature=temperature)

            video_token_ids = torch.where(mask, pred_video_ids, video_token_ids)

            if not is_last_step:
                scores = logits.gather(2, rearrange(pred_video_ids, '... -> ... 1'))
                scores = 1 - rearrange(scores, '... 1 -> ...')
                scores = torch.where(mask, scores, -1e4)

        video = self.cvivit.decode_from_codebook_indices(video_token_ids)
        return video

if __name__ == '__main__':
    # m = MaskGit(dim=2048, num_tokens=1024, max_seq_len=1536, depth=32, dim_context=512, heads=32)  # paper version
    m = MaskGit(dim=1224, num_tokens=8192, max_seq_len=1536, depth=22, dim_context=512, heads=22)  # 6 x 16 x 16
    # print(sum([p.numel() for p in m.parameters()]))
    for name, p in m.named_parameters():
        print(name)
        print(p.shape)
    # x = torch.randint(low=0, high=8192, size=(6, 16, 16)).view(1, -1)
    # text_cond = torch.randn(1, 10, 512)
    # print(m(x, text_cond).shape)


import sys
import datetime
print(sys.maxsize)

s=1
for x in range(2147483647):
    s = s*2
    if(x% 1000000 == 0):
        f = open(str(x)+".txt","w")
        f.write(str(s))
        f.close()
        print(x,datetime.datetime.now())
f = open("answer.txt","w")
f.write(str(s))
f.close()


import markdown

class Article(object):
    def __init__(self, md):
        if not md.startswith("---"):
            return

        head = md[4:md.find("---", 3)].split("\n")[:-1]
        head = [x.lstrip() for x in head]

        for attr in head:
            split = attr.find(":")
            setattr(self, attr[:split], attr[split+1:].strip())

        self.categories = self.categories.split(", ")
        self.text = markdown.markdown(md[md.find("---", 3)+3:])


import datetime
import sqlite3

from src.database_functions import execute_query
from src.utils import database_cache_path

def main():

    date = "2023-01-03"
    location = "southeastern-highlands"

    db_path = database_cache_path()
    assert db_path.exists()

    conn = sqlite3.connect(str(db_path))
    cursor = conn.cursor()

    execute_query(f"SELECT * FROM mwis WHERE (date='{date}' AND location='{location}')", cursor)

    print(cursor.fetchall()[-1])

if __name__ == "__main__":
    main()


import requests,json, time,os,subprocess
from datetime import datetime

url = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/calendarByDistrict?district_id=670&date=16-05-2021"
url2 = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/calendarByDistrict?district_id=670&date=16-05-2021"

flag = True
url_flag = True
skip_count = 0
head = {
    "accept":"application/json, text/plain, */*",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
    "origin": "https://www.cowin.gov.in",
    "referer": "https://www.cowin.gov.in/",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "cross-site",
    "sec-gpc": "1",
    "user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
}

while flag:
    try:
        if url_flag:
            print(url)
            r = requests.get(url, headers=head)
            url_flag = False
        else:
            print(url2)
            r = requests.get(url2, headers=head)
            url_flag = True
        print("request sent " + datetime.now().strftime('%d-%m-%Y %H:%M:%S'))
        # print(r.text)
        #valid_pin = [226028, 226010, 226016]
        valid_pin = []
        exclude_hosp = ['Charak Hospital']
        # exclude_pin = []
        center_list= json.loads(r.text)
        vaccine_available = False
        for center in center_list['centers']:
            if center['pincode'] not in valid_pin:
                for session in center['sessions']:
                    if session['min_age_limit'] < 45 and session['available_capacity'] > 1:
                        print(center['name'] + "-" + str(center["pincode"]) + " : " + session['date'] + " : slots- " +
                              str(session['available_capacity']) + " : " + str(session['vaccine'] + " : age " + str(session['min_age_limit'])))
                        # print(center)
                        if center['name'] not in exclude_hosp:
                            vaccine_available = True
        if vaccine_available:
            os.system('say "Vaccine Slot Available"')
            subprocess.call(["afplay", "alert.wav"])
            # flag = False
            time.sleep(60)
            exit(0)
        else:
            print("no slot")
    except Exception as e:
        if 'Expecting value: line 1 column 1 (char 0)' == str(e) and skip_count < 3:
            print(e)
            os.system('say "Program failed"')
            skip_count += 1
            pass
        else:
            print(e)
            os.system('say "Program failed. Exiting"')
            subprocess.call(["afplay", "alert.wav"])
            exit(-1)
    time.sleep(30)


import numpy as np
import random

import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, SpatialDropout1D
from keras.layers import Activation, Flatten, Input, Masking
from keras.layers import Convolution1D, MaxPooling1D
from keras.utils import np_utils
from keras.layers.normalization import BatchNormalization
from keras import backend as K
from sklearn.metrics import auc, roc_auc_score, roc_curve, precision_recall_curve
from keras.layers.recurrent import LSTM, GRU
from keras.layers.wrappers import Bidirectional
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
from keras import regularizers
from keras import losses
from keras import optimizers
from keras.models import Model
from crf import CRF
from layer.PyramidPooling import PyramidPooling
from keras.utils import multi_gpu_model
from utils import generate_label, make_batch
import time
from sys import argv
script, GOterm = argv

def run_crf(score_map, co_exp_net, testing_size, theta):
    positive_unary_energy = 1 - score_map
    crf = CRF(testing_size, positive_unary_energy, co_exp_net, theta)
    pos_prob_crf = crf.inference(10)

    return pos_prob_crf

#-------------------------------------------------------------------------------
#Loading sequence data
def load_sequence_data():
    X_test_seq = np.load('../data/sequences/human_sequence_test_demo.npy')
    X_test_dm = np.load('../data/domains/human_domain_test_demo.npy')
    X_test_geneid = np.load('../data/id_lists/gene_list_test_demo.npy')
    X_test_isoid = np.load('../data/id_lists/isoform_list_test_demo.npy')

    return X_test_seq, X_test_dm, X_test_geneid, X_test_isoid

#-------------------------------------------------------------------------------
#Loading positive and negtive set
def pos_gene_set(StudiedGOterm):
    positive_set = []

    fr = open('../data/annotations/human_annotations.txt', 'r')
    while True:
        line = fr.readline()
        if not line:
            break
        line = line.split('\n')[0]
        gene = line.split('\t')[0]
        GO = line.split('\t')[1:]
        if StudiedGOterm in GO:
            positive_set.append(gene)
    fr.close()

    return positive_set

#-------------------------------------------------------------------------------
###### main function ######
StudiedGOterm = GOterm

print('Testing model for ' + StudiedGOterm)

positive_Gene = pos_gene_set(StudiedGOterm)

co_exp_net = np.load('../data/co-expression_net/coexp_net_unified_demo.npy')
X_test_seq, X_test_dm, X_test_geneid, X_test_isoid = load_sequence_data()
K_testing_size = X_test_seq.shape[0]
seq_dim = X_test_seq.shape[1]
dm_dim = X_test_dm.shape[1]

print('Generating initial label...')
y_test = generate_label(X_test_geneid, positive_Gene)

## Model architecture
seq_input = Input(shape=(None, ), dtype='int32', name='seq_input')
x1 = Embedding(input_dim = 8001, output_dim = 32)(seq_input)
x1 = Convolution1D(filters = 64, kernel_size = 32, strides = 1,  padding = 'valid', activation = 'relu')(x1)
x1 = PyramidPooling([1, 2, 4, 8])(x1)
x1 = Dense(32, kernel_regularizer=regularizers.l2(0.15))(x1)
x1 = Activation('relu')(x1)
x1 = Dropout(0.5)(x1)
x1 = Dense(16, kernel_regularizer=regularizers.l2(0.15))(x1)
seq_output = Activation('relu')(x1)

domain_input = Input(shape=(dm_dim, ), dtype='int32', name='domain_input')
x2 = Embedding(input_dim = 16155, output_dim = 32, input_length = dm_dim, mask_zero = True)(domain_input)
domain_output = LSTM(16)(x2)
x = keras.layers.concatenate([seq_output, domain_output])

x = Dense(16, kernel_regularizer=regularizers.l2(0.15))(x)
x = Activation('relu')(x)
x = Dropout(0.5)(x)
x = Dense(1, kernel_regularizer=regularizers.l2(0.15))(x)
output = Activation('sigmoid')(x)

model = Model(inputs = [seq_input, domain_input], output = output)

model.summary()

adam = optimizers.Adam(lr = 0.002, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-08, decay=0.01)
model.compile(loss= losses.binary_crossentropy, optimizer= adam, metrics=['accuracy'])

## Load model
model.load_weights('../saved_models/'+ StudiedGOterm + '_DNN.h5')
theta = np.load('../saved_models/'+ StudiedGOterm +'_CRF_weights.npy')

## Testing
tup_idx, tup_gp = make_batch(X_test_seq)
y_pred = np.array([])
for key in tup_gp.keys():
    sel = tup_gp[key]
    st = sel[0]
    ed = sel[1]
    le = sel[2]
    X_test_seq_batch = X_test_seq[tup_idx[st: ed + 1]]
    X_test_dm_batch = X_test_dm[tup_idx[st: ed + 1]]
    for i in range(X_test_seq_batch.shape[0] / 1000 + 1):
        y_pred_l = model.predict_on_batch([X_test_seq_batch[1000 * i : 1000 * (i + 1), seq_dim - le: seq_dim], X_test_dm_batch[1000 * i : 1000 * (i + 1)]])
        y_pred = np.hstack((y_pred, np.transpose(y_pred_l)[0]))
ori_indx = np.argsort(tup_idx)
initial_score = y_pred[ori_indx]

print("Run mean field approximation for CRF.")
pos_prob_crf = run_crf(initial_score, co_exp_net, K_testing_size, theta)

y_test_gene = []
y_pred_gene = []
y_pred = pos_prob_crf
for gid in set(list(X_test_geneid)):
    idx = np.where(X_test_geneid == gid)
    y_test_gene.append(np.max(y_test[idx]))
    y_pred_gene.append(np.max(y_pred[idx]))
auc_roc = roc_auc_score(y_test_gene, y_pred_gene)
print('\nPerformance evaluation on test data:\nAUC:' + str(auc_roc))

precision, recall, thresholds = precision_recall_curve(y_test_gene, y_pred_gene)
auc_prc = auc(recall, precision)
y_test_gene = np.array(y_test_gene)
ratio = len(np.where(y_test_gene == 1)[0]) * 1.0 / len(y_test_gene)
print('AUPRC:' + str(auc_prc) + '(baseline:' + str(ratio) + ')')

# Print predictions
print("\nThe predictions for test data are saved in the results folder.")
fw = open('../results/' + StudiedGOterm + '_prediction_scores.txt', 'w')
for i in range(K_testing_size):
    fw.write(X_test_geneid[i] + '\t' + X_test_isoid[i] + '\t')
    fw.write(str(pos_prob_crf[i]) + '\n')
fw.close()


from pathlib import Path
from tempfile import TemporaryDirectory
from typing import List, Optional
from unittest.mock import Mock

import pandas as pd  # type: ignore

from antarest.core.model import JSON
from antarest.study.storage.rawstudy.model.filesystem.config.model import FileStudyTreeConfig
from antarest.study.storage.rawstudy.model.filesystem.context import ContextServer
from antarest.study.storage.rawstudy.model.filesystem.matrix.matrix import MatrixFrequency, MatrixNode

MOCK_MATRIX_JSON = {
    "index": ["1", "2"],
    "columns": ["a", "b"],
    "data": [[1, 2], [3, 4]],
}

MOCK_MATRIX_DTO = [[1, 2], [3, 4]]

class MockMatrixNode(MatrixNode):
    def __init__(self, context: ContextServer, config: FileStudyTreeConfig) -> None:
        super().__init__(
            config=config,
            context=context,
            freq=MatrixFrequency.ANNUAL,
        )

    def parse(
        self,
        file_path: Optional[Path] = None,
        tmp_dir: Optional[TemporaryDirectory] = None,
        return_dataframe: bool = False,
    ) -> JSON:
        return MOCK_MATRIX_JSON

    # def dump(
    #     self, data: Union[bytes, JSON], url: Optional[List[str]] = None
    # ) -> None:
    #     """Dump the matrix data in JSON format to simplify the tests"""
    #     self.config.path.parent.mkdir(exist_ok=True, parents=True)
    #     self.config.path.write_text(
    #         json.dumps(data, indent=2), encoding="utf-8"
    #     )

    def check_errors(self, data: str, url: Optional[List[str]] = None, raising: bool = False) -> List[str]:
        pass  # not used

class TestMatrixNode:
    def test_normalize(self, tmp_path: Path):
        file = tmp_path / "matrix.json"
        file.touch()

        matrix_service = Mock()
        matrix_service.create.return_value = "my-id"

        resolver = Mock()
        resolver.build_matrix_uri.return_value = "matrix://my-id"

        node = MockMatrixNode(
            context=ContextServer(matrix=matrix_service, resolver=resolver),
            config=FileStudyTreeConfig(study_path=file, path=file, study_id="mi-id", version=-1),
        )

        node.normalize()

        # check the result
        assert node.get_link_path().read_text() == "matrix://my-id"
        assert not file.exists()
        matrix_service.create.assert_called_once_with(MOCK_MATRIX_DTO)
        resolver.build_matrix_uri.assert_called_once_with("my-id")

    def test_denormalize(self, tmp_path: Path):
        file = tmp_path / "matrix.json"

        link = file.parent / f"{file.name}.link"
        link.write_text("my-id")

        resolver = Mock()
        resolver.resolve.return_value = MOCK_MATRIX_JSON

        node = MockMatrixNode(
            context=ContextServer(matrix=Mock(), resolver=resolver),
            config=FileStudyTreeConfig(study_path=file, path=file, study_id="mi-id", version=-1),
        )

        node.denormalize()

        # check the result
        assert not link.exists()
        actual = pd.read_csv(file, sep="\t", header=None)
        assert actual.values.tolist() == MOCK_MATRIX_JSON["data"]


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# irisdc.py

""" Fetches data form a Google Spreadsheet and calculates different variables. """

import argparse
import csv
import json
import sys
import queue
import os
import requests
import mistune

from oauth2client.client import SignedJwtAssertionCredentials
from flask import Flask, jsonify, request, render_template, send_from_directory
from hamlish_jinja import HamlishExtension
from flask.ext.assets import Environment, Bundle

import IrisDimmensionalCalculator

__author__ = "Codeando México"
__license__ = "GPL"
__version__ = "1.0"
__credits__ = "Miguel Salazar, Ricardo Alanis"
__maintainer__ = "Miguel Salazar"
__email__ = "miguel@codeandomexico.org"
__status__ = "Prototype"

app = Flask(__name__)
queue = queue.Queue()

app.jinja_env.filters['env'] = os.getenv

app.jinja_env.add_extension(HamlishExtension)

assets = Environment(app)
assets.url = app.static_url_path

css_bundle = Bundle('css/home.css.css',  output='all.css')
assets.register('css_all', css_bundle)

#js_bundl = Bundle('js/home.js.coffe', filters='coffescript', output='all.js')
#assets.register('js_all', js_bundle)

@app.route('/visualiza-tus-resultados')
def form():
    return render_template("form.html.haml")

@app.route('/', methods=['POST'])
def form_post():

    url = request.form['url']
    call_url = 'http://powerful-oasis-7324.herokuapp.com/api/response?url="'+ url+ '"'
    request_url = requests.get(call_url)
    graphdata = request_url.json()['data']
    graphjson = json.dumps(graphdata)
    graphjson = graphjson
    return render_template("graph.html.haml", graphjson= graphjson)

@app.route('/')
def get_que_es_iris():
    file = open("app/static/content/index.md")
    content = mistune.markdown(file.read())
    return render_template("md.html.haml", title="Inicio", content=content)

@app.route('/contesta-iris')
def get_como_contestar_iris():
    file = open("app/static/content/como-contestar-iris.md")
    content = mistune.markdown(file.read())
    return render_template("md.html.haml", title="Contesta IRIS", content=content)

@app.route('/api/response', methods=['GET'])
def get_response():
	urldoc = request.args.get('url')
	stringurl = str(urldoc)
	iris = IrisDimmensionalCalculator.IrisDimmensionalCalculator(stringurl,queue)

	try:
		iris.start()
		iris_grade = queue.get()
	except:
		iris_grade = "Error de procesamiento"
	return jsonify({'data':[iris_grade]})

if __name__ == '__main__':
	app.run(debug=True, threaded=True)


from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
class CategoryPage():
    def __init__(self,driver):
        self.driver=driver
        self.categoryname="Sipariş Takibi"
        self.alerttext=".alert-orange > .content"

    def get_orderid(self):
        WebDriverWait(self.driver, 8).until(
            expected_conditions.visibility_of_element_located((By.LINK_TEXT, self.categoryname)))
        self.driver.find_element(By.LINK_TEXT, self.categoryname).click()
        self.driver.find_element(By.ID, "orderId").send_keys("123456789")
    def get_customer_email(self):
        self.driver.find_element(By.ID, "customerEmailAddress").click()
        self.driver.find_element(By.ID, "customerEmailAddress").send_keys("test@gmail.com")
        self.driver.find_element(By.ID, "submit-order-find").click()

    def get_alert(self):
        self.driver.find_element(By.CSS_SELECTOR,self.alerttext ).click()
        WebDriverWait(self.driver, 8).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, self.alerttext)))


from typing import List, Dict

class Basic_Adapter:
    """
    abstract base class that needs to be subclassed for every special adapter
    each special class should have an instantiated transformer_dict in the following structure:
    {'db_field_name_for_program': 'key_from_tp_importer', ...}
    """

    transformer_dict = dict()

    def transform(
        self, tp_data: List[Dict], transformer_dict: dict = None
    ) -> List[Dict]:
        """
        transforms the given dictionaries to a form defined in the class variable
        :param tp_data: a list of dictionaries coming from the TP-Importer
        :param transformer_dict: [OPTIONAL] a dictionary with the format {KEY_FOR_APP:KEY_FROM_TPIMPORTER} -
        used for separated importing of further data that is not covered within the original transformer_dict
        :return: List[Dict] (with adapted keys fitting the specific service)
        """
        if not transformer_dict:
            transformer_dict = self.transformer_dict
        tp_data_mam = []
        for title in tp_data:
            title_dict_mam = dict()
            for key_mam, key_tpimporter in transformer_dict.items():
                title_dict_mam[key_mam] = title.get(key_tpimporter)
            tp_data_mam.append(title_dict_mam)
        return tp_data_mam


import json
import logging
import collections

import h5py
import sys
sys.path.append('../lofar_data_interface')

import lofarReadSnippet
from stations import stations

logger = logging.getLogger(__name__)

class ObservationDao(object):
    @staticmethod
    def read_sap_numbers(path):
        return lofarReadSnippet.read_sap_numbers(path)

    @staticmethod
    def read_sap_targets(path):
        return lofarReadSnippet.read_SAP_targets(path)

    @staticmethod
    def get_observation(path, sap):
        print('_________________________________________________________________')
        print('this is path: \n {}'.format(path))
        print('this is sap: \n {}'.format(sap))
        logger.info('Loading observation for sap %d from: %s', sap, path)
        data = lofarReadSnippet.read_hypercube(path, visibilities_in_dB=True, read_visibilities=True,
                                               read_flagging=False)

        file_annotations = lofarReadSnippet.read_file_annotations(path)
        clusters, cluster_algo_annotations = lofarReadSnippet.read_clusters(path)
        info_string = lofarReadSnippet.create_info_string(data, path, file_annotations, clusters, cluster_algo_annotations)
        logger.info('Loaded observation for %s\n%s', path, info_string)
        return data['saps'][sap]

    @staticmethod
    def get_observation_id(path, sap=0,label=None):
        logger.info('Loading observation id from: %s', path)
        data = lofarReadSnippet.read_hypercube(path, read_visibilities=False, read_flagging=False)

        return data.get('sas_id', 'unknown')

    @staticmethod
    def get_cluster_annotations(path, sap, label='latest'):
        logger.info('Loading cluster annotations for label="%s" sap %d from: %s', label, int(sap), path)
        data = lofarReadSnippet.read_hypercube(path, read_visibilities=False, read_flagging=False)
        file_annotations = lofarReadSnippet.read_file_annotations(path)
        clusters, cluster_algo_annotations = lofarReadSnippet.read_clusters(path)
        info_string = lofarReadSnippet.create_info_string(data, path, file_annotations, clusters, cluster_algo_annotations)
        logger.info('Loaded cluster annotations for: %s\n%s', path, info_string)

        return clusters[int(sap)].get('annotations', {})

    @staticmethod
    def get_clusters(path, sap, label):
        logger.info('Loading clusters from: %s', path)
        result = lofarReadSnippet.read_clusters(path, label)
        if result[0] == {}:
            # No saps means no clusters either.
            return {}
        return result[0][sap]['clusters']

    @staticmethod
    def get_annotations(path, sap, label):
        logger.info('Loading annotations from: %s', path)
        result = lofarReadSnippet.read_clusters(path, label)
        if result[0] == {}:
            # No saps means no clusters either.
            return {}
        return result[0][sap]['annotations']

    @staticmethod
    def get_parset(path):
        logger.info('Loading parset from: %s', path)
        return lofarReadSnippet.read_hypercube_parset(path, as_string=True)

    @staticmethod
    def get_info_dict(path):
        logger.info('Loading info_dict from: %s', path)
        return lofarReadSnippet.read_info_dict(path)

    @staticmethod
    def write_clusters(path, sap, clusters, label):
        logger.info('Writing clusters to: %s', path)
        lofarReadSnippet.write_clusters(path, {sap: {'clusters': clusters}}, label=label)

    @staticmethod
    def annotate_cluster(cluster, path, annotation, user, sap, label='latest'):
        lofarReadSnippet.annotate_cluster(path, label, sap, cluster, annotation, user)

    @staticmethod
    def delete_cluster_annotation(path, sap_nr, cluster_nr, annotation_nr, label='latest'):
        lofarReadSnippet.delete_cluster_annotation(path, sap_nr, cluster_nr, annotation_nr, label='latest')

    @staticmethod
    def save_embedding(path, sap, embedding, label):
        ObservationDao._save_dataset_as_json(embedding, 'embedding', label, path, sap)

    @staticmethod
    def get_embedding(path, sap, label='latest'):
        return ObservationDao._get_dataset('embedding', label, path, sap)

    @staticmethod
    def save_labels(path, sap, labels, label):
        ObservationDao._save_dataset_as_json(labels, 'labels', label, path, sap)

    @staticmethod
    def get_labels(path, sap, label='latest'):
        return ObservationDao._get_dataset('labels', label, path, sap)

    @staticmethod
    def get_stations(path, sap, label='latest'):
        return ObservationDao._get_dataset('stations', label, path, sap)

    @staticmethod
    def get_stations_old(path, sap, label='latest'):
        return stations

    @staticmethod
    def save_stations(path, sap, stations, label):
        ObservationDao._save_dataset_as_json(stations, 'stations', label, path, sap)

    @staticmethod
    def save_baselines(path, sap, baselines, label):
        """
        Save baselines information into observation file.
        :param path: Observation file path
        :param sap: Sap number of which the relevant station information will be saved
        :param baselines: List of baselines
        :param label: Job label
        :return:
        """
        bak = baselines
        baseline_counter = 0
        for baseline in baselines:
            temp_list = []
            for b in baseline:
                if isinstance(b, bytes):
                    temp_list.append(b.decode("utf-8"))
                else:
                    break
            bak[baseline_counter] = (temp_list[0],temp_list[1])
            baseline_counter+=1
        baselines =bak
        ObservationDao._save_dataset_as_json(baselines, 'baselines', label, path, sap)

    @staticmethod
    def get_baselines(path, sap, label='latest'):
        return ObservationDao._get_dataset('baselines', label, path, sap)

    @staticmethod
    def get_models(path, sap, label='latest'):
        data = (h5py.File(path)['clustering'].keys())
        return json.loads(json.dumps((data)))

    @staticmethod
    def get_performance(path, sap,label):
        print('_________________________________________________________________')
        print('_________________________________________________________________')
        print('_________________________________________________________________')
        print('_________________________________________________________________')
        print(label , (h5py.File(path)['clustering/{}/saps/0/score'.format(label)][()]))
        print('_________________________________________________________________')
        print('_________________________________________________________________')
        print('_________________________________________________________________')
        print('_________________________________________________________________')
        data = (h5py.File(path)['clustering/{}/saps/0/score'.format(label)][()])
        return json.loads(json.dumps((data)))

    @staticmethod
    def _convert(data):
        if isinstance(data, basestring):
            return str(data)
        elif isinstance(data, collections.Mapping):
            return dict(map(convert, data.iteritems()))
        elif isinstance(data, collections.Iterable):
            return type(data)(map(ObservationDao._convert, data))
        else:
            return data

    @staticmethod
    def _get_dataset(dataset_name, label, path, sap):
        with h5py.File(path, "r+") as observation:
            h5path = ['clustering', label, 'saps', str(sap), 'json', dataset_name, 0]
            dataset = _navigate_to_h5path(observation, h5path, path)
            return json.loads(dataset)

    @staticmethod
    def _save_dataset_as_json(dataset, dataset_name, label, path, sap):
        encoded = json.dumps(dataset).encode(encoding='ascii')
        with h5py.File(path, "r+") as observation:
            h5path = ['clustering', label, 'saps', str(sap)]
            sap_group = _navigate_to_h5path(observation, h5path, path)
            if 'json' not in sap_group:
                json_group = sap_group.create_group('json')
            else:
                json_group = sap_group['json']
            json_group.create_dataset(dataset_name, (1,), h5py.special_dtype(vlen=str), encoded)

def _navigate_to_h5path(h5group, h5path, h5file='h5 file'):
    print(h5path)
    """
    Navigate to a path within a h5 group. Raises a more readable exception if key not found than standard key error.
    :param h5group:
    :param h5path: a list of keys composing the path for example ['path', 'to', 'item', '0']
    :param h5file:
    :return:
    """
    current_group = h5group
    current_path = []
    for key in h5path:
        try:
            current_group = current_group[key]
        except KeyError as e:
            raise KeyError(
                'No "{}" in path "{}" while navigating {}.'.format(key, '/' + '/'.join(current_path), h5file), e)
        current_path.append(key)
    return current_group


from ducho.runner.Runner import MultimodalFeatureExtractor
from datasets import load_dataset
import numpy as np
import os

np.random.seed(42)

def main():
    dataset = load_dataset('ashraq/fashion-product-images-small')
    dataset = np.array(dataset['train'])[np.random.choice(len(dataset['train']), 100, replace=False)].tolist()
    if not os.path.exists('./local/data/demo1/images/'):
        os.makedirs('./local/data/demo1/images/')
    with open(f'./local/data/demo1/descriptions.tsv', 'a') as f:
        f.write('PRODUCT_ID\tdescription\n')
        for file in dataset:
            img = file['image']
            description = f'{file["gender"]} {file["masterCategory"]} ' \
                          f'{file["subCategory"]} {file["articleType"]} ' \
                          f'{file["baseColour"]} {file["season"]} ' \
                          f'{file["year"]} {file["usage"]} ' \
                          f'{file["productDisplayName"]}'
            img.save(f'./local/data/demo1/images/{file["id"]}.jpg')
            f.write(f'{file["id"]}\t{description}\n')
    extractor_obj = MultimodalFeatureExtractor(config_file_path='./demos/demo1/config.yml')
    extractor_obj.execute_extractions()

if __name__ == '__main__':
    main()


import cv2
import numpy as np

img = cv2.imread('1.jpg',1)
cv2.imshow('src',img )
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]  #   获取图像信息 宽度  高度
#   三个位置进行映射  （左上角  左下角  右上角）
matSrc = np.float32([[0,0],[0,height],[width-1,0]])
matDst = np.float32 ([[50,50],[300,height -200],[width-300,100]])
#  组合矩阵
matAffine = cv2.getAffineTransform(matSrc,matDst)
dst = cv2.warpAffine(img,matAffine,(width,height))
cv2.imshow('dst',dst)
cv2.waitKey(0)

图像几何变换/图像仿射边间.py


#!/usr/bin/env python3
import os
import re
import shlex
import subprocess
from pathlib import Path

changed_files = (
    subprocess.check_output(shlex.split("git diff --name-only origin/main HEAD"))
    .decode()
    .split()
)
cov_files = list(
    filter(lambda f: re.search(r"(\.cpp|\.c|\.h|\.hpp)$", f), changed_files)
)
# Can't track back to python.exe for some reason so don't bother.
cov_files = [f for f in cov_files if "python" not in f]
print(
    ";".join(
        [
            os.environ.get(
                "GITHUB_WORKSPACE", str(Path(__file__).parent.parent.absolute())
            )
            + "/"
            + c
            for c in cov_files
        ]
    )
)


import sys
import glob
import serial
import serial.tools.list_ports

def user_select_port_loop():
    """Boucle d'attente de l'utilisateur pour obtenir le port de la carte arduino

    Returns:
        str: Port de la carte
    """
    liste_ports = serial.tools.list_ports.comports()
    ports = [port[0] for port in sorted(liste_ports)]
    r = [str(i) for i in range(len(ports))]
    port_user = None

    while(port_user not in r):

        print("--------------")
        for nb, port in zip(r, ports):
            print(nb, ".", port)

        print("--------------")
        print("A . Actualiser")
        print("S . Simulation")
        print("E . Exit")
        print("--------------")

        port_user = input("Entrer le port désiré : ")
        if (port_user == "E"):
            sys.exit()

        if (port_user == "S"):
            return ""

        if (port_user == "A"):
            liste_ports = serial.tools.list_ports.comports()
            ports = [port[0] for port in sorted(liste_ports)]
            r = [str(i) for i in range(len(ports))]

    return ports[int(port_user)]

if __name__ == '__main__':
    port = user_select_port_loop()
    print(port)
    print(type(port))