<commit_before>class Node:

    def __init__(self, value):
        """
        Creates a list node.
        :param value: The value of the node.
        """
        self.next = None
        self.value = value

    def __str__(self):
        """
        Gets a string representation of this node.
        :return: A string representing this node.
        """
        if self.get_next():
            return str(self.get_value()) + ", " + self.get_next().__str__()
        else:
            return str(self.get_value()) + "]"

    def contains(self, value):
        """
        Recursive function used to determine if a list contains a value.
        :param value: The value to search for.
        :return: True if the node has the specified value. False if the end of the list is reached and the
        value was not found.
        """
        if self.get_value() == value:
            return True
        if self.get_next():
            return self.get_next().contains(value )
        return False

    def count(self):
        """
        Recursive function used to determine the size of a list.
        :return: The size of the list.
        """
        if self.next:
            return self.next.count() + 1
        return 1

    def get(self, index):
        """
        Recursive function used to get a value at a specific position in the list.
        :param index: The index of the item in the list.
        :return: The value at the index in the list.
        :raises: KeyError if the index is out of range.
        """
        if index == 0:
            return self.get_value()
        if self.get_next():
            return self.get_next().get(index-1)
        raise KeyError

    def set(self, index, value):
        """
        Sets the value at the specified index to the new value.
        :param index: The index to update.
        :param value: The new value to update to.
        :return: Nothing.
        :raises: KeyError if the index is out of range.
        """
        if index == 0:
            self.set_value(value)
        if self.get_next():
            self.get_next().set(index-1, value)
        raise KeyError

    def get_next(self):
        """
        Returns the next node after this one.
        :return: The next node.
        """
        return self.next

    def set_next(self, node):
        """
        Updates the pointer to the next node.
        :param node: The node to make next.
        :return: Nothing
        """
        self.next = node

    def get_value(self):
        """
        Returns the value of this node.
        :return: The value of this node.
        """
        return self.value

    def set_value(self, value):
        """
        Updates the value of this node.
        :param value: The value to update the node to.
        :return: Nothing
        """
        self.value = value

    def swap_values(self, node):
        """
        Swaps the value of this node with the node specified.
        :param node: The node to swap values with.
        :return: Nothing
        """
        if node:
            temp = self.get_value()
            self.set_value(node.get_value())
            node.set_value(temp)

class PriorityNode(Node):

    def __init__(self, priority, value):
        """
        Creates a specialized node for use in a priority queue.
        :param priority: The priority assigned to this node.
        :param value: The value that corresponds to this node.
        """
        Node.__init__(self, value)
        self.previous = None
        self.priority = priority

    def __str__(self):
        """
        Gets a string representation of this node.
        :return: A string representing this node.
        """
        if self.get_next():
            return "{0}:{1}, {2}".format(self.get_priority(), self.get_value(), self.get_next().__str__())
        else:
            return "{0}:{1}]".format(self.get_priority(), self.get_value())

    def get_previous(self):
        """
        Returns the previous node in the queue.
        :return: The previous node.
        """
        return self.previous

    def set_previous(self, node):
        """
        Updates the pointer to the previous node.
        :param node: Updates the previous node in the queue.
        :return: Nothing
        """
        self.previous = node

    def get_priority(self):
        """
        Returns the priority assigned to this node.
        :return: The priority of this node.
        """
        return self.priority

    def set_priority(self, priority):
        """
        Updates the priority of this node.
        :param priority: The new priority for this node.
        :return: Nothing
        """
        self.priority = priority

class LinkedList:

    def __init__(self):
        """
        Creats an empty linked list.
        """
        self.head = None

    def __str__(self):
        """
        A string representation of the items in the list.
        :return: A string representing the list.
        """
        if self.head:
            return "[" + self.head.__str__()
        else:
            return "Empty"

    def add(self, value):
        """
        Adds a value to the front of the list.
        :param value: The value to add.
        :return: nothing
        """
        node = Node(value)
        node.set_next(self.head)
        self.head = node

    def contains(self, value):
        """
        Determines if the list contains a specified value.
        :param value: The value to search for.
        :return: True if an instance of this value is found. False if not.
        """
        if self.head:
            return self.head.contains(value)

    def count(self):
        """
        Returns the amount of items in the list.
        :return: The amount of items in the list.
        """
        if self.head:
            return self.head.count()
        return 0

    def get(self, index):
        """
        Gets the value at the specified position in the list.
        :param index: The position of the value in the list.
        :return: The value if the specified index is in range of the length of the list. None otherwise.
        """
        if self.head:
            return self.head.get_next()
        return None

    def is_empty(self):
        """
        Determines if the list is empty or not.
        :return: True if the list contains items. False otherwise.
        """
        return True if not self.head else False

    def remove(self, value):
        """
        Removes the first instance of the specified value from the list.
        :param value: The value to remove from the list.
        :return: True if the value was successfully removed. False otherwise.
        """
        if self.head:
            # Case 1: First item is what is being removed
            if self.head.get_value() == value:
                self.head = self.head.get_next()
                return True
            # Case 2: Item is somewhere in the list, search for it
            previous = self.head
            while previous.get_next():
                current = previous.get_next()
                if current.get_value() == value:
                    previous.set_next(current.get_next())
                    return True
                previous = current
        return False

    def set(self, index, value):
        """
        Sets the value at the specified index to the new value.
        :param index: The index to update.
        :param value: The value to update to.
        :return: Nothing
        """
        if self.head:
            self.head.set(index, value)

class Queue(LinkedList):

    def __init__(self):
        """
        Creates an empty queue.
        """
        LinkedList.__init__(self)

    def add(self, value):
        """
        Not implemented for a queue.
        :param value: N/A
        :return: Nothing
        """
        pass

    def dequeue(self):
        """
        Removes a value from the front of the queue and returns it.
        :return: Nothing
        """
        if self.head:
            value = self.head.get_value()
            self.head = self.head.get_next()
            return value
        return None

    def enqueue(self, value):
        """
        Adds an item to the back of the queue.
        :param value: The value to add to the queue.
        :return: Nothing
        """
        if self.head:
            current = self.head
            while current.get_next():
                current = current.get_next()
            node = Node(value)
            node.set_previous(current)
            current.set_next(node)
        else:
            node = Node(value)
            node.set_next(self.head)
            self.head = node

    def get(self, index):
        """
        Not implemented for a queue.
        :param index: N/A
        :return: Nothing
        """
        pass

    def remove(self, value):
        """
        Not implemented for a queue.
        :param value: N/A
        :return: Nothing
        """
        pass

    def set(self, index, value):
        """
        Not implemented for a queue.
        :param index: N/A
        :param value: N/A
        :return: Nothing
        """
        pass

class PriorityQueue(Queue):

    def __init__(self):
        """
        Creates a minimum priority queue.
        """
        Queue.__init__(self)

    def dequeue(self):
        """
        Removes a value from the front of the queue and returns it.
        :returns Nothing
        """
        if self.head:
            value = self.head.get_value()
            self.head = self.head.get_next()
            self.head.set_previous(None)
            return value
        return None

    def enqueue(self, priority, value):
        """
        Adds an item to the priority queue.
        :param priority: The priority of the item.
        :param value: The value of the item.
        :return: Nothing
        """
        if self.head:
            current = self.head
            while current.get_next():
                current = current.get_next()
            node = PriorityNode(priority, value)
            node.set_previous(current)
            current.set_next(node)
            self.sift_up(node)
        else:
            node = PriorityNode(priority, value)
            self.head = node

    def sift_up(self, node):
        if node.get_previous():
            next = node.get_previous()
            while node.get_previous():
                if next.get_priority() < node.get_priority():
                    break
                else:
                    tp = node.get_priority()
                    tv = node.get_value()
                    node.set_priority(next.get_priority())
                    node.set_value(next.get_value())
                    next.set_priority(tp)
                    next.set_value(tv)
                    node = next
                    next = node.get_previous()

class Stack(LinkedList):

    def __init__(self):
        """
        Creates an empty stack.
        """
        LinkedList.__init__(self)

    def add(self, value):
        """
        Not implemented for a stack.
        :param value: N/A
        :return: Nothing
        """
        pass

    def get(self, index):
        """
        Not implemented for a stack.
        :param index: N/A
        :return: Nothing
        """
        pass

    def peek(self):
        """
        Returns the top value on the stack.
        :return: The top value on the stack.
        """
        return self.head.get_value() if self.head else None

    def pop(self):
        """
        Pops the top value from the stack and returns it.
        :return: The top value on the stack.
        """
        if self.head:
            value = self.head.get_value()
            self.head = self.head.get_next()
            return value
        return None

    def push(self, value):
        """
        Will push a value to the top of the stack.
        :param value: Pushes a value onto the top of the stack.
        :return: Nothing
        """
        node = Node(value)
        node.set_next(self.head)
        self.head = node

    def remove(self, value):
        """
        Not implemented for a stack.
        :param value: N/A
        :return: Nothing
        """
        pass

    def set(self, index, value):
        """
        Not implemented for a stack.
        :param index: N/A
        :param value: N/A
        :return: Nothing
        """
        pass

def main():
    import random, time

    pq = PriorityQueue()
    for i in range(50):
        pq.enqueue(round(random.random()*10, 4), i)
    print(pq)

if __name__ == '__main__':
    main()<commit_msg>Add in a linked-list based set implementation<commit_after>class Node:

    def __init__(self, value):
        """
        Creates a list node.
        :param value: The value of the node.
        """
        self.next = None
        self.value = value

    def __str__(self):
        """
        Gets a string representation of this node.
        :return: A string representing this node.
        """
        if self.get_next():
            return str(self.get_value()) + ", " + self.get_next().__str__()
        else:
            return str(self.get_value()) + "]"

    def contains(self, value):
        """
        Recursive function used to determine if a list contains a value.
        :param value: The value to search for.
        :return: True if the node has the specified value. False if the end of the list is reached and the
        value was not found.
        """
        if self.get_value() == value:
            return True
        if self.get_next():
            return self.get_next().contains(value )
        return False

    def count(self):
        """
        Recursive function used to determine the size of a list.
        :return: The size of the list.
        """
        if self.next:
            return self.next.count() + 1
        return 1

    def get(self, index):
        """
        Recursive function used to get a value at a specific position in the list.
        :param index: The index of the item in the list.
        :return: The value at the index in the list.
        :raises: KeyError if the index is out of range.
        """
        if index == 0:
            return self.get_value()
        if self.get_next():
            return self.get_next().get(index-1)
        raise KeyError

    def set(self, index, value):
        """
        Sets the value at the specified index to the new value.
        :param index: The index to update.
        :param value: The new value to update to.
        :return: Nothing.
        :raises: KeyError if the index is out of range.
        """
        if index == 0:
            self.set_value(value)
        if self.get_next():
            self.get_next().set(index-1, value)
        raise KeyError

    def get_next(self):
        """
        Returns the next node after this one.
        :return: The next node.
        """
        return self.next

    def set_next(self, node):
        """
        Updates the pointer to the next node.
        :param node: The node to make next.
        :return: Nothing
        """
        self.next = node

    def get_value(self):
        """
        Returns the value of this node.
        :return: The value of this node.
        """
        return self.value

    def set_value(self, value):
        """
        Updates the value of this node.
        :param value: The value to update the node to.
        :return: Nothing
        """
        self.value = value

    def swap_values(self, node):
        """
        Swaps the value of this node with the node specified.
        :param node: The node to swap values with.
        :return: Nothing
        """
        if node:
            temp = self.get_value()
            self.set_value(node.get_value())
            node.set_value(temp)

