text:
# Copyright (c) Facebook, Inc. and its affiliates.

import glob
import json

import numpy as np
from PIL import Image
from functools import lru_cache

import torch
from torch.utils.data import Dataset
import torchvision.transforms as transforms

class DigitImageTfSeqDataset(Dataset):

    def __init__(self, dir_dataset, transform=None, downsample_imgs=False, img_type='png'):

        self.dir_dataset = dir_dataset
        self.img_type = img_type

        self.transform = transform
        self.downsample_imgs = downsample_imgs

        self.episodes = []
        self.img_idxs = []
        self.poses = []

        self.min_num_data = 3

        # reduce outliers due to noisy ellip feature estimates at start/end of episode
        self.skip_start_frames = 2
        self.skip_end_frames = 2

        # collect img, pose pairs for each episode and concatenate as lists
        subdirs = sorted(glob.glob("{0}/*".format(dir_dataset)))
        for (curr_idx, dir_episode) in enumerate(subdirs):

            num_data = len(
                glob.glob("{0}/*.{1}".format(dir_episode, img_type)))
            if (num_data < self.min_num_data):
                continue

            episode_idx = int((dir_episode.split('/')[-1]).split('_')[-1])

            # load poses, img_idxs
            file_poses = "{0}/poses2d_episode_{1:04d}.json".format(
                dir_episode, episode_idx)
            data_json = None
            with open(file_poses) as f:
                data_json = json.load(f)
            num_imgs_curr = len(
                glob.glob("{0}/*.{1}".format(dir_episode, self.img_type)))

            curr_episodes = [episode_idx] * num_imgs_curr
            curr_img_idxs = list(range(0, num_imgs_curr))
            curr_poses = data_json['ee_poses2d__obj']

            # number of poses must match number of images in the episode
            assert(num_data == len(curr_poses))
            print("[DigitImageTfSeqDataset] Loading dataset of ({0} images, {1} poses) for episode {2} from dir {3}".format(
                num_data, len(curr_poses), episode_idx, dir_episode))

            # remove start/end set of frames
            self.remove_start_end_frames([curr_episodes, curr_img_idxs, curr_poses])

            self.episodes.append(curr_episodes)
            self.img_idxs.append(curr_img_idxs)
            self.poses.append(curr_poses)

    def remove_start_end_frames(self, list_of_lists):

        for curr_list in list_of_lists:
            curr_list = curr_list[self.skip_start_frames:]
            curr_list = curr_list[:len(curr_list)-self.skip_end_frames]

    def get_item_loc(self, idx):

        loc = "{0}/episode_{1:04d}/{2:04d}".format(
            self.dir_dataset, self.episodes[idx], self.img_idxs[idx])

        return loc

    @lru_cache(maxsize=10000)
    def get_images_seq(self, seq_idx):
        episodes_seq = self.episodes[seq_idx]
        img_idxs_seq = self.img_idxs[seq_idx]
        imgs = []
        for (idx, img_idx) in enumerate(img_idxs_seq):
            imgs.append(Image.open("{0}/episode_{1:04d}/{2:04d}.{3}".format(self.dir_dataset, episodes_seq[idx],
                                                                   img_idx, self.img_type)))
        return imgs

    def __getitem__(self, seq_idx):

        # load image data
        imgs_list = self.get_images_seq(seq_idx)

        # load pose data
        poses = torch.FloatTensor(self.poses[seq_idx])

        if self.transform is not None:
            for idx, img in enumerate(imgs_list):
                imgs_list[idx] = self.transform(img)

        if self.downsample_imgs:
            for idx, img in enumerate(imgs_list):
                img = torch.nn.functional.interpolate(img, size=64)
                img = torch.nn.functional.interpolate(img.permute(0, 2, 1), size=64)
                img = img.permute(0, 2, 1)

                imgs_list[idx] = img

        if any(elem is None for elem in [imgs_list, poses]):
            print(
                "[DigitImageTfSeqDataset] Unable to read img, pose data at seq_idx {0}".format(seq_idx))
            return

        imgs = torch.stack(imgs_list)
        data = (imgs, poses)

        return data

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

ext: py

max_stars_repo_name: psodhi/tactile-pushing

max_issues_repo_name: psodhi/tactile-pushing

max_forks_repo_name: psodhi/tactile-pushing

reason: import numpy


text:
import sys,os,json

# -*- coding: utf-8 -*-
import sys,os

father_path = os.path.join(os.getcwd())
print(father_path, "==father path==")

def find_bert(father_path):
	if father_path.split("/")[-1] == "BERT":
		return father_path

	output_path = ""
	for fi in os.listdir(father_path):
		if fi == "BERT":
			output_path = os.path.join(father_path, fi)
			break
		else:
			if os.path.isdir(os.path.join(father_path, fi)):
				find_bert(os.path.join(father_path, fi))
			else:
				continue
	return output_path

bert_path = find_bert(father_path)
t2t_bert_path = os.path.join(bert_path, "t2t_bert")
sys.path.extend([bert_path, t2t_bert_path])

import numpy as np
import tensorflow as tf
from bunch import Bunch
from data_generator import tokenization
import json

from example import feature_writer, write_to_tfrecords_multitask
from example import classifier_processor
from data_generator import vocab_filter

flags = tf.flags

FLAGS = flags.FLAGS

tf.logging.set_verbosity(tf.logging.INFO)

flags.DEFINE_string("buckets", "", "oss buckets")

## Required parameters
flags.DEFINE_string(
	"multitask_dict", "",
	"if apply rule detector"
	)

flags.DEFINE_string(
	"vocab_file", "",
	"if apply rule detector"
	)

flags.DEFINE_string(
	"lower_case", "",
	"if apply rule detector"
	)

flags.DEFINE_integer(
	"max_length", 128,
	"if apply rule detector"
	)

flags.DEFINE_string(
	"multi_task_type", "",
	"if apply rule detector"
	)

