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

#############################################################################
#
#    Copyright (c) 2007 Martin Reisenhofer <martin.reisenhofer@funkring.net>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields,osv
from openerp.addons.at_base import util
from dateutil.relativedelta import relativedelta

class stock_picking(osv.osv):

    def _location_partner_id(self, cr, uid, ids, field_name, args, context=None):
        res = dict.fromkeys(ids)
        for picking in self.browse(cr, uid, ids, context):
            partner = None
            purchase = picking.purchase_id

            #partner from purchase
            if purchase:
                #purchase destination
                partner = purchase.dest_address_id
                if not partner:
                    # or location destination
                    location = purchase.location_id
                    if location:
                        partner = location.partner_id
                        # take partner from parent locations
                        while not partner:
                            location = location.partner_id
                            if not location:
                                break
                            partner = location.partner_id

            #take default partner from company
            if not partner:
                partner =  picking.company_id.partner_id

            #set partner if found
            if partner:
                res[picking.id] = partner.id
        return res

    _inherit = "stock.picking"
    _columns = {
        "location_partner_id" : fields.function(_location_partner_id, string="Location Partner", type="many2one", obj="res.partner", store=False)
    }

class stock_move(osv.osv):

    def _update_expected_date(self,cr,uid,moves,context=None):
        picking_obj = self.pool.get("stock.picking")
        for move in moves:
            orig_move = move
            move_dest = move.move_dest_id
            delay = orig_move.location_id and orig_move.location_id.chained_delay or 0
            while move_dest:
                orig_expected_date = self.read(cr, uid, orig_move.id, ["date_expected"], context)["date_expected"]
                dest_expected_date = self.read(cr, uid, move_dest.id, ["date_expected"], context)["date_expected"]
                expected_date = util.mergeTimeStr(util.strToTime(orig_expected_date)+relativedelta(days=delay),dest_expected_date)
                #
                if dest_expected_date != expected_date:
                    vals = { "date_expected" : expected_date }
                    # write over picking id
                    # to update stored functions
                    if move_dest.picking_id:
                        picking_obj.write(cr, uid, move_dest.picking_id.id, { "move_lines" : [(1,move_dest.id,vals)] }, context)
                    else:
                        self.write(cr, uid, [move_dest.id], vals, context)

                #
                orig_move = move_dest
                move_dest = orig_move.move_dest_id
                delay = orig_move.location_id and orig_move.location_id.chained_delay or 0

    def create_chained_picking(self, cr, uid, moves, context=None):
        new_moves = super(stock_move, self).create_chained_picking(cr, uid, moves, context=context)
        self._update_expected_date(cr, uid, moves, context)
        return new_moves

    _inherit = "stock.move"
<commit_msg>[FIX] removed unused _location_partner_id<commit_after># -*- coding: utf-8 -*-

#############################################################################
#
#    Copyright (c) 2007 Martin Reisenhofer <martin.reisenhofer@funkring.net>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields,osv
from openerp.addons.at_base import util
from dateutil.relativedelta import relativedelta

class stock_move(osv.osv):

    def _update_expected_date(self,cr,uid,moves,context=None):
        picking_obj = self.pool.get("stock.picking")
        for move in moves:
            orig_move = move
            move_dest = move.move_dest_id
            delay = orig_move.location_id and orig_move.location_id.chained_delay or 0
            while move_dest:
                orig_expected_date = self.read(cr, uid, orig_move.id, ["date_expected"], context)["date_expected"]
                dest_expected_date = self.read(cr, uid, move_dest.id, ["date_expected"], context)["date_expected"]
                expected_date = util.mergeTimeStr(util.strToTime(orig_expected_date)+relativedelta(days=delay),dest_expected_date)
                #
                if dest_expected_date != expected_date:
                    vals = { "date_expected" : expected_date }
                    # write over picking id
                    # to update stored functions
                    if move_dest.picking_id:
                        picking_obj.write(cr, uid, move_dest.picking_id.id, { "move_lines" : [(1,move_dest.id,vals)] }, context)
                    else:
                        self.write(cr, uid, [move_dest.id], vals, context)

                #
                orig_move = move_dest
                move_dest = orig_move.move_dest_id
                delay = orig_move.location_id and orig_move.location_id.chained_delay or 0

    def create_chained_picking(self, cr, uid, moves, context=None):
        new_moves = super(stock_move, self).create_chained_picking(cr, uid, moves, context=context)
        self._update_expected_date(cr, uid, moves, context)
        return new_moves

    _inherit = "stock.move"
<|endoftext|>


<commit_before>import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0, 2, 0.01)
s = np.sin(2*np.pi*t)
c = np.cos(2*np.pi*t)

plt.plot(t, s)
plt.plot(t, c)

plt.show()<commit_msg>Noch mehr Atom-Spielereien<commit_after>import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0, 2, 0.01)
s = np.sin(2*np.pi*t)
c = np.cos(2*np.pi*t)

plt.plot(t, s)
plt.plot(t, c)

plt.show()
<|endoftext|>


<commit_before>import unittest

import numpy as np
import numpy.testing as npt
from astroNN.apogee import gap_delete, apogee_default_dr, bitmask_decompositor, chips_split, bitmask_boolean, \
    apogee_continuum, aspcap_mask
from astroNN.apogee.apogee_shared import apogeeid_digit

class ApogeeToolsCase(unittest.TestCase):
    def test_apogee_tools(self):
        # Example data
        raw_spectra = np.ones((10, 8575))
        raw_spectrum = np.ones(8575)
        wrong_spectrum = np.ones(1024)

        gap_deleted = gap_delete(raw_spectra)
        self.assertEqual(gap_deleted.shape == (10, 7514), True)
        gap_deleted = gap_delete(raw_spectrum)
        self.assertEqual(gap_deleted.shape == (1, 7514), True)
        gap_deleted = gap_delete(raw_spectra, dr=12)
        self.assertEqual(gap_deleted.shape == (10, 7214), True)
        gap_deleted = gap_delete(raw_spectrum, dr=12)
        self.assertEqual(gap_deleted.shape == (1, 7214), True)
        self.assertRaises(EnvironmentError, gap_delete, wrong_spectrum)

        # check gaia default dr
        dr = apogee_default_dr()
        self.assertEqual(dr, 14)
        dr = apogee_default_dr(dr=3)
        self.assertEqual(dr, 3)

        # bitmask
        self.assertEqual(bitmask_decompositor(0), None)
        npt.assert_array_equal(bitmask_decompositor(1), [0])
        npt.assert_array_equal(bitmask_decompositor(3), [0, 1])
        npt.assert_array_equal(bitmask_boolean([0, 1, 2], [1]), [[True, True, False]])
        self.assertRaises(ValueError, bitmask_decompositor, -1)

        # chips_split
        blue, green, red = chips_split(raw_spectra)
        self.assertEqual(np.concatenate((blue, green, red), axis=1).shape == (10, 7514), True)
        blue, green, red = chips_split(raw_spectrum)
        self.assertEqual(np.concatenate((blue, green, red), axis=1).shape == (1, 7514), True)
        self.assertRaises(ValueError, chips_split, raw_spectra, dr=10)

    def test_apogee_continuum(self):
        raw_spectra = np.ones((10, 8575)) * 2
        raw_spectra_err = np.zeros((10, 8575))
        # continuum
        cont_spectra, cont_spectra_arr = apogee_continuum(raw_spectra, raw_spectra_err)
        self.assertAlmostEqual(np.mean(cont_spectra), 1.)

    def test_apogee_digit_extractor(self):
        # Test apogeeid digit extractor
        # just to make no error
        apogeeid_digit(["2M00380508+5608579", "2M00380508+5608579"])
        apogeeid_digit(np.array(["2M00380508+5608579", "2M00380508+5608579"]))

        # check accuracy
        self.assertEqual(apogeeid_digit("2M00380508+5608579"), '2003805085608579')
        npt.assert_array_equal(apogeeid_digit(np.array(["2M00380508+5608579", "2M00380508+5608579"])),
                               ['2003805085608579', '2003805085608579'])

    def test_aspcap_mask(self):
        self.assertEqual(np.all(aspcap_mask('C1') == aspcap_mask('ci')), True)
        self.assertEqual(np.all(aspcap_mask('TIII') == aspcap_mask('ti2')), True)
        # assert for example dr=1 is not supported
        self.assertRaises(ValueError, aspcap_mask, 'al', 1)
        # Make sure if element not found, the case is nicely handled
        self.assertEqual(aspcap_mask('abc'), None)

if __name__ == '__main__':
    unittest.main()
<commit_msg>test aspcap_windows_url_correction<commit_after>import unittest

import numpy as np
import numpy.testing as npt
from astroNN.apogee import gap_delete, apogee_default_dr, bitmask_decompositor, chips_split, bitmask_boolean, \
    apogee_continuum, aspcap_mask
from astroNN.apogee.apogee_shared import apogeeid_digit
from astroNN.apogee.plotting import aspcap_windows_url_correction

