text:
"""Shivali Chandra
Restructured code that takes coordinates of sides and axes of rotation, and rotates the sides
until they meet, forming a closed 3D shape. As of 5/3/15 can fold cubes, square pyramids, and
other prisms with square bases.
"""

import numpy as np
import math
import collections

def find_intersection_distances(p1,y1,x1,y2,x2):
	#checks if axis is horizontal or vertical
	if y2-y1 == 0:
		dist = math.fabs(p1[1]-y1)
	elif x2-x1 == 0:
		dist = math.fabs(p1[0]-x1)
	else:
		#not used as of now, calculating new distance based off of diagonal axis
		axis_line_slope = (y2-y1)/(x2-x1)
		perp_slope = -1/(axis_line_slope)
		c1 = axis_line_slope * x1 - y1
		c2 = perp_slope * p1[0] - p1[1]
		x_intersect = (c2 - c1)/(perp_slope - axis_line_slope)
		y_intersect = perp_slope * x_intersect + c2
		dist = math.sqrt((x_intersect-p1[0])**2+(y_intersect-p1[1])**2)
	hypotenuse = math.sqrt((x2-p1[0])**2+(y2-p1[1])**2)
	return dist

def move_to_actual_coord(old_side,side_dict,side_num,theta):
	"""Change the xy coordinates of the sides to the correct values from the original image
		Input: side coordinates, sides dictionary
		Output: new side coordinates in the sides dictionary
	"""
	final_side = list()
	#y1,x1,y2,x2 are coordinates of axis normalized to zero
	y1 = old_side[0][1]
	x1 = old_side[0][0]
	y2 = old_side[len(old_side)-1][1]
	x2 = old_side[len(old_side)-1][0]

	#variables are coordinates of actual axis
	new_xaxis = side_dict[side_num][1][len(old_side)-1][0]
	new_xaxis1 = side_dict[side_num][1][0][0]
	new_yaxis = side_dict[side_num][1][len(old_side)-1][1]
	new_yaxis1 = side_dict[side_num][1][0][1]

	#iterate through each point of a side
	for i,j in enumerate(old_side):
		x = side_dict[side_num][1][i][0]
		y = side_dict[side_num][1][i][1]
		intersections = find_intersection_distances((j[0],j[1]),y1,x1,y2,x2)
		dist = intersections
		coordinates = {1:(x,(dist+new_yaxis)),2:(x,(new_yaxis-dist)),3:((new_xaxis-dist),y),4:((new_xaxis+dist),y)}
		intersections = find_intersection_distances((j[0],j[1]),y1,x1,y2,x2)
		#check which direction the fold needs to be (inwards or outwards) depending on axis orientation
		if (new_xaxis>new_xaxis1) and (new_yaxis-new_yaxis1==0):
			if (math.degrees(theta) <= 90):
				if (new_yaxis<0):
					[fin_x,fin_y] = coordinates[1]
				else:
					[fin_x,fin_y] = coordinates[2]
			else:
				if (new_yaxis<0):
					[fin_x,fin_y] = coordinates[2]
				else:
					[fin_x,fin_y] = coordinates[1]
		elif (new_xaxis<new_xaxis1) and (new_yaxis-new_yaxis1==0):
			if math.degrees(theta) <= 90:
				if (new_yaxis<0):
					[fin_x,fin_y] = coordinates[2]
				else:
					[fin_x,fin_y] = coordinates[1]
			else:
				if (new_yaxis<0):
					[fin_x,fin_y] = coordinates[1]
				else:
					[fin_x,fin_y] = coordinates[2]
		elif (new_yaxis>new_yaxis1) and (new_xaxis-new_xaxis1==0):
			if math.degrees(theta) <= 90:
				if (new_xaxis<0):
					[fin_x,fin_y] = coordinates[3]
				else:
					[fin_x,fin_y] = coordinates[4]
			else:
				if (new_xaxis<0):
					[fin_x,fin_y] = coordinates[4]
				else:
					[fin_x,fin_y] = coordinates[3]
		else:
			if math.degrees(theta) <=90:
				if (new_xaxis<0):
					[fin_x,fin_y] = coordinates[4]
				else:
					[fin_x,fin_y] = coordinates[3]
			else:
				if (new_xaxis<0):
					[fin_x,fin_y] = coordinates[3]
				else:
					[fin_x,fin_y] = coordinates[4]

		z = j[2]

		#add coordinates to final_side
		final_coord = fin_x/1.0,fin_y/1.0,z
		final_coord = list(final_coord)
		final_side.append(final_coord)

	#add final_side to dictionary of sides
	side_dict[side_num] = list(side_dict[side_num])
	side_dict[side_num].append(final_side)
	return side_dict

def transform_side(side_num,side_dict,theta):
	"""Transform the coordinates of the side onto the perpendicular plane using Euler-Rodrigues formula
		Input: side coordinates, sides dictionary, theta
		Output: new coordinates
	"""
	side = side_dict[side_num][0]
	new_side = list()
	#calculating axis of rotation
	axis = side[len(side)-1][0]-side[0][0],0.0,0.0
	#converting theta to radians
	rad = math.radians(theta)
	for i in side:
		#calculating vector for each point in side
		side_vector = i[0],i[1],0.0
		#Euler-Rodrigues formula to rotate vectors
		axis = np.asarray(axis)
		theta = np.asarray(rad)
		axis = axis/math.sqrt(np.dot(axis, axis))
		a = math.cos(theta/2.0)
		b, c, d = -axis*math.sin(theta/2.0)
		aa, bb, cc, dd = a*a, b*b, c*c, d*d
		bc, ad, ac, ab, bd, cd = b*c, a*d, a*c, a*b, b*d, c*d
		multiplier = np.array([[aa+bb-cc-dd, 2.0*(bc+ad), 2.0*(bd-ac)],
					[2.0*(bc-ad), aa+cc-bb-dd, 2.0*(cd+ab)],
					[2.0*(bd+ac), 2.0*(cd-ab), aa+dd-bb-cc]])
		transform_vector = (np.dot(multiplier, side_vector))
		#round points to nearest whole number, add to list of transformed side coordinates
		folded_vector = round(transform_vector[0]),round(transform_vector[1]),round(transform_vector[2])
		new_side.append(folded_vector)
	#call function to translate the x,y coordinates with relation to the actual side coordinates
	moved_side = move_to_actual_coord(new_side,side_dict,side_num,theta)
	return moved_side

def output(theta,final_list):
	"""Function takes each point and returns the x,y,z coordinates in three lists.
		Input: theta (for testing), final sides dictionary
		Output: three lists of x,y,z
	"""
	#create 3 empty lists for the x,y,z coordinate values
	x = list()
	y = list()
	z = list()
	final_coordinates = []

	#iterate through dictionary to add the side coordinates to a list
	for i in final_list:
		final_coordinates.append(final_list[i][2])

	#iterate through coordinate list to create three lists of x,y, and z respectively
	for i in final_coordinates:
		for j in i:
			x.append(j[0])
			y.append(j[1])
			z.append(j[2])
	return x,y,z

def check_sides(run,temp,theta,fin):
	"""Recursive function takes in each side and continuously checks for sides to meet up
		Input: sides that are being folded, temporary dict with sides to check against, theta,
		full list of sides_old_coordinates
		Output: final theta, dictionary of final sides
	"""
	#create list and dictionaries for storing points
	vector_sides = temp
	data = []
	redo_sides = {}
	new_run = {}

	#iterate through dictionary of the sides still being folded
	for i in run:
		position = run[i][2]
		#create keys of the sets of two points that define each line in a side
		for j in range(1,len(position)):
			if ((position[j][0]==position[j-1][0]) or (position[j][1]==position[j-1][1])):
				key = str(position[j][0])+str(position[j][1])+str(position[j-1][0])+str(position[j-1][1])
			elif ((position[j][0]>position[j-1][0]) or (position[j][1]>position[j-1][1])):
				key = str(position[j][0])+str(position[j][1])+str(position[j-1][0])+str(position[j-1][1])
			else:
				key = str(position[j-1][0])+str(position[j-1][1])+str(position[j][0])+str(position[j][1])
			#add keys, side numbers as values to a dictionary
			if key in vector_sides:
				vector_sides[key].append(i)
			else:
				vector_sides[key] = [i]
	for k in vector_sides:
		#if a key has more than one value, two sides have folded to meet up
		if len(vector_sides[k])>1:
			data.append(vector_sides[k][0])
			data.append(vector_sides[k][1])
	#check to see if all the sides have met another side, new_side_list returns those that have not
	new_side_list = list(set(run.keys())-set(data))
	#if there are no sides left to fold, return the final list of coordinates
	if not new_side_list:
		return theta,run
	else:
		#if there are, add to theta to have the side fold again
		new_t = theta+1

		#create a new dictionary with only the sides that need to be folded
		for i in run.keys():
			if i in new_side_list:
				redo_sides[i] = [run[i][0]]
				redo_sides[i].append(run[i][1])
			else:
				#create a temp dictionary of sides that don't need to be folded (to compare coordinates with)
				new_run[i] = run[i]
			#call function to fold sides again
			redone = transform_side(i,redo_sides,new_t)

		#add the completed and the newly folded sides to a new list
		final = redone.copy()
		final.update(new_run)
		#call this function again to check if all sides have met another side
		if theta < 360:
			return check_sides(redone,new_run,new_t,final)
		else:
			return -1

def make_sides(sides,theta):
	"""Function iterates through the sides and folds each one, returning new coordinates
		Input: sides dictionary, theta
		Output: the transformed sides
	"""
	length = len(sides)

	#iterate through sides to fold each side individually
	for i in sides:
		side = sides[i][0]
		transformed_side = transform_side(i,sides,theta)
	return transformed_side

def main(sides,xy_coord):
	"""Main function that creates a dictionary with the two lists and calls other functions
		Input: list of sides normalized to 0,0, and list of actual side coordinates
		Output: the list of x,y,z coordinates of the final folded shape
	"""
	theta = 20
	sides_old_coordinates = {}

	#iterate through the sides to create a dictionary with side number as key,
	#val 1 as the side normalized to 0, and val 2 as the actual side coordinates
	for i in range(0,len(sides)):
		val1 = sides[i]
		val2 = xy_coord[i]
		if i not in sides_old_coordinates:
			sides_old_coordinates[i] = val1,val2

	#call function to iterate through sides and fold each one
	run_fxn = make_sides(sides_old_coordinates,theta)
	#call function to check that sides have met up (prism has been folded)
	final_sides = check_sides(run_fxn,{},theta,{})
	#call function to output the final lists of x,y,z coordinates
	if final_sides == -1:
		return "Sorry, these sides never meet up!Here is what I am trying to fold:",xy_coord
	else:
		return output(final_sides[0],final_sides[1])

