text:
import RPi.GPIO as GPIO
import pyaudio
import wave
import numpy
from LoopChannel import LoopChannel
from RecordChannel import RecordChannel
from functools import reduce

CHUNK = 1024
FORMAT = pyaudio.paInt16 #paInt8
CHANNELS = 2
RATE = 44100 #sample rate

wf = wave.open('lead.wav', 'rb')
player = pyaudio.PyAudio()

FORMAT = player.get_format_from_width(wf.getsampwidth())
CHANNELS = wf.getnchannels()
RATE = wf.getframerate()

#pick GPIO pins that are not in use. We might look into other solutions, using less pins though
RECBUTTON = 4		#pin 7 on board
LOOPBUTTON1 = 17	#pin 11 on board
LOOPBUTTON2 = 27	#pin 13 on board
LOOPBUTTON3 = 22	#pin 15 on board

GPIO.setmode(GPIO.BCM)
GPIO.setup(RECBUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LOOPBUTTON1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LOOPBUTTON2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LOOPBUTTON3, GPIO.IN, pull_up_down=GPIO.PUD_UP)

class LoopService:
	def __init__(self):
		#loopLength will be used to make sure all loop clips are of the same length
		self.loopLength = None
		self.bpmSet = False
		self.bpm = 0
		self.numberOfLoopChannels = 3
		self.listOfLoopChannels = [0]*self.numberOfLoopChannels
		self.recordChannel = RecordChannel()
		self.initializeChannels()
		self.initializeButtons()

		#when this is true, then if you press a loop channel button, it will record on it instead of playing it.
		#self.record = False

		#for testing
		#self.playLoop()
		self.listOfLoopChannels[0].setWave('drums.wav')
		# self.listOfLoopChannels[0].switchLoop()
		self.listOfLoopChannels[1].setWave('lead.wav')
		self.listOfLoopChannels[1].switchLoop()
		self.listOfLoopChannels[2].setWave('pads.wav')
		self.listOfLoopChannels[2].switchLoop()
		self.loop = True
		self.playMixed()

	def initializeChannels(self):
		print("Initializing Each loop channel")
		for i in range(self.numberOfLoopChannels):
			self.listOfLoopChannels[i] = LoopChannel()

	def initializeButtons(self):
		print("Initializing Interups for buttons")
		GPIO.add_event_detect(RECBUTTON,   GPIO.FALLING, callback=lambda x: self.record(),      bouncetime=300)
		GPIO.add_event_detect(LOOPBUTTON1, GPIO.FALLING, callback=lambda x: self.loopButton(0), bouncetime=500)
		GPIO.add_event_detect(LOOPBUTTON2, GPIO.FALLING, callback=lambda x: self.loopButton(1), bouncetime=500)
		GPIO.add_event_detect(LOOPBUTTON3, GPIO.FALLING, callback=lambda x: self.loopButton(2), bouncetime=500)

	def record(self):
		self.record = not self.record
		if self.recordChannel.record:
			print("Recording")
		else:
			print("stop recording")

	#this might make one mixed chunk louder or more quiet. Debug it later if necessary
	def loopButton(self, index):
		print("You pressed loop button " + str(index))
		if self.record:
			print("implement recording function")
			self.recordChannel.firstRecording()
			self.listOfLoopChannels[index].setWave("record.wav")
			self.listOfLoopChannels[index].switchLoop()
		else:
			self.listOfLoopChannels[index].switchLoop()

	def playLoop(self):
		# This will is just for testing purposes, We will need to mix our loopChannels into one audio.
		self.listOfLoopChannels[0].playLoop()

	# This iterates through all the LoopChannel objects, and sees how many are turned on
	def findNrOfChannelsOn(self):
		number = 0
		for i in range(self.numberOfLoopChannels):
			if self.listOfLoopChannels[i].isLooping():
				number += 1
		return number

	# mixChunks kannski?
	# takes chunks of audio from all channels, and combines (or mixes) them into one chunk of audio
	def mixChannels(self):
		data = [0]*self.numberOfLoopChannels
		decodedData = [0]*self.numberOfLoopChannels
		nrC = self.findNrOfChannelsOn()
		if nrC == 0:
			constant = 0.0
		else:
			constant= 1.0/self.findNrOfChannelsOn()
		print(str(constant))
		print(str(self.findNrOfChannelsOn()))
		combinedData = 0

		for i in range(self.numberOfLoopChannels):
			data[i] = self.listOfLoopChannels[i].readChunk(CHUNK)
			decodedData[i] = numpy.fromstring(data[i], numpy.int16)

			#Only combine audio from channels that are on.
			if self.listOfLoopChannels[i].loop:
				combinedData = combinedData + constant* decodedData[i]
			print(str(combinedData))

		newData = combinedData.astype(numpy.int16)
		return newData

	# Rewinds all the wave files in our loop channels
	def rewindChannels(self):
		for i in range(self.numberOfLoopChannels):
			self.listOfLoopChannels[i].rewindWave()

	# plays all loops that are turned on in our loop channels.
	def playMixed(self):
		p = pyaudio.PyAudio()
		stream = p.open(format=FORMAT,
				channels=CHANNELS,
				rate=RATE,
				output=True)

		data = self.mixChannels().tostring()
		while self.loop :
			stream.write(data)
			data = self.mixChannels().tostring()
			if data == b'' : # If file is over then rewind.
				self.rewindChannels()
				data = self.mixChannels().tostring()

# TODO: see if its possible to use interrupt, instead of constant polling of
# Drumpad loop. Should be run asynchronously
def main():
	# GPIO.setup(RECBUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
	# GPIO.setup(LOOPBUTTON1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
	# GPIO.setup(LOOPBUTTON2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
	# GPIO.setup(LOOPBUTTON3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
	ls = LoopService()

if __name__ == '__main__':main()


text:
import numpy as np
import bpy, gpu, bgl
import os, sys
try:
    import OpenGL as ogl
    try:
        from OpenGL.GL import glGetTexImage   # this fails in <=2020 versions of Python on OS X 11.x
    except ImportError:
        print('Drat, patching for Big Sur')
        from ctypes import util
        orig_util_find_library = util.find_library
        def new_util_find_library( name ):
            res = orig_util_find_library( name )
            if res: return res
            return '/System/Library/Frameworks/'+name+'.framework/'+name
        util.find_library = new_util_find_library
        from OpenGL.GL import glGetTexImage
except ImportError:
    pass

from .signal import Signal
from .camera import Camera
from .utils import find_first_view3d

class OffScreenRenderer:
    '''Provides offscreen scene rendering using Eevee.

    Rendering reusing the first found 3D Space View in Blender. The way this view
    is configured also defines how the resulting image looks like. Use the helper method
    `set_render_style` to adjust the appearance from within Python.

    This class' `render` method is expected to be called from a `POST_PIXEL` callback,
    which `AnimationController` takes care of. That is, invoking `render()` from
    withing `post_frame` is considered save.

    Params
    ------
    camera: btb.Camera, None
        Camera view to be rendered. When None, default camera is used.
    origin: str
        When 'upper-left' flip the rendered data to match OpenCV image coordinate
        system. When 'lower-left' image is created using OpenGL coordinate system. Defaults to 'upper-left'.
    mode: str
        Defines the number of color channels. Either 'RGBA' or 'RGB'
    gamma_coeff: scalar, None
        When not None, applies gamma color correction to the rendered image.
        Blender performs offline rendering in linear color space that when
        viewed directly appears to be darker than expected. Usually a value
        of 2.2 get's the job done. Defaults to None.

    Attributes
    -----------
    proj_matrix: Matrix
        Projection matrix to use. See `btb.camera` for helpers. Update this when
        the camera intrinsics change.
    view_matrix: Matrix
        View matrix to use. See `btb.camera` for helpers. Update this when
        the camera moves.
    '''

    def __init__(self, camera=None, mode='rgba', origin='upper-left', gamma_coeff=None):
        assert mode in ['rgba', 'rgb']
        assert origin in ['upper-left', 'lower-left']
        self.camera = camera or Camera()
        self.offscreen = gpu.types.GPUOffScreen(
            self.shape[1],
            self.shape[0]
        )
        self.area, self.space, self.region = find_first_view3d()
        self.handle = None
        self.origin = origin
        self.gamma_coeff = gamma_coeff
        channels = 4 if mode=='rgba' else 3
        self.buffer = np.zeros(
            (self.shape[0], self.shape[1], channels),
            dtype=np.uint8
        )
        self.mode = bgl.GL_RGBA if mode=='rgba' else bgl.GL_RGB

    @property
    def shape(self):
        return self.camera.shape

    def render(self):
        '''Render the scene and return image as buffer.

        Returns
        -------
        image: HxWxD array
            where D is 4 when `mode=='RGBA'` else 3.
        '''
        print("Start render")
        # switch on nodes
        bpy.context.scene.use_nodes = True
        tree = bpy.context.scene.node_tree
        links = tree.links

        # clear default nodes
        for n in tree.nodes:
            tree.nodes.remove(n)

        # create input render layer node
        rl = tree.nodes.new('CompositorNodeRLayers')
        rl.location = 185,285

        # create output node
        v = tree.nodes.new('CompositorNodeViewer')
        v.location = 750,210
        v.use_alpha = False

        # Links
        links.new(rl.outputs[0], v.inputs[0])  # link Image output to Viewer input
        logfile = 'blender_render.log'
        open(logfile, 'a').close()
        old = os.dup(1)
        sys.stdout.flush()
        os.close(1)
        os.open(logfile, os.O_WRONLY)

        # render
        bpy.context.scene.render.filepath = "~/render_eye"
        bpy.ops.render.render(write_still=True)

        os.close(1)
        os.dup(old)
        os.close(old)

        # get viewer pixels
        pixels = bpy.data.images['Viewer Node'].pixels
        print(len(pixels)) # size is always width * height * 4 (rgba)

        # copy buffer to numpy array for faster manipulation
        self.buffer = np.array(pixels[:]).reshape(self.shape[0], self.shape[1], 4)[:,:,:3]
        buffer = self.buffer
        if self.origin == 'upper-left':
            buffer = np.flipud(buffer)
        if self.gamma_coeff:
            buffer = self._color_correct(buffer, self.gamma_coeff)
        return buffer

    def set_render_style(self, shading='RENDERED', overlays=False):
        self.space.shading.type = shading
        self.space.overlay.show_overlays = overlays

    def _color_correct(self, buffer, coeff=2.2):
        ''''Return sRGB image.'''
        rgb = buffer[...,:3].astype(np.float32) / 255
        rgb = np.uint8(255.0 * rgb**(1/coeff))
        if buffer.shape[-1] == 4:
            return np.concatenate((rgb, buffer[...,3:4]), axis=-1)
        else:
            return rgb

ext: py

reason: import numpy


text:
# lossGrapher.py
# Script to generate graphs from logfile2csv.py
# Input data is in 2 files:
#   1) checkpointLoss.csv
#   2) trainProgress.csv
#
# checkpointLoss.csv has validation loss data:
#   Epoch, iteration, val_loss
# trainProgress.csv has iteration by iteration loss data.
#   Epoch, iteration, loss
#
# Takes 2 arguments:
#   -i /path/to/input/dir/
#   -o /path/to/output/dir/
#
# Dylan Auty, 22/05/16

import sys
import getopt
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import StringIO
import collections

plt.ioff()

def usage():
    sys.exit("Usage: python2 lossGrapher.py -i /path/to/input/dir -o /path/to/output/dir")
    return

