from documents.manager import DocumentsManager
from documents.errors import UnsavedFileError
from serializable import Serializable
from logger.manager import LogsManager
from singleton import TenantInstance, TenantSingleton
from subjects.manager import SubjectsManager
from views.groups import ViewGroup
from views.controller_registry import ViewControllerRegistry
from toasts.manager import ToastsManager, ToastAction

class EditorManager(TenantInstance, Serializable, metaclass=TenantSingleton):
    def __init__(self, tenant_id: str):
        super().__init__(tenant_id)
        self.__subjects = SubjectsManager(tenant_id)
        self.__logger = LogsManager(tenant_id)
        self.__toaster = ToastsManager(tenant_id)
        self.__documents = DocumentsManager(tenant_id)

        initial_group = ViewGroup()
        self.__editor_views = self.__subjects.create("workspace.views.editor.views", [initial_group])
        self.__active_views = self.__subjects.create("workspace.views.editor.active_views", [initial_group.uri])

    def serialize(self):
        active_views = self.__active_views.value()
        return {
            "active": active_views if len(active_views) > 0 else None,
            "groups": [group.serialize() for group in self.__editor_views.value()],
        }

    def add_group(self, index=None):
        index = index if index is not None else len(self.__editor_views.value())
        group = ViewGroup()
        self.__editor_views.value().insert(index, group)
        self.__active_views.value().insert(0, group.uri)
        self.__active_views.update()
        self.__editor_views.update()
        self.__logger.info(f"[Editor] Added editor group {group.uri}")
        return self.select_group(group.uri)

    def add_view(
        self,
        view_source,
        view_type: str = None,
        group_id: str = None,
        prevent_duplicates=False,
        view_configs: dict = None,
    ):
        groups = self.__editor_views.value()

        if prevent_duplicates:
            # TODO Check if configs are the same as well
            for group_uri in self.__active_views.value():
                group = next(filter(lambda group: group.uri == group_uri, groups))
                existing_view = next(
                    filter(
                        (lambda view: view.source == view_source)
                        if view_type is None
                        else (lambda v: (v.source, v.view_type) == (view_source, view_type)),
                        group.views,
                    ),
                    None,
                )
                if existing_view is not None:
                    return self.select_view(existing_view.uri, group_uri)

        group_id = group_id if group_id else self.__active_views.value()[0]
        group = next(filter(lambda group: group.uri == group_id, groups), None)

        if group is None:
            self.__logger.error(f"[Editor] No editor group found for {group_id}")
            raise KeyError(f"No editor group found for {group_id}")

        else:
            document = self.__documents.open(view_source)
            if document is not None and view_type is None:
                view_type = document.get_default_view_type()
            controller = ViewControllerRegistry()[view_type](
                source=view_source,
                document=document,
                configs=view_configs,
                onchange=lambda *_: self.__editor_views.update(),
                onsave=lambda *_: self.__editor_views.update(),
            )

            group.views.append(controller)
            group.active.insert(0, controller.uri)
            self.__editor_views.update()

            self.__logger.info(f"[Editor] Added view {controller.uri}")
            return self.select_view(controller.uri, group.uri)

    def has_view(self, view_uri: str):
        return (
            next(
                filter(lambda group: view_uri in list(map(lambda v: v.uri, group.views)), self.__editor_views.value()),
                None,
            )
            is not None
        )

    def remove_group(self, group_id: str):
        groups = self.__editor_views.value()
        group = next(filter(lambda group: group.uri == group_id, groups), None)

        if group is None:
            self.__logger.error(f"[Editor] No editor group found for {group_id}")
            raise KeyError(f"No editor group found for {group_id}")

        elif len(group.views) > 0:
            self.__logger.error(
                f"[Editor] Found views in editor group {group_id}. Cannot close editor group containing views."
            )
            raise KeyError(f"Found views in editor group {group_id}. Cannot close editor group containing views.")

        else:
            active_views = list(filter(lambda g: g != group_id, self.__active_views.value()))
            groups = list(filter(lambda g: g.uri != group_id, groups))

            self.__logger.info(f"[Editor] Deleted editor group {group_id}")

            if len(groups) == 0:
                group = ViewGroup()
                active_views = [group.uri]
                groups = [group]

            self.__active_views.notify(active_views)
            self.__editor_views.notify(groups)
            return self.select_group(active_views[0])

    def remove_view(self, view_uri: str, group_id: str = None, save: bool = False, allow_closing_if_modified=False):
        groups = self.__editor_views.value()
        group_id = group_id if group_id else self.__active_views.value()[0]
        group = next(filter(lambda group: group.uri == group_id, groups), None)

        if group is None:
            self.__logger.error(f"[Editor] No editor group found for {group_id}")
            raise KeyError(f"No editor group found for {group_id}")

        elif view_uri not in list(map(lambda v: v.uri, group.views)) or view_uri not in group.active:
            self.__logger.error(f"[Editor] No view found for {view_uri} in editor group {group_id}")
            raise KeyError(f"No view found for {view_uri} in editor group {group_id}")

        else:
            try:
                source_uri = next(filter(lambda v: v.uri == view_uri, group.views)).source
                self.__documents.close(source_uri, save=save, allow_closing_if_modified=allow_closing_if_modified)
                group.views = list(filter(lambda v: v.uri != view_uri, group.views))
                group.active.remove(view_uri)

                active = self.__active_views.value()

                if len(group.views) == 0:
                    groups = list(filter(lambda g: g.uri != group.uri, groups))
                    active.remove(group.uri)

                if len(groups) == 0:
                    new_group = ViewGroup()
                    groups.insert(0, new_group)
                    active.insert(0, new_group.uri)

                self.__logger.info(f"[Editor] Deleted {view_uri}")

                self.__editor_views.notify(groups)
                self.__active_views.notify(active)
                return view_uri

            except UnsavedFileError as e:
                self.__logger.error(
                    f"[Editor] An attempt at closing the document was made. The operation failed, as the document contained unsaved modifications."
                )
                self.__toaster.error(
                    message=f"Unsaved modifications in {source_uri.split('/')[-1]}. What would you like to do?",
                    duration=None,
                    actions=[
                        ToastAction("Save and close", lambda *_, **__: self.remove_view(view_uri, group_id, save=True)),
                        ToastAction(
                            "Close without saving",
                            lambda *_, **__: self.remove_view(
                                view_uri,
                                group_id,
                                save=False,
                                allow_closing_if_modified=True,
                            ),
                        ),
                        ToastAction("Cancel", None),
                    ],
                )

            except IOError as e:
                self.__logger.error(f"[Editor] Failed to close document {source_uri}: {e}")
                raise e

    def remove_all(self, save: bool = False, allow_closing_if_modified=False):
        new_groups = []
        for group in self.__editor_views.value():
            group_views = []
            for view in group.views:
                try:
                    self.__documents.close(view.source, save=save, allow_closing_if_modified=allow_closing_if_modified)

                except UnsavedFileError:
                    group_views += [view]

            if len(group_views) > 0:
                group.views = group_views
                views_uris = list(map(lambda v: v.uri, group_views))
                group.active = list(filter(lambda v: v in views_uris, group.active))
                new_groups += [group]

        if len(new_groups) > 0:
            group_uris = list(map(lambda g: g.uri, new_groups))
            new_active = list(filter(lambda g: g in group_uris, self.__active_views.value()))
            self.__editor_views.notify(new_groups)
            self.__active_views.notify(new_active)
            self.__logger.error(f"[Editor] Failed to close all documents.")
            raise UnsavedFileError("Failed to close all documents.")

        new_groups = [ViewGroup()]
        new_active = [new_groups[0].uri]
        self.__editor_views.notify(new_groups)
        self.__active_views.notify(new_active)
        self.__logger.info(f"[Editor] Closed all documents.")

    def save(self):
        if len(self.__active_views.value()) == 0:
            self.__logger.error("[Editor] No editor groups found")
            raise KeyError("No editor groups found")

        active_group_uri = self.__active_views.value()[0]
        group = next(filter(lambda group: group.uri == active_group_uri, self.__editor_views.value()), None)

        if group is None:
            self.__logger.error(f"[Editor] No editor group found for {active_group_uri}")
            raise KeyError(f"No editor group found for {active_group_uri}")

        if len(group.active) == 0:
            self.__logger.info(f"[Editor] Nothing to save")
            return None

        active_view_uri = group.active[0]
        view = next(filter(lambda view: view.uri == active_view_uri, group.views), None)

        if view is None:
            self.__logger.error(f"[Editor] No view found for {active_view_uri} in editor group {group.uri}")
            raise KeyError(f"No view found for {active_view_uri} in editor group {group.uri}")

        view.save()
        self.__logger.info(f"[Editor] Saved {view.uri}")
        return view.uri

    def save_all(self):
        saved = []
        for group in self.__editor_views.value():
            for view in group.views:
                view.save()
                saved.append(view.uri)

        self.__logger.info("[Editor] Saved all views")
        return saved

    def select_group(self, group_id: str):
        if next(filter(lambda group: group.uri == group_id, self.__editor_views.value()), None) is None:
            self.__logger.error(f"[Editor] No editor group found for {group_id}")
            raise KeyError(f"No editor group found for {group_id}")

        self.__logger.info(f"[Editor] Setting active editor group to {group_id}")

        self.__active_views.value().remove(group_id)
        self.__active_views.value().insert(0, group_id)
        self.__active_views.update()
        return group_id

    def select_view(self, view_uri: str, group_id: str = None):
        groups = self.__editor_views.value()
        group_id = group_id if group_id else self.__active_views.value()[0]
        group = next(filter(lambda group: group.uri == group_id, groups), None)

        if group is None:
            self.__logger.error(f"[Editor] No editor group found for {group_id}")
            raise KeyError(f"No editor group found for {group_id}")

        elif view_uri not in map(lambda v: v.uri, group.views):
            self.__logger.error(f"No view found for {view_uri} in editor group {group_id}")
            raise KeyError(f"No view found for {view_uri} in editor group {group_id}")

        self.__logger.info(f"[Editor] Setting active view to {view_uri} for editor group {group_id}")

        group.active.remove(view_uri)
        group.active.insert(0, view_uri)
        self.__editor_views.update()
        self.__active_views.value().remove(group_id)
        self.__active_views.value().insert(0, group_id)
        self.__active_views.update()
        return view_uri

    def handle_event(self, view_uri: str, params: dict):
        for group in self.__editor_views.value():
            for view in group.views:
                if view.uri == view_uri:
                    break
            else:
                view = None

            if view is not None:
                break

        if view is None:
            self.__logger.error(f"[Editor] No view found for {view_uri}")
            raise KeyError(f"No view found for {view_uri}")

        else:
            return view.update(params)


# -*- coding: utf-8 -*-
"""
Created on Wed Dec  6 21:36:26 2017

@author: user
"""

import sqlite3
conn = sqlite3.connect('DB/DB.db')
print ('Opened database successfully')

#our feture functions.
import zero_crossing_rate
import MFCC
import spectral_centroid
import spectral_contrast
import spectral_rollof
import spectral_bandwidth

import librosa
import statistics
import numpy as np
name='Chopin - Nocturne op.9 No.2 - Andante'
y, sr = librosa.load('audio/'+name+'.mp3')#adres secimi yap.

zcr = zero_crossing_rate.zero_crossing_rate(y)
vzcr=statistics.pvariance(zcr[0])
zcr=np.mean(zcr)

centroid=spectral_centroid.spectral_centroid(y=y, sr=sr)
vcentroid=statistics.pvariance(centroid[0])
centroid=np.mean(centroid)

S = np.abs(librosa.stft(y))
contrast = spectral_contrast.spectral_contrast(S=S, sr=sr)
vcontrast=statistics.pvariance(contrast[0])
contrast = np.mean(contrast)

rollof = spectral_rollof.spectral_rolloff(y=y, sr=sr)
vrollof=statistics.pvariance(rollof[0])
rollof=np.mean(rollof)