if __name__ == "__main__":

	#following hard-coded shapes are for testing purposes
	"""CUBE (WORKS)"""
	#side_coordinates = (([0,0],[0,6],[6,6],[6,0]),([0,0],[0,6],[6,6],[6,0]),([0,0],[0,6],[6,6],[6,0]),([0,0],[0,6],[6,6],[6,0]))
	#actual_coordinates = (([6,6],[0,6],[0,12],[6,12]),([6,12],[6,18],[12,18],[12,12]),([12,12],[18,12],[18,6],[12,6]),([12,6],[12,0],[6,0],[6,6]))

	"""side_coordinates = (([0.0, 0.0], [0.0, 194.0], [176.00000000000003, 193.99999999999997], [176.0, 0.0]), ([0.0, 0.0], [0.0, 176.0], [173.0, 176.0], [173.0, 0.0]), ([0.0, 0.0], [0.0, 194.0], [165.00000000000003, 193.99999999999997], [165.0, 0.0]), ([0.0, 0.0], [0.0, 176.0], [147.99999999999997, 176.00000000000003], [148.0, 0.0]))
	actual_coordinates = (([381, 451], [381, 645], [557, 645], [557, 451]), ([205, 451], [381, 451], [381, 278], [205, 278]), ([205, 645], [205, 451], [40, 451], [40, 645]), ([381, 645], [205, 645], [205, 793], [381, 793]))
	print main(side_coordinates,actual_coordinates)"""

	"""side_coordinates = (([0,0],[0,110],[100,100],[100,0]),([0,0],[0,110],[100,100],[100,0]),([0,0],[0,115],[100,100],[100,0]),([0,0],[0,110],[100,100],[100,0]))
	actual_coordinates = (([100,200],[100,310],[200,300],[200,200]),([200,200],[310,200],[300,100],[200,100]),([200,100],[200,-15],[100,0],[100,100]),([100,100],[-10,100],[0,200],[100,200]))
	print main(side_coordinates,actual_coordinates)"""

	side_coordinates = (([0.0, 0.0], [0.0, 173.0], [176.00000000000003, 172.99999999999997], [176.0, 0.0]), ([0.0, 0.0], [0.0, 165.0], [194.0, 165.0], [194.0, 0.0]), ([0.0, 0.0], [0.0, 148.0], [176.0, 148.0], [176.0, 0.0]), ([0.0, 0.0], [0.0, 176.0], [194.0, 176.0], [194.0, 0.0]))
	actual_coordinates = (([381, 451], [381, 278], [205, 278], [205, 451]), ([205, 451], [40, 451], [40, 645], [205, 645]), ([205, 645], [205, 793], [381, 793], [381, 645]), ([381, 645], [557, 645], [557, 451], [381, 451]))
	print main(side_coordinates,actual_coordinates)

	"""RECTANGULAR PRISM
	side_coordinates = (([0.0, 0.4], [0.0, 2.0], [1.5, 2.0], [1.0, 0.0]), ([0.0, 0.0], [0.0, 2.0], [1.0, 2.0], [1.0, 0.0]), ([0.0, 0.0], [0.0, 2.0], [0.9999999999999998, 2.0], [1.0, 0.0]), ([0.0, 0.0], [0.0, 2.0], [0.9999999999999999, 2.0], [1.0, 0.0]))
	actual_coordinates = (([3, 2.2], [3, 0], [2, 0], [2, 2]), ([3, 3], [5, 3], [5, 2], [3, 2]), ([2, 3], [2, 5], [3, 5], [3, 3]), ([2, 2], [0, 2], [0, 3], [2, 3]))

	print main(side_coordinates,actual_coordinates)"""

	"""SQUARE PYRAMID (WORKS)
	side_coordinates = (([0,0],[3,6],[6,0]),([0,0],[3,6],[6,0]),([0,0],[3,6],[6,0]),([0,0],[3,6],[6,0]))
	actual_coordinates = (([6,6],[0,9],[6,12]),([6,12],[9,18],[12,12]),([12,12],[18,9],[12,6]),([12,6],[9,0],[6,6]))

	print main(side_coordinates,actual_coordinates)"""

	"""TRIANGULAR PRISM (WIP)
	side_coordinates = (([0,0],[0,6],[6,6],[6,0]),([0,0],[3,6],[6,0]),([0,0],[0,6],[6,6],[6,0]),([0,0],[3,6],[6,0]))
	actual_coordinates = (([6,6],[0,6],[0,12],[6,12]),([6,12],[9,18],[12,12]),([12,12],[18,12],[18,6],[12,6]),([12,6],[9,0],[6,6]))

	print main(side_coordinates,actual_coordinates)"""

	"""PYRAMID (DOESN'T WORK)
	side_coordinates = (([0,0],[3,6],[6,0]),([0,0],[3,6],[6,0]),([0,0],[3,6],[6,0]))
	actual_coordinates = (([6,0],[0,0],[3,6]),([3,6],[6,12],[9,6]),([9,6],[12,0],[6,0]))

	print main(side_coordinates,actual_coordinates)"""

max_stars_repo_name: ctada/napCAD

max_issues_repo_name: ctada/napCAD

max_forks_repo_name: ctada/napCAD


text:
# Author: Moises Henrique Pereira
# this class handles the auxiliar component ComboboxDoubleListView
# this component has a combobox with completion to help the user
# and has to lists to show to user the allowed values and the not allowes values given a feature

from numpy.lib.arraysetops import isin
from .DoubleRadioButtonView import DoubleRadioButtonView

class DoubleRadioButtonController:

    def __init__(self, parent=None):
        self.__view = DoubleRadioButtonView(parent)

    @property
    def view(self):
        return self.__view

    # this function set the initial values to the component
    def initializeView(self, featureName, value0, value1):
        assert isinstance(featureName, str)
        assert isinstance(value0, str)
        assert isinstance(value1, str)

        self.__view.setContent(featureName, value0, value1)
        self.__view.show()

    # this function set a specific value to corresponding widget
    def setSelectedValue(self, selectedValue):
        # assert isinstance(selectedValue, str)
        assert selectedValue is not None

        self.__view.setSelectedValue(str(selectedValue))

    # this function returns a dictionary with the value of the widgets
    def getContent(self):
        return self.__view.getContent()

ext: py

max_stars_repo_name: MoisesHenr/OCEAN

max_issues_repo_name: MoisesHenr/OCEAN

max_forks_repo_name: MoisesHenr/OCEAN

reason: from numpy


text:
import numpy as np
import pandas as pd
import time
import datetime

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import requests
from bs4 import BeautifulSoup
from tqdm import tqdm

def scrape_links(links_to_scrape, link_type="one", attribute_chosen="article-body"):
    """
    The link collected by scraped pages

    Args:
        link_list (list): All the links to scrape

        sleep_duration (float): Sleep in between scrapes

    Returns:
        [tuple]: heading, content
    """
    # Placeholders
    list_heading = []
    list_content = []
    list_links = []
    bar = tqdm(iterable=links_to_scrape, leave=True, position=0)
    # Traverse over the links
    for link in bar:
        # Request
        page_req = requests.get(link)

        # Check Status code
        if page_req.status_code == 200:
            # Perform scraping
            flag = False
            soup = BeautifulSoup(markup=page_req.content, features="lxml")
            for tag in soup.findAll("div", attrs={"class": str(attribute_chosen)}):
                flag = True
                if link_type == "one":
                    list_content.append(tag.get_text())
                else:
                    list_content.append(
                        " ".join([i.get_text() for i in tag.findAll("p")])
                    )

            # Check if body was found
            if flag:
                # Collect the potential heading
                heading = soup.find("title").string

                # Check if heading is none
                if heading == None:
                    list_heading.pop(-1)
                    continue
                else:
                    list_heading.append(heading)
                    list_links.append(link)
            else:
                # Move to the next point
                continue

            # Printer
            bar.set_description(
                "%s, %s, %s, %s"
                % (heading[:50], len(list_content), len(list_heading), len(list_links))
            )

        else:
            # Bad request
            raise Exception("Error Code --> %d" % (page_req.status_code))

    # Return
    return list_content, list_heading, list_links

class YHNW_scraper(object):
    """
    Creates the Yahoo News Scraper
    """

    def __init__(self, sleep_duration, scroll_depth):
        # Make the instance variables
        self.sleep_duration = sleep_duration
        self.driver = webdriver.Chrome("chromedriver.exe")
        self.scroll_depth = scroll_depth
        self.global_var = 0

    def infinite_scroll(self, wait_time=1.5):
        # Fetch webpage
        self.driver.maximize_window()
        scroll = 0
        for i in range(self.scroll_depth):
            self.driver.execute_script("window.scrollTo(%d, 1000000);" % scroll)
            scroll = self.driver.execute_script("return document.body.scrollHeight")
            time.sleep(wait_time)

        # sleep
        time.sleep(2)

    def infinite_scroll_with_restart(self, wait_time=3, depth=20):
        # Fetch webpage
        self.driver.maximize_window()
        scroll = 0
        for i in range(depth):
            if i == 3 or i == 1:
                self.driver.execute_script(
                    "window.scrollTo(%d, %d);" % (0, scroll - 500000)
                )
            else:
                self.driver.execute_script("window.scrollTo(%d, 1000000);" % scroll)
                scroll = self.driver.execute_script("return document.body.scrollHeight")
            time.sleep(wait_time)

        # sleep
        time.sleep(2)

    def _link_collector(self):
        # Harcode the links that have the same format
        link_library = {
            "type_one": [
                "//h3[@class='Mb(5px)']//a",
                [
                    "https://in.news.yahoo.com/",
                    "https://in.news.yahoo.com/world",
                    "https://in.news.yahoo.com/national",
                    "https://in.finance.yahoo.com/",
                    "https://in.finance.yahoo.com/personal-finance",
                    "https://in.finance.yahoo.com/topic/careers",
                    "https://in.finance.yahoo.com/topic/real-estate",
                    "https://in.finance.yahoo.com/topic/loans-deposits",
                    "https://in.finance.yahoo.com/topic/insurance",
                    "https://in.finance.yahoo.com/topic/tax",
                    "https://in.finance.yahoo.com/topic/mutual-fund",
                    "https://in.finance.yahoo.com/topic/tech",
                    "https://in.finance.yahoo.com/topic/autos",
                    "https://in.style.yahoo.com/",
                    "https://in.news.yahoo.com/sports",
                ],
            ],
            "type_two": [
                "//div[@class='article-thumbnail']//a",
                [
                    "https://cricket.yahoo.net/news",
                ],
            ],
        }

        # Scape the links
        list_links_type_one = []
        list_links_type_two = []

        for link_type, data in link_library.items():
            # link to scrape in
            for link_to_scrape in data[-1]:
                # Fetch the link
                self.driver.get(link_to_scrape)

                # Scroll to end
                if link_type == "type_one":
                    self.infinite_scroll()
                    xpath = data[0]
                    scroll = self.driver.find_elements_by_xpath(xpath=xpath)
                    links_curr = [s.get_attribute("href") for s in scroll]
                    list_links_type_one.extend(links_curr)
                else:
                    self.infinite_scroll_with_restart(wait_time=3)
                    xpath = data[0]
                    scroll = self.driver.find_elements_by_xpath(xpath=xpath)
                    links_curr = [s.get_attribute("href") for s in scroll]
                    list_links_type_two.extend(links_curr)

                self.global_var += len(links_curr)
                print(
                    "%s : %d -- %d" % (link_to_scrape, len(links_curr), self.global_var)
                )

        # Reurn links
        return list_links_type_one, list_links_type_two

    def _scrape(self):
        # Collect type1 and type 2 links
        list_links_type_one, list_links_type_two = self._link_collector()

        # Close the driver
        self.driver.quit()

        # Collect seperate links
        list_content_t1, list_heading_t1, list_links_t1 = scrape_links(
            links_to_scrape=list_links_type_one, attribute_chosen="caas-body"
        )
        list_content_t2, list_heading_t2, list_links_t2 = scrape_links(
            links_to_scrape=list_links_type_two, link_type="two"
        )

        # Save as dataframe
        data_df = pd.DataFrame(
            {
                "title": list_heading_t1 + list_heading_t2,
                "content": list_content_t1 + list_content_t2,
                "links": list_links_t1 + list_links_t2,
            }
        )

        data_df.to_csv("YHNW_data_%s.csv" % datetime.datetime.now().date(), index=False)

        return "Done"

    def run_pipe(self):
        status = self._scrape()

# Run the scraper
inst = YHNW_scraper(1, 5)
inst.run_pipe()

max_stars_repo_name: thepooons/news-rec-cw

max_issues_repo_name: thepooons/news-rec-cw

max_forks_repo_name: thepooons/news-rec-cw

reason: import numpy


text:
#  Version: 2020.02.21
#
#  MIT License
#
#  Copyright (c) 2018 Jiankang Deng and Jia Guo
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in all
#  copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#  SOFTWARE.
#

import argparse
import numpy as np
import mxnet as mx
from config import config
from data import FaceSegIter
from metric import NMEMetric

parser = argparse.ArgumentParser(description='test nme on rec data')
# general
parser.add_argument('--rec', default='./data_2d/ibug.rec', help='rec data path')
parser.add_argument('--prefix', default='', help='model prefix')
parser.add_argument('--epoch', type=int, default=1, help='model epoch')
parser.add_argument('--gpu', type=int, default=0, help='')
parser.add_argument('--landmark-type', default='2d', help='')
parser.add_argument('--image-size', type=int, default=128, help='')
args = parser.parse_args()

rec_path = args.rec
ctx_id = args.gpu
prefix = args.prefix
epoch = args.epoch
image_size = (args.image_size, args.image_size)
config.landmark_type = args.landmark_type
config.input_img_size = image_size[0]

if ctx_id>=0:
  ctx = mx.gpu(ctx_id)
else:
  ctx = mx.cpu()
sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)
all_layers = sym.get_internals()
sym = all_layers['heatmap_output']
#model = mx.mod.Module(symbol=sym, context=ctx, data_names=['data'], label_names=['softmax_label'])
model = mx.mod.Module(symbol=sym, context=ctx, data_names=['data'], label_names=None)
#model = mx.mod.Module(symbol=sym, context=ctx)
model.bind(for_training=False, data_shapes=[('data', (1, 3, image_size[0], image_size[1]))])
model.set_params(arg_params, aux_params)

val_iter = FaceSegIter(path_imgrec = rec_path,
  batch_size = 1,
  aug_level = 0,
  )