class PriorityNode(Node):

    def __init__(self, priority, value):
        """
        Creates a specialized node for use in a priority queue.
        :param priority: The priority assigned to this node.
        :param value: The value that corresponds to this node.
        """
        Node.__init__(self, value)
        self.previous = None
        self.priority = priority

    def __str__(self):
        """
        Gets a string representation of this node.
        :return: A string representing this node.
        """
        if self.get_next():
            return "{0}:{1}, {2}".format(self.get_priority(), self.get_value(), self.get_next().__str__())
        else:
            return "{0}:{1}]".format(self.get_priority(), self.get_value())

    def get_previous(self):
        """
        Returns the previous node in the queue.
        :return: The previous node.
        """
        return self.previous

    def set_previous(self, node):
        """
        Updates the pointer to the previous node.
        :param node: Updates the previous node in the queue.
        :return: Nothing
        """
        self.previous = node

    def get_priority(self):
        """
        Returns the priority assigned to this node.
        :return: The priority of this node.
        """
        return self.priority

    def set_priority(self, priority):
        """
        Updates the priority of this node.
        :param priority: The new priority for this node.
        :return: Nothing
        """
        self.priority = priority

class LinkedList:

    def __init__(self):
        """
        Creats an empty linked list.
        """
        self.head = None

    def __str__(self):
        """
        A string representation of the items in the list.
        :return: A string representing the list.
        """
        if self.head:
            return "[" + self.head.__str__()
        else:
            return "Empty"

    def add(self, value):
        """
        Adds a value to the front of the list.
        :param value: The value to add.
        :return: nothing
        """
        node = Node(value)
        node.set_next(self.head)
        self.head = node

    def contains(self, value):
        """
        Determines if the list contains a specified value.
        :param value: The value to search for.
        :return: True if an instance of this value is found. False if not.
        """
        if self.head:
            return self.head.contains(value)

    def count(self):
        """
        Returns the amount of items in the list.
        :return: The amount of items in the list.
        """
        if self.head:
            return self.head.count()
        return 0

    def get(self, index):
        """
        Gets the value at the specified position in the list.
        :param index: The position of the value in the list.
        :return: The value if the specified index is in range of the length of the list. None otherwise.
        """
        if self.head:
            return self.head.get_next()
        return None

    def is_empty(self):
        """
        Determines if the list is empty or not.
        :return: True if the list contains items. False otherwise.
        """
        return True if not self.head else False

    def remove(self, value):
        """
        Removes the first instance of the specified value from the list.
        :param value: The value to remove from the list.
        :return: True if the value was successfully removed. False otherwise.
        """
        if self.head:
            # Case 1: First item is what is being removed
            if self.head.get_value() == value:
                self.head = self.head.get_next()
                return True
            # Case 2: Item is somewhere in the list, search for it
            previous = self.head
            while previous.get_next():
                current = previous.get_next()
                if current.get_value() == value:
                    previous.set_next(current.get_next())
                    return True
                previous = current
        return False

    def set(self, index, value):
        """
        Sets the value at the specified index to the new value.
        :param index: The index to update.
        :param value: The value to update to.
        :return: Nothing
        """
        if self.head:
            self.head.set(index, value)

class Set(LinkedList):

    def __init__(self):
        LinkedList.__init__(self)

    def add(self, value):
        if not self.contains(value):
            LinkedList.add(self, value)

    def union(self, set):
        """
        Returns a new set that is the mathematical union of the current set instance and the indicated set.
        :param set: The set to union with.
        :return: The mathematical union of the two sets.
        """
        ns = Set()
        for i in range(self.count()):
            ns.add(self.get(i))
        for i in range(set.count()):
            val = set.get(i)
            if not ns.contains(val):
                ns.add(val)

    def intersection(self, set):
        """
        Returns a new set that is the mathematical intersection of the current set instance and the indicated set.
        :param set: The set to intersect with.
        :return: The mathematical intersection of the two sets.
        """
        ns = Set()
        cself = self.count()
        cset = set.count()
        if cself >= cset:
            for i in range(cset):
                val = set.get(i)
                if self.contains(i):
                    ns.add(val)
        else:
            for i in range(cself):
                val = self.get(i)
                if set.contains(i):
                    ns.add(val)
        return ns

    def difference(self, set):
        """
        Returns a new set that is the mathematical difference of the current set instance and the indicated set.
        That is A-B where A is the caller and B is the passed in set.
        :param set: The set to difference with.
        :return: The mathematical difference of the two sets.
        """
        ns = Set()
        for i in range(self.count()):
            val = self.get(i)
            if not set.contains(val):
                ns.add(val)
        return ns

    def subset(self, set):
        """
        Tests whether the indicated set is a subset of this set instance or not.
        :param set: The set to check as a subset.
        :return: True if the indeicated set is a subset of this set instance, False if not.
        """
        for i in range(set.count()):
            if not self.contains(set.get(i)):
                return False
        return True

class Queue(LinkedList):

    def __init__(self):
        """
        Creates an empty queue.
        """
        LinkedList.__init__(self)

    def add(self, value):
        """
        Not implemented for a queue.
        :param value: N/A
        :return: Nothing
        """
        pass

    def dequeue(self):
        """
        Removes a value from the front of the queue and returns it.
        :return: Nothing
        """
        if self.head:
            value = self.head.get_value()
            self.head = self.head.get_next()
            return value
        return None

    def enqueue(self, value):
        """
        Adds an item to the back of the queue.
        :param value: The value to add to the queue.
        :return: Nothing
        """
        if self.head:
            current = self.head
            while current.get_next():
                current = current.get_next()
            node = Node(value)
            node.set_previous(current)
            current.set_next(node)
        else:
            node = Node(value)
            node.set_next(self.head)
            self.head = node

    def get(self, index):
        """
        Not implemented for a queue.
        :param index: N/A
        :return: Nothing
        """
        pass

    def remove(self, value):
        """
        Not implemented for a queue.
        :param value: N/A
        :return: Nothing
        """
        pass

    def set(self, index, value):
        """
        Not implemented for a queue.
        :param index: N/A
        :param value: N/A
        :return: Nothing
        """
        pass

class PriorityQueue(Queue):

    def __init__(self):
        """
        Creates a minimum priority queue.
        """
        Queue.__init__(self)

    def dequeue(self):
        """
        Removes a value from the front of the queue and returns it.
        :returns Nothing
        """
        if self.head:
            value = self.head.get_value()
            self.head = self.head.get_next()
            self.head.set_previous(None)
            return value
        return None

    def enqueue(self, priority, value):
        """
        Adds an item to the priority queue.
        :param priority: The priority of the item.
        :param value: The value of the item.
        :return: Nothing
        """
        if self.head:
            current = self.head
            while current.get_next():
                current = current.get_next()
            node = PriorityNode(priority, value)
            node.set_previous(current)
            current.set_next(node)
            self.sift_up(node)
        else:
            node = PriorityNode(priority, value)
            self.head = node

    def sift_up(self, node):
        if node.get_previous():
            next = node.get_previous()
            while node.get_previous():
                if next.get_priority() < node.get_priority():
                    break
                else:
                    tp = node.get_priority()
                    tv = node.get_value()
                    node.set_priority(next.get_priority())
                    node.set_value(next.get_value())
                    next.set_priority(tp)
                    next.set_value(tv)
                    node = next
                    next = node.get_previous()

class Stack(LinkedList):

    def __init__(self):
        """
        Creates an empty stack.
        """
        LinkedList.__init__(self)

    def add(self, value):
        """
        Not implemented for a stack.
        :param value: N/A
        :return: Nothing
        """
        pass

    def get(self, index):
        """
        Not implemented for a stack.
        :param index: N/A
        :return: Nothing
        """
        pass

    def peek(self):
        """
        Returns the top value on the stack.
        :return: The top value on the stack.
        """
        return self.head.get_value() if self.head else None

    def pop(self):
        """
        Pops the top value from the stack and returns it.
        :return: The top value on the stack.
        """
        if self.head:
            value = self.head.get_value()
            self.head = self.head.get_next()
            return value
        return None

    def push(self, value):
        """
        Will push a value to the top of the stack.
        :param value: Pushes a value onto the top of the stack.
        :return: Nothing
        """
        node = Node(value)
        node.set_next(self.head)
        self.head = node

    def remove(self, value):
        """
        Not implemented for a stack.
        :param value: N/A
        :return: Nothing
        """
        pass

    def set(self, index, value):
        """
        Not implemented for a stack.
        :param index: N/A
        :param value: N/A
        :return: Nothing
        """
        pass

def main():
    import random, time

    pq = PriorityQueue()
    for i in range(50):
        pq.enqueue(round(random.random()*10, 4), i)
    print(pq)

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


<commit_before># -*- coding: utf-8 -*-
{
    'name': "Bestja: FBŻ",
    'summary': "Installation configuration for FPBŻ",
    'description': "Installation configuration for Federacja Polskich Banków Żywności",
    'author': "Laboratorium EE",
    'website': "http://www.laboratorium.ee",
    'version': '0.1',
    'category': 'Specific Industry Applications',
    'depends': [
        'base',
        'bestja_base',
        'bestja_volunteer',
        'bestja_volunteer_notes',
        'bestja_organization',
        'bestja_organization_hierarchy',
        'bestja_project',
        'bestja_offers',
        'bestja_files',
        'email_confirmation',
        'quizzes',
        'bestja_organization_warehouse'
    ],
    'application': True,
}
<commit_msg>Add bestja_project_hierarchy to the list of FPBZ's modules<commit_after># -*- coding: utf-8 -*-
{
    'name': "Bestja: FBŻ",
    'summary': "Installation configuration for FPBŻ",
    'description': "Installation configuration for Federacja Polskich Banków Żywności",
    'author': "Laboratorium EE",
    'website': "http://www.laboratorium.ee",
    'version': '0.1',
    'category': 'Specific Industry Applications',
    'depends': [
        'base',
        'bestja_base',
        'bestja_volunteer',
        'bestja_volunteer_notes',
        'bestja_organization',
        'bestja_organization_hierarchy',
        'bestja_project',
        'bestja_project_hierarchy',
        'bestja_offers',
        'bestja_files',
        'email_confirmation',
        'quizzes',
        'bestja_organization_warehouse'
    ],
    'application': True,
}
<|endoftext|>


