import collections.abc
import contextlib
import io
import typing
from timeit import default_timer
from typing import Collection

import urllib3.connectionpool
import wrapt

from opentelemetry import context

# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined.
from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.urllib3.package import _instruments
from opentelemetry.instrumentation.urllib3.version import __version__
from opentelemetry.instrumentation.utils import (
    _SUPPRESS_INSTRUMENTATION_KEY,
    http_status_to_status_code,
    unwrap,
)
from opentelemetry.metrics import Histogram, get_meter
from opentelemetry.propagate import inject
from opentelemetry.semconv.metrics import MetricInstruments
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace import Span, SpanKind, Tracer, get_tracer
from opentelemetry.trace.status import Status
from opentelemetry.util.http import (
    ExcludeList,
    get_excluded_urls,
    parse_excluded_urls,
)
from opentelemetry.util.http.httplib import set_ip_on_next_http_connection

_excluded_urls_from_env = get_excluded_urls("URLLIB3")

_UrlFilterT = typing.Optional[typing.Callable[[str], str]]
_RequestHookT = typing.Optional[
    typing.Callable[
        [
            Span,
            urllib3.connectionpool.HTTPConnectionPool,
            typing.Dict,
            typing.Optional[str],
        ],
        None,
    ]
]
_ResponseHookT = typing.Optional[
    typing.Callable[
        [
            Span,
            urllib3.connectionpool.HTTPConnectionPool,
            urllib3.response.HTTPResponse,
        ],
        None,
    ]
]

_URL_OPEN_ARG_TO_INDEX_MAPPING = {
    "method": 0,
    "url": 1,
    "body": 2,
}

class URLLib3Instrumentor(BaseInstrumentor):
    def instrumentation_dependencies(self) -> Collection[str]:
        return _instruments

    def _instrument(self, **kwargs):
        """Instruments the urllib3 module

        Args:
            **kwargs: Optional arguments
                ``tracer_provider``: a TracerProvider, defaults to global.
                ``request_hook``: An optional callback that is invoked right after a span is created.
                ``response_hook``: An optional callback which is invoked right before the span is finished processing a response.
                ``url_filter``: A callback to process the requested URL prior
                    to adding it as a span attribute.
                ``excluded_urls``: A string containing a comma-delimited
                    list of regexes used to exclude URLs from tracking
        """
        tracer_provider = kwargs.get("tracer_provider")
        tracer = get_tracer(
            __name__,
            __version__,
            tracer_provider,
            schema_url="https://opentelemetry.io/schemas/1.11.0",
        )

        excluded_urls = kwargs.get("excluded_urls")

        meter_provider = kwargs.get("meter_provider")
        meter = get_meter(
            __name__,
            __version__,
            meter_provider,
            schema_url="https://opentelemetry.io/schemas/1.11.0",
        )

        duration_histogram = meter.create_histogram(
            name=MetricInstruments.HTTP_CLIENT_DURATION,
            unit="ms",
            description="Measures the duration of outbound HTTP requests.",
        )
        request_size_histogram = meter.create_histogram(
            name=MetricInstruments.HTTP_CLIENT_REQUEST_SIZE,
            unit="By",
            description="Measures the size of HTTP request messages.",
        )
        response_size_histogram = meter.create_histogram(
            name=MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE,
            unit="By",
            description="Measures the size of HTTP response messages.",
        )

        _instrument(
            tracer,
            duration_histogram,
            request_size_histogram,
            response_size_histogram,
            request_hook=kwargs.get("request_hook"),
            response_hook=kwargs.get("response_hook"),
            url_filter=kwargs.get("url_filter"),
            excluded_urls=_excluded_urls_from_env
            if excluded_urls is None
            else parse_excluded_urls(excluded_urls),
        )

    def _uninstrument(self, **kwargs):
        _uninstrument()

def _instrument(
    tracer: Tracer,
    duration_histogram: Histogram,
    request_size_histogram: Histogram,
    response_size_histogram: Histogram,
    request_hook: _RequestHookT = None,
    response_hook: _ResponseHookT = None,
    url_filter: _UrlFilterT = None,
    excluded_urls: ExcludeList = None,
):
    def instrumented_urlopen(wrapped, instance, args, kwargs):
        if _is_instrumentation_suppressed():
            return wrapped(*args, **kwargs)

        url = _get_url(instance, args, kwargs, url_filter)
        if excluded_urls and excluded_urls.url_disabled(url):
            return wrapped(*args, **kwargs)

        method = _get_url_open_arg("method", args, kwargs).upper()
        headers = _prepare_headers(kwargs)
        body = _get_url_open_arg("body", args, kwargs)

        span_name = method.strip()
        span_attributes = {
            SpanAttributes.HTTP_METHOD: method,
            SpanAttributes.HTTP_URL: url,
        }

        with tracer.start_as_current_span(
            span_name, kind=SpanKind.CLIENT, attributes=span_attributes
        ) as span, set_ip_on_next_http_connection(span):
            if callable(request_hook):
                request_hook(span, instance, headers, body)
            inject(headers)

            with _suppress_further_instrumentation():
                start_time = default_timer()
                response = wrapped(*args, **kwargs)
                elapsed_time = round((default_timer() - start_time) * 1000)

            _apply_response(span, response)
            if callable(response_hook):
                response_hook(span, instance, response)

            request_size = _get_body_size(body)
            response_size = int(response.headers.get("Content-Length", 0))

            metric_attributes = _create_metric_attributes(
                instance, response, method
            )

            duration_histogram.record(
                elapsed_time, attributes=metric_attributes
            )
            if request_size is not None:
                request_size_histogram.record(
                    request_size, attributes=metric_attributes
                )
            response_size_histogram.record(
                response_size, attributes=metric_attributes
            )

            return response

    wrapt.wrap_function_wrapper(
        urllib3.connectionpool.HTTPConnectionPool,
        "urlopen",
        instrumented_urlopen,
    )

