import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from tqdm import *
import time
import data_utils
import random_walk
from linear_td_lamda import Linear_TD

######################################################################
### (1) Generate random walk training sequences (100 training sets with 10 sequences each)
seedFac = 10
walks = random_walk.generate_walks(1000, seedFac)
training_folds = random_walk.split_training_sets(walks, 10)

### (2) The ideal predictions for the 7 state random-walk are the weights we want to learn,
# that are true probabilities of the walk terminating at state G.
# Note that these values are external to the learning problem (as they requre full knowledge of the MDP),
# and are only used to assess the model.
ideal_predictions = np.array(np.linspace(1./6., 5./6., 5), dtype=np.float64)
rmse = lambda x, y: np.sqrt(np.mean((x-y)**2), dtype = np.float64)

# ## (3) Extracted into numeric csv files from the plots in the paper using webplotdigitizer
# ## (http://arohatgi.info/WebPlotDigitizer/)
fig3 = data_utils.load_fig('fig3.csv')
fig4_lambdas, fig4 = data_utils.load_fig4('fig4')
fig5 = data_utils.load_fig('fig5.csv')

# ## (4) Experiment 1 (Sutton88 figure 3)
# # ex1_lambda = [0.,.1,.3,.5,.7,.9,1.]
# ex1_lambda= np.array([0.,.1,.3,.5,.7,.9,1.], dtype=np.float64)
# ex1_rmse = []
# ex1_sigma = []
# alpha = .025  ### larger aloha learns faster, but if it too large, overshoot.
# epsilon = .03     ### smaller, converge better, but will overfit if too small.
# # epsilon = .01
#
# # E =[0.1, 0.07, 0.05, 0.03,0.01, 0.001, 0.0001]
#
# print "alpha = ", alpha
# print "eps = ", epsilon
#
# start = time.time()
# for L in ex1_lambda:
#     L_rmse = []
#     for f in range(len(training_folds)):
#         td = Linear_TD(lam = L, learning_rate=alpha, epsilon=epsilon)
#         td.fit(training_folds[f])
#         L_rmse.append(rmse(ideal_predictions, td.w[1:6]))
#     ex1_rmse.append(np.mean(L_rmse))
#     ex1_sigma.append(np.std(L_rmse))
# end = time.time()
# ex1_sigma /= np.sqrt(len(training_folds))
#
# print "eps = ", epsilon, '>>>>>>>>'
# print ex1_rmse
# print "time: ", end - start
# print 'ex1_sigma:', ex1_sigma
# print '-----------------'
#
#
#
# plt.plot(ex1_lambda, ex1_rmse, 'go-')
# # plt.errorbar(ex1_lambda, ex1_rmse, yerr=ex1_sigma, fmt='--o', ecolor='g')
# # plt.title('a='+str(alpha) + ', eps=' + str(epsilon) +
# #           'Average Error of Random Walk Problem Under Repeated Presentations', fontsize='10')
# plt.ylabel('RMS Error', fontsize='13')
# plt.xlabel('$\lambda$', fontsize='13')
# plt.grid()
# plt.xlim(-0.1,1.1)
# plt.show()
# quit()

###############################################
### (5) Experiment 2
ex2a_alpha = np.linspace(0., .6, 13)
ex2a_lambda = np.array(fig4_lambdas).astype(np.float)
ex2a_rmse = []
epsilon = .03
T=[]

for L in ex2a_lambda:
    alphas = []
    for a in ex2a_alpha:
        rmses = []
        for f in range(len(training_folds)):
            td = Linear_TD(lam = L, learning_rate=a, epsilon=epsilon, incremental_updates=True)
            td.fit(training_folds[f])
            rmses.append(rmse(ideal_predictions, td.w[1:6]))
        alphas.append(np.mean(rmses))
    ex2a_rmse.append(alphas)

ex2a_points = np.asarray(ex2a_rmse).T
max_rmse = .7
ex2a_points[ex2a_points > max_rmse] = np.nan
#
# plt.plot(ex2a_alpha, ex2a_points,  'o-')
# plt.ylabel('RMS Error', fontsize='12')
# plt.xlabel(r'$\alpha$', fontsize='20')
# plt.title("seedFac: " + str(seedFac))
# plt.grid()
# plt.xlim(-0.1,0.7)
# legend = ['$\lambda$=' + str(l) for l in fig4_lambdas]
# plt.legend(legend, loc='best')
# plt.show()

###############################################
### (6) Experiment 2-2
#iterate over all<lambda,alpha> and select the best alpha for each lambda ex2b_lambda = np.linspace(0., 1., 11)
ex2b_lambda = np.linspace(0., 1., 11)
ex2b_alpha = np.linspace(0., .6, 13)
optimal_params = []

for i in tqdm(range(len(ex2b_lambda))):
    L = ex2b_lambda[i]
    best_alpha = -1.
    lowest_err = float('inf')
    for a in ex2b_alpha:
        rmses = []
        for f in training_folds:
            td = Linear_TD(lam = L, learning_rate=a, incremental_updates=True)
            td.fit(f)
            rmses.append(rmse(ideal_predictions, td.w[1:6]))
        avg_err = np.mean(rmses)
        if avg_err < lowest_err:
            best_alpha = np.asscalar(a)
            lowest_err = avg_err
    optimal_params.append((L, best_alpha))
optimal_params = np.round(optimal_params, decimals=2).tolist()

##### Now we can finally run using the optimal parameter tuples!
# epsilon = .05
ex2b_rmse = []
ex2b_sigma = []
for L, a in optimal_params:
    rmses = []
    for f in tqdm(range(len(training_folds))):
        td = Linear_TD(lam = L, learning_rate=a, incremental_updates=True)
        td.fit(training_folds[f])
        rmses.append(rmse(ideal_predictions, td.w[1:6]))
    ex2b_rmse.append(np.mean(rmses))
    ex2b_sigma.append(np.std(rmses))
ex2b_sigma /= np.sqrt(len(training_folds))

plt.plot(ex2b_lambda, ex2b_rmse, 'go-')
# plt.errorbar(ex2b_lambda, ex2b_rmse, yerr=ex2b_sigma, fmt='--o', ecolor='g')
plt.title('Average Error at Best Alpha on Random Walk Problem', fontsize='10')
plt.ylabel('RMS Error', fontsize='13')
plt.xlabel('$\lambda$', fontsize='13')
plt.grid()
plt.xlim(-0.1,1.1)
plt.show()


from mmdet.apis import init_detector, inference_detector, show_result
import mmcv
import cv2
import numpy as np

config_file = 'configs/refrigerator/faster_rcnn_r50_fpn_1x.py'
checkpoint_file = 'work_dirs/latest.pth'

# build the model from a config file and a checkpoint file
model = init_detector(config_file, checkpoint_file, device='cuda:0')

# test a single image and show the results
img = '/home/zhou/project/refrigerator/data/0221coco/test2017/binocular_72_right.jpg'  # or img = mmcv.imread(img), which will only load it once
cv_img = cv2.imread(img)
result = inference_detector(model, img)

bbox_result = result
bboxes = np.vstack(bbox_result)
labels = [
        np.full(bbox.shape[0], i, dtype=np.int32)
        for i, bbox in enumerate(bbox_result)
    ]
labels = np.concatenate(labels)
print(bboxes)
print(labels)
print(bboxes[:,-1]>0.3)
mmcv.imshow_det_bboxes(
        img,
        bboxes,
        labels,
        class_names=model.CLASSES,
        score_thr=0.3,
        show=True,
        wait_time=0,
        out_file=None)
