text:
import torch
import torchvision
import numpy as np
from numpy.random import randint
from typing import Tuple, Optional, Dict, List
from torch.utils.data import DataLoader, Dataset, Subset, ConcatDataset
from PIL import Image

class Benchmark:
    """
    Base class for continual learning benchmarks.
    It implements logic for loading/serving continual learning datasets.
    """
    def __init__(self,
                 num_tasks: int,
                 per_task_examples: Optional[int] = None,
                 per_task_joint_examples: Optional[int] = 0,
                 per_task_memory_examples: Optional[int] = 0,
                 per_task_subset_examples: Optional[int] = 0,
                 task_input_transforms: Optional[list] = None,
                 task_target_transforms: Optional[list] = None):
        """
        Args:
            num_tasks: The number of tasks for the benchmark.
            per_task_examples: If set, each task will include part of the original benchmark rather than full data.
            per_task_joint_examples: If set, the benchmark will support joint/multitask loading of tasks.
            per_task_memory_examples: If set, the benchmark will support episodic memory/replay buffer loading of tasks.
            per_task_subset_examples: If set, the benchmark will support loading a pre-defined subset of each task.
            task_input_transforms: If set, the benchmark will use the provided torchvision transform.
            task_target_transforms: If set, the benchmark will use the provided target transform for targets.

        . note::
            If :attr:`task_input_transforms` or :attr:`task_target_transforms`, they should be a list
            of size `num_tasks` where each element of the list can be a torchvision (Composed) transform.
        """

        # Task details
        self.num_tasks = num_tasks
        self.per_task_seq_examples = per_task_examples
        self.per_task_joint_examples = per_task_joint_examples
        self.per_task_memory_examples = per_task_memory_examples
        self.per_task_subset_examples = per_task_subset_examples

        # Optional transformations
        self.task_input_transforms = task_input_transforms
        self.task_target_transforms = task_target_transforms

        # Book-keeping variables: mostly used for storing indices of data points for each task
        self.trains, self.tests = {}, {}
        self.joint_indices_train, self.joint_indices_test = {}, {}
        self.memory_indices_train, self.memory_indices_test = {}, {}
        self.seq_indices_train, self.seq_indices_test = {}, {}
        self.subset_indices_train, self.subset_indices_test = {}, {}
        self.sanity_check_inputs()

    def sanity_check_inputs(self):
        """
        A method for sanity checking arguments.
        E.g., checking if all provided arguments are valid.
        """
        self.sanity_check_transforms()

    def sanity_check_transforms(self):
        if self.task_input_transforms is not None and len(self.task_input_transforms) != self.num_tasks:
            raise ValueError("task_input_transform is either None (using default transform),\
                             or should be a list of size `num_tasks` that provides transforms for each task")

    def prepare_datasets(self):
        """
        Prepares datasets: will be called after `load_datasets`.
        Responsible for computing index for various methods. E.g., selecting subset/memory indices for each task.
        """
        if self.per_task_joint_examples:
            self.precompute_joint_indices()
        if self.per_task_memory_examples:
            self.precompute_memory_indices()
        if self.per_task_seq_examples:
            self.precompute_seq_indices()
        if self.per_task_subset_examples:
            self.precompute_subset_indices()

    def load_datasets(self):
        """
        Loading datasets from file.
        """
        raise NotImplementedError

    def precompute_joint_indices(self):
        """
        For each task, (randomly) computes the indices of the subset of data points in the task's dataset.
        Then, once `load_joint()` method is called, uses these indices to return a PyTorch `Subset` dataset.
        .. note:: This method will be called only if the benchmark is initiated with `per_task_joint_examples`.
        """
        for task in range(1, self.num_tasks+1):
            self.joint_indices_train[task] = randint(0, len(self.trains[task]), size=self.per_task_joint_examples)
            self.joint_indices_test[task] = randint(0, len(self.tests[task]), size=self.per_task_joint_examples)

    def precompute_memory_indices(self):
        """
        For each task, (randomly) computes the indices of the subset of data points in the task's dataset.
        Then, once `load_memory()` method is called, uses these indices to return a PyTorch `Subset` dataset.
        .. note:: This method will be called only if the benchmark is initiated with `per_task_memory_examples`.
        """
        raise NotImplementedError

    def precompute_subset_indices(self):
        for task in range(1, self.num_tasks + 1):
            self.subset_indices_train[task] = randint(0, len(self.trains[task]), size=self.per_task_subset_examples)
            self.subset_indices_test[task] = randint(0, len(self.tests[task]), size=min(self.per_task_subset_examples, len(self.tests[task])))

    def precompute_seq_indices(self):
        if self.per_task_seq_examples > len(self.trains[1]):
            raise ValueError(f"per task examples = {self.per_task_seq_examples} but first task's examples = {len(self.trains[1])}")

        for task in range(1, self.num_tasks+1):
            self.seq_indices_train[task] = randint(0, len(self.trains[task]), size=self.per_task_seq_examples)
            self.seq_indices_test[task] = randint(0, len(self.tests[task]), size=min(self.per_task_seq_examples, len(self.tests[task])))

    def _calculate_num_examples_per_class(self, start_class, end_class, num_samples):
        num_classes = end_class - start_class + 1
        num_examples_per_class = num_samples//num_classes
        result = [num_examples_per_class]*num_classes

        # if memory_size can't be divided by num_class classes
        # e.g., memory_size is 32, but we have 5 classes.
        if num_classes * num_examples_per_class < num_samples:
            # how many more examples we need?
            diff = num_samples - (num_classes * num_examples_per_class)
            # add examples
            while diff:
                diff -= 1
                result[randint(0, num_classes)] += 1
        return result

    def sample_uniform_class_indices(self, dataset, start_class, end_class, num_samples) -> List:
        """
        Selects a subset of size `num_samples` from `start_class` to `end_class`.
        Args:
            dataset: The input dataset that the samples will be drawn from.
            start_class: The start_class (inclusive)
            end_class: The end_classes (inclusive)
            num_samples: Number of samples.

        Returns:
            Indices of the selected samples.

        .. note:: This method is specially useful for split datasets. E.g., selecting classes 0 to 4 for Split-CIFAR-100.

        .. warning:: If `num_samples > len(dataset)`, then the output`s shape will be equal to `len(dataset)`.
        """
        target_classes = dataset.targets.clone().detach().numpy()
        num_examples_per_class = self._calculate_num_examples_per_class(start_class, end_class, num_samples)
        class_indices = []
        # choose num_examples_per_class for each class
        for i, cls_number in enumerate(range(start_class, end_class+1)):
            target = (target_classes == cls_number)
            #  maybe that class doesn't exist
            num_candidate_examples = len(np.where(target == 1)[0])
            if num_candidate_examples:
                selected_indices = np.random.choice(np.where(target == 1)[0],
                                                    min(num_candidate_examples, num_examples_per_class[i]),
                                                    replace=False)
                class_indices += list(selected_indices)
        return class_indices

    def load(self,
             task: int,
             batch_size: int,
             shuffle: Optional[bool] = True,
             num_workers: Optional[int] = 0,
             pin_memory: Optional[bool] = True) -> Tuple[DataLoader, DataLoader]:

        """
        Makes train/val dataloaders for a specific task.
        Args:
            task: The task number.
            batch_size: The batch_size for dataloaders.
            shuffle: Should loaders be shuffled? Default: True.
            num_workers: corresponds to Pytorch's `num_workers` argument. Default: 0
            pin_memory: corresponds to Pytorch's `pin_memory` argument. Default: True.

        Returns:
            a Tuple of dataloaders, i.e., (train_loader, validation_loader).

        Examples::
            >>> benchmark = Benchmark(num_tasks=2)
            >>> # task 1 loaders
            >>> train_loader_1, val_loader_1 = benchmark.load(1, batch_size=32)
            >>> # task 2 loaders
            >>> train_loader_2, val_loader_2 = benchmark.load(2, batch_size=64)
        """

        if task > self.num_tasks:
            raise ValueError(f"Asked to load task {task} but the benchmark has {self.num_tasks} tasks")

        if self.per_task_seq_examples:
            trainset = Subset(self.trains[task], self.seq_indices_train[task])
            testset = Subset(self.tests[task], self.seq_indices_test[task])
        else:
            trainset = self.trains[task]
            testset = self.tests[task]
        train_loader = DataLoader(trainset, batch_size, shuffle, num_workers=num_workers,
                                  pin_memory=pin_memory)
        test_loader = DataLoader(testset, 256, True, num_workers=num_workers, pin_memory=pin_memory)

        return train_loader, test_loader

    def load_joint(self,
                   task: int,
                   batch_size: int,
                   shuffle: Optional[bool] = True,
                   num_workers: Optional[int] = 0,
                   pin_memory: Optional[bool] = True) -> Tuple[DataLoader, DataLoader]:
        """
        Makes dataloaders for joint/multitask settings.
        i.e., for task `t` returns datasets for tasks `1, 2, ..., t-1, t`.

        Args:
            task: The task number.
            batch_size: The batch_size for dataloaders.
            shuffle: Should loaders be shuffled? Default: True.
            num_workers: corresponds to Pytorch's `num_workers` argument. Default: 0
            pin_memory: corresponds to Pytorch's `pin_memory` argument. Default: True.

        Returns:
            a Tuple of dataloaders, i.e., (train_loader, validation_loader).

        Examples::
            >>> benchmark = Benchmark(num_tasks=2, per_task_joint_examples=128)
            >>> # task 1 loaders (single): returns 4 batches (i.e., 128 examples)
            >>> train_loader_1, val_loader_1 = benchmark.load(1, batch_size=32)
            >>> # task 1 loaders (joint): returns 4 batches (i.e., 128 examples)
            >>> joint_train_loader_1, joint_val_loader_1 = benchmark.load_joint(1, batch_size=32)
            >>> # task 1 loaders (single): returns 4 batches (i.e., 128 examples)
            >>> train_loader_2, val_loader_2 = benchmark.load(2, batch_size=32)
            >>> # task 1 loaders (single): returns 8 batches (i.e., 256 examples)
            >>> joint_train_loader_2, joint_val_loader_2 = benchmark.load(2, batch_size=32)

        .. warning::
            The method will throw an error if `Benchmark` is instantiated without `per_task_joint_examples`.
            The reason is that, behind the scenese, we compute the indices for joint examples in
            `precompute_joint_indices()` method and this method relies on that computations.
        """
        if not self.per_task_joint_examples:
            raise ValueError("Called load_joint() but per_task_joint_examples is not set")

        if task > self.num_tasks:
            raise ValueError(f"Asked to load task {task} but the benchmark has {self.num_tasks} tasks")

        trains, tests = [], []
        for prev_task in range(1, task + 1):
            prev_train = Subset(self.trains[prev_task], self.joint_indices_train[prev_task])
            prev_test = Subset(self.tests[prev_task], self.joint_indices_test[prev_task])
            trains.append(prev_train)
            tests.append(prev_test)

        trains, tests = ConcatDataset(trains), ConcatDataset(tests)
        train_loader = DataLoader(trains, batch_size, shuffle, num_workers=num_workers, pin_memory=pin_memory)
        test_loader = DataLoader(tests, batch_size, shuffle, num_workers=num_workers, pin_memory=pin_memory)
        return train_loader, test_loader

    def load_subset(self,
                    task: int,
                    batch_size: int,
                    shuffle: Optional[bool] = True,
                    num_workers: Optional[int] = 0,
                    pin_memory: Optional[bool] = True) -> Tuple[DataLoader, DataLoader]:
        if not self.per_task_subset_examples:
            raise ValueError("Called load_subset() without setting per_task_subset_examples")
        subset_train = Subset(self.trains[task], self.subset_indices_train[task])
        subset_test = Subset(self.tests[task], self.subset_indices_test[task])
        train_loader = DataLoader(subset_train, batch_size, shuffle, num_workers=num_workers, pin_memory=pin_memory)
        test_loader = DataLoader(subset_test, batch_size, shuffle, num_workers=num_workers, pin_memory=pin_memory)
        return train_loader, test_loader

    def load_memory(self,
                    task: int,
                    batch_size: int,
                    shuffle: Optional[bool] = True,
                    num_workers: Optional[int] = 0,
                    pin_memory: Optional[bool] = True) -> Tuple[DataLoader, DataLoader]:
        """
        Makes dataloaders for episodic memory/replay buffer.

        Args:
            task: The task number.
            batch_size: The batch_size for dataloaders.
            shuffle: Should loaders be shuffled? Default: True.
            num_workers: corresponds to Pytorch's `num_workers` argument. Default: 0
            pin_memory: corresponds to Pytorch's `pin_memory` argument. Default: True.

        Returns:
            a Tuple of dataloaders, i.e., (train_loader, validation_loader).

        Examples::
            >>> benchmark = Benchmark(num_tasks=2, per_task_memory_examples=16)
            >>> # task 1 memory loaders: returns 2 batches (i.e., 16 examples)
            >>> mem_train_loader_1, mem_val_loader_1 = benchmark.load_memory(1, batch_size=8)
            >>> # task 2 memory loaders: returns 4 batches (i.e., 16 examples)
            >>> mem_train_loader_2, mem_val_loader_2 = benchmark.load_memory(2, batch_size=4)

        .. note::
            This method uses `class_uniform` sampling.  i.e., if each task has 10 classes,
            and `per_task_memory_examples=20`, then the returend samples have 2 examples per class.

        .. warning::
            The method will throw an error if `Benchmark` is instantiated without :attr:`per_task_memory_examples`.
            The reason is that, behind the scenese, we compute the indices for memory examples in
            `precompute_memory_indices()` method and this method relies on that computations.
        """
        if not self.per_task_memory_examples:
            raise ValueError("Called load_memory() but per_task_memory_examples is not set")

        if task > self.num_tasks:
            raise ValueError(f"Asked for memory of task={task} while the benchmark has {self.num_tasks} tasks")

        train_indices = self.memory_indices_train[task]
        test_indices = self.memory_indices_test[task]
        train_dataset = Subset(self.trains[task], train_indices)
        test_dataset = Subset(self.tests[task], test_indices)

        train_loader = DataLoader(train_dataset, batch_size, shuffle, num_workers=num_workers, pin_memory=pin_memory)
        test_loader = DataLoader(test_dataset, batch_size, shuffle, num_workers=num_workers, pin_memory=pin_memory)

        return train_loader, test_loader

    def load_memory_joint(self,
                          task: int,
                          batch_size: int,
                          shuffle: Optional[bool] = True,
                          num_workers: Optional[int] = 0,
                          pin_memory: Optional[bool] = True) -> Tuple[DataLoader, DataLoader]:
        if task > self.num_tasks:
            raise ValueError(f"Asked to load memory of task={task} but the benchmark has {self.num_tasks} tasks")
        trains, tests = [], []
        for t in range(1, task + 1):
            train_indices = self.memory_indices_train[t]
            test_indices = self.memory_indices_test[t]
            train_dataset = Subset(self.trains[t], train_indices)
            test_dataset = Subset(self.tests[t], test_indices)
            trains.append(train_dataset)
            tests.append(test_dataset)

        trains, tests = ConcatDataset(trains), ConcatDataset(tests)
        train_loader = DataLoader(trains, batch_size, shuffle, num_workers=num_workers, pin_memory=pin_memory)
        test_loader = DataLoader(tests, batch_size, shuffle, num_workers=num_workers, pin_memory=pin_memory)
        return train_loader, test_loader

