text:
"""Interfaces for ClientModel and ServerModel."""

from abc import ABC, abstractmethod
import numpy as np
import os
import sys
import tensorflow as tf

from baseline_constants import ACCURACY_KEY

from utils.model_utils import batch_data
from utils.tf_utils import graph_size

class Model(ABC):

    def __init__(self, seed, lr, optimizer=None):
        self.lr = lr
        self.seed = seed
        self._optimizer = optimizer

        self.graph = tf.Graph()
        with self.graph.as_default():
            tf.set_random_seed(123 + self.seed)
            self.features, self.labels, self.train_op, self.eval_metric_ops, self.loss = self.create_model()
            self.saver = tf.train.Saver()
        self.sess = tf.Session(graph=self.graph)

        self.size = graph_size(self.graph)

        with self.graph.as_default():
            self.sess.run(tf.global_variables_initializer())

            metadata = tf.RunMetadata()
            opts = tf.profiler.ProfileOptionBuilder.float_operation()
            self.flops = tf.profiler.profile(self.graph, run_meta=metadata, cmd='scope', options=opts).total_float_ops

        np.random.seed(self.seed)

    def set_params(self, model_params):
        with self.graph.as_default():
            all_vars = tf.trainable_variables()
            for variable, value in zip(all_vars, model_params):
                variable.load(value, self.sess)

    def get_params(self):
        with self.graph.as_default():
            model_params = self.sess.run(tf.trainable_variables())
        return model_params

    @property
    def optimizer(self):
        """Optimizer to be used by the model."""
        if self._optimizer is None:
            self._optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.lr)

        return self._optimizer

    @abstractmethod
    def create_model(self):
        """Creates the model for the task.

        Returns:
            A 4-tuple consisting of:
                features: A placeholder for the samples' features.
                labels: A placeholder for the samples' labels.
                train_op: A Tensorflow operation that, when run with the features and
                    the labels, trains the model.
                eval_metric_ops: A Tensorflow operation that, when run with features and labels,
                    returns the accuracy of the model.
        """
        return None, None, None, None, None

    def train(self, data, num_epochs=1, batch_size=10):
        """
        Trains the client model.

        Args:
            data: Dict of the form {'x': [list], 'y': [list]}.
            num_epochs: Number of epochs to train.
            batch_size: Size of training batches.
        Return:
            comp: Number of FLOPs computed while training given data
            update: List of np.ndarray weights, with each weight array
                corresponding to a variable in the resulting graph
        """
        for _ in range(num_epochs):
            self.run_epoch(data, batch_size)

        update = self.get_params()
        comp = num_epochs * (len(data['y'])//batch_size) * batch_size * self.flops
        return comp, update

    def run_epoch(self, data, batch_size):

        for batched_x, batched_y in batch_data(data, batch_size, seed=self.seed):

            input_data = self.process_x(batched_x)
            target_data = self.process_y(batched_y)

            with self.graph.as_default():
                self.sess.run(self.train_op,
                    feed_dict={
                        self.features: input_data,
                        self.labels: target_data
                    })

    def test(self, data):
        """
        Tests the current model on the given data.

        Args:
            data: dict of the form {'x': [list], 'y': [list]}
        Return:
            dict of metrics that will be recorded by the simulation.
        """
        x_vecs = self.process_x(data['x'])
        labels = self.process_y(data['y'])
        with self.graph.as_default():
            tot_acc, loss = self.sess.run(
                [self.eval_metric_ops, self.loss],
                feed_dict={self.features: x_vecs, self.labels: labels}
            )
        acc = float(tot_acc) / x_vecs.shape[0]
        return {ACCURACY_KEY: acc, 'loss': loss}

    def close(self):
        self.sess.close()

    @abstractmethod
    def process_x(self, raw_x_batch):
        """Pre-processes each batch of features before being fed to the model."""
        pass

    @abstractmethod
    def process_y(self, raw_y_batch):
        """Pre-processes each batch of labels before being fed to the model."""
        pass

class ServerModel:
    def __init__(self, model):
        self.model = model

    @property
    def size(self):
        return self.model.size

    @property
    def cur_model(self):
        return self.model

    def send_to(self, clients):
        """Copies server model variables to each of the given clients

        Args:
            clients: list of Client objects
        """
        var_vals = {}
        with self.model.graph.as_default():
            all_vars = tf.trainable_variables()
            for v in all_vars:
                val = self.model.sess.run(v)
                var_vals[v.name] = val
        for c in clients:
            with c.model.graph.as_default():
                all_vars = tf.trainable_variables()
                for v in all_vars:
                    v.load(var_vals[v.name], c.model.sess)

    def save(self, path='checkpoints/model.ckpt'):
        return self.model.saver.save(self.model.sess, path)

    def close(self):
        self.model.close()


text:
import torch
import numpy as np
import torchvision
import torch.nn as nn
import time
import copy

from torch.autograd import Variable
from sklearn.metrics import f1_score

def train_model(model, data_loaders, weights, args):
	'''
	Main function used for training the networks.

	:param model: model to train
	:param data_loaders: dictionary containing train dataloader and validation dataloader
	:param weights: weights for the weighted crossentropy
	:return: checkpoint of the last epoch
	'''

	print('Start training of {} model:\n'.format(args.model_name))

	# Get weights to optimize
	optim_params = get_optim_params(model, args)

	# Enable cuda if available
	if args.cuda:
		model = model.cuda()
		weights = weights.cuda()

	# Define loss function, optimizer and start time
	criterion = nn.CrossEntropyLoss(reduce=False, weight=weights)
	optimizer = torch.optim.Adam(optim_params, lr=args.lr)
	start = time.time()

	# Dict for storing a number of important variables
	checkpoint_dict = {'train_loss':[], 'validation_loss':[], 'best_f1macro': 0.0, 'best_epoch_f1' : 0.0,
		'best_model_f1': model, 'best_avg_accuracy': 0.0, 'best_epoch_avg': 0, 'best_model_avg': model}

	for epoch in range(1, args.epochs+1):

		model, measures, epoch_loss_train, epoch_loss_val = run_epoch(model, data_loaders, optimizer, criterion, epoch, start, args)

		# Update other checkpoints
		create_checkpoint(checkpoint_dict, model, measures, epoch, epoch_loss_train, epoch_loss_val)

		# Save the checkpoint at specified path, only if path is defined
		if args.model_save_path:
			torch.save(checkpoint_dict, '{}{}_completed'.format(args.model_save_path, args.model_name))

	# Print the end status of the model
	elapsed_time = time.time() - start
	print('Training was completed in {:.0f}m {:.0f}s\n'.format(elapsed_time//60, elapsed_time%60))
	print('Best validation avg accuracy: {:.4f}% at epoch: {}'.format(checkpoint_dict['best_avg_accuracy'], checkpoint_dict['best_epoch_avg']))
	print('Best validation f1-macro: {:.4f} at epoch: {}'.format(checkpoint_dict['best_f1macro'], checkpoint_dict['best_epoch_f1']))

	return model

def create_checkpoint(checkpoint_dict, model, measures, epoch, epoch_loss_train, epoch_loss_val):
	'''
	Update the checkpoint dictionary after each epoch.

	:param checkpoint_dict: current checkpoint dictionary
	:param model: pytorch model object
	:param measures: f1-score macro score and average accuracy score for epoch
	:param epoch: current epoch
	:param epoch_loss_train: average train data loss of epoch
	:param epoch_loss_val: average validation loss of epoch
	:return: checkpoint dictionary

	'''
	# Update variables if the model state is better than previous model states
	if measures['f1_macro'] > checkpoint_dict['best_f1macro']:
		checkpoint_dict['best_f1macro'] = measures['f1_macro']
		checkpoint_dict['best_epoch_f1'] = epoch
		checkpoint_dict['best_model_f1'] = copy.deepcopy(model)
	if measures['avg_acc'] > checkpoint_dict['best_avg_accuracy']:
		checkpoint_dict['best_avg_accuracy'] = measures['avg_acc']
		checkpoint_dict['best_epoch_avg'] = epoch
		checkpoint_dict['best_model_avg'] = copy.deepcopy(model)

	# Extend arrays containing the loss
	checkpoint_dict['train_loss'].append(epoch_loss_train)
	checkpoint_dict['validation_loss'].append(epoch_loss_val)

	return checkpoint_dict

def run_epoch(model, loaders, optimizer, criterion, epoch, start, args):
	'''
	Runs a single epoch.

	:param model: pytorch model object
	:param loaders: dictionary containing train and validation loaders
	:param optimizer: pytorch optimizer object
	:param criterion: pytorch entropy loss
	:param epoch: current epoch
	:param start: model training start time
	:param args: parser arguments
	:return: updated model, model selection measures, epoch train loss and epoch validation loss
	'''

	running_loss_train = 0.0
	running_correct_train = 0.0
	running_loss_val = 0.0
	running_correct_val = 0.0
	validation_targets = []
	validation_predictions = []

	for phase in ['train', 'validation']:

		if phase == 'train':
			model.train()
		else:
			model.eval()

		# Iterate over batches in loader
		for i, (image_tensor, label_tensor, image_index, filepaths, meteo) in enumerate(loaders[phase]):

			features = Variable(image_tensor)
			targets = Variable(label_tensor.view(-1))
			meteo_features = Variable(meteo.type(torch.FloatTensor))

			# If cuda is available, make variables cuda
			if args.cuda:
				features = features.cuda()
				targets = targets.cuda()
				meteo_features = meteo_features.cuda()

			if phase == 'train':
				optimizer.zero_grad()

			# Check if you want to train
			if args.include_meteo:
				outputs = model(features, meteo_features)
			else:
				outputs = model(features)

			 # Get prediction index and no. correct predictions
			_, predictions = torch.max(outputs.data, 1)
			correct = torch.sum(predictions == targets.data)

			# Loss and optimization
			loss = criterion(outputs, targets)

			# Average the loss
			total_loss = torch.mean(loss)

			# Only do backpropagation if in the training phase
			if phase == 'train':
				total_loss.backward()
				optimizer.step()

			# Running loss and number of correct predictions
			if phase == 'train':
				running_loss_train += total_loss.data[0]
				running_correct_train += correct
			else:
				running_loss_val += total_loss.data[0]
				running_correct_val += correct
				validation_targets.extend(list(targets.data))
				validation_predictions.extend(list(predictions))

			# If model is in training phase, show loss every N iterations
			# if (i+1) % 50 == 0:
			if phase == 'train':
				print ('Epoch {}/{}, Iteration {}/{} Train Running Loss: {:.4f}'.format(epoch, args.epochs, i+1,
																			len(loaders[phase].dataset)//args.batch_size + 1,
																			running_loss_train / (i+1)))

	cur_time = time.time() - start

	# Gather epoch losses and evaluation metrics
	epoch_train_loss = running_loss_train / (len(loaders['train'].dataset)//args.batch_size)
	epoch_train_accuracy = (running_correct_train / (len(loaders['train'].dataset)) / args.batch_size * 100)
	epoch_val_loss = running_loss_val / (len(loaders['validation'].dataset) // args.batch_size)
	epoch_val_accuracy= (running_correct_val / (len(loaders['validation'].dataset) // args.batch_size)) / args.batch_size * 100
	f1_macro = f1_score(validation_targets, validation_predictions, average='macro')
	f1_micro = f1_score(validation_targets, validation_predictions, average='micro')
	average_accuracy = get_average_accuracy(validation_predictions, validation_targets)

	# Print the average epoch loss and the average prediction accuracy
	print('\nEpoch {}/{}, Train Time: {:.0f}m {:.0f}s\n Train Loss: {:.4f}, Train Overall Accuracy: {:.4f}%\n'
		  'Validation Loss: {:.4f}, Validation Overall Accuracy: {:.4f}%, Validation Avg Accuracy: {:.4f}% f1_macro: {:.4f}, f1_micro: {:.4f}\n'.format(epoch,
																				args.epochs, cur_time//60, cur_time%60, epoch_train_loss, epoch_train_accuracy,
																				epoch_val_loss, epoch_val_accuracy, average_accuracy, f1_macro, f1_micro))

	# Save the model state selection measures
	selection_measures = {'f1_macro' : f1_macro, 'avg_acc' : average_accuracy}

	return model, selection_measures, running_loss_val, running_correct_val

def get_average_accuracy(predictions, targets):
	'''
	Calculate the average accuracy for epoch predictions and targets.

	:param predictions: numpy array containing predictions of model training epoch
	:param targets: numpy array containing targets of model training epoch
	:return: epoch average accuracy
	'''

	# Lists for holding corrects
	no_fog_correct = 0
	light_fog_correct = 0
	dense_fog_correct = 0

	for pred, target in zip(predictions, targets):
		if pred == 0 and target == 0:
			no_fog_correct += 1
		elif pred == 1 and target == 1:
			light_fog_correct += 1
		elif pred == 2 and target == 2:
			dense_fog_correct += 1

	# Validation counts
	total = np.bincount(targets)
	no_fog_total = total[0]
	light_fog_total = total[1]
	dense_fog_total = total[2]

	# Accuracy per class
	acc_no_fog = no_fog_correct / no_fog_total
	acc_light = light_fog_correct / light_fog_total
	acc_dense = dense_fog_correct / dense_fog_total

	average_acc = (acc_no_fog + acc_light + acc_dense) / 3 * 100

	return average_acc

def get_optim_params(model, args):
	'''
	Retrieve the weights that have to be optimized.

	:param model: model class to be trained
	:param args: parser arguments
	:return: parameters that require optimization
	'''

	if args.model_name != 'shallow_CNN':

		# Per default, freeze all weights and unfreeze the FC layer
		for parameter in model.parameters():
			parameter.requires_grad = False

		for parameter in model.fc.parameters():
			parameter.requires_grad = True

		# Check to see if additional convolutional blocks should be unfrozen
		if args.from_conv_layer:

			# Check if meteo model or not
			if args.include_meteo:
				for parameter in model.meteo_net.parameters():
					parameter.requires_grad = True

				for i, (name, child) in enumerate(model.resnet18_convblocks.named_children()):
					if i + 1 > args.from_conv_layer:
						for name2, params in child.named_parameters():
							params.requires_grad = True

			else:
				# Unfreeze layer until 'from_conv_layer'
				for i, (name, child) in enumerate(model.named_children()):

					if i + 1 > args.from_conv_layer:
						print(name)
						for name2, params in child.named_parameters():
							params.requires_grad = True

	# Print number of countable parameters
	model_parameters = filter(lambda p: p.requires_grad, model.parameters())
	params = sum([np.prod(p.size()) for p in model_parameters])
	print('Number of weights that will be trained: {}'.format(params))

	# Get the optimizer parameters
	optim_params = filter(lambda p: p.requires_grad, model.parameters())

	return optim_params

def calculate_loss_weights(train_targets):
	'''
	Calculates weights for the loss function. Should be used when training the model by feeding it to
	criterion.

	:param train_targets: numpy array containing the train targets
	:return: array of loss weights
	'''

	class_counts = np.bincount(train_targets.astype(int))
	total = len(train_targets)

	proportion_0 = class_counts[0] / total
	proportion_1 = class_counts[1] / total
	proportion_2 = class_counts[2] / total
	proportions = [proportion_0, proportion_1, proportion_2]

	inverse_weights = 1 / torch.Tensor(proportions)

	return inverse_weights

ext: py

max_stars_repo_name: TimMolleman/FogDetection

max_issues_repo_name: TimMolleman/FogDetection

max_forks_repo_name: TimMolleman/FogDetection

reason: import numpy


text:
# coding: utf-8
import os

import cv2
from collections import defaultdict
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from logging import getLogger

logger = getLogger("__main__").getChild("indicator utility")

class IndicatorHolder(object):
    def __init__(self):
        self.indicator_values = defaultdict(list)
        self.indicator_total = defaultdict(pd.Series)
        self.which_area = pd.DataFrame()
        self.file_indicator = None

    def add_indicator_percentile(self, indicator_name, indicator):
        self.indicator_values[indicator_name].append(indicator)

    def add_total_indicator(self, indicator_name, indicator_series):
        self.indicator_total[indicator_name] = pd.concat([self.indicator_total[indicator_name],
                                                          indicator_series], axis=0)

    def add_total_area_info(self, which_area_info):
        self.which_area = pd.concat([self.which_area, which_area_info], axis=0)

    def summarize_file_indicator(self):
        self.file_indicator = pd.DataFrame(self.indicator_values)
        return self.file_indicator

    def summarize_total_indicator(self):
        indicator_summary = pd.DataFrame(self.file_indicator.mean()).T
        for key, indicator in self.indicator_total.items():
            indicator_summary[key] = np.median(indicator)
        return indicator_summary

    def calc_total_indicator_percentile_each_area(self):
        area_indicator = defaultdict(list)
        for area, indicator in zip(self.which_area, self.indicator_total):
            area_indicator[area].append(total)
        area_indicator50 = pd.DataFrame({area: np.median(v) for area, v in area_indicator.items()})
        return area_indicator50

def save_indicator(data, save_dir, save_filename):
    '''
    save CE, EAG as csv

    Paramters
    ---------
    data : list of float
    save_filename : str
    '''

    indicator_save_path = os.path.join(save_dir, save_filename)
    data.to_csv(indicator_save_path, index=False)

def area_of_ans(ans_point, area_info):
    '''
    Fucntion to classify each rows of ans_point to area_num

    Parameters
    ----------
    ans_point : pd.DataFrame
    area_info : pd.DataFrame

    Returns
    -------
    which_area_series : pd.Series
        number of area
    '''
    def get_area(row):
        for area_num in range(1, len(area_info)+1):
            target_area_info = area_info[area_info['area']==area_num]
            x_min = target_area_info['x_position_m'] - target_area_info['x_length']
            x_max = target_area_info['x_position_m'] + target_area_info['x_length']
            y_min = target_area_info['y_position_m'] - target_area_info['y_length']
            y_max = target_area_info['y_position_m'] + target_area_info['y_length']

            if x_min.values[0] <= row['x_position_m'] <= x_max.values[0] \
                and y_min.values[0] <= row['y_position_m'] <= y_max.values[0]:
                return area_num
        return 0
    which_area_series = ans_point.apply(get_area, axis=1)
    return which_area_series

def draw_cumulative_sum(data, indicator_name):
    '''
    draw cumulative sum of EAG and CE

    Parameters
    ----------
    data : list of float
    indicator_name : str
    '''

    eCDF_plus = 100 / len(data)
    eCDF = [eCDF_plus]
    for i in range(len(data)-1):
        eCDF.append(eCDF[i] + eCDF_plus)

    data.sort()

    fig = plt.figure()
    sns.set_style('whitegrid')
    plt.plot(data, eCDF)
    plt.title(f'{indicator_name}')
    if indicator_name is 'CE':
        plt.xlabel(f'{indicator_name} [m]')
    elif indicator_name is 'EAG':
        plt.xlabel(f'{indicator_name} [m/s]')
    plt.ylabel('eCDF')

    plt.close()

    return fig

def draw_histgram(data, indicator_name, percentile=50, bins=None):
    '''
    draw histgram of EAG and CE

    Parameters
    ----------
    data : list of float
    bins : int
    '''

    logger.debug('draw histgram')
    fig = plt.figure()
    sns.set_style('whitegrid')
    plt.rcParams['font.size'] = 12
    percentile_value = calc_percentile(data, percentile)
    sns.distplot(data, kde=False, rug=False, bins=bins)
    plt.axvline(percentile_value, color='k', linestyle='dashed', linewidth=1)
    plt.title(f'{indicator_name}{percentile}: {percentile_value:.2f}')
    plt.xlabel(f'{indicator_name}')
    plt.ylabel('Frequency')

    plt.close()

    return fig

def calc_percentile(data, percentile):
    if percentile < 0 or 100 < percentile:
        raise ValueError('percentile shoud be between 0 and 1')
    return np.percentile(data, q=percentile)

def save_figure(figure, save_dir, save_filename):
    fig_save_path = os.path.join(save_dir, save_filename)
    figure.savefig(fig_save_path)
    logger.debug('save figure at {}'.format(fig_save_path))

def save_total_indicator(data, indicator_name, save_dir, save_filename):
    save_path = os.path.join(save_dir, save_filename)
    save_df = pd.DataFrame({indicator_name : data})
    save_df.to_csv(save_path, index=False)
    logger.debug('save at {}'.format(save_path))

def save_dataframe(save_dir, save_filename, df):
    save_path = os.path.join(save_dir, save_filename)
    df.to_csv(save_path, index=False)

def filter_area_point(evaluation_point, area_info, area_num):
    '''
    Get evaluation point inside target area

    Parameters
    ----------
    evaluation_point: DataFrame
        columns = ['unixtime', 'x_position_m', 'y_position_m']
    area_info: DataFrame
        columns = ['area', 'x_position_m', 'y_position_m', 'x_length', 'y_length']
    area_num: int
        target area number
    '''

    logger.debug('Get area {} evaluation point START'.format(area_num))

    target_area_info = area_info[area_info['area']==area_num]
    x_min = target_area_info['x_position_m'] - target_area_info['x_length']
    x_max = target_area_info['x_position_m'] + target_area_info['x_length']
    y_min = target_area_info['y_position_m'] - target_area_info['y_length']
    y_max = target_area_info['y_position_m'] + target_area_info['y_length']

    area_eval_point = evaluation_point.query(f'{x_min.values[0]} < x_position_m < {x_max.values[0]}')

    area_eval_point = area_eval_point.query(f'{y_min.values[0]} < y_position_m < {y_max.values[0]}')

    logger.debug('Get area {} evaluation point END, area_eval_point:{}'.\
                format(area_num, area_eval_point.shape))
    return area_eval_point

def get_CA_area_weights(evaluation_point, area_info):
    '''
    calculate CA area weights

    Parameters
    ------
    evaluation_point: DataFrame
        columns = ['unixtime', 'x_position_m', 'y_position_m']
    area_info: DataFrame
        columns = ['area', 'x_position_m', 'y_position_m', 'x_length', 'y_length']

    Return
    ------
    weights: list of float
        ratio of area's evaluation points in whole area evaluation points
    '''

    logger.debug('calculate CA area weights START')

    if area_info is None:
        return None

    area_point_counts = []
    for area_num in range(len(area_info)):
        area_eval_point = filter_area_point(evaluation_point, area_info, area_num+1)
        area_point_counts.append(len(area_eval_point))

    total_points = sum(area_point_counts)
    weights = [area_point / total_points for area_point in area_point_counts]

    logger.debug('calculate EDD area weights END, weight: {}'.format(weights))

    return weights

def draw_trajectory(tra_data, map_image, map_size, indicator_name, ref_point, BLE_info, map_color, map_makersize):
    '''
    draw trajectory on maps

    Parameters
    ----------
    tra_data : list of float
    map_image : bitmap
    map_size : float
    '''

    fig = plt.figure(dpi=600)
    map_image2 = cv2.bitwise_not(map_image)
    plt.imshow(map_image2, cmap=map_color[0], extent=[0, map_size[0], 0, map_size[1]])
    plt.plot(tra_data['x_position_m'], tra_data['y_position_m'], color=map_color[1], lw=map_makersize[0], label='Trajectory')
    plt.plot(ref_point['x_position_m'], ref_point['y_position_m'], color=map_color[2], linestyle='None', marker='+', markersize=map_makersize[1], label='Reference')
    plt.plot(BLE_info['x_position_m'], BLE_info['y_position_m'], color=map_color[3], linestyle='None', marker='.', markersize=map_makersize[2], label='BLE')
    plt.title(f'{indicator_name}')
    plt.xlabel('x [m]')
    plt.ylabel('y [m]')
    plt.legend(fontsize='small')
    plt.grid(map_makersize[3])

    return fig

def draw_CE_map(indicator_df, map_image, map_size, indicator_name):
    '''
    draw CE on maps

    Parameters
    ----------
    tra_data : list of float
    map_image : bitmap
    map_size : float
    '''
    import matplotlib.cm as cm
    import matplotlib.colors as colors
    cmap = cm.cool
    cmap_data = cmap(np.arange(cmap.N))
    cmap_data[0, 3] = 0 # 0 のときのα値を0(透明)にする
    customized_cool = colors.ListedColormap(cmap_data)

    fig = plt.figure(dpi=600)
    #ax = fig.add_subplot(111)
    ax = fig.add_axes((0.05, 0.05, 0.8, 0.9))

    ax.imshow(map_image, extent=[0, map_size[0], 0, map_size[1]])

    error_map = calc_CE_map(indicator_df, map_size)
    X, Y = np.mgrid[0:error_map.shape[0], 0:error_map.shape[1]]
    im = ax.pcolormesh(X, Y, error_map, cmap=customized_cool)

    cax = fig.add_axes((0.9, 0.15, 0.03, 0.7))
    plt.colorbar(im, cax=cax)

    ax.set_title(f'{indicator_name}_error_map')
    ax.set_xlabel('x [m]')
    ax.set_ylabel('y [m]')

    return fig

def calc_CE_map(indicator_df, map_size, cell_size=1.0):
    error_map_x = int(map_size[0] / cell_size)
    error_map_y = int(map_size[1] / cell_size)
    error_sum_map = np.zeros((error_map_x, error_map_y))
    error_count_map = np.zeros((error_map_x, error_map_y))

    def add_error(row):
        error_x = int(row['eval_x'] / cell_size)
        error_y = int(row['eval_y'] / cell_size)
        error_sum_map[error_x, error_y] += row['CE']
        error_count_map[error_x, error_y] += 1

    indicator_df.apply(add_error, axis=1)

    # avoid division by zero
    error_count_map += 1e-8
    error_map = error_sum_map / error_count_map

    return error_map


text:
#! /usr/bin/env python

from datetime import datetime
import numpy as np

from pyquaternion import Quaternion as QuaternionType

from std_msgs.msg import Header
from geometry_msgs.msg import Pose, Point, Quaternion
from builtin_interfaces.msg import Time

from .base_communication import BaseCommunication
from robot_framework.state import State
from sync_and_swarm_msgs.msg import State as StateMsg

class ROSCommunication(BaseCommunication):
    def __init__(self, *args, **kwargs):
        self.ident = kwargs.pop('agent_id')
        self.node = kwargs.pop('node')
        self.params = kwargs.pop('params')
        self.received = 0
        self.delayed = 0
        if self.ident != 'base_station':
            self.state_publisher = self.node.create_publisher(
                StateMsg,
                '/state',
                100
            )
        self.state_subscription = self.node.create_subscription(
            StateMsg,
            '/state',
            self.receive_state_callback,
            100
        )
        super(ROSCommunication, self).__init__(
            *args, **kwargs
        )

    def create_state_from_msg(self, msg):
        pos = msg.pose.position
        orient = msg.pose.orientation
        if self.params['orientation_mode']:
            orientation = QuaternionType(
                [orient.w, orient.x, orient.y, orient.z]
            )
        else:
            orientation = None
        position = np.array([pos.x, pos.y, pos.z])
        phase = msg.phase
        return State(
            position=position, phase=phase, orientation=orientation,
            params=self.params,
            sent_timestamp=datetime.fromtimestamp(msg.header.stamp.sec + msg.header.stamp.nanosec / 10**9),
            received_timestamp=datetime.now(),
        )

    def create_state_msg(self, sender, state, recipient_ident):
        now = self.node.get_clock().now()
        nanosec, sec = map(
            int,
            (now.nanoseconds % 10**9, now.nanoseconds/10**9)
        )
        header = Header(
            frame_id="",
            stamp=Time(sec=sec, nanosec=nanosec)
        )
        if state.orientation_mode:
            orientation = Quaternion(
                x=state.orientation.elements[1],
                y=state.orientation.elements[2],
                z=state.orientation.elements[3],
                w=state.orientation.elements[0]
            )
        else:
            orientation = None

        sender_ident = int(sender)
        phase = float(state.phase)
        msg = StateMsg(
            header=header,
            id=sender_ident,
            phase=phase,
            pose=Pose(
                position=Point(
                    x=state.position[0],
                    y=state.position[1],
                    z=state.position[2]
                ),
                orientation=orientation,
            )
        )
        return msg

    def send_state_unconditionally(self, sender, state, recipient_ident):
        msg = self.create_state_msg(sender, state, recipient_ident)
        self.state_publisher.publish(msg)

    def receive_state_callback(self, msg):
        state = self.create_state_from_msg(msg)
        self.receive_state(self.ident, state, str(msg.id).zfill(2))

    def receive_state(self, own_ident, state, sender_ident):
        if sender_ident == own_ident:
            return
        self.received += 1
        if (state.received_timestamp - state.timestamp).total_seconds() > 0.2:
            self.delayed += 1
        if self.check_receive_filters(own_ident, sender_ident, state):
            self.receive_state_unconditionally(
                own_ident, state, sender_ident
            )

    def receive_state_unconditionally(self, own_ident, state, sender_ident):
        self.system_state.knowledge.update_state(
            own_ident=own_ident,
            other_ident=sender_ident,
            new_state=State(state=state, received_timestamp=datetime.now()),
        )

max_stars_repo_name: abarcis/robot-framework

max_issues_repo_name: abarcis/robot-framework

max_forks_repo_name: abarcis/robot-framework


text:
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 18 23:08:13 2018

@author: Subhajeet
"""

def data_clean(df):
    import numpy as np
    df=df.select_dtypes([np.number])
    return df

def data_norm(df):
    import numpy as np
    d_m=np.mean(df)
    d_s=np.std(df)
    d_n=(df-d_m)/d_s
    d_n=d_n.dropna(axis=1)
    return d_n

def data_eig(df):
    import numpy as np
    eig=np.linalg.eig
    dmat=np.matrix(df.T)
    dmatt=dmat.T
    u1=np.matmul(dmat,dmatt)
    v1=np.matmul(dmatt,dmat)
    ueva,uevec=eig(u1)
    veva,vevec=eig(v1)
    m=min(u1.shape)
    n=min(v1.shape)
    j=ueva.argsort()[::-1]
    ue_v=ueva[j]
    uev=uevec[:,j]
    i=veva.argsort()[::-1]
    ve_v=veva[i]
    vev=vevec[:,i]
    return ue_v,uev,ve_v,vev,m,n

def compute_df_u_e_v(df,ue_v,uev,ve_v,vev,m,n):
    import numpy as np
    if m>n:
        l=m-n
        k=n
        diag=np.diag(ve_v)
        z=np.zeros((l,k))
        e=np.concatenate((diag,z))
        eta=np.sqrt(e)
        u=np.matrix(uev)
        v=np.matrix(vev).T
        A=np.matrix(df.T)

    else:
        l=n-m
        k=m
        diag=np.diag(ue_v)
        z=np.zeros((l,k))
        e=np.concatenate((diag,z))
        eta=np.sqrt(e)
        u=np.matrix(vev)
        v=np.matrix(uev).T
        A=np.matrix(df.T).T
    return A,u,v,eta,k,diag

def compute_u(A,v,eta,k):
    import numpy as np
    u_e=[]
    for i in range(k):
        u=np.matmul(A,v[:,i]/np.sqrt(np.linalg.norm(v[:,i])))/np.sqrt(np.linalg.norm(eta))
        u_e.append(u)
    U=np.matrix(np.array(u_e)).T
    return U

def plot_error2(A,U,v,diag,k):
    import numpy as np
    import matplotlib.pyplot as plt
    from numpy.linalg import norm
    from sklearn.metrics import mean_absolute_error as mae
    errr=[]
    for i in range(1,k+1):
        dmatf=np.matmul(np.matmul(U[:,:i],diag[:i,:i])/np.sqrt(norm(diag[:i,:i])),v[:i,:]/np.sqrt(norm(v[:i,:])))
        errr.append(mae(A,dmatf))
    return plt.plot(errr)

def error_svd(A,u,v,eta):
    import numpy as np
    from sklearn.metrics import mean_absolute_error as mae
    dmatf=np.matmul(np.matmul(u,eta),v)
    return mae(A,dmatf)

def svd(df):
    df=data_norm(data_clean(df))
    ue_v,uev,ve_v,vev,m,n=data_eig(df)
    A,u,v,eta,k,diag=compute_df_u_e_v(df,ue_v,uev,ve_v,vev,m,n)
    U=compute_u(A,v,eta,k)
    print(U,eta,v)
    plot_error2(A,U,v,diag,k)

max_stars_repo_name: Sjeetm/svd

max_issues_repo_name: Sjeetm/svd

max_forks_repo_name: Sjeetm/svd

reason: import numpy,from numpy


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

# system libraries
from __future__ import print_function
from mpi4py import MPI
import numpy as np
import sys, os

# runko + auxiliary modules
import pytools  # runko python tools

# problem specific modules
from problem import Configuration_Problem as Configuration

np.random.seed(1)  # global simulation seed

# Prtcl velocity (and location modulation inside cell)
#
# NOTE: Cell extents are from xloc to xloc + 1, i.e., dx = 1 in all dims.
#       Therefore, typically we use position as x0 + RUnif[0,1).
#
# NOTE: Default injector changes odd ispcs's loc to (ispcs-1)'s prtcl loc.
#       This means that positrons/ions are on top of electrons to guarantee
#       charge conservation (because zero charge density initially).
#
def velocity_profile(xloc, ispcs, conf):

    # electrons
    if ispcs == 0:
        delgam = conf.delgam  # * np.abs(conf.mi / conf.me) * conf.temp_ratio

    # positrons/ions/second species
    elif ispcs == 1:
        delgam = conf.delgam

    # perturb position between x0 + RUnif[0,1)
    xx = xloc[0] + np.random.rand(1)
    yy = xloc[1] + np.random.rand(1)
    zz = xloc[2] + np.random.rand(1)

    # velocity sampling
    gamma = conf.gamma
    direction = -1
    ux, uy, uz, uu = pytools.sample_boosted_maxwellian(
        delgam, gamma, direction=direction, dims=3
    )

    x0 = [xx, yy, zz]
    u0 = [ux, uy, uz]
    return x0, u0

# number of prtcls of species 'ispcs' to be added to cell at location 'xloc'
#
# NOTE: Plasma frequency is adjusted based on conf.ppc (and prtcl charge conf.qe/qi
#       and mass conf.me/mi are computed accordingly) so modulate density in respect
#       to conf.ppc only.
#
def density_profile(xloc, ispcs, conf):

    # uniform plasma with default n_0 number density
    return conf.ppc

# Field initialization
def insert_em_fields(grid, conf):

    # into radians
    btheta = conf.btheta / 180.0 * np.pi
    bphi = conf.bphi / 180.0 * np.pi
    beta = conf.beta

    for tile in pytools.tiles_all(grid):
        yee = tile.get_yee(0)

        ii,jj,kk = tile.index if conf.threeD else (*tile.index, 0)

        # insert values into Yee lattices; includes halos from -3 to n+3
        for n in range(-3, conf.NzMesh + 3):
            for m in range(-3, conf.NyMesh + 3):
                for l in range(-3, conf.NxMesh + 3):
                    # get global coordinates
                    iglob, jglob, kglob = pytools.ind2loc((ii, jj, kk), (l, m, n), conf)
                    r = np.sqrt(iglob ** 2 + jglob ** 2 + kglob ** 2)

                    yee.bx[l, m, n] = conf.binit * np.cos(bphi)
                    yee.by[l, m, n] = conf.binit * np.sin(bphi) * np.sin(btheta)
                    yee.bz[l, m, n] = conf.binit * np.sin(bphi) * np.cos(btheta)

                    yee.ex[l, m, n] = 0.0
                    yee.ey[l, m, n] = -beta * yee.bz[l, m, n]
                    yee.ez[l, m, n] = beta * yee.by[l, m, n]
    return

if __name__ == "__main__":

    # --------------------------------------------------
    # initial setup
    do_print = False
    if MPI.COMM_WORLD.Get_rank() == 0:
        do_print = True

    if do_print:
        print("Running pic.py with {} MPI processes.".format(MPI.COMM_WORLD.Get_size()))

    # --------------------------------------------------
    # Timer for profiling
    timer = pytools.Timer()

    timer.start("total")
    timer.start("init")
    timer.do_print = do_print

    # --------------------------------------------------
    # parse command line arguments
    args = pytools.parse_args()

    # create conf object with simulation parameters based on them
    conf = Configuration(args.conf_filename, do_print=do_print)

    # --------------------------------------------------
    # load runko

    if conf.threeD:
        # 3D modules
        import pycorgi.threeD as pycorgi  # corgi ++ bindings
        import pyrunko.pic.threeD as pypic  # runko pic c++ bindings
        import pyrunko.fields.threeD as pyfld  # runko fld c++ bindings

    elif conf.twoD:
        # 2D modules
        import pycorgi.twoD as pycorgi  # corgi ++ bindings
        import pyrunko.pic.twoD as pypic  # runko pic c++ bindings
        import pyrunko.fields.twoD as pyfld  # runko fld c++ bindings

    # --------------------------------------------------
    # setup grid
    grid = pycorgi.Grid(conf.Nx, conf.Ny, conf.Nz)
    grid.set_grid_lims(conf.xmin, conf.xmax, conf.ymin, conf.ymax, conf.zmin, conf.zmax)

    # compute initial mpi ranks using Hilbert's curve partitioning
    pytools.balance_mpi(grid, conf)

    # load pic tiles into grid
    pytools.pic.load_tiles(grid, conf)

    # --------------------------------------------------
    # simulation restart

    # create output folders
    if grid.master():
        pytools.create_output_folders(conf)

    # get current restart file status
    io_stat = pytools.check_for_restart(conf)

    # no restart file; initialize simulation
    if io_stat["do_initialization"]:
        if do_print:
            print("initializing simulation...")
        lap = 0

        np.random.seed(1)  # sync rnd generator seed for different mpi ranks

        # injecting plasma particles
        prtcl_stat = pytools.pic.inject(grid, velocity_profile, density_profile, conf)
        if do_print:
            print("injected:")
            print("    e- prtcls: {}".format(prtcl_stat[0]))
            print("    e+ prtcls: {}".format(prtcl_stat[1]))

        # inserting em grid
        insert_em_fields(grid, conf)

    else:
        if do_print:
            print("restarting simulation from lap {}...".format(io_stat["lap"]))

        # read restart files
        pyfld.read_yee(grid, io_stat["read_lap"], io_stat["read_dir"])
        pypic.read_particles(grid, io_stat["read_lap"], io_stat["read_dir"])

        # step one step ahead
        lap = io_stat["lap"] + 1

    # --------------------------------------------------
    # static load balancing setup; communicate neighborhood info once

    grid.analyze_boundaries()
    grid.send_tiles()
    grid.recv_tiles()
    MPI.COMM_WORLD.barrier()

    # load virtual mpi halo tiles
    pytools.pic.load_virtual_tiles(grid, conf)

    # --------------------------------------------------
    # load physics solvers

    # pusher   = pypic.BorisPusher()
    pusher = pypic.VayPusher()

    # fldprop  = pyfld.FDTD2()
    fldprop = pyfld.FDTD4()

    fintp = pypic.LinearInterpolator()
    currint = pypic.ZigZag()
    flt = pyfld.Binomial2(conf.NxMesh, conf.NyMesh, conf.NzMesh)

    # enhance numerical speed of light slightly to suppress numerical Cherenkov instability
    fldprop.corr = 1.02

    # --------------------------------------------------
    # I/O objects

    # quick field snapshots
    fld_writer = pyfld.FieldsWriter(
        conf.outdir,
        conf.Nx,
        conf.NxMesh,
        conf.Ny,
        conf.NyMesh,
        conf.Nz,
        conf.NzMesh,
        conf.stride,
    )

    # test particles
    prtcl_writer = pypic.TestPrtclWriter(
        conf.outdir,
        conf.Nx,
        conf.NxMesh,
        conf.Ny,
        conf.NyMesh,
        conf.Nz,
        conf.NzMesh,
        conf.ppc,
        len(grid.get_local_tiles()),
        conf.n_test_prtcls,
    )

    mom_writer = pypic.PicMomentsWriter(
        conf.outdir,
        conf.Nx,
        conf.NxMesh,
        conf.Ny,
        conf.NyMesh,
        conf.Nz,
        conf.NzMesh,
        conf.stride,
    )

    # 3D box peripherals
    if conf.threeD:
        slice_xy_writer = pyfld.FieldSliceWriter( conf.outdir,
                conf.Nx, conf.NxMesh, conf.Ny, conf.NyMesh, conf.Nz, conf.NzMesh, 1,
                0, 1)
        slice_xz_writer = pyfld.FieldSliceWriter( conf.outdir,
                conf.Nx, conf.NxMesh, conf.Ny, conf.NyMesh, conf.Nz, conf.NzMesh, 1,
                1, 1)
        slice_yz_writer = pyfld.FieldSliceWriter( conf.outdir,
                conf.Nx, conf.NxMesh, conf.Ny, conf.NyMesh, conf.Nz, conf.NzMesh, 1,
                2, 1)

    # --------------------------------------------------
    # reflecting leftmost wall
    piston = pypic.Piston()

    # set piston wall speed (for standard reflector it is non-moving so gam = 0)
    piston.walloc = 5.0  # leave 5 cell spacing between the wall for boundary conditions

    if conf.wallgamma > 0.0:
        #moving wall
        piston.gammawall = conf.wallgamma
        piston.betawall = np.sqrt(1.0 - 1.0 / conf.wallgamma ** 2.0)
    else:
        #stationary wall
        piston.betawall = 0.0
        piston.gammawall = 1.0

    # --------------------------------------------------
    # --------------------------------------------------
    # --------------------------------------------------
    # end of initialization

    timer.stop("init")
    timer.stats("init")
    # timer.verbose = 1  # 0 normal; 1 - debug mode

    # --------------------------------------------------
    # sync e and b fields

    # mpi e
    grid.send_data(1)
    grid.recv_data(1)
    grid.wait_data(1)

    # mpi b
    grid.send_data(2)
    grid.recv_data(2)
    grid.wait_data(2)

    for tile in pytools.tiles_all(grid):
        tile.update_boundaries(grid)

    ##################################################
    # simulation time step loop

    sys.stdout.flush()

    # simulation loop
    time = lap * (conf.cfl / conf.c_omp)
    for lap in range(lap, conf.Nt + 1):

        # --------------------------------------------------
        # push B half
        t1 = timer.start_comp("push_half_b1")
        for tile in pytools.tiles_all(grid):
            fldprop.push_half_b(tile)
            piston.field_bc(tile)
        timer.stop_comp(t1)

        # --------------------------------------------------
        # comm B
        t1 = timer.start_comp("mpi_b2")
        grid.send_data(2)
        grid.recv_data(2)
        grid.wait_data(2)
        timer.stop_comp(t1)

        # --------------------------------------------------
        # update boundaries
        t1 = timer.start_comp("upd_bc1")
        for tile in pytools.tiles_all(grid):
            tile.update_boundaries(grid)
        timer.stop_comp(t1)

        ##################################################
        # move particles (only locals tiles)

        # --------------------------------------------------
        # interpolate fields
        t1 = timer.start_comp("interp_em")
        for tile in pytools.tiles_local(grid):
            fintp.solve(tile)
        timer.stop_comp(t1)

        # --------------------------------------------------
        # push particles in x and u
        t1 = timer.start_comp("push")
        for tile in pytools.tiles_local(grid):
            pusher.solve(tile)
        timer.stop_comp(t1)

        # --------------------------------------------------
        # apply moving/reflecting walls
        t1 = timer.start_comp("walls")
        for tile in pytools.tiles_local(grid):
            piston.solve(tile)
        timer.stop_comp(t1)

        ##################################################
        # advance B half

        # --------------------------------------------------
        # push B half
        t1 = timer.start_comp("push_half_b2")
        for tile in pytools.tiles_all(grid):
            fldprop.push_half_b(tile)
            piston.field_bc(tile)
        timer.stop_comp(t1)

        # --------------------------------------------------
        # comm B
        t1 = timer.start_comp("mpi_e1")
        grid.send_data(1)
        grid.recv_data(1)
        grid.wait_data(1)
        timer.stop_comp(t1)

        # --------------------------------------------------
        # update boundaries
        t1 = timer.start_comp("upd_bc2")
        for tile in pytools.tiles_all(grid):
            tile.update_boundaries(grid)
        timer.stop_comp(t1)

        ##################################################
        # advance E

        # --------------------------------------------------
        # push E
        t1 = timer.start_comp("push_e")
        for tile in pytools.tiles_all(grid):
            fldprop.push_e(tile)
            piston.field_bc(tile)
        timer.stop_comp(t1)

        # --------------------------------------------------
        # current calculation; charge conserving current deposition
        t1 = timer.start_comp("comp_curr")
        for tile in pytools.tiles_local(grid):
            currint.solve(tile)
        timer.stop_comp(t1)

        # --------------------------------------------------
        # clear virtual current arrays for boundary addition after mpi
        t1 = timer.start_comp("clear_vir_cur")
        for tile in pytools.tiles_virtual(grid):
            tile.clear_current()
        timer.stop_comp(t1)

        # --------------------------------------------------
        # mpi send currents
        t1 = timer.start_comp("mpi_cur")
        grid.send_data(0)
        grid.recv_data(0)
        grid.wait_data(0)
        timer.stop_comp(t1)

        # --------------------------------------------------
        # exchange currents
        t1 = timer.start_comp("cur_exchange")
        for tile in pytools.tiles_all(grid):
            tile.exchange_currents(grid)
        timer.stop_comp(t1)

        ##################################################
        # particle communication (only local/boundary tiles)

        # --------------------------------------------------
        # local particle exchange (independent)
        t1 = timer.start_comp("check_outg_prtcls")
        for tile in pytools.tiles_local(grid):
            tile.check_outgoing_particles()
        timer.stop_comp("check_outg_prtcls")

        # --------------------------------------------------
        # global mpi exchange (independent)
        t1 = timer.start_comp("pack_outg_prtcls")
        for tile in pytools.tiles_boundary(grid):
            tile.pack_outgoing_particles()
        timer.stop_comp(t1)

        # --------------------------------------------------
        # MPI global particle exchange
        # transfer primary and extra data
        t1 = timer.start_comp("mpi_prtcls")
        grid.send_data(3)
        grid.recv_data(3)
        grid.wait_data(3)

        # orig just after send3
        grid.send_data(4)
        grid.recv_data(4)
        grid.wait_data(4)
        timer.stop_comp(t1)

        # --------------------------------------------------
        # global unpacking (independent)
        t1 = timer.start_comp("unpack_vir_prtcls")
        for tile in pytools.tiles_virtual(grid):
            tile.unpack_incoming_particles()
            tile.check_outgoing_particles()
        timer.stop_comp(t1)

        # --------------------------------------------------
        # transfer local + global
        t1 = timer.start_comp("get_inc_prtcls")
        for tile in pytools.tiles_local(grid):
            tile.get_incoming_particles(grid)
        timer.stop_comp(t1)

        # --------------------------------------------------
        # delete local transferred particles
        t1 = timer.start_comp("del_trnsfrd_prtcls")
        for tile in pytools.tiles_local(grid):
            tile.delete_transferred_particles()
        timer.stop_comp(t1)

        # --------------------------------------------------
        # delete all virtual particles (because new prtcls will come)
        t1 = timer.start_comp("del_vir_prtcls")
        for tile in pytools.tiles_virtual(grid):
            tile.delete_all_particles()
        timer.stop_comp(t1)

        ##################################################
        # filter
        timer.start_comp("filter")

        # sweep over npasses times
        for fj in range(conf.npasses):

            # update global neighbors (mpi)
            grid.send_data(0)
            grid.recv_data(0)
            grid.wait_data(0)

            # get halo boundaries and filter
            for tile in pytools.tiles_local(grid):
                tile.update_boundaries(grid)
            for tile in pytools.tiles_local(grid):
                flt.solve(tile)

            MPI.COMM_WORLD.barrier()  # sync everybody

        # clean current behind piston
        if conf.npasses > 0:
            for tile in pytools.tiles_local(grid):
                piston.field_bc(tile)

        # --------------------------------------------------
        timer.stop_comp("filter")

        # --------------------------------------------------
        # add current to E
        t1 = timer.start_comp("add_cur")
        for tile in pytools.tiles_all(grid):
            tile.deposit_current()
        timer.stop_comp(t1)

        # comm E
        t1 = timer.start_comp("mpi_e2")
        grid.send_data(1)
        grid.recv_data(1)
        grid.wait_data(1)
        timer.stop_comp(t1)

        # --------------------------------------------------
        # comm B
        t1 = timer.start_comp("mpi_b1")
        grid.send_data(2)
        grid.recv_data(2)
        grid.wait_data(2)
        timer.stop_comp(t1)

        # --------------------------------------------------
        # update boundaries
        t1 = timer.start_comp("upd_bc0")
        for tile in pytools.tiles_all(grid):
            tile.update_boundaries(grid)
        timer.stop_comp(t1)

        # moving walls
        #piston.walloc += piston.betawall*conf.cfl

        ##################################################
        # data reduction and I/O

        timer.lap("step")
        if lap % conf.interval == 0:
            if do_print:
                print("--------------------------------------------------")
                print("------ lap: {} / t: {}".format(lap, time))

            timer.stats("step")
            timer.comp_stats()
            timer.purge_comps()

            # analyze (independent)
            timer.start("io")

            # shrink particle arrays
            for tile in pytools.tiles_all(grid):
                tile.shrink_to_fit_all_particles()

            # analyze
            # for tile in pytools.tiles_local(grid): analyzer.analyze2d(tile)

            # barrier for quick writers
            MPI.COMM_WORLD.barrier()

            # shallow IO
            # NOTE: do moms before other IOs to keep rho up-to-date
            mom_writer.write(grid, lap)  # pic distribution moments;
            fld_writer.write(grid, lap)  # quick field snapshots
            prtcl_writer.write(grid, lap)  # test particles

            #box peripheries
            if conf.threeD:
                slice_xy_writer.write(grid, lap)
                slice_xz_writer.write(grid, lap)
                slice_yz_writer.write(grid, lap)

            # deep IO
            if conf.full_interval > 0 and (lap % conf.full_interval == 0) and (lap > 0):
                pyfld.write_yee(grid, lap, conf.outdir + "/full_output/")
                pypic.write_particles(grid, lap, conf.outdir + "/full_output/")
                # pypic.write_analysis(grid, lap, conf.outdir + "/full_output/")

            # restart IO (overwrites)
            if (lap % conf.restart == 0) and (lap > 0):

                # flip between two sets of files
                io_stat["deep_io_switch"] = 1 if io_stat["deep_io_switch"] == 0 else 0

                pyfld.write_yee(
                    grid, io_stat["deep_io_switch"], conf.outdir + "/restart/"
                )
                pypic.write_particles(
                    grid, io_stat["deep_io_switch"], conf.outdir + "/restart/"
                )

                # if successful adjust info file
                MPI.COMM_WORLD.barrier()  # sync everybody in case of failure before write
                if grid.rank() == 0:
                    with open(conf.outdir + "/restart/laps.txt", "a") as lapfile:
                        lapfile.write("{},{}\n".format(lap, io_stat["deep_io_switch"]))

            timer.stop("io")

            timer.stats("io")
            timer.start("step")  # refresh lap counter (avoids IO profiling)

            sys.stdout.flush()

        # next step
        time += conf.cfl / conf.c_omp
    # end of loop

    # --------------------------------------------------
    # end of simulation

    timer.stop("total")
    timer.stats("total")


text:
# -*- coding: utf-8 -*-
"""
Produces simple Sankey Diagrams with matplotlib.
@author: Anneya Golob & marcomanz & pierre-sassoulas & jorwoods
                      .-.
                 .--.(   ).--.
      <-.  .-.-.(.->          )_  .--.
       `-`(     )-'             `)    )
         (o  o  )                `)`-'
        (      )                ,)
        ( ()  )                 )
         `---"\    ,    ,    ,/`
               `--' `--' `--'
                |  |   |   |
                |  |   |   |
                '  |   '   |
"""

from collections import defaultdict

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

class PySankeyException(Exception):
    pass

class NullsInFrame(PySankeyException):
    pass

class LabelMismatch(PySankeyException):
    pass

def check_data_matches_labels(labels, data, side):
    if len(labels) > 0:
        if isinstance(data, list):
            data = set(data)
        if isinstance(data, pd.Series):
            data = set(data.unique().tolist())
        if isinstance(labels, list):
            labels = set(labels)
        if labels != data:
            msg = "\n"
            if len(labels) <= 20:
                msg = "Labels: " + ",".join(labels) + "\n"
            if len(data) < 20:
                msg += "Data: " + ",".join(data)
            raise LabelMismatch('{0} labels and data do not match.{1}'.format(side, msg))

def sankey(left, right, leftWeight=None, rightWeight=None, colorDict=None,
           leftLabels=None, rightLabels=None, aspect=4, rightColor=False,
           fontsize=14, figureName=None, closePlot=False):
    '''
    Make Sankey Diagram showing flow from left-->right

    Inputs:
        left = NumPy array of object labels on the left of the diagram
        right = NumPy array of corresponding labels on the right of the diagram
            len(right) == len(left)
        leftWeight = NumPy array of weights for each strip starting from the
            left of the diagram, if not specified 1 is assigned
        rightWeight = NumPy array of weights for each strip starting from the
            right of the diagram, if not specified the corresponding leftWeight
            is assigned
        colorDict = Dictionary of colors to use for each label
            {'label':'color'}
        leftLabels = order of the left labels in the diagram
        rightLabels = order of the right labels in the diagram
        aspect = vertical extent of the diagram in units of horizontal extent
        rightColor = If true, each strip in the diagram will be be colored
                    according to its left label
    Ouput:
        None
    '''
    if leftWeight is None:
        leftWeight = []
    if rightWeight is None:
        rightWeight = []
    if leftLabels is None:
        leftLabels = []
    if rightLabels is None:
        rightLabels = []
    # Check weights
    if len(leftWeight) == 0:
        leftWeight = np.ones(len(left))

    if len(rightWeight) == 0:
        rightWeight = leftWeight

    # Create Dataframe
    if isinstance(left, pd.Series):
        left = left.reset_index(drop=True)
    if isinstance(right, pd.Series):
        right = right.reset_index(drop=True)
    dataFrame = pd.DataFrame({'left': left, 'right': right, 'leftWeight': leftWeight,
                              'rightWeight': rightWeight}, index=range(len(left)))

    if len(dataFrame[(dataFrame.left.isnull()) | (dataFrame.right.isnull())]):
        raise NullsInFrame('Sankey graph does not support null values.')

    # Identify all labels that appear 'left' or 'right'
    allLabels = pd.Series(np.r_[dataFrame.left.unique(), dataFrame.right.unique()]).unique()

    # Identify left labels
    if len(leftLabels) == 0:
        leftLabels = pd.Series(dataFrame.left.unique()).unique()
    else:
        check_data_matches_labels(leftLabels, dataFrame['left'], 'left')

    # Identify right labels
    if len(rightLabels) == 0:
        rightLabels = pd.Series(dataFrame.right.unique()).unique()
    else:
        check_data_matches_labels(rightLabels, dataFrame['right'], 'right')
    # If no colorDict given, make one
    if colorDict is None:
        colorDict = {}
        palette = "hls"
        colorPalette = sns.color_palette(palette, len(allLabels))
        for i, label in enumerate(allLabels):
            colorDict[label] = colorPalette[i]
    else:
        missing = [label for label in allLabels if label not in colorDict.keys()]
        if missing:
            msg = "The colorDict parameter is missing values for the following labels : "
            msg += '{}'.format(', '.join(missing))
            raise ValueError(msg)

    # Determine widths of individual strips
    ns_l = defaultdict()
    ns_r = defaultdict()
    for leftLabel in leftLabels:
        leftDict = {}
        rightDict = {}
        for rightLabel in rightLabels:
            leftDict[rightLabel] = dataFrame[(dataFrame.left == leftLabel) & (dataFrame.right == rightLabel)].leftWeight.sum()
            rightDict[rightLabel] = dataFrame[(dataFrame.left == leftLabel) & (dataFrame.right == rightLabel)].rightWeight.sum()
        ns_l[leftLabel] = leftDict
        ns_r[leftLabel] = rightDict

    # Determine positions of left label patches and total widths
    leftWidths = defaultdict()
    for i, leftLabel in enumerate(leftLabels):
        myD = {}
        myD['left'] = dataFrame[dataFrame.left == leftLabel].leftWeight.sum()
        if i == 0:
            myD['bottom'] = 0
            myD['top'] = myD['left']
        else:
            myD['bottom'] = leftWidths[leftLabels[i - 1]]['top'] + 0.02 * dataFrame.leftWeight.sum()
            myD['top'] = myD['bottom'] + myD['left']
            topEdge = myD['top']
        leftWidths[leftLabel] = myD

    # Determine positions of right label patches and total widths
    rightWidths = defaultdict()
    for i, rightLabel in enumerate(rightLabels):
        myD = {}
        myD['right'] = dataFrame[dataFrame.right == rightLabel].rightWeight.sum()
        if i == 0:
            myD['bottom'] = 0
            myD['top'] = myD['right']
        else:
            myD['bottom'] = rightWidths[rightLabels[i - 1]]['top'] + 0.02 * dataFrame.rightWeight.sum()
            myD['top'] = myD['bottom'] + myD['right']
            topEdge = myD['top']
        rightWidths[rightLabel] = myD

    # Total vertical extent of diagram
    xMax = topEdge / aspect

    # Draw vertical bars on left and right of each  label's section & print label
    for leftLabel in leftLabels:
        plt.fill_between(
            [-0.02 * xMax, 0],
            2 * [leftWidths[leftLabel]['bottom']],
            2 * [leftWidths[leftLabel]['bottom'] + leftWidths[leftLabel]['left']],
            color=colorDict[leftLabel],
            alpha=0.99
        )
        plt.text(
            -0.05 * xMax,
            leftWidths[leftLabel]['bottom'] + 0.5 * leftWidths[leftLabel]['left'],
            leftLabel,
            {'ha': 'right', 'va': 'center'},
            fontsize=fontsize
        )
    for rightLabel in rightLabels:
        plt.fill_between(
            [xMax, 1.02 * xMax], 2 * [rightWidths[rightLabel]['bottom']],
            2 * [rightWidths[rightLabel]['bottom'] + rightWidths[rightLabel]['right']],
            color=colorDict[rightLabel],
            alpha=0.99
        )
        plt.text(
            1.05 * xMax,
            rightWidths[rightLabel]['bottom'] + 0.5 * rightWidths[rightLabel]['right'],
            rightLabel,
            {'ha': 'left', 'va': 'center'},
            fontsize=fontsize
        )

    # Plot strips
    for leftLabel in leftLabels:
        for rightLabel in rightLabels:
            labelColor = leftLabel
            if rightColor:
                labelColor = rightLabel
            if len(dataFrame[(dataFrame.left == leftLabel) & (dataFrame.right == rightLabel)]) > 0:
                # Create array of y values for each strip, half at left value,
                # half at right, convolve
                ys_d = np.array(50 * [leftWidths[leftLabel]['bottom']] + 50 * [rightWidths[rightLabel]['bottom']])
                ys_d = np.convolve(ys_d, 0.05 * np.ones(20), mode='valid')
                ys_d = np.convolve(ys_d, 0.05 * np.ones(20), mode='valid')
                ys_u = np.array(50 * [leftWidths[leftLabel]['bottom'] + ns_l[leftLabel][rightLabel]] + 50 * [rightWidths[rightLabel]['bottom'] + ns_r[leftLabel][rightLabel]])
                ys_u = np.convolve(ys_u, 0.05 * np.ones(20), mode='valid')
                ys_u = np.convolve(ys_u, 0.05 * np.ones(20), mode='valid')

                # Update bottom edges at each label so next strip starts at the right place
                leftWidths[leftLabel]['bottom'] += ns_l[leftLabel][rightLabel]
                rightWidths[rightLabel]['bottom'] += ns_r[leftLabel][rightLabel]
                plt.fill_between(
                    np.linspace(0, xMax, len(ys_d)), ys_d, ys_u, alpha=0.65,
                    color=colorDict[labelColor]
                )
    plt.gca().axis('off')

max_stars_repo_name: gordian-biotechnology/CellOracle

max_issues_repo_name: gordian-biotechnology/CellOracle

max_forks_repo_name: gordian-biotechnology/CellOracle


text:
import cv2
import numpy as np
import win32gui, win32ui, win32con, win32api

class WindowStream:

    def __init__(self, region=None):

        super().__init__()
        if region:
            self.left, self.top, x2, y2 = region
            self.width = x2 - self.left + 1
            self.height = y2 - self.top + 1
        else:
            self.width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
            self.height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
            self.left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
            self.top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    def read(self):

        hwin = win32gui.GetDesktopWindow()
        hwindc = win32gui.GetWindowDC(hwin)
        srcdc = win32ui.CreateDCFromHandle(hwindc)
        memdc = srcdc.CreateCompatibleDC()
        bmp = win32ui.CreateBitmap()
        bmp.CreateCompatibleBitmap(srcdc, self.width, self.height)
        memdc.SelectObject(bmp)
        memdc.BitBlt((0, 0), (self.width, self.height), srcdc, (self.left, self.top), win32con.SRCCOPY)

        signedIntsArray = bmp.GetBitmapBits(True)
        img = np.fromstring(signedIntsArray, dtype='uint8')
        img.shape = (self.height, self.width, 4)

        srcdc.DeleteDC()
        memdc.DeleteDC()
        win32gui.ReleaseDC(hwin, hwindc)
        win32gui.DeleteObject(bmp.GetHandle())

        return cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)

    def get_props(self):
        return self.height, self.width, 4

if __name__ == "__main__":

    stream = WindowStream([0, 25, 800, 625])

    while True:

        image = stream.read()
        cv2.imshow("window_stream", image)
        key = cv2.waitKey(1)
        if key in [ord("q")]:
            break

max_stars_repo_name: Eye-C-U/bumblebee

max_issues_repo_name: Eye-C-U/bumblebee

max_forks_repo_name: Eye-C-U/bumblebee


text:
import streamlit as st
import pickle
import numpy as np
import pandas as pd

# import the model and the dataframe
pipe = pickle.load(open('pipe.pkl', 'rb'))
df = pickle.load(open('df.pkl', 'rb'))

st.title("Laptop Predictor")

# Brand
company = st.selectbox('Brand', df['Company'].unique())

# Type of Laptop
typename = st.selectbox('Type', df['TypeName'].unique())

# RAM
ram = st.selectbox('RAM(in GB)', [2,4,6,8,12,16,24,32,64])

# Weight
weight = st.number_input('Weight of the Laptop')

# Touchscreen
touchscreen = st.selectbox('Touchscreen', ['No', 'Yes'])

# IPS
ips = st.selectbox('IPS', ['No', 'Yes'])

# Screen Size
screen_size = st.number_input('Screen Size')

# Resolution
resolution = st.selectbox('Screen Resolution', ['1920x1080', '1366x768', '1600x900', '3840x2160', '3200x1800', '2880x1800', '2560x1600', '2560x1440', '2304x1440'])

# CPU
cpu = st.selectbox('CPU Brand', df['Cpu brand'].unique())

# HDD
hdd = st.selectbox('HDD(in GB)', [0, 128, 256, 512, 1024, 2048])

# SDD
ssd = st.selectbox('SSD(in GB)', [0, 8, 128, 256, 512, 1024, 2048])

# GPU
gpu = st.selectbox('GPU', df['Gpu brand'].unique())

# OS
os = st.selectbox('OS', df['os'].unique())

if st.button('Predict Price'):
    # query
    # ppi
    ppi = None

    # touchscreen
    if touchscreen == 'Yes':
        touchscreen = 1
    else:
        touchscreen = 0

    # ips
    if ips == 'Yes':
        ips = 1
    else:
        ips = 0

    # resolution
    X_res = int(resolution.split('x')[0])
    Y_res = int(resolution.split('x')[1])
    ppi = ((X_res**2) + (Y_res**2))**0.5/screen_size
    query = np.array([company, typename, ram, weight, touchscreen, ips,ppi, cpu, hdd, ssd, gpu, os])
    query = query.reshape(1,12)

    # Prediction
    st.title("Predicted Price of the Laptp: ₹" + str(int(np.exp(pipe.predict(query)[0]))))


text:
from typing import Any, Dict, Optional

import numpy as np

import torch as th
from .custom_layers import EqualizedConv2d
from .modules import (
    ConDisFinalBlock,
    DisFinalBlock,
    DisGeneralConvBlock,
    GenGeneralConvBlock,
    GenInitialBlock,
)
from torch import Tensor
from torch.nn import Conv2d, LeakyReLU, ModuleList, Sequential
from torch.nn.functional import avg_pool2d, interpolate

def nf(
    stage: int,
    fmap_base: int = 16 << 10,
    fmap_decay: float = 1.0,
    fmap_min: int = 1,
    fmap_max: int = 512,
) -> int:
    """
    computes the number of fmaps present in each stage
    Args:
        stage: stage level
        fmap_base: base number of fmaps
        fmap_decay: decay rate for the fmaps in the network
        fmap_min: minimum number of fmaps
        fmap_max: maximum number of fmaps

    Returns: number of fmaps that should be present there
    """
    return int(
        np.clip(
            int(fmap_base / (2.0 ** (stage * fmap_decay))), fmap_min, fmap_max,
        ).item()
    )

class Generator(th.nn.Module):
    """
    Generator Module (block) of the GAN network
    Args:
        depth: required depth of the Network
        num_channels: number of output channels (default = 3 for RGB)
        latent_size: size of the latent manifold
        use_eql: whether to use equalized learning rate
    """

    def __init__(
        self,
        depth: int = 10,
        num_channels: int = 3,
        latent_size: int = 512,
        use_eql: bool = True,
    ) -> None:
        super().__init__()

        # object state:
        self.depth = depth
        self.latent_size = latent_size
        self.num_channels = num_channels
        self.use_eql = use_eql

        ConvBlock = EqualizedConv2d if use_eql else Conv2d

        self.layers = ModuleList(
            [GenInitialBlock(latent_size, nf(1), use_eql=self.use_eql)]
        )
        for stage in range(1, depth - 1):
            self.layers.append(GenGeneralConvBlock(nf(stage), nf(stage + 1), use_eql))

        self.rgb_converters = ModuleList(
            [
                ConvBlock(nf(stage), num_channels, kernel_size=(1, 1))
                for stage in range(1, depth)
            ]
        )

    def forward(self, x: Tensor, depth: int, alpha: float) -> Tensor:
        """
        forward pass of the Generator
        Args:
            x: input latent noise
            depth: depth from where the network's output is required
            alpha: value of alpha for fade-in effect

        Returns: generated images at the give depth's resolution
        """

        assert depth <= self.depth, f"Requested output depth {depth} cannot be produced"

        if depth == 2:
            y = self.rgb_converters[0](self.layers[0](x))
        else:
            y = x
            for layer_block in self.layers[: depth - 2]:
                y = layer_block(y)
            residual = interpolate(self.rgb_converters[depth - 3](y), scale_factor=2)
            straight = self.rgb_converters[depth - 2](self.layers[depth - 2](y))
            y = (alpha * straight) + ((1 - alpha) * residual)
        return y

    def get_save_info(self) -> Dict[str, Any]:
        return {
            "conf": {
                "depth": self.depth,
                "num_channels": self.num_channels,
                "latent_size": self.latent_size,
                "use_eql": self.use_eql,
            },
            "state_dict": self.state_dict(),
        }

class Discriminator(th.nn.Module):
    """
    Discriminator of the GAN
    Args:
        depth: depth of the discriminator. log_2(resolution)
        num_channels: number of channels of the input images (Default = 3 for RGB)
        latent_size: latent size of the final layer
        use_eql: whether to use the equalized learning rate
        num_classes: number of classes for a conditional discriminator (Default = None)
                     meaning unconditional discriminator
    """

    def __init__(
        self,
        depth: int = 7,
        num_channels: int = 3,
        latent_size: int = 512,
        use_eql: bool = True,
        num_classes: Optional[int] = None,
    ) -> None:
        super().__init__()
        self.depth = depth
        self.num_channels = num_channels
        self.latent_size = latent_size
        self.use_eql = use_eql
        self.num_classes = num_classes
        self.conditional = num_classes is not None

        ConvBlock = EqualizedConv2d if use_eql else Conv2d

        if self.conditional:
            self.layers = [ConDisFinalBlock(nf(1), latent_size, num_classes, use_eql)]
        else:
            self.layers = [DisFinalBlock(nf(1), latent_size, use_eql)]

        for stage in range(1, depth - 1):
            self.layers.insert(
                0, DisGeneralConvBlock(nf(stage + 1), nf(stage), use_eql)
            )
        self.layers = ModuleList(self.layers)
        self.from_rgb = ModuleList(
            reversed(
                [
                    Sequential(
                        ConvBlock(num_channels, nf(stage), kernel_size=(1, 1)),
                        LeakyReLU(0.2)
                        )
                    for stage in range(1, depth)
                ]
            )
        )

    def forward(
        self, x: Tensor, depth: int, alpha: float, labels: Optional[Tensor] = None
    ) -> Tensor:
        """
        forward pass of the discriminator
        Args:
            x: input to the network
            depth: current depth of operation (Progressive GAN)
            alpha: current value of alpha for fade-in
            labels: labels for conditional discriminator (Default = None)
                    shape => (Batch_size,) shouldn't be a column vector

        Returns: raw discriminator scores
        """
        assert (
            depth <= self.depth
        ), f"Requested output depth {depth} cannot be evaluated"

        if self.conditional:
            assert labels is not None, "Conditional discriminator required labels"

        if depth > 2:
            residual = self.from_rgb[-(depth - 2)](
                avg_pool2d(x, kernel_size=2, stride=2)
            )
            straight = self.layers[-(depth - 1)](self.from_rgb[-(depth - 1)](x))
            y = (alpha * straight) + ((1 - alpha) * residual)
            for layer_block in self.layers[-(depth - 2) : -1]:
                y = layer_block(y)
        else:
            y = self.from_rgb[-1](x)
        if self.conditional:
            y = self.layers[-1](y, labels)
        else:
            y = self.layers[-1](y)
        return y

    def get_save_info(self) -> Dict[str, Any]:
        return {
            "conf": {
                "depth": self.depth,
                "num_channels": self.num_channels,
                "latent_size": self.latent_size,
                "use_eql": self.use_eql,
                "num_classes": self.num_classes,
            },
            "state_dict": self.state_dict(),
        }


text:
from flask import Flask, request
import numpy as np
import pandas as pd
import json
import pickle
import os

app = Flask(__name__)
# Load model and scaler files
model_path = os.path.join(os.path.pardir, 'models')
model_filepath = os.path.join(model_path, 'lr_model.pkl')
scaler_filepath = os.path.join(model_path, 'lr_scaler.pkl')

scaler = pickle.load(open(scaler_filepath))
model = pickle.load(open(model_filepath))

# columns
columns = ['Age', 'Fare', 'Family_size', 'Is_mother', 'Is_male',
       'Deck_A', 'Deck_B', 'Deck_C', 'Deck_D', 'Deck_E', 'Deck_F', 'Deck_G',
       'Deck_Z', 'Pclass_1', 'Pclass_2', 'Pclass_3', 'Title_Lady',
       'Title_Master', 'Title_Miss', 'Title_Mr', 'Title_Mrs', 'Title_Officer',
       'Title_Sir', 'Fare_Bin_Very_Low', 'Fare_Bin_Low', 'Fare_Bin_High',
       'Fare_Bin_Very_High', 'Embarked_C', 'Embarked_Q', 'Embarked_S',
       'Is_adult']

@pp.route('/api', methods=['POST'])
def make_predictions():
    # read json object and convert to json string
    data = json.dumps(requests.get_json(force=True))
    # create pandas dataframe using json string
    df = pd.read_json(data)
    # extract passengerids
    passenger_ids = df.PassengerId.ravel()
    # actual survivedd values
    actuals = df.Survived.ravel()
    # exctract required columns and convert to matrix
    X = df[columns].to_numpy().astype('float')
    # transform the input
    X_scaled = scaler.transform(X)
    # make predictions
    predictions = model.predict(X_scaled)
    # create response dataframe
    df_response = pd.DataFrame({'PassengerId': passenger_ids, 'Predicted': predictions, 'Actuals': actuals})
    # return json
    return df_response.to_json()
if __name__ == "__main__":
    # host flask app at port 10001
    app.run(port=10001, debug=True)

max_stars_repo_name: GoroCollins/titanic-disaster

max_issues_repo_name: GoroCollins/titanic-disaster

max_forks_repo_name: GoroCollins/titanic-disaster


text:
# -*- coding: utf-8 -*-
"""
SSVEP experiment module
"""
import time
from threading import Lock
from psychopy import visual
import numpy as np
from analysis import CCAAnalysis

SCORE_TH = .1

class CheckerBoard:
    """Flickering radial checkerboard stimulation"""

    def __init__(self, window, size, position, n_frame, log_time=False):
        """
        Args:
            window (psychopy.visual.window): Psychopy window
            size (int): Size of the checkerboard stimulation
            position (tuple): Position of stimulation on the screen
            n_frame (int): Number of frames for the stim to flicker (frequency = monitor_refresh_rate/n_frame)
            log_time (bool): Whether to log toggle times
        """
        self._window = window
        self._fr_rate = n_frame
        self._fr_counter = n_frame
        pattern = np.ones((4, 4))
        pattern[::2, ::2] *= -1
        pattern[1::2, 1::2] *= -1
        self._stim1 = visual.RadialStim(win=self._window, tex=pattern, pos=position,
                                        size=size, radialCycles=1, texRes=256, opacity=1)
        self._stim2 = visual.RadialStim(win=self._window, tex=pattern*-1, pos=position,
                                        size=size, radialCycles=1, texRes=256, opacity=1)
        self._toggle_flag = False
        self.log_time = log_time
        self.toggle_times = []

    def draw(self):
        """Draw stimulation"""
        if self._fr_counter == 0:
            if self.log_time:
                self.toggle_times.append(time.time())
            self._fr_counter = self._fr_rate
            if self._toggle_flag:
                self._stim1.draw()
            else:
                self._stim2.draw()
            self._toggle_flag = not self._toggle_flag
        self._fr_counter -= 1

    def get_statistics(self):
        """Get stimulation toggle statistics
        Returns:
            mean and standard deviation of the stimulation time length (in case log_time is False returns None)
        """
        assert self.log_time, "Time logging has not been activated for this checkerboard."
        if self.log_time:
            diff_t = np.diff(np.array(self.toggle_times))
            return diff_t.mean(), diff_t.std()

class SSVEPExperiment:
    """Steady State Visual Evoked Potential (SSVEP) Experiment

    This class implements a simple SSVEP experiment in which flickering targets will be shown on the screen. During
    experiment, EEG data is received continuously in the buffer of this class and data will be analysed by CCA
    algorithm to predict the target which the subject has focused on. EEG data is analysed based on sliding windows with
    the length of `signal_len` and with overlap times of `overlap`"""
    def __init__(self, frame_rates, positions, signal_len, eeg_s_rate, overlap=.25, screen_refresh_rate=60):
        """
        Args:
            frame_rates (list): List of number of frames in which each target is flickering (one number for each target)
            positions (list): List of target positions in the screen
            signal_len (float): EEG signal length (in seconds) to be analysed
            eeg_s_rate (int): Sampling rate of EEG signal
            overlap (float): Time overlap between two consecutive data chunk
            screen_refresh_rate (int): Refresh rate of your screen
        """
        self._fr_rates = frame_rates
        self._freqs = [screen_refresh_rate / fr_no for fr_no in self._fr_rates]
        print("Target frequencies: ", self._freqs)
        self.targets = []
        self._positions = positions
        self.win = None
        self._data_buff = np.array([])
        self.chunk_len = signal_len
        self.eeg_s_rate = eeg_s_rate
        self.overlap = overlap
        self.cca = CCAAnalysis(freqs=self._freqs, win_len=self.chunk_len, s_rate=self.eeg_s_rate, n_harmonics=2)
        self.lock = Lock()
        self._prediction_text = []
        self._predicted_ind = None

    def _init_vis(self):
        self._data_buff = np.array([])
        self.win = visual.Window([800, 600], monitor="testMonitor",
                                 fullscr=True, screen=1, units="deg", color=[0.1, 0.1, 0.1])
        self.win.recordFrameIntervals = True
        for fr_no, pos, freq in zip(self._fr_rates, self._positions, self._freqs):
            self.targets.append(CheckerBoard(window=self.win,
                                             size=10,
                                             n_frame=fr_no,
                                             position=pos,
                                             log_time=True))
            self._prediction_text.append(visual.TextStim(win=self.win, pos=[0, 0], text="%.1f Hz" % freq,
                                                         color=(-1, -1, -1), height=1.5,
                                                         colorSpace='rgb', bold=True))

    def run(self, duration):
        """Run the experiment
        Args:
            duration (float): Duration of the experiment
        """
        self._init_vis()

        start_time = time.time()
        while time.time() - start_time < duration:
            self.win.flip()
            for stim in self.targets:
                stim.draw()
            if self._predicted_ind is not None:
                self._prediction_text[self._predicted_ind].draw()
            self._analyze_data()
        self.win.close()

    def update_buffer(self, packet):
        """Update EEG buffer of the experiment

        Args:
            packet (explorepy.packet.EEG): EEG packet

        """
        timestamp, eeg = packet.get_data()
        if not len(self._data_buff):
            self._data_buff = eeg.T
        else:
            self._data_buff = np.concatenate((self._data_buff, eeg.T), axis=0)

    def _analyze_data(self):
        """Analyse data

        This function checks if there is enough data in the buffer and applies CCA on the data. If all scores are less
        than the threshold `SCORE_TH`, no target is chosen, otherwise the target with maximum score will be chosen as
        the prediction.
        """
        if len(self._data_buff) > 0:
            if self._data_buff.shape[0] > self.chunk_len * self.eeg_s_rate:
                with self.lock:
                    scores = self.cca.apply_cca(self._data_buff[:self.chunk_len * self.eeg_s_rate, :])
                    self._data_buff = self._data_buff[:int(self.overlap * self.eeg_s_rate), :]
                print(scores)
                if not all(val < SCORE_TH for val in scores):
                    self._predicted_ind = np.argmax(scores)
                    print(self._predicted_ind)
                else:
                    self._predicted_ind = None

    def show_statistics(self):
        """Show statistics of frame length and frequencies of the targets"""
        for stim in self.targets:
            avg, std = stim.get_statistics()
            print('frequency: {},  std: {}'.format(1 / avg, std))
        frame_intervals = self.win.frameIntervals
        import matplotlib.pyplot as plt
        plt.plot(frame_intervals)
        plt.xlabel("Frame number")
        plt.ylabel("Frame interval (s)")
        plt.show()

max_stars_repo_name: hacklschorsch/explorepy

max_issues_repo_name: hacklschorsch/explorepy

max_forks_repo_name: hacklschorsch/explorepy


text:
#!/usr/bin/env python3
import math
from scipy.spatial.transform import Rotation
import numpy as np
import cv2

class Sensors:
    def __init__(self, robot):
        self.lidar =            LidarSensor("Hokuyo_URG_04LX_UG01",True,robot,240,5.58)
        self.accelerometer =    MovementSensor("accelerometer",True,robot)
        self.bumper =           Sensor("base_cover_link",True,robot)
        self.sonar_01 =         Sensor("base_sonar_01_link",False,robot)
        self.sonar_02 =         Sensor("base_sonar_02_link",False,robot)
        self.sonar_03 =         Sensor("base_sonar_03_link",False,robot)
        self.battery =          Sensor("battery_sensor",False,robot)
        self.camera =           Sensor("camera",True,robot)
        self.gyro =             MovementSensor("gyro",True,robot)
        self.head_horizontal =  Sensor("head_1_joint_sensor",True,robot)
        self.head_vertical =    Sensor("head_2_joint_sensor",True,robot)
        self.inertial_unit =    Sensor("inertial_unit",False,robot)
        self.joystick =         Sensor("joystick",False,robot)
        self.keyboard =         Sensor("keyboard",False,robot)
        self.torso =            Sensor("torso_lift_joint_sensor",True,robot)
        self.left_wheel =       Sensor("wheel_left_joint_sensor",True,robot)
        self.right_wheel =      Sensor("wheel_right_joint_sensor",True,robot)

    def init(self,time_step):
        for key, sensor in vars(self).items():
            if sensor.active and key!='robot':
                sensor.init(time_step)
    def get_device_name(self, device):
        for key, sensor in vars(self).items():
            if sensor.name == device:
                return key

class Sensor:
    def __init__(self, name, active, robot):
        self.name = name
        self.active = active
        self.value = None
        self.__robot = robot

    def init(self,time_step):
        self.__robot.call_service(self.name,"enable",time_step)

    def inertial_unit_callback(self, values):
        q = values.orientation
        rot = Rotation.from_quat([q.x,q.y,q.z,q.w])
        rot_euler = rot.as_euler('xyz', degrees=True)
        self.value = 180*rot_euler[2]/math.pi

    def camera_callback(self, values):
        image = np.ndarray(shape=(values.height, values.width, 4),
                dtype=np.dtype('uint8'), buffer=values.data)
        size = image.shape
        if size[0] > 1 and size[1] > 1:
            self.value = cv2.cvtColor(image.copy(),cv2.COLOR_RGBA2RGB)
        else:
            self.value = None
        #cv2.imshow('frame',image)
        #cv2.waitKey(1)

    def motor_sensor_callback(self, values):
        return values.data

    def head_1_joint_sensor_callback(self, values):
        self.value = self.motor_sensor_callback(values)

    def head_2_joint_sensor_callback(self, values):
        self.value = self.motor_sensor_callback(values)

    def wheel_left_joint_sensor_callback(self, values):
        self.value = self.motor_sensor_callback(values)

    def wheel_right_joint_sensor_callback(self, values):
        self.value = self.motor_sensor_callback(values)

    def torso_lift_joint_sensor_callback(self, values):
        self.value = self.motor_sensor_callback(values)

    def Hokuyo_URG_04LX_UG01_callback(self, values):
        section=24
        point_increment = math.floor(len(values.ranges)/section)
        angle_increment = math.floor(self.fov/section)
        self.value = [(min(values.ranges[i*point_increment:(i+1)*point_increment]),i*angle_increment+math.floor(angle_increment/2-self.fov/2)) for i in range(section)]

    def base_cover_link_callback(self, values):
        self.value = values.data

class MovementSensor(Sensor):
    def __init__(self, name, active, robot):
        super().__init__(name, active, robot)
        self.value=Vector()

    def accelerometer_callback(self, values):
        self.value.x=values.linear_acceleration.x
        self.value.y=values.linear_acceleration.y
        self.value.z=values.linear_acceleration.z
        self.value.t=values.header.stamp

    def gyro_callback(self, values):
        self.value.x=values.angular_velocity.x
        self.value.y=values.angular_velocity.y
        self.value.z=values.angular_velocity.z
        self.value.t=values.header.stamp

class LidarSensor(Sensor):
    def __init__(self, name, active, robot, fov, range_max):
        super().__init__(name, active, robot)
        self.fov=fov
        self.range_max=range_max

class Vector:
    def __init__(self,x=0,y=0,z=0,t=0):
        self.x=x
        self.y=y
        self.z=z
        self.t=t

    def __str__(self):
        return "x:{} y:{} z:{} t:{}".format(self.x,self.y,self.z,self.t)

    def copy(self):
        return Vector(self.x,self.y,self.z,self.t)

max_stars_repo_name: marcolamartina/crowd-detection-system

max_issues_repo_name: marcolamartina/crowd-detection-system

max_forks_repo_name: marcolamartina/crowd-detection-system

reason: import numpy,from scipy