bandwidth = spectral_bandwidth.spectral_bandwidth(y=y, sr=sr)
vbandwidth=statistics.pvariance(bandwidth[0])
bandwidth=np.mean(bandwidth)

mfcc_array=MFCC.mfcc(y=y, sr=sr,n_mfcc=13)
vmfcc1=statistics.pvariance(mfcc_array[0])
mfcc1=np.mean(mfcc_array[0])

vmfcc2=statistics.pvariance(mfcc_array[1])
mfcc2=np.mean(mfcc_array[1])

vmfcc3=statistics.pvariance(mfcc_array[2])
mfcc3=np.mean(mfcc_array[2])

vmfcc4=statistics.pvariance(mfcc_array[3])
mfcc4=np.mean(mfcc_array[3])

vmfcc5=statistics.pvariance(mfcc_array[4])
mfcc5=np.mean(mfcc_array[4])

vmfcc6=statistics.pvariance(mfcc_array[5])
mfcc6=np.mean(mfcc_array[5])

vmfcc7=statistics.pvariance(mfcc_array[6])
mfcc7=np.mean(mfcc_array[6])

vmfcc8=statistics.pvariance(mfcc_array[7])
mfcc8=np.mean(mfcc_array[7])

vmfcc9=statistics.pvariance(mfcc_array[8])
mfcc9=np.mean(mfcc_array[8])

vmfcc10=statistics.pvariance(mfcc_array[9])
mfcc10=np.mean(mfcc_array[9])

vmfcc11=statistics.pvariance(mfcc_array[10])
mfcc11=np.mean(mfcc_array[10])

vmfcc12=statistics.pvariance(mfcc_array[11])
mfcc12=np.mean(mfcc_array[11])

vmfcc13=statistics.pvariance(mfcc_array[12])
mfcc13=np.mean(mfcc_array[12])

#conn.execute("INSERT INTO Feature(NAME,mZcr,vZcr,mCentroid,vCentroid,mContrast,vContrast,mBandwidth,vBandwidth,mRollof,vRollof,mMFFC1,vMFFC1,mMFFC2,vMFFC2,mMFFC3,vMFFC3,mMFFC4,vMFFC4,mMFFC5,vMFFC5,mMFFC6,vMFFC6,mMFFC7,vMFFC7,mMFFC8,vMFFC8,mMFFC9,vMFFC9,mMFFC10,vMFFC10,mMFFC11,vMFFC11,mMFFC12,vMFFC12,mMFFC13,vMFFC13,)  VALUES ('"+name+"', "+zcr+", "+vzcr+", "+centroid+", "+vcentroid+", "+contrast+", "+vcontrast+", "+rollof+", "+vrollof+", "+bandwidth+", "+vbandwidth+", "+mfcc1+", "+vmfcc1+", "+mfcc2+", "+vmfcc2+", "+mfcc3+", "+vmfcc3+" "+mfcc4+", "+vmfcc4+", "+mfcc5+", "+vmfcc5+", "+mfcc6+", "+vmfcc6+", "+mfcc7+", "+vmfcc7+", "+mfcc8+", "+vmfcc8+", "+mfcc9+", "+vmfcc9+", "+mfcc10+", "+vmfcc10+", "+mfcc11+", "+vmfcc11+", "+mfcc12+", "+vmfcc12+", "+mfcc13+", "+vmfcc13+" )")
List=[name,zcr,vzcr,centroid,vcentroid,contrast,vcontrast,rollof,vrollof,bandwidth,vbandwidth,mfcc1,vmfcc1,mfcc2,vmfcc2,mfcc3,vmfcc3,mfcc4,vmfcc4,mfcc5,vmfcc5,mfcc6,vmfcc6,mfcc7,vmfcc7,mfcc8,vmfcc8,mfcc9,vmfcc9,mfcc10,vmfcc10,mfcc11,vmfcc11,mfcc12,vmfcc12,mfcc13,vmfcc13]
conn.execute("INSERT INTO Feature(NAME,mZcr,vZcr,mCentroid,vCentroid,mContrast,vContrast,mBandwidth,vBandwidth,mRollof,vRollof,mMFFC1,vMFFC1,mMFFC2,vMFFC2,mMFFC3,vMFFC3,mMFFC4,vMFFC4,mMFFC5,vMFFC5,mMFFC6,vMFFC6,mMFFC7,vMFFC7,mMFFC8,vMFFC8,mMFFC9,vMFFC9,mMFFC10,vMFFC10,mMFFC11,vMFFC11,mMFFC12,vMFFC12,mMFFC13,vMFFC13)  VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", List)

conn.commit()

conn.close()


"""Get all sites in the GTF that correspond to a coding sequence."""

import pandas as pd

import re

# get CDS features
cds_features = (
    pd.read_csv(
        snakemake.input.gtf,
        sep="\t",
        header=None,
        names=[
            "seqName",
            "source",
            "feature",
            "start",
            "end",
            "score",
            "strand",
            "frame",
            "attribute",
        ],
    )
    .query("feature == 'CDS'")
    .assign(
        frame=lambda x: x["frame"].astype(int),
        gene=lambda x: x["attribute"].str.extract('^gene_id "(\w+)"'),
    )
    .drop(columns=["seqName", "source", "score", "feature", "attribute"])
)

assert all(cds_features["frame"] == 0), "non frame 0 CDSs\n" + str(cds_features)
assert all(cds_features["strand"] == "+"), "non + sense CDS\n" + str(cds_features)

# get sites with codon position assigned
sites = (
    cds_features
    .assign(
        site=lambda x: x.apply(lambda r: list(range(r["start"], r["end"] + 1)), axis=1),
    )
    .explode("site", ignore_index=True)
    .assign(
        codon_position=lambda x: x.apply(
            lambda r: (r["site"] - r["start"]) % 3 + 1, axis=1,
        ),
    )
    .drop(columns=["strand", "frame", "start", "end"])
    .groupby("site", as_index=False)
    .aggregate(
        codon_position=pd.NamedAgg(
            "codon_position",
            lambda s: s.values[0] if s.nunique() == 1 else "mixed",
        ),
        gene=pd.NamedAgg("gene", lambda s: ";".join(s)),
    )
)

sites.to_csv(snakemake.output.csv, index=False)


# This works however parameters/metrics are set to constant,
# so no interaction with metrics achievable during model training

# import platform
# print(platform.python_version())

# https://www.youtube.com/watch?v=w18a5kMV-co

import mlflow

if __name__ == "__main__":
    mlflow.set_experiment(experiment_name="experiment_video")
    with mlflow.start_run(run_name="test_run_name"):
        mlflow.log_param("b", 2)
        for a in range(10):
            mlflow.log_metric("a", a)

        mlflow.log_artifact("file.txt")


lst = [23, 21, 420, 2000, 1, 123]
lst2 = []
#lst2 has only numbers divisible by 5
"""num1 = 8
if num1 % 2 == 0:"""
#lst.append[2]
# All lists in python start with 0
"""for element in lst:
#    if element > 20 and elememt < 100: (or it could also be)
#    if 20 < element < 100:
    if element == 20:
        lst2.append(element)
print lst
print lst2"""

#lst2 = [x for x in lst if x > 25]
lst2 = [x for x in lst if x % 5 == 0]

#print(lst2)

lst3 = ['hello', 'hi', 'wow', 'filthy frank', 'chelsea']
str1 = 'wow'

lst4 = [x[0] for x in lst3 if len(x) <= 6]

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

#print(matrix[0][2])
x = 0
while(x < 69):
    print(x)
    x = x + 2

for variable in lst3:
    print(variable)


import numpy as np

from sklearn.decomposition import PCA

from jPCA.util import ensure_datas_is_list, preprocess
from jPCA.regression import skew_sym_regress

__all__ = ['JPCA']

class JPCA:
    def __init__(self, num_jpcs=2):
        assert num_jpcs % 2 == 0, "num_jpcs must be an even number."
        self.num_jpcs = num_jpcs
        self.jpcs = None

    def _calculate_jpcs(self, M):
        num_jpcs = self.num_jpcs
        D, _ = M.shape

        # Eigenvalues are not necessarily sorted
        eigvals, eigvecs = np.linalg.eig(M)
        idx = np.argsort(np.abs(np.imag(eigvals)))[::-1]
        eigvecs = eigvecs[:, idx]
        eigvals = eigvals[idx]

        jpca_basis = np.zeros((D, num_jpcs))
        for k in range(0, num_jpcs, 2):
            # One eigenvalue will have negative imaginary component,
            # the other will have positive imaginary component.
            if np.imag(eigvals[k]) > 0:
                v1 = eigvecs[:, k] + eigvecs[:, k+1]
                v2 = -np.imag(eigvecs[:, k] - eigvecs[:, k+1])
            else:
                v1 = eigvecs[:, k] + eigvecs[:, k+1]
                v2 = -np.imag(eigvecs[:, k+1] - eigvecs[:, k])

            V = np.real(np.column_stack((v1, v2))) / np.sqrt(2)
            jpca_basis[:, k:k+2] = V
        return jpca_basis

    def align_jpcs(self, datas):
        """
        Given an input list of datas, rotate each pair of jPCs
        such that the preparatory states tend to lie along the X-axis.
        This mirrors the functionality of the MATLAB codepack.
        """
        projected, _ = self.project(datas, )
        for k in range(0, self.num_jpcs, 2):

            # The X and Y axis (for plotting) are the standard basis. We are
            # effectively rotating this basis to align with the prep states.
            # Note that this is purely for aesthetics -- it does not change the
            # amount of variance captured by a given jPCA plane.
            basis = np.array([[1, 0], [0, 1]])
            prep_states = np.row_stack([x[0, k:k+2] for x in projected])
            pca = PCA(n_components=2)
            pca = pca.fit(prep_states)
            rot = pca.components_

            pc1 = np.append(rot[0, :], 0)
            pc2 = np.append(rot[1, :], 0)
            cross = np.cross(pc1, pc2)
            if cross[2] < 0:
                rot[1, :] = -rot[1, :]
            basis = basis @ rot.T
            test_proj = np.concatenate([x[:, k:k+2] @ basis for x in projected])
            if np.max(np.abs(test_proj[:, 1])) > np.max(test_proj[:, 1]):
                basis = -basis

            # Update jpc pair
            self.jpcs[:, k:k+2] = self.jpcs[:, k:k+2] @ basis

    @ensure_datas_is_list
    def project(self, datas):
        """
        Project data into dimensions which capture rotations.
        We assume that rotation_matrix is an orthogonal matrix which was found
        by fitting a rotational LDS to data (e.g via setting dynamics="rotational").
        This function then projects on the top eigenvectors of this rotation matrix.

        Returns
        -------
        out: T x num_components project of the data, which should capture rotations
        """
        assert self.jpcs is not None, "Model must be fit before calling jPCA.project."
        proj = datas @ self.jpcs
        var_capt = np.var(proj, axis=(0,1))
        return [x for x in proj], var_capt

    # def: np.var(x, axis):
    #   mean = np.mean(x, axis=axis, keepdims=True)
    #   x_bar = x - mean
    #   sq_err = np.sum(x_bar**2, axis=axis)
    #   return sq_err / x.shape[axis]

    @ensure_datas_is_list
    def fit(self,
            datas,
            pca=True,
            num_pcs=6,
            subtract_cc_mean=True,
            tstart=0,
            tend=-1,
            times=None,
            align_axes_to_data=True,
            **preprocess_kwargs):

        """
        Fit jPCA to a dataset. This function does not do plotting --
        that is handled separately.
        See "args" section for information on data formatting.
        The tstart and tend arguments are used to exclude certain parts of the data
        during fitting.

        For example, if tstart=-20, tend=10, and times = [-30, -20, -10, 0, 10, 20],
        each trial would include the 2nd through 5th timebins (the other timebins)
        would be discarded. By default the entire dataset is used.

        Args
        ----
            datas: A list containing trials. Each element of the list should be T x D,
                where T is the length of the trial, and D is the number of neurons.
            pca: Boolean, whether or not we preprocess using PCA. Default True.
            num_pcs: Number of PCs to use when pca=True. Default 6.
            subtract_cc_mean: Whether we subtract CC mean during preprocessing.
            tstart: Starting time to use from the data. Default 0.
            tend: Ending time to use from the data. -1 sets it to the end of the dataset.
            times: A list or numpy array containing the time for each time-bin of the data.
                This is used to determine which time bins are included and excluded
                from the data.]

        Returns
        -------
            projected: A list of trials projected onto the jPCS. Each entry is an array
                    with shape T x num_jpcs.
            full_data_variance: Float, variance of the full dataset, for calculating
                                variance captured by projections.
            pca_captured_variance: Array, size is num_pcs. Contains variance captured
                                by each PC.
            jpca_captured_variance: Array, size is num_jpcs. Contains variance captured
                                    by each jPC.
        """
        assert isinstance(datas, list), "datas must be a list."
        assert datas, "datas cannot be empty"
        T = datas[0].shape[1]
        if times is None:
            times = np.arange(T)
            tstart=0
            tend=-1

        processed_datas, full_data_var, pca_var_capt = \
            preprocess(datas, times, tstart=tstart, tend=tend, pca=pca,
                       subtract_cc_mean=subtract_cc_mean, num_pcs=num_pcs,
                       **preprocess_kwargs)
        self.full_data_var = full_data_var

        # Estimate X dot via a first difference, and find the best
        # skew_symmetric matrix which explains the data.
        X = np.concatenate([x[:-1] for x in processed_datas])
        X_dot = np.concatenate([np.diff(x, axis=0) for x in processed_datas])

        # Calculate the jpca basis using the eigenvectors of M_opt
        M_opt = skew_sym_regress(X, X_dot)
        self.M_skew = M_opt
        self.jpcs = self._calculate_jpcs(M_opt)

        # Optionally align axes so data is spread along horizontal axes in plots
        if align_axes_to_data:
            self.align_jpcs(processed_datas)

        projected, jpca_var_capt = self.project(processed_datas)

        return (projected,
                full_data_var,
                pca_var_capt,
                jpca_var_capt)