class DynamicTransformDataset(Dataset):
    """
    A lightweight wrapper around PyTorch dataset.
    Simply, applies custom transforms on the dataset, rather than loading datasets again.
    """
    def __init__(self, task_id: int, dataset: Dataset, input_transform=None, target_transform=None):
        self.task_id = task_id
        self.dataset = dataset
        self.input_transform = input_transform
        self.target_transform = target_transform

    def __getitem__(self, index):
        img, target = self.dataset.data[index], int(self.dataset.targets[index])
        img = Image.fromarray(img.numpy(), mode='L')

        # transforms
        if self.input_transform is not None:
            img = self.input_transform(img)
        if self.target_transform is not None:
            target = self.target_transform(target)

        return img, target, self.task_id

    def __len__(self) -> int:
        return len(self.dataset.data)

class SplitDataset(Dataset):
    """
    A lightweight wrapper around PyTorch dataset for split benchmarks.
    """
    def __init__(self, task_id, classes_per_split, dataset):
        self.inputs = []
        self.targets = []
        self.task_id = task_id
        self.classes_per_split = classes_per_split
        self.__build_split(dataset, task_id)

    def __build_split(self, dataset, task_id):
        start_class = (task_id-1) * self.classes_per_split
        end_class = task_id * self.classes_per_split
        # For CIFAR-like datasets in torchvision where targets are list
        if isinstance(dataset.targets, list):
            target_classes = np.asarray(dataset.targets)
        # for MNIST-like datasets where targets are tensors
        else:
            target_classes = dataset.targets.clone().detach().numpy()
        # target_classes = dataset.targets.clone().detach().numpy()
        selected_indices = np.where(np.logical_and(start_class <= target_classes, target_classes < end_class))[0]
        for idx in selected_indices:
            img, target = dataset[idx]
            target = torch.tensor(target)
            self.inputs.append(img)
            self.targets.append(target)

        self.inputs = torch.stack(self.inputs)
        self.targets = torch.stack(self.targets)

    def __getitem__(self, index: int):
        img, target = self.inputs[index], int(self.targets[index])
        return img, target, self.task_id

    def __len__(self) -> int:
        return len(self.inputs)

max_stars_repo_name: imirzadeh/CL-Gym

max_issues_repo_name: imirzadeh/CL-Gym

max_forks_repo_name: imirzadeh/CL-Gym

reason: import numpy,from numpy


text:
import julia
from julia import DPMMSubClusters
import numpy as np

class prior:
    def to_julia_prior(self):
        pass

class niw(prior):
    def __init__(self, kappa, mu, nu, psi):
        if nu < len(mu):
            raise Exception('nu should be atleast the Dim')
        self.kappa = kappa
        self.mu = mu
        self.nu = nu
        self.psi = psi

    def to_julia_prior(self):
        return DPMMSubClusters.niw_hyperparams(self.kappa,self.mu,self.nu, self.psi)

class multinomial(prior):
    def __init__(self, alpha,dim = 1):
        if isinstance(alpha,np.ndarray):
            self.alpha = alpha
        else:
            self.alpha = np.ones(dim)*alpha

    def to_julia_prior(self):
        return DPMMSubClusters.multinomial_hyper(self.alpha)

ext: py

reason: import numpy


text:
from datetime import datetime, timedelta
import os
from termcolor import colored
import wandb
import torch
import numpy as np
import json

from copy import deepcopy

from entities.metric import Metric
from entities.data_output_log import DataOutputLog
from services.arguments.arguments_service_base import ArgumentsServiceBase
import logging
from logging import Logger

from utils.dict_utils import stringify_dictionary

class LogService:
    def __init__(
            self,
            arguments_service: ArgumentsServiceBase,
            logger: Logger):

        self._arguments_service = arguments_service

        self._log_header = '  Time Epoch Iteration   Progress  (%Epoch) | Train Loss Train Accuracy | Validation Loss Val. Accuracy | Best'
        self._log_template = ' '.join(
            '{:>6.0f},{:>5.0f},{:>9.0f},{:>5.0f}/{:<5.0f} {:>7.0f}%,| {:>10.6f} {:>14.10f} | {:>15.11f} {:>13.9f} | {:>4s}'.split(','))

        self._start_time = datetime.now()

        self._progress_color = 'red'
        self._evaluation_color = 'cyan'

        self._current_epoch = 0
        self._all_iterations = 0
        self._current_iteration = 0

        config_name = self._arguments_service.get_configuration_name()
        self._run_name = f'{config_name}-{self._start_time.strftime("%Y_%m_%d_%H_%M_%S")}'

        self._logger: Logger = self._initialize_logger(logger)
        self.log_debug(f'Starting run \'{self._run_name}\'')

        self._external_logging_enabled = self._arguments_service.enable_external_logging
        if self._external_logging_enabled:
            wandb_project = str(self._arguments_service.challenge)
            wandb_entity = 'eval-historical-texts'
            wandb.init(
                project=wandb_project,
                config=self._arguments_service._arguments,
                entity=wandb_entity,
                force=True,
                name=config_name
                # resume=arguments_service.resume_training,
                # id='' #TODO
            )

            self.log_debug(
                f'W&B initialized [project: \'{wandb_project}\' entity: \'{wandb_entity}\' | name: \'{config_name}\'')

    def log_progress(
            self,
            current_step: int,
            all_steps: int,
            epoch_num: int = None,
            evaluation: bool = False):
        """Logs current progress of the training process

        :param current_step: Current step of the training process
        :type current_step: int
        :param all_steps: Amount of total steps in one epoch
        :type all_steps: int
        :param epoch_num: Current epoch number, defaults to None
        :type epoch_num: int, optional
        :param evaluation: Whether current progress is part of evaluation or training, defaults to False
        :type evaluation: bool, optional
        """

        prefix = 'Train'
        if evaluation:
            prefix = 'Evaluating'
        else:
            self.log_summary('Iteration', current_step)

        epoch_str = 'N/A'
        if epoch_num is not None:
            epoch_str = str(epoch_num)

        print(colored(
            f'{prefix}: {current_step}/{all_steps}       | Epoch: {epoch_str}           \r', self._progress_color), end='')

    def initialize_evaluation(self):
        self.log_debug('Starting training...')

        print(self._log_header)

    def log_evaluation(
            self,
            train_metric: Metric,
            validation_metric: Metric,
            epoch: int,
            iteration: int,
            iterations: int,
            new_best: bool,
            metric_log_key: str = None):
        """Logs current evaluation results

        :param train_metric: Metric of the training iterations
        :type train_metric: Metric
        :param validation_metric: Metric of the validation iterations
        :type validation_metric: Metric
        :param epoch: Current epoch number
        :type epoch: int
        :param iteration: Current iteration number
        :type iteration: int
        :param iterations: Total amount of iterations to perform
        :type iterations: int
        :param new_best: Whether the current result is a new best result
        :type new_best: bool
        :param metric_log_key: Whether a specific metric key should be used to display results. If None is provided, then accuracy is used, defaults to None
        :type metric_log_key: str, optional
        """
        self._current_epoch = epoch
        self._current_iteration = iteration
        self._all_iterations = iterations

        time_passed = self.get_time_passed()
        train_loss = train_metric.get_current_loss()
        train_accuracies = train_metric.get_current_accuracies()
        validation_loss = validation_metric.get_current_loss()
        validation_accuracies = validation_metric.get_current_accuracies()
        if train_accuracies and len(train_accuracies) > 0:
            if metric_log_key is not None and train_metric.contains_accuracy_metric(metric_log_key):
                train_accuracy = train_metric.get_accuracy_metric(
                    metric_log_key)
            else:
                train_accuracy = list(train_accuracies.values())[0]
        else:
            train_accuracy = 0

        if validation_accuracies and len(validation_accuracies) > 0:
            if metric_log_key is not None and validation_metric.contains_accuracy_metric(metric_log_key):
                validation_accuracy = validation_metric.get_accuracy_metric(
                    metric_log_key)
            else:
                validation_accuracy = list(validation_accuracies.values())[0]
        else:
            validation_accuracy = 0

        print(colored(
            self._log_template.format(
                time_passed.total_seconds(),
                epoch,
                iteration,
                1 + iteration,
                iterations,
                100. * (1 + iteration) / iterations,
                train_loss,
                train_accuracy,
                validation_loss,
                validation_accuracy,
                "BEST" if new_best else ""), self._evaluation_color))

        if self._external_logging_enabled:
            current_step = self._get_current_step()
            wandb.log({'Train loss': train_loss},
                      step=current_step)

            for key, value in train_accuracies.items():
                wandb.log({f'Train - {key}': value},
                          step=current_step)

            for key, value in validation_accuracies.items():
                wandb.log({f'Validation - {key}': value},
                          step=current_step)

            wandb.log({'Validation loss': validation_loss},
                      step=current_step)

            if current_step == 0:
                seconds_per_iteration = time_passed.total_seconds()
            else:
                seconds_per_iteration = time_passed.total_seconds() / current_step

            self.log_summary('Seconds per iteration', seconds_per_iteration)

    def log_info(self, message: str):
        self._logger.info(message)

    def log_debug(self, message: str):
        self._logger.debug(message)

    def log_error(self, message: str):
        self._logger.error(message)

    def log_exception(self, message: str, exception: Exception):
        log_message = f'Exception occurred. Message: {message}\nOriginal exception: {exception}'

        self._logger.exception(log_message)

    def log_warning(self, message: str):
        self._logger.warning(message)

    def log_summary(self, key: str, value: object):
        if not self._external_logging_enabled:
            return

        wandb.run.summary[key] = value

    def log_batch_results(self, data_output_log: DataOutputLog):
        """Logs batch results, using a data output log

        :param data_output_log: The data output log which contains the data to be logged
        :type data_output_log: DataOutputLog
        """
        if not self._external_logging_enabled or data_output_log is None:
            return

        columns, data = data_output_log.get_log_data()
        table_log = wandb.Table(columns=columns, data=data)

        wandb.log({
            'batch results': table_log
        }, step=self._get_current_step())

    def log_incremental_metric(self, metric_key: str, metric_value: object):
        if not self._external_logging_enabled:
            return

        wandb.log({
            metric_key: metric_value
        }, step=self._get_current_step())

    def log_heatmap(
            self,
            heatmap_title: str,
            matrix_values: np.array,
            x_labels: list,
            y_labels: list,
            show_text_inside: bool = False):
        if not self._external_logging_enabled:
            return

        wandb.log({
            heatmap_title: wandb.plots.HeatMap(
                x_labels,
                y_labels,
                matrix_values,
                show_text=show_text_inside)
        }, step=self._get_current_step())

    def start_logging_model(self, model: torch.nn.Module, criterion: torch.nn.Module = None):
        if not self._external_logging_enabled:
            return

        wandb.watch(model, criterion=criterion)

    def get_time_passed(self) -> timedelta:
        result = datetime.now() - self._start_time
        return result

    def _get_current_step(self) -> int:
        return (self._current_epoch * self._all_iterations) + self._current_iteration

    def _initialize_logger(self, logger: Logger) -> Logger:
        if len(logger.handlers) > 0:
            return logger

        log_folder = self._arguments_service.log_folder
        if not os.path.exists(log_folder):
            os.mkdir(log_folder)

        log_file_name = f'{self._run_name}.log'
        log_file_path = os.path.join(
            self._arguments_service.log_folder, log_file_name)

        # create file handler which logs even debug messages
        file_handler = logging.FileHandler(log_file_path, encoding='utf8')
        file_handler.setLevel(logging.DEBUG)

        # create console handler with a higher log level
        console_handler = logging.StreamHandler()
        if self._arguments_service.verbose_logging:
            console_handler.setLevel(logging.DEBUG)
        else:
            console_handler.setLevel(logging.INFO)

        # create formatter and add it to the handlers
        file_formatter = logging.Formatter(
            '%(asctime)s | %(name)s | %(levelname)10s | %(message)s')
        file_handler.setFormatter(file_formatter)

        console_formatter = logging.Formatter(
            '%(asctime)s | %(levelname)10s | %(message)s')
        console_handler.setFormatter(console_formatter)

        # add the handlers to the logger
        logger.addHandler(file_handler)
        logger.addHandler(console_handler)

        return logger

    def log_arguments(self):
        args = self._arguments_service.get_arguments_dict()
        args_string = stringify_dictionary(args)
        self.log_debug(f'arguments initialized: {args_string}')

    def __deepcopy__(self, memo):
        # we do not want log service to be re-initialized, so we just return the current object
        return self