class ApogeeToolsCase(unittest.TestCase):
    def test_apogee_tools(self):
        # Example data
        raw_spectra = np.ones((10, 8575))
        raw_spectrum = np.ones(8575)
        wrong_spectrum = np.ones(1024)

        gap_deleted = gap_delete(raw_spectra)
        self.assertEqual(gap_deleted.shape == (10, 7514), True)
        gap_deleted = gap_delete(raw_spectrum)
        self.assertEqual(gap_deleted.shape == (1, 7514), True)
        gap_deleted = gap_delete(raw_spectra, dr=12)
        self.assertEqual(gap_deleted.shape == (10, 7214), True)
        gap_deleted = gap_delete(raw_spectrum, dr=12)
        self.assertEqual(gap_deleted.shape == (1, 7214), True)
        self.assertRaises(EnvironmentError, gap_delete, wrong_spectrum)

        # check gaia default dr
        dr = apogee_default_dr()
        self.assertEqual(dr, 14)
        dr = apogee_default_dr(dr=3)
        self.assertEqual(dr, 3)

        # bitmask
        self.assertEqual(bitmask_decompositor(0), None)
        npt.assert_array_equal(bitmask_decompositor(1), [0])
        npt.assert_array_equal(bitmask_decompositor(3), [0, 1])
        npt.assert_array_equal(bitmask_boolean([0, 1, 2], [1]), [[True, True, False]])
        self.assertRaises(ValueError, bitmask_decompositor, -1)

        # chips_split
        blue, green, red = chips_split(raw_spectra)
        self.assertEqual(np.concatenate((blue, green, red), axis=1).shape == (10, 7514), True)
        blue, green, red = chips_split(raw_spectrum)
        self.assertEqual(np.concatenate((blue, green, red), axis=1).shape == (1, 7514), True)
        self.assertRaises(ValueError, chips_split, raw_spectra, dr=10)

    def test_apogee_continuum(self):
        raw_spectra = np.ones((10, 8575)) * 2
        raw_spectra_err = np.zeros((10, 8575))
        # continuum
        cont_spectra, cont_spectra_arr = apogee_continuum(raw_spectra, raw_spectra_err)
        self.assertAlmostEqual(np.mean(cont_spectra), 1.)

    def test_apogee_digit_extractor(self):
        # Test apogeeid digit extractor
        # just to make no error
        apogeeid_digit(["2M00380508+5608579", "2M00380508+5608579"])
        apogeeid_digit(np.array(["2M00380508+5608579", "2M00380508+5608579"]))

        # check accuracy
        self.assertEqual(apogeeid_digit("2M00380508+5608579"), '2003805085608579')
        npt.assert_array_equal(apogeeid_digit(np.array(["2M00380508+5608579", "2M00380508+5608579"])),
                               ['2003805085608579', '2003805085608579'])

    def test_aspcap_mask(self):
        self.assertEqual(np.all(aspcap_mask('C1') == aspcap_mask('ci')), True)
        self.assertEqual(np.all(aspcap_mask('TIII') == aspcap_mask('ti2')), True)
        # assert for example dr=1 is not supported
        self.assertRaises(ValueError, aspcap_mask, 'al', 1)
        # Make sure if element not found, the case is nicely handled
        self.assertEqual(aspcap_mask('abc'), None)

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


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

import pandas as pd

from astropy.time import Time

from piaa.observation import Observation

def add_result(result):
    print(result.get())

def show_error(e):
    print(e)

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('--image-dir', type=str, help="Image directory containing FITS files")
    parser.add_argument('--target-index', type=int, help="Target index to compute variance for")
    args = parser.parse_args()

    obs = Observation(args.image_dir)

    start = Time.now()
    print("Starting at  {}".format(start))

    # Normalize first
    print("Normalizing stamps")
    obs.create_normalized_stamps()
    print("Normalization done: {:02f} seconds".format(((Time.now() - start).sec)))

    print("Getting variance for index {}".format(args.target_index))
    print(obs.point_sources.iloc[args.target_index])
    result = obs.get_variance_for_target(args.target_index, ipython_widget=True)

    obs.point_sources['V'] = pd.Series(result)

    # Sort the values by lowest total variance
    obs.point_sources.sort_values(by=['V'], inplace=True)

    # Save values to file
    output_file = 'vary_{}.csv'.format(args.target_index)
    print("Saving to file: {}".format(output_file))
    obs.point_sources.to_csv(output_file)

    print("Processing time: {:02f} seconds".format(((Time.now() - start).sec)))
<commit_msg>Small fix to progress bar<commit_after>#!/usr/bin/env python

import pandas as pd

from astropy.time import Time

from piaa.observation import Observation

def add_result(result):
    print(result.get())

def show_error(e):
    print(e)

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('--image-dir', type=str, help="Image directory containing FITS files")
    parser.add_argument('--target-index', type=int, help="Target index to compute variance for")
    args = parser.parse_args()

    obs = Observation(args.image_dir)

    start = Time.now()
    print("Starting at  {}".format(start))

    # Normalize first
    print("Normalizing stamps")
    obs.create_normalized_stamps()
    print("Normalization done: {:02f} seconds".format(((Time.now() - start).sec)))

    print("Getting variance for index {}".format(args.target_index))
    print(obs.point_sources.iloc[args.target_index])
    result = obs.get_variance_for_target(args.target_index)

    obs.point_sources['V'] = pd.Series(result)

    # Sort the values by lowest total variance
    obs.point_sources.sort_values(by=['V'], inplace=True)

    # Save values to file
    output_file = 'vary_{}.csv'.format(args.target_index)
    print("Saving to file: {}".format(output_file))
    obs.point_sources.to_csv(output_file)

    print("Processing time: {:02f} seconds".format(((Time.now() - start).sec)))
<|endoftext|>


<commit_before>from ontobio.ontol_factory import OntologyFactory
from ontobio.assoc_factory import AssociationSetFactory
from ontobio.assocmodel import AssociationSet
from ontobio.io.gafparser import GafParser
import logging
import random

NUCLEUS = 'GO:0005634'
CYTOPLASM = 'GO:0005737'
MITO = 'GO:0005739'
MOUSE = 'NCBITaxon:10090'
TRANSCRIPTION_FACTOR = 'GO:0003700'
TRANSPORTER = 'GO:0005215'

def test_remote_go():
    """
    factory test
    """
    ofactory = OntologyFactory()
    afactory = AssociationSetFactory()
    ont = ofactory.create('go')
    aset = afactory.create(ontology=ont,
                           subject_category='gene',
                           object_category='function',
                           taxon=MOUSE)

    rs = aset.query([TRANSCRIPTION_FACTOR],[])
    print("Mouse genes annotated to TF: {} {}".format(rs, len(rs)))
    set_tf = rs

    rs = aset.query([NUCLEUS],[])
    print("Mouse genes annotated to nucleus: {} {}".format(rs, len(rs)))
    set_nucleus = rs
    assert(len(rs) > 100)

    rs = aset.query([TRANSCRIPTION_FACTOR, NUCLEUS],[])
    print("Mouse TF genes annotated to nucleus: {} {}".format(rs, len(rs)))
    assert(len(rs) > 100)
    set_nucleus_tf = rs
    assert(len(rs) < len(set_nucleus))

    rs = aset.query([NUCLEUS],[TRANSCRIPTION_FACTOR])
    print("Mouse non-TF genes annotated to nucleus: {} {}".format(rs, len(rs)))
    assert(len(rs) > 100)
    set_nucleus_non_tf = rs
    assert(len(rs) < len(set_nucleus))
    assert(len(set_nucleus_tf) + len(set_nucleus_non_tf) == len(set_nucleus))

    enr = aset.enrichment_test(subjects=set_tf, labels=True)
    print("ENRICHMENT (tf): {}".format(enr))
    [match] = [x for x in enr if x['c'] == NUCLEUS]
    print("ENRICHMENT (tf) for NUCLEUS: {}".format(match))
    assert match['p'] < 0.00001

HUMAN='NCBITaxon:9606'
PD = 'DOID:14330'

def test_remote_disease():
    """
    factory test
    """
    ofactory = OntologyFactory()
    afactory = AssociationSetFactory()
    ont = ofactory.create('doid')
    aset = afactory.create(ontology=ont,
                           subject_category='disease',
                           object_category='phenotype',
                           taxon=HUMAN)

    rs = aset.query_associations([PD])
    print("Gene Assocs to PD: {} {}".format(rs, len(rs)))

POMBASE = "tests/resources/truncated-pombase.gaf"
INTRACELLULAR='GO:0005622'
G1 = 'PomBase:SPBC902.04'
def test_gaf():
    """
    Test loading from gaf
    """
    ofactory = OntologyFactory()
    afactory = AssociationSetFactory()
    ont = ofactory.create('go')
    aset = afactory.create_from_gaf(open(POMBASE,"r"),
                                    ontology=ont)
    print(str(aset))
    genes = aset.query([INTRACELLULAR])
    for g in genes:
        print("G={} '{}'".format(g, aset.label(g)))
    assert G1 in genes

<commit_msg>adapting test to filter by relation<commit_after>from ontobio.ontol_factory import OntologyFactory
from ontobio.assoc_factory import AssociationSetFactory
from ontobio.assocmodel import AssociationSet
from ontobio.io.gafparser import GafParser
import logging
import random

NUCLEUS = 'GO:0005634'
CYTOPLASM = 'GO:0005737'
MITO = 'GO:0005739'
MOUSE = 'NCBITaxon:10090'
TRANSCRIPTION_FACTOR = 'GO:0003700'
TRANSPORTER = 'GO:0005215'
PART_OF = 'BFO:0000050'