def _get_url_open_arg(name: str, args: typing.List, kwargs: typing.Mapping):
    arg_idx = _URL_OPEN_ARG_TO_INDEX_MAPPING.get(name)
    if arg_idx is not None:
        try:
            return args[arg_idx]
        except IndexError:
            pass
    return kwargs.get(name)

def _get_url(
    instance: urllib3.connectionpool.HTTPConnectionPool,
    args: typing.List,
    kwargs: typing.Mapping,
    url_filter: _UrlFilterT,
) -> str:
    url_or_path = _get_url_open_arg("url", args, kwargs)
    if not url_or_path.startswith("/"):
        url = url_or_path
    else:
        url = instance.scheme + "://" + instance.host
        if _should_append_port(instance.scheme, instance.port):
            url += ":" + str(instance.port)
        url += url_or_path

    if url_filter:
        return url_filter(url)
    return url

def _get_body_size(body: object) -> typing.Optional[int]:
    if body is None:
        return 0
    if isinstance(body, collections.abc.Sized):
        return len(body)
    if isinstance(body, io.BytesIO):
        return body.getbuffer().nbytes
    return None

def _should_append_port(scheme: str, port: typing.Optional[int]) -> bool:
    if not port:
        return False
    if scheme == "http" and port == 80:
        return False
    if scheme == "https" and port == 443:
        return False
    return True

def _prepare_headers(urlopen_kwargs: typing.Dict) -> typing.Dict:
    headers = urlopen_kwargs.get("headers")

    # avoid modifying original headers on inject
    headers = headers.copy() if headers is not None else {}
    urlopen_kwargs["headers"] = headers

    return headers

def _apply_response(span: Span, response: urllib3.response.HTTPResponse):
    if not span.is_recording():
        return

    span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, response.status)
    span.set_status(Status(http_status_to_status_code(response.status)))

def _is_instrumentation_suppressed() -> bool:
    return bool(
        context.get_value(_SUPPRESS_INSTRUMENTATION_KEY)
        or context.get_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY)
    )

def _create_metric_attributes(
    instance: urllib3.connectionpool.HTTPConnectionPool,
    response: urllib3.response.HTTPResponse,
    method: str,
) -> dict:
    metric_attributes = {
        SpanAttributes.HTTP_METHOD: method,
        SpanAttributes.HTTP_HOST: instance.host,
        SpanAttributes.HTTP_SCHEME: instance.scheme,
        SpanAttributes.HTTP_STATUS_CODE: response.status,
        SpanAttributes.NET_PEER_NAME: instance.host,
        SpanAttributes.NET_PEER_PORT: instance.port,
    }

    version = getattr(response, "version")
    if version:
        metric_attributes[SpanAttributes.HTTP_FLAVOR] = (
            "1.1" if version == 11 else "1.0"
        )

    return metric_attributes

@contextlib.contextmanager
def _suppress_further_instrumentation():
    token = context.attach(
        context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True)
    )
    try:
        yield
    finally:
        context.detach(token)

def _uninstrument():
    unwrap(urllib3.connectionpool.HTTPConnectionPool, "urlopen")


from django.shortcuts import render, redirect
from django.contrib.auth import login
from django.contrib.auth.forms import UserCreationForm

def logged_out(request):
    """Pizzas homepage"""
    return render(request, 'users/logged_out.html')

def register(request):
    """"New user registration"""
    if request.method != 'POST':
        form = UserCreationForm()
    else:
        form = UserCreationForm(data=request.POST)
        if form.is_valid():
            new_user = form.save()
            # Log in system
            login(request, new_user)
            return redirect('pizzas:pizzas')

    context = {'form': form}
    return render(request, 'users/register.html', context)


import requests
from bs4 import BeautifulSoup
import sys

# recebendo valor para a variavel atravez de argumentos. ex: python recon.py http://luci-lua.tk/
url = sys.argv[1]
tag = input("tag: ")

req = requests.get(str(url))
print("Status: ", req.status_code)

html  = BeautifulSoup(req.content, 'html.parser')

def result():
    print("Headers: ", req.headers)

    # print (html.prettify())
    # pretiffy para deixar HTML dentro do escopo

    # Procurar tudo o que for TAG
    forTag = html.find_all(tag)

    print(tag, ":", forTag)

if (req.status_code == 200):
    result()
else:
    print("Nada pode ser encontrado")


import asyncio
import datetime
import json
import random

import requests

url = "http://10.192.0.2:30260/api/metrics/"

for i in range(25000):
    data = {
        "run_id": 1,
        "name": "test_metric @ {}".format(0),  # random.randint(0, 7)),
        "cumulative": False,
        "date": str(datetime.datetime.now()),
        "value": str(random.random() * 100),
        "metadata": {"epoch": i, "rank": 0},
    }

    requests.post(url, data=data)

    if i % 1000 == 0:
        print(i)

# async def main():
#     loop = asyncio.get_event_loop()
#     futures = [
#         loop.run_in_executor(
#             None,
#             requests.post,
#             url,
#             {
#                 "run_id": 3,
#                 "name": 'test_metric @ 0',
#                 "cumulative": False,
#                 "date": str(datetime.datetime.now()),
#                 "value": str(random.random() * 100),
#                 "metadata": {'epoch': i, 'rank': 0}
#                 }
#         )
#         for i in range(100000)
#     ]
#     for response in await asyncio.gather(*futures):
#         pass

# loop = asyncio.get_event_loop()
# loop.run_until_complete(main())

