<commit_before>from models import model
from core import helpers
# import fix for testing
from core.helpers import *

def create_and_addroom(dojo, room_type, room_name):
    '''
    uses create room to create a room
    adds's new room to dojo, if valid
    '''
    status_messages = {}
    status_messages['status'] = None
    status_messages['room_name'] = room_name
    status_messages['room_type'] = room_type
    new_room = None
    try:
        new_room = create_room(room_type, room_name, dojo)
    except TypeError:
        status_messages['status'] = 'failed'

    if new_room == 'duplicates':
        status_messages['status'] = 'Duplicate name'
    elif new_room == 'Invalid name':
        status_messages['status'] = 'Invalid name'
    elif isinstance(new_room, model.Office):
        dojo.add_office(new_room)
        status_messages['status'] = 'ok'
    elif isinstance(new_room, model.LivingSpace):
        dojo.add_livingspace(new_room)
        status_messages['status'] = 'ok'
    return status_messages

def addsperson_chooseroom(dojo, first_name, second_name, person_type, choice_live='N'):
    """
    add a person to dojo and allocates office and [livingspace]
    """
    # set up status message respond
    status_messages = {'status': None, 'person_type': person_type}
    status_messages['name'] = first_name + ' ' + second_name

    choice = 'Y'
    if not choice_live or choice_live.lower() not in 'y':
        choice = 'N'
        choice_live = False
    else:
        choice_live = True
    status_messages['choice_live'] = choice_live

    try:
        new_person = add_person((first_name, second_name), person_type, choice)
        status_messages['status'] = 'ok'
        status_messages['id'] = new_person.id
        new_person.office = None
    except TypeError:
        status_messages['status'] = 'Failed'
        return status_messages

    room_update = allocate_room(new_person, dojo)
    status_messages.update(room_update)
    return status_messages

def allocate_room(new_person, dojo):
    """
    allocates a room to new_person
    Returns a dictionary of status messages about success of adding to rooms
    """
    status_messages = {'office': None, 'livingspace': None}

    if new_person == 'Invalid name':
        status_messages['status'] = 'Invalid name'
        return status_messages
    elif new_person == "Invalid choice":
        status_messages['status'] = 'Invalid choice'
        return status_messages
    elif isinstance(new_person, model.Fellow):
        if new_person.wants_living:
            status_messages['livingspace'] = allocate_livingspace(new_person, dojo=dojo)
        dojo.add_fellow(new_person)
        status_messages['person_type'] = 'fellow'
    else:
        dojo.add_staff(new_person)
        status_messages['person_type'] = 'staff'
    status_messages['office'] = allocate_office(new_person, dojo=dojo)

    return status_messages

def people_inroom(dojo, room_name):
    """
    returns the names of all the people in room_name
    """
    room_name = room_name.strip()
    rooms = list(dojo.office) + list(dojo.livingspace)
    for room in rooms:
        if room.name == room_name:
            return list(room.get_occupants())
    raise NotFoundException

class NotFoundException(Exception):
    pass

def dict_allocations(dojo):
    """
    returns a dict of allocations
    """
    allocations = {}
    rooms = list(dojo.office) + list(dojo.livingspace)
    for room in rooms:
        allocations[room.name] = people_inroom(dojo, room.name)

    return allocations

def list_unallocated(dojo, file_name=''):
    """
    returns a list of unallocated people to the screen
    if file_name is specified values are saved to file_name.txt
    """
    unallocated = []
    person = dojo.person

    # go over fellow first
    for fellow in person['fellow'].values():
        allocated_living = fellow.is_allocated_living()
        allocated_office = fellow.is_allocated_office()
        if not allocated_office or (not allocated_living and fellow.wants_living):
            unallocated.append(fellow)

    # go over staff
    for staff in person['staff'].values():
        if not staff.is_allocated_office():
            unallocated.append(staff)
    return unallocated

def save_txt(file_name, raw_data, mode='wt'):
    save_data_txt(file_name, raw_data, mode)

def load_data_txt(file_name, dojo):
    status_data = []
    try:
        if file_name[len(file_name) - 4:] == '.txt':
            loaded_data = helpers.load_data_txt(file_name)
        else:
            raise FileNotFoundError
    except FileNotFoundError:
        return [{'status': 'filenotfound', 'message': 'File Not Found'}]
    for user_info in loaded_data:
        if len(user_info) > 2 and len(user_info) < 5:
            first_name, second_name, person_type = user_info[:3]
            choice_live = 'N'
            if len(user_info) == 4:
                choice_live = user_info[3]
            status = addsperson_chooseroom(dojo, first_name, second_name, person_type, choice_live)
            status_data.append(status)
        elif len(user_info) > 0:
            msg = {'status': 'illegalformat', 'message': ' '.join(user_info) + ": was not Added."}
            status_data.append(msg)
    return status_data

def reallocate_person(room_name, person_id, dojo):

    person = room = None
    status_messages = {'status': 'Fail'}
    try:
        person_id = int(person_id)
    except ValueError:
        status_messages['message'] = "Invalid  User Id"
        return status_messages

    # get the person to be moved
    person = dojo.get_person(person_id)
    if not person or person_id < 0:
        status_messages['message'] = "Person not found"
        return status_messages

    # get the room to be reallocated to
    room_type = None
    room_info = get_roomname_type(room_name, dojo)
    if room_info['status'] != 'ok':
        status_messages['message'] = "Room not found"
        return status_messages
    else:
        room_name, room_type = room_info['in']
        status_messages['current_room'] = room_name.name

    # find all current rooms allocated to person
    current_rooms = dojo.get_person_room(person)
    current_livingspace = current_office = None
    for room in current_rooms:
        if isinstance(room, model.Office):
            current_office = room
        elif isinstance(room, model.LivingSpace):
            current_livingspace = room
    status_messages['prev_office'] = current_office
    status_messages['prev_livingspace'] = current_livingspace

    deallocation = deallocate_person(room_type, person, current_office, current_livingspace)
    status_messages['deallocation'] = deallocation
    status_messages['name'] = person.name
    status_messages['room_type'] = room_type

    if deallocation != 'Invalid Operation':
        name = room_name.name
        status_messages['status'] = 'ok'
        msg = None
        if room_type == 'O':
            msg = allocate_office(person, name_office=name, dojo=dojo)
        elif person.wants_living:
            msg = allocate_livingspace(person, name_livingspace=name, dojo=dojo)
            status_messages['choice'] = person.wants_living
        if not msg:
            status_messages['status'] = 'Invalid Operation'
    else:
        status_messages['status'] = 'Invalid Operation'

    return status_messages

def deallocate_person(room_type, person, office=None, livingspace=None):
    deallocation = None
    if room_type == 'O' and office:
        deallocation = deallocate_office(person, office)
    elif room_type == 'L' and livingspace:
        deallocation = deallocate_livingspace(person, livingspace)
    elif room_type == 'L' and isinstance(person, model.Staff):
        deallocation = 'Invalid Operation'

    return deallocation

def get_roomname_type(room_name, dojo):
    status_messages = {}
    room_name = room_name.strip().lower()
    if room_name not in dojo.takken_names:
        status_messages['status'] = "Room not found"
    else:
        office = dojo.get_office(room_name.strip().lower())
        livingspace = dojo.get_livingspace(room_name.strip().lower())

        # we can only reallocate one room at a time office or livingspace
        status_messages['in'] = (office, 'O') if office else (livingspace, 'L')
        status_messages['status'] = 'ok'
    return status_messages
<commit_msg>Chore : updated logic.py<commit_after>from models import model
from core import helpers
# import fix for testing
from core.helpers import *

def create_and_addroom(dojo, room_type, room_name):
    '''
    uses create room to create a room
    adds's new room to dojo, if valid
    '''
    status_messages = {}
    status_messages['status'] = None
    status_messages['room_name'] = room_name
    status_messages['room_type'] = room_type
    new_room = None
    try:
        new_room = create_room(room_type, room_name, dojo)
    except TypeError:
        status_messages['status'] = 'failed'

    if new_room == 'duplicates':
        status_messages['status'] = 'Duplicate name'
    elif new_room == 'Invalid name':
        status_messages['status'] = 'Invalid name'
    elif isinstance(new_room, model.Office):
        dojo.add_office(new_room)
        status_messages['status'] = 'ok'
    elif isinstance(new_room, model.LivingSpace):
        dojo.add_livingspace(new_room)
        status_messages['status'] = 'ok'
    return status_messages

def addsperson_chooseroom(dojo, first_name, second_name, person_type, choice_live='N'):
    """
    add a person to dojo and allocates office and [livingspace]
    """
    # set up status message respond
    status_messages = {'status': None, 'person_type': person_type}
    status_messages['name'] = first_name + ' ' + second_name

    choice = 'Y'
    if not choice_live or choice_live.lower() not in 'y':
        choice = 'N'
        choice_live = False
    else:
        choice_live = True
    status_messages['choice_live'] = choice_live

    try:
        new_person = add_person((first_name, second_name), person_type, choice)
        status_messages['status'] = 'ok'
        status_messages['id'] = new_person.id
        new_person.office = None
    except TypeError:
        status_messages['status'] = 'Failed'
        return status_messages

    room_update = allocate_room(new_person, dojo)
    status_messages.update(room_update)
    return status_messages

def allocate_room(new_person, dojo):
    """
    allocates a room to new_person
    Returns a dictionary of status messages about success of adding to rooms
    """
    status_messages = {'office': None, 'livingspace': None}

    if new_person == 'Invalid name':
        status_messages['status'] = 'Invalid name'
        return status_messages
    elif new_person == "Invalid choice":
        status_messages['status'] = 'Invalid choice'
        return status_messages
    elif isinstance(new_person, model.Fellow):
        if new_person.wants_living:
            status_messages['livingspace'] = allocate_livingspace(new_person, dojo=dojo)
        dojo.add_fellow(new_person)
        status_messages['person_type'] = 'fellow'
    else:
        dojo.add_staff(new_person)
        status_messages['person_type'] = 'staff'
    status_messages['office'] = allocate_office(new_person, dojo=dojo)

    return status_messages

def people_inroom(dojo, room_name):
    """
    input: room_name and dojo
    returns: the names of all the people in room_name
    """
    room_name = room_name.strip()
    rooms = list(dojo.office) + list(dojo.livingspace)
    for room in rooms:
        if room.name == room_name:
            return list(room.get_occupants())
    raise NotFoundException

class NotFoundException(Exception):
    pass

def dict_allocations(dojo):
    """
    input: dojo
    returns a dict of allocations
    """
    allocations = {}
    rooms = list(dojo.office) + list(dojo.livingspace)
    for room in rooms:
        allocations[room.name] = people_inroom(dojo, room.name)

    return allocations

