TASK_ACTION_OPTIONS = ["auto", "manual"]
AUTO, MANUAL = TASK_ACTION_OPTIONS

TASK_STATUS_OPTIONS = ["complete", "skipped", "failed"]
COMPLETE, SKIP, FAIL = TASK_STATUS_OPTIONS

DEPENDENCY_STRATEGIES = [
    "one-to-one",
    "all-to-all",
    "one-to-all",
    "manual",
    "none"
]

ONE_TO_ONE, ALL_TO_ALL, ONE_TO_ALL, MANUAL, NONE = DEPENDENCY_STRATEGIES

MAX_SEGMENTS = 10000
ITERATION_LIMIT = 10000

AXES = {
    'x' : [1,0,0],
    'y' : [0,1,0],
    'z' : [0,0,1],
}

NORTH_VECTORS = {
    'x' : [0,0,1],
    'y' : [1,0,0],
    'z' : [0,1,0],
}

ENZO_FIELDS = {
    "density" : ("gas", "density"),
    "temperature" : ("gas", "temperature"),
    "metallicity" : ("gas", "metallicity"),
    "grid_level" : ('index', "grid_level"),
    "tcool_tdyn_ratio" : ('gas', "tcool_tdyn_ratio"),
    "none" : None
}

DEFAULT_UNITS = {
    "density" : "g/cm**3",
    "temperature" : "K",
    "metallicity" : "Zsun",
    "grid_level" : "dimensionless",
    "tcool_tdyn_ratio" : "dimensionless"
}

DEFAULT_PLOT_SETTINGS = {
    "density" : {
        "zlabel" : r"Density\n(g cm$^{-3}$)",
        "zlims" : [1e-30, 5e-26],
        "field_units" : "g/cm**3",
        "cmap" : "viridis"
    },
    "temperature" : {
        "zlabel" : "Temperature (K)",
        "zlims" : [1e3, 1e8],
        "field_units" : "K",
        "cmap" : "plasma"
    },
    "metallicity" : {
        "zlabel" : "Metallicity\n(Zsun)",
        "zlims" : [1e-4, 1],
        "field_units" : "Zsun",
        "cmap" : "dusk"
    },
    "grid_level" : {
        "zlabel" : "Grid Level",
        "zlims" : [0, 6],
        "field_units" : "",
        "cmap" : "jet"
    },
    "tcool_tdyn_ratio" : {
        "zlabel" : r"$t_{cool} / t_{dyn}$",
        "zlims" : [1e-2, 1e2],
        "field_units" : "",
        "cmap" : "coolwarm"
    },
}


import numpy as np
import cv2
from matplotlib import pyplot as plt

cap = cv2.VideoCapture('videos/pull_slinky.MOV')
cv2.namedWindow('Frame',cv2.WINDOW_NORMAL)

OPENCV_OBJECT_TRACKERS = {
	"csrt": cv2.TrackerCSRT_create,
	"kcf": cv2.TrackerKCF_create,
	"boosting": cv2.TrackerBoosting_create,
	"mil": cv2.TrackerMIL_create,
	"tld": cv2.TrackerTLD_create,
	"medianflow": cv2.TrackerMedianFlow_create,
	"mosse": cv2.TrackerMOSSE_create
}

trackers = cv2.MultiTracker_create()
tracker_name = "csrt"
tracker = OPENCV_OBJECT_TRACKERS[tracker_name]()

def get_iou(bb1, bb2):

	assert bb1['x1'] < bb1['x2']
	assert bb1['y1'] < bb1['y2']
	assert bb2['x1'] < bb2['x2']
	assert bb2['y1'] < bb2['y2']

	x_left = max(bb1['x1'], bb2['x1'])
	y_top = max(bb1['y1'], bb2['y1'])
	x_right = min(bb1['x2'], bb2['x2'])
	y_bottom = min(bb1['y2'], bb2['y2'])

	if x_right < x_left or y_bottom < y_top:
		return 0.0

	intersection_area = (x_right - x_left) * (y_bottom - y_top)

	bb1_area = (bb1['x2'] - bb1['x1']) * (bb1['y2'] - bb1['y1'])
	bb2_area = (bb2['x2'] - bb2['x1']) * (bb2['y2'] - bb2['y1'])

	iou = intersection_area / float(bb1_area + bb2_area - intersection_area)
	assert iou >= 0.0
	assert iou <= 1.0
	return iou

def checkallboxes(bb):
	length_list = len(bb)

	bb_copy = bb.copy()
	for i in range(length_list):
			for j in range(i+1, length_list):
				try:
					iou = get_iou(bb_copy[i], bb_copy[j])
					if iou != 0:
						bb_copy.remove(bb_copy[j])
						length_list -= 1

				except Exception as e:
					pass
	return bb_copy