def main(_):

	import json
	multi_task_config = Bunch(json.load(open(os.path.join(FLAGS.buckets, FLAGS.multitask_dict))))

	vocab_path = FLAGS.vocab_file
	# os.path.join(FLAGS.buckets, FLAGS.vocab_file)

	train_file_dict = {}
	test_file_dict = {}
	dev_file_dict = {}
	train_result_dict = {}
	test_result_dict = {}
	dev_result_dict = {}
	label_id_dict = {}
	for task in multi_task_config:
		train_file_dict[task] = os.path.join(FLAGS.buckets,
											multi_task_config[task]["train_file"])

		test_file_dict[task] = os.path.join(FLAGS.buckets,
											multi_task_config[task]["test_file"])

		dev_file_dict[task] = os.path.join(FLAGS.buckets,
											multi_task_config[task]["dev_file"])

		train_result_dict[task] = os.path.join(FLAGS.buckets,
											multi_task_config[task]["train_result_file"])

		test_result_dict[task] = os.path.join(FLAGS.buckets,
											multi_task_config[task]["test_result_file"])

		dev_result_dict[task] = os.path.join(FLAGS.buckets,
											multi_task_config[task]["dev_result_file"])
		label_id_dict[task] = os.path.join(FLAGS.buckets,
											multi_task_config[task]["label_id"])

	print(train_file_dict)

	if FLAGS.lower_case == "True":
		lower_case = True
	else:
		lower_case = False

	tokenizer = tokenization.FullTokenizer(
			vocab_file=vocab_path,
			do_lower_case=lower_case)

	for task in (FLAGS.multi_task_type.split(",")):
		if task not in multi_task_config:
			continue
		data_type = multi_task_config[task]["data_type"]
		if data_type == "single_sentence":
			classifier_data_api = classifier_processor.SentenceProcessor()
			classifier_data_api.get_labels(label_id_dict[task])
		elif data_type == "sentence_pair":
			classifier_data_api = classifier_processor.SentencePairProcessor()
			classifier_data_api.get_labels(label_id_dict[task])

		train_examples = classifier_data_api.get_train_examples(train_file_dict[task],
											is_shuffle=True)
		test_examples = classifier_data_api.get_train_examples(test_file_dict[task],
											is_shuffle=False)
		dev_examples = classifier_data_api.get_train_examples(dev_file_dict[task],
											is_shuffle=False)

		print(classifier_data_api.label2id, task)

		write_to_tfrecords_multitask.convert_multitask_classifier_examples_to_features(
															train_examples,
															classifier_data_api.label2id,
															FLAGS.max_length,
															tokenizer,
															train_result_dict[task],
															task,
															multi_task_config)

		write_to_tfrecords_multitask.convert_multitask_classifier_examples_to_features(
															test_examples,
															classifier_data_api.label2id,
															FLAGS.max_length,
															tokenizer,
															test_result_dict[task],
															task,
															multi_task_config)

		write_to_tfrecords_multitask.convert_multitask_classifier_examples_to_features(
															dev_examples,
															classifier_data_api.label2id,
															FLAGS.max_length,
															tokenizer,
															dev_result_dict[task],
															task,
															multi_task_config)

if __name__ == "__main__":
	tf.app.run()

max_stars_repo_name: yyht/bert

max_issues_repo_name: yyht/bert

max_forks_repo_name: yyht/bert


text:
import os
from unittest import TestCase

import numpy as np
import pandas as pd
import pytest
from testfixtures import TempDirectory

import mlnext

class TestLoadData(TestCase):
    def setUp(self):
        self.d = TempDirectory()

        self.df = pd.DataFrame([0, 1, 2], columns=['a'])
        self.df_path = os.path.join(self.d.path, 'data.csv')
        self.df.to_csv(self.df_path, index=False)

    def tearDown(self):
        self.d.cleanup()

    def test_load_data(self):
        result = mlnext.load_data(path=self.df_path)

        pd.testing.assert_frame_equal(result, self.df)

class TestTemporalize(TestCase):

    def test_temporalize(self):
        arr = np.arange(12).reshape(-1, 2)
        expected = np.arange(12).reshape(-1, 2, 2)

        result = mlnext.temporalize(data=arr, timesteps=2)

        np.testing.assert_array_equal(result, expected)

    def test_temporalize_drop_rows(self):
        arr = np.arange(14).reshape(-1, 2)
        expected = np.arange(12).reshape(-1, 3, 2)

        result = mlnext.temporalize(data=arr, timesteps=3)

        np.testing.assert_array_equal(result, expected)

class TestLoadData3D(TestCase):
    def setUp(self):
        self.d = TempDirectory()

        self.arr = np.arange(8).reshape(4, 2)
        self.df = pd.DataFrame(self.arr, columns=['a', 'b'])
        self.df_path = os.path.join(self.d.path, 'data.csv')
        self.df.to_csv(self.df_path, index=False)

    def tearDown(self):
        self.d.cleanup()

    def test_load_data_3d(self):
        result = mlnext.load_data_3d(path=self.df_path, timesteps=2)
        expected = self.arr.reshape(2, 2, 2)

        np.testing.assert_array_equal(result, expected)

class TestDetemporalize(TestCase):

    def test_detemporalize(self):

        arr = np.arange(12).reshape(3, 2, 2)
        expected = np.arange(12).reshape(6, 2)

        result = mlnext.detemporalize(data=arr)

        np.testing.assert_array_equal(result, expected)

    def test_detemporalize_2d(self):

        arr = np.arange(8).reshape(4, 2)

        result = mlnext.detemporalize(data=arr)

        np.testing.assert_array_equal(result, arr)

class TestSample(TestCase):

    def test_sample_normal(self):

        mean = 0
        std = 1

        np.random.seed(0)
        expected = np.random.normal(loc=mean, scale=std)

        np.random.seed(0)
        result = mlnext.sample_normal(mean=mean, std=std)

        np.testing.assert_array_equal(result, expected)

    def test_sample_bernoulli(self):

        mean = 0.2
        np.random.seed(0)
        expected = np.random.binomial(n=1, p=mean)

        np.random.seed(0)
        result = mlnext.sample_bernoulli(mean=mean)

        self.assertEqual(result, expected)