def list_unallocated(dojo, file_name=''):
    """
    input: dojo & [file_name]
    returns a list of unallocated people to the screen
    if file_name is specified values are saved to file_name.txt
    """
    unallocated = []
    person = dojo.person

    # go over fellow first
    for fellow in person['fellow'].values():
        allocated_living = fellow.is_allocated_living()
        allocated_office = fellow.is_allocated_office()
        if not allocated_office or (not allocated_living and fellow.wants_living):
            unallocated.append(fellow)

    # go over staff
    for staff in person['staff'].values():
        if not staff.is_allocated_office():
            unallocated.append(staff)
    return unallocated

def save_txt(file_name, raw_data, mode='wt'):
    save_data_txt(file_name, raw_data, mode)

def load_data_txt(file_name, dojo):
    status_data = []
    try:
        if file_name[len(file_name) - 4:] == '.txt':
            loaded_data = helpers.load_data_txt(file_name)
        else:
            raise FileNotFoundError
    except FileNotFoundError:
        return [{'status': 'filenotfound', 'message': 'File Not Found'}]
    for user_info in loaded_data:
        if len(user_info) > 2 and len(user_info) < 5:
            first_name, second_name, person_type = user_info[:3]
            choice_live = 'N'
            if len(user_info) == 4:
                choice_live = user_info[3]
            status = addsperson_chooseroom(dojo, first_name, second_name, person_type, choice_live)
            status_data.append(status)
        elif len(user_info) > 0:
            msg = {'status': 'illegalformat', 'message': ' '.join(user_info) + ": was not Added."}
            status_data.append(msg)
    return status_data

def reallocate_person(room_name, person_id, dojo):

    person = room = None
    status_messages = {'status': 'Fail'}
    try:
        person_id = int(person_id)
    except ValueError:
        status_messages['message'] = "Invalid  User Id"
        return status_messages

    # get the person to be moved
    person = dojo.get_person(person_id)
    if not person or person_id < 0:
        status_messages['message'] = "Person not found"
        return status_messages

    # get the room to be reallocated to
    room_type = None
    room_info = get_roomname_type(room_name, dojo)
    if room_info['status'] != 'ok':
        status_messages['message'] = "Room not found"
        return status_messages
    else:
        room_name, room_type = room_info['in']
        status_messages['current_room'] = room_name.name

    # find all current rooms allocated to person
    current_rooms = dojo.get_person_room(person)
    current_livingspace = current_office = None
    for room in current_rooms:
        if isinstance(room, model.Office):
            current_office = room
        elif isinstance(room, model.LivingSpace):
            current_livingspace = room
    status_messages['prev_office'] = current_office
    status_messages['prev_livingspace'] = current_livingspace

    deallocation = deallocate_person(room_type, person, current_office, current_livingspace)
    status_messages['deallocation'] = deallocation
    status_messages['name'] = person.name
    status_messages['room_type'] = room_type

    if deallocation != 'Invalid Operation':
        name = room_name.name
        status_messages['status'] = 'ok'
        msg = None
        if room_type == 'O':
            msg = allocate_office(person, name_office=name, dojo=dojo)
        elif person.wants_living:
            msg = allocate_livingspace(person, name_livingspace=name, dojo=dojo)
            status_messages['choice'] = person.wants_living
        if not msg:
            status_messages['status'] = 'Invalid Operation'
    else:
        status_messages['status'] = 'Invalid Operation'

    return status_messages

def deallocate_person(room_type, person, office=None, livingspace=None):
    deallocation = None
    if room_type == 'O' and office:
        deallocation = deallocate_office(person, office)
    elif room_type == 'L' and livingspace:
        deallocation = deallocate_livingspace(person, livingspace)
    elif room_type == 'L' and isinstance(person, model.Staff):
        deallocation = 'Invalid Operation'

    return deallocation

def get_roomname_type(room_name, dojo):
    status_messages = {}
    room_name = room_name.strip().lower()
    if room_name not in dojo.takken_names:
        status_messages['status'] = "Room not found"
    else:
        office = dojo.get_office(room_name.strip().lower())
        livingspace = dojo.get_livingspace(room_name.strip().lower())

        # we can only reallocate one room at a time office or livingspace
        status_messages['in'] = (office, 'O') if office else (livingspace, 'L')
        status_messages['status'] = 'ok'
    return status_messages
<|endoftext|>


<commit_before># coding=utf-8
__author__ = "AstroPrint Product Team <product@astroprint.com>"
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
__copyright__ = "Copyright (C) 2017 3DaGoGo, Inc - Released under terms of the AGPLv3 License"

import os
from . import PluginService

from flask_login import current_user
from flask import jsonify

import octoprint.util as util
from octoprint.events import eventManager, Events
from octoprint.settings import settings
from octoprint.server import restricted_access

from astroprint.cloud import astroprintCloud
from astroprint.printer.manager import printerManager
from astroprint.printfiles import FileDestinations
from astroprint.printfiles.downloadmanager import downloadManager

class FilesService(PluginService):
	_validEvents = [
		#watch if a file where added
		#'file_added',
		#BROWSER -> PLUGIN
		#watch if a file where deleted
		'file_deleted',
		#watch if a file were be downloaded successfully
		'cloud_download_success',
		################
		#PLUGIN -> BROWSER + PLUGIN
		#watch downloading progress of a print file
		'progress_download_printfile',
		#watch if a print file were downloaded: successfully or failed(error or cancelled)
		'f¡nished_download_printfile'

	]

	def __init__(self):
		super(FilesService, self).__init__()

		#files managing
		self._eventManager.subscribe(Events.FILE_DELETED, self._onFileDeleted)
		self._eventManager.subscribe(Events.CLOUD_DOWNLOAD, self._onCloudDownloadStateChanged)

	def getLocalFiles(self, sendResponse):
		try:
			try:
				files = self._getLocalFileList(FileDestinations.LOCAL)
			except:
				pass

			try:
				files.extend(self._getLocalFileList(FileDestinations.SDCARD))
			except:
				pass
			sendResponse( { 'files': files, 'free': util.getFreeBytes(settings().getBaseFolder("uploads")) })

		except Exception as e:
			self._logger.error("files can not be obtained", exc_info = True)
			sendResponse('no_files_obtained',True)

	def _getLocalFileList(self, data):

		origin = data

		if origin == FileDestinations.SDCARD:
			sdFileList = printerManager().getSdFiles()

			files = []
			if sdFileList is not None:
				for sdFile, sdSize in sdFileList:
					file = {
						"name": sdFile,
						"origin": FileDestinations.SDCARD,
						#"refs": {
						#	"resource": url_for(".readPrintFile", target=FileDestinations.SDCARD, filename=sdFile, _external=True)
						#}
					}
					if sdSize is not None:
						file.update({"size": sdSize})
					files.append(file)
		else:
			files = printerManager().fileManager.getAllFileData()
			'''for file in files:
				file.update({
					"refs": {
						"resource": url_for(".readPrintFile", target=FileDestinations.LOCAL, filename=file["name"], _external=True),
						"download": url_for("index", _external=True) + "downloads/files/" + FileDestinations.LOCAL + "/" + file["name"]
					}
				})'''
		return files

	def _verifyFileExists(self,origin, filename):
		if origin == FileDestinations.SDCARD:
			availableFiles = map(lambda x: x[0], printerManager().getSdFiles())
		else:
			availableFiles = printerManager().fileManager.getAllFilenames()

		return filename in availableFiles

	def printFile(self, data, sendResponse):
		fileDestination = fileName = None

		if 'location' in data:
			fileDestination = data['location']

		if 'fileName' in data:
			fileName = data['fileName']

		if not fileDestination in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
			self._logger.error('Unknown file location', exc_info = True)
			sendResponse('unknown_file_location',True)

		if not fileName or not self._verifyFileExists(fileDestination, fileName):
			self._logger.error('File not found', exc_info = True)
			sendResponse('file_not_found',True)

		printer = printerManager()

		# selects/loads a file
		printAfterLoading = False
		if not printer.isOperational():
			#We try at least once
			printer.connect()

			start = time.time()
			connect_timeout = 5 #5 secs

			while not printer.isOperational() and not printer.isClosedOrError() and time.time() - start < connect_timeout:
				time.sleep(1)

			if not printer.isOperational():
				self._logger.error("The printer is not responding, can't start printing", exc_info = True)
				sendResponse('printer_not_responding',True)
				return

		printAfterLoading = True

		sd = False
		if fileDestination == FileDestinations.SDCARD:
			filenameToSelect = fileName
			sd = True
		else:
			filenameToSelect = printer.fileManager.getAbsolutePath(fileName)

		startPrintingStatus = printer.selectFile(filenameToSelect, sd, printAfterLoading)

		if startPrintingStatus:
			sendResponse({'success':'no error'})
		else:
			sendResponse('printer_not_responding',True)

	def deletePrintFile(self, data, sendResponse):

		fileDestination = fileName = None

		if 'location' in data:
			fileDestination = data['location']

		if 'fileName' in data:
			fileName = data['fileName']

		if not fileDestination in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
			self._logger.error("Unknown file location", exc_info = True)
			sendResponse('unknown_file_location',True)
			return

		if not fileName or not self._verifyFileExists(fileDestination, fileName):
			self._logger.error("File not found", exc_info = True)
			sendResponse('file_not_found',True)
			return

		sd = fileDestination == FileDestinations.SDCARD

		printer = printerManager()

		currentJob = printer.getCurrentJob()
		currentFilename = None
		currentSd = None
		if currentJob is not None and "fileName" in currentJob.keys() and "sd" in currentJob.keys():
			currentFilename = currentJob["fileName"]
			currentSd = currentJob["sd"]

		# prohibit deleting the file that is currently being printed
		if currentFilename == fileName and currentSd == sd and (printer.isPrinting() or printer.isPaused()):
			sendResponse("Trying to delete file that is currently being printed: %s" % fileName,true)

		# deselect the file if it's currently selected
		if currentFilename is not None and fileName == currentFilename:
			printer.unselectFile()

		# delete it
		if sd:
			printer.deleteSdFile(fileName)
		else:
			printer.fileManager.removeFile(fileName)

		eventManager().fire(Events.FILE_DELETED, {"filename": filename})

		self.publishEvent('file_deleted','deleted')

		sendResponse({'success':'no error'})

	def downloadPrintFile(self,printFileId,sendResponse):
		em = eventManager()

		def progressCb(progress):
			self.publishEvent('progress_download_printfile', {
				"type": "progress",
				"id": printFileId,
				"progress": progress
			})

		def successCb(destFile, fileInfo):
			if fileInfo is True:
				#This means the files was already on the device

				data = {
					"type": "success",
					"id": printFileId,
					"filename": printerManager().fileManager._getBasicFilename(destFile)
				}

				self.publishEvent('f¡nished_download_printfile',data)
				em.fire(Events.CLOUD_DOWNLOAD,data)

			else:
				data = {
					"type": "success",
					"id": printFileId,
					"filename": printerManager().fileManager._getBasicFilename(destFile),
					"info": fileInfo["info"],
					"printer": fileInfo["printer"],
					"material": fileInfo["material"],
					"quality": fileInfo["quality"],
					"image": fileInfo["image"],
					"created": fileInfo["created"]
				}

				self.publishEvent('f¡nished_download_printfile',data)

		def errorCb(destFile, error):
			if error == 'cancelled':
				data = {
					"type": "cancelled",
					"id": printFileId
				}

				self.publishEvent('f¡nished_download_printfile',data)
				em.fire(Events.CLOUD_DOWNLOAD,data)

			else:
				data = {
					"type": "error",
					"id": printFileId,
					"reason": error
				}

				em.fire(Events.CLOUD_DOWNLOAD,data)
				self.publishEvent('f¡nished_download_printfile',data)

			if destFile and os.path.exists(destFile):
				os.remove(destFile)

		if astroprintCloud().download_print_file(printFileId, progressCb, successCb, errorCb):
			sendResponse({'success':'no error'})
			return

		sendResponse('error',True)
		return

	def cancelDownloadPrintFile(self, printFileId, sendResponse):

		if downloadManager().cancelDownload(printFileId):
			sendResponse({'success':'no error'})
		else:
			sendResponse('cancel_error',True)

	#EVENTS

	def _onFileDeleted(self,event,data):
		self.publishEvent('file_deleted',data['filename'])

	def _onCloudDownloadStateChanged(self,event,data):
		if data['type'] == 'success':
			self.publishEvent('cloud_download_success',data)
		#else TODO
