from java.io import File
from oracle.weblogic.deploy.util import PyOrderedDict

from wlsdeploy.aliases import alias_utils
from wlsdeploy.aliases.alias_constants import BOOLEAN
from wlsdeploy.aliases.alias_constants import PASSWORD_TOKEN
from wlsdeploy.aliases.location_context import LocationContext
from wlsdeploy.aliases.model_constants import CLUSTER
from wlsdeploy.aliases.model_constants import DEFAULT_WLS_DOMAIN_NAME
from wlsdeploy.aliases.model_constants import KUBERNETES
from wlsdeploy.aliases.model_constants import NAME
from wlsdeploy.exception import exception_helper
from wlsdeploy.tool.util import k8s_helper
from wlsdeploy.util import dictionary_utils
from wlsdeploy.util.model_translator import PythonToFile
from wlsdeploy.yaml.dictionary_list import DictionaryList

API_VERSION = 'apiVersion'
CHANNELS = 'channels'
CLUSTERS = 'clusters'
CLUSTER_NAME = 'clusterName'
DOMAIN_HOME = 'domainHome'
IMAGE = 'image'
IMAGE_PULL_POLICY = 'imagePullPolicy'
IMAGE_PULL_SECRETS = 'imagePullSecrets'
K_NAME = 'name'
KIND = 'kind'
METADATA = 'metadata'
NAMESPACE = 'namespace'
NEVER = 'Never'
REPLICAS = 'replicas'
SPEC = 'spec'
WEBLOGIC_CREDENTIALS_SECRET = 'webLogicCredentialsSecret'

DEFAULT_API_VERSION = 'weblogic.oracle/v6'
DEFAULT_KIND = 'Domain'
DEFAULT_WEBLOGIC_CREDENTIALS_SECRET = PASSWORD_TOKEN
DEFAULT_IMAGE = PASSWORD_TOKEN
DEFAULT_IMAGE_PULL_SECRETS = PASSWORD_TOKEN

class DomainResourceExtractor:
    """
    Create a domain resource file for use with Kubernetes deployment.
    """
    _class_name = "DomainResourceExtractor"

    # the name field keys corresponding to named model elements
    NAME_KEY_MAP = {
        CHANNELS: 'channelName',
        CLUSTERS: CLUSTER_NAME
    }

    def __init__(self, model, model_context, aliases, logger):
        self._model = model
        self._model_context = model_context
        self._aliases = aliases
        self._logger = logger
        return

    def extract(self):
        """
        Deploy resource model elements at the domain level, including multi-tenant elements.
        """
        _method_name = 'extract'

        resource_file = self._model_context.get_domain_resource_file()
        self._logger.info("WLSDPLY-10000", resource_file, method_name=_method_name, class_name=self._class_name)

        # create the target file directory, if needed
        resource_dir = File(resource_file).getParentFile()
        if (not resource_dir.isDirectory()) and (not resource_dir.mkdirs()):
            mkdir_ex = exception_helper.create_deploy_exception('WLSDPLY-10001', resource_dir)
            raise mkdir_ex

        # build the resource file structure from the kubernetes section of the model
        resource_dict = self._create_domain_resource_dictionary()

        # revise the resource file structure with values from command line, and elsewhere in model
        self._update_resource_dictionary(resource_dict)

        # write the resource file structure to the output file
        writer = PythonToFile(resource_dict)

        writer.write_to_file(resource_file)
        return

    def _create_domain_resource_dictionary(self):
        """
        Build the resource file structure from the kubernetes section of the model.
        :return: the resource file structure
        """
        kubernetes_map = self._model.get_model_kubernetes()

        resource_dict = PyOrderedDict()

        attribute_location = self._aliases.get_model_section_attribute_location(KUBERNETES)
        top_attributes_map = self._aliases.get_model_attribute_names_and_types(attribute_location)
        top_folders = self._aliases.get_model_section_top_level_folder_names(KUBERNETES)

        self._process_fields(kubernetes_map, top_folders, top_attributes_map, LocationContext(), resource_dict)
        return resource_dict

    def _process_fields(self, model_dict, folder_names, attributes_map, location, target_dict):
        """
        Transfer folders and attributes from the model dictionary to the target domain resource dictionary.
        For the top level, the folders and attributes are not derived directly from the location.
        :param model_dict: the source model dictionary
        :param folder_names: the names of the folders at this location
        :param attributes_map: the map of attribute names to types for this location
        :param location: the location used for alias processing
        :param target_dict: the target dictionary for the domain resource file.
       """
        for key, model_value in model_dict.items():
            if key in attributes_map.keys():
                type_name = attributes_map[key]
                target_dict[key] = _get_target_value(model_value, type_name)

            elif key in folder_names:
                child_location = LocationContext(location).append_location(key)

                if self._aliases.supports_multiple_mbean_instances(child_location):
                    target_dict[key] = self._build_dictionary_list(key, model_value, child_location)
                else:
                    if key not in target_dict:
                        target_dict[key] = PyOrderedDict()
                    target_child_dict = target_dict[key]
                    self._process_location_fields(model_value, child_location, target_child_dict)
        return

    def _process_location_fields(self, model_dict, location, target_dict):
        """
        Transfer folders and attributes from the model dictionary to the target domain resource dictionary.
        Below the top level, the folders and attributes can be derived from the location.
        :param model_dict: the source model dictionary
        :param location: the location used for alias processing
        :param target_dict: the target dictionary for the domain resource file.
       """
        attributes_map = self._aliases.get_model_attribute_names_and_types(location)
        folder_names = self._aliases.get_model_subfolder_names(location)
        self._process_fields(model_dict, folder_names, attributes_map, location, target_dict)
        return

    def _build_dictionary_list(self, model_key, name_dictionary, location):
        """
        Build a dictionary list object based on the name dictionary and location.
        :param name_dictionary: a dictionary containing named levels
        :param location: the location used for alias resolution
        :return:
        """
        child_list = DictionaryList()
        for name in name_dictionary:
            model_named_dict = name_dictionary[name]
            name_key = self._get_name_key(model_key)
            target_list_dict = PyOrderedDict()
            target_list_dict[name_key] = name
            self._process_location_fields(model_named_dict, location, target_list_dict)
            child_list.append(target_list_dict)
        return child_list

    def _get_name_key(self, key):
        """
        Return the key to be used for the name in a dictionary list element.
        :param key: the folder key in the model
        :return: the name key
        """
        key = dictionary_utils.get_element(self.NAME_KEY_MAP, key)
        if key is not None:
            return key
        return 'name'

    def _update_resource_dictionary(self, resource_dict):
        """
        Revise the resource file structure with values from defaults, command line, and elsewhere in model
        :param resource_dict: the resource file dictionary
        """
        _method_name = '_update_resource_dictionary'

        # add API version if not present
        if API_VERSION not in resource_dict:
            resource_dict[API_VERSION] = DEFAULT_API_VERSION

        # add kind if not present
        if KIND not in resource_dict:
            resource_dict[KIND] = DEFAULT_KIND

        # add a metadata section if not present, since we'll at least add name
        if METADATA not in resource_dict:
            resource_dict[METADATA] = PyOrderedDict()
        metadata_section = resource_dict[METADATA]

        # if metadata name not present, use the domain name from the model, or default
        if K_NAME not in metadata_section:
            domain_name = dictionary_utils.get_element(self._model.get_model_topology(), NAME)
            if domain_name is None:
                domain_name = DEFAULT_WLS_DOMAIN_NAME
            metadata_section[K_NAME] = domain_name

        # add a spec section if not present, since we'll at least add domain home
        if SPEC not in resource_dict:
            resource_dict[SPEC] = PyOrderedDict()
        spec_section = resource_dict[SPEC]

        # only set domain home if it is not present in spec section
        if DOMAIN_HOME not in spec_section:
            spec_section[DOMAIN_HOME] = self._model_context.get_domain_home()

        # only set image if it is not present in spec section
        if IMAGE not in spec_section:
            spec_section[IMAGE] = DEFAULT_IMAGE

        # imagePullSecrets is required unless imagePullPolicy is Never
        pull_secrets_required = True
        if IMAGE_PULL_POLICY in spec_section:
            policy = str(spec_section[IMAGE_PULL_POLICY])
            pull_secrets_required = (policy != NEVER)

        # if imagePullSecrets required and not present, add a list with one FIX ME value
        if pull_secrets_required and (IMAGE_PULL_SECRETS not in spec_section):
            secrets_list = DictionaryList()
            secrets_list.append({'name': DEFAULT_IMAGE_PULL_SECRETS})
            spec_section[IMAGE_PULL_SECRETS] = secrets_list

        # if webLogicCredentialsSecret not present, add it using the FIX ME value
        if WEBLOGIC_CREDENTIALS_SECRET not in spec_section:
            spec_section[WEBLOGIC_CREDENTIALS_SECRET] = DEFAULT_WEBLOGIC_CREDENTIALS_SECRET

        # only update clusters if section is not present in spec section
        if CLUSTERS not in spec_section:
            topology = self._model.get_model_topology()
            model_clusters = dictionary_utils.get_dictionary_element(topology, CLUSTER)
            if len(model_clusters) > 0:
                cluster_list = DictionaryList()
                spec_section[CLUSTERS] = cluster_list
                for cluster_name, cluster_values in model_clusters.items():
                    server_count = k8s_helper.get_server_count(cluster_name, cluster_values, self._model.get_model())
                    cluster_dict = PyOrderedDict()
                    cluster_dict[CLUSTER_NAME] = cluster_name
                    cluster_dict[REPLICAS] = server_count

                    self._logger.info("WLSDPLY-10002", cluster_name, server_count, method_name=_method_name,
                                      class_name=self._class_name)
                    cluster_list.append(cluster_dict)
        return

def _get_target_value(model_value, type_name):
    """
    Return the value for the specified attribute value, to be used in the domain resource file.
    :param model_value: the value to be checked
    :param type_name: the alias type name of the value
    :return: the formatted value
    """
    if type_name == BOOLEAN:
        # the model values can be true, false, 1, 0, etc.
        # target boolean values must be 'true' or 'false'
        return alias_utils.convert_to_type('boolean', model_value)

    return model_value


class Solution:
    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
        x1, y1 = coordinates[0]
        x2, y2 = coordinates[1]

        for i in coordinates[2:]:
            x, y = i

            if (x - x1) * (y2- y1) != (y - y1) * (x2 - x1):
                return False
        return True


from locust import HttpLocust, TaskSet, task

class UserBehavior(TaskSet):
    # runs one time for each user
    def on_start(self):
        self.client.get("/")

    @task(2)  # chance to run 2/3
    def posts(self):
        self.client.get("/posts")

    @task(1)  # chance to run 1/3
    def comment(self):
        data = {
            "postId": 1,
            "name": "my comment",
            "email": "test@user.test",
            "body": "Author is cool. Some text. Hello world!"
        }
        self.client.post("/comments", data)

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 1000
    max_wait = 2000


import logging
from itertools import chain
from typing import Tuple
from bdo_coupon_scanner.scanners.site_scanner import OfficialSiteScanner
from bdo_coupon_scanner.scanners.twitter_scanner import TwitterScanner
from time import perf_counter
from ..db import DatabaseTransaction
from ..db.coupons import Coupon

