<commit_before># Copyright 2021 Google LLC All rights reserved.
#
# 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.

import hashlib
import pickle
import pkg_resources

import pytest

from google.cloud import spanner_v1
from google.cloud.spanner_dbapi.connection import connect, Connection
from . import _helpers

DATABASE_NAME = "dbapi-txn"

DDL_STATEMENTS = (
    """CREATE TABLE contacts (
        contact_id INT64,
        first_name STRING(1024),
        last_name STRING(1024),
        email STRING(1024)
    )
    PRIMARY KEY (contact_id)""",
)

@pytest.fixture(scope="session")
def raw_database(shared_instance, database_operation_timeout):
    databse_id = _helpers.unique_id("dbapi-txn")
    pool = spanner_v1.BurstyPool(labels={"testcase": "database_api"})
    database = shared_instance.database(
        databse_id, ddl_statements=DDL_STATEMENTS, pool=pool,
    )
    op = database.create()
    op.result(database_operation_timeout)  # raises on failure / timeout.

    yield database

    database.drop()

def clear_table(transaction):
    transaction.execute_update("DELETE FROM contacts WHERE true")

@pytest.fixture(scope="function")
def dbapi_database(raw_database):

    raw_database.run_in_transaction(clear_table)

    yield raw_database

    raw_database.run_in_transaction(clear_table)

def test_commit(shared_instance, dbapi_database):
    """Test committing a transaction with several statements."""
    want_row = (
        1,
        "updated-first-name",
        "last-name",
        "test.email_updated@domen.ru",
    )
    # connect to the test database
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    # execute several DML statements within one transaction
    cursor.execute(
        """
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES (1, 'first-name', 'last-name', 'test.email@domen.ru')
    """
    )
    cursor.execute(
        """
UPDATE contacts
SET first_name = 'updated-first-name'
WHERE first_name = 'first-name'
"""
    )
    cursor.execute(
        """
UPDATE contacts
SET email = 'test.email_updated@domen.ru'
WHERE email = 'test.email@domen.ru'
"""
    )
    conn.commit()

    # read the resulting data from the database
    cursor.execute("SELECT * FROM contacts")
    got_rows = cursor.fetchall()
    conn.commit()

    assert got_rows == [want_row]

    cursor.close()
    conn.close()

def test_rollback(shared_instance, dbapi_database):
    """Test rollbacking a transaction with several statements."""
    want_row = (2, "first-name", "last-name", "test.email@domen.ru")
    # connect to the test database
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    cursor.execute(
        """
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES (2, 'first-name', 'last-name', 'test.email@domen.ru')
    """
    )
    conn.commit()

    # execute several DMLs with one transaction
    cursor.execute(
        """
UPDATE contacts
SET first_name = 'updated-first-name'
WHERE first_name = 'first-name'
"""
    )
    cursor.execute(
        """
UPDATE contacts
SET email = 'test.email_updated@domen.ru'
WHERE email = 'test.email@domen.ru'
"""
    )
    conn.rollback()

    # read the resulting data from the database
    cursor.execute("SELECT * FROM contacts")
    got_rows = cursor.fetchall()
    conn.commit()

    assert got_rows == [want_row]

    cursor.close()
    conn.close()

def test_autocommit_mode_change(shared_instance, dbapi_database):
    """Test auto committing a transaction on `autocommit` mode change."""
    want_row = (
        2,
        "updated-first-name",
        "last-name",
        "test.email@domen.ru",
    )
    # connect to the test database
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    cursor.execute(
        """
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES (2, 'first-name', 'last-name', 'test.email@domen.ru')
    """
    )
    cursor.execute(
        """
UPDATE contacts
SET first_name = 'updated-first-name'
WHERE first_name = 'first-name'
"""
    )
    conn.autocommit = True

    # read the resulting data from the database
    cursor.execute("SELECT * FROM contacts")
    got_rows = cursor.fetchall()

    assert got_rows == [want_row]

    cursor.close()
    conn.close()

def test_rollback_on_connection_closing(shared_instance, dbapi_database):
    """
    When closing a connection all the pending transactions
    must be rollbacked. Testing if it's working this way.
    """
    want_row = (1, "first-name", "last-name", "test.email@domen.ru")
    # connect to the test database
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    cursor.execute(
        """
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES (1, 'first-name', 'last-name', 'test.email@domen.ru')
    """
    )
    conn.commit()

    cursor.execute(
        """
UPDATE contacts
SET first_name = 'updated-first-name'
WHERE first_name = 'first-name'
"""
    )
    conn.close()

    # connect again, as the previous connection is no-op after closing
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    # read the resulting data from the database
    cursor.execute("SELECT * FROM contacts")
    got_rows = cursor.fetchall()
    conn.commit()

    assert got_rows == [want_row]

    cursor.close()
    conn.close()

def test_results_checksum(shared_instance, dbapi_database):
    """Test that results checksum is calculated properly."""
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    cursor.execute(
        """
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES
(1, 'first-name', 'last-name', 'test.email@domen.ru'),
(2, 'first-name2', 'last-name2', 'test.email2@domen.ru')
    """
    )
    assert len(conn._statements) == 1
    conn.commit()

    cursor.execute("SELECT * FROM contacts")
    got_rows = cursor.fetchall()

    assert len(conn._statements) == 1
    conn.commit()

    checksum = hashlib.sha256()
    checksum.update(pickle.dumps(got_rows[0]))
    checksum.update(pickle.dumps(got_rows[1]))

    assert cursor._checksum.checksum.digest() == checksum.digest()

def test_execute_many(shared_instance, dbapi_database):
    # connect to the test database
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    row_data = [
        (1, "first-name", "last-name", "test.email@example.com"),
        (2, "first-name2", "last-name2", "test.email2@example.com"),
    ]
    cursor.executemany(
        """
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES (%s, %s, %s, %s)
    """,
        row_data,
    )
    conn.commit()

    cursor.executemany(
        """SELECT * FROM contacts WHERE contact_id = @a1""", ({"a1": 1}, {"a1": 2}),
    )
    res = cursor.fetchall()
    conn.commit()

    assert len(res) == len(row_data)
    for found, expected in zip(res, row_data):
        assert found[0] == expected[0]

    # checking that execute() and executemany()
    # results are not mixed together
    cursor.execute(
        """
SELECT * FROM contacts WHERE contact_id = 1
""",
    )
    res = cursor.fetchone()
    conn.commit()

    assert res[0] == 1
    conn.close()

def test_DDL_autocommit(shared_instance, dbapi_database):
    """Check that DDLs in autocommit mode are immediately executed."""
    conn = Connection(shared_instance, dbapi_database)
    conn.autocommit = True

    cur = conn.cursor()
    cur.execute(
        """
        CREATE TABLE Singers (
            SingerId     INT64 NOT NULL,
            Name    STRING(1024),
        ) PRIMARY KEY (SingerId)
    """
    )
    conn.close()

    # if previous DDL wasn't committed, the next DROP TABLE
    # statement will fail with a ProgrammingError
    conn = Connection(shared_instance, dbapi_database)
    cur = conn.cursor()

    cur.execute("DROP TABLE Singers")
    conn.commit()

def test_DDL_commit(shared_instance, dbapi_database):
    """Check that DDLs in commit mode are executed on calling `commit()`."""
    conn = Connection(shared_instance, dbapi_database)
    cur = conn.cursor()

    cur.execute(
        """
    CREATE TABLE Singers (
        SingerId     INT64 NOT NULL,
        Name    STRING(1024),
    ) PRIMARY KEY (SingerId)
    """
    )
    conn.commit()
    conn.close()

    # if previous DDL wasn't committed, the next DROP TABLE
    # statement will fail with a ProgrammingError
    conn = Connection(shared_instance, dbapi_database)
    cur = conn.cursor()

    cur.execute("DROP TABLE Singers")
    conn.commit()

def test_ping(shared_instance, dbapi_database):
    """Check connection validation method."""
    conn = Connection(shared_instance, dbapi_database)
    conn.validate()
    conn.close()

def test_user_agent(shared_instance, dbapi_database):
    """Check that DB API uses an appropriate user agent."""
    conn = connect(shared_instance.name, dbapi_database.name)
    assert (
        conn.instance._client._client_info.user_agent
        == "dbapi/" + pkg_resources.get_distribution("google-cloud-spanner").version
    )
