<commit_before>#!/usr/bin/python

# Raspberry Pi and Sense Hat Stock Symbol Price Ticker
# A simple stock ticker price change monitor of today's positive or negative change using the Sense Hat
# Shows a red/green solid color 8x8 matrix up/down arrow and stock price ticker

import atexit
import datetime
import decimal
import errno
import os
import signal
import sys
import time
import urllib2
import ystockquote

from socket import error as SocketError

# from decimal import *

from sense_hat import SenseHat

sense = SenseHat()

# Rotation (Default=0)
sense.set_rotation(90)

debug = 0

# Stock quote configuration:
tickerSymbol = 'AAPL'

# Functions

# Turn off all the LEDs
def lightsOut():
	if debug == 1:
		print ('Turning off all LEDs.')
	sense.clear()

# Console message and LEDs off while market closed
def marketClosed():
	print "Stock Market Closed.", now
	lightsOut()
	time.sleep(60)

# Turn the LEDs off at program exit
atexit.register(lightsOut)

# Debugging
if debug == 1:
	allInfo = ystockquote.get_all(tickerSymbol)
	print allInfo
# 	quote = ystockquote.get_change(tickerSymbol)
# 	print quote

# print tickerSymbol + " Price = " + allInfo["price"]
# print tickerSymbol + " Change = " + allInfo["change"]
# print allInfo["change"]

# print allInfo

decimal.getcontext().prec = 8

# Handle timeouts gracefully
def timeoutHandler(signum, frame):
	print "Error: Timeout fetching quote."
	lightsOut()
	time.sleep(10)
	pass

signal.signal(signal.SIGALRM, timeoutHandler)

# Main function to get the quote and animate the LEDs
def getQuote(change):
	if debug == 1:
		print "Function getQuote"

	try:
		signal.alarm(10)
		change = ystockquote.get_change(tickerSymbol)
	except urllib2.HTTPError as err:
		if err.code == 404:
			print "Error: 404 Not Found."
		else:
			print "Error: ", err.code
			# pass
	except urllib2.URLError as err:
		print "Error: Connection reset by peer."
		pass
	except SocketError as err:
	    if err.errno == errno.ECONNRESET:
	        print "Error: Connection reset by peer."
	    	pass
	# price = ystockquote.get_price(tickerSymbol)

	while change == 'N/A':
		print "Error: No price change data."
		time.sleep(60)
		change = ystockquote.get_change(tickerSymbol)

	changedecimal = decimal.Decimal(change)
	# pricedecimal = Decimal(price)
	# changedecimal = 0

	# Reset timeout
	signal.alarm(0)

	# Console message with price change
	print "$ Change: ", changedecimal
	# print pricedecimal

	# lastclose = Decimal(pricedecimal) - Decimal(changedecimal)

	# print lastclose

	# Negative
	if changedecimal < 0:
		LED_BRIGHTNESS = abs(int(round(100 * changedecimal)))
		if LED_BRIGHTNESS > 255:
			LED_BRIGHTNESS = 255
		if LED_BRIGHTNESS < 50:
			LED_BRIGHTNESS = 50
		if debug == 1:
			print "Brightness: ", LED_BRIGHTNESS
		R = [LED_BRIGHTNESS, 0, 0] # Red
		G = [0, LED_BRIGHTNESS, 0]  # Green
		B = [0, 0, 0]  # Black

		down_arrow = [
		R, R, R, R, R, R, R, R,
		R, R, R, R, R, R, R, R,
		B, R, R, R, R, R, R, B,
		B, R, R, R, R, R, R, B,
		B, B, R, R, R, R, B, B,
		B, B, R, R, R, R, B, B,
		B, B, B, R, R, B, B, B,
		B, B, B, R, R, B, B, B
		]

		sense.set_pixels(down_arrow)
		time.sleep(1.5)
		sense.show_message(str (changedecimal), text_colour=[LED_BRIGHTNESS, 0, 0])

	# Positive
	if changedecimal > 0:
		LED_BRIGHTNESS = int(round(100 * changedecimal))
		if LED_BRIGHTNESS > 255:
			LED_BRIGHTNESS = 255
		if LED_BRIGHTNESS < 50:
			LED_BRIGHTNESS = 50
		if debug == 1:
			print "Brightness: ", LED_BRIGHTNESS
		R = [LED_BRIGHTNESS, 0, 0] # Red
		G = [0, LED_BRIGHTNESS, 0]  # Green
		B = [0, 0, 0]  # Black

		up_arrow = [
		B, B, B, G, G, B, B, B,
		B, B, B, G, G, B, B, B,
		B, B, G, G, G, G, B, B,
		B, B, G, G, G, G, B, B,
		B, G, G, G, G, G, G, B,
		B, G, G, G, G, G, G, B,
		G, G, G, G, G, G, G, G,
		G, G, G, G, G, G, G, G
		]

		sense.set_pixels(up_arrow)
		time.sleep(1.5)
		sense.show_message( str (changedecimal), text_colour=[0, LED_BRIGHTNESS, 0])

	# Zero
	if changedecimal == 0:
		if debug == 1:
			print "Zero Change!"
		# sense.clear([50, 50, 50])
		sense.show_message(str (changedecimal), text_colour=[50, 50, 50])

	time.sleep(.5)

# Main program logic follows:
if __name__ == '__main__':

	try:
		while True:
			now = datetime.datetime.now()
			# print now

			# Between Monday - Friday
			if 0 <= now.weekday() <= 5:
				if debug == 1:
					print "Weekday: ", now.weekday()
				# print now.hour

				# Between 9AM - 5PM
				if now.hour == 9:
					if debug == 1:
						print "Hour 9AM"
					if now.minute >= 30:
						if debug == 1:
							print "Minute 30 or later"
						if 9 <= now.hour <= 17:
							# print "Hour: ", now.hour
							# print now.time()

							if debug == 1:
								print "Calling function getQuote"

							getQuote(0)

					else:
						marketClosed()

				else:
					if 9 <= now.hour <= 15:
						if debug == 1:
							print "Hour between 10AM-4PM"
						# print "Hour: ", now.hour
						# print now.time()

						if debug == 1:
							print "Calling function getQuote"

						getQuote(0)

					else:
						marketClosed()

			else:
				marketClosed()

	# Handle Ctrl-C gracefully
	except KeyboardInterrupt:
		lightsOut()
		print ' - Exiting'
		try:
			sys.exit(0)
		except SystemExit:
			os._exit(0)
<commit_msg>possible bug fixed<commit_after>#!/usr/bin/python

# Raspberry Pi and Sense Hat Stock Symbol Price Ticker
# A simple stock ticker price change monitor of today's positive or negative change using the Sense Hat
# Shows a red/green solid color 8x8 matrix up/down arrow and stock price ticker

import atexit
import datetime
import decimal
import errno
import os
import signal
import sys
import time
import urllib2
import ystockquote

from socket import error as SocketError

# from decimal import *

from sense_hat import SenseHat

sense = SenseHat()

# Rotation (Default=0)
sense.set_rotation(90)

debug = 0

# Stock quote configuration:
tickerSymbol = 'AAPL'

# Functions

# Turn off all the LEDs
def lightsOut():
	if debug == 1:
		print ('Turning off all LEDs.')
	sense.clear()

# Console message and LEDs off while market closed
def marketClosed():
	print "Stock Market Closed.", now
	lightsOut()
	time.sleep(60)

# Turn the LEDs off at program exit
atexit.register(lightsOut)

# Debugging
if debug == 1:
	allInfo = ystockquote.get_all(tickerSymbol)
	print allInfo
# 	quote = ystockquote.get_change(tickerSymbol)
# 	print quote

# print tickerSymbol + " Price = " + allInfo["price"]
# print tickerSymbol + " Change = " + allInfo["change"]
# print allInfo["change"]

# print allInfo

decimal.getcontext().prec = 8

# Handle timeouts gracefully
def timeoutHandler(signum, frame):
	print "Error: Timeout fetching quote."
	lightsOut()
	time.sleep(10)
	signal.alarm(0)
	return

signal.signal(signal.SIGALRM, timeoutHandler)

# Main function to get the quote and animate the LEDs
def getQuote(change):
	if debug == 1:
		print "Function getQuote"

	try:
		signal.alarm(10)
		change = ystockquote.get_change(tickerSymbol)
	except urllib2.HTTPError as err:
		if err.code == 404:
			print "Error: 404 Not Found."
		else:
			print "Error: ", err.code
	except urllib2.URLError as err:
		print "Error: Connection reset by peer."
	except SocketError as err:
		if err.errno == errno.ECONNRESET:
			print "Error: Connection reset by peer."
	# price = ystockquote.get_price(tickerSymbol)

	while change == 'N/A':
		print "Error: No price change data."
		time.sleep(60)
		change = ystockquote.get_change(tickerSymbol)

	changedecimal = decimal.Decimal(change)
	# pricedecimal = Decimal(price)
	# changedecimal = 0

	# Reset timeout
	signal.alarm(0)

	# Console message with price change
	print "$ Change: ", changedecimal
	# print pricedecimal

	# lastclose = Decimal(pricedecimal) - Decimal(changedecimal)

	# print lastclose

	# Negative
	if changedecimal < 0:
		LED_BRIGHTNESS = abs(int(round(100 * changedecimal)))
		if LED_BRIGHTNESS > 255:
			LED_BRIGHTNESS = 255
		if LED_BRIGHTNESS < 50:
			LED_BRIGHTNESS = 50
		if debug == 1:
			print "Brightness: ", LED_BRIGHTNESS
		R = [LED_BRIGHTNESS, 0, 0] # Red
		G = [0, LED_BRIGHTNESS, 0]  # Green
		B = [0, 0, 0]  # Black

		down_arrow = [
		R, R, R, R, R, R, R, R,
		R, R, R, R, R, R, R, R,
		B, R, R, R, R, R, R, B,
		B, R, R, R, R, R, R, B,
		B, B, R, R, R, R, B, B,
		B, B, R, R, R, R, B, B,
		B, B, B, R, R, B, B, B,
		B, B, B, R, R, B, B, B
		]

		sense.set_pixels(down_arrow)
		time.sleep(1.5)
		sense.show_message(str (changedecimal), text_colour=[LED_BRIGHTNESS, 0, 0])

	# Positive
	if changedecimal > 0:
		LED_BRIGHTNESS = int(round(100 * changedecimal))
		if LED_BRIGHTNESS > 255:
			LED_BRIGHTNESS = 255
		if LED_BRIGHTNESS < 50:
			LED_BRIGHTNESS = 50
		if debug == 1:
			print "Brightness: ", LED_BRIGHTNESS
		R = [LED_BRIGHTNESS, 0, 0] # Red
		G = [0, LED_BRIGHTNESS, 0]  # Green
		B = [0, 0, 0]  # Black

		up_arrow = [
		B, B, B, G, G, B, B, B,
		B, B, B, G, G, B, B, B,
		B, B, G, G, G, G, B, B,
		B, B, G, G, G, G, B, B,
		B, G, G, G, G, G, G, B,
		B, G, G, G, G, G, G, B,
		G, G, G, G, G, G, G, G,
		G, G, G, G, G, G, G, G
		]

		sense.set_pixels(up_arrow)
		time.sleep(1.5)
		sense.show_message( str (changedecimal), text_colour=[0, LED_BRIGHTNESS, 0])

	# Zero
	if changedecimal == 0:
		if debug == 1:
			print "Zero Change!"
		# sense.clear([50, 50, 50])
		sense.show_message(str (changedecimal), text_colour=[50, 50, 50])

	time.sleep(.5)