# async def main():
#     loop = asyncio.get_event_loop()
#     futures = [
#         loop.run_in_executor(
#             None,
#             requests.get,
#             "http://10.192.0.2:31635/api/metrics/3/?since=1970-01-01T00%3A00%3A00.000Z&metric_type=run&summarize=1000"
#         )
#         for i in range(100)
#     ]
#     for response in await asyncio.gather(*futures):
#         pass

# loop = asyncio.get_event_loop()
# loop.run_until_complete(main())


import curses

from MegaEditor import MegaEditor

class MegaTab:

    def __init__(self, maxy, maxx, window, id, title, pos_x, is_active = False):
        self.id = id
        index = title.rfind("/")
        if index == -1:
            self.title = title
        else:
            self.title = title[index+1:]
        self.pos_x = pos_x
        self.maxy = maxy
        self.maxx = maxx
        self.window = window
        self.is_active = is_active
        self.editor = MegaEditor(id, self.maxy-6, self.maxx-2, title)
        self.editor_panel = curses.panel.new_panel(self.editor.window)
        self.is_dirty = False

    def draw(self):
        if self.is_active:
            if self.is_dirty:
                self.window.addstr(1, self.pos_x, ""+self.title+"* ", curses.A_REVERSE)
            else:
                self.window.addstr(1, self.pos_x, " "+self.title+" ", curses.A_REVERSE)
        else:
            if self.is_dirty:
                self.window.addstr(1, self.pos_x, ""+self.title+"* ", curses.A_NORMAL)
            else:
                self.window.addstr(1, self.pos_x, " "+self.title+" ", curses.A_NORMAL)

    def activate(self):
        self.is_active = True
        self.editor_panel.top()

    def deactivate(self):
        self.is_active = False


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib import cm
from matplotlib.colors import Normalize
from sklearn import preprocessing
from sklearn.linear_model import LogisticRegression
from sklearn.mixture import GaussianMixture

df = pd.read_csv("data/iris.csv")
x = df.drop('X_3', axis=1)
x = x.drop('X_4', axis=1)
x = x.drop('Y', axis=1)
y = df.Y
min_max_scaler = preprocessing.MinMaxScaler()
x_train_scaled = min_max_scaler.fit_transform(x)

lr = LogisticRegression()
lr.fit(x_train_scaled, y)

gmm = GaussianMixture(n_components=2, covariance_type='full', random_state=0)
gmm.fit(x_train_scaled)

x_test = []
y_test = []
f1 = []
f2 = []
f3 = []
for i in np.arange(0, 1, 0.02):
    for j in np.arange(0, 1, 0.02):
        f1.append(abs(lr.predict_proba([[i, j]])[:, 1] - 0.5))
        f2.append(np.exp(gmm.score_samples([[i, j]])))
        x_test.append(i)
        y_test.append(j)
f1 = min_max_scaler.fit_transform(f1)
f2 = min_max_scaler.fit_transform(f2)
for i in range(len(f2)):
    f3.append(f1[i] + f2[i])
    print("(" + str(x_test[i]) + " , " + str(y_test[i]) + ")")
    print("LR Prob:" + str(f1[i]))
    print("GMM Score:" + str(f2[i]))
    print("_____________________________")

f3 = f1
minima = np.min(f3)
maxima = np.max(f3)
norm = Normalize(vmin=minima, vmax=maxima, clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap=cm.RdYlGn)

color = []
for v in f3:
    color.append(mapper.to_rgba(v))

fig, axs = plt.subplots(1, 2, figsize=(10, 6))

for idx in range(len(x_test)):
    axs[0].scatter(x_test[idx], y_test[idx], color=color[idx], s=2)

colors = np.where(y == 'Iris-setosa', 'r', 'b')
axs[1].scatter(x_train_scaled[:, 0], x_train_scaled[:, 1], marker='o', c=colors)
axs[0].set_aspect('equal', adjustable='box')
axs[1].set_aspect('equal', adjustable='box')
plt.show()


#!/usr/bin/env python
# license removed for brevity
import os

import rospy
from std_msgs.msg import String
from sensor_msgs.msg import Image
from vision_msgs.msg import Detection2DArray
import argparse
import glob
import cv2
from cv_bridge import CvBridge

parser = argparse.ArgumentParser(description='Evaluator.')
parser.add_argument('-f', '--folder', type=str, required=True,
                    help='Path to folder with the images.')
parser.add_argument('-of', '--output_folder', type=str, required=True,
                    help='Path to the folder in which to put the txt files.')
args = parser.parse_args()
folder = args.folder

# setup ros stuff
pub = rospy.Publisher('/image', Image, queue_size=1)
rospy.init_node('evaluator')
bridge = CvBridge()

rospy.sleep(1)

# list all images in folder
image_paths = glob.glob(folder + '/*.jpg')

for image_path in image_paths:

    print('Analysing image ' + image_path)
    image = cv2.imread(image_path)

    image_msg = bridge.cv2_to_imgmsg(image, encoding='bgr8')

    pub.publish(image_msg)
    rospy.sleep(0.5)

    cv2.imshow('evaluator', image)
    cv2.waitKey(50)

    print('waiting for detections message ... ')
    detections_msg = rospy.wait_for_message('/detections', Detection2DArray)
    print('detections received')

    text_filename = os.path.basename(image_path)
    text_filename = args.output_folder + '/' + os.path.splitext(text_filename)[0] + '.txt'
    print(text_filename)
    f = open(text_filename, "w")

    print('There are ' + str(len(detections_msg.detections)) + ' detections')
    for detection_idx, detection in enumerate(detections_msg.detections):
        # print('Analysing detection ' + str(detection_idx))

        w = detection.bbox.size_x
        h = detection.bbox.size_y
        x = detection.bbox.center.x - int(w / 2)
        y = detection.bbox.center.y - int(h / 2)

        if detection.results[0].id == 1:
            category = 'Tomato'
        else:
            category = 'Unknown'

        score = detection.results[0].score

        f.write(category + ' ' + str(score) + ' ' + str(int(x)) + ' '
                + str(int(y)) + ' ' + str(int(w)) + ' ' + str(int(h)) + '\n')

    print('Press a key for next image ... ')
    cv2.waitKey(5)
    f.close()