<commit_msg>necessary import time<commit_after># coding=utf-8
__author__ = "AstroPrint Product Team <product@astroprint.com>"
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
__copyright__ = "Copyright (C) 2017 3DaGoGo, Inc - Released under terms of the AGPLv3 License"

import os
import time
from . import PluginService

from flask_login import current_user
from flask import jsonify

import octoprint.util as util
from octoprint.events import eventManager, Events
from octoprint.settings import settings
from octoprint.server import restricted_access

from astroprint.cloud import astroprintCloud
from astroprint.printer.manager import printerManager
from astroprint.printfiles import FileDestinations
from astroprint.printfiles.downloadmanager import downloadManager

class FilesService(PluginService):
	_validEvents = [
		#watch if a file where added
		#'file_added',
		#BROWSER -> PLUGIN
		#watch if a file where deleted
		'file_deleted',
		#watch if a file were be downloaded successfully
		'cloud_download_success',
		################
		#PLUGIN -> BROWSER + PLUGIN
		#watch downloading progress of a print file
		'progress_download_printfile',
		#watch if a print file were downloaded: successfully or failed(error or cancelled)
		'f¡nished_download_printfile'

	]

	def __init__(self):
		super(FilesService, self).__init__()

		#files managing
		self._eventManager.subscribe(Events.FILE_DELETED, self._onFileDeleted)
		self._eventManager.subscribe(Events.CLOUD_DOWNLOAD, self._onCloudDownloadStateChanged)

	def getLocalFiles(self, sendResponse):
		try:
			try:
				files = self._getLocalFileList(FileDestinations.LOCAL)
			except:
				pass

			try:
				files.extend(self._getLocalFileList(FileDestinations.SDCARD))
			except:
				pass
			sendResponse( { 'files': files, 'free': util.getFreeBytes(settings().getBaseFolder("uploads")) })

		except Exception as e:
			self._logger.error("files can not be obtained", exc_info = True)
			sendResponse('no_files_obtained',True)

	def _getLocalFileList(self, data):

		origin = data

		if origin == FileDestinations.SDCARD:
			sdFileList = printerManager().getSdFiles()

			files = []
			if sdFileList is not None:
				for sdFile, sdSize in sdFileList:
					file = {
						"name": sdFile,
						"origin": FileDestinations.SDCARD,
						#"refs": {
						#	"resource": url_for(".readPrintFile", target=FileDestinations.SDCARD, filename=sdFile, _external=True)
						#}
					}
					if sdSize is not None:
						file.update({"size": sdSize})
					files.append(file)
		else:
			files = printerManager().fileManager.getAllFileData()
			'''for file in files:
				file.update({
					"refs": {
						"resource": url_for(".readPrintFile", target=FileDestinations.LOCAL, filename=file["name"], _external=True),
						"download": url_for("index", _external=True) + "downloads/files/" + FileDestinations.LOCAL + "/" + file["name"]
					}
				})'''
		return files

	def _verifyFileExists(self,origin, filename):
		if origin == FileDestinations.SDCARD:
			availableFiles = map(lambda x: x[0], printerManager().getSdFiles())
		else:
			availableFiles = printerManager().fileManager.getAllFilenames()

		return filename in availableFiles

	def printFile(self, data, sendResponse):
		fileDestination = fileName = None

		if 'location' in data:
			fileDestination = data['location']

		if 'fileName' in data:
			fileName = data['fileName']

		if not fileDestination in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
			self._logger.error('Unknown file location', exc_info = True)
			sendResponse('unknown_file_location',True)

		if not fileName or not self._verifyFileExists(fileDestination, fileName):
			self._logger.error('File not found', exc_info = True)
			sendResponse('file_not_found',True)

		printer = printerManager()

		# selects/loads a file
		printAfterLoading = False
		if not printer.isOperational():
			#We try at least once
			printer.connect()

			start = time.time()
			connect_timeout = 5 #5 secs

			while not printer.isOperational() and not printer.isClosedOrError() and time.time() - start < connect_timeout:
				time.sleep(1)

			if not printer.isOperational():
				self._logger.error("The printer is not responding, can't start printing", exc_info = True)
				sendResponse('printer_not_responding',True)
				return

		printAfterLoading = True

		sd = False
		if fileDestination == FileDestinations.SDCARD:
			filenameToSelect = fileName
			sd = True
		else:
			filenameToSelect = printer.fileManager.getAbsolutePath(fileName)

		startPrintingStatus = printer.selectFile(filenameToSelect, sd, printAfterLoading)

		if startPrintingStatus:
			sendResponse({'success':'no error'})
		else:
			sendResponse('printer_not_responding',True)

	def deletePrintFile(self, data, sendResponse):

		fileDestination = fileName = None

		if 'location' in data:
			fileDestination = data['location']

		if 'fileName' in data:
			fileName = data['fileName']

		if not fileDestination in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
			self._logger.error("Unknown file location", exc_info = True)
			sendResponse('unknown_file_location',True)
			return

		if not fileName or not self._verifyFileExists(fileDestination, fileName):
			self._logger.error("File not found", exc_info = True)
			sendResponse('file_not_found',True)
			return

		sd = fileDestination == FileDestinations.SDCARD

		printer = printerManager()

		currentJob = printer.getCurrentJob()
		currentFilename = None
		currentSd = None
		if currentJob is not None and "fileName" in currentJob.keys() and "sd" in currentJob.keys():
			currentFilename = currentJob["fileName"]
			currentSd = currentJob["sd"]

		# prohibit deleting the file that is currently being printed
		if currentFilename == fileName and currentSd == sd and (printer.isPrinting() or printer.isPaused()):
			sendResponse("Trying to delete file that is currently being printed: %s" % fileName,true)

		# deselect the file if it's currently selected
		if currentFilename is not None and fileName == currentFilename:
			printer.unselectFile()

		# delete it
		if sd:
			printer.deleteSdFile(fileName)
		else:
			printer.fileManager.removeFile(fileName)

		eventManager().fire(Events.FILE_DELETED, {"filename": filename})

		self.publishEvent('file_deleted','deleted')

		sendResponse({'success':'no error'})

	def downloadPrintFile(self,printFileId,sendResponse):
		em = eventManager()

		def progressCb(progress):
			self.publishEvent('progress_download_printfile', {
				"type": "progress",
				"id": printFileId,
				"progress": progress
			})

		def successCb(destFile, fileInfo):
			if fileInfo is True:
				#This means the files was already on the device

				data = {
					"type": "success",
					"id": printFileId,
					"filename": printerManager().fileManager._getBasicFilename(destFile)
				}

				self.publishEvent('f¡nished_download_printfile',data)
				em.fire(Events.CLOUD_DOWNLOAD,data)

			else:
				data = {
					"type": "success",
					"id": printFileId,
					"filename": printerManager().fileManager._getBasicFilename(destFile),
					"info": fileInfo["info"],
					"printer": fileInfo["printer"],
					"material": fileInfo["material"],
					"quality": fileInfo["quality"],
					"image": fileInfo["image"],
					"created": fileInfo["created"]
				}

				self.publishEvent('f¡nished_download_printfile',data)

		def errorCb(destFile, error):
			if error == 'cancelled':
				data = {
					"type": "cancelled",
					"id": printFileId
				}

				self.publishEvent('f¡nished_download_printfile',data)
				em.fire(Events.CLOUD_DOWNLOAD,data)

			else:
				data = {
					"type": "error",
					"id": printFileId,
					"reason": error
				}

				em.fire(Events.CLOUD_DOWNLOAD,data)
				self.publishEvent('f¡nished_download_printfile',data)

			if destFile and os.path.exists(destFile):
				os.remove(destFile)

		if astroprintCloud().download_print_file(printFileId, progressCb, successCb, errorCb):
			sendResponse({'success':'no error'})
			return

		sendResponse('error',True)
		return

	def cancelDownloadPrintFile(self, printFileId, sendResponse):

		if downloadManager().cancelDownload(printFileId):
			sendResponse({'success':'no error'})
		else:
			sendResponse('cancel_error',True)

	#EVENTS

	def _onFileDeleted(self,event,data):
		self.publishEvent('file_deleted',data['filename'])

	def _onCloudDownloadStateChanged(self,event,data):
		if data['type'] == 'success':
			self.publishEvent('cloud_download_success',data)
		#else TODO
<|endoftext|>


<commit_before>import pycurl, json
from StringIO import StringIO

class builder:

    def __init__(self):
        self.json_storage = StringIO()
        self.c = pycurl.Curl()

    def _update_json_storage(self, url):
        self.json_storage.truncate(0)
        self.c.setopt(self.c.URL, url)
        self.c.setopt(self.c.HTTPHEADER, ["Content-type:Application/json"])
        self.c.setopt(self.c.WRITEFUNCTION, self.json_storage.write)
        self.c.perform()

    def get_detailed_methods_list(self, method_info):

        markdown = StringIO()

        # if we received a parameters list, use it, otherwise initialize it
        parameters = parameters if "parameters" in locals() else {}

        # process information from the neo4j web service info, if any
        if "neo4j_service_url" in method_info and method_info["neo4j_service_url"] is not None:
            self._update_json_storage(method_info["neo4j_service_url"])