def main(argv):
    CLin = ''
    TPin = ''
    CLout1 = ''
    CLout2 = ''
    TPout1 = ''
    TPout2 = ''
    TPout3 = ''

    try:
        opts, args = getopt.getopt(argv, ":hi:o:")
    except:
        usage()
        sys.exit(2) # This should just... never happen

    for opt, arg in opts:
        if opt == '-h':
            usage()
            sys.exit(2)
        elif opt == '-i':
            CLin = str(arg) + "checkpointLoss.csv"
            TPin = str(arg) + "trainProgress.csv"
        elif opt == '-o':
            CLout1 = str(arg) + "Epoch_valLoss.jpg"
            CLout2 = str(arg) + "Iteration_valLoss.jpg"
            TPout1 = str(arg) + "Epoch_trainLoss.jpg"
            TPout2 = str(arg) + "Iteration_trainLoss.jpg"
            TPout3 = str(arg) + "Epoch_aveTrainLoss.jpg"
        else:
            usage()
            sys.exit(2)

    # Read data from .csv files
    CLdata = np.genfromtxt(CLin, delimiter=',', names=['Epoch', 'Iteration', 'Validation_Loss'])
    TPdata = np.genfromtxt(TPin, delimiter=',', names=['Epoch', 'Iteration', 'Training_Loss'])
    TPave = []
    # Computing average training loss per epoch
    d = collections.defaultdict(list)
    for item in TPdata:
        d[item[0]].append(item)
    for key, value in d.items():
        aveTrainLoss = sum([x[2] for x in value])/float(len(value))
        TPave.append([key, aveTrainLoss])

    TPave = [list(elem) for elem in TPave]
    #print([i[1] for i in TPave])

    # Epoch vs. validation loss
    fig1 = plt.figure()
    plt.plot(CLdata['Epoch'], CLdata['Validation_Loss'], color='b', label='Training Progress, Epoch/Validation Loss')
    plt.xlabel("Epoch")
    plt.ylabel("Validation Loss")
    plt.savefig(CLout1)
    plt.close(fig1)

    # Iteration vs. validation loss
    fig2 = plt.figure()
    plt.plot(CLdata['Iteration'], CLdata['Validation_Loss'], color='b', label='Training Progress, Iteration/Validation Loss')
    plt.xlabel("Iteration")
    plt.ylabel("Validation Loss")
    plt.savefig(CLout2)
    plt.close(fig2)

    # Epoch vs. training loss
    fig3 = plt.figure()
    plt.plot(TPdata['Epoch'], TPdata['Training_Loss'], color='b', label='Training Progress, Epoch/Training Loss')
    plt.xlabel("Epoch")
    plt.ylabel("Training Loss")
    plt.savefig(TPout1)
    plt.close(fig3)

    # Epoch vs. ave. training loss
    fig4 = plt.figure()
        # Sorting the ave list, which for some reason isn't sorted.
    TPsorted = sorted(TPave)

    plt.plot([i[0] for i in TPsorted], [i[1] for i in TPsorted], color='b', label='Training Progress, Epoch/Average Training Loss')
    plt.xlabel("Epoch")
    plt.ylabel("Average Training Loss")
    plt.savefig(TPout3)
    plt.close(fig4)

    # Iteration vs. training loss
    fig5 = plt.figure()
    plt.plot(TPdata['Iteration'], TPdata['Training_Loss'], color='b', label='Training Progress, Iteration/Training Loss')
    plt.xlabel("Iteration")
    plt.ylabel("Training Loss")
    plt.savefig(TPout2)
    plt.close(fig5)

if __name__ == "__main__":
    main(sys.argv[1:])

max_stars_repo_name: DylanAuty/torch-rnn-constrained

max_issues_repo_name: DylanAuty/torch-rnn-constrained

max_forks_repo_name: DylanAuty/torch-rnn-constrained


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

from collections import namedtuple
import numpy as np
import cv2 as cv

Detection = namedtuple("Detection", ["image_path", "gt", "pred"])

def bb_intersection_over_union(box_a, box_b):
    x_a = max(box_a[0], box_b[0])
    y_a = max(box_a[1], box_a[1])
    x_b = min(box_a[2], box_b[2])
    y_b = min(box_a[3], box_b[3])
    inter_area = max(0, x_b - x_a + 1) * max(0, y_b - y_a + 1)
    box_a_area = (box_a[2] - box_a[0] + 1) * (box_a[3] - box_a[1] + 1)
    box_b_area = (box_b[2] - box_b[0] + 1) * (box_b[3] - box_b[1] + 1)
    iou = inter_area / float(box_a_area + box_b_area - inter_area)
    return iou

max_stars_repo_name: afterloe/opencv-practice

max_issues_repo_name: afterloe/Opencv-practice

max_forks_repo_name: afterloe/Opencv-practice


text:
#!/usr/bin/env python

import rospy
from geometry_msgs.msg import Wrench
from nav_msgs.msg import Odometry
from sensor_msgs.msg import LaserScan
import numpy as np
import tf
import nav_msgs.msg

class RocketController:

    def __init__(self):
        rospy.init_node('rocket_controller')
        self.time_last_plan = rospy.Time.now()

        # fin torques
        self.pub_fin1 = rospy.Publisher('/rocket/fin1_torque', Wrench, queue_size=10)
        self.pub_fin2 = rospy.Publisher('/rocket/fin2_torque', Wrench, queue_size=10)
        self.pub_fin3 = rospy.Publisher('/rocket/fin3_torque', Wrench, queue_size=10)
        self.pub_fin4 = rospy.Publisher('/rocket/fin4_torque', Wrench, queue_size=10)

        # where rocket is at
        self.sub_ground_truth = rospy.Subscriber('/rocket/ground_truth', Odometry,
                self.control_callback)

    def control_callback(self, odom):
        # position
        position = odom.pose.pose.position

        # orientation
        orientation = odom.pose.pose.orientation
        euler = tf.transformations.euler_from_quaternion(
            (orientation.x, orientation.y, orientation.z, orientation.w))
        pitch = euler[1]
        yaw = euler[2]

        # rotation rate
        rates = odom.twist.twist.angular
        vel = odom.twist.twist.linear
        yaw_rate = rates.z

        # publish fram for transform tree
        br = tf.TransformBroadcaster()
        br.sendTransform(
            (position.x, position.y, position.z),
            (orientation.x, orientation.y, orientation.z, orientation.w) ,
            rospy.Time.now(),
            "base_link",
            "map")

        # fin1
        msg = Wrench()
        msg.force.x = 0
        msg.force.y = 0
        msg.force.z = 0
        msg.torque.x = 10
        msg.torque.y = 0
        msg.torque.z = 0
        self.pub_fin1.publish(msg)

if __name__ == "__main__":
    RocketController()
    rospy.spin()


text:
import numpy as np
from scipy.integrate import simps

def get_wss_magnitude(wss_vector):
    if (wss_vector.shape[-1] != 3):
        return wss_vector

    return np.sum(wss_vector ** 2, axis=-1) ** 0.5

def get_tawss(wss, dt):
    x = np.arange(0, len(wss))
    x = np.asarray(x) * dt

    wss_int = simps(wss, x, axis=0)
    return wss_int / (x[-1] - x[0])

def get_osi(wss_vector, dt):
    eps = 1e-12
    if (wss_vector.shape[-1] != 3):
        return np.zeros((wss_vector.shape[1], wss_vector.shape[2]))

    x = np.arange(0, len(wss_vector))
    x = np.asarray(x) * dt

    # numerator
    wss_x = simps(wss_vector[...,0], x, axis=0)
    wss_y = simps(wss_vector[...,1], x, axis=0)
    wss_z = simps(wss_vector[...,2], x, axis=0)

    wss_mag = (wss_x ** 2 + wss_y ** 2 + wss_z ** 2) ** 0.5

    # denominator
    denom = get_wss_magnitude(wss_vector)
    denom = simps(denom, x, axis=0)

    frac = (wss_mag + eps) / (denom + eps)

    osi = 0.5 * ( 1 - frac)

    return osi

def get_osi_discrete(wss_vector):
    if (wss_vector.shape[-1] != 3):
        return np.zeros((wss_vector.shape[1], wss_vector.shape[2]))

    # numerator
    wss_x = np.sum(wss_vector[...,0], axis=0)
    wss_y = np.sum(wss_vector[...,1], axis=0)
    wss_z = np.sum(wss_vector[...,2], axis=0)

    wss_mag = (wss_x ** 2 + wss_y ** 2 + wss_z ** 2) ** 0.5

    # denominator
    denom = get_wss_magnitude(wss_vector)
    denom = np.sum(denom, axis=0)

    frac = wss_mag / denom
    osi = 0.5 * ( 1 - frac)

    return osi

max_stars_repo_name: EdwardFerdian/WSSNet

max_issues_repo_name: EdwardFerdian/WSSNet

max_forks_repo_name: EdwardFerdian/WSSNet

reason: import numpy,from scipy


text:
# # Object Detection Demo

###################
###   Imports   ###
###################
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile

from distutils.version import StrictVersion
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image

# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")

# imports from the object detection module.
from utils import label_map_util
from utils import visualization_utils as vis_util

from object_detection.utils import ops as utils_ops

if StrictVersion(tf.__version__) < StrictVersion('1.12.0'):
	raise ImportError('Please upgrade your TensorFlow installation to v1.12.*.')

#---------------------------#
#     Model preparation     #
#---------------------------#
# Any model exported using the `export_inference_graph.py` tool can be loaded here simply by
# changing `PATH_TO_CKPT` to point to a new .pb file. By default we use an "SSD with Mobilenet" model here. See the [
# detection model zoo](https://github.com/tensorflow/models/blob/master/object_detection/g3doc/detection_model_zoo.md
# ) for a list of other models that can be run out-of-the-box with varying speeds and accuracies.

# What model to download.
MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
# MODEL_NAME = 'faster_rcnn_inception_resnet_v2_atrous_oid_v4_2018_12_12'
MODEL_FILE = MODEL_NAME + '.tar.gz'
DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'

# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join('data', 'mscoco_complete_label_map.pbtxt')
# PATH_TO_LABELS = os.path.join('data', 'oid_v4_label_map.pbtxt')

# Download Model if needed, and extract
if not os.path.exists(MODEL_NAME + '/frozen_inference_graph.pb'):
	print ('Downloading the model')
	opener = urllib.request.URLopener()
	opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
	tar_file = tarfile.open(MODEL_FILE)
	for file in tar_file.getmembers():
		file_name = os.path.basename(file.name)
		if 'frozen_inference_graph.pb' in file_name:
			tar_file.extract(file, os.getcwd())
	print ('Download complete')
else:
	print ('Model already exists')

# ## Load a (frozen) Tensorflow model into memory.
# normal graph used for detection
detection_graph = tf.Graph()
with detection_graph.as_default():
	# serialized graph to read frozen model from .pb file
	graph_def = tf.GraphDef()
	with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as f:
		graph_def.ParseFromString(f.read())
		# import serialized graph to 'detection_graph' (set as default above)
		tf.import_graph_def(graph_def, name='')

# ## Loading label map
# Label maps map indices to category names, so that when our convolution network predicts `5`,
# we know that this corresponds to `airplane`. Here we use internal utility functions, but anything that returns a
# dictionary mapping integers to appropriate string labels would be fine
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)

# ## Helper code
def load_image_into_numpy_array(image):
	(im_width, im_height) = image.size
	return np.array(image.getdata()).reshape(
		(im_height, im_width, 3)).astype(np.uint8)

#-----------------------#
#   Detection routine   #
#-----------------------#
def run_inference_for_single_image(image_np_expanded, graph):
	with graph.as_default():
		with tf.Session() as sess:
			# Get handles to input and output tensors
			ops = tf.get_default_graph().get_operations()
			# names of all tensors present in the graph
			all_tensor_names = {output.name for op in ops for output in op.outputs}
			tensor_dict = {}

			# check if any of the tensors in the graph is named as one of the keys below
			for key in ['num_detections', 'detection_boxes', 'detection_scores', 'detection_classes', 'detection_masks']:
				tensor_name = key + ':0'
				# if any tensor has same name as one of the keys, add tensor to 'tensor_dict' using that key as label
				# Es. (tensor_dict['detection_classes'] = detection_classes)
				if tensor_name in all_tensor_names:
					tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(tensor_name)

			# --------------------------------------------------------------------------------------
			# if 'detection mask' is one of tensors found in graph, do SOMETHING WITH IT???
			# --------------------------------------------------------------------------------------
			if 'detection_masks' in tensor_dict:
				# The following processing is only for single image
				detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
				detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
				# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
				real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
				detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
				detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
				detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
					detection_masks, detection_boxes, image_np_expanded.shape[1], image_np_expanded.shape[2])
				detection_masks_reframed = tf.cast(
					tf.greater(detection_masks_reframed, 0.5), tf.uint8)
				# Follow the convention by adding back the batch dimension
				tensor_dict['detection_masks'] = tf.expand_dims(detection_masks_reframed, 0)
			# --------------------------------------------------------------------------------------

			# tensor in the graph corresponding to the picture to analyze, must be fed with picture during Session
			image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')

			# Actual detection (same as in simplified version, just instead of listing the tensors
			# to evaluate you give 'tensor_dict' as argument)
			output_dict = sess.run(tensor_dict, feed_dict={image_tensor: image_np_expanded})

			# all outputs are float32 numpy arrays, so convert types as appropriate
			output_dict['num_detections'] = int(output_dict['num_detections'][0])
			output_dict['detection_classes'] = output_dict['detection_classes'][0].astype(np.int64)
			output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
			output_dict['detection_scores'] = output_dict['detection_scores'][0]
			if 'detection_masks' in output_dict:
				output_dict['detection_masks'] = output_dict['detection_masks'][0]
	return output_dict
