<commit_before>import datetime
import re

from flask import Blueprint, request

from everyclass.server.entity import service as entity_service
from everyclass.server.entity.model import SearchResultItem
from everyclass.server.user import service as user_service
from everyclass.server.utils import generate_error_response, api_helpers, generate_success_response
from everyclass.server.utils.common_helpers import get_logged_in_uid, get_ut_uid
from everyclass.server.utils.encryption import decrypt, RTYPE_ROOM

entity_api_bp = Blueprint('api_entity', __name__)

@entity_api_bp.route('/multi_people_schedule')
def multi_people_schedule():
    people_encoded = request.args.get('people')
    date = request.args.get('date')

    uid = get_logged_in_uid()

    if not people_encoded:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing people parameter')
    if not date:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing date parameter')

    people_list = [decrypt(people)[1] for people in people_encoded.split(',')]
    date = datetime.date(*map(int, date.split('-')))
    schedule = entity_service.multi_people_schedule(people_list, date, uid)
    return generate_success_response(schedule)

@entity_api_bp.route('/multi_people_schedule/_search')
def multi_people_schedule_search():
    keyword = request.args.get('keyword')
    if not keyword:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing keyword parameter')

    print([request.cookies.items()])

    search_result = entity_service.search(keyword)

    uid = get_logged_in_uid()

    items = []
    for s in search_result.students:
        eligible = False
        groups = re.findall(r'\d+', s.klass)
        if len(groups) > 0:
            if int(groups[0][:2]) + 5 >= datetime.date.today().year - 2000:
                eligible = True
        else:
            eligible = True

        if eligible:
            items.append(SearchResultItem(s.name, s.deputy + s.klass, "student", s.student_id_encoded,
                                          *user_service.has_access(s.student_id, uid, False)))

    items.extend([SearchResultItem(t.name, t.unit + t.title, "teacher", t.teacher_id_encoded,
                                   *user_service.has_access(t.teacher_id, uid, False)) for t in search_result.teachers])
    return generate_success_response({'items': items, 'keyword': keyword, 'is_guest': True if uid is None else False})

@entity_api_bp.route('/room')
def get_all_rooms():
    return generate_success_response(entity_service.get_rooms())

@entity_api_bp.route('/room/_available')
def get_available_rooms():
    campus = request.args.get('campus')
    building = request.args.get('building')
    time = request.args.get('time')
    date_str = request.args.get('date')
    if not date_str:
        date = datetime.date.today()
    else:
        date = datetime.date(*map(int, date_str.split('-')))

    # vip 可以选择日期，普通用户只能选择时间

    if not campus:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing campus parameter')
    if not building:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing building parameter')
    if not time:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing time parameter')

    return generate_success_response(entity_service.get_available_rooms(campus, building, date, time))

@entity_api_bp.route('/room/_report_unavailable')
def report_unavailable_room():
    room_id_encoded = request.args.get("room_id")
    time = request.args.get("time")
    date_str = request.args.get("date")
    date = datetime.date(*map(int, date_str.split('-')))

    # 运营策略：报告获得他人认同可以加积分

    if not room_id_encoded:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing room_id parameter')
    if not time:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing time parameter')
    if not date_str:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing date parameter')

    try:
        resource_type, room_id = decrypt(room_id_encoded, resource_type=RTYPE_ROOM)
    except ValueError:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'invalid room_id')

    entity_service.report_unavailable_room(room_id, date, time, *get_ut_uid())
    return generate_success_response(None)
<commit_msg>print cookie for test<commit_after>import datetime
import re

from flask import Blueprint, request

from everyclass.server.entity import service as entity_service
from everyclass.server.entity.model import SearchResultItem
from everyclass.server.user import service as user_service
from everyclass.server.utils import generate_error_response, api_helpers, generate_success_response
from everyclass.server.utils.common_helpers import get_logged_in_uid, get_ut_uid
from everyclass.server.utils.encryption import decrypt, RTYPE_ROOM

entity_api_bp = Blueprint('api_entity', __name__)

@entity_api_bp.route('/multi_people_schedule')
def multi_people_schedule():
    people_encoded = request.args.get('people')
    date = request.args.get('date')

    uid = get_logged_in_uid()

    if not people_encoded:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing people parameter')
    if not date:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing date parameter')

    people_list = [decrypt(people)[1] for people in people_encoded.split(',')]
    date = datetime.date(*map(int, date.split('-')))
    schedule = entity_service.multi_people_schedule(people_list, date, uid)
    return generate_success_response(schedule)

@entity_api_bp.route('/multi_people_schedule/_search')
def multi_people_schedule_search():
    keyword = request.args.get('keyword')
    if not keyword:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing keyword parameter')

    print([request.cookies.get('e_session')])

    search_result = entity_service.search(keyword)

    uid = get_logged_in_uid()

    items = []
    for s in search_result.students:
        eligible = False
        groups = re.findall(r'\d+', s.klass)
        if len(groups) > 0:
            if int(groups[0][:2]) + 5 >= datetime.date.today().year - 2000:
                eligible = True
        else:
            eligible = True

        if eligible:
            items.append(SearchResultItem(s.name, s.deputy + s.klass, "student", s.student_id_encoded,
                                          *user_service.has_access(s.student_id, uid, False)))

    items.extend([SearchResultItem(t.name, t.unit + t.title, "teacher", t.teacher_id_encoded,
                                   *user_service.has_access(t.teacher_id, uid, False)) for t in search_result.teachers])
    return generate_success_response({'items': items, 'keyword': keyword, 'is_guest': True if uid is None else False})

@entity_api_bp.route('/room')
def get_all_rooms():
    return generate_success_response(entity_service.get_rooms())

@entity_api_bp.route('/room/_available')
def get_available_rooms():
    campus = request.args.get('campus')
    building = request.args.get('building')
    time = request.args.get('time')
    date_str = request.args.get('date')
    if not date_str:
        date = datetime.date.today()
    else:
        date = datetime.date(*map(int, date_str.split('-')))

    # vip 可以选择日期，普通用户只能选择时间

    if not campus:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing campus parameter')
    if not building:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing building parameter')
    if not time:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing time parameter')

    return generate_success_response(entity_service.get_available_rooms(campus, building, date, time))

@entity_api_bp.route('/room/_report_unavailable')
def report_unavailable_room():
    room_id_encoded = request.args.get("room_id")
    time = request.args.get("time")
    date_str = request.args.get("date")
    date = datetime.date(*map(int, date_str.split('-')))

    # 运营策略：报告获得他人认同可以加积分

    if not room_id_encoded:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing room_id parameter')
    if not time:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing time parameter')
    if not date_str:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'missing date parameter')

    try:
        resource_type, room_id = decrypt(room_id_encoded, resource_type=RTYPE_ROOM)
    except ValueError:
        return generate_error_response(None, api_helpers.STATUS_CODE_INVALID_REQUEST, 'invalid room_id')

    entity_service.report_unavailable_room(room_id, date, time, *get_ut_uid())
    return generate_success_response(None)
<|endoftext|>


<commit_before>import os

from django import forms, http
from django.conf import settings
from django.contrib.formtools import preview, wizard, utils
from django.test import TestCase
from django.utils import unittest

success_string = "Done was called!"

class TestFormPreview(preview.FormPreview):
    def get_context(self, request, form):
        context = super(TestFormPreview, self).get_context(request, form)
        context.update({'custom_context': True})
        return context

    def get_initial(self, request):
        return {'field1': 'Works!'}

    def done(self, request, cleaned_data):
        return http.HttpResponse(success_string)

class TestForm(forms.Form):
    field1 = forms.CharField()
    field1_ = forms.CharField()
    bool1 = forms.BooleanField(required=False)

class UserSecuredFormPreview(TestFormPreview):
    """
    FormPreview with a custum security_hash method
    """
    def security_hash(self, request, form):
        return "123"

class PreviewTests(TestCase):
    urls = 'django.contrib.formtools.tests.urls'

    def setUp(self):
        # Create a FormPreview instance to share between tests
        self.preview = preview.FormPreview(TestForm)
        input_template = '<input type="hidden" name="%s" value="%s" />'
        self.input = input_template % (self.preview.unused_name('stage'), "%d")
        self.test_data = {'field1':u'foo', 'field1_':u'asdf'}

    def test_unused_name(self):
        """
        Verifies name mangling to get uniue field name.
        """
        self.assertEqual(self.preview.unused_name('field1'), 'field1__')

    def test_form_get(self):
        """
        Test contrib.formtools.preview form retrieval.

        Use the client library to see if we can sucessfully retrieve
        the form (mostly testing the setup ROOT_URLCONF
        process). Verify that an additional  hidden input field
        is created to manage the stage.

        """
        response = self.client.get('/test1/')
        stage = self.input % 1
        self.assertContains(response, stage, 1)
        self.assertEquals(response.context['custom_context'], True)
        self.assertEquals(response.context['form'].initial, {'field1': 'Works!'})

    def test_form_preview(self):
        """
        Test contrib.formtools.preview form preview rendering.

        Use the client library to POST to the form to see if a preview
        is returned.  If we do get a form back check that the hidden
        value is correctly managing the state of the form.

        """
        # Pass strings for form submittal and add stage variable to
        # show we previously saw first stage of the form.
        self.test_data.update({'stage': 1})
        response = self.client.post('/test1/', self.test_data)
        # Check to confirm stage is set to 2 in output form.
        stage = self.input % 2
        self.assertContains(response, stage, 1)

    def test_form_submit(self):
        """
        Test contrib.formtools.preview form submittal.

        Use the client library to POST to the form with stage set to 3
        to see if our forms done() method is called. Check first
        without the security hash, verify failure, retry with security
        hash and verify sucess.

        """
        # Pass strings for form submittal and add stage variable to
        # show we previously saw first stage of the form.
        self.test_data.update({'stage':2})
        response = self.client.post('/test1/', self.test_data)
        self.assertNotEqual(response.content, success_string)
        hash = self.preview.security_hash(None, TestForm(self.test_data))
        self.test_data.update({'hash': hash})
        response = self.client.post('/test1/', self.test_data)
        self.assertEqual(response.content, success_string)

    def test_bool_submit(self):
        """
        Test contrib.formtools.preview form submittal when form contains:
        BooleanField(required=False)

        Ticket: #6209 - When an unchecked BooleanField is previewed, the preview
        form's hash would be computed with no value for ``bool1``. However, when
        the preview form is rendered, the unchecked hidden BooleanField would be
        rendered with the string value 'False'. So when the preview form is
        resubmitted, the hash would be computed with the value 'False' for
        ``bool1``. We need to make sure the hashes are the same in both cases.

        """
        self.test_data.update({'stage':2})
        hash = self.preview.security_hash(None, TestForm(self.test_data))
        self.test_data.update({'hash':hash, 'bool1':u'False'})
        response = self.client.post('/test1/', self.test_data)
        self.assertEqual(response.content, success_string)

    def test_form_submit_django12_hash(self):
        """
        Test contrib.formtools.preview form submittal, using the hash function
        used in Django 1.2
        """
        # Pass strings for form submittal and add stage variable to
        # show we previously saw first stage of the form.
        self.test_data.update({'stage':2})
        response = self.client.post('/test1/', self.test_data)
        self.assertNotEqual(response.content, success_string)
        hash = utils.security_hash(None, TestForm(self.test_data))
        self.test_data.update({'hash': hash})
        response = self.client.post('/test1/', self.test_data)
        self.assertEqual(response.content, success_string)

    def test_form_submit_django12_hash_custom_hash(self):
        """
        Test contrib.formtools.preview form submittal, using the hash function
        used in Django 1.2 and a custom security_hash method.
        """
        # Pass strings for form submittal and add stage variable to
        # show we previously saw first stage of the form.
        self.test_data.update({'stage':2})
        response = self.client.post('/test2/', self.test_data)
        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.content, success_string)
        hash = utils.security_hash(None, TestForm(self.test_data))
        self.test_data.update({'hash': hash})
        response = self.client.post('/test2/', self.test_data)
        self.assertNotEqual(response.content, success_string)