# Main program logic follows:
if __name__ == '__main__':

	try:
		while True:
			now = datetime.datetime.now()
			# print now

			# Between Monday - Friday
			if 0 <= now.weekday() <= 5:
				if debug == 1:
					print "Weekday: ", now.weekday()
				# print now.hour

				# Between 9AM - 5PM
				if now.hour == 9:
					if debug == 1:
						print "Hour 9AM"
					if now.minute >= 30:
						if debug == 1:
							print "Minute 30 or later"
						if 9 <= now.hour <= 17:
							# print "Hour: ", now.hour
							# print now.time()

							if debug == 1:
								print "Calling function getQuote"

							getQuote(0)

					else:
						marketClosed()

				else:
					if 9 <= now.hour <= 15:
						if debug == 1:
							print "Hour between 10AM-4PM"
						# print "Hour: ", now.hour
						# print now.time()

						if debug == 1:
							print "Calling function getQuote"

						getQuote(0)

					else:
						marketClosed()

			else:
				marketClosed()

	# Handle Ctrl-C gracefully
	except KeyboardInterrupt:
		lightsOut()
		print ' - Exiting'
		try:
			sys.exit(0)
		except SystemExit:
			os._exit(0)
<|endoftext|>


<commit_before>#TODO Add intepreter here
#@Author Sean McLaughlin
desc ='''
This module is the first I'm writing for my project for the winter quarter of 2016 in Aaron Roodman's group.
This is built on top of previous work conducted by Aaron and his graduate student Chris Davis. They have
developed WavefrontPSF, which estimates the optical contribution of the PSF with a relatively simple model.
It was found that the optical portion was not as signifcant a fraction of the total PSF as hoped,
so some sort of afterburner is going to need to be added. After the optical portion has been deconvolved
from the observed stars (using Richardson-Lucy deconvolution), the remainder will be treated as the "atmospheric"
portion of the psf. This module load in preprocessed observed stars, run WavefrontPSF on them, deconvolve
the optical PSF, then run PSFEX (a packaged PSF modeler) on the residual.

TODO Details on actually running the module.
'''

from argparse import ArgumentParser
parser = ArgumentParser(description = desc)

parser.add_argument('expid', metavar = 'expid', type = int, help =\
                    'ID of the exposure to analyze')

args = vars(parser.parse_args())

import numpy as np
from itertools import izip
from optical_model import getOpticalPSF
from glob import glob
from astropy.io import fits #TODO check if I should support pyfits
from lucy import deconvolve

#get optical PSF
optPSFStamps, file_directory = getOpticalPSF(args['expid'])

#now we need the data to pair with this.
files = glob(file_directory + '/*{0}'.format('_selpsfcat.fits'))

vignettes = []
full_cats = []
ext = 2

for file in files:
    hdulist = fits.open(file)
    full_cats.append(hdulist[ext].data)
    vignettes.append(hdulist[ext].data['VIGNET'])

vignettes = np.array(vignettes)

from matplotlib import pyplot as plt
plt.subplot(1,2,1)
plt.title('Original')
plt.imshow(vignettes[0,:,:])
plt.subplot(1,2, 2)
plt.title('Optical')
plt.imshow(optPSFStamps[0,:,:])
plt.show()

for optPSFStamp, vignette in izip(optPSFStamps, vignettes):
    aptPSFEst = deconvolve(optPSFStamp,vignette,psi_0=None,mask=None,mu0=0,convergence=1.0e-3,chi2Level=0.,niterations=50)
    break

print aptPSFEst.shape

from matplotlib import pyplot as plt
plt.subplot(1,3,1)
plt.title('Original')
plt.imshow(vignette)
plt.subplot(1,3, 2)
plt.title('Optical')
plt.imshow(optPSFStamp)
plt.subplot(1,3,3)
plt.title('Remainder')
plt.imshow(aptPSFEst)<commit_msg>Starting to work on the aterburner.<commit_after>#TODO Add intepreter here
#@Author Sean McLaughlin
desc ='''
This module is the first I'm writing for my project for the winter quarter of 2016 in Aaron Roodman's group.
This is built on top of previous work conducted by Aaron and his graduate student Chris Davis. They have
developed WavefrontPSF, which estimates the optical contribution of the PSF with a relatively simple model.
It was found that the optical portion was not as signifcant a fraction of the total PSF as hoped,
so some sort of afterburner is going to need to be added. After the optical portion has been deconvolved
from the observed stars (using Richardson-Lucy deconvolution), the remainder will be treated as the "atmospheric"
portion of the psf. This module load in preprocessed observed stars, run WavefrontPSF on them, deconvolve
the optical PSF, then run PSFEX (a packaged PSF modeler) on the residual.

TODO Details on actually running the module.
'''

from argparse import ArgumentParser
parser = ArgumentParser(description = desc)

parser.add_argument('expid', metavar = 'expid', type = int, help =\
                    'ID of the exposure to analyze')

args = vars(parser.parse_args())

import numpy as np
from itertools import izip
from optical_model import getOpticalPSF
from glob import glob
from astropy.io import fits #TODO check if I should support pyfits
from lucy import deconvolve

#get optical PSF
optPSFStamps, file_directory = getOpticalPSF(args['expid'])

#now we need the data to pair with this.
files = glob(file_directory + '/*{0}'.format('_selpsfcat.fits'))

vignettes = []
full_cats = []
ext = 2

for file in files:
    hdulist = fits.open(file)
    full_cats.append(hdulist[ext].data)
    vignettes.append(hdulist[ext].data['VIGNET'])

vignettes = np.array(vignettes)

print vignettes.shape

from matplotlib import pyplot as plt
plt.subplot(1,2,1)
plt.title('Original')
plt.imshow(vignettes[0,:,:])
plt.subplot(1,2, 2)
plt.title('Optical')
plt.imshow(optPSFStamps[0,:,:])
plt.show()

for optPSFStamp, vignette in izip(optPSFStamps, vignettes):
    aptPSFEst = deconvolve(optPSFStamp,vignette,psi_0=None,mask=None,mu0=0,convergence=1.0e-3,chi2Level=0.,niterations=50)
    break

print aptPSFEst.shape

from matplotlib import pyplot as plt
plt.subplot(1,3,1)
plt.title('Original')
plt.imshow(vignette)
plt.subplot(1,3, 2)
plt.title('Optical')
plt.imshow(optPSFStamp)
plt.subplot(1,3,3)
plt.title('Remainder')
plt.imshow(aptPSFEst)<|endoftext|>


<commit_before># -*- coding: utf-8  -*-
#
# Copyright (C) 2009-2012 Ben Kurtovic <ben.kurtovic@verizon.net>
#
# 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.

from earwigbot.commands import Command

class Langcode(Command):
    """Convert a language code into its name and a list of WMF sites in that
    language."""
    name = "langcode"
    commands = ["langcode", "lang", "language"]

    def process(self, data):
        if not data.args:
            self.reply(data, "please specify a language code.")
            return

        code = data.args[0]
        site = self.bot.wiki.get_site()
        matrix = site.api_query(action="sitematrix")["sitematrix"]
        del matrix["count"]
        del matrix["specials"]

        for site in matrix.itervalues():
            if site["code"] == code:
                name = site["name"].encode("utf8")
                localname = site["localname"].encode("utf8")
                if name != localname:
                    name += " ({0})".format(localname)
                sites = ", ".join([s["url"] for s in site["site"]])
                msg = "\x0302{0}\x0F is {1} ({2})".format(code, name, sites)
                self.reply(data, msg)
                return

        self.reply(data, "language \x0302{0}\x0F not found.".format(code))
<commit_msg>Capitalise replies in !langcode<commit_after># -*- coding: utf-8  -*-
#
# Copyright (C) 2009-2012 Ben Kurtovic <ben.kurtovic@verizon.net>
#
# 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.

from earwigbot.commands import Command

class Langcode(Command):
    """Convert a language code into its name and a list of WMF sites in that
    language."""
    name = "langcode"
    commands = ["langcode", "lang", "language"]

    def process(self, data):
        if not data.args:
            self.reply(data, "Please specify a language code.")
            return

        code = data.args[0]
        site = self.bot.wiki.get_site()
        matrix = site.api_query(action="sitematrix")["sitematrix"]
        del matrix["count"]
        del matrix["specials"]

        for site in matrix.itervalues():
            if site["code"] == code:
                name = site["name"].encode("utf8")
                localname = site["localname"].encode("utf8")
                if name != localname:
                    name += " ({0})".format(localname)
                sites = ", ".join([s["url"] for s in site["site"]])
                msg = "\x0302{0}\x0F is {1} ({2})".format(code, name, sites)
                self.reply(data, msg)
                return

        self.reply(data, "Language \x0302{0}\x0F not found.".format(code))
<|endoftext|>


<commit_before>import re
import time
import base64
import urllib
import hashlib
import hmac
import sha

from urllib import quote
from bson import ObjectId
from dateutil.parser import parse

from boto.iam import IAMConnection
from boto.s3.cors import CORSConfiguration
from boto.exception import BotoServerError

from website.util import rubeus

from api import get_bucket_list
import settings as s3_settings