#-----------------------#
#-----------------------#

# #------------------------------#
# #   Send IMAGES to detection   #
# #------------------------------#
# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)
test_img_path = "test_images/"
num_test_images = len([f for f in os.listdir(test_img_path) if os.path.isfile(os.path.join(test_img_path, f))])
test_img_list = [os.path.join(test_img_path, f'image{i:02d}.jpg') for i in range(1, num_test_images+1) ]
img_count = 0
for image_path in test_img_list:
	image = Image.open(image_path)
	# array-based representation of image will be used later to prepare result image with boxes and labels on it
	image_np = load_image_into_numpy_array(image)
	# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
	image_np_expanded = np.expand_dims(image_np, axis=0)

	# Actual detection.
	output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
	# Visualization of the results of a detection.
	vis_util.visualize_boxes_and_labels_on_image_array(image_np,
													   output_dict['detection_boxes'],
													   output_dict['detection_classes'],
													   output_dict['detection_scores'],
													   category_index,
													   instance_masks=output_dict.get('detection_masks'),
													   use_normalized_coordinates=True,
													   line_thickness=8)
	plt.figure(figsize=IMAGE_SIZE)
	plt.imshow(image_np)
	plt.imsave(f'./results/img_{img_count:02d}.jpg', image_np)
	img_count+=1
# #------------------------------#
# #------------------------------#
# #------------------------------#


text:
"""ChunkLoader utilities.
"""
from typing import Optional

import dask.array as da
import numpy as np

def _get_type_str(data) -> str:
    """Get human readable name for the data's type.

    Returns
    -------
    str
        A string like "ndarray" or "dask".
    """
    data_type = type(data)

    if data_type == list:
        if len(data) == 0:
            return "EMPTY"
        # Recursively get the type string of the zeroth level.
        return _get_type_str(data[0])

    if data_type == da.Array:
        # Special case this because otherwise data_type.__name__
        # below would just return "Array".
        return "dask"

    # For class numpy.ndarray this returns "ndarray"
    return data_type.__name__

class StatWindow:
    """Average value over a rolling window.

    Notes
    -----
    Inserting values once the window is full is O(1). However calculating
    the average is O(N) although using numpy.

    Parameters
    ----------
    size : int
        The size of the window.

    Attributes
    ----------
    values : ndarray
        The values in our window.
    """

    def __init__(self, size: int):
        self.size = size
        self.values = np.array([])  # float64 array

        # Once the window is full we insert values at this index, the
        # index loops through the slots circularly, forever.
        self.index = 0

    def add(self, value: float) -> None:
        """Add one value to the window.

        Parameters
        ----------
        value : float
            Add this value to the window.
        """
        if len(self.values) < self.size:
            # Not super efficient but once window is full we are O(1).
            self.values = np.append(self.values, value)
        else:
            # Window is full, poke values in circularly.
            self.values[self.index] = value
            self.index = (self.index + 1) % self.size

    @property
    def average(self) -> Optional[float]:
        """Return the average of all the values in the window.

        Returns
        -------
        float
            The average of all values in the window.
        """
        if len(self.values) == 0:
            return None
        return float(np.average(self.values))

max_stars_repo_name: MaksHess/napari

max_issues_repo_name: MaksHess/napari

max_forks_repo_name: MaksHess/napari


text:
import argparse
import csv
from pathlib import Path
import pickle
from typing import Dict, List, Set, Union

import h5py
import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable
from matplotlib.patches import Ellipse
import numpy as np
from numpy import random
import seaborn as sns
from tqdm import tqdm
import umap

sns.set_theme(style='white', context='paper')

NBINS=50

def reduce_fps(fps_h5: str, k: Union[int, float] = 0.1,
               overwrite: bool = False) -> np.ndarray:
    fps_h5 = Path(fps_h5)
    fps_reduced_npy = fps_h5.with_name(f'{fps_h5.stem}_reduced.npy')
    if fps_reduced_npy.exists() and not overwrite:
        return np.load(fps_reduced_npy)

    with h5py.File(str(fps_h5), 'r') as h5f:
        fps = h5f['fps'][:]

    if isinstance(k, float):
        k = int(k*len(fps))
    train_idxs = random.choice(len(fps), size=k, replace=False)
    X_train = fps[train_idxs]

    transform = umap.UMAP(
        n_neighbors=15, metric='jaccard', n_components=2, verbose=True
    ).fit(X_train)

    fps_reduced = transform.transform(fps)

    fps_reduced = np.empty((len(fps), 2))
    for i in tqdm(range(100000)):
        fps_reduced[i] = transform.tranform(fps[i])
    np.save(fps_reduced_npy, fps_reduced)
    print(f'Reduced fingerprints saved to {fps_reduced_npy}')

    return fps_reduced

def get_num_iters(expt_dir: str) -> int:
    data_dir = Path(expt_dir) / 'data'
    scores_csvs = [p_csv for p_csv in data_dir.iterdir()
                   if 'iter' in p_csv.stem]
    return len(scores_csvs)

def read_scores(scores_csv: str) -> Dict:
    """read the scores contained in the file located at scores_csv"""
    scores = {}
    failures = {}
    with open(scores_csv) as fid:
        reader = csv.reader(fid)
        next(reader)
        for row in reader:
            try:
                scores[row[0]] = float(row[1])
            except:
                failures[row[0]] = None

    return scores, failures

def get_new_smis_by_epoch(scores_csvs: List[str]) -> List[Dict]:
    """get the set of new points and associated scores acquired at each
    iteration in the list of scores_csvs that are already sorted by iteration"""
    all_smis = set()
    new_smiss = []
    for scores_csv in scores_csvs:
        scores, _ = read_scores(scores_csv)
        new_smis = {smi for smi in scores.keys()
                      if smi not in all_smis}
        new_smiss.append(new_smis)
        all_smis.update(new_smis)

    return new_smiss

def add_ellipses(ax, invert=False):
    kwargs = dict(fill=False, color='white' if invert else 'black', lw=1.)
    ax.add_patch(Ellipse(xy=(6.05, -6.0), width=2.9, height=1.2, **kwargs))
    ax.add_patch(Ellipse(xy=(16.05, 4.5), width=1.7, height=2.6, **kwargs))
    ax.add_patch(Ellipse(xy=(2.2, 12.7), width=1.7, height=2.3, **kwargs))

def add_model_data(fig, gs, expt_dir, i, model,
                   d_smi_fp_score, zmin, zmax,
                   portrait, n_models):
    scores_csvs = [p_csv for p_csv in (Path(expt_dir)/'data').iterdir()
                   if 'iter' in p_csv.stem]
    scores_csvs = sorted(scores_csvs, key=lambda p: int(p.stem.split('_')[4]))

    new_smiss = get_new_smis_by_epoch(scores_csvs)
    if portrait:
        MAX_ROW = len(new_smiss)
    else:
        MAX_ROW = n_models

    axs = []
    for j, new_smis in enumerate(new_smiss):
        if portrait:
            row, col = j, i
        else:
            row, col = i, j

        ax = fig.add_subplot(gs[row, col])
        fps_scores = sorted(
            [d_smi_fp_score[smi] for smi in new_smis],
            key=lambda fp_score: fp_score[1], reverse=True
        )
        fps, scores = zip(*fps_scores)
        fps, scores = np.array(fps), -1*np.array(scores)

        ax.scatter(
            fps[:, 0], fps[:, 1],
            marker='.', c=scores, s=2, cmap='plasma', vmin=zmin, vmax=zmax
        )
        add_ellipses(ax)

        if row==0:
            if portrait:
                ax.set_title(model)

        if row==MAX_ROW:
            if not portrait:
                ax.set_xlabel(j)

        if col==0:
            if portrait:
                ax.set_ylabel(row)
            else:
                ax.set_ylabel(model)

        ax.set_xticks([])
        ax.set_yticks([])

        axs.append(ax)

    return fig, axs

def si_fig(d_smi_fp_score, expt_dirs, models,
           portrait=True):
    zmin = -max(score for score in d_smi_score.values() if score < 0)
    zmax = -min(d_smi_score.values())
    zmin = 9 #round((zmin+zmax)/2)

    n_models = len(expt_dirs)
    n_iters = get_num_iters(expt_dirs[0])

    if portrait:
        fig = plt.figure(figsize=(10*1.15, 15), constrained_layout=True)
        gs = fig.add_gridspec(nrows=n_iters, ncols=n_models)
    else:
        fig = plt.figure(figsize=(15*1.15, 10), constrained_layout=True)
        gs = fig.add_gridspec(nrows=n_models, ncols=n_iters)

    axs = []
    for i, (expt_dir, model) in enumerate(zip(expt_dirs, models)):
        fig, axs_ = add_model_data(fig, gs, expt_dir, i, model,
                                   d_smi_fp_score, zmin, zmax,
                                   portrait, n_models)
        axs.extend(axs_)

    ticks = list(range(zmin, round(zmax)))

    colormap = ScalarMappable(cmap='plasma')
    colormap.set_clim(zmin, zmax)
    cbar = plt.colorbar(colormap, ax=axs, aspect=60, ticks=ticks)
    cbar.ax.set_title('Score')

    ticks[0] = f'≤{ticks[0]}'
    cbar.ax.set_yticklabels(ticks)

    if portrait:
        fig.text(0.01, 0.5, 'Iteration', ha='center', va='center',
                 rotation='vertical', fontsize=14, fontweight='bold',)
        # fig.text(0.465, 1.01, 'Model', ha='center', va='top',
        #          fontsize=14, fontweight='bold',)
    else:
        # fig.text(0.01, 0.5, 'Model', ha='center', va='center',
        #          rotation='vertical', fontsize=16, fontweight='bold')
        fig.text(0.48, 0.01, 'Iteration', ha='center', va='center',
                 fontsize=16, fontweight='bold',)

    return fig

def add_top1k_panel(fig, gs, d_smi_fp_score):
    top_1k_fps_scores = sorted(
        d_smi_fp_score.values(),
        key = lambda fp_score: fp_score[1]
    )[:1000]
    top_1k_fps_embedded, _ = zip(*top_1k_fps_scores)
    top_1k_fps_embedded = np.array(top_1k_fps_embedded)

    ax = fig.add_subplot(gs[0:2, 0:2])
    ax.scatter(top_1k_fps_embedded[:, 0], top_1k_fps_embedded[:, 1],
                c='grey', marker='.')
    add_ellipses(ax)

    return fig, ax

def add_density_panel(fig, gs, ax1, d_smi_fp_score):
    fps, _ = zip(*d_smi_fp_score.values())
    fps_embedded = np.array(fps)

    ax2 = fig.add_subplot(gs[0:2, 2:])
    _, _, _, im = ax2.hist2d(
        x=fps_embedded[:, 0], y=fps_embedded[:, 1],
        bins=NBINS, cmap='Purples_r'
    )
    ax2_cbar = plt.colorbar(im, ax=(ax1, ax2), aspect=20)
    ax2_cbar.ax.set_title('Points')

    ax2.set_yticks([])

    add_ellipses(ax2, True)

    return fig, ax2