from .models import User
from rest_framework.parsers import JSONParser

def IsLoggedIn(request):
    if request.session.has_key("username"):
        try:
            user = User.objects.get(username=request.session["username"])
            return user

        except:
            return None
    else:
        return None

def IsRegistered(request):
    try:
        data = User.objects.get(roll=request.data['roll'])
        return data.activated
    except:
        return None

def IsFollowing(username, follow_username):
    user = User.objects.get(username=username)
    try:
        follow_user = user.following.get(username=follow_username)
    except:
        follow_user = None
    if follow_user is not None:
        return True
    else:
        return False


import sys
sys.path.append('../')
from argparse import ArgumentParser
import json
import os
import shutil
import cv2
import numpy as np
import math
from numpy import linalg as LA
from scipy.spatial import distance

import pathlib
import time
import copy
import matplotlib.pyplot as plt
import pickle
import datetime
import json
from collections import defaultdict
from csv import reader
from utils import *
import glob
from PIL import Image

# TypeError: Object of <some_code> is not JSON serializable
# Ref: https://stackoverflow.com/questions/50916422/python-typeerror-object-of-type-int64-is-not-json-serializable
class NpEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        else:
            return super(NpEncoder, self).default(obj)

# ----------------------------------------
#  Configurations of the whole experiment
# ----------------------------------------
class Config:
    def __init__(self):
        # --------------------------
        #  Paramaters of experiment
        # --------------------------
        self.user = 'brcao' # 'anon'
        self.root_path = '/media/' + self.user + '/eData2' # '/home/' + self.user
        self.seq_root_path = self.root_path + '/Data/datasets/RAN/seqs/indoor/scene0'
        self.IMU_200 = False # edit
        if self.IMU_200: self.dataset4model_root_path = self.root_path + '/Data/datasets/RAN4model_dfv4_IMU_200'
        else: self.dataset4model_root_path = self.root_path + '/Data/datasets/RAN4model_dfv4'
        self.seq4model_root_path = self.dataset4model_root_path + '/seqs/indoor'
        if not os.path.exists(self.seq4model_root_path):
            os.makedirs(self.seq4model_root_path)
        print('self.seq_root_path: ', self.seq_root_path)
        self.seq_id_path_ls = glob.glob(self.seq_root_path + '/*')
        self.seq_id_ls = [seq_id_path[-15:] for seq_id_path in self.seq_id_path_ls]

        # -------
        #  Color
        # -------
        self.color_ls = ['crimson', 'lime green', 'royal blue', 'chocolate', 'purple', 'lemon']
        self.color_dict = {
            'crimson': (60,20,220),
            'lime green': (50,205,50),
            'royal blue': (225,105,65),
            'chocolate': (30,105,210),
            'purple': (128,0,128),
            'lemon': (0,247,255)
        }

        # --------------------------------------
        #  To be updated in update_parameters()
        self.seq_path = self.seq_id_path_ls[1] # 1 is a dummy number
        print(); print() # debug
        print('self.seq_id_path_ls: ', self.seq_id_path_ls)
        print('self,seq_id_ls: ', self.seq_id_ls)
        self.exp = 1 # edit
        self.exp_path = self.root_path + '/Data/datasets/RAN/seqs/exps/exp' + str(self.exp)
        self.scene_id = 0
        self.meta_path = self.exp_path + '/meta_indoor.csv'
        self.seq_id_to_start_end_ts16_dfv4 = defaultdict()
        with open(self.meta_path, 'r') as f:
            csv_reader = reader(f)
            for i, row in enumerate(csv_reader):
                if i > 0:
                    print('row: ', row)
                    self.seq_id_to_start_end_ts16_dfv4[row[0]] = defaultdict()
                    self.seq_id_to_start_end_ts16_dfv4[row[0]]['start'] = row[1]
                    self.seq_id_to_start_end_ts16_dfv4[row[0]]['end'] = row[2]
        self.start_end_ts16_indoor_dfv4_path = self.dataset4model_root_path + '/exps/exp' + \
            str(self.exp) + '/start_end_ts16_dfv4_indoor.json'
        # print('\n\n')
        # print('self.seq_id_to_start_ts16: ', self.seq_id_to_start_ts16)
        # print('self.seq_id_to_end_ts16: ', self.seq_id_to_end_ts16)

        self.img_type = 'RGBh_ts16_dfv4_anonymized'
        self.img_path = self.seq_path + '/' + self.img_type
        self.RGBh_ts16_dfv4_ls = []
        self.RGBg_ts16_dfv4_ls = []

        self.subjects = [15, 46, 77, 70, 73]
        self.subj_to_rand_id_file_path = self.exp_path + '/subj_to_rand_id.json'
        with open(self.subj_to_rand_id_file_path, 'r') as f:
            self.subj_to_id_dict = json.load(f)
            print(self.subj_to_rand_id_file_path, 'loaded!')
        self.phone_time_offsets = [0] * len(self.subjects)

        print(); print() # debug
        print('self.subjects: ', self.subjects)

        # ----------------------
        #  GND # Note dfv4 here
        # ----------------------
        # self.GND_path = self.seq_path + '/GND/RAN_' + self.seq_id + '-export.json'
        self.GND_ts16_dfv2_path = self.seq_path + '/GND_ts16_dfv2' # Note ts16_dfv4 here
        if not os.path.exists(self.GND_ts16_dfv2_path): os.makedirs(self.GND_ts16_dfv2_path)
        self.GND_ts16_dfv2_to_BBX5H_dfv2_path = self.GND_ts16_dfv2_path + '/GND_ts16_dfv2_to_BBX5H_dfv2.json'
        with open(self.GND_ts16_dfv2_to_BBX5H_dfv2_path, 'r') as f:
            self.GND_ts16_dfv2_to_BBX5H_dfv2 = json.load(f)
            print(self.GND_ts16_dfv2_to_BBX5H_dfv2_path, 'loaded!')

        # -----
        #  IMU
        # -----
        self.IMU19_data_types = ['ACCEL', 'GYRO', 'MAG', 'GRAV', 'LINEAR', 'Quaternion'] # ['ACCEL', 'GRAV', 'LINEAR', 'Quaternion', 'MAG', 'GYRO']
        self.IMUlgyq10_data_types = ['LINEAR', 'GYRO', 'Quaternion']
        self.IMUlgyqm13_data_types = ['LINEAR', 'GYRO', 'Quaternion', 'MAG']
        self.IMUaccgym_data_types = ['ACCEL', 'GYRO', 'MAG']
        self.IMU_path = self.seq_path + '/IMU'
        self.IMU_dfv4_path = self.seq_path + '/IMU_dfv4' # ts13_dfv4 with offsets (os)
        if not os.path.exists(self.IMU_dfv4_path): os.makedirs(self.IMU_dfv4_path)
        self.subj_id_to_IMU19_ts16_dfv4_path = ''
        self.subj_id_to_IMU19_ts16_dfv4 = defaultdict()
        self.subj_id_to_IMUlgyq10_ts16_dfv4_path = ''
        self.subj_id_to_IMUlgyq10_ts16_dfv4 = defaultdict()
        self.subj_id_to_IMUlgyqm13_ts16_dfv4_path = ''
        self.subj_id_to_IMUlgyqm13_ts16_dfv4 = defaultdict()
        self.subj_id_to_IMUaccgym9_ts16_dfv4_path = ''
        self.subj_id_to_IMUaccgym9_ts16_dfv4 = defaultdict()
        self.subj_id_to_IMU_NED_pos_ts16_dfv4_path = ''
        self.subj_id_to_IMU_NED_pos_ts16_dfv4 = defaultdict()

        if self.IMU_200:
            self.subj_id_to_IMU19_200_ts16_dfv4_path = ''
            self.subj_id_to_IMU19_200_ts16_dfv4 = defaultdict()
            self.subj_id_to_IMUaccgym_200_ts16_dfv4_path = ''
            self.subj_id_to_IMUaccgym_200_ts16_dfv4 = defaultdict()

        # -----
        #  FTM
        # -----
        self.FTM_path = self.seq_path + '/WiFi'
        self.FTM_dfv4_path = self.seq_path + '/FTM_dfv4' # ts13_dfv4 with offsets (os)
        if not os.path.exists(self.FTM_dfv4_path): os.makedirs(self.FTM_dfv4_path)
        self.subj_id_to_FTM_ts16_dfv4_path = ''
        self.subj_id_to_FTM_ts16_dfv4 = defaultdict()

        # ----------------------------------------------------------
        #  Synchronized data: BBXC3,BBX5,FTM,IMU,_sync_dfv4 to save
        # ----------------------------------------------------------
        # One sample per frame >>>
        self.BBXC3H_sync_dfv4, self.BBX5H_sync_dfv4 = [], []
        self.IMU19_sync_dfv4, self.IMUlgyq10_sync_dfv4 = [], []
        self.IMU_NED_pos_sync_dfv4, self.IMUlgyqm13_sync_dfv4 = [], []
        self.FTM_sync_dfv4 = []
        self.seq_id = self.seq_id_ls[0]
        self.seq_path_for_model = self.seq4model_root_path + '/scene' + str(self.scene_id) + '/' + self.seq_id
        self.RGBh_ts16_dfv4_ls_path = self.seq_path_for_model + '/RGBh_ts16_dfv4_ls.json'
        self.RGBg_ts16_dfv4_ls_path = self.seq_path_for_model + '/RGBg_ts16_dfv4_ls.json'
        self.sync_dfv4_path = self.seq_path_for_model + '/sync_ts16_dfv4'
        if not os.path.exists(self.sync_dfv4_path): os.makedirs(self.sync_dfv4_path)
        self.BBXC3H_sync_dfv4_path = self.sync_dfv4_path + '/BBXC3H_sync_dfv4.pkl'
        self.BBX5H_sync_dfv4_path = self.sync_dfv4_path + '/BBX5H_sync_dfv4.pkl'
        self.IMU19_sync_dfv4_path = self.sync_dfv4_path + '/IMU19_sync_dfv4.pkl'
        self.IMUlgyq10_sync_dfv4_path = self.sync_dfv4_path + '/IMUlgyq10_sync_dfv4.pkl'
        self.IMUlgyqm13_sync_dfv4_path = self.sync_dfv4_path + '/IMUlgyqm13_sync_dfv4.pkl'
        self.IMUaccgym9_sync_dfv4_path = self.sync_dfv4_path + '/IMUaccgym9_sync_dfv4.pkl'
        self.IMU_NED_pos_sync_dfv4_path = self.sync_dfv4_path + '/IMU_NED_pos_sync_dfv4.pkl'
        self.FTM_sync_dfv4_path = self.sync_dfv4_path + '/FTM_sync_dfv4.pkl'
        if not os.path.exists(self.sync_dfv4_path): os.makedirs(self.sync_dfv4_path)
        # One sample per frame <<<

        if self.IMU_200:
            # 200 samples per frame >>>
            self.IMU19_200_sync_dfv4, self.IMUaccgym9_200_sync_dfv4 = [[] for _ in range(200)], [[] for _ in range(200)]
            self.IMU19_200_sync_dfv4_path = self.sync_dfv4_path + '/IMU19_200_sync_dfv4.pkl'
            self.IMUaccgym9_200_sync_dfv4_path = self.sync_dfv4_path + '/IMUaccgym9_200_sync_dfv4.pkl'
            # 200 samples per frame <<<
        #  To de updated in update_parameters()
        # --------------------------------------