max_stars_repo_name: ktodorov/historical-ocr

max_issues_repo_name: ktodorov/historical-ocr


text:
import re
import math
from evoplotter.dims import *
from evoplotter.templates import *
from evoplotter import printer
import numpy as np

CHECK_CORRECTNESS_OF_FILES = 1

def print_props_filenames(props):
    for p in props:
        if "evoplotter.file" in p:
            print(p["evoplotter.file"])
        elif "thisFileName" in p:
            print(p["thisFileName"])
        else:
            print("'thisFileName' not specified! Printing content instead: " + str(p))

def create_errors_listing(error_props, filename, dir_path):
    f = open("{}listings/{}".format(dir_path, filename), "w")
    print("Creating log of errors ({0})...".format(filename))
    for i, p in enumerate(error_props):
        if i > 0:
            f.write("\n" + ("-" * 50) + "\n")
        for k in sorted(p.keys()):
            v = p[k]
            f.write("{0} = {1}\n".format(k, v))
    f.close()

def create_errors_solver_listing(error_props, filename, dir_path, pred=None):
    if pred is None:
        pred = lambda x: True
    f = open("{}listings/{}".format(dir_path, filename), "w")
    print("Creating log of errors ({0})...".format(filename))
    for i, p in enumerate(error_props):
        if not pred(p):  # ignore properties with certain features, e.g., types of errors
            continue

        if i > 0:
            f.write("\n" + ("-" * 50) + "\n\n")

        # read the whole original file, because multiline error messages are not preserved in dicts
        with open(p["evoplotter.file"], 'r') as content_file:
            content = content_file.read()
            f.write(content)
    f.close()

def load_correct_props(folders, dir_path, exts=None):
    # if exts is None:
    #     exts = [".cdgp"]
    props_cdgpError = utils.load_properties_dirs(folders, exts=[".cdgp.error"], add_file_path=True)
    props0 = utils.load_properties_dirs(folders, exts=exts, ignoreExts=[".txt", ".error"], add_file_path=True)

    # Delete wrong files
    # utils.deleteFilesByPredicate(props0, lambda p: len(p["maxGenerations"]) > 7, simulate=False)
    # utils.deleteFilesByPredicate(props_cdgpError, lambda p: len(p["maxGenerations"]) > 7, simulate=False)

    def is_correct(p):
        return "method" in p and (p["method"] not in {"CDGP", "CDGPprops", "GP"} or
                                  ("result.best.verificationDecision" in p and "result.validation.best.testMSE" in p))

    for p in props0:
        if "method" not in p:
            print("No method for: {0}".format(p["evoplotter.file"]))

    # Filtering props so only correct ones are left
    props = [p for p in props0 if "method" in p and is_correct(p)]

    # print("Filtered (props):")
    # for p in props:
    #     if "resistance_par3_c1_10" in p["benchmark"] and p["method"] == "CDGP":
    #         print(p["evoplotter.file"])
    # print("Filtered (props_cdgpError):")
    # for p in props_cdgpError:
    #     if "resistance_par3_c1_10" in p["benchmark"] and p["method"] == "CDGP":
    #         print(p["evoplotter.file"])

    # Clear log file
    # print("[del] props")
    # fun = lambda p: p["method"] == "CDGP" and p["partialConstraintsInFitness"] == "true"
    # delete_logs(props, fun, simulate=True)
    # print("[del] props_cdgpError")
    # delete_logs(props_cdgpError, fun, simulate=True)

    create_errors_solver_listing(props_cdgpError, filename="errors_solver.txt", dir_path=dir_path)
    # Terminating program due to time limit may trigger solver manager in CDGP, which shows as solver error.
    # This type of error can be recognized by the "No line found" message, so we create here a file without those false positives.
    def predSolverIssue(p):
        return "terminatingException" not in p or \
               ("No line found" not in p["terminatingException"] and "model is not available" not in p["terminatingException"])
    create_errors_solver_listing(props_cdgpError, filename="errors_solver_issues.txt", dir_path=dir_path, pred=predSolverIssue)

    # Printing names of files which finished with error status or are incomplete.
    if CHECK_CORRECTNESS_OF_FILES:
        props_errors = [p for p in props0 if not is_correct(p)]
        create_errors_listing(props_errors, filename="errors_run.txt", dir_path=dir_path)
        if len(props_errors) > 0:
            print("Files with error status:")
            print_props_filenames(props_errors)
        print("Loaded: {0} correct property files, {1} incorrect; All log files: {2}".format(len(props), len(props_errors), len(props) + len
                                                                                (props_errors)))
    print("Runs that ended with '.cdgp.error': {0}".format(len(props_cdgpError)))
    print_props_filenames(props_cdgpError)
    return props

def produce_status_matrix(dim, props):
    """Generates a status data in the form of a python list. It can be
    later used to retry missing runs.

    :param dim: (Dimension) dimensions on which data are to be divided.
    :param props: (dict[str,str]) properties files.
    :return: (str) Python code of a list containing specified data.
    """
    text = "["
    for config in dim:
        numRuns = len(config.filter_props(props))
        text += "({0}, {1}), ".format(config.stored_values, numRuns)
    return text + "]"

def save_listings(props, dim_rows, dim_cols, dir_path):
    """Saves listings of various useful info to separate text files."""
    assert isinstance(dim_rows, Dim)
    assert isinstance(dim_cols, Dim)
    utils.ensure_dir("{}listings/errors/".format(dir_path))

    # Saving optimal verified solutions
    for dr in dim_rows:
        bench = dr.get_caption()
        bench = bench[:bench.rfind(".")] if "." in bench else bench
        f = open("{}listings/verified_{}.txt".format(dir_path, bench), "w")
        f_errors = open("{}listings/errors/verified_{}.txt".format(dir_path, bench), "w")

        props_bench = dr.filter_props(props)
        for dc in dim_cols:
            f.write("{0}\n".format(dc.get_caption()))
            f_errors.write("{0}\n".format(dc.get_caption())) # TODO: finish
            props_final = [p for p in dc.filter_props(props_bench) if is_optimal_solution(p)]

            for p in props_final:
                fname = p["thisFileName"].replace("/home/ibladek/workspace/GECCO19/gecco19/", "")
                best = p["result.best"]
                fit = float(p["result.best.trainMSE"])
                if fit >= 1e-15:
                    f.write("{0}\t\t\t(FILE: {1}) (MSE: {2})\n".format(best, fname, fit))
                else:
                    f.write("{0}\t\t\t(FILE: {1})\n".format(best, fname))

            f.write("\n\n")
        f.close()
        f_errors.close()

def normalized_total_time(p, max_time=3600000):
    """If time was longer than max_time, then return max_time, otherwise return time. Time is counted in miliseconds."""
    if "cdgp.wasTimeout" in p and p["cdgp.wasTimeout"] == "true":
        v = 3600000
    else:
        v = int(float(p["result.totalTimeSystem"]))
    return max_time if v > max_time else v

def isOptimalVerification(p):
    if "result.best.correctVerification" not in p:
        return False
    else:
        return p["result.best.correctVerification"] == "true"
def isOptimalTests(p):
    # p["result.best.correctTests"] == "true" is computed wrongly for CDGPprops
    value = float(p["result.best.trainMSE"])
    return value < float(p["cdgp.optThresholdMSE"])
