<commit_before>"""\
Guitar
By Joe Esposito

Contains classes for storing and manipulating guitar tabs in a structured manor.
"""

from collections import OrderedDict
from itertools import product
from StringIO import StringIO

# TODO: str(song), len(song.staffs), song.song_info, song.errors
# def __str__(self):
#     head = str(self.song_info) if self.show_meta else ''
#     body = '\n\n'.join(map(str, self.Staffs))
#     return head + ('\n' if head and body else '') + body

class Song(object):
    """Represents a guitar tablature."""
    def __init__(self, staffs=[], show_meta=True):
        self.staffs = staffs

    @staticmethod
    def parse(s):
        """Parses the specified string and creates a guitar tab song."""
        postfixes = ':', ' :', '-', ' -'
        errors = []
        staffs = []
        fd = StringIO(s)
        try:
            staffstrings = []
            while True:
                line = fd.readline()
                if line == '': break
                line = line.strip()
                if line == '': continue
                # Find song tabs
                line = line.lower()
                staff_prefixes = ['|-'] + map(''.join, product('abcdefg', ['-', ':-', '|-', '||-']))
                if not starts_with_any(line, staff_prefixes):
                    continue
                staffstrings.append(line[2:])
                if len(staffstrings) < 6:
                    continue
                staff, staff_errors = Staff.create(staffstrings)
                staffs.append(staff); errors += staff_errors
                staffstrings = []
        finally:
            fd.close()
        return Song(staffs), errors

    def __str__(self):
        return '\n\n'.join(map(str, self.staffs))

class SongInfo(object):
    """Contains information about a song."""
    def __init__(self, artist=None, title=None, album=None, author=None, url=None):
        self.artist = artist
        self.title = title
        self.album = album
        self.author = author
        self.url = url
        self.show_url = False

    @staticmethod
    def extract(s):
        """Extracts song information from a string."""
        def value_of_any(line, keys):
            """Local function to check a line for a key and return the value or None if not available"""
            lowerline = line.lower()
            for key in keys:
                if lowerline.startswith(key.lower()):
                    return line[len(key):].strip()
            return None
        song_info = SongInfo()
        fd = StringIO(s)
        try:
            while True:
                line = fd.readline()
                if line == '': break
                line = line.strip()
                if line == '': continue
                # Parse song info
                s = value_of_any(line, map(''.join, product(['artist'], postfixes)))
                if s: song_info.artist = s
                s = value_of_any(line, map(''.join, product(['song', 'title'], postfixes)))
                if s: song_info.title = s
                s = value_of_any(line, map(''.join, product(['album'], postfixes)))
                if s: song_info.album = s
                s = value_of_any(line, map(''.join, product(['tabbed by'], postfixes)))
                if s: song_info.author = s
        finally:
            fd.close()
        return song_info

    def __str__(self):
        labels = OrderedDict([('artist', 'Artist'), ('title', 'Title'), ('album', 'Album'), ('author', 'Tabbed by'), ('url', 'Url')])
        values = map(lambda key: getattr(self, key), labels)
        items = filter(lambda x: x[1], zip(labels.keys(), values))
        return '\n'.join(map(lambda item: ': '.join(item), items))

class Staff(object):
    """Class for Guitar Tab staffs."""
    def __init__(self, measures=[]):
        self.measures = measures

    # TODO: Tuning
    base_notes = 'E', 'B', 'G', 'D', 'A', 'E'

    @staticmethod
    def create(staffstrings):
        """Creates a guitar tab staff from a text fragment."""
        if len(staffstrings) != 6:
            raise ValueError('Parameter staffstrings should be a list of length 6, one line for each tab string on the staff.')

        # Find max staff size and normalize
        size = max(map(len, staffstrings))
        staffstrings = map(lambda s: s.ljust(size), staffstrings)

        # Find measures
        errors = []
        measurestrings = [''] * 6
        measures = []
        for i in range(size):
            if not any(map(lambda s: s[i] == '|', staffstrings)):
                for j in range(6):
                    measurestrings[j] += staffstrings[j][i]
            elif len(measurestrings[0]) > 0:
                measure, measure_errors = Measure.create(measurestrings)
                if not measure.is_empty:
                    measures.append(measure)
                errors += measure_errors
                measurestrings = [''] * 6
        if len(measurestrings[0]) > 0:
            measure, measure_errors = Measure.create(measurestrings)
            if not measure.is_empty:
                measures.append(measure)
            errors += measure_errors
        return Staff(measures), errors

    def __str__(self):
        start = map(''.join, product(Staff.base_notes, ['|-']))
        ends = ['-|'] * 6
        s = [''] * 6
        for measure in self.measures:
            m = str(measure).split('\n')
            for i in range(len(s)):
                if s[i] != '': s[i] += '-|-'
                s[i] += m[i]
        s = map(''.join, zip(start, s, ends))
        return '\n'.join(s)

class Measure(object):
    """Class for Guitar Tab measures."""
    def __init__(self, chords=[]):
        self.chords = chords or []
        self.is_empty = len(self.chords) == 0 or all(map(lambda note_list: not note_list, self.chords))

    @staticmethod
    def create(measurestrings):
        """Creates a guitar tab staff from a text fragment."""
        if len(measurestrings) != 6:
            raise ValueError('Parameter measurestrings should be a list of length 6, one line for each tab string on the staff.')
        size = max(map(len, measurestrings))
        measurestrings = map(lambda s: s.ljust(size), measurestrings)

        # Find chords
        errors = []
        note_attr_list = [''] * 6
        note_attr_lists = [list(note_attr_list)]
        note_list = [''] * 6
        note_lists = []
        for i in range(size):
            isbar = True
            isspace = True
            for j in range(6):
                ch = measurestrings[j][i].lower()
                if ch != ' ' and ch != '\t' and ch != '\n': isspace = False
                if ch.isdigit() or ch == 'x' or ch == 't' or ch == '.':
                    note_list[j] += ch
                    isbar = False
                elif ch == '-' or ch == ' ' or ch == '\t':
                    continue
                # Check for attributes
                elif ch == 'h': note_attr_list[j] += 'h'
                elif ch == 'p': note_attr_list[j] += 'p'
                elif ch == '^': note_attr_list[j] += 'p'
                elif ch == 'b': note_attr_lists[-1][j] += 'b'
                elif ch == 'r': note_attr_list[j] += 'r'
                elif ch == '~': note_attr_lists[-1][j] += '~'
                elif ch == 'v': note_attr_lists[-1][j] += '~'
                elif ch == '/': note_attr_list[j] += '/'
                elif ch == '\\':note_attr_list[j] += '/'
                elif ch == 's': note_attr_list[j] += '/'
                else: errors.append("'%s' found in measure and is not recognized; tab may not be accurately formatted" % ch)
            if isbar and any(note_list):
                note_lists.append(note_list)
                note_list = [''] * 6
                note_attr_lists.append(note_attr_list)
                note_attr_list = [''] * 6
            if isspace:
                break
        if any(note_list):
            note_lists.append(note_list)
        for j in range(len(note_attr_list)):
            note_attr_lists[-1][j] += note_attr_list[j]
        # Create chords
        chords = []
        for i in range(len(note_lists)):
            chord, chord_errors = Chord.create(note_lists[i], note_attr_lists[i])
            chords.append(chord)
            errors += chord_errors
        return Measure(chords), errors

    def __str__(self):
        s = [''] * 6
        for chord in self.chords:
            note_list = str(chord).split('\n')
            starts_with_digit = any(map(lambda x: starts_with_any(x, '0123456789'), note_list))
            for i in range(6):
                if starts_with_digit and s[i] != '': s[i] += "--"
                s[i] += note_list[i]
        return '\n'.join(s)

class Chord(object):
    """Class for Guitar Tab chords."""
    def __init__(self, notes_and_attributes=None):
        self.notes_and_attributes = tuple(notes_and_attributes) if notes_and_attributes else (None,) * 6

    @staticmethod
    def create(note_list, note_attr_list):
        """Creates a guitar tab staff from a text fragment."""
        errors = []
        if len(note_attr_list) != 6:
            errors.append('attribute didnt have exactly 6 strings; tab may not be accurately formatted - ' + str(note_attr_list))
            note_attr_list = note_attr_list[:6] + [''] * (6 - len(note_attr_list[:6]))
        if len(note_list) != 6:
            errors.append('chord didnt have exactly 6 strings; tab may not be accurately formatted - ' + str(note_list))
            note_list = note_list[:6] + [''] * (6 - len(note_list[:6]))
        return Chord(zip(map(parse_int_or_string, note_list), note_attr_list)), errors

    def __str__(self):
        notePre, noteVal, notePost = [], [], []
        sizePre, sizeVal, sizePost =  0,  0,  0
        for i in range(6):
            note_and_attribute = self.notes_and_attributes[i]
            pre, val, post = '', '', ''
            if note_and_attribute is not None:
                note, attr = note_and_attribute
                for a in attr:
                    if a == 'h' or a == 'p' or a == 'r' or a == '/':
                        pre += a
                    if a == 'b' or a == '~':
                        post += a
                val = str(note)
            notePre.append(pre)
            noteVal.append(val)
            notePost.append(post)
            sizePre, sizeVal, sizePost = max(len(pre), sizePre), max(len(val), sizeVal), max(len(post), sizePost)
        for i in range(6):
            notePre[i] = notePre[i].rjust(sizePre, '-')
            noteVal[i] = noteVal[i].rjust(sizeVal, '-')
            notePost[i] = notePost[i].ljust(sizePost, '-')
        s = ''
        for i in range(6):
            if s != '':
                s += '\n'
            s += notePre[i]
            s += noteVal[i]
            s += notePost[i]
        return s