class SecurityHashTests(unittest.TestCase):

    def test_textfield_hash(self):
        """
        Regression test for #10034: the hash generation function should ignore
        leading/trailing whitespace so as to be friendly to broken browsers that
        submit it (usually in textareas).
        """
        f1 = HashTestForm({'name': 'joe', 'bio': 'Nothing notable.'})
        f2 = HashTestForm({'name': '  joe', 'bio': 'Nothing notable.  '})
        hash1 = utils.security_hash(None, f1)
        hash2 = utils.security_hash(None, f2)
        self.assertEqual(hash1, hash2)

    def test_empty_permitted(self):
        """
        Regression test for #10643: the security hash should allow forms with
        empty_permitted = True, or forms where data has not changed.
        """
        f1 = HashTestBlankForm({})
        f2 = HashTestForm({}, empty_permitted=True)
        hash1 = utils.security_hash(None, f1)
        hash2 = utils.security_hash(None, f2)
        self.assertEqual(hash1, hash2)

class FormHmacTests(unittest.TestCase):
    """
    Same as SecurityHashTests, but with form_hmac
    """

    def test_textfield_hash(self):
        """
        Regression test for #10034: the hash generation function should ignore
        leading/trailing whitespace so as to be friendly to broken browsers that
        submit it (usually in textareas).
        """
        f1 = HashTestForm({'name': 'joe', 'bio': 'Nothing notable.'})
        f2 = HashTestForm({'name': '  joe', 'bio': 'Nothing notable.  '})
        hash1 = utils.form_hmac(f1)
        hash2 = utils.form_hmac(f2)
        self.assertEqual(hash1, hash2)

    def test_empty_permitted(self):
        """
        Regression test for #10643: the security hash should allow forms with
        empty_permitted = True, or forms where data has not changed.
        """
        f1 = HashTestBlankForm({})
        f2 = HashTestForm({}, empty_permitted=True)
        hash1 = utils.form_hmac(f1)
        hash2 = utils.form_hmac(f2)
        self.assertEqual(hash1, hash2)

class HashTestForm(forms.Form):
    name = forms.CharField()
    bio = forms.CharField()

class HashTestBlankForm(forms.Form):
    name = forms.CharField(required=False)
    bio = forms.CharField(required=False)

#
# FormWizard tests
#

class WizardPageOneForm(forms.Form):
    field = forms.CharField()

class WizardPageTwoForm(forms.Form):
    field = forms.CharField()

class WizardPageTwoAlternativeForm(forms.Form):
    field = forms.CharField()

class WizardPageThreeForm(forms.Form):
    field = forms.CharField()

class WizardClass(wizard.FormWizard):

    def get_template(self, step):
        return 'formwizard/wizard.html'

    def done(self, request, cleaned_data):
        return http.HttpResponse(success_string)

class UserSecuredWizardClass(WizardClass):
    """
    Wizard with a custum security_hash method
    """
    def security_hash(self, request, form):
        return "123"

class DummyRequest(http.HttpRequest):

    def __init__(self, POST=None):
        super(DummyRequest, self).__init__()
        self.method = POST and "POST" or "GET"
        if POST is not None:
            self.POST.update(POST)
        self._dont_enforce_csrf_checks = True

class WizardTests(TestCase):
    urls = 'django.contrib.formtools.tests.urls'

    def setUp(self):
        self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
        settings.TEMPLATE_DIRS = (
            os.path.join(
                os.path.dirname(__file__),
                'templates'
            ),
        )
        # Use a known SECRET_KEY to make security_hash tests deterministic
        self.old_SECRET_KEY = settings.SECRET_KEY
        settings.SECRET_KEY = "123"

    def tearDown(self):
        settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
        settings.SECRET_KEY = self.old_SECRET_KEY

    def test_step_starts_at_zero(self):
        """
        step should be zero for the first form
        """
        response = self.client.get('/wizard/')
        self.assertEquals(0, response.context['step0'])

    def test_step_increments(self):
        """
        step should be incremented when we go to the next page
        """
        response = self.client.post('/wizard/', {"0-field":"test", "wizard_step":"0"})
        self.assertEquals(1, response.context['step0'])

    def test_bad_hash(self):
        """
        Form should not advance if the hash is missing or bad
        """
        response = self.client.post('/wizard/',
                                    {"0-field":"test",
                                     "1-field":"test2",
                                     "wizard_step": "1"})
        self.assertEquals(0, response.context['step0'])

    def test_good_hash_django12(self):
        """
        Form should advance if the hash is present and good, as calculated using
        django 1.2 method.
        """
        # We are hard-coding a hash value here, but that is OK, since we want to
        # ensure that we don't accidentally change the algorithm.
        data = {"0-field": "test",
                "1-field": "test2",
                "hash_0": "2fdbefd4c0cad51509478fbacddf8b13",
                "wizard_step": "1"}
        response = self.client.post('/wizard/', data)
        self.assertEquals(2, response.context['step0'])

    def test_good_hash_django12_subclass(self):
        """
        The Django 1.2 method of calulating hashes should *not* be used as a
        fallback if the FormWizard subclass has provided their own method
        of calculating a hash.
        """
        # We are hard-coding a hash value here, but that is OK, since we want to
        # ensure that we don't accidentally change the algorithm.
        data = {"0-field": "test",
                "1-field": "test2",
                "hash_0": "2fdbefd4c0cad51509478fbacddf8b13",
                "wizard_step": "1"}
        response = self.client.post('/wizard2/', data)
        self.assertEquals(0, response.context['step0'])

    def test_good_hash_current(self):
        """
        Form should advance if the hash is present and good, as calculated using
        current method.
        """
        data = {"0-field": "test",
                "1-field": "test2",
                "hash_0": "7e9cea465f6a10a6fb47fcea65cb9a76350c9a5c",
                "wizard_step": "1"}
        response = self.client.post('/wizard/', data)
        self.assertEquals(2, response.context['step0'])

    def test_14498(self):
        """
        Regression test for ticket #14498.  All previous steps' forms should be
        validated.
        """
        that = self

        class WizardWithProcessStep(WizardClass):
            def process_step(self, request, form, step):
                that.assertTrue(hasattr(form, 'cleaned_data'))

        wizard = WizardWithProcessStep([WizardPageOneForm,
                                        WizardPageTwoForm,
                                        WizardPageThreeForm])
        data = {"0-field": "test",
                "1-field": "test2",
                "hash_0": "7e9cea465f6a10a6fb47fcea65cb9a76350c9a5c",
                "wizard_step": "1"}
        wizard(DummyRequest(POST=data))

    def test_14576(self):
        """
        Regression test for ticket #14576.

        The form of the last step is not passed to the done method.
        """
        reached = [False]
        that = self

        class Wizard(WizardClass):
            def done(self, request, form_list):
                reached[0] = True
                that.assertTrue(len(form_list) == 2)

        wizard = Wizard([WizardPageOneForm,
                         WizardPageTwoForm])

        data = {"0-field": "test",
                "1-field": "test2",
                "hash_0": "2fdbefd4c0cad51509478fbacddf8b13",
                "wizard_step": "1"}
        wizard(DummyRequest(POST=data))
        self.assertTrue(reached[0])

    def test_15075(self):
        """
        Regression test for ticket #15075.  Allow modifying wizard's form_list
        in process_step.
        """
        that = self

        class WizardWithProcessStep(WizardClass):
            def process_step(self, request, form, step):
                if step == 0:
                    self.form_list[1] = WizardPageTwoAlternativeForm
                if step == 1:
                    that.assertTrue(isinstance(form, WizardPageTwoAlternativeForm))

        wizard = WizardWithProcessStep([WizardPageOneForm,
                                        WizardPageTwoForm,
                                        WizardPageThreeForm])
        data = {"0-field": "test",
                "1-field": "test2",
                "hash_0": "7e9cea465f6a10a6fb47fcea65cb9a76350c9a5c",
                "wizard_step": "1"}
        wizard(DummyRequest(POST=data))
<commit_msg>Fixed some bugs with formtools tests.<commit_after>import os

from django import forms, http
from django.conf import settings
from django.contrib.formtools import preview, wizard, utils
from django.test import TestCase
from django.utils import unittest

success_string = "Done was called!"

class TestFormPreview(preview.FormPreview):
    def get_context(self, request, form):
        context = super(TestFormPreview, self).get_context(request, form)
        context.update({'custom_context': True})
        return context

    def get_initial(self, request):
        return {'field1': 'Works!'}

    def done(self, request, cleaned_data):
        return http.HttpResponse(success_string)

class TestForm(forms.Form):
    field1 = forms.CharField()
    field1_ = forms.CharField()
    bool1 = forms.BooleanField(required=False)

class UserSecuredFormPreview(TestFormPreview):
    """
    FormPreview with a custum security_hash method
    """
    def security_hash(self, request, form):
        return "123"