def add_model_row(fig, gs, expt_dir, row, iters, model,
                  d_smi_fp_score, zmin, zmax, ):
    scores_csvs = [p_csv for p_csv in (Path(expt_dir)/'data').iterdir()
                   if 'iter' in p_csv.stem]
    scores_csvs = sorted(scores_csvs, key=lambda p: int(p.stem.split('_')[4]))

    col = 0
    axs = []
    for j, new_smis in enumerate(get_new_smis_by_epoch(scores_csvs)):
        if j not in iters:
            continue

        ax = fig.add_subplot(gs[row, col])
        fps_scores = sorted(
            [d_smi_fp_score[smi] for smi in new_smis],
            key=lambda fp_score: fp_score[1], reverse=True)
        fps, scores = zip(*fps_scores)
        fps, scores = np.array(fps), -1*np.array(scores)
        ax.scatter(
            fps[:, 0], fps[:, 1], alpha=0.75,
            marker='.', c=scores, s=2, cmap='plasma', vmin=zmin, vmax=zmax
        )
        add_ellipses(ax)

        if row==4:
            ax.set_xlabel(j)
        if col==0:
            ax.set_ylabel(model)

        ax.set_xticks([])
        ax.set_yticks([])

        axs.append(ax)
        col+=1

    return fig, axs

def main_fig(d_smi_fp_score, expt_dirs,
             models=None, iters=None,):
    models = ['RF', 'NN', 'MPN'] or models
    iters = [0, 1, 3, 5] or iters[:4]

    _, scores = d_smi_fp_score.values()
    zmax = -min(scores)
    zmin = -max(score for score in scores if score < 0)
    zmin = 9 #round(2*(zmin+zmax)/3)

    nrows = 2+len(expt_dirs)
    ncols = 4
    fig = plt.figure(figsize=(2*ncols*1.15, 2*nrows), constrained_layout=True)
    gs = fig.add_gridspec(nrows=nrows, ncols=4)

    fig, ax1 = add_top1k_panel(fig, gs, d_smi_fp_score)
    fig, ax2 = add_density_panel(fig, gs, ax1, d_smi_fp_score)

    axs = []
    for i, (expt_dir, model) in enumerate(zip(expt_dirs, models)):
        fig, axs_ = add_model_row(fig, gs, expt_dir, i+2, iters, model,
                                  d_smi_fp_score, zmin, zmax)
        axs.extend(axs_)

    colormap = ScalarMappable(cmap='plasma')
    colormap.set_clim(zmin, zmax)

    ticks = list(range(zmin, round(zmax)))

    cbar = plt.colorbar(colormap, ax=axs, aspect=30, ticks=ticks)
    cbar.ax.set_title('Score')

    ticks[0] = f'≤{ticks[0]}'
    cbar.ax.set_yticklabels(ticks)

    fig.text(-0.03, 1.03, 'A', transform=ax1.transAxes,
             fontsize=16, fontweight='bold', va='center', ha='right')
    fig.text(-0.0, 1.03, 'B', transform=ax2.transAxes,
             fontsize=16, fontweight='bold', va='center', ha='left')
    fig.text(-0.03, -0.075, 'C', transform=ax1.transAxes,
                fontsize=16, fontweight='bold', va='center', ha='right')

    fig.text(0.475, 0.005, 'Iteration', ha='center', va='center',
             fontweight='bold')

    return fig

def restricted_float_or_int(arg: str) -> Union[float, int]:
    try:
        value = int(arg)
        if value < 0:
            raise argparse.ArgumentTypeError(f'{value} is less than 0')
    except ValueError:
        value = float(arg)
        if value < 0 or value > 1:
            raise argparse.ArgumentTypeError(f'{value} must be in [0,1]')

    return value

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--fps-h5',
                        help='an HDF5 file containing the full fingerprint of each molecule in the library.')
    parser.add_argument('-k', type=restricted_float_or_int, default=0.1,
                        help='the number or fraction of the library from which to train a UMAP embedding. Large libraries (e.g., HTS) are memory-limited and must be trained on a fraction.')
    parser.add_argument('--overwrite', action='store_true', default=False,
                        help='The reduced fingerprints are saved in a file after calculation for reproducibility/speed. Setting this to true will recalculate and overwrite the file, if it exists.')
    parser.add_argument('--fps-npy',
                        help='the filepath of a .npy file containing the reduced fingerprints')
    parser.add_argument('--scores-dict-pkl',
                        help='the filepath of a pickle file containing the scores dictionary')
    parser.add_argument('--smis-csv',
                        help='a csv file containing the SMILES string associated with each fingerprint in fps-h5. Must be in the same ordering as fps-h5')
    parser.add_argument('--expt-dirs', nargs='+',
                        help='the MolPAL output directories')
    parser.add_argument('--models', nargs='+',
                        help='the respective name of each model used in --expt-dirs')
    parser.add_argument('--iters', nargs=4, type=int, default=[0, 1, 3, 5],
                        help='the FOUR iterations of points to show in the main figure')
    parser.add_argument('--si-fig', action='store_true', default=False,
                        help='whether to produce generate the SI fig instead of the main fig')
    parser.add_argument('--landscape', action='store_true', default=False,
                        help='whether to produce a landscape SI figure')
    args = parser.parse_args()

    if args.fps_npy:
        fps_embedded = np.load(args.fps_npy)
    else:
        fps_embedded = reduce_fps(args.fps_h5, args.k, args.overwrite)

    d_smi_score = pickle.load(open(args.scores_dict_pkl, 'rb'))

    with open(args.smis_csv, 'r') as fid:
        reader = csv.reader(fid); next(reader)
        smis = [row[0] for row in tqdm(reader)]

    d_smi_idx = {smi: i for i, smi in enumerate(smis)}

    d_smi_fp_score = {
        smi: (fps_embedded[i], d_smi_score[smi])
        for i, smi in tqdm(enumerate(smis)) if smi in d_smi_score
    }

    if not args.si_fig:
        fig = main_fig(
            d_smi_fp_score, args.expt_dirs, args.models, args.iters
        )

        name = input('Figure name: ')
        fig.savefig(f'paper/figures/{name}.pdf')
    else:
        fig = si_fig(
            d_smi_fp_score, args.expt_dirs, args.models, not args.landscape
        )

        name = input('Figure name: ')
        fig.savefig(f'paper/figures/{name}.pdf')

max_stars_repo_name: mchaker/lab-molpal

max_issues_repo_name: mchaker/lab-molpal

max_forks_repo_name: mchaker/lab-molpal

reason: import numpy,from numpy


text:
import numpy as np

from PuzzleLib import Config
from PuzzleLib.Backend import gpuarray

from PuzzleLib.Modules.Module import ModuleError
from PuzzleLib.Modules.DeconvND import DeconvND

class Deconv3D(DeconvND):
	def __init__(self, inmaps, outmaps, size, stride=1, pad=0, dilation=1, postpad=0, wscale=1.0, useBias=True,
				 name=None, initscheme=None, empty=False, groups=1):
		super().__init__(
			3, inmaps, outmaps, size, stride, pad, dilation, postpad, wscale, useBias, name, initscheme, empty, groups
		)
		self.registerBlueprint(locals())

	def checkDataShape(self, shape):
		if len(shape) != 5:
			raise ModuleError("Data must be 5d tensor")

		_, inmaps, _, _, _ = shape
		if inmaps != self.W.shape[0]:
			raise ModuleError("Data has %d maps (expected: %d)" % (inmaps, self.W.shape[0]))

	def dataShapeFrom(self, shape):
		batchsize, inmaps, ind, inh, inw = shape
		_, outmaps, fd, fh, fw = self.W.shape

		dpad, hpad, wpad = self.pad
		dpostpad, hpostpad, wpostpad = self.postpad

		ddilation, hdilation, wdilation = self.dilation
		dstride, hstride, wstride = self.stride

		outmaps *= self.groups
		outd = (ind - 1) * dstride + ddilation * (fd - 1) - 2 * dpad + 1 + dpostpad
		outh = (inh - 1) * hstride + hdilation * (fh - 1) - 2 * hpad + 1 + hpostpad
		outw = (inw - 1) * wstride + wdilation * (fw - 1) - 2 * wpad + 1 + wpostpad

		return batchsize, outmaps, outd, outh, outw

	def checkGradShape(self, shape):
		if len(shape) != 5:
			raise ModuleError("Grad must be 5d tensor")

		_, outmaps, outd, outh, outw = shape
		_, _, fd, fh, fw = self.W.shape

		dpad, hpad, wpad = self.pad
		ddilation, hdilation, wdilation = self.dilation

		if outmaps != self.W.shape[1] * self.groups:
			raise ModuleError("Grad has %d maps (expected: %d)" % (outmaps, self.W.shape[1] * self.groups))

		if outd + 2 * dpad < ddilation * (fd - 1) + 1:
			raise ModuleError(
				"Grad maps depth is too small (got %d, expected at least %d)" %
				(outd + 2 * dpad, ddilation * (fd - 1) + 1)
			)

		if outh + 2 * hpad < hdilation * (fh - 1) + 1:
			raise ModuleError(
				"Grad maps height is too small (got %d, expected at least %d)" %
				(outh + 2 * hpad, hdilation * (fh - 1) + 1)
			)

		if outw + 2 * wpad < wdilation * (fw - 1) + 1:
			raise ModuleError(
				"Grad maps width is too small (got %d, expected at least %d)" %
				(outw + 2 * wpad, wdilation * (fw - 1) + 1)
			)

	def gradShapeFrom(self, shape):
		batchsize, outmaps, outd, outh, outw = shape
		inmaps, _, fd, fh, fw = self.W.shape

		dpad, hpad, wpad = self.pad
		ddilation, hdilation, wdilation = self.dilation
		dstride, hstride, wstride = self.stride

		ind = (outd + 2 * dpad - ddilation * (fd - 1) - 1) // dstride + 1
		inh = (outh + 2 * hpad - hdilation * (fh - 1) - 1) // hstride + 1
		inw = (outw + 2 * wpad - wdilation * (fw - 1) - 1) // wstride + 1

		return batchsize, inmaps, ind, inh, inw

def unittest():
	if Config.backend in {Config.Backend.cuda, Config.Backend.hip}:
		multiMapsWithPadsTest()

	trainTest()

def multiMapsWithPadsTest():
	batchsize, inmaps, d, h, w = 5, 4, 2, 2, 2
	outmaps, size, stride, pad, dilation = 4, 2, 2, 1, 2

	hostData = np.random.randn(batchsize, inmaps, d, h, w).astype(np.float32)
	data = gpuarray.to_gpu(hostData)

	deconv = Deconv3D(inmaps, outmaps, size=size, stride=stride, pad=pad, dilation=dilation, initscheme="gaussian")
	deconv(data)

	hostW, hostBias = deconv.W.get(), deconv.b.get()
	dl = dilation

	hostOutData = np.zeros(
		deconv.data.shape[:2] +
		(deconv.data.shape[2] + 2 * pad, deconv.data.shape[3] + 2 * pad, deconv.data.shape[4] + 2 * pad),
		dtype=np.float32
	)

	for c in range(outmaps):
		hostOutData[:, c, :, :, :] = hostBias[0, c, 0, 0, 0]

	for b in range(batchsize):
		for oc in range(outmaps):
			for ic in range(inmaps):
				for z in range(d):
					for y in range(h):
						for x in range(w):
							for dz in range(size):
								for dy in range(size):
									for dx in range(size):
										hostOutData[b, oc, z*stride + dz*dl, y*stride + dy*dl, x*stride + dx*dl] += \
											hostW[ic, oc, dz, dy, dx] * hostData[b, ic, z, y, x]

	assert np.allclose(hostOutData[:, :, pad:-pad, pad:-pad, pad:-pad], deconv.data.get())

	hostGrad = np.random.randn(*deconv.data.shape).astype(np.float32)
	grad = gpuarray.to_gpu(hostGrad)

	deconv.backward(grad)

	hostExtGrad = np.zeros(
		grad.shape[:2] + (grad.shape[2] + 2 * pad, grad.shape[3] + 2 * pad, grad.shape[4] + 2 * pad), dtype=np.float32
	)

	hostExtGrad[:, :, pad:-pad, pad:-pad, pad:-pad] = hostGrad
	hostGrad = hostExtGrad

	hostInGrad = np.zeros(hostData.shape, dtype=np.float32)

	for b in range(batchsize):
		for ic in range(inmaps):
			for oc in range(outmaps):
				for z in range(d):
					for y in range(h):
						for x in range(w):
							for dz in range(size):
								for dy in range(size):
									for dx in range(size):
										hostInGrad[b, ic, z, y, x] += \
											hostGrad[b, oc, z*stride + dz*dl, y*stride + dy*dl, x*stride+dx*dl] * \
											hostW[ic, oc, dz, dy, dx]

	assert np.allclose(hostInGrad, deconv.grad.get())

	hostWGrad = np.zeros(deconv.getVar("W").grad.shape, dtype=np.float32)
	for b in range(batchsize):
		for ic in range(inmaps):
			for oc in range(outmaps):
				for dz in range(size):
					for dy in range(size):
						for dx in range(size):
							for z in range(d):
								for y in range(h):
									for x in range(w):
										hostWGrad[ic, oc, dz, dy, dx] += \
											hostGrad[b, oc, z*stride + dz*dl, y*stride + dy*dl, x*stride + dx*dl] * \
											hostData[b, ic, z, y, x]

	assert np.allclose(hostWGrad, deconv.getVar("W").grad.get())

	hostBGrad = np.empty(hostBias.shape, dtype=np.float32)
	for oc in range(outmaps):
		hostBGrad[0, oc, 0, 0, 0] = np.sum(hostGrad[:, oc, :, :, :])

	assert np.allclose(hostBGrad, deconv.getVar("b").grad.get())