@pytest.fixture
def data(request) -> np.ndarray:
    """Create a numpy array. Expects request to be a tuple of (rows, features).

    Args:
        request (Any): Tuple of (rows, features).

    Returns:
        np.ndarray: Returns an numpy array of shape (rows, features).
    """
    rows, features = request.param
    i, j = np.ogrid[:rows, :features]
    return 10 * i + j

@pytest.mark.parametrize(
    'data,timesteps,stride,exp',
    [
        ((6, 3), 2, 0,
         np.array([[[0, 1, 2],
                    [10, 11, 12]],

                   [[20, 21, 22],
                    [30, 31, 32]],

                   [[40, 41, 42],
                    [50, 51, 52]]])),
        ((6, 3), 2, 2,
         np.array([[[0, 1, 2],
                    [10, 11, 12]],

                   [[20, 21, 22],
                    [30, 31, 32]],

                   [[40, 41, 42],
                    [50, 51, 52]]])),
        ((6, 3), 3, 1,
         np.array([[[0, 1, 2],
                    [10, 11, 12],
                    [20, 21, 22]],

                   [[10, 11, 12],
                    [20, 21, 22],
                    [30, 31, 32]],

                   [[20, 21, 22],
                    [30, 31, 32],
                    [40, 41, 42]],

                   [[30, 31, 32],
                    [40, 41, 42],
                    [50, 51, 52]]])),
        ((8, 3), 3, 2,
         np.array([[[0, 1, 2],
                    [10, 11, 12],
                    [20, 21, 22]],

                   [[20, 21, 22],
                    [30, 31, 32],
                    [40, 41, 42]],

                   [[40, 41, 42],
                    [50, 51, 52],
                    [60, 61, 62]]]))
    ],
    indirect=['data']
)
def test_temporalize(
    data: np.ndarray,
    timesteps: int,
    stride: int,
    exp: np.ndarray
):

    result = mlnext.temporalize(data, timesteps=timesteps, stride=stride)
    print(result)
    np.testing.assert_array_equal(result, exp)

@pytest.mark.parametrize(
    'data,timesteps,stride,err_msg',
    [
        (np.ones(10), 10, 10,
         'Expected array of dimension 2, but got 1 for array at position 0.'),
        (np.ones((10, 1)), 10, -2,
         'Stride must be greater than 0.'),
        (np.ones((10, 1)), -1, 1,
         'Timesteps must be greater than 1.')
    ]
)
def test_temporalize_fails(
    data: np.ndarray,
    timesteps: int,
    stride: int,
    err_msg: str
):
    with pytest.raises(ValueError) as exc_info:
        mlnext.temporalize(data, timesteps=timesteps, stride=stride)

    assert exc_info.value.args[0] == err_msg

@pytest.mark.parametrize(
    'data,timesteps,stride,last_point_only,exp',
    [
        ((6, 3), 2, 0, False,
         np.array([[0, 1, 2],
                   [10, 11, 12],
                   [20, 21, 22],
                   [30, 31, 32],
                   [40, 41, 42],
                   [50, 51, 52]])
         ),
        ((6, 3), 3, 1, False,
         np.array([[0, 1, 2],
                   [10, 11, 12],
                   [20, 21, 22],
                   [30, 31, 32],
                   [40, 41, 42],
                   [50, 51, 52]])
         ),
        ((6, 3), 3, 2, False,
         np.array([[0, 1, 2],
                   [10, 11, 12],
                   [20, 21, 22],
                   [30, 31, 32],
                   [40, 41, 42]])
         ),
        ((6, 3), 3, 1, True,
         np.array([[20, 21, 22],
                   [30, 31, 32],
                   [40, 41, 42],
                   [50, 51, 52]])
         )
    ],
    indirect=['data']
)
def test_detemporalize(
    data: np.ndarray,
    timesteps: int,
    stride: int,
    last_point_only: bool,
    exp: np.ndarray
):
    data_3d = mlnext.temporalize(data, timesteps=timesteps, stride=stride)
    result = mlnext.detemporalize(
        data_3d, stride=stride, last_point_only=last_point_only)

    np.testing.assert_array_equal(result, exp)

max_stars_repo_name: PLCnext/MLnext-Framework

max_issues_repo_name: PLCnext/MLnext-Framework

max_forks_repo_name: PLCnext/MLnext-Framework


text:
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#  * Neither the name of NVIDIA CORPORATION nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
BSD 3-Clause License
Copyright (c) Soumith Chintala 2016,
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

# ResNet implementation kindly borrow from pytorch-vision and modified to match the original casenet caffe implementation.

import torch.nn as nn
import torch
import math
from torch.autograd import Variable
import numpy as np

__all__ = ['casenet101']

BatchNorm = nn.BatchNorm2d

class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, inplanes, planes, block_no, stride=1, downsample=None):  # add dilation factor
        super(Bottleneck, self).__init__()
        if block_no < 5:
            dilation = 2
            padding = 2
        else:
            dilation = 4
            padding = 4

        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False, stride=stride)
        self.bn1 = BatchNorm(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1,
                               padding=padding, bias=False, dilation=dilation)
        self.bn2 = BatchNorm(planes)
        self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
        self.bn3 = BatchNorm(planes * 4)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

class Crop(nn.Module):
    def __init__(self, axis, offset):
        super(Crop, self).__init__()
        self.axis = axis
        self.offset = offset

    def forward(self, x, ref):
        """

        :param x: input layer
        :param ref: reference usually data in
        :return:
        """
        for axis in range(self.axis, x.dim()):
            ref_size = ref.size(axis)
            indices = torch.arange(self.offset, self.offset + ref_size).long()
            indices = x.data.new().resize_(indices.size()).copy_(indices).long()
            x = x.index_select(axis, Variable(indices))
        return x