def is_optimal_solution(p):
    if p["method"] in {"CDGP", "CDGPprops", "GP"}:
        return isOptimalVerification(p) and isOptimalTests(p)
    else:
        return False

def get_num_optimal(props):
    props2 = [p for p in props if is_optimal_solution(p)]
    return len(props2)
def get_num_optimalOnlyMse(props):
    # "cdgp.optThreshold" in p and
    for p in props:
        if "optThreshold" not in p:
            print(str(p))
    # Sometimes it is 'optThreshold', and sometimes 'cdgp.optThreshold'...
    # props2 = [p for p in props if float(p["result.best.trainMSE"]) <= float(p["optThreshold"])]
    num = 0
    for p in props:
        if "optThreshold" in p:
            tr = p["optThreshold"]
        elif "optThreshold" in p:
            tr = p["cdgp.optThreshold"]
        else:
            raise Exception("No optThreshold in log file")
        if float(p["result.best.trainMSE"]) <= tr:
            num += 1
    return num

def scNotClearTrailingZeros(a):
    tab = a.split('E')
    if "-" in tab[1]:
        r = a.split('E-')[1]
        # add rstrip('0').rstrip('.') to remove trailing zeros from mantissa
        return tab[0] + 'E-' + r[:-1].lstrip('0') + r[-1]
    else:
        r = a.split('E')[1]
        return tab[0] + 'E' + r[:-1].lstrip('0') + r[-1]
def scientificNotationLatex(x):
    s = "%0.1E" % x
    if "E" not in s:
        s += "E0"
    s = "$" + s.replace("E+", "E")
    s = scNotClearTrailingZeros(s)
    s = s.replace("E", "\cdot 10^{") + "}$"
    return s

def p_allPropertiesMet_smt(p):
    if "result.best.correctVerification" in p:
        if p["result.best.correctVerification"] == "true":
            return True
        else:
            return False
    else:
        return None

def p_allPropertiesMet_verificator(p):
    if "result.best.verificator.decisions" in p:
        properties = p["result.best.verificator.decisions"].split(",")
        if all(pr == "1" for pr in properties):
            return True
        else:
            return False
    else:
        return None

def get_num_allPropertiesMet_verificator(props):
    sumCorrect = 0
    for p in props:
        if p_allPropertiesMet_verificator(p):
            sumCorrect += 1
    return sumCorrect

def get_num_allPropertiesMet(props):
    sumCorrect = 0
    for p in props:
        if p["method"] in {"CDGP", "GP", "CDGPprops", "CDSR", "CDSRprops"}:
            # props2 = [p for p in props if "result.best.correctVerification" in p and p["result.best.correctVerification"] == "true"]
            if "result.best.correctVerification" in p and p["result.best.correctVerification"] == "true":
                sumCorrect += 1
        # elif "result.best.verificator.decisions" in p:  # scikit regressors hopefully
        #     props = p["result.best.verificator.decisions"].split(",")
        #     if all(pr == "1" for pr in props):
        #         sumCorrect += 1
    return sumCorrect
def get_num_trainMseBelowThresh(props):
    # "result.best.correctTests" cannot be trusted, results were wrong
    # props2 = [p for p in props if p["result.best.correctTests"] == "true"]
    numSucc = 0
    for p in props:
        value = float(p["result.best.trainMSE"])
        if value < float(p["cdgp.optThresholdMSE"]):
            numSucc += 1
    return numSucc

def get_num_computed(filtered):
    return len(filtered)
def fun_successRate_full(filtered):
    if len(filtered) == 0:
        return "-"
    num_opt = get_num_optimal(filtered)
    return "{0}/{1}".format(str(num_opt), str(len(filtered)))
def get_successRate(filtered):
    num_opt = get_num_optimal(filtered)
    return float(num_opt) / float(len(filtered))
def fun_successRateMseOnly(filtered):
    if len(filtered) == 0:
        return "-"
    n = get_num_optimalOnlyMse(filtered)
    if n == 0:
        return "-"
    else:
        sr = n / float(len(filtered))
        return "{0}".format("%0.2f" % round(sr, 2))
def fun_successRate(filtered):
    if len(filtered) == 0:
        return "-"
    sr = get_successRate(filtered)
    return "{0}".format("%0.2f" % round(sr, 2))
def fun_allPropertiesMet(filtered):
    if len(filtered) == 0 or len([p for p in filtered if "result.best.correctVerification" in p]) == 0: # or "result.best.correctVerification" not in filtered[0]:
        return "-"
    num_opt = get_num_allPropertiesMet(filtered)
    sr = float(num_opt) / float(len(filtered))
    return "{0}".format("%0.2f" % round(sr, 2))
def fun_allPropertiesMet_verificator(filtered):
    if len(filtered) == 0: # or "result.best.correctVerification" not in filtered[0]:
        return "-"
    num_opt = get_num_allPropertiesMet_verificator(filtered)
    sr = float(num_opt) / float(len(filtered))
    return "{0}".format("%0.2f" % round(sr, 2))
def fun_trainMseBelowThresh(filtered):
    if len(filtered) == 0:
        return "-"
    num_opt = get_num_trainMseBelowThresh(filtered)
    sr = float(num_opt) / float(len(filtered))
    return "{0}".format("%0.2f" % round(sr, 2))
def get_stats_size(props):
    vals = [float(p["result.best.size"]) for p in props]
    if len(vals) == 0:
        return "-"#-1.0, -1.0
    else:
        return str(int(round(np.mean(vals)))) #, np.std(vals)
def get_stats_sizeOnlySuccessful(props):
    vals = [float(p["result.best.size"]) for p in props if is_optimal_solution(p)]
    if len(vals) == 0:
        return "-"#-1.0, -1.0
    else:
        return str(int(round(np.mean(vals)))) #, np.std(vals)
def get_stats_maxSolverTime(props):
    if len(props) == 0 or "solver.allTimesCountMap" not in props[0]:
        return "-"
    times = []
    for p in props:
        timesMap = p["solver.allTimesCountMap"]
        parts = timesMap.split(", ")[-1].split(",")
        times.append(float(parts[0].replace("(", "")))
    return "%0.3f" % max(times)
def get_stats_avgSolverTime(props):
    if len(props) == 0 or "solver.allTimesCountMap" not in props[0] or props[0]["method"] != "CDGP":
        return "-"
    sum = 0.0
    sumWeights = 0.0
    for p in props:
        timesMap = p["solver.allTimesCountMap"]
        pairs = timesMap.split(", ")
        if len(pairs) == 0:
            continue
        for x in pairs:
            time = float(x.split(",")[0].replace("(", ""))
            weight = float(x.split(",")[1].replace(")", ""))
            sum += time * weight
            sumWeights += weight
    if sumWeights == 0.0:
        return "%0.3f" % 0.0
    else:
        return "%0.3f" % (sum / sumWeights)
def get_avgSolverTotalCalls(props):
    if len(props) == 0 or "solver.totalCalls" not in props[0]:
        return "-"
    vals = [float(p["solver.totalCalls"]) / 1000.0 for p in props]
    return "%0.1f" % round(np.mean(vals), 1) # "%d"
def get_numSolverCallsOverXs(props):
    if len(props) == 0 or "solver.allTimesCountMap" not in props[0]:
        return "-"
    TRESHOLD = 0.5
    sum = 0
    for p in props:
        timesMap = p["solver.allTimesCountMap"]
        pairs = timesMap.split(", ")
        if len(pairs) == 0:
            continue
        for x in pairs:
            time = float(x.split(",")[0].replace("(", ""))
            if time > TRESHOLD:
                # print("Name of file: " + p["thisFileName"])
                weight = int(x.split(",")[1].replace(")", ""))
                sum += weight
    return sum
def get_avg_totalTests(props):
    vals = [float(p["tests.total"]) for p in props]
    if len(vals) == 0:
        return "-"
    else:
        x = np.mean(vals)
        if x < 1e-5:
            x = 0.0
        # return str(int(round(x)))
        return "{:.1f}".format(x)
mse_dformat = "%0.4f"
def get_avg_trainMSE(props):
    vals = []
    for p in props:
        vals.append(float(p["result.best.trainMSE"]))
    if len(vals) == 0:
        return "-"
    else:
        return mse_dformat % np.mean(vals)  # , np.std(vals)
def get_median_trainMSE(props):
    vals = []
    for p in props:
        vals.append(float(p["result.best.trainMSE"]))
    if len(vals) == 0:
        return "-"
    else:
        # return mse_dformat % np.median(vals)  # , np.std(vals)
        return scientificNotationLatex(np.median(vals))
def get_median_mseOptThresh(props):
    vals = []
    for p in props:
        vals.append(float(p["cdgp.optThresholdMSE"]))
    if len(vals) == 0:
        return "-"
    else:
        # return mse_dformat % np.median(vals)  # , np.std(vals)
        return scientificNotationLatex(np.median(vals))
def get_avg_testMSE(props):
    vals = []
    for p in props:
        vals.append(float(p["result.best.testMSE"]))
    if len(vals) == 0:
        return "-"
    else:
        return mse_dformat % np.mean(vals)  # , np.std(vals)
def get_median_testMSE(props):
    if len(props) == 0:
        return "-"
    else:
        median = np.median([float(p["result.best.testMSE"]) for p in props])
        return scientificNotationLatex(median)  # , np.std(vals)
def get_median_testMSE_noScNotation(props):
    if len(props) == 0:
        return "-"
    else:
        median = np.median([float(p["result.best.testMSE"]) for p in props])
        return median
def get_min_trainMSE(props):
    # L = [float(p["result.best.trainMSE"]) for p in props if float(p["result.best.trainMSE"]) <= 1.e-8 and float(p["result.best.trainMSE"]) >= 1.e-15]  #good line
    L = [float(p["result.best.trainMSE"]) for p in props if float(p["result.best.trainMSE"]) <= 1.e-10]  #experimental line
    if len(L) == 0:
        return "-"
    else:
        return scientificNotationLatex(np.min(L))
def get_MSE_bestOnValidSet_p(p, outputKey):
    """For a single dict p, returns a $outputKey (train | valid | test) MSE of the best solution on the valid set.
     Checks, if best of run was better on valid set, since in some cases it terminates search in CDGP before best of valid is updated."""
    if p["method"] in ["CDGP", "CDGPprops"]:
        if "result.validation.best.testMSE" not in p:
            print("No validation set logging!!!")
            raise Exception("No validation set logging for file {0}".format(p["evoplotter.file"]))
        else:
            best_run = float(p["result.best.validMSE"])
            best_valid = float(p["result.validation.best.validMSE"])
            if best_run < best_valid:
                return float(p["result.best.{}".format(outputKey)])  # the best valid would be updated anyway, but was deemed to be a solution and search terminated
            else:
                return float(p["result.validation.best.{}".format(outputKey)])
    else:
        k = "result.best.{}".format(outputKey)
        if k in p:
            return float(p[k])  # scikit algorithms
        else:
            return None

def get_testMSE_bestOnValidSet_p(p):
    return get_MSE_bestOnValidSet_p(p, "testMSE")
def get_validMSE_bestOnValidSet_p(p):
    return get_MSE_bestOnValidSet_p(p, "validMSE")
def get_trainMSE_bestOnValidSet_p(p):
    return get_MSE_bestOnValidSet_p(p, "trainMSE")

def get_median_testMSE_bestOnValidCDGP(props):
    if len(props) == 0:
        return "-"
    else:
        res = []
        for p in props:
            res.append(get_testMSE_bestOnValidSet_p(p))
        median = np.median(res)
        return scientificNotationLatex(median)  # , np.std(vals)
def get_median_testMSE_noScNot(props):
    vals = [float(p["result.best.testMSE"]) for p in props]
    if len(vals) == 0:
        return "-"
    else:
        return mse_dformat % np.median(vals)  # , np.std(vals)