def remove_duplicates_by_key(selector, items):
    history = []
    for x in items:
        key = selector(x)
        seen = False
        for old_key in history:
            if key != old_key:
                continue
            seen = True
        if seen:
            continue
        history.append(key)
        yield x

async def get_new_codes(save_to_db: bool) -> Tuple[list[Coupon], float]:
    log = logging.getLogger(__name__)
    start_t = perf_counter()

    codes = []
    try:
        site_codes = OfficialSiteScanner().get_codes()
        codes = chain(codes, site_codes)
        log.debug(f"Site codes: {site_codes}")
    except Exception:
        log.error("Could not get site codes!")

    try:
        log.debug(f"Code chain before 2 scan: {codes}")
        twitter_codes = TwitterScanner().get_codes()
        codes = chain(codes, twitter_codes)
        log.debug(f"Twitter codes: {twitter_codes}")
    except Exception:
        log.error("Could not get twitter codes!")

    codes = remove_duplicates_by_key(lambda x: x.code, codes)

    delta_codes = []
    with DatabaseTransaction() as db:
        existing_codes = list(
            db.coupons.get_all()
        )  # Must be a list as an iterator would get exhausted on first pass.
        for new_code in codes:
            exists = False
            for old_code in existing_codes:
                if new_code.code != old_code.code:
                    continue
                exists = True
                break
            if not exists:
                if save_to_db:
                    db.coupons.add(new_code)
                delta_codes.append(new_code)
    end_t = perf_counter()
    elapsed_s = round(end_t - start_t, 2)
    return delta_codes, elapsed_s


def calc(x,y,opr):
    expression = x + opr + y
    print(expression)

print("""
1. Add
2. Sub
3. Div
4. Mul
""")

choice = int(input("Enter your choice : 1/2/3/4 : "))
fnum = input("Enter first number : ")
snum = input("Enter second number : ")

operations = {
    1 : "+",
    2 : "-",
    3 : "/",
    4 : "*"
}
opr = operations.get(choice)
calc(fnum, snum, opr)


import scipy.misc
import numpy as np
import SimpleITK as sitk
from prepare.utility import get_segmented_lungs, get_augmented_cube
from configs import RESOURCES_PATH, OUTPUT_PATH
from glob import glob
from skimage.measure import regionprops

class CTScan(object):
    def __init__(self, seriesuid, centers, radii, clazz):
        self._seriesuid = seriesuid
        self._centers = centers
        paths = glob(f'''{RESOURCES_PATH}/*/{self._seriesuid}.mhd''')
        path = paths[0]
        self._ds = sitk.ReadImage(path)
        self._spacing = np.array(list(reversed(self._ds.GetSpacing())))
        self._origin = np.array(list(reversed(self._ds.GetOrigin())))
        self._image = sitk.GetArrayFromImage(self._ds)
        self._radii = radii
        self._clazz = clazz
        self._mask = None

    def preprocess(self):
        self._resample()
        self._segment_lung_from_ct_scan()
        self._normalize()
        self._zero_center()
        self._change_coords()

    def save_preprocessed_image(self):
        subdir = 'negatives' if self._clazz == 0 else 'positives'
        file_path = f'''preprocessed/{subdir}/{self._seriesuid}.npy'''
        np.save(f'{OUTPUT_PATH}/{file_path}', self._image)

    def get_info_dict(self):
        (min_z, min_y, min_x, max_z, max_y, max_x) = (None, None, None, None, None, None)
        for region in regionprops(self._mask):
            min_z, min_y, min_x, max_z, max_y, max_x = region.bbox
        assert (min_z, min_y, min_x, max_z, max_y, max_x) != (None, None, None, None, None, None)
        min_point = (min_z, min_y, min_x)
        max_point = (max_z, max_y, max_x)
        return {'seriesuid': self._seriesuid, 'radii': self._radii, 'centers': self._centers,
                'spacing': list(self._spacing), 'lungs_bounding_box': [min_point, max_point], 'class': self._clazz}

    def _resample(self):
        spacing = np.array(self._spacing, dtype=np.float32)
        new_spacing = [1, 1, 1]
        imgs = self._image
        new_shape = np.round(imgs.shape * spacing / new_spacing)
        true_spacing = spacing * imgs.shape / new_shape
        resize_factor = new_shape / imgs.shape
        imgs = scipy.ndimage.interpolation.zoom(imgs, resize_factor, mode='nearest')
        self._image = imgs
        self._spacing = true_spacing

    def _segment_lung_from_ct_scan(self):
        result_img = []
        result_mask = []
        for slicee in self._image:
            rimg, rmsk = get_segmented_lungs(slicee)
            result_img.append(rimg)
            result_mask.append(rmsk)
        self._image = np.asarray(result_img)
        self._mask = np.asarray(result_mask, dtype=int)

    def _world_to_voxel(self, worldCoord):
        stretchedVoxelCoord = np.absolute(np.array(worldCoord) - np.array(self._origin))
        voxelCoord = stretchedVoxelCoord / np.array(self._spacing)
        return voxelCoord.astype(int)

    def _get_world_to_voxel_coords(self, idx):
        return tuple(self._world_to_voxel(self._centers[idx]))

    def _get_voxel_coords(self):
        voxel_coords = [self._get_world_to_voxel_coords(j) for j in range(len(self._centers))]
        return voxel_coords

    def _change_coords(self):
        new_coords = self._get_voxel_coords()
        self._centers = new_coords

    def _normalize(self):
        MIN_BOUND = -1200
        MAX_BOUND = 600.
        self._image = (self._image - MIN_BOUND) / (MAX_BOUND - MIN_BOUND)
        self._image[self._image > 1] = 1.
        self._image[self._image < 0] = 0.
        self._image *= 255.

    def _zero_center(self):
        PIXEL_MEAN = 0.25 * 256
        self._image = self._image - PIXEL_MEAN

class PatchMaker(object):
    def __init__(self, seriesuid: str, coords: list, radii: list, spacing: list, lungs_bounding_box: list,
                 file_path: str,
                 clazz: int):
        self._seriesuid = seriesuid
        self._coords = coords
        self._spacing = spacing
        self._radii = radii
        self._image = np.load(file=f'{file_path}')
        self._clazz = clazz
        self._lungs_bounding_box = lungs_bounding_box

    def _get_augmented_patch(self, idx, rot_id=None):
        return get_augmented_cube(img=self._image, radii=self._radii, centers=self._coords,
                                  spacing=tuple(self._spacing), rot_id=rot_id, main_nodule_idx=idx,
                                  lungs_bounding_box=self._lungs_bounding_box)

    def get_augmented_patches(self):
        radii = self._radii
        list_of_dicts = []
        for i in range(len(self._coords)):
            times_to_sample = 1
            if radii[i] > 15.:
                times_to_sample = 2
            elif radii[i] > 20.:
                times_to_sample = 6
            for j in range(times_to_sample):
                rot_id = int((j / times_to_sample) * 24 + np.random.randint(0, int(24 / times_to_sample)))
                img, radii2, centers, lungs_bounding_box, spacing, existing_nodules_in_patch = \
                    self._get_augmented_patch(idx=i, rot_id=rot_id)
                existing_radii = [radii2[i] for i in existing_nodules_in_patch]
                existing_centers = [centers[i] for i in existing_nodules_in_patch]
                subdir = 'negatives' if self._clazz == 0 else 'positives'
                file_path = f'''augmented/{subdir}/{self._seriesuid}_{i}_{j}.npy'''
                list_of_dicts.append(
                    {'seriesuid': self._seriesuid, 'centers': existing_centers, 'sub_index': f'{i}_{j}',
                     'lungs_bounding_box': lungs_bounding_box, 'radii': existing_radii, 'class': self._clazz})
                np.save(f'{OUTPUT_PATH}/{file_path}', img)
        return list_of_dicts


import argparse
from dataclasses import dataclass

from azureml.core import Datastore, Workspace

@dataclass
class Args:
    config: str

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("-c", "--config", help="path to config json", default="config.json")

    args = parser.parse_args()
    return Args(
        config=args.config,
    )

def main():
    args = parse_args()
    ws = Workspace.from_config(_file_name=args.config)
    for x in ws.datastores:
        # ds: Union[Datastore, None] = ws.datastores.get(x)
        ds = Datastore.get(ws, x)
        if ds is not None:
            print(ds.name)

if __name__ == '__main__':
    main()


import VPM
import unittest

from utils import *

class TestCmdVostok(unittest.TestCase):

    def setUp(self):
        ctrl = ControlFile()
        ctrl.settings['Name'] = 'foo'

        self.testfolder = 'foo-test'

        hook_d = HookScript()
        hook_d.set_modes(HookScript.SUCCEED)

        make_vpm(self.testfolder, ctrl, hook_d)

        self.env = VPM.Environment('/tmp/vpm')
        self.p = VPM.Package(self.env)

        r, self.v, e = self.p.pack(self.testfolder, '.')

        self.vpmfile = self.v

    def tearDown(self):
        rm_rf(self.vpmfile)
        rm_rf(self.testfolder)

        rm_rf(self.env.install_root)

    def testGetHook(self):
        res, val, err = self.p.install(self.vpmfile)

        rc = self.p.get_hook('foo', 'start')

        test_result = (True, '/tmp/vpm/srv/web-apps/foo/info/hooks/start', None)
        self.assertEquals(rc, test_result)

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

#
# EOF


import time
from socket import *

serverName = '192.168.1.114'
serverPort = 8001
while(True):
	clientSocket = socket(AF_INET, SOCK_STREAM)
	clientSocket.connect((serverName, serverPort))
	data = 'client'
	clientSocket.send(data.encode())
	data = clientSocket.recv(1024)
	print(data.decode(), end='')
	print('\r')
	time.sleep(5)


from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseRedirect
from django.views.generic import TemplateView, ListView

from foundation.models import SiteUser
from friends.models import Subscription

class FriendsListView(LoginRequiredMixin, TemplateView):
    template_name = 'friends/friends_list.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        subscribers_s = Subscription.objects.filter(subscriptor_id=self.request.user.id)
        subscribers = []
        for sub in subscribers_s:
            subscribers.append(sub.subscriber)
        subscriptions_s = Subscription.objects.filter(subscriber_id=self.request.user.id)
        subscriptions = []
        for sub in subscriptions_s:
            subscriptions.append(sub.subscriptor)
        friends = []
        for u in SiteUser.objects.exclude(id=self.request.user.id):
            if subscriptions_s.filter(subscriptor_id=u.id).exists() & subscribers_s.filter(subscriber_id=u.id).exists():
                friends.append(u)
        context['friends'] = friends
        context['subscribers'] = list(set(subscribers) - set(friends))
        context['subscriptions'] = list(set(subscriptions) - set(friends))
        return context

@login_required
def subscribe(request, pk):
    Subscription.objects.create(subscriber=request.user, subscriptor_id=pk)
    next_page = request.POST.get('next', '/')
    return HttpResponseRedirect(next_page)

@login_required
def unsubscribe(request, pk):
    Subscription.objects.get(subscriber=request.user, subscriptor_id=pk).delete()
    next_page = request.POST.get('next', '/')
    return HttpResponseRedirect(next_page)


import torch
import torch.optim as optim
import pytest
import helpers
import poptorch