def boxes_to_track(frame, no_of_features=10, bbox_size=20):
	frame_copy = frame.copy()
	gray = cv2.cvtColor(frame_copy,cv2.COLOR_BGR2GRAY)

	corners = cv2.goodFeaturesToTrack(gray,no_of_features,0.01,10)
	corners = np.int0(corners)
	bboxes = []
	for i in corners:
		x,y = i.ravel()
		bbox = {'x1':x-bbox_size//2,'x2':x+bbox_size//2, 'y1':y-bbox_size//2, 'y2':y+bbox_size//2}
		bboxes.append(bbox)

	bbox_cleaned = checkallboxes(bboxes)
	boxes_mod = []

	for box in bbox_cleaned:
		w,h = box['x2'] - box['x1'],  box['y2']-box['y1']
		box_mod = (box['x1'],  box['y1'], w, h)
		boxes_mod.append(box_mod)

	return boxes_mod

if __name__ == '__main__':

	ret,frame = cap.read()
	boxes_calc = boxes_to_track(frame,no_of_features=50)
	frame_no = 0
	while True:

		if frame_no < len(boxes_calc):
			(success, boxes) = trackers.update(frame)

			box = boxes_calc[frame_no]
			(x, y, w, h) = [int(v) for v in box]
			cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
			cv2.circle(frame,(x+w//2,y+w//2), 3,(255,0,0),thickness=3)

			tracker = OPENCV_OBJECT_TRACKERS[tracker_name]()
			trackers.add(tracker, frame, box)
			cv2.imshow("Frame", frame)
			key = cv2.waitKey(1) & 0xFF

		else:
			ret,frame = cap.read()
			(success, boxes) = trackers.update(frame)
			for box in boxes:
				(x, y, w, h) = [int(v) for v in box]
				cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)

			# save this frame as video
			cv2.imshow("Frame", frame)
			key = cv2.waitKey(1) & 0xFF
			if key == ord("q"):
				break

		frame_no+=1

	cap.release()
	cv2.destroyAllWindows()


from flask import Flask, render_template, flash, redirect
import os

from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, SubmitField, EmailField
from wtforms.validators import DataRequired, Email, InputRequired

from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

base_dir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)
app.config['SECRET_KEY'] = "mysecretkeyiwillhideit:)"
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+os.path.join(base_dir, 'users.db')

######### FORM ################
class NamerForm(FlaskForm):
    name = StringField('Enter your name:', validators=[DataRequired()])
    age = IntegerField('Enter your age:')
    submit = SubmitField('Register')

class UserForm(FlaskForm):
    name = StringField('Enter your Name:', validators=[DataRequired()])
    email = EmailField('Enter your Email:', validators=[DataRequired()])
    submit = SubmitField('Register User')

####### DATABASE ##############
db = SQLAlchemy(app)

class UsersDb(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False )
    email = db.Column(db.String, nullable=False, unique=True)
    date_added = db.Column(db.DateTime, default=datetime.utcnow)

    def __repr__(self):
        return f'{self.name} and email:{self.email}'

############## VIEWS #######################
@app.route("/")
def hello():
    return render_template('hello.html')

@app.route("/user/<name>")
def user(name):
    return render_template('user.html', name=name)

@app.route("/user/add", methods=['GET', 'POST'])
def add_user_to_db():
    form = UserForm()
    name = None
    email = None

    if form.validate_on_submit():
        name = form.name.data
        email = form.email.data

        # querying the db if the email is already present
        user = UsersDb.query.filter_by(email=email).first()
        if user:
            flash(f'{email} already exists. Try logging-in')
        else:
            new_user = UsersDb(name=name, email=email)
            db.session.add(new_user)
            db.session.commit()
            flash(f'{name} with {email} was registered successfully!')

        form = UserForm(formdata=None)

    # quering all users and ordering with date_added
    disp_all_users = UsersDb.query.order_by(UsersDb.date_added)

    return render_template('add-user-to-db.html', form=form, name=name, email=email, disp_all_users=disp_all_users)

@app.route("/name", methods=['GET', 'POST'])
def enter_name():
    name = None
    age = None
    form = NamerForm()

    if form.validate_on_submit():
        name = form.name.data
        age = form.age.data

        form=NamerForm(formdata=None)
        flash(f'{name} was registered successfully!')

        # return redirect(url_for('enter_name'))

    return render_template('name.html', form=form, name=name, age=age)

###### custom error page

# invalid url
@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

#internal server error
@app.errorhandler(500)
def internal_server_e(e):
    return render_template('500.html'), 500

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


from __future__ import annotations
import os
import torch
import torch.nn as nn
from lpips import LPIPS
from preloaded_models.resnet import ResNet, BasicBlock

class PerceptualLoss(nn.Module):
    """
    Perceptual loss using features from pretrained deep neural networks trained. The function supports networks
    pretrained on: ImageNet that use the LPIPS approach from Zhang, et al. "The unreasonable effectiveness of deep
    features as a perceptual metric." https://arxiv.org/abs/1801.03924 ; RadImagenet from Mei, et al. "RadImageNet: An
    Open Radiologic Deep Learning Research Dataset for Effective Transfer Learning"
    https://pubs.rsna.org/doi/full/10.1148/ryai.210315 ; and MedicalNet from Chen et al. "Med3D: Transfer Learning for
    3D Medical Image Analysis" https://arxiv.org/abs/1904.00625 .

    The fake 3D implementation is based on a 2.5D approach where we calculate the 2D perceptual on slices from the
    three axis.

    Args:
        spatial_dims: number of spatial dimensions.
        network_type: {``"alex"``, ``"vgg"``, ``"squeeze"``, ``"radimagenet_resnet50"``,
        ``"medicalnet_resnet10_23datasets"``, ``"medicalnet_resnet50_23datasets"``}
            Specifies the network architecture to use. Defaults to ``"alex"``.
        is_fake_3d: if True use 2.5D approach for a 3D perceptual loss.
        fake_3d_ratio: ratio of how many slices per axis are used in the 2.5D approach.
    """

    def __init__(
        self, spatial_dims: int, network_type: str = "alex", is_fake_3d: bool = True, fake_3d_ratio: float = 0.5,
            from_preloaded = False, path_net = None, device = None,
    ):
        super().__init__()
        if device is None:
            self.device = "cuda" if torch.cuda.is_available() else "cpu"
        else:
            self.device = device
        if spatial_dims not in [2, 3]:
            raise NotImplementedError("Perceptual loss is implemented only in 2D and 3D.")

        if (spatial_dims == 2 or is_fake_3d) and "medicalnet_" in network_type:
            raise ValueError(
                "MedicalNet networks are only compatible with ``spatial_dims=3``."
                "Argument is_fake_3d must be set to False."
            )

        self.spatial_dims = spatial_dims
        if spatial_dims == 3 and is_fake_3d is False:
            self.perceptual_function = MedicalNetPerceptualSimilarity(net=network_type, verbose=False,
                                                                      from_preloaded = from_preloaded,
                                                                      path_net = path_net,
                                                                      device = self.device)
        elif "radimagenet_" in network_type:
            self.perceptual_function = RadImageNetPerceptualSimilarity(net=network_type, verbose=False)
        else:
            self.perceptual_function = LPIPS(pretrained=True, net=network_type, verbose=False)
        self.is_fake_3d = is_fake_3d
        self.fake_3d_ratio = fake_3d_ratio

    def _calculate_axis_loss(self, input: torch.Tensor, target: torch.Tensor, spatial_axis: int) -> torch.Tensor:
        """
        Calculate perceptual loss in one of the axis used in the 2.5D approach. After the slices of one spatial axis
        is transformed into different instances in the batch, we compute the loss using the 2D approach.

        Args:
            input: input 5D tensor. BNHWD
            target: target 5D tensor. BNHWD
            spatial_axis: spatial axis to obtain the 2D slices.
        """

        def batchify_axis(x: torch.Tensor, fake_3d_perm: tuple) -> torch.Tensor:
            """
            Transform slices from one spatial axis into different instances in the batch.
            """
            slices = x.float().permute((0,) + fake_3d_perm).contiguous()
            slices = slices.view(-1, x.shape[fake_3d_perm[1]], x.shape[fake_3d_perm[2]], x.shape[fake_3d_perm[3]])

            return slices

        preserved_axes = [2, 3, 4]
        preserved_axes.remove(spatial_axis)

        channel_axis = 1
        input_slices = batchify_axis(x=input, fake_3d_perm=(spatial_axis, channel_axis) + tuple(preserved_axes))
        indices = torch.randperm(input_slices.shape[0])[: int(input_slices.shape[0] * self.fake_3d_ratio)].to(
            input_slices.device
        )
        input_slices = torch.index_select(input_slices, dim=0, index=indices)
        target_slices = batchify_axis(x=target, fake_3d_perm=(spatial_axis, channel_axis) + tuple(preserved_axes))
        target_slices = torch.index_select(target_slices, dim=0, index=indices)

        axis_loss = torch.mean(self.perceptual_function(input_slices, target_slices))

        return axis_loss

    def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
        """
        Args:
            input: the shape should be BNHW[D].
            target: the shape should be BNHW[D].
        """
        if target.shape != input.shape:
            raise ValueError(f"ground truth has differing shape ({target.shape}) from input ({input.shape})")

        if self.spatial_dims == 3 and self.is_fake_3d:
            # Compute 2.5D approach
            loss_sagittal = self._calculate_axis_loss(input, target, spatial_axis=2)
            loss_coronal = self._calculate_axis_loss(input, target, spatial_axis=3)
            loss_axial = self._calculate_axis_loss(input, target, spatial_axis=4)
            loss = loss_sagittal + loss_axial + loss_coronal
        else:
            # 2D and real 3D cases
            loss = self.perceptual_function(input, target)

        return torch.mean(loss)

class MedicalNetPerceptualSimilarity(nn.Module):
    """
    Component to perform the perceptual evaluation with the networks pretrained by Chen, et al. "Med3D: Transfer
    Learning for 3D Medical Image Analysis". This class uses torch Hub to download the networks from
    "Warvito/MedicalNet-models".

    Args:
        net: {``"medicalnet_resnet10_23datasets"``, ``"medicalnet_resnet50_23datasets"``}
            Specifies the network architecture to use. Defaults to ``"medicalnet_resnet10_23datasets"``.
        verbose: if false, mute messages from torch Hub load function.
    """

    def __init__(self, net: str = "medicalnet_resnet10_23datasets", verbose: bool = False,
                 from_preloaded=False, path_net = None, device = None) -> None:
        super().__init__()
        if device is None:
            device = "cuda" if torch.cuda.is_available() else "cpu"
        else:
            device = device
        torch.hub._validate_not_a_forked_repo = lambda a, b, c: True
        if from_preloaded:
            self.model = ResNet(BasicBlock, [1, 1, 1, 1])
            pretrained_state_dict = torch.load(path_net)
            pretrained_state_dict = pretrained_state_dict["state_dict"]
            pretrained_state_dict = {k.replace("module.", ""): v for k, v in pretrained_state_dict.items()}
            self.model.load_state_dict(pretrained_state_dict)
        else:
            self.model = torch.hub.load("Warvito/MedicalNet-models", model=net, verbose=verbose, force_reload=False)

        self.model = self.model.to(device)

        self.eval()

    def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
        """
        Compute perceptual loss using MedicalNet 3D networks. The input and target tensors are inputted in the
        pre-trained MedicalNet that is used for feature extraction. Then, these extracted features are normalised across
        the channels. Finally, we compute the difference between the input and target features and calculate the mean
        value from the spatial dimensions to obtain the perceptual loss.

        Args:
            input: 3D input tensor with shape BCDHW.
            target: 3D target tensor with shape BCDHW.
        """
        input = medicalnet_intensity_normalisation(input)
        target = medicalnet_intensity_normalisation(target)

        # Get model outputs
        outs_input = self.model.forward(input)
        outs_target = self.model.forward(target)

        # Normalise through the channels
        feats_input = normalize_tensor(outs_input)
        feats_target = normalize_tensor(outs_target)

        results = (feats_input - feats_target) ** 2
        results = spatial_average_3d(results.sum(dim=1, keepdim=True), keepdim=True)

        return results

def spatial_average_3d(x: torch.Tensor, keepdim: bool = True) -> torch.Tensor:
    return x.mean([2, 3, 4], keepdim=keepdim)

def normalize_tensor(x: torch.Tensor, eps: float = 1e-10) -> torch.Tensor:
    norm_factor = torch.sqrt(torch.sum(x**2, dim=1, keepdim=True))
    return x / (norm_factor + eps)

def medicalnet_intensity_normalisation(volume):
    """Based on https://github.com/Tencent/MedicalNet/blob/18c8bb6cd564eb1b964bffef1f4c2283f1ae6e7b/datasets/brains18.py#L133"""
    mean = volume.mean()
    std = volume.std()
    return (volume - mean) / std

class RadImageNetPerceptualSimilarity(nn.Module):
    """
    Component to perform the perceptual evaluation with the networks pretrained on RadImagenet (pretrained by Mei, et
    al. "RadImageNet: An Open Radiologic Deep Learning Research Dataset for Effective Transfer Learning"). This class
    uses torch Hub to download the networks from "Warvito/radimagenet-models".

    Args:
        net: {``"radimagenet_resnet50"``}
            Specifies the network architecture to use. Defaults to ``"radimagenet_resnet50"``.
        verbose: if false, mute messages from torch Hub load function.
    """

    def __init__(self, net: str = "radimagenet_resnet50", verbose: bool = False) -> None:
        super().__init__()
        self.model = torch.hub.load("Warvito/radimagenet-models", model=net, verbose=verbose)
        self.eval()

    def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
        """
        We expect that the input is normalised between [0, 1]. Given the preprocessing performed during the training at
        https://github.com/BMEII-AI/RadImageNet, we make sure that the input and target have 3 channels, reorder it from
         'RGB' to 'BGR', and then remove the mean components of each input data channel. The outputs are normalised
        across the channels, and we obtain the mean from the spatial dimensions (similar approach to the lpips package).
        """
        # If input has just 1 channel, repeat channel to have 3 channels
        if input.shape[1] == 1 and target.shape[1] == 1:
            input = input.repeat(1, 3, 1, 1)
            target = target.repeat(1, 3, 1, 1)

        # Change order from 'RGB' to 'BGR'
        input = input[:, [2, 1, 0], ...]
        target = target[:, [2, 1, 0], ...]

        # Subtract mean used during training
        input = subtract_mean(input)
        target = subtract_mean(target)

        # Get model outputs
        outs_input = self.model.forward(input)
        outs_target = self.model.forward(target)

        # Normalise through the channels
        feats_input = normalize_tensor(outs_input)
        feats_target = normalize_tensor(outs_target)

        results = (feats_input - feats_target) ** 2
        results = spatial_average(results.sum(dim=1, keepdim=True), keepdim=True)

        return results

def spatial_average(x: torch.Tensor, keepdim: bool = True) -> torch.Tensor:
    return x.mean([2, 3], keepdim=keepdim)

def subtract_mean(x: torch.Tensor) -> torch.Tensor:
    mean = [0.406, 0.456, 0.485]
    x[:, 0, :, :] -= mean[0]
    x[:, 1, :, :] -= mean[1]
    x[:, 2, :, :] -= mean[2]
    return x


from django.conf.urls import url
from django.views.generic.base import RedirectView

from . import views

urlpatterns = [
    url(r'^catalogue_goods$', views.catalogue_goods, name='catalogue_goods'),
    url(r'^catalogue_stored$', views.catalogue_stored, name='catalogue_stored'),
    url(r'^catalogue_rented$', views.catalogue_rented, name='catalogue_rented'),
    url(r'^receive_back_item/(?P<item_pk>[0-9]+)$', views.receive_back_item, name='receive_back_item'),
    url(r'^rent_item/(?P<item_pk>[0-9]+)$', views.rent_item, name='rent_item'),
    url(r'^return_item/(?P<item_pk>[0-9]+)$', views.return_item, name='return_item'),
    url(r'^search_rented$', views.search_items_rented, name='search_rented'),
    url(r'^search_stored$', views.search_items_stored, name='search_stored'),
    url(r'^search_goods$', views.search_items_goods, name='search_goods'),
    url(r'^item_stored/(?P<item_pk>[0-9]+)', views.item_stored_fullinfo, name='item_stored_fullinfo'),
    url(r'^item_rented/(?P<item_pk>[0-9]+)', views.item_rented_fullinfo, name='item_rented_fullinfo'),
    url(r'^item_good/(?P<item_pk>[0-9]+)', views.item_good_fullinfo, name='item_good_fullinfo'),
    url(r'^reset_status', views.reset_status, name='reset_status'),
    url(r'^.*$', RedirectView.as_view(url='catalogue_goods', permanent=False))
]


import numpy as np
import nvidia.dali.types as types
from nvidia.dali import fn, pipeline_def
from nvidia.dali.auto_aug import auto_augment

from rosetta.data.dali import BaseDALIPipeline
from rosetta.data.wds_utils import ModalityConfig

class ViTPipeline(BaseDALIPipeline):

  ## all pipelines are extpected to take wds_config, per_shard_batch_size into constructor
  def __init__(self,
               wds_config,
               shard_id,
               num_shards,
               num_classes,
               image_shape,
               training=True,
              ):

    self.num_classes = num_classes
    self.image_shape = image_shape
    modalities = [ModalityConfig(name='images',
                                 ftype='jpg',
                                 out_type='float32',
                                 shape=self.image_shape),
                  ModalityConfig(name='labels',
                                 ftype='cls',
                                 out_type='float32',
                                 shape=(self.num_classes,))]

    super().__init__(wds_config=wds_config,
                     modalities=modalities,
                     shard_id=shard_id,
                     num_shards=num_shards,
                     training=training)

  def get_wds_pipeline(self):
    @pipeline_def(batch_size=self.per_shard_batch_size, num_threads=1, device_id=None, seed=self.seed)
    def wds_vit_pipeline():
      ## assumes a particular order to the ftypes
      img, clss = fn.readers.webdataset(
        paths=self.urls,
        index_paths=self.index_paths,
        ext=[m.ftype for m in self.modalities],
        missing_component_behavior='error',
        random_shuffle=self.shuffle,
        shard_id=self.shard_id,
        num_shards=self.num_shards,
        pad_last_batch=False if self.training else True)
      return img, clss
    return wds_vit_pipeline()

  def non_image_preprocessing(self, raw_text, num_classes):
    """ preprocessing of class labels. """
    bs = len(raw_text.shape())
    ascii = [np.asarray(raw_text[i]) for i in range(bs)]

    one_hot = np.zeros((bs, num_classes))
    for i, el in enumerate(ascii):
      idx = int(bytes(el).decode('utf-8'))
      one_hot[i][idx] = 1

    return one_hot

  def data_source(self, num_classes):
    while True:
      preprocessed_img, raw_text = self.pipe.run()
      preprocessed_label = self.non_image_preprocessing(raw_text, num_classes)
      yield preprocessed_img, preprocessed_label

  def get_dali_pipeline(self):

    ## need to enable conditionals for auto-augment
    @pipeline_def(batch_size=self.per_shard_batch_size, num_threads=self.num_workers, device_id=None, enable_conditionals=True, seed=self.seed, prefetch_queue_depth=self.prefetch)
    def main_vit_pipeline():
      jpegs, labels = fn.external_source(source=self.data_source(self.num_classes), num_outputs=2)

      img = fn.decoders.image(jpegs, device='cpu', output_type=types.RGB)

      if self.training:
        img = fn.random_resized_crop(img, size=self.image_shape[:-1], seed=self.seed)
        img = fn.flip(img, depthwise=0, horizontal=fn.random.coin_flip(seed=self.seed))

        ## color jitter
        brightness = fn.random.uniform(range=[0.6,1.4], seed=self.seed)
        contrast = fn.random.uniform(range=[0.6,1.4], seed=self.seed)
        saturation = fn.random.uniform(range=[0.6,1.4], seed=self.seed)
        hue = fn.random.uniform(range=[0.9,1.1], seed=self.seed)
        img = fn.color_twist(img,
                             brightness=brightness,
                             contrast=contrast,
                             hue=hue,
                             saturation=saturation)

        ## auto-augment
        ## `shape` controls the magnitude of the translation operations
        img = auto_augment.auto_augment_image_net(img, seed=self.seed)

      else:
        img = fn.resize(img, size=self.image_shape[:-1])

      ## normalize
      ## https://github.com/NVIDIA/DALI/issues/4469
      mean = np.asarray([0.485, 0.456, 0.406])[None, None, :]
      std = np.asarray([0.229, 0.224, 0.225])[None, None, :]
      scale = 1 / 255.
      img = fn.normalize(img,
          mean=mean / scale,
          stddev=std,
          scale=scale,
          dtype=types.FLOAT)

      return img, labels

    return main_vit_pipeline()


import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets,linear_model
from sklearn.metrics import mean_squared_error,r2_score

df=datasets.load_diabetes()
df['feature_names']

x,y=datasets.load_diabetes(return_X_y=True)
x.shape

x=x[:,:1]
x.shape

x_train=x[:-20]
x_test=x[-20:]

y_train=y[:-20]
y_test=y[-20:]

regr=linear_model.LinearRegression()
regr.fit(x_train,y_train)

y_pred=regr.predict(x_test)

print(regr.coef_)
print(mean_squared_error(y_test,y_pred))
print(r2_score(y_test,y_pred))

plt.scatter(x_test,y_test)
plt.plot(x_test,y_pred)

x,y=datasets.load_diabetes(return_X_y=True)
x=x[:,[0,2]]

x_train=x[:-20]
x_test=x[-20:]

y_train=y[:-20]
y_test=y[-20:]

regr=linear_model.LinearRegression()
regr.fit(x_train,y_train)

ypred=regr.predict(x_test)

print(regr.coef_)
print(regr.intercept_)
print(mean_squared_error(y_test,y_pred))
print(r2_score(y_test,y_pred))


import urllib.request
import json
import random
import sys

def main(argv):
    #智研上报的URL
    url='http://zhiyan.monitor.access.inner.woa.com:8080/access_v1.http_service/HttpCurveReportRpc'
    report_data = [
        {"metric":str(argv[1]),"value": argv[2],"tags":{"metricType": "test"}},
        {"metric":str(argv[3]),"value": argv[4],"tags":{"metricType": "test"}},
        {"metric":str(argv[5]),"value": argv[6],"tags":{"metricType": "test"}},
        {"metric":str(argv[7]),"value": argv[8],"tags":{"metricType": "test"}}]
    report_datas = json.dumps(report_data)
    rand_num = random.randint(100000, 999999)
    request_data = {
        "pkg_seq": rand_num,
        "report_cnt": 1 ,
        "app_mark": "3633_20699_test_qinglin",
        "sec_lvl_en_name": "default",
        "env":"prod",
        "report_data": report_datas,
        "instance_mark": "9.134.235.217",
        "report_ip": "9.134.235.217",
        "data_type": 0 ,
        "method": 1
    }

    report_datas = json.dumps(request_data)
    headers={"Content-Type":"application/json"}
    report_datas=bytes(report_datas,'utf8')
    #发送POST请求
    request = urllib.request.Request(url, report_datas, headers)
    response = urllib.request.urlopen(request)
    ret = response.read()
    print (ret)

if __name__ == "__main__":
    main(sys.argv)


import cv2, sys, numpy as np
from matplotlib.pyplot import get

if len (sys.argv) != 2:
    print ("Usage: %s <image-file>" % sys.argv[1], file=sys.stderr)
    exit (1)
print ("The filename to work on is %s." % sys.argv[1])

image = cv2.imread (sys.argv[1])
b_image = image[:,:,0]

cv2.namedWindow (sys.argv[0], cv2.WINDOW_NORMAL)
ny, nx, nc = image.shape
cv2.resizeWindow (sys.argv[0], nx//2, ny//2)
cv2.imshow (sys.argv[0], b_image)
cv2.waitKey (0)


import re
import pdb
import nltk
import pickle
import random
import argparse
import numpy as np
import unicodedata
from tqdm import tqdm
from collections import defaultdict
from nltk.corpus import wordnet as wn

import synsets
import main_func

def get_idx2sense():
    idx2sense = defaultdict()
    with open('../semcor/SemCor/semcor.gold.key.txt') as inputfile:
        lines = inputfile.readlines()
        for line in lines:
            line = line.strip()
            parts = line.split(' ')
            temp = parts[1][:parts[1].find('%')]
            Punc = False
            for c in temp:
                if synsets._is_punctuation(c):
                    Punc = True
            if not Punc:
                idx2sense[parts[0]] = parts[1]
    return idx2sense

def get_idx2synsets(idx2sense):
    # In order to get the level information, we start from depth=1, then depth=2 (the final augumented version used 2 levels)
    hyp = lambda s:s.hypernyms()
    idx2synsets = defaultdict()
    for idx in idx2sense:
        try:
            if ('%1:') in idx2sense[idx]:
                synset = synsets.synset_from_sense_key(idx2sense[idx])
                for i in list(synset.closure(hyp, depth=1)):
                    lemma_list = i.lemmas()
                    for j in lemma_list:
                        if '_' not in j._name and '.' not in j._name and '-' not in j._name and '\'' not in j._name:
                            if idx not in idx2synsets:
                                idx2synsets[idx] = [j._name+'%%d_1']
                            else:
                                idx2synsets[idx] += [j._name+'%%d_1']
                for i in list(synset.closure(hyp, depth=2)):
                    lemma_list = i.lemmas()
                    for j in lemma_list:
                        if '_' not in j._name and '.' not in j._name and '-' not in j._name and '\'' not in j._name:
                            if idx not in idx2synsets:
                                idx2synsets[idx] = [j._name+'%%d_2']
                            elif j._name+'%%d_1' not in idx2synsets[idx]:
                                idx2synsets[idx] += [j._name+'%%d_2']

        except:
            pass
    print('Number of words with noun senses that we will replace in our dataset %d' %len(idx2synsets))
    return idx2synsets

def get_sequence_info(filename, idx2synsets):
    print('*'*50)
    # idx is sentence index which is represented by the first location of the sentece.
    idxword_to_replacedword_dict, idx_to_original_word_sentence_dict = main_func.get_sentence_and_replaced_word_for_sentence(
        filename, idx2synsets)
    idx_to_replacedword_dict = main_func.get_idx_to_replaceword_dict(idxword_to_replacedword_dict)
    # this token list is the repeted sentences
    token_list, level_list, hypernym_set_list, hyper_token_idx_list = main_func.construct_token_list_and_level_list(
        idx_to_original_word_sentence_dict, idx_to_replacedword_dict)
    return token_list, level_list, hypernym_set_list, hyper_token_idx_list

def combine_hyperset(train_hypernym_set_list, dev_hypernym_set_list, test_hypernym_set_list):
    print('*'*50)
    print('combining hyperset to create hyperset index')
    hypernym_set_list = train_hypernym_set_list+dev_hypernym_set_list+test_hypernym_set_list
    hyperset_set = set()
    for hyper in hypernym_set_list:
        if tuple(hyper) not in hyperset_set:
            hyperset_set.add(tuple(hyper))
    print('Number of unique hyperset', len(hyperset_set))
    idx2hyperset = list(hyperset_set)
    hyperset2idx = {idx2hyperset[i]: i+1 for i in range(len(idx2hyperset))}
    assert len(idx2hyperset) == len(hyperset2idx)
    return idx2hyperset, hyperset2idx

def get_hyperset_idx(hyperset2idx, hypernym_set_list):
    hyperset_idx_list = []
    for hyper in hypernym_set_list:
        if hyper!="None":
            hyperset_idx_list.append(hyperset2idx[tuple(hyper)])
        else:
            hyperset_idx_list.append(0)
    return hyperset_idx_list

def read_file(filename):
    with open(filename, 'rb') as f:
        return pickle.load(f)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--data_file", default=None, type=str, required=True, help="The input training data file (a text file)."
    )
    args = parser.parse_args()

    idx2sense = get_idx2sense()
    idx2synsets = get_idx2synsets(idx2sense)

    filename = '../semcor/SemCor/semcor.'+args.data_file+'_data.xml'
    bert_filename = 'aug_'+args.data_file
    token_list, level_list, hypernym_set_list, hyper_token_idx_list = get_sequence_info(
        filename, idx2synsets)
    if args.data_file == 'dev':
        assert len(token_list) == 734152
    elif args.data_file == 'test':
        assert len(token_list) == 881918

    elif args.data_file == 'train':
        assert len(token_list) == 6664040
    else:
        raise ValueError('Invalid data_file input, choose from dev, test or train')

    # read file
    updated_hyper_token_idx_list = read_file('../semcor/updated_'+args.data_file+'_idx.pkl')
    updated_level_list = read_file('../semcor/updated_'+args.data_file+'_level_idx.pkl')
    updated_hyperset_list = read_file('../semcor/updated_'+args.data_file+'_hyperset_idx.pkl')
    word_tokens = read_file('../semcor/'+bert_filename+'_word_tokens.pkl')
    bpe_tokens = read_file('../semcor/'+bert_filename+'_bpe_tokens.pkl')
    bpe_idx = read_file('../semcor/'+bert_filename+'_bpe_idx.pkl')

    assert bpe_tokens.shape == bpe_idx.shape
    bpe_tokens = list(bpe_tokens)
    # check stats
    sub_token_size = 0
    for k in bpe_tokens[:]:
        if k.startswith('#'):
            sub_token_size+=1
    print('Number of word tokens %d' %len(word_tokens))
    print('Number of sub token size %d' %sub_token_size)
    print('Number of bpe size %d' %len(bpe_tokens))

    assert sub_token_size+len(word_tokens) == len(bpe_tokens)
    # check stats
    # 0 for normal word
    # 1 for hypernym
    # 2 for word-piece weird word
    print('Getting masked_list...')
    marked_list = []
    offset = 0
    for original_idx in tqdm(range(len(word_tokens))):
        bpe_idx = original_idx+offset
        if original_idx in updated_hyper_token_idx_list:
            marked_list.append(1)
        else:
            marked_list.append(0)
        while bpe_idx+1 < len(bpe_tokens) and str(bpe_tokens[bpe_idx+1]).startswith('#'):
            bpe_idx+=1
            offset+=1
            marked_list.append(2)
    assert len(marked_list) == len(bpe_tokens)
    with open('../semcor/marked_'+args.data_file+'_idx.pkl', 'wb') as f:
        pickle.dump(np.asarray(marked_list), f)

    # update level list
    marked_level_list = []
    offset = 0
    for original_idx in tqdm(range(len(updated_level_list))):
        bpe_idx = original_idx+offset
        if original_idx in updated_hyper_token_idx_list:
            marked_level_list.append(updated_level_list[original_idx])
        else:
            marked_level_list.append(updated_level_list[original_idx])
        while bpe_idx+1 < len(bpe_tokens) and str(bpe_tokens[bpe_idx+1]).startswith('#'):
            bpe_idx+=1
            offset+=1
            marked_level_list.append(updated_level_list[original_idx])

    # update hyperset list
    marked_hyperset_list = []
    offset = 0
    for original_idx in tqdm(range(len(updated_hyperset_list))):
        bpe_idx = original_idx+offset
        if original_idx in updated_hyper_token_idx_list:
            marked_hyperset_list.append(updated_hyperset_list[original_idx])
        else:
            marked_hyperset_list.append(updated_hyperset_list[original_idx])
        while bpe_idx+1 < len(bpe_tokens) and str(bpe_tokens[bpe_idx+1]).startswith('#'):
            bpe_idx+=1
            offset+=1
            marked_hyperset_list.append(updated_hyperset_list[original_idx])

    assert len(marked_list) == len(marked_level_list) == len(marked_hyperset_list)
    with open('../semcor/marked_bpe'+args.data_file+'_level.pkl', 'wb') as f:
        pickle.dump(np.asarray(marked_level_list), f)
    with open('../semcor/marked_bpe'+args.data_file+'_hyperset.pkl', 'wb') as f:
        pickle.dump(np.asarray(marked_hyperset_list), f)

if __name__ == "__main__":
    main()


# Заменям 'Report time' със 'Search time'
# Добавям бутон 'Search' в мода
# Добавям поле за въвеждане на брой WS цикли

import colorsys
from tkinter import *
from tkinter.ttk import *
import math
import datetime
import json

mode = ' '

window = Tk()
window.title("Battery Life Estimator ")
window.geometry("695x695")
window.configure(bg="gray")

sv1 = StringVar()
sv2 = StringVar()

sv3 = StringVar()
sv4 = StringVar()
sv5 = StringVar()
sv6 = StringVar()
sv7 = StringVar()
sv8 = StringVar()
sv9 = StringVar()
sv10= StringVar()

# trace wants a callback with nearly useless parameters, fixing with lambda.
sv1.trace('w', lambda nm, idx, mode, var=sv1: validate_float(var))
sv2.trace('w', lambda nm, idx, mode, var=sv2: validate_float(var))
sv3.trace('w', lambda nm, idx, mode, var=sv3: validate_float(var))
sv4.trace('w', lambda nm, idx, mode, var=sv4: validate_float(var))
sv5.trace('w', lambda nm, idx, mode, var=sv5: validate_float(var))
sv6.trace('w', lambda nm, idx, mode, var=sv6: validate_float(var))
sv7.trace('w', lambda nm, idx, mode, var=sv7: validate_float(var))
sv8.trace('w', lambda nm, idx, mode, var=sv8: validate_float(var))
sv9.trace('w', lambda nm, idx, mode, var=sv9: validate_float(var))
sv10.trace('w', lambda nm, idx, mode, var=sv10: validate_float(var))

txt_empty0 = Entry(window, width=0).grid(row=0, column=6)
txt_empty1 = Entry(window, width=0).grid(row=1, column=6)
txt_empty2 = Entry(window, width=1).grid(row=3, column=6)
txt_empty3 = Entry(window, width=1).grid(row=5, column=6)
txt_empty4 = Entry(window, width=1).grid(row=7, column=6)
txt_empty5 = Entry(window, width=1).grid(row=9, column=6)
txt_empty6 = Entry(window, width=1).grid(row=11, column=6)
txt_empty7 = Entry(window, width=1).grid(row=13, column=6)
txt_empty8 = Entry(window, width=1).grid(row=15, column=6)
txt_empty9 = Entry(window, width=1).grid(row=17, column=6)
txt_empty13 = Entry(window, width=1).grid(row=18, column=6)
txt_empty14 = Entry(window, width=1).grid(row=19, column=6)
txt_empty11 = Entry(window, width=1).grid(row=20, column=6)
txt_empty10 = Entry(window, width=1).grid(row=21, column=6)
txt_empty12 = Entry(window, width=1).grid(row=24, column=6)

lbl1 = Label(window, text="Battery capacity", font=("Arial Bold", 15))
lbl1.grid(column=0, row=0)
lbl3 = Label(window, text="Capacity *", font=("Arial Bold", 13))
lbl3.grid(column=0, row=2)
lbl5 = Label(window, text="Device", font=("Arial Bold", 15))
lbl5.grid(column=0, row=4)
lbl8 = Label(window, text="Search time ", font=("Arial Bold", 13))
lbl8.grid(column=0, row=8)
lbl9 = Label(window, text="Work time *", font=("Arial Bold", 13))
lbl9.grid(column=0, row=10)
lbl10 = Label(window, text="Sleep time *", font=("Arial Bold", 13))
lbl10.grid(column=0, row=12)
lbl13 = Label(window, text="Battery self discharge", font=("Arial Bold", 15))
lbl13.grid(column=0, row=14)
lbl15 = Label(window, text="self discharge", font=("Arial Bold", 13))
lbl15.grid(column=0, row=16)
lbl19 = Label(window, text="Cyclic mode *", font=("Arial Bold", 15))
lbl19.grid(column=0, row=18)
lbl21 = Label(window, text="Estimated battery lifetime", font=("Arial Bold", 15))
lbl21.grid(column=0, row=20)
lbl23 = Label(window, width=10, text="Lifetime", font=("Arial Bold", 13))
lbl23.grid(column=0, row=22)
lbl24 = Label(window, width=10, text="Iterations", font=("Arial Bold", 13))
lbl24.grid(column=0, row=23)

lbl_lifetime = Label(window, width=10, text=" ", font=("Arial Bold", 13))
lbl_lifetime.grid(column=1, row=22)
lbl_iterations = Label(window, width=10, text=" ", font=("Arial Bold", 13))
lbl_iterations.grid(column=1, row=23)

lbl61 = Label(window, text="Ah", font=("Arial Bold", 13))
lbl61.grid(column=3, row=2)
lbl62 = Label(window,width=10, text="Search cap", font=("Arial Bold", 13))
lbl62.grid(column=3, row=8)
lbl63 = Label(window, text="s", font=("Arial Bold", 13))
lbl63.grid(column=2, row=8)
lbl63 = Label(window, text="s", font=("Arial Bold", 13))
lbl63.grid(column=3, row=10)
lbl64 = Label(window, text="s", font=("Arial Bold", 13))
lbl64.grid(column=3, row=12)
lbl65 = Label(window, text="%", font=("Arial Bold", 13))
lbl65.grid(column=3, row=16)

lbl100 = Label(window, text="      ", width=12, font=("Arial Bold", 13))
lbl100.grid(column=2, row=18)

lbl101 = Label(window, text="mA", width=3, font=("Arial Bold", 13))
lbl101.grid(column=5, row=8)
lbl_wrc_cap = Label(window, text="work cap ", width=12, font=("Arial Bold", 13))
lbl_wrc_cap.grid(column=4, row=10)
lbl102 = Label(window, text="mA", width=3, font=("Arial Bold", 13))
lbl102.grid(column=5, row=10)
lbl_slp_cap = Label(window, text="sleep cap ", width=12, font=("Arial Bold", 13))
lbl_slp_cap.grid(column=4, row=12)
lbl103 = Label(window, text="mA", width=3, font=("Arial Bold", 13))
lbl103.grid(column=5, row=12)
lbl_ws_cycles = Label(window, width=13, text="WS Cycles", font=("Arial Bold", 13))
lbl_ws_cycles.grid(column=4, row=18)

txt_batt_cap = Entry(window, validate='key', textvariable=sv1, width=10)
txt_batt_cap.grid(column=2, row=2)
txt_src_time = Entry(window, validate='key', textvariable=sv2, width=10)
txt_src_time.grid(column=1, row=8)
txt_wrk_time = Entry(window, validate='key', textvariable=sv3, width=10)
txt_wrk_time.grid(column=2, row=10)
txt_slp_time = Entry(window, validate='key', textvariable=sv4, width=10)
txt_slp_time.grid(column=2, row=12)
txt_bsd = Entry(window, validate='key', textvariable=sv5, width=10)
txt_bsd.grid(column=2, row=16)
txt_rep_freq = Entry(window,validate='key', textvariable=sv9, width=10)
txt_rep_freq.grid(row=19, column=4)

txt_src_cap = Entry(window, validate='key', textvariable=sv10, width=10)
txt_src_cap.grid(column=4, row=8)

combo = Combobox(window)
combo['values'] = ("Findy Bike", "Findy Car", "Findy Raid", "Findy Pet", "Water Meter", "Test Device")
combo.current(0)
combo.grid(column=0, row=6)

flag = False  # flag_test_device
flag1 = False # Check Enter Valid data

def clicked_get_device():
    kind_of_device = combo.get()
    global class_device, txt_rep_cap, txt_wrk_cap, txt_slp_cap, flag
    if kind_of_device == "Test Device":
        flag = True
    if kind_of_device == "Test Device":

        txt_wrk_cap = Entry(window, validate='key', textvariable=sv7, width=10)
        txt_wrk_cap.grid(column=4, row=11)
        txt_slp_cap = Entry(window, validate='key', textvariable=sv8, width=10)
        txt_slp_cap.grid(column=4, row=13)

        lbl_wrc_cap.configure(text="work cap")
        lbl_slp_cap.configure(text="sleep cap")
    else:
        if flag:
            txt_rep_cap.delete(0, END)
            txt_wrk_cap.delete(0, END)
            txt_slp_cap.delete(0, END)

        device = combo.get()
        if device == "Findy Bike":
            class_device = FB
        elif device == "Findy Car":
            class_device = FC
        elif device == "Findy Pet":
            class_device = F5
        elif device == "Findy Raid":
            class_device = FR
        elif device == "Water Meter":
            class_device = WaterMeter

        lbl_wrc_cap.configure(text=class_device.AVG_CURRENT_CONSUMPTION_WORK)
        lbl_slp_cap.configure(text=class_device.AVG_CURRENT_CONSUMPTION_SLEEP)

def check_valid_data():
    global flag1

    if len(txt_batt_cap.get()) == 0:
        lbl_iterations.configure(text="batt cap")
    elif len(txt_wrk_time.get()) == 0:
        lbl_iterations.configure(text="work time")
    elif len(txt_slp_time.get()) == 0:
        lbl_iterations.configure(text="sleep time")
    elif mode == '' or mode == ' ':
        lbl_iterations.configure(text="")
        lbl_iterations.configure(text="mode")
    else:
        flag1 = True

    if not combo.get() == "Test Device":
        if flag1:
            print_result()
    else:
        clicked_get_device()
        if flag1:

            if len(txt_wrk_cap.get()) == 0:
                lbl_lifetime.configure(text="Enter")
                lbl_iterations.configure(text="work cap")
            elif len(txt_slp_cap.get()) == 0:
                lbl_lifetime.configure(text="Enter")
                lbl_iterations.configure(text="sleep cap")
            else:
                print_result()
    if flag1 == False:
        lbl_lifetime.configure(text="Enter")

    print(mode)

def print_result():
    result_dict = {}
    clicked_get_device()

    global class_device
    batt_capacity = float(txt_batt_cap.get())
    work_time = float(txt_wrk_time.get()) / 3600
    sleep_time = float(txt_slp_time.get()) / 3600

    if len(txt_bsd.get()) == 0:
        batt_self_discharge = 0
    else:
        batt_self_discharge = float(txt_bsd.get())

    if len(txt_src_time.get()) == 0:
        search_time = 0
    else:
        search_time = float(txt_src_time.get()) / 3600

    if len(txt_src_cap.get()) == 0:
        search_cap = 0
    else:
        search_cap = float(txt_src_cap.get())

    device = combo.get()
    type_report = ws_cycles()

    if device == "Findy Bike":
        class_device = FB
    elif device == "Findy Car":
        class_device = FC
    elif device == "Findy Pet":
        class_device = F5
    elif device == "Findy Raid":
        class_device = FR
    elif device == "Water Meter":
        class_device = WaterMeter
    else:
        class_device = TestDevice

    if class_device == TestDevice:
        report_cap = float(txt_rep_cap.get())  # *******
        work_cap = float(txt_wrk_cap.get())
        sleep_cap = float(txt_slp_cap.get())

        result_dict["report cap"] = report_cap # ********
        result_dict["work cap"] = work_cap
        result_dict["sleep cap"] = sleep_cap

        #dev = class_device(batt_capacity, report_time, report_cap, sleep_time,
                           #sleep_cap, work_time, work_cap, type_report, batt_self_discharge)

        dev = class_device(batt_capacity, search_time, search_cap, sleep_time,
                           sleep_cap, work_time, work_cap, type_report, batt_self_discharge)
    else:
        dev = class_device(batt_capacity, search_time, sleep_time, work_time, type_report, batt_self_discharge)

        #result_dict["report cap"] = dev.AVG_CURRENT_CONSUMPTION_REPORT
        result_dict["work cap"] = dev.AVG_CURRENT_CONSUMPTION_WORK
        result_dict["sleep cap"] = dev.AVG_CURRENT_CONSUMPTION_SLEEP

    lbl_lifetime.configure(text=dev.batt_life_time)
    lbl_iterations.configure(text=dev.num_iterations)

    result_dict["batt self discharge"] = batt_self_discharge
    result_dict["cycling mode"] = type_report
    result_dict["device"] = device
    result_dict["batt capacity"] = batt_capacity
    result_dict["report time"] = txt_rep_time.get()
    result_dict["work time"] = txt_wrk_time.get()
    result_dict["sleep time"] = txt_slp_time.get()
    result_dict["lifetime"] = dev.batt_life_time
    result_dict["iterations"] = dev.num_iterations

    return result_dict

def print_r():
    global mode
    if len(mode) < 10 and not mode[-1] == "R":
        mode += "R"
    lbl100.configure(text=mode)

def print_s():
    global mode
    if len(mode) < 10 and not mode[-1] == "S":
        mode += "S"
    lbl100.configure(text=mode)

def print_w():
    global mode
    if len(mode) < 10 and not mode[-1] == "W":
        mode += "W"
    lbl100.configure(text=mode)

def search():
    global mode
    if len(txt_src_time.get()) > 0 and len(txt_src_cap.get()) > 0:
        if len(mode) < 10 and not mode[-1] == "C":
            mode += "C"
    lbl100.configure(text=mode)

def clean():
    global mode
    if len(mode) > 1:
        mode = mode[:-1]
    lbl100.configure(text=mode)

def validate_float(var):
    new_value = var.get()
    try:
        new_value == '' or float(new_value)
        validate_float.old_value = new_value
    except:
        var.set(validate_float.old_value)

def save():
    if flag1:
        generate_report()

def nums_protocol():
    try:
        open('my.nums')
    except:
        data = '1'
        with open('my.nums', 'w') as file:
            file.writelines(data)

    with open('my.nums', 'r') as f:
        x = f.read()
    x += '1'
    with open('my.nums', 'w') as file:
        file.writelines(x)
    return len(x)

def generate_report():
    info = {}
    info = print_result()
    window2 = Tk()
    window2.title("Saved data")
    window2.geometry("400x500")

    info["number protocol"] = nums_protocol()
    date = datetime.datetime.now()
    date_format = date.strftime("%Y-%m-%d %H:%M:%S")
    info["date"] = date_format
    print(info)
    print()

    num_protocol = info["number protocol"]
    data = info["date"]
    device = info["device"]
    batt_capacity = info["batt capacity"]
    report_time = info["report time"]
    report_cap = info["report cap"]
    work_time = info["work time"]
    work_cap = info["work cap"]
    sleep_time = info["sleep time"]
    sleep_cap = info["sleep cap"]
    batt_self_discharge = info["batt self discharge"]
    cycling_mode = info["cycling mode"]
    batt_life_time = info["lifetime"]
    iterations = info["iterations"]

    lbl_protocol = Label(window2, text=f"Num Protocol:{num_protocol}"
                                       f"\n\n Data: {data}"
                                       f"\n\n Device: {device}"
                                       f"\n\n Batt Capacity: {batt_capacity} mA"
                                       f"\n\n Report Time: {report_time}s"
                                       f"\n\n Avg report cap: {report_cap}mA"
                                       f"\n\n Work Time: {work_time}s"
                                       f"\n\n Avg work cap: {work_cap}mA"
                                       f"\n\n Sleep Time: {sleep_time}s "
                                       f"\n\n Avg sleep cap: {sleep_cap}mA"
                                       f"\n\n Batt Self Discharge: {batt_self_discharge} %"
                                       f"\n\n Cycling Mode: {cycling_mode}"
                                       f"\n\n Batt Life Time: {batt_life_time}"
                                       f"\n\n Iterations: {iterations}").pack()

    try:
        data = json.load(open('my.protocol'))
    except:
        data = []
    data.append(info)
    with open('my.protocol', 'w') as file:
        json.dump(data, file, indent=3)

def ws_cycles():
    global new_mode
    if len(txt_rep_freq.get()) > 0:
        report_frequency = int(txt_rep_freq.get())

        if "WS" in mode and not report_frequency == 0:
            pos = mode.index("WS")
            # coun = mode.count("WS")
            # if coun > 1:
            #     report_frequency -= coun
            new_mode = mode[:pos] + ("WS" * report_frequency) + mode[pos + 2:]

        else:
            new_mode = mode

    else:
        report_frequency = 0
        new_mode = mode

    print(new_mode)
    return new_mode

btn_device = Button(window,width=14, text="Device consumption", command=clicked_get_device)
btn_device.grid(column=4, row=6)
btn_calculate = Button(window, width=14, text="        Calculate       ", command=check_valid_data)
btn_calculate.grid(column=4, row=22)
btn_save = Button(window, width=6, text="Save", command=save).grid(column=4, row=25)

btn_report = Button(window, text="Report", command=print_r)
btn_report.grid(column=1, row=19)
btn_work = Button(window, text="Work", command=print_w)
btn_work.grid(column=2, row=19)
btn_sleep = Button(window, text="Sleep", command=print_s)
btn_sleep.grid(column=3, row=19)
btn_clean = Button(window, text="X", command=clean)
btn_clean.grid(column=3, row=18)
btn_src = Button(window, text="Search", command=search)
btn_src.grid(column=1, row=18)

btn_ws = Button(window, text="WS Cycles", command=ws_cycles ).grid(column=4, row=20)

validate_float.old_value = ''
window.mainloop()


import torch.nn as nn

class AlexNetEncoder(nn.Module):
    def __init__(self, additional_layers=False):
        super(AlexNetEncoder, self).__init__()

        network = [
            nn.Conv2d(3, 96, kernel_size=11, stride=4),
            nn.ReLU(True),
            nn.LocalResponseNorm(size=5, k=2),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(96, 256, kernel_size=5, padding=2, stride=1),
            nn.ReLU(True),
            nn.LocalResponseNorm(size=5, k=2),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(256, 384, kernel_size=3, padding=1, stride=1),
            nn.ReLU(True)
        ]

        if additional_layers:
            network += [
                nn.Conv2d(384, 384, kernel_size=3, padding=1, stride=1),
                nn.ReLU(True)
            ]

        self.network = nn.Sequential(*network)

    def forward(self, x):
        return self.network(x)

class AlexNetProcessor(nn.Module):
    def __init__(self):
        super(AlexNetProcessor, self).__init__()

        self.network = nn.Sequential(
            nn.Conv2d(384, 384, kernel_size=3, padding=1, stride=1),
            nn.ReLU(True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1, stride=1),
            nn.ReLU(True),
            nn.MaxPool2d(kernel_size=3, stride=2)
        )

    def forward(self, x):
        return self.network(x)

class AlexNetDecoder(nn.Module):
    def __init__(self, num_classes):
        super(AlexNetDecoder, self).__init__()

        self.network = nn.Sequential(
            nn.AdaptiveAvgPool2d((6, 6)),
            nn.Flatten(),
            nn.Dropout(),
            nn.Linear(256*6*6, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(True),
            nn.Linear(4096, num_classes)
        )

    def forward(self, x):
        return self.network(x)

def alexnet(num_classes, additional_layers=False):
    return \
        AlexNetEncoder(additional_layers), \
        AlexNetProcessor(), \
        AlexNetDecoder(num_classes)


# 0721
# idea: 시뮬레이션, 방향을 설정해주는 것이 핵심. 2차원 리스트를 선언할 때는 컴프리헨션을 이용하는 것이 효율적이다.
# BFS
from collections import deque
import sys

n, m = map(int, sys.stdin.readline().split())
x, y, d = map(int, sys.stdin.readline().split())
arr = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]

visited = [[False] * m for _ in range(n)]

# 상, 하, 좌, 우
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

def bfs(x, y):
    global ans
    q = deque()
    q.append((x, y))
    while q:
        x, y = q.popleft()
        visited[x][y] = True
        ans += 1
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < n and 0 <= ny < m and arr[nx][ny] == 0 and not visited[nx][ny]:
                visited[nx][ny] = True
                q.append((nx, ny))

ans = 0
bfs(x, y)
print(ans)

# DFS
import sys

n, m = map(int, sys.stdin.readline().split())
x, y, d = map(int, sys.stdin.readline().split())
arr = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]