def trainTest():
	batchsize, inmaps, d, h, w = 5, 5, 2, 2, 2
	outmaps = 1
	size = 3

	data = gpuarray.to_gpu(np.random.normal(0.0, 1.0, (batchsize, inmaps, d, h, w)).astype(np.float32))
	deconv = Deconv3D(inmaps, outmaps, size)

	from PuzzleLib.Cost.MSE import MSE
	mse = MSE()

	target = gpuarray.to_gpu(np.random.normal(0.0, 1.0, (batchsize, outmaps, 4, 4, 4)).astype(np.float32))

	for i in range(100):
		learnRate = 1e-2

		deconv(data)
		error, grad = mse(deconv.data, target)

		deconv.backward(grad)
		deconv.updateParams(learnRate)

		if (i + 1) % 5 == 0:
			print("Iteration #%d error: %s" % (i + 1, error))

if __name__ == "__main__":
	unittest()

max_stars_repo_name: EmilPi/PuzzleLib

max_issues_repo_name: EmilPi/PuzzleLib

max_forks_repo_name: EmilPi/PuzzleLib


text:
import numpy as np
from absl import app, flags, logging
from absl.flags import FLAGS

import Arena
from MCTS import MCTS
from hex.matrix_hex_game import MatrixHexGame
from hex.graph_hex_game import GraphHexGame
from hex.graph_hex_board import GraphHexBoard
from hex.NNet import NNetWrapper as NNet, FakeNNet, value_from_shortest_path
from hex.hex_players import RandomPlayer, HumanHexPlayer, UIPlayer, PureMCTSPlayer, GPureMCTSPlayer
from utils import dotdict, config_rec

"""
use this script to play any two agents against each other, or play manually with
any agent.
"""

flags.DEFINE_enum('board_type', 'hex',
    ['hex', 'vortex'],
    'hex: standard hex grid Hex board, '
    'vortex: random grap board played on a Voronoi diagram')
flags.DEFINE_enum('player2', 'nnet',
    ['nnet', 'human', 'random', 'MCTS'],
    'nnet: nnet vs nnet, '
    'human: play interactivly with a human, '
    'random: nnet vs random play agent, '
    'MCTS: nnet vs MCTS with random agent')
flags.DEFINE_boolean('verbose', False, 'show playout')
flags.DEFINE_boolean('graphic', False, 'show playout graphically')
flags.DEFINE_boolean('node_nums', False, 'show node numbers on UI')
flags.DEFINE_integer('num_games', 2, 'Number of games to play')
flags.DEFINE_string('cpu1_checkpoint', 'temp/gat/strong_5x5_b.pth.tar', 'pretrained weights for computer player 1')
flags.DEFINE_string('cpu2_checkpoint', 'temp/gat/strong_5x5_b.pth.tar', 'pretrained weights for computer player 2')
flags.DEFINE_integer('p1_MCTS_sims', 100, 'number of simulated steps taken by tree search for player 1')
flags.DEFINE_integer('p2_MCTS_sims', 100, 'number of simulated steps taken by tree search for player 2 if usincg MCTS')
flags.DEFINE_integer('game_board_size', 5, 'overide default size')
flags.DEFINE_string('p1_nnet', 'base_gat', 'neural net for p,v estimation for player 1')
flags.DEFINE_string('p2_nnet', 'base_gat', 'neural net for p,v estimation for player 2')

def get_action_func(search):
    def action_func(x, p):
        return np.argmax(search.getActionProb(x, temp=0))

    return action_func

def main(_argv):
    if FLAGS.board_type == 'hex':
        g = MatrixHexGame(FLAGS.game_board_size, FLAGS.game_board_size)
        if FLAGS.graphic:
            raise Exception("graphic display not implemented for hex boards")
    elif FLAGS.board_type == 'vortex':
        board = GraphHexBoard.new_vortex_board(FLAGS.game_board_size)
        g = GraphHexGame(board)
        if FLAGS.verbose:
            raise Exception("ascii display not implemented for vortex boards")

    # nnet player 1
    n1 = NNet(g, net_type=FLAGS.p1_nnet)
    n1.load_checkpoint('./', FLAGS.cpu1_checkpoint)
    args1 = dotdict({'numMCTSSims': FLAGS.p1_MCTS_sims, 'cpuct': 1.0})
    mcts1 = MCTS(g, n1, args1)
    player1 = get_action_func(mcts1)
    on_move_end = None
    on_game_end = None

    # player 2
    if FLAGS.player2 == 'human':
        if FLAGS.board_type == 'hex':
            player2 = HumanHexPlayer(g).play
        elif FLAGS.board_type == 'vortex':
            ui = UIPlayer(g, show_node_numbers=FLAGS.node_nums)
            on_move_end = ui.update
            player2 = ui.play
    elif FLAGS.player2 == 'random':
        player2 = RandomPlayer(g).play
    elif FLAGS.player2 == 'nnet':
        n2 = NNet(g, net_type=FLAGS.p2_nnet)
        n2.load_checkpoint('./', FLAGS.cpu2_checkpoint)
        args2 = dotdict({'numMCTSSims': FLAGS.p2_MCTS_sims, 'cpuct': 1.0})
        mcts2 = MCTS(g, n2, args2)
        #player2 = lambda x, p: np.argmax(mcts2.getActionProb(x, temp=0))
        player2 = get_action_func(mcts2)
    elif FLAGS.player2 == 'MCTS':
        if FLAGS.board_type == 'hex' or not FLAGS.graphic:
            mcts2 = PureMCTSPlayer(g, sims=FLAGS.p2_MCTS_sims)
        elif FLAGS.board_type == 'vortex':
            mcts2 = GPureMCTSPlayer(g, sims=FLAGS.p2_MCTS_sims, show_node_numbers=FLAGS.node_nums)
            on_move_end = mcts2.update
            on_game_end = mcts2.on_game_end
        player2 = mcts2.play

    if FLAGS.board_type == 'hex':
        arena = Arena.Arena(player1, player2, g, display=MatrixHexGame.display, display_move=g.display_move)
    elif FLAGS.board_type == 'vortex':
        arena = Arena.Arena(player1, player2, g, on_move_end=on_move_end, on_game_end=on_game_end)

    result = config_rec()
    result["p1_wins"], result["p2_wins"], _ = arena.playGames(FLAGS.num_games, verbose=FLAGS.verbose)
    print("Results:\n\tP1:{}\n\tP2:{}".format(result["p1_wins"], result["p2_wins"]))
    logging.info("result:" + str(result))

if __name__ == '__main__':
    app.run(main)

max_stars_repo_name: phil-hawkins/alpha-zero-general

max_issues_repo_name: phil-hawkins/alpha-zero-general

max_forks_repo_name: phil-hawkins/alpha-zero-general


text:
"""."""

import warnings
import numpy as _numpy

INTERACTIVE_LIST = []

def interactive(obj):
    """."""
    INTERACTIVE_LIST.append(
        {
            'module': obj.__module__,
            'name': obj.__name__
        }
    )

    return obj

def deprecated(function):
    """Define decorator for deprecated functions."""
    def new_function(*args, **kwargs):
        warnings.warn(
            "call to deprecated function {}".format(function.__name__),
            category=DeprecationWarning
        )
        return function(*args, **kwargs)

    new_function.__name__ = function.__name__
    new_function.__doc__ = function.__doc__
    new_function.__dict__.update(function.__dict__)

    return new_function

class Polynom(_numpy.ndarray):
    """."""

    def __new__(cls, polynom):
        """."""
        shape = (len(polynom),)
        array = _numpy.ndarray.__new__(cls, shape=shape)
        array[:] = polynom[:]
        array._polynom = polynom
        return array

    def __setitem__(self, index, value):
        """."""
        if hasattr(self, '_polynom'):
            self._polynom[index] = value
        super().__setitem__(index, value)

    def __eq__(self, other):
        """."""
        if not isinstance(other, Polynom):
            return NotImplemented
        if len(self) != len(other):
            return False
        if (self != other).any():
            return False
        return True

# class Matrix(_numpy.ndarray):

#     def __new__(cls, polynom):
#         shape = (len(polynom), len(polynom[0]))
#         array = _numpy.ndarray.__new__(cls, shape=shape)
#         for i, pol in enumerate(polynom):
#             array[i] = pol[:]
#         array._polynom = polynom
#         return array

#     def __setitem__(self, index, value):
#         if hasattr(self, '_polynom'):
#             self._polynom[index] = value
#         super().__setitem__(index, value)

#     def __eq__(self, other):
#         if not isinstance(other, Polynom):
#             return NotImplemented
#         if len(self) != len(other):
#             return False
#         if (self != other).any():
#             return False
#         return True

max_stars_repo_name: carneirofc/pyaccel

max_issues_repo_name: carneirofc/pyaccel

max_forks_repo_name: carneirofc/pyaccel


text:
from tkinter import *
import numpy as np

from keras.models import load_model
from time import sleep, time
from scipy.io import savemat