<commit_before>#
# image.py
# Created by pira on 2017/07/28.
#

#coding: utf-8
u"""For image processing."""

import sys
import numpy as np
import cv2

RED   = 2
GREEN = 1
BLUE  = 0

def readGrayImage(filename):
	u"""Read grayscale image.
	@param  filename : filename
	@return img      : 2 dimension np.ndarray[Height][Width]
	"""
	#imread flags=0(cv2.IMREAD_GRAYSCALE):GrayScale
	img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
	print('Read "' + filename + '".')
	return img

def readColorImage(filename):
	u"""Read color image. [notice] Grayscale images are treated as RGB image. (ex. if pixel value is 100, it's treated [100][100][100] RGB image.)
	@param  filename : filename
	@return img      : 3 dimension np.ndarray[Height][Width][BGR]
	"""
	#imread flags>0(cv2.IMREAD_COLOR):3ChannelColors
	#白黒画像でも強制的にRGBで扱ってしまう．
	img = cv2.imread(filename, cv2.IMREAD_COLOR)
	print('Read "' + filename + '".')
	return img

def getRgbLayer(img, rgb=RED):
	u"""Read grayscale image.
	@param  img   : a 3 dimension color image like np.ndarray[Height][Width][BGR]
	@param  rgb   : a returned layer number. Blue is 0, Green is 1 and Red is 2. You can also give a colorname like RED.
	@return layer : a color layer of image, only red, green or blue.
	"""
	height = img.shape[0]
	width  = img.shape[1]
	layer = np.empty([height, width])

	for i in np.arange(height):
		for j in np.arange(width):
			layer[i][j] = img[i][j][rgb]

	return layer

def writeImage(filename, img):
	u"""Export image data.
	@param filename : filename for export image data
	@param img      : 2 or 3 dimension image array
	"""
	cv2.imwrite(filename, img)
	print('Write "'+filename+'".')