visited = [[False] * m for _ in range(n)]

def dfs(x, y):
    global ans
    if 0 <= x < n and 0 <= y < m and arr[x][y] == 0 and not visited[x][y]:
        visited[x][y] = True
        ans += 1
        # 상, 하, 좌, 우
        dfs(x-1, y)
        dfs(x+1, y)
        dfs(x, y-1)
        dfs(x, y+1)

ans = 0
dfs(x, y)
print(ans)

# 구현
import sys
n, m = map(int, sys.stdin.readline().split())
x, y, d = map(int, sys.stdin.readline().split())
arr = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]

visited = [[False] * m for _ in range(n)]

dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

# 왼쪽으로 회전
def turn_left():
    global d
    d -= 1
    if d == -1:
        d = 3

ans = 1
turn_time = 0
visited[x][y] = True
while True:
    turn_left()
    nx = x + dx[d]
    ny = y + dy[d]
    # 회전한 이후 정면에 육지고 가보지 않은 경우
    if arr[nx][ny] == 0 and not visited[nx][ny]:
        visited[nx][ny] = True
        x, y = nx, ny
        turn_time = 0
        ans += 1
    # 회전한 이후 정면에 바다거나 가 본 경우
    else:
        turn_time += 1
    # 네 방향 모두 갈 수 없는 경우
    if turn_time == 4:
        nx = x - dx[d]
        ny = y - dy[d]
        if arr[nx][ny] == 0:
            x, y = nx, ny
        else:
            break
        turn_time = 0