#     #!/usr/bin/env python
# import rospy
# from std_msgs.msg import String
#
# def callback(data):
#     rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
#
# def listener():
#
#     # In ROS, nodes are uniquely named. If two nodes with the same
#     # name are launched, the previous one is kicked off. The
#     # anonymous=True flag means that rospy will choose a unique
#     # name for our 'listener' node so that multiple listeners can
#     # run simultaneously.
#     rospy.init_node('listener', anonymous=True)
#
#     rospy.Subscriber("chatter", String, callback)
#
#     # spin() simply keeps python from exiting until this node is stopped
#     rospy.spin()
#
# if __name__ == '__main__':
#     listener()


import json
from lxml import etree
from urllib.parse import urlparse
from datetime import datetime

class MSRecordParser:
    def __init__(self, xml):
        """
        Create a Metashare record object.

        :param xml: an lxml object, representing a CMDI record
        """
        self.xml = xml

    def _get_language_contents(self, xpath):
        """
        Retrieve the content from XML tree and XPath expression for different language versions.

        :param xpath: The XPath expression to select the desired trees.
        :return: A dictionary of content for different language versions.
        """
        result = {}

        languages = ["en", "fi", "und"]

        for lang in languages:
            query = self.xml.xpath(
                f"{xpath}[@lang='{lang}']/text()",
                namespaces={"info": "http://www.ilsp.gr/META-XMLSchema"},
            )
            if query:
                result[lang] = query[0].strip()

        return result

    def _get_text_xpath(self, xpath):
        """
        Retrieves text content of the first element from given XPath expression.

        :param xpath: The XPath expression to select the desired trees.
        :return: The text content of the selected element.

        """
        return self.xml.xpath(
            xpath, namespaces={"info": "http://www.ilsp.gr/META-XMLSchema"}
        )[0]

    def get_identifier(self, xpath):
        """
        Retrieves the urn of the given XPath's url.
        """
        identifier_url = self._get_text_xpath(xpath).strip()
        netloc, path = urlparse(
            identifier_url).netloc, urlparse(identifier_url).path
        return netloc + path

    def _get_date(self, xpath):
        """
        Retrieves the date of the given XPath and returns it  appropriate date-time format.

        """
        date_str = self._get_text_xpath(xpath)
        if date_str:
            datetime_obj = datetime.strptime(date_str, "%Y-%m-%d")
            formatted_date_str = datetime_obj.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
            return formatted_date_str
        else:
            raise ValueError("No date found")

    def check_pid_exists(self):
        """
        Only records with PIDs are relevant.
        """
        urn = self.xml.xpath(
            "//info:identificationInfo/info:identifier",
            namespaces={"info": "http://www.ilsp.gr/META-XMLSchema"},
        )

        return bool(urn)

    def _get_list_of_licenses(self):
        """
        Retrieves all licenseInfo elements.
        """
        license_elements_list = self.xml.xpath(
            "//info:distributionInfo/info:licenceInfo",
            namespaces={"info": "http://www.ilsp.gr/META-XMLSchema"},
        )
        if license_elements_list:
            return license_elements_list

    def check_resourcetype_corpus(self):
        """
        Helper method to only retrieve "corpus" records.
        """
        resourcetype = self._get_text_xpath("//info:resourceType/text()")
        if resourcetype == "corpus":
            return True

    def _get_license_url_from_documentation(self):
        """
        Retrieves the license url.
        """
        doc_elements = self.xml.xpath(
            "//info:resourceDocumentationInfo/info:documentation",
            namespaces={"info": "http://www.ilsp.gr/META-XMLSchema"},
        )

        for doc_element in doc_elements:
            doc_unstruct_element = doc_element.xpath(
                "info:documentUnstructured/text()",
                namespaces={"info": "http://www.ilsp.gr/META-XMLSchema"},
            )
            doc_info_elements = doc_element.xpath(
                "info:documentInfo",
                namespaces={"info": "http://www.ilsp.gr/META-XMLSchema"},
            )

            if doc_unstruct_element:
                doc_unstruct_text = doc_unstruct_element[0].lower()
                if "license:" in doc_unstruct_text or "license" in doc_unstruct_text:
                    license_urn = [
                        word
                        for word in doc_unstruct_text.split()
                        if word.startswith("http://urn.fi")
                    ]
                    if license_urn:
                        return license_urn[0]

            elif doc_info_elements:
                for doc_info_elem in doc_info_elements:
                    title_element = doc_info_elem.xpath(
                        "info:title[@lang='en']/text()",
                        namespaces={
                            "info": "http://www.ilsp.gr/META-XMLSchema"},
                    )
                    if title_element and "license" in title_element[0].lower():
                        license_urn = doc_info_elem.xpath(
                            "info:url/text()",
                            namespaces={
                                "info": "http://www.ilsp.gr/META-XMLSchema"},
                        )
                        if license_urn:
                            return license_urn[0]

        return None

    def _get_license_information(self, license_element, mapped_licenses_dict):
        """
        Retrieves the license and its possible url to a dictionary.
        """
        license_dict = {}

        license_text = license_element.xpath(
            "info:licence/text()",
            namespaces={"info": "http://www.ilsp.gr/META-XMLSchema"},
        )[0]

        if license_text in mapped_licenses_dict:
            license_dict["url"] = mapped_licenses_dict[license_text]
            custom_url = self._get_license_url_from_documentation()
            if custom_url:
                license_dict["custom_url"] = custom_url

        return license_dict

    def _get_access_type(self):
        """
        Retrieves and maps access type to a dictionary.
        """
        access_type_dict = {}

        availability = self.xml.xpath(
            "//info:distributionInfo/info:availability/text()",
            namespaces={"info": "http://www.ilsp.gr/META-XMLSchema"},
        )[0]
        if availability == "available-unrestrictedUse":
            access_type_dict[
                "url"
            ] = "http://uri.suomi.fi/codelist/fairdata/access_type/code/open"

        else:
            access_type_dict[
                "url"
            ] = "http://uri.suomi.fi/codelist/fairdata/access_type/code/restricted"

        return access_type_dict

    def _map_access_rights(self):
        """
        Retrieves and maps all license and access type information to a dictionary.
        """
        license_package = {}
        license_mappings = {
            "CLARIN_PUB": "http://uri.suomi.fi/codelist/fairdata/license/code/ClarinPUB-1.0",
            "CLARIN_ACA": "http://uri.suomi.fi/codelist/fairdata/license/code/ClarinACA-1.0",
            "CLARIN_ACA-NC": "http://uri.suomi.fi/codelist/fairdata/license/code/ClarinACA+NC-1.0",
            "CLARIN_RES": "http://uri.suomi.fi/codelist/fairdata/license/code/ClarinRES-1.0",
            "other": "http://uri.suomi.fi/codelist/fairdata/license/code/other",
            "underNegotiation": "http://uri.suomi.fi/codelist/fairdata/license/code/undernegotiation",
            "proprietary": "http://uri.suomi.fi/codelist/fairdata/license/code/other-closed",
            "CC-BY": "http://uri.suomi.fi/codelist/fairdata/license/code/CC-BY-1.0",
            "CC-BY-ND": "http://uri.suomi.fi/codelist/fairdata/license/code/CC-BY-ND-4.0",
            "CC-BY-NC": "http://uri.suomi.fi/codelist/fairdata/license/code/CC-BY-NC-2.0",
            "CC-BY-SA": "http://uri.suomi.fi/codelist/fairdata/license/code/CC-BY-SA-3.0",
            "CC-BY-NC-ND": "http://uri.suomi.fi/codelist/fairdata/license/code/CC-BY-NC-ND-4.0",
            "CC-BY-NC-SA": "http://uri.suomi.fi/codelist/fairdata/license/code/CC-BY-NC-SA-4.0",
            "CC-ZERO": "http://uri.suomi.fi/codelist/fairdata/license/code/CC0-1.0",
            "ApacheLicence_2.0": "http://uri.suomi.fi/codelist/fairdata/license/code/Apache-2.0",
        }

        license_elements_list = self._get_list_of_licenses()
        license_list = []

        if license_elements_list:
            for license_element in license_elements_list:
                license = self._get_license_information(
                    license_element, license_mappings
                )
                license_list.append(license)
        else:
            license = {"url": license_mappings["other"]}
            license_list.append(license)

        access_type = self._get_access_type()

        license_package["license"] = license_list
        license_package["access_type"] = access_type

        return license_package

    def to_dict(self):
        """
        Converts text and dictionaries to Metax compliant dictionary.
        """
        return {
            # data_catalog, language and field_of_science is dummy data until they are implemented later on
            "data_catalog": "urn:nbn:fi:att:data-catalog-kielipankki",
            "language": [{"url": "http://lexvo.org/id/iso639-3/fin"}],
            "field_of_science": [
                {
                    "url": "http://www.yso.fi/onto/okm-tieteenala/ta112",
                }
            ],
            "persistent_identifier": self.get_identifier(
                "//info:identificationInfo/info:identifier/text()"
            ),
            "title": self._get_language_contents("//info:resourceName"),
            "description": self._get_language_contents("//info:description"),
            "modified": self._get_date(
                "//info:metadataInfo/info:metadataLastDateUpdated/text()"
            ),
            "issued": self._get_date(
                "//info:metadataInfo/info:metadataCreationDate/text()"
            ),
            "access_rights": self._map_access_rights(),
        }