def get_median_testMSEsuccessRateForThresh(props):
    if len(props) == 0:
        return "-"
    else:
        numSucc = 0.0
        for p in props:
            value = float(p["result.best.testMSE"])
            if value < float(p["cdgp.optThresholdMSE"]):
                numSucc += 1.0
        return "%0.2f" % round(numSucc / len(props), 2)
def get_median_testMSEsuccessRateForThresh_onlyVerified(props):
    if len(props) == 0:
        return "-"
    else:
        numSucc = 0.0
        for p in props:
            value = float(p["result.best.testMSE"])
            if is_optimal_solution(p) and value < float(p["cdgp.optThresholdMSE"]):
                numSucc += 1.0
        return "%0.2f" % round(numSucc / len(props), 2)
def get_avg_doneAlgRestarts(props):
    vals = []
    for p in props:
        if "cdgp.doneAlgRestarts" in p:
            vals.append(float(p["cdgp.doneAlgRestarts"]))
    if len(vals) == 0:
        return "-"
    else:
        return "%0.1f" % np.mean(vals)  # , np.std(vals)
def get_avg_runtime_helper(vals):
    if len(vals) == 0:
        return "n/a"
    else:
        x = np.mean(vals)
        if x >= 10.0:
            return "%d" % x
        else:
            return "%0.1f" % x  # , np.std(vals)
def get_avg_runtimeOnlySuccessful(props):
    if len(props) == 0:
        return "-"
    else:
        vals = [float(normalized_total_time(p, max_time=1800000)) / 1000.0 for p in props if is_optimal_solution(p)]
        return get_avg_runtime_helper(vals)
def get_avg_runtime(props):
    if len(props) == 0:
        return "-"
    else:
        vals = [float(normalized_total_time(p, max_time=1800000)) / 1000.0 for p in props]
        return get_avg_runtime_helper(vals)
def get_avg_generation(props):
    if len(props) == 0:
        return "-"
    else:
        vals = [float(p["result.totalGenerations"]) for p in props if "result.totalGenerations" in p]
        if len(vals) == 0:
            return "-"
        else:
            return str(int(round(np.mean(vals)))) #"%0.1f" % np.mean(vals)  # , np.std(vals)
def get_avg_generationSuccessful(props):
    if len(props) == 0:
        return "-"
    else:
        vals = [float(p["result.best.generation"]) for p in props if is_optimal_solution(p)]
        if len(vals) == 0:
            return "n/a"
        else:
            return str(int(round(np.mean(vals))))  # "%0.1f" % np.mean(vals)  # , np.std(vals)
def get_avg_evaluated(props):
    if len(props) == 0:
        return "-"
    vals = []
    for p in props:
        if "evolutionMode" in p:
            if p["evolutionMode"] == "steadyState":
                vals.append(float(p["result.totalGenerations"]))
            else:
                vals.append(float(p["result.totalGenerations"]) * float(p["populationSize"]))
    if len(vals) == 0:
        return "-"
    else:
        return "{:.1f}".format(np.mean(vals) / 1000.0)
        # return str(int(round(np.mean(vals)))) #"%0.1f" % np.mean(vals)  # , np.std(vals)
def get_avg_evaluatedSuccessful(props):
    if len(props) == 0:
        return "-"
    vals = []
    for p in props:
        if is_optimal_solution(p):
            if p["evolutionMode"] == "steadyState":
                vals.append(float(p["result.totalGenerations"]))
            else:
                vals.append(float(p["result.totalGenerations"]) * float(p["populationSize"]))
    if len(vals) == 0:
        return "n/a"
    else:
        return str(int(round(np.mean(vals))))  # "%0.1f" % np.mean(vals)  # , np.std(vals)
def get_avg_runtimePerProgram(props):
    if len(props) == 0:
        return "-"
    sAvgGen = get_avg_generation(props)
    if sAvgGen == "-" or sAvgGen is None:
        return "-"
    avgGen = float(sAvgGen)  # avg number of generations in all runs
    avgRuntime = float(get_avg_runtime(props))  # avg runtime of all runs
    populationSize = float(props[0]["populationSize"])
    if props[0]["evolutionMode"] == "steadyState":
        approxNumPrograms = populationSize + avgGen  # in steady state we have many generations, but in each of them created is one new program
    else:
        approxNumPrograms = populationSize * avgGen
    approxTimePerProgram = avgRuntime / approxNumPrograms
    return "%0.3f" % approxTimePerProgram
def get_sum_solverRestarts(props):
    if len(props) == 0:
        return "-"
    vals = [int(p["solver.totalRestarts"]) for p in props if "solver.totalRestarts" in p]
    if len(vals) != len(props):
        print("WARNING: solver.totalRestarts was not present in all files.")
    if len(vals) == 0:
        return "0"
    else:
        return str(np.sum(vals))
def getAvgSatStochasticVerificator(props):
    assert len(props) > 0
    if any(["result.best.verificator.decisions" not in p for p in props]):
        return "n/a"
    # it is assumed that props contain only props with scikit runs
    sumSat = 0
    numProps = None
    for p in props:
        if "result.best.verificator.decisions" in p:
            satVector = p["result.best.verificator.decisions"].split(",")
            satVector = [int(s) for s in satVector]
            sumSat += sum(satVector)
            numProps = len(satVector)
        else:
            raise Exception("No information about satisfied constraints")
    avgSat = float(sumSat) / len(props)
    return avgSat, numProps

def getAvgSatRatioStochasticVerifier_p(p):
    if "result.best.verificator.decisions" in p:
        satVector = p["result.best.verificator.decisions"].split(",")
        satVector = [float(s) for s in satVector]
        return sum(satVector) / len(satVector)
    else:
        return None

def getAvgSat(props):
    assert len(props) > 0
    # it is assumed that props contain only props with scikit runs
    sumSat = 0
    numProps = None
    for p in props:
        if "result.best.verificator.decisions" in p:
            satVector = p["result.best.verificator.decisions"].split(",")
            satVector = [int(s) for s in satVector]
            sumSat += sum(satVector)
            numProps = len(satVector)
        elif "result.best.passedConstraints" in p:
            # result.best.passedConstraints = List(1, 1, 1, 1)
            satVector = p["result.best.passedConstraints"][5:-1].split(", ")
            satVector = [int(s) for s in satVector]
            for i, s in enumerate(satVector):  # reversing, because 0 in CDSR logs means that constraint was passed
                if satVector[i] == 1:
                    satVector[i] = 0
                elif satVector[i] == 0:
                    satVector[i] = 1
            sumSat += sum(satVector)
            numProps = len(satVector)
        else:
            raise Exception("No information about satisfied constraints")
    avgSat = float(sumSat) / len(props)
    return avgSat, numProps
def getAvgSatisfiedProps1(props):
    if len(props) == 0:
        return "-"
    avgSat, numProps = getAvgSat(props)
    avgSat = avgSat / numProps
    return "{0}".format("%0.2f" % avgSat)
def getAvgSatisfiedProps2(props):
    if len(props) == 0:
        return "-"
    avgSat, numProps = getAvgSat(props)
    avgSat = int(avgSat) if int(avgSat) == avgSat else round(avgSat, 2)
    return "{0}/{1}".format(avgSat, numProps)
def getAvgSatisfiedRatios(props):
    if len(props) == 0:
        return "-"
    sumSat = 0
    numProps = None
    for p in props:
        satVector = p["result.best.verificator.ratios"].split(",")
        satVector = [float(s) for s in satVector]
        sumSat += sum(satVector)
        numProps = len(satVector)
    avgSat = float(sumSat) / len(props)
    avgSat = avgSat / numProps
    return "{0}".format("%0.2f" % avgSat)
def get_validation_testSet_ratio(props):
    mse_ratios = []
    for p in props:
        b_mse = float(p["result.best.testMSE"])
        if "result.validation.best.testMSE" not in p:
            print("'result.validation.best.testMSE' not present for {0}".format(p["evoplotter.file"]))
        else:
            v_mse = float(p["result.validation.best.testMSE"])
            if math.isclose(v_mse, 0.0):
                if math.isclose(b_mse, 0.0):
                    mse_ratios.append(1.0)
                else:
                    raise Exception("This shouldn't happen!")
            else:
                mse_ratios.append(b_mse / v_mse)
    if len(mse_ratios) == 0:
        return "-"
    else:
        return "{0}".format("%0.2f" % np.mean(mse_ratios))
def get_freqCounterexamples(props):
    if len(props) == 0:
        return "-"
    counterex = {}
    for p in props:
        # e.g. "ArrayBuffer((Map(m1 -> 0.0, m2 -> 0.0, r -> 0.0),None), (Map(m1 -> 1.0, m2 -> 2.0, r -> 1.0),None)
        s = p["tests.collected"]
        cxs = re.findall("\(Map\((?:[^,]+ -> [^,]+(?:, )?)+\),None\)", s)  # ^ makes to match all charecters other than ','
        # cxs = <class 'list'>: ['(Map(m1 -> 0.0, m2 -> 0.0, r -> 0.0),None)', '(Map(m1 -> 1.0, m2 -> 2.0, r -> 1.0),None)']
        for cx in cxs:
            # e.g. cx = (Map(m1 -> 0.0, m2 -> 0.0, r -> 0.0),None)
            if "None" in cx: # we are interested only in the noncomplete tests
                cx = cx[len("(Map("):-len("),None)")]
                # cx = 'm1 -> 0.0, m2 -> 0.0, r -> 0.0'
                fargs = re.findall("[^,]+ -> [^,]+(?:, )?", cx)  # findall returns non-overlapping matches in string
                fargs = [a.replace(", ", "") for a in fargs]
                fargs.sort(key=lambda a: a.split(" -> ")[0])
                sargs = ",".join(fargs).replace(" -> ", "=")
                if sargs in counterex:
                    counterex[sargs] += 1
                else:
                    counterex[sargs] = 1
    counterex_items = list(counterex.items())
    if len(counterex_items) == 0:
        return "n/a"
    else:
        counterex_items.sort(key=lambda x: (x[1], x[0]), reverse=True)
        NUM_SHOWN = 5
        # For some strange reason makecell doesn't work, even when it is a suggested answer (https://tex.stackexchange.com/questions/2441/how-to-add-a-forced-line-break-inside-a-table-cell)
        # return "\\makecell{" + "{0}  ({1})\\\\{2}  ({3})".format(counterex_items[0][0], counterex_items[0][1],
        #                                        counterex_items[1][0], counterex_items[1][1]) + "}"
        res = "\\pbox[l][" + str(17 * min(NUM_SHOWN, len(counterex_items))) + "pt][b]{20cm}{"
        for i in range(NUM_SHOWN):
            if i >= len(counterex_items):
                break
            if i > 0:
                res += "\\\\ \\ "
            # res += "{0}  ({1})".format(counterex_items[i][0], counterex_items[i][1])  # absolute value
            percent = round(100.0*float(counterex_items[i][1]) / len(props), 1)
            color = printer.getLatexColorCode(percent, [0., 50., 100.], ["darkred!50!white", "orange", "darkgreen"])
            res += "{0}  ({1})".format(counterex_items[i][0], r"\textbf{\textcolor{" + color + "}{" + str(percent) + "\%}}")  # percentage of runs
        res += "}"
        return res