class PreviewTests(TestCase):
    urls = 'django.contrib.formtools.tests.urls'

    def setUp(self):
        # Create a FormPreview instance to share between tests
        self.preview = preview.FormPreview(TestForm)
        input_template = '<input type="hidden" name="%s" value="%s" />'
        self.input = input_template % (self.preview.unused_name('stage'), "%d")
        self.test_data = {'field1':u'foo', 'field1_':u'asdf'}

    def test_unused_name(self):
        """
        Verifies name mangling to get uniue field name.
        """
        self.assertEqual(self.preview.unused_name('field1'), 'field1__')

    def test_form_get(self):
        """
        Test contrib.formtools.preview form retrieval.

        Use the client library to see if we can sucessfully retrieve
        the form (mostly testing the setup ROOT_URLCONF
        process). Verify that an additional  hidden input field
        is created to manage the stage.

        """
        response = self.client.get('/test1/')
        stage = self.input % 1
        self.assertContains(response, stage, 1)
        self.assertEquals(response.context['custom_context'], True)
        self.assertEquals(response.context['form'].initial, {'field1': 'Works!'})

    def test_form_preview(self):
        """
        Test contrib.formtools.preview form preview rendering.

        Use the client library to POST to the form to see if a preview
        is returned.  If we do get a form back check that the hidden
        value is correctly managing the state of the form.

        """
        # Pass strings for form submittal and add stage variable to
        # show we previously saw first stage of the form.
        self.test_data.update({'stage': 1})
        response = self.client.post('/test1/', self.test_data)
        # Check to confirm stage is set to 2 in output form.
        stage = self.input % 2
        self.assertContains(response, stage, 1)

    def test_form_submit(self):
        """
        Test contrib.formtools.preview form submittal.

        Use the client library to POST to the form with stage set to 3
        to see if our forms done() method is called. Check first
        without the security hash, verify failure, retry with security
        hash and verify sucess.

        """
        # Pass strings for form submittal and add stage variable to
        # show we previously saw first stage of the form.
        self.test_data.update({'stage':2})
        response = self.client.post('/test1/', self.test_data)
        self.assertNotEqual(response.content, success_string)
        hash = self.preview.security_hash(None, TestForm(self.test_data))
        self.test_data.update({'hash': hash})
        response = self.client.post('/test1/', self.test_data)
        self.assertEqual(response.content, success_string)

    def test_bool_submit(self):
        """
        Test contrib.formtools.preview form submittal when form contains:
        BooleanField(required=False)

        Ticket: #6209 - When an unchecked BooleanField is previewed, the preview
        form's hash would be computed with no value for ``bool1``. However, when
        the preview form is rendered, the unchecked hidden BooleanField would be
        rendered with the string value 'False'. So when the preview form is
        resubmitted, the hash would be computed with the value 'False' for
        ``bool1``. We need to make sure the hashes are the same in both cases.

        """
        self.test_data.update({'stage':2})
        hash = self.preview.security_hash(None, TestForm(self.test_data))
        self.test_data.update({'hash':hash, 'bool1':u'False'})
        response = self.client.post('/test1/', self.test_data)
        self.assertEqual(response.content, success_string)

    def test_form_submit_django12_hash(self):
        """
        Test contrib.formtools.preview form submittal, using the hash function
        used in Django 1.2
        """
        # Pass strings for form submittal and add stage variable to
        # show we previously saw first stage of the form.
        self.test_data.update({'stage':2})
        response = self.client.post('/test1/', self.test_data)
        self.assertNotEqual(response.content, success_string)
        hash = utils.security_hash(None, TestForm(self.test_data))
        self.test_data.update({'hash': hash})
        response = self.client.post('/test1/', self.test_data)
        self.assertEqual(response.content, success_string)

    def test_form_submit_django12_hash_custom_hash(self):
        """
        Test contrib.formtools.preview form submittal, using the hash function
        used in Django 1.2 and a custom security_hash method.
        """
        # Pass strings for form submittal and add stage variable to
        # show we previously saw first stage of the form.
        self.test_data.update({'stage':2})
        response = self.client.post('/test2/', self.test_data)
        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.content, success_string)
        hash = utils.security_hash(None, TestForm(self.test_data))
        self.test_data.update({'hash': hash})
        response = self.client.post('/test2/', self.test_data)
        self.assertNotEqual(response.content, success_string)

class SecurityHashTests(unittest.TestCase):

    def test_textfield_hash(self):
        """
        Regression test for #10034: the hash generation function should ignore
        leading/trailing whitespace so as to be friendly to broken browsers that
        submit it (usually in textareas).
        """
        f1 = HashTestForm({'name': 'joe', 'bio': 'Nothing notable.'})
        f2 = HashTestForm({'name': '  joe', 'bio': 'Nothing notable.  '})
        hash1 = utils.security_hash(None, f1)
        hash2 = utils.security_hash(None, f2)
        self.assertEqual(hash1, hash2)

    def test_empty_permitted(self):
        """
        Regression test for #10643: the security hash should allow forms with
        empty_permitted = True, or forms where data has not changed.
        """
        f1 = HashTestBlankForm({})
        f2 = HashTestForm({}, empty_permitted=True)
        hash1 = utils.security_hash(None, f1)
        hash2 = utils.security_hash(None, f2)
        self.assertEqual(hash1, hash2)

class FormHmacTests(unittest.TestCase):
    """
    Same as SecurityHashTests, but with form_hmac
    """

    def test_textfield_hash(self):
        """
        Regression test for #10034: the hash generation function should ignore
        leading/trailing whitespace so as to be friendly to broken browsers that
        submit it (usually in textareas).
        """
        f1 = HashTestForm({'name': 'joe', 'bio': 'Nothing notable.'})
        f2 = HashTestForm({'name': '  joe', 'bio': 'Nothing notable.  '})
        hash1 = utils.form_hmac(f1)
        hash2 = utils.form_hmac(f2)
        self.assertEqual(hash1, hash2)

    def test_empty_permitted(self):
        """
        Regression test for #10643: the security hash should allow forms with
        empty_permitted = True, or forms where data has not changed.
        """
        f1 = HashTestBlankForm({})
        f2 = HashTestForm({}, empty_permitted=True)
        hash1 = utils.form_hmac(f1)
        hash2 = utils.form_hmac(f2)
        self.assertEqual(hash1, hash2)

class HashTestForm(forms.Form):
    name = forms.CharField()
    bio = forms.CharField()

class HashTestBlankForm(forms.Form):
    name = forms.CharField(required=False)
    bio = forms.CharField(required=False)

#
# FormWizard tests
#

class WizardPageOneForm(forms.Form):
    field = forms.CharField()

class WizardPageTwoForm(forms.Form):
    field = forms.CharField()

class WizardPageTwoAlternativeForm(forms.Form):
    field = forms.CharField()

class WizardPageThreeForm(forms.Form):
    field = forms.CharField()

class WizardClass(wizard.FormWizard):

    def get_template(self, step):
        return 'formwizard/wizard.html'

    def done(self, request, cleaned_data):
        return http.HttpResponse(success_string)

class UserSecuredWizardClass(WizardClass):
    """
    Wizard with a custum security_hash method
    """
    def security_hash(self, request, form):
        return "123"

class DummyRequest(http.HttpRequest):

    def __init__(self, POST=None):
        super(DummyRequest, self).__init__()
        self.method = POST and "POST" or "GET"
        if POST is not None:
            self.POST.update(POST)
        self._dont_enforce_csrf_checks = True

class WizardTests(TestCase):
    urls = 'django.contrib.formtools.tests.urls'

    def setUp(self):
        self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
        settings.TEMPLATE_DIRS = (
            os.path.join(
                os.path.dirname(__file__),
                'templates'
            ),
        )
        # Use a known SECRET_KEY to make security_hash tests deterministic
        self.old_SECRET_KEY = settings.SECRET_KEY
        settings.SECRET_KEY = "123"

    def tearDown(self):
        settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
        settings.SECRET_KEY = self.old_SECRET_KEY

    def test_step_starts_at_zero(self):
        """
        step should be zero for the first form
        """
        response = self.client.get('/wizard/')
        self.assertEquals(0, response.context['step0'])

    def test_step_increments(self):
        """
        step should be incremented when we go to the next page
        """
        response = self.client.post('/wizard/', {"0-field":"test", "wizard_step":"0"})
        self.assertEquals(1, response.context['step0'])

    def test_bad_hash(self):
        """
        Form should not advance if the hash is missing or bad
        """
        response = self.client.post('/wizard/',
                                    {"0-field":"test",
                                     "1-field":"test2",
                                     "wizard_step": "1"})
        self.assertEquals(0, response.context['step0'])

    def test_good_hash_django12(self):
        """
        Form should advance if the hash is present and good, as calculated using
        django 1.2 method.
        """
        # We are hard-coding a hash value here, but that is OK, since we want to
        # ensure that we don't accidentally change the algorithm.
        data = {"0-field": "test",
                "1-field": "test2",
                "hash_0": "2fdbefd4c0cad51509478fbacddf8b13",
                "wizard_step": "1"}
        response = self.client.post('/wizard/', data)
        self.assertEquals(2, response.context['step0'])

    def test_good_hash_django12_subclass(self):
        """
        The Django 1.2 method of calulating hashes should *not* be used as a
        fallback if the FormWizard subclass has provided their own method
        of calculating a hash.
        """
        # We are hard-coding a hash value here, but that is OK, since we want to
        # ensure that we don't accidentally change the algorithm.
        data = {"0-field": "test",
                "1-field": "test2",
                "hash_0": "2fdbefd4c0cad51509478fbacddf8b13",
                "wizard_step": "1"}
        response = self.client.post('/wizard2/', data)
        self.assertEquals(0, response.context['step0'])

    def test_good_hash_current(self):
        """
        Form should advance if the hash is present and good, as calculated using
        current method.
        """
        data = {"0-field": "test",
                "1-field": "test2",
                "hash_0": "7e9cea465f6a10a6fb47fcea65cb9a76350c9a5c",
                "wizard_step": "1"}
        response = self.client.post('/wizard/', data)
        self.assertEquals(2, response.context['step0'])

    def test_14498(self):
        """
        Regression test for ticket #14498.  All previous steps' forms should be
        validated.
        """
        reached = [False]
        that = self

        class WizardWithProcessStep(WizardClass):
            def process_step(self, request, form, step):
                that.assertTrue(hasattr(form, 'cleaned_data'))
                reached[0] = True

        wizard = WizardWithProcessStep([WizardPageOneForm,
                                        WizardPageTwoForm,
                                        WizardPageThreeForm])
        data = {"0-field": "test",
                "1-field": "test2",
                "hash_0": "7e9cea465f6a10a6fb47fcea65cb9a76350c9a5c",
                "wizard_step": "1"}
        wizard(DummyRequest(POST=data))
        self.assertTrue(reached[0])

    def test_14576(self):
        """
        Regression test for ticket #14576.

        The form of the last step is not passed to the done method.
        """
        reached = [False]
        that = self

        class Wizard(WizardClass):
            def done(self, request, form_list):
                reached[0] = True
                that.assertTrue(len(form_list) == 2)

        wizard = Wizard([WizardPageOneForm,
                         WizardPageTwoForm])

        data = {"0-field": "test",
                "1-field": "test2",
                "hash_0": "2fdbefd4c0cad51509478fbacddf8b13",
                "wizard_step": "1"}
        wizard(DummyRequest(POST=data))
        self.assertTrue(reached[0])

    def test_15075(self):
        """
        Regression test for ticket #15075.  Allow modifying wizard's form_list
        in process_step.
        """
        reached = [False]
        that = self

        class WizardWithProcessStep(WizardClass):
            def process_step(self, request, form, step):
                if step == 0:
                    self.form_list[1] = WizardPageTwoAlternativeForm
                if step == 1:
                    that.assertTrue(isinstance(form, WizardPageTwoAlternativeForm))
                    reached[0] = True

        wizard = WizardWithProcessStep([WizardPageOneForm,
                                        WizardPageTwoForm,
                                        WizardPageThreeForm])
        data = {"0-field": "test",
                "1-field": "test2",
                "hash_0": "7e9cea465f6a10a6fb47fcea65cb9a76350c9a5c",
                "wizard_step": "1"}
        wizard(DummyRequest(POST=data))
        self.assertTrue(reached[0])