import pyrealsense2 as rs

def main():
    # Inicializa el dispositivo RealSense
    pipeline = rs.pipeline()
    config = rs.config()
    config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
    config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
    pipeline.start(config)

    # Obtiene la profundidad y la imagen RGB
    depth_frame = pipeline.wait_for_frames().get_depth_frame()
    color_frame = pipeline.wait_for_frames().get_color_frame()

    # Ajusta la densidad de puntos al máximo
    config.depth_points_density = rs.config.DepthPointsDensity.MAXIMUM

    # Transmite la imagen de profundidad en tiempo real
    depth_image = np.asanyarray(depth_frame.get_data())
    cv2.imshow("Depth", depth_image)
    cv2.waitKey(1)

    # Transmite la imagen RGB en tiempo real
    color_image = np.asanyarray(color_frame.get_data())
    cv2.imshow("Color", color_image)
    cv2.waitKey(1)

    # Detiene el dispositivo RealSense
    pipeline.stop()

if __name__ == "__main__":
    main()


import json
import os

from flask import Flask, request, redirect, render_template, session
from flask_bootstrap import Bootstrap

import spotipy
import spotipy.oauth2
from appdirs import user_cache_dir

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
app.secret_key = "YOUR OWN SECRET KEY HERE"
bootstrap = Bootstrap(app)

# Flask Parameters
CLIENT_SIDE_URL = "http://127.0.0.1"
PORT = 8000
REDIRECT_URI = "{}:{}/topartists".format(CLIENT_SIDE_URL, PORT)
SCOPE = ("user-top-read")

TEMPLATES_AUTO_RELOAD = True
SEND_FILE_MAX_AGE_DEFAULT = 0