C = Config()

def update_parameters(seq_id_idx):
    C.seq_id = C.seq_id_ls[seq_id_idx]
    C.seq_path = C.seq_id_path_ls[seq_id_idx]
    C.img_type = 'RGBh_ts16_dfv4_anonymized'
    C.img_path = C.seq_path + '/' + C.img_type

    C.subjects = [15, 46, 77, 70, 73]
    C.seq_date = C.seq_id[:8]
    print('C.seq_date: ', C.seq_date)
    if '202012' in C.seq_date:
        if C.seq_date == '20201228':
            C.phone_time_offsets = [2.219273, 2.962273, 2.764273, 3.461273, 2.948273]
        print(); print() # debug
        print('C.subjects: ', C.subjects)
        print('C.seq_date: ', C.seq_date)
        print('C.phone_time_offsets: ', C.phone_time_offsets)

        # -----
        #  GND
        # -----
        # C.GND_path = C.seq_path + '/GND/RAN_' + C.seq_id + '-export.json'
        C.GND_ts16_dfv2_path = C.seq_path + '/GND_ts16_dfv2' # Note ts16_dfv4 here
        if not os.path.exists(C.GND_ts16_dfv2_path): os.makedirs(C.GND_ts16_dfv2_path)
        C.GND_ts16_dfv2_to_BBX5H_dfv2_path = C.GND_ts16_dfv2_path + '/GND_ts16_dfv2_to_BBX5H_dfv2.json'
        with open(C.GND_ts16_dfv2_to_BBX5H_dfv2_path, 'r') as f:
            C.GND_ts16_dfv2_to_BBX5H_dfv2 = json.load(f)
            print(C.GND_ts16_dfv2_to_BBX5H_dfv2_path, 'loaded!')

        # -----
        #  IMU
        # -----
        C.IMU19_data_types = ['ACCEL', 'GYRO', 'MAG', 'GRAV', 'LINEAR', 'Quaternion'] # ['ACCEL', 'GRAV', 'LINEAR', 'Quaternion', 'MAG', 'GYRO']
        C.IMUlgyq10_data_types = ['LINEAR', 'GYRO', 'Quaternion']
        C.IMU_path = C.seq_path + '/IMU'
        C.IMU_dfv4_path = C.seq_path + '/IMU_dfv4' # ts13_dfv4 with offsets (os)
        if not os.path.exists(C.IMU_dfv4_path): os.makedirs(C.IMU_dfv4_path)
        C.subj_id_to_IMU19_ts16_dfv4_path = C.IMU_dfv4_path + '/subj_id_to_IMU19_ts16_dfv4.json'
        with open(C.subj_id_to_IMU19_ts16_dfv4_path, 'r') as f:
            C.subj_id_to_IMU19_ts16_dfv4 = json.load(f)
            print(C.subj_id_to_IMU19_ts16_dfv4_path, 'loaded!')
        C.subj_id_to_IMUlgyq10_ts16_dfv4_path = C.IMU_dfv4_path + '/subj_id_to_IMUlgyq10_ts16_dfv4.json'
        with open(C.subj_id_to_IMUlgyq10_ts16_dfv4_path, 'r') as f:
            C.subj_id_to_IMUlgyq10_ts16_dfv4 = json.load(f)
            print(C.subj_id_to_IMUlgyq10_ts16_dfv4_path, 'loaded!')
        C.subj_id_to_IMUlgyqm13_ts16_dfv4_path = C.IMU_dfv4_path + '/subj_id_to_IMUlgyqm13_ts16_dfv4.json'
        with open(C.subj_id_to_IMUlgyqm13_ts16_dfv4_path, 'r') as f:
            C.subj_id_to_IMUlgyqm13_ts16_dfv4 = json.load(f)
            print(C.subj_id_to_IMUlgyqm13_ts16_dfv4_path, 'loaded!')
        C.subj_id_to_IMUaccgym9_ts16_dfv4_path = C.IMU_dfv4_path + '/subj_id_to_IMUaccgym9_ts16_dfv4.json'
        with open(C.subj_id_to_IMUaccgym9_ts16_dfv4_path, 'r') as f:
            C.subj_id_to_IMUaccgym9_ts16_dfv4 = json.load(f)
            print(C.subj_id_to_IMUaccgym9_ts16_dfv4_path, 'loaded!')
        C.subj_id_to_IMU_NED_pos_ts16_dfv4_path = C.IMU_dfv4_path + '/subj_id_to_IMU_NED_pos_ts16_dfv4.json'
        with open(C.subj_id_to_IMU_NED_pos_ts16_dfv4_path, 'r') as f:
            C.subj_id_to_IMU_NED_pos_ts16_dfv4 = json.load(f)
            print(C.subj_id_to_IMU_NED_pos_ts16_dfv4_path, 'loaded!')

        if C.IMU_200:
            # 200 samples per frame >>>
            C.subj_id_to_IMU19_200_ts16_dfv4_path = C.IMU_dfv4_path + '/subj_id_to_IMU19_200_ts16_dfv4.json'
            with open(C.subj_id_to_IMU19_200_ts16_dfv4_path, 'r') as f:
                C.subj_id_to_IMU19_200_ts16_dfv4 = json.load(f)
                print(C.subj_id_to_IMU19_200_ts16_dfv4_path, 'loaded!')
            C.subj_id_to_IMUaccgym_200_ts16_dfv4_path = C.IMU_dfv4_path + '/subj_id_to_IMUaccgym_200_ts16_dfv4.json'
            with open(C.subj_id_to_IMUaccgym_200_ts16_dfv4_path, 'r') as f:
                C.subj_id_to_IMUaccgym_200_ts16_dfv4 = json.load(f)
                print(C.subj_id_to_IMUaccgym_200_ts16_dfv4_path, 'loaded!')
            # 200 samples per frame <<<

        # -----
        #  FTM
        # -----
        C.FTM_path = C.seq_path + '/WiFi'
        C.FTM_dfv4_path = C.seq_path + '/FTM_dfv4' # ts13_dfv4 with offsets (os)
        if not os.path.exists(C.FTM_dfv4_path): os.makedirs(C.FTM_dfv4_path)
        C.subj_id_to_FTM_ts16_dfv4_path = C.FTM_dfv4_path + '/subj_id_to_FTM_ts16_dfv4.json'
        with open(C.subj_id_to_FTM_ts16_dfv4_path, 'r') as f:
            C.subj_id_to_FTM_ts16_dfv4 = json.load(f)
            print(C.subj_id_to_FTM_ts16_dfv4_path, 'loaded!')

        # ------------------------------------------------------
        #  Synchronized data: BBXC3,BBX5,IMU,_sync_dfv4 to save
        # ------------------------------------------------------
        C.BBXC3H_sync_dfv4, C.BBX5H_sync_dfv4 = [], []
        C.IMU19_sync_dfv4, C.IMUlgyq10_sync_dfv4 = [], []
        C.IMUlgyqm13_sync_dfv4, C.IMUaccgym9_sync_dfv4 = [], []
        C.IMU_NED_pos_sync_dfv4 = []
        C.FTM_sync_dfv4 = []
        if C.IMU_200:
            # 200 samples per frame >>>
            C.IMU19_200_sync_dfv4, C.IMUaccgym9_200_sync_dfv4 = [], []
            # 200 samples per frame <<<
        C.seq_path_for_model = C.seq4model_root_path + '/scene' + str(C.scene_id) + '/' + C.seq_id
        C.RGBh_ts16_dfv4_ls_path = C.seq_path_for_model + '/RGBh_ts16_dfv4_ls.json'
        # C.RGBh_ts16_dfv4_ls = [img_path[-21:-4] for img_path in glob.glob(C.img_path + '/*')]
        # print(); print() # debug
        # print('C.RGBh_ts16_dfv4_ls: ', C.RGBh_ts16_dfv4_ls)
        with open(C.RGBh_ts16_dfv4_ls_path, 'r') as f:
            C.RGBh_ts16_dfv4_ls = json.load(f)
            print(C.RGBh_ts16_dfv4_ls_path, 'loaded!')

        C.RGBg_ts16_dfv4_ls = []
        C.RGBg_ts16_dfv4_ls_path = C.seq_path_for_model + '/RGBg_ts16_dfv4_ls.json'
        C.sync_dfv4_path = C.seq_path_for_model + '/sync_ts16_dfv4'
        if not os.path.exists(C.sync_dfv4_path): os.makedirs(C.sync_dfv4_path)
        C.BBXC3H_sync_dfv4_path = C.sync_dfv4_path + '/BBXC3H_sync_dfv4.pkl'
        C.BBX5H_sync_dfv4_path = C.sync_dfv4_path + '/BBX5H_sync_dfv4.pkl'
        C.IMU19_sync_dfv4_path = C.sync_dfv4_path + '/IMU19_sync_dfv4.pkl'
        C.IMUlgyq10_sync_dfv4_path = C.sync_dfv4_path + '/IMUlgyq10_sync_dfv4.pkl'
        C.IMUlgyqm13_sync_dfv4_path = C.sync_dfv4_path + '/IMUlgyqm13_sync_dfv4.pkl'
        C.IMUaccgym9_sync_dfv4_path = C.sync_dfv4_path + '/IMUaccgym9_sync_dfv4.pkl'
        C.IMU_NED_pos_sync_dfv4_path = C.sync_dfv4_path + '/IMU_NED_pos_sync_dfv4.pkl'
        C.FTM_sync_dfv4_path = C.sync_dfv4_path + '/FTM_sync_dfv4.pkl'
        if C.IMU_200:
            # 200 samples per frame >>>
            C.IMU19_200_sync_dfv4_path = C.sync_dfv4_path + '/IMU19_200_sync_dfv4.pkl'
            C.IMUaccgym9_200_sync_dfv4_path = C.sync_dfv4_path + '/IMUaccgym9_200_sync_dfv4.pkl'
            # 200 samples per frame <<<
        if not os.path.exists(C.sync_dfv4_path): os.makedirs(C.sync_dfv4_path)