class GenericFeedback:
    on = True

    attention_x = 0.0
    attention_y = 0.0

    # Named fields according to Warren doc !
    FIELDS = {"COUNTER": 0, "DATA-TYPE": 1, "AF3": 4, "F7": 5, "F3": 2, "FC5": 3, "T7": 6, "P7": 7, "01": 8, "02": 9,
              "P8": 10, "T8": 11, "FC6": 14, "F4": 15, "F8": 12, "AF4": 13, "DATALINE_1": 16, "DATALINE_2": 17}

    def __init__(self, size=250, block_size=5, device=None, model_path="model-0.38.hdf5"):
        self.size = size
        self.block_size = block_size
        self.device = device
        self.model_path = model_path

        self.fen_init()

    def fen_init(self):
        '''
        Used in the __init__ to define the window
        :return:
        '''
        self.master = Tk()
        self.master.attributes('-fullscreen', True)
        self.can = Canvas(self.master, height=self.size * self.block_size, width=self.size * self.block_size,
                          bg="white")

        self.frame = Frame(self.master, height=self.size * self.block_size, width=self.size * self.block_size)

        self.can.place(relx=0.5, rely=0.5, anchor=CENTER)

        self.__draw_center_cross()

        self.can.pack()
        self.frame.pack()

    def run(self):
        '''
        Create the window and start the animation thread
        :return: None
        '''
        self.master.title("Attention FeedBack")
        self.animationLoop()

        self.master.mainloop()

        # If we reach this,  tkinter frame is closed and we can terminate linked thread
        self.on = False
        # Close the device
        self.device.on = False
        return

    def animationLoop(self):
        '''
        Animation loop body this is called at each screen refresh
        :return: None
        '''
        self.can.delete(ALL)
        self.__draw_center_cross()

        self.__draw_targets()
        self.__plot_attention()

        self.master.after(1, self.animationLoop)

        return

    def get_attention(self):
        '''
        Get data from the device, compute fft etc... and use a model to predict attention, that's the default function.
        Just set the attention_x, attention_y values
        This sould be run in a separate thread.
        :return:
        '''

        self.padding = 30

        self.attention_model = load_model(self.model_path)

        while self.on:
            # Do nothing if no available data
            if len(self.device.data) == 0:
                sleep(0.1)
                continue
            # Get data from the more recent to the oldest
            data = np.asarray(self.device.data)

            ampl = np.transpose(np.abs(np.fft.rfft(data, 50, axis=0)))

            # Best freqs to use
            useful_freqs = [3, 5, 7]

            ampl = ampl[:][:, useful_freqs]
            ampl = np.reshape(ampl, (1, ampl.shape[0] * ampl.shape[1],))

            m = np.max(ampl)
            ampl = ampl / m

            corners = np.asarray(
                [[self.size - self.padding, self.size - self.padding], [self.size - self.padding, self.padding]
                    , [self.padding, self.padding], [self.padding, self.size - self.padding]])
            p = self.attention_model.predict(ampl)

            point = np.sum([corners[i] * p[0][i] for i in range(corners.shape[0])], axis=0)

            print(p)

            self.attention_x, self.attention_y = point[0], point[1]

            pass

        print("Terminate Attention Tracking Model")
        return 0

    def __draw_targets(self):
        center = (int(self.size / 2)) * self.block_size + 1, (int(self.size / 2) + 1) * self.block_size

        posx = center[0] - 100
        posy = center[0] - 100
        self.can.create_rectangle(posx, posy, posx + self.block_size, posy + self.block_size, fill="red")

        posx = center[0] + 100
        posy = center[0] - 100
        self.can.create_rectangle(posx, posy, posx + self.block_size, posy + self.block_size, fill="red")

        posx = center[0] - 100
        posy = center[0] + 100
        self.can.create_rectangle(posx, posy, posx + self.block_size, posy + self.block_size, fill="red")

        posx = center[0] + 100
        posy = center[0] + 100
        self.can.create_rectangle(posx, posy, posx + self.block_size, posy + self.block_size, fill="red")

    def __draw_center_cross(self):
        center = int(self.size / 2) + 1, int(self.size / 2) + 1

        posx = center[0] * self.block_size - self.block_size
        posy = center[1] * self.block_size

        self.can.create_rectangle(posx, posy, posx + 3 * self.block_size, posy + self.block_size, fill="black")

        posx = center[0] * self.block_size
        posy = center[1] * self.block_size - self.block_size

        self.can.create_rectangle(posx, posy, posx + self.block_size, posy + 3 * self.block_size, fill="black")

    def __plot_attention(self):
        posx = int(self.attention_x * self.block_size)
        posy = int(self.attention_y * self.block_size)

        self.can.create_rectangle(posx, posy, posx + self.block_size, posy + self.block_size, fill="blue")

class GenericTask:
    on = True

    BUFFER_SIZE = 256

    # Named fields according to Warren doc !
    FIELDS = {"COUNTER": 0, "DATA-TYPE": 1, "AF3": 4, "F7": 5, "F3": 2, "FC5": 3, "T7": 6, "P7": 7, "01": 8, "02": 9,
              "P8": 10, "T8": 11, "FC6": 14, "F4": 15, "F8": 12, "AF4": 13, "DATALINE_1": 16, "DATALINE_2": 17}

    POS_TARGET = []
    DATES_TARGET = []
    DATES_CLUE = []
    POS_CLUE = []
    ANSWERS = []

    answer = None

    def __init__(self, size=250, block_size=5, device=None, model_path="model-0.38.hdf5", path_save_data="data"):
        self.size = size
        self.block_size = block_size
        self.device = device
        self.model_path = model_path
        self.target_size = 2
        self.path_save_data = path_save_data

        self.begin = time()

        self.fen_init()

    def fen_init(self):
        '''
        Used in the __init__ to define the window
        :return:
        '''

        self.master = Tk()

        self.master.attributes('-fullscreen', True)

        self.can = Canvas(self.master, height=self.size * self.block_size, width=self.size * self.block_size,
                          bg="white")

        self.frame = Frame(self.master, height=self.size * self.block_size, width=self.size * self.block_size)

        self.can.place(relx=0.5, rely=0.5, anchor=CENTER)

        self.__draw_center_cross()

        self.master.bind("q", self.answer_different)
        self.master.bind("d", self.answer_same)

        self.can.pack()
        self.frame.pack()

    def answer_different(self, d):
        self.answer = -1

    def answer_same(self, d):
        self.answer = 1

    def run(self):
        '''
        Create the window and start the animation thread
        :return: None
        '''
        self.master.title("Attention Task")
        self.animationLoop(0)

        self.master.mainloop()

        # dump data

        savemat(self.path_save_data, {"EEG": np.asarray(self.device.data) - self.begin,
                                      "POS_TARGET": self.POS_TARGET,
                                      "POS_CLUE": self.POS_CLUE,
                                      "DATES_TARGET": self.DATES_TARGET,
                                      "DATES_CLUE": self.DATES_CLUE,
                                      "ANSWERS": self.ANSWERS
                                      })

        # If we reach this,  tkinter frame is closed and we can terminate linked thread
        self.on = False
        # Close the device
        self.device.on = False
        return

    def animationLoop(self, state):
        '''
        Animation loop body this is called at each screen refresh
        :return: None
        '''

        if state == 0:
            self.can.delete(ALL)
            self.__draw_center_cross()

            self.answer = 0

            self.rst_time = np.random.randint(1, 5 + 1)

            self.target_pos = np.random.randint(1, 4 + 1)
            self.clue_pos = np.random.randint(1, 4 + 1)

            self.target_sleeptime = np.random.randint(1, 5 + 1)

            self.master.after(self.rst_time*1000, self.animationLoop, 1)

        elif state == 1:
            self.__draw_clue(self.clue_pos)
            self.POS_CLUE.append(self.clue_pos)
            self.DATES_CLUE.append(time()-self.begin)

            self.master.after(500, self.animationLoop, 2)

        elif state == 2:
            self.can.delete(ALL)
            self.__draw_center_cross()

            self.master.after(self.target_sleeptime*1000, self.animationLoop, 3)

        elif state == 3:
            self.__draw_target(self.target_pos, color="#f7f6ef", distance=400)
            self.POS_TARGET.append(self.clue_pos)
            self.DATES_TARGET.append(time()-self.begin)

            self.master.after(200, self.animationLoop, 4)

        elif state == 4:
            self.can.delete(ALL)
            self.__draw_center_cross()

            self.master.after(1000, self.animationLoop, 5)

        elif state == 5:
            self.ANSWERS.append(self.answer)
            self.master.after(10, self.animationLoop, 0)

    def __draw_clue(self, pos, color="red", distance=100):
        center = (int(self.size / 2)) * self.block_size + 1, (int(self.size / 2) + 1) * self.block_size
        posx, posy = 0, 0

        if pos == 1:
            posx = center[0] + distance
            posy = center[0] + distance

        elif pos == 2:
            posx = center[0] - distance
            posy = center[0] + distance

        elif pos == 3:
            posx = center[0] - distance
            posy = center[0] - distance

        elif pos == 4:
            posx = center[0] + distance
            posy = center[0] - distance

        self.can.create_rectangle(posx, posy, posx + self.block_size, posy + self.block_size, fill=color)

    def __draw_target(self, pos, color="red", distance=100):
        center = (int(self.size / 2)) * self.block_size + 1, (int(self.size / 2) + 1) * self.block_size
        posx, posy = 0, 0

        if pos == 1:
            posx = center[0] + distance
            posy = center[0] + distance

        elif pos == 2:
            posx = center[0] - distance
            posy = center[0] + distance

        elif pos == 3:
            posx = center[0] - distance
            posy = center[0] - distance

        elif pos == 4:
            posx = center[0] + distance
            posy = center[0] - distance

        self.can.create_rectangle(posx, posy, posx + self.target_size, posy + self.target_size, fill=color)

    def __draw_targets(self):
        center = (int(self.size / 2)) * self.block_size + 1, (int(self.size / 2) + 1) * self.block_size

        posx = center[0] - 100
        posy = center[0] - 100
        self.can.create_rectangle(posx, posy, posx + self.block_size, posy + self.block_size, fill="red")

        posx = center[0] + 100
        posy = center[0] - 100
        self.can.create_rectangle(posx, posy, posx + self.block_size, posy + self.block_size, fill="red")

        posx = center[0] - 100
        posy = center[0] + 100
        self.can.create_rectangle(posx, posy, posx + self.block_size, posy + self.block_size, fill="red")

        posx = center[0] + 100
        posy = center[0] + 100
        self.can.create_rectangle(posx, posy, posx + self.block_size, posy + self.block_size, fill="red")

    def __draw_center_cross(self):
        center = int(self.size / 2) + 1, int(self.size / 2) + 1

        posx = center[0] * self.block_size - self.block_size
        posy = center[1] * self.block_size

        self.can.create_rectangle(posx, posy, posx + 3 * self.block_size, posy + self.block_size, fill="black")

        posx = center[0] * self.block_size
        posy = center[1] * self.block_size - self.block_size

        self.can.create_rectangle(posx, posy, posx + self.block_size, posy + 3 * self.block_size, fill="black")

max_stars_repo_name: icannos/emotiv-neurofeedback-model

max_issues_repo_name: icannos/emotiv-neurofeedback-model

max_forks_repo_name: icannos/emotiv-neurofeedback-model


text:
import os
import pickle
import numpy as np
from tqdm import tqdm

import torch
import torch.utils.data as data
import torchaudio

from mask_cyclegan_vc.model import Generator, Discriminator
from args.cycleGAN_test_arg_parser import CycleGANTestArgParser
from dataset.vc_dataset import VCDataset
from dataset.base_functions import make_dataset
from mask_cyclegan_vc.utils import decode_melspectrogram
from logger.train_logger import TrainLogger
from saver.model_saver import ModelSaver
import ntpath
from PIL import Image

from mask_cyclegan_vc.utils import denorm_and_numpy, getTimeSeries, extract, power_to_db
import soundfile as sf
from dataset.noise_dataset import NoiseDataset
import time