@app.route("/")
def index():
    """Redirect user to Spotify login/auth."""
    sp_oauth = get_oauth()
    return redirect(sp_oauth.get_authorize_url())

@app.route("/toptracks")
def display_top_tracks():
    if request.args.get("code"):
        get_spotify(request.args["code"])

    """render users top tracks"""
    top_tracks_short = get_user_top_tracks(time_range='short_term')
    top_tracks_med = get_user_top_tracks(time_range='medium_term')
    top_tracks_long = get_user_top_tracks(time_range='long_term')
    session["track_names_long"] = [track["name"] for track in top_tracks_long]
    session["track_names_med"] = [track["name"] for track in top_tracks_med]
    session["track_names_short"] = [track["name"] for track in top_tracks_short]

    return render_template("toptracks.html", top_tracks_long=top_tracks_long, top_tracks_med = top_tracks_med, top_tracks_short=top_tracks_short)

@app.route("/topartists")
def display_top_artists():
    """render users top artists"""
    # This is the route which the Spotify OAuth redirects to.
    # We finish getting an access token here.
    if request.args.get("code"):
        get_spotify(request.args["code"])

    top_artists_short = get_user_top_artists(time_range='short_term', limit=50)
    top_artists_med = get_user_top_artists(time_range='medium_term', limit=50)
    top_artists_long = get_user_top_artists(time_range='long_term', limit=50)
    session["artist_names_long"] = [artist["name"] for artist in top_artists_long]
    session["artist_names_med"] = [artist["name"] for artist in top_artists_med]
    session["artist_names_short"] = [artist["name"] for artist in top_artists_short]

    return render_template("topartists.html", top_artists_long=top_artists_long, top_artists_med = top_artists_med, top_artists_short=top_artists_short)

def get_oauth():
    """Return a Spotipy Oauth2 object."""
    prefs = get_prefs()
    cache_dir = user_cache_dir(appname='statsify')
    if not os.path.isdir(cache_dir):
        os.makedirs(cache_dir)
    cache_path = os.path.join(cache_dir, 'cache')
    return spotipy.oauth2.SpotifyOAuth(
        prefs["ClientID"], prefs["ClientSecret"], REDIRECT_URI, scope=SCOPE,
        cache_path=cache_path)

def get_spotify(auth_token=None):
    """Return an authenticated Spotify object."""
    oauth = get_oauth()
    token_info = oauth.get_cached_token()
    if not token_info and auth_token:
        token_info = oauth.get_access_token(auth_token)
    return spotipy.Spotify(token_info["access_token"])

def get_prefs():
    with open("config.json") as prefs_file:
        prefs = json.load(prefs_file)

    return prefs

def get_names(tracks):
    """Return just the name component of a list of name/id tuples."""
    return [track[0] for track in tracks]

def get_user_playlists():
    """Return an id, name, images tuple of a user's playlists."""
    sp = get_spotify()
    user_id = sp.current_user()["id"]
    results = sp.user_playlists(user_id)

    playlists = results["items"]
    while results["next"]:
        results = sp.next(results)
        playlists.extend(results["items"])

    playlist_names = [{"id": playlist["id"], "name": playlist["name"],
                       "images": playlist["images"]} for playlist in playlists]

    return playlist_names

def get_user_top_artists(limit=50, time_range='medium_term'):
    sp = get_spotify()
    sp.trace = False
    user_id = sp.current_user()["id"]
    results = sp.current_user_top_artists(offset = 0, limit = limit, time_range = time_range)

    artists = results["items"]
    while results["next"]:
        results = sp.next(results)
        artists.extend(results["items"])

    artist_names = [{"id": artist["id"], "name": artist["name"],
                    "images": artist["images"], "genres": artist["genres"],
                    "popularity": artist["popularity"]} for artist in artists]

    return artist_names

def get_user_top_tracks(limit=50, time_range='medium_term'):
    sp = get_spotify()
    sp.trace = False
    session['user_id'] = sp.current_user()["id"]
    session['results'] = sp.current_user_top_tracks(offset = 0, limit = limit, time_range = time_range)

    tracks = session['results']["items"]

    session['track_names'] = [{"id": track["id"], "name": track["name"], "popularity": track["popularity"],
                    "album_images": track["album"]["images"], "album_name": track["album"]["name"],
                    "explicit": track["explicit"], "artist_name": track["artists"][0]["name"],
                    "artist_id": track["artists"][0]["id"], "album_id": track["album"]["id"]} for track in tracks]

    return session['track_names']

def get_playlist_id_by_name(name):
    """Return the id for a playlist with name: 'name'."""
    return [playlist["id"] for playlist in get_user_playlists() if
            playlist["name"] == name][0]

if __name__ == "__main__":
    app.run(debug=True, port=PORT)


'''
@ ! D:/Python366
@ -*- coding:utf-8 -*-
@ Time: 2019/7/19, 10:50
@ Author: LiangHongli
@ Mail: l.hong.li@foxmail.com
@ File: validation_mapping1.py
@ Software: PyCharm
读MERSI L1数据，自行做云检测等处理，用训练好的模型预测，并和原始的MODIS分布做比较
'''
from pathlib import Path
import numpy as np
import read_files as rf
import drawing_mapping_funcs as dmf
import h5py
from scipy import stats