def process_save_sync_dfv4():
    # ----------------
    #  Each Timestamp
    # ----------------
    for i, ts16_dfv4 in enumerate(C.RGBh_ts16_dfv4_ls):
        # print(); print() # debug
        # print('i: ', i, ', ts16_dfv4: ', ts16_dfv4)
        # print('len(C.RGBh_ts16_dfv4_ls): ', len(C.RGBh_ts16_dfv4_ls))
        # print('ots26_to_ts16_dfv4(C.start_ots): ', ots26_to_ts16_dfv4(C.start_ots))
        # print('ots26_to_ts16_dfv4(C.end_ots): ', ots26_to_ts16_dfv4(C.end_ots))
        # print('C.subj_id_to_IMU19_ts16_dfv4: ', C.subj_id_to_IMU19_ts16_dfv4)

        # -----------------------
        #  Valid Timestamp range
        # -----------------------
        # if ts16_dfv4 >= ots26_to_ts16_dfv4(C.start_ots) and ts16_dfv4 <= ots26_to_ts16_dfv4(C.end_ots):
        # if i > 10 and i < len(C.RGBh_ts16_dfv4_ls) - 10:
        # if True: #  dummy condition for debugging
        if ts16_dfv4 >= C.seq_id_to_start_end_ts16_dfv4[C.seq_id]['start'] and \
            ts16_dfv4 < C.seq_id_to_start_end_ts16_dfv4[C.seq_id]['end']:
            C.RGBg_ts16_dfv4_ls.append(ts16_dfv4)
            BBXC3H_ts16_dfv4_ls, BBX5H_ts16_dfv4_ls = [], []
            IMU19_ts16_dfv4_ls, IMUlgyq10_ts16_dfv4_ls = [], []
            IMUlgyqm13_ts16_dfv4_ls, IMUaccgym9_ts16_dfv4_ls = [], []
            IMU_NED_pos_ts16_dfv4_ls = []
            if C.IMU_200:
                # 200 samples per frame >>>
                IMU19_200_ts16_dfv4_ls, IMUaccgym_200_ts16_dfv4_ls = [], []
                # 200 samples per frame <<<
            FTM_ts16_dfv4_ls = []
            # print(); print() # debug
            # print('ts16_dfv4: ', ts16_dfv4)
            # e.g. 1608750784.030037

            # --------------
            #  Each Subject
            # --------------
            for subj_i, subj in enumerate(C.subjects):
                subj_id = str(C.subj_to_id_dict['Indoor'][subj])
                # print('\n\n subj_id: ', subj_id)
                # if subj == 'Others': continue # Only process data with phone data
                # -----
                #  BBX
                # -----
                BBXC3H_ts16_dfv4_subj = [[np.nan, np.nan, np.nan]]
                BBX5H_ts16_dfv4_subj = [[np.nan, np.nan, np.nan, np.nan, np.nan]]
                if ts16_dfv4 in C.GND_ts16_dfv2_to_BBX5H_dfv2 and subj in C.GND_ts16_dfv2_to_BBX5H_dfv2[ts16_dfv4]:
                    # print(); print() # debug
                    # print('C.GND_ts16_dfv2_to_BBX5H_dfv2: ', C.GND_ts16_dfv2_to_BBX5H_dfv2)
                    # print('C.GND_ts16_dfv2_to_BBX5H_dfv2[ts16_dfv4]:', C.GND_ts16_dfv2_to_BBX5H_dfv2[ts16_dfv4])

                    d = C.GND_ts16_dfv2_to_BBX5H_dfv2[ts16_dfv4][subj]
                    # print(); print() # debug
                    # print('C.GND_ts16_dfv2_to_BBX5H_dfv2[ts16_dfv4][subj]: ', C.GND_ts16_dfv2_to_BBX5H_dfv2[ts16_dfv4][subj])
                    BBXC3H_ts16_dfv4_subj = [[d['c_col'], d['c_row'], d['d']]]
                    BBX5H_ts16_dfv4_subj = [[d['c_col'], d['c_row'], d['d'], d['w'], d['h']]]
                BBXC3H_ts16_dfv4_ls.append(BBXC3H_ts16_dfv4_subj)
                BBX5H_ts16_dfv4_ls.append(BBX5H_ts16_dfv4_subj)

                # -----
                #  IMU
                # -----
                # print('\n\n C.subj_id_to_IMU19_ts16_dfv4: ', C.subj_id_to_IMU19_ts16_dfv4)
                # print('\n\n C.subj_id_to_IMU19_ts16_dfv4[subj_id]: ', C.subj_id_to_IMU19_ts16_dfv4[subj_id])
                IMU19_ts16_dfv4_subj = C.subj_id_to_IMU19_ts16_dfv4[subj_id][ts16_dfv4]
                # print(); print() # debug
                # print('IMU19_ts16_dfv4_subj: ', IMU19_ts16_dfv4_subj)
                IMU19_ts16_dfv4_ls.append(IMU19_ts16_dfv4_subj)

                IMUlgyq10_ts16_dfv4_subj = C.subj_id_to_IMUlgyq10_ts16_dfv4[subj_id][ts16_dfv4]
                # print(); print() # debug
                # print('IMUlgyq10_ts16_dfv4_subj: ', IMUlgyq10_ts16_dfv4_subj)
                IMUlgyq10_ts16_dfv4_ls.append(IMUlgyq10_ts16_dfv4_subj)

                IMUlgyqm13_ts16_dfv4_subj = C.subj_id_to_IMUlgyqm13_ts16_dfv4[subj_id][ts16_dfv4]
                # print(); print() # debug
                # print('IMUlgyqm13_ts16_dfv4_subj: ', IMUlgyqm13_ts16_dfv4_subj)
                IMUlgyqm13_ts16_dfv4_ls.append(IMUlgyqm13_ts16_dfv4_subj)

                IMUaccgym9_ts16_dfv4_subj = C.subj_id_to_IMUaccgym9_ts16_dfv4[subj_id][ts16_dfv4]
                # print(); print() # debug
                # print('IMUIMUaccgym9_ts16_dfv4_subj: ', IMUIMUaccgym9_ts16_dfv4_subj)
                IMUaccgym9_ts16_dfv4_ls.append(IMUaccgym9_ts16_dfv4_subj)

                IMU_NED_pos_ts16_dfv4_subj = C.subj_id_to_IMU_NED_pos_ts16_dfv4[subj_id][ts16_dfv4]
                # print(); print() # debug
                # print('IMU_NED_pos_ts16_dfv4_subj: ', IMU_NED_pos_ts16_dfv4_subj)
                IMU_NED_pos_ts16_dfv4_ls.append(IMU_NED_pos_ts16_dfv4_subj)

                if C.IMU_200:
                    # 200 samples per frame >>>
                    IMU19_200_ts16_dfv4_subj = C.subj_id_to_IMU19_200_ts16_dfv4[subj_id][ts16_dfv4]
                    # print(); print() # debug
                    # print('np.shape(IMU19_200_ts16_dfv4_subj): ', np.shape(IMU19_200_ts16_dfv4_subj))
                    IMU19_200_ts16_dfv4_ls.append(IMU19_200_ts16_dfv4_subj)

                    IMUaccgym_200_ts16_dfv4_subj = C.subj_id_to_IMUaccgym_200_ts16_dfv4[subj_id][ts16_dfv4]
                    # print(); print() # debug
                    # print('np.shape(IMUaccgym_200_ts16_dfv4_subj): ', np.shape(IMUaccgym_200_ts16_dfv4_subj))
                    IMUaccgym_200_ts16_dfv4_ls.append(IMUaccgym_200_ts16_dfv4_subj)
                    # 200 samples per frame <<<

                # -----
                #  FTM
                # -----
                FTM_ts16_dfv4_subj = C.subj_id_to_FTM_ts16_dfv4[subj_id][ts16_dfv4]
                # print(); print() # debug
                # print('FTM_ts16_dfv4_subj: ', FTM_ts16_dfv4_subj)
                FTM_ts16_dfv4_ls.append(FTM_ts16_dfv4_subj)

            # print('np.shape(BBXC3H_ts16_dfv4_ls): ', np.shape(BBXC3H_ts16_dfv4_ls)) # (5, 1, 3)
            # print('np.shape(BBX5H_ts16_dfv4_ls): ', np.shape(BBX5H_ts16_dfv4_ls)) # (5, 1, 5)

            # ----------------------------
            #  Save data in one ts16_dfv4
            # ----------------------------
            C.BBXC3H_sync_dfv4.append(BBXC3H_ts16_dfv4_ls)
            C.BBX5H_sync_dfv4.append(BBX5H_ts16_dfv4_ls)
            C.IMU19_sync_dfv4.append(IMU19_ts16_dfv4_ls)
            C.IMUlgyq10_sync_dfv4.append(IMUlgyq10_ts16_dfv4_ls)
            C.IMUlgyqm13_sync_dfv4.append(IMUlgyqm13_ts16_dfv4_ls)
            C.IMUaccgym9_sync_dfv4.append(IMUaccgym9_ts16_dfv4_ls)
            if C.IMU_200:
                # 200 samples per frame >>>
                C.IMU19_200_sync_dfv4.append(IMU19_200_ts16_dfv4_ls)
                C.IMUaccgym9_200_sync_dfv4.append(IMUaccgym_200_ts16_dfv4_ls)
                # 200 samples per frame <<<
            C.IMU_NED_pos_sync_dfv4.append(IMU_NED_pos_ts16_dfv4_ls)
            C.FTM_sync_dfv4.append(FTM_ts16_dfv4_ls)

    C.BBXC3H_sync_dfv4 = np.array(C.BBXC3H_sync_dfv4)
    C.BBX5H_sync_dfv4 = np.array(C.BBX5H_sync_dfv4)
    C.IMU19_sync_dfv4 = np.array(C.IMU19_sync_dfv4)
    C.IMUlgyq10_sync_dfv4 = np.array(C.IMUlgyq10_sync_dfv4)
    C.IMUlgyqm13_sync_dfv4 = np.array(C.IMUlgyqm13_sync_dfv4)
    C.IMUaccgym9_sync_dfv4 = np.array(C.IMUaccgym9_sync_dfv4)
    C.IMU_NED_pos_sync_dfv4 = np.array(C.IMU_NED_pos_sync_dfv4)
    if C.IMU_200:
        # 200 samples per frame >>>
        C.IMU19_200_sync_dfv4 = np.array(C.IMU19_200_sync_dfv4)
        C.IMUaccgym9_200_sync_dfv4 = np.array(C.IMUaccgym9_200_sync_dfv4)
        # 200 samples per frame <<<
    C.FTM_sync_dfv4 = np.array(C.FTM_sync_dfv4)
    # --------
    #  Verify
    # --------
    print('np.shape(C.BBXC3H_sync_dfv4): ', np.shape(C.BBXC3H_sync_dfv4))
    print('np.shape(C.BBX5H_sync_dfv4): ', np.shape(C.BBX5H_sync_dfv4))
    print('np.shape(C.IMU19_sync_dfv4): ', np.shape(C.IMU19_sync_dfv4))
    print('np.shape(C.IMUlgyq10_sync_dfv4): ', np.shape(C.IMUlgyq10_sync_dfv4))
    print('np.shape(C.IMUlgyqm13_sync_dfv4): ', np.shape(C.IMUlgyqm13_sync_dfv4))
    print('np.shape(C.IMUaccgym9_sync_dfv4): ', np.shape(C.IMUaccgym9_sync_dfv4))
    print('np.shape(C.IMU_NED_pos_sync_dfv4): ', np.shape(C.IMU_NED_pos_sync_dfv4))
    print('np.shape(C.FTM_sync_dfv4): ', np.shape(C.FTM_sync_dfv4))
    if C.IMU_200:
        print('np.shape(C.IMU19_200_sync_dfv4): ', np.shape(C.IMU19_200_sync_dfv4)) # 200 samples per frame
        print('np.shape(C.IMUaccgym9_200_sync_dfv4): ', np.shape(C.IMUaccgym9_200_sync_dfv4)) # 200 samples per frame
    '''
    e.g.
    202012 RGBh: 3fps
    np.shape(C.BBXC3H_sync_dfv4):  (554, 5, 1, 3)
    np.shape(C.BBX5H_sync_dfv4):  (554, 5, 1, 5)
    np.shape(C.IMU19_sync_dfv4):  (554, 5, 1, 19)
    np.shape(C.IMUlgyq10_sync_dfv4):  (554, 5, 1, 10)
    np.shape(C.IMUlgyqm13_sync_dfv4):  (554, 5, 1, 13)
    np.shape(C.IMU_NED_pos_sync_dfv4):  (554, 5, 1, 2)
    np.shape(C.FTM_sync_dfv4):  (554, 5, 1, 2)
    np.shape(C.IMU19_200_sync_dfv4):  (554, 5, 200, 19)
    np.shape(C.IMUaccgym9_200_sync_dfv4):  (554, 5, 200, 9)
    '''

    # ----------------
    #  Save Sync Data
    # ----------------
    pickle.dump(C.BBX5H_sync_dfv4, open(C.BBX5H_sync_dfv4_path, 'wb'))
    pickle.dump(C.IMU19_sync_dfv4, open(C.IMU19_sync_dfv4_path, 'wb'))
    pickle.dump(C.IMUlgyq10_sync_dfv4, open(C.IMUlgyq10_sync_dfv4_path, 'wb'))
    pickle.dump(C.IMUlgyqm13_sync_dfv4, open(C.IMUlgyqm13_sync_dfv4_path, 'wb'))
    pickle.dump(C.IMUaccgym9_sync_dfv4, open(C.IMUaccgym9_sync_dfv4_path, 'wb'))
    pickle.dump(C.IMU_NED_pos_sync_dfv4, open(C.IMU_NED_pos_sync_dfv4_path, 'wb'))
    if C.IMU_200:
        pickle.dump(C.IMU19_200_sync_dfv4, open(C.IMU19_200_sync_dfv4_path, 'wb')) # 200 samples per frame
        pickle.dump(C.IMUaccgym9_200_sync_dfv4, open(C.IMUaccgym9_200_sync_dfv4_path, 'wb')) # 200 samples per frame
    pickle.dump(C.FTM_sync_dfv4, open(C.FTM_sync_dfv4_path, 'wb'))
    print(C.BBXC3H_sync_dfv4_path, 'saved!'); print(C.BBX5H_sync_dfv4_path, 'saved!')
    print(C.IMU19_sync_dfv4_path, 'saved!'); print(C.IMUlgyq10_sync_dfv4_path, 'saved!')
    print(C.IMUlgyqm13_sync_dfv4_path, 'saved!'); print(C.IMUaccgym9_sync_dfv4_path, 'saved!')
    print(C.IMU_NED_pos_sync_dfv4_path, 'saved!')
    if C.IMU_200:
        # 200 samples per frame >>>
        print(C.IMU19_200_sync_dfv4_path, 'saved!'); print(C.IMUaccgym9_200_sync_dfv4_path, 'saved!')
        # 200 samples per frame <<<
    print(C.FTM_sync_dfv4_path, 'saved!')

    with open(C.RGBg_ts16_dfv4_ls_path, 'w') as f:
        json.dump(C.RGBg_ts16_dfv4_ls, f, cls=NpEncoder)
        print(C.RGBg_ts16_dfv4_ls_path, 'saved!')