def test_remote_go():
    """
    factory test
    """
    ofactory = OntologyFactory()
    afactory = AssociationSetFactory()
    ont = ofactory.create('go').subontology(relations=['subClassOf', PART_OF])
    aset = afactory.create(ontology=ont,
                           subject_category='gene',
                           object_category='function',
                           taxon=MOUSE)

    rs = aset.query([TRANSCRIPTION_FACTOR],[])
    print("Mouse genes annotated to TF: {} {}".format(rs, len(rs)))
    for g in rs:
        print("  Gene: {} {}".format(g,aset.label(g)))
    set_tf = rs

    rs = aset.query([NUCLEUS],[])
    print("Mouse genes annotated to nucleus: {} {}".format(rs, len(rs)))
    set_nucleus = rs
    assert(len(rs) > 100)

    rs = aset.query([TRANSCRIPTION_FACTOR, NUCLEUS],[])
    print("Mouse TF genes annotated to nucleus: {} {}".format(rs, len(rs)))
    assert(len(rs) > 100)
    set_nucleus_tf = rs
    assert(len(rs) < len(set_nucleus))

    rs = aset.query([NUCLEUS],[TRANSCRIPTION_FACTOR])
    print("Mouse non-TF genes annotated to nucleus: {} {}".format(rs, len(rs)))
    assert(len(rs) > 100)
    set_nucleus_non_tf = rs
    assert(len(rs) < len(set_nucleus))
    assert(len(set_nucleus_tf) + len(set_nucleus_non_tf) == len(set_nucleus))

    enr = aset.enrichment_test(subjects=set_tf, labels=True)
    print("ENRICHMENT (tf): {}".format(enr))
    [match] = [x for x in enr if x['c'] == NUCLEUS]
    print("ENRICHMENT (tf) for NUCLEUS: {}".format(match))
    assert match['p'] < 0.00001

HUMAN='NCBITaxon:9606'
PD = 'DOID:14330'

def test_remote_disease():
    """
    factory test
    """
    ofactory = OntologyFactory()
    afactory = AssociationSetFactory()
    ont = ofactory.create('doid')
    aset = afactory.create(ontology=ont,
                           subject_category='disease',
                           object_category='phenotype',
                           taxon=HUMAN)

    rs = aset.query_associations([PD])
    print("Gene Assocs to PD: {} {}".format(rs, len(rs)))

POMBASE = "tests/resources/truncated-pombase.gaf"
INTRACELLULAR='GO:0005622'
G1 = 'PomBase:SPBC902.04'
def test_gaf():
    """
    Test loading from gaf
    """
    ofactory = OntologyFactory()
    afactory = AssociationSetFactory()
    ont = ofactory.create('go')
    aset = afactory.create_from_gaf(open(POMBASE,"r"),
                                    ontology=ont)
    print(str(aset))
    genes = aset.query([INTRACELLULAR])
    for g in genes:
        print("G={} '{}'".format(g, aset.label(g)))
    assert G1 in genes

<|endoftext|>


<commit_before>import common
import test_data

import os
from mock import patch, ANY, MagicMock

from google.appengine.api import memcache

@patch('dtmm_utils.get_url_content', autospec=True)
@patch('build_status.BuildStatusHandler.notify_status', autospec=True)
class TestBuildStatus(common.DMSHandlerTestCase):
    def test_build_status_passing(self, notify_status, get_url_content):
        for platform, url in test_data.PLATFORM_W_URLS:
            get_url_content.return_value = {'-1': {'text': 'successful'}}
            self.testapp.get(url)

            notify_status.assert_called_with(ANY, 'passing')

    def test_build_status_failing(self, notify_status, get_url_content):
        for platform, url in test_data.PLATFORM_W_URLS:

            get_url_content.return_value = {'-1': {'text': 'failed'}}
            self.testapp.get(url)

            notify_status.assert_called_with(ANY, 'failing')

    def test_build_status_unknown_with_bad_data(self, notify_status, get_url_content):
        for platform, url in test_data.PLATFORM_W_URLS:
            get_url_content.return_value = []
            self.testapp.get(url)

            notify_status.assert_called_with(ANY, 'unknown')

    def test_build_status_unknown_with_no_status(self, notify_status, get_url_content):
        for platform, url in test_data.PLATFORM_W_URLS:
            get_url_content.return_value = {'-1': {'text': 'garbage'}}
            self.testapp.get(url)

            notify_status.assert_called_with(ANY, 'unknown')

    def test_build_status_invalid_platfrom(self, notify_status, _):
        self.testapp.get('/status/invalid.png')

        values = memcache.get_multi(test_data.PLATFORMS, 'build_status_')
        self.assertTrue(not any(values))

    def test_build_status_with_memcache(self, notify_status, _):
        memcache.set('build_status_linux', 'unknown')
        self.testapp.get('/status/linux.png')

        notify_status.assert_called_with(ANY, 'unknown')

@patch('webapp2.RequestHandler.response', autospec=True)
class TestNotifyStatus(common.DMSTestCase):
    def test_notify_status(self, response):
        path = os.path.join(os.path.dirname(__file__), '../src/results/passing.png')
        with open(path, 'rb') as fh:
            data = fh.read()

        import build_status
        handler = build_status.BuildStatusHandler(MagicMock(), MagicMock())
        handler.notify_status('passing')

        handler.response.write.assert_called_with(data)

if __name__ == '__main__':
    common.main()
<commit_msg>We don't need to access the response object like this, only patch it<commit_after>import common
import test_data

import os
from mock import patch, ANY, MagicMock

from google.appengine.api import memcache

@patch('dtmm_utils.get_url_content', autospec=True)
@patch('build_status.BuildStatusHandler.notify_status', autospec=True)
class TestBuildStatus(common.DMSHandlerTestCase):
    def test_build_status_passing(self, notify_status, get_url_content):
        for platform, url in test_data.PLATFORM_W_URLS:
            get_url_content.return_value = {'-1': {'text': 'successful'}}
            self.testapp.get(url)

            notify_status.assert_called_with(ANY, 'passing')

    def test_build_status_failing(self, notify_status, get_url_content):
        for platform, url in test_data.PLATFORM_W_URLS:

            get_url_content.return_value = {'-1': {'text': 'failed'}}
            self.testapp.get(url)

            notify_status.assert_called_with(ANY, 'failing')

    def test_build_status_unknown_with_bad_data(self, notify_status, get_url_content):
        for platform, url in test_data.PLATFORM_W_URLS:
            get_url_content.return_value = []
            self.testapp.get(url)

            notify_status.assert_called_with(ANY, 'unknown')

    def test_build_status_unknown_with_no_status(self, notify_status, get_url_content):
        for platform, url in test_data.PLATFORM_W_URLS:
            get_url_content.return_value = {'-1': {'text': 'garbage'}}
            self.testapp.get(url)

            notify_status.assert_called_with(ANY, 'unknown')

    def test_build_status_invalid_platfrom(self, notify_status, _):
        self.testapp.get('/status/invalid.png')

        values = memcache.get_multi(test_data.PLATFORMS, 'build_status_')
        self.assertTrue(not any(values))

    def test_build_status_with_memcache(self, notify_status, _):
        memcache.set('build_status_linux', 'unknown')
        self.testapp.get('/status/linux.png')

        notify_status.assert_called_with(ANY, 'unknown')

@patch('webapp2.RequestHandler.response', autospec=True)
class TestNotifyStatus(common.DMSTestCase):
    def test_notify_status(self, _):
        path = os.path.join(os.path.dirname(__file__), '../src/results/passing.png')
        with open(path, 'rb') as fh:
            data = fh.read()

        import build_status
        handler = build_status.BuildStatusHandler(MagicMock(), MagicMock())
        handler.notify_status('passing')

        handler.response.write.assert_called_with(data)

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


<commit_before>def test_import():
    import choicemodels

def test_simple_estimation():
    import choicemodels
    import numpy as np
    import pandas as pd
    from collections import OrderedDict
    endog = np.random.randint(2, size=50)
    exog = np.random.rand(50, 5)
    m = choicemodels.Logit(endog, exog)
    results = m.fit()
    results.summary()

def test_distance_matrix():
    import pandas as pd, numpy as np
    from choicemodels.tools import distancematrix as dm
    df = pd.DataFrame()
    df['lat'] = [37.86, 37.85, 37.84, 37.87, 37.88]
    df['lng'] = [-122.27, -122.28, -122.26, -122.29, -122.25]
    dists_eu = dm.distance_matrix(df, method='euclidean')
    dists_gc = dm.distance_matrix(df, method='greatcircle')
    distances = [0, 2000, 4000, np.inf]
    db = dm.distance_bands(dists_gc, distances)
<commit_msg>Remove call to results.summary()<commit_after>def test_import():
    import choicemodels

def test_simple_estimation():
    import choicemodels
    import numpy as np
    import pandas as pd
    from collections import OrderedDict
    endog = np.random.randint(2, size=50)
    exog = np.random.rand(50, 5)
    m = choicemodels.Logit(endog, exog)
    results = m.fit()
#    results.summary()

def test_distance_matrix():
    import pandas as pd, numpy as np
    from choicemodels.tools import distancematrix as dm
    df = pd.DataFrame()
    df['lat'] = [37.86, 37.85, 37.84, 37.87, 37.88]
    df['lng'] = [-122.27, -122.28, -122.26, -122.29, -122.25]
    dists_eu = dm.distance_matrix(df, method='euclidean')
    dists_gc = dm.distance_matrix(df, method='greatcircle')
    distances = [0, 2000, 4000, np.inf]
    db = dm.distance_bands(dists_gc, distances)
<|endoftext|>


<commit_before><commit_msg>Common: fixed log issues in rtk player<commit_after><|endoftext|>


<commit_before>import sys
import unittest

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

import asn1tools