def main():
    fpath = Path(r'I:\FY3D')
    files11 = [x for x in fpath.glob('*.HDF') if '20181126' in x.name and '1000M' in x.name]
    n_valid = int(6**2*0.9)
    xdt = np.zeros((1,93),dtype=float)
    xdb = np.zeros((1,93),dtype=float)
    for k in range(len(files11)):
        f = files11[k]
        #加经纬度判断，先判断是否有在该范围的lonlat_valid,否则查找很费时间
        label = f.name.split('_')
        label[6] = 'GEO1K'
        geoname = '_'.join(label)
        fgeo = f.parent/geoname
        with h5py.File(fgeo, 'r') as g:
            lats = g['Geolocation/Latitude'][:]
            lat = np.mean(lats)
            lons = g['Geolocation/Longitude'][:]
            lon = np.mean(lons)
        if lat > 50 or lat < 20 or lon > 125 or lon < 70:
            print('文件{}覆盖范围不包括中国区域或覆盖范围很小'.format(f.name))
            continue
        else:
            xmersi,lon_lats = rf.read_MERSI(f,fgeo)
            xmersi = xmersi[:,:,:-1] # 去掉时间

        for ii in range(xmersi.shape[0]):
            # is3 = xmersi[] #去除冰雪像元
            for jj in range(xmersi.shape[1]):
                is1 = any(xmersi[ii,jj,:]==-99) # 去除无效像元
                #对于MODIS的算法，是取3*3窗口，0.51μm的表观反射率标准差大于0.006时，判定为有云，所以这里的云判别标准可以调整
                is2 = np.std(xmersi[ii-3:ii+3,jj-3:jj+3,1])>0.006 or any(xmersi[ii-3:ii+3,jj-3:jj+3,0])>0.25 #去除云像元
                if is1 or is2:
                    continue
                else:
                    temp = np.zeros((1,93),dtype=float)
                    for k in range(xmersi.shape[-1]-1):
                        xx = xmersi[ii-3:ii+3,jj-3:jj+3,k]
                        xx = xx[xx!=-99]
                        if len(xx)<n_valid:
                            continue
                        else:
                            temp[0,k] = np.mean()
                            temp[0,k+30] = np.median(xmersi[ii-3:ii+3,jj-3:jj+3,k])
                            temp[0,k+60] = np.std(xmersi[ii-3:ii+3,jj-3:jj+3,k])
                            temp[0,-3] = np.mean(lon_lats[ii-3:ii+3,jj-3:jj+3,0]) # longitude
                            temp[0,-2] = np.mean(lon_lats[ii-3:ii+3,jj-3:jj+3,1]) # latitude
                            temp[0,-1] = stats.mode(xmersi[ii-3:ii+3,jj-3:jj+3,-1]) # land type

                    # 判断用深蓝还是暗目标算法
                    if temp[0,6]>0.25:
                        xdb = np.append(xdb,temp,axis=0)
                    elif temp[0,6]<=0.25 and all(temp!=0):
                        xdt = np.append(xdt,temp,axis=0)

        print('MERSI文件{}完成'.format(f.name))
    xdt = xdt[1:]
    xdb = xdb[1:]
    with h5py.File(r'H:\RS competition\training_dataset\for_validation.HDF') as g:
        g['x_dt'] = xdt
        g['x_db'] = xdb

if __name__ =="__main__":
    main()


import tensorflow as tf
from tensorflow.keras import backend as K
K.set_image_data_format('channels_last')
from constants.AI_params import TrainingParams
from io_project.read_utils import stringToArray
from constants_proj.AI_proj_params import *
from metrics_proj.isop_metrics import swstate_tf
import pandas as pd
import numpy as np

from constants_proj.AI_proj_params import *
# def proj_mse(y_true, y_pred):
#     # Flatten all the arrays
#     y_true_f = K.flatten(y_true)
#     y_pred_f = K.flatten(y_pred)
#     return tf.math.squared_difference(y_true_f, y_pred_f)