#            print self.json_storage.getvalue()
            service_info = json.loads(self.json_storage.getvalue())
            method_info["long_description"] = service_info["description"]

            # process neo4j parameters
            for p in service_info["parameters"]:
                parameters[p["name"]] = p

        # just a kludge to keep from failing if there is no description
        method_info["long_description"] = method_info["long_description"] if "long_description" in method_info else ""

        # process parameters for printing
        required_keys = []
        optional_keys = []
        for name, p in parameters.iteritems():
            if p["optional"] == True:
                p["style_modifier"] = ""
                optional_keys.append(name)
            else:
                p["style_modifier"] = "**"
                required_keys.append(name)

        # now print the preamble
        markdown.write(self.method_preamble_template.format(**method_info))

        # and the parameters
        for key_set in [sorted(required_keys), sorted(optional_keys)]:
            for p in key_set:
                markdown.write(self.method_parameter_template.format(**parameters[p]))

        # and the example
        markdown.write(self.method_example_template.format(**method_info))

        return markdown.getvalue()

    def get_methods_summary(self, method_group):

        markdown = StringIO()

        markdown.write("## {g.title}\n".format(g=method_group))
        markdown.write(method_group.long_description+"\n\n")
        markdown.write("| URL                                        | Verb   | Summary                |\n");
        markdown.write("| -------------------------------------------|--------|------------------------|\n");

        for m in method_group.methods_list:
            markdown.write("|[`{m[relative_url]}`](#{m[anchor_name]}) | {m[http_verb]} | {m[short_description]} |\n".format(m=m));

        return markdown.getvalue()

    def get_doc_preamble(self, method_groups):

        markdown = StringIO()

        mgm = ""
        for g in method_groups:
            mgm += self.method_group_list_item_template.format(g=g)
        markdown.write(self.doc_preamble_template.format(method_groups=mgm))

        return markdown.getvalue()

    def get_doc_postamble(self):
        return self.doc_postamble_template

    doc_preamble_template = """**Warning: this is a draft version undergoing heavy revision on the week of September 8, 2014**

This documentation describes version 2.0 of the Open Tree of Life APIs. There is also documentation for [version 1.0](https://github.com/OpenTreeOfLife/opentree/wiki/Open-Tree-of-Life-APIs-V1), which is still active but may be deprecated in the future.

The Open Tree of Life APIs include services to access the following types of data:

{method_groups}

The base URL for all services:

	http://api.opentreeoflife.org/v2

**Caveats**

* the JSON return values are not consistent between software components. We are working to standardize on the documented [NexSON](http://purl.org/opentree/nexson) format.
* [neo4j](http://neo4j.org) implements POST for some methods that really should be GET.

**Questions / Comments**

If you have questions, or have a problem with any of these methods, please leave an issue in the [feedback issue tracker](https://github.com/OpenTreeOfLife/feedback/issues).\n\n"""

    method_group_list_item_template = """**[{g.title}](#{g.anchor_name})** : {g.short_description}.\n\n"""

    method_preamble_template = """### {method_name}

*{short_description}*

```
{http_verb}    {relative_url}
```

{long_description}

##### Parameters:\n\n"""

    method_parameter_template = """{style_modifier}```{name}``` : {type}{style_modifier}

{description}\n\n"""

    method_example_template = """*Example:*

```bash
$ {example_command}
```

```json
{example_result}
```\n\n"""

    doc_postamble_template = """## Other API docs
The following links provide information about ongoing development: methods being tested, developed or discussed.

* [documentation of the datastore API](https://github.com/OpenTreeOfLife/phylesystem-api/blob/master/docs/README.md)
* [list of neo4j methods](https://github.com/OpenTreeOfLife/opentree/blob/master/neo4j_services_docs.md)
* [Python library for interacting with OpenTree APIs](https://github.com/OpenTreeOfLife/peyotl)
* [Overview of methods used internally between OpenTree components](https://github.com/OpenTreeOfLife/phylesystem-api/wiki/overview-of-open-tree-of-life-api-calls)

If you have questions or feedback, leave an [issue on GitHub](https://github.com/OpenTreeOfLife/feedback/issues) or join us on IRC on the #opentreeoflife channel on [Freenode](http://freenode.net/)."""<commit_msg>Update Documentation.py<commit_after>import pycurl, json
from StringIO import StringIO

class builder:

    def __init__(self):
        self.json_storage = StringIO()
        self.c = pycurl.Curl()

    def _update_json_storage(self, url):
        self.json_storage.truncate(0)
        self.c.setopt(self.c.URL, url)
        self.c.setopt(self.c.HTTPHEADER, ["Content-type:Application/json"])
        self.c.setopt(self.c.WRITEFUNCTION, self.json_storage.write)
        self.c.perform()

    def get_detailed_methods_list(self, method_info):

        markdown = StringIO()

        # if we received a parameters list, use it, otherwise initialize it
        parameters = parameters if "parameters" in locals() else {}

        # process information from the neo4j web service info, if any
        if "neo4j_service_url" in method_info and method_info["neo4j_service_url"] is not None:
            self._update_json_storage(method_info["neo4j_service_url"])
#            print self.json_storage.getvalue()
            service_info = json.loads(self.json_storage.getvalue())
            method_info["long_description"] = service_info["description"]

            # process neo4j parameters
            for p in service_info["parameters"]:
                parameters[p["name"]] = p

        # just a kludge to keep from failing if there is no description
        method_info["long_description"] = method_info["long_description"] if "long_description" in method_info else ""

        # process parameters for printing
        required_keys = []
        optional_keys = []
        for name, p in parameters.iteritems():
            if p["optional"] == True:
                p["style_modifier"] = ""
                optional_keys.append(name)
            else:
                p["style_modifier"] = "**"
                required_keys.append(name)

        # now print the preamble
        markdown.write(self.method_preamble_template.format(**method_info))

        # and the parameters
        for key_set in [sorted(required_keys), sorted(optional_keys)]:
            for p in key_set:
                markdown.write(self.method_parameter_template.format(**parameters[p]))

        # and the example
        markdown.write(self.method_example_template.format(**method_info))

        return markdown.getvalue()

    def get_methods_summary(self, method_group):

        markdown = StringIO()

        markdown.write("## {g.title}\n".format(g=method_group))
        markdown.write(method_group.long_description+"\n\n")
        markdown.write("| URL                                        | Verb   | Summary                |\n");
        markdown.write("| -------------------------------------------|--------|------------------------|\n");

        for m in method_group.methods_list:
            markdown.write("|[`{m[relative_url]}`](#{m[anchor_name]}) | {m[http_verb]} | {m[short_description]} |\n".format(m=m));

        return markdown.getvalue()

    def get_doc_preamble(self, method_groups):

        markdown = StringIO()

        mgm = ""
        for g in method_groups:
            mgm += self.method_group_list_item_template.format(g=g)
        markdown.write(self.doc_preamble_template.format(method_groups=mgm))

        return markdown.getvalue()

    def get_doc_postamble(self):
        return self.doc_postamble_template

    doc_preamble_template = """**Warning: this is a draft version undergoing heavy revision on the week of September 8, 2014**

This documentation describes version 2.0 of the Open Tree of Life APIs. There is also documentation for [version 1.0](https://github.com/OpenTreeOfLife/opentree/wiki/Open-Tree-of-Life-APIs-V1), which is still active but is deprecated and will eventually be retired.

The Open Tree of Life APIs include services to access the following types of data:

{method_groups}

The base URL for all services:

	http://api.opentreeoflife.org/v2

**Caveats**

* the JSON return values are not consistent between software components. We are working to standardize on the documented [NexSON](http://purl.org/opentree/nexson) format.
* [neo4j](http://neo4j.org) implements POST for some methods that really should be GET.

**Questions / Comments**

If you have questions, or have a problem with any of these methods, please leave an issue in the [feedback issue tracker](https://github.com/OpenTreeOfLife/feedback/issues).\n\n"""

    method_group_list_item_template = """**[{g.title}](#{g.anchor_name})** : {g.short_description}.\n\n"""

    method_preamble_template = """### {method_name}

*{short_description}*

```
{http_verb}    {relative_url}
```

{long_description}

##### Parameters:\n\n"""

    method_parameter_template = """{style_modifier}```{name}``` : {type}{style_modifier}

{description}\n\n"""

    method_example_template = """*Example:*

```bash
$ {example_command}
```

```json
{example_result}
```\n\n"""

    doc_postamble_template = """## Other API docs
The following links provide information about ongoing development: methods being tested, developed or discussed.

* [documentation of the datastore API](https://github.com/OpenTreeOfLife/phylesystem-api/blob/master/docs/README.md)
* [list of neo4j methods](https://github.com/OpenTreeOfLife/opentree/blob/master/neo4j_services_docs.md)
* [Python library for interacting with OpenTree APIs](https://github.com/OpenTreeOfLife/peyotl)
* [Overview of methods used internally between OpenTree components](https://github.com/OpenTreeOfLife/phylesystem-api/wiki/overview-of-open-tree-of-life-api-calls)

If you have questions or feedback, leave an [issue on GitHub](https://github.com/OpenTreeOfLife/feedback/issues) or join us on IRC on the #opentreeoflife channel on [Freenode](http://freenode.net/)."""
<|endoftext|>


<commit_before>#!/usr/bin/env python

import sys
import rospy
import pymongo
import numpy as np
import math
import copy

from strands_navigation_msgs.srv import *
from strands_exploration_msgs.srv import *
from strands_navigation_msgs.msg import TopologicalMap