<|endoftext|>


<commit_before>"""
The ``basic_example`` module shows a simple usage of the windpowerlib.

"""

__copyright__ = "Copyright oemof developer group"
__license__ = "GPLv3"

import os
import pandas as pd

try:
    from matplotlib import pyplot as plt
except ImportError:
    plt = None

from windpowerlib import modelchain
from windpowerlib import wind_turbine as wt

# You can use the logging package to get logging messages from the windpowerlib
# Change the logging level if you want more or less messages
import logging
logging.getLogger().setLevel(logging.DEBUG)

def get_weather_data(filename, datetime_column='time_index', **kwargs):
    r"""
    Imports weather data from a file and specifies height of weather data.

    The data include wind speed at two different heights in m/s, air
    temperature in two different heights in K, surface roughness length in m
    and air pressure in Pa.

    Parameters
    ----------
    filename : string
        Filename of the weather data file.
    datetime_column : string
            Name of the datetime column of the weather DataFrame.
            Default: 'time_index'.

    Other Parameters
    ----------------
    datapath : string, optional
        Path where the weather data file is stored.
        Default: 'windpowerlib/example'.

    Returns
    -------
    Tuple (pd.DataFrame, dictionary)
        `weather` DataFrame contains weather data time series.
        `data_height` dictionary contains height for which corresponding
        weather data applies.

    """
    def read_weather_data(filename, datetime_column='time_index',
                          **kwargs):
        r"""
        Fetches weather data from a file.

        The file is located in the example folder of the windpowerlib.

        Parameters
        ----------
        filename : string
            Filename of the weather data file.
        datetime_column : string
            Name of the datetime column of the weather DataFrame.

        Other Parameters
        ----------------
        datapath : string, optional
            Path where the weather data file is stored.
            Default: 'windpowerlib/example'.

        Returns
        -------
        pandas.DataFrame
            Contains weather data time series.

        """
        if 'datapath' not in kwargs:
            kwargs['datapath'] = os.path.join(os.path.split(
                os.path.dirname(__file__))[0], 'example')

        file = os.path.join(kwargs['datapath'], filename)
        df = pd.read_csv(file)
        return df.set_index(pd.to_datetime(df[datetime_column])).tz_localize(
            'UTC').tz_convert('Europe/Berlin').drop(datetime_column, 1)

    # read weather data from csv
    weather = read_weather_data(filename=filename,
                                datetime_column=datetime_column, **kwargs)

    # dictionary specifying the height for which the weather data applies
    # data in m
    data_height = {
        'pressure': 0,
        'temp_air': 2,
        'v_wind': 10,
        'temp_air_2': 10,
        'v_wind_2': 80}

    return (weather, data_height)

def initialise_wind_turbines():
    r"""
    Initialises two :class:`~.wind_turbine.WindTurbine` objects.

    Function shows two ways to initialise a WindTurbine object. You can either
    specify your own turbine, as done below for 'myTurbine', or fetch power
    and/or power coefficient curve data from data files provided by the
    windpowerlib, as done for the 'enerconE126'.
    Execute ``windpowerlib.wind_turbine.get_turbine_types()`` or
    ``windpowerlib.wind_turbine.get_turbine_types(filename='cp_curves.csv')``
    to get a list of all wind turbines for which power and power coefficient
    curves respectively are provided.

    Returns
    -------
    Tuple (WindTurbine, WindTurbine)

    """

    # specification of own wind turbine (Note: power coefficient values and
    # nominal power have to be in Watt)
    myTurbine = {
        'turbine_name': 'myTurbine',
        'nominal_power': 3e6,  # in W
        'hub_height': 105,  # in m
        'd_rotor': 90,  # in m
        'p_values': pd.DataFrame(
            data={'p': [p * 1000 for p in
                        [0.0, 26.0, 180.0, 1500.0, 3000.0, 3000.0]]},  # in W
            index=[0.0, 3.0, 5.0, 10.0, 15.0, 25.0])  # in m/s
    }
    # initialise WindTurbine object
    my_turbine = wt.WindTurbine(**myTurbine)

    # specification of wind turbine where power curve is provided
    # if you want to use the power coefficient curve add {'fetch_curve': 'cp'}
    # to the dictionary
    enerconE126 = {
        'turbine_name': 'ENERCON E 126 7500',  # turbine name as in register
        'hub_height': 135,  # in m
        'd_rotor': 127  # in m
    }
    # initialise WindTurbine object
    e126 = wt.WindTurbine(**enerconE126)

    return (my_turbine, e126)

def calculate_power_output(weather, data_height, my_turbine, e126):
    r"""
    Calculates power output of wind turbines using the
    :class:`~.modelchain.ModelChain`.

    The :class:`~.modelchain.ModelChain` is a class that provides all necessary
    steps to calculate the power output of a wind turbine. You can either use
    the default methods for the calculation steps, as done for 'my_turbine',
    or choose different methods, as done for the 'e126'.

    Parameters
    ----------
    weather : pd.DataFrame
        Contains weather data time series.
    data_height : dictionary
        Contains height for which corresponding weather data applies.
    my_turbine : WindTurbine
        WindTurbine object with self provided power curve.
    e126 : WindTurbine
        WindTurbine object with power curve from data file provided by the
        windpowerlib.

    """

    # power output calculation for my_turbine
    # initialise ModelChain with default parameters and use run_model method
    # to calculate power output
    mc_my_turbine = modelchain.ModelChain(my_turbine).run_model(
        weather, data_height)
    # write power output timeseries to WindTurbine object
    my_turbine.power_output = mc_my_turbine.power_output

    # power output calculation for e126
    # own specifications for ModelChain setup
    modelchain_data = {
        'obstacle_height': 0,  # default: 0
        'wind_model': 'logarithmic',  # 'logarithmic' (default) or 'hellman'
        'rho_model': 'ideal_gas',  # 'barometric' (default) or 'ideal_gas'
        'power_output_model': 'p_values',  # 'p_values' (default) or
                                           # 'cp_values'
        'density_corr': True,  # False (default) or True
        'hellman_exp': None}  # None (default) or None
    # initialise ModelChain with own specifications and use run_model method
    # to calculate power output
    mc_e126 = modelchain.ModelChain(e126, **modelchain_data).run_model(
        weather, data_height)
    # write power output timeseries to WindTurbine object
    e126.power_output = mc_e126.power_output

    return

def plot_or_print(my_turbine, e126):
    r"""
    Plots or prints power output and power (coefficient) curves.

    Parameters
    ----------
    my_turbine : WindTurbine
        WindTurbine object with self provided power curve.
    e126 : WindTurbine
        WindTurbine object with power curve from data file provided by the
        windpowerlib.

    """

    # plot or print turbine power output
    if plt:
        e126.power_output.plot(legend=True, label='Enercon E126')
        my_turbine.power_output.plot(legend=True, label='myTurbine')
        plt.show()
    else:
        print(e126.power_output)
        print(my_turbine.power_output)

    # plot or print power (coefficient) curve
    if plt:
        if e126.cp_values is not None:
            e126.cp_values.plot(style='*', title='Enercon E126')
            plt.show()
        if e126.p_values is not None:
            e126.p_values.plot(style='*', title='Enercon E126')
            plt.show()
        if my_turbine.cp_values is not None:
            my_turbine.cp_values.plot(style='*', title='myTurbine')
            plt.show()
        if my_turbine.p_values is not None:
            my_turbine.p_values.plot(style='*', title='myTurbine')
            plt.show()
    else:
        if e126.cp_values is not None:
            print(e126.cp_values)
        if e126.p_values is not None:
            print("The P value at a wind speed of 5 m/s: {0}".format(
                e126.p_values.P[5.0]))

def run_basic_example():
    r"""
    Run the basic example.

    """
    weather, data_height = get_weather_data('weather.csv')
    my_turbine, e126 = initialise_wind_turbines()
    calculate_power_output(weather, data_height, my_turbine, e126)
    plot_or_print(my_turbine, e126)

if __name__ == "__main__":
    run_basic_example()<commit_msg>Change p_values.P to p_values.p<commit_after>"""
The ``basic_example`` module shows a simple usage of the windpowerlib.

"""

__copyright__ = "Copyright oemof developer group"
__license__ = "GPLv3"

import os
import pandas as pd

try:
    from matplotlib import pyplot as plt
except ImportError:
    plt = None

from windpowerlib import modelchain
from windpowerlib import wind_turbine as wt

# You can use the logging package to get logging messages from the windpowerlib
# Change the logging level if you want more or less messages
import logging
logging.getLogger().setLevel(logging.DEBUG)