<commit_msg>test: fix executemany call to use expected DBAPI formatting (#569)<commit_after># Copyright 2021 Google LLC All rights reserved.
#
# 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.

import hashlib
import pickle
import pkg_resources

import pytest

from google.cloud import spanner_v1
from google.cloud.spanner_dbapi.connection import connect, Connection
from . import _helpers

DATABASE_NAME = "dbapi-txn"

DDL_STATEMENTS = (
    """CREATE TABLE contacts (
        contact_id INT64,
        first_name STRING(1024),
        last_name STRING(1024),
        email STRING(1024)
    )
    PRIMARY KEY (contact_id)""",
)

@pytest.fixture(scope="session")
def raw_database(shared_instance, database_operation_timeout):
    databse_id = _helpers.unique_id("dbapi-txn")
    pool = spanner_v1.BurstyPool(labels={"testcase": "database_api"})
    database = shared_instance.database(
        databse_id, ddl_statements=DDL_STATEMENTS, pool=pool,
    )
    op = database.create()
    op.result(database_operation_timeout)  # raises on failure / timeout.

    yield database

    database.drop()

def clear_table(transaction):
    transaction.execute_update("DELETE FROM contacts WHERE true")

@pytest.fixture(scope="function")
def dbapi_database(raw_database):

    raw_database.run_in_transaction(clear_table)

    yield raw_database

    raw_database.run_in_transaction(clear_table)

def test_commit(shared_instance, dbapi_database):
    """Test committing a transaction with several statements."""
    want_row = (
        1,
        "updated-first-name",
        "last-name",
        "test.email_updated@domen.ru",
    )
    # connect to the test database
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    # execute several DML statements within one transaction
    cursor.execute(
        """
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES (1, 'first-name', 'last-name', 'test.email@domen.ru')
    """
    )
    cursor.execute(
        """
UPDATE contacts
SET first_name = 'updated-first-name'
WHERE first_name = 'first-name'
"""
    )
    cursor.execute(
        """
UPDATE contacts
SET email = 'test.email_updated@domen.ru'
WHERE email = 'test.email@domen.ru'
"""
    )
    conn.commit()

    # read the resulting data from the database
    cursor.execute("SELECT * FROM contacts")
    got_rows = cursor.fetchall()
    conn.commit()

    assert got_rows == [want_row]

    cursor.close()
    conn.close()

def test_rollback(shared_instance, dbapi_database):
    """Test rollbacking a transaction with several statements."""
    want_row = (2, "first-name", "last-name", "test.email@domen.ru")
    # connect to the test database
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    cursor.execute(
        """
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES (2, 'first-name', 'last-name', 'test.email@domen.ru')
    """
    )
    conn.commit()

    # execute several DMLs with one transaction
    cursor.execute(
        """
UPDATE contacts
SET first_name = 'updated-first-name'
WHERE first_name = 'first-name'
"""
    )
    cursor.execute(
        """
UPDATE contacts
SET email = 'test.email_updated@domen.ru'
WHERE email = 'test.email@domen.ru'
"""
    )
    conn.rollback()

    # read the resulting data from the database
    cursor.execute("SELECT * FROM contacts")
    got_rows = cursor.fetchall()
    conn.commit()

    assert got_rows == [want_row]

    cursor.close()
    conn.close()

def test_autocommit_mode_change(shared_instance, dbapi_database):
    """Test auto committing a transaction on `autocommit` mode change."""
    want_row = (
        2,
        "updated-first-name",
        "last-name",
        "test.email@domen.ru",
    )
    # connect to the test database
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    cursor.execute(
        """
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES (2, 'first-name', 'last-name', 'test.email@domen.ru')
    """
    )
    cursor.execute(
        """
UPDATE contacts
SET first_name = 'updated-first-name'
WHERE first_name = 'first-name'
"""
    )
    conn.autocommit = True

    # read the resulting data from the database
    cursor.execute("SELECT * FROM contacts")
    got_rows = cursor.fetchall()

    assert got_rows == [want_row]

    cursor.close()
    conn.close()

def test_rollback_on_connection_closing(shared_instance, dbapi_database):
    """
    When closing a connection all the pending transactions
    must be rollbacked. Testing if it's working this way.
    """
    want_row = (1, "first-name", "last-name", "test.email@domen.ru")
    # connect to the test database
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    cursor.execute(
        """
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES (1, 'first-name', 'last-name', 'test.email@domen.ru')
    """
    )
    conn.commit()

    cursor.execute(
        """
UPDATE contacts
SET first_name = 'updated-first-name'
WHERE first_name = 'first-name'
"""
    )
    conn.close()

    # connect again, as the previous connection is no-op after closing
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    # read the resulting data from the database
    cursor.execute("SELECT * FROM contacts")
    got_rows = cursor.fetchall()
    conn.commit()

    assert got_rows == [want_row]

    cursor.close()
    conn.close()

def test_results_checksum(shared_instance, dbapi_database):
    """Test that results checksum is calculated properly."""
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    cursor.execute(
        """
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES
(1, 'first-name', 'last-name', 'test.email@domen.ru'),
(2, 'first-name2', 'last-name2', 'test.email2@domen.ru')
    """
    )
    assert len(conn._statements) == 1
    conn.commit()

    cursor.execute("SELECT * FROM contacts")
    got_rows = cursor.fetchall()

    assert len(conn._statements) == 1
    conn.commit()

    checksum = hashlib.sha256()
    checksum.update(pickle.dumps(got_rows[0]))
    checksum.update(pickle.dumps(got_rows[1]))

    assert cursor._checksum.checksum.digest() == checksum.digest()

def test_execute_many(shared_instance, dbapi_database):
    # connect to the test database
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    row_data = [
        (1, "first-name", "last-name", "test.email@example.com"),
        (2, "first-name2", "last-name2", "test.email2@example.com"),
    ]
    cursor.executemany(
        """
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES (%s, %s, %s, %s)
    """,
        row_data,
    )
    conn.commit()

    cursor.executemany(
        """SELECT * FROM contacts WHERE contact_id = %s""", ((1,), (2,)),
    )
    res = cursor.fetchall()
    conn.commit()

    assert len(res) == len(row_data)
    for found, expected in zip(res, row_data):
        assert found[0] == expected[0]

    # checking that execute() and executemany()
    # results are not mixed together
    cursor.execute(
        """
SELECT * FROM contacts WHERE contact_id = 1
""",
    )
    res = cursor.fetchone()
    conn.commit()

    assert res[0] == 1
    conn.close()

def test_DDL_autocommit(shared_instance, dbapi_database):
    """Check that DDLs in autocommit mode are immediately executed."""
    conn = Connection(shared_instance, dbapi_database)
    conn.autocommit = True

    cur = conn.cursor()
    cur.execute(
        """
        CREATE TABLE Singers (
            SingerId     INT64 NOT NULL,
            Name    STRING(1024),
        ) PRIMARY KEY (SingerId)
    """
    )
    conn.close()

    # if previous DDL wasn't committed, the next DROP TABLE
    # statement will fail with a ProgrammingError
    conn = Connection(shared_instance, dbapi_database)
    cur = conn.cursor()

    cur.execute("DROP TABLE Singers")
    conn.commit()

def test_DDL_commit(shared_instance, dbapi_database):
    """Check that DDLs in commit mode are executed on calling `commit()`."""
    conn = Connection(shared_instance, dbapi_database)
    cur = conn.cursor()

    cur.execute(
        """
    CREATE TABLE Singers (
        SingerId     INT64 NOT NULL,
        Name    STRING(1024),
    ) PRIMARY KEY (SingerId)
    """
    )
    conn.commit()
    conn.close()

    # if previous DDL wasn't committed, the next DROP TABLE
    # statement will fail with a ProgrammingError
    conn = Connection(shared_instance, dbapi_database)
    cur = conn.cursor()

    cur.execute("DROP TABLE Singers")
    conn.commit()

def test_ping(shared_instance, dbapi_database):
    """Check connection validation method."""
    conn = Connection(shared_instance, dbapi_database)
    conn.validate()
    conn.close()

def test_user_agent(shared_instance, dbapi_database):
    """Check that DB API uses an appropriate user agent."""
    conn = connect(shared_instance.name, dbapi_database.name)
    assert (
        conn.instance._client._client_info.user_agent
        == "dbapi/" + pkg_resources.get_distribution("google-cloud-spanner").version
    )
<|endoftext|>


<commit_before># Copyright (C) 2013-2016 Martin Vejmelka, UC Denver
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
# A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import numpy as np
import sys

class FuelMoistureModel:
    """
    A pure-python implementation of individual moisture models running on a grid.
    """
    Tk = np.array([1.0, 10.0, 100.0, 1000.0]) * 3600    # nominal fuel delays
    r0 = 0.05                                           # threshold rainfall [mm/h]
    rk = 8.0                                            # saturation rain intensity [mm/h]
    Trk = 14.0 * 3600                                   # time constant for wetting model [s]
    S = 2.5                                             # saturation intensity [dimensionless]

    def __init__(self, m0, Tk=None, P0=None):
        """
        Initialize the model with given position and moisture levels.

        :param m0: the initial condition for the entire grid (shape: grid0 x grid1 x num_fuels)
        :param Tk: drying/wetting time constants of simulated fuels (one per fuel), default [1 10 100 1000]
        :param P0: initial state error covariance
        """
        s0, s1, k = m0.shape
        if Tk is not None:
            self.Tk = Tk
        dim = k + 2
        assert k == len(self.Tk)
        self.m_ext = np.zeros((s0, s1, dim))
        self.m_ext[:, :, :k] = m0

        # note: the moisture advance will proceed by fuel moisture types
        # thus we only need space for one class at a time
        self.m_i = np.zeros((s0, s1))
        #self.mn_i = np.zeros((s0,s1))
        self.rlag = np.zeros((s0, s1))
        self.equi = np.zeros((s0, s1))
        self.model_ids = np.zeros((s0, s1))

        self.EdA = np.zeros((s0, s1))
        self.EwA = np.zeros((s0, s1))

        # state covariance current and forecasted
        self.P = np.zeros((s0, s1, dim, dim))
        self.P2 = np.zeros((dim, dim))
        for s in np.ndindex((s0, s1)):
            self.P[s[0], s[1], :, :] = P0

        # fill out the fixed parts of the jacobian
        self.J = np.zeros((s0, s1, dim, dim))
        self.Jii = np.zeros((s0, s1))

        # note: the observation operator H is common for all dims
        self.H = np.zeros((k, dim))

    def advance_model(self, Ed, Ew, r, dt, mQ=None):
        """
        This model captures the moisture dynamics at each grid point independently.

        Ed - drying equilibrium [grid shape]
        Ew - wetting equilibrium [grid shape]
        r - rain intensity for time unit [grid shape] [mm/h]
        dt - integration step [scalar] [s]
        mQ - the proess noise matrix [shape dim x dim, common across grid points]
        """
        Tk = self.Tk
        k = Tk.shape[0]                 # number of fuel classes
        m_ext = self.m_ext
        equi = self.equi
        rlag = self.rlag
        model_ids = self.model_ids
        m_i = self.m_i
        #mn_i = self.mn_i
        J = self.J
        Jii = self.Jii
        P2 = self.P2
        P = self.P
        EdA, EwA = self.EdA, self.EwA

        # add assimilated equilibrium difference, which is shared across spatial locations
        EdA[:, :] = Ed
        EdA += m_ext[:, :, k]
        EwA[:, :] = Ew
        EwA += m_ext[:, :, k]

        dS = m_ext[:, :, k + 1]

        print('GMM: EdA (%g,%g,%g)  EwA (%g,%g,%g)' % (np.amin(EdA),np.mean(EdA),np.amax(EdA), np.amin(EwA),np.mean(EdA),np.amax(EdA)))

        assert np.all(EdA >= EwA)

        # re-initialize the Jacobian (in case we must recompute it)
        J[:] = 0.0
        J[:, :, k, k] = 1.0
        J[:, :, k + 1, k + 1] = 1.0

        # where rainfall is above threshold (spatially different), apply
        # saturation model, equi and rlag are specific to fuel type and
        # location
        for i in range(k):

            # initialize equilibrium with current moisture
            m_i[:, :] = m_ext[:, :, i]
            equi[:, :] = m_i
            rlag[:, :] = 0.0
            model_ids[:] = 4

            # on grid locations where there is rain, modify equilibrium
            has_rain = r > self.r0
            no_rain = np.logical_not(has_rain)

            equi[has_rain] = self.S + dS[has_rain]
            rlag[has_rain] = 1.0 / self.Trk * (1.0 - np.exp(- (r[has_rain] - self.r0) / self.rk))
            model_ids[has_rain] = 3

            # equilibrium is selected according to current moisture level
            is_drying = np.logical_and(no_rain, m_i > EdA)
            equi[is_drying] = EdA[is_drying]
            model_ids[is_drying] = 1

            is_wetting = np.logical_and(no_rain, m_i < EwA)
            equi[is_wetting] = EwA[is_wetting]
            model_ids[is_wetting] = 2

            rlag[no_rain] = 1.0 / Tk[i]

            dead_zone = (model_ids == 4)

            # optional inspection of changes at given position
            #dg_pos = (86,205)
            #mi_old = m_i[dg_pos]

            # select appropriate integration method according to change for each fuel
            # and location
            rlag *= dt
            change = rlag
            big_change = change > 0.01
            m_i[big_change] += (equi[big_change] - m_i[big_change]) * (1.0 - np.exp(-change[big_change]))
            small_change = np.logical_not(big_change)
            m_i[small_change] += (equi[small_change] - m_i[small_change]) * change[small_change] * (
                1.0 - 0.5 * change[small_change])

            #print('Diag at 86,205 fuel %d: model_id %d equi %g rlag %g change %g value %g -> %g' %
            #        (i, model_ids[dg_pos], equi[dg_pos], rlag[dg_pos], change[dg_pos], mi_old, m_i[dg_pos]))

            # store in state matrix
            m_ext[:, :, i] = m_i

            # update model state covariance if requested using the old state (the jacobian must be computed as well)
            if mQ is not None:

                # partial m_i/partial m_i
                Jii[big_change] = np.exp(-change[big_change])
                Jii[small_change] = 1.0 - change[small_change] * (1.0 - 0.5 * change[small_change])
                Jii[dead_zone] = 1.0
                J[:, :, i, i] = Jii

                # partial m_i/partial deltaE
                Jii[:] = 0.0
                norain_big = np.logical_and(no_rain, big_change)
                norain_small = np.logical_and(no_rain, small_change)
                Jii[norain_big] = 1.0 - np.exp(-change[norain_big])
                Jii[norain_small] = change[norain_small] * (1.0 - 0.5 * change[norain_small])
                Jii[dead_zone] = 0.0
                J[:, :, i, k] = Jii

                # partial m_i / partial deltaS
                Jii[:] = 0.0
                rain_big = np.logical_and(has_rain, big_change)
                rain_small = np.logical_and(has_rain, small_change)
                Jii[rain_big] = 1.0 - np.exp(-change[rain_big])
                Jii[rain_small] = change[rain_small] * (1.0 - 0.5 * change[rain_small])
                Jii[dead_zone] = 0.0
                J[:, :, i, k+1] = Jii

        # transformed to run in-place with one pre-allocated temporary
        dom_shape = P[:, :, 0, 0].shape
        for i in range(dom_shape[0]):
            for j in range(dom_shape[1]):
                # Ps = Js P[s,:,:] Js^T
                Ps = P[i, j, :, :]
                Js = J[i, j, :, :]
                np.dot(Js, Ps, P2)
                np.dot(P2, Js.T, Ps)

                # P[s,:,:] = Js P[s,:,:] Js^T + mQ
                Ps += mQ
                P[i, j, :, :] = Ps

    def get_state(self):
        """
        Return the current state for all grid points. READ-ONLY under normal circumstances.
        """
        return self.m_ext

    def get_state_covar(self):
        """
        Return the state covariance for all grid points. READ-ONLY under normal circumstances.
        """
        return self.P

    def kalman_update(self, O, V, fuel_types, Kg=None):
        """
        Updates the state using the observation at the grid point.

          O - the observations [shape grid_size x len(fuel_types)]
          V - the (diagonal) variance matrix of the measurements [shape grid_size x len(fuel_types) x len(fuel_types)]
          fuel_types - the fuel types for which the observations exist (used to construct observation vector)

        """
        m_ext, P, H, P2 = self.m_ext, self.P, self.H, self.P2
        H[:] = 0.0

        # construct an observation operator tailored to observed fuel types
        for i in fuel_types:
            H[i, i] = 1.0
        Ho = H[fuel_types, :]

        for i, j in np.ndindex(P[:, :, 0, 0].shape):
            # re-use P2 to store forecast covariance for position s
            P2[:, :] = P[i, j, :, :]

            if np.any(np.diag(P2) < 0.0):
                print("ERROR: negative diagonal %s" % (str(np.diag(P2))))

            # compute Kalman gain
            I = np.dot(np.dot(Ho, P2), Ho.T) + V[i, j, :, :]
            K = np.dot(np.dot(P2, Ho.T), np.linalg.inv(I))

            if K[1, 0] > 1.0 or K[1, 0] < 0.0:
                print("ERROR at %s, Kalman gain %g out of bounds! I=%g V=%g" % (
                    str((i, j)), K[fuel_type, 0], I, V[s[0], s[1], 0, 0]))

            # update state and state covariance
            m_ext[i, j, :] += np.dot(K, O[i, j, :] - m_ext[i, j, fuel_types])
            P[i, j, :, :] -= np.dot(np.dot(K, Ho), P2)

            if Kg is not None:
                Kg[i, j, :] = K[:, 0]

    def kalman_update_single(self, O, V, fuel_type, Kg=None):
        """
        Perform an optimized Kalman update assuming there is only one fuel type.
        """
        m_ext, P, P2 = self.m_ext, self.P, self.P2

        #print("******* GRID MODEL ********")
        #print("grid: O = %.18e  V = %.18e   fuel_type = %d" % (O[0,0], V[0,0,0,0], fuel_type))

        dom_shape = P[:, :, 0, 0].shape
        for i in range(dom_shape[0]):
            for j in range(dom_shape[1]):
                P2[:, :] = P[i, j, :, :]
                #print(P2)

                if np.any(np.diag(P2) < 0.0):
                    print("ERROR: negative diagonal %s" % (str(np.diag(P2))))

                I = P2[fuel_type, fuel_type] + V[i, j, 0, 0]
                K = P2[:, fuel_type] / I

                if K[fuel_type] > 1.0 or K[fuel_type] < 0.0:
                    print("WARN: at %s, Kalman gain %g out of bounds! I=%g V=%g" % (
                        str((i, j)), K[fuel_type], I, V[i, j, 0, 0]))

                m_ext[i, j, :] += K * (O[i, j, 0] - m_ext[i, j, fuel_type])

                # NOTE: this update may produce matrices that are not symmetric positive definite
                P2 -= np.dot(K[:, np.newaxis], P2[fuel_type:fuel_type + 1, :])
                P[i, j, :, :] = P2

                if Kg is not None:
                    Kg[i, j, :] = K

    def kalman_update_single2(self, O, V, fuel_type, Kg):

        m_ext, P = self.m_ext, self.P

        if np.any(O < 0.0):
            print("ERROR: found negative observations!")
            sys.exit(5)

        # I is dom_shape
        I = P[:, :, fuel_type, fuel_type] + V[:, :, 0, 0]
        # K is dom_shape x dim
        Kg[:, :, :] = P[:, :, :, fuel_type] / I[:, :, np.newaxis]

        if np.any(np.logical_or(Kg[:, :, fuel_type] > 1.0, Kg[:, :, fuel_type] < 0.0)):
            print("ERROR: some Kalman gains for 10-hr fuel are out of bounds.")

        # m_ext is dom_shape x dim, O[:,:,0] and m_ext[:,:,fuel_type] are dom_shape
        m_ext += Kg * (O - m_ext[:, :, fuel_type:fuel_type + 1])

        # try to do a tensor product here
        # Kg[:,:,:,newaxis] is shape dom_shape x dim x 1
        # P[:,:,fuel_type:fuel_type+1,:] is dom_shape x 1 x dim
        dom_shape = P[:, :, 0, 0].shape
        for i in range(dom_shape[0]):
            for j in range(dom_shape[1]):
                P[i, j, :, :] -= np.dot(Kg[i, j, :, np.newaxis], P[i, j, fuel_type:fuel_type + 1, :])

<commit_msg>removed numpy code from class attributes in fuel_moisture_model<commit_after># Copyright (C) 2013-2016 Martin Vejmelka, UC Denver
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
# A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import numpy as np
import sys

class FuelMoistureModel:
    """
    A pure-python implementation of independent moisture models running on a grid.
    """

    def __init__(self, m0, Tk=None, P0=None):
        """
        Initialize the model with given position and moisture levels.

        :param m0: the initial condition for the entire grid (shape: grid0 x grid1 x num_fuels)
        :param Tk: drying/wetting time constants of simulated fuels (one per fuel), default [1 10 100 1000]
        :param P0: initial state error covariance
        """
        self.Tk = np.array([1.0, 10.0, 100.0, 1000.0]) * 3600    # nominal fuel delays
        self.r0 = 0.05                                           # threshold rainfall [mm/h]
        self.rk = 8.0                                            # saturation rain intensity [mm/h]
        self.Trk = 14.0 * 3600                                   # time constant for wetting model [s]
        self.S = 2.5                                             # saturation intensity [dimensionless]

        s0, s1, k = m0.shape
        if Tk is not None:
            self.Tk = Tk
        dim = k + 2
        assert k == len(self.Tk)
        self.m_ext = np.zeros((s0, s1, dim))
        self.m_ext[:, :, :k] = m0

        # note: the moisture advance will proceed by fuel moisture types
        # thus we only need space for one class at a time
        self.m_i = np.zeros((s0, s1))
        #self.mn_i = np.zeros((s0,s1))
        self.rlag = np.zeros((s0, s1))
        self.equi = np.zeros((s0, s1))
        self.model_ids = np.zeros((s0, s1))

        self.EdA = np.zeros((s0, s1))
        self.EwA = np.zeros((s0, s1))

        # state covariance current and forecasted
        self.P = np.zeros((s0, s1, dim, dim))
        self.P2 = np.zeros((dim, dim))
        for s in np.ndindex((s0, s1)):
            self.P[s[0], s[1], :, :] = P0

        # fill out the fixed parts of the jacobian
        self.J = np.zeros((s0, s1, dim, dim))
        self.Jii = np.zeros((s0, s1))

        # note: the observation operator H is common for all dims
        self.H = np.zeros((k, dim))

    def advance_model(self, Ed, Ew, r, dt, mQ=None):
        """
        This model captures the moisture dynamics at each grid point independently.

        Ed - drying equilibrium [grid shape]
        Ew - wetting equilibrium [grid shape]
        r - rain intensity for time unit [grid shape] [mm/h]
        dt - integration step [scalar] [s]
        mQ - the proess noise matrix [shape dim x dim, common across grid points]
        """
        Tk = self.Tk
        k = Tk.shape[0]                 # number of fuel classes
        m_ext = self.m_ext
        equi = self.equi
        rlag = self.rlag
        model_ids = self.model_ids
        m_i = self.m_i
        #mn_i = self.mn_i
        J = self.J
        Jii = self.Jii
        P2 = self.P2
        P = self.P
        EdA, EwA = self.EdA, self.EwA

        # add assimilated equilibrium difference, which is shared across spatial locations
        EdA[:, :] = Ed
        EdA += m_ext[:, :, k]
        EwA[:, :] = Ew
        EwA += m_ext[:, :, k]

        dS = m_ext[:, :, k + 1]

        print('GMM: EdA (%g,%g,%g)  EwA (%g,%g,%g)' % (np.amin(EdA),np.mean(EdA),np.amax(EdA), np.amin(EwA),np.mean(EdA),np.amax(EdA)))

        assert np.all(EdA >= EwA)

        # re-initialize the Jacobian (in case we must recompute it)
        J[:] = 0.0
        J[:, :, k, k] = 1.0
        J[:, :, k + 1, k + 1] = 1.0

        # where rainfall is above threshold (spatially different), apply
        # saturation model, equi and rlag are specific to fuel type and
        # location
        for i in range(k):

            # initialize equilibrium with current moisture
            m_i[:, :] = m_ext[:, :, i]
            equi[:, :] = m_i
            rlag[:, :] = 0.0
            model_ids[:] = 4

            # on grid locations where there is rain, modify equilibrium
            has_rain = r > self.r0
            no_rain = np.logical_not(has_rain)

            equi[has_rain] = self.S + dS[has_rain]
            rlag[has_rain] = 1.0 / self.Trk * (1.0 - np.exp(- (r[has_rain] - self.r0) / self.rk))
            model_ids[has_rain] = 3

            # equilibrium is selected according to current moisture level
            is_drying = np.logical_and(no_rain, m_i > EdA)
            equi[is_drying] = EdA[is_drying]
            model_ids[is_drying] = 1

            is_wetting = np.logical_and(no_rain, m_i < EwA)
            equi[is_wetting] = EwA[is_wetting]
            model_ids[is_wetting] = 2

            rlag[no_rain] = 1.0 / Tk[i]

            dead_zone = (model_ids == 4)

            # optional inspection of changes at given position
            #dg_pos = (86,205)
            #mi_old = m_i[dg_pos]

            # select appropriate integration method according to change for each fuel
            # and location
            rlag *= dt
            change = rlag
            big_change = change > 0.01
            m_i[big_change] += (equi[big_change] - m_i[big_change]) * (1.0 - np.exp(-change[big_change]))
            small_change = np.logical_not(big_change)
            m_i[small_change] += (equi[small_change] - m_i[small_change]) * change[small_change] * (
                1.0 - 0.5 * change[small_change])

            #print('Diag at 86,205 fuel %d: model_id %d equi %g rlag %g change %g value %g -> %g' %
            #        (i, model_ids[dg_pos], equi[dg_pos], rlag[dg_pos], change[dg_pos], mi_old, m_i[dg_pos]))

            # store in state matrix
            m_ext[:, :, i] = m_i

            # update model state covariance if requested using the old state (the jacobian must be computed as well)
            if mQ is not None:

                # partial m_i/partial m_i
                Jii[big_change] = np.exp(-change[big_change])
                Jii[small_change] = 1.0 - change[small_change] * (1.0 - 0.5 * change[small_change])
                Jii[dead_zone] = 1.0
                J[:, :, i, i] = Jii

                # partial m_i/partial deltaE
                Jii[:] = 0.0
                norain_big = np.logical_and(no_rain, big_change)
                norain_small = np.logical_and(no_rain, small_change)
                Jii[norain_big] = 1.0 - np.exp(-change[norain_big])
                Jii[norain_small] = change[norain_small] * (1.0 - 0.5 * change[norain_small])
                Jii[dead_zone] = 0.0
                J[:, :, i, k] = Jii

                # partial m_i / partial deltaS
                Jii[:] = 0.0
                rain_big = np.logical_and(has_rain, big_change)
                rain_small = np.logical_and(has_rain, small_change)
                Jii[rain_big] = 1.0 - np.exp(-change[rain_big])
                Jii[rain_small] = change[rain_small] * (1.0 - 0.5 * change[rain_small])
                Jii[dead_zone] = 0.0
                J[:, :, i, k+1] = Jii

        # transformed to run in-place with one pre-allocated temporary
        dom_shape = P[:, :, 0, 0].shape
        for i in range(dom_shape[0]):
            for j in range(dom_shape[1]):
                # Ps = Js P[s,:,:] Js^T
                Ps = P[i, j, :, :]
                Js = J[i, j, :, :]
                np.dot(Js, Ps, P2)
                np.dot(P2, Js.T, Ps)

                # P[s,:,:] = Js P[s,:,:] Js^T + mQ
                Ps += mQ
                P[i, j, :, :] = Ps

    def get_state(self):
        """
        Return the current state for all grid points. READ-ONLY under normal circumstances.
        """
        return self.m_ext

    def get_state_covar(self):
        """
        Return the state covariance for all grid points. READ-ONLY under normal circumstances.
        """
        return self.P

    def kalman_update(self, O, V, fuel_types, Kg=None):
        """
        Updates the state using the observation at the grid point.

          O - the observations [shape grid_size x len(fuel_types)]
          V - the (diagonal) variance matrix of the measurements [shape grid_size x len(fuel_types) x len(fuel_types)]
          fuel_types - the fuel types for which the observations exist (used to construct observation vector)

        """
        m_ext, P, H, P2 = self.m_ext, self.P, self.H, self.P2
        H[:] = 0.0

        # construct an observation operator tailored to observed fuel types
        for i in fuel_types:
            H[i, i] = 1.0
        Ho = H[fuel_types, :]

        for i, j in np.ndindex(P[:, :, 0, 0].shape):
            # re-use P2 to store forecast covariance for position s
            P2[:, :] = P[i, j, :, :]

            if np.any(np.diag(P2) < 0.0):
                print("ERROR: negative diagonal %s" % (str(np.diag(P2))))

            # compute Kalman gain
            I = np.dot(np.dot(Ho, P2), Ho.T) + V[i, j, :, :]
            K = np.dot(np.dot(P2, Ho.T), np.linalg.inv(I))

            if K[1, 0] > 1.0 or K[1, 0] < 0.0:
                print("ERROR at %s, Kalman gain %g out of bounds! I=%g V=%g" % (
                    str((i, j)), K[fuel_type, 0], I, V[s[0], s[1], 0, 0]))

            # update state and state covariance
            m_ext[i, j, :] += np.dot(K, O[i, j, :] - m_ext[i, j, fuel_types])
            P[i, j, :, :] -= np.dot(np.dot(K, Ho), P2)

            if Kg is not None:
                Kg[i, j, :] = K[:, 0]

    def kalman_update_single(self, O, V, fuel_type, Kg=None):
        """
        Perform an optimized Kalman update assuming there is only one fuel type.
        """
        m_ext, P, P2 = self.m_ext, self.P, self.P2

        #print("******* GRID MODEL ********")
        #print("grid: O = %.18e  V = %.18e   fuel_type = %d" % (O[0,0], V[0,0,0,0], fuel_type))

        dom_shape = P[:, :, 0, 0].shape
        for i in range(dom_shape[0]):
            for j in range(dom_shape[1]):
                P2[:, :] = P[i, j, :, :]
                #print(P2)

                if np.any(np.diag(P2) < 0.0):
                    print("ERROR: negative diagonal %s" % (str(np.diag(P2))))

                I = P2[fuel_type, fuel_type] + V[i, j, 0, 0]
                K = P2[:, fuel_type] / I

                if K[fuel_type] > 1.0 or K[fuel_type] < 0.0:
                    print("WARN: at %s, Kalman gain %g out of bounds! I=%g V=%g" % (
                        str((i, j)), K[fuel_type], I, V[i, j, 0, 0]))

                m_ext[i, j, :] += K * (O[i, j, 0] - m_ext[i, j, fuel_type])

                # NOTE: this update may produce matrices that are not symmetric positive definite
                P2 -= np.dot(K[:, np.newaxis], P2[fuel_type:fuel_type + 1, :])
                P[i, j, :, :] = P2

                if Kg is not None:
                    Kg[i, j, :] = K

    def kalman_update_single2(self, O, V, fuel_type, Kg):

        m_ext, P = self.m_ext, self.P

        if np.any(O < 0.0):
            print("ERROR: found negative observations!")
            sys.exit(5)

        # I is dom_shape
        I = P[:, :, fuel_type, fuel_type] + V[:, :, 0, 0]
        # K is dom_shape x dim
        Kg[:, :, :] = P[:, :, :, fuel_type] / I[:, :, np.newaxis]

        if np.any(np.logical_or(Kg[:, :, fuel_type] > 1.0, Kg[:, :, fuel_type] < 0.0)):
            print("ERROR: some Kalman gains for 10-hr fuel are out of bounds.")

        # m_ext is dom_shape x dim, O[:,:,0] and m_ext[:,:,fuel_type] are dom_shape
        m_ext += Kg * (O - m_ext[:, :, fuel_type:fuel_type + 1])

        # try to do a tensor product here
        # Kg[:,:,:,newaxis] is shape dom_shape x dim x 1
        # P[:,:,fuel_type:fuel_type+1,:] is dom_shape x 1 x dim
        dom_shape = P[:, :, 0, 0].shape
        for i in range(dom_shape[0]):
            for j in range(dom_shape[1]):
                P[i, j, :, :] -= np.dot(Kg[i, j, :, np.newaxis], P[i, j, fuel_type:fuel_type + 1, :])

<|endoftext|>


<commit_before>from PIL import Image
import random
import os

class BackgroundGenerator(object):

    def __init__(self,width,height,path_to_backrounds):
        """
        Return a background for a Target

        :param width: width of background
        :type width: int
        :param height: height of background
        :type height: int
        :param path_to_backrounds: location of backgrounds directory on computer
        """
        self.width=width
        self.height=height
        self.path_to_backrounds=path_to_backrounds

    def generate_background(self):
        try:
            os.remove(self.path_to_backrounds + "/.DS_Store")
        except:
            pass
        background = Image.open(self.path_to_backrounds + "/" + random.choice(os.listdir(self.path_to_backrounds)))
        crop_coordinates = self.get_random_coordinates(background.width,background.height)
        background = background.crop(crop_coordinates)
        return background

    def get_random_coordinates(self,background_width,background_height):
        x1 = random.randint(0,background_width-self.width)
        y1 = random.randint(0,background_height-self.height)
        x2 = x1+self.width
        y2 = y1+self.height
        return (x1,y1,x2,y2)
<commit_msg>added methods to background.py --> should make it more useful for TargetField<commit_after>from PIL import Image
import random, os

class BackgroundGenerator(object):

    def __init__(self,path_to_backrounds):
        """
        Create a background for a Target or TargetField

        :param path_to_backrounds: location of backgrounds directory
        """
        self.path_to_backrounds=path_to_backrounds

    def generate_full_background(self):
        """
        Return a full sized background
        """
        image_list = os.listdir(self.path_to_backrounds)
        image_list = [f for f in image_list if not f.startswith(".")]

        background = Image.open(self.path_to_backrounds + "/" + random.choice(image_list))
        return background

    def generate_specific_background(self,specified_width,specified_height):
        """
        Return a background with a specified size

        :param width: width of background in pixels
        :type width: int
        :param height: height of background in pixels
        :type height: int
        """
        background = self.generate_full_background()
        crop_coordinates = self.get_random_coordinates(background.width,background.height,specified_width,specified_height)
        background = background.crop(crop_coordinates)
        return background

    def get_random_coordinates(self,background_width,background_height,specified_width,specified_height):
        x1 = random.randint(0,background_width-specified_width)
        y1 = random.randint(0,background_height-specified_height)
        x2 = x1+specified_width
        y2 = y1+specified_height
        return (x1,y1,x2,y2)
<|endoftext|>


<commit_before>import sys
from datetime import date, timedelta

from setuptools_scm import integration
from setuptools_scm.utils import do, has_command
from setuptools_scm import git
import pytest
from datetime import datetime
from os.path import join as opj
from setuptools_scm.file_finder_git import git_find_files

SCRIPT = u"""
#!/usr/bin/env python

from setuptools import setup

def calver_by_date():
    from setuptools_scm.version import guess_next_version, format_version, trace
    def version_scheme(version):
        version_ = version.branch  # FIXME set to None
        if version.branch.startswith('release-'):
            trace('in release branch')
            version_ = version.branch.replace('relese-', '')
        elif version.dirty:
            # get next release
            trace('in dirty')
            version_ = guess_next_version(version.tag)
        elif version.exact:
            trace('in exact')
            version_ = version.format_with("{tag}")
        else:
            trace('in default fallback')
            version_ = format_version(version, version_scheme=None, local_scheme=None)
        return version_

    return {'version_scheme': version_scheme}

setup(use_scm_version=calver_by_date)

"""

def add_test_file(working_dir):
    working_dir.write('test.txt', 'hi test file')

def date_to_str(date_, days_offset=0):
    date_ = date_ - timedelta(days=days_offset)
    return date_.strftime("%y.%-m.%-d")

@pytest.fixture
def today(days_offset=0):
    date_ = date.today()
    return date_to_str(date_)

@pytest.fixture
def wd(wd, monkeypatch):
    monkeypatch.delenv("HOME", raising=False)
    monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
    wd("git init")
    wd("git config user.email test@example.com")
    wd('git config user.name "a test"')
    wd.write("setup.py", SCRIPT)
    wd("git add .")
    wd("git commit -a -m test-{reason}")
    return wd

def test_seq_clean(wd):
    wd("git tag 0.1")
    res = do((sys.executable, "setup.py", "--version"), wd.cwd)
    assert res == "0.1"

def test_date_clean(wd, today):
    wd(f"git tag {today}")
    res = do((sys.executable, "setup.py", "--version"), wd.cwd)
    assert res == today

def test_seq_uncommited(wd):
    wd("git tag 0.1")
    add_test_file(wd)
    res = do((sys.executable, "setup.py", "--version"), wd.cwd)
    assert res == "0.2"

def test_date_uncommited(wd, today):
    wd(f"git tag {today}")
    add_test_file(wd)
    res = do((sys.executable, "setup.py", "--version"), wd.cwd)
    assert res == "0.2"<commit_msg>Added schema to version and set in the cfg. Moving tests to test_version file<commit_after><|endoftext|>


<commit_before>import json
import logging
import math
import os
import sys
from tempfile import NamedTemporaryFile

parser = sys.argv[1]
sys.path.append(parser)
sys.path.append(os.path.join(os.path.dirname(parser), 'ejml-0.23.jar'))

from edu.stanford.nlp.process import Morphology, PTBTokenizer, WordTokenFactory
from edu.stanford.nlp.parser.common import ParserConstraint
from edu.stanford.nlp.parser.lexparser import Options
from edu.stanford.nlp.parser.lexparser import LexicalizedParser
from edu.stanford.nlp.ling import Sentence
from edu.stanford.nlp.trees import PennTreebankLanguagePack

from java.io import StringReader
from java.util.regex import Pattern

class StanfordParser:

    @staticmethod
    def get_constraints(sentence, pos):
        constraints = []
        length = len(sentence)
        if pos == 'n':
            constraints.append(
                ParserConstraint(0, length, Pattern.compile("NP.*")))
        return constraints

    def __init__(self, parser_file,
                 parser_options=['-maxLength', '80',
                                 '-retainTmpSubcategories']):

        """@param parser_file: path to the serialised parser model
            (e.g. englishPCFG.ser.gz)
        @param parser_options: options
        """

        assert os.path.exists(parser_file)
        options = Options()
        options.setOptions(parser_options)
        self.lp = LexicalizedParser.getParserFromFile(parser_file, options)
        tlp = PennTreebankLanguagePack()
        self.gsf = tlp.grammaticalStructureFactory()
        self.lemmer = Morphology()
        self.word_token_factory = WordTokenFactory()
        self.parser_query = None

    def tokenize(self, text):
        reader = StringReader(text)
        tokeniser = PTBTokenizer(reader, self.word_token_factory, None)
        tokens = tokeniser.tokenize()
        return tokens

    def get_parse(self, sentence):
        tokens = [unicode(x) for x in self.tokenize(sentence)]
        parse = self.lp.apply(Sentence.toWordList(tokens))
        return parse

    def get_grammatical_structure(self, parse):
        return self.gsf.newGrammaticalStructure(parse)

    def get_kbest(self, query, k=3):
        for candidate_tree in query.getKBestPCFGParses(k):
            parse = candidate_tree.object()
            prob = math.e ** candidate_tree.score()
            yield prob, parse

    def parse(self, sentence):
        return self.parse_with_constraints(sentence, None)

    def parse_with_constraints(self, sentence, constraints):
        # logging.debug("getting query...")
        query = self.lp.parserQuery()
        if constraints is not None:
            query.setConstraints(constraints)
        # logging.debug("tokenizing...")
        toks = self.tokenize(sentence)
        # logging.debug("running parse...")
        query.parse(toks)
        # logging.debug("getting best...")
        parse = query.getBestParse()
        # logging.debug("getting gs...")
        gs = self.get_grammatical_structure(parse)
        # dependencies = gs.typedDependenciesCollapsed()
        dependencies = gs.typedDependenciesCCprocessed()
        return parse, gs, dependencies

    def parse_sens(self, in_file, out_file, log=False):
        logging.debug("reading input...")
        with open(in_file) as in_obj:
            sens = json.load(in_obj)
        parsed_sens = []
        if log:
            log_file = NamedTemporaryFile(dir="/tmp", delete=False)
        for c, sentence in enumerate(sens):
            if log and c % 100 == 0:
                log_file.write("parsed {0} sentences\n".format(c))
                log_file.flush()
            parse, _, dependencies = self.parse(sentence)

            dep_strings = map(unicode, dependencies)
            parsed_sens.append({
                'sen': sentence,
                'deps': dep_strings})

        with open(out_file, 'w') as out:
            json.dump(parsed_sens, out)

    def parse_definitions(self, in_file, out_file):
        with open(in_file) as in_obj:
            entries = json.load(in_obj)
        with NamedTemporaryFile(dir="/tmp", delete=False) as log_file:
            for c, entry in enumerate(entries):
                if c % 100 == 0:
                    log_file.write("parsed {0} entries\n".format(c))
                    log_file.flush()
                for sense in entry['senses']:
                    sentence = sense['definition']
                    if sentence is None:
                        continue
                    # sentence += '.'  # fixes some parses and ruins others
                    pos = sense['pos']
                    constraints = StanfordParser.get_constraints(sentence, pos)
                    # logging.info('sen: {0}, constraints: {1}'.format(
                    #    sentence, constraints))
                    try:
                        parse, _, dependencies = self.parse_with_constraints(
                            sentence, constraints)
                    except:
                        sys.stderr.write(
                            u'parse failed on sentence: {0}'.format(
                                sentence).encode('utf-8'))
                        dep_strings = []
                    else:
                        dep_strings = map(unicode, dependencies)

                    sense['definition'] = {
                        'sen': sentence,
                        'deps': dep_strings}

        with open(out_file, 'w') as out:
            json.dump(entries, out)

def test():
    logging.warning("running test, not main!")
    parser = StanfordParser(sys.argv[2])

    # dv_model = parser.lp.reranker.getModel()
    # print dv_model

    # sentence = 'the size of a radio wave used to broadcast a radio signal'
    sentence = 'a man whose job is to persuade people to buy his company\'s \
        products.'
    pos = 'n'
    parse, gs, dependencies = parser.parse_with_constraints(
        sentence, StanfordParser.get_constraints(sentence, pos))

    print type(parse), type(gs)
    print parse.pennPrint()
    print "\n".join(map(str, dependencies))

def main():
    parser_file, in_file, out_file, is_defs, loglevel = sys.argv[2:7]
    logging.basicConfig(
        level=int(loglevel),
        format="%(asctime)s : " +
        "%(module)s (%(lineno)s) - %(levelname)s - %(message)s")
    logging.debug("initializing parser...")
    parser = StanfordParser(parser_file)
    if int(is_defs):
        parser.parse_definitions(in_file, out_file)
    else:
        parser.parse_sens(in_file, out_file)

if __name__ == "__main__":
    main()
    # test()
<commit_msg>some extra logging<commit_after>import json
import logging
import math
import os
import sys
from tempfile import NamedTemporaryFile

parser = sys.argv[1]
sys.path.append(parser)
sys.path.append(os.path.join(os.path.dirname(parser), 'ejml-0.23.jar'))

from edu.stanford.nlp.process import Morphology, PTBTokenizer, WordTokenFactory
from edu.stanford.nlp.parser.common import ParserConstraint
from edu.stanford.nlp.parser.lexparser import Options
from edu.stanford.nlp.parser.lexparser import LexicalizedParser
from edu.stanford.nlp.ling import Sentence
from edu.stanford.nlp.trees import PennTreebankLanguagePack

from java.io import StringReader
from java.util.regex import Pattern

class StanfordParser:

    @staticmethod
    def get_constraints(sentence, pos):
        constraints = []
        length = len(sentence)
        if pos == 'n':
            constraints.append(
                ParserConstraint(0, length, Pattern.compile("NP.*")))
        return constraints

    def __init__(self, parser_file,
                 parser_options=['-maxLength', '80',
                                 '-retainTmpSubcategories']):

        """@param parser_file: path to the serialised parser model
            (e.g. englishPCFG.ser.gz)
        @param parser_options: options
        """

        assert os.path.exists(parser_file)
        options = Options()
        options.setOptions(parser_options)
        self.lp = LexicalizedParser.getParserFromFile(parser_file, options)
        tlp = PennTreebankLanguagePack()
        self.gsf = tlp.grammaticalStructureFactory()
        self.lemmer = Morphology()
        self.word_token_factory = WordTokenFactory()
        self.parser_query = None

    def tokenize(self, text):
        reader = StringReader(text)
        tokeniser = PTBTokenizer(reader, self.word_token_factory, None)
        tokens = tokeniser.tokenize()
        return tokens

    def get_parse(self, sentence):
        tokens = [unicode(x) for x in self.tokenize(sentence)]
        parse = self.lp.apply(Sentence.toWordList(tokens))
        return parse

    def get_grammatical_structure(self, parse):
        return self.gsf.newGrammaticalStructure(parse)

    def get_kbest(self, query, k=3):
        for candidate_tree in query.getKBestPCFGParses(k):
            parse = candidate_tree.object()
            prob = math.e ** candidate_tree.score()
            yield prob, parse

    def parse(self, sentence):
        return self.parse_with_constraints(sentence, None)

    def parse_with_constraints(self, sentence, constraints):
        # logging.debug("getting query...")
        query = self.lp.parserQuery()
        if constraints is not None:
            query.setConstraints(constraints)
        # logging.debug("tokenizing...")
        toks = self.tokenize(sentence)
        # logging.debug("running parse...")
        query.parse(toks)
        # logging.debug("getting best...")
        parse = query.getBestParse()
        # logging.debug("getting gs...")
        gs = self.get_grammatical_structure(parse)
        # dependencies = gs.typedDependenciesCollapsed()
        dependencies = gs.typedDependenciesCCprocessed()
        return parse, gs, dependencies

    def parse_sens(self, in_file, out_file, log=False):
        logging.debug("reading input...")
        with open(in_file) as in_obj:
            sens = json.load(in_obj)
        parsed_sens = []
        if log:
            log_file = NamedTemporaryFile(dir="/tmp", delete=False)
        for c, sentence in enumerate(sens):
            if log and c % 100 == 0:
                log_file.write("parsed {0} sentences\n".format(c))
                log_file.flush()
            parse, _, dependencies = self.parse(sentence)

            dep_strings = map(unicode, dependencies)
            parsed_sens.append({
                'sen': sentence,
                'deps': dep_strings})

        with open(out_file, 'w') as out:
            json.dump(parsed_sens, out)

    def parse_definitions(self, in_file, out_file):
        with open(in_file) as in_obj:
            logging.info("loading input...")
            entries = json.load(in_obj)
            logging.info("done!")
        with NamedTemporaryFile(dir="/tmp", delete=False) as log_file:
            logging.info('logging to {0}'.format(log_file.name))
            for c, entry in enumerate(entries):
                # log_file.write(
                #     'entry: {0}\n'.format(entry['hw']).encode('utf-8'))
                # log_file.flush()
                if c % 100 == 0:
                    log_file.write("parsed {0} entries\n".format(c))
                    log_file.flush()
                for sense in entry['senses']:
                    sentence = sense['definition']
                    if sentence is None:
                        continue
                    # sentence += '.'  # fixes some parses and ruins others
                    pos = sense['pos']
                    constraints = StanfordParser.get_constraints(sentence, pos)
                    try:
                        parse, _, dependencies = self.parse_with_constraints(
                            sentence, constraints)
                    except:
                        sys.stderr.write(
                            u'parse failed on sentence: {0}'.format(
                                sentence).encode('utf-8'))
                        dep_strings = []
                    else:
                        dep_strings = map(unicode, dependencies)

                    sense['definition'] = {
                        'sen': sentence,
                        'deps': dep_strings}

        with open(out_file, 'w') as out:
            json.dump(entries, out)

def test():
    logging.warning("running test, not main!")
    parser = StanfordParser(sys.argv[2])

    # dv_model = parser.lp.reranker.getModel()
    # print dv_model

    # sentence = 'the size of a radio wave used to broadcast a radio signal'
    sentence = 'a man whose job is to persuade people to buy his company\'s \
        products.'
    pos = 'n'
    parse, gs, dependencies = parser.parse_with_constraints(
        sentence, StanfordParser.get_constraints(sentence, pos))

    print type(parse), type(gs)
    print parse.pennPrint()
    print "\n".join(map(str, dependencies))

def main():
    parser_file, in_file, out_file, is_defs, loglevel = sys.argv[2:7]
    logging.basicConfig(
        level=int(loglevel),
        format="%(asctime)s : " +
        "%(module)s (%(lineno)s) - %(levelname)s - %(message)s")
    logging.info("initializing parser...")
    parser = StanfordParser(parser_file)
    logging.info("done!")
    if int(is_defs):
        parser.parse_definitions(in_file, out_file)
    else:
        parser.parse_sens(in_file, out_file)

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


<commit_before>""" Helpers for training """
from math import pi

import tensorflow as tf
from tensorflow.python.framework import ops # pylint: disable=no-name-in-module
from tensorflow.python.ops import math_ops # pylint: disable=no-name-in-module
from tensorflow.math import sin, asin, floor # pylint: disable=import-error

def piecewise_constant(global_step, *args, **kwargs):
    """ Constant learning rate decay (uses global_step param instead of x) """
    return tf.train.piecewise_constant(global_step, *args, **kwargs)

def cyclic_learning_rate(learning_rate, global_step, max_lr=0.1, step_size=10, mode='triangular', name=None):
    """Applies cyclic learning rate (CLR).
    https://arxiv.org/pdf/1506.01186.pdf.

    This function varies the learning rate between the
    minimum (learning_rate) and the maximum (max_lr).
    It returns the cyclic learning rate.

    Args:
    learning_rate: A scalar `float32` or `float64` `Tensor` or a
        Python number. The minimun learning rate boundary.
    global_step: A scalar `int32` or `int64` `Tensor` or a Python number.
        Global step to use for the cyclic computation.  Must not be negative.
    max_lr:  A scalar. The maximum learning rate boundary.
        Default 0.1.
    step_size: A scalar. The number of iterations in half a cycle.
        Default 10.
    mode:
        If 'sin' or 'sine' or 'sine wave':
            Learning rate changes as a sine wave, starting
            from (max_lr-learning_rate)/2 then decreasing to `learning_rate`.
        If 'triangular' or 'triangular wave' or 'zigzag':
            Default, linearly increasing then linearly decreasing the
            learning rate at each cycle. Learning rate starting
            from (max_lr-learning_rate)/2 then decreasing to `learning_rate`.
        If 'sawtooth' or 'saw' or 'sawtooth wave' or 'saw wave':
            Linearly increasing to `max_lr` then again increasing from `learning_rate`
            learning rate at each cycle. Learning rate starting from `learning_rate`
            then increasing.
    name: String.  Optional name of the operation.  Defaults to
        'CyclicLearningRate'.

    Returns:
    A scalar `Tensor` of the same type as `learning_rate`. The cyclic
    learning rate.
    """
    with ops.name_scope_v2(name or "CyclicLearningRate"):
        learning_rate = ops.convert_to_tensor(learning_rate, name="learning_rate")
        dtype = learning_rate.dtype
        global_step = math_ops.cast(global_step, dtype)
        step_size = math_ops.cast(step_size, dtype)

        if mode in ('sin', 'sine', 'sine wave'):
        # (max_lr - learning_rate)/2 * sin(pi * global_step/step_size - pi) + (max_lr + learning_rate)/2
            first_factor = math_ops.divide(math_ops.subtract(learning_rate, max_lr), 2.)
            second_factor = sin(math_ops.divide(math_ops.multiply(pi, global_step), step_size))
            second_comp = math_ops.divide(math_ops.add(learning_rate, max_lr), 2.)
            return math_ops.add(math_ops.multiply(first_factor, second_factor), second_comp)
        if mode in ('triangular', 'triangular wave', 'zigzag'):
        # ((max_lr - learning_rate)/pi) * asin(sin(((2 * -pi)/step_size) * global_step)) + (max_lr + learning_rate)/2
            first_factor = math_ops.divide(math_ops.subtract(max_lr, learning_rate), pi)
            inside_sin = math_ops.multiply(math_ops.divide(math_ops.multiply(2., -pi), step_size), global_step)
            second_factor = asin(sin(inside_sin))
            second_comp = math_ops.divide(math_ops.add(learning_rate, max_lr), 2.)
            return math_ops.add(math_ops.multiply(first_factor, second_factor), second_comp)
        if mode in ('sawtooth', 'saw', 'sawtooth wave', 'saw wave'):
        # (max_lr - learning_rate) * (global_step/step_size - floor(global_step/step_size)) + learning_rate
            first_factor = math_ops.subtract(max_lr, learning_rate)
            divided_global_step = math_ops.divide(global_step, step_size)
            second_factor = math_ops.subtract(divided_global_step, floor(divided_global_step))
            return math_ops.add(math_ops.multiply(first_factor, second_factor), learning_rate)
<commit_msg>Refactor code and fix doc<commit_after>""" Helpers for training """
from math import pi

import tensorflow as tf
from tensorflow.python.framework import ops # pylint: disable=no-name-in-module
from tensorflow.python.ops import math_ops # pylint: disable=no-name-in-module
from tensorflow.math import sin, asin, floor # pylint: disable=import-error

def piecewise_constant(global_step, *args, **kwargs):
    """ Constant learning rate decay (uses global_step param instead of x) """
    return tf.train.piecewise_constant(global_step, *args, **kwargs)

def cyclic_learning_rate(learning_rate, global_step, max_lr=0.1, step_size=10, mode='triangular', name=None):
    """Applies cyclic learning rate (CLR).
    https://arxiv.org/pdf/1506.01186.pdf.

    This function varies the learning rate between the
    minimum (learning_rate) and the maximum (max_lr).
    It returns the cyclic learning rate.

    Args:
    learning_rate: A scalar `float32` or `float64` `Tensor` or a
        Python number. The minimun learning rate boundary.
    global_step: A scalar `int32` or `int64` `Tensor` or a Python number.
        Global step to use for the cyclic computation.  Must not be negative.
    max_lr:  A scalar. The maximum learning rate boundary.
        Default 0.1.
    step_size: A scalar. The number of iterations in half a cycle.
        Default 10.
    mode:
        If 'sin' or 'sine' or 'sine wave':
            Learning rate changes as a sine wave, starting
            from (max_lr-learning_rate)/2 then decreasing to `learning_rate`.
        If 'triangular' or 'triangular wave' or 'zigzag':
            Default, linearly increasing then linearly decreasing the
            learning rate at each cycle. Learning rate starting
            from (max_lr-learning_rate)/2 then decreasing to `learning_rate`.
        If 'sawtooth' or 'saw' or 'sawtooth wave' or 'saw wave':
            Learning rate linearly increasing from `learning_rate` to `max_lr`
            and then sharply drops to `learning_rate` at each cycle.
            Learning rate starting from `learning_rate` then increasing.
    name: String.  Optional name of the operation.  Defaults to
        'CyclicLearningRate'.

    Returns:
    A scalar `Tensor` of the same type as `learning_rate`. The cyclic
    learning rate.
    """
    with ops.name_scope_v2(name or "CyclicLearningRate"):
        learning_rate = ops.convert_to_tensor(learning_rate, name="learning_rate")
        dtype = learning_rate.dtype
        global_step = math_ops.cast(global_step, dtype)
        step_size = math_ops.cast(step_size, dtype)

        if mode in ('sin', 'sine', 'sine wave'):
        # (max_lr - learning_rate)/2 * sin(pi * global_step/step_size - pi) + (max_lr + learning_rate)/2
            first_factor = math_ops.divide(math_ops.subtract(learning_rate, max_lr), 2.)
            second_factor = sin(math_ops.divide(math_ops.multiply(pi, global_step), step_size))
            second_comp = math_ops.divide(math_ops.add(learning_rate, max_lr), 2.)
        elif mode in ('triangular', 'triangular wave', 'zigzag'):
        # ((max_lr - learning_rate)/pi) * asin(sin(((2 * -pi)/step_size) * global_step)) + (max_lr + learning_rate)/2
            first_factor = math_ops.divide(math_ops.subtract(max_lr, learning_rate), pi)
            inside_sin = math_ops.multiply(math_ops.divide(math_ops.multiply(2., -pi), step_size), global_step)
            second_factor = asin(sin(inside_sin))
            second_comp = math_ops.divide(math_ops.add(learning_rate, max_lr), 2.)
        elif mode in ('sawtooth', 'saw', 'sawtooth wave', 'saw wave'):
        # (max_lr - learning_rate) * (global_step/step_size - floor(global_step/step_size)) + learning_rate
            first_factor = math_ops.subtract(max_lr, learning_rate)
            divided_global_step = math_ops.divide(global_step, step_size)
            second_factor = math_ops.subtract(divided_global_step, floor(divided_global_step))
            return math_ops.add(math_ops.multiply(first_factor, second_factor), learning_rate)
        return math_ops.add(math_ops.multiply(first_factor, second_factor), second_comp)
<|endoftext|>


<commit_before># Copyright 2017-2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
    'name': 'Partner Company Type',
    'summary': 'Adds a company type to partner that are companies',
    'version': '12.0.1.0.0',
    'license': 'AGPL-3',
    'author': 'ACSONE SA/NV,Odoo Community Association (OCA)',
    'website': 'https://github.com/OCA/partner-contact',
    'depends': [
        'base',
        'contacts',
    ],
    'data': [
        'security/res_partner_company_type.xml',
        'views/res_partner_company_type.xml',
        'views/res_partner.xml',
    ],
    'demo': [
        'demo/res_partner_company_type.xml',
    ],
}
<commit_msg>partner_company_type 12.0.1.1.0<commit_after># Copyright 2017-2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
    'name': 'Partner Company Type',
    'summary': 'Adds a company type to partner that are companies',
    'version': '12.0.1.1.0',
    'license': 'AGPL-3',
    'author': 'ACSONE SA/NV,Odoo Community Association (OCA)',
    'website': 'https://github.com/OCA/partner-contact',
    'depends': [
        'base',
        'contacts',
    ],
    'data': [
        'security/res_partner_company_type.xml',
        'views/res_partner_company_type.xml',
        'views/res_partner.xml',
    ],
    'demo': [
        'demo/res_partner_company_type.xml',
    ],
}
<|endoftext|>