#  Test the reductions work as expected
@pytest.mark.parametrize("reduction", ["none", "mean", "sum"])
def test_non_final_loss_reductions(reduction):
    torch.manual_seed(42)

    base_model = torch.nn.Linear(10, 10)

    class CustomLoss(torch.nn.Module):
        # Mean squared error scaled.
        def forward(self, x, target):
            partial_loss = poptorch.identity_loss(x - target,
                                                  reduction=reduction)
            loss = partial_loss * partial_loss * 5
            return partial_loss, poptorch.identity_loss(loss, reduction="mean")

    loss_fn = CustomLoss()

    class ModelWithLoss(torch.nn.Module):
        def __init__(self):
            super().__init__()
            self.base_model = base_model

        def forward(self, data, target):
            out = base_model(data)
            loss = loss_fn(out, target)
            return out, loss

    model = ModelWithLoss()
    poptorch_model = poptorch.trainingModel(model)

    target = torch.randn(10)
    input = torch.randn(10)

    # Capture what the loss function will see before the loss changes.
    x, _ = model(input, target)
    _, (partial_loss, _) = poptorch_model(input, target)

    # Check we have actually reduced the loss
    if reduction != "none":
        assert torch.numel(partial_loss) == 1

    if reduction == "mean":
        simulated_loss = torch.mean(x - target)
    elif reduction == "sum":
        simulated_loss = torch.sum(x - target)
    elif reduction == "none":
        simulated_loss = x - target

    helpers.assert_allclose(expected=simulated_loss.reshape_as(partial_loss),
                            actual=partial_loss,
                            rtol=1e-02,
                            atol=1e-02)

# Test custom loss by training to targets
def run_custom_loss_test(loss_fn,
                         base_model=None,
                         input=None,
                         target=None,
                         test_output_vs_target=True):
    torch.manual_seed(42)

    if base_model is None:
        base_model = torch.nn.Linear(10, 10)
    if input is None:
        input = torch.randn(1, 10)
    if target is None:
        target = torch.randint(0, 10, [1])

    class ModelWithLoss(torch.nn.Module):
        def __init__(self):
            super().__init__()
            self.base_model = base_model
            self.loss_fn = loss_fn

        def forward(self, data, target):
            out = base_model(data)
            loss = self.loss_fn(out, target)
            return out, loss

    model = ModelWithLoss()
    poptorch_model = poptorch.trainingModel(model)

    # Pytorch native.
    native_out, loss = model(input, target)

    #Make sure the first run doesn't already pass the test.
    original, original_loss = poptorch_model(input, target)

    assert original_loss > 0.1

    if test_output_vs_target:
        assert not torch.allclose(native_out, target, rtol=1e-02, atol=1e-02)
        assert not torch.allclose(original, target, rtol=1e-02, atol=1e-02)

    for _ in range(0, 2500):
        out, loss = poptorch_model(input, target)

    # Check we have trained the "model"
    assert loss < 0.1

    if test_output_vs_target:
        helpers.assert_allclose(actual=out,
                                expected=target,
                                rtol=1e-02,
                                atol=1e-02)

        # Check that the pytorch native model is also returning the trained
        # value that was trained on IPU.
        out, _ = model(input, target)
        helpers.assert_allclose(actual=out,
                                expected=target,
                                rtol=1e-02,
                                atol=1e-02)

    return poptorch_model

def test_custom_loss():
    torch.manual_seed(42)

    class CustomLoss(torch.nn.Module):
        # Mean squared error scaled.
        def forward(self, x, target):
            loss = poptorch.identity_loss(x - target, reduction="none")
            loss = loss * loss * 5.0
            return poptorch.identity_loss(loss, reduction="mean")

    run_custom_loss_test(loss_fn=CustomLoss(),
                         input=torch.randn(10),
                         target=torch.randn(10))

def test_custom_loss_l1():
    torch.manual_seed(42)

    class CustomLoss(torch.nn.Module):
        # Mean squared error scaled.
        def forward(self, x, target):
            loss = torch.nn.functional.l1_loss(x, target)
            loss = loss * loss * 5.0
            return poptorch.identity_loss(loss, reduction="mean")

    run_custom_loss_test(loss_fn=CustomLoss(),
                         input=torch.randn(10),
                         target=torch.randn(10))

def test_custom_loss_nll():
    torch.manual_seed(42)

    class CustomLoss(torch.nn.Module):
        # Mean squared error scaled.
        def forward(self, x, target):
            loss = torch.nn.functional.nll_loss(x, target)
            loss = loss * 5.0
            return poptorch.identity_loss(loss, reduction="mean")

    base_model = torch.nn.Sequential(torch.nn.Linear(10, 10),
                                     torch.nn.LogSoftmax(dim=1))

    input = torch.randn(1, 10)
    target = torch.randint(0, 10, [1])

    out = base_model(input)

    model = run_custom_loss_test(loss_fn=CustomLoss(),
                                 base_model=base_model,
                                 input=input,
                                 target=target,
                                 test_output_vs_target=False)
    model.copyWeightsToHost()

    # Check that the pytorch native model is also returning the trained
    # value that was trained on IPU.
    out = base_model(input)

    assert torch.argmax(out, dim=1) == target

def test_two_custom_losses():
    torch.manual_seed(42)

    base_model = torch.nn.Sequential(torch.nn.Linear(10, 10),
                                     torch.nn.LogSoftmax(dim=1))

    class CustomLoss(torch.nn.Module):
        # Mean squared error scaled.
        def forward(self, x, target):
            loss = torch.nn.functional.nll_loss(x, target)
            loss2 = torch.nn.functional.nll_loss(x, target) * 5.0
            return loss + loss2

    error_msg = ("Multiple independent losses found in graph. "
                 "Graph must have one final loss. "
                 "Wrap final graph loss in poptorch.identity_loss.")
    with pytest.raises(poptorch.Error, match=error_msg):
        run_custom_loss_test(loss_fn=CustomLoss(), base_model=base_model)

def test_two_custom_losses_with_id_wrapper():
    torch.manual_seed(42)

    base_model = torch.nn.Sequential(torch.nn.Linear(10, 10),
                                     torch.nn.LogSoftmax(dim=1))

    class CustomLoss(torch.nn.Module):
        # Mean squared error scaled.
        def forward(self, x, target):
            loss = torch.nn.functional.nll_loss(x, target)
            loss2 = torch.nn.functional.nll_loss(x, target) * 5.0
            return poptorch.identity_loss(loss + loss2, reduction="mean")

    run_custom_loss_test(loss_fn=CustomLoss(),
                         base_model=base_model,
                         test_output_vs_target=False)

def test_no_loss():
    torch.manual_seed(42)

    class Model(torch.nn.Module):
        def __init__(self):
            super().__init__()
            self.model = torch.nn.Sequential(torch.nn.Linear(10, 10),
                                             torch.nn.LogSoftmax(dim=1))

        # Mean squared error scaled.
        def forward(self, x, target):
            fwd = self.model(x)
            loss = fwd * 12
            loss2 = target + 1
            a = loss + loss2
            return fwd, a, loss

    model = Model()
    optimizer = optim.SGD(model.parameters(), lr=0.01)

    poptorch_model = poptorch.trainingModel(model, optimizer=optimizer)

    label = torch.randint(0, 10, [1])
    input = torch.randn(1, 10)

    error_msg = "Couldn't find a loss in graph"
    with pytest.raises(poptorch.Error, match=error_msg):
        _ = poptorch_model(input, label)


import tensorflow as tf
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.python.keras import regularizers

layers = tf.keras.layers

SAVE_PATH = "keras_model_files\SemEval\\emoji_to_trinary.h5"

L2_RATIO = 0.000001
LEARNING_RATE = 0.0001
PATIENCE_RATIO = 0.05

# best acheived 66.2% accuracy on SemEval with 0.0001 L2_RATIO, LEARNING_RATE=0.00001

class MojiToTrinary:
    def build_model(self):
        self.model = tf.keras.Sequential()
        self.model.add(
            layers.Dense(128,input_shape=(64, ), activation='tanh',kernel_regularizer=regularizers.l2(L2_RATIO),
                activity_regularizer=regularizers.l1(L2_RATIO)))
        self.model.add(
            layers.Dense(128, activation='tanh',kernel_regularizer=regularizers.l2(L2_RATIO),
                activity_regularizer=regularizers.l1(L2_RATIO)))
        self.model.add(
            layers.Dense(128, activation='tanh',kernel_regularizer=regularizers.l2(L2_RATIO),
                activity_regularizer=regularizers.l1(L2_RATIO)))
        self.model.add(
            layers.Dense(128, activation='tanh', kernel_regularizer=regularizers.l2(L2_RATIO),
                activity_regularizer=regularizers.l1(L2_RATIO)))
        self.model.add(
            layers.Dense(128, activation='tanh', kernel_regularizer=regularizers.l2(L2_RATIO),
                activity_regularizer=regularizers.l1(L2_RATIO)))
        self.model.add(
            layers.Dense(3, activation='softmax',kernel_regularizer=regularizers.l2(L2_RATIO),
                activity_regularizer=regularizers.l1(L2_RATIO)))
        self.model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=LEARNING_RATE),
                           loss='categorical_crossentropy',
                           metrics=['accuracy'])
        return

    def __init__(self):
        self.build_model()
        self.stopper_1 = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=PATIENCE_RATIO/LEARNING_RATE)
        # self.stopper_2 = EarlyStopping(monitor='val_acc', mode='min', verbose=1, patience=200)
        self.checkpointer = ModelCheckpoint(SAVE_PATH, monitor='val_acc', mode='max', save_best_only=True)

    def __call__(self):
        return self.model

    def train(self, train_ins, train_outs, test_ins, test_outs, max_epochs=4000):
        history = self().fit(train_ins, train_outs, validation_data=(test_ins, test_outs), epochs=max_epochs, verbose=1,
                             callbacks=[self.stopper_1, self.checkpointer], batch_size=6021)
        return history


# Import the required Libraries
from tkinter import *
from tkinter import filedialog
import os

#Create an instance of tkinter frame
win= Tk()

#Set the geometry for the window or frame
win.geometry("600x400")

#Define a function to open the application

def app():
   file= filedialog.askopenfilename()
   text.config(text= file)
   #Open the program
   os.system('"%s"' %file)

#Create a button
Button(win, text='Click to Open a Program',font=('Poppins bold', 10),
command=app).pack(pady=20)

#Create a Label after button event
text= Label(win, text= "", font= ('Poppins bold', 10))
text.pack(pady=20)

#Keep running the window or frame
win.mainloop()


# coding: utf-8

# In[ ]:

# 下载气象数据
get_ipython().system('wget -nc http://labfile.oss.aliyuncs.com/courses/780/WeatherData.zip')

# 安装 unzip 解压缩
get_ipython().system('apt-get install unzip')

# 解压缩
get_ipython().system('unzip -o WeatherData.zip')

import numpy as np
import pandas as pd
import datetime

df_ferrara = pd.read_csv('WeatherData/ferrara_270615.csv')
df_milano = pd.read_csv('WeatherData/milano_270615.csv')
df_mantova = pd.read_csv('WeatherData/mantova_270615.csv')
df_ravenna = pd.read_csv('WeatherData/ravenna_270615.csv')
df_torino = pd.read_csv('WeatherData/torino_270615.csv')
df_asti = pd.read_csv('WeatherData/asti_270615.csv')
df_bologna = pd.read_csv('WeatherData/bologna_270615.csv')
df_piacenza = pd.read_csv('WeatherData/piacenza_270615.csv')
df_cesena = pd.read_csv('WeatherData/cesena_270615.csv')
df_faenza = pd.read_csv('WeatherData/faenza_270615.csv')