def parse_int_or_string(s):
    """Parse an integer or return the original string if it cannot be done."""
    try:
        return int(s)
    except ValueError:
        return s

def starts_with_any(s, prefixes):
    for prefix in prefixes:
        if s.startswith(prefix):
            return True
    return False
<commit_msg>Use 'for' over the open file instead of a 'while' loop with 'readline'.<commit_after>"""\
Guitar
By Joe Esposito

Contains classes for storing and manipulating guitar tabs in a structured manor.
"""

from collections import OrderedDict
from itertools import product
from StringIO import StringIO

# TODO: str(song), len(song.staffs), song.song_info, song.errors
# def __str__(self):
#     head = str(self.song_info) if self.show_meta else ''
#     body = '\n\n'.join(map(str, self.Staffs))
#     return head + ('\n' if head and body else '') + body

class Song(object):
    """Represents a guitar tablature."""
    def __init__(self, staffs=[], show_meta=True):
        self.staffs = staffs

    @staticmethod
    def parse(s):
        """Parses the specified string and creates a guitar tab song."""
        postfixes = ':', ' :', '-', ' -'
        errors = []
        staffs = []
        fd = StringIO(s)
        try:
            staffstrings = []
            for line in fd:
                line = line.strip()
                if line == '': continue
                # Find song tabs
                line = line.lower()
                staff_prefixes = ['|-'] + map(''.join, product('abcdefg', ['-', ':-', '|-', '||-']))
                if not starts_with_any(line, staff_prefixes):
                    continue
                staffstrings.append(line[2:])
                if len(staffstrings) < 6:
                    continue
                staff, staff_errors = Staff.create(staffstrings)
                staffs.append(staff); errors += staff_errors
                staffstrings = []
        finally:
            fd.close()
        return Song(staffs), errors

    def __str__(self):
        return '\n\n'.join(map(str, self.staffs))

class SongInfo(object):
    """Contains information about a song."""
    def __init__(self, artist=None, title=None, album=None, author=None, url=None):
        self.artist = artist
        self.title = title
        self.album = album
        self.author = author
        self.url = url
        self.show_url = False

    @staticmethod
    def extract(s):
        """Extracts song information from a string."""
        def value_of_any(line, keys):
            """Local function to check a line for a key and return the value or None if not available"""
            lowerline = line.lower()
            for key in keys:
                if lowerline.startswith(key.lower()):
                    return line[len(key):].strip()
            return None
        song_info = SongInfo()
        fd = StringIO(s)
        try:
            for line in fd:
                line = line.strip()
                if line == '': continue
                # Parse song info
                s = value_of_any(line, map(''.join, product(['artist'], postfixes)))
                if s: song_info.artist = s
                s = value_of_any(line, map(''.join, product(['song', 'title'], postfixes)))
                if s: song_info.title = s
                s = value_of_any(line, map(''.join, product(['album'], postfixes)))
                if s: song_info.album = s
                s = value_of_any(line, map(''.join, product(['tabbed by'], postfixes)))
                if s: song_info.author = s
        finally:
            fd.close()
        return song_info

    def __str__(self):
        labels = OrderedDict([('artist', 'Artist'), ('title', 'Title'), ('album', 'Album'), ('author', 'Tabbed by'), ('url', 'Url')])
        values = map(lambda key: getattr(self, key), labels)
        items = filter(lambda x: x[1], zip(labels.keys(), values))
        return '\n'.join(map(lambda item: ': '.join(item), items))

class Staff(object):
    """Class for Guitar Tab staffs."""
    def __init__(self, measures=[]):
        self.measures = measures

    # TODO: Tuning
    base_notes = 'E', 'B', 'G', 'D', 'A', 'E'

    @staticmethod
    def create(staffstrings):
        """Creates a guitar tab staff from a text fragment."""
        if len(staffstrings) != 6:
            raise ValueError('Parameter staffstrings should be a list of length 6, one line for each tab string on the staff.')

        # Find max staff size and normalize
        size = max(map(len, staffstrings))
        staffstrings = map(lambda s: s.ljust(size), staffstrings)

        # Find measures
        errors = []
        measurestrings = [''] * 6
        measures = []
        for i in range(size):
            if not any(map(lambda s: s[i] == '|', staffstrings)):
                for j in range(6):
                    measurestrings[j] += staffstrings[j][i]
            elif len(measurestrings[0]) > 0:
                measure, measure_errors = Measure.create(measurestrings)
                if not measure.is_empty:
                    measures.append(measure)
                errors += measure_errors
                measurestrings = [''] * 6
        if len(measurestrings[0]) > 0:
            measure, measure_errors = Measure.create(measurestrings)
            if not measure.is_empty:
                measures.append(measure)
            errors += measure_errors
        return Staff(measures), errors

    def __str__(self):
        start = map(''.join, product(Staff.base_notes, ['|-']))
        ends = ['-|'] * 6
        s = [''] * 6
        for measure in self.measures:
            m = str(measure).split('\n')
            for i in range(len(s)):
                if s[i] != '': s[i] += '-|-'
                s[i] += m[i]
        s = map(''.join, zip(start, s, ends))
        return '\n'.join(s)

class Measure(object):
    """Class for Guitar Tab measures."""
    def __init__(self, chords=[]):
        self.chords = chords or []
        self.is_empty = len(self.chords) == 0 or all(map(lambda note_list: not note_list, self.chords))

    @staticmethod
    def create(measurestrings):
        """Creates a guitar tab staff from a text fragment."""
        if len(measurestrings) != 6:
            raise ValueError('Parameter measurestrings should be a list of length 6, one line for each tab string on the staff.')
        size = max(map(len, measurestrings))
        measurestrings = map(lambda s: s.ljust(size), measurestrings)

        # Find chords
        errors = []
        note_attr_list = [''] * 6
        note_attr_lists = [list(note_attr_list)]
        note_list = [''] * 6
        note_lists = []
        for i in range(size):
            isbar = True
            isspace = True
            for j in range(6):
                ch = measurestrings[j][i].lower()
                if ch != ' ' and ch != '\t' and ch != '\n': isspace = False
                if ch.isdigit() or ch == 'x' or ch == 't' or ch == '.':
                    note_list[j] += ch
                    isbar = False
                elif ch == '-' or ch == ' ' or ch == '\t':
                    continue
                # Check for attributes
                elif ch == 'h': note_attr_list[j] += 'h'
                elif ch == 'p': note_attr_list[j] += 'p'
                elif ch == '^': note_attr_list[j] += 'p'
                elif ch == 'b': note_attr_lists[-1][j] += 'b'
                elif ch == 'r': note_attr_list[j] += 'r'
                elif ch == '~': note_attr_lists[-1][j] += '~'
                elif ch == 'v': note_attr_lists[-1][j] += '~'
                elif ch == '/': note_attr_list[j] += '/'
                elif ch == '\\':note_attr_list[j] += '/'
                elif ch == 's': note_attr_list[j] += '/'
                else: errors.append("'%s' found in measure and is not recognized; tab may not be accurately formatted" % ch)
            if isbar and any(note_list):
                note_lists.append(note_list)
                note_list = [''] * 6
                note_attr_lists.append(note_attr_list)
                note_attr_list = [''] * 6
            if isspace:
                break
        if any(note_list):
            note_lists.append(note_list)
        for j in range(len(note_attr_list)):
            note_attr_lists[-1][j] += note_attr_list[j]
        # Create chords
        chords = []
        for i in range(len(note_lists)):
            chord, chord_errors = Chord.create(note_lists[i], note_attr_lists[i])
            chords.append(chord)
            errors += chord_errors
        return Measure(chords), errors

    def __str__(self):
        s = [''] * 6
        for chord in self.chords:
            note_list = str(chord).split('\n')
            starts_with_digit = any(map(lambda x: starts_with_any(x, '0123456789'), note_list))
            for i in range(6):
                if starts_with_digit and s[i] != '': s[i] += "--"
                s[i] += note_list[i]
        return '\n'.join(s)