class Asn1ToolsCommandLineTest(unittest.TestCase):

    maxDiff = None

    def test_command_line_decode_foo_question(self):
        argv = ['asn1tools',
                'decode',
                'tests/files/foo.asn',
                'Question',
                '300e0201011609497320312b313d333f']

        stdout = sys.stdout
        sys.argv = argv
        sys.stdout = StringIO()

        try:
            asn1tools._main()

        finally:
            actual_output = sys.stdout.getvalue()
            sys.stdout = stdout

        self.assertTrue('id: 1' in actual_output)
        self.assertTrue('question: Is 1+1=3?' in actual_output)

    def test_command_line_decode_rrc_bcch_dl_sch_message(self):
        argv = ['asn1tools',
                'decode',
                'tests/files/snmp_v1.asn',
                'Message',
                '30819f02010004067075626c6963a3819102013c020100020100308185302206'
                '122b06010401817d08330a0201070a86deb735040c3137322e33312e31392e37'
                '33301706122b06010401817d08330a0201050a86deb960020102302306122b06'
                '010401817d08330a0201070a86deb736040d3235352e3235352e3235352e3030'
                '2106122b06010401817d08330a0201070a86deb738040b3137322e33312e3139'
                '2e32']

        stdout = sys.stdout
        sys.argv = argv
        sys.stdout = StringIO()

        try:
            asn1tools._main()

        finally:
            actual_output = sys.stdout.getvalue()
            sys.stdout = stdout

        print(actual_output)

        expected_output = [
            "community: '7075626c6963'",
            "data:",
            "  set-request:",
            "    variable-bindings:",
            "      [0]:",
            "        value:",
            "          simple:",
            "            string: '3137322e33312e31392e3733'",
            "        name: 1.3.6.1.4.1.253.8.51.10.2.1.7.10.14130101",
            "      [1]:",
            "        value:",
            "          simple:",
            "            number: 2",
            "        name: 1.3.6.1.4.1.253.8.51.10.2.1.5.10.14130400",
            "      [2]:",
            "        value:",
            "          simple:",
            "            string: '3235352e3235352e3235352e30'",
            "        name: 1.3.6.1.4.1.253.8.51.10.2.1.7.10.14130102",
            "      [3]:",
            "        value:",
            "          simple:",
            "            string: '3137322e33312e31392e32'",
            "        name: 1.3.6.1.4.1.253.8.51.10.2.1.7.10.14130104",
            "    error-index: 0",
            "    error-status: 0",
            "    request-id: 60",
            "version: 0"
        ]

        for line in expected_output:
            self.assertTrue(line in actual_output)

if __name__ == '__main__':
    unittest.main()
<commit_msg>Removed debug print in testcase.<commit_after>import sys
import unittest

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

import asn1tools

class Asn1ToolsCommandLineTest(unittest.TestCase):

    maxDiff = None

    def test_command_line_decode_foo_question(self):
        argv = ['asn1tools',
                'decode',
                'tests/files/foo.asn',
                'Question',
                '300e0201011609497320312b313d333f']

        stdout = sys.stdout
        sys.argv = argv
        sys.stdout = StringIO()

        try:
            asn1tools._main()

        finally:
            actual_output = sys.stdout.getvalue()
            sys.stdout = stdout

        self.assertTrue('id: 1' in actual_output)
        self.assertTrue('question: Is 1+1=3?' in actual_output)

    def test_command_line_decode_rrc_bcch_dl_sch_message(self):
        argv = ['asn1tools',
                'decode',
                'tests/files/snmp_v1.asn',
                'Message',
                '30819f02010004067075626c6963a3819102013c020100020100308185302206'
                '122b06010401817d08330a0201070a86deb735040c3137322e33312e31392e37'
                '33301706122b06010401817d08330a0201050a86deb960020102302306122b06'
                '010401817d08330a0201070a86deb736040d3235352e3235352e3235352e3030'
                '2106122b06010401817d08330a0201070a86deb738040b3137322e33312e3139'
                '2e32']

        stdout = sys.stdout
        sys.argv = argv
        sys.stdout = StringIO()

        try:
            asn1tools._main()

        finally:
            actual_output = sys.stdout.getvalue()
            sys.stdout = stdout

        expected_output = [
            "community: '7075626c6963'",
            "data:",
            "  set-request:",
            "    variable-bindings:",
            "      [0]:",
            "        value:",
            "          simple:",
            "            string: '3137322e33312e31392e3733'",
            "        name: 1.3.6.1.4.1.253.8.51.10.2.1.7.10.14130101",
            "      [1]:",
            "        value:",
            "          simple:",
            "            number: 2",
            "        name: 1.3.6.1.4.1.253.8.51.10.2.1.5.10.14130400",
            "      [2]:",
            "        value:",
            "          simple:",
            "            string: '3235352e3235352e3235352e30'",
            "        name: 1.3.6.1.4.1.253.8.51.10.2.1.7.10.14130102",
            "      [3]:",
            "        value:",
            "          simple:",
            "            string: '3137322e33312e31392e32'",
            "        name: 1.3.6.1.4.1.253.8.51.10.2.1.7.10.14130104",
            "    error-index: 0",
            "    error-status: 0",
            "    request-id: 60",
            "version: 0"
        ]

        for line in expected_output:
            self.assertTrue(line in actual_output)

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


<commit_before>from __future__ import with_statement
import os
import subprocess
import shutil
import re
from gen.env import Environment
from gen import path

Path = path.Path

def mkarg(x):
    if isinstance(x, str):
        pass
    elif isinstance(x, Path):
        x = x.native
    elif isinstance(x, tuple):
        a = ''
        for i in x:
            if isinstance(i, str):
                pass
            elif isinstance(i, Path):
                i = i.native
            else:
                raise TypeError('invalid command argument: %r' % (x,))
            a += i
        x = a
    else:
        raise TypeError('invalid command argument: %r' % (x,))
    return x

class Commands(object):
    """A target which runs a command.

    Requires an _env slot, which is initialized by the constructor.
    Subclasses should override the 'commands' method, which should
    return an iterable of commands to run.  Each command is itself an
    iterable of arguments, and each argument is either a string or a
    Path, or a tuple of strings and Paths to be concatenated.

    Subclasses may override the 'name' function, which should return
    the name which is printed when VERBOSE is not enabled.

    Subclasses should also override the input and output methods.
    """
    def __init__(self, env):
        if not isinstance(env, Environment):
            raise TypeError('env must be an Environment object')
        self._env = env

    def input(self):
        assert False

    def output(self):
        assert False

    def commands(self):
        assert False

    def build(self):
        env = self._env
        quiet = not env.VERBOSE
        if quiet:
            try:
                name = self.name
            except AttributeError:
                quiet = False
            else:
                name = name()
                output = ''
                for o in self.output():
                    output = o.posix
                    break
                if output:
                    print name, output
                else:
                    print name
        for cmd in self.commands():
            if not cmd:
                raise ValueError('command is empty')
            args = [mkarg(arg) for arg in cmd]
            if not quiet:
                print ' '.join(args)
            if (cmd[0] == 'cp' and len(cmd) == 3 and
                isinstance(cmd[1], Path) and isinstance(cmd[2], Path)):
                shutil.copyfile(args[1], args[2])
            else:
                proc = subprocess.Popen(args)
                status = proc.wait()
                if status:
                    return False
        return True

class DepTarget(Commands):
    """Target which does nothing, but depends on other targets.

    The name should be a string.
    """
    __slots__ = ['_name', '_deps', '_env']
    def __init__(self, name, deps, env):
        Commands.__init__(self, env)
        if not isinstance(name, str):
            raise TypeError('name should be string')
        deps = list(deps)
        for d in deps:
            if not isinstance(d, (Path, str)):
                raise TypeError(
                    'deps should be sequence of paths and strings')
        self._name = name
        self._deps = deps

    def input(self):
        return iter(self._deps)

    def output(self):
        yield self._name

class CC(Commands):
    """Compile a C, C++, or Objective C file.

    This is an abstract superclass for platform-specific versions.
    """
    __slots__ = ['_dest', '_src', '_env', '_sourcetype']
    def __init__(self, dest, src, env, sourcetype):
        Commands.__init__(self, env)
        if not isinstance(dest, Path):
            raise TypeError('dest must be path')
        if not isinstance(src, Path):
            raise TypeError('src must be path')
        if sourcetype not in ('c', 'm', 'cxx', 'mm'):
            raise ValueError('not a C file type: %r (%s)' %
                             (sourcetype, dest.posix))
        self._dest = dest
        self._src = src
        self._sourcetype = sourcetype

    def input(self):
        yield self._src

    def output(self):
        yield self._dest

    def name(self):
        if self._sourcetype in ('c', 'm'):
            return 'CC'
        elif self._sourcetype in ('cxx', 'mm'):
            return 'CXX'

class LD(Commands):
    """Link an executable.

    This is an abstract superclass for platform-specific versions.
    """
    __slots__ = ['_dest', '_src', '_env']
    def __init__(self, dest, src, env):
        Commands.__init__(self, env)
        if not isinstance(dest, Path):
            raise TypeError('dest must be Path')
        src = list(src)
        for s in src:
            if not isinstance(s, Path):
                raise TypeError('src must be iterable of Path objects')
        self._dest = dest
        self._src = src

    def input(self):
        return iter(self._src)

    def output(self):
        yield self._dest

    def name(self):
        return 'LD'

class CopyFile(Commands):
    """A target which copies a file."""
    __slots__ = ['_dest', '_src', '_env']
    def __init__(self, dest, src, env):
        Commands.__init__(self, env)
        if not isinstance(dest, Path):
            raise TypeError('dest must be Path')
        if not isinstance(src, Path):
            raise TypeError('src must be Path')
        self._dest = dest
        self._src = src

    def input(self):
        yield self._src

    def output(self):
        yield self._dest

    def commands(self):
        return [['cp', self._src, self._dest]]

    def name(self):
        return 'COPY'