get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from dateutil import parser

# 取出我们要分析的温度和日期数据
y1 = df_milano['temp']
x1 = df_milano['day']

# 把日期数据转换成 datetime 的格式
day_milano = [parser.parse(x) for x in x1]

# 调用 subplot 函数, fig 是图像对象，ax 是坐标轴对象
fig, ax = plt.subplots()

# 调整x轴坐标刻度，使其旋转70度，方便查看
plt.xticks(rotation=70)

# 设定时间的格式
hours = mdates.DateFormatter('%H:%M')

# 设定X轴显示的格式
ax.xaxis.set_major_formatter(hours)

# 画出图像，day_milano是X轴数据，y1是Y轴数据，‘r’代表的是'red' 红色
ax.plot(day_milano ,y1, 'r')

# 读取温度和日期数据
y1 = df_ravenna['temp']
x1 = df_ravenna['day']
y2 = df_faenza['temp']
x2 = df_faenza['day']
y3 = df_cesena['temp']
x3 = df_cesena['day']
y4 = df_milano['temp']
x4 = df_milano['day']
y5 = df_asti['temp']
x5 = df_asti['day']
y6 = df_torino['temp']
x6 = df_torino['day']

# 把日期从 string 类型转化为标准的 datetime 类型
day_ravenna = [parser.parse(x) for x in x1]
day_faenza = [parser.parse(x) for x in x2]
day_cesena = [parser.parse(x) for x in x3]
day_milano = [parser.parse(x) for x in x4]
day_asti = [parser.parse(x) for x in x5]
day_torino = [parser.parse(x) for x in x6]

# 调用 subplots() 函数，重新定义 fig, ax 变量
fig, ax = plt.subplots()
plt.xticks(rotation=70)

hours = mdates.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(hours)

#这里需要画出三根线，所以需要三组参数， 'g'代表'green'
ax.plot(day_ravenna,y1,'r',day_faenza,y2,'r',day_cesena,y3,'r')
ax.plot(day_milano,y4,'g',day_asti,y5,'g',day_torino,y6,'g')

# dist 是一个装城市距离海边距离的列表
dist = [df_ravenna['dist'][0],
    df_cesena['dist'][0],
    df_faenza['dist'][0],
    df_ferrara['dist'][0],
    df_bologna['dist'][0],
    df_mantova['dist'][0],
    df_piacenza['dist'][0],
    df_milano['dist'][0],
    df_asti['dist'][0],
    df_torino['dist'][0]
]

# temp_max 是一个存放每个城市最高温度的列表
temp_max = [df_ravenna['temp'].max(),
    df_cesena['temp'].max(),
    df_faenza['temp'].max(),
    df_ferrara['temp'].max(),
    df_bologna['temp'].max(),
    df_mantova['temp'].max(),
    df_piacenza['temp'].max(),
    df_milano['temp'].max(),
    df_asti['temp'].max(),
    df_torino['temp'].max()
]

# temp_min 是一个存放每个城市最低温度的列表
temp_min = [df_ravenna['temp'].min(),
    df_cesena['temp'].min(),
    df_faenza['temp'].min(),
    df_ferrara['temp'].min(),
    df_bologna['temp'].min(),
    df_mantova['temp'].min(),
    df_piacenza['temp'].min(),
    df_milano['temp'].min(),
    df_asti['temp'].min(),
    df_torino['temp'].min()
]
fig, ax = plt.subplots()
ax.plot(dist,temp_max,'ro')

from sklearn.svm import SVR

# dist1是靠近海的城市集合，dist2是远离海洋的城市集合
dist1 = dist[0:5]
dist2 = dist[5:10]

# 改变列表的结构，dist1现在是5个列表的集合
# 之后我们会看到 nbumpy 中 reshape() 函数也有同样的作用
dist1 = [[x] for x in dist1]
dist2 = [[x] for x in dist2]

# temp_max1 是 dist1 中城市的对应最高温度
temp_max1 = temp_max[0:5]
# temp_max2 是 dist2 中城市的对应最高温度
temp_max2 = temp_max[5:10]

# 我们调用SVR函数，在参数中规定了使用线性的拟合函数
# 并且把 C 设为1000来尽量拟合数据（因为不需要精确预测不用担心过拟合）
svr_lin1 = SVR(kernel='linear', C=1e3)
svr_lin2 = SVR(kernel='linear', C=1e3)

# 加入数据，进行拟合（这一步可能会跑很久，大概10多分钟，休息一下:) ）
svr_lin1.fit(dist1, temp_max1)
svr_lin2.fit(dist2, temp_max2)

# 关于 reshape 函数请看代码后面的详细讨论
xp1 = np.arange(10,100,10).reshape((9,1))
xp2 = np.arange(50,400,50).reshape((7,1))
yp1 = svr_lin1.predict(xp1)
yp2 = svr_lin2.predict(xp2)

# 限制了 x 轴的取值范围
fig, ax = plt.subplots()
ax.set_xlim(0,400)

# 画出图像
ax.plot(xp1, yp1, c='b', label='Strong sea effect')
ax.plot(xp2, yp2, c='g', label='Light sea effect')
ax.plot(dist,temp_max,'ro')

print(svr_lin1.coef_)  #斜率
print(svr_lin1.intercept_)  # 截距
print(svr_lin2.coef_)
print(svr_lin2.intercept_)

from scipy.optimize import fsolve

# 定义了第一条拟合直线
def line1(x):
    a1 = svr_lin1.coef_[0][0]
    b1 = svr_lin1.intercept_[0]
    return a1*x + b1

# 定义了第二条拟合直线
def line2(x):
    a2 = svr_lin2.coef_[0][0]
    b2 = svr_lin2.intercept_[0]
    return a2*x + b2

# 定义了找到两条直线的交点的 x 坐标的函数
def findIntersection(fun1,fun2,x0):
    return fsolve(lambda x : fun1(x) - fun2(x),x0)

result = findIntersection(line1,line2,0.0)
print("[x,y] = [ %d , %d ]" % (result,line1(result)))

# x = [0,10,20, ..., 300]
x = np.linspace(0,300,31)
plt.plot(x,line1(x),x,line2(x),result,line1(result),'ro')

# 读取湿度数据
y1 = df_ravenna['humidity']
x1 = df_ravenna['day']
y2 = df_faenza['humidity']
x2 = df_faenza['day']
y3 = df_cesena['humidity']
x3 = df_cesena['day']
y4 = df_milano['humidity']
x4 = df_milano['day']
y5 = df_asti['humidity']
x5 = df_asti['day']
y6 = df_torino['humidity']
x6 = df_torino['day']

# 重新定义 fig 和 ax 变量
fig, ax = plt.subplots()
plt.xticks(rotation=70)

# 把时间从 string 类型转化为标准的 datetime 类型
day_ravenna = [parser.parse(x) for x in x1]
day_faenza = [parser.parse(x) for x in x2]
day_cesena = [parser.parse(x) for x in x3]
day_milano = [parser.parse(x) for x in x4]
day_asti = [parser.parse(x) for x in x5]
day_torino = [parser.parse(x) for x in x6]

# 规定时间的表示方式
hours = mdates.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(hours)

#表示在图上
ax.plot(day_ravenna,y1,'r',day_faenza,y2,'r',day_cesena,y3,'r')
ax.plot(day_milano,y4,'g',day_asti,y5,'g',day_torino,y6,'g')

# 获取最大湿度数据
hum_max = [df_ravenna['humidity'].max(),
df_cesena['humidity'].max(),
df_faenza['humidity'].max(),
df_ferrara['humidity'].max(),
df_bologna['humidity'].max(),
df_mantova['humidity'].max(),
df_piacenza['humidity'].max(),
df_milano['humidity'].max(),
df_asti['humidity'].max(),
df_torino['humidity'].max()
]

plt.plot(dist,hum_max,'bo')

# 获取最小湿度
hum_min = [
df_ravenna['humidity'].min(),
df_cesena['humidity'].min(),
df_faenza['humidity'].min(),
df_ferrara['humidity'].min(),
df_bologna['humidity'].min(),
df_mantova['humidity'].min(),
df_piacenza['humidity'].min(),
df_milano['humidity'].min(),
df_asti['humidity'].min(),
df_torino['humidity'].min()
]
plt.plot(dist,hum_min,'bo')


import pytest
from unittest.mock import patch
import deadletter_watcher.datadog as datadog
import datetime

@patch('requests.get')
def test_validate(patched_get):
    # Arrange
    patched_get.return_value.status_code = 200
    secrets = {
        "DL-WATCHER": {
            "DD_API_KEY" : "0121-DO-1"
        }
    }

    # Act
    response = datadog.validate(secrets)

    # Assert
    assert patched_get.called is True
    assert response.status_code == 200

@patch('deadletter_watcher.datadog.__datadog_request_tenant_id')
@patch('deadletter_watcher.datadog.__datadog_request_emails')
def test_datadog_log_query_successful_request(patched_emails, patched_tenant):
    # Arrange
    message_id = "12345678-1234-1234-1234-12345678901234"
    secrets = {
        "DL-WATCHER": {
            "DD_API_KEY" : "0121-DO-1"
        }
    }
    now = datetime.datetime.now()

    # Arrange Mock
    patched_emails.return_value = {
        'statusCode': 200,
        'logs': [
            {
                "content": {
                    "attributes" : {
                        "sender_email": "sender@unittest.com",
                        "recipient_email": "recipient@unittest.com"
                    }
                }
            }
        ]
    }
    patched_tenant.return_value = {
        'statusCode': 200,
        'logs': [
            {
                "content": {
                    "timestamp": "unittest_time",
                    "attributes" : {
                        "tenant_name": "unittest_tenant"
                    }
                }
            }
        ]
    }

    # Act
    deadletter_details = datadog.datadog_log_query(message_id, now, secrets)

    # Assert
    assert patched_emails.called is True
    assert patched_tenant.called is True
    assert deadletter_details['timestamp'] == "unittest_time" and \
            deadletter_details['tenant_name'] == "unittest_tenant" and \
            deadletter_details['sender_email'] == "sender@unittest.com" and \
            deadletter_details['recipient_email'] == "recipient@unittest.com"

@patch('deadletter_watcher.datadog.__datadog_request_tenant_id')
@patch('deadletter_watcher.datadog.__datadog_request_emails')
def test_datadog_log_query_email_details_not_found_in_logs(patched_emails, patched_tenant):
    # Arrange
    message_id = "12345678-1234-1234-1234-12345678901234"
    secrets = {
        "DL-WATCHER": {
            "DD_API_KEY" : "0121-DO-1"
        }
    }
    now = datetime.datetime.now()

    patched_emails.return_value = {
        # Empty logs
        'statusCode': 200,
        'logs': [],
    }

    # Act
    deadletter_details = datadog.datadog_log_query(message_id, now, secrets)

    error_message = "Cannot Find in DataDog Log"

    # Assert
    assert patched_emails.called is True
    assert patched_tenant.called is False
    assert deadletter_details['tenant_name'] == error_message
    assert deadletter_details['sender_email'] == error_message
    assert deadletter_details['recipient_email'] == error_message