class Chord(object):
    """Class for Guitar Tab chords."""
    def __init__(self, notes_and_attributes=None):
        self.notes_and_attributes = tuple(notes_and_attributes) if notes_and_attributes else (None,) * 6

    @staticmethod
    def create(note_list, note_attr_list):
        """Creates a guitar tab staff from a text fragment."""
        errors = []
        if len(note_attr_list) != 6:
            errors.append('attribute didnt have exactly 6 strings; tab may not be accurately formatted - ' + str(note_attr_list))
            note_attr_list = note_attr_list[:6] + [''] * (6 - len(note_attr_list[:6]))
        if len(note_list) != 6:
            errors.append('chord didnt have exactly 6 strings; tab may not be accurately formatted - ' + str(note_list))
            note_list = note_list[:6] + [''] * (6 - len(note_list[:6]))
        return Chord(zip(map(parse_int_or_string, note_list), note_attr_list)), errors

    def __str__(self):
        notePre, noteVal, notePost = [], [], []
        sizePre, sizeVal, sizePost =  0,  0,  0
        for i in range(6):
            note_and_attribute = self.notes_and_attributes[i]
            pre, val, post = '', '', ''
            if note_and_attribute is not None:
                note, attr = note_and_attribute
                for a in attr:
                    if a == 'h' or a == 'p' or a == 'r' or a == '/':
                        pre += a
                    if a == 'b' or a == '~':
                        post += a
                val = str(note)
            notePre.append(pre)
            noteVal.append(val)
            notePost.append(post)
            sizePre, sizeVal, sizePost = max(len(pre), sizePre), max(len(val), sizeVal), max(len(post), sizePost)
        for i in range(6):
            notePre[i] = notePre[i].rjust(sizePre, '-')
            noteVal[i] = noteVal[i].rjust(sizeVal, '-')
            notePost[i] = notePost[i].ljust(sizePost, '-')
        s = ''
        for i in range(6):
            if s != '':
                s += '\n'
            s += notePre[i]
            s += noteVal[i]
            s += notePost[i]
        return s

def parse_int_or_string(s):
    """Parse an integer or return the original string if it cannot be done."""
    try:
        return int(s)
    except ValueError:
        return s

def starts_with_any(s, prefixes):
    for prefix in prefixes:
        if s.startswith(prefix):
            return True
    return False
<|endoftext|>


<commit_before>from featurex.stimuli import video
from featurex.extractors import StimExtractor
from featurex.core import Value
import time
import numpy as np
import tempfile
import os

# Optional dependencies
try:
    from metamind.api import (set_api_key, get_api_key,
                              general_image_classifier, ClassificationModel)
except ImportError:
    pass

try:
    import cv2
except ImportError:
    pass

class ImageExtractor(StimExtractor):

    ''' Base Image Extractor class; all subclasses can only be applied to
    images. '''
    target = video.ImageStim

class CornerDetectionExtractor(ImageExtractor):

    ''' Wraps OpenCV's FastFeatureDetector; should not be used for anything
    important yet. '''

    def __init__(self):
        super(self.__class__, self).__init__()
        self.fast = cv2.FastFeatureDetector()

    def apply(self, img):
        kp = self.fast.detect(img, None)
        return Value(img, self, {'corners_detected': kp})