print(ans)

# 회고: BFS/DFS/구현 순으로 만들어보았다. DFS가 가장 간결하지만 BFS가 가장 빠르지만 주어진 방향을 쓰지 않으므로 구현을 쓰는 게 좋을 것 같다.


import os

import cv2
import keras
import numpy as np
import tensorflow as tf

###############################################################################
#   Here are three example model submissions.
#
#   Each model takes an OpenCV image and the name of the
#   input image (not the path). The model then performs
#   some operation on the image and saves to the designated
#   output path. The output paths are where the organizers
#   will receive the results of each model. An empty output
#   folder will signify there was no submission for that
#   that model. You must submit one model and have it write
#   its output to /root/output0. DO NOT MODIFY.
#
#   How each model handles its IO, either from input
#   source / to output or its method calls is up to the
#   discretion of the performer. See description in
#   __main__ for some IO optimization for Docker caching
#   recommendations and an example of it.
#
#   Absolute Requirements:
#   1.  A minimum of one model must be submitted
#   2.  The model must load all input images from /root/input
#   3.  The model must modify and save a new copy of all input images
#   4.  All image names must remain the same.
#       The image compression/extension can be changed to any
#       Ubuntu supported .jpeg, .jpg, .tiff, .tif, .png format.
###############################################################################