<commit_before># Copyright 2018 Google LLC
#
# 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
#
#     https://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.

"""Load dependencies needed to compile and test the google-cloud-cpp library."""

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")

def google_cloud_cpp_deps():
    """Loads dependencies need to compile the google-cloud-cpp library.

    Application developers can call this function from their WORKSPACE file
    to obtain all the necessary dependencies for google-cloud-cpp, including
    gRPC and its dependencies. This function only loads dependencies that
    have not been previously loaded, allowing application developers to
    override the version of the dependencies they want to use.
    """

    # Load platforms, we use it directly
    if "platforms" not in native.existing_rules():
        http_archive(
            name = "platforms",
            urls = [
                "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.5/platforms-0.0.5.tar.gz",
                "https://github.com/bazelbuild/platforms/releases/download/0.0.5/platforms-0.0.5.tar.gz",
            ],
            sha256 = "379113459b0feaf6bfbb584a91874c065078aa673222846ac765f86661c27407",
        )

    # Load rules_cc, used by googletest
    if "rules_cc" not in native.existing_rules():
        http_archive(
            name = "rules_cc",
            urls = [
                "https://github.com/bazelbuild/rules_cc/releases/download/0.0.1/rules_cc-0.0.1.tar.gz",
            ],
            sha256 = "4dccbfd22c0def164c8f47458bd50e0c7148f3d92002cdb459c2a96a68498241",
        )

    # Load Abseil
    if "com_google_absl" not in native.existing_rules():
        http_archive(
            name = "com_google_absl",
            strip_prefix = "abseil-cpp-20211102.0",
            urls = [
                "https://github.com/abseil/abseil-cpp/archive/20211102.0.tar.gz",
            ],
            sha256 = "dcf71b9cba8dc0ca9940c4b316a0c796be8fab42b070bb6b7cab62b48f0e66c4",
        )

    # Load a version of googletest that we know works.
    if "com_google_googletest" not in native.existing_rules():
        http_archive(
            name = "com_google_googletest",
            strip_prefix = "googletest-release-1.11.0",
            urls = [
                "https://github.com/google/googletest/archive/release-1.11.0.tar.gz",
            ],
            sha256 = "b4870bf121ff7795ba20d20bcdd8627b8e088f2d1dab299a031c1034eddc93d5",
        )

    # Load a version of benchmark that we know works.
    if "com_google_benchmark" not in native.existing_rules():
        http_archive(
            name = "com_google_benchmark",
            strip_prefix = "benchmark-1.6.1",
            urls = [
                "https://github.com/google/benchmark/archive/v1.6.1.tar.gz",
            ],
            sha256 = "6132883bc8c9b0df5375b16ab520fac1a85dc9e4cf5be59480448ece74b278d4",
        )

    # Load the googleapis dependency.
    if "com_google_googleapis" not in native.existing_rules():
        http_archive(
            name = "com_google_googleapis",
            urls = [
                "https://github.com/googleapis/googleapis/archive/cf88418a2dea51a1007792ad19739d53d4ee510e.tar.gz",
            ],
            strip_prefix = "googleapis-cf88418a2dea51a1007792ad19739d53d4ee510e",
            sha256 = "17c34d62592df2c684c19102079c4146faaafdba8244f517d2a52848a3e6b8b7",
            build_file = "@com_github_googleapis_google_cloud_cpp//bazel:googleapis.BUILD",
            # Scaffolding for patching googleapis after download. For example:
            #   patches = ["googleapis.patch"]
            # NOTE: This should only be used while developing with a new
            # protobuf message. No changes to `patches` should ever be
            # committed to the main branch.
            patch_tool = "patch",
            patch_args = ["-p1"],
            patches = [],
        )

    # Load protobuf.
    if "com_google_protobuf" not in native.existing_rules():
        http_archive(
            name = "com_google_protobuf",
            strip_prefix = "protobuf-3.20.1",
            urls = [
                "https://github.com/protocolbuffers/protobuf/archive/v3.20.1.tar.gz",
            ],
            sha256 = "8b28fdd45bab62d15db232ec404248901842e5340299a57765e48abe8a80d930",
        )

    # Load BoringSSL, this is used by gRPC, but as I write this (2021-06-03, circa gRPC-1.37.1), the version used by
    # gRPC does not compile with GCC-11
    if "boringssl" not in native.existing_rules():
        http_archive(
            name = "boringssl",
            # Use github mirror instead of https://boringssl.googlesource.com/boringssl
            # to obtain a boringssl archive with consistent sha256
            sha256 = "f0f433106f98f6f50ed6bbc2169f7c42dd73d13d0f09d431082519b5649903a6",
            strip_prefix = "boringssl-56eb68f64215738552be452e311da12047934ab4",
            urls = [
                "https://github.com/google/boringssl/archive/56eb68f64215738552be452e311da12047934ab4.tar.gz",
            ],
        )

    # Load gRPC and its dependencies, using a similar pattern to this function.
    if "com_github_grpc_grpc" not in native.existing_rules():
        http_archive(
            name = "com_github_grpc_grpc",
            strip_prefix = "grpc-1.46.1",
            urls = [
                "https://github.com/grpc/grpc/archive/v1.46.1.tar.gz",
            ],
            sha256 = "291db3c4e030164421b89833ee761a2e6ca06b1d1f8e67953df762665d89439d",
        )

    # We use the cc_proto_library() rule from @com_google_protobuf, which
    # assumes that grpc_cpp_plugin and grpc_lib are in the //external: module
    native.bind(
        name = "grpc_cpp_plugin",
        actual = "@com_github_grpc_grpc//src/compiler:grpc_cpp_plugin",
    )

    native.bind(
        name = "grpc_lib",
        actual = "@com_github_grpc_grpc//:grpc++",
    )

    # We need libcurl for the Google Cloud Storage client.
    if "com_github_curl_curl" not in native.existing_rules():
        http_archive(
            name = "com_github_curl_curl",
            urls = [
                "https://curl.haxx.se/download/curl-7.69.1.tar.gz",
            ],
            strip_prefix = "curl-7.69.1",
            sha256 = "01ae0c123dee45b01bbaef94c0bc00ed2aec89cb2ee0fd598e0d302a6b5e0a98",
            build_file = "@com_github_googleapis_google_cloud_cpp//bazel:curl.BUILD",
        )

    # We need the nlohmann_json library
    if "com_github_nlohmann_json" not in native.existing_rules():
        http_archive(
            name = "com_github_nlohmann_json",
            urls = [
                "https://github.com/nlohmann/json/releases/download/v3.10.5/include.zip",
            ],
            sha256 = "b94997df68856753b72f0d7a3703b7d484d4745c567f3584ef97c96c25a5798e",
            build_file = "@com_github_googleapis_google_cloud_cpp//bazel:nlohmann_json.BUILD",
        )

    # Load google/crc32c, a library to efficiently compute CRC32C checksums.
    if "com_github_google_crc32c" not in native.existing_rules():
        http_archive(
            name = "com_github_google_crc32c",
            strip_prefix = "crc32c-1.1.2",
            urls = [
                "https://github.com/google/crc32c/archive/1.1.2.tar.gz",
            ],
            sha256 = "ac07840513072b7fcebda6e821068aa04889018f24e10e46181068fb214d7e56",
            build_file = "@com_github_googleapis_google_cloud_cpp//bazel:crc32c.BUILD",
        )