def adjust_cors(s3wrapper, clobber=False):
    """Set CORS headers on a bucket, removing pre-existing headers set by the
    OSF. Optionally clear all pre-existing headers.

    :param S3Wrapper s3wrapper: S3 wrapper instance
    :param bool clobber: Remove all pre-existing rules. Note: if this option
        is set to True, remember to warn or prompt the user first!

    """
    rules = s3wrapper.get_cors_rules()

    # Remove some / all pre-existing rules
    if clobber:
        rules = CORSConfiguration([])
    else:
        rules = CORSConfiguration([
            rule
            for rule in rules
            if 'osf-s3' not in (rule.id or '')
        ])

    # Add new rule
    rules.add_rule(
        ['PUT', 'GET'],
        s3_settings.ALLOWED_ORIGIN,
        allowed_header=['*'],
        id='osf-s3-{0}'.format(ObjectId()),
    )

    # Save changes
    s3wrapper.set_cors_rules(rules)

def wrapped_key_to_json(wrapped_key, node):
    urls = build_urls(node, quote(wrapped_key.s3Key.key.encode('utf-8')))
    rv = {
        rubeus.KIND: _key_type_to_rubeus(wrapped_key.type),
        'name': wrapped_key.name,
        'size': (wrapped_key.size, wrapped_key.size) if wrapped_key.size is not None else '--',
        'ext': wrapped_key.extension if wrapped_key.extension is not None else '--',
        'lastMod': wrapped_key.lastMod.strftime("%Y/%m/%d %I:%M %p") if wrapped_key.lastMod is not None else '--',
        'urls': {
            # TODO: Don't use ternary operators here
            'download': urls['download'] if wrapped_key.type == 'file' else None,
            'delete': urls['delete'] if wrapped_key.type == 'file' else None,
            'view': urls['view'] if wrapped_key.type == 'file' else None,
            'fetch': node.api_url + 's3/hgrid/' + wrapped_key.s3Key.key if wrapped_key.type == 'folder' else None,
            'upload': urls['upload'],
        }
    }
    if wrapped_key.type == 'folder':
        rv.update({
            'nodeUrl': node.url,
            'nodeApiUrl': node.api_url,
        })
    return rv

def get_bucket_drop_down(user_settings):
    try:
        return [
            bucket.name
            for bucket in get_bucket_list(user_settings)
        ]
    except BotoServerError:
        return None

def _key_type_to_rubeus(key_type):
    if key_type == 'folder':
        return rubeus.FOLDER
    else:
        return rubeus.FILE

def create_version_list(wrapper, key_name, node):
    versions = wrapper.get_file_versions(key_name)
    return [
        {
            'id': x.version_id if x.version_id != 'null' else 'Pre-versioning',
            'date': parse(x.last_modified).ctime(),
            'download': build_urls(node, key_name, vid=x.version_id, url='download'),
        }
        for x in versions
    ]

def serialize_bucket(s3wrapper):
    return [
        {
            'name': x.name,
            'path': x.s3Key.key,
            'version_id': s3wrapper.bucket.get_key(x.s3Key.key).version_id,
        }
        for x in s3wrapper.get_wrapped_keys()
    ]

def create_osf_user(access_key, secret_key, name):

    connection = IAMConnection(access_key, secret_key)

    user_name = u'osf-{0}'.format(ObjectId())

    try:
        connection.get_user(user_name)
    except BotoServerError:
        connection.create_user(user_name)

    try:
        connection.get_user_policy(
            user_name,
            s3_settings.OSF_USER_POLICY
        )
    except BotoServerError:
        connection.put_user_policy(
            user_name,
            s3_settings.OSF_USER_POLICY_NAME,
            s3_settings.OSF_USER_POLICY
        )

    response = connection.create_access_key(user_name)
    access_key = response['create_access_key_response']['create_access_key_result']['access_key']
    return user_name, access_key

def remove_osf_user(user_settings):
    connection = IAMConnection(user_settings.access_key, user_settings.secret_key)
    connection.delete_access_key(
        user_settings.access_key,
        user_settings.s3_osf_user
    )
    connection.delete_user_policy(
        user_settings.s3_osf_user,
        s3_settings.OSF_USER_POLICY_NAME
    )
    return connection.delete_user(user_settings.s3_osf_user)

def build_urls(node, file_name, url=None, etag=None, vid=None):
    rv = {
        'upload': u'{node_api}s3/'.format(node_api=node.api_url),
        'download': u'{node_url}s3/{file_name}/download/{vid}'.format(node_url=node.url, file_name=file_name, vid='' if not vid else '?vid={0}'.format(vid)),
        'view': u'{node_url}s3/{file_name}/'.format(node_url=node.url, file_name=file_name),
        'delete': u'{node_api}s3/{file_name}/'.format(node_api=node.api_url, file_name=file_name),
        'render': u'{node_api}s3/{file_name}/render/{etag}'.format(node_api=node.api_url,
            file_name=file_name, etag='' if not etag else '?etag={0}'.format(etag)),
        'fetch': u'{node_api}s3/hgrid/{file_name}'.format(node_api=node.api_url, file_name=file_name),
        'info': u'{node_api}s3/{file_name}/info/'.format(node_api=node.api_url, file_name=file_name),
    }
    if not url:
        return rv
    else:
        return rv[url]

def get_cache_file_name(key_name, etag):
    return u'{0}_{1}.html'.format(
        hashlib.md5(key_name).hexdigest(),
        etag,
    )

def validate_bucket_name(name):
    validate_name = re.compile('^(?!.*(\.\.|-\.))[^.][a-z0-9\d.-]{2,61}[^.]$')
    return bool(validate_name.match(name))

def generate_signed_url(mime, file_name, s3):

    expires = int(time.time() + 10)
    amz_headers = 'x-amz-acl:private'

    request_to_sign = str("PUT\n\n{mime_type}\n{expires}\n{amz_headers}\n/{resource}".format(
        mime_type=mime, expires=expires, amz_headers=amz_headers, resource=s3.bucket + '/' + file_name))

    url = 'https://s3.amazonaws.com/{bucket}/{filename}'.format(
        filename=file_name, bucket=s3.bucket)

    signed = urllib.quote_plus(base64.encodestring(
        hmac.new(str(s3.user_settings.secret_key), request_to_sign, sha).digest()).strip())

    return '{url}?AWSAccessKeyId={access_key}&Expires={expires}&Signature={signed}'.format(url=url, access_key=s3.user_settings.access_key, expires=expires, signed=signed),
    #/blackhttpmagick
<commit_msg>Replace info link with api_url_for<commit_after>import re
import time
import base64
import urllib
import hashlib
import hmac
import sha

from urllib import quote
from bson import ObjectId
from dateutil.parser import parse

from boto.iam import IAMConnection
from boto.s3.cors import CORSConfiguration
from boto.exception import BotoServerError

from website.util import rubeus

from api import get_bucket_list
import settings as s3_settings

def adjust_cors(s3wrapper, clobber=False):
    """Set CORS headers on a bucket, removing pre-existing headers set by the
    OSF. Optionally clear all pre-existing headers.

    :param S3Wrapper s3wrapper: S3 wrapper instance
    :param bool clobber: Remove all pre-existing rules. Note: if this option
        is set to True, remember to warn or prompt the user first!

    """
    rules = s3wrapper.get_cors_rules()

    # Remove some / all pre-existing rules
    if clobber:
        rules = CORSConfiguration([])
    else:
        rules = CORSConfiguration([
            rule
            for rule in rules
            if 'osf-s3' not in (rule.id or '')
        ])

    # Add new rule
    rules.add_rule(
        ['PUT', 'GET'],
        s3_settings.ALLOWED_ORIGIN,
        allowed_header=['*'],
        id='osf-s3-{0}'.format(ObjectId()),
    )

    # Save changes
    s3wrapper.set_cors_rules(rules)

def wrapped_key_to_json(wrapped_key, node):
    urls = build_urls(node, quote(wrapped_key.s3Key.key.encode('utf-8')))
    rv = {
        rubeus.KIND: _key_type_to_rubeus(wrapped_key.type),
        'name': wrapped_key.name,
        'size': (wrapped_key.size, wrapped_key.size) if wrapped_key.size is not None else '--',
        'ext': wrapped_key.extension if wrapped_key.extension is not None else '--',
        'lastMod': wrapped_key.lastMod.strftime("%Y/%m/%d %I:%M %p") if wrapped_key.lastMod is not None else '--',
        'urls': {
            # TODO: Don't use ternary operators here
            'download': urls['download'] if wrapped_key.type == 'file' else None,
            'delete': urls['delete'] if wrapped_key.type == 'file' else None,
            'view': urls['view'] if wrapped_key.type == 'file' else None,
            'fetch': node.api_url + 's3/hgrid/' + wrapped_key.s3Key.key if wrapped_key.type == 'folder' else None,
            'upload': urls['upload'],
        }
    }
    if wrapped_key.type == 'folder':
        rv.update({
            'nodeUrl': node.url,
            'nodeApiUrl': node.api_url,
        })
    return rv

def get_bucket_drop_down(user_settings):
    try:
        return [
            bucket.name
            for bucket in get_bucket_list(user_settings)
        ]
    except BotoServerError:
        return None

def _key_type_to_rubeus(key_type):
    if key_type == 'folder':
        return rubeus.FOLDER
    else:
        return rubeus.FILE

def create_version_list(wrapper, key_name, node):
    versions = wrapper.get_file_versions(key_name)
    return [
        {
            'id': x.version_id if x.version_id != 'null' else 'Pre-versioning',
            'date': parse(x.last_modified).ctime(),
            'download': build_urls(node, key_name, vid=x.version_id, url='download'),
        }
        for x in versions
    ]

def serialize_bucket(s3wrapper):
    return [
        {
            'name': x.name,
            'path': x.s3Key.key,
            'version_id': s3wrapper.bucket.get_key(x.s3Key.key).version_id,
        }
        for x in s3wrapper.get_wrapped_keys()
    ]

def create_osf_user(access_key, secret_key, name):

    connection = IAMConnection(access_key, secret_key)

    user_name = u'osf-{0}'.format(ObjectId())

    try:
        connection.get_user(user_name)
    except BotoServerError:
        connection.create_user(user_name)

    try:
        connection.get_user_policy(
            user_name,
            s3_settings.OSF_USER_POLICY
        )
    except BotoServerError:
        connection.put_user_policy(
            user_name,
            s3_settings.OSF_USER_POLICY_NAME,
            s3_settings.OSF_USER_POLICY
        )

    response = connection.create_access_key(user_name)
    access_key = response['create_access_key_response']['create_access_key_result']['access_key']
    return user_name, access_key

def remove_osf_user(user_settings):
    connection = IAMConnection(user_settings.access_key, user_settings.secret_key)
    connection.delete_access_key(
        user_settings.access_key,
        user_settings.s3_osf_user
    )
    connection.delete_user_policy(
        user_settings.s3_osf_user,
        s3_settings.OSF_USER_POLICY_NAME
    )
    return connection.delete_user(user_settings.s3_osf_user)