class LoadImage(object):

    def __call__(self, results):
        if isinstance(results['img'], str):
            results['filename'] = results['img']
        else:
            results['filename'] = None
        img = mmcv.imread(results['img'])
        results['img'] = img
        results['img_shape'] = img.shape
        results['ori_shape'] = img.shape
        return results

def inference_detector(model, img):
    """Inference image(s) with the detector.

    Args:
        model (nn.Module): The loaded detector.
        imgs (str/ndarray or list[str/ndarray]): Either image files or loaded
            images.

    Returns:
        If imgs is a str, a generator will be returned, otherwise return the
        detection results directly.
    """
    cfg = model.cfg
    device = next(model.parameters()).device  # model device
    # build the data pipeline
    test_pipeline = [LoadImage()] + cfg.data.test.pipeline[1:]
    test_pipeline = Compose(test_pipeline)
    # prepare data
    data = dict(img=img)
    data = test_pipeline(data)
    data = scatter(collate([data], samples_per_gpu=1), [device])[0]
    # forward the model
    with torch.no_grad():
        result = model(return_loss=False, rescale=True, **data)
    return result

'''
# visualize the results in a new window
show_result(img, result, model.CLASSES)
# or save the visualization results to image files
show_result(img, result, model.CLASSES, out_file='result.jpg')

# test a video and show the results
video = mmcv.VideoReader('video.mp4')
for frame in video:
    result = inference_detector(model, frame)
    show_result(frame, result, model.CLASSES, wait_time=1)
'''
def show_result(img,
                result,
                class_names,
                score_thr=0.3,
                wait_time=0,
                show=True,
                out_file=None):
    """Visualize the detection results on the image.

    Args:
        img (str or np.ndarray): Image filename or loaded image.
        result (tuple[list] or list): The detection result, can be either
            (bbox, segm) or just bbox.
        class_names (list[str] or tuple[str]): A list of class names.
        score_thr (float): The threshold to visualize the bboxes and masks.
        wait_time (int): Value of waitKey param.
        show (bool, optional): Whether to show the image with opencv or not.
        out_file (str, optional): If specified, the visualization result will
            be written to the out file instead of shown in a window.

    Returns:
        np.ndarray or None: If neither `show` nor `out_file` is specified, the
            visualized image is returned, otherwise None is returned.
    """
    assert isinstance(class_names, (tuple, list))
    img = mmcv.imread(img)
    img = img.copy()
    if isinstance(result, tuple):
        bbox_result, segm_result = result
    else:
        bbox_result, segm_result = result, None
    bboxes = np.vstack(bbox_result)
    labels = [
        np.full(bbox.shape[0], i, dtype=np.int32)
        for i, bbox in enumerate(bbox_result)
    ]
    labels = np.concatenate(labels)
    # draw bounding boxes
    mmcv.imshow_det_bboxes(
        img,
        bboxes,
        labels,
        class_names=class_names,
        score_thr=score_thr,
        show=show,
        wait_time=wait_time,
        out_file=out_file)
    if not (show or out_file):
        return img


from __future__ import absolute_import
from __future__ import division

from __future__ import print_function

import apache_beam as beam
from tensorflow_data_validation import constants
from tensorflow_data_validation import types
from tensorflow_data_validation.statistics import stats_options
from tensorflow_data_validation.statistics.generators import basic_stats_generator
from tensorflow_data_validation.statistics.generators import stats_generator
from tensorflow_data_validation.statistics.generators import top_k_stats_generator
from tensorflow_data_validation.statistics.generators import top_k_uniques_combiner_stats_generator
from tensorflow_data_validation.statistics.generators import uniques_stats_generator

from tensorflow_data_validation.utils import batch_util
from tensorflow_data_validation.types_compat import Iterable, List, Optional, TypeVar

from tensorflow_metadata.proto.v0 import statistics_pb2

@beam.typehints.with_input_types(types.BeamExample)
@beam.typehints.with_output_types(statistics_pb2.DatasetFeatureStatisticsList)
class GenerateStatisticsImpl(beam.PTransform):
  """PTransform that applies a set of generators."""

  def __init__(
      self,
      options = stats_options.StatsOptions()
      ):
    self._options = options

  def expand(self, dataset):
    # Initialize a list of stats generators to run.
    stats_generators = _get_default_generators(self._options)

    if self._options.generators is not None:
      # Add custom stats generators.
      stats_generators.extend(self._options.generators)

    # If a set of whitelist features are provided, keep only those features.
    if self._options.feature_whitelist:
      dataset |= ('RemoveNonWhitelistedFeatures' >> beam.Map(
          _filter_features, feature_whitelist=self._options.feature_whitelist))

    result_protos = []
    # Iterate over the stats generators. For each generator,
    #   a) if it is a CombinerStatsGenerator, wrap it as a beam.CombineFn
    #      and run it.
    #   b) if it is a TransformStatsGenerator, wrap it as a beam.PTransform
    #      and run it.
    for generator in stats_generators:
      if isinstance(generator, stats_generator.CombinerStatsGenerator):
        # TODO(b/120863006): Consider removing fanout once BEAM-4030 is
        # resolved, and all the Beam OSS Runners support CombineFn.compact
        fanout = 16
        # TODO(b/88250100): Remove fanout once multi-shard combining is enabled
        # for single-thread cases.
        result_protos.append(
            dataset
            | generator.name >> beam.CombineGlobally(
                _BatchedCombineFnWrapper(generator)).with_fanout(fanout))
      elif isinstance(generator, stats_generator.TransformStatsGenerator):
        result_protos.append(
            dataset
            | generator.name >> generator.ptransform)
      else:
        raise TypeError('Statistics generator must extend one of '
                        'CombinerStatsGenerator or TransformStatsGenerator, '
                        'found object of type %s' %
                        generator.__class__.__name__)

    # Each stats generator will output a PCollection of DatasetFeatureStatistics
    # protos. We now flatten the list of PCollections into a single PCollection,
    # then merge the DatasetFeatureStatistics protos in the PCollection into a
    # single DatasetFeatureStatisticsList proto.
    return (result_protos
            | 'FlattenFeatureStatistics' >> beam.Flatten()
            | 'MergeDatasetFeatureStatisticsProtos' >>
            beam.CombineGlobally(_merge_dataset_feature_stats_protos)
            | 'MakeDatasetFeatureStatisticsListProto' >>
            beam.Map(_make_dataset_feature_statistics_list_proto))

def _get_default_generators(
    options, in_memory = False
):
  """Initialize default list of stats generators.

  Args:
    options: A StatsOptions object.
    in_memory: Whether the generators will be used to generate statistics in
      memory (True) or using Beam (False).

  Returns:
    A list of stats generator objects.
  """
  stats_generators = [
      basic_stats_generator.BasicStatsGenerator(
          schema=options.schema,
          weight_feature=options.weight_feature,
          num_values_histogram_buckets=options.num_values_histogram_buckets,
          num_histogram_buckets=options.num_histogram_buckets,
          num_quantiles_histogram_buckets=\
            options.num_quantiles_histogram_buckets,
          epsilon=options.epsilon)
  ]
  if in_memory:
    stats_generators.append(
        top_k_uniques_combiner_stats_generator.
        TopKUniquesCombinerStatsGenerator(
            schema=options.schema,
            weight_feature=options.weight_feature,
            num_top_values=options.num_top_values,
            num_rank_histogram_buckets=options.num_rank_histogram_buckets))
  else:
    stats_generators.extend([
        top_k_stats_generator.TopKStatsGenerator(
            schema=options.schema,
            weight_feature=options.weight_feature,
            num_top_values=options.num_top_values,
            num_rank_histogram_buckets=options.num_rank_histogram_buckets),
        uniques_stats_generator.UniquesStatsGenerator(schema=options.schema)
    ])
  return stats_generators