try:
    model = keras.models.load_model('superres_v3.0_2018-01-25 22:38:29.338372.h5', custom_objects={"tf":tf})
except:
    model = keras.models.load_model('/root/performer/superres_v3.0_2018-01-25 22:38:29.338372.h5', custom_objects={"tf": tf})

#   An example model that makes no changes to the input image
def model0(img, name):
    save = os.path.join('/root', 'output0', name)
    print(save)
    print(img.shape)
    print(np.expand_dims(img[:,:,0:], 0).shape)
    size = 32
    apron = 2
    new_img = np.zeros((img.shape[0]*2 + 2*apron, img.shape[1]*2 + 2*apron, 3))
    img = cv2.copyMakeBorder(img, apron, apron, apron, apron, cv2.BORDER_REFLECT)
    for x in np.arange(0, img.shape[0], size - 2*apron):
        for y in np.arange(0, img.shape[1], size - 2*apron):

            # print(model.predict(np.ones((1,160,160,1))))
            chunk = img[x:x + size, y:y + size]
            new_chunk0 = model.predict(np.stack([chunk[:,:,0:1],chunk[:,:, 1:2], chunk[:,:,2:]]))[0]
            # print(chunk.shape, new_chunk0.shape)
            # if new_chunk0.shape[0]>4 and new_chunk0.shape[1]>4:
            # if new_img[2 * x+apron*2:2 * x + 2 * size-apron*2, 2 * y +apron*2:2 * y + 2 * size -apron*2, 0].shape == new_chunk0[0, apron*2:-apron*2, apron*2:-apron*2, 0]:
            if True:
                new_img[2 * x+apron*2:2 * x + 2 * size-apron*2, 2 * y +apron*2:2 * y + 2 * size -apron*2, 0] = new_chunk0[0, apron*2:-apron*2, apron*2:-apron*2, 0]
                new_img[2 * x+apron*2:2 * x + 2 * size-apron*2, 2 * y +apron*2:2 * y + 2 * size -apron*2, 1] = new_chunk0[1, apron*2:-apron*2, apron*2:-apron*2, 0]
                new_img[2 * x+apron*2:2 * x + 2 * size-apron*2, 2 * y +apron*2:2 * y + 2 * size -apron*2, 2] = new_chunk0[2, apron*2:-apron*2, apron*2:-apron*2, 0]
    # print(new_img)
    #
    cv2.imwrite(save, new_img)

#   An example model that performs a median blur on the input image
# def model1(img, name):
#     save = os.path.join('', 'root', 'output1', name)
#
#     cv2.medianBlur(img, 3)
#
#     cv2.imwrite(save, img)

#   An example model that switches channel order
#   and changes image compression on the input image
# def model2(img, name):
#     save = os.path.join('', 'root', 'output2', name)
#
#     red = img[:,:,2].copy()
#     blue = img[:,:,0].copy()
#     img[:,:,0] = red
#     img[:,:,2] = blue
#
#     cv2.imwrite(save.split('.')[0]+'.tif', img)