def build_urls(node, file_name, url=None, etag=None, vid=None):
    rv = {
        'upload': u'{node_api}s3/'.format(node_api=node.api_url),
        'download': u'{node_url}s3/{file_name}/download/{vid}'.format(node_url=node.url, file_name=file_name, vid='' if not vid else '?vid={0}'.format(vid)),
        'view': u'{node_url}s3/{file_name}/'.format(node_url=node.url, file_name=file_name),
        'delete': u'{node_api}s3/{file_name}/'.format(node_api=node.api_url, file_name=file_name),
        'render': u'{node_api}s3/{file_name}/render/{etag}'.format(node_api=node.api_url,
            file_name=file_name, etag='' if not etag else '?etag={0}'.format(etag)),
        'fetch': u'{node_api}s3/hgrid/{file_name}'.format(node_api=node.api_url, file_name=file_name),
        'info': node.api_url_for('file_delete_info', fid=file_name),
    }
    if not url:
        return rv
    else:
        return rv[url]

def get_cache_file_name(key_name, etag):
    return u'{0}_{1}.html'.format(
        hashlib.md5(key_name).hexdigest(),
        etag,
    )

def validate_bucket_name(name):
    validate_name = re.compile('^(?!.*(\.\.|-\.))[^.][a-z0-9\d.-]{2,61}[^.]$')
    return bool(validate_name.match(name))

def generate_signed_url(mime, file_name, s3):

    expires = int(time.time() + 10)
    amz_headers = 'x-amz-acl:private'

    request_to_sign = str("PUT\n\n{mime_type}\n{expires}\n{amz_headers}\n/{resource}".format(
        mime_type=mime, expires=expires, amz_headers=amz_headers, resource=s3.bucket + '/' + file_name))

    url = 'https://s3.amazonaws.com/{bucket}/{filename}'.format(
        filename=file_name, bucket=s3.bucket)

    signed = urllib.quote_plus(base64.encodestring(
        hmac.new(str(s3.user_settings.secret_key), request_to_sign, sha).digest()).strip())

    return '{url}?AWSAccessKeyId={access_key}&Expires={expires}&Signature={signed}'.format(url=url, access_key=s3.user_settings.access_key, expires=expires, signed=signed),
    #/blackhttpmagick
<|endoftext|>


<commit_before>
from pyramid.view import view_config
from ..contentbase import (
    Root
)
from pyelasticsearch import ElasticSearch
es = ElasticSearch('http://localhost:9200')

# Query class should be improved to accomodate filters and other ES functionality
class Query(dict):

    def __init__(self):
        self.query = dict()
        self.facets = dict()
        self.fields = []
        self.highlight = dict({'fields': {"*": {}}})

    def __setattr__(self, k, v):
        if k in self.keys():
            self[k] = v
        elif not hasattr(self, k):
            self[k] = v
        else:
            raise AttributeError("Cannot set '%s', cls attribute already exists" % (k, ))

    def __getattr__(self, k):
        if k in self.keys():
            return self[k]
        raise AttributeError

#FilteredQuery class is extended version of Query class
class FilteredQuery(dict):

    def __init__(self):
        self.query = dict({'filtered': {'query': {'queryString': {"query": ''}}, 'filter': {'and': {'filters': []}}}})
        self.facets = dict()
        self.fields = []
        self.highlight = dict({'fields': {"*": {}}})

    def __setattr__(self, k, v):
        if k in self.keys():
            self[k] = v
        elif not hasattr(self, k):
            self[k] = v
        else:
            raise AttributeError("Cannot set '%s', cls attribute already exists" % (k, ))

    def __getattr__(self, k):
        if k in self.keys():
            return self[k]
        raise AttributeError

    def __setterm__(self, v):
        self['query']['filtered']['query']['queryString']['query'] = v

    def __setfilter__(self, k, v):
        self['query']['filtered']['filter']['and']['filters'].append({'bool': {'must': {'term': {k: v}}}})

    def __setmissingfilter__(self, v):
        self['query']['filtered']['filter']['and']['filters'].append({'missing': {'field': v}})

@view_config(name='search', context=Root, request_method='GET', permission='search')
def search(context, request):
    ''' Search view connects to ElasticSearch and returns the results'''

    result = context.__json__(request)
    params = request.params
    root = request.root
    result.update({
        '@id': '/search/',
        '@type': ['search'],
        'title': 'Search ENCODE',
        'facets': [],
        '@graph': [],
        'columns': {},
        'count': {}
    })
    size = 999999
    # if no searchTerm
    try:
        search_term = params['searchTerm']
    except:
        return result

    try:
        search_type = params['type']
    except:
        query = Query()
        # This code block executes the search for all the types of data
        query.query = {'query_string': {'query': search_term}}
        indices = ['antibody_approval', 'biosample', 'experiment', 'target']
        fields = ['@id', '@type']
        for index in indices:
            collection = root[index]
            result['columns'].update(collection.columns)
            for column in collection.columns:
                fields.append(column)

        query.fields = list(set(fields))
        # Should have some limit on size to have better
        s = es.search(query, index=indices, size=999999)
        antibody_count = biosample_count = experiment_count = target_count = 0
        for dataS in s['hits']['hits']:
            data_highlight = dataS['fields']
            if dataS['fields']['@type'][0] == 'antibody_approval':
                antibody_count = antibody_count + 1
            elif dataS['fields']['@type'][0] == 'biosample':
                biosample_count = biosample_count + 1
            elif dataS['fields']['@type'][0] == 'experiment':
                experiment_count = experiment_count + 1
            elif dataS['fields']['@type'][0] == 'target':
                target_count = target_count + 1
            result['count']['biosamples'] = biosample_count
            result['count']['antibodies'] = antibody_count
            result['count']['experiments'] = experiment_count
            result['count']['targets'] = target_count
            if 'highlight' in dataS:
                for key in dataS['highlight'].keys():
                    data_highlight['highlight'] = dataS['highlight'][key]
            else:
                data_highlight['highlight'] = []
            result['@graph'].append(data_highlight)
        return result
    else:
        collections = root.by_item_type
        fields = ['@id', '@type']
        for collection_name in collections:
            if search_type == root.by_item_type[collection_name].__name__:
                collection = root[collection_name]
                index = collection_name
                schema = collection.schema
                result['columns'] = columns = collection.columns
                break
        query = Query()
        for column in columns:
            fields.append(column)
        query.fields = fields
        query.query = {'query_string': {'query': search_term}}

        for facet in schema['facets']:
            face = {'terms': {'field': '', 'size': size}}
            face['terms']['field'] = schema['facets'][facet] + '.untouched'
            query.facets[facet] = face

        # Builds filtered query which supports multiple facet selection
        if len(params) > 2:
            for param in params:
                if param not in ['searchTerm', 'type']:
                    query['filter'] = {'term': {param + '.untouched': params[param]}}
        results = es.search(query, index=index, size=100)
        facet_results = results['facets']
        for facet in facet_results:
            face = {}
            face['field'] = schema['facets'][facet]
            face[facet] = []
            for term in facet_results[facet]['terms']:
                face[facet].append({term['term']: term['count']})
            if facet_results[facet]['missing'] != 0:
                face[facet].append({'other': facet_results[facet]['missing']})
            result['facets'].append(face)

        for dataS in results['hits']['hits']:
            data_highlight = dataS['fields']
            if 'highlight' in dataS:
                for key in dataS['highlight'].keys():
                    data_highlight['highlight'] = dataS['highlight'][key]
            else:
                data_highlight['highlight'] = []
            result['@graph'].append(data_highlight)

        result['count'][search_type] = results['hits']['total']
        return result
<commit_msg>Added limit for loading collection pages!<commit_after>
from pyramid.view import view_config
from ..contentbase import (
    Root
)
from pyelasticsearch import ElasticSearch
es = ElasticSearch('http://localhost:9200')

# Query class should be improved to accomodate filters and other ES functionality
class Query(dict):

    def __init__(self):
        self.query = dict()
        self.facets = dict()
        self.fields = []
        self.highlight = dict({'fields': {"*": {}}})

    def __setattr__(self, k, v):
        if k in self.keys():
            self[k] = v
        elif not hasattr(self, k):
            self[k] = v
        else:
            raise AttributeError("Cannot set '%s', cls attribute already exists" % (k, ))

    def __getattr__(self, k):
        if k in self.keys():
            return self[k]
        raise AttributeError

#FilteredQuery class is extended version of Query class
class FilteredQuery(dict):

    def __init__(self):
        self.query = dict({'filtered': {'query': {'queryString': {"query": ''}}, 'filter': {'and': {'filters': []}}}})
        self.facets = dict()
        self.fields = []
        self.highlight = dict({'fields': {"*": {}}})

    def __setattr__(self, k, v):
        if k in self.keys():
            self[k] = v
        elif not hasattr(self, k):
            self[k] = v
        else:
            raise AttributeError("Cannot set '%s', cls attribute already exists" % (k, ))

    def __getattr__(self, k):
        if k in self.keys():
            return self[k]
        raise AttributeError

    def __setterm__(self, v):
        self['query']['filtered']['query']['queryString']['query'] = v

    def __setfilter__(self, k, v):
        self['query']['filtered']['filter']['and']['filters'].append({'bool': {'must': {'term': {k: v}}}})

    def __setmissingfilter__(self, v):
        self['query']['filtered']['filter']['and']['filters'].append({'missing': {'field': v}})