# ------------------
#  Process all seqs
# ------------------
# --------
#  indoor
# --------
for seq_id_idx, seq_id in enumerate(C.seq_id_ls):
    update_parameters(seq_id_idx)
    if '202012' in C.seq_date:
        process_save_sync_dfv4()


import tkinter as tk
class WindowWithContents:

    def __init__(self, r, f):
        self.rootWindow = r
        self.frameList = f

#Makes window with frames
def makeWindowWithXFrames(amountOfFrames, width, height):
    newRoot = tk.Tk()
    newRoot.minsize(width, height)
    newRoot.maxsize(width, height)
    frameList = []
    for i in range(amountOfFrames):
        frameList.append(tk.Frame(newRoot))
    return (newRoot, frameList)


#coding=utf-8
import sympy

from double_integration.trapezoidal_double import trapezoidal_double

def test_trapezoidal_double_byhand():
    f = lambda x, y: 2 * x + y
    a = 0
    b = 2
    c = 2
    d = 3
    x, y = sympy.symbols('x y')
    I_expected = sympy.integrate(f(x, y), (x, a, b), (y, c, d))
    for nx, ny in (3, 5), (4, 4), (5, 3):
        I_computed1 = trapezoidal_double(f, a, b, c, d, nx, ny)
        tol = 1E-14
        assert abs((I_computed1 - I_expected) / I_expected) < tol


from bridge import Bridge
import json
import config
import binascii
from preprompt import templates

class Adapter:

    def __init__(self, input):
        self.base_url = "https://api.openai.com/v1/engines/davinci/completions"
        self.id = input.get('id', '1')
        self.request_data = input.get('data')
        if self.validate_request_data():
            self.bridge = Bridge()
            self.set_params()
            self.create_request()
        else:
            self.result_error('No data provided')

    def validate_request_data(self):
        if self.request_data is None:
            return False
        if self.request_data == {}:
            return False
        return True

    def set_params(self):
        self.prompt = self.request_data.get("prompt")
        self.item_type = self.request_data.get("itemType").strip()
        print('TYPE: ', self.item_type, 'Weapons' in self.item_type)
        self.template = templates[self.item_type]

    def create_request(self):
        print(self.template.format(self.prompt))
        try:
            headers={
    "Authorization": f"Bearer {config.API_KEY}",
    "Content-Type": 'application/json'
  }
            json={
                "prompt": self.template.format(self.prompt),
                "max_tokens": 50,
                "temperature": 0.70,
                "top_p": 1,
                "n": 1,
                "frequency_penalty": 0.05,
                "presence_penalty": 0.05,
                "stop": ["\n"]
            }
            response = self.bridge.request(self.base_url, json=json, headers=headers)
            data = response.json()['choices'][0]['text'][1:]
            print('Response: ', data)

            hex_data = binascii.hexlify(data.encode('utf8'))
            bytes_data = '0x' + str(hex_data)[2:-1]
            self.result = bytes_data
            self.result_success(bytes_data)
        except Exception as e:
            self.result_error(e)
        finally:
            self.bridge.close()

    def result_success(self, data):
        self.result = {
            'jobRunID': self.id,
            'data': data,
            'result': self.result,
            'statusCode': 200,
        }

    def result_error(self, error):
        self.result = {
            'jobRunID': self.id,
            'status': 'errored',
            'error': f'There was an error: {error}',
            'statusCode': 500,
        }


from setuptools import setup

with open("README.md", "r") as fh:
    long_description = fh.read()

setup(
    name="trompa-solid",
    author="Music Technology Group, Universitat Pompeu Fabra",
    install_requires=['requests', 'redis', 'PyJWT>=2.0.0', 'jwcrypto', 'six', 'cryptography'],
    description="A python library for communicating with a SOLID pod",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/trompamusic/solid-auth-demo",
    packages=['trompasolid', 'trompasolid.backend', 'trompasolid.model'],
    use_scm_version=True,
    setup_requires=['setuptools_scm'],
    classifiers=[
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: Apache Software License",
        "Operating System :: OS Independent",
        "Topic :: Scientific/Engineering :: Information Analysis",
        "Topic :: Software Development :: Libraries"
    ],
    python_requires='>=3.7',
)


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

### Contents of the main file ###
import streamlit as st
from PIL import Image

st.set_page_config(layout="wide")
image = Image.open('assets/irri.jpg')

col1, col2 = st.columns([2,10])
with col1:
    st.image(image, width=100)
with col2:
    st.title('Welcome!')
st.header("Do you know your cluster membership?")


from __future__ import annotations

import logging
import re
import typing as t
from argparse import Namespace
from collections import defaultdict
from pathlib import Path

from dbt import flags
from dbt.adapters.factory import register_adapter, reset_adapters
from dbt.config import Profile, Project, RuntimeConfig
from dbt.config.profile import read_profile
from dbt.config.renderer import DbtProjectYamlRenderer, ProfileRenderer
from dbt.parser.manifest import ManifestLoader
from dbt.tracking import do_not_track

from sqlmesh.dbt.basemodel import Dependencies
from sqlmesh.dbt.model import ModelConfig
from sqlmesh.dbt.package import MacroConfig
from sqlmesh.dbt.seed import SeedConfig
from sqlmesh.dbt.source import SourceConfig
from sqlmesh.dbt.target import TargetConfig
from sqlmesh.dbt.test import TestConfig
from sqlmesh.dbt.util import DBT_VERSION
from sqlmesh.utils.errors import ConfigError
from sqlmesh.utils.jinja import MacroInfo, MacroReference, extract_call_names, nodes

if t.TYPE_CHECKING:
    from dbt.contracts.graph.manifest import Macro, Manifest
    from dbt.contracts.graph.nodes import ManifestNode, SourceDefinition

logger = logging.getLogger(__name__)

TestConfigs = t.Dict[str, TestConfig]
ModelConfigs = t.Dict[str, ModelConfig]
SeedConfigs = t.Dict[str, SeedConfig]
SourceConfigs = t.Dict[str, SourceConfig]
MacroConfigs = t.Dict[str, MacroConfig]