class StaticFile(object):
    """A target which creates a file from a string.

    Requires an _env and _dest slot, both of which are initialized by
    the constructor.  Subclasess should override the 'write' method,
    which takes a file-like object and writes data to the file.
    """
    def __init__(self, dest, env):
        if not isinstance(dest, Path):
            raise TypeError('dest must be Path')
        if not isinstance(env, Environment):
            raise TypeError('env must be Environment')
        self._dest = dest
        self._env = env

    def input(self):
        return iter(())

    def output(self):
        yield self._dest

    def write(self, f):
        assert False

    def build(self):
        env = self._env
        print 'FILE', self._dest.posix
        with open(self._dest, 'wb') as f:
            self.write(f)
        return True

class Template(object):
    """Target which creates an file from a template."""
    __slots__ = ['_dest', '_src', '_env']
    def __init__(self, dest, src, env):
        if not isinstance(dest, Path):
            raise TypeError('destination must be path')
        if not isinstance(src, Path):
            raise TypeError('source must be path')
        self._dest = dest
        self._src = src
        self._env = env

    def input(self):
        yield self._src

    def output(self):
        yield self._dest

    def build(self):
        print 'TEMPLATE', self._dest.posix
        data = open(self._src.native, 'rb').read()
        env = self._env
        def repl(m):
            w = m.group(1)
            try:
                return env[w]
            except KeyError:
                pass
            print >>sys.stderr, '%s: unknown variable %r' % (ipath, w)
            return m.group(0)
        data = re.sub(r'\${(\w+)}', repl, data)
        with open(self._dest.native, 'wb') as f:
            f.write(data)
        return True
<commit_msg>build: fix DepTarget target, remove method stubs<commit_after>from __future__ import with_statement
import os
import subprocess
import shutil
import re
from gen.env import Environment
from gen import path

Path = path.Path

def mkarg(x):
    if isinstance(x, str):
        pass
    elif isinstance(x, Path):
        x = x.native
    elif isinstance(x, tuple):
        a = ''
        for i in x:
            if isinstance(i, str):
                pass
            elif isinstance(i, Path):
                i = i.native
            else:
                raise TypeError('invalid command argument: %r' % (x,))
            a += i
        x = a
    else:
        raise TypeError('invalid command argument: %r' % (x,))
    return x

class Commands(object):
    """A target which runs a command.

    Requires an _env slot, which is initialized by the constructor.
    Subclasses should override the 'commands' method, which should
    return an iterable of commands to run.  Each command is itself an
    iterable of arguments, and each argument is either a string or a
    Path, or a tuple of strings and Paths to be concatenated.

    Subclasses may override the 'name' function, which should return
    the name which is printed when VERBOSE is not enabled.

    Subclasses should also override the input and output methods.
    """
    def __init__(self, env):
        if not isinstance(env, Environment):
            raise TypeError('env must be an Environment object')
        self._env = env

    def build(self):
        env = self._env
        quiet = not env.VERBOSE
        if quiet:
            try:
                name = self.name
            except AttributeError:
                quiet = False
            else:
                name = name()
                output = ''
                for o in self.output():
                    output = o.posix
                    break
                if output:
                    print name, output
                else:
                    print name
        for cmd in self.commands():
            if not cmd:
                raise ValueError('command is empty')
            args = [mkarg(arg) for arg in cmd]
            if not quiet:
                print ' '.join(args)
            if (cmd[0] == 'cp' and len(cmd) == 3 and
                isinstance(cmd[1], Path) and isinstance(cmd[2], Path)):
                shutil.copyfile(args[1], args[2])
            else:
                proc = subprocess.Popen(args)
                status = proc.wait()
                if status:
                    return False
        return True

class DepTarget(Commands):
    """Target which does nothing, but depends on other targets.

    The name should be a string.
    """
    __slots__ = ['_name', '_deps', '_env']
    def __init__(self, name, deps, env):
        Commands.__init__(self, env)
        if not isinstance(name, str):
            raise TypeError('name should be string')
        deps = list(deps)
        for d in deps:
            if not isinstance(d, (Path, str)):
                raise TypeError(
                    'deps should be sequence of paths and strings')
        self._name = name
        self._deps = deps

    def input(self):
        return iter(self._deps)

    def output(self):
        yield self._name

    def commands(self):
        return ()

class CC(Commands):
    """Compile a C, C++, or Objective C file.

    This is an abstract superclass for platform-specific versions.
    """
    __slots__ = ['_dest', '_src', '_env', '_sourcetype']
    def __init__(self, dest, src, env, sourcetype):
        Commands.__init__(self, env)
        if not isinstance(dest, Path):
            raise TypeError('dest must be path')
        if not isinstance(src, Path):
            raise TypeError('src must be path')
        if sourcetype not in ('c', 'm', 'cxx', 'mm'):
            raise ValueError('not a C file type: %r (%s)' %
                             (sourcetype, dest.posix))
        self._dest = dest
        self._src = src
        self._sourcetype = sourcetype

    def input(self):
        yield self._src

    def output(self):
        yield self._dest

    def name(self):
        if self._sourcetype in ('c', 'm'):
            return 'CC'
        elif self._sourcetype in ('cxx', 'mm'):
            return 'CXX'

class LD(Commands):
    """Link an executable.

    This is an abstract superclass for platform-specific versions.
    """
    __slots__ = ['_dest', '_src', '_env']
    def __init__(self, dest, src, env):
        Commands.__init__(self, env)
        if not isinstance(dest, Path):
            raise TypeError('dest must be Path')
        src = list(src)
        for s in src:
            if not isinstance(s, Path):
                raise TypeError('src must be iterable of Path objects')
        self._dest = dest
        self._src = src

    def input(self):
        return iter(self._src)

    def output(self):
        yield self._dest

    def name(self):
        return 'LD'

class CopyFile(Commands):
    """A target which copies a file."""
    __slots__ = ['_dest', '_src', '_env']
    def __init__(self, dest, src, env):
        Commands.__init__(self, env)
        if not isinstance(dest, Path):
            raise TypeError('dest must be Path')
        if not isinstance(src, Path):
            raise TypeError('src must be Path')
        self._dest = dest
        self._src = src

    def input(self):
        yield self._src

    def output(self):
        yield self._dest

    def commands(self):
        return [['cp', self._src, self._dest]]

    def name(self):
        return 'COPY'

class StaticFile(object):
    """A target which creates a file from a string.

    Requires an _env and _dest slot, both of which are initialized by
    the constructor.  Subclasess should override the 'write' method,
    which takes a file-like object and writes data to the file.
    """
    def __init__(self, dest, env):
        if not isinstance(dest, Path):
            raise TypeError('dest must be Path')
        if not isinstance(env, Environment):
            raise TypeError('env must be Environment')
        self._dest = dest
        self._env = env

    def input(self):
        return iter(())

    def output(self):
        yield self._dest

    def write(self, f):
        assert False

    def build(self):
        env = self._env
        print 'FILE', self._dest.posix
        with open(self._dest, 'wb') as f:
            self.write(f)
        return True

class Template(object):
    """Target which creates an file from a template."""
    __slots__ = ['_dest', '_src', '_env']
    def __init__(self, dest, src, env):
        if not isinstance(dest, Path):
            raise TypeError('destination must be path')
        if not isinstance(src, Path):
            raise TypeError('source must be path')
        self._dest = dest
        self._src = src
        self._env = env

    def input(self):
        yield self._src

    def output(self):
        yield self._dest

    def build(self):
        print 'TEMPLATE', self._dest.posix
        data = open(self._src.native, 'rb').read()
        env = self._env
        def repl(m):
            w = m.group(1)
            try:
                return env[w]
            except KeyError:
                pass
            print >>sys.stderr, '%s: unknown variable %r' % (ipath, w)
            return m.group(0)
        data = re.sub(r'\${(\w+)}', repl, data)
        with open(self._dest.native, 'wb') as f:
            f.write(data)
        return True
<|endoftext|>


<commit_before>import pytest

import cuckoopy.hashutils as hashutils

from cuckoopy.cuckoofilter import CuckooFilter
from cuckoopy.exceptions import CuckooFilterFullException

@pytest.fixture
def cuckoo_filter():
    return CuckooFilter(capacity=100, fingerprint_size=1)

def test_insert(cuckoo_filter):
    assert cuckoo_filter.insert('fake_insert_value')

def test_insert_alternate_location(cuckoo_filter):
    fake_value = 'fake_value'
    # When bucket_size + 1 values are inserted, the one bucket will be full
    # and the other bucket will have a size of 1.
    for _ in range(cuckoo_filter.bucket_size + 1):
        cuckoo_filter.insert(fake_value)
    first_index = cuckoo_filter._get_index(fake_value)
    fingerprint = hashutils.fingerprint(fake_value,
                                        cuckoo_filter.fingerprint_size)
    alt_index = cuckoo_filter._get_alternate_index(first_index, fingerprint)
    first_bucket = cuckoo_filter.buckets[first_index]
    alt_bucket = cuckoo_filter.buckets[alt_index]
    assert len(first_bucket) == cuckoo_filter.bucket_size
    assert len(alt_bucket) == 1

def test_insert_filter_full(cuckoo_filter):
    # We can artificially make a filter full by inserting the same item
    # (2 * bucket size) times, so another insert of the same item should fail.
    fake_value = 'fake_value'
    for _ in range(2 * cuckoo_filter.bucket_size):
        cuckoo_filter.insert(fake_value)
    with pytest.raises(CuckooFilterFullException):
        cuckoo_filter.insert(fake_value)