@view_config(name='search', context=Root, request_method='GET', permission='search')
def search(context, request):
    ''' Search view connects to ElasticSearch and returns the results'''

    result = context.__json__(request)
    params = request.params
    root = request.root
    result.update({
        '@id': '/search/',
        '@type': ['search'],
        'title': 'Search ENCODE',
        'facets': [],
        '@graph': [],
        'columns': {},
        'count': {}
    })

    if 'limit' in params:
        size = 999999
    else:
        size = 100
    # if no searchTerm
    try:
        search_term = params['searchTerm']
    except:
        return result

    try:
        search_type = params['type']
    except:
        query = Query()
        # This code block executes the search for all the types of data
        query.query = {'query_string': {'query': search_term}}
        indices = ['antibody_approval', 'biosample', 'experiment', 'target']
        fields = ['@id', '@type']
        for index in indices:
            collection = root[index]
            result['columns'].update(collection.columns)
            for column in collection.columns:
                fields.append(column)

        query.fields = list(set(fields))
        # Should have some limit on size to have better
        s = es.search(query, index=indices, size=999999)
        antibody_count = biosample_count = experiment_count = target_count = 0
        for dataS in s['hits']['hits']:
            data_highlight = dataS['fields']
            if dataS['fields']['@type'][0] == 'antibody_approval':
                antibody_count = antibody_count + 1
            elif dataS['fields']['@type'][0] == 'biosample':
                biosample_count = biosample_count + 1
            elif dataS['fields']['@type'][0] == 'experiment':
                experiment_count = experiment_count + 1
            elif dataS['fields']['@type'][0] == 'target':
                target_count = target_count + 1
            result['count']['biosamples'] = biosample_count
            result['count']['antibodies'] = antibody_count
            result['count']['experiments'] = experiment_count
            result['count']['targets'] = target_count
            if 'highlight' in dataS:
                for key in dataS['highlight'].keys():
                    data_highlight['highlight'] = dataS['highlight'][key]
            else:
                data_highlight['highlight'] = []
            result['@graph'].append(data_highlight)
        return result
    else:
        collections = root.by_item_type
        fields = ['@id', '@type']
        for collection_name in collections:
            if search_type == root.by_item_type[collection_name].__name__:
                collection = root[collection_name]
                index = collection_name
                schema = collection.schema
                result['columns'] = columns = collection.columns
                break
        query = Query()
        for column in columns:
            fields.append(column)
        query.fields = fields
        query.query = {'query_string': {'query': search_term}}

        for facet in schema['facets']:
            face = {'terms': {'field': '', 'size': size}}
            face['terms']['field'] = schema['facets'][facet] + '.untouched'
            query.facets[facet] = face

        # Builds filtered query which supports multiple facet selection
        if len(params) > 2:
            for param in params:
                if param not in ['searchTerm', 'type', 'limit']:
                    query['filter'] = {'term': {param + '.untouched': params[param]}}
        results = es.search(query, index=index, size=size)
        facet_results = results['facets']
        for facet in facet_results:
            face = {}
            face['field'] = schema['facets'][facet]
            face[facet] = []
            for term in facet_results[facet]['terms']:
                face[facet].append({term['term']: term['count']})
            if facet_results[facet]['missing'] != 0:
                face[facet].append({'other': facet_results[facet]['missing']})
            result['facets'].append(face)

        for dataS in results['hits']['hits']:
            data_highlight = dataS['fields']
            if 'highlight' in dataS:
                for key in dataS['highlight'].keys():
                    data_highlight['highlight'] = dataS['highlight'][key]
            else:
                data_highlight['highlight'] = []
            result['@graph'].append(data_highlight)

        result['count'][search_type] = results['hits']['total']
        return result
<|endoftext|>


<commit_before>from django.db import models
from .. import *
#from ..database.coursecatalog import CourseCatalog

class Schedule(models.Model):
    # Stuff stored by the Schedule
    lectureList = models.ManyToManyField(Lecture, null=True, blank=False, symmetrical=False)
    tutorialList = models.ManyToManyField(Tutorial, null=True, blank=False, symmetrical=False)
    labList = models.ManyToManyField(Lab, null=True, blank=False, symmetrical=False)
    semester = models.CharField(max_length=120, default="Fall")
    year = models.IntegerField(default=1)

    # Returns list of List of Lecture, Tutorials and Lab
    # To Use in front end: iterate over each Type, then within each type, each section
    # for each section, access its event. Event has all that's needed for display. See Event model for description
    def view_schedule(self):

        returnlist = []
        returnlist.append(self.lectureList.all())
        returnlist.append(self.tutorialList.all())
        returnlist.append(self.labList.all())
        return returnlist

    # Adds a Lecture/Tutorial/Lab to the schedule. Must do Type Inference to store correctly
    def add_course(self, subcourse_item):

        # if Lecture, must mean there isn't any lab or tutorial associated with it
        if CourseCatalog.typeofSection(subcourse_item) == "Lecture":
            self.lectureList.add(subcourse_item)

        # if Tutorial, must add tutorial and its Lecture
        if CourseCatalog.typeofSection(subcourse_item) == "Tutorial":
            self.tutorialList.add(subcourse_item)
            self.lectureList.add(subcourse_item.lecture)

        # if Lab, must add Lecture. Then must check if there are tutorials to add as well.
        if CourseCatalog.typeofSection(subcourse_item) == "Lab":
            self.lectureList.add(subcourse_item.lecture)
            self.labList.add(subcourse_item)

            if subcourse_item.tutorial is not None:
                self.tutorialList.add(subcourse_item.tutorial)

    # Removes an Item from the schedule, must provide lowest possible section Lecture > Tutorial > Lab
    def remove_item(self, subcourse_item):

        # if Lecture, must mean there isn't any lab or tutorial associated with it
        if CourseCatalog.typeofSection(subcourse_item) == "Lecture":
            self.lectureList.remove(subcourse_item)

        # if Tutorial, must add tutorial and its Lecture
        if CourseCatalog.typeofSection(subcourse_item) == "Tutorial":
            self.tutorialList.remove(subcourse_item)
            self.lectureList.remove(subcourse_item.lecture)

        # if Lab, must add Lecture. Then must check if there are tutorials to add as well.
        if CourseCatalog.typeofSection(subcourse_item) == "Lab":
            self.lectureList.remove(subcourse_item.lecture)
            self.labList.remove(subcourse_item)

            if subcourse_item.tutorial is not None:
                self.tutorialList.remove(subcourse_item.tutorial)

    class Meta:
        app_label = 'app'
        managed= True

# End class Schedule<commit_msg>EC - 46: Refactored use of CourseCatalog to check for subcourse item type to use .name() fucntions.<commit_after>from django.db import models
from .. import *
#from ..database.coursecatalog import CourseCatalog

class Schedule(models.Model):
    # Stuff stored by the Schedule
    lectureList = models.ManyToManyField(Lecture, null=True, blank=False, symmetrical=False)
    tutorialList = models.ManyToManyField(Tutorial, null=True, blank=False, symmetrical=False)
    labList = models.ManyToManyField(Lab, null=True, blank=False, symmetrical=False)
    semester = models.CharField(max_length=120, default="Fall")
    year = models.IntegerField(default=1)

    # Returns list of List of Lecture, Tutorials and Lab
    # To Use in front end: iterate over each Type, then within each type, each section
    # for each section, access its event. Event has all that's needed for display. See Event model for description
    def view_schedule(self):

        returnlist = []
        returnlist.append(self.lectureList.all())
        returnlist.append(self.tutorialList.all())
        returnlist.append(self.labList.all())
        return returnlist

    # Adds a Lecture/Tutorial/Lab to the schedule. Must do Type Inference to store correctly
    def add_course(self, subcourse_item):

        # if Lecture, must mean there isn't any lab or tutorial associated with it
        if subcourse_item.name() == "Lecture":
            self.lectureList.add(subcourse_item)

        # if Tutorial, must add tutorial and its Lecture
        if subcourse_item.name() == "Tutorial":
            self.tutorialList.add(subcourse_item)
            self.lectureList.add(subcourse_item.lecture)

        # if Lab, must add Lecture. Then must check if there are tutorials to add as well.
        if subcourse_item.name() == "Lab":
            self.lectureList.add(subcourse_item.lecture)
            self.labList.add(subcourse_item)

            if subcourse_item.tutorial is not None:
                self.tutorialList.add(subcourse_item.tutorial)

    # Removes an Item from the schedule, must provide lowest possible section Lecture > Tutorial > Lab
    def remove_item(self, subcourse_item):

        # if Lecture, must mean there isn't any lab or tutorial associated with it
        if subcourse_item.name() == "Lecture":
            self.lectureList.remove(subcourse_item)

        # if Tutorial, must add tutorial and its Lecture
        if subcourse_item.name() == "Tutorial":
            self.tutorialList.remove(subcourse_item)
            self.lectureList.remove(subcourse_item.lecture)

        # if Lab, must add Lecture. Then must check if there are tutorials to add as well.
        if subcourse_item.name() == "Lab":
            self.lectureList.remove(subcourse_item.lecture)
            self.labList.remove(subcourse_item)

            if subcourse_item.tutorial is not None:
                self.tutorialList.remove(subcourse_item.tutorial)

    class Meta:
        app_label = 'app'
        managed = True

# End class Schedule<|endoftext|>


<commit_before># -*- encoding: utf-8 -*-

import logging
from datetime import datetime, timedelta
from openerp.osv import orm, fields

_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)

class ResPartner(orm.Model):
    _inherit = 'res.partner'

    def _check_vat_readonly(self, cr, uid, ids, field_name, arg, context):
        """
            this method reads from system parameters the value from the key 'vatcf_minutes_amount_readonly'
            if the value exists uses the minutes given to calculate if the vat must be in readonly mode
            so the user can't modify the value of VAT
            if the user has a group 'Configuration' he CAN always modifies the value of the VAT

        """
        results = {}

        # return the value or False
        minutes_str = self.pool['ir.config_parameter'].get_param(cr, uid, 'vatcf_minutes_amount_readonly')

        if minutes_str:
            minutes_amount = int(minutes_str)
        else:
            minutes_amount = 0

        # boolean True or False
        has_group = self.pool['res.users'].has_group(cr, uid, 'base.group_system')

        for partner in self.browse(cr, uid, ids, context):

            results[partner.id] = False

            if minutes_amount and minutes_amount > 0:
                time_right_now = datetime.now()
                time_from_last_update = partner.write_date and (datetime.strptime(partner.write_date, '%Y-%m-%d %H:%M:%S') + timedelta(minutes=minutes_amount)) or time_right_now
                delta_left = time_from_last_update - time_right_now
                seconds_left = int(delta_left.total_seconds())

                if seconds_left > 0:
                    results[partner.id] = True

            if has_group:
                results[partner.id] = False

        return results

    _columns = {
        'vat_readonly': fields.function(_check_vat_readonly, method=True, type='boolean'),
        'write_date': fields.datetime(string='Write Date')
    }
<commit_msg>[partner_vat_and_cf_readonly] change write_date to create_date<commit_after># -*- encoding: utf-8 -*-

import logging
from datetime import datetime, timedelta
from openerp.osv import orm, fields

_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)