class exploration_slots(object):

    def __init__(self):
        self.map_received =False
        host = rospy.get_param("mongodb_host")
        port = rospy.get_param("mongodb_port")
        self.mongo_client = pymongo.MongoClient(host, port)

        rospy.Subscriber('/topological_map', TopologicalMap, self.MapCallback)
        rospy.loginfo("Waiting for Topological map ...")
        while not self.map_received:
            rospy.sleep(rospy.Duration(0.1))
        rospy.loginfo("... Got Topological map")

        self.explore_srv=rospy.Service('/edge_exploration/get_exploration_task', strands_exploration_msgs.srv.GetExplorationTasks, self.get_exploration_task_cb)
        rospy.loginfo("All Done ...")
        rospy.spin()

    """
     MapCallback

     This function receives the Topological Map
    """
    def MapCallback(self, msg) :
        self.top_map = msg
        self.map_received = True
        self.get_list_of_edges()

    def get_list_of_edges(self):
        self.edgid=[]
        self.eids=[]
        rospy.loginfo("Querying for list of edges")
        for i in self.top_map.nodes :
            for j in i.edges:
                if j.edge_id not in self.edgid :
                    self.edgid.append(j.edge_id)
                    val={}
                    val["edge_id"]=j.edge_id
                    val["entropy"]=0.0
                    val["time"]=0.0
                    val['samples']= 0
                    val['score']= 0.0
                    self.eids.append(val)
        fdbmsg = 'Done. %d edges found' %len(self.edgid)
        rospy.loginfo(fdbmsg)

    def get_exploration_task_cb(self, req):
        exptsk=[]
        tskscr=[]

        print "exploration time"
        explotime = rospy.Duration.from_sec((req.end_time.secs - req.start_time.secs))
        timeslot = req.start_time + rospy.Duration.from_sec((req.end_time.secs - req.start_time.secs)/2)
        print timeslot.secs, explotime.secs

        ent = self.predict_entropy(timeslot)
        est = self.predict_edges(timeslot)
        self.fill_values(ent, est)
        self.estimate_scores()

        results = copy.copy(self.eids)
        results = sorted(results, key=lambda k: k['score'], reverse=True)

        task_time=0
        for i in results:
            print task_time, i['time'].secs*2
            task_time+=(i['time'].secs*2)
            if task_time < explotime.secs:
                print i['edge_id'], i['samples'], i['time'].secs, i['entropy'], i['score']
                exptsk.append(i['edge_id'])
                tskscr.append(i['score'])
            else:
                break

        return exptsk, tskscr

    def estimate_scores(self):
        samples = np.array([x['samples'] for x in self.eids])
        mean = np.mean(samples)
        minimum = np.min(samples)
        maximum = np.max(samples)
        #print "Mean: %f Max: %f Min: %f"%(mean, minimum, maximum)
        print mean, minimum, maximum
        for i in range(len(self.eids)):
            if self.eids[i]['samples'] >= mean:
                sbs = (float(self.eids[i]['samples'])-maximum)/(2*(mean-maximum))
            else:
                sbs = 1 + (float(self.eids[i]['samples'])-minimum)/(2*(minimum-mean))

            if sbs >= 0.9 :
                wsbs = (math.sqrt(1-sbs))
                self.eids[i]['score'] = (wsbs*self.eids[i]['entropy'])+((1-wsbs)*sbs)
            else:
                self.eids[i]['score'] = self.eids[i]['entropy']#((1-(1/(float(self.eids[i]['samples'])+1)))*self.eids[i]['entropy'])+(1/(float(self.eids[i]['samples'])+1))

            if self.eids[i]['score'] < 0.0:
                self.eids[i]['score']=0.0
            elif self.eids[i]['score'] > 1.0:
                self.eids[i]['score']=1.0

#            print "+++++"
#            print i, self.eids[i]['edge_id'], self.eids[i]['samples'], self.eids[i]['entropy'], self.eids[i]['score']
            #print wsbs, self.eids[i]['entropy'], 1-wsbs, sbs, self.eids[i]['score']

    def fill_values(self, entropies, estimations):
        db=self.mongo_client.message_store
        collection=db['nav_stats']

        for i in range(len(entropies.edge_ids)):
            ind=self.edgid.index(entropies.edge_ids[i])
            self.eids[ind]['entropy']= entropies.probs[i]

        for i in range(len(estimations.edge_ids)):
            ind=self.edgid.index(estimations.edge_ids[i])
            self.eids[ind]['time']= estimations.durations[i]

        for i in range(len(self.eids)):
            query =  {'topological_map': self.top_map.pointset, 'edge_id':self.eids[i]['edge_id']}
            self.eids[i]['samples'] = collection.find(query).count()

    def predict_entropy(self, epoch):
        rospy.wait_for_service('/topological_prediction/edge_entropies')
        try:
            get_prediction = rospy.ServiceProxy('/topological_prediction/edge_entropies', strands_navigation_msgs.srv.PredictEdgeState)
            print "Requesting prediction for %f"%epoch.secs
            resp1 = get_prediction(epoch)
            print "got %d edge ids, %d probs"%(len(resp1.edge_ids),len(resp1.probs))
            return resp1
        except rospy.ServiceException, e:
            print "Service call failed: %s"%e

    def predict_edges(self, epoch):
        rospy.wait_for_service('/topological_prediction/predict_edges')
        try:
            get_prediction = rospy.ServiceProxy('/topological_prediction/predict_edges', strands_navigation_msgs.srv.PredictEdgeState)
            print "Requesting prediction for %f"%epoch.secs
            resp1 = get_prediction(epoch)
            print "got %d edge ids, %d probs"%(len(resp1.edge_ids),len(resp1.probs))
            return resp1
        except rospy.ServiceException, e:
            print "Service call failed: %s"%e

if __name__ == "__main__":
    rospy.init_node('topological_prediction_test')
    server = exploration_slots()<commit_msg>changing service name<commit_after>#!/usr/bin/env python

import sys
import rospy
import pymongo
import numpy as np
import math
import copy

from strands_navigation_msgs.srv import *
from strands_exploration_msgs.srv import *
from strands_navigation_msgs.msg import TopologicalMap

class exploration_slots(object):

    def __init__(self):
        self.map_received =False
        host = rospy.get_param("mongodb_host")
        port = rospy.get_param("mongodb_port")
        self.mongo_client = pymongo.MongoClient(host, port)

        rospy.Subscriber('/topological_map', TopologicalMap, self.MapCallback)
        rospy.loginfo("Waiting for Topological map ...")
        while not self.map_received:
            rospy.sleep(rospy.Duration(0.1))
        rospy.loginfo("... Got Topological map")

        self.explore_srv=rospy.Service('/exploration_services/get_edge_exploration', strands_exploration_msgs.srv.GetExplorationTasks, self.get_exploration_task_cb)
        rospy.loginfo("All Done ...")
        rospy.spin()

    """
     MapCallback

     This function receives the Topological Map
    """
    def MapCallback(self, msg) :
        self.top_map = msg
        self.map_received = True
        self.get_list_of_edges()

    def get_list_of_edges(self):
        self.edgid=[]
        self.eids=[]
        rospy.loginfo("Querying for list of edges")
        for i in self.top_map.nodes :
            for j in i.edges:
                if j.edge_id not in self.edgid :
                    self.edgid.append(j.edge_id)
                    val={}
                    val["edge_id"]=j.edge_id
                    val["entropy"]=0.0
                    val["time"]=0.0
                    val['samples']= 0
                    val['score']= 0.0
                    self.eids.append(val)
        fdbmsg = 'Done. %d edges found' %len(self.edgid)
        rospy.loginfo(fdbmsg)

    def get_exploration_task_cb(self, req):
        exptsk=[]
        tskscr=[]

        print "exploration time"
        explotime = rospy.Duration.from_sec((req.end_time.secs - req.start_time.secs))
        timeslot = req.start_time + rospy.Duration.from_sec((req.end_time.secs - req.start_time.secs)/2)
        print timeslot.secs, explotime.secs

        ent = self.predict_entropy(timeslot)
        est = self.predict_edges(timeslot)
        self.fill_values(ent, est)
        self.estimate_scores()

        results = copy.copy(self.eids)
        results = sorted(results, key=lambda k: k['score'], reverse=True)

        task_time=0
        for i in results:
            print task_time, i['time'].secs*2
            task_time+=(i['time'].secs*2)
            if task_time < explotime.secs:
                print i['edge_id'], i['samples'], i['time'].secs, i['entropy'], i['score']
                exptsk.append(i['edge_id'])
                tskscr.append(i['score'])
            else:
                break

        return exptsk, tskscr

    def estimate_scores(self):
        samples = np.array([x['samples'] for x in self.eids])
        mean = np.mean(samples)
        minimum = np.min(samples)
        maximum = np.max(samples)
        #print "Mean: %f Max: %f Min: %f"%(mean, minimum, maximum)
        print mean, minimum, maximum
        for i in range(len(self.eids)):
            if self.eids[i]['samples'] >= mean:
                sbs = (float(self.eids[i]['samples'])-maximum)/(2*(mean-maximum))
            else:
                sbs = 1 + (float(self.eids[i]['samples'])-minimum)/(2*(minimum-mean))

            if sbs >= 0.9 :
                wsbs = (math.sqrt(1-sbs))
                self.eids[i]['score'] = (wsbs*self.eids[i]['entropy'])+((1-wsbs)*sbs)
            else:
                self.eids[i]['score'] = self.eids[i]['entropy']#((1-(1/(float(self.eids[i]['samples'])+1)))*self.eids[i]['entropy'])+(1/(float(self.eids[i]['samples'])+1))

            if self.eids[i]['score'] < 0.0:
                self.eids[i]['score']=0.0
            elif self.eids[i]['score'] > 1.0:
                self.eids[i]['score']=1.0

#            print "+++++"
#            print i, self.eids[i]['edge_id'], self.eids[i]['samples'], self.eids[i]['entropy'], self.eids[i]['score']
            #print wsbs, self.eids[i]['entropy'], 1-wsbs, sbs, self.eids[i]['score']

    def fill_values(self, entropies, estimations):
        db=self.mongo_client.message_store
        collection=db['nav_stats']

        for i in range(len(entropies.edge_ids)):
            ind=self.edgid.index(entropies.edge_ids[i])
            self.eids[ind]['entropy']= entropies.probs[i]

        for i in range(len(estimations.edge_ids)):
            ind=self.edgid.index(estimations.edge_ids[i])
            self.eids[ind]['time']= estimations.durations[i]

        for i in range(len(self.eids)):
            query =  {'topological_map': self.top_map.pointset, 'edge_id':self.eids[i]['edge_id']}
            self.eids[i]['samples'] = collection.find(query).count()

    def predict_entropy(self, epoch):
        rospy.wait_for_service('/topological_prediction/edge_entropies')
        try:
            get_prediction = rospy.ServiceProxy('/topological_prediction/edge_entropies', strands_navigation_msgs.srv.PredictEdgeState)
            print "Requesting prediction for %f"%epoch.secs
            resp1 = get_prediction(epoch)
            print "got %d edge ids, %d probs"%(len(resp1.edge_ids),len(resp1.probs))
            return resp1
        except rospy.ServiceException, e:
            print "Service call failed: %s"%e

    def predict_edges(self, epoch):
        rospy.wait_for_service('/topological_prediction/predict_edges')
        try:
            get_prediction = rospy.ServiceProxy('/topological_prediction/predict_edges', strands_navigation_msgs.srv.PredictEdgeState)
            print "Requesting prediction for %f"%epoch.secs
            resp1 = get_prediction(epoch)
            print "got %d edge ids, %d probs"%(len(resp1.edge_ids),len(resp1.probs))
            return resp1
        except rospy.ServiceException, e:
            print "Service call failed: %s"%e

if __name__ == "__main__":
    rospy.init_node('topological_prediction_test')
    server = exploration_slots()<|endoftext|>


<commit_before>import collections
from sprecgrammars.rules import astree
from sprecgrammars.actions import nodes