def get_weather_data(filename, datetime_column='time_index', **kwargs):
    r"""
    Imports weather data from a file and specifies height of weather data.

    The data include wind speed at two different heights in m/s, air
    temperature in two different heights in K, surface roughness length in m
    and air pressure in Pa.

    Parameters
    ----------
    filename : string
        Filename of the weather data file.
    datetime_column : string
            Name of the datetime column of the weather DataFrame.
            Default: 'time_index'.

    Other Parameters
    ----------------
    datapath : string, optional
        Path where the weather data file is stored.
        Default: 'windpowerlib/example'.

    Returns
    -------
    Tuple (pd.DataFrame, dictionary)
        `weather` DataFrame contains weather data time series.
        `data_height` dictionary contains height for which corresponding
        weather data applies.

    """
    def read_weather_data(filename, datetime_column='time_index',
                          **kwargs):
        r"""
        Fetches weather data from a file.

        The file is located in the example folder of the windpowerlib.

        Parameters
        ----------
        filename : string
            Filename of the weather data file.
        datetime_column : string
            Name of the datetime column of the weather DataFrame.

        Other Parameters
        ----------------
        datapath : string, optional
            Path where the weather data file is stored.
            Default: 'windpowerlib/example'.

        Returns
        -------
        pandas.DataFrame
            Contains weather data time series.

        """
        if 'datapath' not in kwargs:
            kwargs['datapath'] = os.path.join(os.path.split(
                os.path.dirname(__file__))[0], 'example')

        file = os.path.join(kwargs['datapath'], filename)
        df = pd.read_csv(file)
        return df.set_index(pd.to_datetime(df[datetime_column])).tz_localize(
            'UTC').tz_convert('Europe/Berlin').drop(datetime_column, 1)

    # read weather data from csv
    weather = read_weather_data(filename=filename,
                                datetime_column=datetime_column, **kwargs)

    # dictionary specifying the height for which the weather data applies
    # data in m
    data_height = {
        'pressure': 0,
        'temp_air': 2,
        'v_wind': 10,
        'temp_air_2': 10,
        'v_wind_2': 80}

    return (weather, data_height)

def initialise_wind_turbines():
    r"""
    Initialises two :class:`~.wind_turbine.WindTurbine` objects.

    Function shows two ways to initialise a WindTurbine object. You can either
    specify your own turbine, as done below for 'myTurbine', or fetch power
    and/or power coefficient curve data from data files provided by the
    windpowerlib, as done for the 'enerconE126'.
    Execute ``windpowerlib.wind_turbine.get_turbine_types()`` or
    ``windpowerlib.wind_turbine.get_turbine_types(filename='cp_curves.csv')``
    to get a list of all wind turbines for which power and power coefficient
    curves respectively are provided.

    Returns
    -------
    Tuple (WindTurbine, WindTurbine)

    """

    # specification of own wind turbine (Note: power coefficient values and
    # nominal power have to be in Watt)
    myTurbine = {
        'turbine_name': 'myTurbine',
        'nominal_power': 3e6,  # in W
        'hub_height': 105,  # in m
        'd_rotor': 90,  # in m
        'p_values': pd.DataFrame(
            data={'p': [p * 1000 for p in
                        [0.0, 26.0, 180.0, 1500.0, 3000.0, 3000.0]]},  # in W
            index=[0.0, 3.0, 5.0, 10.0, 15.0, 25.0])  # in m/s
    }
    # initialise WindTurbine object
    my_turbine = wt.WindTurbine(**myTurbine)

    # specification of wind turbine where power curve is provided
    # if you want to use the power coefficient curve add {'fetch_curve': 'cp'}
    # to the dictionary
    enerconE126 = {
        'turbine_name': 'ENERCON E 126 7500',  # turbine name as in register
        'hub_height': 135,  # in m
        'd_rotor': 127  # in m
    }
    # initialise WindTurbine object
    e126 = wt.WindTurbine(**enerconE126)

    return (my_turbine, e126)

def calculate_power_output(weather, data_height, my_turbine, e126):
    r"""
    Calculates power output of wind turbines using the
    :class:`~.modelchain.ModelChain`.

    The :class:`~.modelchain.ModelChain` is a class that provides all necessary
    steps to calculate the power output of a wind turbine. You can either use
    the default methods for the calculation steps, as done for 'my_turbine',
    or choose different methods, as done for the 'e126'.

    Parameters
    ----------
    weather : pd.DataFrame
        Contains weather data time series.
    data_height : dictionary
        Contains height for which corresponding weather data applies.
    my_turbine : WindTurbine
        WindTurbine object with self provided power curve.
    e126 : WindTurbine
        WindTurbine object with power curve from data file provided by the
        windpowerlib.

    """

    # power output calculation for my_turbine
    # initialise ModelChain with default parameters and use run_model method
    # to calculate power output
    mc_my_turbine = modelchain.ModelChain(my_turbine).run_model(
        weather, data_height)
    # write power output timeseries to WindTurbine object
    my_turbine.power_output = mc_my_turbine.power_output

    # power output calculation for e126
    # own specifications for ModelChain setup
    modelchain_data = {
        'obstacle_height': 0,  # default: 0
        'wind_model': 'logarithmic',  # 'logarithmic' (default) or 'hellman'
        'rho_model': 'ideal_gas',  # 'barometric' (default) or 'ideal_gas'
        'power_output_model': 'p_values',  # 'p_values' (default) or
                                           # 'cp_values'
        'density_corr': True,  # False (default) or True
        'hellman_exp': None}  # None (default) or None
    # initialise ModelChain with own specifications and use run_model method
    # to calculate power output
    mc_e126 = modelchain.ModelChain(e126, **modelchain_data).run_model(
        weather, data_height)
    # write power output timeseries to WindTurbine object
    e126.power_output = mc_e126.power_output

    return

def plot_or_print(my_turbine, e126):
    r"""
    Plots or prints power output and power (coefficient) curves.

    Parameters
    ----------
    my_turbine : WindTurbine
        WindTurbine object with self provided power curve.
    e126 : WindTurbine
        WindTurbine object with power curve from data file provided by the
        windpowerlib.

    """

    # plot or print turbine power output
    if plt:
        e126.power_output.plot(legend=True, label='Enercon E126')
        my_turbine.power_output.plot(legend=True, label='myTurbine')
        plt.show()
    else:
        print(e126.power_output)
        print(my_turbine.power_output)

    # plot or print power (coefficient) curve
    if plt:
        if e126.cp_values is not None:
            e126.cp_values.plot(style='*', title='Enercon E126')
            plt.show()
        if e126.p_values is not None:
            e126.p_values.plot(style='*', title='Enercon E126')
            plt.show()
        if my_turbine.cp_values is not None:
            my_turbine.cp_values.plot(style='*', title='myTurbine')
            plt.show()
        if my_turbine.p_values is not None:
            my_turbine.p_values.plot(style='*', title='myTurbine')
            plt.show()
    else:
        if e126.cp_values is not None:
            print(e126.cp_values)
        if e126.p_values is not None:
            print("The P value at a wind speed of 5 m/s: {0}".format(
                e126.p_values.p[5.0]))

def run_basic_example():
    r"""
    Run the basic example.

    """
    weather, data_height = get_weather_data('weather.csv')
    my_turbine, e126 = initialise_wind_turbines()
    calculate_power_output(weather, data_height, my_turbine, e126)
    plot_or_print(my_turbine, e126)

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


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

# Hive Appier Framework
# Copyright (C) 2008-2012 Hive Solutions Lda.
#
# This file is part of Hive Appier Framework.
#
# Hive Appier Framework 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.
#
# Hive Appier Framework 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 Hive Appier Framework. If not, see <http://www.gnu.org/licenses/>.

__author__ = "João Magalhães joamag@hive.pt>"
""" The author(s) of the module """

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2012 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "GNU General Public License (GPL), Version 3"
""" The license for the module """

import util

class Observable(object):
    """
    The base class that implements the observable
    patter allowing the object to handle a series of
    event in a dynamic fashion.

    The class should be able to handle both context
    based event (instance level) and global level
    events (class level).

    This class should be friendly to multiple inheritance
    and should avoid variable naming collision.
    """

    _events_g = {}
    """ The dictionary containing the global association
    between the global event names and the handler methods """

    def __init__(self, *args, **kwargs):
        self._events = {}

    @classmethod
    def name_f(cls, name):
        cls_name = cls.__name__
        cls_name = util.camel_to_underscore(cls_name)
        name_f = cls_name + "." + name
        return name_f

    @classmethod
    def bind_g(cls, name, method):
        name_f = cls.name_f(name)
        methods = cls._events_g.get(name_f, [])
        methods.append(method)
        cls._events_g[name_f] = methods

    @classmethod
    def unbind_g(cls, name, method = None):
        name_f = cls.name_f(name)
        methods = cls._events_g.get(name_f, [])
        if method: methods.remove(method)
        else: del methods[:]

    @classmethod
    def trigger_g(cls, name, *args, **kwargs):
        name_f = cls.name_f(name)
        methods = cls._events_g.get(name_f, [])
        for method in methods: method(*args, **kwargs)

    def bind(self, name, method):
        methods = self._events.get(name, [])
        methods.append(method)
        self._events[name] = methods

    def unbind(self, name, method = None):
        methods = self._events.get(name, [])
        if method: methods.remove(method)
        else: del methods[:]

    def trigger(self, name, *args, **kwargs):
        cls = self.__class__
        self.trigger_l(name, cls, *args, **kwargs)

    def trigger_l(self, name, level, *args, **kwargs):
        methods = self._events.get(name, [])
        for method in methods: method(*args, **kwargs)
        level.trigger_g(name, self, *args, **kwargs)
<commit_msg>new unbind all support<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

# Hive Appier Framework
# Copyright (C) 2008-2012 Hive Solutions Lda.
#
# This file is part of Hive Appier Framework.
#
# Hive Appier Framework 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.
#
# Hive Appier Framework 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 Hive Appier Framework. If not, see <http://www.gnu.org/licenses/>.

__author__ = "João Magalhães joamag@hive.pt>"
""" The author(s) of the module """

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2012 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "GNU General Public License (GPL), Version 3"
""" The license for the module """

import util