# -*- coding: utf-8 -*-
"""
Created on Wed Mar 13 13:28:06 2019

@author: asuto
"""

from django.urls import path

from . import views
from . import views2

urlpatterns=[
        path("test/",views2.hello, name = "hello"),

        path("",views.index, name = "index"),
        #ログアウト処理
        path("logout/",views.logout, name = "logout"),

        #ホーム画面へ遷移
        path("home/", views.home, name="home"),

        #投稿処理
        path("home/upload/",views.upload, name = "upload"),

        #わっしょい処理
        path("home/good/<str:user_name>/<int:image_id>/<int:my_id>/",views.good, name = "good"),

        #タグ検索処理
        path("home/tagsearch/",views.tagSearch, name = "tag"),

        #他ユーザのマイページへジャンプ
        path("home/fripage/<int:friend_id>",views.mypage, name = "friend"),

        #マイページへジャンプ
        path("home/mypage/", views.mypage, name="mypage"),

        #フォロワーの追加
        path("home/add/",views.addFriend, name = "add"),

        #ユーザの検索
        path("home/search/",views.search, name = "search"),

        #プロフィール画像の登録
        path("home/samne/", views.samne, name="samne"),

        #自己紹介文の登録
        path("home/introduce/",views.introduce, name = "introduce"),

        #パスワードの変更

        #新規ユーザー登録

        #ユーザの削除

        #トレンドワード検索

]


import streamlit as st
import numpy as np
from PIL import Image
from detection.object_detection import detect_object
import cv2

def main():
    st.title("Employee Working or Not Working")

    img_array = upload_image_ui()
    # st.write(img_array.shape)
    # st.write(img_array.shape)

    # rgbImage = cv2.cvtColor(img_array, cv2.COLOR_RGBA2RGB)
    # img_array = rgbImage

    if isinstance(img_array, np.ndarray):
        image = detect_object(img_array)

        st.image(image, width = 412)
#         st.write(f"Person is {label}")

def upload_image_ui():
    uploaded_image = st.file_uploader("Please upload an image file", type=["png", "jpg", "jpeg"])

    if uploaded_image is not None:
        try:
            image = Image.open(uploaded_image)

        except Exception:
            st.error("Error: Invalid image")
        else:
            img_array = np.array(image)
            img_array = cv2.cvtColor(img_array, cv2.COLOR_RGBA2RGB)
            return img_array

if __name__ == '__main__':
    main()


# 10:40 -> 11:10
# GG
# 11:55 [Success]
K = int(int(input()))

move = []
cnt = 0

def hanoi(n, from_pos, to_pos, via):
    global cnt
    if n == 0:
        return
    hanoi(n-1, from_pos, via, to_pos)
    move.append([from_pos, to_pos])
    cnt += 1
    hanoi(n-1, via, to_pos, from_pos)

hanoi(K, 1, 3, 2)
print(cnt)
for step in move:
    print(step[0], step[1])


'''
二叉树的前序遍历
给你二叉树的根节点 root ，返回它节点值的 前序 遍历。
'''

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
from typing import List, Optional

class Solution:
    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        '''递归写法'''
        # def preorder(root:TreeNode):
        #     if root==None:
        #         return
        #     res.append(root.val) # 根节点在前
        #     preorder(root.left)
        #     preorder(root.right)
        # res=[]
        # preorder(root)
        # return res

        '''迭代写法
        也可以用迭代的方式实现方法一的递归函数，两种方式是等价的，
        区别在于递归的时候隐式地维护了一个栈，而我们在迭代的时候需要显式地将这个栈模拟出来，
        其余的实现与细节都相同'''
        ans=[]
        stack=[]
        stack.append(root)
        while stack:
            node=stack.pop()
            if node==None:
                continue
            ans.append(node.val)
            stack.append(node.right)
            stack.append(node.left)
        return ans

code/数据结构/二叉树/树的遍历/2022-04-05_1-二叉树的前序遍历.py


from enum import IntEnum
from functools import reduce
from common import io

def run():
    print("Part 1 result:", part1())
    print("Part 2 result:", part2())

def part1():
    return [x for x in follow_instructions(parse_input("day10/input.txt"))
        if x[1] and x[1][0][0] == 17 and x[1][1][0] == 61][0][0]

def part2():
    outputs = ([x for x in follow_instructions(parse_input("day10/input.txt"))][-1])[2]
    return reduce(lambda acc, x: (acc * x[0]), outputs[:3], 1)

def follow_instructions(instr):
    # Perform all one-time initialisation before the generator cycle
    bot_count = max([x[i] for i in [2, 4] if x[i+1] == DestType.Bot] for x in instr if x[0] == InstructionType.Give)[0] + 1
    out_count = max([x[i] for i in [2, 4] if x[i+1] == DestType.Output] for x in instr if x[0] == InstructionType.Give)[0] + 1

    bots = [[] for _ in range(bot_count)]
    outputs = [[] for _ in range(out_count)]
    dist = {x[1]: ((x[2], x[3]), (x[4], x[5])) for x in instr if x[0] == InstructionType.Give}

    for x in instr:
        if x[0] == InstructionType.Init:
            bots[x[1]].append(x[2])

    # Evaluation cycle
    eval = [i for (i, x) in enumerate(bots) if len(x) == 2]
    while eval:
        i = eval.pop()
        vals = sorted(bots[i])
        assign = dist[i]

        yield((i, [(vals[ix], assign[ix]) for ix in range(2)], outputs))  # Yield { bot, [(val0, tgt0), (val1, tgt1)], output }

        bots[i] = []
        for (i, x) in enumerate(assign):
            [bots, outputs][x[1] == DestType.Output][x[0]].append(vals[i])

        eval.extend([x[0] for x in assign if x[1] == DestType.Bot and len(bots[x[0]]) == 2])

    yield((None, None, outputs))

def parse_input(path):
    rows = [x.split() for x in io.read_file(path).splitlines(False)]
    return [
        (InstructionType.Init, int(x[5]), int(x[1])) if x[2] == 'goes'      # { bot, init-value }
        else (InstructionType.Give, int(x[1]),                              # { bot, give-low, type-low, give-high, type-high }
              int(x[6]), DestType.Output if x[5] == 'output' else DestType.Bot,
              int(x[11]), DestType.Output if x[10] == 'output' else DestType.Bot) if x[2] == 'gives'

        else "ERROR"
    for x in rows]

class InstructionType(IntEnum):
    Init = 0
    Give = 1

class DestType(IntEnum):
    Bot = 0
    Output = 1


# Programming exercise of Python Programming, Udacity.com
#LESSION 2

def cylinder_volume(height, radius):
    pi = 3.14159
    return height * pi * radius ** 2

volume = cylinder_volume(10, 3)
print(volume)

#this will calculate the week
def readable_timeline(days):
    weeks = days // 7
    remainder = days % 7
    return "{} weeks and {} days." .format(weeks, remainder)
print(readable_timeline(15))

#this will calculate months
def year_timeline(days):
    month = days // 30
    day_calculator = days % 30
    return "{}month(s) and {}day(s)" .format(month, day_calculator)
print(year_timeline(121))

how_many_snakes = 1
snake_string = """
Welcome to Python3!

             ____
            / . .\\
            \  ---<
             \  /
   __________/ /
-=:___________/

<3, kushal Atreya
"""
print(snake_string * how_many_snakes)

phone_balance = 7
bank_balance = 180


# -*- coding: utf-8 -*-
"""
Created on Wed Mar 27 12:57:16 2019

@author: 123456
"""
import numpy as np

from sklearn.metrics.pairwise import cosine_similarity

def file2matrix(filename):
    f = open(filename) # 打开文件
    dataSet = f.readlines() # 读取文件的全部内容
    numberOfLines = len(dataSet) # 获得数据集的行数
    data = np.zeros((numberOfLines, 3)) # 创建一个初始值为0，
                                             # 大小为 numberOfLines x 3 的数组
    label = [] # 用于保存没个数据的类别标签
    index = 0
    for line in dataSet: # 处理每一行数据
        line = line.strip() # 去掉行首尾的空白字符,(包括'\n', '\r', '\t', ' ')
        listFromLine = line.split() # 分割每行数据，保存到一个列表中
        data[index, :] = listFromLine[0:3] # 将列表中的特征保存到reurnMat中
        label.append(int(listFromLine[-1])) # 保存分类标签
        index += 1
    label = np.array(label)
    return data, label

data, label = file2matrix('dating/datingTestSet2.txt')

sims = cosine_similarity(data)

sorted_sims = np.argsort(-sims)  #np.sort(-sims)
print(sorted_sims)


class Solution(object):
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # DP
        # dp[i]: number of steps needed to jump from i to last index
        # dp[-1] = 0: already at last index, no need to jump
        # iterate from back to forth
        l = len(nums)
        if l <= 1:
            return 0

        dp = [10001]*l
        dp[-1] = 0

        for i in range(l-2, -1, -1):
            for j in range(1, nums[i]+1):
                if i + j <= l-1:
                    # j is step length
                    # i + j is the index that may reach
                    # dp[i+j] + 1 means jump from i to i+j needs one step, and from i + j to last index need dp[i+j] steps
                    dp[i] = min(dp[i], dp[i+j] + 1)
        return dp[0]


'''Вызов встроенной функ-
ции exec(open(‘module.py’).read()) – это еще один способ выполнять файлы из
интерактивной оболочки, фактически не импортируя модуль. Каждый по-
следующий вызов exec будет выполнять текущую версию файла и ликвиди-
рует необходимость позднее выполнять перезагрузку модуля'''
exec(open('list.py').read())

a=[1,2,3]
print(a.__class__.__module__)
obj=__import__( a.__class__.__module__)

di=obj.dict(((1,2),(3,4)))
print(di)

функция exec.py

функция exec.py


#https://www.youtube.com/watch?v=UbyhOgBN834&t=291s
class Solution:
    def checkInclusion(self, s1: str, s2: str) -> bool:
        if len(s1)>len(s2): return False
        def same_permutation(a, b):
            d = collections.defaultdict(int)
            for x in a:
                d[x] += 1
            for x in b:
                d[x] -= 1
            return not any(d.values())

        # return same_permutation(s1,s2[0:3])

        l = len(s1)
        set1 = set(s1)
        for i in range(len(s2)-l + 1):
            if s2[i] in set1:
                if same_permutation(s1, s2[i:i+l]):
                    return True
            else:
                i = i + l
        return False


import os, json, sys
import numpy as np
import pandas as pd
from numpy.random import randn
from numpy.random import seed
from numpy import mean
from numpy import var
from math import sqrt
import pickle
from scipy import stats
from scipy.stats import ttest_1samp, ttest_ind, ttest_rel
import statistics
import matplotlib.pyplot as plt
import logging, socket
import statsmodels.api as sm
from scipy.stats import mannwhitneyu
from statsmodels.sandbox.regression.predstd import wls_prediction_std

# class Stat_Group():
#################################################
# Logging configuration
#################################################
computername = socket.gethostname()
if computername == "BigBang":
    mstfile = "G:\\Unity\\MST_JSAM\\analyse_csvs\\Data_Rogens\\MST\\17_TimQueißertREST1fertig.csv"
    dirname = ".\\Data_Rogens\\Results"
if computername == "XenonBang":
    dirname = "G:\\Programming\\MST_JSAM\\analyse_csvs\\Data_Rogens\\Results"