class RecognitionResultsTree:

    def __init__(self, root_rule_node, node_ids):
        self.root_rule_node = root_rule_node
        self.node_ids = node_ids
        self.initialize_fields()

    def walk_tree(self):
        for node_info in self.root_rule_node.walk():
            yield RuleNodeWrapper(node_info['node'], node_info['ancestors'], self.node_ids)

    def initialize_fields(self):
        self.node_map = {}
        self.variables = collections.OrderedDict()
        for node_wrapper in self.walk_tree():
            is_ambiguous = (isinstance(node_wrapper.node, astree.GroupingNode) or
                isinstance(node_wrapper.node, astree.Rule) and node_wrapper.node.name == '_dictate')
            if is_ambiguous:
                self.variables[node_wrapper.path] = node_wrapper
            self.node_map[node_wrapper.path] = node_wrapper

    def get_full_path_engine_variables(self, engine_variables):
        full_path_engine_variables = collections.defaultdict(list)
        path = ()
        for i, (var_id, var_val) in enumerate(engine_variables):
            split_id = var_id.split('-', 1)
            if split_id[0] == 'dictation':
                var_id = split_id[1]
            if path not in self.node_map:
                path = ()
                continue
            next_path = path + (var_id,)
            if next_path not in self.node_map:
                val = engine_variables[i - 1][1]
                action = self.leaf_action(self.node_map[path].node, val)
                full_path_engine_variables[path].append(action)
                next_path = self.next_path(list(path), var_id)
            path = next_path
        if path:
            val = engine_variables[-1][1]
            action = self.leaf_action(self.node_map[path].node, val)
            full_path_engine_variables[path].append(action)
        return full_path_engine_variables

    def next_path(self, current_path, next_end):
        next_path = []
        while current_path:
            path = tuple(current_path + [next_end])
            if path in self.node_map:
                return path
            current_path.pop()
        return (next_end,)

    def leaf_action(self, node, result_text):
        if getattr(node, 'action_substitute', None) is not None:
            return node.action_substitute
        if isinstance(node, astree.WordNode):
           return nodes.LiteralKeysAction(node.text)
        if isinstance(node, astree.Rule) and node.name == '_dictate':
           return nodes.LiteralKeysAction(result_text)

    def action_variables(self, engine_variables):
        results = collections.OrderedDict({path: nodes.RootAction() for path in self.variables})
        full_path_engine_variables = self.get_full_path_engine_variables(engine_variables)
        for i, (full_path, actions) in enumerate(full_path_engine_variables.items()):
            for grouping_path, node_wrapper in self.variables.items():
                if full_path[:len(grouping_path)] == grouping_path:
                    results[grouping_path].children.extend(actions)
        return list(results.values())

class RuleNodeWrapper:

    def __init__(self, node, ancestors, node_ids):
        self.node = node
        self.ancestors = ancestors
        self.full_path = tuple([node_ids[n] for n in ancestors] + [node_ids[node]])

    @property
    def path(self):
        return tuple(self.full_path[1:])<commit_msg>variable results cleanup<commit_after>import collections
from sprecgrammars.rules import astree
from sprecgrammars.actions import nodes

class RecognitionResultsTree:

    def __init__(self, root_rule_node, node_ids):
        self.root_rule_node = root_rule_node
        self.node_ids = node_ids
        self.initialize_fields()

    def walk_tree(self):
        for node_info in self.root_rule_node.walk():
            yield RuleNodeWrapper(node_info['node'], node_info['ancestors'], self.node_ids)

    def initialize_fields(self):
        self.node_map = {}
        self.variables = []
        for node_wrapper in self.walk_tree():
            is_ambiguous = (isinstance(node_wrapper.node, astree.GroupingNode) or
                isinstance(node_wrapper.node, astree.Rule) and node_wrapper.node.name == '_dictate')
            if is_ambiguous:
                self.variables.append(node_wrapper.path)
            self.node_map[node_wrapper.path] = node_wrapper

    def get_full_path_engine_variables(self, engine_variables):
        full_path_engine_variables = collections.defaultdict(list)
        path = ()
        for i, (var_id, var_val) in enumerate(engine_variables):
            split_id = var_id.split('-', 1)
            if split_id[0] == 'dictation':
                var_id = split_id[1]
            if path not in self.node_map:
                path = ()
                continue
            next_path = path + (var_id,)
            if next_path not in self.node_map:
                val = engine_variables[i - 1][1]
                action = self.leaf_action(self.node_map[path].node, val)
                full_path_engine_variables[path].append(action)
                next_path = self.next_path(list(path), var_id)
            path = next_path
        if path:
            val = engine_variables[-1][1]
            action = self.leaf_action(self.node_map[path].node, val)
            full_path_engine_variables[path].append(action)
        return full_path_engine_variables

    def next_path(self, current_path, next_end):
        next_path = []
        while current_path:
            path = tuple(current_path + [next_end])
            if path in self.node_map:
                return path
            current_path.pop()
        return (next_end,)

    def leaf_action(self, node, result_text):
        if getattr(node, 'action_substitute', None) is not None:
            return node.action_substitute
        if isinstance(node, astree.WordNode):
           return nodes.LiteralKeysAction(node.text)
        if isinstance(node, astree.Rule) and node.name == '_dictate':
           return nodes.LiteralKeysAction(result_text)

    def action_variables(self, engine_variables):
        results = collections.OrderedDict({path: nodes.RootAction() for path in self.variables})
        full_path_engine_variables = self.get_full_path_engine_variables(engine_variables)
        for full_path, actions in full_path_engine_variables.items():
            for variable_path in self.variables:
                if full_path[:len(variable_path)] == variable_path:
                    results[variable_path].children.extend(actions)
        return list(results.values())

class RuleNodeWrapper:

    def __init__(self, node, ancestors, node_ids):
        self.node = node
        self.ancestors = ancestors
        self.full_path = tuple([node_ids[n] for n in ancestors] + [node_ids[node]])

    @property
    def path(self):
        return tuple(self.full_path[1:])<|endoftext|>


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

from org.wikiup.database.orm import WikiupEntityManager
from org.wikiup.modules.jython.orm import PythonEntity

def getEntity(name, ctx = None):
	return PythonEntity(_getEntity(name, ctx))

def query(name, condition = {} ,ctx = None):
	ent = _getEntity(name, ctx)
	for k in condition:
		ent.set(k, condition[k])
	ent.select()
	return PythonEntity(ent)

def _getEntity(name, ctx):
	return ctx.getEntity(name) if ctx is not None else WikiupEntityManager.getInstance().getEntity(name)<commit_msg>--------<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from org.wikiup.database.orm import WikiupEntityManager
from org.wikiup.modules.jython.orm import PythonEntity

def getEntity(name, condition=None, ctx=None):
    return PythonEntity(_getEntity(name, condition, ctx))

def query(name, relation, condition=None ,ctx=None):
    entity = _getEntity(name, condition, ctx)
    relatives = entity.getRelatives(relation, None)
    props = [str(i.getName()) for i in relatives.getAttributes()]
    return [dict([(j, str(i.getAttribute(j))) for j in props]) for i in relatives.getChildren()]

def _getEntity(name, condition=None, ctx=None):
    entity = (ctx or WikiupEntityManager.getInstance()).getEntity(name)
    if condition:
        for k in condition:
            entity.set(k, condition[k])
        entity.select()
    return entity<|endoftext|>


<commit_before><commit_msg>Removed useless plugins directory<commit_after><|endoftext|>


<commit_before>#  -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#    Copyright (C) 2010-2010 Camptocamp Austria (<http://www.camptocamp.at>)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import osv, fields
import decimal_precision as dp

import math
#from _common import rounding
import re
from tools.translate import _

import sys

#----------------------------------------------------------
# Price Unit
#----------------------------------------------------------
class c2c_product_price_unit(osv.osv):
    _name = "c2c_product.price_unit"
    _description = 'Product Price Unit'
    _columns = {
       'code'               : fields.char    ('PU', size=3,  translate=True, help="a 3 char code to pe placed next to the price"),
       'coefficient'        : fields.integer ('Coefficient', required=True, help="Values will be calculated as price / coefficient "),
       'name'               : fields.char    ('Price per Unit', size=32, required=True, translate=True, help="Enter something like: Price for 100 units"),
    }
    _order = "coefficient asc,name"

    def get_coeff(self, cr, uid, price_unit_id, context=None):
    #def get_coeff(self, cr, uid, price_unit_id):
        if not context:
            context = {}

        print >> sys.stderr , 'price_unit_id',  price_unit_id
        coeff = 1.0
        if price_unit_id:
           cr.execute('select coefficient from c2c_product_price_unit where id=%s' , (price_unit_id,))
        #res = cr.fetchone()[0] or 1.0
           coeff = cr.fetchone()[0]
           print >> sys.stderr , 'coeff', coeff
        return coeff

    def get_default_id(self, cr, uid, price_unit_id, context=None):
        if context is None:
            context = {}

        print >> sys.stderr , 'price_unit_id',  price_unit_id

        if not price_unit_id:
           cr.execute('select min(id) from c2c_product_price_unit where coefficient=1' )
        #res = cr.fetchone()[0] or 1.0
           price_unit_id = cr.fetchone()[0] or ''

           print >> sys.stderr , 'pu default', price_unit_id
        return price_unit_id

c2c_product_price_unit()

#----------------------------------------------------------
# Product INHERIT
#----------------------------------------------------------
class product_template(osv.osv):
    _inherit = "product.template"

    def _get_default_id(self, cr, uid, price_unit_id, context=None):
       print >>sys.stderr,'invoice pi_id ',price_unit_id
       pu = self.pool.get('c2c_product.price.unit')
       if not pu: return
       return pu.get_default_id(cr, uid, price_unit_id, context)

    _columns = {
        'price_unit_id'    :fields.many2one('c2c_product.price_unit','Price Unit'),
        'standard_price_pu':fields.float(string='Cost Price PU',digits_compute=dp.get_precision('Purchase Price') , \
                            help='Price using "Price Units"') ,
        'list_price_unit_id'    :fields.many2one('c2c_product.price_unit','Price Unit'),
        'list_price_pu'    :fields.float(string='List Price PU',digits_compute=dp.get_precision('Sale Price'), \
                            help='Price using "Price Units"'),
        'standard_price'   :fields.float(string='Cost Price',  digits=(16, 8), help="Product's cost for accounting stock valuation. It is the base price for the supplier price."),
        'list_price'       :fields.float('Sale Price', digits=(16, 8), help="Base price for computing the customer price. Sometimes called the catalog price."),

    }
    def init(self, cr):
      cr.execute("""
update product_template set standard_price_pu=standard_price  where standard_price_pu is null;
      """)
      cr.execute("""
update product_template set price_unit_id = (select min(id) from c2c_product_price_unit where coefficient=1) where price_unit_id is null;
      """)
      cr.execute("""
update product_template set list_price_pu=list_price  where list_price_pu is null;
      """)
      cr.execute("""
update product_template set list_price_unit_id = (select min(id) from c2c_product_price_unit where coefficient=1) where list_price_unit_id is null;
      """)

    _columns = {
        'price_unit_id'    :fields.many2one('c2c_product.price_unit','Price Unit', required=True),
        'standard_price_pu':fields.float(string='Cost Price PU',digits_compute=dp.get_precision('Purchase Price') , required=True, \
                            help='Price using "Price Units"') ,
        'list_price_unit_id'    :fields.many2one('c2c_product.price_unit','Price Unit', required=True),
        'list_price_pu'    :fields.float(string='List Price PU',digits_compute=dp.get_precision('Sale Price'), \
                            help='Price using "Price Units"'),
        'standard_price'   :fields.float(string='Cost Price', required=True, digits=(16, 8), help="Product's cost for accounting stock valuation. It is the base price for the supplier price."),
        'list_price'       :fields.float('Sale Price', digits=(16, 8), help="Base price for computing the customer price. Sometimes called the catalog price."),

    }
    _defaults = {
        'price_unit_id'   : _get_default_id,
        'standard_price_pu': 0.0,
        'list_price_unit_id'   : _get_default_id,
        'list_price_pu': 0.0,
        'standard_price': 0.0,

    }
    def init(self, cr):
      cr.execute("""
update product_template set standard_price_pu=standard_price  where standard_price_pu is null;
      """)