class Observable(object):
    """
    The base class that implements the observable
    patter allowing the object to handle a series of
    event in a dynamic fashion.

    The class should be able to handle both context
    based event (instance level) and global level
    events (class level).

    This class should be friendly to multiple inheritance
    and should avoid variable naming collision.
    """

    _events_g = {}
    """ The dictionary containing the global association
    between the global event names and the handler methods """

    def __init__(self, *args, **kwargs):
        self._events = {}

    def __del__(self):
        self.unbind_all()

    @classmethod
    def name_f(cls, name):
        cls_name = cls.__name__
        cls_name = util.camel_to_underscore(cls_name)
        name_f = cls_name + "." + name
        return name_f

    @classmethod
    def bind_g(cls, name, method):
        name_f = cls.name_f(name)
        methods = cls._events_g.get(name_f, [])
        methods.append(method)
        cls._events_g[name_f] = methods

    @classmethod
    def unbind_g(cls, name, method = None):
        name_f = cls.name_f(name)
        methods = cls._events_g.get(name_f, [])
        if method: methods.remove(method)
        else: del methods[:]

    @classmethod
    def trigger_g(cls, name, *args, **kwargs):
        name_f = cls.name_f(name)
        methods = cls._events_g.get(name_f, [])
        for method in methods: method(*args, **kwargs)

    def build(self):
        pass

    def destroy(self):
        self.unbind_all()

    def bind(self, name, method):
        methods = self._events.get(name, [])
        methods.append(method)
        self._events[name] = methods

    def unbind(self, name, method = None):
        methods = self._events.get(name, [])
        if method: methods.remove(method)
        else: del methods[:]

    def unbind_all(self):
        for methods in self._events.itervalues(): del methods[:]
        self._events.clear()

    def trigger(self, name, *args, **kwargs):
        cls = self.__class__
        self.trigger_l(name, cls, *args, **kwargs)

    def trigger_l(self, name, level, *args, **kwargs):
        methods = self._events.get(name, [])
        for method in methods: method(*args, **kwargs)
        level.trigger_g(name, self, *args, **kwargs)
<|endoftext|>


<commit_before>#! /usr/bin/env python
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Generates model predictions.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from pydoc import locate

import os
import yaml
import seq2seq

from six import string_types

import tensorflow as tf
from tensorflow import gfile

from seq2seq import tasks, models
from seq2seq.contrib.monitored_session import MonitoredTrainingSession
from seq2seq.configurable import _maybe_load_yaml, _deep_merge_dict
from seq2seq.data import input_pipeline
from seq2seq.inference import create_inference_graph
from seq2seq.training import utils as training_utils

tf.flags.DEFINE_string("tasks", "{}", "List of inference tasks to run.")
tf.flags.DEFINE_string("model_params", "{}", """Optionally overwrite model
                        parameters for inference""")

tf.flags.DEFINE_integer("num_threads", None,"num threads[None]")
tf.flags.DEFINE_string("config_path", None,
                       """Path to a YAML configuration file defining FLAG
                       values and hyperparameters. Refer to the documentation
                       for more details.""")

tf.flags.DEFINE_string("input_pipeline", None,
                       """Defines how input data should be loaded.
                       A YAML string.""")

tf.flags.DEFINE_string("model_dir", None, "directory to load model from")
tf.flags.DEFINE_string("checkpoint_path", None,
                       """Full path to the checkpoint to be loaded. If None,
                       the latest checkpoint in the model dir is used.""")
tf.flags.DEFINE_integer("batch_size", 32, "the train/dev batch size")
tf.flags.DEFINE_string("save_pred_path", None, "save pred path[None], None is print only")

FLAGS = tf.flags.FLAGS

def main(_argv):
  """Program entry point.
  """

  # Load flags from config file
  if FLAGS.config_path:
    with gfile.GFile(FLAGS.config_path) as config_file:
      config_flags = yaml.load(config_file)
      for flag_key, flag_value in config_flags.items():
        setattr(FLAGS, flag_key, flag_value)

  if isinstance(FLAGS.tasks, string_types):
    FLAGS.tasks = _maybe_load_yaml(FLAGS.tasks)

  if isinstance(FLAGS.input_pipeline, string_types):
    FLAGS.input_pipeline = _maybe_load_yaml(FLAGS.input_pipeline)

  input_pipeline_infer = input_pipeline.make_input_pipeline_from_def(
      FLAGS.input_pipeline, mode=tf.contrib.learn.ModeKeys.INFER,
      shuffle=False, num_epochs=1)

  # Load saved training options
  train_options = training_utils.TrainOptions.load(FLAGS.model_dir)

  # Create the model
  model_cls = locate(train_options.model_class) or \
    getattr(models, train_options.model_class)
  model_params = train_options.model_params
  model_params = _deep_merge_dict(
      model_params, _maybe_load_yaml(FLAGS.model_params))
  model = model_cls(
      params=model_params,
      mode=tf.contrib.learn.ModeKeys.INFER)

  #get static global steps from checkpoint path
  ckpt = tf.train.get_checkpoint_state(FLAGS.model_dir)
  global_steps = int(os.path.basename(ckpt.model_checkpoint_path).split('-')[1])
  if FLAGS.save_pred_path is not None:
    FLAGS.save_pred_path = FLAGS.save_pred_path + "." + str(global_steps)
  # Load inference tasks
  hooks = []
  for tdict in FLAGS.tasks:
    if not "params" in tdict:
      tdict["params"] = {}
    if tdict["class"] == "DecodeText":
      tdict["params"]["save_pred_path"] = FLAGS.save_pred_path
    task_cls = locate(tdict["class"]) or getattr(tasks, tdict["class"])
    task = task_cls(tdict["params"])
    hooks.append(task)

  # Create the graph used for inference
  predictions, _, _ = create_inference_graph(
      model=model,
      input_pipeline=input_pipeline_infer,
      batch_size=FLAGS.batch_size)

  saver = tf.train.Saver()
  checkpoint_path = FLAGS.checkpoint_path
  if not checkpoint_path:
    checkpoint_path = tf.train.latest_checkpoint(FLAGS.model_dir)

  def session_init_op(_scaffold, sess):
    saver.restore(sess, checkpoint_path)
    tf.logging.info("Restored model from %s", checkpoint_path)

  scaffold = tf.train.Scaffold(init_fn=session_init_op)
  session_creator = tf.train.ChiefSessionCreator(scaffold=scaffold,config=tf.ConfigProto(intra_op_parallelism_threads=FLAGS.num_threads))
  with MonitoredSession(
      session_creator=session_creator,
      hooks=hooks) as sess:

    # Run until the inputs are exhausted
    while not sess.should_stop():
      sess.run([])

if __name__ == "__main__":
  tf.logging.set_verbosity(tf.logging.INFO)
  tf.app.run()
<commit_msg>fix infert model<commit_after>#! /usr/bin/env python
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Generates model predictions.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from pydoc import locate

import os
import yaml
import seq2seq

from six import string_types

import tensorflow as tf
from tensorflow import gfile

from seq2seq import tasks, models
from seq2seq.contrib.monitored_session import MonitoredTrainingSession
from seq2seq.configurable import _maybe_load_yaml, _deep_merge_dict
from seq2seq.data import input_pipeline
from seq2seq.inference import create_inference_graph
from seq2seq.training import utils as training_utils

tf.flags.DEFINE_string("tasks", "{}", "List of inference tasks to run.")
tf.flags.DEFINE_string("model_params", "{}", """Optionally overwrite model
                        parameters for inference""")

tf.flags.DEFINE_integer("num_threads", None,"num threads[None]")
tf.flags.DEFINE_string("config_path", None,
                       """Path to a YAML configuration file defining FLAG
                       values and hyperparameters. Refer to the documentation
                       for more details.""")

tf.flags.DEFINE_string("input_pipeline", None,
                       """Defines how input data should be loaded.
                       A YAML string.""")

tf.flags.DEFINE_string("model_dir", None, "directory to load model from")
tf.flags.DEFINE_string("checkpoint_path", None,
                       """Full path to the checkpoint to be loaded. If None,
                       the latest checkpoint in the model dir is used.""")
tf.flags.DEFINE_integer("batch_size", 32, "the train/dev batch size")
tf.flags.DEFINE_string("save_pred_path", None, "save pred path[None], None is print only")

FLAGS = tf.flags.FLAGS

def main(_argv):
  """Program entry point.
  """

  # Load flags from config file
  if FLAGS.config_path:
    with gfile.GFile(FLAGS.config_path) as config_file:
      config_flags = yaml.load(config_file)
      for flag_key, flag_value in config_flags.items():
        setattr(FLAGS, flag_key, flag_value)

  if isinstance(FLAGS.tasks, string_types):
    FLAGS.tasks = _maybe_load_yaml(FLAGS.tasks)

  if isinstance(FLAGS.input_pipeline, string_types):
    FLAGS.input_pipeline = _maybe_load_yaml(FLAGS.input_pipeline)

  input_pipeline_infer = input_pipeline.make_input_pipeline_from_def(
      FLAGS.input_pipeline, mode=tf.contrib.learn.ModeKeys.INFER,
      shuffle=False, num_epochs=1)

  # Load saved training options
  train_options = training_utils.TrainOptions.load(FLAGS.model_dir)

  # Create the model
  model_cls = locate(train_options.model_class) or \
    getattr(models, train_options.model_class)
  model_params = train_options.model_params
  model_params = _deep_merge_dict(
      model_params, _maybe_load_yaml(FLAGS.model_params))
  model = model_cls(
      params=model_params,
      mode=tf.contrib.learn.ModeKeys.INFER)

  #get static global steps from checkpoint path
  ckpt = tf.train.get_checkpoint_state(FLAGS.model_dir)
  global_steps = int(os.path.basename(ckpt.model_checkpoint_path).split('-')[1])
  if FLAGS.save_pred_path is not None:
    FLAGS.save_pred_path = FLAGS.save_pred_path + "." + str(global_steps)
  # Load inference tasks
  hooks = []
  for tdict in FLAGS.tasks:
    if not "params" in tdict:
      tdict["params"] = {}
    if tdict["class"] == "DecodeText":
      tdict["params"]["save_pred_path"] = FLAGS.save_pred_path
    task_cls = locate(tdict["class"]) or getattr(tasks, tdict["class"])
    task = task_cls(tdict["params"])
    hooks.append(task)

  # Create the graph used for inference
  predictions, _, _ = create_inference_graph(
      model=model,
      input_pipeline=input_pipeline_infer,
      batch_size=FLAGS.batch_size)

  saver = tf.train.Saver()
  checkpoint_path = FLAGS.checkpoint_path
  if not checkpoint_path:
    checkpoint_path = tf.train.latest_checkpoint(FLAGS.model_dir)

  def session_init_op(_scaffold, sess):
    saver.restore(sess, checkpoint_path)
    tf.logging.info("Restored model from %s", checkpoint_path)

  scaffold = tf.train.Scaffold(init_fn=session_init_op)
  session_creator = tf.train.ChiefSessionCreator(scaffold=scaffold,config=tf.ConfigProto(intra_op_parallelism_threads=FLAGS.num_threads))
  with tf.train.MonitoredSession(
      session_creator=session_creator,
      hooks=hooks) as sess:

    # Run until the inputs are exhausted
    while not sess.should_stop():
      sess.run([])