class ResPartner(orm.Model):
    _inherit = 'res.partner'

    def _check_vat_readonly(self, cr, uid, ids, field_name, arg, context):
        """
            this method reads from system parameters the value from the key 'vatcf_minutes_amount_readonly'
            if the value exists uses the minutes given to calculate if the vat must be in readonly mode
            so the user can't modify the value of VAT
            if the user has a group 'Configuration' he CAN always modifies the value of the VAT

        """
        results = {}

        # return the value or False
        minutes_str = self.pool['ir.config_parameter'].get_param(cr, uid, 'vatcf_minutes_amount_readonly')

        if minutes_str:
            minutes_amount = int(minutes_str)
        else:
            minutes_amount = 0

        # boolean True or False
        has_group = self.pool['res.users'].has_group(cr, uid, 'base.group_system')

        for partner in self.browse(cr, uid, ids, context):

            results[partner.id] = True

            if minutes_amount and minutes_amount > 0:
                time_right_now = datetime.now()
                time_from_last_update = partner.write_date and (datetime.strptime(partner.write_date, '%Y-%m-%d %H:%M:%S') + timedelta(minutes=minutes_amount)) or time_right_now
                delta_left = time_from_last_update - time_right_now
                seconds_left = int(delta_left.total_seconds())

                if seconds_left > 0:
                    results[partner.id] = False

            if has_group:
                results[partner.id] = False

        return results

    _columns = {
        'vat_readonly': fields.function(_check_vat_readonly, method=True, type='boolean'),
        'write_date': fields.datetime(string='Write Date')
    }
<|endoftext|>


<commit_before>import os
import mimetypes

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

from django.conf import settings
from django.core.files.base import File
from django.core.files.storage import Storage
from django.core.exceptions import ImproperlyConfigured

try:
    from S3 import AWSAuthConnection, QueryStringAuthGenerator, CallingFormat
except ImportError:
    raise ImproperlyConfigured, "Could not load amazon's S3 bindings.\
    \nSee http://developer.amazonwebservices.com/connect/entry.jspa?externalID=134"

ACCESS_KEY_NAME     = getattr(settings, 'AWS_ACCESS_KEY_ID', None)
SECRET_KEY_NAME     = getattr(settings, 'AWS_SECRET_ACCESS_KEY', None)
HEADERS             = getattr(settings, 'AWS_HEADERS', {})
DEFAULT_ACL         = getattr(settings, 'AWS_DEFAULT_ACL', 'public-read')
QUERYSTRING_ACTIVE  = getattr(settings, 'AWS_QUERYSTRING_ACTIVE', False)
QUERYSTRING_EXPIRE  = getattr(settings, 'AWS_QUERYSTRING_EXPIRE', 60)
SECURE_URLS         = getattr(settings, 'AWS_S3_SECURE_URLS', False)
BUCKET_PREFIX       = getattr(settings, 'AWS_BUCKET_PREFIX', '')
CALLING_FORMAT      = getattr(settings, 'AWS_CALLING_FORMAT', CallingFormat.PATH)

IS_GZIPPED          = getattr(settings, 'AWS_IS_GZIPPED', False)
GZIP_CONTENT_TYPES  = getattr(settings, 'GZIP_CONTENT_TYPES', (
    'text/css',
    'application/javascript',
    'application/x-javascript'
))

if IS_GZIPPED:
    from gzip import GzipFile

class S3Storage(Storage):
    """Amazon Simple Storage Service"""

    def __init__(self, bucket=settings.AWS_STORAGE_BUCKET_NAME,
            access_key=None, secret_key=None, acl=DEFAULT_ACL,
            calling_format=CALLING_FORMAT, encrypt=False,
            gzip=IS_GZIPPED, gzip_content_types=GZIP_CONTENT_TYPES):
        self.bucket = bucket
        self.acl = acl
        self.encrypt = encrypt
        self.gzip = gzip
        self.gzip_content_types = gzip_content_types

        if encrypt:
            try:
                import ezPyCrypto
            except ImportError:
                raise ImproperlyConfigured, "Could not load ezPyCrypto.\
                \nSee http://www.freenet.org.nz/ezPyCrypto/ to install it."
            self.crypto_key = ezPyCrypto.key

        if not access_key and not secret_key:
            access_key, secret_key = self._get_access_keys()

        self.connection = AWSAuthConnection(access_key, secret_key,
                            calling_format=calling_format)
        self.generator = QueryStringAuthGenerator(access_key, secret_key,
                            calling_format=calling_format,
                            is_secure=SECURE_URLS)
        self.generator.set_expires_in(QUERYSTRING_EXPIRE)

        self.headers = HEADERS

    def _get_access_keys(self):
        access_key = ACCESS_KEY_NAME
        secret_key = SECRET_KEY_NAME
        if (access_key or secret_key) and (not access_key or not secret_key):
            access_key = os.environ.get(ACCESS_KEY_NAME)
            secret_key = os.environ.get(SECRET_KEY_NAME)

        if access_key and secret_key:
            # Both were provided, so use them
            return access_key, secret_key

        return None, None

    def _get_connection(self):
        return AWSAuthConnection(*self._get_access_keys())

    def _clean_name(self, name):
        # Useful for windows' paths
        return os.path.join(BUCKET_PREFIX, os.path.normpath(name).replace('\\', '/'))

    def _compress_string(self, s):
        """Gzip a given string."""
        zbuf = StringIO()
        zfile = GzipFile(mode='wb', compresslevel=6, fileobj=zbuf)
        zfile.write(s)
        zfile.close()
        return zbuf.getvalue()

    def _put_file(self, name, content):
        if self.encrypt:

            # Create a key object
            key = self.crypto_key()

            # Read in a public key
            fd = open(settings.CRYPTO_KEYS_PUBLIC, "rb")
            public_key = fd.read()
            fd.close()

            # import this public key
            key.importKey(public_key)

            # Now encrypt some text against this public key
            content = key.encString(content)

        content_type = mimetypes.guess_type(name)[0] or "application/x-octet-stream"

        if self.gzip and content_type in self.gzip_content_types:
            content = self._compress_string(content)
            self.headers.update({'Content-Encoding': 'gzip'})

        self.headers.update({
            'x-amz-acl': self.acl,
            'Content-Type': content_type,
            'Content-Length' : len(content),
        })
        response = self.connection.put(self.bucket, name, content, self.headers)
        if response.http_response.status not in (200, 206):
            raise IOError("S3StorageError: %s" % response.message)

    def _open(self, name, mode='rb'):
        name = self._clean_name(name)
        remote_file = S3StorageFile(name, self, mode=mode)
        return remote_file

    def _read(self, name, start_range=None, end_range=None):
        name = self._clean_name(name)
        if start_range is None:
            headers = {}
        else:
            headers = {'Range': 'bytes=%s-%s' % (start_range, end_range)}
        response = self.connection.get(self.bucket, name, headers)
        if response.http_response.status not in (200, 206):
            raise IOError("S3StorageError: %s" % response.message)
        headers = response.http_response.msg

        if self.encrypt:
            # Read in a private key
            fd = open(settings.CRYPTO_KEYS_PRIVATE, "rb")
            private_key = fd.read()
            fd.close()

            # Create a key object, and auto-import private key
            key = self.crypto_key(private_key)

            # Decrypt this file
            response.object.data = key.decString(response.object.data)

        return response.object.data, headers.get('etag', None), headers.get('content-range', None)

    def _save(self, name, content):
        name = self._clean_name(name)
        content.open()
        if hasattr(content, 'chunks'):
            content_str = ''.join(chunk for chunk in content.chunks())
        else:
            content_str = content.read()
        self._put_file(name, content_str)
        return name

    def delete(self, name):
        name = self._clean_name(name)
        response = self.connection.delete(self.bucket, name)
        if response.http_response.status != 204:
            raise IOError("S3StorageError: %s" % response.message)

    def exists(self, name):
        name = self._clean_name(name)
        response = self.connection._make_request('HEAD', self.bucket, name)
        return response.status == 200

    def size(self, name):
        name = self._clean_name(name)
        response = self.connection._make_request('HEAD', self.bucket, name)
        content_length = response.getheader('Content-Length')
        return content_length and int(content_length) or 0

    def url(self, name):
        name = self._clean_name(name)
        if QUERYSTRING_ACTIVE:
            return self.generator.generate_url('GET', self.bucket, name)
        else:
            return self.generator.make_bare_url(self.bucket, name)

    ## UNCOMMENT BELOW IF NECESSARY
    #def get_available_name(self, name):
    #    """ Overwrite existing file with the same name. """
    #    name = self._clean_name(name)
    #    return name

class S3StorageFile(File):
    def __init__(self, name, storage, mode):
        self._name = name
        self._storage = storage
        self._mode = mode
        self._is_dirty = False
        self.file = StringIO()
        self.start_range = 0

    @property
    def size(self):
        if not hasattr(self, '_size'):
            self._size = self._storage.size(self._name)
        return self._size

    def read(self, num_bytes=None):
        if num_bytes is None:
            args = []
            self.start_range = 0
        else:
            args = [self.start_range, self.start_range+num_bytes-1]
        data, etags, content_range = self._storage._read(self._name, *args)
        if content_range is not None:
            current_range, size = content_range.split(' ', 1)[1].split('/', 1)
            start_range, end_range = current_range.split('-', 1)
            self._size, self.start_range = int(size), int(end_range)+1
        self.file = StringIO(data)
        return self.file.getvalue()

    def write(self, content):
        if 'w' not in self._mode:
            raise AttributeError("File was opened for read-only access.")
        self.file = StringIO(content)
        self._is_dirty = True

    def close(self):
        if self._is_dirty:
            self._storage._put_file(self._name, self.file.getvalue())
        self.file.close()
<commit_msg>Minor cosmetic changes.<commit_after>import os
import mimetypes

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

from django.conf import settings
from django.core.files.base import File
from django.core.files.storage import Storage
from django.core.exceptions import ImproperlyConfigured

try:
    from S3 import AWSAuthConnection, QueryStringAuthGenerator, CallingFormat
except ImportError:
    raise ImproperlyConfigured("Could not load amazon's S3 bindings.\nSee "
        "http://developer.amazonwebservices.com/connect/entry.jspa?externalID=134")

ACCESS_KEY_NAME     = getattr(settings, 'AWS_ACCESS_KEY_ID', None)
SECRET_KEY_NAME     = getattr(settings, 'AWS_SECRET_ACCESS_KEY', None)
HEADERS             = getattr(settings, 'AWS_HEADERS', {})
DEFAULT_ACL         = getattr(settings, 'AWS_DEFAULT_ACL', 'public-read')
QUERYSTRING_ACTIVE  = getattr(settings, 'AWS_QUERYSTRING_ACTIVE', False)
QUERYSTRING_EXPIRE  = getattr(settings, 'AWS_QUERYSTRING_EXPIRE', 60)
SECURE_URLS         = getattr(settings, 'AWS_S3_SECURE_URLS', False)
BUCKET_PREFIX       = getattr(settings, 'AWS_BUCKET_PREFIX', '')
CALLING_FORMAT      = getattr(settings, 'AWS_CALLING_FORMAT', CallingFormat.PATH)