class FaceDetectionExtractor(ImageExtractor):

    ''' Face detection based on OpenCV's CascadeClassifier. This will generally
    not work well without training, and should not be used for anything
    important at the moment. '''

    def __init__(self):
        self.cascade = cv2.CascadeClassifier(
            '/Users/tal/Downloads/cascade.xml')
        super(self.__class__, self).__init__()

    def apply(self, img, show=False):
        data = img.data
        gray = cv2.cvtColor(data, cv2.COLOR_BGR2GRAY)
        faces = self.cascade.detectMultiScale(
            gray,
            scaleFactor=1.3,
            minNeighbors=3,
            minSize=(30, 30),
            flags=cv2.cv.CV_HAAR_SCALE_IMAGE
        )

        if show:
            for (x, y, w, h) in faces:
                cv2.rectangle(data, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.imshow('frame', data)
            cv2.waitKey(1)

        return Value(img, self, {'num_faces': len(faces)})

class BrightnessExtractor(ImageExtractor):

    ''' Gets the average luminosity of the pixels in the image '''

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

    def apply(self, img):
        data = img.data
        hsv = cv2.cvtColor(data, cv2.COLOR_BGR2HSV)
        avg_brightness = hsv[:, :, 2].mean()

        return Value(img, self, {'avg_brightness': avg_brightness})

class SharpnessExtractor(ImageExtractor):

    ''' Gets the degree of blur/sharpness of the image '''

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

    def apply(self, img):
        # Taken from
        # http://stackoverflow.com/questions/7765810/is-there-a-way-to-detect-if-an-image-is-blurry?lq=1
        data = img.data
        gray_image = cv2.cvtColor(data, cv2.COLOR_BGR2GRAY)

        sharpness = np.max(cv2.convertScaleAbs(cv2.Laplacian(gray_image, 3)))
        return Value(img, self, {'sharpness': sharpness})

class MetamindFeaturesExtractor(ImageExtractor):

    ''' Uses the MetaMind API to extract features with an existing classifier.
    Args:
        api_key (str): A valid key for the MetaMind API. Only needs to be
            passed the first time a MetaMindExtractor is initialized.
        classifier (str, int): The name or ID of the MetaMind classifier to
            use. If None or 'general', defaults to the general image
            classifier. Otherwise, must be an integer ID for the desired
            classifier.
    '''

    def __init__(self, api_key=None, classifier=None):
        ImageExtractor.__init__(self)
        api_key = get_api_key() if api_key is None else api_key
        if api_key is None:
            raise ValueError("A valid MetaMind API key must be passed the "
                             "first time a MetaMind extractor is initialized.")
        set_api_key(api_key, verbose=False)

        # TODO: Can add a lookup dictionary somewhere that has name --> ID
        # translation for commonly used classifiers.
        if classifier is None:
            self.classifier = general_image_classifier
        else:
            self.classifier = ClassificationModel(id=classifier)

    def apply(self, img):
        data = img.data
        temp_file = tempfile.mktemp()
        cv2.imwrite(temp_file, data)
        labels = self.classifier.predict(temp_file, input_type='files')
        os.remove(temp_file)
        time.sleep(1.0)  # Prevents server error somewhat

        return Value(img, self, {'labels': labels})
<commit_msg>small changes and temp file fix<commit_after>from featurex.stimuli import video
from featurex.extractors import StimExtractor
from featurex.core import Value
import time
import numpy as np
import tempfile
import os

# Optional dependencies
try:
    from metamind.api import (set_api_key, get_api_key,
                              general_image_classifier, ClassificationModel)
except ImportError:
    pass

try:
    import cv2
except ImportError:
    pass

class ImageExtractor(StimExtractor):

    ''' Base Image Extractor class; all subclasses can only be applied to
    images. '''
    target = video.ImageStim

class CornerDetectionExtractor(ImageExtractor):

    ''' Wraps OpenCV's FastFeatureDetector; should not be used for anything
    important yet. '''

    def __init__(self):
        super(self.__class__, self).__init__()
        self.fast = cv2.FastFeatureDetector()

    def apply(self, img):
        kp = self.fast.detect(img, None)
        return Value(img, self, {'corners_detected': kp})

class FaceDetectionExtractor(ImageExtractor):

    ''' Face detection based on OpenCV's CascadeClassifier. This will generally
    not work well without training, and should not be used for anything
    important at the moment. '''

    def __init__(self):
        self.cascade = cv2.CascadeClassifier(
            '/Users/tal/Downloads/cascade.xml')
        super(self.__class__, self).__init__()

    def apply(self, img, show=False):
        data = img.data
        gray = cv2.cvtColor(data, cv2.COLOR_BGR2GRAY)
        faces = self.cascade.detectMultiScale(
            gray,
            scaleFactor=1.3,
            minNeighbors=3,
            minSize=(30, 30),
            flags=cv2.cv.CV_HAAR_SCALE_IMAGE
        )

        if show:
            for (x, y, w, h) in faces:
                cv2.rectangle(data, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.imshow('frame', data)
            cv2.waitKey(1)

        return Value(img, self, {'num_faces': len(faces)})

class BrightnessExtractor(ImageExtractor):

    ''' Gets the average luminosity of the pixels in the image '''

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

    def apply(self, img):
        data = img.data
        hsv = cv2.cvtColor(data, cv2.COLOR_BGR2HSV)
        avg_brightness = hsv[:, :, 2].mean() / 255.0

        return Value(img, self, {'avg_brightness': avg_brightness})

class SharpnessExtractor(ImageExtractor):

    ''' Gets the degree of blur/sharpness of the image '''

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

    def apply(self, img):
        # Taken from
        # http://stackoverflow.com/questions/7765810/is-there-a-way-to-detect-if-an-image-is-blurry?lq=1
        data = img.data
        gray_image = cv2.cvtColor(data, cv2.COLOR_BGR2GRAY)

        sharpness = np.max(cv2.convertScaleAbs(cv2.Laplacian(gray_image, 3))) / 255.0
        return Value(img, self, {'sharpness': sharpness})

class MetamindFeaturesExtractor(ImageExtractor):

    ''' Uses the MetaMind API to extract features with an existing classifier.
    Args:
        api_key (str): A valid key for the MetaMind API. Only needs to be
            passed the first time a MetaMindExtractor is initialized.
        classifier (str, int): The name or ID of the MetaMind classifier to
            use. If None or 'general', defaults to the general image
            classifier. Otherwise, must be an integer ID for the desired
            classifier.
    '''

    def __init__(self, api_key=None, classifier=None):
        ImageExtractor.__init__(self)
        api_key = get_api_key() if api_key is None else api_key
        if api_key is None:
            raise ValueError("A valid MetaMind API key must be passed the "
                             "first time a MetaMind extractor is initialized.")
        set_api_key(api_key, verbose=False)

        # TODO: Can add a lookup dictionary somewhere that has name --> ID
        # translation for commonly used classifiers.
        if classifier is None:
            self.classifier = general_image_classifier
        else:
            self.classifier = ClassificationModel(id=classifier)

    def apply(self, img):
        data = img.data
        temp_file = tempfile.mktemp() + '.png'
        cv2.imwrite(temp_file, data)
        labels = self.classifier.predict(temp_file, input_type='files')
        os.remove(temp_file)
        time.sleep(1.0)  # Prevents server error somewhat

        return Value(img, self, {'labels': labels})
<|endoftext|>


<commit_before># Flask imports
from flask import Flask, render_template, session, redirect, url_for, escape, request
# Peewee
from peewee import *
# File manipulation
import os
from settings import APP_STATIC
# Other
#from datetime import date

# Create Flask app
app = Flask(__name__)

# Load secret key
with open(os.path.join(APP_STATIC, 'file.dat')) as f:
    app.secret_key = f.read()

# http://docs.peewee-orm.com/en/latest/peewee/quickstart.html
database = SqliteDatabase('developmentData.db')

#class Device(Model):
#	idNumber = IntField()
#	serialNumber = CharField()
#	typeCategory = CharField()
#	description = TextField()
#	issues = TextField()
#	photo = CharField()
#	quality = CharField()

@app.route('/')
def index():
	# http://flask.pocoo.org/snippets/15/
	if 'username' in session:
		return render_template('inventory.html', inventoryData="", deviceLogData="")
	return redirect(url_for('login'))

@app.route('/login', methods=['GET', 'POST'])
def login():
	if request.method == 'POST':
		try:
			session['username'] = request.form['username']
		except Exception as e:
			return str(e)
		return 'finished'#redirect(url_for('index'))
	return '''<form action="" method="post">
<input type=text name=username>
<input type=submit value=Login></form>'''

@app.route('/logout')
def logout():
	session.pop('username', None)
	return redirect(url_for('index'))

app.secret_key

if __name__ == '__main__':
	db.connect()
	app.run(debug = True)
<commit_msg>Test file reading<commit_after># Flask imports
from flask import Flask, render_template, session, redirect, url_for, escape, request
# Peewee
from peewee import *
# File manipulation
import os
from settings import APP_STATIC
# Other
#from datetime import date

# Create Flask app
app = Flask(__name__)

# Load secret key
with open('static/file.dat', 'r') as f:
    app.secret_key = f.read()

# http://docs.peewee-orm.com/en/latest/peewee/quickstart.html
database = SqliteDatabase('developmentData.db')

#class Device(Model):
#	idNumber = IntField()
#	serialNumber = CharField()
#	typeCategory = CharField()
#	description = TextField()
#	issues = TextField()
#	photo = CharField()
#	quality = CharField()

@app.route('/')
def index():
	# http://flask.pocoo.org/snippets/15/
	if 'username' in session:
		return render_template('inventory.html', inventoryData="", deviceLogData="")
	return redirect(url_for('login'))

@app.route('/login', methods=['GET', 'POST'])
def login():
	if request.method == 'POST':
		try:
			session['username'] = request.form['username']
		except Exception as e:
			return str(e)
		return 'finished'#redirect(url_for('index'))
	return '''<form action="" method="post">
<input type=text name=username>
<input type=submit value=Login></form>'''

@app.route('/logout')
def logout():
	session.pop('username', None)
	return redirect(url_for('index'))

app.secret_key

if __name__ == '__main__':
	db.connect()
	app.run(debug = True)
<|endoftext|>


<commit_before># Generated by Django 3.1.4 on 2020-12-11 00:39

from django.db import migrations

def copy_columns(apps, schema_editor):
    Exercise = apps.get_model('exercises', 'Exercise')
    ExerciseBase = apps.get_model('exercises', 'ExerciseBase')

    for exercise in Exercise.objects.all():
        exercise_base = ExerciseBase.objects.create(
            category=exercise.category,
        )
        exercise_base.equipment.set(exercise.equipment.all())
        exercise_base.muscles.set(exercise.muscles.all())
        exercise_base.muscles_secondary.set(exercise.muscles_secondary.all())
        exercise_base.status = exercise.status
        exercise_base.save()

        exercise.exercise_base = exercise_base
        exercise.save()

class Migration(migrations.Migration):

    dependencies = [
        ('exercises', '0008_exercisebase'),
    ]

    operations = [
        migrations.RunPython(copy_columns),
    ]
<commit_msg>Also set the license data when copying over from the exercise model<commit_after># Generated by Django 3.1.4 on 2020-12-11 00:39

from django.db import migrations

def copy_columns(apps, schema_editor):
    Exercise = apps.get_model('exercises', 'Exercise')
    ExerciseBase = apps.get_model('exercises', 'ExerciseBase')

    for exercise in Exercise.objects.all():
        exercise_base = ExerciseBase.objects.create(
            category=exercise.category,
        )
        exercise_base.equipment.set(exercise.equipment.all())
        exercise_base.muscles.set(exercise.muscles.all())
        exercise_base.muscles_secondary.set(exercise.muscles_secondary.all())
        exercise_base.status = exercise.status
        exercise_base.license = exercise.license
        exercise_base.license_author = exercise.license_author
        exercise_base.license_author = exercise.license_author
        exercise_base.save()

        exercise.exercise_base = exercise_base
        exercise.save()

class Migration(migrations.Migration):

    dependencies = [
        ('exercises', '0008_exercisebase'),
    ]

    operations = [
        migrations.RunPython(copy_columns),
    ]
<|endoftext|>


<commit_before>import os
import re
import pytest

from threadpoolctl import threadpool_limits, threadpool_info, _ThreadpoolInfo
from threadpoolctl import _ALL_PREFIXES, _ALL_USER_APIS

from .utils import cython_extensions_compiled
from .utils import libopenblas_paths
from .utils import scipy

def is_old_openblas(module):
    # Possible bug in getting maximum number of threads with OpenBLAS < 0.2.16
    # and OpenBLAS does not expose its version before 0.3.4.
    return module.internal_api == "openblas" and module.version is None

def effective_num_threads(nthreads, max_threads):
    if nthreads is None or nthreads > max_threads:
        return max_threads
    return nthreads

def _threadpool_info():
    # Like threadpool_info but return the object instead of the list of dicts
    return _ThreadpoolInfo(user_api=_ALL_USER_APIS)

def test_threadpool_limits_public_api():
    # Check consistency between threadpool_info and _ThreadpoolInfo
    public_info = threadpool_info()
    private_info = _threadpool_info()

    for module1, module2 in zip(public_info, private_info):
        assert module1 == module2.todict()

def test_ThreadpoolInfo_todicts():
    info = _threadpool_info()

    assert info.todicts() == [module.todict() for module in info]
    assert info.todicts() == [module.todict() for module in info.modules]

    for module in info:
        module_dict = module.todict()
        assert "user_api" in module_dict
        assert "internal_api" in module_dict
        assert "prefix" in module_dict
        assert "filepath" in module_dict
        assert "version" in module_dict
        assert "num_threads" in module_dict

@pytest.mark.parametrize("prefix", _ALL_PREFIXES)
@pytest.mark.parametrize("limit", [1, 3])
def test_threadpool_limits_by_prefix(prefix, limit):
    # Check that the maximum number of threads can be set by prefix
    original_infos = _threadpool_info()

    modules_matching_prefix = original_infos.get_modules("prefix", prefix)
    if not modules_matching_prefix:
        pytest.skip("Requires {} runtime".format(prefix))

    with threadpool_limits(limits={prefix: limit}):
        for module in modules_matching_prefix:
            if is_old_openblas(module):
                continue
            # threadpool_limits only sets an upper bound on the number of
            # threads.
            assert 0 < module.get_num_threads() <= limit
    assert _threadpool_info() == original_infos

@pytest.mark.parametrize("user_api", (None, "blas", "openmp"))
@pytest.mark.parametrize("limit", [1, 3])
def test_set_threadpool_limits_by_api(user_api, limit):
    # Check that the maximum number of threads can be set by user_api
    original_infos = _threadpool_info()

    modules_matching_api = original_infos.get_modules("user_api", user_api)
    if not modules_matching_api:
        user_apis = _ALL_USER_APIS if user_api is None else [user_api]
        pytest.skip("Requires a library which api is in {}".format(user_apis))

    with threadpool_limits(limits=limit, user_api=user_api):
        for module in modules_matching_api:
            if is_old_openblas(module):
                continue
            # threadpool_limits only sets an upper bound on the number of
            # threads.
            assert 0 < module.get_num_threads() <= limit

    assert _threadpool_info() == original_infos

def test_threadpool_limits_function_with_side_effect():
    # Check that threadpool_limits can be used as a function with
    # side effects instead of a context manager.
    original_infos = _threadpool_info()

    threadpool_limits(limits=1)
    try:
        for module in _threadpool_info():
            if is_old_openblas(module):
                continue
            assert module.num_threads == 1
    finally:
        # Restore the original limits so that this test does not have any
        # side-effect.
        threadpool_limits(limits=original_infos)

    assert _threadpool_info() == original_infos

def test_set_threadpool_limits_no_limit():
    # Check that limits=None does nothing.
    original_infos = _threadpool_info()
    with threadpool_limits(limits=None):
        assert _threadpool_info() == original_infos

    assert _threadpool_info() == original_infos

def test_threadpool_limits_manual_unregister():
    # Check that threadpool_limits can be used as an object which holds the
    # original state of the threadpools and that can be restored thanks to the
    # dedicated unregister method
    original_infos = _threadpool_info()

    limits = threadpool_limits(limits=1)
    try:
        for module in _threadpool_info():
            if is_old_openblas(module):
                continue
            assert module.num_threads == 1
    finally:
        # Restore the original limits so that this test does not have any
        # side-effect.
        limits.unregister()

    assert _threadpool_info() == original_infos

def test_threadpool_limits_bad_input():
    # Check that appropriate errors are raised for invalid arguments
    match = re.escape("user_api must be either in {} or None."
                      .format(_ALL_USER_APIS))
    with pytest.raises(ValueError, match=match):
        threadpool_limits(limits=1, user_api="wrong")

    with pytest.raises(TypeError,
                       match="limits must either be an int, a list or a dict"):
        threadpool_limits(limits=(1, 2, 3))

@pytest.mark.skipif(not cython_extensions_compiled,
                    reason='Requires cython extensions to be compiled')
@pytest.mark.parametrize('num_threads', [1, 2, 4])
def test_openmp_limit_num_threads(num_threads):
    # checks that OpenMP effectively uses the number of threads requested by
    # the context manager
    from ._openmp_test_helper import check_openmp_num_threads

    old_num_threads = check_openmp_num_threads(100)

    with threadpool_limits(limits=num_threads):
        assert check_openmp_num_threads(100) in (num_threads, old_num_threads)
    assert check_openmp_num_threads(100) == old_num_threads

@pytest.mark.skipif(not cython_extensions_compiled,
                    reason='Requires cython extensions to be compiled')
@pytest.mark.parametrize('nthreads_outer', [None, 1, 2, 4])
def test_openmp_nesting(nthreads_outer):
    # checks that OpenMP effectively uses the number of threads requested by
    # the context manager when nested in an outer OpenMP loop.
    from ._openmp_test_helper import check_nested_openmp_loops
    from ._openmp_test_helper import get_inner_compiler
    from ._openmp_test_helper import get_outer_compiler

    inner_cc = get_inner_compiler()
    outer_cc = get_outer_compiler()

    outer_num_threads, inner_num_threads = check_nested_openmp_loops(10)

    original_infos = _threadpool_info()
    openmp_infos = original_infos.get_modules("user_api", "openmp")

    if "gcc" in (inner_cc, outer_cc):
        assert original_infos.get_modules("prefix", "libgomp")

    if "clang" in (inner_cc, outer_cc):
        assert original_infos.get_modules("prefix", "libomp")

    if inner_cc == outer_cc:
        # The openmp runtime should be shared by default, meaning that
        # the inner loop should automatically be run serially by the OpenMP
        # runtime.
        assert inner_num_threads == 1
    else:
        # There should be at least 2 OpenMP runtime detected.
        assert len(openmp_infos) >= 2

    with threadpool_limits(limits=1) as threadpoolctx:
        max_threads = threadpoolctx.get_original_num_threads()["openmp"]
        nthreads = effective_num_threads(nthreads_outer, max_threads)

        # Ask outer loop to run on nthreads threads and inner loop run on 1
        # thread
        outer_num_threads, inner_num_threads = \
            check_nested_openmp_loops(10, nthreads)

    # The state of the original state of all threadpools should have been
    # restored.
    assert _threadpool_info() == original_infos

    # The number of threads available in the outer loop should not have been
    # decreased:
    assert outer_num_threads == nthreads

    # The number of threads available in the inner loop should have been
    # set to 1 so avoid oversubscription and preserve performance:
    if inner_cc != outer_cc:
        if inner_num_threads != 1:
            # XXX: this does not always work when nesting independent openmp
            # implementations. See: https://github.com/jeremiedbb/Nested_OpenMP
            pytest.xfail("Inner OpenMP num threads was %d instead of 1"
                         % inner_num_threads)
    assert inner_num_threads == 1

def test_shipped_openblas():
    # checks that OpenBLAS effectively uses the number of threads requested by
    # the context manager
    original_info = _threadpool_info()

    openblas_modules = original_info.get_modules("internal_api", "openblas")

    with threadpool_limits(1):
        for module in openblas_modules:
            assert module.get_num_threads() == 1

    assert original_info == _threadpool_info()

@pytest.mark.skipif(len(libopenblas_paths) < 2,
                    reason="need at least 2 shipped openblas library")
def test_multiple_shipped_openblas():
    # This redundant test is meant to make it easier to see if the system
    # has 2 or more active openblas runtimes available just be reading the
    # pytest report (whether or not this test has been skipped).
    test_shipped_openblas()

@pytest.mark.skipif(scipy is None, reason="requires scipy")
@pytest.mark.parametrize("nthreads_outer", [None, 1, 2, 4])
def test_nested_prange_blas(nthreads_outer):
    # Check that the BLAS linked to scipy effectively uses the number of
    # threads requested by the context manager when nested in an outer OpenMP
    # loop.
    import numpy as np
    from ._openmp_test_helper import check_nested_prange_blas

    original_info = _threadpool_info()

    blas_info = original_info.get_modules("user_api", "blas")
    blis_info = original_info.get_modules("internal_api", "blis")

    # skip if the BLAS used by numpy is an old openblas. OpenBLAS 0.3.3 and
    # older are known to cause an unrecoverable deadlock at process shutdown
    # time (after pytest has exited).
    # numpy can be linked to BLIS for CBLAS and OpenBLAS for LAPACK. In that
    # case this test will run BLIS gemm so no need to skip.
    if not blis_info and any(is_old_openblas(module) for module in blas_info):
        pytest.skip("Old OpenBLAS: skipping test to avoid deadlock")

    A = np.ones((1000, 10))
    B = np.ones((100, 10))

    with threadpool_limits(limits=1) as threadpoolctx:
        max_threads = threadpoolctx.get_original_num_threads()["openmp"]
        nthreads = effective_num_threads(nthreads_outer, max_threads)

        result = check_nested_prange_blas(A, B, nthreads)
        C, prange_num_threads, threadpool_infos = result

    assert np.allclose(C, np.dot(A, B.T))
    assert prange_num_threads == nthreads

    nested_blas_info = threadpool_infos.get_modules("user_api", "blas")
    assert len(nested_blas_info) == len(blas_info)
    for module in nested_blas_info:
        assert module.num_threads == 1

    assert original_info == _threadpool_info()

@pytest.mark.parametrize("limit", [1, None])
def test_get_original_num_threads(limit):
    # Tests the method get_original_num_threads of the context manager
    with threadpool_limits(limits=2, user_api="blas") as ctl:
        # set different blas num threads to start with (when multiple openblas)
        if ctl._original_info:
            ctl._original_info.modules[0].set_num_threads(1)

        original_infos = _threadpool_info()
        with threadpool_limits(limits=limit, user_api="blas") as threadpoolctx:
            original_num_threads = threadpoolctx.get_original_num_threads()
            print(original_num_threads)

            assert "openmp" not in original_num_threads

            blas_infos = original_infos.get_modules("user_api", "blas")
            if blas_infos:
                expected = min(module.num_threads for module in blas_infos)
                assert original_num_threads["blas"] == expected
            else:
                assert original_num_threads["blas"] is None

            if len(libopenblas_paths) >= 2:
                with pytest.warns(None, match="Multiple value possible"):
                    threadpoolctx.get_original_num_threads()

def test_mkl_threading_layer():
    # Check that threadpool_info correctly recovers the threading layer used
    # by mkl
    mkl_info = _threadpool_info().get_modules("internal_api", "mkl")
    expected_layer = os.getenv("MKL_THREADING_LAYER")

    if not (mkl_info and expected_layer):
        pytest.skip("requires MKL and the environment variable "
                    "MKL_THREADING_LAYER set")

    actual_layer = mkl_info.modules[0].threading_layer
    assert actual_layer == expected_layer.lower()
<commit_msg>better tests for todict(s) methods<commit_after>import os
import re
import pytest

from threadpoolctl import threadpool_limits, threadpool_info, _ThreadpoolInfo
from threadpoolctl import _ALL_PREFIXES, _ALL_USER_APIS

from .utils import cython_extensions_compiled
from .utils import libopenblas_paths
from .utils import scipy

def is_old_openblas(module):
    # Possible bug in getting maximum number of threads with OpenBLAS < 0.2.16
    # and OpenBLAS does not expose its version before 0.3.4.
    return module.internal_api == "openblas" and module.version is None

def effective_num_threads(nthreads, max_threads):
    if nthreads is None or nthreads > max_threads:
        return max_threads
    return nthreads

def _threadpool_info():
    # Like threadpool_info but return the object instead of the list of dicts
    return _ThreadpoolInfo(user_api=_ALL_USER_APIS)

def test_threadpool_limits_public_api():
    # Check consistency between threadpool_info and _ThreadpoolInfo
    public_info = threadpool_info()
    private_info = _threadpool_info()

    for module1, module2 in zip(public_info, private_info):
        assert module1 == module2.todict()

def test_ThreadpoolInfo_todicts():
    # Check all keys expected for the public api are in the dicts returned by
    # the .todict(s) methods
    info = _threadpool_info()

    assert threadpool_info() == [module.todict() for module in info.modules]
    assert info.todicts() == [module.todict() for module in info]
    assert info.todicts() == [module.todict() for module in info.modules]

    for module in info:
        module_dict = module.todict()
        assert "user_api" in module_dict
        assert "internal_api" in module_dict
        assert "prefix" in module_dict
        assert "filepath" in module_dict
        assert "version" in module_dict
        assert "num_threads" in module_dict

        if module.internal_api == "mkl":
            assert "threading_layer" in module_dict

@pytest.mark.parametrize("prefix", _ALL_PREFIXES)
@pytest.mark.parametrize("limit", [1, 3])
def test_threadpool_limits_by_prefix(prefix, limit):
    # Check that the maximum number of threads can be set by prefix
    original_infos = _threadpool_info()

    modules_matching_prefix = original_infos.get_modules("prefix", prefix)
    if not modules_matching_prefix:
        pytest.skip("Requires {} runtime".format(prefix))

    with threadpool_limits(limits={prefix: limit}):
        for module in modules_matching_prefix:
            if is_old_openblas(module):
                continue
            # threadpool_limits only sets an upper bound on the number of
            # threads.
            assert 0 < module.get_num_threads() <= limit
    assert _threadpool_info() == original_infos

@pytest.mark.parametrize("user_api", (None, "blas", "openmp"))
@pytest.mark.parametrize("limit", [1, 3])
def test_set_threadpool_limits_by_api(user_api, limit):
    # Check that the maximum number of threads can be set by user_api
    original_infos = _threadpool_info()

    modules_matching_api = original_infos.get_modules("user_api", user_api)
    if not modules_matching_api:
        user_apis = _ALL_USER_APIS if user_api is None else [user_api]
        pytest.skip("Requires a library which api is in {}".format(user_apis))

    with threadpool_limits(limits=limit, user_api=user_api):
        for module in modules_matching_api:
            if is_old_openblas(module):
                continue
            # threadpool_limits only sets an upper bound on the number of
            # threads.
            assert 0 < module.get_num_threads() <= limit

    assert _threadpool_info() == original_infos

def test_threadpool_limits_function_with_side_effect():
    # Check that threadpool_limits can be used as a function with
    # side effects instead of a context manager.
    original_infos = _threadpool_info()

    threadpool_limits(limits=1)
    try:
        for module in _threadpool_info():
            if is_old_openblas(module):
                continue
            assert module.num_threads == 1
    finally:
        # Restore the original limits so that this test does not have any
        # side-effect.
        threadpool_limits(limits=original_infos)

    assert _threadpool_info() == original_infos

def test_set_threadpool_limits_no_limit():
    # Check that limits=None does nothing.
    original_infos = _threadpool_info()
    with threadpool_limits(limits=None):
        assert _threadpool_info() == original_infos

    assert _threadpool_info() == original_infos

def test_threadpool_limits_manual_unregister():
    # Check that threadpool_limits can be used as an object which holds the
    # original state of the threadpools and that can be restored thanks to the
    # dedicated unregister method
    original_infos = _threadpool_info()

    limits = threadpool_limits(limits=1)
    try:
        for module in _threadpool_info():
            if is_old_openblas(module):
                continue
            assert module.num_threads == 1
    finally:
        # Restore the original limits so that this test does not have any
        # side-effect.
        limits.unregister()

    assert _threadpool_info() == original_infos

def test_threadpool_limits_bad_input():
    # Check that appropriate errors are raised for invalid arguments
    match = re.escape("user_api must be either in {} or None."
                      .format(_ALL_USER_APIS))
    with pytest.raises(ValueError, match=match):
        threadpool_limits(limits=1, user_api="wrong")

    with pytest.raises(TypeError,
                       match="limits must either be an int, a list or a dict"):
        threadpool_limits(limits=(1, 2, 3))

@pytest.mark.skipif(not cython_extensions_compiled,
                    reason='Requires cython extensions to be compiled')
@pytest.mark.parametrize('num_threads', [1, 2, 4])
def test_openmp_limit_num_threads(num_threads):
    # checks that OpenMP effectively uses the number of threads requested by
    # the context manager
    from ._openmp_test_helper import check_openmp_num_threads

    old_num_threads = check_openmp_num_threads(100)

    with threadpool_limits(limits=num_threads):
        assert check_openmp_num_threads(100) in (num_threads, old_num_threads)
    assert check_openmp_num_threads(100) == old_num_threads

@pytest.mark.skipif(not cython_extensions_compiled,
                    reason='Requires cython extensions to be compiled')
@pytest.mark.parametrize('nthreads_outer', [None, 1, 2, 4])
def test_openmp_nesting(nthreads_outer):
    # checks that OpenMP effectively uses the number of threads requested by
    # the context manager when nested in an outer OpenMP loop.
    from ._openmp_test_helper import check_nested_openmp_loops
    from ._openmp_test_helper import get_inner_compiler
    from ._openmp_test_helper import get_outer_compiler

    inner_cc = get_inner_compiler()
    outer_cc = get_outer_compiler()

    outer_num_threads, inner_num_threads = check_nested_openmp_loops(10)

    original_infos = _threadpool_info()
    openmp_infos = original_infos.get_modules("user_api", "openmp")

    if "gcc" in (inner_cc, outer_cc):
        assert original_infos.get_modules("prefix", "libgomp")

    if "clang" in (inner_cc, outer_cc):
        assert original_infos.get_modules("prefix", "libomp")

    if inner_cc == outer_cc:
        # The openmp runtime should be shared by default, meaning that
        # the inner loop should automatically be run serially by the OpenMP
        # runtime.
        assert inner_num_threads == 1
    else:
        # There should be at least 2 OpenMP runtime detected.
        assert len(openmp_infos) >= 2

    with threadpool_limits(limits=1) as threadpoolctx:
        max_threads = threadpoolctx.get_original_num_threads()["openmp"]
        nthreads = effective_num_threads(nthreads_outer, max_threads)

        # Ask outer loop to run on nthreads threads and inner loop run on 1
        # thread
        outer_num_threads, inner_num_threads = \
            check_nested_openmp_loops(10, nthreads)

    # The state of the original state of all threadpools should have been
    # restored.
    assert _threadpool_info() == original_infos

    # The number of threads available in the outer loop should not have been
    # decreased:
    assert outer_num_threads == nthreads

    # The number of threads available in the inner loop should have been
    # set to 1 so avoid oversubscription and preserve performance:
    if inner_cc != outer_cc:
        if inner_num_threads != 1:
            # XXX: this does not always work when nesting independent openmp
            # implementations. See: https://github.com/jeremiedbb/Nested_OpenMP
            pytest.xfail("Inner OpenMP num threads was %d instead of 1"
                         % inner_num_threads)
    assert inner_num_threads == 1

def test_shipped_openblas():
    # checks that OpenBLAS effectively uses the number of threads requested by
    # the context manager
    original_info = _threadpool_info()

    openblas_modules = original_info.get_modules("internal_api", "openblas")

    with threadpool_limits(1):
        for module in openblas_modules:
            assert module.get_num_threads() == 1

    assert original_info == _threadpool_info()

@pytest.mark.skipif(len(libopenblas_paths) < 2,
                    reason="need at least 2 shipped openblas library")
def test_multiple_shipped_openblas():
    # This redundant test is meant to make it easier to see if the system
    # has 2 or more active openblas runtimes available just be reading the
    # pytest report (whether or not this test has been skipped).
    test_shipped_openblas()

@pytest.mark.skipif(scipy is None, reason="requires scipy")
@pytest.mark.parametrize("nthreads_outer", [None, 1, 2, 4])
def test_nested_prange_blas(nthreads_outer):
    # Check that the BLAS linked to scipy effectively uses the number of
    # threads requested by the context manager when nested in an outer OpenMP
    # loop.
    import numpy as np
    from ._openmp_test_helper import check_nested_prange_blas

    original_info = _threadpool_info()

    blas_info = original_info.get_modules("user_api", "blas")
    blis_info = original_info.get_modules("internal_api", "blis")

    # skip if the BLAS used by numpy is an old openblas. OpenBLAS 0.3.3 and
    # older are known to cause an unrecoverable deadlock at process shutdown
    # time (after pytest has exited).
    # numpy can be linked to BLIS for CBLAS and OpenBLAS for LAPACK. In that
    # case this test will run BLIS gemm so no need to skip.
    if not blis_info and any(is_old_openblas(module) for module in blas_info):
        pytest.skip("Old OpenBLAS: skipping test to avoid deadlock")

    A = np.ones((1000, 10))
    B = np.ones((100, 10))

    with threadpool_limits(limits=1) as threadpoolctx:
        max_threads = threadpoolctx.get_original_num_threads()["openmp"]
        nthreads = effective_num_threads(nthreads_outer, max_threads)

        result = check_nested_prange_blas(A, B, nthreads)
        C, prange_num_threads, threadpool_infos = result

    assert np.allclose(C, np.dot(A, B.T))
    assert prange_num_threads == nthreads

    nested_blas_info = threadpool_infos.get_modules("user_api", "blas")
    assert len(nested_blas_info) == len(blas_info)
    for module in nested_blas_info:
        assert module.num_threads == 1

    assert original_info == _threadpool_info()

@pytest.mark.parametrize("limit", [1, None])
def test_get_original_num_threads(limit):
    # Tests the method get_original_num_threads of the context manager
    with threadpool_limits(limits=2, user_api="blas") as ctl:
        # set different blas num threads to start with (when multiple openblas)
        if ctl._original_info:
            ctl._original_info.modules[0].set_num_threads(1)

        original_infos = _threadpool_info()
        with threadpool_limits(limits=limit, user_api="blas") as threadpoolctx:
            original_num_threads = threadpoolctx.get_original_num_threads()
            print(original_num_threads)

            assert "openmp" not in original_num_threads

            blas_infos = original_infos.get_modules("user_api", "blas")
            if blas_infos:
                expected = min(module.num_threads for module in blas_infos)
                assert original_num_threads["blas"] == expected
            else:
                assert original_num_threads["blas"] is None

            if len(libopenblas_paths) >= 2:
                with pytest.warns(None, match="Multiple value possible"):
                    threadpoolctx.get_original_num_threads()

def test_mkl_threading_layer():
    # Check that threadpool_info correctly recovers the threading layer used
    # by mkl
    mkl_info = _threadpool_info().get_modules("internal_api", "mkl")
    expected_layer = os.getenv("MKL_THREADING_LAYER")

    if not (mkl_info and expected_layer):
        pytest.skip("requires MKL and the environment variable "
                    "MKL_THREADING_LAYER set")

    actual_layer = mkl_info.modules[0].threading_layer
    assert actual_layer == expected_layer.lower()
<|endoftext|>


<commit_before># Create your views here.
from django.shortcuts import render_to_response, redirect
from django.core import serializers
from django.http import HttpResponse, HttpResponseServerError
from django.utils import simplejson
#from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_exempt
from django.template import RequestContext
from django.core.urlresolvers import resolve

import inspect
from django import forms
import os
my_module = None

def _load(module_name):
    #global my_module
    #if not my_module:
    my_module = __import__(module_name, None, None, [''])
    return my_module

def _get_function_information(module_name, function_name,):
    m = _load(module_name)
    # Get every member of this module (be it variable, function, whatever)
    members = {}
    for k,v in inspect.getmembers(m):
        members[k] = v
    my_function = members[function_name]
    docstring = inspect.getdoc(my_function)

    # Generate fields which resemble our functions default arguments
    argspec = inspect.getargspec( os.chdir )
    args,varargs,varkw,defaults = argspec
    if defaults is None:
        defaults = []
    else:
        defaults = list(defaults)
    print args
    print varargs
    print varkw
    print defaults
    return
    for k,v in function_paramaters.items():
        self.fields[k] = forms.CharField( label=k, initial=v)

    while len(defaults) > 0:
        value = defaults.pop()
        field = args.pop()
        self.fields[field].initial = value

@csrf_exempt
def handle_request(request, module_name, attribute, format):
    m = _load(module_name)
    # TODO: Only allow function calls if method == POST
    members = {}
    for k,v in inspect.getmembers(m):
        members[k] = v
    item = members[attribute]
    docstring = inspect.getdoc(item)
    if request.method == 'GET':
        if format == 'help':
            result = inspect.getdoc(item)
        elif not inspect.isfunction(item):
            result = item
        else:
            arguments = request.GET
            c = {}
            c['function_name'] = attribute
            c['form'] = CallFunctionForm(function=item, initial=request.GET)
            c['docstring'] = docstring
            if not request.GET.items():
                return render_to_response('function_form.html', c, context_instance = RequestContext(request))
            # Handle get parameters
	    arguments = {}
            for k, v in request.GET.items():
                # TODO: Is it safe to turn all digits to int ?
                #if str(v).isdigit(): v = int(float(v))
                arguments[k.encode('utf-8')] = v.encode('utf-8')
            result = item( **arguments )
    elif request.method == 'POST':
        item = members[attribute]
        if not inspect.isfunction(item):
            result = item
        else:
            arguments = {} #request.POST.items()
            for k, v in request.POST.items():
                arguments[k.encode('utf-8')] = v.encode('utf-8')
            result = item( **arguments )
    else:
        raise BaseException("Unsupported operation: %s" % (request.method))
    # Everything below is just about formatting the results
    if format == 'json':
        result = simplejson.dumps( result, ensure_ascii=False, sort_keys=True,skipkeys=True, indent=4 )
        mimetype='application/javascript'
    elif format == 'xml':
            # TODO: For some reason Ubuntu does not have this module. Where is it? Should we use lxml instead ?
            import xml.marshal.generic
            result = xml.marshal.generic.dumps(result)
            mimetype='application/xml'
    elif format == 'txt':
        result = str(result)
        mimetype='text/plain'
    else:
        raise BaseException("Unsupported format: '%s'. Valid formats: json xml txt" % format)
    return HttpResponse(result, mimetype=mimetype)

def index( request, module_name ):
    m = _load(module_name)
    gets,puts = [],[]
    blacklist = ( 'argv', 'environ', 'exit', 'path', 'putenv', 'getenv', )
    for k,v in inspect.getmembers(m):
        if k.startswith('_'): continue
        if k in blacklist: continue
        if inspect.ismodule(v):
            continue
        elif inspect.isfunction(v):
            puts.append( k )
        else:
            gets.append( k )
    c = {}
    c['module_name'] = module_name
    c['gets'] = gets
    c['puts'] = puts
    c['module_documenation'] = inspect.getdoc(m)
    return render_to_response('index.html', c, context_instance = RequestContext(request))

def javascript(request, module_name):
    """ Create a javascript library that will wrap around module_name module """
    m = _load(module_name)
    variables,functions = [],[]
    blacklist = ( 'argv', 'environ', 'exit', 'path', 'putenv', 'getenv', )
    members = {}
    for k,v in inspect.getmembers(m):
        if k.startswith('_'):
            continue
        if k in blacklist:
            continue
        if inspect.ismodule(v):
            continue
        if inspect.isfunction(v):
            functions.append( k )
            members[k] = v
        else:
            variables.append( k )
    c = {}
    c['module_name'] = module_name
    c['gets'] = variables
    c['puts'] = functions
    c['module_documenation'] = inspect.getdoc(m)
    current_url =  request.get_full_path()
    baseurl = current_url.replace('.js','')
    # Find every function, prepare what is needed so template can
    # print arguments with their default values.
    for i in functions:
        argspec = inspect.getargspec( members[i] )
        args,varargs,varkw,defaults = argspec
        docstring = inspect.getdoc(i)
        if defaults is None:
            defaults = []
        else:
            defaults = list(defaults)
            # Lets create argstring, for the javascript needed
        tmp = [] + args
        argstring = []
        for num,default in enumerate(reversed(defaults)):
            argstring.append('%s=%s' % (tmp.pop(), default) )
        argstring.reverse()
        argstring = tmp + argstring
        argstring = ','.join(argstring)

        members[i] = {}
        members[i]['args'] = args
        members[i]['argstring'] = ','.join(args)
        members[i]['varargs'] = varargs
        members[i]['varkw'] = varkw
        members[i]['defaults'] = defaults
        members[i]['docstring'] = docstring
        members[i]['url'] = baseurl + "/json/" + i
        args,varargs,varkw,defaults = argspec
    c['functions'] = members
    #return c
    #_get_function_information('os','curdir')
    return render_to_response('javascript.html', c, mimetype="text/javascript", context_instance = RequestContext(request))

class CallFunctionForm(forms.Form):
    def __init__(self, function, *args, **kwargs):
        super(forms.Form,self).__init__( *args, **kwargs)
        function_paramaters = {} # We will create a field for every function_paramater
        # If any paramaters were past via querystring, lets generate fields for them
        if kwargs.has_key('initial'):
            for k,v in kwargs['initial'].items():
                function_paramaters[k] = v
        # Generate fields which resemble our functions default arguments
        argspec = inspect.getargspec( function )
        args,varargs,varkw,defaults = argspec
        if defaults is None:
            defaults = []
        else:
            defaults = list(defaults)
        for i in args:
            self.fields[i] = forms.CharField( label=i )
        for k,v in function_paramaters.items():
            self.fields[k] = forms.CharField( label=k, initial=v)
        while len(defaults) > 0:
            value = defaults.pop()
            field = args.pop()
            self.fields[field].initial = value

<commit_msg>rest/views: cleanup extra print statements<commit_after># Create your views here.
from django.shortcuts import render_to_response, redirect
from django.core import serializers
from django.http import HttpResponse, HttpResponseServerError
from django.utils import simplejson
#from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_exempt
from django.template import RequestContext
from django.core.urlresolvers import resolve

import inspect
from django import forms
import os
my_module = None

def _load(module_name):
    #global my_module
    #if not my_module:
    my_module = __import__(module_name, None, None, [''])
    return my_module

def _get_function_information(module_name, function_name,):
    m = _load(module_name)
    # Get every member of this module (be it variable, function, whatever)
    members = {}
    for k,v in inspect.getmembers(m):
        members[k] = v
    my_function = members[function_name]
    docstring = inspect.getdoc(my_function)

    # Generate fields which resemble our functions default arguments
    argspec = inspect.getargspec( os.chdir )
    args,varargs,varkw,defaults = argspec
    if defaults is None:
        defaults = []
    else:
        defaults = list(defaults)
    for k,v in function_paramaters.items():
        self.fields[k] = forms.CharField( label=k, initial=v)

    while len(defaults) > 0:
        value = defaults.pop()
        field = args.pop()
        self.fields[field].initial = value

@csrf_exempt
def handle_request(request, module_name, attribute, format):
    m = _load(module_name)
    # TODO: Only allow function calls if method == POST
    members = {}
    for k,v in inspect.getmembers(m):
        members[k] = v
    item = members[attribute]
    docstring = inspect.getdoc(item)
    if request.method == 'GET':
        if format == 'help':
            result = inspect.getdoc(item)
        elif not inspect.isfunction(item):
            result = item
        else:
            arguments = request.GET
            c = {}
            c['function_name'] = attribute
            c['form'] = CallFunctionForm(function=item, initial=request.GET)
            c['docstring'] = docstring
            if not request.GET.items():
                return render_to_response('function_form.html', c, context_instance = RequestContext(request))
            # Handle get parameters
	    arguments = {}
            for k, v in request.GET.items():
                # TODO: Is it safe to turn all digits to int ?
                #if str(v).isdigit(): v = int(float(v))
                arguments[k.encode('utf-8')] = v.encode('utf-8')
            result = item( **arguments )
    elif request.method == 'POST':
        item = members[attribute]
        if not inspect.isfunction(item):
            result = item
        else:
            arguments = {} #request.POST.items()
            for k, v in request.POST.items():
                arguments[k.encode('utf-8')] = v.encode('utf-8')
            result = item( **arguments )
    else:
        raise BaseException("Unsupported operation: %s" % (request.method))
    # Everything below is just about formatting the results
    if format == 'json':
        result = simplejson.dumps( result, ensure_ascii=False, sort_keys=True,skipkeys=True, indent=4 )
        mimetype='application/javascript'
    elif format == 'xml':
            # TODO: For some reason Ubuntu does not have this module. Where is it? Should we use lxml instead ?
            import xml.marshal.generic
            result = xml.marshal.generic.dumps(result)
            mimetype='application/xml'
    elif format == 'txt':
        result = str(result)
        mimetype='text/plain'
    else:
        raise BaseException("Unsupported format: '%s'. Valid formats: json xml txt" % format)
    return HttpResponse(result, mimetype=mimetype)

def index( request, module_name ):
    m = _load(module_name)
    gets,puts = [],[]
    blacklist = ( 'argv', 'environ', 'exit', 'path', 'putenv', 'getenv', )
    for k,v in inspect.getmembers(m):
        if k.startswith('_'): continue
        if k in blacklist: continue
        if inspect.ismodule(v):
            continue
        elif inspect.isfunction(v):
            puts.append( k )
        else:
            gets.append( k )
    c = {}
    c['module_name'] = module_name
    c['gets'] = gets
    c['puts'] = puts
    c['module_documenation'] = inspect.getdoc(m)
    return render_to_response('index.html', c, context_instance = RequestContext(request))

def javascript(request, module_name):
    """ Create a javascript library that will wrap around module_name module """
    m = _load(module_name)
    variables,functions = [],[]
    blacklist = ( 'argv', 'environ', 'exit', 'path', 'putenv', 'getenv', )
    members = {}
    for k,v in inspect.getmembers(m):
        if k.startswith('_'):
            continue
        if k in blacklist:
            continue
        if inspect.ismodule(v):
            continue
        if inspect.isfunction(v):
            functions.append( k )
            members[k] = v
        else:
            variables.append( k )
    c = {}
    c['module_name'] = module_name
    c['gets'] = variables
    c['puts'] = functions
    c['module_documenation'] = inspect.getdoc(m)
    current_url =  request.get_full_path()
    baseurl = current_url.replace('.js','')
    # Find every function, prepare what is needed so template can
    for i in functions:
        argspec = inspect.getargspec( members[i] )
        args,varargs,varkw,defaults = argspec
        docstring = inspect.getdoc(i)
        if defaults is None:
            defaults = []
        else:
            defaults = list(defaults)
            # Lets create argstring, for the javascript needed
        tmp = [] + args
        argstring = []
        for num,default in enumerate(reversed(defaults)):
            argstring.append('%s=%s' % (tmp.pop(), default) )
        argstring.reverse()
        argstring = tmp + argstring
        argstring = ','.join(argstring)

        members[i] = {}
        members[i]['args'] = args
        members[i]['argstring'] = ','.join(args)
        members[i]['varargs'] = varargs
        members[i]['varkw'] = varkw
        members[i]['defaults'] = defaults
        members[i]['docstring'] = docstring
        members[i]['url'] = baseurl + "/json/" + i
        args,varargs,varkw,defaults = argspec
    c['functions'] = members

    return render_to_response('javascript.html', c, mimetype="text/javascript", context_instance = RequestContext(request))

class CallFunctionForm(forms.Form):
    def __init__(self, function, *args, **kwargs):
        super(forms.Form,self).__init__( *args, **kwargs)
        function_paramaters = {} # We will create a field for every function_paramater
        # If any paramaters were past via querystring, lets generate fields for them
        if kwargs.has_key('initial'):
            for k,v in kwargs['initial'].items():
                function_paramaters[k] = v
        # Generate fields which resemble our functions default arguments
        argspec = inspect.getargspec( function )
        args,varargs,varkw,defaults = argspec
        if defaults is None:
            defaults = []
        else:
            defaults = list(defaults)
        for i in args:
            self.fields[i] = forms.CharField( label=i )
        for k,v in function_paramaters.items():
            self.fields[k] = forms.CharField( label=k, initial=v)
        while len(defaults) > 0:
            value = defaults.pop()
            field = args.pop()
            self.fields[field].initial = value

<|endoftext|>