class MaskCycleGANVCTesting(object):
    """Tester for MaskCycleGAN-VC
    """

    def __init__(self, args):
        """
        Args:
            args (Namespace): Program arguments from argparser
        """
        # Store Args
        self.device = args.device
        self.use_res = args.use_res

        args.num_threads = 0   # test code only supports num_threads = 0
        args.serial_batches = True  # disable data shuffling; comment this line if results on randomly chosen images are needed.
        args.no_flip = True    # no flip; comment this line if results on flipped images are needed.
        args.max_mask_len = 50 #Does not have any impact since an all 1s mask is used

        if hasattr(args,'eval') and args.eval:
            self.eval=True
            os.makedirs(args.eval_save_dir, exist_ok=True)
            self.eval_save_path = os.path.join(args.eval_save_dir,args.filename)
        else:
            self.eval=False
            self.converted_audio_dir = os.path.join(args.save_dir, args.name, 'converted_audio')
            os.makedirs(self.converted_audio_dir, exist_ok=True)

        self.model_name = args.model_name

        self.speaker_A_id = args.speaker_A_id
        self.speaker_B_id = args.speaker_B_id

        self.sample_rate = args.sample_rate

        self.dataset = NoiseDataset(args)

        self.test_dataloader = torch.utils.data.DataLoader(dataset=self.dataset,
                                                           batch_size=1,
                                                           shuffle=False,
                                                           drop_last=False)

        # Generator
        in_channels= 3 if self.use_res else 2
        out_channels = 2 if self.use_res else 1
        self.generator = Generator(in_channels=in_channels, out_channels=out_channels).to(self.device)
        self.generator.eval()

        # Load Generator from ckpt
        self.saver = ModelSaver(args)
        self.saver.load_model(self.generator, self.model_name)

        #Getting train stats

        self.A_max = -float('inf')
        self.A_min = float('inf')
        self.B_max = -float('inf')
        self.B_min = float('inf')

        train_A = os.path.join(args.dataroot,args.class_ids[0],'train')
        train_B = os.path.join(args.dataroot,args.class_ids[1],'train')
        A_paths = sorted(make_dataset(train_A, args.max_dataset_size))
        B_paths = sorted(make_dataset(train_B, args.max_dataset_size))

        for path in A_paths:
            mag_spec, phase, sr = extract(path, sr=8000, energy=1.0, state = 'train')
            log_spec = power_to_db(mag_spec)
            self.A_max = max(self.A_max,log_spec.max())
            self.A_min = min(self.A_min,log_spec.min())

        for path in B_paths:
            mag_spec, phase, sr = extract(path, sr=8000, energy=1.0, state = 'train')
            log_spec = power_to_db(mag_spec)
            self.B_max = max(self.B_max,log_spec.max())
            self.B_min = min(self.B_min,log_spec.min())

    def save_audio(self, opt, visuals_list, img_path, label):

        """
        Borrowed from https://github.com/shashankshirol/GeneratingNoisySpeechData
        """

        results_dir = os.path.join(opt.save_dir, opt.name)
        img_dir = os.path.join(results_dir, 'audios')
        short_path = ntpath.basename(img_path[0])
        name = os.path.splitext(short_path)[0]

        file_name = '%s/%s.wav' % (label, name)
        os.makedirs(os.path.join(img_dir, label), exist_ok=True)
        save_path = os.path.join(img_dir, file_name)

        flag_first = True

        for visual in visuals_list:
            im_data = visual #Obtaining the generated Output
            im = denorm_and_numpy(im_data.unsqueeze(1)) #De-Normalizing the output tensor to reconstruct the spectrogram

            #Resizing the output to 129x128 size (original splits)
            if(im.shape[-1] == 1): #to drop last channel
                im = im[:,:,0]
            im = Image.fromarray(im)
            im = im.resize((128, 129), Image.LANCZOS)
            im = np.asarray(im).astype(np.float)

            if(flag_first):
                spec = im
                flag_first = False
            else:
                spec = np.concatenate((spec, im), axis=1) #concatenating specs to obtain original.

        if label[-1] == 'A':
            train_max = self.A_max
            train_min = self.A_min
        elif label[-1] == 'B':
            train_max = self.B_max
            train_min = self.B_min

        data, sr = getTimeSeries(spec, img_path, opt.spec_power, opt.energy, state = opt.phase, train_min = train_min,train_max =train_max)
        sf.write(save_path, data, sr)

        return

    def loadPickleFile(self, fileName):
        """Loads a Pickle file.

        Args:
            fileName (str): pickle file path

        Returns:
            file object: The loaded pickle file object
        """
        with open(fileName, 'rb') as f:
            return pickle.load(f)

    def test(self):

        ds_len = self.dataset.get_clean_len() if self.model_name == 'generator_A2B' else self.dataset.get_noisy_len()
        idx = 0
        datas = []

        for i, data in enumerate(self.test_dataloader):
            datas.append(data)
        while idx < ds_len:

            # if(idx >= args.num_test):
            #     break

            if self.model_name == 'generator_A2B':
                real = datas[idx]['A'].to(self.device, dtype=torch.float)
                mask = datas[idx]['A_mask'].to(self.device, dtype=torch.float)
                img_path = datas[idx]['A_paths']
            else:
                real = datas[idx]['B'].to(self.device, dtype=torch.float)
                mask = datas[idx]['B_mask'].to(self.device, dtype=torch.float)
                img_path = datas[idx]['B_paths']
            if not self.use_res:
                fake = self.generator(real, mask)
            else:
                fake, _ = self.generator(real, mask, torch.zeros_like(real))
            visuals_list = [fake.detach().cpu()]
            num_comps = datas[idx]["A_comps"] if self.model_name == 'generator_A2B' else datas[idx]["B_comps"]
            comps_processed = 1

            while(comps_processed < num_comps):
                idx += 1
                if self.model_name == 'generator_A2B':
                    real = datas[idx]['A'].to(self.device, dtype=torch.float)
                    mask = datas[idx]['A_mask'].to(self.device, dtype=torch.float)
                    img_path = datas[idx]['A_paths']
                else:
                    real = datas[idx]['B'].to(self.device, dtype=torch.float)
                    mask = datas[idx]['B_mask'].to(self.device, dtype=torch.float)
                    img_path = datas[idx]['B_paths']
                if not self.use_res:
                    fake = self.generator(real, mask)
                else:
                    fake, _ = self.generator(real, mask, torch.zeros_like(real))
                del real
                del mask
                visuals_list.append(fake.detach().cpu())
                comps_processed += 1

            print("saving: ", img_path[0])
            self.save_audio(args, visuals_list, img_path, label= 'fake_B' if self.model_name == 'generator_A2B' else 'fake_A' )
            idx += 1

if __name__ == "__main__":
    start =time.time()
    parser = CycleGANTestArgParser()
    args = parser.parse_args()
    tester = MaskCycleGANVCTesting(args)
    tester.test()
    end = time.time()
    duration = end-start
    print(f'Time taken is {duration//60} minutes {duration%60} seconds.')

max_stars_repo_name: leandermaben/MaskCycleGAN-Augment

max_issues_repo_name: leandermaben/MaskCycleGAN-Augment

max_forks_repo_name: leandermaben/MaskCycleGAN-Augment


text:
import os
import sys

import cv2
import numpy as np

# Load input data
in_file = 'words.data'

# Define visualization parameters
scale_factor = 10
s_index = 6
e_index = -1
h, w = 16, 8

# Loop until you encounter the Esc key
with open(in_file, 'r') as f:
    for line in f.readlines():
        information = np.array([255*float(x) for x in line.split('\t')[s_index:e_index]])
        image = np.reshape(information, (h,w))
        image_scaled = cv2.resize(image, None, fx=scale_factor, fy=scale_factor)
        cv2.imshow('Image', image_scaled)
        a = cv2.waitKey()
        if a == 10:
            break


text:
import os
import shutil
import numpy as np
import pandas as pd
from noboard.csv import SummaryWriter

def test_scalar():
    # Cleaup from failed tests
    shutil.rmtree("test", ignore_errors=True)

    # Write
    scalars = [1, 40, 1000, -1]
    log_dir = "test"
    writer = SummaryWriter(log_dir=log_dir)
    for i, scalar in enumerate(scalars):
        writer.add_scalar("scalar", scalar, global_step=i)
    writer.close()
    scalars = np.asarray(scalars)

    # Read
    df = pd.read_csv("test/scalar.csv")
    recovered = df["scalar"].to_numpy()

    # Cleaup from this test
    shutil.rmtree("test", ignore_errors=True)

    # Test
    assert np.alltrue(np.isclose(scalars, recovered))

def test_scalar_no_write_to_disk():
    # Cleaup from failed tests
    shutil.rmtree("test", ignore_errors=True)

    # Write
    scalars = [1, 40, 1000, -1]
    log_dir = "test"
    writer = SummaryWriter(log_dir=log_dir, write_to_disk=False)
    for i, scalar in enumerate(scalars):
        writer.add_scalar("scalar", scalar, global_step=i)
    writer.close()
    scalars = np.asarray(scalars)

    # Test
    if os.path.exists("test/scalar.csv"):
        assert False  # fail
    else:
        assert True  # pass

    # Cleaup from this test
    shutil.rmtree("test", ignore_errors=True)

max_stars_repo_name: parenthetical-e/noboard

max_issues_repo_name: parenthetical-e/noboard

max_forks_repo_name: parenthetical-e/noboard


text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import sys
import random
import numpy as np
import logging
import tensorflow as tf
from tensorflow.contrib.keras import utils

from constant import *
import utility
from dataset import DataBatchIterator
from mlp import MLP

logger = logging.getLogger(__file__)
formatter_log = "[%(asctime)s - %(filename)s:line %(lineno)s] %(message)s"
logging.basicConfig(
    format = formatter_log,
    datefmt = '%d %b %H:%M:%S')
logger.setLevel(logging.INFO)

flags = tf.app.flags

# GPU option
flags.DEFINE_integer("ses_threads", 4, "Tensorflow CPU session threads to use")
flags.DEFINE_float("gpu_memory_fraction", 0.05, "Fraction of GPU memory to use")
flags.DEFINE_integer("gpu", 0, "select a GPU, 0:gpu0 1:gpu1 (default: 0)")

flags.DEFINE_string("rootpath", ROOT_PATH, "rootdir of the data and model (default: %s)"%ROOT_PATH)
flags.DEFINE_string('train_collection', 'msrvtt10ktrain', 'collection dataset for model training')
flags.DEFINE_string('val_collection', 'msrvtt10kval', 'collection dataset for model validation')
flags.DEFINE_string("model_name", DEFAULT_MODEL_NAME, "model configuration (default: %s)" % DEFAULT_MODEL_NAME)
flags.DEFINE_string('annotation_name', 'conceptsbigram288.txt', 'annotation / lablel name list')
flags.DEFINE_string('vf_name', 'mean_pyresnet-101_rbps13k_step_3_aug_1_dist_2x2,resnet-imagenet-101-0,39,flatten0_output', 'name of the visual feature')
flags.DEFINE_integer('overwrite', 0, 'overwrite existing file (default: 0)')
flags.DEFINE_integer('multi_task', 0, 'multi task learning (default: 0)')
flags.DEFINE_string('aux_train_collection', '', 'auxiliary collection dataset for model training')
flags.DEFINE_string('aux_annotation_name', '', 'auxiliary annotation / lablel name list')

FLAGS = flags.FLAGS

def run_epoch(session, model, train_dataset, val_dataset, multi_task=False):
    train_loss_list = [0] * train_dataset.num_batches
    progbar = utils.Progbar(train_dataset.num_batches, verbose=1)

    for minibatch_index, (ids_train, X_train, Y_train, YE_train) in enumerate(train_dataset):
        if multi_task:
            loss, _ = session.run([model.loss, model.train_op],
                feed_dict={model.input_data: X_train, model.targets: Y_train, model.aux_targets:YE_train})
        else:
            loss, _ = session.run([model.loss, model.train_op],
                feed_dict={model.input_data: X_train, model.targets: Y_train})
        progbar.add(1, values=[("train loss", loss)])
        train_loss_list[minibatch_index] = loss

    overall_perf = np.zeros((3,4))
    progbar = utils.Progbar(val_dataset.num_batches)
    n = 0
    for minibatch_index, (ids_val, X_val, Y_val, _) in enumerate(val_dataset):
        valid_pred_top_indices = session.run(model.pred_top_indices,
            feed_dict={model.input_data: X_val, model.targets: Y_val})
        res = utility.compute_hit_precision_recall_f1(valid_pred_top_indices, Y_val)
        overall_perf += res * X_val.shape[0]
        n += X_val.shape[0]
        perf_so_far = overall_perf / n # hit5, p5, r5 f5
        progbar.add(1, values=zip(str.split("hit5 p5 recall5"), perf_so_far[1,:3]))

    train_loss = np.mean(train_loss_list)
    val_perf = utility.convert_to_one_metric(perf_so_far)

    return (train_loss,  val_perf)