if computername == "Laptop-LittleBang":
    dirname = "D:\\Programming\\MST_JSAM\\analyse_csvs\\Data_Rogens\\Results"

logfilename = os.path.join(dirname, "results.log")
logger_stat = logging.getLogger("")
logger_stat.setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s :: %(module)s :: %(levelname)s :: %(name)s :: %(message)s')
file_handler_stat = logging.FileHandler(logfilename, 'w+')
file_handler_stat.setLevel(logging.DEBUG)
file_handler_stat.setFormatter(formatter)

c_handler = logging.StreamHandler(sys.stdout)
c_handler.setLevel(logging.DEBUG)
c_handler.setFormatter(formatter)

logger_stat.addHandler(file_handler_stat)
logger_stat.addHandler(c_handler)
logger_stat.debug("entering stats ....")

class Statistic_Exp_Dir():
    """ takes an directory with multiple files and
        performes the statistik with the information in these files
        """
    def __init__(self, datadir, resultsdir, output_filename = "stats_output.txt"): #experiment_name = 'MST', groups = [], key = "cor_seqsum_lpn", level = "pn", paradigma = 0, is_independent = False):
        self.datadir = datadir
        self.resultsdir = resultsdir
        self.output_filename = output_filename
        self.all_exp = []
        self.groups = [] # a list of the groups... each group consists of a list of experiement classes
        self.print_output = []
        self.is_print_to_std = True
        self.first_write = True
        self.first_write
        # self.experiment_name = experiment_name
        # self.key = key
        # self.is_independent = is_independent
        # self.paradigma = paradigma
        # self.level = level
#        self.test_group_differences_ttest(key, self.is_independent, self.paradigma, self.level)
    def perform_all_available_analyses(self):
        self.read_exp_files_from_dir()
        self.create_groups()
        self.check_data_consistency()
        self.was_there_learning_in_each_experiment()
        self.estimate_q(is_q_fake_abs = True)
        self.estimate_phi()
        self.estimate_anova()

    def filter_experiments(self, experiment_names = [], days= [], vpns =[]):
        """ filter the experiments according to the given parameter
            return a list of the corresponding experiements"""
        exps = []
        for group in self.groups:
            for exp in group:
                is_exp = False
                is_day = False
                is_vpn = False
                if len(experiment_names) == 0:
                    is_exp = True
                else:
                    if exp.experiment_name in experiment_names:
                        is_exp = True
                if len(days) == 0:
                    is_day = True
                else:
                    if exp.day in days:
                        is_day = True
                if len(vpns) == 0:
                    is_vpn = True
                else:
                    if exp.vpn in vpns:
                        is_vpn = True
                if (is_exp and is_day and is_vpn):
                    exps.append(exp)

        return exps

    def estimate_anova(self):
        """ Wyombs 2012
        We collected three behavioral variables during training: the time between key presses (i.e.,
        the vector of inter-key intervals), movement time (MT), and error. MT is the time elapsed
        from the initial to final key press. Error was scored as any trial not produced in the correct
        order as well as those trials not completed within the 8 s time limit. To test for learning, we
        entered the MT data for each subject, sequence, and session into a repeated-measures
        ANOVA (with subject treated as a random factor). To test for differences in error over
        training, we combined error for each frequent sequence and entered them for each subject
        and session using a repeated-measures ANOVA. """
        self.myprint("performing an ANOVA")

    # def create_one_csv(self):
    #     """ creates one big csv for further analysis with R"""
    #     self.all_exp = []
    #     self.groups = []
    #     self.read_exp_files_from_dir()
    #     self.create_groups()
    #     self.check_data_consistency()
    #     # create a dataframe with all important variables
    #     columnnames = [
    #         'experiment_name',
    #         'vpn',                                         # die Versuchspersonennummer
    #         'day',                                                               # DER Trainingstag
    #         'paradigma',                                    # falls an einem Tag unterschiedliche Interventionen erfolgten (MST_21 vs. MST_22 vs. MST_23)
    #         'sequence_length',
    #         'filename',
    #         'root_dir',
    #         'data_dir',
    #         'is_delete_first',                            #! ein wesentlicher Unterschied ist noch, dass beim MST der erste ipi nicht geloescht wird
    #         'all_ipi_lsln',
    #         'cor_ipi_lsln',
    #         'err_ipi_lsln',
    #         'all_hits_lsln',
    #         'all_ipi_lblsln',
    #         'cor_ipi_lblsln',
    #         'err_ipi_lblsln',
    #         'all_hits_lblsln',
    #         'all_ipi_lplsln',
    #         'cor_ipi_lplsln',
    #         'err_ipi_lplsln',
    #         'all_hits_lplsln',
    #         'all_ipi_lplblsln',
    #         'cor_ipi_lplblsln',
    #         'err_ipi_lplblsln',
    #         'all_hits_lplblsln',
    #         'all_seqsum_lpn',
    #         'all_seqsum_lplbn',
    #         'all_seqtimesum_lplsn',
    #         'all_seqtimesum_lplblsn',
    #         'cor_seqsum_lpn',
    #         'cor_seqsum_lplbn',
    #         'cor_seqtimesum_lplsn',
    #         'cor_seqtimesum_lplblsn',
    #         'err_seqsum_lpn',
    #         'err_seqsum_lplbn',
    #         'err_seqtimesum_lplsn',
    #         'err_seqtimesum_lplblsn',
    #         'all_seqtimesum_slope_lpn',
    #         'all_seqtimesum_to_max_slope_lpn',
    #         'cor_seqtimesum_slope_lpn',
    #         'cor_seqtimesum_to_max_slope_lpn',
    #         'err_seqtimesum_slope_lpn',
    #         'err_seqtimesum_to_max_slope_lpn',
    #         'all_seqtimesum_per_block_slope_lpn',
    #         'all_seqtimesum_per_block_to_max_slope_lpn',
    #         'cor_seqtimesum_per_block_slope_lpn',
    #         'cor_seqtimesum_per_block_to_max_slope_lpn',
    #         'err_seqtimesum_per_block_slope_lpn',
    #         'err_seqtimesum_per_block_to_max_slope_lpn',
    #         'all_seqnum_per_block_slope_lpn',
    #         'cor_seqnum_per_block_slope_lpn',
    #         'err_seqnum_per_block_slope_lpn',
    #         'net_A',
    #         'net_C',
    #         'net_c',
    #         'net_ipi',
    #         'net_is_estimate_clustering',
    #         'net_k',
    #         'net_kappa',
    #         'net_m',
    #         'net_my2',
    #         'net_phi',
    #         'net_phi_real',
    #         'net_phi_fake_list',
    #         'net_q_real',
    #         'net_q_real_t',
    #         'net_q_real_p',
    #         'net_q_fake_list',
    #         'net_g_real',
    #         'net_g_fake_list',
    #         'net_is_adapt_communities_across_trials',
    #         'net_is_estimate_Q',
    #         'net_num_random_Q',
    #         'net_resolution_parameter'
    #     ]

    #     columnnames = [
    #         'experiment_name',
    #         'vpn',                                         # die Versuchspersonennummer
    #         'day',                                                               # DER Trainingstag
    #         'paradigma',                                    # falls an einem Tag unterschiedliche Interventionen erfolgten (MST_21 vs. MST_22 vs. MST_23)
    #         'sequence_length',
    #         'filename',
    #         'root_dir',
    #         'data_dir',
    #         'is_delete_first',                            #! ein wesentlicher Unterschied ist noch, dass beim MST der erste ipi nicht geloescht wird
    #         'all_ipi_lsln',
    #         'cor_ipi_lsln',
    #         'err_ipi_lsln',
    #         'all_hits_lsln',
    #         'all_ipi_lblsln',
    #         'cor_ipi_lblsln',
    #         'err_ipi_lblsln',
    #         'all_hits_lblsln',
    #         'all_ipi_lplsln',
    #         'cor_ipi_lplsln',
    #         'err_ipi_lplsln',
    #         'all_hits_lplsln',
    #         'all_ipi_lplblsln',
    #         'cor_ipi_lplblsln',
    #         'err_ipi_lplblsln',
    #         'all_hits_lplblsln',
    #         'all_seqsum_lpn',
    #         'all_seqsum_lplbn',
    #         'all_seqtimesum_lplsn',
    #         'all_seqtimesum_lplblsn',
    #         'cor_seqsum_lpn',
    #         'cor_seqsum_lplbn',
    #         'cor_seqtimesum_lplsn',
    #         'cor_seqtimesum_lplblsn',
    #         'err_seqsum_lpn',
    #         'err_seqsum_lplbn',
    #         'err_seqtimesum_lplsn',
    #         'err_seqtimesum_lplblsn',
    #         'all_seqtimesum_slope_lpn',
    #         'all_seqtimesum_to_max_slope_lpn',
    #         'cor_seqtimesum_slope_lpn',
    #         'cor_seqtimesum_to_max_slope_lpn',
    #         'err_seqtimesum_slope_lpn',
    #         'err_seqtimesum_to_max_slope_lpn',
    #         'all_seqtimesum_per_block_slope_lpn',
    #         'all_seqtimesum_per_block_to_max_slope_lpn',
    #         'cor_seqtimesum_per_block_slope_lpn',
    #         'cor_seqtimesum_per_block_to_max_slope_lpn',
    #         'err_seqtimesum_per_block_slope_lpn',
    #         'err_seqtimesum_per_block_to_max_slope_lpn',
    #         'all_seqnum_per_block_slope_lpn',
    #         'cor_seqnum_per_block_slope_lpn',
    #         'err_seqnum_per_block_slope_lpn',
    #         'net_A',
    #         'net_C',
    #         'net_c',
    #         'net_ipi',
    #         'net_is_estimate_clustering',
    #         'net_k',
    #         'net_kappa',
    #         'net_m',
    #         'net_my2',
    #         'net_phi',
    #         'net_phi_real',
    #         'net_phi_fake_list',
    #         'net_q_real',
    #         'net_q_real_t',
    #         'net_q_real_p',
    #         'net_q_fake_list',
    #         'net_g_real',
    #         'net_g_fake_list',
    #         'net_is_adapt_communities_across_trials',
    #         'net_is_estimate_Q',
    #         'net_num_random_Q',
    #         'net_resolution_parameter'
    #     ]
    #     df = pd.DataFrame(columns=columnnames)
    #     # perform an loop to fill all experiments to it
    #     for i in range(5):
    #         df.loc[i, 'lib'] = 'name' + str(i)#

    def read_exp_files_from_dir(self):
        """ read all files from one directory and append them to self.all_exp
        """
        files = [os.path.join(self.datadir, f) for f in os.listdir(self.datadir) if os.path.isfile(os.path.join(self.datadir, f))]
        for file in files:
            with open(file, 'rb') as fp:
                self.all_exp.append(pickle.load(fp))

    def create_groups(self):
        """ group the experiment data according to the performed experiment
        """
        # estimate the number of group
        self.experiment_names = []
        for exp in self.all_exp:
            if not(exp.experiment_name in self.experiment_names):
                self.experiment_names.append(exp.experiment_name)
        self.num_groups = len(self.experiment_names)

        for i in range(self.num_groups):
            self.groups.append([])
        for exp in self.all_exp:
            g = self.groups[self.experiment_names.index(exp.experiment_name)]
            g.append(exp)

        self.myprint(f"found {self.num_groups} groups in the given directory")
        #logger_stat.debug(tmp)
        for gr in range(self.num_groups):
            self.myprint(f"Group {gr} with name = {self.experiment_names[gr]} ({len(self.groups[gr])} files)")
            exp = self.groups[gr][0]
            self.myprint(f"number of Paradigma: {len(exp.cor_seqsum_lpn)}")
            self.myprint(f"... with number of blocks:", end=" ")
            for i in range(len(exp.cor_seqsum_lpn)):
                self.myprint(f"{len(exp.all_seqsum_lplbn[i])}", end=" ")
            self.myprint(f" ")

    def check_data_consistency(self):
        """ checking that all experiment of a given type have the same number
             paradigma and blocks
        """

        #logger_stat.debug(tmp)
        self.myprint("checking dataconsistency ...")
        self.myprint("checking that every experiment file of a given type and day has the same number of paradigma")
        for gr in range(self.num_groups):
            subj_counter = 0
            self.myprint("____________________________________")
            self.myprint(f"analysing Group {self.experiment_names[gr]}")
            for exp in self.groups[gr]:
                num_p = len(exp.cor_seqsum_lpn)
                num_b = []
                for i in range(num_p):
                    num_b.append(exp.all_seqsum_lplbn[i])
                if subj_counter>0:

                    if (not(num_p==num_p_old) or not(num_b==num_b_old)):
                        # in SEQ and SRTT there are always the same number of sequences per block
                        if (self.experiment_names[gr]=="SEQ" or self.experiment_names[gr]=="SRTT"):
                            self.myprint("DIFFERENCE")
                        if (self.experiment_names[gr]=="MST"):
                            if not(len(num_b)==len(num_b_old)):
                                self.myprint("DIFFERENCE")
                        #self.myprint(f"difference detected in group{gr} subj{subj_counter}  num_p = {num_p} num_bloecke = {num_b} .... to")
                        #self.myprint(f"difference detected in group{gr} subj{subj_counter-1}  num_p = {num_p_old} num_bloecke = {num_b_old}")
                else:
                    num_p_old = num_p
                    num_b_old = num_b
                self.myprint(f"subj{subj_counter: >4} file={exp.filename: >10} num_p = {num_p} num_bloecke = {num_b:} .... to")

                subj_counter +=1

        self.myprint("checking dataconsistency finished")
        self.myprint("______________________________________________")

    def myprint(self, mystring, end = "\n"):
        if not(end=="\n"):
            mystring.strip("\n")
        if self.is_print_to_std:
            print(mystring, end = end)

        # try:
        #     last_char = self.print_output[-1][-1]
        # except:
        #     last_char = "\n"
        # if last_char == "\n":
        self.print_output.append(mystring + end)
        # else:
        #     self.print_output[-1] = self.print_output[-1] + mystring + end

        if self.first_write:
            with open(os.path.join(self.resultsdir, self.output_filename), "w") as f:
                f.write(mystring)
            self.first_write = False
        else:

            with open(os.path.join(self.resultsdir, self.output_filename), "a") as f:
                f.write(mystring)
                f.write(end)

    """We collected three behavioral variables during training: the time between key presses (i.e.,
    the vector of inter-key intervals), movement time (MT), and error. MT is the time elapsed
    from the initial to final key press. Error was scored as any trial not produced in the correct
    order as well as those trials not completed within the 8 s time limit. To test for learning, we
    entered the MT data for each subject, sequence, and session into a repeated-measures
    ANOVA (with subject treated as a random factor). To test for differences in error over
    training, we combined error for each frequent sequence and entered them for each subject
    and session using a repeated-measures ANOVA. For all statistical tests, we set a probability
    threshold of P < 0.05 for the rejection of the null hypothesis.
        x #      phi
                    phi_real':             self.phi_real,
                    phi_fake_list
        n #     'phi_real_slope':       phi_real_slope,
        y #     'q_real':               self.q_real,
        y #     'q_real_t':             self.q_real_t,
        y #     'q_real_p':             self.q_real_p,
        y #     'q_fake_list':          self.q_fake_list,
        n #     'q_fake_list_mean':     sum(self.q_fake_list)/len(self.q_fake_list),
        y #     'g_real':               self.tolist_ck(self.g_real),
        y #     'g_fake_list':          self.tolist_ck(self.g_fake_list), # arrays verschachtelt in einer Liste
        y #     'A':                    self.A.tolist()
        C
        ipi
        k
        kappa
        m
        my2
            # }
    """

    def estimate_q(self, is_q_fake_abs = True):
        """ is_q_fake_abs whether the difference between shuffeled an real Q is
            used or whether the absolute value will be used
        """
        self.myprint(" ")
        self.myprint("__________________________________________________________")
        self.myprint("________________________Q_________________________________")
        self.myprint("__________________________________________________________")
        self.myprint("We quantified chunking within each sequence by the optimized modularity Qmulti–trial of the")
        self.myprint("sequence networks. Modularity in this case measures the separability between clusters of")
        self.myprint("IKIs. Higher values of Q indicate a greater ease in separating chunks.")
        self.myprint("___________MST_______")
        exp_list = self.filter_experiments(experiment_names=["MST"], days=[1], vpns=[])
        self.group_q(exp_list, "MST Day 1", q_fake_abs = is_q_fake_abs)
        exp_list = self.filter_experiments(experiment_names=["MST"], days=[2], vpns=[])
        self.group_q(exp_list, "MST Day 2", q_fake_abs = is_q_fake_abs)
        self.myprint("___________SRTT_______")
        exp_list = self.filter_experiments(experiment_names=["SRTT"], days=[1], vpns=[])
        self.group_q(exp_list, "SRTT Day 1", q_fake_abs = is_q_fake_abs)
        exp_list = self.filter_experiments(experiment_names=["SRTT"], days=[2], vpns=[])
        self.group_q(exp_list, "SRTT Day 2", q_fake_abs = is_q_fake_abs)
        self.myprint("___________SEQ_______")
        exp_list = self.filter_experiments(experiment_names=["SEQ"], days=[1], vpns=[])
        self.group_q(exp_list, "SEQ Day 1", q_fake_abs = is_q_fake_abs)
        exp_list = self.filter_experiments(experiment_names=["SEQ"], days=[2], vpns=[])
        self.group_q(exp_list, "SEQ Day 2", q_fake_abs = is_q_fake_abs)
        self.myprint("end estimation group q")

    def group_q(self,exp_list, desc, q_fake_abs = False):
        """ in case of q_fake_abs = True then the abs difference will be computed
            after z-score correction
            otherwise the simple difference will be used (this is not really correcte becaus different distributions)
        """
        if not exp_list:
            self.myprint("not experiments for {desc}")
            return
        subj_q = [] # a list
        subj_q_fake = []
        subj_q_fake_z = []
        subj_q_abs = []
        subj_q_z = []
        print(len(exp_list))
        for exp in exp_list:
            subj_q_abs.append(abs(exp.net.Q_list[0] - statistics.mean(exp.net.Q_list[1:])))
            subj_q.append(exp.net.Q_list[0])
            subj_q_fake.append(statistics.mean(exp.net.Q_list[1:]))
            stderr=statistics.stdev(exp.net.Q_list[1:])/sqrt(len(exp.net.Q_list[1:]))
            subj_q_z.append((exp.net.Q_list[0]-statistics.mean(exp.net.Q_list[1:]))/stderr)
            # fuege jeden normalisieren fake wert in Form eines Z Wertes nun ein
            for f in exp.net.Q_list[1:]:
                subj_q_fake_z.append((f-statistics.mean(exp.net.Q_list[1:]))/stderr)
        if q_fake_abs:
            self.myprint(f"Subject z-score for real Q ")
            self.myprint(f"{str(subj_q_z)}")
            self.myprint(f"with mean = {statistics.mean(subj_q_z)}")
            self.myprint(exp.net.print_Q_parts())
            G1 = [abs(elem) for elem in subj_q_z]
            G2 = [abs(elem) for elem in subj_q_fake_z]
            #G2 = abs(subj_q_fake_z)
            #t, p = stats.ttest_ind(G1, G2)
            m = [statistics.mean(G1), statistics.mean(G2)]
            std = [statistics.stdev(G1), statistics.stdev(G2)]
            #self.print_pt_2g(key=desc, t=t,p=p, mymean = m, std=std)
            # data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
            # data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075, -0.169]
            t, p = mannwhitneyu(G1, G2)

            self.print_pt_2g(key=desc, t=t,p=p, mymean = m, std=std)