def force_monotonic_density_loss(config):

    LOCATIONS = config[ProjTrainingParams.locations]
    depth_size = 78
    stats_file = config[ProjTrainingParams.stats_file]
    batch_size = config[TrainingParams.batch_size]
    tot_loc = len(LOCATIONS)

    # =================================== READING DENORM DATA =================================================
    # Read the statistics file
    df = pd.read_csv(stats_file)
    all_mean_temp = np.zeros((len(LOCATIONS), depth_size), dtype=np.float32)
    all_mean_saln = np.zeros((len(LOCATIONS), depth_size), dtype=np.float32)
    all_std_temp = np.zeros((len(LOCATIONS), depth_size), dtype=np.float32)
    all_std_saln = np.zeros((len(LOCATIONS), depth_size), dtype=np.float32)
    max_depth_idx = np.zeros(len(LOCATIONS), dtype=np.int)

    for i_loc, c_loc in enumerate(LOCATIONS):
        all_mean_temp[i_loc,:] = stringToArray(df.loc[c_loc, "mean_temp"])
        all_mean_saln[i_loc,:] = stringToArray(df.loc[c_loc, "mean_saln"])
        all_std_temp[i_loc,:] = stringToArray(df.loc[c_loc, "std_temp"])
        all_std_saln[i_loc,:] = stringToArray(df.loc[c_loc, "std_saln"])
        max_depth_idx[i_loc] = np.where(np.isnan(all_mean_temp[i_loc,:]))[0][0] - 1

    all_mean_temp_tf = tf.constant(all_mean_temp, dtype=tf.float32)
    all_mean_saln_tf = tf.constant(all_mean_saln, dtype=tf.float32)
    all_std_temp_tf = tf.constant(all_std_temp, dtype=tf.float32)
    all_std_saln_tf = tf.constant(all_std_saln, dtype=tf.float32)
    max_depth_idx_tf = tf.constant(max_depth_idx, dtype=tf.int32)

    # Generate a tensor with the all_depths
    all_depths = tf.constant([0.0,2.0,4.0,6.0,8.0,10.0,15.0,20.0,25.0,30.0,35.0,40.0,45.0,50.0,55.0,60.0,65.0,70.0,75.0,80.0,85.0,90.0,95.0,100.0,110.0,120.0,130.0,140.0,150.0,160.0,170.0,180.0,190.0,200.0,220.0,240.0,260.0,280.0,300.0,350.0,400.0,500.0,600.0,700.0,800.0,900.0,1000.0,1100.0,1200.0,1300.0,1400.0,1500.0,1600.0,1800.0,2000.0,2200.0,2400.0,2600.0,2800.0,3000.0,3200.0,3400.0,3600.0,3800.0,4000.0,4200.0,4400.0,4600.0,4800.0,5000.0,5200.0,5400.0,5600.0,5800.0,6000.0,6200.0,6400.0,6600.0])
    # =================================== Computing the loss function =================================================

    @tf.function
    def loss(y_true, y_pred):
        # Here we should start the loss function
        # batch (timesteps), T and S, locations, depth
        y_pred_res = tf.reshape(y_pred, [batch_size, 2, tot_loc, 78])
        # y_true_res = tf.reshape(y_true, [batch_size, 2, tot_loc, 78])

        for i in range(tot_loc):
            c_max_depth = max_depth_idx_tf[i]
            # ============ Denormalizing
            t = (y_pred_res[:, 0, i, :c_max_depth]*all_std_temp_tf[i,:c_max_depth]) + all_mean_temp_tf[i,:c_max_depth]
            s = (y_pred_res[:, 1, i, :c_max_depth]*all_std_saln_tf[i,:c_max_depth]) + all_mean_saln_tf[i,:c_max_depth]
            # ============ Computing density
            d = swstate_tf(s, t, all_depths[:c_max_depth])
            # ============ Loss function for monotonic restricted density profile
            diff = d[:,:-2] - d[:,1:-1]
            if i == 0:
                error_mon = tf.reduce_sum(diff[diff > 0])/tot_loc
            else:
                error_mon += tf.reduce_sum(diff[diff > 0])/tot_loc

            # true_t = (y_true_res[c_batch, 0, i, :c_max_depth]*all_std_temp_tf[i,:c_max_depth]) + all_mean_temp_tf[i,:c_max_depth]
            # true_s = (y_true_res[c_batch, 1, i, :c_max_depth]*all_std_saln_tf[i,:c_max_depth]) + all_mean_saln_tf[i,:c_max_depth]
            # true_d = swstate_tf(true_s, true_t, all_depths[:c_max_depth])
            # for j in range(max_depth_idx[i] - 2): # The -2 is because we are also printing the difference and there we lost one index
            #     tf.print(F"%%%%%%%%%%%%% {j} %%%%%%%%%%%%%%%%%%%%%%")
            #     tf.print(y_true_res[c_batch,0,i,j])
            #     tf.print(y_true_res[c_batch,1,i,j])
            #     tf.print(y_pred_res[c_batch,0,i,j])
            #     tf.print(y_pred_res[c_batch,1,i,j])
            #     tf.print(F"************* {j} **********************")
            #     tf.print(true_t[j])
            #     tf.print(true_s[j])
            #     tf.print(t[j])
            #     tf.print(s[j])
            #     tf.print(F"------------- {j} ----------------------")
            #     tf.print(true_d[j])
            #     tf.print(d[j])
            #     tf.print(F"&&&&&&&&&&&&")
            #     tf.print(diff[j])

        # y_true_f = K.flatten(y_true)
        # y_pred_f = K.flatten(y_pred)
        # rmse = tf.math.reduce_mean(tf.math.squared_difference(y_true_f, y_pred_f))
        # tf.print(F"RMSE:")
        # tf.print(rmse)
        # tf.print(F"ERROR:")
        # tf.print(error_mon)
        # return rmse + error_mon
        return error_mon
        # return rmse

    return loss

def only_ocean_mse(y_true, y_pred, smooth=1.0):
    eps = .00001
    # Flatten all the arrays
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)

    # The true values on land should be -0.5 in order for this to work
    temp = tf.math.ceil(y_true_f + eps)  # This should make the ocean values = 1 and the land values = 0
    y_pred_c = y_pred_f * temp # Make 0 the prediction outside the prostate
    # Keep only the water values
    y_true_c = y_true_f * temp

    return tf.math.squared_difference(y_true_c, y_pred_c)

# ========== Just for testing ============
if __name__ == '__main__':

    from config.MainConfigByProfiles import get_training_2d
    from io_project.read_utils import get_all_profiles, normDenormData

    config = get_training_2d()
    LOCATIONS = config[ProjTrainingParams.locations]
    batch_size = config[TrainingParams.batch_size]
    RAND_LOC = len(LOCATIONS)
    depth_size = 78
    output_size = RAND_LOC*depth_size*2  # We want to output all the profiles all_depths for temperature and salinity

    # ========= With random data ==============
    # y_true = tf.random.uniform([batch_size, output_size])
    # y_pred = tf.random.uniform([batch_size, output_size])

    # ========= With real data ==============
    ssh, temp_profile, saln_profile, years, dyear, depths, latlons = get_all_profiles("/data/SubsurfaceFields/PreprocGoM", LOCATIONS, time_steps=range(10))
    temp_profile, saln_profile = normDenormData("/data/SubsurfaceFields/PreprocGoM/MEAN_STD_by_loc.csv", temp_profile, saln_profile, loc=LOCATIONS)

    y_true = np.zeros([batch_size, output_size], dtype=np.float32)
    y_pred = np.zeros([batch_size, output_size], dtype=np.float32)

    for i in range(batch_size):
        c_timestep = i
        ty = np.concatenate((temp_profile[c_timestep,:,:].flatten(), saln_profile[c_timestep,:,:].flatten()))
        y_true[i,:] = ty
        y_pred[i,:] = ty

    # ========= Test function ==============
    myloss = force_monotonic_density_loss(config)
    lval = myloss(y_true, y_pred)
    print(F"Test loss value: {lval}")