IS_GZIPPED          = getattr(settings, 'AWS_IS_GZIPPED', False)
GZIP_CONTENT_TYPES  = getattr(settings, 'GZIP_CONTENT_TYPES', (
    'text/css',
    'application/javascript',
    'application/x-javascript'
))

if IS_GZIPPED:
    from gzip import GzipFile

class S3Storage(Storage):
    """Amazon Simple Storage Service"""

    def __init__(self, bucket=settings.AWS_STORAGE_BUCKET_NAME,
            access_key=None, secret_key=None, acl=DEFAULT_ACL,
            calling_format=CALLING_FORMAT, encrypt=False,
            gzip=IS_GZIPPED, gzip_content_types=GZIP_CONTENT_TYPES):
        self.bucket = bucket
        self.acl = acl
        self.encrypt = encrypt
        self.gzip = gzip
        self.gzip_content_types = gzip_content_types

        if encrypt:
            try:
                import ezPyCrypto
            except ImportError:
                raise ImproperlyConfigured("Could not load ezPyCrypto.\nSee "
                    "http://www.freenet.org.nz/ezPyCrypto/ to install it.")
            self.crypto_key = ezPyCrypto.key

        if not access_key and not secret_key:
            access_key, secret_key = self._get_access_keys()

        self.connection = AWSAuthConnection(access_key, secret_key,
                            calling_format=calling_format)
        self.generator = QueryStringAuthGenerator(access_key, secret_key,
                            calling_format=calling_format,
                            is_secure=SECURE_URLS)
        self.generator.set_expires_in(QUERYSTRING_EXPIRE)

        self.headers = HEADERS

    def _get_access_keys(self):
        access_key = ACCESS_KEY_NAME
        secret_key = SECRET_KEY_NAME
        if (access_key or secret_key) and (not access_key or not secret_key):
            access_key = os.environ.get(ACCESS_KEY_NAME)
            secret_key = os.environ.get(SECRET_KEY_NAME)

        if access_key and secret_key:
            # Both were provided, so use them
            return access_key, secret_key

        return None, None

    def _get_connection(self):
        return AWSAuthConnection(*self._get_access_keys())

    def _clean_name(self, name):
        # Useful for windows' paths
        return os.path.join(BUCKET_PREFIX, os.path.normpath(name).replace('\\', '/'))

    def _compress_string(self, s):
        """Gzip a given string."""
        zbuf = StringIO()
        zfile = GzipFile(mode='wb', compresslevel=6, fileobj=zbuf)
        zfile.write(s)
        zfile.close()
        return zbuf.getvalue()

    def _put_file(self, name, content):
        if self.encrypt:

            # Create a key object
            key = self.crypto_key()

            # Read in a public key
            fd = open(settings.CRYPTO_KEYS_PUBLIC, "rb")
            public_key = fd.read()
            fd.close()

            # import this public key
            key.importKey(public_key)

            # Now encrypt some text against this public key
            content = key.encString(content)

        content_type = mimetypes.guess_type(name)[0] or "application/x-octet-stream"

        if self.gzip and content_type in self.gzip_content_types:
            content = self._compress_string(content)
            self.headers.update({'Content-Encoding': 'gzip'})

        self.headers.update({
            'x-amz-acl': self.acl,
            'Content-Type': content_type,
            'Content-Length' : len(content),
        })
        response = self.connection.put(self.bucket, name, content, self.headers)
        if response.http_response.status not in (200, 206):
            raise IOError("S3StorageError: %s" % response.message)

    def _open(self, name, mode='rb'):
        name = self._clean_name(name)
        remote_file = S3StorageFile(name, self, mode=mode)
        return remote_file

    def _read(self, name, start_range=None, end_range=None):
        name = self._clean_name(name)
        if start_range is None:
            headers = {}
        else:
            headers = {'Range': 'bytes=%s-%s' % (start_range, end_range)}
        response = self.connection.get(self.bucket, name, headers)
        if response.http_response.status not in (200, 206):
            raise IOError("S3StorageError: %s" % response.message)
        headers = response.http_response.msg

        if self.encrypt:
            # Read in a private key
            fd = open(settings.CRYPTO_KEYS_PRIVATE, "rb")
            private_key = fd.read()
            fd.close()

            # Create a key object, and auto-import private key
            key = self.crypto_key(private_key)

            # Decrypt this file
            response.object.data = key.decString(response.object.data)

        return response.object.data, headers.get('etag', None), headers.get('content-range', None)

    def _save(self, name, content):
        name = self._clean_name(name)
        content.open()
        if hasattr(content, 'chunks'):
            content_str = ''.join(chunk for chunk in content.chunks())
        else:
            content_str = content.read()
        self._put_file(name, content_str)
        return name

    def delete(self, name):
        name = self._clean_name(name)
        response = self.connection.delete(self.bucket, name)
        if response.http_response.status != 204:
            raise IOError("S3StorageError: %s" % response.message)

    def exists(self, name):
        name = self._clean_name(name)
        response = self.connection._make_request('HEAD', self.bucket, name)
        return response.status == 200

    def size(self, name):
        name = self._clean_name(name)
        response = self.connection._make_request('HEAD', self.bucket, name)
        content_length = response.getheader('Content-Length')
        return content_length and int(content_length) or 0

    def url(self, name):
        name = self._clean_name(name)
        if QUERYSTRING_ACTIVE:
            return self.generator.generate_url('GET', self.bucket, name)
        else:
            return self.generator.make_bare_url(self.bucket, name)

    ## UNCOMMENT BELOW IF NECESSARY
    #def get_available_name(self, name):
    #    """ Overwrite existing file with the same name. """
    #    name = self._clean_name(name)
    #    return name

class S3StorageFile(File):
    def __init__(self, name, storage, mode):
        self._name = name
        self._storage = storage
        self._mode = mode
        self._is_dirty = False
        self.file = StringIO()
        self.start_range = 0

    @property
    def size(self):
        if not hasattr(self, '_size'):
            self._size = self._storage.size(self._name)
        return self._size

    def read(self, num_bytes=None):
        if num_bytes is None:
            args = []
            self.start_range = 0
        else:
            args = [self.start_range, self.start_range+num_bytes-1]
        data, etags, content_range = self._storage._read(self._name, *args)
        if content_range is not None:
            current_range, size = content_range.split(' ', 1)[1].split('/', 1)
            start_range, end_range = current_range.split('-', 1)
            self._size, self.start_range = int(size), int(end_range)+1
        self.file = StringIO(data)
        return self.file.getvalue()

    def write(self, content):
        if 'w' not in self._mode:
            raise AttributeError("File was opened for read-only access.")
        self.file = StringIO(content)
        self._is_dirty = True

    def close(self):
        if self._is_dirty:
            self._storage._put_file(self._name, self.file.getvalue())
        self.file.close()
<|endoftext|>


<commit_before>#TODO Add intepreter here
#@Author Sean McLaughlin
desc ='''
This module is the first I'm writing for my project for the winter quarter of 2016 in Aaron Roodman's group.
This is built on top of previous work conducted by Aaron and his graduate student Chris Davis. They have
developed WavefrontPSF, which estimates the optical contribution of the PSF with a relatively simple model.
It was found that the optical portion was not as signifcant a fraction of the total PSF as hoped,
so some sort of afterburner is going to need to be added. After the optical portion has been deconvolved
from the observed stars (using Richardson-Lucy deconvolution), the remainder will be treated as the "atmospheric"
portion of the psf. This module load in preprocessed observed stars, run WavefrontPSF on them, deconvolve
the optical PSF, then run PSFEX (a packaged PSF modeler) on the residual.

TODO Details on actually running the module.
'''

from argparse import ArgumentParser
parser = ArgumentParser(description = desc)

parser.add_argument('expid', metavar = 'expid', type = int, help =\
                    'ID of the exposure to analyze')

args = vars(parser.parse_args())

import numpy as np
from itertools import izip
from optical_model import getOpticalPSF
from glob import glob
from astropy.io import fits #TODO check if I should support pyfits
from lucy import deconvolve

#get optical PSF
optPSFStamps, full_cat = getOpticalPSF(args['expid'])

vignettes = np.zeros((optPSFStamps.shape[0], 32,32))
i=0
#TODO See if this is slow and optomize
for rec_arr in full_cat:
    for v in rec_arr['VIGNET']:
        #TODO Check for off by one errors and centering.
        #Slice 63x63 down to 32x32 so deconv will work.
        #TODO Turn sliced off pixels into background estimate
        vignettes[i] = v[15:47, 15:47]
        i+=1

for optPSFStamp, vignette in izip(optPSFStamps, vignettes):
    aptPSFEst,diffs,psiByIter,chi2ByIter = deconvolve(optPSFStamp,vignette,psi_0=None,mask=None,mu0=6e3,convergence=1.0e-3,chi2Level=0.,niterations=50, extra= True)
    break

from matplotlib import pyplot as plt
from matplotlib.colors import LogNorm

plt.subplot(1,3,1)
plt.title('Original')
plt.imshow(vignette,interpolation='none',origin='lower',cmap='gray',norm=LogNorm(vmin=1.0e-4, vmax=1.0))
plt.subplot(1,3, 2)
plt.title('Optical')
plt.imshow(optPSFStamp,interpolation='none',origin='lower',cmap='gray',norm=LogNorm(vmin=1.0e-4, vmax=1.0))
plt.subplot(1,3,3)
plt.title('Remainder')
plt.imshow(aptPSFEst,interpolation='none',origin='lower',cmap='gray',norm=LogNorm(vmin=1.0e-4, vmax=1.0))

plt.show()

f2,ax2Arr = plt.subplots(1,10)
for i in xrange(10):
    ax2Arr[i].imshow(psiByIter[i],interpolation='none',origin='lower',cmap='gray',norm=LogNorm(vmin=1.0e-4, vmax=1.0))
f2.show()
<commit_msg>Issues with plotting.<commit_after>#TODO Add intepreter here
#@Author Sean McLaughlin
desc ='''
This module is the first I'm writing for my project for the winter quarter of 2016 in Aaron Roodman's group.
This is built on top of previous work conducted by Aaron and his graduate student Chris Davis. They have
developed WavefrontPSF, which estimates the optical contribution of the PSF with a relatively simple model.
It was found that the optical portion was not as signifcant a fraction of the total PSF as hoped,
so some sort of afterburner is going to need to be added. After the optical portion has been deconvolved
from the observed stars (using Richardson-Lucy deconvolution), the remainder will be treated as the "atmospheric"
portion of the psf. This module load in preprocessed observed stars, run WavefrontPSF on them, deconvolve
the optical PSF, then run PSFEX (a packaged PSF modeler) on the residual.

TODO Details on actually running the module.
'''