def main(unused_args):
    model_dir = utility.get_model_dir(FLAGS)
    if os.path.exists(model_dir) and not FLAGS.overwrite:
        logger.info('%s exists. quit', model_dir)
        sys.exit(0)

    rootpath = FLAGS.rootpath
    overwrite = FLAGS.overwrite
    train_collection = FLAGS.train_collection
    aux_train_collection = FLAGS.aux_train_collection
    val_collection = FLAGS.val_collection
    vf_name = FLAGS.vf_name
    annotation_name = FLAGS.annotation_name
    aux_annotation_name = FLAGS.aux_annotation_name
    # Load model configuration
    config_path = os.path.join(os.path.dirname(__file__), 'model_conf', FLAGS.model_name + '.py')
    config = utility.load_config(config_path)

    train_loss_hist_file = os.path.join(model_dir, 'train_loss_hist.txt')
    val_per_hist_file = os.path.join(model_dir, '%s.txt' % val_collection)

    if os.path.exists(train_loss_hist_file) and not overwrite:
        logger.info('%s exists. quit', train_loss_hist_file)
        sys.exit(0)

    if not os.path.exists(model_dir):
        os.makedirs(model_dir)
    else:
        os.system('rm -rf %s/*' % model_dir)

    collections = [train_collection, aux_train_collection] if FLAGS.multi_task else [train_collection]
    annotation_names = [annotation_name, aux_annotation_name] if FLAGS.multi_task else [annotation_name]
    concept_files = [os.path.join(rootpath, col, 'Annotations', anno) for (col,anno) in zip(collections, annotation_names)]
    train_dataset = DataBatchIterator(collections, concept_files, vf_name, config.batch_size, rootpath)
    val_dataset = DataBatchIterator([val_collection], concept_files[:1], vf_name, config.batch_size, rootpath)

    aux_classes = train_dataset.aux_num_labels

    # Start model training
    gpu_options = tf.GPUOptions(
          per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction)
    config_proto = tf.ConfigProto(
        intra_op_parallelism_threads=FLAGS.ses_threads, gpu_options=gpu_options, allow_soft_placement=True)

    with tf.Graph().as_default(), tf.Session(config=config_proto) as session:
      with tf.variable_scope("MLPModel"):
        model = MLP(is_training=True,
                     n_layers=[train_dataset.feat_dim, config.hidden_size, train_dataset.num_labels],
                     last_nl_fun=config.output,
                     loss_fun=config.loss,
                     keep_prob=config.keep_prob,
                     top_k=config.top_k,
                     aux_classes=aux_classes,
                     flag_with_saver=True,
                     gpu=FLAGS.gpu)

      if tf.__version__ < '1.0':
          tf.initialize_all_variables().run()
      else:
          tf.global_variables_initializer().run()
      model.assign_lr(session, config.learning_rate)

      best_val_perf = 0
      count_down = config.tolerate
      train_loss_hist = []
      val_per_hist = []
      learning_rate = config.learning_rate

      for epoch in range(config.max_epochs):
          logger.info('epoch %d', epoch)
          train_dataset.shuffle()
          train_loss, val_perf = run_epoch(session, model, train_dataset, val_dataset, FLAGS.multi_task)
          logger.info('train loss %.4f, val perf %.4f', train_loss, val_perf)
          train_loss_hist.append(train_loss)
          val_per_hist.append(val_perf)

          if val_perf > best_val_perf:
              best_val_perf = val_perf
              best_ckpt_name = 'epoch_%d.ckpt' % epoch
              model_file = os.path.join(model_dir, best_ckpt_name)
              model.saver.save(session, model_file)
              logger.info('*** better model -> val_perf=%.4f', val_perf)
          else:
              count_down -= 1
              if 0 == count_down:
                  if learning_rate > 1e-8:
                      count_down = config.tolerate
                      learning_rate /= 2.0
                      model.assign_lr(session, learning_rate)
                      logger.info('adjust lr to %g', learning_rate)
                  else:
                      logger.info("early-stop happend at epoch %d", epoch)
                      break

      with open(train_loss_hist_file, 'w') as fout:
          for i, loss in enumerate(train_loss_hist):
              fout.write("epoch_" + str(i) + " " + str(loss) + "\n")
          fout.close()

      sorted_epoch_perf = sorted(zip(range(len(val_per_hist)), val_per_hist), key = lambda x: x[1], reverse=True)
      with open(val_per_hist_file, 'w') as fout:
          for i, perf in sorted_epoch_perf:
              fout.write("epoch_" + str(i) + " " + str(perf) + "\n")
          fout.close()

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

max_stars_repo_name: li-xirong/coco-cn

max_issues_repo_name: li-xirong/coco-cn

max_forks_repo_name: li-xirong/coco-cn


text:
# Copyright (C) 2018-2019 David Thompson
#
# This file is part of Grassland
#
# It is subject to the license terms in the LICENSE file found in the top-level
# directory of this distribution.
#
# No part of Grassland, including this file, may be copied, modified,
# propagated, or distributed except according to the terms contained in the
# LICENSE file.

import os
import numpy as np
import operator
import tensorflow as tf
import settings
import time

class DetectionModel:
    def __init__(self, path_to_ckpt):
        # Parts of this "__init__" "method adapted from https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb
        self.path_to_ckpt = path_to_ckpt

        self.detection_graph = tf.Graph()
        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(self.path_to_ckpt, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

        self.default_graph = self.detection_graph.as_default()
        self.sess = tf.Session(graph=self.detection_graph)

        # Definite input and output Tensors for detection_graph
        self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')
        # Each box represents a part of the image where a particular object was detected.
        self.detection_boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
        # Each score represent how level of confidence for each of the objects.
        # Score is shown on the result image, together with the class label.
        self.detection_scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
        self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
        self.num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')

    def predict(self, image):
        # Code adapted from https://gist.github.com/madhawav/1546a4b99c8313f06c0b2d7d7b4a09e2
        # Expand dimensions since the trained_model expects images to have shape: [1, None, None, 3]
        image_np_expanded = np.expand_dims(image, axis=0)
        # Actual detection.
        start_time = time.time()
        (boxes, scores, classes, num) = self.sess.run(
            [self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections],
            feed_dict={self.image_tensor: image_np_expanded})
        end_time = time.time()

        print("Elapsed Time:", end_time-start_time)

        im_height, im_width,_ = image.shape

        ''' Commented out to return normalized coordinates instead
        # boxes_list = [None for i in range(boxes.shape[1])]
        # for i in range(boxes.shape[1]):
        #     boxes_list[i] = (int(boxes[0,i,0] * im_height),
        #                 int(boxes[0,i,1]*im_width),
        #                 int(boxes[0,i,2] * im_height),
        #                 int(boxes[0,i,3]*im_width))

        # return boxes_list, scores[0].tolist(), [int(x) for x in classes[0].tolist()], int(num[0])
        '''

        output_dict = {}
        output_dict['detection_boxes'] = boxes[0].tolist()
        output_dict['detection_scores'] = scores[0].tolist()
        output_dict['detection_classes'] =  [int(x) for x in classes[0].tolist()]
        output_dict['num_detections'] = int(num[0])

        return output_dict

    def __enter__(self):
        # for using with "with" block
        return self

    def __exit__(self, type_, value, traceback):
        # close session at the end of "with" block
        self.destroy()

    def destroy(self):
        '''
        Close TensorFlow session
        '''
        self.sess.close()
        self.default_graph.close()


text:
"""Integrator that does not sample points during the training phase but uses a fixed dataset of points"""
from collections.abc import Sequence, Mapping
import pickle

import numpy as np
import torch

from .base_integrator import BaseIntegrator

class FixedSampleSurveyIntegrator(BaseIntegrator):
    """Integrator that trains its model during the survey phase using a pre-computed sample provided externally"""

    def __init__(self, f, trainer, sample=None, n_iter=None, n_iter_survey=1, n_iter_refine=10,
                 n_points=None, n_points_survey=None, n_points_refine=10000, use_survey=False,
                 verbosity=None, trainer_verbosity=None, **kwargs):
        """

        Parameters
        ----------
        f: callable
            ZuNIS-compatible function
        trainer: BasicTrainer
            trainer object used to perform the survey
        sample: tuple of torch.Tensor
            (x, fx, px): target-space point batch drawn from some PDF p, function value batch, PDF value batch p(x)
        n_iter: int
            number of iterations (used for both survey and  refine unless specified)
        n_iter_survey: int
            number of iterations for survey
        n_iter_refine: int
            number of iterations for refine
        n_points: int
            number of points for both survey and refine unless specified
        n_points_survey: int
            number of points for survey
        n_points_refine: int
            number of points for refine
        use_survey: bool
            whether to use the integral estimations from the survey phase. This makes error estimation formally
            incorrect since samples from the refine depend on the survey training, but these correlation can be negligible
            in some cases.
        verbosity: int
            level of verbosity for the integrator-level logger
        trainer_verbosity: int
            level of verbosity for the trainer-level logger
        kwargs
        """
        super(FixedSampleSurveyIntegrator, self).__init__(f=f,
                                                          trainer=trainer,
                                                          trainer_verbosity=trainer_verbosity,
                                                          n_iter=n_iter,
                                                          n_iter_survey=n_iter_survey,
                                                          n_iter_refine=n_iter_refine,
                                                          n_points=n_points,
                                                          n_points_survey=n_points_survey,
                                                          n_points_refine=n_points_refine,
                                                          use_survey=use_survey,
                                                          verbosity=verbosity,
                                                          **kwargs)

        self.sample = sample

    def sample_survey(self, n_points=None, **kwargs):
        """Sample points from the internally stored sample

        Parameters
        ----------
        n_points: int, None
            size of the batch to select from the sample
        kwargs

        Returns
        -------
            tuple of torch.Tensor
                (x,px,fx): sampled points, sampling distribution PDF values, function values

        """
        assert self.sample is not None, "The training sample must be instantiated before starting the survey"

        if n_points is None:
            n_points = self.n_points_survey

        if n_points is None:
            return self.sample

        x, px, fx = self.sample

        sample_size = x.shape[0]
        assert n_points <= sample_size

        # Sample n_points indices randomly among the sample_size options with equal probability
        indices = torch.multinomial(torch.ones(sample_size), n_points)
        return x[indices], px[indices], fx[indices]

    def set_sample(self, sample):
        """Assign a sample to be trained on

        Parameters
        ----------
        sample: tuple of torch.Tensor
            (x,px,fx): sampled points, sampling distribution PDF values, function values
        """
        assert len(sample) == 3, "A sample is a Sequence of three elements"
        for el in sample:
            assert isinstance(el, torch.Tensor), "All elements of a sample must be tensors"
        assert sample[0].shape[0] == sample[1].shape[0] == sample[2].shape[0], \
            "All elements of a sample must share the same batch size"
        assert len(sample[0].shape) == 2 and len(sample[1].shape) == len(sample[2].shape) == 1, \
            "Sample shapes must be (n_batch, n_dim), (n_batch), (n_batch)"
        assert sample[0].shape[1] == self.model_trainer.flow.d, "Number of dimensions must match the flow"

        self.sample = sample

    def set_sample_pickle(self, pickle_path, device=None):
        """Assign a sample to be trained on from a pickle file
        The pickle file must either contain a tuple (x,px,fx) of point batch, PDF value batch, function batch
        or a mapping with keys "x", "px", "fx". In either case, these batches must be valid inputs for torch.tensor

        Parameters
        ----------
        pickle_path: str
            path to the pickle file.
        device: torch.device, None
            device on which to send the sample. If none is provided, flow parameter device will be used
        """
        with open(pickle_path, "r") as picklefile:
            pickle_sample = pickle.load(picklefile)

        if device is None:
            device = next(self.model_trainer.flow.parameters()).device

        if isinstance(pickle_sample, Sequence):
            self.set_sample([torch.tensor(el).to(device) for el in pickle_sample])

        if isinstance(pickle_sample, Mapping):
            self.set_sample([
                torch.tensor(pickle_sample[key]).to(device) for key in ["x", "px", "fx"]
            ])

        raise TypeError("Pickled sample must be either sequences or mappings")

    def set_sample_csv(self, csv_path, device=None, delimiter=",", dtype=np.float):
        """Assign a sample to be trained on from a csv file
        The file must contain equal length rows with at least four columns, all numerical.
        All columns but the last two are interpreted as point coordinates,
        the next-to-last is the point PDF and the last is the function value.

        Parameters
        ----------
        csv_path: str
            path to the csv file
        device: torch.device
            device to which to send the sample
        delimiter: str
            delimiter of the csv file
        """

        data = np.genfromtxt(csv_path, delimiter=delimiter, dtype=dtype)
        x = torch.tensor(data[:, :-2]).to(device)
        px = torch.tensor(data[:, -2]).to(device)
        fx = torch.tensor(data[:, -1]).to(device)

        self.set_sample((x, px, fx))

max_stars_repo_name: NGoetz/zunis

max_issues_repo_name: NGoetz/zunis

max_forks_repo_name: NGoetz/zunis