def _filter_features(
    example,
    feature_whitelist):
  """Remove features that are not whitelisted.

  Args:
    example: Input example.
    feature_whitelist: A list of feature names to whitelist.

  Returns:
    An example containing only the whitelisted features of the input example.
  """
  return {
      feature_name: example[feature_name]
      for feature_name in feature_whitelist
      if feature_name in example
  }

def _merge_dataset_feature_stats_protos(
    stats_protos
):
  """Merge together a list of DatasetFeatureStatistics protos.

  Args:
    stats_protos: A list of DatasetFeatureStatistics protos to merge.

  Returns:
    The merged DatasetFeatureStatistics proto.
  """
  stats_per_feature = {}
  # Iterate over each DatasetFeatureStatistics proto and merge the
  # FeatureNameStatistics protos per feature.
  for stats_proto in stats_protos:
    for feature_stats_proto in stats_proto.features:
      if feature_stats_proto.name not in stats_per_feature:
        stats_per_feature[feature_stats_proto.name] = feature_stats_proto
      else:
        stats_per_feature[feature_stats_proto.name].MergeFrom(
            feature_stats_proto)

  # Create a new DatasetFeatureStatistics proto.
  result = statistics_pb2.DatasetFeatureStatistics()
  num_examples = None
  for feature_stats_proto in stats_per_feature.values():
    # Add the merged FeatureNameStatistics proto for the feature
    # into the DatasetFeatureStatistics proto.
    new_feature_stats_proto = result.features.add()
    new_feature_stats_proto.CopyFrom(feature_stats_proto)

    # Get the number of examples from one of the features that
    # has common stats.
    if num_examples is None:
      stats_type = feature_stats_proto.WhichOneof('stats')
      stats_proto = None
      if stats_type == 'num_stats':
        stats_proto = feature_stats_proto.num_stats
      else:
        stats_proto = feature_stats_proto.string_stats

      if stats_proto.HasField('common_stats'):
        num_examples = (stats_proto.common_stats.num_non_missing +
                        stats_proto.common_stats.num_missing)

  # Set the num_examples field.
  if num_examples is not None:
    result.num_examples = num_examples
  return result

def _make_dataset_feature_statistics_list_proto(
    stats_proto
):
  """Constructs a DatasetFeatureStatisticsList proto.

  Args:
    stats_proto: The input DatasetFeatureStatistics proto.

  Returns:
    The DatasetFeatureStatisticsList proto containing the input stats proto.
  """
  # Create a new DatasetFeatureStatisticsList proto.
  result = statistics_pb2.DatasetFeatureStatisticsList()

  # Add the input DatasetFeatureStatistics proto.
  dataset_stats_proto = result.datasets.add()
  dataset_stats_proto.CopyFrom(stats_proto)
  return result

# Have a type variable to represent the type of the accumulator
# in a combiner stats generator.
ACCTYPE = TypeVar('ACCTYPE')

class _BatchedCombineFnAcc(object):
  """Batched combiner wrapper accumulator."""

  def __init__(self, partial_accumulator):  # pytype: disable=invalid-annotation
    # Partial accumulator state of the underlying CombinerStatsGenerator.
    self.partial_accumulator = partial_accumulator
    # Input examples to be processed.
    self.input_examples = []

@beam.typehints.with_input_types(types.Example)
@beam.typehints.with_output_types(statistics_pb2.DatasetFeatureStatistics)
class _BatchedCombineFnWrapper(beam.CombineFn):
  """A beam.CombineFn wrapping CombinerStatsGenerator with batching.

  This wrapper does two things:
    1. Wraps a combiner stats generator as a beam.CombineFn
    2. Batches input examples before passing it to the underlying
       stats generator.

  We do this by accumulating examples in the combiner state until we
  accumulate a large enough batch, at which point we send them through the
  add_input step of the underlying combiner stats generator. When merging,
  we merge the accumulators of the stats generator and accumulate
  examples accordingly. We finally process any remaining examples
  before producing the final output value.

  This wrapper is needed to support slicing as we need the ability to
  perform slice-aware batching. But currently there is no way to do key-aware
  batching in Beam. Hence, this wrapper does batching and combining together.

  See also:
  BEAM-3737: Key-aware batching function
  (https://issues.apache.org/jira/browse/BEAM-3737).
  """

  # This needs to be large enough to allow for efficient TF invocations during
  # batch flushing, but shouldn't be too large as it also acts as cap on the
  # maximum memory usage of the computation.
  # TODO(b/120863006): Consider increasing once BEAM-4030 is
  # resolved, and all the Beam OSS Runners support CombineFn.compact
  _DEFAULT_DESIRED_BATCH_SIZE = 100

  def __init__(
      self,
      generator,
      desired_batch_size = None):
    self._generator = generator

    # We really want the batch size to be adaptive like it is in
    # beam.BatchElements(), but there isn't an easy way to make it so.
    # TODO(b/73789023): Figure out how to make this batch size dynamic.
    if desired_batch_size and desired_batch_size > 0:
      self._desired_batch_size = desired_batch_size
    else:
      self._desired_batch_size = self._DEFAULT_DESIRED_BATCH_SIZE

    # Metrics
    self._combine_batch_size = beam.metrics.Metrics.distribution(
        constants.METRICS_NAMESPACE,
        'combine_batch_size_' + self._generator.name)
    self._num_compacts = beam.metrics.Metrics.counter(
        constants.METRICS_NAMESPACE, 'num_compacts_' + self._generator.name)

  def create_accumulator(self
                        ):  # pytype: disable=invalid-annotation
    return _BatchedCombineFnAcc(self._generator.create_accumulator())

  def _maybe_do_batch(self, accumulator,
                      force = False):
    """Maybe update accumulator in place.

    Checks if accumulator has enough examples for a batch, and if so, does the
    stats computation for the batch and updates accumulator in place.

    Args:
      accumulator: Accumulator. Will be updated in place.
      force: Force computation of stats even if accumulator has less examples
        than the batch size.
    """
    batch_size = len(accumulator.input_examples)
    if (force and batch_size > 0) or batch_size >= self._desired_batch_size:
      self._combine_batch_size.update(batch_size)
      accumulator.partial_accumulator = self._generator.add_input(
          accumulator.partial_accumulator,
          batch_util.merge_single_batch(accumulator.input_examples))
      del accumulator.input_examples[:]  # Clear processed examples.

  def add_input(self, accumulator,
                input_example):
    accumulator.input_examples.append(input_example)
    self._maybe_do_batch(accumulator)
    return accumulator

  def merge_accumulators(self, accumulators
                        ):
    result = self.create_accumulator()
    for acc in accumulators:
      result.partial_accumulator = self._generator.merge_accumulators(
          [result.partial_accumulator, acc.partial_accumulator])
      result.input_examples.extend(acc.input_examples)
      self._maybe_do_batch(result)
    return result

  # TODO(pachristopher): Consider adding CombinerStatsGenerator.compact method.
  def compact(self, accumulator):
    self._maybe_do_batch(accumulator, force=True)
    self._num_compacts.inc(1)
    return accumulator

  def extract_output(
      self,
      accumulator
  ):  # pytype: disable=invalid-annotation
    # Make sure we have processed all the examples.
    self._maybe_do_batch(accumulator, force=True)
    return self._generator.extract_output(accumulator.partial_accumulator)