product_template()

class product_product(osv.osv):
    _inherit = "product.product"
    def onchange_price_unit(self, cr, uid, ids, field_name,price_pu, price_unit_id):
        if  price_pu and  price_unit_id:
           pu = self.pool.get('c2c_product.price_unit').browse(cr, uid, price_unit_id)
           price = price_pu / float(pu.coefficient)
           return {'value': {field_name : price}}
        return False

product_product()

<commit_msg>[FIX] removed print<commit_after>#  -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#    Copyright (C) 2010-2010 Camptocamp Austria (<http://www.camptocamp.at>)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import osv, fields
import decimal_precision as dp

import math
#from _common import rounding
import re
from tools.translate import _

import sys

#----------------------------------------------------------
# Price Unit
#----------------------------------------------------------
class c2c_product_price_unit(osv.osv):
    _name = "c2c_product.price_unit"
    _description = 'Product Price Unit'
    _columns = {
       'code'               : fields.char    ('PU', size=3,  translate=True, help="a 3 char code to pe placed next to the price"),
       'coefficient'        : fields.integer ('Coefficient', required=True, help="Values will be calculated as price / coefficient "),
       'name'               : fields.char    ('Price per Unit', size=32, required=True, translate=True, help="Enter something like: Price for 100 units"),
    }
    _order = "coefficient asc,name"

    def get_coeff(self, cr, uid, price_unit_id, context=None):
    #def get_coeff(self, cr, uid, price_unit_id):
        if not context:
            context = {}

        #print >> sys.stderr , 'price_unit_id',  price_unit_id
        coeff = 1.0
        if price_unit_id:
           cr.execute('select coefficient from c2c_product_price_unit where id=%s' , (price_unit_id,))
        #res = cr.fetchone()[0] or 1.0
           coeff = cr.fetchone()[0]
           #print >> sys.stderr , 'coeff', coeff
        return coeff

    def get_default_id(self, cr, uid, price_unit_id, context=None):
        if context is None:
            context = {}

        #print >> sys.stderr , 'price_unit_id',  price_unit_id

        if not price_unit_id:
           cr.execute('select min(id) from c2c_product_price_unit where coefficient=1' )
        #res = cr.fetchone()[0] or 1.0
           price_unit_id = cr.fetchone()[0] or ''

           #print >> sys.stderr , 'pu default', price_unit_id
        return price_unit_id

c2c_product_price_unit()

#----------------------------------------------------------
# Product INHERIT
#----------------------------------------------------------
class product_template(osv.osv):
    _inherit = "product.template"

    def _get_default_id(self, cr, uid, price_unit_id, context=None):
       #print >>sys.stderr,'invoice pi_id ',price_unit_id
       pu = self.pool.get('c2c_product.price.unit')
       if not pu: return
       return pu.get_default_id(cr, uid, price_unit_id, context)

    _columns = {
        'price_unit_id'    :fields.many2one('c2c_product.price_unit','Price Unit'),
        'standard_price_pu':fields.float(string='Cost Price PU',digits_compute=dp.get_precision('Purchase Price') , \
                            help='Price using "Price Units"') ,
        'list_price_unit_id'    :fields.many2one('c2c_product.price_unit','Price Unit'),
        'list_price_pu'    :fields.float(string='List Price PU',digits_compute=dp.get_precision('Sale Price'), \
                            help='Price using "Price Units"'),
        'standard_price'   :fields.float(string='Cost Price',  digits=(16, 8), help="Product's cost for accounting stock valuation. It is the base price for the supplier price."),
        'list_price'       :fields.float('Sale Price', digits=(16, 8), help="Base price for computing the customer price. Sometimes called the catalog price."),

    }
    def init(self, cr):
      cr.execute("""
update product_template set standard_price_pu=standard_price  where standard_price_pu is null;
      """)
      cr.execute("""
update product_template set price_unit_id = (select min(id) from c2c_product_price_unit where coefficient=1) where price_unit_id is null;
      """)
      cr.execute("""
update product_template set list_price_pu=list_price  where list_price_pu is null;
      """)
      cr.execute("""
update product_template set list_price_unit_id = (select min(id) from c2c_product_price_unit where coefficient=1) where list_price_unit_id is null;
      """)

    _columns = {
        'price_unit_id'    :fields.many2one('c2c_product.price_unit','Price Unit', required=True),
        'standard_price_pu':fields.float(string='Cost Price PU',digits_compute=dp.get_precision('Purchase Price') , required=True, \
                            help='Price using "Price Units"') ,
        'list_price_unit_id'    :fields.many2one('c2c_product.price_unit','Price Unit', required=True),
        'list_price_pu'    :fields.float(string='List Price PU',digits_compute=dp.get_precision('Sale Price'), \
                            help='Price using "Price Units"'),
        'standard_price'   :fields.float(string='Cost Price', required=True, digits=(16, 8), help="Product's cost for accounting stock valuation. It is the base price for the supplier price."),
        'list_price'       :fields.float('Sale Price', digits=(16, 8), help="Base price for computing the customer price. Sometimes called the catalog price."),

    }
    _defaults = {
        'price_unit_id'   : _get_default_id,
        'standard_price_pu': 0.0,
        'list_price_unit_id'   : _get_default_id,
        'list_price_pu': 0.0,
        'standard_price': 0.0,

    }
    def init(self, cr):
      cr.execute("""
update product_template set standard_price_pu=standard_price  where standard_price_pu is null;
      """)
product_template()

class product_product(osv.osv):
    _inherit = "product.product"
    def onchange_price_unit(self, cr, uid, ids, field_name,price_pu, price_unit_id):
        if  price_pu and  price_unit_id:
           pu = self.pool.get('c2c_product.price_unit').browse(cr, uid, price_unit_id)
           price = price_pu / float(pu.coefficient)
           return {'value': {field_name : price}}
        return False

product_product()

<|endoftext|>


<commit_before>import random
from django.shortcuts import render, get_object_or_404
from web.views_collections import item_view, list_view
from web.settings import ENABLED_COLLECTIONS
from web.views import _index_extraContext as web_index_extraContext
from cpro import models, filters
from web.utils import ajaxContext

def _index_extraContext(context):
    web_index_extraContext(context)
    context['card'] = models.Card.objects.order_by('?').filter(art__isnull=False).exclude(art='')[0]
    context['awakened'] = random.choice([True, False]) if context['card'].id_awakened else False

def index(request):
    collection = ENABLED_COLLECTIONS['activity'].copy()
    collection['list'] = collection['list'].copy()
    collection['list']['before_template'] = 'include/index'
    collection['list']['extra_context'] = _index_extraContext
    collection['list']['full_width'] = True
    del(collection['list']['filter_form'])
    return list_view(request, 'activity', collection)

def cardstat(request, card):
    context = ajaxContext(request)
    context['card'] = get_object_or_404(models.Card, pk=card)
    return render(request, 'include/cards-stats.html', context)

def cardcollection(request, card):
    collection = ENABLED_COLLECTIONS['card'].copy()
    collection['item'] = collection['item'].copy()
    request.GET = request.GET.copy()
    request.GET['collection'] = True
    collection['item']['template'] = '../include/cards-collection'
    return item_view(request, 'card', collection, pk=card, ajax=True)

def addcard(request, card):
    collection = 'collection' in request.GET
    queryset = models.Card
    if not collection:
        # Note: calling filterCards will add extra info need to display the card
        queryset = filters.filterCards(models.Card.objects.all(), {}, request)
    card = get_object_or_404(queryset, pk=card)
    account = get_object_or_404(models.Account, pk=request.POST.get('account', None), owner=request.user)
    models.OwnedCard.objects.create(card=card, account=account)
    if not collection:
        card.total_owned += 1
    if collection:
        return cardcollection(request, card.id)
    else:
        return item_view(request, 'card', ENABLED_COLLECTIONS['card'], pk=card.id, item=card, ajax=True)

def account_about(request, account):
    context = ajaxContext(request)
    context['account'] = get_object_or_404(models.Account.objects.select_related('starter'), pk=account)
    return render(request, 'ajax/account_about.html', context)
<commit_msg>Fix filter_form when not in collection<commit_after>import random
from django.shortcuts import render, get_object_or_404
from web.views_collections import item_view, list_view
from web.settings import ENABLED_COLLECTIONS
from web.views import _index_extraContext as web_index_extraContext
from cpro import models, filters
from web.utils import ajaxContext

def _index_extraContext(context):
    web_index_extraContext(context)
    context['card'] = models.Card.objects.order_by('?').filter(art__isnull=False).exclude(art='')[0]
    context['awakened'] = random.choice([True, False]) if context['card'].id_awakened else False

def index(request):
    collection = ENABLED_COLLECTIONS['activity'].copy()
    collection['list'] = collection['list'].copy()
    collection['list']['before_template'] = 'include/index'
    collection['list']['extra_context'] = _index_extraContext
    collection['list']['full_width'] = True
    if 'filter_form' in collection['list']:
        del(collection['list']['filter_form'])
    return list_view(request, 'activity', collection)

def cardstat(request, card):
    context = ajaxContext(request)
    context['card'] = get_object_or_404(models.Card, pk=card)
    return render(request, 'include/cards-stats.html', context)

def cardcollection(request, card):
    collection = ENABLED_COLLECTIONS['card'].copy()
    collection['item'] = collection['item'].copy()
    request.GET = request.GET.copy()
    request.GET['collection'] = True
    collection['item']['template'] = '../include/cards-collection'
    return item_view(request, 'card', collection, pk=card, ajax=True)