def test_delete(cuckoo_filter):
    fake_delete_value = 'fake_delete_value'
    cuckoo_filter.insert(fake_delete_value)
    assert cuckoo_filter.delete(fake_delete_value)
    assert not cuckoo_filter.contains(fake_delete_value)

def test_delete_non_existent_value(cuckoo_filter):
    assert not cuckoo_filter.delete('fake_non_existent_value')

def test_size(cuckoo_filter):
    insert_values = ['fake_value1', 'fake_value2']
    for val in insert_values:
        cuckoo_filter.insert(val)
    assert cuckoo_filter.size == len(insert_values)
    assert len(cuckoo_filter) == len(insert_values)

def test_contains(cuckoo_filter):
    fake_value = 'fake_value'
    cuckoo_filter.insert(fake_value)
    assert cuckoo_filter.contains(fake_value)
    assert fake_value in cuckoo_filter

def test_contains_non_existent_value(cuckoo_filter):
    fake_value = 'fake_value'
    assert not cuckoo_filter.contains(fake_value)
    assert fake_value not in cuckoo_filter
<commit_msg>Test filter with large number of values<commit_after>import os
import pytest

import cuckoopy.hashutils as hashutils

from cuckoopy.cuckoofilter import CuckooFilter
from cuckoopy.exceptions import CuckooFilterFullException

@pytest.fixture
def cuckoo_filter():
    return CuckooFilter(capacity=100, fingerprint_size=1)

@pytest.fixture
def cf():
    return CuckooFilter(capacity=400000, fingerprint_size=1)

def test_insert(cuckoo_filter):
    assert cuckoo_filter.insert('fake_insert_value')

def test_insert_alternate_location(cuckoo_filter):
    fake_value = 'fake_value'
    # When bucket_size + 1 values are inserted, the one bucket will be full
    # and the other bucket will have a size of 1.
    for _ in range(cuckoo_filter.bucket_size + 1):
        cuckoo_filter.insert(fake_value)
    first_index = cuckoo_filter._get_index(fake_value)
    fingerprint = hashutils.fingerprint(fake_value,
                                        cuckoo_filter.fingerprint_size)
    alt_index = cuckoo_filter._get_alternate_index(first_index, fingerprint)
    first_bucket = cuckoo_filter.buckets[first_index]
    alt_bucket = cuckoo_filter.buckets[alt_index]
    assert len(first_bucket) == cuckoo_filter.bucket_size
    assert len(alt_bucket) == 1

def test_insert_filter_full(cuckoo_filter):
    # We can artificially make a filter full by inserting the same item
    # (2 * bucket size) times, so another insert of the same item should fail.
    fake_value = 'fake_value'
    for _ in range(2 * cuckoo_filter.bucket_size):
        cuckoo_filter.insert(fake_value)
    with pytest.raises(CuckooFilterFullException):
        cuckoo_filter.insert(fake_value)

@pytest.mark.skipif(not os.path.isfile('/usr/share/dict/words'),
                    reason='Dictionary file /usr/share/dict/words'
                           'does not exist')
def test_insert_large_number_of_values(cf):
    with open('/usr/share/dict/words') as f:
        words = f.readlines()
    for word in words:
        cf.insert(word.strip())
    assert len(cf) == len(words)

def test_delete(cuckoo_filter):
    fake_delete_value = 'fake_delete_value'
    cuckoo_filter.insert(fake_delete_value)
    assert cuckoo_filter.delete(fake_delete_value)
    assert not cuckoo_filter.contains(fake_delete_value)

def test_delete_non_existent_value(cuckoo_filter):
    assert not cuckoo_filter.delete('fake_non_existent_value')

@pytest.mark.skipif(not os.path.isfile('/usr/share/dict/words'),
                    reason='Dictionary file /usr/share/dict/words'
                           'does not exist')
def test_delete_large_number_of_values(cf):
    with open('/usr/share/dict/words') as f:
        words = f.readlines()
    words = [w.strip() for w in words]
    for word in words:
        cf.insert(word)
    assert len(cf) == len(words)
    deleted = True
    for word in words:
        if not cf.delete(word):
            deleted = False
    assert deleted

def test_size(cuckoo_filter):
    insert_values = ['fake_value1', 'fake_value2']
    for val in insert_values:
        cuckoo_filter.insert(val)
    assert cuckoo_filter.size == len(insert_values)
    assert len(cuckoo_filter) == len(insert_values)

def test_contains(cuckoo_filter):
    fake_value = 'fake_value'
    cuckoo_filter.insert(fake_value)
    assert cuckoo_filter.contains(fake_value)
    assert fake_value in cuckoo_filter

@pytest.mark.skipif(not os.path.isfile('/usr/share/dict/words'),
                    reason='Dictionary file /usr/share/dict/words'
                           'does not exist')
def test_contains_large_number_of_values(cf):
    with open('/usr/share/dict/words') as f:
        words = f.readlines()
    words = [w.strip() for w in words]
    for word in words:
        cf.insert(word)
    assert len(cf) == len(words)
    contains = True
    for word in words:
        if not cf.contains(word):
            contains = False
    assert contains

def test_contains_non_existent_value(cuckoo_filter):
    fake_value = 'fake_value'
    assert not cuckoo_filter.contains(fake_value)
    assert fake_value not in cuckoo_filter
<|endoftext|>


<commit_before>#!/usr/bin/env python
import os, sys, json, requests, types, re, copy
from datetime import datetime
import requests_cache

from fv_prov_es import create_app
from fv_prov_es.lib.import_utils import get_es_conn, import_prov

from prov_es.model import get_uuid, ProvEsDocument

requests_cache.install_cache('gcis-import')

def get_image_prov(j, gcis_url):
    """Generate PROV-ES JSON from GCIS image metadata."""

    # create doc
    gcis_ns = "http://data.globalchange.gov/gcis.owl#"
    doc = ProvEsDocument(namespaces={ "gcis": gcis_ns })
    bndl = None

    # create image, figure, chapter and report entities
    img_id = "gcis:%s" % j['uri'][1:].replace('/', '-')
    img_title = j['title']
    img_url = None
    img_thumbnail_url = None
    for file_md in j.get('files', []):
        img_url = file_md['href']
        img_thumbnail_url = file_md['thumbnail_href']
    img_attrs = [
        ( "prov:type", 'gcis:Image' ),
        ( "prov:label", img_title ),
    ]
    if img_url is None:
        img_attrs.append(( "prov:location", "%s%s" % (gcis_url, j['uri']) ))
    else:
        img_attrs.append(( "prov:location", img_url ))
    if img_thumbnail_url is None:
        img_attrs.append(( "hysds:thumbnail", img_thumbnail_url ))
    doc.entity(img_id, img_attrs)
    reports = []
    chapters = []
    findings = []
    figures = []
    for figure in j.get('figures', []):
        report_uri = "/report/%s" % figure['report_identifier']
        chapter_uri = "/chapter/%s" % figure['chapter_identifier']
        figure_uri = "/figure/%s" % figure['identifier']

        # create report
        r = requests.get('%s%s.json' % (gcis_url, report_uri))
        r.raise_for_status()
        report = r.json()
        report_id = "gcis:%s" % report_uri[1:].replace('/', '-')
        if report_id not in reports:
            doc.entity(report_id, [
                ( "prov:type", 'gcis:Report' ),
                ( "prov:label", report['title'] ),
                ( "prov:location", report['url'] ),
            ])
            reports.append(report_id)

        # create chapter
        r = requests.get('%s%s%s.json' % (gcis_url, report_uri, chapter_uri))
        r.raise_for_status()
        chapter = r.json()
        chapter_id = "gcis:%s" % chapter_uri[1:].replace('/', '-')
        if chapter_id not in chapters:
            doc.entity(chapter_id, [
                ( "prov:type", 'gcis:Chapter' ),
                ( "prov:label", chapter['title'] ),
                ( "prov:location", chapter['url'] ),
            ])
            chapters.append(chapter_id)
        doc.hadMember(report_id, chapter_id)

        # create findings
        r = requests.get('%s%s%s/finding.json' % (gcis_url, report_uri, chapter_uri))
        r.raise_for_status()
        for f in r.json():
            finding_id = "gcis:%s" % f['identifier']
            if finding_id not in findings:
                doc.entity(finding_id, [
                    ( "prov:type", 'gcis:Finding' ),
                    ( "prov:label", f['identifier'] ),
                    ( "prov:location", f['href'] ),
                ])
                findings.append(finding_id)
            doc.hadMember(report_id, finding_id)
            doc.hadMember(chapter_id, finding_id)

        # create figure
        r = requests.get('%s%s%s%s.json' % (gcis_url, report_uri, chapter_uri, figure_uri))
        r.raise_for_status()
        figure_md = r.json()
        figure_id = "gcis:%s" % figure_uri[1:].replace('/', '-')
        if figure_id not in figures:
            doc.entity(figure_id, [
                ( "prov:type", 'gcis:Figure' ),
                ( "prov:label", figure_md['title'] ),
                ( "prov:location", "%s%s" % (gcis_url, figure_md['uri']) ),
            ])
            figures.append(figure_id)
            doc.hadMember(chapter_id, figure_id)
        doc.hadMember(figure_id, img_id)

    # create agents or organizations
    agent_ids = {}
    org_ids = {}
    for cont in j.get('contributors', []):
        # replace slashes because we get prov.model.ProvExceptionInvalidQualifiedName errors
        agent_id = "gcis:%s" % cont['uri'][1:].replace('/', '-')

        # create person
        if len(cont['person']) > 0:
            # agent
            agent_name  = " ".join([cont['person'][i] for i in
                                   ('first_name', 'middle_name', 'last_name')
                                   if cont['person'].get(i, None) is not None])
            doc.agent(agent_id, [
                ( "prov:type", "gcis:Person" ),
                ( "prov:label", agent_name ),
                ( "prov:location", "%s%s" % (gcis_url, cont['uri']) ),
            ])
            agent_ids[agent_id] = []

        # organization
        if len(cont['organization']) > 0:
            org = cont['organization']
            org_id = "gcis:%s" % cont['organization']['identifier']
            if org_id not in org_ids:
                doc.governingOrganization(org_id, cont['organization']['name'])
                org_ids[org_id] = True
            if agent_id in agent_ids: agent_ids[agent_id].append(org_id)

    # create activity
    start_time = j['create_dt']
    end_time = j['create_dt']
    for parent in j.get('parents', []):
        input_id = "gcis:%s" % parent['url'][1:].replace('/', '-')
        input_name = parent['label']
        doc.entity(input_id, [
            ( "prov:type", "gcis:Dataset" ),
            ( "prov:label", input_name ),
            ( "prov:location", "%s%s" % (gcis_url, parent['url']) ),
        ])
        # some activity uri's are null
        if parent['activity_uri'] is None:
            act_id = "gcis:derive-from-%s" % input_id
        else:
            act_id = "gcis:%s" % parent['activity_uri'][1:].replace('/', '-')
        attrs = []
        for agent_id in agent_ids:
            waw_id = "gcis:%s" % get_uuid("%s:%s" % (act_id, agent_id))
            doc.wasAssociatedWith(act_id, agent_id, None, waw_id, {'prov:role': 'gcis:Contributor'})
            for org_id in agent_ids[agent_id]:
                del_id = "gcis:%s" % get_uuid("%s:%s:%s" % (agent_id, org_id, act_id))
                doc.delegation(agent_id, org_id, act_id, del_id, {'prov:type': 'gcis:worksAt'})
        for org_id in org_ids:
            waw_id = "gcis:%s" % get_uuid("%s:%s" % (act_id, org_id))
            doc.wasAssociatedWith(act_id, org_id, None, waw_id, {'prov:role': 'gcis:Funder'})
        act = doc.activity(act_id, start_time, end_time, attrs)
        doc.used(act, input_id, start_time, "gcis:%s" % get_uuid("%s:%s" % (act_id, input_id)))
        doc.wasGeneratedBy(img_id, act, end_time, "gcis:%s" % get_uuid("%s:%s" % (img_id, act_id)))

    # serialize
    prov_json = json.loads(doc.serialize())

    # for hadMember relations, add prov:type
    for hm_id in prov_json.get('hadMember', {}):
        hm = prov_json['hadMember'][hm_id]
        col = hm['prov:collection']
        ent = hm['prov:entity']
        if col in reports and ent in chapters:
            hm['prov:type'] = 'gcis:hasChapter'
        elif col in chapters and ent in figures:
            hm['prov:type'] = 'gcis:hasFigure'
        elif col in figures and ent == img_id:
            hm['prov:type'] = 'gcis:hasImage'

    #print(json.dumps(prov_json, indent=2))

    return prov_json