if __name__ == '__main__':
    #   This is the path to where the input images will be when
    #   this container is executed by the organizers. Changing
    #   this path will fail to produce results. DO NOT CHANGE.
    input_ = '/root/input'
    # input_ = './root/input'

    paths = os.listdir(input_)

    #   In this example, the loop iterates over all of the input
    #   images and loads the image and then provides a copy
    #   to each model. Other image IO methods and model function
    #   calls can be used. E.g. pass image path to the model.
    #
    #   If a model can perform on one image at a time without
    #   effecting the performance, then this style is the optimal
    #   loading paradim for Docker caching rather than doing the
    #   entire input image set for a model, then repeating for
    #   subsequent models.
    for p in paths:
        img = cv2.imread(os.path.join(input_, p))

        #   Submit a minimum of one model, but a
        #   second and third model is optional.
        model0(np.copy(img), p)
        #model1(np.copy(img), p)   # uncomment to use more than one model
        #model2(np.copy(img), p)   # uncomment to use more than one model
    from keras import backend as K
    K.clear_session()


# FATEC Ourinhos - Programação 1 - Seg Info
# Augusto Finotti Oliveira
# Prof. Rogério Lazanha
# 30/04/2022

# 12- Ler um numero inteiro. Se o numero lido for negativo, escreva a mensagem “Numero invalido”.
# Se o numero for positivo, calcular o logaritmo deste numero.

# Libraires
import math
# Input
num = int(input("Digite um número: "))
# Processing and Output
if(num>0):
    log = math.log(num)
    print("O logaritmo desse número é de: %.2f" %(log))
else:
    print("Número inválido")


import hashlib
import logging
from Bio.Seq import Seq
from modelseedpy import MSGenome
from modelseedpy_ext.re.etl.etl_transform_graph import ETLTransformGraph

logger = logging.getLogger(__name__)

def locate_feature_dna_sequence_in_contig(f, contigs):
    ll = []
    for l in f.data["location"]:
        contig_id, start, direction, seq_size = l
        if contig_id in contigs:
            off_set = seq_size
            sub_str_start = start - 1
            sub_str_end = sub_str_start + off_set
            if direction == "+":
                contig_sub_string = contigs[contig_id][0][
                    sub_str_start : (sub_str_start + seq_size)
                ]
                if contig_sub_string == f.dna_sequence:
                    ll.append(
                        [
                            contig_id,
                            f.id,
                            sub_str_start,
                            sub_str_start + seq_size,
                            False,
                            direction,
                        ]
                    )
                # print(f.id, contig_sub_string[:10], contig_sub_string[-10:])
                # print(f.id, f.dna_sequence[:10], f.dna_sequence[-10:])
            elif direction == "-":
                contig_sub_string = contigs[contig_id][1][
                    (sub_str_start - seq_size + 1) : (sub_str_start + 1)
                ]
                if contig_sub_string == f.dna_sequence[::-1]:
                    ll.append(
                        [
                            contig_id,
                            f.id,
                            sub_str_start - seq_size + 1,
                            sub_str_start + 1,
                            True,
                            direction,
                        ]
                    )
                # print(f.id, contig_sub_string[:10], contig_sub_string[-10:])
                # print(f.id, f.dna_sequence[::-1][:10], f.dna_sequence[::-1][-10:])
            else:
                print("not sure what this means:", direction)

            # print(f.id, l, f.functions, contig_sub_string == f.dna_sequence)
        else:
            print("contig not found:", contig_id)
    return ll

class ETLTransformKBaseGenome:

    def __init__(self, transform_info, transform_contigs, kb_re, kbase):
        self.transform_info = transform_info
        self.transform_contigs = transform_contigs
        self.kb_re = kb_re
        self.kbase = kbase

    def _process_contigs(self, assembly, handle_path='/scratch/fliu/data/kbase/handle/'):
        i = str(assembly.info).replace("/", "_")
        self.kbase.download_file_from_kbase2(
            assembly.fasta_handle_ref, f"{handle_path}/{i}"
        )
        assembly_contigs = MSGenome.from_fasta(f"{handle_path}/{i}")
        contig_set = [x.seq for x in assembly_contigs.features]

        return contig_set

    def _process_features(self, genome, n_genome_id, G):
        seq_dna_hashes = []
        seq_protein_hashes = []
        for f in genome.features:
            seq_dna = f.dna_sequence
            seq_protein = f.protein_translation
            h_dna = self.kb_re.dna_store.store_sequence(seq_dna)
            h_protein = self.kb_re.protein_store.store_sequence(seq_protein)
            seq_dna_hashes.append(h_dna)
            seq_protein_hashes.append(h_protein)

            node_seq_dna = G.add_transform_node(h_dna, "re_seq_dna")
            node_seq_protein = G.add_transform_node(h_protein, "re_seq_protein")

            G.add_transform_edge(n_genome_id, node_seq_dna.id, "ws_genome_has_seq_dna",
                                 {'feature_id': f.id, 'location': f.location})
            G.add_transform_edge(n_genome_id, node_seq_protein.id, "ws_genome_has_seq_protein",
                                 {'feature_id': f.id})

            seq_translation = str(Seq(seq_dna).translate())[:-1]
            if seq_translation == seq_protein:
                G.add_transform_edge(node_seq_dna.id, node_seq_protein.id,
                                     "re_seq_dna_has_translation")
            else:
                G.add_transform_edge(node_seq_dna.id, node_seq_protein.id,
                                     "re_seq_dna_has_partial_translation")

        hash_dna_set = hashlib.sha256(
            "_".join(sorted(seq_dna_hashes)).encode("utf-8")
        ).hexdigest()
        hash_protein_set = hashlib.sha256(
            "_".join(sorted(seq_protein_hashes)).encode("utf-8")
        ).hexdigest()

        node_seq_dna_set = G.add_transform_node(hash_dna_set, "re_seq_dna_set")
        node_seq_protein_set = G.add_transform_node(hash_protein_set, "re_seq_protein_set")

        G.add_transform_edge(n_genome_id, node_seq_dna_set.id,
                             "ws_genome_has_seq_dna_set")
        G.add_transform_edge(n_genome_id, node_seq_protein_set.id,
                             "ws_genome_has_seq_protein_set")

    def transform(self, genome):
        assembly = self.kbase.get_from_ws(genome.assembly_ref)

        contig_set = self._process_contigs(assembly)

        g_contigs = self.transform_contigs.transform(contig_set)

        g_genome_info = self.transform_info.transform(genome.info)
        g_assembly_info = self.transform_info.transform(assembly.info)

        n_contig_set_id = list(g_contigs.t_nodes['re_contig_set'])[0]

        n_assembly_id = list(g_assembly_info.t_nodes['kbase_object'])[0]
        n_genome_id = list(g_genome_info.t_nodes['kbase_object'])[0]

        g_compose = g_genome_info.concat(g_assembly_info)
        g_compose = g_compose.concat(g_contigs)
        g_compose.add_transform_edge(n_genome_id, n_assembly_id,
                                     "kbase_object_has_object_reference",
                                     {"value": "assembly_ref"})
        g_compose.add_transform_edge(n_assembly_id, n_contig_set_id,
                                     "kbase_object_has_re_contig_set")

        self._process_features(genome, n_genome_id, g_compose)

        return g_compose

class ETLTransformKBaseGenomeOld(ETLTransformGraph):
    def __init__(self, dna_store, protein_store):
        super().__init__()
        self.dna_store = dna_store
        self.protein_store = protein_store

    def transform(self, kb_genome, contigs):
        nodes = {}
        edges = {}

        def add_node(node_id, label, data=None):
            if label not in nodes:
                nodes[label] = {}
            if label in nodes:
                _node = self.build_node(node_id, label, data)
                if _node.id not in nodes[label]:
                    nodes[label][_node.id] = _node
                else:
                    # print('dup', _node.id)
                    pass

                return nodes[label][_node.id]

            logger.error("add_node error")
            return None

        def add_edge(node_from, node_to, label, data=None):
            if label not in edges:
                edges[label] = []
            if label in edges:
                _edge = self.transform_edge(node_from, node_to, data)
                edges[label].append(_edge)
                return _edge

            logger.error("add_edge error")
            return None

        _contigs = {}
        _contigs_hash = {}
        for contig in contigs.features:
            _contigs[contig.id] = [contig.seq, str(Seq(contig.seq).complement())]
            _contigs_hash[contig.id] = self.dna_store.store_sequence(contig.seq)

        print(len(_contigs))
        print(_contigs_hash)

        node_ws_genome_object = None
        if kb_genome.info:

            key = str(kb_genome.info).replace("/", ":")
            node_ws_genome_object = add_node(
                key,
                "ws_object_version",
                data={
                    "workspace_id": kb_genome.info.workspace_uid,
                    "object_id": kb_genome.info.uid,
                    "version": kb_genome.info.version,
                    "name": kb_genome.info.id,
                },
            )

        seq_dna_hashes = []
        seq_protein_hashes = []
        for f in kb_genome.features:
            seq_dna = f.dna_sequence
            seq_protein = f.protein_translation
            h_dna = self.dna_store.store_sequence(seq_dna)
            h_protein = self.protein_store.store_sequence(seq_protein)
            seq_dna_hashes.append(h_dna)
            seq_protein_hashes.append(h_protein)

            node_seq_dna = add_node(h_dna, "re_seq_dna", data=None)
            node_seq_protein = add_node(h_protein, "re_seq_protein", data=None)

            if node_ws_genome_object:
                add_edge(node_ws_genome_object, node_seq_dna, "ws_genome_has_seq_dna")
                add_edge(
                    node_ws_genome_object, node_seq_protein, "ws_genome_has_seq_protein"
                )

            seq_translation = str(Seq(seq_dna).translate())[:-1]
            if seq_translation == seq_protein:
                add_edge(node_seq_dna, node_seq_protein, "re_seq_dna_has_translation")
            else:
                add_edge(
                    node_seq_dna, node_seq_protein, "re_seq_dna_has_partial_translation"
                )

        hash_dna_set = hashlib.sha256(
            "_".join(sorted(seq_dna_hashes)).encode("utf-8")
        ).hexdigest()
        hash_protein_set = hashlib.sha256(
            "_".join(sorted(seq_protein_hashes)).encode("utf-8")
        ).hexdigest()

        print(hash_dna_set)
        print(hash_protein_set)

        node_seq_dna_set = add_node(hash_dna_set, "re_seq_dna_set", data=None)
        node_seq_protein_set = add_node(
            hash_protein_set, "re_seq_protein_set", data=None
        )

        add_edge(node_ws_genome_object, node_seq_dna_set, "ws_genome_has_seq_dna_set")
        add_edge(
            node_ws_genome_object, node_seq_protein_set, "ws_genome_has_seq_protein_set"
        )

        return nodes, edges

class ETLGenome(ETLTransformGraph):
    def __init__(self, transform_kbase_object, transform_contigs, kbase, handle_path='/home/fliu/kbase/cache/handle'):
        super().__init__()
        self.kbase = kbase
        self.transform_kbase_object = transform_kbase_object
        self.transform_contigs = transform_contigs
        self.handle_path = handle_path

    def etl(self, genome):
        nodes = {}
        edges = {}

        def add_node(node_id, label, data=None):
            if label not in nodes:
                nodes[label] = {}
            if label in nodes:
                _node = self.build_node(node_id, label, data)
                if _node.id not in nodes[label]:
                    nodes[label][_node.id] = _node
                else:
                    # print('dup', _node.id)
                    pass

                return nodes[label][_node.id]

            logger.error("add_node error")
            return None

        def add_edge(node_from, node_to, label, data=None):
            if label not in edges:
                edges[label] = []
            if label in edges:
                _edge = self.transform_edge(node_from, node_to, data)
                edges[label].append(_edge)
                return _edge

            logger.error("add_edge error")
            return None

        assembly = self.kbase.get_from_ws(genome.assembly_ref)

        g_nodes, g_edges = self.transform_kbase_object.transform(genome.info)
        if assembly:
            a_nodes, a_edges = self.transform_kbase_object.transform(assembly.info)
            node_genome = list(a_nodes["kbase_object"].values())[0]
            node_assembly = list(g_nodes["kbase_object"].values())[0]
            add_edge(
                node_genome,
                node_assembly,
                "kbase_object_has_object_reference",
                {"value": "assembly_ref"},
            )

            i = str(assembly.info).replace("/", "_")
            self.kbase.download_file_from_kbase2(
                assembly.fasta_handle_ref, f"{self.handle_path}/{i}"
            )
            assembly_contigs = MSGenome.from_fasta(f"{self.handle_path}/{i}")
            contig_set = [x.seq for x in assembly_contigs.features]

            c_nodes, c_edges = self.transform_contigs.transform(contig_set)

            node_contig_set = list(c_nodes["re_contig_set"].values())[0]

            add_edge(node_contig_set, node_assembly, "re_contig_set_in_object")