from argparse import ArgumentParser
parser = ArgumentParser(description = desc)

parser.add_argument('expid', metavar = 'expid', type = int, help =\
                    'ID of the exposure to analyze')

args = vars(parser.parse_args())

import numpy as np
from itertools import izip
from optical_model import getOpticalPSF
from glob import glob
from astropy.io import fits #TODO check if I should support pyfits
from lucy import deconvolve

#get optical PSF
optPSFStamps, full_cat = getOpticalPSF(args['expid'])

vignettes = np.zeros((optPSFStamps.shape[0], 32,32))
i=0
#TODO See if this is slow and optomize
for rec_arr in full_cat:
    for v in rec_arr['VIGNET']:
        #TODO Check for off by one errors and centering.
        #Slice 63x63 down to 32x32 so deconv will work.
        #TODO Turn sliced off pixels into background estimate
        vignettes[i] = v[15:47, 15:47]
        i+=1

for optPSFStamp, vignette in izip(optPSFStamps, vignettes):
    aptPSFEst,diffs,psiByIter,chi2ByIter = deconvolve(optPSFStamp,vignette,psi_0=None,mask=None,mu0=6e3,convergence=1e-3,chi2Level=0.,niterations=50, extra= True)
    break

from matplotlib import pyplot as plt
from matplotlib.colors import LogNorm

plt.subplot(1,3,1)
plt.title('Original')
plt.imshow(vignette,interpolation='none',origin='lower',cmap='gray',norm=LogNorm(vmin=1.0e-4, vmax=1.0))
plt.subplot(1,3, 2)
plt.title('Optical')
plt.imshow(optPSFStamp,interpolation='none',origin='lower',cmap='gray',norm=LogNorm(vmin=1.0e-4, vmax=1.0))
plt.subplot(1,3,3)
plt.title('Remainder')
plt.imshow(aptPSFEst,interpolation='none',origin='lower',cmap='gray',norm=LogNorm(vmin=1.0e-4, vmax=1.0))

plt.show()

f2,ax2Arr = plt.subplots(1,10)
print len(ax2Arr), len(psiByIter)
for i in xrange(10):
    ax2Arr[i].imshow(psiByIter[i],interpolation='none',origin='lower',cmap='gray',norm=LogNorm(vmin=1.0e-4, vmax=1.0))
f2.show()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# The MIT License (MIT)
#
# Copyright (c) 2013 ton1517 <tonton1517@gmail.com>
#
# 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.

"""
randfilter

Usage:
    randfilter (-n <num> | -p <probability>) [-u | --unorder] [-i | --ignore-empty] [<files>...]
    randfilter -h | --help
    randfilter -v | --version

Options:
    <files>...          Choose and output lines at random in files. If omitted, use stdin.
    -n <num>            The choise number of lines.
    -p <probability>    The choise probability of lines. The value is 0.0 to 1.0.
    -u --unorder        Output lines are unordered.
    -i --ignore-empty   Ignore empty line.
    -h --help           Show this screen.
    -v --version        Show version.
"""

import sys
import itertools
from random import random, sample

from docopt import docopt
from schema import Schema, SchemaError, And, Or, Use

#=======================================
# config
#=======================================

NAME = 'randfilter'
VERSION = '0.0.1'
LICENSE = 'MIT License'
DESCRIPTION = ''
URL = 'https://github.com/ton1517/randfilter'
AUTHOR = 'ton1517'
AUTHOR_EMAIL = 'tonton1517@gmail.com'

#=======================================
# functions
#=======================================

def iter_files(opened_files, ignore_empty = False):
    """return generator includes all file's lines."""

    it = itertools.chain(*opened_files)

    for line in it:
        if ignore_empty and line.strip() == "":
            continue

        yield line

def validate_args(args):
    """validate arguments."""

    schema = Schema({
        '-n': Or(None, And(Use(int), lambda n: 0 <= n), error="-n should be positive integer"),
        '-p': Or(None, And(Use(float), lambda n: 0.0 <= n <= 1.0), error="-p should be float 0 <= N <= 1.0"),
        '--unorder': bool,
        '--ignore-empty': bool,
        '--help': bool,
        '--version': bool,
        '<files>': [Use(open, error="Files should be readable")]
    })

    args = schema.validate(args)

    if len(args["<files>"]) == 0:
        args["<files>"].append(sys.stdin)

    return args

#=======================================
# random functions
#=======================================

def choose_random_lines_probability(files_iter, probability, unorder):
    lines = [line for line in files_iter if random() < probability]

    if unorder:
        random.shuffle(lines)

    return lines

def choose_random_lines_num(files_iter, num, unorder):
    lines = [line for line in files_iter]
    length = len(lines)
    num = length if num > length else num
    line_nums = sample(range(length), num)

    if not unorder:
        line_nums.sort()

    return [lines[i] for i in line_nums]

#=======================================
# main
#=======================================

def main():
    args = docopt(__doc__, version="{0} {1}".format(NAME, VERSION))

    try:
        args = validate_args(args)
    except SchemaError as e:
        print(e)
        sys.exit(1)

if __name__ == "__main__":
    main()
<commit_msg>add main.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# The MIT License (MIT)
#
# Copyright (c) 2013 ton1517 <tonton1517@gmail.com>
#
# 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.

"""
randfilter

Usage:
    randfilter (-n <num> | -p <probability>) [-u | --unorder] [-i | --ignore-empty] [<files>...]
    randfilter -h | --help
    randfilter -v | --version

Options:
    <files>...          Choose and output lines at random in files. If omitted, use stdin.
    -n <num>            The choise number of lines.
    -p <probability>    The choise probability of lines. The value is 0.0 to 1.0.
    -u --unorder        Output lines are unordered.
    -i --ignore-empty   Ignore empty line.
    -h --help           Show this screen.
    -v --version        Show version.
"""

from __future__ import print_function
import sys
import itertools
from random import random, sample

from docopt import docopt
from schema import Schema, SchemaError, And, Or, Use

#=======================================
# config
#=======================================

NAME = 'randfilter'
VERSION = '0.0.1'
LICENSE = 'MIT License'
DESCRIPTION = ''
URL = 'https://github.com/ton1517/randfilter'
AUTHOR = 'ton1517'
AUTHOR_EMAIL = 'tonton1517@gmail.com'

#=======================================
# functions
#=======================================

def iter_files(opened_files, ignore_empty = False):
    """return generator includes all file's lines."""

    it = itertools.chain(*opened_files)

    for line in it:
        if ignore_empty and line.strip() == "":
            continue

        yield line

def validate_args(args):
    """validate arguments."""

    schema = Schema({
        '-n': Or(None, And(Use(int), lambda n: 0 <= n), error="-n should be positive integer"),
        '-p': Or(None, And(Use(float), lambda n: 0.0 <= n <= 1.0), error="-p should be float 0 <= N <= 1.0"),
        '--unorder': bool,
        '--ignore-empty': bool,
        '--help': bool,
        '--version': bool,
        '<files>': [Use(open, error="Files should be readable")]
    })

    args = schema.validate(args)

    if len(args["<files>"]) == 0:
        args["<files>"].append(sys.stdin)

    return args

#=======================================
# random functions
#=======================================

def choose_random_lines_probability(files_iter, probability, unorder):
    lines = [line for line in files_iter if random() < probability]

    if unorder:
        random.shuffle(lines)

    return lines

def choose_random_lines_num(files_iter, num, unorder):
    lines = [line for line in files_iter]
    length = len(lines)
    num = length if num > length else num
    line_nums = sample(range(length), num)

    if not unorder:
        line_nums.sort()

    return [lines[i] for i in line_nums]

#=======================================
# main
#=======================================

def main():
    args = docopt(__doc__, version="{0} {1}".format(NAME, VERSION))

    try:
        args = validate_args(args)
    except SchemaError as e:
        print(e)
        sys.exit(1)

    it = iter_files(args["<files>"], args["--ignore-empty"])

    if not args["-n"] is None:
        lines = choose_random_lines_num(it, args["-n"], args["--unorder"])
    else:
        lines = choose_random_lines_probability(it, args["-p"], args["--unorder"])

    print("".join(lines), end="")

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before>#!/bin/env python2
# -*- coding: utf-8 -*-
#
# This file is part of the VecNet Zika modeling interface.
# For copyright and licensing information about this package, see the
# NOTICE.txt and LICENSE.txt files in its top-level directory; they are
# available at https://github.com/vecnet/zika
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License (MPL), version 2.0.  If a copy of the MPL was not distributed
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

from django.contrib import admin

from website.apps.home.models import Location, Simulation, Data, SimulationModel, Totals

admin.site.register(Location)
admin.site.register(Simulation)
admin.site.register(Data)
admin.site.register(Totals)
admin.site.register(SimulationModel)
<commit_msg>Admin panel improvements<commit_after>#!/bin/env python2
# -*- coding: utf-8 -*-
#
# This file is part of the VecNet Zika modeling interface.
# For copyright and licensing information about this package, see the
# NOTICE.txt and LICENSE.txt files in its top-level directory; they are
# available at https://github.com/vecnet/zika
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License (MPL), version 2.0.  If a copy of the MPL was not distributed
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

from django.contrib import admin

from website.apps.home.models import Location, Simulation, Data, SimulationModel, Totals

class LocationAdmin(admin.ModelAdmin):
    list_display = ("id", "department", "department_code", "municipality", "municipality_code", "creation_timestamp")
    ordering = ("department", "municipality")
    search_fields = ("department", "department_code", "municipality", "municipality_code")
admin.site.register(Location, LocationAdmin)

class SimulationAdmin(admin.ModelAdmin):
    list_display = ("id", "name", "disease", "historical", "is_uploaded", "created_by", "creation_timestamp")
    ordering = ("-creation_timestamp",)
    search_fields = ("name", "created_by__username")
admin.site.register(Simulation, SimulationAdmin)

class DataAdmin(admin.ModelAdmin):
    list_display = ("location", "simulation", "value_low", "value_mid", "value_high", "date")
    ordering = ("-simulation",)
    search_fields = ("simulation__name", "location__department", "location__municipality", "simulation__created_by__username")
    list_filter = ("simulation", "location__department")
admin.site.register(Data, DataAdmin)

admin.site.register(Totals)
admin.site.register(SimulationModel)
<|endoftext|>