_metric = NMEMetric()
#val_metric = mx.metric.create(_metric)
#val_metric.reset()
#val_iter.reset()
nme = []
for i, eval_batch in enumerate(val_iter):
  if i%10==0:
    print('processing', i)
  #print(eval_batch.data[0].shape, eval_batch.label[0].shape)
  batch_data = mx.io.DataBatch(eval_batch.data)
  model.forward(batch_data, is_train=False)
  #model.update_metric(val_metric, eval_batch.label, True)
  pred_label = model.get_outputs()[-1].asnumpy()
  label = eval_batch.label[0].asnumpy()
  _nme = _metric.cal_nme(label, pred_label)
  nme.append(_nme)
print(np.mean(nme))

max_stars_repo_name: drawdy/CompreFace

max_issues_repo_name: drawdy/CompreFace

max_forks_repo_name: drawdy/CompreFace


text:
import numpy as np

p1 = np.array([1.689097,33.64849827])
p2 = np.array([-85.3136, 61.37882604])

p3 = np.array([2.814350386,37.04438674])
p4 = np.array([-85.28607818, 65.03660994])

lane_xy = [(p1,p2),(p3,p4)]

#p3 = np.array([34.8143775, -82.33344691])
#p3 = np.array([34.81435821, -82.33352041])
#p3 = np.array([34.81431098, -82.3335742])
p3 = np.array([-4.004671222, 35.52612991])

p3= np.array([-59.98240897, 57.28428551])

d= abs(np.cross(p2-p1,p3-p1)/np.linalg.norm(p2-p1))

#print (d)

def cte(lane_id, p3):
    p_ref = lane_xy[lane_id]
    p1, p2 = p_ref[0], p_ref[1]
    d = abs(np.cross(p2-p1,p3-p1)/np.linalg.norm(p2-p1))
    return d

# point on lane 1
p3 = np.array([-82.9886972, 64.34750368])
print (cte(0,p3))

#point on lane 0
p3= np.array([-78.68226814, 59.37106661])
print (cte(1,p3))


text:
# Импортировали драйвер устройства.
import pycuda.driver as drv
# Определили какой именно GPU в нашей системе доступен для работы
import pycuda.autoinit
# Директива компилятора nVidia (nvcc).
from pycuda.compiler import SourceModule
import numpy

# Сформировали матрицу 5 x 5, значения которой выбираются случайным образом.
a = numpy.random.randn(5, 5)
# GPU поддерживает только одинарную точность.
a = a.astype(numpy.float32)

# Выделили память на устройстве для нашей матрицы.
a_gpu = drv.mem_alloc(a.nbytes)

# Перенесли матрицу на устройство.
drv.memcpy_htod(a_gpu, a)

# Сформировали исполнительный модуль.
mod = SourceModule("""
  __global__ void doubles_matrix(float *a){
    int idx = threadIdx.x + threadIdx.y*4;
    a[idx] *= 2;}
  """)

# Указали исполняемую функцию.
func = mod.get_function("doubles_matrix")

# Запустили функцию ядра.
func(a_gpu, block=(5, 5, 1))

# Выделили область памяти для скопированной матрицы.
a_doubled = numpy.empty_like(a)

# Перенесли новую матрицу с устройства в оперативную память
drv.memcpy_dtoh(a_doubled, a_gpu)

# Отобразили результат.
print("ORIGINAL MATRIX")
print(a)
print("DOUBLED MATRIX AFTER PyCUDA EXECUTION")
print(a_doubled)

max_stars_repo_name: superbe/DataAnalysisAlgorithms

max_issues_repo_name: superbe/DataAnalysisAlgorithms

max_forks_repo_name: superbe/DataAnalysisAlgorithms

reason: import numpy,import pycuda,from pycuda


text:
"""Main window for PySeus.

Classes
-------

**MainWindow** - Class representing the main window for PySeus.
"""

import sys
import webbrowser
from functools import partial
import os
from PySide2 import QtWidgets
from PySide2.QtCore import QLine

from PySide2.QtWidgets import QButtonGroup, QDialog, QDialogButtonBox, QFormLayout, QLineEdit, QMainWindow, QAction, QLabel, QFileDialog, \
                              QFrame, QPushButton, QRadioButton, QVBoxLayout, QHBoxLayout, QWidget
from PySide2.QtGui import QIcon

from .view import ViewWidget
from .sidebar import ConsoleWidget, InfoWidget, MetaWidget
from .thumbs import ThumbsWidget

from pyseus.denoising.tv import TV
import scipy.io
import matplotlib.pyplot as plt

class MainWindow(QMainWindow):  # pylint: disable=R0902
    """Class representing the main window for PySeus."""

    def __init__(self, app):
        QMainWindow.__init__(self)
        self.setWindowTitle("PySEUS")

        self.app = app
        """Reference to the main application object."""

        self.thumbs = ThumbsWidget(app)
        """Reference to the thumbs widget."""

        self.view = ViewWidget(app)
        """Reference to the view widget."""

        self.info = InfoWidget(app)
        """Reference to the info sidebar widget."""

        self.meta = MetaWidget(app)
        """Reference to the meta sidebar widget."""

        self.console = ConsoleWidget(app)
        """Reference to the console sidebar widget."""

        # Default path for file open dialoge
        self._open_path = ""

        # Horizontal layout (thumbs, view, sidebar)
        wrapper = QFrame(self)
        wrapper.setLayout(QHBoxLayout())
        wrapper.layout().setContentsMargins(0, 0, 0, 0)
        wrapper.layout().addWidget(self.thumbs)
        wrapper.layout().addWidget(self.view)

        # Sidebar / Vertical layout (info, meta, console)
        sidebar = QFrame(self)
        sidebar.setLayout(QVBoxLayout())
        sidebar.layout().setContentsMargins(0, 0, 5, 0)

        sidebar.layout().addWidget(SidebarHeading("File Info", True))
        sidebar.layout().addWidget(self.info)

        sidebar.layout().addWidget(SidebarHeading("Metadata"))
        sidebar.layout().addWidget(self.meta)

        sidebar.layout().addWidget(SidebarHeading("Console"))
        sidebar.layout().addWidget(self.console)

        wrapper.layout().addWidget(sidebar)

        self.setup_menu()

        self.statusBar().setSizeGripEnabled(False)

        self.setCentralWidget(wrapper)

        icon = QIcon(os.path.abspath(os.path.join(
            os.path.dirname(__file__), "./icon.png")))
        self.setWindowIcon(icon)

        # Window dimensions
        geometry = self.app.qt_app.desktop().availableGeometry(self)
        self.resize(geometry.width() * 0.6, geometry.height() * 0.6)

    def add_menu_item(self, menu, title, callback, shortcut=""):
        """Create a menu item."""
        action = QAction(title, self)
        if shortcut != "":
            action.setShortcut(shortcut)
        action.triggered.connect(callback)
        menu.addAction(action)
        return action

    def setup_menu(self):
        """Setup the menu bar. Items in the *Evaluate* menu are created
        in the *setup_menu* function of tool classes."""
        ami = self.add_menu_item
        menu_bar = self.menuBar()

        self.file_menu = menu_bar.addMenu("&File")
        ami(self.file_menu, "&Load", self._action_open, "Ctrl+O")
        ami(self.file_menu, "&Reload", self._action_reload, "Ctrl+L")
        self.file_menu.addSeparator()
        ami(self.file_menu, "&Quit", self._action_quit, "Ctrl+Q")

        self.view_menu = menu_bar.addMenu("&View")
        for mode in self.app.modes:
            mode.setup_menu(self.app, self.view_menu, self.add_menu_item)
        self.view_menu.addSeparator()

        ami(self.view_menu, "Zoom &in", self._action_zoom_in, "+")
        ami(self.view_menu, "Zoom &out", self._action_zoom_out, "-")
        ami(self.view_menu, "Zoom to &fit", self._action_zoom_fit, "#")
        ami(self.view_menu, "Reset &Zoom", self._action_zoom_reset, "0")
        self.view_menu.addSeparator()

        ami(self.view_menu, "&Lower Window", self._action_win_lower, "q")
        ami(self.view_menu, "&Raise Window", self._action_win_raise, "w")
        ami(self.view_menu, "&Shrink Window", self._action_win_shrink, "a")
        ami(self.view_menu, "&Enlarge Window", self._action_win_enlarge, "s")
        ami(self.view_menu, "Reset &Window", self._action_win_reset, "d")

        self.explore_menu = menu_bar.addMenu("E&xplore")
        ami(self.explore_menu, "Nex&t Slice",
            partial(self._action_slice, 1), "PgUp")
        ami(self.explore_menu, "P&revious Slice",
            partial(self._action_slice, -1), "PgDown")
        self.explore_menu.addSeparator()

        ami(self.explore_menu, "Rotate z",
            partial(self._action_rotate, 2), "Ctrl+E")
        ami(self.explore_menu, "Rotate x",
            partial(self._action_rotate, 1), "Ctrl+R")
        ami(self.explore_menu, "Rotate y",
            partial(self._action_rotate, 0), "Ctrl+T")
        self.explore_menu.addSeparator()
        ami(self.explore_menu, "Flip x (L-R)",
            partial(self._action_flip, 1), "Ctrl+D")
        ami(self.explore_menu, "Flip y (U-D)",
            partial(self._action_flip, 0), "Ctrl+F")
        ami(self.explore_menu, "Flip z (F-B)",
            partial(self._action_flip, 2), "Ctrl+G")
        self.explore_menu.addSeparator()
        ami(self.explore_menu, "Reset Scan",
            partial(self._action_rotate, -1), "Ctrl+Z")
        self.explore_menu.addSeparator()

        ami(self.explore_menu, "Next &Scan",
            partial(self._action_scan, 1), "Alt+PgUp")
        ami(self.explore_menu, "Previous Sc&an",
            partial(self._action_scan, -1), "Alt+PgDown")
        self.explore_menu.addSeparator()
        ami(self.explore_menu, "Cine Play", self._action_cine, "Ctrl+#")

        self.tools_menu = menu_bar.addMenu("&Evaluate")
        for tool in self.app.tools:
            tool.setup_menu(self.app, self.tools_menu, self.add_menu_item)
        self.tools_menu.addSeparator()
        ami(self.tools_menu, "&Clear RoI", self._action_tool_clear, "Esc")

        self.denoise_menu = menu_bar.addMenu("&Denoise")
        ami(self.denoise_menu, "TV",
            self._open_dialog_tv, "---")
        ami(self.denoise_menu, "TGV",
            print('test'), "---")
        ami(self.denoise_menu, "H1",
            print('test'), "---")

        # About action is its own top level menu
        ami(menu_bar, "&About", self._action_about)

    def show_status(self, message):
        """Display *message* in the status bar."""
        self.statusBar().showMessage(message)

    def resizeEvent(self, event):  # pylint: disable=C0103
        """Keep the viewport centered and adjust zoom on window resize."""
        x_factor = event.size().width() / event.oldSize().width()
        # y_factor = event.size().height() / event.oldSize().height()
        # @TODO x_factor if xf < yf or xf * width * zoom_factor < viewport_x
        self.view.zoom(x_factor, True)

    def _action_quit(self):
        self.app.qt_app.quit()
        sys.exit()

    def _action_open(self):
        path, _ = QFileDialog.getOpenFileName(None, "Open file",
                                              self._open_path, "*.*")

        if not path == "":
            self._open_path = os.path.dirname(path)
            self.app.load_file(path)

    def _action_reload(self):
        if self.app.dataset is not None:
            self.app.load_file(self.app.dataset.path)

    def _action_zoom_in(self):
        self.view.zoom(1.25)

    def _action_zoom_out(self):
        self.view.zoom(0.8)

    def _action_zoom_fit(self):
        self.view.zoom_fit()

    def _action_zoom_reset(self):
        self.view.zoom(1, False)

    def _action_about(self):  # pylint: disable=R0201
        webbrowser.open_new("https://github.com/IMTtugraz/PySeus")

    def _action_win_lower(self):
        self.app.mode.move_window(-20)
        self.app.refresh()

    def _action_win_raise(self):
        self.app.mode.move_window(20)
        self.app.refresh()

    def _action_win_shrink(self):
        self.app.mode.scale_window(-25)
        self.app.refresh()

    def _action_win_enlarge(self):
        self.app.mode.scale_window(25)
        self.app.refresh()

    def _action_win_reset(self):
        self.app.mode.reset_window()
        self.app.refresh()

    def _action_slice(self, step):
        self.app.select_slice(step, True)

    def _action_scan(self, step):
        self.app.select_scan(step, True)

    def _action_tool_clear(self):
        self.app.clear_tool()

    def _action_rotate(self, axis):
        self.app.rotate(axis)

    def _action_flip(self, direction):
        self.app.flip(direction)

    def _action_cine(self):
        self.app.toggle_cine()

    def _open_dialog_tv(self):

        self.alpha = 0
        self.lambd = 0
        self.iter = 0

        dialog = QDialog()
        vlayout = QVBoxLayout()
        hlayout = QHBoxLayout()
        dialog.setWindowTitle("Denoise")

        data_selection = QLabel("Data Selection")
        slice_group = QButtonGroup()
        curr_slice = QRadioButton("Current Slice")
        curr_slice.setChecked(True)
        whole_dataset = QRadioButton("Whole Dataset")
        slice_group.addButton(curr_slice)
        slice_group.addButton(whole_dataset)
        whole_dataset_2D = QRadioButton("2D")
        whole_dataset_3D = QRadioButton("3D")

        # subgroup of radio buttons to dataset selection
        denoise_type = QLabel("Denoising Type")

        tv_group = QButtonGroup()
        tv_L1 = QRadioButton("L1")
        tv_L2 = QRadioButton("L2")
        tv_ROF = QRadioButton("HuberROF")
        tv_group.addButton(tv_L1)
        tv_group.addButton(tv_L2)
        tv_group.addButton(tv_ROF)

        form = QFormLayout()
        self.qline_lambd = QLineEdit()
        #TV_lambda.setStyleSheet("color: white; background-color: darkgray")
        form.addRow("Lambda",self.qline_lambd)
        self.qline_iter = QLineEdit()
        #TV_iter.setStyleSheet("color: white; background-color: darkgray")
        form.addRow("Iterations",self.qline_iter)
        self.qline_alpha = QLineEdit()
        #TV_alpha.setStyleSheet("color: white; background-color: darkgray")
        form.addRow("Alpha",self.qline_alpha)

        btns = QDialogButtonBox()
        btns.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok)
        btns.accepted.connect(self.signal_ok)
        btns.rejected.connect(lambda:dialog.close())

        vlayout.addWidget(data_selection)
        vlayout.addWidget(curr_slice)
        vlayout.addWidget(whole_dataset)
        vlayout.addWidget(denoise_type)
        vlayout.addWidget(tv_L1)
        vlayout.addWidget(tv_L2)
        vlayout.addWidget(tv_ROF)
        vlayout.addWidget(btns)

        hlayout.addLayout(vlayout)
        hlayout.addLayout(form)

        dialog.setLayout(hlayout)
        #dialog.setStyleSheet('color: white')
        dialog.setStyleSheet("QLineEdit"
                                "{"
                                "color: white; background : darkgray;"
                                "}"
                            "QLabel"
                                "{"
                                "color: white;"
                                "}"
                            "QRadioButton"
                                "{"
                                "color: white;"
                                "}"
                            )
        dialog.exec_()

    def signal_ok(self):
        self.alpha = float(self.qline_alpha.text())
        self.lambd = float(self.qline_lambd.text())
        self.iter = int(self.qline_iter.text())

        noisy = scipy.io.loadmat('./tests/cameraman_noise.mat')['im']
        denoise = TV()
        denoised_L2 = denoise.tv_denoising_L2(noisy,self.lambd,self.iter)

        plt.figure(figsize=(16,10))
        plt.subplot(121)
        plt.imshow(noisy, cmap=plt.cm.gray)
        plt.axis('off')
        plt.title('noisy', fontsize=20)
        plt.subplot(122)
        plt.imshow(denoised_L2, cmap=plt.cm.gray)
        plt.axis('off')
        plt.title('denoised', fontsize=20)

        plt.get_current_fig_manager().window.showMaximized()
        plt.show()

    #def signal_cancel(self, dialog_object):

        #dialog_object.close()