def get_rankingOfBestSolversCDSR(dim_ranking, ONLY_VISIBLE_SOLS=True, NUM_SHOWN=5):
    def sorted_list_lambda(props):
        valuesList = []
        for config in dim_ranking:
            name = config.get_caption()
            props2 = config.filter_props(props)
            if len(props2) > 0:
                v = np.median([float(p["result.best.testMSE"]) for p in props2])
                valuesList.append((name, float(v)))

        valuesList.sort(key=lambda x: (x[1], x[0]), reverse=False)
        return valuesList

    def entry_formatter_lambda(allSolutions, entryIndex):
        # entry = (name, best.testMSE)
        entry = allSolutions[entryIndex]
        value = float(entry[1])
        valueSc = scientificNotationLatex(value)

        def nameFormatter(x):
            if "CDGP" in str(x):
                return r"\textcolor{darkblue}{" + str(x) + "}"
            elif x == "ALL":
                return r"\underline{ALL}"
            else:
                return x

        color = printer.getLatexColorCode(value, [allSolutions[0][1], (allSolutions[-1][1] + allSolutions[0][1]) / 2.0,
                                                  allSolutions[-1][1]],
                                          ["darkgreen", "orange", "darkred!50!white"])
        return "{0}  ({1})".format(nameFormatter(entry[0]),
                                   r"\textbf{\textcolor{" + color + "}{" + str(valueSc) + "}}")  # percentage of runs

    return rankingFunctionGenerator(sorted_list_lambda, entry_formatter_lambda, ONLY_VISIBLE_SOLS=ONLY_VISIBLE_SOLS,
                                    NUM_SHOWN=NUM_SHOWN)

def get_averageAlgorithmRanksCDSR(dim_ranking, dim_ranks_trials, ONLY_VISIBLE_SOLS=True, NUM_SHOWN=15):
    def sorted_list_lambda(props):
        allRanks = {}  # for each config name contains a list of its ranks
        for config_trial in dim_ranks_trials:
            props_trial = config_trial.filter_props(props)

            valuesList = []
            for config in dim_ranking:
                name = config.get_caption()
                if name not in allRanks:
                    allRanks[name] = []
                props2 = config.filter_props(props_trial)
                if len(props2) > 0:
                    v = np.median([float(p["result.best.testMSE"]) for p in props2])
                    valuesList.append((name, float(v)))

            valuesList.sort(key=lambda x: (x[1], x[0]), reverse=False)

            # "If there are tied values, assign to each tied value the average of
            #  the ranks that would have been assigned without ties."
            import scipy.stats as ss
            # In[19]: ss.rankdata([3, 1, 4, 15, 92])
            # Out[19]: array([2., 1., 3., 4., 5.])
            #
            # In[20]: ss.rankdata([1, 2, 3, 3, 3, 4, 5])
            # Out[20]: array([1., 2., 4., 4., 4., 6., 7.])
            ranks = ss.rankdata([x[1] for x in valuesList])
            for (r, (name, value)) in zip(ranks, valuesList):
                allRanks[name].append(r)

            # The code below incorrectly handles ties
            # for i, (name, value) in enumerate(valuesList):
            #     allRanks[name].append(i + 1)  # 'i' is incremented so that the first element has rank 1

        # Remove from allRanks all algorithms with empty list of ranks
        allRanks = {k: allRanks[k] for k in allRanks if len(allRanks[k]) > 0}

        # Here we should have a dictionary containing lists of ranks
        valuesList = [(name, np.mean(ranks)) for (name, ranks) in allRanks.items()]
        valuesList.sort(key=lambda x: (x[1], x[0]), reverse=False)
        return valuesList

    def entry_formatter_lambda(allSolutions, entryIndex):
        entry = allSolutions[entryIndex]
        value = round(float(entry[1]), 2)
        nameFormatter = lambda x: r"\textcolor{darkblue}{" + str(x) + "}" if "CDGP" in str(x) else x
        color = printer.getLatexColorCode(value, [allSolutions[0][1], (allSolutions[-1][1] + allSolutions[0][1]) / 2.0,
                                                  allSolutions[-1][1]],
                                          ["darkgreen", "orange", "darkred!50!white"])
        return "{0}  ({1})".format(nameFormatter(entry[0]),
                                   r"\textbf{\textcolor{" + color + "}{" + str(value) + "}}")  # percentage of runs

    return rankingFunctionGenerator(sorted_list_lambda, entry_formatter_lambda, ONLY_VISIBLE_SOLS=ONLY_VISIBLE_SOLS,
                                    NUM_SHOWN=NUM_SHOWN)

def get_rankingOfBestSolutionsCDSR(ONLY_VISIBLE_SOLS=True, NUM_SHOWN=15, STR_LEN_LIMIT=70, showSimplified=True):

    def shortenLongConstants(s):
        # cxs = re.findall("\(Map\((?:[^,]+ -> [^,]+(?:, )?)+\),None\)", s)
        cxs = re.findall("[0-9]+[.][0-9][0-9][0-9]+", s)
        for cx in cxs:
            x = float(cx)
            s = s.replace(cx, str(round(x, 2)) + "..")
        return s

    def sorted_list_lambda(props):
        if showSimplified:
            solutions = [(p["result.best"], int(p["result.best.size"]), float(p["result.best.testMSE"])) for p in props]
        else:
            solutions = [(p["result.bestOrig"], int(p["result.bestOrig.size"]), float(p["result.best.testMSE"])) for p in props]
        solutions.sort(key=lambda x: (x[2], x[1]), reverse=False)
        return solutions

    def entry_formatter_lambda(allSolutions, entryIndex):
        # solution = (bestOrig, bestOrig.size, testMSE)
        solution = allSolutions[entryIndex]
        def latexTextColor(color, x):
            return r"\textbf{\textcolor{" + str(color) + "}{" + str(x) + "}}"

        value = float(solution[2])
        valueSc = scientificNotationLatex(value)
        colorValue = printer.getLatexColorCode(value, [allSolutions[0][2], (allSolutions[-1][2] + allSolutions[0][2]) / 2.0, allSolutions[-1][2]],
                                               ["darkgreen", "orange", "darkred!50!white"])
        size = int(solution[1])
        sizeStr = r"\textcolor{darkblue}{\textbf{" + str(size) + r"}}"
        # colorSize = printer.getLatexColorCode(value, sizeColorScale, ["darkred!50!white", "orange", "darkgreen"])

        sol = solution[0]
        sol = shortenLongConstants(sol)
        sol = sol[:STR_LEN_LIMIT] + " [..]" if len(sol) > STR_LEN_LIMIT else sol
        # sol = r"\texttt{" + sol + "}"
        return r"{0}  ({1}) ({2})".format(sol, latexTextColor(colorValue, valueSc), sizeStr)

    return rankingFunctionGenerator(sorted_list_lambda, entry_formatter_lambda, ONLY_VISIBLE_SOLS=ONLY_VISIBLE_SOLS,
                                    NUM_SHOWN=NUM_SHOWN)

def print_solved_in_time(props, upper_time):
    if len(props) == 0:
        return
    # totalTimeSystem is in miliseconds
    solved = 0
    solvedRuns = 0
    num = 0
    for p in props:
        if p["result.best.isOptimal"] == "false":
            continue
        num += 1
        if int(normalized_total_time(p, max_time=1800000)) <= upper_time:
            solved += 1

    for p in props:
        if int(normalized_total_time(p, max_time=1800000)) <= upper_time:
            solvedRuns += 1
    print("\nRuns which ended under {0} s:  {1} / {2}  ({3} %)".format(upper_time / 1000.0, solvedRuns, len(props), solvedRuns / len(props)))
    print("Optimal solutions found under {0} s:  {1} / {2}  ({3} %)\n".format(upper_time / 1000.0, solved, num, solved / num))

def print_and_save_status_matrix(props, dim_rows, dim_cols, dir_path):
    d = dim_rows * dim_cols
    matrix = produce_status_matrix(d, props)
    print("\n****** Status matrix:")
    print(matrix + "\n")
    print("Saving status matrix to file: {0}".format("{}status.txt".format(dir_path)))
    utils.save_to_file("{}status.txt".format(dir_path), matrix)

def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places. Source: https://kodify.net/python/math/round-decimals/#round-decimal-places-down-in-python
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor

max_stars_repo_name: iwob/evoplotter

max_issues_repo_name: iwob/evoplotter

max_forks_repo_name: iwob/evoplotter

reason: import numpy,import scipy


text:
import argparse
from os import makedirs, path

import numpy as np
import torch
from torch.nn import DataParallel
from tqdm import tqdm

from data.data_loader_test import TestDataLoader
from model.resnet import ResNet
from utils.model_loader import load_state

class Extractor:
    def __init__(
        self,
        model_path,
        source,
        image_list,
        dest,
        net_mode,
        depth,
        batch_size,
        workers,
        drop_ratio,
        device,
    ):

        self.loader = TestDataLoader(batch_size, workers, source, image_list)
        self.batch_size = batch_size
        self.image_paths = np.asarray(self.loader.dataset.samples)
        self.model = None
        self.device = device
        self.destination = dest

        self.model = self.create_model(depth, drop_ratio, net_mode, model_path)
        self.model.eval()

    def create_model(self, depth, drop_ratio, net_mode, model_path):
        model = DataParallel(ResNet(depth, drop_ratio, net_mode)).to(self.device)
        load_state(
            model=model, path_to_model=model_path, model_only=True, load_head=False
        )

        model.eval()

        return model

    def run(self):
        idx = 0
        with torch.no_grad():
            for imgs in tqdm(iter(self.loader)):
                imgs = imgs.to(device)

                embeddings = self.model(imgs)
                embeddings = embeddings.cpu().numpy()

                image_paths = self.image_paths[idx : idx + self.batch_size]
                self.save_features(image_paths, embeddings)
                idx += self.batch_size

    def save_features(self, image_paths, embeddings):
        for i in range(0, len(embeddings)):
            image_name = path.split(image_paths[i])[1]
            sub_folder = path.basename(path.normpath(path.split(image_paths[i])[0]))
            dest_path = path.join(self.destination, sub_folder)

            if not path.exists(dest_path):
                makedirs(dest_path)

            features_name = path.join(dest_path, image_name[:-3] + "npy")
            np.save(features_name, embeddings[i])

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Extract features.")
    parser.add_argument("--source", "-s", help="Path to the images.")
    parser.add_argument("--image_list", "-i", help="File with images names.")
    parser.add_argument("--dest", "-d", help="Path to save the predictions.")
    parser.add_argument("--batch_size", "-b", help="Batch size.", default=250, type=int)
    parser.add_argument("--model", "-m", help="Path to model.")
    parser.add_argument(
        "--net_mode", "-n", help="Residual type [ir, ir_se].", default="ir_se", type=str
    )
    parser.add_argument(
        "--depth", "-dp", help="Number of layers [50, 100, 152].", default=50, type=int
    )
    parser.add_argument("--workers", "-w", help="Workers number.", default=4, type=int)

    args = parser.parse_args()

    if not path.exists(args.dest):
        makedirs(args.dest)

    drop_ratio = 0.4
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    extractor = Extractor(
        args.model,
        args.source,
        args.image_list,
        args.dest,
        args.net_mode,
        args.depth,
        args.batch_size,
        args.workers,
        drop_ratio,
        device,
    )
    extractor.run()


text:
#!/usr/bin/env python
# coding: utf-8
# Copyright 2019 Lawrence Livermore National Security, LLC and other PSINN
# developers. See the top-level LICENSE file for more details.
#
# SPDX-License-Identifier: MIT

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import tensorflow as tf
import pandas as pd
from scipy import integrate
from scipy.integrate import solve_ivp
import pywt

# for reproducibility
def reset_graph(seed=42):
    tf.reset_default_graph()
    tf.set_random_seed(seed)
    np.random.seed(seed)

# This cleaned data set takes the cos of the angular data
# In angular data there is a switch point, we remove this by taking the average of the points on either side
# The mag data is not edited, we will normalize in this notebook
data = pd.read_csv('all_clean_data_10ms.csv', index_col=0)
column_names = list(data.columns.values.tolist())
data.head()