import os
import time
import util
import conv
import strassen
import strassenSeuil

def genMatrix(n):
	for i in range(0, 5):
		os.system("../gen_matrix/Gen " + str(n) + " " + str(i) + ".txt > dump.txt")

def evalMethod(func, matrices):
	times = []
	for i in range(0, 5):
		for j in range(i + 1, 5):
			init = time.time()
			m3 = func(matrices[i], matrices[j])
			end = time.time()
			times.append((end - init) * 1000)
	return times

def printStats(n, times, method):
	for i in times:
		print(method, n, i, sep="\t")

def eval():
	for n in range(1, 10):
		genMatrix(n)
		m = [None] * 5
		for j in range(0, 5):
			m[j] = util.readMatrix("./" + str(j) + ".txt")

		printStats(n, evalMethod(	conv.conv, m), "conv")
		printStats(n, evalMethod(strassen.strassen, m), "strassen")
		#evalMethod(strassenSeuil.strassenSeuil)

m = [None] * 5
for j in range(0, 5):
	m[j] = util.readMatrix("./" + str(j) + ".txt")

printStats(9, evalMethod(strassen.strassen, m), "strassen")


#!/usr/bin/python3
"""
check if an object is from a a class or a base class
"""

def inherits_from(obj, a_class):
    """
    lookup of the object in the class and the base class
    """

    return issubclass(type(obj), a_class) and type(obj) is a_class

if __name__ == "__main__":
    a = True
    if inherits_from(a, int):
        print("{} inherited from class {}".format(a, int.__name__))
    if inherits_from(a, bool):
        print("{} inherited from class {}".format(a, bool.__name__))
    if inherits_from(a, object):
        print("{} inherited from class {}".format(a, object.__name__))


import os
import numpy as np
import pands as pd
import rasterio as rio
import rasterio
from rasterio.windows import Window

class SITSPatches:
  def __init__(self, sits_path, output_dir, patch_size=0, year=0):
    # self.__init__()
    self.sits_path = sits_path
    self.out_dir = output_dir
    self.patch_size = patch_size
    self.year = year

    dtypes = np.dtype([("Patch_path", str),("Rows", int),("Columns", int)])
    self.dataframe = pd.DataFrame(np.empty(0, dtype=dtypes))

  def patch_sits(self):
    out_year_dir = os.path.join(self.out_dir, self.year)
    if not os.path.exists(out_year_dir):
        os.makedirs(out_year_dir)
    patches = []
    rows = []
    cols = []

    with rio.open(self.sits_path) as src:
      for x in range(0, src.width, self.patch_size):
          for y in range(0, src.height, self.patch_size):
              window = Window(x, y, self.patch_size, self.patch_size)
              patch = src.read(window=window) #-- ??, out_shape=(src.count, self.patch_size, self.patch_size)
              output_file = f'patch_{x}_{y}.tif'
              patch_path = os.path.join(out_year_dir, output_file)
              patches.append(patch_path)
              rows.append(x)
              cols.append(y)
              self.dataframe=pd.DataFrame({'Patch_path': patches, 'Rows': rows, 'Columns': cols})

              with rio.open(patch_path,
                            'w',
                            driver='GTiff',
                            width=self.patch_size,
                            height=self.patch_size,
                            count=src.count,
                            dtype=patch.dtype,
                            crs=src.crs,
                            transform=src.window_transform(window)
                            ) as dst:
                  dst.write(patch)

    self.dataframe.to_csv(os.path.join(out_year_dir, f'df_patches_{self.year}.csv'), index=False)

project_dir = "/share/projects/erasmus/pasteis"
for y in ['data/2018/2018_subset.tif', 'data/2019/2019_subset.tif']:
    patches_ = SITSPatches(sits_path=os.path.join(project_dir, y),output_dir = os.path.join(project_dir,"/prepData/patches"), patch_size = 128, year=str(y))
    patches_.patch_sits()

# if __name__ == "__main__" :
#     project_dir = "/share/projects/erasmus/pasteis"
#     for y in ['data/2018/2018_subset.tif', 'data/2019/2019_subset.tif']:
#         patches_ = SITSPatches(sits_path=os.path.join(project_dir, y),output_dir = os.path.join(project_dir,"/prepData/patches"), patch_size = 32, year=str(y))
#         patches_.patch_sits()


"""
TwitterClient handles interaction with Twitter API.
"""

class TwitterClient:
    """
    TwitterClient handles interaction with Twitter API.
    """
    def __init__(self, AT, AT_S, CON, CON_S):
        import twitter
        self.twitter = twitter.api.Api(consumer_key=CON,
                                       consumer_secret=CON_S,
                                       access_token_key=AT,
                                       access_token_secret=AT_S,
                                       tweet_mode='extended')

    def get_tweets(self, account, initial_date=None, range_date=0, file="rawtweets.csv"):
        """
        Get Tweets and put them in a temp_file.
        """
        import datetime as dt

        if initial_date == dt.datetime.now().strftime("%Y-%m-%d"):
            query = "q=to%3A" + account + " %40" + account +\
                    " since%3A" + initial_date +\
                    "&count=100"
        else:
            until = dt.datetime.strptime(initial_date, "%Y-%m-%d") + dt.timedelta(days=range_date)
            query = "q=to%3A" + account + " %40" + account +\
                    " since%3A" + initial_date+\
                    " until%3A" + until.strftime("%Y-%m-%d")+"&count=100"

        feed = self.twitter.GetSearch(raw_query=query)
        text_array = []
        for tweet in feed:
            if tweet.full_text:
                text_array.append(tweet.full_text)
            else:
                text_array.append(tweet.text)

        self.save_tweets_csv(text_array, file)

    def clean_data(self, path="alltweets.csv", debug=False):
        """Clean the data you got from getTweets.
        Return a dictionary of cleansed tweets with assigned value
        """
        import unidecode
        symbols = list('''!()-[]{};:+'´"\\,<>.=/?@#$%^&*_~''')

        data = self.open_tweets_csv(path)
        json_data = self.open_stop_words('stopwords.json')

        clean_data = {}
        for i, value in data.items():
            words_array = value[0].split(" ")
            clean_words = []
            for word in words_array:
                word_uni = unidecode.unidecode(word.lower())
                if word_uni and word_uni not in json_data and \
                word[0] != '@' and word[0] != '#' and word_uni.find("http"):
                    for letter in word_uni:
                        if letter in symbols:
                            word_uni = word_uni.replace(letter, '')
                    word_uni = word_uni.replace('\n', '')
                    word_length = len(word_uni)
                    if word_length > 0 and clean_data != "":
                        clean_words.append(word_uni)
            clean_data[i] = [" ".join(clean_words), value[1]]

        if debug:
            self.pretty_print(clean_data)
        return clean_data

    def open_stop_words(self, path):
        """Returns stop words as a json dictionary.
        """
        import json
        with open(path) as json_file:
            json_data = json.load(json_file)
        return json_data

    def save_tweets_csv(self, data, path="rawtweets.csv"):
        """Save Tweets as CSV file.
        """
        import pandas as pd
        dataframe = pd.DataFrame(data, columns=["Tweet"])
        dataframe["Value"] = 'Undef'
        dataframe.to_csv(path, index=False, encoding='utf-8')

    def open_tweets_csv(self, path):
        """Open tweets and return them as a dictionary
        """
        import csv
        graded_tweets = {}
        with open(path) as csvfile:
            read_csv = csv.reader(csvfile, delimiter=',')
            next(read_csv, None)
            for i, row in enumerate(read_csv):
                if len(row) == 2:
                    graded_tweets[i] = [row[0], row[1]]

        return graded_tweets

    def pretty_print(self, data):
        """Pretty print interface.
        """
        import pprint

        pretty_printer = pprint.PrettyPrinter(indent=4)
        pretty_printer.pprint(data)


from tkinter import *

#instantiates an object of class Tk
root = Tk()

#sets up window title and size
root.title("This is a title")
root.geometry("500x300")

#sets up window to start listening for events
root.mainloop()


import os
import random
from collections import OrderedDict
from datetime import datetime

import numpy as np
import torch
from torchvision import transforms
from tqdm import tqdm

import utils
from BB.models.BB_Inception_V3 import get_model_isic
from Logger.logger_cubs import Logger_CUBS
from dataset.dataset_ham10k import load_ham_data

def train(args):
    device = utils.get_device()
    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    root = f"lr_{args.lr}_epochs_{args.epochs}_optim_{args.optim}"
    chk_pt_path = os.path.join(args.checkpoints, args.dataset, "BB", root, args.arch)
    output_path = os.path.join(args.output, args.dataset, "BB", root, args.arch)
    tb_logs_path = os.path.join(args.logs, args.dataset, "BB", f"{root}_{args.arch}")
    os.makedirs(chk_pt_path, exist_ok=True)
    os.makedirs(output_path, exist_ok=True)
    os.makedirs(tb_logs_path, exist_ok=True)

    print(f"output_path: {output_path}")
    os.makedirs(output_path, exist_ok=True)

    normalize = transforms.Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]
    )
    transform = transforms.Compose(
        [
            transforms.Resize(299),
            transforms.CenterCrop(299),
            transforms.ToTensor(),
            normalize
        ]
    )

    model, model_bottom, model_top = get_model_isic(args.bb_dir, args.model_name)
    train_loader, val_loader, idx_to_class = load_ham_data(args, transform, args.class_to_idx)
    criterion = torch.nn.CrossEntropyLoss()
    solver = None
    if args.optim == "SGD":
        solver = torch.optim.SGD(
            model.parameters(),
            lr=args.lr,
            momentum=args.momentum,
            weight_decay=args.weight_decay
        )
    elif args.optim == "Adam":
        solver = torch.optim.Adam(
            filter(lambda p: p.requires_grad, model.parameters()),
            lr=args.lr, weight_decay=args.weight_decay
        )

    final_parameters = OrderedDict(
        arch=[args.arch],
        dataset=[args.dataset],
        now=[datetime.today().strftime('%Y-%m-%d-%HH-%MM-%SS')]
    )
    run_id = utils.get_runs(final_parameters)[0]
    run_manager = Logger_CUBS(
        1, chk_pt_path, tb_logs_path, output_path, train_loader, val_loader, len(args.labels)
    )

    model = fit(
        args,
        model,
        criterion,
        solver,
        train_loader,
        val_loader,
        run_manager,
        run_id,
        device
    )

    test_and_save_model(
        model, train_loader, val_loader, idx_to_class, output_path, args.labels, device
    )

def fit(
        args,
        net,
        criterion,
        solver,
        train_loader,
        val_loader,
        run_manager,
        run_id,
        device
):
    run_manager.begin_run(run_id)
    for epoch in range(args.epochs):
        run_manager.begin_epoch()
        net.train()
        with tqdm(total=len(train_loader)) as t:
            for batch_id, (images, target) in enumerate(train_loader):
                solver.zero_grad()
                images = images.to(device)
                target = target.to(device)
                y_hat = net(images)
                y_hat = y_hat.logits
                train_loss = criterion(y_hat, target)
                train_loss.backward()
                solver.step()

                run_manager.track_train_loss(train_loss.item())
                run_manager.track_total_train_correct_per_epoch(y_hat, target)
                t.set_postfix(
                    epoch='{0}'.format(epoch),
                    training_loss='{:05.3f}'.format(run_manager.epoch_train_loss))
                t.update()

        net.eval()
        out_put_GT = torch.FloatTensor().cuda()
        out_put_predict_bb = torch.FloatTensor().cuda()
        with torch.no_grad():
            with tqdm(total=len(val_loader)) as t:
                for batch_id, (images, target) in enumerate(val_loader):
                    images = images.to(device)
                    target = target.to(device)
                    y_hat = net(images)
                    val_loss = criterion(y_hat, target)

                    run_manager.track_val_loss(val_loss.item())
                    run_manager.track_total_val_correct_per_epoch(y_hat, target)

                    out_put_predict_bb = torch.cat((out_put_predict_bb, y_hat), dim=0)
                    out_put_GT = torch.cat((out_put_GT, target), dim=0)
                    t.set_postfix(
                        epoch='{0}'.format(epoch),
                        validation_loss='{:05.3f}'.format(run_manager.epoch_val_loss))
                    t.update()

        run_manager.end_epoch(net)
        proba = torch.nn.Softmax(dim=1)(out_put_predict_bb)[:, 1]
        val_auroc, val_aurpc = utils.compute_AUC(out_put_GT, pred=proba)
        print(f"Epoch: [{epoch + 1}/{args.epochs}] "
              f"Train_loss: {round(run_manager.get_final_train_loss(), 4)} "
              f"Train_Accuracy: {round(run_manager.get_final_train_accuracy(), 4)} (%) "
              f"Val_loss: {round(run_manager.get_final_val_loss(), 4)} "
              f"Val_Accuracy: {round(run_manager.get_final_val_accuracy(), 4)} (%) "
              f"Best_Val_Accuracy: {round(run_manager.get_final_best_val_accuracy(), 4)} (%)  "
              f"Val_AUROC: {round(val_auroc, 4)} (%)  "
              f"Val_AURPC: {round(val_aurpc, 4)} (%)  "
              f"Epoch_Duration: {round(run_manager.get_epoch_duration(), 4)}")

    run_manager.end_run()

    return net