# class DialogDenoise(QDialog):

#   def __init__(self, parent = None):
#       super(DialogDenoise, self).__init__(parent)

#       create Widgets, order them in Layout, connect signal to slots

#       initialize all widget with self.

#   def MethodsForSignals...

class SidebarHeading(QLabel):  # pylint: disable=R0903
    """Widget for sidebar separators and headings."""

    def __init__(self, text="", first=False):
        QLabel.__init__(self)
        self.setText(text)
        role = "widget_heading__first" if first else "widget_heading"
        self.setProperty("role", role)
        self.setMinimumHeight(24)
        self.setMaximumHeight(24)

reason: import scipy


text:
"""
Trivial example to test the code
"""

import numpy as np
import matrix_interp
from sklearn.gaussian_process.kernels import *

def test():

    #Create a Simulation Design Points
    a = np.arange(10)
    DESIGN = np.zeros([len(a),2])
    DESIGN[:,0] = a
    np.random.shuffle(a)
    DESIGN[:,1] = a

    #Create positive definite random matrices for each point
    TARGET = [0]*DESIGN.shape[0]
    for i in range(0,len(TARGET)):
        TARGET[i] = np.random.rand(30,30)
        TARGET[i] = TARGET[i]+TARGET[i].T
        TARGET[i] += np.eye(30)*30

    m = matrix_interp.matrix_interp(DESIGN,TARGET,kernel=Matern(),n_restarts=0)

    m.print_info()

    p = np.array([1.3,2.64])

    predicted = m.predict(p)

    print predicted

if __name__=="__main__":
    test()

max_stars_repo_name: jl-bernal/BACCUS

max_issues_repo_name: jl-bernal/BACCUS

max_forks_repo_name: jl-bernal/BACCUS


text:
import numpy as np
import torch

def undo_keypoint_normalisation(normalised_keypoints, img_wh):
    """
    Converts normalised keypoints from [-1, 1] space to pixel space i.e. [0, img_wh]
    """
    keypoints = (normalised_keypoints + 1) * (img_wh/2.0)
    return keypoints

def check_joints2d_visibility(joints2d, img_wh):
    vis = np.ones(joints2d.shape[1])
    vis[joints2d[0] > img_wh] = 0
    vis[joints2d[1] > img_wh] = 0
    vis[joints2d[0] < 0] = 0
    vis[joints2d[1] < 0] = 0

    return vis

def check_joints2d_visibility_torch(joints2d, img_wh):
    """
    Checks if 2D joints are within the image dimensions.
    """
    vis = torch.ones(joints2d.shape[:2], device=joints2d.device, dtype=torch.bool)
    vis[joints2d[:, :, 0] > img_wh] = 0
    vis[joints2d[:, :, 1] > img_wh] = 0
    vis[joints2d[:, :, 0] < 0] = 0
    vis[joints2d[:, :, 1] < 0] = 0

    return vis


text:
userID="Anshul"
from fer import FER
import matplotlib.pyplot as plt
import time
import pymongo
import datetime
import numpy as np
import cv2
import requests

cap = cv2.VideoCapture(0)

while(True):
	ret, frame = cap.read()
	time.sleep(5.5)
	img=frame
	detector = FER(mtcnn=True)
	mydict = detector.detect_emotions(img)[0]

	url = 'https://www.w3schools.com/python/demopage.php'
	x = requests.post(url, data = mydict)
	print(x.text)

	cv2.imshow('frame',frame)
	if cv2.waitKey(1) & 0xFF == ord('q'):
	    break

cap.release()
cv2.destroyAllWindows()

max_stars_repo_name: EphemeralAnarchist/BluePizza

max_issues_repo_name: Akshaya-vc/BluePizza

max_forks_repo_name: Akshaya-vc/BluePizza


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

import tensorflow as tf
from .layer import Layer
import numpy as np

class MeanEncoder(Layer):
    def __init__(self, vocab_size, emb_size, region_radius, trainable=True, name="mean_encoder",
                 initializer=None, **kwargs):
        Layer.__init__(self, name, **kwargs)
        self._vocab_size = vocab_size
        self._emb_size = emb_size
        self._region_radius = region_radius
        self._bias = np.tile(np.array([i for i in range(region_radius*2+1)]),(1,1,1,1))
        self._W = tf.get_variable(name + '_W', shape=[(vocab_size)*(region_radius*2+1), emb_size],
                                  initializer=tf.contrib.layers.xavier_initializer(uniform=True, seed=None, dtype=tf.float32),
                                  trainable=trainable)

    def _forward(self, seq):
        paddings = tf.constant([[0, 0], [self._region_radius, self._region_radius], [0, 0]])
        region_size = 2*self._region_radius + 1
        padded_seq = tf.pad(seq, paddings, "CONSTANT")
        s = tf.shape(seq)
        r = tf.range(region_size)
        seq = tf.Print(seq, [seq])
        align_emb = tf.map_fn(lambda i: padded_seq[:,i:i+s[1],:], r)
        align_emb = tf.Print(align_emb, [align_emb])
        align_emb = tf.transpose(align_emb, perm=[1, 2, 3, 0])*region_size + self._bias
        mask = tf.expand_dims(tf.cast(tf.not_equal(seq, 0), dtype=tf.float32), axis=-1)
#        W = tf.concat((tf.zeros(shape=[region_size, self._emb_size]), self._W), 0)
        # [batch_size, max_len, region_radius, emb_size]
        align_emb = tf.nn.embedding_lookup(self._W, align_emb)
        trigram_emb = tf.reduce_sum(align_emb,axis=-2)
        h = tf.tanh(trigram_emb) * mask
        return h

class RegionEncoder(Layer):
    def __init__(self, vocab_size, emb_size, region_radius, trainable=True, name="region_encoder",
                 initializer=None, **kwargs):
        Layer.__init__(self, name, **kwargs)
        self._vocab_size = vocab_size
        self._emb_size = emb_size
        self._region_radius = region_radius
        self._paddings = tf.constant([[0, 0], [self._region_radius, self._region_radius, [0, 0]]])
        self._bias = np.tile(np.array([i for i in range(region_radius*2+1)]),(1,1,1,1))
        self._W = tf.get_variable(name + '_W', shape=[vocab_size, emb_size],
                                  initializer=tf.contrib.layers.xavier_initializer(uniform=True, seed=None, dtype=tf.float32),
                                  trainable=trainable)
        # Context matrix
        self._U = tf.get_variable(name + '_U',
                     shape=[(vocab_size)*(region_radius*2+1), emb_size],
                     dtype=tf.float32,
                     trainable=trainable)

    def _forward(self, seq):
        region_size = 2*self._region_radius + 1
        padded_seq = tf.pad(seq, self._paddings, "CONSTANT")
        s = tf.shape(seq)
        r = tf.range(region_size)
        align_emb = tf.map_fn(lambda i: padded_seq[:,i:i+s[1],:], r)
        align_emb = tf.transpose(align_emb, perm=[1, 2, 3, 0])*region_size + self._bias
        # Word Embedding
#        W = tf.concat((tf.zeros(shape=[1, self._emb_size]), self._W), 0)
        trigram_emb = tf.nn.embedding_lookup(self._W, seq)
        # Context-Word Embedding
#        U = tf.concat((tf.zeros(shape=[region_size, self._emb_size]), self._U), 0)
        align_emb = tf.nn.embedding_lookup(self._U, align_emb)
        trigram_emb = tf.expand_dims(trigram_emb, -2)
        # [batch_size, max_len, region_radius, emb_size]
        projected_emb = align_emb * trigram_emb
        mask = tf.expand_dims(tf.cast(tf.not_equal(seq, 0), dtype=tf.float32), axis=-1)
        h = tf.reduce_max(projected_emb, axis=-2) * mask
        return h

class CNNEncoder(Layer):
    def __init__(self, vocab_size, emb_size, groups, filters, kernel_size,
                 trainable=True, name="cnn_encoder", initializer=None, emb=None, **kwargs):
        Layer.__init__(self, name, **kwargs)
        if emb is None:
            self._W = tf.get_variable(name + '_W', shape=[vocab_size, emb_size],
                                      initializer=tf.contrib.layers.xavier_initializer(uniform=True, seed=None, dtype=tf.float32),
                                      trainable=trainable)
        self._emb = emb
        self._emb_size = emb_size
        self._groups = groups
        self._filters = filters
        self._kernel_size = kernel_size
        self._name = name
        self._conv_layers = []
        for i in range(self._groups):
            self._conv_layers.append(tf.layers.Conv1D(kernel_size=self._kernel_size[i],
                                      filters=self._filters[i],
                                      strides=1,
                                      padding='same',
                                      activation=tf.tanh,
                                      use_bias=False,
                                      kernel_initializer=tf.contrib.layers.xavier_initializer(uniform=True, seed=None, dtype=tf.float32),
                                      name=self._name+'_'+str(i)))

    def _forward(self, seq):
        if self._emb is None:
            mask = tf.expand_dims(tf.cast(tf.not_equal(seq, 0), dtype=tf.float32), axis=-1)