#             G1 = np.array(abs(subj_q_z))
#             G2 = np.array(abs(subj_q_fake_z))
# #            G1 = np.array(subj_q_abs)
# #            G1 = subj_q_abs
# #            self.myprint(str(G1))
#             t, p = stats.ttest_1samp(G1,0.0)
#             m = G1.mean()
#             #m = statistics.mean(G1)
#             std = G1.std()
# #           std = statistics.stdev(G1)
#             self.print_pt_1g(desc, t,p, m, std)

        if not q_fake_abs:
            G1 = subj_q
            G2 = subj_q_fake
            t, p = stats.ttest_rel(G1, G2)
            m = [statistics.mean(G1), statistics.mean(G2)]
            std = [statistics.stdev(G1), statistics.stdev(G2)]
            self.print_pt_2g(key=desc, t=t,p=p, mymean = m, std=std)

    def estimate_phi(self):
        pass

    def was_there_learning_in_each_experiment(self):
        self.myprint("Question 1: Was there learning?")
        self.myprint("1.1. is there a linear increase in the number of correct sequences per block per paradigma (linear Regression)?")

        for group_idx in range(len(self.groups)):
            exp_name = self.experiment_names[group_idx]
            if exp_name == "MST":
                self.myprint("___________________________________________________")
                self.myprint("START____MST______________________________________")
                self.myprint("MST")
                self.myprint("1.1. is there a linear increase in the number of correct sequences per block per paradigma (linear Regression)?")
                self.myprint(".... for day 1...")
                exp_list = self.filter_experiments(experiment_names=["MST"], days=[1], vpns=[])
                self.estimate_linear_regression(exp_list, "cor_seqsum_lplbn", 0, mean_last_dim = False, num_blocks=10)
                self.myprint(".... for day 2...")
                exp_list = self.filter_experiments(experiment_names=["MST"], days=[2], vpns=[])
                self.estimate_linear_regression(exp_list, "cor_seqsum_lplbn", 0, mean_last_dim = False, num_blocks=10)
                self.myprint("1.1. is there a linear decrease in the Time to perform an sequence per block per paradigma (linear Regression)?")
                self.myprint(".... for day 1...")
                exp_list = self.filter_experiments(experiment_names=["MST"], days=[1], vpns=[])
                self.estimate_linear_regression(exp_list, "cor_seqtimesum_lplblsn", 0, mean_last_dim = True, num_blocks=10)
                self.myprint(".... for day 2...")
                exp_list = self.filter_experiments(experiment_names=["MST"], days=[2], vpns=[])
                self.estimate_linear_regression(exp_list, "cor_seqtimesum_lplblsn", 0, mean_last_dim = True, num_blocks=10)
                self.myprint("now use the slope of each subject and perform a t-test for the group")
                exp_list = self.filter_experiments(experiment_names=["MST"], days=[1], vpns=[])
                self.ttest_one_group(exp_list, "cor_seqtimesum_slope_lpn", 0)
                exp_list = self.filter_experiments(experiment_names=["MST"], days=[2], vpns=[])
                self.ttest_one_group(exp_list, "cor_seqtimesum_slope_lpn", 0)
                self.myprint("END____MST______________________________________")
                self.myprint("___________________________________________________")
                self.myprint(" ")
            if exp_name == "SEQ":
                self.myprint("___________________________________________________")
                self.myprint("START____SEQ______________________________________")
                self.myprint("SEQ")
                # self.myprint("1.1. is there a linear decrease in the number of correct sequences per block per paradigma (linear Regression)?")
                # self.myprint(".... for day 1...")
                # exp_list = self.filter_experiments(experiment_names=["SEQ"], days=[1], vpns=[])
                # self.estimate_linear_regression(exp_list, "cor_seqsum_lplbn", 0)
                # self.myprint(".... for day 2...")
                # exp_list = self.filter_experiments(experiment_names=["SEQ"], days=[2], vpns=[])
                # self.estimate_linear_regression(exp_list, "cor_seqsum_lplbn", 0)
                self.myprint("1.1. is there a linear decrease in the Time to perform an sequence per block per paradigma (linear Regression)?")
                self.myprint(".... for day 1...")
                exp_list = self.filter_experiments(experiment_names=["SEQ"], days=[1], vpns=[])
                print([e.filename for e in exp_list])
                self.estimate_linear_regression(exp_list, "cor_seqtimesum_lplblsn", 0, mean_last_dim = True, num_blocks=10)
                self.myprint(".... for day 2...")
                exp_list = self.filter_experiments(experiment_names=["SEQ"], days=[2], vpns=[])
                print([e.filename for e in exp_list])
                self.estimate_linear_regression(exp_list, "cor_seqtimesum_lplblsn", 0, mean_last_dim = True, num_blocks=10)
                self.myprint("now use the slope of each subject and perform a t-test for the group")
                exp_list = self.filter_experiments(experiment_names=["SEQ"], days=[1], vpns=[])
                self.ttest_one_group(exp_list, "cor_seqtimesum_slope_lpn", 0)
                exp_list = self.filter_experiments(experiment_names=["SEQ"], days=[2], vpns=[])
                self.ttest_one_group(exp_list, "cor_seqtimesum_slope_lpn", 0)
                self.myprint("END____SEQ______________________________________")
                self.myprint("___________________________________________________")
                self.myprint(" ")
            if exp_name == "SRTT":
                self.myprint("___________________________________________________")
                self.myprint("START____SRTT______________________________________")
                self.myprint("SRTT")
                self.myprint("1.1. is there a linear decrease in the Time to perform an sequence per block per paradigma (linear Regression)?")
                self.myprint(".... for day 1...")
                exp_list = self.filter_experiments(experiment_names=["SRTT"], days=[1], vpns=[])
                self.estimate_linear_regression(exp_list, "cor_seqtimesum_lplblsn", 0, mean_last_dim = True, num_blocks=6)
                self.myprint(".... for day 2...")
                exp_list = self.filter_experiments(experiment_names=["SRTT"], days=[2], vpns=[])
                self.estimate_linear_regression(exp_list, "cor_seqtimesum_lplblsn", 0, mean_last_dim = True, num_blocks=6)
                self.myprint("1.1. is there a linear decrease in the Time to perform an sequence per block per paradigma (linear Regression) ALLL?")
                self.myprint(".... for day 1...")
                exp_list = self.filter_experiments(experiment_names=["SRTT"], days=[1], vpns=[])
                self.estimate_linear_regression(exp_list, "all_seqtimesum_lplblsn", 0, mean_last_dim = True, num_blocks=6)
                self.myprint(".... for day 2...")
                exp_list = self.filter_experiments(experiment_names=["SRTT"], days=[2], vpns=[])
                self.estimate_linear_regression(exp_list, "all_seqtimesum_lplblsn", 0, mean_last_dim = True, num_blocks=6)

                self.myprint("now use the slope of each subject and perform a t-test for the group")
                exp_list = self.filter_experiments(experiment_names=["SRTT"], days=[1], vpns=[])
                self.ttest_one_group(exp_list, "cor_seqtimesum_slope_lpn", 0)
                exp_list = self.filter_experiments(experiment_names=["SRTT"], days=[2], vpns=[])
                self.ttest_one_group(exp_list, "cor_seqtimesum_slope_lpn", 0)
                self.myprint("END____SRTT______________________________________")
                self.myprint("_________________________________________________")
                self.myprint(" ")

    def ttest_one_group(self, exp_list, key, paradigma, mean_last_dim = False, is_independent = True):
        # reduce a list of lists to a list by averaging
        subj_values = [] # a list
        for exp in exp_list:
            v = getattr(exp,key)
            t = v[paradigma][0]

            if mean_last_dim:
                t = self.list_of_list_to_list(t)
            subj_values.append(t)

        G1 = np.array(subj_values)

        # G1 = self.list_of_list_to_list(data[0])
        # G2 = self.list_of_list_to_list(data[1])
        self.myprint(str(G1))
        t, p = stats.ttest_1samp(G1,0.0)
        m = G1.mean()
        #m = statistics.mean(G1)
        std = G1.std()