def test_and_save_model(
        model, train_loader, val_loader, idx_to_class, output_path, labels, device
):
    model = model.eval()
    print(f"Train dataset size: {len(train_loader.dataset)}")
    print(f"Val dataset size: {len(val_loader.dataset)}")
    print(idx_to_class)

    out_put_GT = torch.FloatTensor().cuda()
    out_put_predict_bb = torch.FloatTensor().cuda()
    with tqdm(total=len(val_loader)) as t:
        for batch in enumerate(val_loader):
            batch_idx, (images, target) = batch
            images = images.to(device)
            target = target.to(device)
            with torch.no_grad():
                y_hat_bb = model(images)

            out_put_predict_bb = torch.cat((out_put_predict_bb, y_hat_bb), dim=0)
            out_put_GT = torch.cat((out_put_GT, target), dim=0)

            t.set_postfix(iteration=f"{batch_idx}")
            t.update()

    proba = torch.nn.Softmax(dim=1)(out_put_predict_bb)[:, 1]
    val_auroc, val_aurpc = utils.compute_AUC(out_put_GT, pred=proba)

    out_put_GT_np = out_put_GT.cpu().numpy()
    y_hat_bb = out_put_predict_bb.cpu().argmax(dim=1)
    acc_bb = utils.cal_accuracy(out_put_GT_np, y_hat_bb)
    cls_report = utils.cal_classification_report(out_put_GT_np, y_hat_bb, labels)
    print(f"Accuracy of the network: {acc_bb * 100} (%)")
    print(f"Val AUROC of the network: {val_auroc} (0-1)")
    print(cls_report)

    np.save(os.path.join(output_path, f"out_put_GT_prune.npy"), out_put_GT_np)
    torch.save(out_put_predict_bb.cpu(), os.path.join(output_path, f"out_put_predict_logits_bb.pt"))
    torch.save(y_hat_bb, os.path.join(output_path, f"out_put_predict_bb.pt"))
    print(os.path.join(output_path, f"out_put_predict_bb.pt"))
    # np.save(os.path.join(output_path, f"out_put_predict_bb_prune.npy"), y_hat_bb)
    utils.dump_in_pickle(
        output_path=output_path, file_name="classification_report.pkl", stats_to_dump=cls_report
    )


import multiprocessing

import torch
from torch.utils.data import DataLoader

from tests.gfl_test.model import Net, MnistOptimizer, CrossEntropyLoss
from tests.gfl_test.dataset import mnist_dataset

GPU = "cuda:0"

def train(step):
    print("Start step %d" % step)
    model = Net().to(GPU)
    optimizer = MnistOptimizer(model.parameters(), lr=0.01)
    loss_f = CrossEntropyLoss()
    dataloader = DataLoader(mnist_dataset, batch_size=32, shuffle=True)

    for e in range(3):
        correct = 0
        total = 0
        loss_v = 0
        iter_num = 0

        for i, data in enumerate(dataloader):
            inputs, labels = data
            inputs = inputs.to(GPU)
            labels = labels.to(GPU)

            optimizer.zero_grad()

            outputs = model(inputs)
            loss = loss_f(outputs, labels)

            if torch.is_grad_enabled():
                loss.backward()
                optimizer.step()

            loss_v += loss.item()
            iter_num += 1

            _, pred = torch.max(outputs, 1)
            c = (pred == labels).squeeze()
            for i, lb in enumerate(labels):
                correct += c[i].item()
                total += 1

        print("[%d] EPOCH %d OF %d" % (step, e, 3))
        print("[%d] \tLoss: %s, Acc: %s" % (step, loss_v / iter_num, correct / total))

    print("End step %d" % step)

if __name__ == "__main__":
    """
    t1 = threading.Thread(target=train, args=(1, ))
    t2 = threading.Thread(target=train, args=(2, ))
    t3 = threading.Thread(target=train, args=(3, ))
    """
    t1 = multiprocessing.Process(target=train, args=(1, ))
    t2 = multiprocessing.Process(target=train, args=(2, ))
    t3 = multiprocessing.Process(target=train, args=(3, ))
    t1.start()
    t2.start()
    t3.start()
    """
    t1.join()
    t2.join()
    t3.join()
    """

    # train(1)
    pass


from collections import deque

n, m = map(int, input().split())
graph = [set() for _ in range(n + 1)]       # 현재 가수 다음에 나올 가수들의 집합
in_degree = [set() for _ in range(n + 1)]   # 현재 가수 이전에 나와야 할 가수들의 집합(다 담고 길이로 바꿔줄 예정)

for i in range(m):
    temp = list(map(int, input().split()))      # PD가 정한 순서
    for i in range(1, len(temp)):
        graph[temp[i]] = graph[temp[i]] | set(temp[i + 1:])     # 다음에 나올 가수들 합집합
        in_degree[temp[i]] = in_degree[temp[i]] | set(temp[1:i])    # 이전에 나올 가수들 합집합

que = deque()                   # 순서에 따라 가수들을 담을 큐
for i in range(1, n + 1):
    in_degree[i] = len(in_degree[i])        # 집합을 길이로 바꿔 위상 정렬
    if in_degree[i] == 0:       # 위상이 0인(가장 먼저 나와도 되는 가수) 가수를 먼저 담는다.
        que.append(i)

result = []                     # 순서대로 출력할 result 배열(순서를 정하는게 불가능한 경우를 대비)
while que:
    cur = que.popleft()         # 위상이 0인 가수들을 pop
    result.append(cur)          # result에 가수들을 담아준다.
    for nxt in graph[cur]:      # 현재 가수 이후에 나오는 가수들을 순회
        in_degree[nxt] -= 1     # 다음에 나오는 가수들의 위상을 1씩 줄여준다.
        if in_degree[nxt] == 0: # 위상이 0인 가수들을 큐에 담는다.
            que.append(nxt)

if len(result) != n:            # 불가능한 경우
    print(0)
else:                           # 가능한 경우
    for singer in result:
        print(singer)


a = input()
n = len(a)
c = 0
ans = 0

for i in range(n-1,-1,-1):
    if a[i] == 'g':
        c += 1
    elif a[i] == 'a':
        ans += c
        ans = ans % 1000000007
print(ans)


"""
**Instruction**
Please see instruction document.

"""
from BST_Helper import *

def P1(root: TreeNode, low: int, high: int) -> int:
    ret = 0
    if low <= root.val <= high:
        ret += root.val
    if root.right and root.val < high:
        ret += P1(root.right, low, high)
    if root.left and root.val > low:
        ret += P1(root.left, low, high)
    return ret
    ##### End of your code #####


import json
from django.shortcuts import render
from django.conf import settings
from .models import Dewey, Publication

# Create your views here.

CONTEXT_GLOBAL = {
    "mediatheque_name": "Bibliothèque de Bebounet",
    "mediatheque_adr": "Aramon",
    "dev_name": "Nicolas Sagnier",
    "dev_github": "https://github.com/Bebounet/babel-bebounet",
    "dev_cadre": "Formation Django/Python FORMEXT",
}

def publication(request):

    record = Dewey.objects.get(number="100")
    record_list = Dewey.objects.all()

    publication_list = Publication.objects.all()

    context_local = {
        "title": "Liste des publications du catalogue",
        "description": "Vous trouverez tous les ouvrages et leurs classifications",
    }
    context_page = {
        "global": CONTEXT_GLOBAL,
        "local": context_local,
        "dewey_object": record,
        "dewey_object_list": record_list,
        "publication_object_list": publication_list,
    }
    return render(request, "catalog/publication.html", context=context_page)

def home(request):
    context_local = {
        "title": "Page d'accueil de Babel",
        "description": "Bienvenue sur cette page en cours de réalisation",
    }
    context_page = {"global": CONTEXT_GLOBAL, "local": context_local}
    return render(request, "catalog/index.html", context=context_page)

def about(request):
    context_local = {
        "title": "A propos de Babel",
        "description": "Vous trouverez tout les détails de spécification ici.",
    }
    context_page = {"global": CONTEXT_GLOBAL, "local": context_local}
    return render(request, "catalog/about.html", context=context_page)

def newsroom(request):
    """ The home page for babel"""

    basedir = settings.BASE_DIR
    filename = basedir + "/scrap/checkurl.json"
    try:
        with open(filename, "r") as f:
            dict_checkurl = json.load(f)
    except Exception as e:
        dict_checkurl = {"error", str(e)}
    context_local = {
        "title": "Salle de presse",
        "description": "Decouvrez des sites !",
    }

    context_page = {
        "global": CONTEXT_GLOBAL,
        "checkurl": dict_checkurl,
        "local": context_local,
    }
    #  Pour ajouter un dictionnaire a un dictionnaire, j'utilise
    #  bigdict = { **onedict, **anotherdict }
    return render(request, "catalog/newsroom.html", context=context_page)


from sqlalchemy import Column, Integer, String, Text, Sequence, ForeignKey, Date, Boolean, create_engine

from extension.utils import get_tools
from upd.models import db
from .tool import tool as UPD
import time

@UPD.init_task
def self_check(tool, *args, **kwargs):
    console = kwargs.get('console')
    console.out('self check')
    console.out('checking version: v1.2.0 (already up to date)')

@UPD.init_task
def count_one_to_ten(tool, *args, **kwargs):
    console = kwargs.get('console')
    ten =  10
    for i in range(ten):
        console.progress('count one to ten', i, ten)
        time.sleep(0.3)

    console.done()

@UPD.init_task
def initialize_db(tool, *args, **kwargs):
    console = kwargs.get('console')
    console.line('initialize_database')

    total = len(get_tools())
    with console.block() as block:
        for i, tool in enumerate(get_tools()):
            block.progress('creating tables', i, total)
            db.create_tables(tool.models)

    block.done()


# Set the python path
import inspect
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))))

import threading
from http.server import HTTPServer
import socket
import time

from prometheus.collectors import Gauge
from prometheus.registry import Registry
from prometheus.exporter import PrometheusMetricHandler
import psutil

PORT_NUMBER = 4444

def gather_data(registry):
    """Gathers the metrics"""

    # Get the host name of the machine
    host = socket.gethostname()

    # Create our collectors
    ram_metric = Gauge("memory_usage_bytes", "Memory usage in bytes.",
                       {'host': host})
    cpu_metric = Gauge("cpu_usage_percent", "CPU usage percent.",
                       {'host': host})

    # register the metric collectors
    registry.register(ram_metric)
    registry.register(cpu_metric)

    # Start gathering metrics every second
    while True:
        time.sleep(1)

        # Add ram metrics
        ram = psutil.virtual_memory()
        swap = psutil.swap_memory()

        ram_metric.set({'type': "virtual", }, ram.used)
        ram_metric.set({'type': "virtual", 'status': "cached"}, ram.cached)
        ram_metric.set({'type': "swap"}, swap.used)

        # Add cpu metrics
        for c, p in enumerate(psutil.cpu_percent(interval=1, percpu=True)):
            cpu_metric.set({'core': c}, p)

if __name__ == "__main__":

    # Create the registry
    registry = Registry()

    # Create the thread that gathers the data while we serve it
    thread = threading.Thread(target=gather_data, args=(registry, ))
    thread.start()

    # Set a server to export (expose to prometheus) the data (in a thread)
    try:
        # We make this to set the registry in the handler
        def handler(*args, **kwargs):
            PrometheusMetricHandler(registry, *args, **kwargs)

        server = HTTPServer(('', PORT_NUMBER), handler)
        server.serve_forever()

    except KeyboardInterrupt:
        server.socket.close()
        thread.join()