#            W = tf.concat((tf.zeros(shape=[1, self._emb_size]), self._W), 0)
            # [batch_size, max_len, emb_size]
            char_emb = tf.nn.embedding_lookup(self._W, seq) * mask
        else:
            char_emb = self._emb(seq, zero_forward=True)
        h = []
        for i in range(self._groups):
            # [batch_size, max_len, filters[i]]
            h.append(self._conv_layers[i](char_emb))
        h = tf.concat(h, axis=-1)

        return h

def main():
    """main"""
    pass

if '__main__' == __name__:
    main()


text:
""" Some functions to help with visualizations """
# imports
import numpy as np
import ipywidgets as ipyw
from matplotlib import pyplot as plt

class ImageSliceViewer3D:
    """
    https://github.com/mohakpatel/ImageSliceViewer3D/blob/master/ImageSliceViewer3D.ipynb
    """

    def __init__(self, volume, figsize=(8, 8), cmap='gray'):
        self.volume = volume
        self.figsize = figsize
        self.cmap = cmap
        self.v = [np.min(volume), np.max(volume)]

        # Call to select slice plane
        ipyw.interact(self.view_selection, view=ipyw.RadioButtons(
            options=['x-y', 'y-z', 'z-x'], value='x-y',
            description='Slice plane selection:', disabled=False,
            style={'description_width': 'initial'}))

    def view_selection(self, view):
        # Transpose the volume to orient according to the slice plane selection
        orient = {"y-z": [1, 2, 0], "z-x": [2, 0, 1], "x-y": [0, 1, 2]}
        self.vol = np.transpose(self.volume, orient[view])
        maxZ = self.vol.shape[2] - 1

        # Call to view a slice within the selected slice plane
        ipyw.interact(self.plot_slice,
                      z=ipyw.IntSlider(min=0, max=maxZ, step=1, continuous_update=False,
                                       description='Image Slice:'))

    def plot_slice(self, z):
        # Plot slice for the given plane and slice
        self.fig = plt.figure(figsize=self.figsize)
        plt.imshow(self.vol[:, :, z], cmap=plt.get_cmap(self.cmap),
                   vmin=self.v[0], vmax=self.v[1])

max_stars_repo_name: awerdich/werdich-cfr

max_issues_repo_name: awerdich/werdich-cfr

max_forks_repo_name: awerdich/werdich-cfr


text:
# Copyright (c) 2016 by Mike Jarvis and the other collaborators on GitHub at
# https://github.com/rmjarvis/Piff  All rights reserved.
#
# Piff is free software: Redistribution and use in source and binary forms
# with or without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the disclaimer given in the accompanying LICENSE
#    file.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the disclaimer given in the documentation
#    and/or other materials provided with the distribution.

from __future__ import print_function
import numpy as np
import piff
import os
import galsim
import fitsio
import yaml
import subprocess

from piff_test_helper import get_script_name, timer

@timer
def test_twodstats():
    """Make sure we can execute and print a readout of the plot
    """
    if __name__ == '__main__':
        logger = piff.config.setup_logger(2)
    else:
        logger = None

    model = piff.Gaussian(fastfit=True)
    interp = piff.Polynomial(order=1)  # should find that order=1 is better
    # create background model
    stars, true_model = generate_starlist(100)
    psf = piff.SimplePSF(model, interp)
    psf.fit(stars, None, None)

    # check the coeffs of sigma and g2, which are actually linear fits
    # skip g1 since it is actually a 2d parabola
    # factor of 0.263 is to account for going from pixel xy to wcs uv
    np.testing.assert_almost_equal(psf.interp.coeffs[0].flatten(),
                                   np.array([0.4, 0, 1. / (0.263 * 2048), 0]), decimal=4)
    np.testing.assert_almost_equal(psf.interp.coeffs[2].flatten(),
                                   np.array([-0.1 * 1000 / 2048, 0, 0.1 / (0.263 * 2048), 0]),
                                   decimal=4)

    stats = piff.TwoDHistStats(number_bins_u=5, number_bins_v=5, reducing_function='np.mean')
    stats.compute(psf, stars, logger=logger)
    # check the twodhists
    # get the average value in the bin
    u_i = 3
    v_i = 3
    icen = stats.twodhists['u'][v_i, u_i] / 0.263
    jcen = stats.twodhists['v'][v_i, u_i] / 0.263
    print('icen = ',icen)
    print('jcen = ',jcen)
    icenter = 1000
    jcenter = 2000
    # the average value in the bin should match up with the model for the average coordinates
    sigma, g1, g2 = psf_model(icen, jcen, icenter, jcenter)
    sigma_average = stats.twodhists['T'][v_i, u_i]
    g1_average = stats.twodhists['g1'][v_i, u_i]
    g2_average = stats.twodhists['g2'][v_i, u_i]
    # assert equal to 4th decimal
    print('sigma, g1, g2 = ',[sigma,g1,g2])
    print('av sigma, g1, g2 = ',[sigma_average,g1_average,g2_average])
    np.testing.assert_almost_equal([sigma, g1, g2], [sigma_average, g1_average, g2_average],
                                   decimal=2)

    # Test the plotting and writing
    twodstats_file = os.path.join('output','twodstats.pdf')
    stats.write(twodstats_file)

    # repeat for whisker
    stats = piff.WhiskerStats(number_bins_u=21, number_bins_v=21, reducing_function='np.mean')
    stats.compute(psf, stars)
    # Test the plotting and writing
    twodstats_file = os.path.join('output','whiskerstats.pdf')
    stats.write(twodstats_file)

@timer
def test_shift_cmap():
    from matplotlib import cm

    # test vmax and vmin center issues
    vmin = -1
    vmax = 8
    center = 2

    # color map vmin > center
    cmap = piff.TwoDHistStats._shift_cmap(vmin, vmax, vmin - 1)
    assert cmap == cm.Reds

    # color map vmax < center
    cmap = piff.TwoDHistStats._shift_cmap(vmin, vmax, vmax + 1)
    assert cmap == cm.Blues_r

    # test without center
    cmap = piff.TwoDHistStats._shift_cmap(vmin, vmax)
    midpoint = (0 - vmin) * 1. / (vmax - vmin)
    unshifted_cmap = cm.RdBu_r
    # check segment data
    # NOTE: that because of interpolation cmap(midpont) does not have to equal
    # unshifted_cmap(0.5)
    for val, color in zip(unshifted_cmap(0.5), ['red', 'green', 'blue', 'alpha']):
        assert midpoint == cmap._segmentdata[color][128][0]
        assert val == cmap._segmentdata[color][128][1]
        assert val == cmap._segmentdata[color][128][2]
    # but edge values are the same
    assert cmap(0.) == unshifted_cmap(0.)
    assert cmap(1.) == unshifted_cmap(1.)

    # test with center
    cmap = piff.TwoDHistStats._shift_cmap(vmin, vmax, center)
    midpoint = (center - vmin) * 1. / (vmax - vmin)
    unshifted_cmap = cm.RdBu_r
    for val, color in zip(unshifted_cmap(0.5), ['red', 'green', 'blue', 'alpha']):
        assert midpoint == cmap._segmentdata[color][128][0]
        assert val == cmap._segmentdata[color][128][1]
        assert val == cmap._segmentdata[color][128][2]
    assert cmap(0.) == unshifted_cmap(0.)
    assert cmap(1.) == unshifted_cmap(1.)

    # what if vmax < vmin?
    cmap = piff.TwoDHistStats._shift_cmap(vmax, vmin, center)
    midpoint = 1. - (center - vmax) * 1. / (vmin - vmax)
    unshifted_cmap = cm.RdBu_r
    for val, color in zip(unshifted_cmap(0.5), ['red', 'green', 'blue', 'alpha']):
        assert midpoint == cmap._segmentdata[color][128][0]
        assert val == cmap._segmentdata[color][128][1]
        assert val == cmap._segmentdata[color][128][2]
    assert cmap(0.) == unshifted_cmap(1.)
    assert cmap(1.) == unshifted_cmap(0.)

def make_star(icen=500, jcen=700, ccdnum=28,
              sigma=1, g1=0, g2=0,
              pixel_to_focal=False,
              properties={},
              fit_kwargs={}):

    properties['ccdnum'] = ccdnum
    # setting scale is crucial
    stardata = piff.Star.makeTarget(x=icen, y=jcen, properties=properties,
                                    scale=0.263)
    # apply Gaussian sigma, g1, g2
    params = np.array([sigma, g1, g2])

    starfit = piff.StarFit(params, **fit_kwargs)

    star = piff.Star(stardata.data, starfit)

    return star

def psf_model(icens, jcens, icenter, jcenter):
    sigmas = icens * (2. - 1.) / 2048. + 0.4
    g1s = ((jcens - jcenter) / 4096.) ** 2 * -0.2
    g2s = (icens - icenter) * 0.1 / 2048.
    return sigmas, g1s, g2s

def generate_starlist(n_samples=500):
    # create n_samples images from the 63 ccds and pixel coordinates
    np_rng = np.random.RandomState(1234)
    icens = np_rng.randint(100, 2048, n_samples)
    jcens = np_rng.randint(100, 4096, n_samples)
    ccdnums = np_rng.randint(1, 63, n_samples)
    icenter = 1000
    jcenter = 2000

    # throw out any icens and jcens that are within 400 pixels of the center
    conds = (np.abs(icens - icenter) > 400) | (np.abs(jcens - jcenter) > 400)
    icens = icens[conds]
    jcens = jcens[conds]
    ccdnums = ccdnums[conds]

    sigmas, g1s, g2s = psf_model(icens, jcens, icenter, jcenter)

    # throw in a 2d polynomial function for sigma g1 and g2
    # all sigma > 0, all g1 < 0, and g2 straddles.

    star_list = [make_star(icen, jcen, ccdnum, sigma, g1, g2)
                 for icen, jcen, ccdnum, sigma, g1, g2
                 in zip(icens, jcens, ccdnums, sigmas, g1s, g2s)]

    # load up model and draw the stars
    model = piff.Gaussian(fastfit=True)
    star_list = [model.draw(star) for star in star_list]
    star_list = [model.initialize(star) for star in star_list]
    star_list = [model.fit(star) for star in star_list]

    return star_list, model

@timer
def setup():
    """Build an input image and catalog used by a few tests below.
    """

    # Make the image (copied from test_single_image in test_simple.py)
    image = galsim.Image(2048, 2048, scale=0.26)

    # Where to put the stars.
    x_list = [ 123.12, 345.98, 567.25, 1094.94, 924.15, 1532.74, 1743.11, 888.39, 1033.29, 1409.31 ]
    y_list = [ 345.43, 567.45, 1094.32, 924.29, 1532.92, 1743.83, 888.83, 1033.19, 1409.20, 123.11 ]

    # Draw a Gaussian PSF at each location on the image.
    sigma = 1.3
    g1 = 0.23
    g2 = -0.17
    dx = 0.31  # in pixels
    dy = -0.32
    flux = 123.45
    psf = galsim.Gaussian(sigma=sigma).shear(g1=g1, g2=g2) * flux
    for x, y in zip(x_list, y_list):
        bounds = galsim.BoundsI(int(x-31), int(x+32), int(y-31), int(y+32))
        offset = galsim.PositionD( x-int(x)-0.5 + dx, y-int(y)-0.5 + dy)
        psf.drawImage(image=image[bounds], method='no_pixel', offset=offset)
    image.addNoise(galsim.GaussianNoise(rng=galsim.BaseDeviate(1234), sigma=1e-6))

    # Write out the image to a file
    image_file = os.path.join('output','test_stats_image.fits')
    image.write(image_file)

    # Write out the catalog to a file
    dtype = [ ('x','f8'), ('y','f8') ]
    data = np.empty(len(x_list), dtype=dtype)
    data['x'] = x_list
    data['y'] = y_list
    cat_file = os.path.join('output','test_stats_cat.fits')
    fitsio.write(cat_file, data, clobber=True)