def index_gcis(gcis_url, es_url, index, alias):
    """Index GCIS into PROV-ES ElasticSearch index."""

    conn = get_es_conn(es_url, index, alias)
    r = requests.get('%s/image.json' % gcis_url, params={ 'all': 1 })
    r.raise_for_status()
    imgs = r.json()
    #print(json.dumps(images, indent=2))
    #print(len(images))
    for img in imgs:
        img_id = img['identifier']
        img_href = img['href']
        r2 = requests.get(img_href, params={ 'all': 1 })
        r2.raise_for_status()
        img_md = r2.json()
        #print(json.dumps(img_md, indent=2))
        prov = get_image_prov(img_md, gcis_url)
        #print(json.dumps(prov, indent=2))
        import_prov(conn, index, alias, prov)

if __name__ == "__main__":
    env = os.environ.get('PROVES_ENV', 'prod')
    app = create_app('fv_prov_es.settings.%sConfig' % env.capitalize(), env=env)
    es_url = app.config['ES_URL']
    gcis_url =  "http://data.globalchange.gov"
    dt = datetime.utcnow()
    #index = "%s-%04d.%02d.%02d" % (app.config['PROVES_ES_PREFIX'],
    #                               dt.year, dt.month, dt.day)
    index = "%s-gcis" % app.config['PROVES_ES_PREFIX']
    alias = app.config['PROVES_ES_ALIAS']
    index_gcis(gcis_url, es_url, index, alias)
<commit_msg>updated to for latest prov_es lib updates<commit_after>#!/usr/bin/env python
import os, sys, json, requests, types, re, copy
from datetime import datetime
import requests_cache

from fv_prov_es import create_app
from fv_prov_es.lib.import_utils import get_es_conn, import_prov

from prov_es.model import (get_uuid, ProvEsDocument, GCIS, PROV, PROV_TYPE,
                           PROV_ROLE, PROV_LABEL, PROV_LOCATION, HYSDS)

requests_cache.install_cache('gcis-import')

def get_image_prov(j, gcis_url):
    """Generate PROV-ES JSON from GCIS image metadata."""

    # create doc
    doc = ProvEsDocument()
    bndl = None

    # create image, figure, chapter and report entities
    img_id = GCIS["%s" % j['uri'][1:].replace('/', '-')]
    img_title = j['title']
    img_url = None
    img_thumbnail_url = None
    for file_md in j.get('files', []):
        img_url = file_md['href']
        img_thumbnail_url = file_md['thumbnail_href']
    img_attrs = [
        ( PROV_TYPE, GCIS['Image'] ),
        ( PROV_LABEL, img_title ),
    ]
    if img_url is None:
        img_attrs.append(( PROV_LOCATION, "%s%s" % (gcis_url, j['uri']) ))
    else:
        img_attrs.append(( PROV_LOCATION, img_url ))
    if img_thumbnail_url is None:
        img_attrs.append(( HYSDS['thumbnail'], img_thumbnail_url ))
    doc.entity(img_id, img_attrs)
    reports = []
    chapters = []
    findings = []
    figures = []
    for figure in j.get('figures', []):
        report_uri = "/report/%s" % figure['report_identifier']
        chapter_uri = "/chapter/%s" % figure['chapter_identifier']
        figure_uri = "/figure/%s" % figure['identifier']

        # create report
        r = requests.get('%s%s.json' % (gcis_url, report_uri))
        r.raise_for_status()
        report = r.json()
        report_id = GCIS["%s" % report_uri[1:].replace('/', '-')]
        if report_id not in reports:
            doc.entity(report_id, [
                ( PROV_TYPE, GCIS['Report'] ),
                ( PROV_LABEL, report['title'] ),
                ( PROV_LOCATION, report['url'] ),
            ])
            reports.append(report_id)

        # create chapter
        r = requests.get('%s%s%s.json' % (gcis_url, report_uri, chapter_uri))
        r.raise_for_status()
        chapter = r.json()
        chapter_id = GCIS["%s" % chapter_uri[1:].replace('/', '-')]
        if chapter_id not in chapters:
            doc.entity(chapter_id, [
                ( PROV_TYPE, GCIS['Chapter'] ),
                ( PROV_LABEL, chapter['title'] ),
                ( PROV_LOCATION, chapter['url'] ),
            ])
            chapters.append(chapter_id)
        doc.hadMember(report_id, chapter_id)

        # create findings
        r = requests.get('%s%s%s/finding.json' % (gcis_url, report_uri, chapter_uri))
        r.raise_for_status()
        for f in r.json():
            finding_id = GCIS["%s" % f['identifier']]
            if finding_id not in findings:
                doc.entity(finding_id, [
                    ( PROV_TYPE, GCIS['Finding'] ),
                    ( PROV_LABEL, f['identifier'] ),
                    ( PROV_LOCATION, f['href'] ),
                ])
                findings.append(finding_id)
            doc.hadMember(report_id, finding_id)
            doc.hadMember(chapter_id, finding_id)

        # create figure
        r = requests.get('%s%s%s%s.json' % (gcis_url, report_uri, chapter_uri, figure_uri))
        r.raise_for_status()
        figure_md = r.json()
        figure_id = GCIS["%s" % figure_uri[1:].replace('/', '-')]
        if figure_id not in figures:
            doc.entity(figure_id, [
                ( PROV_TYPE, GCIS['Figure'] ),
                ( PROV_LABEL, figure_md['title'] ),
                ( PROV_LOCATION, "%s%s" % (gcis_url, figure_md['uri']) ),
            ])
            figures.append(figure_id)
            doc.hadMember(chapter_id, figure_id)
        doc.hadMember(figure_id, img_id)

    # create agents or organizations
    agent_ids = {}
    org_ids = {}
    for cont in j.get('contributors', []):
        # replace slashes because we get prov.model.ProvExceptionInvalidQualifiedName errors
        agent_id = GCIS["%s" % cont['uri'][1:].replace('/', '-')]

        # create person
        if len(cont['person']) > 0:
            # agent
            agent_name  = " ".join([cont['person'][i] for i in
                                   ('first_name', 'middle_name', 'last_name')
                                   if cont['person'].get(i, None) is not None])
            doc.agent(agent_id, [
                ( PROV_TYPE, GCIS["Person"] ),
                ( PROV_LABEL, agent_name ),
                ( PROV_LOCATION, "%s%s" % (gcis_url, cont['uri']) ),
            ])
            agent_ids[agent_id] = []

        # organization
        if len(cont['organization']) > 0:
            org = cont['organization']
            org_id = GCIS["%s" % cont['organization']['identifier']]
            if org_id not in org_ids:
                doc.governingOrganization(org_id, cont['organization']['name'])
                org_ids[org_id] = True
            if agent_id in agent_ids: agent_ids[agent_id].append(org_id)

    # create activity
    start_time = j['create_dt']
    end_time = j['create_dt']
    for parent in j.get('parents', []):
        input_id = GCIS["%s" % parent['url'][1:].replace('/', '-')]
        input_name = parent['label']
        doc.entity(input_id, [
            ( PROV_TYPE, GCIS["Dataset"] ),
            ( PROV_LABEL, input_name ),
            ( PROV_LOCATION, "%s%s" % (gcis_url, parent['url']) ),
        ])
        # some activity uri's are null
        if parent['activity_uri'] is None:
            act_id = GCIS["derive-from-%s" % input_id]
        else:
            act_id = GCIS["%s" % parent['activity_uri'][1:].replace('/', '-')]
        attrs = []
        for agent_id in agent_ids:
            waw_id = GCIS["%s" % get_uuid("%s:%s" % (act_id, agent_id))]
            doc.wasAssociatedWith(act_id, agent_id, None, waw_id, {'prov:role': GCIS['Contributor']})
            for org_id in agent_ids[agent_id]:
                del_id = GCIS["%s" % get_uuid("%s:%s:%s" % (agent_id, org_id, act_id))]
                doc.delegation(agent_id, org_id, act_id, del_id, {'prov:type': GCIS['worksAt']})
        for org_id in org_ids:
            waw_id = GCIS["%s" % get_uuid("%s:%s" % (act_id, org_id))]
            doc.wasAssociatedWith(act_id, org_id, None, waw_id, {'prov:role': GCIS['Funder']})
        act = doc.activity(act_id, start_time, end_time, attrs)
        doc.used(act, input_id, start_time, GCIS["%s" % get_uuid("%s:%s" % (act_id, input_id))])
        doc.wasGeneratedBy(img_id, act, end_time, GCIS["%s" % get_uuid("%s:%s" % (img_id, act_id))])

    # serialize
    prov_json = json.loads(doc.serialize())

    # for hadMember relations, add prov:type
    for hm_id in prov_json.get('hadMember', {}):
        hm = prov_json['hadMember'][hm_id]
        col = hm['prov:collection']
        ent = hm['prov:entity']
        if col in reports and ent in chapters:
            hm['prov:type'] = GCIS['hasChapter']
        elif col in chapters and ent in figures:
            hm['prov:type'] = GCIS['hasFigure']
        elif col in figures and ent == img_id:
            hm['prov:type'] = GCIS['hasImage']

    #print(json.dumps(prov_json, indent=2))

    return prov_json