if __name__ == "__main__":
  tf.logging.set_verbosity(tf.logging.INFO)
  tf.app.run()
<|endoftext|>


<commit_before>from myhdl import Signal, delay, always, now, Simulation

def HelloWorld():

    interval = delay(10)

    @always(interval)
    def sayHello():
        print "%s Hello World!" % now()

    return sayHello

def main():
    inst = HelloWorld()
    sim = Simulation(inst)
    sim.run(30)

if __name__ == '__main__':
    main()

<commit_msg>hello1.py: print function<commit_after>from myhdl import Signal, delay, always, now, Simulation

def HelloWorld():

    interval = delay(10)

    @always(interval)
    def sayHello():
        print("%s Hello World!" % now())

    return sayHello

def main():
    inst = HelloWorld()
    sim = Simulation(inst)
    sim.run(30)

if __name__ == '__main__':
    main()

<|endoftext|>


<commit_before># coding: utf-8
from __future__ import print_function

import numpy as np

from devito.at_controller import AutoTuner
from devito.dimension import Dimension
from devito.interfaces import DenseData, TimeData
from examples.acoustic.fwi_operators import *
from examples.source_type import SourceLike

class Acoustic_cg(object):
    """
    Class to setup the problem for the Acoustic Wave.

    Note: s_order must always be greater than t_order
    """
    def __init__(self, model, data, source, nbpml=40, t_order=2, s_order=2,
                 auto_tuning=False, dse=True, dle='advanced', compiler=None,
                 legacy=True):
        self.model = model
        self.t_order = t_order
        self.s_order = s_order
        self.data = data
        self.source = source
        self.dtype = np.float32
        # Time step can be \sqrt{3}=1.73 bigger with 4th order
        self.dt = self.model.critical_dt
        if self.t_order == 4:
            self.dt *= 1.73

        if auto_tuning and legacy:  # auto tuning with dummy forward operator
            nt, nrec = self.data.shape
            nsrc = self.source.shape[1]
            ndim = len(self.model.shape)
            h = self.model.get_spacing()
            dtype = self.model.dtype
            nbpml = self.model.nbpml

            # Create source symbol
            p_src = Dimension('p_src', size=nsrc)
            src = SourceLike(name="src", dimensions=[time, p_src], npoint=nsrc, nt=nt,
                             dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                             coordinates=self.source.receiver_coords)
            src.data[:] = self.source.traces[:]

            # Create receiver symbol
            p_rec = Dimension('p_rec', size=nrec)
            rec = SourceLike(name="rec", dimensions=[time, p_rec], npoint=nrec, nt=nt,
                             dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                             coordinates=self.data.receiver_coords)

            # Create the forward wavefield
            u = TimeData(name="u", shape=self.model.shape_domain, time_dim=nt,
                         time_order=2, space_order=self.s_order, save=False,
                         dtype=dtype)
            fw = ForwardOperator(self.model, u, src, rec, self.data,
                                 time_order=self.t_order, spc_order=self.s_order,
                                 profile=True, save=False, dse=dse, compiler=compiler)
            self.at = AutoTuner(fw)
            self.at.auto_tune_blocks(self.s_order + 1, self.s_order * 4 + 2)

    # Forward modelling
    def Forward(self, save=False, cache_blocking=None, auto_tuning=False,
                dse='advanced', dle='advanced', compiler=None, u_ini=None, legacy=True):
        nt, nrec = self.data.shape
        nsrc = self.source.shape[1]
        ndim = len(self.model.shape)
        h = self.model.get_spacing()
        dtype = self.model.dtype
        nbpml = self.model.nbpml

        # Create source symbol
        p_src = Dimension('p_src', size=nsrc)
        src = SourceLike(name="src", dimensions=[time, p_src], npoint=nsrc, nt=nt,
                         dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                         coordinates=self.source.receiver_coords)
        src.data[:] = self.source.traces[:]

        # Create receiver symbol
        p_rec = Dimension('p_rec', size=nrec)
        rec = SourceLike(name="rec", dimensions=[time, p_rec], npoint=nrec, nt=nt,
                         dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                         coordinates=self.data.receiver_coords)

        if legacy and auto_tuning:
            cache_blocking = self.at.block_size

        # Create the forward wavefield
        u = TimeData(name="u", shape=self.model.shape_domain, time_dim=nt,
                     time_order=2, space_order=self.s_order, save=save,
                     dtype=self.model.dtype)
        u.pad_time = save
        if u_ini is not None:
            u.data[0:3, :] = u_ini[:]

        # Execute operator and return wavefield and receiver data
        fw = ForwardOperator(self.model, u, src, rec, self.data,
                             time_order=self.t_order, spc_order=self.s_order,
                             save=save, cache_blocking=cache_blocking, dse=dse,
                             dle=dle, compiler=compiler, profile=True, u_ini=u_ini,
                             legacy=legacy)

        if legacy:
            fw.apply()
            return (rec.data, u, fw.propagator.gflopss,
                    fw.propagator.oi, fw.propagator.timings)
        else:
            summary = fw.apply(autotune=auto_tuning)
            return rec.data, u, summary.gflopss, summary.oi, summary.timings

    # Adjoint modelling
    def Adjoint(self, recin, cache_blocking=None, auto_tuning=False,
                dse='advanced', dle='advanced', compiler=None, u_ini=None, legacy=True):
        nt, nrec = self.data.shape
        nsrc = self.source.shape[1]
        ndim = len(self.model.shape)
        h = self.model.get_spacing()
        dtype = self.model.dtype
        nbpml = self.model.nbpml

        # Create source symbol
        p_src = Dimension('p_src', size=nsrc)
        srca = SourceLike(name="srca", dimensions=[time, p_src], npoint=nsrc, nt=nt,
                          dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                          coordinates=self.source.receiver_coords)

        # Create receiver symbol
        p_rec = Dimension('p_rec', size=nrec)
        rec = SourceLike(name="rec", dimensions=[time, p_rec], npoint=nrec, nt=nt,
                         dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                         coordinates=self.data.receiver_coords)
        rec.data[:] = recin[:]

        if legacy and auto_tuning:
            cache_blocking = self.at.block_size

        # Create the forward wavefield
        v = TimeData(name="v", shape=self.model.shape_domain, time_dim=nt,
                     time_order=2, space_order=self.s_order,
                     dtype=self.model.dtype)

        # Execute operator and return wavefield and receiver data
        adj = AdjointOperator(self.model, v, srca, rec, self.data,
                              time_order=self.t_order, spc_order=self.s_order,
                              cache_blocking=cache_blocking, dse=dse,
                              dle=dle, compiler=compiler, profile=True,
                              legacy=legacy)

        if legacy:
            adj.apply()
            return (srca.data, v, adj.propagator.gflopss,
                    adj.propagator.oi, adj.propagator.timings)
        else:
            summary = adj.apply(autotune=auto_tuning)
            return srca.data, v, summary.gflopss, summary.oi, summary.timings

    # Gradient operator (adjoint of Linearized Born modelling, action of
    # the Jacobian adjoint on an input data)
    def Gradient(self, recin, u, cache_blocking=None, auto_tuning=False,
                 dse='advanced', dle='advanced', compiler=None, legacy=True):
        nt, nrec = self.data.shape
        ndim = len(self.model.shape)
        h = self.model.get_spacing()
        dtype = self.model.dtype
        nbpml = self.model.nbpml

        # Create receiver symbol
        p_rec = Dimension('p_rec', size=nrec)
        rec = SourceLike(name="rec", dimensions=[time, p_rec], npoint=nrec, nt=nt,
                         dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                         coordinates=self.data.receiver_coords)
        rec.data[:] = recin[:]

        # Gradient symbol
        grad = DenseData(name="grad", shape=self.model.shape_domain,
                         dtype=self.model.dtype)
        if legacy and auto_tuning:
            cache_blocking = self.at.block_size

        # Create the forward wavefield
        v = TimeData(name="v", shape=self.model.shape_domain, time_dim=nt,
                     time_order=2, space_order=self.s_order,
                     dtype=self.model.dtype)

        # Execute operator and return wavefield and receiver data
        gradop = GradientOperator(self.model, v, grad, rec, u, self.data,
                                  time_order=self.t_order, spc_order=self.s_order,
                                  cache_blocking=cache_blocking, dse=dse,
                                  dle=dle, compiler=compiler, profile=True,
                                  legacy=legacy)

        if legacy:
            grad.apply()
            return (grad.data, gradop.propagator.gflopss,
                    gradop.propagator.oi, gradop.propagator.timings)
        else:
            summary = gradop.apply(autotune=auto_tuning)
            return grad.data, summary.gflopss, summary.oi, summary.timings

    # Linearized Born modelling
    def Born(self, dmin, cache_blocking=None, auto_tuning=False,
             dse='advanced', dle='advanced', compiler=None, legacy=True):
        nt, nrec = self.data.shape
        nsrc = self.source.shape[1]
        ndim = len(self.model.shape)
        h = self.model.get_spacing()
        dtype = self.model.dtype
        nbpml = self.model.nbpml

        # Create source symbol
        p_src = Dimension('p_src', size=nsrc)
        src = SourceLike(name="src", dimensions=[time, p_src], npoint=nsrc, nt=nt,
                         dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                         coordinates=self.source.receiver_coords)
        src.data[:] = self.source.traces[:]

        # Create receiver symbol
        p_rec = Dimension('p_rec', size=nrec)
        rec = SourceLike(name="rec", dimensions=[time, p_rec], npoint=nrec, nt=nt,
                         dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                         coordinates=self.data.receiver_coords)

        if legacy and auto_tuning:
            cache_blocking = self.at.block_size

        # Create the forward wavefield
        u = TimeData(name="u", shape=self.model.shape_domain, time_dim=nt,
                     time_order=2, space_order=self.s_order,
                     dtype=self.model.dtype)
        U = TimeData(name="ul", shape=self.model.shape_domain, time_dim=nt,
                     time_order=2, space_order=self.s_order,
                     dtype=self.model.dtype)

        dm = DenseData(name="dm", shape=self.model.shape_domain, dtype=self.model.dtype)
        dm.data[:] = self.model.pad(dmin)
        # Execute operator and return wavefield and receiver data
        born = BornOperator(self.model, u, U, src, rec, dm, self.data,
                            time_order=self.t_order, spc_order=self.s_order,
                            cache_blocking=cache_blocking, dse=dse,
                            dle=dle, compiler=compiler, profile=True,
                            legacy=legacy)
        if legacy:
            born.apply()
            return (rec.data, u, U, born.propagator.gflopss,
                    born.propagator.oi, born.propagator.timings)
        else:
            summary = born.apply(autotune=auto_tuning)
            return rec.data, u, U, summary.gflopss, summary.oi, summary.timings