# Lets start by normalizing the magnitude data between -1 and 1
# so we can find the integral of the data
for name in ['c1_mag', 'c2_mag', 'c3_mag', 'l1_mag', 'l2_mag', 'l3_mag']:
    data[name] = data[name] - np.mean(data[name])
    data[name] = data[name] / max(np.abs(data[name]))

# The angular data is already between -1 and 1 because we took the cos

# Integrate all the data streams
t = np.linspace(0, data.shape[0]-1, data.shape[0])/100
for name in column_names:
    data[name+'_int'] = integrate.cumtrapz(data[name], t, initial = 0)

data.drop(data.tail(5).index,inplace=True)

def lowpassfilter(signal, thresh = .63, wavelet="db4"):
    thresh = thresh*np.nanmax(signal)
    coeff = pywt.wavedec(signal, wavelet, mode="per" )
    coeff[1:] = (pywt.threshold(i, value=thresh, mode="soft" ) for i in coeff[1:])
    reconstructed_signal = pywt.waverec(coeff, wavelet, mode="per" )
    return reconstructed_signal

# Lets run all angular data through a low pass filter
# for name in ['c1_ang', 'c2_ang', 'c3_ang', 'l1_ang', 'l2_ang', 'l3_ang']:
#     data[name+'_int'] = lowpassfilter(data[name+'_int'], thresh = 500)[0:-1]

#Set up data to feed into the neural network
# batch_size = data.shape[0]
batch_size = 5000
numbatches = 1
n_d = 6
j=0

data_x = np.zeros((numbatches, batch_size, n_d))
data_y = np.zeros((numbatches, batch_size, n_d))

for i in range(numbatches):
    data_x[i,:,:] = data.iloc[i*batch_size:(i+1)*batch_size,j:n_d+j].values
    data_y[i,:,:] = data.iloc[i*batch_size:(i+1)*batch_size,12+j:12+n_d+j].values

# set up neural model for vector field
reset_graph()

# set parameters
n_L = batch_size

# want to incorporate M trajectories, each of length L, and each in R^d;
# the vector field will be the model Y = f(X)
X = tf.placeholder(tf.float32, [None, n_L, n_d], name="X")
Y = tf.placeholder(tf.float32, [None, n_L, n_d], name="Y")

# set up neural model for vector field

# set number of shape functions
n_s = 2

# set depth of network (number of hidden layers)
depth = 1

# set number of neurons in each layer
# this should be a list of integers, with length = depth
numneurons = [4]

#poly initialization
initvec1 = tf.expand_dims(tf.concat([tf.ones([1]), -tf.ones([1]), tf.zeros([numneurons[0]-2])], axis = 0), 1)
initvec2 = tf.expand_dims(tf.concat([tf.ones([1]), tf.ones([1]), tf.zeros([numneurons[0]-2])], axis = 0), 1)

# create all the shape functions
weights = [[]]*n_s
biases = [[]]*n_s
for i in range(n_s):
    for j in range(depth + 1):
        if (j==0):
            ninp = 1
        else:
            ninp = numneurons[j-1]
        if (j==depth):
            nout = 1
        else:
            nout = numneurons[j]
        wname = "weight_" + str(i) + "_" + str(j)
        bname = "bias_" + str(i) + "_" + str(j)

        if (j==0):
            weights[i] = [tf.Variable(tf.transpose(initvec1), name=wname, dtype=tf.float32)]
            biases[i] = [tf.Variable(tf.zeros([1, nout]), name=bname, dtype=tf.float32)]
        else:
            if (i==0):
                biases[i].append(tf.Variable(tf.ones([1, nout]), name=bname, dtype=tf.float32))
            else:
                biases[i].append(tf.Variable(tf.zeros([1, nout]), name=bname, dtype=tf.float32))
            if (i % 2 == 0):
                weights[i].append(tf.Variable(initvec2, name=wname, dtype=tf.float32))
            else:
                weights[i].append(tf.Variable(initvec1, name=wname, dtype=tf.float32))

# define the shape functions
def psi(si, x):
    psiout = x
    for j in range(depth):
        if si >= 1:
            temp = tf.math.pow(tf.nn.relu(tf.tensordot(psiout, weights[si][j], axes=[2, 0]) + biases[si][j]), tf.constant(si, dtype=tf.float32))
        else:
            temp = tf.nn.relu(tf.tensordot(0*psiout, weights[si][j], axes=[2, 0]) + biases[si][j])

    temp = tf.tensordot(temp, weights[si][depth], axes=[2, 0]) + biases[si][depth]
    return temp

# split input by dimension
# splitX now contains a list of n_d tensors each of the form M x L x 1
splitX = tf.split(X, n_d, axis=2, name='split')

# now for each of these tensors, we pass them through all the shape functions
allouts = [[]]*n_d
for i in range(n_d):
    for j in range(n_s):
        if (j==0):
            allouts[i] = [psi(j, splitX[i])]
        else:
            allouts[i].append(psi(j, splitX[i]))

# now that we have all the 1d shape functions applied to each dimension,
# we form multidim shape functions up to a particular shell

# numpy tensor product matrix
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

# functions to create all multiindices with summed degree < maxdeg
maxdeg = n_s

def padder(x):
    if len(x) < n_d:
        return '0' * (n_d - len(x)) + x
    else:
        return x