class MyIdentity(nn.Module):
    def __init__(self, axis, offset):
        super(MyIdentity, self).__init__()
        self.axis = axis
        self.offset = offset

    def forward(self, x, ref):
        """

        :param x: input layer
        :param ref: reference usually data in
        :return:
        """
        return x

class SideOutputCrop(nn.Module):
    """
    This is the original implementation ConvTranspose2d (fixed) and crops
    """

    def __init__(self, num_output, kernel_sz=None, stride=None, upconv_pad=0, do_crops=True):
        super(SideOutputCrop, self).__init__()
        self._do_crops = do_crops
        self.conv = nn.Conv2d(num_output, out_channels=1, kernel_size=1, stride=1, padding=0, bias=True)

        if kernel_sz is not None:
            self.upsample = True
            self.upsampled = nn.ConvTranspose2d(1, out_channels=1, kernel_size=kernel_sz, stride=stride,
                                                padding=upconv_pad,
                                                bias=False)
            ##doing crops
            if self._do_crops:
                self.crops = Crop(2, offset=kernel_sz // 4)
            else:
                self.crops = MyIdentity(None, None)
        else:
            self.upsample = False

    def forward(self, res, reference=None):
        side_output = self.conv(res)
        if self.upsample:
            side_output = self.upsampled(side_output)
            side_output = self.crops(side_output, reference)

        return side_output

class Res5OutputCrop(nn.Module):

    def __init__(self, in_channels=2048, kernel_sz=16, stride=8, nclasses=20, upconv_pad=0, do_crops=True):
        super(Res5OutputCrop, self).__init__()
        self._do_crops = do_crops
        self.conv = nn.Conv2d(in_channels, nclasses, kernel_size=1, stride=1, padding=0, bias=True)
        self.upsampled = nn.ConvTranspose2d(nclasses, out_channels=nclasses, kernel_size=kernel_sz, stride=stride,
                                            padding=upconv_pad,
                                            bias=False, groups=nclasses)
        if self._do_crops is True:
            self.crops = Crop(2, offset=kernel_sz // 4)
        else:
            self.crops = MyIdentity(None, None)

    def forward(self, res, reference):
        res = self.conv(res)
        res = self.upsampled(res)
        res = self.crops(res, reference)
        return res

def get_upsample_filter(size):
    """Make a 2D bilinear kernel suitable for upsampling"""
    factor = (size + 1) // 2
    if size % 2 == 1:
        center = factor - 1
    else:
        center = factor - 0.5
    og = np.ogrid[:size, :size]
    filter = (1 - abs(og[0] - center) / factor) * \
             (1 - abs(og[1] - center) / factor)
    return torch.from_numpy(filter).float()

class ResNet(nn.Module):

    def __init__(self, block, layers, nclasses=20):

        self.inplanes = 64
        super(ResNet, self).__init__()
        self._nclasses = nclasses
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=1, padding=3,
                               bias=False)

        self.bn1 = BatchNorm(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=0, ceil_mode=True)  # define ceil mode

        self.layer1 = self._make_layer(block, 64, layers[0], 2)  # res2
        self.layer2 = self._make_layer(block, 128, layers[1], 3, stride=2)  # res3

        self.layer3 = self._make_layer(block, 256, layers[2], 4, stride=2)  # res4
        self.layer4 = self._make_layer(block, 512, layers[3], 5, stride=1)  # res5

        self.normals = None

        ####let's make pointers to keep compatibility for now

        SideOutput_fn = SideOutputCrop
        Res5Output_fn = Res5OutputCrop

        self.use_pytorch_upsample = False

        # The original casenet implementation has padding when upsampling cityscapes that are not used for SBD.
        # Leaving like this for now such that it is clear and it matches the original implementation (for fair comparison)

        if nclasses == 19:
            print('Assuming Cityscapes CASENET')
            self.score_edge_side1 = SideOutput_fn(64)
            self.score_edge_side2 = SideOutput_fn(256, kernel_sz=4, stride=2, upconv_pad=1, do_crops=False)
            self.score_edge_side3 = SideOutput_fn(512, kernel_sz=8, stride=4, upconv_pad=2, do_crops=False)
            self.score_cls_side5 = Res5Output_fn(kernel_sz=16, stride=8, nclasses=self._nclasses, upconv_pad=4,
                                                 do_crops=False)
        else:
            print('Assuming Classical SBD CASENET')
            self.score_edge_side1 = SideOutput_fn(64)
            self.score_edge_side2 = SideOutput_fn(256, kernel_sz=4, stride=2)
            self.score_edge_side3 = SideOutput_fn(512, kernel_sz=8, stride=4)
            self.score_cls_side5 = Res5Output_fn(kernel_sz=16, stride=8, nclasses=self._nclasses)

        num_classes = self._nclasses
        self.ce_fusion = nn.Conv2d(4 * num_classes, num_classes, groups=num_classes, kernel_size=1, stride=1, padding=0,
                                   bias=True)

        self.sigmoid = nn.Sigmoid()

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, BatchNorm):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
            elif isinstance(m, nn.ConvTranspose2d):
                c1, c2, h, w = m.weight.data.size()
                weight = get_upsample_filter(h)
                m.weight.data = weight.view(1, 1, h, w).repeat(c1, c2, 1, 1)
                if m.bias is not None:
                    m.bias.data.zero_()

        # manually initializing the new layers.
        self.score_edge_side1.conv.weight.data.normal_(0, 0.01)
        self.score_edge_side1.conv.bias.data.zero_()
        # -
        self.score_edge_side2.conv.weight.data.normal_(0, 0.01)
        self.score_edge_side2.conv.bias.data.zero_()
        # -
        self.score_edge_side3.conv.weight.data.normal_(0, 0.01)
        self.score_edge_side3.conv.bias.data.zero_()
        # -
        self.ce_fusion.weight.data.fill_(0.25)
        self.ce_fusion.bias.data.zero_()
        # ---

    def _make_layer(self, block, planes, blocks, block_no, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion,
                          kernel_size=1, stride=stride, bias=False),
                BatchNorm(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, block_no, stride, downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes, block_no))

        return nn.Sequential(*layers)

    def _sliced_concat(self, res1, res2, res3, res5, num_classes):
        out_dim = num_classes * 4
        out_tensor = Variable(torch.FloatTensor(res1.size(0), out_dim, res1.size(2), res1.size(3))).cuda()
        class_num = 0
        for i in range(0, out_dim, 4):
            out_tensor[:, i, :, :] = res5[:, class_num, :, :]
            out_tensor[:, i + 1, :, :] = res1[:, 0, :, :]  # it needs this trick for multibatch
            out_tensor[:, i + 2, :, :] = res2[:, 0, :, :]
            out_tensor[:, i + 3, :, :] = res3[:, 0, :, :]

            class_num += 1

        return out_tensor

    def forward(self, x, normals_mask=None):
        assert x.shape[1] == 3, 'N,3,H,W BGR Image?'
        input_data = x

        # res1
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        side_1 = self.score_edge_side1(x, input_data)

        # res2
        x = self.maxpool(x)
        x = self.layer1(x)
        side_2 = self.score_edge_side2(x, input_data)

        # res3
        x = self.layer2(x)
        side_3 = self.score_edge_side3(x, input_data)

        # res4
        x = self.layer3(x)

        # res5
        x = self.layer4(x)

        side_5 = self.score_cls_side5(x, input_data)

        # combine outputs and classify
        sliced_cat = self._sliced_concat(side_1, side_2, side_3, side_5, self._nclasses)
        acts = self.ce_fusion(sliced_cat)

        normals = None

        return [acts, side_5, normals, (side_1, side_2, side_3)]  # sigmoid can be taken later