#        std = statistics.stdev(G1)

        self.print_pt_1g(key, t,p, m, std)

    def print_pt_1g(self,key, t,p, mymean, std):
        self.myprint(f"{key} t-test different from 0  p={p} t={t}, m={mymean}, std={std}")

    def estimate_linear_regression(self, exp_list, key, paradigma, mean_last_dim = False, num_blocks=6):
        """ performing linear regression with statsmodels"""
        subj_values = [] # a list

        for exp in exp_list:
            v = getattr(exp,key)
            t = v[paradigma]
            print(f"the data of file {exp.filename}  and parameter={key}... {t}")
            if mean_last_dim:
                t = self.list_of_list_to_list(t)
            if len(t)==(num_blocks-1):
                t.append(mean(t))
            subj_values.append(t)
            # print(len(t))
            # print(exp.experiment_name)
            # print(exp.filename)

        X = np.array(subj_values)
        print(X)
        #Xm = np.mean(X,axis=0)
        Xm = np.sum(X,axis=0)
        Xm =sm.add_constant(Xm, prepend=False)
        print(Xm)
        y = np.arange(0,Xm.shape[0],1)
        mod = sm.OLS(y,Xm)
        self.myprint("Regression across the mean of all subjects for each blocks ...")
        self.myprint(str(Xm))
        res = mod.fit()
        self.myprint(res.summary().as_text())

    def test_group_differences_ttest(self, key, is_independent, paradigma, level):
        # ich laufe ueber die Buchstaben der levelbeschreibung und ziehe die richtigen Daten heraus
        values = self.get_target_values_by_key(key)
        for i in range(len(level)):
            if level[i]== 'p':
                values = self.filter_target_values_by_paradigma(values, paradigma)
            if level[i]=='n':
                self.test_group_differences_two_groups(key, values, is_independent=is_independent)

#        d = self.get_target_values_by_key_and_level(key, paradigma, level)
 #       if len(d)==2:

    def get_target_values_by_key(self, key):
        """ get the target attributes out of the experiment objects
            and put these into a list for each group
        """
        target_val = []
        for group in self.groups:
            subject_list = []
            for subj_exp in group.subj_exp_list:
                exp_value = getattr(subj_exp, key)
                subject_list.append(exp_value)
            target_val.append(subject_list)
        return target_val

    def filter_target_values_by_paradigma(self,values, paradigma):
        new_val = []
        for attribute_list in values:
            new_attribute_list = []
            for attribute in attribute_list:
                new_attribute_list.append(attribute[paradigma])
            new_val.append(new_attribute_list)
        return new_val

    def list_of_list_to_list(self, input_list):
        # if input_list is a list of list then it will be transformed to a list
        # by averaging the second dimension
        if any(isinstance(el, list) for el in input_list):
            input_list = [statistics.mean(f) for f in input_list]
        return input_list

    def test_group_differences_two_groups(self, key, data, is_independent=True):
        # reduce a list of lists to a list by averaging
        G1 = self.list_of_list_to_list(data[0])
        G2 = self.list_of_list_to_list(data[1])

        if is_independent:
            t, p = stats.ttest_ind(G1, G2)
        else:
            t, p = stats.ttest_rel(G1, G2)
        m = [statistics.mean(G1), statistics.mean(G2)]
        std = [statistics.stdev(G1), statistics.stdev(G2)]

        self.print_pt_2g(key=key, t=t,p=p, mymean = m, std=std)

    def get_target_values_by_key_level_1(self, key, paradigma):
        # extracts from the dictionaries of all groups the value
        # with key = key
        # returns a list with groups, the group list consists of a list with the key elements
        target_val = []
        for group in self.groups:
            subject_list = []
            for subj_exp in group.subj_exp_list:
                exp_value = getattr(subj_exp, key)[paradigma]
                print(exp_value)
                subject_list.append(exp_value)
            target_val.append(subject_list)
        return target_val

    def print_pt_2g(self, key, t, p, mymean=0, std=0):
        mymean = [float(m) for m in mymean]
        std = [float(s) for s in std]
        self.myprint(f"{key} p = {p:.7}  with t = {t:.3}  (mean = {mymean[0]:.3} +- {std[0]:.4}  vs. {mymean[1]:.3} +- {std[1]:.4}")

    def show_group_differences(self, key):
        data = self.get_target_values_by_key(key)
        data = np.asarray(data)
        print(f"Group Results of {key}")
        df = pd.DataFrame(data.T, columns = [self._ids[0], self._ids[1]])
        print(df.head(30))

    def plot_one_group_sequence(self, key):
        data = self.get_target_values_by_key(key)
        #print(data)
        #print("---")
        data = data[0]
        #print(np.asarray(data))
        #print(data)
        #print(data.shape)
        for subj in data:
            plt.plot(subj)

        plt.show()
        # plt.plot( 'x', 'y1', data=data, marker='o', markerfacecolor='blue', markersize=12, color='skyblue', linewidth=4)
        # plt.plot( 'x', 'y2', data=df, marker='', color='olive', linewidth=2)
        # plt.plot( 'x', 'y3', data=df, marker='', color='olive', linewidth=2, linestyle='dashed', label="toto")
        # plt.legend()

if __name__ == "__main__":
    # seed random number generator
    experiment_name = 'MST'
    experiment_name = 'ASTEROID'
    _ids = ["MST_G1_", "MST_G2_"]
    _ids = ["ASTEROID_G1_", "ASTEROID_G2_"]
    my_stat = Statistic(experiment = experiment_name, group_list= [], data_path = ".\\Data_python", _ids = _ids)
    #my_stat.test_group_differences_ttest('corrsq_slope')
    my_stat.test_group_differences_ttest('success_per_block_slope', is_independent=False)
    my_stat.test_group_differences_ttest('abs_success', is_independent=False)
    my_stat.show_group_differences('abs_success')
    my_stat.show_group_differences('success_per_block_slope')


class Solution:
    def slowestKey(self, rt: List[int], kp: str) -> str:
        beginT = rt[0]
        beginK = kp[0]
        for i in range(1, len(rt)):
            diff = rt[i]-rt[i-1]
            if diff > beginT or (diff == beginT and ord(kp[i]) > ord(beginK)):
                beginT = diff
                beginK = kp[i]
        return beginK