@timer
def test_twodstats_config():
    """Test running stats through a config file.
    """
    if __name__ == '__main__':
        logger = piff.config.setup_logger(verbose=2)
    else:
        logger = piff.config.setup_logger(log_file='output/test_twodstats_config.log')

    image_file = os.path.join('output','test_stats_image.fits')
    cat_file = os.path.join('output','test_stats_cat.fits')
    psf_file = os.path.join('output','test_twodstats.fits')
    twodhist_file = os.path.join('output','test_twodhiststats.pdf')
    twodhist_std_file = os.path.join('output','test_twodhiststats_std.pdf')
    config = {
        'input' : {
            'image_file_name' : image_file,
            'cat_file_name' : cat_file,
            'stamp_size' : 48
        },
        'psf' : {
            'model' : { 'type' : 'Gaussian',
                        'fastfit': True,
                        'include_pixel': False },
            'interp' : { 'type' : 'Mean' },
        },
        'output' : {
            'file_name' : psf_file,
            'stats' : [
                {
                    'type': 'TwoDHist',
                    'file_name': twodhist_file,
                    'number_bins_u': 3,
                    'number_bins_v': 3,
                },
                {
                    'type': 'TwoDHist',
                    'file_name': twodhist_std_file,
                    'reducing_function': 'np.std',
                    'number_bins_u': 3,
                    'number_bins_v': 3,
                },
            ]
        }
    }
    piff.piffify(config, logger)
    assert os.path.isfile(twodhist_file)
    assert os.path.isfile(twodhist_std_file)

    # repeat with plotify function
    os.remove(twodhist_file)
    os.remove(twodhist_std_file)
    piff.plotify(config, logger)
    assert os.path.isfile(twodhist_file)
    assert os.path.isfile(twodhist_std_file)

@timer
def test_rhostats_config():
    """Test running stats through a config file.
    """
    if __name__ == '__main__':
        logger = piff.config.setup_logger(verbose=2)
    else:
        logger = piff.config.setup_logger(log_file='output/test_rhostats_config.log')

    image_file = os.path.join('output','test_stats_image.fits')
    cat_file = os.path.join('output','test_stats_cat.fits')
    psf_file = os.path.join('output','test_rhostats.fits')
    rho_file = os.path.join('output','test_rhostats.pdf')
    config = {
        'input' : {
            'image_file_name' : image_file,
            'cat_file_name' : cat_file,
            'stamp_size' : 48
        },
        'psf' : {
            'model' : { 'type' : 'Gaussian',
                        'fastfit': True,
                        'include_pixel': False },
            'interp' : { 'type' : 'Mean' },
        },
        'output' : {
            'file_name' : psf_file,
            'stats' : {  # Note: stats doesn't have to be a list.
                'type': 'Rho',
                'file_name': rho_file
            }
        },
    }
    piff.piffify(config, logger)
    assert os.path.isfile(rho_file)

    # repeat with plotify function
    os.remove(rho_file)
    piff.plotify(config, logger)
    assert os.path.isfile(rho_file)

    # Test rho statistics directly.
    min_sep = 1
    max_sep = 100
    bin_size = 0.1
    psf = piff.read(psf_file)
    orig_stars, wcs, pointing = piff.Input.process(config['input'], logger)
    stats = piff.RhoStats(min_sep=min_sep, max_sep=max_sep, bin_size=bin_size)
    stats.compute(psf, orig_stars)

    rhos = [stats.rho1, stats.rho2, stats.rho3, stats.rho4, stats.rho5]
    for rho in rhos:
        # Test the range of separations
        radius = np.exp(rho.logr)
        # last bin can be one bigger than max_sep
        np.testing.assert_array_less(radius, np.exp(np.log(max_sep) + bin_size))
        np.testing.assert_array_less(min_sep, radius)
        np.testing.assert_array_almost_equal(np.diff(rho.logr), bin_size, decimal=5)

        # Test that the max absolute value of each rho isn't crazy
        np.testing.assert_array_less(np.abs(rho.xip), 1)

        # # Check that each rho isn't precisely zero. This means the sum of abs > 0
        np.testing.assert_array_less(0, np.sum(np.abs(rho.xip)))

    # Test using the piffify executable
    os.remove(rho_file)
    config['verbose'] = 0
    with open('rho.yaml','w') as f:
        f.write(yaml.dump(config, default_flow_style=False))
    piffify_exe = get_script_name('piffify')
    p = subprocess.Popen( [piffify_exe, 'rho.yaml'] )
    p.communicate()
    assert os.path.isfile(rho_file)

    # Test using the plotify executable
    os.remove(rho_file)
    plotify_exe = get_script_name('plotify')
    p = subprocess.Popen( [plotify_exe, 'rho.yaml'] )
    p.communicate()
    assert os.path.isfile(rho_file)

    # test running plotify with dir in config, with no logger, and with a modules specification.
    # (all to improve test coverage)
    config['output']['dir'] = '.'
    config['modules'] = [ 'custom_wcs' ]
    os.remove(rho_file)
    piff.plotify(config)
    assert os.path.isfile(rho_file)

@timer
def test_shapestats_config():
    """Test running stats through a config file.
    """
    if __name__ == '__main__':
        logger = piff.config.setup_logger(verbose=2)
    else:
        logger = piff.config.setup_logger(log_file='output/test_shapestats_config.log')

    image_file = os.path.join('output','test_stats_image.fits')
    cat_file = os.path.join('output','test_stats_cat.fits')
    psf_file = os.path.join('output','test_shapestats.fits')
    shape_file = os.path.join('output','test_shapestats.pdf')
    config = {
        'input' : {
            'image_file_name' : image_file,
            'cat_file_name' : cat_file,
            'stamp_size' : 48
        },
        'psf' : {
            'model' : { 'type' : 'Gaussian',
                        'fastfit': True,
                        'include_pixel': False },
            'interp' : { 'type' : 'Mean' },
        },
        'output' : {
            'file_name' : psf_file,
            'stats' : [
                {
                    'type': 'ShapeHistograms',
                    'file_name': shape_file
                },
            ]
        },
    }
    piff.piffify(config, logger)
    assert os.path.isfile(shape_file)

    # repeat with plotify function
    os.remove(shape_file)
    piff.plotify(config, logger)
    assert os.path.isfile(shape_file)

    # Test ShapeHistogramStats directly
    psf = piff.read(psf_file)
    shapeStats = piff.ShapeHistogramsStats()
    orig_stars, wcs, pointing = piff.Input.process(config['input'], logger)
    shapeStats.compute(psf, orig_stars)

    # test their characteristics
    sigma = 1.3  # (copied from setup())
    g1 = 0.23
    g2 = -0.17
    np.testing.assert_array_almost_equal(sigma, shapeStats.T, decimal=4)
    np.testing.assert_array_almost_equal(sigma, shapeStats.T_model, decimal=3)
    np.testing.assert_array_almost_equal(g1, shapeStats.g1, decimal=4)
    np.testing.assert_array_almost_equal(g1, shapeStats.g1_model, decimal=3)
    np.testing.assert_array_almost_equal(g2, shapeStats.g2, decimal=4)
    np.testing.assert_array_almost_equal(g2, shapeStats.g2_model, decimal=3)

@timer
def test_starstats_config():
    """Test running stats through a config file.
    """
    if __name__ == '__main__':
        logger = piff.config.setup_logger(verbose=2)
    else:
        logger = piff.config.setup_logger(log_file='output/test_starstats_config.log')

    image_file = os.path.join('output','test_stats_image.fits')
    cat_file = os.path.join('output','test_stats_cat.fits')
    psf_file = os.path.join('output','test_starstats.fits')
    star_file = os.path.join('output', 'test_starstats.pdf')
    star_noadjust_file = os.path.join('output', 'test_starstats_noadjust.pdf')
    config = {
        'input' : {
            'image_file_name' : image_file,
            'cat_file_name' : cat_file,
            'stamp_size' : 48
        },
        'psf' : {
            'model' : { 'type' : 'Gaussian',
                        'fastfit': True,
                        'include_pixel': False },
            'interp' : { 'type' : 'Mean' },
        },
        'output' : {
            'file_name' : psf_file,
            'stats' : [
                {
                    'type': 'Star',
                    'file_name': star_file,
                    'number_plot': 5,
                    'adjust_stars': True,
                }
            ]
        }
    }
    piff.piffify(config, logger)
    assert os.path.isfile(star_file)

    # repeat with plotify function
    os.remove(star_file)
    piff.plotify(config, logger)
    assert os.path.isfile(star_file)

    # check default number_plot
    psf = piff.read(psf_file)
    starStats = piff.StarStats()
    orig_stars, wcs, pointing = piff.Input.process(config['input'], logger)
    starStats.compute(psf, orig_stars)
    assert starStats.number_plot == len(starStats.stars)
    assert starStats.number_plot == len(starStats.models)
    assert starStats.number_plot == len(starStats.indices)
    np.testing.assert_array_equal(starStats.stars[2].image.array,
                                  orig_stars[starStats.indices[2]].image.array)

    # check number_plot = 6
    starStats = piff.StarStats(number_plot=6)
    starStats.compute(psf, orig_stars)
    assert len(starStats.stars) == 6

    # check number_plot >> len(stars)
    starStats = piff.StarStats(number_plot=1000000)
    starStats.compute(psf, orig_stars)
    assert len(starStats.stars) == len(orig_stars)
    # if use all stars, no randomness
    np.testing.assert_array_equal(starStats.stars[3].image.array, orig_stars[3].image.array)
    np.testing.assert_array_equal(starStats.indices, np.arange(len(orig_stars)))

    # check number_plot = 0
    starStats = piff.StarStats(number_plot=0)
    starStats.compute(psf, orig_stars)
    assert len(starStats.stars) == len(orig_stars)
    # if use all stars, no randomness
    np.testing.assert_array_equal(starStats.stars[3].image.array, orig_stars[3].image.array)
    np.testing.assert_array_equal(starStats.indices, np.arange(len(orig_stars)))

    # rerun with adjust stars and see if it did the right thing
    # first with starstats == False
    starStats = piff.StarStats(number_plot=0, adjust_stars=False)
    starStats.compute(psf, orig_stars, logger=logger)
    fluxs_noadjust = np.array([s.fit.flux for s in starStats.stars])
    ds_noadjust = np.array([s.fit.center for s in starStats.stars])
    # check that fluxes 1
    np.testing.assert_array_equal(fluxs_noadjust, 1)
    # check that ds are 0
    np.testing.assert_array_equal(ds_noadjust, 0)

    # now with starstats == True
    starStats = piff.StarStats(number_plot=0, adjust_stars=True)
    starStats.compute(psf, orig_stars, logger=logger)
    fluxs_adjust = np.array([s.fit.flux for s in starStats.stars])
    ds_adjust = np.array([s.fit.center for s in starStats.stars])
    # copy the right values from setup()
    dx = 0.31
    dy = -0.32
    flux = 123.45
    # compare fluxes
    np.testing.assert_allclose(fluxs_adjust, flux, rtol=1e-4)
    # compare dx and dy, keeping in mind that ds_adjust is dx/y * 0.26 (scale)
    dx_adjust = ds_adjust[:, 0] / 0.26
    dy_adjust = ds_adjust[:, 1] / 0.26
    np.testing.assert_allclose(dx_adjust, dx, rtol=1e-4)
    np.testing.assert_allclose(dy_adjust, dy, rtol=1e-4)

    # do once with adjust_stars = False to graphically demonstrate
    config['output']['stats'][0]['file_name'] = star_noadjust_file
    config['output']['stats'][0]['adjust_stars'] = False
    piff.plotify(config, logger)
    assert os.path.isfile(star_noadjust_file)

if __name__ == '__main__':
    setup()
    test_twodstats()
    test_shift_cmap()
    test_twodstats_config()
    test_rhostats_config()
    test_shapestats_config()
    test_starstats_config()

max_stars_repo_name: lsst-dm/piff

max_issues_repo_name: lsst-dm/piff

max_forks_repo_name: lsst-dm/piff


text:
#!/usr/bin/env python

import math
import rospy
import geometry_msgs
import tf_conversions
from crazyflie_driver.msg import Position
from crazyflie_driver.msg import FullState
from std_msgs.msg import Empty
from geometry_msgs.msg import PoseStamped, Twist
from crazyflie_driver.msg import VelocityWorld, FullState
from crazyflie_init import crazyflie_init
from crazyflie_driver.srv import UpdateParams
from drone_model import drone_model_t
from drone_control import drone_controller_t
import numpy as np

#noramalize vector
def normal(ax, ay, bx, by):
    result = math.sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by))
    if result < 0.001:
        return 1
    else:
        return result

#distance between two agents
def dist(ax, ay, bx, by):
    dist_square = (ax - bx) * (ax - bx) + (ay - by) * (ay - by)
    return math.sqrt(dist_square)