<commit_msg>chore(deps): update dependency com_github_grpc_grpc to v1.46.2 (#8984)<commit_after># Copyright 2018 Google LLC
#
# 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
#
#     https://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.

"""Load dependencies needed to compile and test the google-cloud-cpp library."""

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")

def google_cloud_cpp_deps():
    """Loads dependencies need to compile the google-cloud-cpp library.

    Application developers can call this function from their WORKSPACE file
    to obtain all the necessary dependencies for google-cloud-cpp, including
    gRPC and its dependencies. This function only loads dependencies that
    have not been previously loaded, allowing application developers to
    override the version of the dependencies they want to use.
    """

    # Load platforms, we use it directly
    if "platforms" not in native.existing_rules():
        http_archive(
            name = "platforms",
            urls = [
                "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.5/platforms-0.0.5.tar.gz",
                "https://github.com/bazelbuild/platforms/releases/download/0.0.5/platforms-0.0.5.tar.gz",
            ],
            sha256 = "379113459b0feaf6bfbb584a91874c065078aa673222846ac765f86661c27407",
        )

    # Load rules_cc, used by googletest
    if "rules_cc" not in native.existing_rules():
        http_archive(
            name = "rules_cc",
            urls = [
                "https://github.com/bazelbuild/rules_cc/releases/download/0.0.1/rules_cc-0.0.1.tar.gz",
            ],
            sha256 = "4dccbfd22c0def164c8f47458bd50e0c7148f3d92002cdb459c2a96a68498241",
        )

    # Load Abseil
    if "com_google_absl" not in native.existing_rules():
        http_archive(
            name = "com_google_absl",
            strip_prefix = "abseil-cpp-20211102.0",
            urls = [
                "https://github.com/abseil/abseil-cpp/archive/20211102.0.tar.gz",
            ],
            sha256 = "dcf71b9cba8dc0ca9940c4b316a0c796be8fab42b070bb6b7cab62b48f0e66c4",
        )

    # Load a version of googletest that we know works.
    if "com_google_googletest" not in native.existing_rules():
        http_archive(
            name = "com_google_googletest",
            strip_prefix = "googletest-release-1.11.0",
            urls = [
                "https://github.com/google/googletest/archive/release-1.11.0.tar.gz",
            ],
            sha256 = "b4870bf121ff7795ba20d20bcdd8627b8e088f2d1dab299a031c1034eddc93d5",
        )

    # Load a version of benchmark that we know works.
    if "com_google_benchmark" not in native.existing_rules():
        http_archive(
            name = "com_google_benchmark",
            strip_prefix = "benchmark-1.6.1",
            urls = [
                "https://github.com/google/benchmark/archive/v1.6.1.tar.gz",
            ],
            sha256 = "6132883bc8c9b0df5375b16ab520fac1a85dc9e4cf5be59480448ece74b278d4",
        )

    # Load the googleapis dependency.
    if "com_google_googleapis" not in native.existing_rules():
        http_archive(
            name = "com_google_googleapis",
            urls = [
                "https://github.com/googleapis/googleapis/archive/cf88418a2dea51a1007792ad19739d53d4ee510e.tar.gz",
            ],
            strip_prefix = "googleapis-cf88418a2dea51a1007792ad19739d53d4ee510e",
            sha256 = "17c34d62592df2c684c19102079c4146faaafdba8244f517d2a52848a3e6b8b7",
            build_file = "@com_github_googleapis_google_cloud_cpp//bazel:googleapis.BUILD",
            # Scaffolding for patching googleapis after download. For example:
            #   patches = ["googleapis.patch"]
            # NOTE: This should only be used while developing with a new
            # protobuf message. No changes to `patches` should ever be
            # committed to the main branch.
            patch_tool = "patch",
            patch_args = ["-p1"],
            patches = [],
        )

    # Load protobuf.
    if "com_google_protobuf" not in native.existing_rules():
        http_archive(
            name = "com_google_protobuf",
            strip_prefix = "protobuf-3.20.1",
            urls = [
                "https://github.com/protocolbuffers/protobuf/archive/v3.20.1.tar.gz",
            ],
            sha256 = "8b28fdd45bab62d15db232ec404248901842e5340299a57765e48abe8a80d930",
        )

    # Load BoringSSL, this is used by gRPC, but as I write this (2021-06-03, circa gRPC-1.37.1), the version used by
    # gRPC does not compile with GCC-11
    if "boringssl" not in native.existing_rules():
        http_archive(
            name = "boringssl",
            # Use github mirror instead of https://boringssl.googlesource.com/boringssl
            # to obtain a boringssl archive with consistent sha256
            sha256 = "f0f433106f98f6f50ed6bbc2169f7c42dd73d13d0f09d431082519b5649903a6",
            strip_prefix = "boringssl-56eb68f64215738552be452e311da12047934ab4",
            urls = [
                "https://github.com/google/boringssl/archive/56eb68f64215738552be452e311da12047934ab4.tar.gz",
            ],
        )

    # Load gRPC and its dependencies, using a similar pattern to this function.
    if "com_github_grpc_grpc" not in native.existing_rules():
        http_archive(
            name = "com_github_grpc_grpc",
            strip_prefix = "grpc-1.46.2",
            urls = [
                "https://github.com/grpc/grpc/archive/v1.46.2.tar.gz",
            ],
            sha256 = "64b791c3dad387230f8b46cbd0c474b6ae751f7f805d05cb3bae36ac0ff2a7e7",
        )

    # We use the cc_proto_library() rule from @com_google_protobuf, which
    # assumes that grpc_cpp_plugin and grpc_lib are in the //external: module
    native.bind(
        name = "grpc_cpp_plugin",
        actual = "@com_github_grpc_grpc//src/compiler:grpc_cpp_plugin",
    )

    native.bind(
        name = "grpc_lib",
        actual = "@com_github_grpc_grpc//:grpc++",
    )

    # We need libcurl for the Google Cloud Storage client.
    if "com_github_curl_curl" not in native.existing_rules():
        http_archive(
            name = "com_github_curl_curl",
            urls = [
                "https://curl.haxx.se/download/curl-7.69.1.tar.gz",
            ],
            strip_prefix = "curl-7.69.1",
            sha256 = "01ae0c123dee45b01bbaef94c0bc00ed2aec89cb2ee0fd598e0d302a6b5e0a98",
            build_file = "@com_github_googleapis_google_cloud_cpp//bazel:curl.BUILD",
        )

    # We need the nlohmann_json library
    if "com_github_nlohmann_json" not in native.existing_rules():
        http_archive(
            name = "com_github_nlohmann_json",
            urls = [
                "https://github.com/nlohmann/json/releases/download/v3.10.5/include.zip",
            ],
            sha256 = "b94997df68856753b72f0d7a3703b7d484d4745c567f3584ef97c96c25a5798e",
            build_file = "@com_github_googleapis_google_cloud_cpp//bazel:nlohmann_json.BUILD",
        )

    # Load google/crc32c, a library to efficiently compute CRC32C checksums.
    if "com_github_google_crc32c" not in native.existing_rules():
        http_archive(
            name = "com_github_google_crc32c",
            strip_prefix = "crc32c-1.1.2",
            urls = [
                "https://github.com/google/crc32c/archive/1.1.2.tar.gz",
            ],
            sha256 = "ac07840513072b7fcebda6e821068aa04889018f24e10e46181068fb214d7e56",
            build_file = "@com_github_googleapis_google_cloud_cpp//bazel:crc32c.BUILD",
        )
<|endoftext|>


<commit_before>import bbio, os, cape_manager
from bbio.platform.util import spidev

class SPI_Bus(spidev.SPIDev):
  def open(self):
    overlay = 'BB-SPIDEV%i' % (self.bus-1)
    cape_manager.load(overlay, auto_unload=False)
    bbio.delay(250) # Give driver time to load
    assert cape_manager.isLoaded(overlay), "Could not load SPI overlay"
    super(SPI_Bus, self).open()

  def begin(self):
    self.open()

  def end(self):
    self.close()

# Initialize the global SPI  instances:
SPI0 = SPI_Bus(1)
SPI1 = SPI_Bus(2)
<commit_msg>initialize to default spi settings<commit_after>import bbio, os, cape_manager
from bbio.platform.util import spidev

class SPI_Bus(spidev.SPIDev):
  def open(self):
    overlay = 'BB-SPIDEV%i' % (self.bus-1)
    cape_manager.load(overlay, auto_unload=False)
    bbio.delay(250) # Give driver time to load
    assert cape_manager.isLoaded(overlay), "Could not load SPI overlay"
    super(SPI_Bus, self).open()
    # Initialize to default parameters:
    self.setBitsPerWord(8)
    self.setMaxFrequency(8000000)
    self.setClockMode(0)

  def begin(self):
    self.open()

  def end(self):
    self.close()

# Initialize the global SPI  instances:
SPI0 = SPI_Bus(1)
SPI1 = SPI_Bus(2)
<|endoftext|>