class ManifestHelper:
    def __init__(
        self,
        project_path: Path,
        profiles_path: Path,
        profile_name: str,
        target: TargetConfig,
    ):
        self.project_path = project_path
        self.profiles_path = profiles_path
        self.profile_name = profile_name
        self.target = target

        self.__manifest: t.Optional[Manifest] = None
        self._project_name: str = ""

        self._is_loaded: bool = False
        self._tests_per_package: t.Dict[str, TestConfigs] = defaultdict(dict)
        self._models_per_package: t.Dict[str, ModelConfigs] = defaultdict(dict)
        self._seeds_per_package: t.Dict[str, SeedConfigs] = defaultdict(dict)
        self._sources_per_package: t.Dict[str, SourceConfigs] = defaultdict(dict)
        self._macros_per_package: t.Dict[str, MacroConfigs] = defaultdict(dict)

        self._tests_by_owner: t.Dict[str, t.List[TestConfig]] = defaultdict(list)
        self._disabled_refs: t.Optional[t.Set[str]] = None
        self._disabled_sources: t.Optional[t.Set[str]] = None

    def tests(self, package_name: t.Optional[str] = None) -> TestConfigs:
        self._load_all()
        return self._tests_per_package[package_name or self._project_name]

    def models(self, package_name: t.Optional[str] = None) -> ModelConfigs:
        self._load_all()
        return self._models_per_package[package_name or self._project_name]

    def seeds(self, package_name: t.Optional[str] = None) -> SeedConfigs:
        self._load_all()
        return self._seeds_per_package[package_name or self._project_name]

    def sources(self, package_name: t.Optional[str] = None) -> SourceConfigs:
        self._load_all()
        return self._sources_per_package[package_name or self._project_name]

    def macros(self, package_name: t.Optional[str] = None) -> MacroConfigs:
        self._load_all()
        return self._macros_per_package[package_name or self._project_name]

    @property
    def all_macros(self) -> t.Dict[str, t.Dict[str, MacroInfo]]:
        self._load_all()
        result: t.Dict[str, t.Dict[str, MacroInfo]] = defaultdict(dict)
        for package_name, macro_configs in self._macros_per_package.items():
            for macro_name, macro_config in macro_configs.items():
                result[package_name][macro_name] = macro_config.info
        return result

    def _load_all(self) -> None:
        if self._is_loaded:
            return
        self._load_macros()
        self._load_sources()
        self._load_tests()
        self._load_models_and_seeds()
        self._is_loaded = True

    def _load_sources(self) -> None:
        for source in self._manifest.sources.values():
            source_dict = source.to_dict()
            if source_dict.get("database") == self.target.database:
                source_dict.pop("database", None)  # Only needed if overrides project's db

            source_config = SourceConfig(
                **_config(source),
                **source_dict,
            )
            self._sources_per_package[source.package_name][
                source_config.config_name
            ] = source_config

    def _load_macros(self) -> None:
        for macro in self._manifest.macros.values():
            if macro.name.startswith("test_"):
                macro.macro_sql = _convert_jinja_test_to_macro(macro.macro_sql)

            dependencies = Dependencies(macros=_macro_references(self._manifest, macro))
            if not macro.name.startswith("materialization_") and not macro.name.startswith("test_"):
                dependencies = dependencies.union(self._extra_dependencies(macro.macro_sql))

            self._macros_per_package[macro.package_name][macro.name] = MacroConfig(
                info=MacroInfo(
                    definition=macro.macro_sql,
                    depends_on=dependencies.macros,
                ),
                dependencies=dependencies,
                path=Path(macro.original_file_path),
            )

    def _load_tests(self) -> None:
        for node in self._manifest.nodes.values():
            if node.resource_type != "test":
                continue

            skip_test = False
            refs = _refs(node)
            for ref in refs:
                if self._is_disabled_ref(ref):
                    logger.info(
                        "Skipping test '%s' which references a disabled model '%s'",
                        node.name,
                        ref,
                    )
                    skip_test = True
                    break

            if skip_test:
                continue

            dependencies = Dependencies(
                macros=_macro_references(self._manifest, node),
                refs=refs,
                sources=_sources(node),
            )
            # Implicit dependencies for model test arg
            dependencies.macros.append(MacroReference(package="dbt", name="get_where_subquery"))
            dependencies.macros.append(MacroReference(package="dbt", name="should_store_failures"))

            sql = node.raw_code if DBT_VERSION >= (1, 3) else node.raw_sql  # type: ignore
            dependencies = dependencies.union(self._extra_dependencies(sql))
            dependencies = dependencies.union(
                self._macro_source_ref_dependencies(dependencies.macros, node.package_name)
            )

            test_model = _test_model(node)
            node_config = _node_base_config(node)
            node_config["database"] = node.database or self.target.database

            test = TestConfig(
                sql=sql,
                model_name=test_model,
                test_kwargs=node.test_metadata.kwargs if hasattr(node, "test_metadata") else {},
                dependencies=dependencies,
                **node_config,
            )
            self._tests_per_package[node.package_name][node.name.lower()] = test
            if test_model:
                self._tests_by_owner[test_model].append(test)

    def _load_models_and_seeds(self) -> None:
        for node in self._manifest.nodes.values():
            if node.resource_type not in ("model", "seed"):
                continue

            macro_references = _macro_references(self._manifest, node)
            tests = (
                self._tests_by_owner[node.name]
                + self._tests_by_owner[f"{node.package_name}.{node.name}"]
            )
            node_config = _node_base_config(node)

            if node.resource_type == "model":
                sql = node.raw_code if DBT_VERSION >= (1, 3) else node.raw_sql  # type: ignore
                dependencies = Dependencies(
                    macros=macro_references, refs=_refs(node), sources=_sources(node)
                )
                dependencies = dependencies.union(self._extra_dependencies(sql))
                dependencies = dependencies.union(
                    self._macro_source_ref_dependencies(dependencies.macros, node.package_name)
                )

                self._models_per_package[node.package_name][node.name] = ModelConfig(
                    sql=sql,
                    dependencies=dependencies,
                    tests=tests,
                    **node_config,
                )
            else:
                self._seeds_per_package[node.package_name][node.name] = SeedConfig(
                    dependencies=Dependencies(macros=macro_references),
                    tests=tests,
                    **node_config,
                )

    @property
    def _manifest(self) -> Manifest:
        if not self.__manifest:
            self.__manifest = self._load_manifest()
        return self.__manifest

    def _load_manifest(self) -> Manifest:
        do_not_track()

        args: Namespace = Namespace(
            vars={} if DBT_VERSION >= (1, 5) else "{}",
            profile=self.profile_name,
            profiles_dir=str(self.profiles_path),
            target=self.target.name,
            macro_debugging=False,
        )
        flags.set_from_args(args, None)

        profile = self._load_profile()
        project = self._load_project(profile)

        if not any(k in project.models for k in ("start", "+start")):
            raise ConfigError(
                f"SQLMesh's requires a start date in order to have a finite range of backfilling data. Add start to the 'models:' block in dbt_project.yml. https://sqlmesh.readthedocs.io/en/stable/integrations/dbt/#setting-model-backfill-start-dates"
            )

        runtime_config = RuntimeConfig.from_parts(project, profile, args)

        self._project_name = project.project_name

        register_adapter(runtime_config)
        manifest = ManifestLoader.get_full_manifest(runtime_config)
        reset_adapters()
        return manifest

    def _load_project(self, profile: Profile) -> Project:
        project_renderer = DbtProjectYamlRenderer(profile)
        return Project.from_project_root(str(self.project_path), project_renderer)

    def _load_profile(self) -> Profile:
        profile_renderer = ProfileRenderer({})
        raw_profiles = read_profile(str(self.profiles_path))
        return Profile.from_raw_profiles(
            raw_profiles=raw_profiles,
            profile_name=self.profile_name,
            renderer=profile_renderer,
            target_override=self.target.name,
        )

    def _is_disabled_ref(self, ref: str) -> bool:
        if self._disabled_refs is None:
            self._load_disabled()

        return ref in self._disabled_refs  # type: ignore

    def _is_disabled_source(self, source: str) -> bool:
        if self._disabled_sources is None:
            self._load_disabled()

        return source in self._disabled_sources  # type: ignore

    def _load_disabled(self) -> None:
        self._disabled_refs = set()
        self._disabled_sources = set()
        for nodes in self._manifest.disabled.values():
            for node in nodes:
                if node.resource_type in ("model", "snapshot", "seed"):
                    self._disabled_refs.add(f"{node.package_name}.{node.name}")
                    self._disabled_refs.add(node.name)
                elif node.resource_type == "source":
                    self._disabled_sources.add(f"{node.package_name}.{node.name}")

        for node in self._manifest.nodes.values():
            if node.resource_type in ("model", "snapshot", "seed"):
                self._disabled_refs.discard(node.name)
            elif node.resource_type == "source":
                self._disabled_sources.discard(node.name)

    def _macro_source_ref_dependencies(
        self, macros: t.List[MacroReference], default_package: str
    ) -> Dependencies:
        dependencies = Dependencies()
        for macro in macros:
            macro_config = self._macros_per_package[macro.package or default_package].get(
                macro.name
            )
            if macro_config:
                dependencies = dependencies.union(macro_config.dependencies)
        dependencies.macros = []
        return dependencies

    def _extra_dependencies(self, target: str) -> Dependencies:
        # We sometimes observe that the manifest doesn't capture all macros, refs, and sources within a macro.
        # This behavior has been observed with macros like dbt.current_timestamp(), dbt_utils.slugify(), and source().
        # Here we apply our custom extractor to make a best effort to supplement references captured in the manifest.
        dependencies = Dependencies()
        for call_name, node in extract_call_names(target):
            if len(call_name) == 2 and call_name[0] in ("dbt", "dbt_utils"):
                dependencies.macros.append(MacroReference(package=call_name[0], name=call_name[1]))
            elif call_name[0] == "source":
                args = [_jinja_call_arg_name(arg) for arg in node.args]
                if args and all(arg for arg in args):
                    source = ".".join(args)
                    if not self._is_disabled_source(source):
                        dependencies.sources.append(source)
            elif call_name[0] == "ref":
                args = [_jinja_call_arg_name(arg) for arg in node.args]
                if args and all(arg for arg in args):
                    ref = ".".join(args)
                    if not self._is_disabled_ref(ref):
                        dependencies.refs.append(ref)

        return dependencies

def _config(node: t.Union[ManifestNode, SourceDefinition]) -> t.Dict[str, t.Any]:
    return node.config.to_dict()

def _macro_references(
    manifest: Manifest, node: t.Union[ManifestNode, Macro]
) -> t.Set[MacroReference]:
    result = set()
    for macro_node_id in node.depends_on.macros:
        macro_node = manifest.macros[macro_node_id]
        macro_name = macro_node.name
        macro_package = (
            macro_node.package_name if macro_node.package_name != node.package_name else None
        )
        result.add(MacroReference(package=macro_package, name=macro_name))
    return result

def _refs(node: ManifestNode) -> t.Set[str]:
    if DBT_VERSION >= (1, 5):
        return {f"{r.package}.{r.name}" if r.package else r.name for r in node.refs}  # type: ignore
    else:
        return {".".join(r) for r in node.refs}  # type: ignore

def _sources(node: ManifestNode) -> t.Set[str]:
    return {".".join(s) for s in node.sources}

def _model_node_id(model_name: str, package: str) -> str:
    return f"model.{package}.{model_name}"

def _test_model(node: ManifestNode) -> t.Optional[str]:
    attached_node = getattr(node, "attached_node", None)
    if attached_node:
        pieces = attached_node.split(".")
        return pieces[-1] if pieces[0] in ["model", "seed"] else None

    key_name = getattr(node, "file_key_name", None)
    if key_name:
        pieces = key_name.split(".")
        return pieces[-1] if pieces[0] in ["models", "seeds"] else None

    return None

def _node_base_config(node: ManifestNode) -> t.Dict[str, t.Any]:
    node_dict = node.to_dict()
    node_dict.pop("database", None)  # picked up from the `config` attribute
    return {
        **_config(node),
        **node_dict,
        "path": Path(node.original_file_path),
    }

def _convert_jinja_test_to_macro(test_jinja: str) -> str:
    TEST_TAG_REGEX = "\s*{%\s*test\s+"
    ENDTEST_REGEX = "{%\s*endtest\s*%}"
    match = re.match(TEST_TAG_REGEX, test_jinja)
    if not match:
        # already a macro
        return test_jinja

    macro = "{% macro test_" + test_jinja[match.span()[-1] :]
    return re.sub(ENDTEST_REGEX, "{% endmacro %}", macro)

def _jinja_call_arg_name(node: nodes.Node) -> str:
    if isinstance(node, nodes.Const):
        return node.value
    return ""


"""
第一种，使用itertools.permutations()
在乎顺序，且选择过的元素之后将不在考虑范围内
"""
from itertools import permutations

items = ['a', 'b', 'c']
for p in permutations(items):
    print(p)

"""
output
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
"""

# 想得到较短长度的全排列
for p in permutations(items, 2):
    print(p)

"""
output
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')
"""

"""
第二种，使用itertools.combinations()函数
选择过的元素之后将不在考虑范围内，且不考虑顺序问题
"""
from itertools import combinations

for p in combinations(items, 3):
    print(p)

"""
output
('a', 'b', 'c')
"""

for p in combinations(items, 2):
    print(p)

"""
output
('a', 'b')
('a', 'c')
('b', 'c')
"""

"""
上面两种方法，被选择过的元素将从考虑范围内删去
使用itertools.combinations_with_replacement函数可以解除这一限制
"""
from itertools import combinations_with_replacement

for p in combinations_with_replacement(items, 3):
    print(p)

"""
output
('a', 'a', 'a')
('a', 'a', 'b')
('a', 'a', 'c')
('a', 'b', 'b')
('a', 'b', 'c')
('a', 'c', 'c')
('b', 'b', 'b')
('b', 'b', 'c')
('b', 'c', 'c')
('c', 'c', 'c')
"""

for p in combinations_with_replacement(items, 2):
    print(p)

"""
output
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'b')
('b', 'c')
('c', 'c')
"""

迭代器和生成器/迭代所有可能的组合和排列_P127.py

迭代所有可能的组合和排列_P127.py


# 대각선 확인
def check(x):
    for i in range(x):
        if abs(graph[x] - graph[i]) == (x-i):
            return False
    return True

def DFS(x):
    global cnt
    if x == N:
        cnt += 1
        return

    for i in range(N):
        if visited[i] == 0:
            graph[x] = i

            if check(x):
                visited[i] = True
                DFS(x+1)
                visited[i] = False

N = int(input())

cnt = 0
visited = [0 for _ in range(N)]
graph = [0 for _ in range(N)]

DFS(0)
print(cnt)


from marshmallow import fields as ma_fields, post_load
from marshmallow_annotations import AnnotationSchema

from ..models import (
    Download,
    DownloadAttempt,
    Pagination,
    PaginationData,
    Playlist,
    Video,
)
from .ytdl_options import YtdlDownloadOptions

class PlaylistSchema(AnnotationSchema):
    videos = ma_fields.List(ma_fields.Nested("VideoSchema", exclude=("playlists",)))

    class Meta:
        target = Playlist
        register_as_scheme = True