def index_gcis(gcis_url, es_url, index, alias):
    """Index GCIS into PROV-ES ElasticSearch index."""

    conn = get_es_conn(es_url, index, alias)
    r = requests.get('%s/image.json' % gcis_url, params={ 'all': 1 })
    r.raise_for_status()
    imgs = r.json()
    #print(json.dumps(images, indent=2))
    #print(len(images))
    for img in imgs:
        img_id = img['identifier']
        img_href = img['href']
        r2 = requests.get(img_href, params={ 'all': 1 })
        r2.raise_for_status()
        img_md = r2.json()
        #print(json.dumps(img_md, indent=2))
        prov = get_image_prov(img_md, gcis_url)
        #print(json.dumps(prov, indent=2))
        import_prov(conn, index, alias, prov)

if __name__ == "__main__":
    env = os.environ.get('PROVES_ENV', 'prod')
    app = create_app('fv_prov_es.settings.%sConfig' % env.capitalize(), env=env)
    es_url = app.config['ES_URL']
    gcis_url =  "http://data.globalchange.gov"
    dt = datetime.utcnow()
    #index = "%s-%04d.%02d.%02d" % (app.config['PROVES_ES_PREFIX'],
    #                               dt.year, dt.month, dt.day)
    index = "%s-gcis" % app.config['PROVES_ES_PREFIX']
    alias = app.config['PROVES_ES_ALIAS']
    index_gcis(gcis_url, es_url, index, alias)
<|endoftext|>


<commit_before>########################################
# Use:
# python noteAverage.py <songList.txt> [options]
#
# Format of songList.txt. make sure there are no trailing lines at end
# of file:
# file1.csv
# file2.csv
#
# Options:
# -w : THe presence of notes in graph are weighted (Default: not weighted)
# -t graph_song_title : This becomes the song title on the graph (Default: fils name)
# -s : Save the graph in ../graphs/ (Default: not saved)
# --no-display : don't display graph (Default: displayed through X-11 forwarding)
# python notePresence.py songlist.txt -w -t Haru_Haru -s
########################################

import sys
import matplotlib.pyplot as plt
import numpy as np
from textwrap import wrap
import math

csvListFileName = sys.argv[1]
WEIGHTFLAG = False
DISPLAYFLAG = True
SAVEFLAG = False
GRAPHSONGTITLE = csvListFileName
FILESAVENAME = csvListFileName

argNum = 2
while argNum < len(sys.argv):
   if sys.argv[argNum] == "-w":
      WEIGHTFLAG = True
      print "Using weighted velocity"
   elif sys.argv[argNum] == "-t":
      argNum += 1
      GRAPHSONGTITLE = sys.argv[argNum]
      print "Using Graph Title: " + GRAPHSONGTITLE
   elif sys.argv[argNum] == "-s":
      argNum += 1
      SAVEFLAG = True
      FILESAVENAME = sys.argv[argNum]
      print "Using file save name: " + FILESAVENAME
   elif sys.argv[argNum] == "--no-display":
      DISPLAYFLAG = False
      print "Not displaying graph"
   argNum += 1

songCount = sum(1 for line in open(csvListFileName))
averagePerSong = [0]*songCount
std_dev = [0]*songCount
songNumber = 0
labels = [""]*songCount

csvListFilePointer = open(csvListFileName)
for song in csvListFilePointer:
   print "looping"
   song = song.strip()
   csvFilePointer = open("../csv/"+song)
   lastNoteTime = [0]*128 #arr 0-127
   noteWeightsTemp = [0]*128
   noteWeights = [0]*128
   maxMidiClocks = 0
   maxVelocity = 0

   #find max velocity
   for line in csvFilePointer:
      lineArr = [x.strip() for x in line.split(",")]
      if len(lineArr) < 6: continue
      velocity = int(lineArr[5])
      if maxVelocity < velocity:
         maxVelocity = velocity
   csvFilePointer.close()

   #read through file
   csvFilePointer = open("../csv/"+song)
   for line in csvFilePointer:
      lineArr = [x.strip() for x in line.split(",")]
      if len(lineArr) < 6: continue
      timeStamp = int(lineArr[1])
      eventName = lineArr[2]
      note = int(lineArr[4])
      velocity = int(lineArr[5])
      if eventName == "Note_on_c":
         lastNoteTime[note] = timeStamp
         noteWeightsTemp[note] = velocity
      elif eventName == "Note_off_c":
         noteLength = timeStamp - lastNoteTime[note]
         if WEIGHTFLAG:
            noteWeights[note] = noteWeights[note] + float(noteWeightsTemp[note])/maxVelocity * noteLength
         else:
            noteWeights[note] = noteWeights[note] + noteLength
      if maxMidiClocks < timeStamp:
         maxMidiClocks = timeStamp

   noteWeightsNormalized = [float(x)/maxMidiClocks*100 for x in noteWeights]
   #mean DOUBLE CHECK THIS CALCULATION
   expectedPreprocess = [0]*128
   for i in range(0,128):
      expectedPreprocess[i] = noteWeightsNormalized[i]*i
   averagePerSong[songNumber] = sum(expectedPreprocess)/sum(noteWeightsNormalized)
   print averagePerSong

   #variance
   variancePreprocess = [0]*128
   for i in range(0,128):
      variancePreprocess[i] = noteWeightsNormalized[i]*(i**2)#(averagePerSong[songNumber]-i)**2
   std_dev[songNumber] = math.sqrt(\
      sum(variancePreprocess)/sum(noteWeightsNormalized) \
      - (averagePerSong[songNumber]**2))
   print std_dev

   #label
   labels[songNumber] = song[:-4]
   songNumber += 1

#plot
print "plotting..."
fig, ax = plt.subplots()
# ax.plot(averagePerSong,range(1,songCount+1),'bo')
ax.errorbar(averagePerSong, range(1,songCount+1), xerr=std_dev, fmt='o', capthick=1)

#plot labels
ax.set_ylabel('Song Name')
if WEIGHTFLAG:
   ax.set_xlabel('Average Pitch in song (60-middle C)')
   ax.set_title('Average Pitch of Selected Songs')
else:
   ax.set_xlabel('Weighted Average Pitch in song (60-middle C)')
   ax.set_title('Weighted Average Pitch of Selected Songs')
labels = [ '\n'.join(wrap(l, 20)) for l in labels ]
plt.yticks(range(1,songCount+1), labels)

if SAVEFLAG:
   saveLocation = '../graphs/'+FILESAVENAME
   if WEIGHTFLAG:
      saveLocation += '_weighted'
   saveLocation += '_note_average.png'
   plt.savefig(saveLocation)

if DISPLAYFLAG:
   plt.show()<commit_msg>Delete noteAverageCurve.py<commit_after><|endoftext|>