#takeoff
def takeoff(rospy, posi_pub, posi_msg, rate, x_init, y_init, height, frequency):
    while not rospy.is_shutdown():
        for i in range(int(0.3 * frequency)):
            posi_msg.x = x_init
            posi_msg.y = y_init
            posi_msg.yaw = 0.0
            posi_msg.z = i * 0.3 / (0.3 * frequency)
            posi_msg.header.seq += 1
            posi_msg.header.stamp = rospy.Time.now()
            posi_pub.publish(posi_msg)
            rate.sleep()
        for i in range(int(2 * frequency)):
            posi_msg.x = x_init
            posi_msg.y = y_init
            posi_msg.yaw = 0.0
            posi_msg.z = 0.3 + (height - 0.3) * i / (2 * frequency)
            posi_msg.header.seq += 1
            posi_msg.header.stamp = rospy.Time.now()
            posi_pub.publish(posi_msg)
            rate.sleep()
        break

#hover
def hover(rospy, posi_pub, posi_msg, rate, x_init, y_init, height, time):
    while not rospy.is_shutdown():
        for i in range(int(time)):
            posi_msg.x = x_init
            posi_msg.y = y_init
            posi_msg.yaw = 0.0
            posi_msg.z = height
            posi_msg.header.seq += 1
            posi_msg.header.stamp = rospy.Time.now()
            posi_pub.publish(posi_msg)
            rate.sleep()
        break

#land
def land(rospy, posi_pub, posi_msg, rate, x_end, y_end, z_end, frequency):
    while not rospy.is_shutdown():
        for i in range(int(2 * frequency)):
            posi_msg.x = x_end
            posi_msg.y = y_end
            posi_msg.yaw = 0.0
            posi_msg.z = z_end * (2 * frequency - i) / (2 * frequency)
            posi_msg.header.seq += 1
            posi_msg.header.stamp = rospy.Time.now()
            posi_pub.publish(posi_msg)
            rate.sleep()
        break

#position callback
def agent1_cb(pose):
    global x1
    global y1
    global z1
    #update the position of agent1
    x1 = pose.pose.position.x
    y1 = pose.pose.position.y
    z1 = pose.pose.position.z

def agent2_cb(pose):
    global x2
    global y2
    global z2
    #update the position of agent1
    x2 = pose.pose.position.x
    y2 = pose.pose.position.y
    z2 = pose.pose.position.z

def agent3_cb(pose):
    global x3
    global y3
    global z3
    #update the position of agent1
    x3 = pose.pose.position.x
    y3 = pose.pose.position.y
    z3 = pose.pose.position.z

def updatePIDparmeters(rospy):
    rospy.wait_for_service('update_params')
    rospy.loginfo("found update_params service")
    update_params = rospy.ServiceProxy('update_params', UpdateParams)
    xki = rospy.get_param("~xki", 0)
    yki = rospy.get_param("~yki", 0)
    rospy.set_param("posCtlPid/xKi", xki)
    rospy.set_param("posCtlPid/yKi", yki)
    update_params(["posCtlPid/xKi", "posCtlPid/yKi"])
    vxki = rospy.get_param("~vxki", 3)
    vyki = rospy.get_param("~vyki", 3)
    vxkp = rospy.get_param("~vxkp", 25)
    vykp = rospy.get_param("~vykp", 25)
    vxkd = rospy.get_param("~vxkd", 1)
    vykd = rospy.get_param("~vykd", 1)
    rospy.set_param("velCtlPid/vxKi", vxki)
    rospy.set_param("velCtlPid/vyKi", vyki)
    rospy.set_param("velCtlPid/vxKp", vxkp)
    rospy.set_param("velCtlPid/vyKp", vykp)
    rospy.set_param("velCtlPid/vxKd", vxkd)
    rospy.set_param("velCtlPid/vyKd", vykd)
    update_params(["velCtlPid/vxKi", "velCtlPid/vyKi", "velCtlPid/vxKp", "velCtlPid/vyKp", "velCtlPid/vxKd", "velCtlPid/vyKd"])

#initialize global variable
count_dist = 0
def distanceTopic(rospy, dist_pub, x1, y1, x2, y2, x3, y3, dp1x, dp1y, dp2x, dp2y, dp3x, dp3y):
    dist12 = dist(x1, y1, x2, y2)
    dist13 = dist(x1, y1, x3, y3)
    dist23 = dist(x2, y2, x3, y3)
    dist_des12 = dist(dp1x, dp1y, dp2x, dp2y)
    dist_des13 = dist(dp1x, dp1y, dp3x, dp3y)
    dist_des23 = dist(dp2x, dp2y, dp3x, dp3y)

    global count_dist
    dist_msg = PoseStamped()
    count_dist += 1
    dist_msg.header.seq = count_dist
    dist_msg.header.frame_id = "/world"
    dist_msg.header.stamp = rospy.Time.now()
    dist_msg.pose.position.x = dist12
    dist_msg.pose.position.y = dist13
    dist_msg.pose.position.z = dist23
    dist_msg.pose.orientation.x = dist_des12
    dist_msg.pose.orientation.y = dist_des13
    dist_msg.pose.orientation.z = dist_des23
    dist_pub.publish(dist_msg)

count_full_state = 0
def publish_full_state(rospy, full_state_pub, worldFrame, pos_cmd, vel_cmd):
    global count_full_state
    count_full_state += 1
    msg = FullState()
    msg.header.seq = count_full_state
    msg.header.stamp = rospy.Time.now()
    msg.header.frame_id = worldFrame
    msg.pose.position.x    = pos_cmd[0]
    msg.pose.position.y    = pos_cmd[1]
    msg.pose.position.z    = pos_cmd[2]
    msg.twist.linear.x     = vel_cmd[0]
    msg.twist.linear.y     = vel_cmd[1]
    msg.twist.linear.z     = vel_cmd[2]
    msg.acc.x              = 0.0
    msg.acc.y              = 0.0
    msg.acc.z              = 0.0
    msg.pose.orientation   = geometry_msgs.msg.Quaternion(*tf_conversions.transformations.quaternion_from_euler(0, 0, 0))
    msg.twist.angular.x    = 0.0
    msg.twist.angular.y    = 0.0
    msg.twist.angular.z    = 0.0
    full_state_pub.publish(msg)

#electrical fence
def now_in_elctricial_fence(x1, y1, x2, y2, x3, y3):
    a1_state = x1 < 1.5 and x1 > -1.5 and y1 < 1.5 and y1 > -1
    a2_state = x2 < 1.5 and x2 > -1.5 and y2 < 1.5 and y2 > -1
    a3_state = x3 < 1.5 and x3 > -1.5 and y3 < 1.5 and y3 > -1
    if (a1_state and a2_state and a3_state):
        return True

if __name__ == '__main__':
    print 'start'
    rospy.init_node('position', anonymous=True)

    if not crazyflie_init(rospy):
        rospy.logwarn(rospy.get_name()+" init failed !")
        exit(1)
    worldFrame = rospy.get_param("~worldFrame", "/world")
    #initialize position of agent
    x1 = 0
    y1 = 0
    z1 = 0
    x2 = 0
    y2 = 0
    z2 = 0
    x3 = 0
    y3 = 0
    z3 = 0
    #initial value
    topic = rospy.get_param("~topic", "/crazyflie1/external_pose")
    pose = rospy.wait_for_message(topic, PoseStamped)
    x_init = pose.pose.position.x
    y_init = pose.pose.position.y
    z_init = pose.pose.position.z
    #maximum veloctiy limit
    threh_max_vel = 1
    #permutation based on the number of agent
    if topic == "/crazyflie1/external_pose":
        topic_1 = "/crazyflie1/external_pose"
        topic_2 = "/crazyflie2/external_pose"
        topic_3 = "/crazyflie3/external_pose"
    elif topic == "/crazyflie2/external_pose":
        topic_1 = "/crazyflie2/external_pose"
        topic_2 = "/crazyflie3/external_pose"
        topic_3 = "/crazyflie1/external_pose"
    else:
        topic_1 = "/crazyflie3/external_pose"
        topic_2 = "/crazyflie1/external_pose"
        topic_3 = "/crazyflie2/external_pose"
    #define the position callback
    pose1 = rospy.Subscriber(topic_1, PoseStamped, agent1_cb)
    pose2 = rospy.Subscriber(topic_2, PoseStamped, agent2_cb)
    pose3 = rospy.Subscriber(topic_3, PoseStamped, agent3_cb)
    #update PID parameters
    updatePIDparmeters(rospy)
    #desired value
    dp1x = rospy.get_param("~a1x")
    dp1y = rospy.get_param("~a1y")
    dp2x = rospy.get_param("~a2x")
    dp2y = rospy.get_param("~a2y")
    dp3x = rospy.get_param("~a3x")
    dp3y = rospy.get_param("~a3y")
    height = rospy.get_param("~height", 0.6)
    coef = rospy.get_param("~coef", 1.8)
    coef_integral = rospy.get_param("~coef_integral", 0.3)
    frequency = rospy.get_param("~frequency", 30)
    rate = rospy.Rate(frequency)
    #position command
    posi_msg = Position()
    posi_msg.header.seq = 0
    posi_msg.header.stamp = rospy.Time.now()
    posi_msg.header.frame_id = worldFrame
    posi_msg.x = 0.0
    posi_msg.y = 0.0
    posi_msg.z = 0.0
    posi_msg.yaw = 0.0

    #stop command
    stop_msg = Empty()

    takeoff_flag = True

    if takeoff_flag:
        posi_pub = rospy.Publisher("cmd_position", Position, queue_size=1)
        #stop command
        stop_msg = Empty()
        stop_pub = rospy.Publisher("cmd_stop", Empty, queue_size=1)
        #full state command
        full_state_pub = rospy.Publisher("cmd_full_state", FullState, queue_size=1)
    else:
        posi_pub = rospy.Publisher("test_cmd_position", Position, queue_size=1)

        stop_pub = rospy.Publisher("test_cmd_stop", Empty, queue_size=1)
        #full state command
        full_state_pub = rospy.Publisher("test_cmd_full_state", FullState, queue_size=1)

    #publish three distances among agent1, agent2, agent3
    dist_pub = rospy.Publisher("info_dist", PoseStamped, queue_size=1)

    takeoff(rospy, posi_pub, posi_msg, rate, x_init, y_init, height, frequency)
    hover(rospy, posi_pub, posi_msg, rate, x_init, y_init, height, 3 * frequency)

    #init controller and model
    kp = 0.1
    kv = 0
    init_pos_group = [\
    [x1, y1],\
    [x2, y2],\
    [x3, y3]\
    ]

    start_vel = np.array([0,0])

    destination_pos_group = [\
    [dp1x, dp1y],\
    [dp2x, dp2y],\
    [dp3x, dp3y]\
    ]

    id_group = range(len(init_pos_group))

    drones = []
    for id in id_group:
        drone = drone_model_t(id, np.array(init_pos_group[id]), start_vel, destination_pos_group[id])
        drones.append(drone)

    controllers = []
    for id in id_group:
        controller = drone_controller_t(id, kp, kv)
        controllers.append(controller)

    #the time of begin
    begin_time = rospy.get_time()
    #convergence
    while not rospy.is_shutdown():
        now = rospy.get_time()
        t = now - begin_time
        if (t > 20.0):
            break

        if False == now_in_elctricial_fence(x1, y1, x2, y2, x3, y3):
            break

        neis = [{}, {}, {}]
        for id in id_group:
            neis[id]['vel'] = np.array(drones[id].get_vel())
            neis[id]['des'] = np.array(drones[id].get_destination())

        neis[0]['pos'] = np.array([x1, y1])
        neis[1]['pos'] = np.array([x2, y2])
        neis[2]['pos'] = np.array([x3, y3])

        u = [[], [], []]
        u[0] = controllers[0].update(neis[0], [neis[1], neis[2]])
        u[1] = controllers[1].update(neis[1], [neis[0], neis[2]])
        u[2] = controllers[2].update(neis[2], [neis[0], neis[1]])

        for id in id_group:
            drones[id].update(u[id], rospy.get_time())

        pos_cmd = [drones[0].get_pos()[0], drones[0].get_pos()[1], height]
        # vel_cmd = [u[0][0], u[0][1], 0]
        vel_cmd = [0, 0, 0]
        publish_full_state(rospy, full_state_pub, worldFrame, pos_cmd, vel_cmd)
        distanceTopic(rospy, dist_pub, x1, y1, x2, y2, x3, y3, dp1x, dp1y, dp2x, dp2y, dp3x, dp3y)

    # control break
    land(rospy, posi_pub, posi_msg, rate, x1, y1, z1, frequency)


text:
# python3
from sys import stdin
import scipy as sp
import scipy.optimize

def pivet_op(mat, row, col):
  pivet = mat[row][col]
  for j in range(0, len(mat[0])):
    mat[row][j] = mat[row][j] / pivet

  i = 0
  for i in range(len(mat)):
    if i == row:
      continue
    ratio = mat[i][col]
    for j in range(len(mat[0])):
      mat[i][j] -= (ratio * mat[row][j])

  return mat