<commit_msg>Acoustic: Use docstrings for operator comments<commit_after># coding: utf-8
from __future__ import print_function

import numpy as np

from devito.at_controller import AutoTuner
from devito.dimension import Dimension
from devito.interfaces import DenseData, TimeData
from examples.acoustic.fwi_operators import *
from examples.source_type import SourceLike

class Acoustic_cg(object):
    """
    Class to setup the problem for the Acoustic Wave.

    Note: s_order must always be greater than t_order
    """
    def __init__(self, model, data, source, nbpml=40, t_order=2, s_order=2,
                 auto_tuning=False, dse=True, dle='advanced', compiler=None,
                 legacy=True):
        self.model = model
        self.t_order = t_order
        self.s_order = s_order
        self.data = data
        self.source = source
        self.dtype = np.float32
        # Time step can be \sqrt{3}=1.73 bigger with 4th order
        self.dt = self.model.critical_dt
        if self.t_order == 4:
            self.dt *= 1.73

        if auto_tuning and legacy:  # auto tuning with dummy forward operator
            nt, nrec = self.data.shape
            nsrc = self.source.shape[1]
            ndim = len(self.model.shape)
            h = self.model.get_spacing()
            dtype = self.model.dtype
            nbpml = self.model.nbpml

            # Create source symbol
            p_src = Dimension('p_src', size=nsrc)
            src = SourceLike(name="src", dimensions=[time, p_src], npoint=nsrc, nt=nt,
                             dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                             coordinates=self.source.receiver_coords)
            src.data[:] = self.source.traces[:]

            # Create receiver symbol
            p_rec = Dimension('p_rec', size=nrec)
            rec = SourceLike(name="rec", dimensions=[time, p_rec], npoint=nrec, nt=nt,
                             dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                             coordinates=self.data.receiver_coords)

            # Create the forward wavefield
            u = TimeData(name="u", shape=self.model.shape_domain, time_dim=nt,
                         time_order=2, space_order=self.s_order, save=False,
                         dtype=dtype)
            fw = ForwardOperator(self.model, u, src, rec, self.data,
                                 time_order=self.t_order, spc_order=self.s_order,
                                 profile=True, save=False, dse=dse, compiler=compiler)
            self.at = AutoTuner(fw)
            self.at.auto_tune_blocks(self.s_order + 1, self.s_order * 4 + 2)

    def Forward(self, save=False, cache_blocking=None, auto_tuning=False,
                dse='advanced', dle='advanced', compiler=None, u_ini=None, legacy=True):
        """
        Forward modelling
        """
        nt, nrec = self.data.shape
        nsrc = self.source.shape[1]
        ndim = len(self.model.shape)
        h = self.model.get_spacing()
        dtype = self.model.dtype
        nbpml = self.model.nbpml

        # Create source symbol
        p_src = Dimension('p_src', size=nsrc)
        src = SourceLike(name="src", dimensions=[time, p_src], npoint=nsrc, nt=nt,
                         dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                         coordinates=self.source.receiver_coords)
        src.data[:] = self.source.traces[:]

        # Create receiver symbol
        p_rec = Dimension('p_rec', size=nrec)
        rec = SourceLike(name="rec", dimensions=[time, p_rec], npoint=nrec, nt=nt,
                         dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                         coordinates=self.data.receiver_coords)

        if legacy and auto_tuning:
            cache_blocking = self.at.block_size

        # Create the forward wavefield
        u = TimeData(name="u", shape=self.model.shape_domain, time_dim=nt,
                     time_order=2, space_order=self.s_order, save=save,
                     dtype=self.model.dtype)
        u.pad_time = save
        if u_ini is not None:
            u.data[0:3, :] = u_ini[:]

        # Execute operator and return wavefield and receiver data
        fw = ForwardOperator(self.model, u, src, rec, self.data,
                             time_order=self.t_order, spc_order=self.s_order,
                             save=save, cache_blocking=cache_blocking, dse=dse,
                             dle=dle, compiler=compiler, profile=True, u_ini=u_ini,
                             legacy=legacy)

        if legacy:
            fw.apply()
            return (rec.data, u, fw.propagator.gflopss,
                    fw.propagator.oi, fw.propagator.timings)
        else:
            summary = fw.apply(autotune=auto_tuning)
            return rec.data, u, summary.gflopss, summary.oi, summary.timings

    def Adjoint(self, recin, cache_blocking=None, auto_tuning=False,
                dse='advanced', dle='advanced', compiler=None, u_ini=None, legacy=True):
        """
        Adjoint modelling
        """
        nt, nrec = self.data.shape
        nsrc = self.source.shape[1]
        ndim = len(self.model.shape)
        h = self.model.get_spacing()
        dtype = self.model.dtype
        nbpml = self.model.nbpml

        # Create source symbol
        p_src = Dimension('p_src', size=nsrc)
        srca = SourceLike(name="srca", dimensions=[time, p_src], npoint=nsrc, nt=nt,
                          dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                          coordinates=self.source.receiver_coords)

        # Create receiver symbol
        p_rec = Dimension('p_rec', size=nrec)
        rec = SourceLike(name="rec", dimensions=[time, p_rec], npoint=nrec, nt=nt,
                         dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                         coordinates=self.data.receiver_coords)
        rec.data[:] = recin[:]

        if legacy and auto_tuning:
            cache_blocking = self.at.block_size

        # Create the forward wavefield
        v = TimeData(name="v", shape=self.model.shape_domain, time_dim=nt,
                     time_order=2, space_order=self.s_order,
                     dtype=self.model.dtype)

        # Execute operator and return wavefield and receiver data
        adj = AdjointOperator(self.model, v, srca, rec, self.data,
                              time_order=self.t_order, spc_order=self.s_order,
                              cache_blocking=cache_blocking, dse=dse,
                              dle=dle, compiler=compiler, profile=True,
                              legacy=legacy)

        if legacy:
            adj.apply()
            return (srca.data, v, adj.propagator.gflopss,
                    adj.propagator.oi, adj.propagator.timings)
        else:
            summary = adj.apply(autotune=auto_tuning)
            return srca.data, v, summary.gflopss, summary.oi, summary.timings

    def Gradient(self, recin, u, cache_blocking=None, auto_tuning=False,
                 dse='advanced', dle='advanced', compiler=None, legacy=True):
        """
        Gradient operator (adjoint of Linearized Born modelling, action of
        the Jacobian adjoint on an input data)
        """
        nt, nrec = self.data.shape
        ndim = len(self.model.shape)
        h = self.model.get_spacing()
        dtype = self.model.dtype
        nbpml = self.model.nbpml

        # Create receiver symbol
        p_rec = Dimension('p_rec', size=nrec)
        rec = SourceLike(name="rec", dimensions=[time, p_rec], npoint=nrec, nt=nt,
                         dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                         coordinates=self.data.receiver_coords)
        rec.data[:] = recin[:]

        # Gradient symbol
        grad = DenseData(name="grad", shape=self.model.shape_domain,
                         dtype=self.model.dtype)
        if legacy and auto_tuning:
            cache_blocking = self.at.block_size

        # Create the forward wavefield
        v = TimeData(name="v", shape=self.model.shape_domain, time_dim=nt,
                     time_order=2, space_order=self.s_order,
                     dtype=self.model.dtype)

        # Execute operator and return wavefield and receiver data
        gradop = GradientOperator(self.model, v, grad, rec, u, self.data,
                                  time_order=self.t_order, spc_order=self.s_order,
                                  cache_blocking=cache_blocking, dse=dse,
                                  dle=dle, compiler=compiler, profile=True,
                                  legacy=legacy)

        if legacy:
            grad.apply()
            return (grad.data, gradop.propagator.gflopss,
                    gradop.propagator.oi, gradop.propagator.timings)
        else:
            summary = gradop.apply(autotune=auto_tuning)
            return grad.data, summary.gflopss, summary.oi, summary.timings

    def Born(self, dmin, cache_blocking=None, auto_tuning=False,
             dse='advanced', dle='advanced', compiler=None, legacy=True):
        """
        Linearized Born modelling
        """
        nt, nrec = self.data.shape
        nsrc = self.source.shape[1]
        ndim = len(self.model.shape)
        h = self.model.get_spacing()
        dtype = self.model.dtype
        nbpml = self.model.nbpml

        # Create source symbol
        p_src = Dimension('p_src', size=nsrc)
        src = SourceLike(name="src", dimensions=[time, p_src], npoint=nsrc, nt=nt,
                         dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                         coordinates=self.source.receiver_coords)
        src.data[:] = self.source.traces[:]

        # Create receiver symbol
        p_rec = Dimension('p_rec', size=nrec)
        rec = SourceLike(name="rec", dimensions=[time, p_rec], npoint=nrec, nt=nt,
                         dt=self.dt, h=h, ndim=ndim, nbpml=nbpml, dtype=dtype,
                         coordinates=self.data.receiver_coords)

        if legacy and auto_tuning:
            cache_blocking = self.at.block_size

        # Create the forward wavefield
        u = TimeData(name="u", shape=self.model.shape_domain, time_dim=nt,
                     time_order=2, space_order=self.s_order,
                     dtype=self.model.dtype)
        U = TimeData(name="ul", shape=self.model.shape_domain, time_dim=nt,
                     time_order=2, space_order=self.s_order,
                     dtype=self.model.dtype)

        dm = DenseData(name="dm", shape=self.model.shape_domain, dtype=self.model.dtype)
        dm.data[:] = self.model.pad(dmin)
        # Execute operator and return wavefield and receiver data
        born = BornOperator(self.model, u, U, src, rec, dm, self.data,
                            time_order=self.t_order, spc_order=self.s_order,
                            cache_blocking=cache_blocking, dse=dse,
                            dle=dle, compiler=compiler, profile=True,
                            legacy=legacy)
        if legacy:
            born.apply()
            return (rec.data, u, U, born.propagator.gflopss,
                    born.propagator.oi, born.propagator.timings)
        else:
            summary = born.apply(autotune=auto_tuning)
            return rec.data, u, U, summary.gflopss, summary.oi, summary.timings
<|endoftext|>