class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(BasicBlock, self).__init__()
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = BatchNorm(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = BatchNorm(planes)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

def casenet101(pretrained=False, **kwargs):
    """Constructs a ResNet-101 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(Bottleneck, [3, 4, 23, 3], **kwargs)
    if pretrained:
        raise NotImplementedError()
    return model

def conv3x3(in_planes, out_planes, stride=1):
    "3x3 convolution with padding"
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                     padding=1, bias=False)

max_stars_repo_name: jblanche/STEAL

max_issues_repo_name: jblanche/STEAL


text:
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 20 16:03:39 2018
小提琴图
当我们的数据是num_subj*num_var，且有几个诊断组时，我们一般希望把var name作为x，把var value作为y，把诊断组作为hue
来做小提琴图，以便于观察每个var的组间差异。
此时，用于sns的特殊性，我们要将数据变换未长列的形式。
行数目为：num_subj*num_var。列数目=3，分别是hue，x以及y

input：
    data_path=r'D:\others\彦鸽姐\final_data.xlsx'
    x_location=np.arange(5,13,1)#筛选数据的列位置

未来改进：封装为类，增加可移植性
@author: lenovo
"""
#==========================================================
# 载入绘图模块
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
#==========================================================
class ViolinPlot():
    # initial parameters
    def __init__(sel,
                       data_path=r'D:\others\彦鸽姐\final_data.xlsx',
                       x_location=np.arange(5,13,1),
                       x_name='脑区',
                       y_name='reho',
                       hue_name='分组',
                       hue_order=[2,1],
                       if_save_figure=0,
                       figure_name='violin.tiff'):
           #======================================
           sel.data_path=data_path
           sel.x_location=x_location
           sel.x_name=x_name
           sel.y_name=y_name
           sel.hue_name=hue_name
           sel.hue_order=hue_order
           sel.if_save_figure=if_save_figure
           sel.figure_name=figure_name

#====================================================
    def data_preparation(sel):

        # load data
        df = pd.read_excel(sel.data_path,index=False)

        # 筛选数据
        df_selected=df.iloc[:,sel.x_location]

        #把需要呈现的数据concat到一列
        n_subj,n_col=df_selected.shape
        df_decreased_long=pd.DataFrame([])
        for nc in range(n_col):
            df_decreased_long=pd.concat([df_decreased_long,df_selected.iloc[:,nc]])

        # 整理columns
        col_name=list(df_selected.columns)
        col_name_long=[pd.DataFrame([name]*n_subj) for name in col_name]
        col_name_long=pd.concat(col_name_long)

        #整理分组标签
        group=pd.DataFrame([])
        for i in range(n_col):
            group=pd.concat([group,df[sel.hue_name]])

        #整合
        sel.data=pd.concat([group,col_name_long,df_decreased_long],axis=1)

        # 加列名
        sel.data.columns=[sel.hue_name, sel.x_name, sel.y_name]

        return sel

    def plot(sel):
        sel.data=sel.data_preparation().data
        # plot
        plt.plot(figsize=(5, 15))

        # 小提琴框架
        ax=sns.violinplot(x=sel.x_name, y=sel.y_name,hue=sel.hue_name,
                    data=sel.data,palette="Set2",
                    split=False,scale_hue=True,hue_order=sel.hue_order,
                    orient="v",inner="box")
#

        # 设置label，以及方位
        label_x = ax.get_xticklabels()
        label_y = ax.get_yticklabels()
        plt.setp(label_x, size=10,rotation=0, horizontalalignment='right')
        plt.setp(label_y, size=10,rotation=0, horizontalalignment='right')

        # save figure
        if sel.if_save_figure:
            f.savefig(sel.figure_name, dpi=300, bbox_inches='tight')

#        plt.hold
#        #加点/风格1
#        sns.swarmplot(x=sel.x_name, y=sel.y_name,hue=sel.hue_name,data=sel.data,
#                              color="w", alpha=.5,palette="Set1")
##
        #加点/风格2
#        sns.stripplot(x=sel.x_name, y=sel.y_name,hue=sel.hue_name,data=sel.data,
#                              color="w", alpha=.5,palette="Set1", jitter=False)

#        plt.show()

        return sel

if __name__ == '__main__':
    sel = ViolinPlot(data_path=r'D:\WorkStation_2018\WorkStation_dynamicFC_V3\Data\results_cluster\results_of_individual\temploral_properties.xlsx',
                     x_location=np.arange(1, 2, 1),
                     hue_name='group',
                     hue_order=None)
    df =sel.data_preparation()
    sel.plot()

max_stars_repo_name: dongmengshi/easylearn

max_issues_repo_name: dongmengshi/easylearn

max_forks_repo_name: dongmengshi/easylearn


text:
##############################################################################
# MSIBI: A package for optimizing coarse-grained force fields using multistate
#   iterative Boltzmann inversion.
# Copyright (c) 2017 Vanderbilt University and the Authors
#
# Authors: Christoph Klein, Timothy C. Moore
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files, to deal
# in MSIBI without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# # copies of MSIBI, and to permit persons to whom MSIBI is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of MSIBI.
#
# MSIBI IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH MSIBI OR THE USE OR OTHER DEALINGS ALONG WITH
# MSIBI.
#
# You should have received a copy of the MIT license.
# If not, see <https://opensource.org/licenses/MIT/>.
##############################################################################

from __future__ import division

import numpy as np

def calc_similarity(arr1, arr2):
    f_fit = np.sum(np.absolute(arr1 - arr2))
    f_fit /= np.sum((np.absolute(arr1) + np.absolute(arr2)))
    return 1.0 - f_fit


text:
import pytest
import numpy as np
from util import bisect_left_naive

@pytest.fixture
def positive_uniform():
    yield np.array(range(0, 10))

@pytest.fixture
def negative_uniform():
    yield np.flip(np.array(range(0, 10)))

@pytest.mark.parametrize(['target', 'expected'], [
    (0, 0),
    (1, 1),
    (8, 8),
])
def test_exact_match(positive_uniform, target, expected):
    obtained = bisect_left_naive(positive_uniform, target)
    assert expected == obtained

@pytest.mark.parametrize(['target', 'expected'], [
    (0.5, 0),
    (1.5, 1),
    (8.999, 8)
])
def test_in_interval(positive_uniform, target, expected):
    obtained = bisect_left_naive(positive_uniform, target)
    assert expected == obtained

@pytest.mark.parametrize(['target', 'expected'], [
    (0, None),
    (1, 8),
    (8, 1)
])
def test_exact_match(negative_uniform, target, expected):
    obtained = bisect_left_naive(negative_uniform, target)
    assert expected == obtained

@pytest.mark.parametrize(['target', 'expected'], [
    (0.5, 8),
    (1.5, 7),
    (8.999, 0)
])
def test_in_interval(negative_uniform, target, expected):
    obtained = bisect_left_naive(negative_uniform, target)
    assert expected == obtained

@pytest.mark.parametrize('target', [-100, -0.001, 10, 100])
def test_outside_interval(positive_uniform, target):
    obtained = bisect_left_naive(positive_uniform, target)
    assert obtained is None


text:
import numpy as np
from sklearn.metrics import f1_score

def f1_score_func(preds, labels):
    preds_flat = np.argmax(preds, axis=1).flatten()
    labels_flat = labels.flatten()
    return f1_score(labels_flat, preds_flat, average='weighted')

max_stars_repo_name: sciosci/demographicx

max_issues_repo_name: sciosci/demographicx

max_forks_repo_name: sciosci/demographicx


text:
import numpy as np

class FourMomentum:
    def __init__(self, vec, basis='x,y,z,m'):
        self.set_basis(vec,basis)

    def set_basis(self, vec, basis='x,y,z,m'):
        """
        @brief Set the four-momentum where vec is a four vector in the basis defined by
        the string basis, e.g 'xyzm' corresponds to cartesian where the fourth

        @params
        vec: A list/tuple of four numbers
        basis: a string defining the basis the four numbers are  based in
        """
        basis = basis.lower()
        if basis == 'x,y,z,m':
            self._px = vec[0]
            self._py = vec[1]
            self._pz = vec[2]
            self._m = vec[3]
        elif basis == 'x,y,z,e':
            self._px = vec[0]
            self._py = vec[1]
            self._pz = vec[2]
            e = vec[3]
            mag = np.abs(self.p**2-e**2)
            self._m = mag * np.sign(self.p**2-e**2)
        elif basis == 'pt,eta,phi,m':
            self._px = vec[0]*np.cos(vec[2])
            self._py = vec[0]*np.sin(vec[2])
            self._pz = vec[0]*np.sinh(vec[1])
            self._m = vec[3]
        elif basis == 'pt,eta,phi,e':
            self._px = vec[0]*np.cos(vec[2])
            self._py = vec[0]*np.sin(vec[2])
            self._pz = vec[0]*np.sinh(vec[1])
            e = vec[3]
            self._m = mag * np.sign(self.p**2-vec[3]**2)

        elif basis == 'p,theta,phi,m':
            self._px = vec[0]*np.sin(vec[1])*np.cos(vec[2])
            self._py = vec[0]*np.sin(vec[1])*np.sin(vec[2])
            self._pz = vec[0]*np.cos(vec[1])
            self._m = vec[3]

        elif basis == 'p,theta,phi,e':
            self._px = vec[0]*np.sin(vec[1])*np.cos(vec[2])
            self._py = vec[0]*np.sin(vec[1])*np.sin(vec[2])
            self._pz = vec[0]*np.cos(vec[1])
            e = vec[3]
            self._m = mag * np.sign(self.p**2-vec[3]**2)

        else:
            raise ValueError(' Basis %s is not one of the supported options. It must be "xyzm", "xyze", "pTEtaPhiE", "pTEtaPhiE"'%(basis))

    @property
    def eta(self):
        return 0.5*np.log( (self.p+self.pz)/(self.p-self.pz)  )

    @property
    def p(self):
        return np.sqrt(self.px**2 + self.py**2 + self.pz**2)

    @property
    def e(self):
        return np.sqrt(self.m**2 + self.p**2 )

    @property
    def pt(self):
        return np.sqrt(self.px**2 + self.py**2)

    @property
    def px(self):
        return self._px

    @property
    def py(self):
        return self._py

    @property
    def pz(self):
        return self._pz

    @property
    def m(self):
        return self._m

    @property
    def theta(self):
        return np.arccos(self.pz/self.p)

    @property
    def z(self):
        return self.pz/self.p

    @property
    def phi(self):
        return np.arctan(self.py/self.px)

    @property
    def beta(self):
        return self.p/self.e

    def __add__(self, b):
        """
        Define the result of self + b, where b is a four momenta
        """
        res = FourMomentum(
                (self.px + b.px,
                self.py + b.py,
                self.pz + b.pz,
                self.e + b.e),
                'x,y,z,e'
            )
        return res

max_stars_repo_name: JacobRawling/OstrawlingMC

max_issues_repo_name: JacobRawling/OstrawlingMC

max_forks_repo_name: JacobRawling/OstrawlingMC


text:
import numpy as np
import pickle
from multiprocessing import Pool, Process
import os
import traceback
import pandas as pd

class ConstructSample:

    def __init__(self, path_to_samples, cnt_round, dic_traffic_env_conf):
        self.parent_dir = path_to_samples
        self.path_to_samples = path_to_samples + "/round_" + str(cnt_round)
        self.cnt_round = cnt_round
        self.dic_traffic_env_conf = dic_traffic_env_conf
        self.logging_data_list_per_gen = None
        self.hidden_states_list = None
        self.samples = []
        self.samples_all_intersection = [None]*self.dic_traffic_env_conf['NUM_INTERSECTIONS']

    def load_data(self, folder, i):

        try:
            f_logging_data = open(os.path.join(self.path_to_samples, folder, "inter_{0}.pkl".format(i)), "rb")
            logging_data = pickle.load(f_logging_data)
            f_logging_data.close()
            return 1, logging_data

        except Exception as e:
            print("Error occurs when making samples for inter {0}".format(i))
            print('traceback.format_exc():\n%s' % traceback.format_exc())
            return 0, None

    def load_data_for_system(self, folder):
        '''
        Load data for all intersections in one folder
        :param folder:
        :return: a list of logging data of one intersection for one folder
        '''
        self.logging_data_list_per_gen = []
        # load settings
        print("Load data for system in ", folder)
        self.measure_time = self.dic_traffic_env_conf["MEASURE_TIME"]
        self.interval = self.dic_traffic_env_conf["MIN_ACTION_TIME"]

        for i in range(self.dic_traffic_env_conf['NUM_INTERSECTIONS']):
            pass_code, logging_data = self.load_data(folder, i)
            if pass_code == 0:
                return 0
            self.logging_data_list_per_gen.append(logging_data)
        return 1

    def load_hidden_state_for_system(self, folder):
        print("loading hidden states: {0}".format(os.path.join(self.path_to_samples, folder, "hidden_states.pkl")))
        # load settings
        if self.hidden_states_list is None:
            self.hidden_states_list = []

        try:
            f_hidden_state_data = open(os.path.join(self.path_to_samples, folder, "hidden_states.pkl"), "rb")
            hidden_state_data = pickle.load(f_hidden_state_data) # hidden state_data is a list of numpy array
            # print(hidden_state_data)
            print(len(hidden_state_data))
            self.hidden_states_list.append(np.stack(hidden_state_data, axis=2))
            return 1
        except Exception as e:
            print("Error occurs when loading hidden states in ", folder)
            print('traceback.format_exc():\n%s' % traceback.format_exc())
            return 0

    def construct_state(self,features,time,i):
        '''

        :param features:
        :param time:
        :param i:  intersection id
        :return:
        '''

        state = self.logging_data_list_per_gen[i][time]
        assert time == state["time"]
        if self.dic_traffic_env_conf["BINARY_PHASE_EXPANSION"]:
            state_after_selection = {}
            for key, value in state["state"].items():
                if key in features:
                    if "cur_phase" in key:
                        state_after_selection[key] = self.dic_traffic_env_conf['PHASE'][self.dic_traffic_env_conf['SIMULATOR_TYPE']][value[0]]
                    else:
                        state_after_selection[key] = value
        else:
            state_after_selection = {key: value for key, value in state["state"].items() if key in features}
        # print(state_after_selection)
        return state_after_selection

    def _construct_state_process(self, features, time, state, i):
        assert time == state["time"]
        if self.dic_traffic_env_conf["BINARY_PHASE_EXPANSION"]:
            state_after_selection = {}
            for key, value in state["state"].items():
                if key in features:
                    if "cur_phase" in key:
                        state_after_selection[key] = self.dic_traffic_env_conf['PHASE'][self.dic_traffic_env_conf['SIMULATOR_TYPE']][value[0]]
                    else:
                        state_after_selection[key] = value
        else:
            state_after_selection = {key: value for key, value in state["state"].items() if key in features}
        return state_after_selection, i

    def get_reward_from_features(self, rs):
        reward = {}
        reward["sum_lane_queue_length"] = np.sum(rs["lane_queue_length"])
        reward["sum_lane_wait_time"] = np.sum(rs["lane_sum_waiting_time"])
        reward["sum_lane_num_vehicle_left"] = np.sum(rs["lane_num_vehicle_left"])
        reward["sum_duration_vehicle_left"] = np.sum(rs["lane_sum_duration_vehicle_left"])
        reward["sum_num_vehicle_been_stopped_thres01"] = np.sum(rs["lane_num_vehicle_been_stopped_thres01"])
        reward["sum_num_vehicle_been_stopped_thres1"] = np.sum(rs["lane_num_vehicle_been_stopped_thres1"])
        reward['pressure'] = np.absolute(np.sum(rs["pressure"]))
        return reward

    def cal_reward(self, rs, rewards_components):
        r = 0
        for component, weight in rewards_components.items():
            if weight == 0:
                continue
            if component not in rs.keys():
                continue
            if rs[component] is None:
                continue
            r += rs[component] * weight
        return r

    def construct_reward(self,rewards_components,time, i):

        rs = self.logging_data_list_per_gen[i][time + self.measure_time - 1]
        assert time + self.measure_time - 1 == rs["time"]
        rs = self.get_reward_from_features(rs['state'])
        r_instant = self.cal_reward(rs, rewards_components)

        # average
        list_r = []
        for t in range(time, time + self.measure_time):
            #print("t is ", t)
            rs = self.logging_data_list_per_gen[i][t]
            assert t == rs["time"]
            rs = self.get_reward_from_features(rs['state'])
            r = self.cal_reward(rs, rewards_components)
            list_r.append(r)
        r_average = np.average(list_r)

        return r_instant, r_average

    def judge_action(self,time,i):
        if self.logging_data_list_per_gen[i][time]['action'] == -1:
            raise ValueError
        else:
            return self.logging_data_list_per_gen[i][time]['action']

    def make_reward(self, folder, i):
        '''
        make reward for one folder and one intersection,
        add the samples of one intersection into the list.samples_all_intersection[i]
        :param i: intersection id
        :return:
        '''
        if self.samples_all_intersection[i] is None:
            self.samples_all_intersection[i] = []

        if i % 100 == 0:
            print("make reward for inter {0} in folder {1}".format(i, folder))

        list_samples = []

        try:
            total_time = int(self.logging_data_list_per_gen[i][-1]['time'] + 1)
            # construct samples
            time_count = 0
            for time in range(0, total_time - self.measure_time + 1, self.interval):
                state = self.construct_state(self.dic_traffic_env_conf["LIST_STATE_FEATURE"], time, i)
                reward_instant, reward_average = self.construct_reward(self.dic_traffic_env_conf["DIC_REWARD_INFO"],
                                                                       time, i)
                action = self.judge_action(time, i)

                if time + self.interval == total_time:
                    next_state = self.construct_state(self.dic_traffic_env_conf["LIST_STATE_FEATURE"],
                                                      time + self.interval - 1, i)

                else:
                    next_state = self.construct_state(self.dic_traffic_env_conf["LIST_STATE_FEATURE"],
                                                      time + self.interval, i)
                sample = [state, action, next_state, reward_average, reward_instant, time,
                          folder+"-"+"round_{0}".format(self.cnt_round)]
                list_samples.append(sample)

            self.samples_all_intersection[i].extend(list_samples)
            return 1
        except Exception as e:
            print("Error occurs when making rewards in generator {0} for intersection {1}".format(folder, i))
            print('traceback.format_exc():\n%s' % traceback.format_exc())
            return 0

    def make_reward_for_system(self):
        '''
        Iterate all the generator folders, and load all the logging data for all intersections for that folder
        At last, save all the logging data for that intersection [all the generators]
        :return:
        '''
        for folder in os.listdir(self.path_to_samples):
            print(folder)
            if "generator" not in folder:
                continue
            if self.dic_traffic_env_conf['MODEL_NAME'] == "STGAT":
                if not self.evaluate_sample(folder) or not self.load_data_for_system(folder) or not self.load_hidden_state_for_system(folder):
                    continue
            else:
                if not self.evaluate_sample(folder) or not self.load_data_for_system(folder):
                    continue

            for i in range(self.dic_traffic_env_conf['NUM_INTERSECTIONS']):
                pass_code = self.make_reward(folder, i)
                if pass_code == 0:
                    continue

        for i in range(self.dic_traffic_env_conf['NUM_INTERSECTIONS']):
            self.dump_sample(self.samples_all_intersection[i],"inter_{0}".format(i))

        if self.dic_traffic_env_conf["MODEL_NAME"]=="STGAT":
            self.dump_hidden_states("")

    def dump_hidden_states(self, folder):
        total_hidden_states = np.vstack(self.hidden_states_list)
        print("dump_hidden_states shape:",total_hidden_states.shape)
        if folder == "":
            with open(os.path.join(self.parent_dir, "total_hidden_states.pkl"),"ab+") as f:
                pickle.dump(total_hidden_states, f, -1)
        elif "inter" in folder:
            with open(os.path.join(self.parent_dir, "total_hidden_states_{0}.pkl".format(folder)),"ab+") as f:
                pickle.dump(total_hidden_states, f, -1)
        else:
            with open(os.path.join(self.path_to_samples, folder, "hidden_states_{0}.pkl".format(folder)),'wb') as f:
                pickle.dump(total_hidden_states, f, -1)

    def evaluate_sample(self, generator_folder):
        return True
        print("Evaluate samples")
        list_files = os.listdir(os.path.join(self.path_to_samples, generator_folder, ""))
        df = []
        # print(list_files)
        for file in list_files:
            if ".csv" not in file:
                continue
            data = pd.read_csv(os.path.join(self.path_to_samples, generator_folder, file))
            df.append(data)
        df = pd.concat(df)
        num_vehicles = len(df['Unnamed: 0'].unique()) -len(df[df['leave_time'].isna()]['leave_time'])
        if num_vehicles < self.dic_traffic_env_conf['VOLUME']* self.dic_traffic_env_conf['NUM_ROW'] and self.cnt_round > 10: # Todo Heuristic
            return False
        else:
            return True

    def dump_sample(self, samples, folder):
        if folder == "":
            with open(os.path.join(self.parent_dir, "total_samples.pkl"),"ab+") as f:
                pickle.dump(samples, f, -1)
        elif "inter" in folder:
            with open(os.path.join(self.parent_dir, "total_samples_{0}.pkl".format(folder)),"ab+") as f:
                pickle.dump(samples, f, -1)
        else:
            with open(os.path.join(self.path_to_samples, folder, "samples_{0}.pkl".format(folder)),'wb') as f:
                pickle.dump(samples, f, -1)