text:
import tensorflow as tf
import os
import numpy as np
import re

class DataLoader:
    LOW_IMG_SIZE = (36, 200)
    MID_IMG_SIZE = (48, 260)
    HIGH_IMG_SIZE = (60, 320)

    def __init__(self, batch_size, train_root_path, test_root_path):
        self.batch_size = batch_size
        self.train_root_path = train_root_path
        self.test_root_path = test_root_path

    def train_init(self):
        train_files = []

        '''오른쪽/왼쪽 눈 파일 경로 불러오기'''
        for (path, dirs, files) in os.walk(self.train_root_path):
            for file in files:
                train_files.append(os.path.join(path, file))

        '''오른쪽/왼쪽 눈 파일 개수'''
        self.train_tot_len = len(train_files)

        '''오른쪽/왼쪽 눈 x, y 데이터 설정'''
        train_random_sort = np.random.permutation(self.train_tot_len)

        train_x = np.asarray(train_files)[train_random_sort]
        train_y = [int(re.search('.*\\\\(.*)\\\\.*\.png', x, re.IGNORECASE).group(1)) for x in train_x]

        '''리스트 or 배열 텐서화'''
        with tf.variable_scope('train_data_tensor'):
            self.train_x = tf.convert_to_tensor(train_x, dtype=tf.string, name='train_x')
            self.train_y = tf.convert_to_tensor(train_y, dtype=tf.int64, name='train_y')

    def test_init(self):
        test_files = []

        '''오른쪽/왼쪽 눈 파일 경로 불러오기'''
        for (path, dirs, files) in os.walk(self.test_root_path):
            for file in files:
                test_files.append(os.path.join(path, file))

        '''오른쪽/왼쪽 눈 파일 개수'''
        self.test_tot_len = len(test_files)

        '''오른쪽/왼쪽 눈 x, y 데이터 설정'''
        test_random_sort = np.random.permutation(self.test_tot_len)

        test_x = np.asarray(test_files)[test_random_sort]
        test_y = [int(re.search('.*\\\\(.*)\\\\.*\.png', x, re.IGNORECASE).group(1)) for x in test_x]

        '''리스트 or 배열 텐서화'''
        with tf.variable_scope('test_data_tensor'):
            self.test_x = tf.convert_to_tensor(test_x, dtype=tf.string, name='test_x')
            self.test_y = tf.convert_to_tensor(test_y, dtype=tf.int64, name='test_y')

    '''
        훈련 데이터 로딩 부분
    '''
    def tf_equalize_histogram(self, image):
        values_range = tf.constant([0., 255.], dtype=tf.float32)
        histogram = tf.histogram_fixed_width(tf.to_float(image), values_range, 256)
        cdf = tf.cumsum(histogram)
        cdf_min = cdf[tf.reduce_min(tf.where(tf.greater(cdf, 0)))]

        img_shape = tf.shape(image)
        pix_cnt = img_shape[-3] * img_shape[-2]
        px_map = tf.round(tf.to_float(cdf - cdf_min) * 255. / tf.to_float(pix_cnt - 1))
        px_map = tf.cast(px_map, tf.uint8)

        eq_hist = tf.expand_dims(tf.gather_nd(px_map, tf.cast(image, tf.int32)), 2)
        return eq_hist

    '''
        Low Resolution Augmentation
    '''
    def train_low_normal(self, x, y):
        with tf.variable_scope('train_low_normal'):
            x = tf.read_file(x)
            x = tf.image.decode_png(x, channels=1, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.LOW_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_low_crop(self, x, y):
        with tf.variable_scope('train_low_crop'):
            x = tf.read_file(x)
            x = tf.image.decode_png(x, channels=1, name='decode_img')
            x = tf.image.resize_images(x, size=(50, 220), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.random_crop(value=x, size=(DataLoader.LOW_IMG_SIZE[0], DataLoader.LOW_IMG_SIZE[1], 1))
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_low_brightness(self, x, y):
        with tf.variable_scope('train_low_brightness'):
            x = tf.read_file(x)
            x = tf.image.decode_jpeg(x, channels=3, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.LOW_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.image.random_brightness(x, max_delta=0.5)
            x = tf.image.rgb_to_grayscale(x)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_low_contrast(self, x, y):
        with tf.variable_scope('train_low_contrast'):
            x = tf.read_file(x)
            x = tf.image.decode_jpeg(x, channels=3, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.LOW_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.image.random_contrast(x, lower=0.2, upper=2.0)
            x = tf.image.rgb_to_grayscale(x)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_low_hue(self, x, y):
        with tf.variable_scope('train_low_hue'):
            x = tf.read_file(x)
            x = tf.image.decode_jpeg(x, channels=3, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.LOW_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.image.random_hue(x, max_delta=0.08)
            x = tf.image.rgb_to_grayscale(x)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_low_saturation(self, x, y):
        with tf.variable_scope('train_low_saturation'):
            x = tf.read_file(x)
            x = tf.image.decode_jpeg(x, channels=3, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.LOW_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.image.random_saturation(x,lower=0.2, upper=2.0)
            x = tf.image.rgb_to_grayscale(x)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    '''
        Mid Resolution Augmentation
    '''
    def train_mid_normal(self, x, y):
        with tf.variable_scope('train_mid_normal'):
            x = tf.read_file(x)
            x = tf.image.decode_png(x, channels=1, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.MID_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_mid_crop(self, x, y):
        with tf.variable_scope('train_mid_crop'):
            x = tf.read_file(x)
            x = tf.image.decode_png(x, channels=1, name='decode_img')
            x = tf.image.resize_images(x, size=(60, 280), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.random_crop(value=x, size=(DataLoader.MID_IMG_SIZE[0], DataLoader.MID_IMG_SIZE[1], 1))
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_mid_brightness(self, x, y):
        with tf.variable_scope('train_mid_brightness'):
            x = tf.read_file(x)
            x = tf.image.decode_jpeg(x, channels=3, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.MID_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.image.random_brightness(x, max_delta=0.5)
            x = tf.image.rgb_to_grayscale(x)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_mid_contrast(self, x, y):
        with tf.variable_scope('train_mid_contrast'):
            x = tf.read_file(x)
            x = tf.image.decode_jpeg(x, channels=3, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.MID_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.image.random_contrast(x, lower=0.2, upper=2.0)
            x = tf.image.rgb_to_grayscale(x)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_mid_hue(self, x, y):
        with tf.variable_scope('train_mid_hue'):
            x = tf.read_file(x)
            x = tf.image.decode_jpeg(x, channels=3, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.MID_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.image.random_hue(x, max_delta=0.08)
            x = tf.image.rgb_to_grayscale(x)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_mid_saturation(self, x, y):
        with tf.variable_scope('train_mid_saturation'):
            x = tf.read_file(x)
            x = tf.image.decode_jpeg(x, channels=3, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.MID_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.image.random_saturation(x, lower=0.2, upper=2.0)
            x = tf.image.rgb_to_grayscale(x)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    '''
        High Resolution Augmentation
    '''
    def train_high_normal(self, x, y):
        with tf.variable_scope('train_high_normal'):
            x = tf.read_file(x)
            x = tf.image.decode_png(x, channels=1, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.HIGH_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_high_crop(self, x, y):
        with tf.variable_scope('train_high_crop'):
            x = tf.read_file(x)
            x = tf.image.decode_png(x, channels=1, name='decode_img')
            x = tf.image.resize_images(x, size=(70, 340), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.random_crop(value=x, size=(DataLoader.HIGH_IMG_SIZE[0], DataLoader.HIGH_IMG_SIZE[1], 1))
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_high_brightness(self, x, y):
        with tf.variable_scope('train_high_brightness'):
            x = tf.read_file(x)
            x = tf.image.decode_jpeg(x, channels=3, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.HIGH_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.image.random_brightness(x, max_delta=0.5)
            x = tf.image.rgb_to_grayscale(x)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_high_contrast(self, x, y):
        with tf.variable_scope('train_high_contrast'):
            x = tf.read_file(x)
            x = tf.image.decode_jpeg(x, channels=3, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.HIGH_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.image.random_contrast(x, lower=0.2, upper=2.0)
            x = tf.image.rgb_to_grayscale(x)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_high_hue(self, x, y):
        with tf.variable_scope('train_high_hue'):
            x = tf.read_file(x)
            x = tf.image.decode_jpeg(x, channels=3, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.HIGH_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.image.random_hue(x, max_delta=0.08)
            x = tf.image.rgb_to_grayscale(x)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def train_high_saturation(self, x, y):
        with tf.variable_scope('train_high_saturation'):
            x = tf.read_file(x)
            x = tf.image.decode_jpeg(x, channels=3, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.HIGH_IMG_SIZE, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            x = tf.image.random_saturation(x, lower=0.2, upper=2.0)
            x = tf.image.rgb_to_grayscale(x)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def dataset_batch_loader(self, dataset, ref_func, name):
        '''
            dataset.map().batch(): 데이터셋의 맵함수를 통해 배치사이즈별로 잘라내는데 사용하는 함수를 맵함수 안에 넣어준다.
            dataset_map.make_one_shot_iterator(): 데이터셋을 이터레이터를 통해 지속적으로 불러준다.
            iterator.get_next(): 세션이 런 될 때마다 반복해서 이터레이터를 소환한다. 그렇게 해서 다음 배치 데이터셋을 불러온다.
        '''
        with tf.variable_scope(name_or_scope=name):
            dataset_map = dataset.map(ref_func).batch(self.batch_size)
            iterator = dataset_map.make_one_shot_iterator()
            batch_input = iterator.get_next()
        return batch_input

    def train_low_loader(self):
        with tf.variable_scope('train_low_loader'):
            '''데이터셋을 불러오는데 불러오는 데이터는 텐서타입이어서 배치단위로 계속 부르기 위해 텐서슬라이스 함수를 반복적으로 사용한다.'''
            dataset = tf.contrib.data.Dataset.from_tensor_slices((self.train_x, self.train_y)).repeat()

            normal_batch = self.dataset_batch_loader(dataset, self.train_low_normal, name='normal_loader')
            crop_batch = self.dataset_batch_loader(dataset, self.train_low_crop, name='crop_loader')
            brightness_batch = self.dataset_batch_loader(dataset, self.train_low_brightness, name='brightness_loader')
            contrast_batch = self.dataset_batch_loader(dataset, self.train_low_contrast, name='contrast_loader')
            hue_batch = self.dataset_batch_loader(dataset, self.train_low_hue, name='hue_loader')
            saturation_batch = self.dataset_batch_loader(dataset, self.train_low_saturation, name='saturation_loader')

        return normal_batch, crop_batch, brightness_batch, contrast_batch, hue_batch, saturation_batch

    def train_mid_loader(self):
        with tf.variable_scope('train_mid_loader'):
            '''데이터셋을 불러오는데 불러오는 데이터는 텐서타입이어서 배치단위로 계속 부르기 위해 텐서슬라이스 함수를 반복적으로 사용한다.'''
            dataset = tf.contrib.data.Dataset.from_tensor_slices((self.train_x, self.train_y)).repeat()

            normal_batch = self.dataset_batch_loader(dataset, self.train_mid_normal, name='normal_loader')
            crop_batch = self.dataset_batch_loader(dataset, self.train_mid_crop, name='crop_loader')
            brightness_batch = self.dataset_batch_loader(dataset, self.train_mid_brightness, name='brightness_loader')
            contrast_batch = self.dataset_batch_loader(dataset, self.train_mid_contrast, name='contrast_loader')
            hue_batch = self.dataset_batch_loader(dataset, self.train_mid_hue, name='hue_loader')
            saturation_batch = self.dataset_batch_loader(dataset, self.train_mid_saturation, name='saturation_loader')

        return normal_batch, crop_batch, brightness_batch, contrast_batch, hue_batch, saturation_batch

    def train_high_loader(self):
        with tf.variable_scope('train_high_loader'):
            '''데이터셋을 불러오는데 불러오는 데이터는 텐서타입이어서 배치단위로 계속 부르기 위해 텐서슬라이스 함수를 반복적으로 사용한다.'''
            dataset = tf.contrib.data.Dataset.from_tensor_slices((self.train_x, self.train_y)).repeat()

            normal_batch = self.dataset_batch_loader(dataset, self.train_high_normal, name='normal_loader')
            crop_batch = self.dataset_batch_loader(dataset, self.train_high_crop, name='crop_loader')
            brightness_batch = self.dataset_batch_loader(dataset, self.train_high_brightness, name='brightness_loader')
            contrast_batch = self.dataset_batch_loader(dataset, self.train_high_contrast, name='contrast_loader')
            hue_batch = self.dataset_batch_loader(dataset, self.train_high_hue, name='hue_loader')
            saturation_batch = self.dataset_batch_loader(dataset, self.train_high_saturation, name='saturation_loader')

        return normal_batch, crop_batch, brightness_batch, contrast_batch, hue_batch, saturation_batch

    def test_low_normal(self, x, y):
        with tf.variable_scope('test_low_normal'):
            x = tf.read_file(x)
            x = tf.image.decode_png(x, channels=1, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.LOW_IMG_SIZE)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def test_mid_normal(self, x, y):
        with tf.variable_scope('test_mid_normal'):
            x = tf.read_file(x)
            x = tf.image.decode_png(x, channels=1, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.MID_IMG_SIZE)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def test_high_normal(self, x, y):
        with tf.variable_scope('test_high_normal'):
            x = tf.read_file(x)
            x = tf.image.decode_png(x, channels=1, name='decode_img')
            x = tf.image.resize_images(x, size=DataLoader.HIGH_IMG_SIZE)
            x = self.tf_equalize_histogram(x)
            x = tf.divide(tf.cast(x, tf.float32), 255.)
        return x, y

    def test_low_loader(self):
        with tf.variable_scope('test_low_loader'):
            dataset = tf.contrib.data.Dataset.from_tensor_slices((self.test_x, self.test_y)).repeat()

            normal_dataset_map = dataset.map(self.test_low_normal).batch(self.batch_size)
            normal_iterator = normal_dataset_map.make_one_shot_iterator()
            normal_batch = normal_iterator.get_next()

        return normal_batch

    def test_mid_loader(self):
        with tf.variable_scope('test_mid_loader'):
            dataset = tf.contrib.data.Dataset.from_tensor_slices((self.test_x, self.test_y)).repeat()

            normal_dataset_map = dataset.map(self.test_mid_normal).batch(self.batch_size)
            normal_iterator = normal_dataset_map.make_one_shot_iterator()
            normal_batch = normal_iterator.get_next()

        return normal_batch

    def test_high_loader(self):
        with tf.variable_scope('test_high_loader'):
            dataset = tf.contrib.data.Dataset.from_tensor_slices((self.test_x, self.test_y)).repeat()

            normal_dataset_map = dataset.map(self.test_high_normal).batch(self.batch_size)
            normal_iterator = normal_dataset_map.make_one_shot_iterator()
            normal_batch = normal_iterator.get_next()

        return normal_batch


text:
#!/usr/bin/env python
import numpy as np
import pdb

input                   = 'period_Azm_tomo.inv'
output                  = 'Period_Azm_reSmp.inv'

data                    = np.loadtxt(input, dtype=np.float64)

nx                      = 38 -2
ny                      = 42 - 2
nt                      = 36

# add percentage column
newdata                 = np.zeros((data.shape[0], data.shape[1]+1));
newdata[:, 0:4]         = data[:, 0:4]
newdata[:, 5:]          = data[:, 4:]

num                     = nx * ny

for zz in np.arange(nt):
    rows                = np.arange(num*zz, num*(zz+1))
    meanp               = np.mean(data[rows, 3])
    newdata[rows, 3]    = (data[rows, 3]-meanp)/meanp*100
    newdata[rows, 4]    = meanp

lineArr                 = []
for zz in np.arange(0, nt):
    for yy in np.arange(0, ny, 2):
        for xx in np.arange(0, nx, 2):
            index       = xx + yy * nx + zz * nx * ny
            lineArr.append(index)

dataRe                  = newdata[lineArr, :]
fmt                     = '%9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f'
np.savetxt(output, dataRe, delimiter=" ", fmt=fmt)

ext: py

max_stars_repo_name: core-man/DAzimSurfTomo

max_issues_repo_name: core-man/DAzimSurfTomo

max_forks_repo_name: core-man/DAzimSurfTomo

reason: import numpy


text:
import cv2
import numpy as np
import os

WIDTH = 400
HEIGHT = 250
EPSILON = 1e-7

def half_plane(empty_map, pt1, pt2, fill_right=True):
    map_copy = empty_map.copy()

    x = np.arange((map_copy.shape[1]))
    y = np.arange((map_copy.shape[0]))
    X, Y = np.meshgrid(x, y)

    slope = (pt1[1] - pt2[1]) / (pt1[0] - pt2[0] + EPSILON)
    bias = pt1[1] - slope * pt1[0]

    value = Y - slope * X - bias

    if fill_right:
        map_copy[value > 0] = 0
    else:
        map_copy[value <= 0] = 0

    return map_copy

def fill_polygon(empty_map, line_points, display_progress=False):
    line_1_points = line_points[0]
    line_2_points = line_points[1]
    line_3_points = [line_2_points[1], line_1_points[0]]

    line_4_points = [line_2_points[1], line_1_points[0]]
    line_5_points = line_points[2]
    line_6_points = line_points[3]

    half_plane_1 = half_plane(empty_map, line_1_points[0], line_1_points[1], True)

    half_plane_2 = half_plane(empty_map, line_2_points[0], line_2_points[1], False)

    half_plane_3 = half_plane(empty_map, line_3_points[0], line_3_points[1], False)

    half1 = half_plane_1 + half_plane_2 + half_plane_3

    half_plane_4 = half_plane(empty_map, line_4_points[0], line_4_points[1], True)

    half_plane_5 = half_plane(empty_map, line_5_points[0], line_5_points[1], True)

    half_plane_6 = half_plane(empty_map, line_6_points[0], line_6_points[1], False)

    half2 = half_plane_4 + half_plane_5 + half_plane_6
    print(display_progress)
    if display_progress:

        cv2.namedWindow('img', cv2.WINDOW_NORMAL)
        cv2.moveWindow('img', 10, 10)

        cv2.imshow('img', half_plane_1)
        cv2.waitKey(0)

        cv2.imshow('img', half_plane_2)
        cv2.waitKey(0)

        cv2.imshow('img', half_plane_3)
        cv2.waitKey(0)

        cv2.imshow('img', half1)
        cv2.waitKey(0)

        cv2.imshow('img', half_plane_4)
        cv2.waitKey(0)

        cv2.imshow('img', half_plane_5)
        cv2.waitKey(0)

        cv2.imshow('img', half_plane_6)
        cv2.waitKey(0)

        cv2.imshow('img', half2)
        cv2.waitKey(0)

    map_copy = cv2.bitwise_and(half2, half1)

    return map_copy

def fill_hexagon(empty_map, center, s2s, display_progress=False):
    hex_line_points = get_hex_line_points(center, s2s)

    line_1_points = hex_line_points[0]
    line_2_points = hex_line_points[1]
    line_3_points = hex_line_points[2]
    line_4_points = hex_line_points[3]
    line_5_points = hex_line_points[4]
    line_6_points = hex_line_points[5]

    half_plane_1 = half_plane(empty_map, line_1_points[0], line_1_points[1], True)

    half_plane_2 = half_plane(empty_map, line_2_points[0], line_2_points[1], False)

    half_plane_3 = half_plane(empty_map, line_3_points[0], line_3_points[1], False)

    half_plane_4 = half_plane(empty_map, line_4_points[0], line_4_points[1], False)

    half_plane_5 = half_plane(empty_map, line_5_points[0], line_5_points[1], False)

    half_plane_6 = half_plane(empty_map, line_6_points[0], line_6_points[1], True)

    half1 = half_plane_1 + half_plane_2

    half2 = half_plane_3 + half1

    half3 = half_plane_4 + half2

    half4 = half_plane_5 + half3

    if display_progress:
        cv2.namedWindow('img', cv2.WINDOW_NORMAL)
        cv2.moveWindow('img', 10, 10)
        cv2.imshow('img', half_plane_1)
        cv2.waitKey(0)
        cv2.imshow('img', half_plane_2)
        cv2.waitKey(0)
        cv2.imshow('img', half_plane_3)
        cv2.waitKey(0)
        cv2.imshow('img', half_plane_4)
        cv2.waitKey(0)
        cv2.imshow('img', half_plane_5)
        cv2.waitKey(0)
        cv2.imshow('img', half_plane_6)
        cv2.waitKey(0)
        cv2.imshow('img', half1)
        cv2.waitKey(0)
        cv2.imshow('img', half2)
        cv2.waitKey(0)
        cv2.imshow('img', half3)
        cv2.waitKey(0)
        cv2.imshow('img', half4)
        cv2.waitKey(0)

    map_copy = half_plane_6 + half4

    return map_copy

def fill_circle(empty_map, center, diameter):
    map_copy = empty_map.copy()

    x = np.arange(map_copy.shape[1])
    y = np.arange(map_copy.shape[0])

    xx, yy = np.meshgrid(x, y)

    map_copy[(xx - center[0]) ** 2 + (yy - center[1]) ** 2 - (diameter // 2) ** 2 <= 0] = 0

    return map_copy

def get_hex_line_points(center, s2s):
    """
            *
        *       *

        *       *
            *
        Order of points in our Hexagon

            1
        6       2

        5       3
            4

    edge = s2s // sqrt(3)

    Point 1 coordinates = (center_x, center_y + edge // 2 + s2s // 2 * sqrt(3))
    Point 2 coordinates = (center_x + s2s // 2, center_y + edge // 2)
    Point 3 coordinates = (center_x + s2s // 2, center_y - edge // 2)
    Point 4 coordinates = (center_x, center_y - edge // 2 - s2s // 2 * sqrt(3))
    Point 5 coordinates = (center_x - s2s // 2, center_y - edge // 2)
    Point 6 coordinates = (center_x - s2s // 2, center_y + edge // 2)

    """

    center_x, center_y = center[0], center[1]
    edge = s2s // np.sqrt(3)

    point_1 = [center_x, center_y - s2s // np.sqrt(3)]
    point_2 = [center_x + s2s // 2, center_y - edge // 2]
    point_3 = [center_x + s2s // 2, center_y + edge // 2]
    point_4 = [center_x, center_y + s2s // np.sqrt(3)]
    point_5 = [center_x - s2s // 2, center_y + edge // 2]
    point_6 = [center_x - s2s // 2, center_y - edge // 2]

    line_1_2 = [point_1, point_2]
    line_2_3 = [point_2, point_3]
    line_3_4 = [point_3, point_4]
    line_4_5 = [point_4, point_5]
    line_5_6 = [point_5, point_6]
    line_6_1 = [point_6, point_1]

    lines = [line_1_2, line_2_3, line_3_4, line_4_5, line_5_6, line_6_1]

    return lines

def fill_map(empty_map, line_points=None, center=None, s2s=None, type='circle', display_progress = False):
    if line_points is None:
        if type == 'circle':
            map_copy = fill_circle(empty_map, center, s2s)
        if type == 'hexagon':
            map_copy = fill_hexagon(empty_map, center, s2s, display_progress)
    else:
        map_copy = fill_polygon(empty_map, line_points, display_progress)

    return map_copy

def generate_map(display_progress=False):
    empty_map = np.ones((HEIGHT, WIDTH), np.uint8) * 255

    # create concave polygon
    line_1_points = [[36, 65], [115, 40]]
    line_2_points = [[115, 40], [80, 70]]
    line_3_points = [[80, 70], [105, 150]]
    line_4_points = [[105, 150], [36, 65]]

    # create hexagon
    hex_center = [200, 150]
    hex_s2s = 70

    # create circle
    circle_center = [300, 65]
    circle_diameter = 80

    map_with_polygon = fill_map(empty_map, [line_1_points, line_2_points, line_3_points, line_4_points], display_progress=display_progress)
    if display_progress:
        cv2.namedWindow('polygon', cv2.WINDOW_NORMAL)
        cv2.moveWindow('polygon', 10, 10)
        cv2.imshow('polygon', map_with_polygon)
        cv2.waitKey(0)

    map_with_hexagon = fill_map(empty_map, None, hex_center, hex_s2s, 'hexagon', display_progress)
    if display_progress:
        cv2.namedWindow('hexagon', cv2.WINDOW_NORMAL)
        cv2.moveWindow('hexagon', 10, 10)
        cv2.imshow('hexagon', map_with_hexagon)
        cv2.waitKey(0)

    map_with_circle = fill_map(empty_map, None, circle_center, circle_diameter, 'circle', display_progress)
    if display_progress:
        cv2.namedWindow('circle', cv2.WINDOW_NORMAL)
        cv2.moveWindow('circle', 10, 10)
        cv2.imshow('circle', map_with_circle)
        cv2.waitKey(0)

    final_map = cv2.bitwise_and(cv2.bitwise_and(map_with_polygon, map_with_hexagon), map_with_circle)
    if display_progress:
        cv2.namedWindow('final_map', cv2.WINDOW_NORMAL)
        cv2.moveWindow('final_map', 10, 10)
        cv2.imshow('final_map', final_map)
        cv2.waitKey(0)

    return final_map

def start_map_generation(display_progress):
    out_dir = "./"

    final_map = generate_map(display_progress)

    cv2.imwrite(os.path.join(out_dir, "final_map.png"), final_map)

if __name__ == "__main__":

    start_map_generation(display_progress=True)


text:
import os, sys, argparse

def get_gold_sent_buget(label_file, buget_file):
    bugets = []
    with open(buget_file, 'w', encoding='utf8', buffering=1) as fout:
        for line in open(label_file, encoding='utf8'):
            nsent = line.count('T')
            if nsent == 0: nsent = 1
            fout.write( '{}\n'.format(nsent) )
            bugets.append(nsent)

    import numpy
    bugets = numpy.asarray(bugets)
    print( 'sentence buget: min {} max {} avg {} std {}'.format(bugets.min(), bugets.max(), bugets.mean(), bugets.std()) )

def get_gold_word_buget(sum_file, buget_file):
    bugets = []
    with open(buget_file, 'w', encoding='utf8', buffering=1) as fout:
        for line in open(sum_file, encoding='utf8'):
            nword = len(line.strip().split())
            if nword == 0: nsent = 1
            fout.write( '{}\n'.format(nword) )
            bugets.append(nword)

    import numpy
    bugets = numpy.asarray(bugets)
    print( 'word buget: min {} max {} avg {} std {}'.format(bugets.min(), bugets.max(), bugets.mean(), bugets.std()) )

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('gold_file')
    parser.add_argument('buget_file')
    parser.add_argument('--type', default='sentence')
    args = parser.parse_args()

    if args.type == 'sentence':
        get_gold_sent_buget(args.gold_file, args.buget_file)
    elif args.type == 'word':
        get_gold_word_buget(args.gold_file, args.buget_file)

max_stars_repo_name: xssstory/STAS

max_issues_repo_name: xssstory/STAS

max_forks_repo_name: xssstory/STAS


text:
'''Code modified from: https://github.com/coxlab/prednet'''

# Ensure random seed
import numpy as np
import tensorflow as tf
import random as rn
np.random.seed(123)
rn.seed(123)
from keras import backend as K
tf.set_random_seed(123)

import hickle as hkl
from keras.preprocessing.image import Iterator
from copy import deepcopy

# Data generator that creates sequences for input into PredNet.
class SequenceGenerator(Iterator):
	def __init__(self, data_file, source_file, nt,
				 batch_size=8, shuffle=False, seed=None,
				 output_mode='error', sequence_start_mode='all', N_seq=None,
				 data_format=K.image_data_format()):

		self.X = hkl.load(data_file)

		# X will be like (n_images, nb_cols, nb_rows, nb_channels)
		self.sources = hkl.load(source_file)
		self.nt = nt
		self.batch_size = batch_size
		self.data_format = data_format
		assert sequence_start_mode in {'all', 'unique'}, 'sequence_start_mode must be in {all, unique}'
		self.sequence_start_mode = sequence_start_mode
		assert output_mode in {'error', 'prediction'}, 'output_mode must be in {error, prediction}'
		self.output_mode = output_mode

		if (self.data_format == 'channels_last') and ((self.X.shape[3] != 1) and (self.X.shape[3] != 3) and (self.X.shape[3] != 4) and (self.X.shape[3] != 2)):
			self.X = np.transpose(self.X, (0, 2, 3, 1))

		self.im_shape = self.X[0].shape

		print "SHAPE", self.X.shape

		if self.sequence_start_mode == 'all':  # allow for any possible sequence, starting from any frame
			self.possible_starts = np.array([i for i in range(self.X.shape[0] - self.nt) if self.sources[i] == self.sources[i + self.nt - 1]])
		elif self.sequence_start_mode == 'unique':  # create sequences where each unique frame is in at most one sequence
			curr_location = 0
			possible_starts = []
			while curr_location < self.X.shape[0] - self.nt + 1:
				if self.sources[curr_location] == self.sources[curr_location + self.nt - 1]:
					possible_starts.append(curr_location)
					curr_location += self.nt
				else:
					curr_location += 1
			self.possible_starts = possible_starts

		if shuffle:
			self.possible_starts = np.random.permutation(self.possible_starts)
		if N_seq is not None and len(self.possible_starts) > N_seq:  # select a subset of sequences if want to
			self.possible_starts = self.possible_starts[:N_seq]
		self.N_sequences = len(self.possible_starts)
		super(SequenceGenerator, self).__init__(len(self.possible_starts), batch_size, shuffle, seed)

	def next(self):
		with self.lock:
			index_array, current_index, current_batch_size = next(self.index_generator)
		batch_x = np.zeros((current_batch_size, self.nt) + self.im_shape, np.float16)
		for i, idx in enumerate(index_array):
			idx = self.possible_starts[idx]
			batch_x[i] = self.preprocess(self.X[idx:idx+self.nt])
		if self.output_mode == 'error':  # model outputs errors, so y should be zeros
			batch_y = np.zeros(current_batch_size, np.float16)
		elif self.output_mode == 'prediction':  # output actual pixels
			batch_y = batch_x
		return batch_x, batch_y

	def preprocess(self, X):
		return  X.astype(np.float16)

	def create_all(self):
		X_all = np.zeros((self.N_sequences, self.nt) + self.im_shape, np.float16) # np.float32)
		for i, idx in enumerate(self.possible_starts):
			X_all[i] = self.preprocess(self.X[idx:idx+self.nt])
		return X_all

max_stars_repo_name: mark-koren/AdaptiveStressTestingToolbox

max_issues_repo_name: mark-koren/AdaptiveStressTestingToolbox

max_forks_repo_name: mark-koren/AdaptiveStressTestingToolbox


text:
#! /usr/bin/python
#
# This is the answer code for the course "Learning from Data" on edX.org
# https://www.edx.org/course/caltechx/cs1156x/learning-data/1120
#
# The software is intended for course usage, no guarantee whatsoever.
# Date: 10/4/2013
# Created by: kirbs
# See notes at bottom for further details.

import sys
import os
import random
import pylab
import scipy
import numpy as np

#############################################################################
#############################################################################

# Returns a list of points with y (indicating 1/-1) as the last element
# and the x,y coordinates for the two points separating line.
# Returns a list of points; each point is a list in the following format.
# [x0, x1, x2, y] i.e. [dummy 1 to represent threshold, x1 value, x2 value, sample points correct sign (+1/-1)]
def generatePoints(numberOfPoints):
##    random.seed(1) # used for testing
    x1 = random.uniform(-1, 1)
    y1 = random.uniform(-1, 1)
    x2 = random.uniform(-1, 1)
    y2 = random.uniform(-1, 1)
    points = []

    for i in range (0,numberOfPoints - 1):
##        random.seed(1) # used for testing
        x = random.uniform (-1, 1)
        y = random.uniform (-1, 1)
        points.append([1, x, y, targetFunction(x1, y1, x2, y2, x, y)]) # add 1/-1 indicator to the end of each point list
    return x1, y1, x2, y2, points

# This function determines the cross product between a line and a given point.
# Returns 1 if above the line and -1 if below the line.
def targetFunction(x1,y1,x2,y2,x3,y3):
    u = (x2-x1)*(y3-y1) - (y2-y1)*(x3-x1)
    if u >= 0:
        return 1
    elif u < 0:
        return -1

# Simple sign function
def sign(y):
    if y >= 0:
        return 1
    elif y < 0:
        return -1

# a.k.a dot product
def perceptronCalc(x, w):
    return x[0]*w[0] + x[1]*w[1] + x[2]*w[2]

def train(training_points, iterationLimit):
    w = [0.0,0.0,0.0] # initialize weights for w[0], w[1], w[2]
    learned = False
    iterations = 0 # keep track of the iteration count

    # This method is the primary PLA implentation.
    # It returns True when all sample points are corectly classfied by the hypothesis.
    # Returns False if there was a misclassified point and the weight vector changed.
    def updateWeights():
        random.shuffle(training_points) # randomize training points
        for point in training_points:
            result = sign(perceptronCalc(point,w)) # caclulate point and determine its sign.
            if point[3] != result: # does sample point's result match our calculated result?
                # Use line below to watch the perceptron's weights change
                # print str(iterations) + " " + str(w) + " " + str(result) + " " + str(point) + " " + str(perceptronCalc(point))

                # if not update weights by sample point's result
                w[0] += point[0]*point[3]
                w[1] += point[1]*point[3]
                w[2] += point[2]*point[3]

                return False # break out of loop and return
        return True # if the loop reaches this point all calculated points in the training points match their expected y's

    while not learned:
        iterations += 1
        noErrors = updateWeights()
        if iterations == iterationLimit or noErrors:
            learned = True
            break

    return iterations, w

# Calculates approximate probability of hypothesis function returns a result
# that is different from the target function.
def findErrorProbability(x1,y1,x2,y2, weights, numberOfPointsToTest):
    numberOfErrors = 0
    for i in range(0, numberOfPointsToTest-1):
        #generate random test points
        x = random.uniform(-1,1)
        y = random.uniform(-1,1)

        #compare results from target function and hypothesis function
        if targetFunction(x1,y1,x2,y2,x,y) != sign(perceptronCalc([1,x,y], weights)):
            numberOfErrors += 1 # keep track of errors
    return numberOfErrors/float(numberOfPointsToTest)

# Runs runTrial specified number of times.
# Returns average iterations, average error probability, and a histogram of trial iteration count.
def runSimulation(numberOfTrials, numberOfTestPoints, iterationLimit):
    interations = []
    probability = []
    for t in range(1,numberOfTrials+1):
        iteration_count, w, error_probability = runTrial(numberOfTestPoints, iterationLimit)
        interations.append(iteration_count)
        probability.append(error_probability)

    print "Avg. iterations: " + str(np.mean(interations)) + " : Avg. error probability: " + str(np.mean(probability))
    pylab.hist(interations)
    pylab.show()

# Runs one trial based on the number of test points desired and an iteration limit to cap run time.
# If showChart is set to True, this function with also return a chart of the points, target function and hypothesis.
# Returns the number of iterations perceptron took to converge, final weights, and the error probability.
def runTrial(numberOfTestPoints, iterationLimit, showChart = False):
    x1, y1, x2, y2, points = generatePoints(numberOfTestPoints)
    iterations, w = train(points, iterationLimit)
    errorProb = findErrorProbability(x1,y1,x2,y2,w, 10000)

    if showChart:
        if iterations == iterationLimit:
            print "No solution found in " + str(iterations) + " iterations!"
        print "Iterations: " + str(iterations) + ' | Weights: ' + str(w)

        # plot points above(green) and below(blue) the target function.
        green_x = []
        green_y = []
        blue_x = []
        blue_y = []
        for x in points:
            if x[3] == 1:
                green_x.append(x[1])
                green_y.append(x[2])
            else:
                blue_x.append(x[1])
                blue_y.append(x[2])
        pylab.plot(green_x, green_y, 'go')
        pylab.plot(blue_x, blue_y, 'bo')

        # plot target function(black) and hypothesis function(red) lines
        x = np.array( [-1,1] )
        slope = (y2-y1)/(x2-x1)
        intercept = y2 - slope * x2
        pylab.plot(x, slope*x + intercept, 'k--')
        pylab.plot( x, -w[1]/w[2] * x - w[0] / w[2] , 'r' ) # this will throw an error if w[2] == 0
        pylab.ylim([-1,1])
        pylab.xlim([-1,1])
        pylab.show()

    return iterations, w, errorProb

########################################################################
############################----NOTES----###############################
########################################################################
# Uncomment one line below and reload the script in your favorite Python
# environment. Or load the script and type the method with requireed
# paramaters you want to execute.

########################################################################
########################################################################
# runSimulation takes 3 arguments, number of trials to run, number of test points, and interation limit.
# The higher you set each parameter, the longer this method takes to run.
# This will return the average number of iterations the perceptron took to converge
# and the average error probability.

# Question 7/8
# runSimulation(1000, 10, 100)

# Question 9/10
# runSimulation(1000, 100, 1000)

#########################################################################
#########################################################################
# runTrial takes 3 arguments, number of points, iteration limit, and boolean if a chart should be shown.
# This method returns the number of iteration perceptron took to converge, the final
# weights vector, and the error probability.

# runTrial(10, 100, True) # Show graph of one trial with points, hypothesis (red line), and target funtion (black line).
# runTrial(10, 100) # No chart
# runTrial(10, 100, False) # No chart

reason: import numpy,import scipy


text:
#!/usr/bin/env python

# --------------------------------------------------------
# Faster R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------

INPUT_MODE = 0 #0 = images, 1 = Camera, 2 = ScreenCapture
GUI = 1
RECORD = 1
PRINT_STATS = 0
MODE = 1 #0 = sub, 1 = RObotX

import _init_paths
from fast_rcnn.config import cfg
from fast_rcnn.test import im_detect
from fast_rcnn.nms_wrapper import nms
from utils.timer import Timer
import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
import caffe, os, sys, cv2
import argparse
import time

found_objects = []

class camera: #cameraProperties
    def __init__ (self, camera_model,frame_rate, fov_width, fov_height, focal_length, camera_direction):
        self.cameraModel = camera_model
        self.FRAME_RATE = frame_rate
        self.FOV_WIDTH = fov_width
        self.FOV_HEGIHT = fov_height
        self.FOCAL_LENGTH = focal_length
        self.CAMERA_DIRECTION = camera_direction # 1=Foward, 2=Down

class detectedObject:
    def __init__(self, name):
        self.name = name
        self.px_location = ((None,None),(None,None))
        #self.average_px_location =((None,None),(None, None))
        self.prev_px_location = ((0,0),(0,0))
        self.inFrame = None
        self.width = None#Calculated - Not Shown
        self.height = None#Calculated - Not Shown
        self.rl_location = (None,None,None)#Not Calculated - Not Shown
        self.size_array = np.zeros([5]) #Not Calculated - Not Shown
        self.size = None#Calculated - Not Shown
        self.distance = None#Not Calculated - Not Shown
        self.average_size = None#Not Calculated - Not Shown
        self.life = 1#Calculated -  Shown
        self.confidence = None #Calculated - Not Shown
        self.area = None #Calculated - Not Shown
        self.objectCenter = None #Calculated - Not Shown
        self.time_not_seen = 0
        self.average_location_bin = 3

        self.x1_array = np.zeros([self.average_location_bin])
        self.y1_array = np.zeros([self.average_location_bin])
        self.x2_array = np.zeros([self.average_location_bin])
        self.y2_array = np.zeros([self.average_location_bin])

        self.x1_average = None
        self.y1_average = None
        self.x2_average = None
        self.y2_average = None

    def average_location(self, new_x1, new_y1, new_x2, new_y2):
        self.x1_array = np.roll(self.x1_array,1)
        self.y1_array = np.roll(self.y1_array,1)
        self.x2_array = np.roll(self.x2_array,1)
        self.y2_array = np.roll(self.y2_array,1)

        self.x1_array[self.average_location_bin-1] = new_x1
        self.y1_array[self.average_location_bin-1] = new_y1
        self.x2_array[self.average_location_bin-1] = new_x2
        self.y2_array[self.average_location_bin-1] = new_y2

        self.x1_average = int(np.average(self.x1_array))
        self.y1_average = int(np.average(self.y1_array))
        self.x2_average = int(np.average(self.x2_array))
        self.y2_average = int(np.average(self.y2_array))

    def update_name(self, new_name):
        self.name = new_name

    def update_px_location(self, new_px_location):
        self.px_location = new_px_location

    def update_rl_location(self, new_rl_location):
        self.rl_location = new_rl_location

    def update_size(self, new_size):
        self.size = new_size

    def update_distance(self, new_distance):
        self.distance = new_distance

    def shift_size(self, new_size):
        self.size_array = np.roll(self.size_array,1)
        self.size_array[4] = new_size
        self.update_avg_size()

    def update_avg_size(self):
        self.average_size = np.average(self.size_array)
        #print self.average_size

    def print_sizes(self):
        print self.size_array

    def increment_life(self):
        self.life = self.life + 1

def distance_to_camera_mm(focal_mm,real_height_mm,image_height_px,object_height_px,sensor_height_mm):
    #http://photo.stackexchange.com/questions/12434/how-do-i-calculate-the-distance-of-an-object-in-a-photo
    return (focal_mm*real_height_mm*image_height_px)/(object_height_px * sensor_height_mm)

def distance_to_camera(knownWidth, cameraFocalLength, perceivedWidth):
    if perceivedWidth:
        return ((knownWidth * cameraFocalLength) / perceivedWidth )*.1
    else:
        return 0
if MODE ==0:
	CLASSES = ('__background__', 'gate','redbuoy','greenbuoy','yellowbuoy','path','gateinv','torpedoboard','binbannana','binlightning','bincan','binorange',)#old
#CLASSES = ('__background__', 'gate','redbuoy','greenbuoy','yellowbuoy','path','gateinv','torpedoboard2016','torpedoboard2016cover','n','s','e','w','torpedoboard','binbannana','binlightning','bincan','binorange')
if MODE ==1:
	CLASSES =('__background__', \
	'blacktower','redtower','bluetower','greentower','yellowtower', \
	'redcircle','redtriangle','redcruciform', \
        'greencircle','greentriangle','greencruciform',\
	'bluecircle','bluetriangle','bluecruciform',\
	'greenbuoy','redbuoy','redbuoy','whitebuoy','blackbuoy','yellowbuoy', 'bluebuoy',\
	'orangeball','blackball',\
	'person')  #23 + 1 Classes

NETS = {'vgg16': ('VGG16',
                  'VGG16_faster_rcnn_final.caffemodel'),
        'zf': ('ZF',
                  'ZF_faster_rcnn_final.caffemodel')}

def vis_detections_video(im, class_name, dets, thresh=0.5):
    """Draw detected bounding boxes."""
    inds = np.where(dets[:, -1] >= thresh)[0]

    if len(inds) == 0: #is that class found?
        return im

    for i in inds: #Go through each of that class item
        bbox = dets[i, :4]
        score = dets[i, -1]

        objFound =0
        for fObject in found_objects:
            if fObject.name ==class_name: #Object with that name already exists
                if abs(bbox[0] - fObject.px_location[0][0]) < 300: #
                    if abs(bbox[1] - fObject.px_location[0][1]) < 300:
                        fObject.confidence = score
                        fObject.increment_life()
                        fObject.inFrame = True
                        fObject.prev_px_location =   fObject.px_location
                        fObject.update_px_location(((bbox[0],bbox[1]),(bbox[2],bbox[3])))

                        fObject.average_location(bbox[0],bbox[1],bbox[2],bbox[3])

                        fObject.width = abs(bbox[2]-bbox[0])
                        fObject.height = abs(bbox[3]-bbox[1])
                        fObject.area = fObject.width * fObject.height
                        fObject.objectCenter = (int(bbox[0]+fObject.width/2),int(bbox[1]+fObject.height/2))
                        fObject.time_not_seen = 0

			if MODE == 0:#SUB
		            #GIVES ME CANCER!!! SHOULD BE CLASS WITH LOOKUPS
		            if fObject.name=="gateinv":
		                objSize = 2400.0 # MM Width of Object
		            elif fObject.name =="torpedoboard":
		                objSize = 1200.0
		            elif fObject.name =="gate":
		                objSize = 3050.0
		            elif fObject.name =="path":
		                objSize = 150.0
		            else: #buoys
		            	objSize = 200.0

			if MODE == 1: #RobotX
		            #GIVES ME CANCER!!! SHOULD BE CLASS WITH LOOKUPS
		            if fObject.name=="blacktower" or  fObject.name =='redtower'or fObject.name =='bluetower' or fObject.name =='greentower' or fObject.name =='yellowtower':
		                objSize = 380.0 # MM Width of Object
		            elif fObject.name =="greenbuoy" or fObject.name =='redbuoy' or fObject.name =='whitebuoy' or fObject.name =='blackbuoy' or fObject.name =='yellowbuoy'or fObject.name =='bluebuoy':
		                objSize = 450.0
		            elif fObject.name =="orangeball":
		                objSize = 430.0
		            elif fObject.name =="blackball":
		                objSize = 990.0
		            else: #shapes
		            	objSize = 1370.0

                        fObject.shift_size(fObject.width)

                        fObject.update_distance(distance_to_camera(objSize,cameraProperties.FOCAL_LENGTH,fObject.average_size))
			'''
			fx = 321.147192922
                        fy = 327.401895861
                        cx = 442.213579778
                        cy = 332.232842003
                        c_x =cx
                        c_y = cy
                        z_world = fObject.distance
                        f_x = fx
                        f_y = fy
                        x_screen,y_screen = fObject.objectCenter
                        x_world = (x_screen - c_x) * z_world / f_x
                        y_world = (y_screen - c_y) * z_world / f_y
                        #print x_world,y_world,z_world
                        fObject.rl_location=(x_world,y_world,z_world)
                        '''

			objectCenter = fObject.objectCenter
			frameCenter = (1920/2,1200/2)
			objectWidth = fObject.width
			knownObjectWidth = objSize
			distPerPx =    knownObjectWidth / objectWidth
			xOffSetPx = objectCenter[0] - frameCenter[0]
			yOffSetPx = objectCenter[1] - frameCenter[1]
			x_world = xOffSetPx * distPerPx *.001 *1
			y_world = yOffSetPx * distPerPx *.001 *1 * -1
			z_world = fObject.distance
			fObject.rl_location=(x_world,y_world,z_world)
                        objFound =1
                        break

        if not objFound: #First time any object is found
            newObject = detectedObject(class_name)
            newObject.update_px_location(((bbox[0],bbox[1]),(bbox[2],bbox[3])))
            found_objects.append(newObject)

        cv2.rectangle(im,(bbox[0],bbox[1]),(bbox[2],bbox[3]),(0,0,255),2)

        cv2.rectangle(im,(int(bbox[0]),int(bbox[1]-20)),(int(bbox[0]+200),int(bbox[1])),(180,180,180),-1)
        cv2.putText(im,'{:s} {:.3f}'.format(class_name, score),(int(bbox[0]),int(bbox[1]-2)),cv2.FONT_HERSHEY_SIMPLEX,.75,(200,0,0))#,cv2.CV_AA)
    return im

def demo_video(net, im):
    """Detect object classes in an image using pre-computed object proposals."""

    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(net, im)
    timer.toc()
    print ('Detection took {:.3f}s for '
           '{:d} object proposals    {:.3f}FPS').format(timer.total_time, boxes.shape[0],1/timer.total_time)

    # Visualize detections for each class
    CONF_THRESH = 0.80
    NMS_THRESH = 0.2
    for fObject in found_objects:
        fObject.inFrame = False

    #print 'new frame \n\n\n\n'
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1 # because we skipped background
        cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]

        cls_scores = scores[:, cls_ind]
	#print cls_scores
        dets = np.hstack((cls_boxes,
                          cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        im=vis_detections_video(im, cls, dets, thresh=CONF_THRESH)

    for fObject in found_objects:
        if not fObject.inFrame:
            fObject.time_not_seen +=1

    cv2.circle(im,(1920/2,1200/2),int(5),(0,0,255),thickness=-1,lineType =8,shift=0) #center dot
    textColor = (0,121,255)
    textColor = (200,102,38)
    cv2.putText(im,'Num Objects: {:d}'.format(len(found_objects)),(50,50),cv2.FONT_HERSHEY_SIMPLEX,.75,textColor)#,cv2.CV_AA
    count =3
    for fObject in found_objects:
        #Prints info about all objects
        cv2.putText(im,'{:s} - L: {:d} - D: {:d}'.format(fObject.name, fObject.life, fObject.time_not_seen),(50,25*count),cv2.FONT_HERSHEY_SIMPLEX,.75,textColor)#,cv2.CV_AA
        count +=1
        if fObject.inFrame: #Prints info for only currently seen objects
            cv2.putText(im,'{:s} - Life: {:.0f} - Loc: ({:.1f},{:.1f},{:.1f})'.format(fObject.name, fObject.life,fObject.rl_location[0],fObject.rl_location[1],fObject.rl_location[2]),(int(fObject.px_location[0][0]),int(fObject.px_location[0][1])-50),cv2.FONT_HERSHEY_SIMPLEX,.75,textColor)
            cv2.putText(im,'Width: {:1f} - Avg Width: {:.1f}'.format(fObject.width, fObject.average_size),(int(fObject.px_location[0][0]),int(fObject.px_location[0][1])-75),cv2.FONT_HERSHEY_SIMPLEX,.75,textColor)
            cv2.putText(im,'Distance: {:1f} - Area: {:.1f}'.format(fObject.distance, fObject.area),(int(fObject.px_location[0][0]),int(fObject.px_location[0][1])-100),cv2.FONT_HERSHEY_SIMPLEX,.75,textColor)
          #  cv2.circle(im,fObject.objectCenter,int(5),(255,0,255),thickness=-1,lineType =8,shift=0) #object cirlce
            #cv2.circle(im,(int(fObject.prev_px_location[0][0]),int(fObject.prev_px_location[0][1])),int(5),(255,0,255),thickness=-1,lineType =8,shift=0)
           # cv2.rectangle(im,(fObject.x1_average,fObject.y1_average),(fObject.x2_average,fObject.y2_average),(230,224,176),2) #average object

    if PRINT_STATS:
        for fObject in found_objects:
            pass

    if RECORD:
        cv2.imwrite(os.path.join('output',str(time.time())+'.jpg'),im)

    if GUI:
        cv2.imshow('ret',im)
        cv2.waitKey(20)

def parse_args():
    """Parse input arguments."""
    parser = argparse.ArgumentParser(description='Faster R-CNN demo')
    parser.add_argument('--gpu', dest='gpu_id', help='GPU device id to use [0]',
                        default=0, type=int)
    parser.add_argument('--cpu', dest='cpu_mode',
                        help='Use CPU mode (overrides --gpu)',
                        action='store_true')
    parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16]',
                        choices=NETS.keys(), default='vgg16')

    args = parser.parse_args()

    return args

def getImage(camera,cameraIndex=0, format='bgr', scale=1.0, windowName='Live Video'):
    while cv2.waitKey(1) == -1:
        image = camera.GrabNumPyImage(format)
        if scale != 1.0:
            image = cv2.resize(image, (0, 0), fx=scale, fy=scale)
            return image
        return image

if __name__ == '__main__':
    cfg.TEST.HAS_RPN = True  # Use RPN for proposals

    cameraProperties = camera("Blackfly", 10, 100, 63, 4.0, 1) #mm
    imagePath = '/home/goring/Documents/DataSets/Sub/2015/Transdec/buoys'  #0
    imagePath = '/home/goring/Documents/DataSets/Sub/2015/Transdec/1437520092_14466389'  #1
    imagePath = '/home/goring/Documents/DataSets/Sub/2015/Transdec/1437520291_14466389'  #2
    imagePath = '/home/goring/Documents/DataSets/Sub/2016/Transdec/1469722328_14466387'  #3
    imagePath = '/home/goring/Documents/DataSets/Sub/2016/Transdec/1469737334_14466387'  #4
    imagePath = '/home/goring/Documents/DataSets/Sub/2015/Transdec/1437581376_14466389'  #5
    imagePath = '/home/goring/Documents/DataSets/Sub/2015/Transdec/1437520549_14466389'  #6
    imagePath = '/home/goring/Documents/DataSets/Sub/2015/Transdec/1437520546_14466387'  #7
    imagePath = '/home/goring/Documents/DataSets/Sub/2015/Transdec/1437520291_14466389'  #8
    imagePath = '/home/goring/Documents/DataSets/Sub/2015/Transdec/1437520089_14466387'  #9
    imagePath = '/home/goring/Documents/DataSets/Sub/2015/Transdec/1437508031_14466389'  #10
    imagePath = '/home/goring/Documents/DataSets/Sub/2015/Transdec/1437491784_14466387'  #11
    imagePath = '/home/goring/Documents/DataSets/Sub/2015/Transdec/1437491781_14466389'  #12
    imagePath = '/home/goring/Documents/DataSets/Sub/2015/Transdec/1437491365_14466389'  #13

    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-detectdeliver12-151' #1
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-detectdeliver12-152' #2
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-detectdeliver12-153' #3
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-detectdeliver12-154' #4
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-detectdeliver12-1548' #5
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-detectdeliver12-1548auto3'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-detectdeliver12-1548auto4'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-detectdeliver12-1548auto5'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-detectdeliver12-1548auto6'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-detectdeliver12-1548auto7'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-detectdeliver12-1548auto9'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-detectdeliver12-1548auto10'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-dock1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-dock2'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-dock3'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-dock112-15'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-dock_14'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-docking'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-docking22'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-docking333'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-Dock_run_1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-Dock_run_2'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-Dock_run_3'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-Dock_strafe'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-obstacles_1H1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-schultz_1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-schultz_2'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-schultz_3H1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-schultz_4H1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-schultz_5H1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-tower3'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-tower4'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-tower_1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-tower_2'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-tower_3'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM1-tower_4'

    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-detectdeliver12-151'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-detectdeliver12-152'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-detectdeliver12-153'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-detectdeliver12-154'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-detectdeliver12-1548'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-detectdeliver12-1548auto3'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-detectdeliver12-1548auto4'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-detectdeliver12-1548auto5'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-detectdeliver12-1548auto6'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-detectdeliver12-1548auto7'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-detectdeliver12-1548auto10'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-dock1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-dock2'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-dock3'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-dock2H1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-docking'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-Dock_run_1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-Dock_run_2'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-Dock_run_3'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-Dock_strafe'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-light_tower_1H1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-obstacles_1H1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-schultz_2'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-schultz_2H1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-schultz_3H1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-schultz_4H1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-schultz_5H1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-tower3'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-tower4'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-tower_1'
    imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-tower_2'
    #imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-tower_3'
    #imagePath = '/home/goring/Documents/DataSets/RobotX/Hawaii/Images/CAM2-tower_4'

    imageFiles = []
    for f in os.listdir(imagePath):
        if f.endswith('jpg') or f.endswith('.jpeg'):
            imageFiles.append(f)
    imageFiles = sorted(imageFiles)

    if GUI:
        cv2.namedWindow('ret',0)

    args = parse_args()
    if args.cpu_mode:
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()
        caffe.set_device(args.gpu_id)
        cfg.GPU_ID = args.gpu_id
    if MODE ==0:
    	prototxt = '/home/goring/Documents/py-faster-rcnn/models/pascal_voc/VGG16/faster_rcnn_end2end/12/test.prototxt'
    	caffemodel = '/home/goring/Documents/py-faster-rcnn/output/faster_rcnn_end2end/voc_2007_trainval/idk/50000robosub2015.caffemodel'
    if MODE ==1:
	prototxt = '/home/goring/Documents/py-faster-rcnn/models/pascal_voc/VGG16/faster_rcnn_end2end/25/test.prototxt'
	caffemodel = '/home/goring/Documents/py-faster-rcnn/output/faster_rcnn_end2end/voc_2007_trainval/vgg16_faster_rcnn_iter_100000.caffemodel'
    if not os.path.isfile(caffemodel):
        raise IOError(('{:s} not found.\nDid you run ./data/script/'
                       'fetch_faster_rcnn_models.sh?').format(caffemodel))
    net = caffe.Net(prototxt, caffemodel, caffe.TEST)

    print '\n\nLoaded network {:s}'.format(caffemodel)

    if INPUT_MODE ==1:
        import pyfly2
        context = pyfly2.Context()
        if context.num_cameras < 1:
            raise ValueError('No cameras found')
        camera = context.get_camera(0)
        camera.Connect()
        camera.StartCapture()
        while True:
            image = getImage(camera)
            demo_video(net,image)
    elif INPUT_MODE ==0:
        for imageName in imageFiles:
            image = cv2.imread(os.path.join(imagePath,imageName))
            demo_video(net,image)
    elif INPUT_MODE ==2:
        try: ##Windows/OSX
            from PIL import ImageGrab
            while True:
                img = ImageGrab.grab()
                image = np.array(img)
                demo_video(net,image)
        except: #LINUX
            print "Does not work on Linux"

            ##import pyscreenshot as ImageGrab
            ##while True:
            ##    try:
            ##        im=ImageGrab.grab()
            ##        image = np.array(im)
            ##        cv2.imshow('fuck',image)
            ##        cv2.waitKey(20)
            ##        demo_video(net,image)
            ##    except:
            ##       pass

max_stars_repo_name: rtgoring/py-faster-rcnn-thesis

max_issues_repo_name: rtgoring/py-faster-rcnn-thesis

max_forks_repo_name: rtgoring/py-faster-rcnn-thesis


text:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf

physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], enable=True)

from models.TF import coerceVAE
from datetime import datetime as dt
import numpy as np

from utils import plotUtils as pU
from utils import dataUtils as dU

def main():

    now = dt.now().strftime('%Y-%m-%d--%H-%M-%S-coerce')
    os.makedirs(f'results/{now}')

    nInp      = 784  # (28*28) shaped images
    batchSize = 1024
    EPOCHS    = 150

    # --------- [ Generate the data ] ---------------------
    (x_train, y_train), (x_test, y_test) = dU.getMNISTData()
    y_train1 = y_train.reshape(-1, 1).astype( np.float32 ) # required for the concatenation
    y_test1  = y_test.reshape(-1, 1).astype(  np.float32 ) # required for the concatenation
    train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train1))
    train_dataset = train_dataset.shuffle(buffer_size=2048).batch(batchSize)

    # --------- [ Generate the model ] ---------------------
    layers      = [700, 500, 100]
    activations = ['tanh', 'tanh', 'tanh']
    nLatent     = 2

    coerceVae = coerceVAE.CoerceVAE(nInp, layers=layers, activations = activations, nLatent = nLatent)

    # --------- [ Train the model ] ---------------------
    losses = []
    for epoch in range(EPOCHS):
        print('Start of epoch %d' % (epoch,), end='-> ')

        # Iterate over the batches of the dataset.
        for step, (x, y) in enumerate(train_dataset):
            reconLoss, klLoss, coerceLoss, loss = coerceVae.step( x, y )
            losses.append([reconLoss, klLoss, coerceLoss, loss])

            if step % 100 == 0:
                print(reconLoss, klLoss, coerceLoss, loss)

    # ------------- [plot everything] -----------------
    losses = np.array(losses).T
    losses = {
        'reconstruction' : losses[0],
        'KL Divergence'  : losses[1],
        'coerce Loss'    : losses[2],
        'Total'          : losses[3],
        }

    pU.plotLosses(losses, folder=now)
    pU.plotMNISTLatentSpaceCoerce(epoch, coerceVae, x_test, y_test, folder=now)
    pU.plotMNISTImages(epoch, coerceVae, x_test, y_test, logits=True, folder=now, condition=False)
    pU.plotMNISTLatentReconstruction(epoch, coerceVae, extent=(-2, 2), nSteps=21, logits=True, folder=now, condition=False)

    return

if __name__ == "__main__":
    main()

max_stars_repo_name: sankhaMukherjee/vae

max_issues_repo_name: sankhaMukherjee/vae

max_forks_repo_name: sankhaMukherjee/vae


text:
"""" ``sklearn_xarray.externals`` """

try:
    import numpy_groupies
except ImportError:
    from . import _numpy_groupies_np as numpy_groupies

max_stars_repo_name: liminleitt/phausamanne

max_issues_repo_name: liminleitt/phausamanne

max_forks_repo_name: liminleitt/phausamanne


text:
#This script contains code for replicating Figure 2 from the paper "Bayesian Classifier Fusion with an Explicit Model of Correlation"
#For two examples with different marginal distributions two 3-dimensional categorical base distributions are fused with the Correlated
#Fusion Model assuming successively higher correlations between the corresponding base classifiers. The fused distributions are
#additionally compared to the result distributions of the two meta classifiers that can be obtained when using the Independent Fusion
#Model with K = 1 for each individual classifier.

from __future__ import division
import numpy as np

import matplotlib as mpl
rc_fonts = {
    "text.usetex": True,
    'text.latex.preview': True, # Gives correct legend alignment.
    #'mathtext.default': 'regular',
    'mathtext.fontset': 'cm',
    'font.family':'Times New Roman',
    'text.latex.preamble': [r"""\usepackage{bm}"""],
}
mpl.rcParams.update(rc_fonts)
import matplotlib.pyplot as plt

from matplotlib.legend_handler import HandlerLine2D, HandlerTuple

fs = 11.5

plt.rc('xtick', labelsize = fs)
plt.rc('ytick', labelsize = fs)

np.set_printoptions(precision=3)

#import code of IFM and CFM
import independent_fusion_model
import correlated_fusion_model

#fuse base distributions x1 and x2 with J dimensions using the CFM with marginal parameters alpha and assuming different correlation levels
#additionally create the meta classifiers' result distributions
#x1: categorical base distribution x1
#x2: categorical base distribution x2
#J: number of classes / dimensions of the categorical distributions
#alpha: marginal parameters of modeling correlated Dirichlet distributions, shape (2,J,J)
def create_fused_and_meta_dists(x1, x2, J, alpha):

    #correlations that will be assumed for fusing with CFM
    correlations = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6 ,0.7, 0.8, 0.9, 1.0]
    #container for storing all fused distributions
    all_fused = np.zeros((len(correlations), J))

    #loop over all correlations
    for i in range(len(correlations)):
        r = correlations[i]
        #set correlation parameters delta according to r:
        if r == 0:
            delta = np.array([[0.1,0.1,0.1], [0.1,0.1,0.1], [0.1,0.1,0.1]])  #generates a correlation of approx 0
        elif r == 1:
            #set delta depending on the marginal parameters alpha
            if alpha[0,0,0] == 101:
                delta = np.array([[100.9,99.9,99.9], [99.9,100.9,99.9], [99.9,99.9,100.9]])  #generates a correlation of approx 1
            else:
                delta = np.array([[101.9,99.9,99.9], [99.9,101.9,99.9], [99.9,99.9,101.9]])  #generates a correlation of approx 1
        else:
            delta = r * alpha[0] # generates a correlation of approx r

        #fuse
        fused_cat, samples_t = correlated_fusion_model.sample_t(J, x1, x2, alpha, delta)

        #console output
        print('r = %.1f' %r)
        print('fused distribution:')
        print(fused_cat)

        #add fused distribution to container
        all_fused[i] = fused_cat

    #meta classifier results

    #use the Independent Fusion Model as meta classifier individually for
    # x1 given marginal Dirichlet parameters of classifier 1 (alpha[0])
    # x2 given marginal Dirichlet parameters of classifier 2 (alpha[1])
    meta1 = independent_fusion_model.fuse_analytical(1, J, np.array([x1]), np.array([alpha[0]]))
    meta2 = independent_fusion_model.fuse_analytical(1, J, np.array([x2]), np.array([alpha[1]]))

    #merge base distributions, fused distributions, and meta classifier's results
    dists = np.vstack((x1, x2, all_fused, meta1, meta2))

    #return all distributions (base distributions, fused distributions, meta classifiers' results)
    return dists

#plot the base distributions, fused distributions for different correlations, and meta classifiers' results as bar plots
#ax: the axis to plot
#dists: the distributions to be plotted
#offset: the offset of the bars (to be able to plot the bars of the two examples side by side)
#colors: the colors of the bars
def plot_results(dists, ax, offset, colors):
    #width of bars
    width = 0.4

    nr_dists = dists.shape[0]

    #set x ticks of the categorical distributions in the plot
    x_ticks = np.array([0,1,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5,11.5,12.5, 14,15])

    #plot categorical distributions as bar plots
    p1 = ax.bar(x_ticks + offset, dists[:,0], width = width, color = colors[0], edgecolor = 'black')
    p2 = ax.bar(x_ticks + offset, dists[:,1], width = width, bottom = dists[:,0], color = colors[1], edgecolor = 'black')
    p3 = ax.bar(x_ticks + offset, dists[:,2], width = width, bottom = dists[:,0] + dists[:,1], color = colors[2], edgecolor = 'black')

    #x lim, ticks, and labels
    ax.set_xlim([-0.7,15.7])
    ax.set_xticks(x_ticks)
    ax.set_xticklabels([r'$\bm{x_i^1}$', r'$\bm{x_i^2}$', 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
                            r'$\bm{m_i^1}$', r'$\bm{m_i^2}$'], fontsize = fs)
    ax.set_xlabel(' ', fontsize = fs, labelpad = 1.2)
    ax.text(-0.1,-0.3,'base', fontsize = fs, multialignment = 'center')
    ax.text(4.65,-0.3,'fused dependent on $r$', fontsize = fs, multialignment = 'center')
    ax.text(13.9,-0.3,'meta', fontsize = fs, multialignment = 'center')

    #y lim, ticks, and labels
    ax.set_ylim([0,1])
    ax.set_yticks([0, 0.2, 0.4, 0.6, 0.8, 1])
    ax.set_yticklabels([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
    ax.set_ylabel('categ. distributions', fontsize = fs)

    #for creating the legend, return p1, p2, p3
    return p1, p2, p3

####################
#reproduce figure 2#

#set model parameters applying to both examples 1 and 2

#nr of classes
J = 3

#base distributions to be fused
x1 = np.array([0.6,0.2,0.2])
x2 = np.array([0.6,0.2,0.2])

#set up plot
fig, ax = plt.subplots()
fig.set_size_inches(6,2.5)

#first example (IOP marginals, blue bars in figure 2)

#marginal parameters of modeling correlated Dirichlet distribution
alpha1 = np.array([[[101,100,100], [100,101,100], [100,100,101]],      #marginal parameters of classifier 1 for t_i = 1, t_i = 2, t_i = 3
                    [[101,100,100], [100,101,100], [100,100,101]]])    #marginal parameters of classifier 2 for t_i = 1, t_i = 2, t_i = 3

#fuse x1 and x2 with the CFM using marginals alpha1 and different correlation levels, and create meta classifiers' result distributions
dists1 = create_fused_and_meta_dists(x1, x2, J, alpha1)

#set offset and bar colors for plot
offset1 = -0.2
colors1 = ['lightskyblue', 'royalblue', 'navy']

#plot the resulting distributions
p1, p2, p3 = plot_results(dists1, ax, offset1, colors1)

#second example (marginals leading to more uncertainty reduction, orange bars in figure 2)

#marginal parameters of modeling correlated Dirichlet distribution
alpha2 = np.array([[[102,100,100], [100,102,100], [100,100,102]],      #marginal parameters of classifier 1 for t_i = 1, t_i = 2, t_i = 3
                    [[102,100,100], [100,102,100], [100,100,102]]])    #marginal parameters of classifier 2 for t_i = 1, t_i = 2, t_i = 3

#fuse x1 and x2 with the CFM using marginals alpha2 and different correlation levels, and create meta classifiers' result distributions
dists2 = create_fused_and_meta_dists(x1, x2, J, alpha2)

#set offset and bar colors for plot
offset2 = 0.2
colors2 = ['orange', "#FF6F00", 'sienna']

#plot the resulting distributions
p4, p5, p6 = plot_results(dists2, ax, offset2, colors2)

#set legend
ax.legend([(p1, p4), (p2, p5), (p3, p6)], [r'$p(t_i = 1)$', r'$p(t_i = 2)$', r'$p(t_i = 3)$'], loc = 'lower right', fontsize = fs,
            numpoints=1, handler_map={tuple: HandlerTuple(ndivide=2, pad = 0)})

#show plot
plt.tight_layout()
plt.show()


text:
from typing import (
    List,
    Tuple,
    Dict,
    Iterator,
)

from collections import Counter

import torch
import torchvision.transforms as transforms
import torch.utils.data as data
import os
import pickle
import numpy as np
from tqdm import tqdm

BIODataEntry = Dict[str, object]

class BIODataset(object):
    '''
    Given the file of a BIO encoded dataset, parses the all the files and
    creates a data list, where the list is tuples of (input, tagged) where input and tagged
    are two lists of the exact same elements, and tagged contains all the BIO tags for
    the input.

    Arguments:
        file_name: the name of the BIO encoded file
    '''
    def __init__(self, dataset_id: int, file_name: str, binary_class: str = None):
        self.file_name = file_name
        self.data = []
        self.word_list = Counter()
        self.tags = Counter()
        self.binary_class = binary_class
        self.dataset_id = dataset_id

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

    def _convert_binary(self, token: str) -> str:
        if self.binary_class is None or token == 'O':
            return token
        elif token[2:] != self.binary_class:
            return 'O'
        return token

    def __getitem__(self, index) -> BIODataEntry:
        return self.data[index]

    def __iter__(self) -> Iterator[BIODataEntry]:
        for item in self.data.__iter__():
            yield item

    def parse_file(self) -> None:
        with open(self.file_name) as f:
            currInput = []
            currOutput = []
            for _, line in enumerate(tqdm(f)):
                if len(line.strip()) == 0 or line.startswith('-DOCSTART'):
                    if len(currInput) > 0:
                        # marks the end of a sentence
                        self.data.append(
                            {
                                'id': len(self.data),
                                'input': currInput,
                                'output': currOutput,
                                'weight': 1.0,
                            }
                        )
                        currInput = []
                        currOutput = []
                else:
                    tokens = line.split()
                    # there has to be at least 2 things
                    assert len(tokens) >= 2

                    # seperates each line to 2 different things
                    # [word, tag]
                    # word, pos, sync_chunk, output = tokens
                    word, output = tokens[0], tokens[-1]
                    output = self._convert_binary(output)
                    self.word_list[word] += 1
                    currInput.append(word)

                    self.tags[output] += 1

                    currOutput.append(output)

class ActiveBIODataset(BIODataset):
    def __init__(
        self,
        data: List[Dict[str, object]],
        dataset_id: int,
        binary_class: str,
    ):
        super().__init__(
            dataset_id=dataset_id,
            file_name='temp.txt',
            binary_class=binary_class,
        )
        self.dataset_id = dataset_id
        self.data = data

class UnlabeledBIODataset(BIODataset):
    def __init__(
        self,
        bio_data: BIODataset,
        dataset_id: int,
    ):
        super().__init__(
            dataset_id=dataset_id,
            file_name='temp.txt',
            binary_class=bio_data.binary_class,
        )

        self.data = [
            {
                'input': data['input'],
                'id': data['id'],
                'weight': data['weight'],
            } for data in bio_data
        ]

    def remove(self, query) -> None:
        s_id, s_in = query
        for i, item in enumerate(self.data):
            if item['id'] == s_id:
                assert s_in == item['input']
                del self.data[i]
                break

max_stars_repo_name: AkshatSh/DPD

max_issues_repo_name: AkshatSh/DPD

max_forks_repo_name: AkshatSh/DPD


text:
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 12 09:06:26 2019

@author: TANVEER_MUSTAFA
"""

# -*- coding: utf-8 -*-
"""
Created on Thu Aug 30 18:28:45 2018

@author: TANVEER MUSTAFA
"""

import os
clear = lambda: os.system('cls')
clear()

import cv2
import numpy as np
import tensorflow as tf
import sys

from PIL import Image
import matplotlib.pyplot as plt
from os.path import isfile, join
import time

from utils import label_map_util
from utils import visualization_utils as vis_util

def choose_video(filename):

    start_time = time.time()

    image_array=[]

    sys.path.append("..")

    MODEL_NAME = 'inference_graph'
    VIDEO_NAME = filename

    CWD_PATH = os.getcwd()

    PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,'frozen_inference_graph.pb')

    PATH_TO_LABELS = os.path.join(CWD_PATH,'training','mscoco_label_map.pbtxt')

    # Path to video
    PATH_TO_VIDEO = os.path.join(CWD_PATH,VIDEO_NAME)

    # Number of classes the object detector can identify
    NUM_CLASSES = 6

    try:
        if not os.path.exists('z1video_frames4'):
            os.makedirs('z1video_frames4')
    except OSError:
        print ('Error: Creating directory of data')

    try:
        if not os.path.exists('z1video_frames5'):
            os.makedirs('z1video_frames5')
    except OSError:
        print ('Error: Creating directory of data')

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    # Load the Tensorflow model into memory.
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

        sess = tf.Session(graph=detection_graph)

    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')

    num_detections = detection_graph.get_tensor_by_name('num_detections:0')

    video = cv2.VideoCapture(PATH_TO_VIDEO)

    currentFrame = 0
    i=0
    j=0
    count=1
    while(video.isOpened()):

        ret, frame = video.read()
        if ret:

            resize = cv2.resize(frame, (320, 240))
            resize1 = cv2.resize(frame, (620, 440))
            #resize1 = frame.resize( (620, 440))
            frame_expanded = np.expand_dims(frame, axis=0)

            (boxes, scores, classes, num) = sess.run(
                [detection_boxes, detection_scores, detection_classes, num_detections],
                feed_dict={image_tensor: frame_expanded})

            vis_util.visualize_boxes_and_labels_on_image_array(
                frame,
                np.squeeze(boxes),
                np.squeeze(classes).astype(np.int32),
                np.squeeze(scores),
                category_index,
                use_normalized_coordinates=True,
                line_thickness=4,
                min_score_thresh=0.40)

            name = './z1video_frames4/frame' + str(currentFrame) + '.jpg'
            name1 = './z1video_frames5/frame' + str(currentFrame) + '.jpg'

            #print('name',name)

            cv2.imwrite(name, frame)
            cv2.imwrite(name1, frame)
            cv2.imshow('Object detector', frame)
                # To stop duplicate images
            currentFrame += 1

            #****************************************************

            # Press 'q' to quit
            if cv2.waitKey(1) == ord('q'):
                break

        else:
            break

    #plot_graph()

    video.release()
    cv2.destroyAllWindows()

    print("--- %s seconds ---" % (time.time() - start_time))


text:
import os
import json
from posixpath import basename, join
from copy import copy, deepcopy
from io import BytesIO
import numpy as np
from time import time,strftime

# Action names
__all__ = [] # type: List[str]

# Action methods
ALL_ACTIONS = [] # type: List[Callable[Any, Any]]

def cache(action):
    """
    Decorator which adds the *cached* attribute to the function.

    Use *@cache* to force caching to always occur (for example, when
    the function references remote resources, vastly reduces memory, or is
    expensive to compute.  Use *@nocache* when debugging a function
    so that it will be recomputed each time regardless of whether or not it
    is seen again.
    """
    action.cached = True
    return action

def nocache(action):
    """
    Decorator which adds the *cached* attribute to the function.

    Use *@cache* to force caching to always occur (for example, when
    the function references remote resources, vastly reduces memory, or is
    expensive to compute.  Use *@nocache* when debugging a function
    so that it will be recomputed each time regardless of whether or not it
    is seen again.
    """
    action.cached = False
    return action

def module(action):
    """
    Decorator which records the action in *ALL_ACTIONS*.

    This just collects the action, it does not otherwise modify it.
    """
    ALL_ACTIONS.append(action)
    __all__.append(action.__name__)

    # Sort modules alphabetically
    ALL_ACTIONS.sort(key=lambda action: action.__name__)
    __all__.sort()

    # This is a decorator, so return the original function
    return action

@module
def LoadDCS(filelist=None, check_timestamps=True):
    """
    loads a data file into a SansData obj and returns that.
    Checks to see if data being loaded is 2D; if not, quits

    **Inputs**

    filelist (fileinfo[]): Files to open.

    check_timestamps (bool): verify that timestamps on file match request

    **Returns**

    output (raw[]): all the entries loaded.

    2018-04-25 Brian Maranville
    """
    from dataflow.fetch import url_get
    from .dcsdata import readDCS
    if filelist is None:
        filelist = []
    data = []
    for fileinfo in filelist:
        path, mtime, entries = fileinfo['path'], fileinfo.get('mtime', None), fileinfo.get('entries', None)
        name = basename(path)
        fid = BytesIO(url_get(fileinfo, mtime_check=check_timestamps))
        entry = readDCS(name, fid)
        data.append(entry)

    return data

@module
def RawToEfTwoTheta(rawdata):
    """
    converts raw data to Q,E

    **Inputs**

    rawdata (raw): data in

    **Returns**

    output (ef2th): modified data

    2018-04-29 Daniel Pajerowski
    """
    from io import BytesIO
    from .dcsdata import EfTwoThetaData
    from .dcs_detector_info import detector_info_txt
    detInfo = np.genfromtxt(BytesIO(detector_info_txt.encode()), skip_header=1)#, skip_footer=17)
    detToTwoTheta = detInfo[:,9] # 10th column

    masterSpeed = rawdata.metadata['ch_ms']
    speedRatDenom = rawdata.metadata['ch_srdenom']
    t_SD_min = rawdata.metadata['tsdmin']
    t_channel = np.arange(rawdata.histodata.shape[0])
    ef = Ef_from_timechannel(t_channel, t_SD_min, speedRatDenom, masterSpeed)

    new_metadata = deepcopy(rawdata.metadata)
    new_ef2th = EfTwoThetaData(rawdata.name, rawdata.histodata, ef=ef, twotheta=detToTwoTheta, metadata=new_metadata)
    return new_ef2th

@module
def RawToEQ(rawdata):
    """
    converts raw data to Q,E

    **Inputs**

    rawdata (raw): data in

    **Returns**

    output (eq): modified data

    2018-04-28 Daniel Pajerowski
    """
    from io import BytesIO
    from .dcsdata import EQData
    from .dcs_detector_info import detector_info_txt
    detInfo = np.genfromtxt(BytesIO(detector_info_txt.encode()), skip_header=1)#, skip_footer=17)
    detToTwoTheta = detInfo[:,9] # 10th column

    ch_wl = rawdata.metadata['ch_wl']
    Ei = Elam(ch_wl)
    ki = kE(Ei)
    dE = abs(0.5*(-0.10395+0.05616 *Ei+0.00108 *Ei**2)) #take the putative resolution and halve it
    masterSpeed = rawdata.metadata['ch_ms']
    speedRatDenom = rawdata.metadata['ch_srdenom']
    t_SD_min = rawdata.metadata['tsdmin']

    #binning resolution
    Q_max = Qfunc(ki,ki,150)
    Q_min = 0
    E_bins = np.linspace(-Ei, Ei, int(2*Ei/dE)*0.5 )
    Q_bins = np.linspace(Q_min,Q_max,301*0.5)

    #for every point in {timechannel, detectorchannel} space, map into a bin of {E,Q} space
    #remember, data is organized as data[detectorchannel][timechannel]
    data = rawdata.histodata.T
    i,j = np.indices(data.shape)
    ef = Ef_from_timechannel(j, t_SD_min, speedRatDenom, masterSpeed)
    #print np.shape(data)
    #print np.shape(ki), np.shape(kE(ef)), np.shape(detToTwoTheta[:, None])
    #print detToTwoTheta

    Q_ = Qfunc(ki, kE(ef), detToTwoTheta[:, None])

    E_transfer = Ei-ef
    E_mask = (E_transfer > -Ei)

    EQ_dataarray, xedges, yedges = np.histogram2d(Q_[E_mask], E_transfer[E_mask], bins=(Q_bins, E_bins), range=([Q_min,Q_max], [-Ei, Ei]), weights=data[E_mask])
    # normalize to number of pixels in each histogram bin:
    norm_data = np.ones_like(data)
    EQ_norm, xedges, yedges = np.histogram2d(Q_[E_mask], E_transfer[E_mask], bins=(Q_bins, E_bins), range=([Q_min,Q_max], [-Ei, Ei]), weights=norm_data[E_mask])

    norm_mask = (EQ_norm != 0)
    EQ_normalized = np.copy(EQ_dataarray)
    EQ_normalized[norm_mask] = EQ_dataarray[norm_mask] / EQ_norm[norm_mask]

    new_metadata = deepcopy(rawdata.metadata)
    new_metadata['Ei'] = Ei
    new_metadata['Ef'] = ef
    new_metadata['Q_max'] = Q_max
    new_metadata['Q_min'] = Q_min
    new_eq = EQData(rawdata.name, EQ_normalized, new_metadata)
    return new_eq

@module
def sliceEQData(data, slicebox=[None,None,None,None]):
    """
    Sum 2d data along both axes and return 1d datasets

    **Inputs**

    data (eq) : data in

    slicebox (range?:xy): region over which to integrate (in data coordinates)

    **Returns**

    xout (eq1d) : xslice

    yout (eq1d) : yslice

    2018-04-22 Brian Maranville
    """

    from .dcsdata import DCS1dData

    if slicebox is None:
        slicebox = [None, None, None, None]
    xmin, xmax, ymin, ymax = slicebox

    # then use q-values
    xdata = data.xaxis['values']
    ydata = data.yaxis['values']

    xmin = data.xaxis["min"]
    xmax = data.xaxis["max"]
    x_in = data.xaxis["values"]
    y_in = data.yaxis["values"]

    xslice = slice(get_index(x_in, xmin), get_index(x_in, xmax))
    yslice = slice(get_index(y_in, ymin), get_index(y_in, ymax))
    x_out = x_in[xslice]
    y_out = y_in[yslice]
    dx = np.zeros_like(x_out)
    dy = np.zeros_like(y_out)

    dataslice = (xslice, yslice)
    x_sum = np.sum(data.data[dataslice], axis=1)
    y_sum = np.sum(data.data[dataslice], axis=0)

    x_output = DCS1dData(x_out, x_sum, dx=dx, dv=dx, xlabel=data.xaxis["label"], vlabel="I",
                    xunits="", vunits="neutrons", metadata=data.metadata)
    y_output = DCS1dData(y_out, y_sum, dx=dy, dv=dy, xlabel=data.yaxis["label"], vlabel="I",
                    xunits="", vunits="neutrons", metadata=data.metadata)

    return x_output, y_output

def Elam(lam):
    """
    convert wavelength in angstroms to energy in meV
    """
    return 81.81/lam**2

def Ek(k):
    """
    convert wave-vector in inver angstroms to energy in meV
    """
    return 2.072*k**2

def kE(E):
    return np.sqrt(E/2.072)

def Qfunc(ki, kf, theta):
    """
    evaluate the magnitude of Q from ki, kf, and theta
    theta is the angle between kf and ki, sometimes called 2 theta, units of degrees
    """
    return np.sqrt(  ki**2 + kf**2 - 2*ki*kf*np.cos(theta*np.pi/180)  )

def Ef_from_timechannel(timeChannel, t_SD_min, speedRatDenom, masterSpeed):
    """
    using the parameters
        t_SD_min = minimum sample to detector time
        speedRatDenom = to set FOL chopper speed
        masterSpeed = chopper speed (except for FOL chopper)
    using the variabl
        timeChannel, where I am numbering from 1 <be careful of this convention>
    """
    return 8.41e7 / (t_SD_min + (timeChannel+1)*    (6e4 *(speedRatDenom/masterSpeed))   )**2

def get_index(t, x):
    if (x == "" or x == None):
        return None
    if float(x) > t.max():
        return None
    if float(x) < t.min():
        return None
    tord = np.argsort(t)
    return tord[np.searchsorted(t, float(x), sorter=tord)]


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""splitting up old arc.py file into individual experiment runs for clarity.

===========
ARC4 Version
===========

New version of the above for arc4 using a conda environment for my sanity.
Use the standard means of cloning the git but use this venv instead.

module load anaconda
conda create -p /nobackup/medrclaa/ukf_py python=3 numpy scipy matplotlib shapely imageio seaborn
source activate /nobackup/medrclaa/ukf_py

Extract files using usual scp commands
If we are accessing arc remotely we have two remote servers
(one with virtually no storage) to go through so use proxy jump to avoid being
excommunicated by the arc team.

Shamelessly stolen from :

https://superuser.com/questions/276533/scp-files-via-intermediate-host

With the format:

scp -oProxyJump=user@remote-access.leeds.ac.uk
e.g.
scp -oProxyJump=medrclaa@remote-access.leeds.ac.uk medrclaa@arc4.leeds.ac.uk:/nobackup/medrclaa/dust/Projects/ABM_DA/experiments/ukf_experiments/results/agg* /Users/medrclaa/new_aggregate_results

"""
import sys
import numpy as np
import pickle

from arc import arc

# import ex3 modules
sys.path.append("..")
sys.path.append("../modules")
from modules.ex3.ukf_ex3 import rj_params, get_gates, set_gates
from modules.ex3.rjmcmc_ukf import rjmcmc_ukf
import modules.default_ukf_configs as configs

# import stationsim for pickler
sys.path.append('../../..')
sys.path.append('../../../../stationsim')
from stationsim.ukf2 import pickler

# %%

def ex3_parameters(parameter_lists, test):
    """let the arc task array choose experiment parameters to run

    Parameters
    ----------
    test : bool
        if test is true we choose some simple parameters to run on arc.
        this is to test the file works and produces results before running
        a larger batch of jobs to have none of them succeed and 400 abort
        emails.

    parameter_lists : list
        `parameter_lists` is a list of lists where each element of the list
        is some set of experiment parameters we wish to run. E.g. this may be
        [10, 1.0, 1] for the first experiment running with 10 agents 100%
        observed.

    Returns
    -------
    n, run_id : int
        agent population `n` and unique `run_id` for each n and prop.
    prop : float
        proportion of agents observed `prop`
    """

    if not test:
        # assign parameters according to task array
        n = parameter_lists[int(sys.argv[1])-1][0]
        jump_rate = parameter_lists[int(sys.argv[1])-1][1]
        run_id = parameter_lists[int(sys.argv[1])-1][2]
    else:
        # if testing use these default parameters for a single quick run.
        n = 5
        jump_rate = 5
        run_id = "test"
    # Set how many stepwise jumps are made to 5 no matter what.
    # Add any other fixed parameters too.
    n_jumps = 5

    return n, jump_rate, n_jumps, run_id

def arc_ex3_main(parameter_lists, test):
    """main function to run ukf experiment 1 on arc.

    Parameters
    ----------
    parameter_lists : list
        `parameter_lists` is a list of lists where each element of the list
        is some set of experiment parameters we wish to run. E.g. this may be
        [10, 1.0, 1] for the first experiment running with 10 agents 100%
        observed.
    test : bool
        if test is true we choose some simple parameters to run on arc.
        this is to test the file works and produces results before running
        a larger batch of jobs to have none of them succeed and 400 abort
        emails.

    Returns
    ------
    u : cls
        `u` instance of finished rjukf run
    """
    # load in default params
    ukf_params = configs.ukf_params
    model_params = configs.model_params
    if test:
        model_params["seed"] = 8

    # load in experiment 3 parameters
    n, jump_rate, n_jumps, run_id = ex3_parameters(parameter_lists, test)
    # update model and ukf parameters for given experiment and its' parameters
    model_params, ukf_params, base_model =  rj_params(n, jump_rate, n_jumps,
                                                            model_params,
                                                            ukf_params)
    #file name to save results to
    file_name = "rjukf_agents_{}_jump_rate_{}-{}".format(
        str(n).zfill(3),
        str(jump_rate),
        str(run_id).zfill(3)) + ".pkl"
    #where to save the file
    destination = "../results/"

    # initiate arc class
    ex3_arc = arc(test)
    # run define ukf_args and run ukf_ss filter for arc class
    ukf_args =  model_params, ukf_params, base_model, get_gates, set_gates
    u = ex3_arc.arc_main(rjmcmc_ukf, file_name, *ukf_args)
    # save entire ukf class as a pickle
    ex3_arc.arc_save(ex3_save, destination, file_name)

    return u

class ex3_saver:
    """store required components from rjmcmc_UKF run for plotting.
    saves a bunch of space
    """
    def __init__(self, u):
        """load only necessary parameters for a smaller pickle.

        Parameters
        ----------
        u : cls
            Finished rjmcmc instance `u`.

        Returns
        -------
        None.

        """
        self.truths = u.truths
        self.ukf_histories = u.ukf_histories
        self.jump_rate = u.jump_rate
        self.pop_total = u.pop_total
        self.sample_rate = u.sample_rate
        self.true_gate = u.true_gate
        self.estimated_gates = u.estimated_gates

def ex3_save(u, destination, file_name):
    """ function for making and pickling ex3_saver class

    Parameters
    ----------
    u : cls
        Finished rjmcmc instance `u`.
    destination , file_name : str
        `destination` folder and `file_name` name of file

    Returns
    -------
    None.

    """
    # init class with only required attributes
    saved_class = ex3_saver(u)
    # pickle it at the destination with the file name
    f = open(destination + file_name, "wb")
    pickle.dump(saved_class, f)
    f.close()

if __name__ == '__main__':

    #if testing set to True. if running batch experiments set to False
    test = True
    if test:
        print("Test set to true. If you're running an experiment, it wont go well.")

    # agent populations from 10 to 30
    num_age = [10, 20, 30]
    # 25 to 100 % proportion observed in 25% increments. must be 0<=x<=1
    jump_rates = [5, 10, 20]
    # how many experiments per population and proportion pair. 30 by default.
    run_id = np.arange(0, 50, 1)
    #cartesian product list giving all combinations of experiment parameters.
    param_list = [(x, y, z) for x in num_age for y in jump_rates for z in run_id]

    u = arc_ex3_main(param_list, test)

max_stars_repo_name: RobertClay/DUST-RC

max_issues_repo_name: RobertClay/DUST-RC

max_forks_repo_name: RobertClay/DUST-RC