def generate_statistics_in_memory(
    examples,
    options = stats_options.StatsOptions()
):
  """Generates statistics for an in-memory list of examples.

  Args:
    examples: A list of input examples.
    options: Options for generating data statistics.

  Returns:
    A DatasetFeatureStatisticsList proto.
  """
  stats_generators = _get_default_generators(options, in_memory=True)

  if options.generators is not None:
    for generator in options.generators:
      if isinstance(generator, stats_generator.CombinerStatsGenerator):
        stats_generators.append(generator)
      else:
        raise TypeError('Statistics generator used in '
                        'generate_statistics_in_memory must '
                        'extend CombinerStatsGenerator, found object of type '
                        '%s.' %
                        generator.__class__.__name__)

  batch = batch_util.merge_single_batch(examples)

  # If whitelist features are provided, keep only those features.
  if options.feature_whitelist:
    batch = {
        feature_name: batch[feature_name]
        for feature_name in options.feature_whitelist
    }

  outputs = [
      generator.extract_output(
          generator.add_input(generator.create_accumulator(), batch))
      # The type checker raises a false positive here because the type hint for
      # the return value of _get_default_generators (which created the list of
      # stats_generators) is StatsGenerator, but add_input, create_accumulator,
      # and extract_output can be called only on CombinerStatsGenerators.
      for generator in stats_generators  # pytype: disable=attribute-error
  ]

  return _make_dataset_feature_statistics_list_proto(
      _merge_dataset_feature_stats_protos(outputs))


import copy
import os

import torch
from torch import nn
from torchprofile import profile_macs

from models import networks
from models.modules.loss import GANLoss, VGGLoss
from utils import util