def addcard(request, card):
    collection = 'collection' in request.GET
    queryset = models.Card
    if not collection:
        # Note: calling filterCards will add extra info need to display the card
        queryset = filters.filterCards(models.Card.objects.all(), {}, request)
    card = get_object_or_404(queryset, pk=card)
    account = get_object_or_404(models.Account, pk=request.POST.get('account', None), owner=request.user)
    models.OwnedCard.objects.create(card=card, account=account)
    if not collection:
        card.total_owned += 1
    if collection:
        return cardcollection(request, card.id)
    else:
        return item_view(request, 'card', ENABLED_COLLECTIONS['card'], pk=card.id, item=card, ajax=True)

def account_about(request, account):
    context = ajaxContext(request)
    context['account'] = get_object_or_404(models.Account.objects.select_related('starter'), pk=account)
    return render(request, 'ajax/account_about.html', context)
<|endoftext|>


<commit_before>#!/usr/bin/env python
import sys
__actions = ("acf", "slk", "fdr", "peaks", "region_p", "hist", "splot",
             "manhattan")

def main():
    if len(sys.argv) == 1 or sys.argv[1] in ("-h", "--help"):
        print >>sys.stderr,"""
Tools for viewing and adjusting p-values in BED files.

   Contact: Brent Pedersen - bpederse@gmail.com
   License: BSD

To run, indicate one of:

   acf       - calculate autocorrelation within BED file
   slk       - Stouffer-Liptak-Kechris correction of correlated p-values
   fdr       - Benjamini-Hochberg correction of p-values
   peaks     - find peaks in a BED file.
   region_p  - generate SLK p-values for a region (of p-values)
   hist      - plot a histogram of a column and check for uniformity.
   splot     - a scatter plot of column(s) in a bed file for a given region.
   manhattan - a manhattan plot of values in a BED file.

NOTE: most of these assume *sorted* BED files.
SEE: https://github.com/brentp/combined-pvalues for documentation
    """
        sys.exit()
    if not sys.argv[1] in __actions:
        sys.argv.pop(1)
        _pipeline()
    else:
        module = __import__(sys.argv[1])
        # remove the action
        sys.argv.pop(1)
        module.main()

def _pipeline():
    import sys
    import os.path as op
    sys.path.insert(0, op.join(op.dirname(__file__), ".."))
    from cpv import acf, slk, fdr, peaks, region_p
    from _common import get_col_num
    import argparse
    import operator

    p = argparse.ArgumentParser(description=__doc__,
                   formatter_class=argparse.RawDescriptionHelpFormatter)

    p.add_argument("-c", dest="c", help="column number that has the value to"
                   "take the  acf", type=int, default=4)
    p.add_argument("--tau", help="tau for the truncated product",
                   type=float, default=0.1)
    p.add_argument("--dist", dest="dist", help="Maximum dist to extend the"
             " ACF calculation", type=int)
    p.add_argument("--step", dest="step", help="step size for the"
             " ACF calculation", type=int)

    p.add_argument("--seed", dest="seed", help="A value must be at least this"
                 " large/small in order to seed a region.", type=float,
                 default=0.1)
    p.add_argument("--threshold", dest="threshold", help="After seeding, a value"
                 " of at least this number can extend a region. ",
                 type=float)
    p.add_argument("--random", action="store_true", default=False,
            help="If specified, sample from the uniform distribution rather "
            "than the observed p-values. The latter deflates significance.")

    p.add_argument("-p", dest="prefix", help="prefix for output files",
                   default=None)

    p.add_argument('files', nargs='+', help='files to process')

    args = p.parse_args()

    if not (args.prefix and args.step):
        sys.exit(p.print_help())

    if not args.threshold:
        args.threshold = args.seed

    lags = range(1, args.dist, args.step)
    lags.append(lags[-1] + args.step)
    col_num = get_col_num(args.c)

    acf_vals = acf.acf(args.files, lags, col_num, simple=False)

    with open(args.prefix + ".acf.txt", "w") as fh:
        acf_vals = acf.write_acf(acf_vals, fh)

    print >>sys.stderr, "ACF:\n", open(args.prefix + ".acf.txt").read()
    with open(args.prefix + ".slk.bed", "w") as fh:
        for row in slk.adjust_pvals(args.files, col_num, acf_vals):
            fh.write("%s\t%i\t%i\t%.4g\t%.4g\n" % row)
        print >>sys.stderr, "wrote: %s" % fh.name

    with open(args.prefix + ".fdr.bed", "w") as fh:
        for bh, l in fdr.fdr(args.prefix + ".slk.bed", -1, 0.05):
            fh.write("%s\t%.4g\n" % (l.rstrip("\r\n"), bh))
        print >>sys.stderr, "wrote: %s" % fh.name

    fregions = args.prefix + ".regions.bed"
    with open(fregions, "w") as fh:
        peaks.peaks(args.prefix + ".fdr.bed", -1, args.threshold, args.seed,
            args.step, fh, operator.le)
        print >>sys.stderr, "wrote: %s" % fh.name
    print >>sys.stderr, "%i regions" % (sum(1 for _ in open(fregions)))

    with open(args.prefix + ".regions-p.bed", "w") as fh:
        fh.write("#chrom\tstart\tend\tmin-p\tn-probes\tslk-p\tslk-sidak-p\n")
        # use -2 for original, uncorrected p-values in slk.bed
        for region_line, slk_p, slk_sidak_p in region_p.region_p(
                               args.prefix + ".slk.bed",
                               args.prefix + ".regions.bed", -2,
                               5000, args.tau, args.step,
                               random=args.random):
            fh.write("%s\t%.4g\t%.4g\n" % (region_line, slk_p, slk_sidak_p))
        print >>sys.stderr, "wrote: %s" % fh.name

if __name__ == "__main__":
    main()
<commit_msg>implement guessing of step-size and truncation of ACF if no correlation. reduces false+<commit_after>#!/usr/bin/env python
import sys
__actions = ("acf", "slk", "fdr", "peaks", "region_p", "hist", "splot",
             "manhattan")

def main():
    if len(sys.argv) == 1 or sys.argv[1] in ("-h", "--help"):
        print >>sys.stderr,"""
Tools for viewing and adjusting p-values in BED files.

   Contact: Brent Pedersen - bpederse@gmail.com
   License: BSD

To run, indicate one of:

   acf       - calculate autocorrelation within BED file
   slk       - Stouffer-Liptak-Kechris correction of correlated p-values
   fdr       - Benjamini-Hochberg correction of p-values
   peaks     - find peaks in a BED file.
   region_p  - generate SLK p-values for a region (of p-values)
   hist      - plot a histogram of a column and check for uniformity.
   splot     - a scatter plot of column(s) in a bed file for a given region.
   manhattan - a manhattan plot of values in a BED file.

NOTE: most of these assume *sorted* BED files.
SEE: https://github.com/brentp/combined-pvalues for documentation
    """
        sys.exit()
    if not sys.argv[1] in __actions:
        sys.argv.pop(1)
        _pipeline()
    else:
        module = __import__(sys.argv[1])
        # remove the action
        sys.argv.pop(1)
        module.main()

def _pipeline():
    import sys
    import os.path as op
    sys.path.insert(0, op.join(op.dirname(__file__), ".."))
    from cpv import acf, slk, fdr, peaks, region_p, stepsize
    from _common import get_col_num
    import argparse
    import operator

    p = argparse.ArgumentParser(description=__doc__,
                   formatter_class=argparse.RawDescriptionHelpFormatter)

    p.add_argument("-c", dest="c", help="column number that has the value to"
                   "take the  acf", type=int, default=4)
    p.add_argument("--tau", help="tau for the truncated product",
                   type=float, default=0.1)
    p.add_argument("--dist", dest="dist", help="Maximum dist to extend the"
             " ACF calculation", type=int)
    p.add_argument("--seed", dest="seed", help="A value must be at least this"
                 " large/small in order to seed a region.", type=float,
                 default=0.1)
    p.add_argument("--threshold", dest="threshold", help="After seeding, a value"
                 " of at least this number can extend a region. ",
                 type=float)
    p.add_argument("--random", action="store_true", default=False,
            help="If specified, sample from the uniform distribution rather "
            "than the observed p-values. The latter deflates significance.")

    p.add_argument("-p", dest="prefix", help="prefix for output files",
                   default=None)

    p.add_argument('bed_file', help='sorted bed file to process')

    args = p.parse_args()

    if not (args.prefix):
        sys.exit(p.print_help())

    if not args.threshold:
        args.threshold = args.seed

    col_num = get_col_num(args.c)
    step = stepsize.stepsize(args.bed_file, col_num)
    print >>sys.stderr, "calculcated stepsize as: %i" % step

    lags = range(1, args.dist, step)
    lags.append(lags[-1] + step)

    # go out to max requested distance but stop once an autocorrelation
    # < 0.05 is added.
    CUTOFF = 0.05
    putative_acf_vals = acf.acf((args.bed_file,), lags, col_num, simple=False)
    acf_vals = []
    for a in putative_acf_vals:
        # a is ((lmin, lmax), (corr, N))
        acf_vals.append(a)
        if a[1][0] < CUTOFF: break

    with open(args.prefix + ".acf.txt", "w") as fh:
        acf_vals = acf.write_acf(acf_vals, fh)
        print >>sys.stderr, "wrote: %s" % fh.name

    print >>sys.stderr, "ACF:\n", open(args.prefix + ".acf.txt").read()
    with open(args.prefix + ".slk.bed", "w") as fh:
        for row in slk.adjust_pvals((args.bed_file,), col_num, acf_vals):
            fh.write("%s\t%i\t%i\t%.4g\t%.4g\n" % row)
        print >>sys.stderr, "wrote: %s" % fh.name

    with open(args.prefix + ".fdr.bed", "w") as fh:
        for bh, l in fdr.fdr(args.prefix + ".slk.bed", -1, 0.05):
            fh.write("%s\t%.4g\n" % (l.rstrip("\r\n"), bh))
        print >>sys.stderr, "wrote: %s" % fh.name

    fregions = args.prefix + ".regions.bed"
    with open(fregions, "w") as fh:
        peaks.peaks(args.prefix + ".fdr.bed", -1, args.threshold, args.seed,
            step, fh, operator.le)
    n_regions = sum(1 for _ in open(fregions))
    print >>sys.stderr, "wrote: %s (%i regions)" % (fregions, n_regions)

    with open(args.prefix + ".regions-p.bed", "w") as fh:
        N = 0
        fh.write("#chrom\tstart\tend\tmin-p\tn-probes\tslk-p\tslk-sidak-p\n")
        # use -2 for original, uncorrected p-values in slk.bed
        for region_line, slk_p, slk_sidak_p in region_p.region_p(
                               args.prefix + ".slk.bed",
                               args.prefix + ".regions.bed", -2,
                               5000, args.tau, step,
                               random=args.random):
            fh.write("%s\t%.4g\t%.4g\n" % (region_line, slk_p, slk_sidak_p))
            N += int(slk_sidak_p < 0.05)
        print >>sys.stderr, "wrote: %s, (regions with corrected-p < 0.05: %i)" \
                % (fh.name, N)

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