def int2str(x):
    if x < 0:
        return "-" + int2str(-x)
    return ("" if x < maxdeg else int2str(x // maxdeg))            + digits[x % maxdeg]

def padint2str(x):
    return padder(int2str(x))

# index mapping
def index_mapping():
    index = 0
    index_map = {}
    allpos = list(map(padint2str, range(maxdeg ** n_d)))
    for d in range(maxdeg):
        for s in allpos:
            y = list(map(int, s))[::-1]
            if sum(y) == d:
                index_map[tuple(y)] = index
                index += 1

    return index_map

# all multiindices
indmap = index_mapping()
inds = list(indmap.keys())

# make list of tensors

# future idea: let the 0-th shape function correspond to a constant
# so in fact we have (n_s + 1) total shape functions, but only n_s trainable ones

tenlist = []
for ind in inds:
    temp = allouts[0][ind[0]]
    for j in range(1, n_d):
        temp = tf.multiply(temp, allouts[j][ind[j]])

    tenlist.append(tf.squeeze(temp, axis=2))

# multidimensional shape functions of inputs
n_ms = len(inds)
multishapes = tf.stack(tenlist,axis=2)

# coefficient matrix
beta = tf.Variable(tf.random.normal([n_ms, n_d]))

# neural vector field!
neuralf = tf.tensordot(multishapes, beta, axes=[2, 0])

# loss function and optimization setup
mseloss = tf.reduce_mean(tf.square(neuralf - Y))

# optimization
optimizer = tf.train.AdamOptimizer(learning_rate = .001)
training_op = optimizer.minimize(mseloss, var_list = beta)

# typical TF initialization
init = tf.global_variables_initializer()

# set up TF to save model to disk
saver = tf.train.Saver()

with tf.Session() as sess:
    init.run()
    # temp = sess.run(multishapes, feed_dict = {X:X_batch})
    maxsteps = 20000
    for i in range(maxsteps):
        sess.run(training_op, feed_dict = {X:data_y, Y:data_x})
        if (i % 100) == 0:
            print(i, "Training MSE:",
                  mseloss.eval(feed_dict = {X:data_y, Y:data_x}))

    saver.save(sess, "./trained_neuralf")
    betaout, weightsout, biasesout = sess.run([beta, weights, biases], feed_dict = {X:data_y, Y:data_x})

# set up TF to save model to disk
# saver = tf.train.Saver()

# with tf.Session() as sess:
#     saver.restore(sess, "./trained_neuralf")
#     x = tf.linspace(-1.0, 1.0, 201)
#     xnum = sess.run(x)
#     x = tf.expand_dims(x, axis=0)
#     x = tf.expand_dims(x, axis=2)
#     psigraphs = []
#     for i in range(n_s):
#         psigraphs.append(np.squeeze(sess.run(psi(i, x))))

#     graphtenlist = []
#     for ind in inds:
#         graphtemp = psigraphs[ind[0]]
#         for j in range(1, n_d):
#             graphtemp = np.outer(graphtemp, psigraphs[ind[j]])

#         graphtenlist.append(graphtemp)

#     betaout, weightsout, biasesout = sess.run([beta, weights, biases], feed_dict = {X:data_x, Y:data_y})

# ReLU fucntion for NumPy purposes
def reluNP(x):
    y = np.zeros(x.shape)
    y[x>=0] = x[x>=0]
    return y

#one dimensional shape functions for NumPy purposes
def psiNP(si,x):
    psiout = x
    for j in range(depth):
        if si >= 1:
            psiout = np.power(reluNP(np.dot(psiout, weightsout[si][j]) + biasesout[si][j]), si)
        else:
            psiout = reluNP(np.dot(psiout*0, weightsout[si][j]) + biasesout[si][j])

    psiout = np.dot(psiout, weightsout[si][depth]) + biasesout[si][depth]
    return psiout

def nvf(t,x):
    # split unput by dimesion along axis 1
    y = x.T
    splitNP = np.split(y, n_d, axis=1)

    # now for each of these tensors, we pass them through shape functions
    alloutsNP = [[]]*n_d
    for i in range(n_d):
        for j in range(n_s):
            if (j==0):
                alloutsNP[i] = [psiNP(j, splitNP[i])]
            else:
                alloutsNP[i].append(psiNP(j, splitNP[i]))

    tenlistNP = []
    for ind in inds:
        temp = alloutsNP[0][ind[0]]
        for j in range(1, n_d):
            temp = np.multiply(temp, alloutsNP[j][ind[j]])

        tenlistNP.append(np.squeeze(temp, axis=1))

    # multidimensional shape functions of inputs
    n_ms = len(inds)
    multishapesNP = np.stack(tenlistNP,axis=1)

    # neural vector field!
    nvfNP = np.matmul(multishapesNP, betaout)
    return nvfNP.T

romtraj = np.zeros((1, batch_size, n_d))
ft = (batch_size-1)/100
tvec = np.linspace(0, ft, batch_size)

# ic = data_x[[0],0,:].T
ic = data_y[[0],0,:].T
yic = np.squeeze(ic)
sol = solve_ivp(fun=nvf, t_span=[0, ft], y0 = yic, t_eval=tvec, rtol=1e-8, atol=1e-8, vectorized = True)
romtraj[0,:,:] = sol.y.T

f, axarr = plt.subplots(3, 4, figsize=(20, 10))
axarr[0,0].axis('off')
for i in range(1, n_d+1):
    plt.subplot(3, 4, i)
    plt.plot(romtraj[0,:,i-1],'o', color = 'r')
    plt.plot(data_y[0,:,i-1], color = 'k')

max_stars_repo_path: psinn/Full Data.py

max_stars_repo_name: tadesautels/PSINN

max_issues_repo_path: psinn/Full Data.py

max_issues_repo_name: tadesautels/PSINN

max_forks_repo_path: psinn/Full Data.py

max_forks_repo_name: tadesautels/PSINN

reason: import numpy,from scipy


text:
import numpy as np
import cv2
import matplotlib.pyplot as plt

def get_contour():
    """
       Gives a fixed contour
    """
    contours = np.array([[[600, 320]], [[563, 460]],
                         [[460, 562]], [[320, 600]],
                         [[180, 563]], [[78, 460]],
                         [[40, 320]], [[77, 180]],
                         [[179, 78]],
                         [[319, 40]], [[459, 77]], [[562, 179]]]
                        )

    return contours

def draw_contours(img, contours, colour):
    """
        Draws the contour points
    """
    for contour in contours:
        sqz = np.squeeze(contour)

        for pixel in sqz:
            pixel = tuple(pixel.reshape(1, -1)[0])
            cv2.circle(img, pixel, 10, colour, -1)

def output_plot(img, pos, title='image'):
    """
        Plots image
    """
    rgb_img = img[:, :, :: -1]
    plt.subplot(1, 3, pos)
    plt.imshow(rgb_img)
    plt.title(title)
    plt.axis('off')

def create_figure():
    """
        Specifies the figure dimensions and creates its canvas
    """
    fig = plt.figure(figsize=(12, 5))
    plt.suptitle('contours', fontsize=12)
    fig.patch.set_facecolor('silver')

    canvas = np.zeros([640, 640, 3], dtype=np.uint8)

    return fig, canvas

def draw_cont_outline(image, contours, colour, thickness=1):
    """
        Draws outlines of each contour
    """
    for ct in contours:
        cv2.drawContours(image, [ct], 0, colour, thickness)

def draw_outlines():
    """
        Draws contour outlines on images
    """
    fig, canvas = create_figure()
    contours = get_contour()[0]

    img_points = canvas.copy()
    img_outline = canvas.copy()
    img_points_outline = canvas.copy()

    # Points
    draw_contours(img_points, contours(255, 0, 255))

    # Outline
    draw_cont_outline(img_outline, contours, (0, 255, 255), 3)

    # Points + Outline
    draw_cont_outline(img_points_outline, contours, (255, 0, 0), 3)
    draw_contours(img_points_outline, contours, (0, 0, 255))

    for i, img in enumerate([img_points, img_outline, img_points_outline]):
        output_plot(img, i)
    plt.show()


text:
""" Testing driver LatinHypercubeDriver."""

import unittest
from random import seed
from types import GeneratorType

import numpy as np

from openmdao.api import IndepVarComp, Group, Problem, Component
from openmdao.test.paraboloid import Paraboloid
from openmdao.test.util import assert_rel_error

from openmdao.drivers.latinhypercube_driver import LatinHypercubeDriver, OptimizedLatinHypercubeDriver
from openmdao.drivers.latinhypercube_driver import _is_latin_hypercube, _rand_latin_hypercube, _mmlhs, _LHC_Individual

class TestLatinHypercubeDriver(unittest.TestCase):

    def setUp(self):
        self.seed()
        self.hypercube_sizes = ((3, 1), (5, 5), (20, 8))

    def seed(self):
        # seedval = None
        self.seedval = 1
        seed(self.seedval)
        np.random.seed(self.seedval)

    def test_rand_latin_hypercube(self):
        for n, k in self.hypercube_sizes:
            test_lhc = _rand_latin_hypercube(n, k)

            self.assertTrue(_is_latin_hypercube(test_lhc))

    def _test_mmlhs_latin(self, n, k):
        p = 1
        population = 3
        generations = 6

        test_lhc = _rand_latin_hypercube(n, k)
        best_lhc = _LHC_Individual(test_lhc, 1, p)
        mmphi_initial = best_lhc.mmphi()
        for q in (1, 2, 5, 10, 20, 50, 100):
            lhc_start = _LHC_Individual(test_lhc, q, p)
            lhc_opt = _mmlhs(lhc_start, population, generations)
            if lhc_opt.mmphi() < best_lhc.mmphi():
                best_lhc = lhc_opt

        self.assertTrue(
                best_lhc.mmphi() < mmphi_initial,
                "'_mmlhs' didn't yield lower phi. Seed was {}".format(self.seedval))

    def test_mmlhs_latin(self):
        for n, k in self.hypercube_sizes:
            self._test_mmlhs_latin(n, k)

    def test_algorithm_coverage_lhc(self):

        prob = Problem()
        root = prob.root = Group()

        root.add('p1', IndepVarComp('x', 50.0), promotes=['*'])
        root.add('p2', IndepVarComp('y', 50.0), promotes=['*'])
        root.add('comp', Paraboloid(), promotes=['*'])

        prob.driver = LatinHypercubeDriver(100)

        prob.driver.add_desvar('x', lower=-50.0, upper=50.0)
        prob.driver.add_desvar('y', lower=-50.0, upper=50.0)

        prob.driver.add_objective('f_xy')

        prob.setup(check=False)
        runList = prob.driver._build_runlist()
        prob.run()

        # Ensure generated run list is a generator
        self.assertTrue(
                (type(runList) == GeneratorType),
                "_build_runlist did not return a generator.")

        # Add run list to dictionaries
        xDict = []
        yDict = []
        countRuns = 0
        for inputLine in runList:
            countRuns += 1
            x, y = dict(inputLine).values()
            xDict.append(np.floor(x))
            yDict.append(np.floor(y))

        # Assert we had the correct number of runs
        self.assertTrue(
                countRuns == 100,
                "Incorrect number of runs generated.")

        # Assert all input values in range [-50,50]
        valuesInRange = True
        for value in xDict + yDict:
            if value < (-50) or value > 49:
                valuesInRange = False
        self.assertTrue(
                valuesInRange,
                "One of the input values was outside the given range.")

        # Assert a single input in each interval [n,n+1] for n = [-50,49]
        self.assertTrue(
                len(xDict) == 100,
                "One of the intervals wasn't covered.")
        self.assertTrue(
                len(yDict) == 100,
                "One of the intervals wasn't covered.")

    def test_algorithm_coverage_olhc(self):

        prob = Problem()
        root = prob.root = Group()

        root.add('p1', IndepVarComp('x', 50.0), promotes=['*'])
        root.add('p2', IndepVarComp('y', 50.0), promotes=['*'])
        root.add('comp', Paraboloid(), promotes=['*'])

        prob.driver = OptimizedLatinHypercubeDriver(100, population=5)
        prob.driver.add_desvar('x', lower=-50.0, upper=50.0)
        prob.driver.add_desvar('y', lower=-50.0, upper=50.0)

        prob.driver.add_objective('f_xy')

        prob.setup(check=False)
        runList = prob.driver._build_runlist()
        prob.run()

        # Ensure generated run list is a generator
        self.assertTrue(
                (type(runList) == GeneratorType),
                "_build_runlist did not return a generator.")

        # Add run list to dictionaries
        xDict = []
        yDict = []
        countRuns = 0
        for inputLine in runList:
            countRuns += 1
            x, y = dict(inputLine).values()
            xDict.append(np.floor(x))
            yDict.append(np.floor(y))

        # Assert we had the correct number of runs
        self.assertTrue(
                countRuns == 100,
                "Incorrect number of runs generated.")

        # Assert all input values in range [-50,50]
        valuesInRange = True
        for value in xDict + yDict:
            if value < (-50) or value > 49:
                valuesInRange = False
        self.assertTrue(
                valuesInRange,
                "One of the input values was outside the given range.")

        # Assert a single input in each interval [n,n+1] for n = [-50,49]
        self.assertTrue(
                len(xDict) == 100,
                "One of the intervals wasn't covered.")
        self.assertTrue(
                len(yDict) == 100,
                "One of the intervals wasn't covered.")

    '''
    def test_seed_works(self):
    '''

class ParaboloidArray(Component):
    """ Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 """

    def __init__(self):
        super(ParaboloidArray, self).__init__()

        self.add_param('X', val=np.array([0., 0.]))
        self.add_output('f_xy', val=0.0)

        self._history = []

    def solve_nonlinear(self, params, unknowns, resids):
        """f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3
        """

        x = params['X'][0]
        y = params['X'][1]
        self._history.append((x, y))
        unknowns['f_xy'] = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0

class TestLatinHypercubeDriverArray(unittest.TestCase):

    def test_rand_latin_hypercube(self):
        top = Problem()
        root = top.root = Group()

        root.add('p1', IndepVarComp('X', np.array([50., 50.])), promotes=['*'])

        root.add('comp', ParaboloidArray(), promotes=['*'])

        top.driver = OptimizedLatinHypercubeDriver(num_samples=4, seed=0, population=20,
                                                   generations=4, norm_method=2)
        top.driver.add_desvar('X', lower=np.array([-50., -50.]), upper=np.array([50., 50.]))

        top.driver.add_objective('f_xy')

        top.setup(check=False)
        top.run()

        results = top.root.comp._history
        #assert_rel_error(self, results[0][0], -11.279662, 1e-4)
        #assert_rel_error(self, results[0][1], -32.120265, 1e-4)
        #assert_rel_error(self, results[1][0], 40.069084, 1e-4)
        #assert_rel_error(self, results[1][1], -11.377920, 1e-4)
        #assert_rel_error(self, results[2][0], 10.5913699, 1e-4)
        #assert_rel_error(self, results[2][1], 41.147352826, 1e-4)
        #assert_rel_error(self, results[3][0], -39.06031971, 1e-4)
        #assert_rel_error(self, results[3][1], 22.29432501, 1e-4)

if __name__ == "__main__":
    unittest.main()


text:
# %%
from data import batch_load_crop_case, analyze_cases, \
    batch_resample_normalize_case, batch_regions_crop_case, CaseDataset
from visualize import case_plt

from pathlib import Path
from tqdm import tqdm
import numpy as np
from matplotlib import pyplot as plt
from datetime import datetime
import csv

image_dir = '/mnt/main/dataset/Task20_Kidney/imagesTr'
vessel_label_dir = '/mnt/main/dataset/Task20_Kidney/labelsTr_vessel'
vessel_crop_dir = 'data/Task20_Kidney/vessel_crop'
kidney_region_crop_dir = 'data/Task20_Kidney/kidney_region_crop'
vessel_region_crop_dir = 'data/Task20_Kidney/vessel_region_crop'
vessel_region_norm_dir = 'data/Task20_Kidney/vessel_region_norm'

region_based_on_dir = 'data/Task20_Kidney/kidney_label'

normalize_stats = {'median': 136.0,
                   'mean': 137.51925659179688,
                   'std': 88.92479705810547,
                   'min': -148.0,
                   'max': 1262.0,
                   'pct_00_5': -69.0,
                   'pct_99_5': 426.0}

# %%

batch_load_crop_case(image_dir,
                     vessel_label_dir,
                     vessel_crop_dir)

# %%
batch_regions_crop_case(vessel_crop_dir,
                        vessel_region_crop_dir,
                        threshold=10000,
                        pred_dir=region_based_on_dir)

# %%
batch_resample_normalize_case(vessel_region_crop_dir,
                              vessel_region_norm_dir,
                              (0.7, 0.7, 1),
                              normalize_stats)

# %%
analyze_cases(vessel_region_crop_dir)

# %%

def analyze_labels(load_dir, save_dir='chart/', num_labels=3, data_range=None):
    save_dir = Path(save_dir)
    if not save_dir.exists():
        save_dir.mkdir(parents=True)

    load_dir = Path(load_dir)
    cases = CaseDataset(load_dir)

    if data_range is None:
        data_range = range(len(cases))

    filename = "frequencies-{}.csv".format(datetime.now().strftime("%y%m%d%H%M"))
    csv_file = save_dir / filename
    with open(csv_file, 'w', newline='', encoding="utf-8-sig") as csvfile:
        csvWriter = csv.writer(csvfile, dialect='excel', quoting=csv.QUOTE_NONNUMERIC)
        for i in tqdm(data_range):
            case = cases[i]
            content = []
            for c in range(num_labels):
                label_np = case['label'] == c
                content.append(np.sum(label_np).item())

            csvWriter.writerow(content)

def analyze_annotations(load_dir, num_labels=4, data_range=None):
    load_dir = Path(load_dir)
    cases = CaseDataset(load_dir)
    sums = [0]*num_labels
    frequencies = [0]*num_labels
    if data_range is None:
        data_range = range(len(cases))

    for i in tqdm(data_range):
        case = cases[i]
        label_indices = np.unique(case['label'])
        for index in label_indices:
            sums[index] += 1
    frequencies = [v/len(data_range) for v in sums]
    print(sums)
    print(frequencies)
    return frequencies
    # plt.bar(sums)

analyze_labels(vessel_region_crop_dir, num_labels=3)

# %%