def showImage(img):
	u"""Show imsge data.
	@param img : image array
	"""
	cv2.imshow('Image', img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()

def grayimage2block(img, size):
	u"""Divide gray image into blocks.
	@param  img    : a 2 dimension gray image like a np.array[height][width]
	@param  size   : block size list like a [height, width]
	@return blocks : a 4 dimension blocks like a np.ndarray[block_height][block_width][height][width]
	"""
	image_height = int(img.shape[0])
	image_width  = int(img.shape[1])
	block_height = int(size[0])
	block_width  = int(size[1])

	#print(image_height, image_width, block_height, block_width)

	if image_height%block_height != 0 or image_width%block_width != 0:
		print('Please give an appropriate bloxk size.')
		print('You can use only numbers that are divisors of image height and width as block size.')
		sys.exit()

	row = int(image_height / block_height)
	col = int(image_width  / block_width)
	blocks = np.empty([row, col, block_height, block_width])
	#print('blocks shape =', blocks.shape)

	for i in np.arange(row):
		for j in np.arange(col):
			for k in np.arange(block_height):
				for l in np.arange(block_width):
					blocks[i][j][k][l] = img[i*block_height+k][j*block_width+l]

	return blocks

def grayblock2image(blocks):
	u"""Convert the blocks to a gray image..
	@param  blocks : a 4 dimension gray image such as np.ndarray[block_y][block_x][block_height][block_width]
	@return image  : an image array.
	"""
	block_y, block_x, block_height, block_width = blocks.shape

	image = np.empty((0,block_x*block_width), int)

	for x in blocks:
		col_block = np.empty((block_height,0), int)
		for block in x:
			col_block = np.concatenate([col_block, block], axis=1)
		image = np.concatenate([image, col_block], axis=0)
		print(image.shape)

	return image

def colorimage2block(img, size):
	u"""Divide color image into blocks.
	@param  img    : a 3 dimension image like a np.array[height][width][BGR]
	@param  size   : block size list like a [height, width]
	@return blocks : a 5 dimension blocks like a np.ndarray[block_height][block_width][height][width][BGR]
	"""
	image_height = int(img.shape[0])
	image_width  = int(img.shape[1])
	block_height = int(size[0])
	block_width  = int(size[1])
	color_num = int(img.shape[2])

	#print(image_height, image_width, block_height, block_width)

	if image_height%block_height != 0 or image_width%block_width != 0:
		print('Please give an appropriate bloxk size.')
		print('You can use only numbers that are divisors of image height and width as block size.')
		sys.exit()

	row = int(image_height / block_height)
	col = int(image_width  / block_width)
	blocks = np.empty([row, col, block_height, block_width, color_num])

	#print('blocks shape =', blocks.shape)

	for i in np.arange(row):
		for j in np.arange(col):
			for k in np.arange(block_height):
				for l in np.arange(block_width):
						for rgb in np.arange(color_num):
							blocks[i][j][k][l][rgb] = img[i*block_height+k][j*block_width+l][rgb]

	return blocks

def rgb2ycc(img):
	u"""RGB to YCbCr.
	@param  img      : 3 dimension np.ndarray[Height][Width][RGB]
	@return ycc_data : 3 dimension np.ndarray[Height][Width][YCC]
	"""
	height = img.shape[0]
	width  = img.shape[1]
	ycc_data = np.empty([height,width,3])
	for i in np.arange(height):
		for j in np.arange(width):
			ycc_data[i][j][0] =  0.299*img[i][j][2] + 0.587*img[i][j][1] + 0.114*img[i][j][0] #Y
			ycc_data[i][j][1] = -0.169*img[i][j][2] - 0.331*img[i][j][1] + 0.500*img[i][j][0] #Cb
			ycc_data[i][j][2] =  0.500*img[i][j][2] - 0.419*img[i][j][1] - 0.081*img[i][j][0] #Cr
	return ycc_data

def ycc2rgb(img):
	u"""YCbCr to BGR.
	@param  img      : 3 dimension np.ndarray[Height][Width][YCC]
	@return rgb_data : 3 dimension np.ndarray[Height][Width][BGR]
	"""
	height = img.shape[0]
	width  = img.shape[1]
	rgb_data = np.empty([height,width,3])
	for i in np.arange(height):
		for j in np.arange(width):
			rgb_data[i][j][0] = img[i][j][0] + 1.772*img[i][j][1] #B
			rgb_data[i][j][1] = img[i][j][0] - 0.344*img[i][j][1] - 0.714*img[i][j][2] #G
			rgb_data[i][j][2] = img[i][j][0] + 1.402*img[i][j][2] #R
	return rgb_data

def get_y(img):
	u"""Get only Y from YCC image.
	@param  img     : 3 dimension np.ndarray[Height][Width][YCC]
	@return y_data  : 2 dimension np.ndarray[Height][Width]. Including only Y.
	"""
	height = img.shape[0]
	width  = img.shape[1]
	y_data = np.empty([height,width])
	for i in np.arange(height):
		for j in np.arange(width):
			y_data[i][j] = img[i][j][0]
	return y_data

<commit_msg>update function grayblock2image<commit_after>#
# image.py
# Created by pira on 2017/07/28.
#

#coding: utf-8
u"""For image processing."""

import sys
import numpy as np
import cv2

RED   = 2
GREEN = 1
BLUE  = 0

def readGrayImage(filename):
	u"""Read grayscale image.
	@param  filename : filename
	@return img      : 2 dimension np.ndarray[Height][Width]
	"""
	#imread flags=0(cv2.IMREAD_GRAYSCALE):GrayScale
	img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
	print('Read "' + filename + '".')
	return img

def readColorImage(filename):
	u"""Read color image. [notice] Grayscale images are treated as RGB image. (ex. if pixel value is 100, it's treated [100][100][100] RGB image.)
	@param  filename : filename
	@return img      : 3 dimension np.ndarray[Height][Width][BGR]
	"""
	#imread flags>0(cv2.IMREAD_COLOR):3ChannelColors
	#白黒画像でも強制的にRGBで扱ってしまう．
	img = cv2.imread(filename, cv2.IMREAD_COLOR)
	print('Read "' + filename + '".')
	return img

def getRgbLayer(img, rgb=RED):
	u"""Read grayscale image.
	@param  img   : a 3 dimension color image like np.ndarray[Height][Width][BGR]
	@param  rgb   : a returned layer number. Blue is 0, Green is 1 and Red is 2. You can also give a colorname like RED.
	@return layer : a color layer of image, only red, green or blue.
	"""
	height = img.shape[0]
	width  = img.shape[1]
	layer = np.empty([height, width])

	for i in np.arange(height):
		for j in np.arange(width):
			layer[i][j] = img[i][j][rgb]

	return layer

def writeImage(filename, img):
	u"""Export image data.
	@param filename : filename for export image data
	@param img      : 2 or 3 dimension image array
	"""
	cv2.imwrite(filename, img)
	print('Write "'+filename+'".')

def showImage(img):
	u"""Show imsge data.
	@param img : image array
	"""
	cv2.imshow('Image', img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()

def grayimage2block(img, size):
	u"""Divide gray image into blocks.
	@param  img    : a 2 dimension gray image like a np.array[height][width]
	@param  size   : block size list like a [height, width]
	@return blocks : a 4 dimension blocks like a np.ndarray[block_height][block_width][height][width]
	"""
	image_height = int(img.shape[0])
	image_width  = int(img.shape[1])
	block_height = int(size[0])
	block_width  = int(size[1])

	#print(image_height, image_width, block_height, block_width)

	if image_height%block_height != 0 or image_width%block_width != 0:
		print('Please give an appropriate bloxk size.')
		print('You can use only numbers that are divisors of image height and width as block size.')
		sys.exit()

	row = int(image_height / block_height)
	col = int(image_width  / block_width)
	blocks = np.empty([row, col, block_height, block_width])
	#print('blocks shape =', blocks.shape)

	for i in np.arange(row):
		for j in np.arange(col):
			for k in np.arange(block_height):
				for l in np.arange(block_width):
					blocks[i][j][k][l] = img[i*block_height+k][j*block_width+l]

	return blocks

def grayblock2image(blocks):
	u"""Convert the blocks to a gray image..
	@param  blocks : a 4 dimension gray image such as np.ndarray[block_y][block_x][block_height][block_width]
	@return image  : an image array.
	"""
	block_y, block_x, block_height, block_width = blocks.shape

	image = np.empty((0,block_x*block_width), int)

	for x in blocks:
		col_block = np.empty((block_height,0), int)
		for block in x:
			col_block = np.concatenate([col_block, block], axis=1)
		image = np.concatenate([image, col_block], axis=0)

	return image

def colorimage2block(img, size):
	u"""Divide color image into blocks.
	@param  img    : a 3 dimension image like a np.array[height][width][BGR]
	@param  size   : block size list like a [height, width]
	@return blocks : a 5 dimension blocks like a np.ndarray[block_height][block_width][height][width][BGR]
	"""
	image_height = int(img.shape[0])
	image_width  = int(img.shape[1])
	block_height = int(size[0])
	block_width  = int(size[1])
	color_num = int(img.shape[2])

	#print(image_height, image_width, block_height, block_width)

	if image_height%block_height != 0 or image_width%block_width != 0:
		print('Please give an appropriate bloxk size.')
		print('You can use only numbers that are divisors of image height and width as block size.')
		sys.exit()

	row = int(image_height / block_height)
	col = int(image_width  / block_width)
	blocks = np.empty([row, col, block_height, block_width, color_num])

	#print('blocks shape =', blocks.shape)

	for i in np.arange(row):
		for j in np.arange(col):
			for k in np.arange(block_height):
				for l in np.arange(block_width):
						for rgb in np.arange(color_num):
							blocks[i][j][k][l][rgb] = img[i*block_height+k][j*block_width+l][rgb]

	return blocks

def rgb2ycc(img):
	u"""RGB to YCbCr.
	@param  img      : 3 dimension np.ndarray[Height][Width][RGB]
	@return ycc_data : 3 dimension np.ndarray[Height][Width][YCC]
	"""
	height = img.shape[0]
	width  = img.shape[1]
	ycc_data = np.empty([height,width,3])
	for i in np.arange(height):
		for j in np.arange(width):
			ycc_data[i][j][0] =  0.299*img[i][j][2] + 0.587*img[i][j][1] + 0.114*img[i][j][0] #Y
			ycc_data[i][j][1] = -0.169*img[i][j][2] - 0.331*img[i][j][1] + 0.500*img[i][j][0] #Cb
			ycc_data[i][j][2] =  0.500*img[i][j][2] - 0.419*img[i][j][1] - 0.081*img[i][j][0] #Cr
	return ycc_data

def ycc2rgb(img):
	u"""YCbCr to BGR.
	@param  img      : 3 dimension np.ndarray[Height][Width][YCC]
	@return rgb_data : 3 dimension np.ndarray[Height][Width][BGR]
	"""
	height = img.shape[0]
	width  = img.shape[1]
	rgb_data = np.empty([height,width,3])
	for i in np.arange(height):
		for j in np.arange(width):
			rgb_data[i][j][0] = img[i][j][0] + 1.772*img[i][j][1] #B
			rgb_data[i][j][1] = img[i][j][0] - 0.344*img[i][j][1] - 0.714*img[i][j][2] #G
			rgb_data[i][j][2] = img[i][j][0] + 1.402*img[i][j][2] #R
	return rgb_data

def get_y(img):
	u"""Get only Y from YCC image.
	@param  img     : 3 dimension np.ndarray[Height][Width][YCC]
	@return y_data  : 2 dimension np.ndarray[Height][Width]. Including only Y.
	"""
	height = img.shape[0]
	width  = img.shape[1]
	y_data = np.empty([height,width])
	for i in np.arange(height):
		for j in np.arange(width):
			y_data[i][j] = img[i][j][0]
	return y_data

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import time

from odoo import api, fields, models, _
from odoo.addons import decimal_precision as dp
from odoo.exceptions import UserError

class SaleAdvancePaymentInv(models.TransientModel):
    _name = "sale.advance.payment.inv"
    _description = "Sales Advance Payment Invoice"

    @api.model
    def _count(self):
        return len(self._context.get('active_ids', []))

    @api.model
    def _get_advance_payment_method(self):
        if self._count() == 1:
            sale_obj = self.env['sale.order']
            order = sale_obj.browse(self._context.get('active_ids'))[0]
            if order.order_line.filtered(lambda dp: dp.is_downpayment) and order.invoice_ids.filtered(lambda invoice: invoice.state != 'cancel') or order.order_line.filtered(lambda l: l.qty_to_invoice < 0):
                return 'all'
            else:
                return 'delivered'
        return 'all'

    @api.model
    def _default_product_id(self):
        product_id = self.env['ir.config_parameter'].sudo().get_param('sale.default_deposit_product_id')
        return self.env['product.product'].browse(int(product_id))

    @api.model
    def _default_deposit_account_id(self):
        return self._default_product_id().property_account_income_id

    @api.model
    def _default_deposit_taxes_id(self):
        return self._default_product_id().taxes_id

    advance_payment_method = fields.Selection([
        ('delivered', 'Invoiceable lines'),
        ('all', 'Invoiceable lines (deduct down payments)'),
        ('percentage', 'Down payment (percentage)'),
        ('fixed', 'Down payment (fixed amount)')
        ], string='What do you want to invoice?', default=_get_advance_payment_method, required=True)
    product_id = fields.Many2one('product.product', string='Down Payment Product', domain=[('type', '=', 'service')],
        default=_default_product_id)
    count = fields.Integer(default=_count, string='Order Count')
    amount = fields.Float('Down Payment Amount', digits=dp.get_precision('Account'), help="The amount to be invoiced in advance, taxes excluded.")
    deposit_account_id = fields.Many2one("account.account", string="Income Account", domain=[('deprecated', '=', False)],
        help="Account used for deposits", default=_default_deposit_account_id)
    deposit_taxes_id = fields.Many2many("account.tax", string="Customer Taxes", help="Taxes used for deposits", default=_default_deposit_taxes_id)

    @api.onchange('advance_payment_method')
    def onchange_advance_payment_method(self):
        if self.advance_payment_method == 'percentage':
            return {'value': {'amount': 0}}
        return {}

    @api.multi
    def _create_invoice(self, order, so_line, amount):
        inv_obj = self.env['account.invoice']
        ir_property_obj = self.env['ir.property']

        account_id = False
        if self.product_id.id:
            account_id = self.product_id.property_account_income_id.id or self.product_id.categ_id.property_account_income_categ_id.id
        if not account_id:
            inc_acc = ir_property_obj.get('property_account_income_categ_id', 'product.category')
            account_id = order.fiscal_position_id.map_account(inc_acc).id if inc_acc else False
        if not account_id:
            raise UserError(
                _('There is no income account defined for this product: "%s". You may have to install a chart of account from Accounting app, settings menu.') %
                (self.product_id.name,))

        if self.amount <= 0.00:
            raise UserError(_('The value of the down payment amount must be positive.'))
        context = {'lang': order.partner_id.lang}
        if self.advance_payment_method == 'percentage':
            amount = order.amount_untaxed * self.amount / 100
            name = _("Down payment of %s%%") % (self.amount,)
        else:
            amount = self.amount
            name = _('Down Payment')
        del context
        taxes = self.product_id.taxes_id.filtered(lambda r: not order.company_id or r.company_id == order.company_id)
        if order.fiscal_position_id and taxes:
            tax_ids = order.fiscal_position_id.map_tax(taxes, self.product_id, order.partner_shipping_id).ids
        else:
            tax_ids = taxes.ids

        invoice = inv_obj.create({
            'name': order.client_order_ref or order.name,
            'origin': order.name,
            'type': 'out_invoice',
            'reference': False,
            'account_id': order.partner_id.property_account_receivable_id.id,
            'partner_id': order.partner_invoice_id.id,
            'partner_shipping_id': order.partner_shipping_id.id,
            'invoice_line_ids': [(0, 0, {
                'name': name,
                'origin': order.name,
                'account_id': account_id,
                'price_unit': amount,
                'quantity': 1.0,
                'discount': 0.0,
                'uom_id': self.product_id.uom_id.id,
                'product_id': self.product_id.id,
                'sale_line_ids': [(6, 0, [so_line.id])],
                'invoice_line_tax_ids': [(6, 0, tax_ids)],
                'analytic_tag_ids': [(6, 0, so_line.analytic_tag_ids.ids)],
                'account_analytic_id': order.analytic_account_id.id or False,
            })],
            'currency_id': order.pricelist_id.currency_id.id,
            'payment_term_id': order.payment_term_id.id,
            'fiscal_position_id': order.fiscal_position_id.id or order.partner_id.property_account_position_id.id,
            'team_id': order.team_id.id,
            'user_id': order.user_id.id,
            'comment': order.note,
        })
        invoice.compute_taxes()
        invoice.message_post_with_view('mail.message_origin_link',
                    values={'self': invoice, 'origin': order},
                    subtype_id=self.env.ref('mail.mt_note').id)
        return invoice

    @api.multi
    def create_invoices(self):
        sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', []))

        if self.advance_payment_method == 'delivered':
            sale_orders.action_invoice_create()
        elif self.advance_payment_method == 'all':
            sale_orders.action_invoice_create(final=True)
        else:
            # Create deposit product if necessary
            if not self.product_id:
                vals = self._prepare_deposit_product()
                self.product_id = self.env['product.product'].create(vals)
                self.env['ir.config_parameter'].sudo().set_param('sale.default_deposit_product_id', self.product_id.id)

            sale_line_obj = self.env['sale.order.line']
            for order in sale_orders:
                if self.advance_payment_method == 'percentage':
                    amount = order.amount_untaxed * self.amount / 100
                else:
                    amount = self.amount
                if self.product_id.invoice_policy != 'order':
                    raise UserError(_('The product used to invoice a down payment should have an invoice policy set to "Ordered quantities". Please update your deposit product to be able to create a deposit invoice.'))
                if self.product_id.type != 'service':
                    raise UserError(_("The product used to invoice a down payment should be of type 'Service'. Please use another product or update this product."))
                taxes = self.product_id.taxes_id.filtered(lambda r: not order.company_id or r.company_id == order.company_id)
                if order.fiscal_position_id and taxes:
                    tax_ids = order.fiscal_position_id.map_tax(taxes, self.product_id, order.partner_shipping_id).ids
                else:
                    tax_ids = taxes.ids
                context = {'lang': order.partner_id.lang}
                analytic_tag_ids = []
                for line in order.order_line:
                    analytic_tag_ids = [(4, analytic_tag.id, None) for analytic_tag in line.analytic_tag_ids]
                so_line = sale_line_obj.create({
                    'name': _('Down Payment: %s') % (time.strftime('%m %Y'),),
                    'price_unit': amount,
                    'product_uom_qty': 0.0,
                    'order_id': order.id,
                    'discount': 0.0,
                    'product_uom': self.product_id.uom_id.id,
                    'product_id': self.product_id.id,
                    'analytic_tag_ids': analytic_tag_ids,
                    'tax_id': [(6, 0, tax_ids)],
                    'is_downpayment': True,
                })
                del context
                self._create_invoice(order, so_line, amount)
        if self._context.get('open_invoices', False):
            return sale_orders.action_view_invoice()
        return {'type': 'ir.actions.act_window_close'}

    def _prepare_deposit_product(self):
        return {
            'name': 'Down payment',
            'type': 'service',
            'invoice_policy': 'order',
            'property_account_income_id': self.deposit_account_id.id,
            'taxes_id': [(6, 0, self.deposit_taxes_id.ids)],
            'company_id': False,
        }
<commit_msg>[IMP] sale: invoice creation for downpayments cleanup<commit_after># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import time

from odoo import api, fields, models, _
from odoo.addons import decimal_precision as dp
from odoo.exceptions import UserError

class SaleAdvancePaymentInv(models.TransientModel):
    _name = "sale.advance.payment.inv"
    _description = "Sales Advance Payment Invoice"

    @api.model
    def _count(self):
        return len(self._context.get('active_ids', []))

    @api.model
    def _get_advance_payment_method(self):
        if self._count() == 1:
            sale_obj = self.env['sale.order']
            order = sale_obj.browse(self._context.get('active_ids'))[0]
            if order.order_line.filtered(lambda dp: dp.is_downpayment) and order.invoice_ids.filtered(lambda invoice: invoice.state != 'cancel') or order.order_line.filtered(lambda l: l.qty_to_invoice < 0):
                return 'all'
            else:
                return 'delivered'
        return 'all'

    @api.model
    def _default_product_id(self):
        product_id = self.env['ir.config_parameter'].sudo().get_param('sale.default_deposit_product_id')
        return self.env['product.product'].browse(int(product_id))

    @api.model
    def _default_deposit_account_id(self):
        return self._default_product_id().property_account_income_id

    @api.model
    def _default_deposit_taxes_id(self):
        return self._default_product_id().taxes_id

    advance_payment_method = fields.Selection([
        ('delivered', 'Invoiceable lines'),
        ('all', 'Invoiceable lines (deduct down payments)'),
        ('percentage', 'Down payment (percentage)'),
        ('fixed', 'Down payment (fixed amount)')
        ], string='What do you want to invoice?', default=_get_advance_payment_method, required=True)
    product_id = fields.Many2one('product.product', string='Down Payment Product', domain=[('type', '=', 'service')],
        default=_default_product_id)
    count = fields.Integer(default=_count, string='Order Count')
    amount = fields.Float('Down Payment Amount', digits=dp.get_precision('Account'), help="The amount to be invoiced in advance, taxes excluded.")
    deposit_account_id = fields.Many2one("account.account", string="Income Account", domain=[('deprecated', '=', False)],
        help="Account used for deposits", default=_default_deposit_account_id)
    deposit_taxes_id = fields.Many2many("account.tax", string="Customer Taxes", help="Taxes used for deposits", default=_default_deposit_taxes_id)

    @api.onchange('advance_payment_method')
    def onchange_advance_payment_method(self):
        if self.advance_payment_method == 'percentage':
            return {'value': {'amount': 0}}
        return {}

    @api.multi
    def _create_invoice(self, order, so_line, amount):
        inv_obj = self.env['account.invoice']
        ir_property_obj = self.env['ir.property']

        account_id = False
        if self.product_id.id:
            account_id = self.product_id.property_account_income_id.id or self.product_id.categ_id.property_account_income_categ_id.id
        if not account_id:
            inc_acc = ir_property_obj.get('property_account_income_categ_id', 'product.category')
            account_id = order.fiscal_position_id.map_account(inc_acc).id if inc_acc else False
        if not account_id:
            raise UserError(
                _('There is no income account defined for this product: "%s". You may have to install a chart of account from Accounting app, settings menu.') %
                (self.product_id.name,))

        if self.amount <= 0.00:
            raise UserError(_('The value of the down payment amount must be positive.'))
        context = {'lang': order.partner_id.lang}
        if self.advance_payment_method == 'percentage':
            amount = order.amount_untaxed * self.amount / 100
            name = _("Down payment of %s%%") % (self.amount,)
        else:
            amount = self.amount
            name = _('Down Payment')
        del context
        taxes = self.product_id.taxes_id.filtered(lambda r: not order.company_id or r.company_id == order.company_id)
        if order.fiscal_position_id and taxes:
            tax_ids = order.fiscal_position_id.map_tax(taxes, self.product_id, order.partner_shipping_id).ids
        else:
            tax_ids = taxes.ids

        invoice = inv_obj.create({
            'name': order.client_order_ref,
            'origin': order.name,
            'type': 'out_invoice',
            'reference': False,
            'account_id': order.partner_id.property_account_receivable_id.id,
            'partner_id': order.partner_invoice_id.id,
            'partner_shipping_id': order.partner_shipping_id.id,
            'invoice_line_ids': [(0, 0, {
                'name': name,
                'origin': order.name,
                'account_id': account_id,
                'price_unit': amount,
                'quantity': 1.0,
                'discount': 0.0,
                'uom_id': self.product_id.uom_id.id,
                'product_id': self.product_id.id,
                'sale_line_ids': [(6, 0, [so_line.id])],
                'invoice_line_tax_ids': [(6, 0, tax_ids)],
                'analytic_tag_ids': [(6, 0, so_line.analytic_tag_ids.ids)],
                'account_analytic_id': order.analytic_account_id.id or False,
            })],
            'currency_id': order.pricelist_id.currency_id.id,
            'payment_term_id': order.payment_term_id.id,
            'fiscal_position_id': order.fiscal_position_id.id or order.partner_id.property_account_position_id.id,
            'team_id': order.team_id.id,
            'user_id': order.user_id.id,
            'comment': order.note,
        })
        invoice.compute_taxes()
        invoice.message_post_with_view('mail.message_origin_link',
                    values={'self': invoice, 'origin': order},
                    subtype_id=self.env.ref('mail.mt_note').id)
        return invoice

    @api.multi
    def create_invoices(self):
        sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', []))

        if self.advance_payment_method == 'delivered':
            sale_orders.action_invoice_create()
        elif self.advance_payment_method == 'all':
            sale_orders.action_invoice_create(final=True)
        else:
            # Create deposit product if necessary
            if not self.product_id:
                vals = self._prepare_deposit_product()
                self.product_id = self.env['product.product'].create(vals)
                self.env['ir.config_parameter'].sudo().set_param('sale.default_deposit_product_id', self.product_id.id)

            sale_line_obj = self.env['sale.order.line']
            for order in sale_orders:
                if self.advance_payment_method == 'percentage':
                    amount = order.amount_untaxed * self.amount / 100
                else:
                    amount = self.amount
                if self.product_id.invoice_policy != 'order':
                    raise UserError(_('The product used to invoice a down payment should have an invoice policy set to "Ordered quantities". Please update your deposit product to be able to create a deposit invoice.'))
                if self.product_id.type != 'service':
                    raise UserError(_("The product used to invoice a down payment should be of type 'Service'. Please use another product or update this product."))
                taxes = self.product_id.taxes_id.filtered(lambda r: not order.company_id or r.company_id == order.company_id)
                if order.fiscal_position_id and taxes:
                    tax_ids = order.fiscal_position_id.map_tax(taxes, self.product_id, order.partner_shipping_id).ids
                else:
                    tax_ids = taxes.ids
                context = {'lang': order.partner_id.lang}
                analytic_tag_ids = []
                for line in order.order_line:
                    analytic_tag_ids = [(4, analytic_tag.id, None) for analytic_tag in line.analytic_tag_ids]
                so_line = sale_line_obj.create({
                    'name': _('Down Payment: %s') % (time.strftime('%m %Y'),),
                    'price_unit': amount,
                    'product_uom_qty': 0.0,
                    'order_id': order.id,
                    'discount': 0.0,
                    'product_uom': self.product_id.uom_id.id,
                    'product_id': self.product_id.id,
                    'analytic_tag_ids': analytic_tag_ids,
                    'tax_id': [(6, 0, tax_ids)],
                    'is_downpayment': True,
                })
                del context
                self._create_invoice(order, so_line, amount)
        if self._context.get('open_invoices', False):
            return sale_orders.action_view_invoice()
        return {'type': 'ir.actions.act_window_close'}

    def _prepare_deposit_product(self):
        return {
            'name': 'Down payment',
            'type': 'service',
            'invoice_policy': 'order',
            'property_account_income_id': self.deposit_account_id.id,
            'taxes_id': [(6, 0, self.deposit_taxes_id.ids)],
            'company_id': False,
        }
<|endoftext|>


<commit_before># coding: utf-8

from openerp import models, api

class sale_order_line(models.Model):
    _inherit = 'sale.order.line'

    @api.multi
    def _check_routing(self, product, warehouse):
        """ skip stock verification if the route goes from supplier to customer
            As the product never goes in stock, no need to verify it's availibility
        """
        res = super(sale_order_line, self)._check_routing(product, warehouse)
        if not res:
            for line in self:
                for pull_rule in line.route_id.pull_ids:
                    if (pull_rule.picking_type_id.default_location_src_id.usage == 'supplier' and
                            pull_rule.picking_type_id.default_location_dest_id.usage == 'customer'):
                        res = True
                        break
        return res
<commit_msg>[FIX] stock_dropshipping: raise a warning in a specific invoice creation config<commit_after># coding: utf-8

from openerp import models, api, _
from openerp.exceptions import Warning

class sale_order_line(models.Model):
    _inherit = 'sale.order.line'

    @api.multi
    def _check_routing(self, product, warehouse):
        """ skip stock verification if the route goes from supplier to customer
            As the product never goes in stock, no need to verify it's availibility
        """
        res = super(sale_order_line, self)._check_routing(product, warehouse)
        if not res:
            for line in self:
                for pull_rule in line.route_id.pull_ids:
                    if (pull_rule.picking_type_id.default_location_src_id.usage == 'supplier' and
                            pull_rule.picking_type_id.default_location_dest_id.usage == 'customer'):
                        res = True
                        break
        return res

class purchase_order(models.Model):
    _inherit = 'purchase.order'

    @api.multi
    def wkf_confirm_order(self):
        for po in self:
            if po.invoice_method == 'picking' and po.location_id.usage == 'customer':
                for proc in po.order_line.mapped('procurement_ids'):
                    if proc.sale_line_id.order_id.order_policy == 'picking':
                        raise Warning(_('In the case of a dropship route, it is not possible to have an invoicing control set on "Based on incoming shipments" and a sale order with an invoice creation on "On Delivery Order"'))
        super(purchase_order, self).wkf_confirm_order()
<|endoftext|>


<commit_before># This file is part of OpenHatch.
# Copyright (C) 2010 Jack Grigg
# Copyright (C) 2010 OpenHatch, Inc.
#
# 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/>.

import logging
import urllib2

import gevent.pool
import gevent.monkey

from django.core.management.base import BaseCommand

import django.conf
django.conf.settings.CELERY_ALWAYS_EAGER = True

import mysite.customs.mechanize_helpers

import mysite.customs.bugtrackers.bugzilla
import mysite.customs.bugtrackers.launchpad
import mysite.customs.bugtrackers.roundup
import mysite.customs.bugtrackers.trac

### All this code runs synchronously once a day.
### For now, we can crawl all the bug trackers in serial.

### One day, though, we will crawl so many bug trackers that it will take
### too long.

### I suggest that, at that point, we fork a number of worker processes, and
### turn these lists into a queue or something. That should be easy with the
### multiprocessing module. Then we can do N at once.

### We could do something smart with statistics, detecting the average
### refresh time within the bug tracker of a bug, then polling at some
### approximation of that rate. (But, really, why bother?)

### Since most of the time we're waiting on the network, we could also use
### Twisted or Stackless Python or something. That would be super cool, too.
### If somone can show me how to migrate this codebase to it, that'd be neat.
### I'm unlikely to look into it myself, though.
###
### ### UPDATE: We now have an asynchronous bug importer based on Twisted. It
### ### will eventually handle all bug importing, making this file obsolete.
### ### And it didn't even take a year!
### ### -- Jack - 2011-04-16

### (If at some point we start indexing absolutely every bug in free and open
### source software, then the above ideas will actually become meaningful.)

### -- New Age Asheesh, 2010-05-31.

class Command(BaseCommand):
    args = '<tracker_type tracker_type ...> <ohloh>'
    help = """Call this once a day to make sure we run Bug search-related nightly jobs.
If argument(s) are supplied, only the corresponding functions are run.
With no arguments, all importers and the Ohloh broken link expunger are run.

Supported tracker types:
 * roundup
 * trac
 * bugzilla
 * launchpad"""

    def find_and_update_enabled_roundup_trackers(self):
        things_to_do = []
        enabled_roundup_trackers = []

        ### First, the "find" step
        for thing_name in dir(mysite.customs.bugtrackers.roundup):
            thing = getattr(mysite.customs.bugtrackers.roundup,
                            thing_name)
            if hasattr(thing, 'enabled'):
                if getattr(thing, 'enabled'):
                    enabled_roundup_trackers.append(thing)

        ### Okay, now update!
        for thing in enabled_roundup_trackers:
            def do_it(thing=thing):
                instantiated = thing()
                project_name = instantiated.project.name
                logging.info("[Roundup] About to update bugs from project named %s." % project_name)
                try:
                    instantiated.update()
                except urllib2.URLError, e:
                    logging.error("[Roundup] ERROR: %s importer failed with urllib2.URLError, skipping..." % project_name)
                    logging.error("[Roundup] Error message: %s" % str(e))
            things_to_do.append(do_it)
        return things_to_do

    def find_and_update_enabled_trac_instances(self):
        things_to_do = []
        enabled_trac_instances = []

        ### First, the "find" step
        for thing_name in dir(mysite.customs.bugtrackers.trac):
            thing = getattr(mysite.customs.bugtrackers.trac,
                            thing_name)
            if hasattr(thing, 'enabled'):
                if getattr(thing, 'enabled'):
                    enabled_trac_instances.append(thing)

        ### Okay, now update!
        for thing in enabled_trac_instances:
            def do_it(thing=thing):
                instantiated = thing()
                tracker_name = instantiated.tracker_name
                logging.info("[Trac] About to update bugs from project named %s." % tracker_name)
                try:
                    instantiated.update()
                except urllib2.URLError, e:
                    logging.error("[Trac] ERROR: %s importer failed with urllib2.URLError, skipping..." % tracker_name)
                    logging.error("[Trac] Error message: %s" % str(e))
            things_to_do.append(do_it)
        return things_to_do

    def find_and_update_enabled_bugzilla_instances(self):
        things_to_do = []
        enabled_bugzilla_instances = []

        ### First, the "find" step
        for thing_name in dir(mysite.customs.bugtrackers.bugzilla):
            thing = getattr(mysite.customs.bugtrackers.bugzilla,
                            thing_name)
            if hasattr(thing, 'enabled'):
                if getattr(thing, 'enabled'):
                    enabled_bugzilla_instances.append(thing)

        ### Okay, now update!
        for thing in enabled_bugzilla_instances:
            def do_it(thing=thing):
                instantiated = thing()
                tracker_name = instantiated.tracker_name
                logging.info("[Bugzilla] About to update bugs from project named %s." % tracker_name)
                # FIXME: The Bugzilla trackers seem to throw error 500 a lot.
                # For now, chuck in a dirty big try except to stop importer
                # breaking.
                try:
                    instantiated.update()
                except urllib2.URLError, e:
                    logging.error("[Bugzilla] ERROR: %s importer failed with urllib2.URLError, skipping..." % tracker_name)
                    logging.error("[Bugzilla] Error message: %s" % str(e))
            things_to_do.append(do_it)
        return things_to_do

    def update_launchpad_hosted_projects(self):
        ### For Launchpad:
        things_to_do = [
            # First, we ask the projects' bug trackers if there are new bugs we should know about
            mysite.customs.bugtrackers.launchpad.refresh_bugs_from_all_indexed_launchpad_projects,
            # Second, we go through our *own* database of Launchpad-sourced bugs, and make sure they are all up to date
            mysite.customs.bugtrackers.launchpad.refresh_all_launchpad_bugs]
        return things_to_do

    def check_for_broken_ohloh_links(self):
        things_to_do = []
        for citation in mysite.profile.models.Citation.untrashed.filter(
            data_import_attempt__source__in=('oh', 'rs')):
            def do_it(citation=citation):
                # check citation URL for being a 404
                # if so, remove set the URL to None. Also, log it.
                if citation.url:
                    if mysite.customs.mechanize_helpers.link_works(citation.url):
                        # then we do nothing
                        pass
                    else:
                        # aww shucks, I guess we have to remove it.
                        logging.warning("We had to remove the url %s from the citation whose PK is %d" % (
                                citation.url, citation.pk))
                        citation.url = None
                        citation.save()
            things_to_do.append(do_it)
        return things_to_do

    def handle(self, override_cdt_fns=None, *args, **options):
        # Patch modules like urllib2 so that gevent's green-threads can know
        # to switch to the next thread when one thread blocks on network I/O.
        gevent.monkey.patch_all()

        if override_cdt_fns:
            cdt_fns = override_cdt_fns
        else:
            cdt_fns = {
                'ohloh': self.check_for_broken_ohloh_links,
                'trac': self.find_and_update_enabled_trac_instances,
                'roundup': self.find_and_update_enabled_roundup_trackers,
                'bugzilla': self.find_and_update_enabled_bugzilla_instances,
                'launchpad': self.update_launchpad_hosted_projects
                }
        ## Which ones do we plan to do this, time we run?
        # Well, if the user supplied arguments on the command line, then put those in the list.
        if args:
            tasks = args
        else:
            # Plan to do them all, since the user did not sub-specify.
            tasks = cdt_fns.keys()

        logging.info("Creating gevent pool.")
        pool = gevent.pool.Pool(500)

        # Okay, so do the work.
        for key in tasks:
            # Each of these keys returns an iterable of functions to call.
            #
            # We wrap each callable with a big Exception handler so that
            # one failure does not kill the entire process.
            for function in cdt_fns[key]():
                def do_it(function=function, key=key):
                    # Here, we call every one of the worker functions.
                    #
                    # If they fail wih an error, we log the error and proceed
                    # to the next worker function. (The alternative is that we
                    # let the exception bubble-up and make the entire call to the
                    # management command fail.)
                    try:
                        function()
                    except Exception:
                        logging.exception("During %s, the particular importer failed. "
                                          "Skipping it.", key)
                pool.spawn(do_it)

        # By calling join(), we refuse to proceed until all the threads finish.
        pool.join()
<commit_msg>Add initial support for bug objects to refresh themselves<commit_after># This file is part of OpenHatch.
# Copyright (C) 2010 Jack Grigg
# Copyright (C) 2010 OpenHatch, Inc.
#
# 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/>.

import logging
import urllib2

import gevent.pool
import gevent.monkey

from django.core.management.base import BaseCommand

import django.conf
django.conf.settings.CELERY_ALWAYS_EAGER = True

import mysite.customs.mechanize_helpers

import mysite.customs.bugtrackers.bugzilla
import mysite.customs.bugtrackers.launchpad
import mysite.customs.bugtrackers.roundup
import mysite.customs.bugtrackers.trac

### All this code runs synchronously once a day.
### For now, we can crawl all the bug trackers in serial.

### One day, though, we will crawl so many bug trackers that it will take
### too long.

### I suggest that, at that point, we fork a number of worker processes, and
### turn these lists into a queue or something. That should be easy with the
### multiprocessing module. Then we can do N at once.

### We could do something smart with statistics, detecting the average
### refresh time within the bug tracker of a bug, then polling at some
### approximation of that rate. (But, really, why bother?)

### Since most of the time we're waiting on the network, we could also use
### Twisted or Stackless Python or something. That would be super cool, too.
### If somone can show me how to migrate this codebase to it, that'd be neat.
### I'm unlikely to look into it myself, though.
###
### ### UPDATE: We now have an asynchronous bug importer based on Twisted. It
### ### will eventually handle all bug importing, making this file obsolete.
### ### And it didn't even take a year!
### ### -- Jack - 2011-04-16

### (If at some point we start indexing absolutely every bug in free and open
### source software, then the above ideas will actually become meaningful.)

### -- New Age Asheesh, 2010-05-31.

class Command(BaseCommand):
    args = '<tracker_type tracker_type ...> <ohloh>'
    help = """Call this once a day to make sure we run Bug search-related nightly jobs.
If argument(s) are supplied, only the corresponding functions are run.
With no arguments, all importers and the Ohloh broken link expunger are run.

Supported tracker types:
 * roundup
 * trac
 * bugzilla
 * launchpad"""

    def find_and_update_enabled_roundup_trackers(self):
        things_to_do = []
        enabled_roundup_trackers = []

        ### First, the "find" step
        for thing_name in dir(mysite.customs.bugtrackers.roundup):
            thing = getattr(mysite.customs.bugtrackers.roundup,
                            thing_name)
            if hasattr(thing, 'enabled'):
                if getattr(thing, 'enabled'):
                    enabled_roundup_trackers.append(thing)

        ### Okay, now update!
        for thing in enabled_roundup_trackers:
            def do_it(thing=thing):
                instantiated = thing()
                project_name = instantiated.project.name
                logging.info("[Roundup] About to update bugs from project named %s." % project_name)
                try:
                    instantiated.update()
                except urllib2.URLError, e:
                    logging.error("[Roundup] ERROR: %s importer failed with urllib2.URLError, skipping..." % project_name)
                    logging.error("[Roundup] Error message: %s" % str(e))
            things_to_do.append(do_it)
        return things_to_do

    def find_and_update_enabled_trac_instances(self):
        things_to_do = []
        enabled_trac_instances = []

        ### First, the "find" step
        for thing_name in dir(mysite.customs.bugtrackers.trac):
            thing = getattr(mysite.customs.bugtrackers.trac,
                            thing_name)
            if hasattr(thing, 'enabled'):
                if getattr(thing, 'enabled'):
                    enabled_trac_instances.append(thing)

        ### Okay, now update!
        for thing in enabled_trac_instances:
            def do_it(thing=thing):
                instantiated = thing()
                tracker_name = instantiated.tracker_name
                logging.info("[Trac] About to update bugs from project named %s." % tracker_name)
                try:
                    instantiated.update()
                except urllib2.URLError, e:
                    logging.error("[Trac] ERROR: %s importer failed with urllib2.URLError, skipping..." % tracker_name)
                    logging.error("[Trac] Error message: %s" % str(e))
            things_to_do.append(do_it)
        return things_to_do

    def find_and_update_enabled_bugzilla_instances(self):
        things_to_do = []
        enabled_bugzilla_instances = []

        ### First, the "find" step
        for thing_name in dir(mysite.customs.bugtrackers.bugzilla):
            thing = getattr(mysite.customs.bugtrackers.bugzilla,
                            thing_name)
            if hasattr(thing, 'enabled'):
                if getattr(thing, 'enabled'):
                    enabled_bugzilla_instances.append(thing)

        ### Okay, now update!
        for thing in enabled_bugzilla_instances:
            def do_it(thing=thing):
                instantiated = thing()
                tracker_name = instantiated.tracker_name
                logging.info("[Bugzilla] About to update bugs from project named %s." % tracker_name)
                # FIXME: The Bugzilla trackers seem to throw error 500 a lot.
                # For now, chuck in a dirty big try except to stop importer
                # breaking.
                try:
                    instantiated.update()
                except urllib2.URLError, e:
                    logging.error("[Bugzilla] ERROR: %s importer failed with urllib2.URLError, skipping..." % tracker_name)
                    logging.error("[Bugzilla] Error message: %s" % str(e))
            things_to_do.append(do_it)
        return things_to_do

    def update_launchpad_hosted_projects(self):
        ### For Launchpad:
        things_to_do = [
            # First, we ask the projects' bug trackers if there are new bugs we should know about
            mysite.customs.bugtrackers.launchpad.refresh_bugs_from_all_indexed_launchpad_projects,
            # Second, we go through our *own* database of Launchpad-sourced bugs, and make sure they are all up to date
            mysite.customs.bugtrackers.launchpad.refresh_all_launchpad_bugs]
        return things_to_do

    def check_for_broken_ohloh_links(self):
        things_to_do = []
        for citation in mysite.profile.models.Citation.untrashed.filter(
            data_import_attempt__source__in=('oh', 'rs')):
            def do_it(citation=citation):
                # check citation URL for being a 404
                # if so, remove set the URL to None. Also, log it.
                if citation.url:
                    if mysite.customs.mechanize_helpers.link_works(citation.url):
                        # then we do nothing
                        pass
                    else:
                        # aww shucks, I guess we have to remove it.
                        logging.warning("We had to remove the url %s from the citation whose PK is %d" % (
                                citation.url, citation.pk))
                        citation.url = None
                        citation.save()
            things_to_do.append(do_it)
        return things_to_do

    def ask_every_bug_to_refresh_itself(self):
        '''Bug objects now store a pointer to a .bug_tracker object. This lets
        them refresh themselves. Let's take advantage of this.'''
        bug_tracker2bug_to_refresh = {}

        for bug in mysite.search.models.Bug.open_ones.all():
            # Skip the fresh ones
            if bug.data_is_more_fresh_than_one_day():
                continue

            # Skip the ones with no bug_tracker link
            if bug.bug_tracker is None:
                logging.warn("Bug is missing a bug_tracker link: %s", bug)
                continue

            if bug.bug_tracker not in bug_tracker2bug_to_refresh:
                bug_tracker2bug_to_refresh[bug.bug_tracker] = []

            bug_tracker2bug_to_refresh[bug.bug_tracker].append(bug)

        worker_functions = []
        for bug_tracker in bug_tracker2bug_to_refresh:
            bugs = bug_tracker2bug_to_refresh[bug_tracker]
            def do_it(bug_tracker=bug_tracker, bugs=bugs):
                instance = bug_tracker.make_instance()
                for bug in bugs:
                    instance.refresh_one_bug(bug)
            worker_functions.append(do_it)
        return worker_functions

    def handle(self, override_cdt_fns=None, *args, **options):
        # Patch modules like urllib2 so that gevent's green-threads can know
        # to switch to the next thread when one thread blocks on network I/O.
        gevent.monkey.patch_all()

        if override_cdt_fns:
            cdt_fns = override_cdt_fns
        else:
            cdt_fns = {
                'ohloh': self.check_for_broken_ohloh_links,
                'trac': self.find_and_update_enabled_trac_instances,
                'roundup': self.find_and_update_enabled_roundup_trackers,
                'bugzilla': self.find_and_update_enabled_bugzilla_instances,
                'launchpad': self.update_launchpad_hosted_projects,
                'bugs_themselves': self.ask_every_bug_to_refresh_itself,
                }
        ## Which ones do we plan to do this, time we run?
        # Well, if the user supplied arguments on the command line, then put those in the list.
        if args:
            tasks = args
        else:
            # Plan to do them all, since the user did not sub-specify.
            tasks = cdt_fns.keys()

        logging.info("Creating gevent pool.")
        pool = gevent.pool.Pool(500)

        # Okay, so do the work.
        for key in tasks:
            # Each of these keys returns an iterable of functions to call.
            #
            # We wrap each callable with a big Exception handler so that
            # one failure does not kill the entire process.
            for function in cdt_fns[key]():
                def do_it(function=function, key=key):
                    # Here, we call every one of the worker functions.
                    #
                    # If they fail wih an error, we log the error and proceed
                    # to the next worker function. (The alternative is that we
                    # let the exception bubble-up and make the entire call to the
                    # management command fail.)
                    try:
                        function()
                    except Exception:
                        logging.exception("During %s, the particular importer failed. "
                                          "Skipping it.", key)
                pool.spawn(do_it)

        # By calling join(), we refuse to proceed until all the threads finish.
        pool.join()
<|endoftext|>


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

import scanpy as sc
import scIB
import argparse
import os
import sys
import warnings
from pathlib import Path

warnings.filterwarnings('ignore')

RESULT_TYPES = [
    "full",  # reconstructed expression data
    "embed", # embedded/latent space
    "knn"    # corrected neighbourhood graph
]

if __name__=='__main__':
    """
    Save embeddings for all scenarios, methods and settings
    """

    parser = argparse.ArgumentParser(description='Save embeddings')

    parser.add_argument('-i', '--input', required=True)
    parser.add_argument('-o', '--outfile', required=True,
                        help='Output coordinates CSV file')
    parser.add_argument('-m', '--method', required=True, help='Name of method')
    parser.add_argument('-r', '--result', required=True, choices=RESULT_TYPES,
                        help='Type of result: full, embed, knn')
    parser.add_argument('-b', '--batch_key', required=True, help='Key of batch')
    parser.add_argument('-l', '--label_key', required=True,
                        help='Key of annotated labels e.g. "cell_type"')

    args = parser.parse_args()

    outfile = args.outfile
    method = args.method
    result = args.result
    setup = f'{method}_{result}'
    batch_key = args.batch_key
    label_key = args.label_key

    outdir = os.path.dirname(outfile)
    labels_png = os.path.join(outdir, f'{setup}_labels.png')
    batches_png = os.path.join(outdir, f'{setup}_batch.png')

    if os.stat(args.input).st_size == 0:
        print(f'{args.input} is empty, empty output files will be created')

        # Delete any old output files
        for file in [labels_png, batches_png, outfile]:
            if os.path.exists(file):
                print(f'Deleting existing {file}')
                os.remove(file)

        # Create empty output files
        for file in [labels_png, batches_png, outfile]:
            print(f'Creating empty {file}')
            Path(file).touch()

        # End script
        sys.exit()

    adata = sc.read_h5ad(args.input)

    print('Preparing dataset...')
    if result == 'embed':
        scIB.pp.reduce_data(adata, n_top_genes=None, neighbors=True,
                            use_rep='X_emb', pca=False, umap=False)
    elif result == 'full':
        sc.pp.filter_genes(adata, min_cells=1)
        scIB.pp.reduce_data(adata, n_top_genes=2000, neighbors=True,
                            use_rep='X_pca', pca=True, umap=False)

    # Calculate embedding
    if args.method.startswith('conos'):
        print('Calculating graph embedding...')
        sc.tl.draw_graph(adata, key_added_ext='graph')
        basis = 'draw_graph_graph'
        label = 'Graph'
    else:
        print('Calculating UMAP...')
        sc.tl.umap(adata)
        basis = 'umap'
        label = 'UMAP'

    # Save embedding plots
    if not os.path.isdir(outdir):
        os.mkdir(outdir)
    print(f'Saving embedding plot for labels "{label_key}"...')
    fig = sc.pl.embedding(adata, basis=basis, color=label_key, return_fig=True)
    fig.set_size_inches(10, 8)
    fig.savefig(labels_png, bbox_inches='tight')
    print(f'Saving embedding plot for batches "{batch_key}"...')
    fig = sc.pl.embedding(adata, basis=basis, color=batch_key, return_fig=True)
    fig.set_size_inches(10, 8)
    fig.savefig(batches_png, bbox_inches='tight')

    # Save embedding coordinates
    print('Saving embedding coordinates...')
    adata.obs[label + '1'] = adata.obsm['X_' + basis][:, 0]
    adata.obs[label + '2'] = adata.obsm['X_' + basis][:, 1]
    coords = adata.obs[[label_key, batch_key, label + '1', label + '2' ]]

    coords.to_csv(os.path.join(outfile), index_label='CellID')
<commit_msg>Overwrite with empty file instead of deleting<commit_after>#!/usr/bin/env python
# coding: utf-8

import scanpy as sc
import scIB
import argparse
import os
import sys
import warnings

warnings.filterwarnings('ignore')

RESULT_TYPES = [
    "full",  # reconstructed expression data
    "embed", # embedded/latent space
    "knn"    # corrected neighbourhood graph
]

if __name__=='__main__':
    """
    Save embeddings for all scenarios, methods and settings
    """

    parser = argparse.ArgumentParser(description='Save embeddings')

    parser.add_argument('-i', '--input', required=True)
    parser.add_argument('-o', '--outfile', required=True,
                        help='Output coordinates CSV file')
    parser.add_argument('-m', '--method', required=True, help='Name of method')
    parser.add_argument('-r', '--result', required=True, choices=RESULT_TYPES,
                        help='Type of result: full, embed, knn')
    parser.add_argument('-b', '--batch_key', required=True, help='Key of batch')
    parser.add_argument('-l', '--label_key', required=True,
                        help='Key of annotated labels e.g. "cell_type"')

    args = parser.parse_args()

    outfile = args.outfile
    method = args.method
    result = args.result
    setup = f'{method}_{result}'
    batch_key = args.batch_key
    label_key = args.label_key

    outdir = os.path.dirname(outfile)
    labels_png = os.path.join(outdir, f'{setup}_labels.png')
    batches_png = os.path.join(outdir, f'{setup}_batch.png')

    if os.stat(args.input).st_size == 0:
        print(f'{args.input} is empty, empty output files will be created')

        # Create empty output files
        for file in [labels_png, batches_png, outfile]:
            print(f'Creating empty {file}')
            open(file, "w").close()

        # End script
        sys.exit()

    adata = sc.read_h5ad(args.input)

    print('Preparing dataset...')
    if result == 'embed':
        scIB.pp.reduce_data(adata, n_top_genes=None, neighbors=True,
                            use_rep='X_emb', pca=False, umap=False)
    elif result == 'full':
        sc.pp.filter_genes(adata, min_cells=1)
        scIB.pp.reduce_data(adata, n_top_genes=2000, neighbors=True,
                            use_rep='X_pca', pca=True, umap=False)

    # Calculate embedding
    if args.method.startswith('conos'):
        print('Calculating graph embedding...')
        sc.tl.draw_graph(adata, key_added_ext='graph')
        basis = 'draw_graph_graph'
        label = 'Graph'
    else:
        print('Calculating UMAP...')
        sc.tl.umap(adata)
        basis = 'umap'
        label = 'UMAP'

    # Save embedding plots
    if not os.path.isdir(outdir):
        os.mkdir(outdir)
    print(f'Saving embedding plot for labels "{label_key}"...')
    fig = sc.pl.embedding(adata, basis=basis, color=label_key, return_fig=True)
    fig.set_size_inches(10, 8)
    fig.savefig(labels_png, bbox_inches='tight')
    print(f'Saving embedding plot for batches "{batch_key}"...')
    fig = sc.pl.embedding(adata, basis=basis, color=batch_key, return_fig=True)
    fig.set_size_inches(10, 8)
    fig.savefig(batches_png, bbox_inches='tight')

    # Save embedding coordinates
    print('Saving embedding coordinates...')
    adata.obs[label + '1'] = adata.obsm['X_' + basis][:, 0]
    adata.obs[label + '2'] = adata.obsm['X_' + basis][:, 1]
    coords = adata.obs[[label_key, batch_key, label + '1', label + '2' ]]

    coords.to_csv(os.path.join(outfile), index_label='CellID')
<|endoftext|>


<commit_before>from nose.tools import *
import networkx as nx
import networkx.algorithms.link_prediction.predict as pred
from networkx.exception import *

class TestPredictCommonNeighbors():
    def setUp(self):
        def test_func(G, expected):
            result = list(pred.predict_common_neighbors(G))
            assert len(result) == len(expected)
            for (u, v, p) in expected:
                assert (u, v, p) in result or (v, u, p) in result
        self.test = test_func

    def test_P4(self):
        G = nx.path_graph(4)
        expected = [(0, 2, 1), (0, 3, 0), (1, 3, 1)]
        self.test(G, expected)

    def test_S4(self):
        G = nx.star_graph(4)
        expected = [
            (1, 2, 1), (1, 3, 1), (1, 4, 1), (2, 3, 1), (2, 4, 1), (3, 4, 1)
        ]
        self.test(G, expected)

class TestPredictResourceAllocationIndex():
    def setUp(self):
        def test_func(G, expected):
            result = list(pred.predict_resource_allocation_index(G))
            assert len(result) == len(expected)
            for (u, v, p) in expected:
                assert (u, v, p) in result or (v, u, p) in result
        self.test = test_func

    def test_P4(self):
        G = nx.path_graph(4)
        expected = [(0, 2, 0.5), (0, 3, 0), (1, 3, 0.5)]
        self.test(G, expected)

    def test_S4(self):
        G = nx.star_graph(4)
        expected = [
            (1, 2, 0.25), (1, 3, 0.25), (1, 4, 0.25), (2, 3, 0.25),
            (2, 4, 0.25), (3, 4, 0.25)
        ]
        self.test(G, expected)

class TestPredictCNSoundarajanHopcroft():
    def setUp(self):
        def test_func(G, expected):
            result = list(pred.predict_cn_soundarajan_hopcroft(G))
            assert len(result) == len(expected)
            for (u, v, p) in expected:
                assert (u, v, p) in result or (v, u, p) in result
        self.test = test_func

    def test_P4(self):
        G = nx.path_graph(4)
        G.node[0]['community'] = 0
        G.node[1]['community'] = 0
        G.node[2]['community'] = 0
        G.node[3]['community'] = 0
        expected = [(0, 2, 2), (0, 3, 0), (1, 3, 2)]
        self.test(G, expected)

    def test_S4(self):
        G = nx.star_graph(4)
        G.node[0]['community'] = 1
        G.node[1]['community'] = 1
        G.node[2]['community'] = 1
        G.node[3]['community'] = 0
        G.node[4]['community'] = 0
        expected = [
            (1, 2, 2), (1, 3, 1), (1, 4, 1), (2, 3, 1), (2, 4, 1), (3, 4, 1)
        ]
        self.test(G, expected)

class TestPredictRAIndexSoundarajanHopcroft():
    def setUp(self):
        self.P4 = nx.path_graph(4)
        self.P4.node[0]['community'] = 0
        self.P4.node[1]['community'] = 0
        self.P4.node[2]['community'] = 0
        self.P4.node[3]['community'] = 0

        self.S4 = nx.star_graph(4)
        self.S4.node[0]['community'] = 1
        self.S4.node[1]['community'] = 1
        self.S4.node[2]['community'] = 1
        self.S4.node[3]['community'] = 0
        self.S4.node[4]['community'] = 0

        def test_func(G, expected):
            result = list(pred.predict_ra_index_soundarajan_hopcroft(G))
            assert len(result) == len(expected)
            for (u, v, p) in expected:
                assert (u, v, p) in result or (v, u, p) in result
        self.test = test_func

    def test_P4(self):
        expected = [(0, 2, 0.5), (0, 3, 0), (1, 3, 0.5)]
        self.test(self.P4, expected)

    def test_S4(self):
        expected = [
            (1, 2, 0.25), (1, 3, 0), (1, 4, 0), (2, 3, 0), (2, 4, 0), (3, 4, 0)
        ]
        self.test(self.S4, expected)
<commit_msg>Refactored test codes<commit_after>from nose.tools import *
import networkx as nx
import networkx.algorithms.link_prediction.predict as pred
from networkx.exception import *

class TestPredictCommonNeighbors():
    def setUp(self):
        def test_func(G, expected):
            result = list(pred.predict_common_neighbors(G))
            assert len(result) == len(expected)
            for (u, v, p) in expected:
                assert (u, v, p) in result or (v, u, p) in result
        self.test = test_func

    def test_P4(self):
        G = nx.path_graph(4)
        expected = [(0, 2, 1), (0, 3, 0), (1, 3, 1)]
        self.test(G, expected)

    def test_S4(self):
        G = nx.star_graph(4)
        expected = [
            (1, 2, 1), (1, 3, 1), (1, 4, 1), (2, 3, 1), (2, 4, 1), (3, 4, 1)
        ]
        self.test(G, expected)

class TestPredictResourceAllocationIndex():
    def setUp(self):
        def test_func(G, expected):
            result = list(pred.predict_resource_allocation_index(G))
            assert len(result) == len(expected)
            for (u, v, p) in expected:
                assert (u, v, p) in result or (v, u, p) in result
        self.test = test_func

    def test_P4(self):
        G = nx.path_graph(4)
        expected = [(0, 2, 0.5), (0, 3, 0), (1, 3, 0.5)]
        self.test(G, expected)

    def test_S4(self):
        G = nx.star_graph(4)
        expected = [
            (1, 2, 0.25), (1, 3, 0.25), (1, 4, 0.25), (2, 3, 0.25),
            (2, 4, 0.25), (3, 4, 0.25)
        ]
        self.test(G, expected)

class TestPredictCNSoundarajanHopcroft():
    def setUp(self):
        def test_func(G, expected):
            result = list(pred.predict_cn_soundarajan_hopcroft(G))
            assert len(result) == len(expected)
            for (u, v, p) in expected:
                assert (u, v, p) in result or (v, u, p) in result
        self.test = test_func

    def test_P4(self):
        G = nx.path_graph(4)
        G.node[0]['community'] = 0
        G.node[1]['community'] = 0
        G.node[2]['community'] = 0
        G.node[3]['community'] = 0
        expected = [(0, 2, 2), (0, 3, 0), (1, 3, 2)]
        self.test(G, expected)

    def test_S4(self):
        G = nx.star_graph(4)
        G.node[0]['community'] = 1
        G.node[1]['community'] = 1
        G.node[2]['community'] = 1
        G.node[3]['community'] = 0
        G.node[4]['community'] = 0
        expected = [
            (1, 2, 2), (1, 3, 1), (1, 4, 1), (2, 3, 1), (2, 4, 1), (3, 4, 1)
        ]
        self.test(G, expected)

class TestPredictRAIndexSoundarajanHopcroft():
    def setUp(self):
        def test_func(G, expected):
            result = list(pred.predict_ra_index_soundarajan_hopcroft(G))
            assert len(result) == len(expected)
            for (u, v, p) in expected:
                assert (u, v, p) in result or (v, u, p) in result
        self.test = test_func

    def test_P4(self):
        G = nx.path_graph(4)
        G.node[0]['community'] = 0
        G.node[1]['community'] = 0
        G.node[2]['community'] = 0
        G.node[3]['community'] = 0
        expected = [(0, 2, 0.5), (0, 3, 0), (1, 3, 0.5)]
        self.test(G, expected)

    def test_S4(self):
        G = nx.star_graph(4)
        G.node[0]['community'] = 1
        G.node[1]['community'] = 1
        G.node[2]['community'] = 1
        G.node[3]['community'] = 0
        G.node[4]['community'] = 0
        expected = [
            (1, 2, 0.25), (1, 3, 0), (1, 4, 0), (2, 3, 0), (2, 4, 0), (3, 4, 0)
        ]
        self.test(G, expected)
<|endoftext|>