def simplex(mat, XS):
  anst = 0
  opt = False
  n = len(mat)
  m = len(mat[0])

  while (not opt) and (anst == 0):
    mi = 0.0
    pi_col = 0
    j = 1
    while j < m - n + 1:
      cj = mat[0][j] * 1.0

      if cj < mi:
        mi = cj
        pi_col = j
      j += 1
    if mi == 0.0:
      opt = True
      continue

    pi_row = i = 0
    min_val = 10 ** 15

    for xi in mat:
      if i == 0:
        i += 1
        continue
      xij = xi[pi_col]
      if xij > 0:
        loc_min = xi[0]/ xij
        if loc_min > 0 and (loc_min < min_val or min_val == 10 ** 15):
          min_val = loc_min
          pi_row = i
      i += 1
    if min_val == 10 ** 15:
      anst = 1
      continue
    #print(pi_row, pi_col)
    mat = pivet_op(mat, pi_row, pi_col)
    XS[pi_col - 1] = mat[pi_row][0]
    '''
    print('\n----')
    for row in mat:
      print(row)
    print('----\n')
    #'''
  print('\n----')
  for row in mat:
    print(row)
  print('----\n')
  return [anst, XS[0:m - n]]

def solve_diet_problem(n, m, A, b, c):
  # Write your code here
  mat = [[0 for _ in range(n + m + 1)] for _ in range(n + 1)]
  XS = [0] * (m + n)
  for i in range(m, m + n):
    XS[i] = b[i - m]

  mat[0][0] = 0
  for i in range(1, m + 1):
    mat[0][i] = (-1 * c[i - 1])
  for i in range(m + 1, m + n + 1):
    mat[0][i] = 0

  for i in range(1, n + 1):
    mat[i][0] = b[i - 1]
    for j in range(1,m + 1):
      mat[i][j] = A[i - 1][j - 1]
    for j in range(m + 1, n + m + 1):
      if j == m + i:
        mat[i][j] = 1
      else:
        mat[i][j] = 0

  return simplex(mat, XS)

def solve_diet_problem0(n, mm, A, b, c):

    cmin = [-1 * x for x in c]
    # built in function in scipy and numpy module to solve linear programming
    res = sp.optimize.linprog(cmin, A, b)
    if 3 == res.status:
        ans = 1
        x = None
    elif 0 == res.status:
        ans = 0
        x = list(res.x)
    else:
        ans = -1
        x = None
    return ans, x

n, m = list(map(int, stdin.readline().split()))
A = []
for i in range(n):
  A += [list(map(int, stdin.readline().split()))]
b = list(map(int, stdin.readline().split()))
c = list(map(int, stdin.readline().split()))

anst, ansx = solve_diet_problem0(n, m, A, b, c)

if anst == -1:
  print("No solution")
if anst == 0:
  print("Bounded solution")
  print(' '.join(list(map(lambda x : '%.18f' % round(x, 18), ansx))))
if anst == 1:
  print("Infinity")


text:
from copy import deepcopy
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import gym
from collections import deque
import random

class Net(nn.Module):
    def __init__(self, state_dim, act_dim):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(state_dim, 32)
        self.fc2 = nn.Linear(32, 32)
        self.fc3 = nn.Linear(32, act_dim)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

class ReplayBuffer:
    def __init__(self, state_dim, act_dim, max_size=10000):
        self.max_size = max_size
        self.mem_control = 0
        self.state_memory = torch.zeros((self.max_size, *state_dim), dtype=torch.double)
        self.new_state_memory = torch.zeros((self.max_size, *state_dim), dtype=torch.double)
        self.action_memory = torch.zeros((self.max_size, act_dim))
        self.reward_memory = torch.zeros(self.max_size, dtype=torch.float)
        self.terminal_memory = torch.zeros(self.max_size, dtype=torch.uint8)

    def store_transition(self, state, action, reward, state_, done):
        index = self.mem_control % self.max_size
        self.state_memory[index] = torch.from_numpy(state)
        self.new_state_memory[index] = torch.from_numpy(state_)
        self.action_memory[index] = torch.tensor(action)
        self.reward_memory[index] = torch.from_numpy(np.array([reward]).astype(np.float))
        self.terminal_memory[index] = torch.from_numpy(np.array([1 - done]).astype(np.uint8))
        self.mem_control += 1

    def sample_buffer(self, batch_size):
        mem_size = min(self.mem_control, self.max_size)

        batch = np.random.choice(mem_size, batch_size)

        states = self.state_memory[batch]
        actions = self.action_memory[batch]
        rewards = self.reward_memory[batch]
        states_ = self.new_state_memory[batch]
        terminal = self.terminal_memory[batch]

        return states, states_, actions, rewards, terminal

    def __len__(self):
        return self.mem_control

class Trainer:
    def __init__(self, env, model, buffer, max_epoch=3000, max_timestep=200, rendering=False):
        self.env = env
        self.policy_net = model
        self.policy_net = self.policy_net.to(torch.double)
        self.target_net = deepcopy(self.policy_net)
        self.buffer = buffer
        self.max_epoch = max_epoch
        self.max_timestep = max_timestep
        self.rendering = rendering
        self.gamma = 0.95
        self.criterion = nn.MSELoss()

        self.optimizer = optim.Adam(self.policy_net.parameters(), lr=0.0001)

    def train(self):
        self.target_update()
        recent_10_steps = deque(maxlen=10)
        total_step = 0
        for epoch in range(self.max_epoch):
            state = self.env.reset()

            for timestep in range(self.max_timestep):
                action = self.decide_action(torch.tensor(state), epoch)
                next_state, reward, done, _ = self.env.step(action)

                total_step += 1

                if self.rendering:
                    self.env.render()

                terminal = 0

                if done:
                    terminal = 1
                    if timestep < 195:
                        reward = -1

                    recent_10_steps.append(timestep+1)

                self.buffer.store_transition(state, action, reward, next_state, terminal)
                state = next_state
                self.model_update()

                if done:
                    average_recent_10_rewards = sum(list(recent_10_steps)) / 10
                    if epoch % 10 == 0:
                        print('epoch:{}, timestep:{}, mean of recent 10 time steps:{}'.format(
                            epoch, timestep, average_recent_10_rewards))
                    break

            if epoch % 10 == 0:
                self.target_update()

    def model_update(self, batch_size=32):
        if len(self.buffer) < batch_size:
            return

        states, states_, actions, rewards, terminals = self.buffer.sample_buffer(batch_size)

        state_action_values = self.policy_net(states).gather(1, actions.to(torch.long))

        with torch.no_grad():
            next_state_values = self.target_net(states_).max(1)[0].detach()
            expected_state_action_values = self.gamma * next_state_values + rewards

        loss = self.criterion(state_action_values, expected_state_action_values.unsqueeze(1))

        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()

    def target_update(self):
        self.target_net.load_state_dict(self.policy_net.state_dict())

    def decide_action(self, state, epoch):

        epsilon = 0.3 - 0.0001 * epoch

        if epsilon <= random.random():
            with torch.no_grad():
                return self.policy_net(state).max(0)[1].item()
        else:
            return self.env.action_space.sample()

def main():
    seed = 42
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    env = gym.make('CartPole-v0')
    model = Net(env.observation_space.shape[0], env.action_space.n)
    buffer = ReplayBuffer([env.observation_space.shape[0]], 1)
    trainer = Trainer(env, model, buffer)
    trainer.train()

if __name__ == '__main__':
    main()


text:
"""
Show an image displayed the correct way.

* The green dots should be at the corners that are darker/brighter.
* The green dots should be in the center of these pixels.
* The darker corner is in the bottom left.
"""
# test_example = true

import numpy as np
from wgpu.gui.auto import WgpuCanvas, run
import pygfx as gfx

canvas = WgpuCanvas()
renderer = gfx.renderers.WgpuRenderer(canvas)
scene = gfx.Scene()

im = np.array(
    [
        [0, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 2],
    ],
    np.float32,
)

image = gfx.Image(
    gfx.Geometry(grid=gfx.Texture(im, dim=2).get_view(filter="nearest")),
    gfx.ImageBasicMaterial(clim=(0, 2)),
)
scene.add(image)

points = gfx.Points(
    gfx.Geometry(positions=[[0, 0, 1], [3, 3, 1]]),
    gfx.PointsMaterial(color=(0, 1, 0, 1), size=20),
)
scene.add(points)

camera = gfx.OrthographicCamera(10, 10)

canvas.request_draw(lambda: renderer.render(scene, camera))

if __name__ == "__main__":
    print(__doc__)
    run()


text:
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals

import numpy as np

from sklearn.model_selection import StratifiedShuffleSplit
from thesis.constants import RANDOM_SEED

class NotEnoughSensesError(ValueError):
    pass

def filter_minimum(target, min_count=2, invalid_target=-1):
    valid_targets = np.where(target != invalid_target)[0]

    labels, counts = np.unique(target[valid_targets], return_counts=True)
    over_minimum_count = np.where(counts >= min_count)[0]

    if over_minimum_count.shape[0] < 2:
        raise NotEnoughSensesError('Not enough labels to cover minimum count')

    return np.arange(target.shape[0])[np.in1d(target, labels[over_minimum_count])]

def validation_split(target, validation_ratio=0.1, force_split=True, random_seed=RANDOM_SEED):
    try:
        sss = StratifiedShuffleSplit(n_splits=1, test_size=validation_ratio, random_state=random_seed)
        return next(sss.split(np.zeros_like(target), target))
    except ValueError:
        if not force_split:
            raise
        num_classes = np.unique(target).shape[0]
        sss = StratifiedShuffleSplit(n_splits=1, test_size=num_classes / np.float(target.shape[0]),
                                     random_state=random_seed)
        return next(sss.split(np.zeros_like(target), target))


text:
#this program will combine images to make a master frame

#if you use this code, please cite Oelkers & Stassun 2018

#import the relevant libraries for basic tools
import pyfits
import numpy as np
import scipy
from scipy import stats
from os import path
import math
import time

#import relevant libraries for a list
import glob, os
from os import listdir
from os.path import isfile, join, exists

###UPDATE HERE#####
#what field are you looking at?
camera = '2'
ccd = '2'
blknum = 50 #how many images go into each holder?

#useful directories
cdedir = '../code/master/' #code directory
clndir = '../clean/' #directory where the cleaned images reside

###END UPDATE###

#get the image list and the number of files which need reduction
files = np.loadtxt(cdedir+'images.dat', dtype = 'string', unpack = 1, usecols = (0))
nfiles = len(files)

#iterate through the files
cnt = 0 #counter for the number of images used
kk = 0 #the jumper for file placement

for ii in range(0,len(files)):

	if (ii == 0):  # get size on first iteration only
		nx = pyfits.getval(clndir+files[0], 'NAXIS2')
	        ny = pyfits.getval(clndir+files[0], 'NAXIS1')
	        all_data = np.ndarray(shape=(blknum,nx,ny))
		expt = np.zeros(blknum)

	#read in the image
	img_data = pyfits.getdata(clndir+files[ii])
	expt[cnt] = pyfits.getval(clndir+files[ii],'EXPOSURE')

	#add the image to the vector
	all_data[cnt] = img_data
	cnt = cnt+1

	if (ii % 10 == 0) and (ii > 0):
		print 'Finished with 10 images at '+str(time.strftime("%a %d %b %Y %H:%M:%S"))+'.'

	if (ii == len(files)-1) or ((ii+1) % blknum == 0):

		#median combine the data
		combined_data = np.median(all_data,axis=0)

		# Write data to new file
		new_image = pyfits.PrimaryHDU(combined_data)
		new_image.header.set('NUMCOMB', cnt)
		new_image.header.set('EXPTIME', np.median(expt))

		#print the file with the appropriate counter
		if (kk < 10):
			new_image.writeto(cdedir+'frames/'+camera+'_'+ccd+'_master_0'+str(kk)+'.fits',clobber=True)
		else:
			if (kk >= 10) and (kk < 100):
				new_image.writeto(cdedir+'frames/'+camera+'_'+ccd+'_master_'+str(kk)+'.fits',clobber=True)

		print "The master frame hold was created using a median of "+str(cnt)+" images."
		kk = kk+1
		cnt = 0

		#clear the data file
	        all_data = np.ndarray(shape=(blknum,nx,ny))
		expt = np.zeros(blknum)

del all_data, img_data # clear up some memory

max_stars_repo_name: martinberoiz/DIA

max_issues_repo_name: martinberoiz/DIA

max_forks_repo_name: martinberoiz/DIA

reason: import numpy,import scipy,from scipy