class VideoSchema(AnnotationSchema):
    downloads = ma_fields.List(
        ma_fields.Nested("DownloadSchema", exclude=("video", "video_id"))
    )

    class Meta:
        target = Video
        register_as_scheme = True

        class Fields:
            playlists = {"exclude": ("videos",)}

class DownloadSchema(AnnotationSchema):
    _IGNORED_ATTEMPT_FILEDS = ("download", "download_id", "video", "video_id")

    attempts = ma_fields.List(
        ma_fields.Nested("DownloadAttemptSchema", exclude=_IGNORED_ATTEMPT_FILEDS)
    )
    latest_attempt = ma_fields.Nested(
        "DownloadAttemptSchema", exclude=_IGNORED_ATTEMPT_FILEDS
    )

    class Meta:
        target = Download
        register_as_scheme = True

        class Fields:
            video = {"exclude": ("downloads",)}

class DownloadAttemptSchema(AnnotationSchema):
    class Meta:
        target = DownloadAttempt
        register_as_scheme = True

        class Fields:
            download = {"exclude": ("attempts", "latest_attempt", "video", "video_id")}
            video = {"exclude": ("downloads",)}

class PaginationDataSchema(AnnotationSchema):
    class Meta:
        target = PaginationData
        register_as_scheme = True

class YtdlDownloadOptionsSchema(AnnotationSchema):
    class Meta:
        target = YtdlDownloadOptions
        register_as_scheme = True

    @post_load
    def into_instance(self, data, **kwargs):
        opts = YtdlDownloadOptions()
        opts.download_archive = data["download_archive"]
        opts.outtmpl = data["outtmpl"]
        return opts


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

from django.shortcuts import render
from django.http import HttpResponse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from ..models import Exam, Exam_result
from ..util import get_current_group

# Views for Groups

def exams_list(request):
    # check if we need to show results for only one group of students
    current_group = get_current_group(request)
    if current_group:
        exams = Exam.objects.filter(group_examing=current_group)
    else:
        # otherwise show results for all groups
        exams = Exam.objects.all()

    # try to order exams list
    order_by = request.GET.get('order_by', '')
    if order_by in ('date', 'subject', 'group_examing'):
        exams = exams.order_by(order_by)
        if request.GET.get('reverse', '') == '1':
            exams = exams.reverse()

    return render(request, 'students/exams_list.html', {'content': exams})

def exam_result(request, sid):
    results = Exam_result.objects.all().filter(exam_res=sid)
    exam = Exam.objects.get(id=sid)

    # try to order students list in group
    order_by = request.GET.get('order_by', '')
    if order_by == 'last_name':
        results = results.order_by('student_res')
        if request.GET.get('reverse', '') == '1':
            results = results.reverse()

    return render(request, 'students/exam_result.html',
                  {'results': results, 'exam': exam})

def exam_add(request):
    return HttpResponse('
Exam Add Form
')


x = int(input('Que valor você quer sacar? R$'))
t = x
c = 50
tc = 0
while True:
    if t >= c:
        t -= c
        tc += 1
    else:
        if tc > 0:
            print(f'Total de {tc} cédulas de R${c}')
        if c == 50:
            c = 20
        elif c == 20:
            c = 10
        elif c == 10:
            c = 1
        tc = 0
        if t == 0:
            break
print('Volte sempre ao Banco!')


def extractEndlessfantasyTranslations(item):
	"""
	Parser for 'EndlessFantasy Translations'
	"""
	vol, chp, frag, postfix = extractVolChapterFragmentPostfix(item['title'])
	if not (chp or vol) or 'preview' in item['title'].lower():
		return None

	if 'fanfic' in item['title'].lower():
		return None

	titlemap = [
		('My Father-in-law is Lu Bu – ',                                        'My Father-in-law is Lu Bu',                                      'translated'),
		('My Father-in-law is Lu Bu Chapter',                                   'My Father-in-law is Lu Bu',                 'translated'),
		('Masked Knight – ',                                                    'Masked Knight',                                                  'translated'),
		('Masked Knight Chapter',                                               'Masked Knight',                                                  'translated'),
		('Epoch of Twilight – Chapter',                                         'Epoch of Twilight',                                              'translated'),

		('Age of Cosmic Exploration – Chapter',                                 'Age of Cosmic Exploration',                                      'translated'),
		('MMORPG: Rebirth of the Legendary Guardian- Chapter',                  'MMORPG: Rebirth of the Legendary Guardian',                      'translated'),
		('MMORPG: Rebirth of the Legendary Guardian – Chapter',                 'MMORPG: Rebirth of the Legendary Guardian',                      'translated'),
		('MMORPG: Rebirth of the Legendary Guardian : Chapter',                 'MMORPG: Rebirth of the Legendary Guardian',                      'translated'),

		('Full Marks Hidden Marriage: Pick Up a Son, Get a Free Husband –',     'Full Marks Hidden Marriage: Pick Up a Son, Get a Free Husband',  'translated'),
		('I Am Supreme – Chapter',                                              'I am Supreme',                                                   'translated'),
		('Pursuit of the Truth –',                                              'Pursuit of the Truth',                                           'translated'),

	]

	for titlecomponent, name, tl_type in titlemap:
		if titlecomponent.lower() in item['title'].lower():
			return buildReleaseMessageWithType(item, name, vol, chp, frag=frag, postfix=postfix, tl_type=tl_type)

	return False


#!/home/nate/python/python_env/bin/python3
"""Guess the Number"""
import random
from os import system, name

def mode_check():
    # This defines either easy or hard mode.
    mode = input(
        f"\nHello, {name}!\nWelcome to Guess the Number!! Would you like easy mode or hard mode?\n Easy mode consists of 10 numbers from 1 - 20. You will have 10 attempts.\n Hard mode consists of 20 numbers from 1 - 40. You will have 20 attempts.\n"
    )
    easy = ["easy", "Easy"]
    hard = ["hard", "Hard"]
    mode_choice = f"\nYou have selected {mode} mode.\n Good luck, {name}!!\n"
    # Generate list for Easy Mode
    if mode in easy:
        list = [random.randint(1, 30) for i in range(10)]
        print(mode_choice)
        return list
    # Generate list for Hard Mode
    elif mode in hard:
        list = [random.randint(1, 40) for i in range(20)]
        print(mode_choice)
        return list
    else:
        return mode_check()

def guess_the_number():
    # Determines which function to choose next by examining the length of the list that is created.
    if len(list) == 10:
        easy_counter()
    else:
        hard_counter()

def easy_counter():
    # This function outputs the list of numbers to choose from and tells you how many turns you have left based on a counter.
    global counter
    # This chooses the winning number based on a random selection of an index position.
    correct_num = list[(random.randint(0, 9))]
    clear()
    while counter < 11:
        winner = f"\nYou win! {correct_num} is the correct number!"
        incorrect = f"\nIncorrect. Please try again. You have {10 - counter} attempts remaining.\n"
        lose = "You lose. Goodbye"
        guess = input(
            f"Take a guess!\nYou have a total of {len(list)} numbers to choose from.\n\nYour choices are:\n{list}\n"
        )
        try:
            # This verifies that the input is a whole number and not a float or str.
            int(guess)
        except ValueError:
            counter += 1
            print(incorrect)
            if counter == 10:
                print(lose)
                break
            # continue
        guess = int(guess)
        if guess is correct_num:
            print(winner)
            break
        if counter == 10:
            clear()
            print(lose)
            break
        elif guess != correct_num:
            clear()
            counter += 1
            print(incorrect)

def hard_counter():
    # This function outputs the list of numbers to choose from and tells you how many turns you have left based on a counter.
    global counter
    # This chooses the winning number based on a random selection of an index position.
    correct_num = list[(random.randint(0, 19))]
    clear()
    while counter < 21:
        winner = f"\nYou win! {correct_num} is the correct number!"
        incorrect = f"\nIncorrect. Please try again. You have {20 - counter} attempts remaining.\n"
        lose = "You lose. Goodbye"
        guess = input(
            f"Take a guess!\nYou have a total of {len(list)} numbers to choose from.\n\nYour choices are:\n{list}\n"
        )
        try:
            int(guess)
        except ValueError:
            counter += 1
            print(incorrect)
            if counter == 20:
                print(lose)
                break
        guess = int(guess)
        if guess is correct_num:
            print(winner)
            break
        if counter == 20:
            clear()
            print(lose)
            break
        elif guess != correct_num:
            clear()
            counter += 1
            print(incorrect)

def clear():
    # for windows
    if name == "nt":
        _ = system("cls")

    # for mac and linux(here, os.name is 'posix')
    else:
        _ = system("clear")

name = (input(f"\nHello, what" "s your name?\n")).capitalize()
counter = 0
list = mode_check()
guess_the_number()


import inspect
import os
from itertools import dropwhile

import nbformat
import sys
import six

from parade.core.task import ETLTask, SingleSourceETLTask
from parade.command.notebook import NotebookCommand
from parade.command import ParadeCommand

def get_function_body(func):
    source_lines = inspect.getsourcelines(func)[0]
    source_lines = dropwhile(lambda x: x.startswith('@'), source_lines)
    def_line = next(source_lines).strip()
    if def_line.startswith('def ') and def_line.endswith(':'):
        # Handle functions that are not one-liners
        first_line = next(source_lines)
        # Find the indentation of the first line
        indentation = len(first_line) - len(first_line.lstrip())
        return [first_line[indentation:]] + [line[indentation:] for line in source_lines]
    else:
        # Handle single line functions
        return [def_line.rsplit(':')[-1].strip()]

def get_imports(obj):
    source_lines = inspect.getsourcelines(sys.modules[obj.__module__])[0]
    source_lines = dropwhile(lambda x: x.startswith('@'), source_lines)
    import_lines = filter(lambda x: x.startswith('import ') or x.startswith('from '), source_lines)
    return list(import_lines)

class DebugCommand(ParadeCommand):
    requires_workspace = True

    def run_internal(self, context, **kwargs):
        NotebookCommand.init_jupyter(context)

        task = context.get_task(kwargs.get('task'), task_class=ETLTask)
        import_lines = get_imports(task)
        if isinstance(task, SingleSourceETLTask):
            source = task.source
            if isinstance(source, six.string_types):
                body_lines = ['source = """{}"""\n'.format(source)]
            else:
                body_lines = ['source = {}\n'.format({'a': 1})]

            if task.is_source_query:
                body_lines.append('context.load_query(source, conn=\'{}\')\n'.format(task.source_conn))
            else:
                body_lines.append('context.load(source, conn=\'{}\')\n'.format(task.source_conn))

        else:
            body_lines = get_function_body(task.execute_internal)
            body_lines = list(map(lambda x: x.replace('return ', ''), body_lines))
        source_lines = import_lines + ['\n\n'] + body_lines

        notebook = nbformat.v4.new_notebook()
        cell = nbformat.v4.new_code_cell(source=source_lines)
        notebook.cells.append(cell)

        ipynb_path = os.path.join(context.workdir, '.jupyter', 'notebook', task.name + '.ipynb')
        with open(ipynb_path, 'wb') as f:
            ipy_content = nbformat.writes(notebook)
            f.write(ipy_content.encode())
            f.flush()

        os.system('jupyter notebook {}.ipynb'.format(task.name))

    def short_desc(self):
        return 'debug a specified task in notebook'

    def config_parser(self, parser):
        parser.add_argument('task', help='the task to debug')


from polar_convert.constants import (
    VALID_HEMISPHERES,
    VALID_GRID_SIZES,
)

def validate_grid_size(grid_size):
    if grid_size not in VALID_GRID_SIZES:
        raise ValueError(
            f'Got `grid_size` of {grid_size} but expected one of {VALID_GRID_SIZES}'
        )

    return grid_size

def validate_hemisphere(hemisphere):
    if not isinstance(hemisphere, str) or hemisphere.lower() not in VALID_HEMISPHERES:
        raise ValueError(
            f'Got `hemisphere` of {hemisphere} but expected one of {VALID_HEMISPHERES}'
        )

    return hemisphere.lower()