class SPADEModelModules(nn.Module):
    def __init__(self, opt):
        opt = copy.deepcopy(opt)
        if len(opt.gpu_ids) > 0:
            opt.gpu_ids = opt.gpu_ids[:1]
        self.gpu_ids = opt.gpu_ids
        super(SPADEModelModules, self).__init__()
        self.opt = opt
        self.model_names = ['G']
        self.visual_names = ['labels', 'fake_B', 'real_B']
        self.netG = networks.define_G(opt.netG, init_type=opt.init_type,
                                      init_gain=opt.init_gain, gpu_ids=self.gpu_ids, opt=opt)
        if opt.isTrain:
            self.model_names.append('D')
            self.netD = networks.define_D(opt.netD, init_type=opt.init_type,
                                          init_gain=opt.init_gain, gpu_ids=self.gpu_ids, opt=opt)
            self.criterionGAN = GANLoss(opt.gan_mode)
            self.criterionFeat = nn.L1Loss()
            self.criterionVGG = VGGLoss()
            self.optimizers = []
            self.loss_names = ['G_gan', 'G_feat', 'G_vgg', 'D_real', 'D_fake']
        else:
            self.netG.eval()
        self.config = None

    def create_optimizers(self):
        if self.opt.no_TTUR:
            beta1, beta2 = self.opt.beta1, self.opt.beta2
            G_lr, D_lr = self.opt.lr, self.opt.lr
        else:
            beta1, beta2 = 0, 0.9
            G_lr, D_lr = self.opt.lr / 2, self.opt.lr * 2
        optimizer_G = torch.optim.Adam(list(self.netG.parameters()), lr=G_lr, betas=(beta1, beta2))
        optimizer_D = torch.optim.Adam(list(self.netD.parameters()), lr=D_lr, betas=(beta1, beta2))
        return optimizer_G, optimizer_D

    def forward(self, input_semantics, real_B=None, mode='generate_fake'):

        if self.config is not None:
            self.netG.config = self.config
        if mode == 'generate_fake':
            fake_B = self.netG(input_semantics)
            return fake_B
        elif mode == 'G_loss':
            assert real_B is not None
            return self.compute_G_loss(input_semantics, real_B)
        elif mode == 'D_loss':
            assert real_B is not None
            return self.compute_D_loss(input_semantics, real_B)
        elif mode == 'calibrate':
            with torch.no_grad():
                self.netG(input_semantics)
        else:
            raise NotImplementedError('Unknown forward mode [%s]!!!' % mode)

    def profile(self, input_semantics):
        netG = self.netG
        if isinstance(netG, nn.DataParallel):
            netG = netG.module
        if self.config is not None:
            netG.config = self.config
        with torch.no_grad():
            macs = profile_macs(netG, (input_semantics,))
        params = 0
        for p in netG.parameters():
            params += p.numel()
        return macs, params

    def compute_G_loss(self, input_semantics, real_B):
        fake_B = self.netG(input_semantics)
        pred_fake, pred_real = self.discriminate(input_semantics, fake_B, real_B)
        loss_G_gan = self.criterionGAN(pred_fake, True, for_discriminator=False) * self.opt.lambda_gan
        num_D = len(pred_fake)
        loss_G_feat = 0
        for i in range(num_D):
            num_intermediate_outputs = len(pred_fake[i]) - 1
            for j in range(num_intermediate_outputs):  # for each layer output
                unweighted_loss = self.criterionFeat(
                    pred_fake[i][j], pred_real[i][j].detach())
                loss_G_feat += unweighted_loss * self.opt.lambda_feat / num_D
        loss_G_vgg = self.criterionVGG(fake_B, real_B) * self.opt.lambda_vgg
        loss_G = loss_G_gan + loss_G_feat + loss_G_vgg
        losses = {'loss_G': loss_G, 'G_gan': loss_G_gan,
                  'G_feat': loss_G_feat, 'G_vgg': loss_G_vgg}
        return losses

    def compute_D_loss(self, input_semantics, real_B):
        with torch.no_grad():
            fake_B = self.netG(input_semantics)
        pred_fake, pred_real = self.discriminate(input_semantics, fake_B, real_B)
        loss_D_fake = self.criterionGAN(pred_fake, False, for_discriminator=True)
        loss_D_real = self.criterionGAN(pred_real, True, for_discriminator=True)
        loss_D = loss_D_fake + loss_D_real
        losses = {'loss_D': loss_D, 'D_fake': loss_D_fake, 'D_real': loss_D_real}
        return losses

    def discriminate(self, input_semantics, fake_B, real_B):
        fake_concat = torch.cat([input_semantics, fake_B], dim=1)
        real_concat = torch.cat([input_semantics, real_B], dim=1)
        fake_and_real = torch.cat([fake_concat, real_concat], dim=0)
        discriminator_out = self.netD(fake_and_real)
        pred_fake, pred_real = self.divide_pred(discriminator_out)
        return pred_fake, pred_real

    # Take the prediction of fake and real images from the combined batch
    def divide_pred(self, pred):
        # the prediction contains the intermediate outputs of multiscale GAN,
        # so it's usually a list
        if type(pred) == list:
            fake = []
            real = []
            for p in pred:
                fake.append([tensor[:tensor.size(0) // 2] for tensor in p])
                real.append([tensor[tensor.size(0) // 2:] for tensor in p])
        else:
            fake = pred[:pred.size(0) // 2]
            real = pred[pred.size(0) // 2:]

        return fake, real

    def load_networks(self, verbose=True):
        for name in self.model_names:
            net = getattr(self, 'net' + name, None)
            path = getattr(self.opt, 'restore_%s_path' % name, None)
            if path is not None:
                util.load_network(net, path, verbose)

    def save_networks(self, epoch, save_dir):
        for name in self.model_names:
            if isinstance(name, str):
                save_filename = '%s_net_%s.pth' % (epoch, name)
                save_path = os.path.join(save_dir, save_filename)
                net = getattr(self, 'net' + name)
                if len(self.gpu_ids) > 0 and torch.cuda.is_available():
                    torch.save(net.cpu().state_dict(), save_path)
                    net.cuda(self.gpu_ids[0])
                else:
                    torch.save(net.cpu().state_dict(), save_path)


class Solution:
    def maxScore(self, nums: List[int]) -> int:
        def calcu_score(nums, scores):
            if not nums:
                return 0
            if tuple(nums) in scores:
                return scores[tuple(nums)]
            n = len(nums)/2
            maxscore = 0
            for i in range(int(2*n)):
                for j in range(i+1,int(2*n)):
                    a = nums[i]
                    b=nums[j]
                    nums_copy = nums.copy()
                    #gcd = find_gcd(a,b)
                    nums_copy.remove(a)
                    nums_copy.remove(b)
                    score = int(n*math.gcd(a,b)+calcu_score(nums_copy,scores))
                    if score>maxscore:
                        maxscore=score
            scores[tuple(nums)]=maxscore
            return maxscore
        return calcu_score(nums,{})


from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

# Create your models here.
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    follows = models.ManyToManyField(
        "self",
        related_name="followed_by",
        symmetrical=False,
        blank=True
    )

    def __str__(self):
        return self.user.username

# Create a profile for each new user.
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        # We want to follow our own profile to see our own musings and rediculousness
        # user_profile = Profile(user=instance, follows=[instance])

        user_profile = Profile(user=instance)
        user_profile.save()

        # Implementation 01: Using .set() for adding single objects to many-to-many relationships
        # user_profile.follows.set([instance.profile.id])

        # Implementation 02: Using .add() for adding single objects to many-to-many relationships
        user_profile.follows.add(instance.profile)
        user_profile.save()

# Previous method without using the decorator @receiver
# post_save.connect(create_profile, sender=User)

class Dweet(models.Model):
    user = models.ForeignKey(
        User, related_name="dweets", on_delete=models.DO_NOTHING
    )
    body = models.CharField(max_length=140)
    created_at = models.DateTimeField(auto_now_add=True)

    # Option 02
    # We can set our ordering in our model class.
    # This will be the default ordering for the object, for use
    # when obtaining lists of objects.
    # class Meta:
    #     ordering = ['-created_at']

    def __str__(self):
        return (
            f"{self.user} "
            f"({self.created_at:%Y-%m-%d %H:%M}): "
            f"{self.body[:30]}..."
        )


import scraper as fs
import mqttconnection as mqtt
import signal
import sys
import time

INTERVAL = 5.0

class FritzScraper(object):

  def __init__(self):
    self._exit = False
    signal.signal(signal.SIGTERM, self._sigterm_handler)

    self._scraper = fs.Scraper()
    self._mqttConnection = mqtt.MqttConnection()

  def _sigterm_handler(self, signal, frame):
    print("SIGTERM received")
    self._exit = True

  def run(self):
    while not self._exit:
      self._mqttConnection.connect()
      fscargo = self._scraper.get_cargo()
      self._mqttConnection.publish(fscargo)

      print("Sleep for " + str(INTERVAL) + "s")
      sys.stdout.flush()
      time.sleep(INTERVAL)

  def close(self):
    print("Cleaning up...")
    self._mqttConnection.disconnect()

if __name__ == '__main__':

  try:
    print("Creating FritzScraper...")
    fritzScraper = FritzScraper()
  except Exception as e:
    sys.exit("Could not create FritzScraper: " + str(e))
  else:
    fritzScraper.run()

    fritzScraper.close()


import pytest
import numpy as np
import pandas as pd

AUTHORIZED_DF_NAMES = ["main_dataframe_pretreated", "newclients_data", "score_data", "test_data", "train_data"]
DATA_PATH = "Marchand_David_1_dashboard_et_API_052023/backend/ressources/data/"
ID_COLUMN_NAME = "SK_ID_CURR"

def test_return_ids_list(df_name):
    df = pd.read_csv(DATA_PATH + df_name + ".csv")
    output_ids = sorted(df[ID_COLUMN_NAME].unique().tolist())
    output_ids.insert(0, "")

    assert type(df_name) == str
    assert df_name in AUTHORIZED_DF_NAMES
    assert type(output_ids) == list
    assert len(output_ids) > 0

def test_return_data_per_id(df_name, row_id=None):
    df = pd.read_csv(DATA_PATH + df_name + ".csv")
    min_id = min(df[ID_COLUMN_NAME])
    max_id = max(df[ID_COLUMN_NAME])
    row_id = {ID_COLUMN_NAME : np.random.randint(min_id, max_id)}

    output = df[df.loc[:, ID_COLUMN_NAME] == row_id[ID_COLUMN_NAME]].to_dict()

    assert type(output) == dict
    assert len(output) > 0


import torch
from torch import multiprocessing
from torch import distributed
from torch import backends
from torch import cuda
from torch import utils
from torch import optim
from torch import nn
from torchvision import models
from torchvision import transforms
from torchvision import utils as vutils
from tensorboardX import SummaryWriter
from chainercv import evaluations
from collections import OrderedDict
from PIL import Image
from modules import *
from samplers import *
from distributed import *
from utils import *
import visualization
import numpy as np
import itertools
import functools
import importlib
import argparse
import datetime
import shutil
import random
import json
import time
import glob
import os

class MultiMNIST(utils.data.Dataset):

    def __init__(self, metafile, transform=None, target_transform=None):
        with open(metafile) as file:
            self.meta = list(json.load(file).items())
        self.dirname = os.path.dirname(metafile)
        self.transform = transform
        self.target_transform = target_transform

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

    def __getitem__(self, index):
        filename, target = self.meta[index]
        image = Image.open(filename).convert('RGB')
        if self.transform is not None:
            image = self.transform(image)
        if self.target_transform is not None:
            target = self.target_transform(target)
        return image, target

class MNISTModel(nn.Module):

    def __init__(self, conv_params, attention_param, linear_params):
        super().__init__()
        self.network = nn.Sequential(OrderedDict(
            conv_blocks=nn.Sequential(*[
                nn.Sequential(OrderedDict(
                    conv=nn.Conv2d(**conv_param),
                    actv=nn.ReLU()
                )) for conv_param in conv_params
            ]),
            attention_network=AttentionNetwork(**attention_param),
            linear_blocks=nn.Sequential(*[
                nn.Sequential(
                    nn.Identity() if i else nn.ReLU(),
                    nn.Linear(**linear_param)
                ) for i, linear_param in enumerate(linear_params)
            ])
        ))

    def forward(self, input):
        output = self.conv_blocks(input)
        output, attention = self.attention_network(output)
        output = self.linear_blocks(output)
        return output, attention

def main(args):

    with open(args.config) as file:
        config = json.load(file)
        config.update(vars(args))
        config = apply_dict(Dict, config)

    # Multi-process single-GPU distributed training
    # See https://pytorch.org/docs/1.1.0/distributed.html
    # and https://pytorch.org/docs/stable/nn.html#torch.nn.parallel.DistributedDataParallel

    # On PyTorch, we should specify `MASTER_ADDR` and `MASTER_PORT` by environment variable.
    init_process_group(backend='nccl')  # For PyTorch
    # On Parrots, we don't have to specify them.
    # distributed.init_process_group(backend='nccl') # For Parrots

    # Force each process to run on a single device.
    cuda.set_device(distributed.get_rank() % cuda.device_count())

    # NOTE: Using fork method causes an error in a data loader.
    multiprocessing.set_start_method('spawn', force=True)

    backends.cudnn.enabled = True
    backends.cudnn.benchmark = False

    random.seed(config.seed)
    np.random.seed(config.seed)
    torch.manual_seed(config.seed)
    cuda.manual_seed(config.seed)

    config.dataset.update(global_batch_size=config.dataset.local_batch_size * distributed.get_world_size())

    dprint(f'\n{"=" * 32} Configuration {"=" * 32}')
    dprint(json.dumps(config, indent=4))

    train_dataset = MultiMNIST(
        metafile=config.dataset.train.metafile,
        transform=transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.5,) * 3, (0.5,) * 3)
        ]),
        target_transform=lambda target: {key: torch.tensor(value) for key, value in target.items()}
    )
    val_dataset = MultiMNIST(
        metafile=config.dataset.val.metafile,
        transform=transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.5,) * 3, (0.5,) * 3)
        ]),
        target_transform=lambda target: {key: torch.tensor(value) for key, value in target.items()}
    )

    # Just run 1 iteration for debug.
    if config.debug:
        indices = range(config.dataset.global_batch_size)
        train_dataset = utils.data.Subset(train_dataset, indices)
        eval_datasets = Dict({name: utils.data.Subset(val_dataset, indices) for name, val_dataset in eval_datasets.items()})

    # Sampler for distributed training.
    # This guarantees that each process loads a different batch in each training step.
    train_sampler = DistributedSampler(train_dataset, shuffle=True)
    val_sampler = DistributedSampler(val_dataset, shuffle=False)

    train_data_loader = utils.data.DataLoader(
        dataset=train_dataset,
        sampler=train_sampler,
        batch_size=config.dataset.local_batch_size,
        num_workers=config.dataset.num_workers,
        pin_memory=True,
        drop_last=False
    )
    val_data_loader = utils.data.DataLoader(
        dataset=val_dataset,
        sampler=val_sampler,
        batch_size=config.dataset.local_batch_size,
        num_workers=config.dataset.num_workers,
        pin_memory=True,
        drop_last=False
    )

    model = MNISTModel(
        conv_params=[
            Dict(in_channels=3, out_channels=32, stride=2),
            Dict(in_channels=32, out_channels=64, stride=2),
        ],
        attention_param=Dict(
            conv_param=[
                Dict(in_channels=64, out_channels=32, stride=2),
                Dict(in_channels=32, out_channels=16, stride=2),
            ],
            linear_params=[
                Dict(in_features=1024, out_features=64),
                Dict(in_features=64, out_features=1024),
            ],
            deconv_params=[
                Dict(in_channels=16, out_channels=8, stride=2),
                Dict(in_channels=8, out_channels=4, stride=2),
            ]
        ),
        linear_params=[
            Dict(in_features=256, out_features=1024),
            Dict(in_features=1024, out_features=10),
        ]
    )
    model.cuda()

    num_process_groups = distributed.get_world_size() // config.distributed.batch_norm_group_size
    process_groups = [distributed.new_group(ranks) for ranks in np.split(np.arange(distributed.get_world_size()), num_process_groups)]
    model = nn.SyncBatchNorm.convert_sync_batchnorm(model, process_groups[distributed.get_rank() // config.distributed.batch_norm_group_size])
    model = nn.parallel.DistributedDataParallel(model, [distributed.get_rank() % cuda.device_count()], broadcast_buffers=False)

    # Scale learning rate following the `global` batch size (`local batch size` * `world size`)
    config.optimizer.lr *= config.global_batch_size / config.global_batch_denom
    optimizer = optim.Adam(model.parameters(), **config.optimizer)

    epoch = -1
    step = -1
    if config.saving.resume_model:
        checkpoint = Dict(torch.load(config.saving.resume_model, map_location=lambda storage, location: storage.cuda()))
        model.load_state_dict(checkpoint.model_state_dict, strict=True)
        optimizer.load_state_dict(checkpoint.optimizer_state_dict, strict=True)
        epoch = checkpoint.epoch
        step = checkpoint.step

    writer = SummaryWriter('logs') if not distributed.get_rank() else None
    saver = Saver('ckpts') if not distributed.get_rank() else None

    stop_watch = StopWatch()
    ema_meter = Dict(EMAMeter())

    def train(data_loader):
        nonlocal step
        dprint(f'\n{"=" * 32} Training started {"=" * 32}')
        model.train()
        stop_watch.start()
        for step, (input, target) in enumerate(data_loader, step + 1):
            input = to_gpu(input, non_blocking=True)
            ema_meter.update(data_time=stop_watch.stop())
            stop_watch.start()
            logit, attention = model(input)
            loss = nn.functional.cross_entropy(logit, target)
            prediction = torch.argmax(logit, dim=-1)
            accuracy = torch.mean(prediction == target)
            ema_meter.update(forward_time=stop_watch.stop())
            stop_watch.start()
            optimizer.zero_grad()
            if torch.isnan(loss):
                dprint('NaN in the loss...')
            elif torch.isinf(loss):
                dprint('Inf in the loss...')
            else:
                loss.backward(retain_graph=False)
            if not isinstance(model, nn.parallel.DistributedDataParallel):
                average_gradients(model.parameters())
            optimizer.step()
            ema_meter.update(backward_time=stop_watch.stop())
            stop_watch.start()
            if not step % config.training.log_steps:
                average_tensors([loss, accuracy])
                if writer:
                    writer.add_scalar(f'loss/train', loss, step)
                    writer.add_scalar(f'accuracy/train', accuracy, step)
                    writer.add_image(f'attention/train', vutils.make_grid(visualization.linear_map(attention, attention.min(), attention.max(), 0, 1)), step)
                progress = step / (config.training.train_epochs * len(data_loader)) * 100
                eta_seconds = (config.training.train_epochs * len(data_loader) - step) * sum(ema_meter.values())
                eta_string = str(datetime.timedelta(seconds=eta_seconds))
                dprint(f'\n[training] epoch: {epoch} progress: {progress:.2f}% ETA: {eta_string} loss: {loss} accuracy: {accuracy}')
                dprint(f' '.join(f"{name}: {time:.4f} sec" for name, time in ema_meter.items()))
            distributed.barrier()
        stop_watch.stop()

    if config.train:
        stop_watch.start()
        broadcast_tensors(model.state_dict().values())
        for epoch in range(epoch + 1, config.training.train_epochs):
            train_sampler.set_epoch(epoch)
            train(train_data_loader)
            if saver:
                saver.save(
                    filename=f'epoch_{epoch}',
                    model_state_dict=model.state_dict(),
                    optimizer_state_dict=optimizer.state_dict(),
                    epoch=epoch,
                    step=step
                )
        dprint(f'\n{"=" * 32} Training finished {"=" * 32}')
        dprint(f'Elapsed time: {stop_watch.stop()} sec')

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Jigsaw Puzzle')
    parser.add_argument('--seed', type=int, default=0)
    parser.add_argument('--config', type=str, default='config.json')
    parser.add_argument('--train', action='store_true')
    parser.add_argument('--evaluate', action='store_true')
    parser.add_argument('--debug', action='store_true')
    args = parser.parse_args()

    main(args)


class Res:

	def __init__(self,resource_type):
		self.resource_type = resource_type

	def get_ec2_resources_on_taginfo(self,**kwargs):

		instance_list=[]
		tag_instance_list=[]
		# get all the running instances
		instances = self.resource_type.instances.filter(
		Filters=[
		{'Name':'instance-state-name','Values':['running']},
		]

		)

		#print("runninng instances",instances)
		#print("kwargs",kwargs)
		if "taginfo" in kwargs:
			print("isnide taginfo")
			for instance in instances:
				if(instance.tags == None):
					instance_list.append(instance.id)

		if "tagonly" in kwargs:
			#print("tagonly found")
			key = kwargs['tagonly']
			#print("i am in tag only")

			for instance in instances:
				if(instance.tags != None):
					for tags in instance.tags:
						print("key is",key)
						print("Tags[Key]",tags['Key'])
						if (key == tags['Key']):
							instance_list.append(instance.id)
							print("found instance with key",key)

		if ("key" in kwargs and "value" in kwargs):
			key = kwargs['key']
			value = kwargs['value']
			#print("has both key and value")
			for instance in instances:
				if(instance.tags != None):
					for tags in instance.tags:
						#print("key is",key)
						#print("Tags[Key]",tags['Key'])
						if (key == tags['Key'] and value == tags['Value']):
							instance_list.append(instance.id)
							#print("found instance with key",key)

		return(instance_list)


import numpy as np
import matplotlib.pyplot as plt

def data1():
    x = [np.random.rand() for i in range(1000)]
    y = [x[i] + 0.05*np.random.rand() for i in range(1000)]
    return [x, y]

def data2():
    x = [np.random.rand() for i in range(1000)]
    y = [(x[i])**2 + 0.05*np.random.rand() for i in range(1000)]
    return [x, y]

# Standardisation of Data
def std_data(nparray):
    x, y = nparray[0], nparray[1]
    x_std = (x - np.mean(x))/np.std(x)
    y_std = (y - np.mean(y))/np.std(y)
    return np.array([x_std, y_std])

def DimReduction(arr):
    data_set = np.array(arr)
    std_data_set = std_data(data_set)

    # Computing the covariance matrix
    cov_matrix = np.cov(std_data_set)

    # Computing the eigenvectors and eigenvalues of the covariance matrix to identify the principal components
    eigen_data = np.linalg.eigh(cov_matrix)

    # Creating a feature vector to decide which principal components to keep
    u = eigen_data[1][:][-1] # Retaining the eigenvector corresponding to greatest eigenvalue

    # Recasting the data along the principal components axes
    m = u[1]/u[0]*np.std(data_set[1])/np.std(data_set[0])
    c = np.mean(data_set[1] - data_set[0]*m)

    # Displaying the result using matplotlib
    plt.scatter(data_set[0], data_set[1], color = "red")
    plt.plot(data_set[0], data_set[0]*m + c)
    print("Slope =", m, "Intercept =", c)
    plt.title("Best Fit Line")
    plt.show()

DimReduction(data1())
DimReduction(data2())


"""Write a python script to create variables to store your name, age, qualification, years
of experience and print it."""

your_name = input("Enter your name: ")
age = int(input("Enter your age: "))
qual= input("Enter your qualfication :")
years_exp = float(input("Enter no of years of experience: "))

print(your_name, age, qual, years_exp, sep =',')


import logging
import os.path
from typing import List

import config
from boring import images
from boring.utils import *
from config import WIDTH, HEIGHT
from scene_objects.character import Character
from scene_objects.dialogue import Dialogue
from scene_objects.escape_point import EscapePoint
from scene_objects.monologue import Monologue
from scene_objects.utils import create_event_from_data

logger = logging.getLogger(__name__)

class GameScene:
    """
    This class holds the information of one game screen, the game is made of many game screens
    Information include:
    - background
    - Yellow points (escape points)
    - Characters present
    - Dialogues
    - Monologues

    These windows are rendered in the game engine
    """

    def __init__(self, engine):
        self.background: pygame.Surface | None = None
        self.escape_points: List[EscapePoint] | None = None
        self.charcter: Character | None = None

        self.game_events = []
        self.event_index = 0

        self.engine = engine

        self.name = None

    def get_current_event(self) -> "Monologue" or "Dialogue" or None:
        if self.event_index < len(self.game_events):
            return self.game_events[self.event_index]
        else:
            return None

    def load_scene(self, scene_name: str):
        import json

        with open(os.path.join(config.data_path), encoding="utf-8") as f:
            data = json.load(f)

        self.name = scene_name
        scene_data = data[scene_name]
        self.background = images.load(os.path.join("scenes", scene_data["background"]), size=(WIDTH, HEIGHT))
        self.escape_points = []
        for point_data in scene_data["escape_points"]:
            self.escape_points.append(EscapePoint(**point_data, game_engine=self.engine))
        if "character" in scene_data:
            self.charcter = Character(**scene_data["character"])
        if "events" in scene_data:
            for event_data in scene_data["events"]:
                self.game_events.append(create_event_from_data(event_data, self))
        return self

    def next_event(self):
        self.event_index += 1
        logger.info(f"Next event: {self.get_current_event()}")

    def change_affinity(self, character_name, amount):
        self.engine.change_affinity(character_name, amount)

    def add_event(self, event):
        logger.debug(f"Adding event {event}")
        self.game_events.insert(self.event_index + 1, event)
        logger.debug(f"Events: {self.game_events}")

    def __repr__(self):
        return f"GameScene({self.name})"


# Utils for Keras 1/2 compatibility

import keras

keras_2 = int(keras.__version__.split(".")[0]) > 1  # Keras > 1

def add_activity_regularizer(layer):
    if layer.activity_regularizer and not keras_2:
        layer.activity_regularizer.set_layer(layer)
        if not hasattr(layer, 'regularizers'):
            layer.regularizers = []
            layer.regularizers.append(layer.activity_regularizer)

def l1l2(l1_weight=0, l2_weight=0):
    if keras_2:
        from keras.regularizers import L1L2
        return L1L2(l1_weight, l2_weight)
    else:
        from keras.regularizers import l1l2
        return l1l2(l1_weight, l2_weight)

def get_initializer(initializer):
    if keras_2:
        from keras import initializers
        return initializers.get(initializer)
    else:
        from keras import initializations
        return initializations.get(initializer)

def fit(model, x, y, epochs=100, **kwargs):
    if keras_2:
        return model.fit(x, y, epochs=epochs, **kwargs)
    else:
        return model.fit(x, y, nb_epoch=epochs, **kwargs)

def add_weight(layer,
               shape,
               name,
               initializer='random_uniform',
               regularizer=None,
               constraint=None):
    initializer = get_initializer(initializer)
    if keras_2:
        return layer.add_weight(initializer=initializer,
                                shape=shape,
                                name=name,
                                regularizer=regularizer,
                                constraint=constraint)
    else:
        # create weight
        w = initializer(shape, name=name)
        # add to trainable_weights
        if not hasattr(layer, 'trainable_weights'):
            layer.trainable_weights = []
        layer.trainable_weights.append(w)
        # add to regularizers
        if regularizer:
            if not hasattr(layer, 'regularizers'):
                layer.regularizers = []
            regularizer.set_param(w)
            layer.regularizers.append(regularizer)
        return w


from dash import html, dcc
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
from dash_bootstrap_templates import ThemeChangerAIO
from app import app

style_sidebar = style={"box-shadow": "2px 2px 10px 0px rgba(10, 9, 7, 0.10)",
                    "margin": "10px",
                    "padding": "10px",
                    "height": "100vh"}

# =========  Layout  =========== #
layout = dbc.Card(
    [
        html.H2("ASIMOV", style={'font-family': 'Voltaire', 'font-size': '60px'}),
        html.Hr(),
        html.P("A simple sidebar layout with navigation links", className="lead"),
        dbc.Nav(
            [
                dbc.NavLink("Campaigns", href="/", active="exact"),
                dbc.NavLink("Adsets", href="/adsets", active="exact"),
            ], vertical=True, pills=True, style={"margin-bottom": "50px"}),
        ThemeChangerAIO(aio_id="theme", radio_props={"value":dbc.themes.QUARTZ})
    ], style=style_sidebar
)


# source: http://linanqiu.github.io/2015/10/07/word2vec-sentiment/

from gensim import utils
from gensim.models.doc2vec import LabeledSentence
from gensim.models import Doc2Vec

# numpy
import numpy

# random
from random import shuffle

# classifier
from sklearn.linear_model import LogisticRegression

size = 100

class LabeledLineSentence(object):
    def __init__(self, sources):
        self.sources = sources

        flipped = {}

        # make sure that keys are unique
        for key, value in sources.items():
            if value not in flipped:
                flipped[value] = [key]
            else:
                raise Exception('Non-unique prefix encountered')

    def __iter__(self):
        for source, prefix in self.sources.items():
            with utils.smart_open(source) as fin:
                for item_no, line in enumerate(fin):
                    yield LabeledSentence(utils.to_unicode(line).split(), [prefix + '_%s' % item_no])

    def to_array(self):
        self.sentences = []
        for source, prefix in self.sources.items():
            with utils.smart_open(source) as fin:
                for item_no, line in enumerate(fin):
                    self.sentences.append(LabeledSentence(utils.to_unicode(line).split(), [prefix + '_%s' % item_no]))
        return self.sentences

    def sentences_perm(self):
        shuffle(self.sentences)
        return self.sentences

def trainning():
    #./manage.py shell -c="from tweets.p2v import trainning; trainning()"

    #sources = {'offcom_yes.txt':'YES', 'offcom_no.txt':'NO', 'offcom_uns.txt':'UNS'}
    #sources = {'offcom_yes.txt':'YES', 'offcom_no.txt':'NO'}
    sources = {'kagggle_test_yes.txt':'TEST_YES', 'kagggle_test_no.txt':'TEST_NO', 'kaggle_train_yes.txt':'TRAIN_YES', 'kaggle_train_no.txt':'TRAIN_NO', 'kaggle_train_uns.txt':'TRAIN_UNS'}
    #sources = {'kagggle_test_yes.txt':'TEST_YES', 'kagggle_test_no.txt':'TEST_NO', 'kaggle_train_yes.txt':'TRAIN_YES', 'kaggle_train_no.txt':'TRAIN_NO'}

    sentences = LabeledLineSentence(sources)

    model = Doc2Vec(min_count=1, window=10, size=size, sample=1e-4, negative=5, workers=7)

    model.build_vocab(sentences.to_array())

    model.train(sentences.sentences_perm(), total_words=model.corpus_count, epochs=10)

    model.save('kagggle.d2v')
    test()

def test():
    #./manage.py shell -c="from tweets.p2v import test; test()"

    model = Doc2Vec.load('kagggle.d2v')

    train_arrays = numpy.zeros((3945, size))
    train_labels = numpy.zeros(3945)

    for i in range(2896):
        train_arrays[i] = model.docvecs['TRAIN_NO_' + str(i)]
        train_labels[i] = 0

    for i in range(1049):
        train_arrays[2896+i] = model.docvecs['TRAIN_YES_' + str(i)]
        train_labels[2896+i] = 1

    test_arrays = numpy.zeros((2646, size))
    test_labels = numpy.zeros(2646)

    for i in range(1953):
        test_arrays[i] = model.docvecs['TEST_NO_' + str(i)]
        test_labels[i] = 0

    for i in range(693):
        test_arrays[1953+i] = model.docvecs['TEST_YES_' + str(i)]
        test_labels[1953+i] = 1

    #print(train_labels)
    #print(test_labels)

    #numpy.savetxt("kagggle_train_LogisticRegression_labels.csv", numpy.asarray(train_labels), delimiter=",")
    #numpy.savetxt("kagggle_teste_LogisticRegression_labels.csv", numpy.asarray(test_labels), delimiter=",")

    classifier = LogisticRegression()
    classifier.fit(train_arrays, train_labels)
    print(classifier.score(test_arrays, test_labels))


from sys import stdin

def main():
    sum = cnt = 0
    for num in stdin:
        num = int(num)
        if num > 0:
            cnt += 1
            sum += num
    print(f"The mean is {sum / cnt}.")

main()


import json
import boto3
import numpy as np
import io

# endpoint name defined in Jupyter notebook when the endpoint is created.
# You will need it to call the endpoint to do a new prediction.
endpoint_name = "<your endpoint>"

def lambda_handler(event, context):

    # reshape the data to fit the model
    data = np.array(event)
    data = data.reshape(1,data.shape[0]*data.shape[1])
    data = data.tolist()

    # call the Sagemaker endpoint
    client = boto3.client("runtime.sagemaker")
    response = client.invoke_endpoint(EndpointName=endpoint_name, Body=json.dumps(data))
    response_body = response['Body']
    bstr = response_body.read().decode("utf-8")

    # check the prediction result
    dbstr = eval(bstr)
    output_list = dbstr["outputs"]["score"]["floatVal"]
    y_class = np.array(output_list).argmax(axis=-1)
    print(y_class)

    # Send the prediction result back to our field sof sensor.
    iot = boto3.client('iot-data', region_name='eu-central-1')

    # Topic defined here must be the same as the subscribed topic in Node-RED:
    response = iot.publish(topic='status/mls160/', qos=1, payload=str(y_class))

    # As an example: If class is 2, then send message to mobile phone. Be sure to have the policy permission set.
    if(y_class == 2):

        # AWS SNS client
        sns = boto3.client('sns', region_name='eu-west-1') # SNS dont support every region
        number = "<cellphone numer>"
        sns.publish(PhoneNumber = number, Message="Hello this is a message!")


#intializing the prices
cake_buy = 20
cake_sell = 40
bread_buy = 10
bread_sell = 15
salary = 200 #for each employee 100 (2 employee= 2*100)

#getting input
count_bread = 0
count_cake = 0
bread = []      #array declaration
cake = []       #array declaration

#for loop to get input in array
for i in range(0,10):
    cnt1 = int(input("Enter the cake nos for customer : "))
    cake.append(cnt1)
    cnt2 = int(input("Enter the bread nos for customer : "))
    bread.append(cnt2)
    count_cake += cake[i]       #count the number of cakes
    count_bread += bread[i]     #count the number of breads
print("NUMBER OF CAKES : ",count_cake)
print("NUMBER OF BREADS: ",count_bread)

#to find profit
revenue_cake = count_cake * cake_sell      #price of one cake = 40
revenue_bread = count_bread * bread_sell    #price of one bread = 15
profit_cake = revenue_cake - (count_cake * cake_buy)
profit_bread = revenue_bread - (count_bread * bread_buy)
total_profit = profit_cake + profit_bread - salary

#print profit or loss
if(total_profit > 0):
    print("\n THE PROFIT IF : ",total_profit)
elif(total_profit < 0):
    print("\n THE LOSS IS : ",total_profit)
else:
    print("\n NO PROFIT OT LOSS")

August/07-08-2022/bakery profit or loss.py


# Recap 5
# For Loops

# Prints out all the indexes in the string / array
for item in "Python":
    print(item)

for bud in [4, 2, 0]:
    print(bud)
print("*" * 36)

# Prints out all the numbers in the range of numbers up to 10
for numbers in range(10):
    print(numbers)
print("*" * 36)

# Prints out all the numbers in the range 5 to 10 (increments in 2s)
for digits in range(5, 10, 2):
    print(digits)
print("*" * 36)

# Getting the total of an array
# Some variables
prices = [10, 20, 30]
total = 0

for price in prices:
    total += price
print(f"The total is: {total}")
print("*" * 36)

# An example of a nested loop
loopOutput = '''
0, 0
0, 1
0, 2
1, 0
1, 1
1, 2
2, 0
2, 1
2, 2
3, 0
3, 1
3, 2
'''
for x in range(4):
    for y in range(3):
        print(f"{x}, {y}")
print("*" * 36)

# Small challenge
numbers = [5, 2, 5, 2, 2]
letter = "x"

for digit in numbers:
    output = ""
    for count in range(digit):
        output += letter
    print(output)