<commit_before># SPDX-License-Identifier: Apache-2.0
#
# Copyright (C) 2019, Arm Limited and contributors.
#
# 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 math

import pandas as pd
import numpy as np

PELT_WINDOW = 1024*1024*1e-9
"""
PELT window in seconds.
"""

PELT_HALF_LIFE = 32
"""
PELT half-life in number of windows.
"""

PELT_SCALE = 1024

def simulate_pelt(activations, init=0, index=None, clock=None, window=PELT_WINDOW, half_life=PELT_HALF_LIFE, scale=PELT_SCALE):
    """
    Simulate a PELT signal out of a series of activations.

    :param activations: Series of a task's activations:
        ``1 == running`` and ``0 == sleeping``.
    :type activations: pandas.Series

    :param init: Initial value of the signal
    :type init: float

    :param index: Optional index at which the PELT values should be computed.
        If ``None``, a value will be computed when the task starts sleeping and
        when it wakes up. Note that there is no emulation of scheduler tick
        updating the signal while it's running.
    :type index: pandas.Index

    :param clock: Series of clock values to be used instead of the timestamp index.
    :type clock: pandas.Series

    :param window: PELT window in seconds.
    :type window: float

    :param half_life: PELT half-life in number of windows.
    :type half_life: int

    :param scale: Scale of the signal, i.e. maximum value it can take.
    :type scale: float

    .. note:: PELT windowing is not time-invariant, i.e. it depends on the
        absolute value of the timestamp. This means that the timestamp of the
        activations matters, and it is recommended to use the ``clock``
        parameter to provide the actual clock used by PELT.

        Also note that the kernel uses integer arithmetic with a different way
        of computing the signal. This means that the simulation cannot
        perfectly match the kernel's signal.
    """
    if index is not None:
        activations = activations.reindex(index, method='ffill')
        activations.dropna(inplace=True)

    df = pd.DataFrame({'activations': activations})
    df['clock'] = clock if clock is not None else df.index
    df['delta'] = df['clock'].diff()
    # First row of "delta" is NaN
    df['delta'].iloc[0] = 0

    # Compute the number of crossed PELT windows between each sample Since PELT
    # windowing is not time invariant (windows are at "millisecond"
    # boundaries), we need non-normalized timestamps
    window_series = df['clock'] // window
    df['crossed_windows'] = window_series.diff()
    df['crossed_windows'].iloc[0] = 0

    def make_pelt_sim(init, scale, window, half_life):
        decay = (1/2)**(1/half_life)
        # Alpha as defined in https://en.wikipedia.org/wiki/Moving_average
        alpha = 1 - decay

        # Accumulator of running time within a PELT window
        acc = 0
        # Output signal
        signal = init / scale

        def pelt(row):
            nonlocal acc, signal

            # 1=running 0=sleeping
            running = row['activations']
            clock = row['clock']
            delta = row['delta']
            windows = row['crossed_windows'].astype('int')

            # We crossed one or more windows boundaries
            if windows:
                # Handle last piece of the window in which this activation started
                first_window_fraction = window - ((clock - delta) % window)
                first_window_fraction /= window

                acc += running * first_window_fraction
                signal = alpha * acc + (1-alpha) * signal

                # Handle the windows we fully crossed
                for _ in range(windows - 1):
                    signal = alpha * running + (1-alpha) * signal

                # Handle the current incomplete window
                last_window_fraction = (clock % window) / window
                signal += alpha * running * last_window_fraction
                acc = 0
            # If we are still in the same window, just accumulate the running
            # time
            else:
                acc += running * delta / window

            return signal * scale

        return pelt

    sim = make_pelt_sim(
        init=init,
        window=window,
        half_life=half_life,
        scale=scale,
    )
    df['pelt'] = df.apply(sim, axis=1)
    return df['pelt']

def pelt_settling_time(margin_pct=1, init=0, final=1024, window=PELT_WINDOW, half_life=PELT_HALF_LIFE):
    """
    Compute an approximation of the PELT settling time.

    :param margin_pct: How close to the final value we want to get, as a
        percentage of ``final``.
    :type margin_pct: float

    :param init: Initial PELT value.
    :type init: float

    :param final: Final PELT value.
    :type final: float

    :param window: PELT window in seconds.
    :type window: float

    :param half_life: PELT half life, in number of windows.
    :type half_life: int

    :param scale: PELT scale.
    :type scale: float

    .. note:: The PELT signal is approximated as a first order filter. This
        does not take into account the averaging inside a window, but the
        window is small enough in practice for that effect to be negligible.
    """

    # Compute the time constant of an equivalent continuous-time system as
    # defined by:
    # tau = period * (alpha / (1-alpha))
    # https://en.wikipedia.org/wiki/Low-pass_filter#Simple_infinite_impulse_response_filter

    # Alpha as defined in https://en.wikipedia.org/wiki/Moving_average
    decay = (1/2)**(1/half_life)
    alpha = 1 - decay
    tau = window * ((1 - alpha) / alpha)

    # Response of a first order low pass filter:
    # y(t) = u(t) * (1 - exp(-t/tau))
    # We want to find `t` such as the output y(t) is as close as we want from
    # the input u(t):
    # A * u(t) = u(t) * (1 - exp(-t/tau))
    # A is how close from u(t) we want the output to get after a time `t`
    # From which follows:
    # A = (1 - exp(-t/tau))
    # t = -tau * log(1-A)

    # Since the equation we have is for a step response, i.e. from 0 to a final
    # value
    if init > final:
        init, final = final, init
    final -= init

    margin = margin_pct / 100
    A = 1 - margin

    settling_time = - tau * math.log(1 - A)
    return settling_time

# vim :set tabstop=4 shiftwidth=4 textwidth=80 expandtab
<commit_msg>lisa.pelt: Fix SettingWithCopy pandas warning in simulate_pelt()<commit_after># SPDX-License-Identifier: Apache-2.0
#
# Copyright (C) 2019, Arm Limited and contributors.
#
# 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 math

import pandas as pd
import numpy as np

PELT_WINDOW = 1024*1024*1e-9
"""
PELT window in seconds.
"""

PELT_HALF_LIFE = 32
"""
PELT half-life in number of windows.
"""

PELT_SCALE = 1024

def simulate_pelt(activations, init=0, index=None, clock=None, window=PELT_WINDOW, half_life=PELT_HALF_LIFE, scale=PELT_SCALE):
    """
    Simulate a PELT signal out of a series of activations.

    :param activations: Series of a task's activations:
        ``1 == running`` and ``0 == sleeping``.
    :type activations: pandas.Series

    :param init: Initial value of the signal
    :type init: float

    :param index: Optional index at which the PELT values should be computed.
        If ``None``, a value will be computed when the task starts sleeping and
        when it wakes up. Note that there is no emulation of scheduler tick
        updating the signal while it's running.
    :type index: pandas.Index

    :param clock: Series of clock values to be used instead of the timestamp index.
    :type clock: pandas.Series

    :param window: PELT window in seconds.
    :type window: float

    :param half_life: PELT half-life in number of windows.
    :type half_life: int

    :param scale: Scale of the signal, i.e. maximum value it can take.
    :type scale: float

    .. note:: PELT windowing is not time-invariant, i.e. it depends on the
        absolute value of the timestamp. This means that the timestamp of the
        activations matters, and it is recommended to use the ``clock``
        parameter to provide the actual clock used by PELT.

        Also note that the kernel uses integer arithmetic with a different way
        of computing the signal. This means that the simulation cannot
        perfectly match the kernel's signal.
    """
    if index is not None:
        activations = activations.reindex(index, method='ffill')

    df = pd.DataFrame({'activations': activations})
    df['clock'] = clock if clock is not None else df.index
    df['delta'] = df['clock'].diff()

    # Compute the number of crossed PELT windows between each sample Since PELT
    # windowing is not time invariant (windows are at "millisecond"
    # boundaries), we need non-normalized timestamps
    window_series = df['clock'] // window
    df['crossed_windows'] = window_series.diff()

    # First row of "delta" is NaN, and activations reindex may have produced
    # some NaN at the beginning of the dataframe as well
    df.dropna(inplace=True)

    def make_pelt_sim(init, scale, window, half_life):
        decay = (1/2)**(1/half_life)
        # Alpha as defined in https://en.wikipedia.org/wiki/Moving_average
        alpha = 1 - decay

        # Accumulator of running time within a PELT window
        acc = 0
        # Output signal
        signal = init / scale

        def pelt(row):
            nonlocal acc, signal

            # 1=running 0=sleeping
            running = row['activations']
            clock = row['clock']
            delta = row['delta']
            windows = row['crossed_windows'].astype('int')

            # We crossed one or more windows boundaries
            if windows:
                # Handle last piece of the window in which this activation started
                first_window_fraction = window - ((clock - delta) % window)
                first_window_fraction /= window

                acc += running * first_window_fraction
                signal = alpha * acc + (1-alpha) * signal

                # Handle the windows we fully crossed
                for _ in range(windows - 1):
                    signal = alpha * running + (1-alpha) * signal

                # Handle the current incomplete window
                last_window_fraction = (clock % window) / window
                signal += alpha * running * last_window_fraction
                acc = 0
            # If we are still in the same window, just accumulate the running
            # time
            else:
                acc += running * delta / window

            return signal * scale

        return pelt

    sim = make_pelt_sim(
        init=init,
        window=window,
        half_life=half_life,
        scale=scale,
    )
    df['pelt'] = df.apply(sim, axis=1)
    return df['pelt']

def pelt_settling_time(margin_pct=1, init=0, final=1024, window=PELT_WINDOW, half_life=PELT_HALF_LIFE):
    """
    Compute an approximation of the PELT settling time.

    :param margin_pct: How close to the final value we want to get, as a
        percentage of ``final``.
    :type margin_pct: float

    :param init: Initial PELT value.
    :type init: float

    :param final: Final PELT value.
    :type final: float

    :param window: PELT window in seconds.
    :type window: float

    :param half_life: PELT half life, in number of windows.
    :type half_life: int

    :param scale: PELT scale.
    :type scale: float

    .. note:: The PELT signal is approximated as a first order filter. This
        does not take into account the averaging inside a window, but the
        window is small enough in practice for that effect to be negligible.
    """

    # Compute the time constant of an equivalent continuous-time system as
    # defined by:
    # tau = period * (alpha / (1-alpha))
    # https://en.wikipedia.org/wiki/Low-pass_filter#Simple_infinite_impulse_response_filter

    # Alpha as defined in https://en.wikipedia.org/wiki/Moving_average
    decay = (1/2)**(1/half_life)
    alpha = 1 - decay
    tau = window * ((1 - alpha) / alpha)

    # Response of a first order low pass filter:
    # y(t) = u(t) * (1 - exp(-t/tau))
    # We want to find `t` such as the output y(t) is as close as we want from
    # the input u(t):
    # A * u(t) = u(t) * (1 - exp(-t/tau))
    # A is how close from u(t) we want the output to get after a time `t`
    # From which follows:
    # A = (1 - exp(-t/tau))
    # t = -tau * log(1-A)

    # Since the equation we have is for a step response, i.e. from 0 to a final
    # value
    if init > final:
        init, final = final, init
    final -= init

    margin = margin_pct / 100
    A = 1 - margin

    settling_time = - tau * math.log(1 - A)
    return settling_time

# vim :set tabstop=4 shiftwidth=4 textwidth=80 expandtab
<|endoftext|>


<commit_before>temperatura = []
acm = 0
for i in range(12):
    tempx = float(input("Digite a temperatura do mes%d: "%(i+1)))
    temperatura.append(tempx)
    acm = acm + tempx

media = acm/12

for i in range(12):
    if temperatura[i] > media:
        print(temperatura[i], i+1)

print(media)

<commit_msg>Delete listas_13.py<commit_after><|endoftext|>


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

# The MIT License
#
# Copyright 2018 David Pursehouse. All rights reserved.
#
# 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.

"""Live server tests."""

import base64
import pytest
import unittest
from pygerrit2 import GerritRestAPI, GerritReview, HTTPBasicAuth, \
    HTTPDigestAuth
from testcontainers.core.container import DockerContainer
from testcontainers.core.waiting_utils import wait_container_is_ready

TEST_TOPIC = "test-topic"

class GerritContainer(DockerContainer):
    """Gerrit container."""

    def __init__(self, version):
        """Construct a GerritContainer with the given version."""
        image = "gerritcodereview/gerrit:" + version
        super(GerritContainer, self).__init__(image)
        self.with_exposed_ports(8080)

@wait_container_is_ready()
def _initialize(api):
    api.get("/changes/")

@pytest.fixture(scope="module",
                params=["2.14.18", "2.15.8", "2.16.3"])
def gerrit_api(request):
    """Create a Gerrit container for the given version and return an API."""
    with GerritContainer(request.param) as gerrit:
        port = gerrit.get_exposed_port(8080)
        url = "http://localhost:%s" % port
        if request.param == "2.13.11":
            auth = HTTPDigestAuth("admin", "secret")
        else:
            auth = HTTPBasicAuth("admin", "secret")
        api = GerritRestAPI(url=url, auth=auth)
        _initialize(api)
        yield api

class TestGerritAgainstLiveServer(object):
    """Run tests against a live server."""

    def _get_test_change(self, gerrit_api, topic=TEST_TOPIC):
        results = gerrit_api.get("/changes/?q=topic:" + topic)
        assert len(results) == 1
        return results[0]

    def test_put_with_json_dict(self, gerrit_api):
        """Test a PUT request passing data as a dict to `json`.

        Tests that the PUT request works as expected when data is passed
        via the `json` argument as a `dict`.

        Creates the test project which is used by subsequent tests.
        """
        projectinput = {"create_empty_commit": "true"}
        gerrit_api.put("/projects/test-project", json=projectinput)
        gerrit_api.get("/projects/test-project")

    def test_put_with_data_dict(self, gerrit_api):
        """Test a PUT request passing data as a dict to `data`.

        Tests that the PUT request works as expected when data is passed
        via the `data` argument as a `dict`.
        """
        description = {"description": "New Description"}
        gerrit_api.put("/projects/test-project/description", data=description)
        project = gerrit_api.get("/projects/test-project")
        assert project["description"] == "New Description"

    def test_post_with_data_dict_and_no_data(self, gerrit_api):
        """Test a POST request passing data as a dict to `data`.

        Tests that the POST request works as expected when data is passed
        via the `data` argument as a `dict`.
        """
        changeinput = {"project": "test-project",
                       "subject": "subject",
                       "branch": "master",
                       "topic": "post-with-data"}
        result = gerrit_api.post("/changes/", data=changeinput)
        change = self._get_test_change(gerrit_api, "post-with-data")
        assert change["id"] == result["id"]

        # Subsequent post without data or json should not have the Content-Type
        # json header, and should succeed.
        result = gerrit_api.post("/changes/" + change["id"] + "/abandon")
        assert result["status"] == "ABANDONED"

    def test_post_with_json_dict(self, gerrit_api):
        """Test a POST request passing data as a dict to `json`.

        Tests that the POST request works as expected when data is passed
        via the `json` argument as a `dict`.

        Creates the change which is used by subsequent tests.
        """
        changeinput = {"project": "test-project",
                       "subject": "subject",
                       "branch": "master",
                       "topic": TEST_TOPIC}
        result = gerrit_api.post("/changes/", json=changeinput)
        change = self._get_test_change(gerrit_api)
        assert change["id"] == result["id"]

    def test_put_with_data_as_string(self, gerrit_api):
        """Test a PUT request passing data as a string to `data`.

        Tests that the PUT request works as expected when data is passed
        via the `data` parameter as a string.

        Creates a change edit that is checked in the subsequent test.
        """
        change_id = self._get_test_change(gerrit_api)["id"]
        gerrit_api.put("/changes/" + change_id + "/edit/foo",
                       data="Content with non base64 valid chars åäö")

    def test_get_base64_data(self, gerrit_api):
        """Test a GET request on an API that returns base64 encoded response.

        Tests that the headers can be overridden on the GET call, resulting
        in the data being returned as text/plain, and that the content of the
        response can be base64 decoded.
        """
        change_id = self._get_test_change(gerrit_api)["id"]
        response = gerrit_api.get("/changes/" + change_id + "/edit/foo",
                                  headers={'Accept': 'text/plain'})

        # Will raise binascii.Error if content is not properly encoded
        base64.b64decode(response)

    def test_get_patch_zip(self, gerrit_api):
        """Test a GET request to get a patch file (issue #19)."""
        change_id = self._get_test_change(gerrit_api)["id"]
        gerrit_api.get("/changes/" + change_id + "/revisions/current/patch?zip")

    def test_put_with_no_content(self, gerrit_api):
        """Test a PUT request with no content."""
        change_id = self._get_test_change(gerrit_api)["id"]
        gerrit_api.put("/changes/" + change_id + "/edit/foo")

    def test_review(self, gerrit_api):
        """Test that a review can be posted by the review API."""
        change_id = self._get_test_change(gerrit_api)["id"]
        review = GerritReview()
        review.set_message("Review from live test")
        review.add_labels({"Code-Review": 1})
        gerrit_api.review(change_id, "current", review)

if __name__ == '__main__':
    unittest.main()
<commit_msg>Run livetests against 2.15.9 and 2.16.4<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# The MIT License
#
# Copyright 2018 David Pursehouse. All rights reserved.
#
# 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.

"""Live server tests."""

import base64
import pytest
import unittest
from pygerrit2 import GerritRestAPI, GerritReview, HTTPBasicAuth, \
    HTTPDigestAuth
from testcontainers.core.container import DockerContainer
from testcontainers.core.waiting_utils import wait_container_is_ready

TEST_TOPIC = "test-topic"

class GerritContainer(DockerContainer):
    """Gerrit container."""

    def __init__(self, version):
        """Construct a GerritContainer with the given version."""
        image = "gerritcodereview/gerrit:" + version
        super(GerritContainer, self).__init__(image)
        self.with_exposed_ports(8080)

@wait_container_is_ready()
def _initialize(api):
    api.get("/changes/")

@pytest.fixture(scope="module",
                params=["2.14.18", "2.15.9", "2.16.4"])
def gerrit_api(request):
    """Create a Gerrit container for the given version and return an API."""
    with GerritContainer(request.param) as gerrit:
        port = gerrit.get_exposed_port(8080)
        url = "http://localhost:%s" % port
        if request.param == "2.13.11":
            auth = HTTPDigestAuth("admin", "secret")
        else:
            auth = HTTPBasicAuth("admin", "secret")
        api = GerritRestAPI(url=url, auth=auth)
        _initialize(api)
        yield api

class TestGerritAgainstLiveServer(object):
    """Run tests against a live server."""

    def _get_test_change(self, gerrit_api, topic=TEST_TOPIC):
        results = gerrit_api.get("/changes/?q=topic:" + topic)
        assert len(results) == 1
        return results[0]

    def test_put_with_json_dict(self, gerrit_api):
        """Test a PUT request passing data as a dict to `json`.

        Tests that the PUT request works as expected when data is passed
        via the `json` argument as a `dict`.

        Creates the test project which is used by subsequent tests.
        """
        projectinput = {"create_empty_commit": "true"}
        gerrit_api.put("/projects/test-project", json=projectinput)
        gerrit_api.get("/projects/test-project")

    def test_put_with_data_dict(self, gerrit_api):
        """Test a PUT request passing data as a dict to `data`.

        Tests that the PUT request works as expected when data is passed
        via the `data` argument as a `dict`.
        """
        description = {"description": "New Description"}
        gerrit_api.put("/projects/test-project/description", data=description)
        project = gerrit_api.get("/projects/test-project")
        assert project["description"] == "New Description"

    def test_post_with_data_dict_and_no_data(self, gerrit_api):
        """Test a POST request passing data as a dict to `data`.

        Tests that the POST request works as expected when data is passed
        via the `data` argument as a `dict`.
        """
        changeinput = {"project": "test-project",
                       "subject": "subject",
                       "branch": "master",
                       "topic": "post-with-data"}
        result = gerrit_api.post("/changes/", data=changeinput)
        change = self._get_test_change(gerrit_api, "post-with-data")
        assert change["id"] == result["id"]

        # Subsequent post without data or json should not have the Content-Type
        # json header, and should succeed.
        result = gerrit_api.post("/changes/" + change["id"] + "/abandon")
        assert result["status"] == "ABANDONED"

    def test_post_with_json_dict(self, gerrit_api):
        """Test a POST request passing data as a dict to `json`.

        Tests that the POST request works as expected when data is passed
        via the `json` argument as a `dict`.

        Creates the change which is used by subsequent tests.
        """
        changeinput = {"project": "test-project",
                       "subject": "subject",
                       "branch": "master",
                       "topic": TEST_TOPIC}
        result = gerrit_api.post("/changes/", json=changeinput)
        change = self._get_test_change(gerrit_api)
        assert change["id"] == result["id"]

    def test_put_with_data_as_string(self, gerrit_api):
        """Test a PUT request passing data as a string to `data`.

        Tests that the PUT request works as expected when data is passed
        via the `data` parameter as a string.

        Creates a change edit that is checked in the subsequent test.
        """
        change_id = self._get_test_change(gerrit_api)["id"]
        gerrit_api.put("/changes/" + change_id + "/edit/foo",
                       data="Content with non base64 valid chars åäö")

    def test_get_base64_data(self, gerrit_api):
        """Test a GET request on an API that returns base64 encoded response.

        Tests that the headers can be overridden on the GET call, resulting
        in the data being returned as text/plain, and that the content of the
        response can be base64 decoded.
        """
        change_id = self._get_test_change(gerrit_api)["id"]
        response = gerrit_api.get("/changes/" + change_id + "/edit/foo",
                                  headers={'Accept': 'text/plain'})

        # Will raise binascii.Error if content is not properly encoded
        base64.b64decode(response)

    def test_get_patch_zip(self, gerrit_api):
        """Test a GET request to get a patch file (issue #19)."""
        change_id = self._get_test_change(gerrit_api)["id"]
        gerrit_api.get("/changes/" + change_id + "/revisions/current/patch?zip")

    def test_put_with_no_content(self, gerrit_api):
        """Test a PUT request with no content."""
        change_id = self._get_test_change(gerrit_api)["id"]
        gerrit_api.put("/changes/" + change_id + "/edit/foo")

    def test_review(self, gerrit_api):
        """Test that a review can be posted by the review API."""
        change_id = self._get_test_change(gerrit_api)["id"]
        review = GerritReview()
        review.set_message("Review from live test")
        review.add_labels({"Code-Review": 1})
        gerrit_api.review(change_id, "current", review)

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


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

# Uses atlod to extract the HI spectra and the 2GHz continuum for a day
#
# This program extracts the HI spectra (1.42 GHz) and continuum (2 GHz) data
# from the day's RPFITS files into miriad format. It then backs up the metadata
# to allow any processing to be simply rolled back.

# Author James Dempsey
# Date 7 Jul 2016

from __future__ import print_function, division

import csv
import glob
import logging
import magmo
import os
import shutil
import sys
import time

# functions
def get_day_data(day):
    """
    Read the magmo-days-full.csv file and find the row for the requested day.
    :param day: The day to be found
    :return: The day's row, or None if the day is not defined
    """

    with open('magmo-days-full.csv', 'rb') as magmodays:
        reader = csv.reader(magmodays)
        for row in reader:
            if row[0] == day:
                return row
    return None

def load_rpfits(freq, ifsel, restFreq, dayNum, inFilePatterns):
    """
    Run atlod to extract data files for a particular frequency from a set of
    RPFITS into Miriad format.

    :param freq: The short title of the frequency
    :param ifsel: IF number(s) to select
    :param restFreq:  The rest frequency, in GHz, for line observations.
    :param dayNum: The MAGMO observation day number
    :param inFilePatterns: The patterns to match the RPFITS files.
    :return: None
    """
    outFile = "day" + dayNum + "/MAGMO_day" + dayNum + "_" + freq + ".uv"
    atlodCmd = "atlod in='" + inFilePatterns + "' out='" + outFile + "' " \
               + "ifsel=" + str(ifsel) + " restfreq=" + restFreq \
               + " options=birdie,xycorr,noauto,rfiflag"
    magmo.run_os_cmd(atlodCmd)

# ### Script starts here ###

# Read day parameter
if len(sys.argv) != 2:
    print("Incorrect number of parameters.")
    print("Usage: python load-data.py day")
    exit(1)
day = sys.argv[1]
start = time.time()

# Read metadata for the day (file pattern fragments etc)
dayRow = get_day_data(day)
if dayRow is None:
    print ("Day %s is not defined." % (day))
    exit(1)
print ("#### Started loading MAGMO day %s at %s ####" % \
      (day, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start))))
print (dayRow)

# Make day directory
dayDirName = "day" + day
magmo.ensure_dir_exists(dayDirName)

# Load files
freqList = ["1421", "1757"]
ifList = [7, 1]
restFreqList = ["1.420405752", "1.757"]
inFilePatterns = ""
startDatePatterns = 2
for i in range(startDatePatterns, len(dayRow)):
    inFilePatterns += "rawdata/" + dayRow[i] + "* "

for i in range(0, len(freqList)):
    freq = freqList[i]
    ifsel = ifList[i]
    restFreq = restFreqList[i]
    load_rpfits(freq, ifsel, restFreq, dayRow[0], inFilePatterns)

# Split files
os.chdir("day" + dayRow[0])
for freq in freqList:
    uvFile = "MAGMO_day" + dayRow[0] + "_" + freq + ".uv"
    uvsplitCmd = "uvsplit vis=" + uvFile
    magmo.run_os_cmd(uvsplitCmd)
# Rename any 1420.5 folders to remove the decimal
uv_dirs = glob.glob('*.[0-9][0-9][0-9][0-9].[0-9]')
for uvdir in uv_dirs:
    if os.exists(uvdir[:-2]):
        logging.warning("Duplicate folders exist for " + uvdir)
    else:
        os.rename(uvdir, uvdir[:-2])
os.chdir("..")

# Backup
backupDirName = dayDirName+"/backup"
magmo.ensure_dir_exists(backupDirName)
for uvDir in os.listdir(dayDirName):
    uvDirName = dayDirName + "/" + uvDir
    if (not uvDirName.endswith("backup")) and (not uvDirName.endswith(".uv")) and os.path.isdir(uvDirName):
        uvBackupDir = backupDirName + "/" + uvDir
        magmo.ensure_dir_exists(uvBackupDir)
        for name in ['flags', 'header', 'history']:
            shutil.copy2(uvDirName + "/" + name, uvBackupDir)

# Cleanup
#for freq in freqList:
#    cmd = 'rm -rf day' + dayRow[0] + '/MAGMO_day' + dayRow[0] + '_' + freq + '.uv'
#    magmo.run_os_cmd(cmd)

# Report
end = time.time()
print ('#### Loading completed at %s ####' \
      % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end))))
print ('Processed in %.02f s' % (end - start))
<commit_msg>Fix exists test<commit_after>#!/usr/bin/env python -u

# Uses atlod to extract the HI spectra and the 2GHz continuum for a day
#
# This program extracts the HI spectra (1.42 GHz) and continuum (2 GHz) data
# from the day's RPFITS files into miriad format. It then backs up the metadata
# to allow any processing to be simply rolled back.

# Author James Dempsey
# Date 7 Jul 2016

from __future__ import print_function, division

import csv
import glob
import logging
import magmo
import os
import shutil
import sys
import time

# functions
def get_day_data(day):
    """
    Read the magmo-days-full.csv file and find the row for the requested day.
    :param day: The day to be found
    :return: The day's row, or None if the day is not defined
    """

    with open('magmo-days-full.csv', 'rb') as magmodays:
        reader = csv.reader(magmodays)
        for row in reader:
            if row[0] == day:
                return row
    return None

def load_rpfits(freq, ifsel, restFreq, dayNum, inFilePatterns):
    """
    Run atlod to extract data files for a particular frequency from a set of
    RPFITS into Miriad format.

    :param freq: The short title of the frequency
    :param ifsel: IF number(s) to select
    :param restFreq:  The rest frequency, in GHz, for line observations.
    :param dayNum: The MAGMO observation day number
    :param inFilePatterns: The patterns to match the RPFITS files.
    :return: None
    """
    outFile = "day" + dayNum + "/MAGMO_day" + dayNum + "_" + freq + ".uv"
    atlodCmd = "atlod in='" + inFilePatterns + "' out='" + outFile + "' " \
               + "ifsel=" + str(ifsel) + " restfreq=" + restFreq \
               + " options=birdie,xycorr,noauto,rfiflag"
    magmo.run_os_cmd(atlodCmd)

# ### Script starts here ###

# Read day parameter
if len(sys.argv) != 2:
    print("Incorrect number of parameters.")
    print("Usage: python load-data.py day")
    exit(1)
day = sys.argv[1]
start = time.time()

# Read metadata for the day (file pattern fragments etc)
dayRow = get_day_data(day)
if dayRow is None:
    print ("Day %s is not defined." % (day))
    exit(1)
print ("#### Started loading MAGMO day %s at %s ####" % \
      (day, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start))))
print (dayRow)

# Make day directory
dayDirName = "day" + day
magmo.ensure_dir_exists(dayDirName)

# Load files
freqList = ["1421", "1757"]
ifList = [7, 1]
restFreqList = ["1.420405752", "1.757"]
inFilePatterns = ""
startDatePatterns = 2
for i in range(startDatePatterns, len(dayRow)):
    inFilePatterns += "rawdata/" + dayRow[i] + "* "

for i in range(0, len(freqList)):
    freq = freqList[i]
    ifsel = ifList[i]
    restFreq = restFreqList[i]
    load_rpfits(freq, ifsel, restFreq, dayRow[0], inFilePatterns)

# Split files
os.chdir("day" + dayRow[0])
for freq in freqList:
    uvFile = "MAGMO_day" + dayRow[0] + "_" + freq + ".uv"
    uvsplitCmd = "uvsplit vis=" + uvFile
    magmo.run_os_cmd(uvsplitCmd)
# Rename any 1420.5 folders to remove the decimal
uv_dirs = glob.glob('*.[0-9][0-9][0-9][0-9].[0-9]')
for uvdir in uv_dirs:
    if os.path.exists(uvdir[:-2]):
        logging.warning("Duplicate folders exist for " + uvdir)
    else:
        os.rename(uvdir, uvdir[:-2])
os.chdir("..")

# Backup
backupDirName = dayDirName+"/backup"
magmo.ensure_dir_exists(backupDirName)
for uvDir in os.listdir(dayDirName):
    uvDirName = dayDirName + "/" + uvDir
    if (not uvDirName.endswith("backup")) and (not uvDirName.endswith(".uv")) and os.path.isdir(uvDirName):
        uvBackupDir = backupDirName + "/" + uvDir
        magmo.ensure_dir_exists(uvBackupDir)
        for name in ['flags', 'header', 'history']:
            shutil.copy2(uvDirName + "/" + name, uvBackupDir)

# Cleanup
#for freq in freqList:
#    cmd = 'rm -rf day' + dayRow[0] + '/MAGMO_day' + dayRow[0] + '_' + freq + '.uv'
#    magmo.run_os_cmd(cmd)

# Report
end = time.time()
print ('#### Loading completed at %s ####' \
      % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end))))
print ('Processed in %.02f s' % (end - start))
<|endoftext|>


<commit_before># Copyright 2012 the rootpy developers
# distributed under the terms of the GNU General Public License
from __future__ import absolute_import

import sys
from collections import namedtuple

# DO NOT expose ROOT at module level here since that conflicts with rootpy.ROOT
# See issue https://github.com/rootpy/rootpy/issues/343
import ROOT as R

from .logger import log
# Needed for "from rootpy import QROOT" by other modules
from .utils import quickroot as QROOT
from . import defaults
from .base import Object
from .info import __version_info__, __version__

__all__ = [
    'log',
    'ROOT_VERSION',
    'QROOT',
    'rootpy_source_dir',
    'asrootpy',
    'lookup',
    'lookup_by_name',
    'lookup_rootpy',
    'register',
    'create',
]

class ROOTVersion(namedtuple('_ROOTVersionBase',
                             ['major', 'minor', 'micro'])):

    def __new__(cls, version):
        if version < 1E4:
            raise ValueError(
                "{0:d} is not a valid ROOT version integer".format(version))
        return super(ROOTVersion, cls).__new__(
            cls,
            int(version / 1E4), int((version / 1E2) % 100), int(version % 100))

    def __repr__(self):
        return str(self)

    def __str__(self):
        return '{0:d}.{1:02d}/{2:02d}'.format(*self)

# Note: requires defaults import
ROOT_VERSION = ROOTVersion(QROOT.gROOT.GetVersionInt())
log.debug("Using ROOT {0}".format(ROOT_VERSION))

IN_NOSETESTS = False
if sys.argv and sys.argv[0].endswith('nosetests'):
    IN_NOSETESTS = True

IN_IPYTHON = '__IPYTHON__' in __builtins__
if IN_IPYTHON:
    from IPython.kernel.zmq.iostream import OutStream
    IN_IPYTHON_NOTEBOOK = isinstance(sys.stdout, OutStream)
else:
    IN_IPYTHON_NOTEBOOK = False

class ROOTError(RuntimeError):
    """
    Exception class representing a ROOT error/warning message.
    """
    def __init__(self, level, location, msg):
        self.level, self.location, self.msg = level, location, msg

    def __str__(self):
        return "level={0}, loc='{1}', msg='{2}'".format(
            self.level, self.location, self.msg)

def rootpy_source_dir():
    import rootpy
    from os.path import abspath, dirname
    from inspect import getfile
    from sys import modules
    path = dirname(getfile(modules[__name__]))
    absp = abspath(path)
    return path, absp

_ROOTPY_SOURCE_PATH, _ROOTPY_SOURCE_ABSPATH = rootpy_source_dir()
del rootpy_source_dir

"""
All rootpy wrappers are registered below. This dict maps the ROOT class name to
the path to the rootpy class or a tuple of both the path and keyword arguments
used in the dynamic_cls classmethod (see the ``Hist``, ``Hist2D`` and
``Hist3D`` classes in ``plotting.hist``).

This way rootpy is aware of all classes that inherit from ROOT classes without
needing to import everything when rootpy is first imported. This registry is
required to cast ROOT objects into the rootpy form when extracted from a ROOT
TFile, for example.
"""
INIT_REGISTRY = {

    'TList': 'collection.List',
    'TObjArray': 'collection.ObjArray',

    'TTree': 'tree.tree.Tree',
    'TNtuple': 'tree.tree.Ntuple',

    'TKey': 'io.file.Key',
    'TDirectoryFile': 'io.file.Directory',
    'TFile': 'io.file.File',
    'TMemFile': 'io.file.MemFile',

    'TStyle': 'plotting.style.Style',
    'TCanvas': 'plotting.canvas.Canvas',
    'TPad': 'plotting.canvas.Pad',
    'TPave': 'plotting.box.Pave',
    'TPaveStats': 'plotting.box.PaveStats',
    'TLegend': 'plotting.legend.Legend',
    'TEllipse': 'plotting.shapes.Ellipse',
    'TLine': 'plotting.shapes.Line',
    'TArrow': 'plotting.shapes.Arrow',

    'TF1': 'plotting.func.F1',
    'TF2': 'plotting.func.F2',
    'TF3': 'plotting.func.F3',

    'TGraph': ('plotting.graph.Graph', dict(type='default')),
    'TGraphErrors': ('plotting.graph.Graph', dict(type='errors')),
    'TGraphAsymmErrors': ('plotting.graph.Graph', dict(type='asymm')),
    'TGraphBentErrors': ('plotting.graph.Graph', dict(type='benterrors')),

    'TGraph2D': ('plotting.graph.Graph2D', dict(type='default')),
    'TGraph2DErrors': ('plotting.graph.Graph2D', dict(type='errors')),

    'TProfile': 'plotting.profile.Profile',
    'TProfile2D': 'plotting.profile.Profile2D',
    'TProfile3D': 'plotting.profile.Profile3D',

    'TH1C': ('plotting.hist.Hist', dict(type='C')),
    'TH1S': ('plotting.hist.Hist', dict(type='S')),
    'TH1I': ('plotting.hist.Hist', dict(type='I')),
    'TH1F': ('plotting.hist.Hist', dict(type='F')),
    'TH1D': ('plotting.hist.Hist', dict(type='D')),

    'TH2C': ('plotting.hist.Hist2D', dict(type='C')),
    'TH2S': ('plotting.hist.Hist2D', dict(type='S')),
    'TH2I': ('plotting.hist.Hist2D', dict(type='I')),
    'TH2F': ('plotting.hist.Hist2D', dict(type='F')),
    'TH2D': ('plotting.hist.Hist2D', dict(type='D')),

    'TH3C': ('plotting.hist.Hist3D', dict(type='C')),
    'TH3S': ('plotting.hist.Hist3D', dict(type='S')),
    'TH3I': ('plotting.hist.Hist3D', dict(type='I')),
    'TH3F': ('plotting.hist.Hist3D', dict(type='F')),
    'TH3D': ('plotting.hist.Hist3D', dict(type='D')),

    'TEfficiency': 'plotting.hist.Efficiency',

    'THStack': 'plotting.hist.HistStack',

    'TAxis': 'plotting.axis.Axis',

    'TVector2': 'vector.Vector2',
    'TVector3': 'vector.Vector3',
    'TLorentzVector': 'vector.LorentzVector',
    'TRotation': 'vector.Rotation',
    'TLorentzRotation': 'vector.LorentzRotation',

    'TMatrixT<float>': ('matrix.Matrix', dict(type='float')),
    'TMatrixT<double>': ('matrix.Matrix', dict(type='double')),
    'TMatrixTSym<float>': ('matrix.SymmetricMatrix', dict(type='float')),
    'TMatrixTSym<double>': ('matrix.SymmetricMatrix', dict(type='double')),

    'RooWorkspace': 'stats.workspace.Workspace',
    'RooStats::ModelConfig': 'stats.modelconfig.ModelConfig',
    'RooArgSet': 'stats.collection.ArgSet',
    'RooArgList': 'stats.collection.ArgList',
    'RooRealVar': 'stats.value.RealVar',
    'RooSimultaneous': 'stats.pdf.Simultaneous',
    'RooAddPdf': 'stats.pdf.AddPdf',
    'RooProdPdf': 'stats.pdf.ProdPdf',
    'RooCatType': 'stats.category.CatType',
    'RooCategory': 'stats.category.Category',
    'RooDataSet': 'stats.dataset.DataSet',
    'RooMinimizer': 'stats.fit.Minimizer',
    'RooFitResult': 'stats.fit.FitResult',

    'RooStats::HistFactory::Data': 'stats.histfactory.Data',
    'RooStats::HistFactory::Sample': 'stats.histfactory.Sample',
    'RooStats::HistFactory::HistoSys': 'stats.histfactory.HistoSys',
    'RooStats::HistFactory::HistoFactor': 'stats.histfactory.HistoFactor',
    'RooStats::HistFactory::OverallSys': 'stats.histfactory.OverallSys',
    'RooStats::HistFactory::NormFactor': 'stats.histfactory.NormFactor',
    'RooStats::HistFactory::ShapeSys': 'stats.histfactory.ShapeSys',
    'RooStats::HistFactory::ShapeFactor': 'stats.histfactory.ShapeFactor',
    'RooStats::HistFactory::Channel': 'stats.histfactory.Channel',
    'RooStats::HistFactory::Measurement': 'stats.histfactory.Measurement',
}

# map rootpy name to location in rootpy (i.e. Axis -> plotting.axis)
INIT_REGISTRY_ROOTPY = {}
for rtype, rptype in INIT_REGISTRY.items():
    if isinstance(rptype, tuple):
        rptype = rptype[0]
    cls_path, _, cls_name = rptype.rpartition('.')
    INIT_REGISTRY_ROOTPY[cls_name] = cls_path

# these dicts are populated as classes are registered at runtime
# ROOT class name -> rootpy class
REGISTRY = {}
# rootpy class name -> rootpy class
REGISTRY_ROOTPY = {}

def asrootpy(thing, **kwargs):

    # is this thing already converted?
    if isinstance(thing, Object):
        return thing

    warn = kwargs.pop('warn', True)
    after_init = kwargs.pop('after_init', False)

    # is this thing a class?
    if isinstance(thing, QROOT.PyRootType):
        if issubclass(thing, Object):
            return thing
        result = lookup(thing)
        if result is None:
            if warn:
                log.warn(
                    "There is no rootpy implementation "
                    "of the class `{0}`".format(thing.__name__))
            return thing
        if after_init:
            # preserve ROOT's __init__
            class asrootpy_cls(result):
                def __new__(self, *args, **kwargs):
                    return asrootpy(thing(*args, **kwargs), warn=warn)
            asrootpy_cls.__name__ = '{0}_asrootpy'.format(thing.__name__)
            return asrootpy_cls
        return result

    thing_cls = thing.__class__
    rootpy_cls = lookup(thing_cls)
    if rootpy_cls is None:
        if warn:
            log.warn(
                "A subclass of `{0}` is not "
                "implemented in rootpy".format(
                    thing_cls.__name__))
        return thing

    # cast
    thing.__class__ = rootpy_cls
    # call the _post_init if one exists
    if hasattr(thing, '_post_init'):
        thing._post_init(**kwargs)

    return thing

def _get_class(path, name):

    rootpy_module = __import__(
        path, globals(), locals(), [name], -1)
    return getattr(rootpy_module, name)

def lookup(cls):

    cls_name = cls.__name__
    return lookup_by_name(cls_name)

def lookup_by_name(cls_name):

    if cls_name in REGISTRY:
        return REGISTRY[cls_name]
    if cls_name not in INIT_REGISTRY:
        return None
    entry = INIT_REGISTRY[cls_name]
    if isinstance(entry, tuple):
        path, dynamic_kwargs = entry
    elif isinstance(entry, basestring):
        path = entry
        dynamic_kwargs = None
    cls_path, _, rootpy_cls_name = path.rpartition('.')

    rootpy_cls = _get_class(cls_path, rootpy_cls_name)

    if dynamic_kwargs is not None:
        rootpy_cls = rootpy_cls.dynamic_cls(**dynamic_kwargs)
    REGISTRY[cls_name] = rootpy_cls
    return rootpy_cls

def lookup_rootpy(rootpy_cls_name):

    rootpy_cls = REGISTRY_ROOTPY.get(rootpy_cls_name, None)
    if rootpy_cls is not None:
        return rootpy_cls
    cls_path = INIT_REGISTRY_ROOTPY.get(rootpy_cls_name, None)
    if cls_path is None:
        return None

    rootpy_cls = _get_class(cls_path, rootpy_cls_name)

    REGISTRY_ROOTPY[rootpy_cls_name] = rootpy_cls
    return rootpy_cls

class register(object):

    def __init__(self, names=None, builtin=False):

        if names is not None:
            if type(names) not in (list, tuple):
                names = [names]
        self.names = names
        self.builtin = builtin

    def __call__(self, cls):

        if issubclass(cls, Object):
            # all rootpy classes which inherit from ROOT classes
            # must place the ROOT base class as the last class
            # in the inheritance list and inherit from Object
            rootbase = cls.__bases__[-1]
            cls_names = [rootbase.__name__]
        else:
            cls_names = [cls.__name__]

        if self.names is not None:
            cls_names += self.names

        for name in cls_names:
            if name in REGISTRY:
                log.warn(
                    "duplicate registration of "
                    "class `{0}`".format(name))
            REGISTRY[name] = cls
        return cls

def create(cls_name, *args, **kwargs):

    cls = getattr(R, cls_name, None)
    if cls is None:
        return None
    obj = cls(*args, **kwargs)
    return asrootpy(obj)
<commit_msg>Fix from rootpy import *<commit_after># Copyright 2012 the rootpy developers
# distributed under the terms of the GNU General Public License
from __future__ import absolute_import

import sys
from collections import namedtuple

# DO NOT expose ROOT at module level here since that conflicts with rootpy.ROOT
# See issue https://github.com/rootpy/rootpy/issues/343
import ROOT as R

from .logger import log
# Needed for "from rootpy import QROOT" by other modules
from .utils import quickroot as QROOT
from . import defaults
from .base import Object
from .info import __version_info__, __version__

__all__ = [
    'log',
    'ROOT_VERSION',
    'QROOT',
    'asrootpy',
    'lookup',
    'lookup_by_name',
    'lookup_rootpy',
    'register',
    'create',
]

class ROOTVersion(namedtuple('_ROOTVersionBase',
                             ['major', 'minor', 'micro'])):

    def __new__(cls, version):
        if version < 1E4:
            raise ValueError(
                "{0:d} is not a valid ROOT version integer".format(version))
        return super(ROOTVersion, cls).__new__(
            cls,
            int(version / 1E4), int((version / 1E2) % 100), int(version % 100))

    def __repr__(self):
        return str(self)

    def __str__(self):
        return '{0:d}.{1:02d}/{2:02d}'.format(*self)

# Note: requires defaults import
ROOT_VERSION = ROOTVersion(QROOT.gROOT.GetVersionInt())
log.debug("Using ROOT {0}".format(ROOT_VERSION))

IN_NOSETESTS = False
if sys.argv and sys.argv[0].endswith('nosetests'):
    IN_NOSETESTS = True

IN_IPYTHON = '__IPYTHON__' in __builtins__
if IN_IPYTHON:
    from IPython.kernel.zmq.iostream import OutStream
    IN_IPYTHON_NOTEBOOK = isinstance(sys.stdout, OutStream)
else:
    IN_IPYTHON_NOTEBOOK = False

class ROOTError(RuntimeError):
    """
    Exception class representing a ROOT error/warning message.
    """
    def __init__(self, level, location, msg):
        self.level, self.location, self.msg = level, location, msg

    def __str__(self):
        return "level={0}, loc='{1}', msg='{2}'".format(
            self.level, self.location, self.msg)

def rootpy_source_dir():
    import rootpy
    from os.path import abspath, dirname
    from inspect import getfile
    from sys import modules
    path = dirname(getfile(modules[__name__]))
    absp = abspath(path)
    return path, absp

_ROOTPY_SOURCE_PATH, _ROOTPY_SOURCE_ABSPATH = rootpy_source_dir()
del rootpy_source_dir

"""
All rootpy wrappers are registered below. This dict maps the ROOT class name to
the path to the rootpy class or a tuple of both the path and keyword arguments
used in the dynamic_cls classmethod (see the ``Hist``, ``Hist2D`` and
``Hist3D`` classes in ``plotting.hist``).

This way rootpy is aware of all classes that inherit from ROOT classes without
needing to import everything when rootpy is first imported. This registry is
required to cast ROOT objects into the rootpy form when extracted from a ROOT
TFile, for example.
"""
INIT_REGISTRY = {

    'TList': 'collection.List',
    'TObjArray': 'collection.ObjArray',

    'TTree': 'tree.tree.Tree',
    'TNtuple': 'tree.tree.Ntuple',

    'TKey': 'io.file.Key',
    'TDirectoryFile': 'io.file.Directory',
    'TFile': 'io.file.File',
    'TMemFile': 'io.file.MemFile',

    'TStyle': 'plotting.style.Style',
    'TCanvas': 'plotting.canvas.Canvas',
    'TPad': 'plotting.canvas.Pad',
    'TPave': 'plotting.box.Pave',
    'TPaveStats': 'plotting.box.PaveStats',
    'TLegend': 'plotting.legend.Legend',
    'TEllipse': 'plotting.shapes.Ellipse',
    'TLine': 'plotting.shapes.Line',
    'TArrow': 'plotting.shapes.Arrow',

    'TF1': 'plotting.func.F1',
    'TF2': 'plotting.func.F2',
    'TF3': 'plotting.func.F3',

    'TGraph': ('plotting.graph.Graph', dict(type='default')),
    'TGraphErrors': ('plotting.graph.Graph', dict(type='errors')),
    'TGraphAsymmErrors': ('plotting.graph.Graph', dict(type='asymm')),
    'TGraphBentErrors': ('plotting.graph.Graph', dict(type='benterrors')),

    'TGraph2D': ('plotting.graph.Graph2D', dict(type='default')),
    'TGraph2DErrors': ('plotting.graph.Graph2D', dict(type='errors')),

    'TProfile': 'plotting.profile.Profile',
    'TProfile2D': 'plotting.profile.Profile2D',
    'TProfile3D': 'plotting.profile.Profile3D',

    'TH1C': ('plotting.hist.Hist', dict(type='C')),
    'TH1S': ('plotting.hist.Hist', dict(type='S')),
    'TH1I': ('plotting.hist.Hist', dict(type='I')),
    'TH1F': ('plotting.hist.Hist', dict(type='F')),
    'TH1D': ('plotting.hist.Hist', dict(type='D')),

    'TH2C': ('plotting.hist.Hist2D', dict(type='C')),
    'TH2S': ('plotting.hist.Hist2D', dict(type='S')),
    'TH2I': ('plotting.hist.Hist2D', dict(type='I')),
    'TH2F': ('plotting.hist.Hist2D', dict(type='F')),
    'TH2D': ('plotting.hist.Hist2D', dict(type='D')),

    'TH3C': ('plotting.hist.Hist3D', dict(type='C')),
    'TH3S': ('plotting.hist.Hist3D', dict(type='S')),
    'TH3I': ('plotting.hist.Hist3D', dict(type='I')),
    'TH3F': ('plotting.hist.Hist3D', dict(type='F')),
    'TH3D': ('plotting.hist.Hist3D', dict(type='D')),

    'TEfficiency': 'plotting.hist.Efficiency',

    'THStack': 'plotting.hist.HistStack',

    'TAxis': 'plotting.axis.Axis',

    'TVector2': 'vector.Vector2',
    'TVector3': 'vector.Vector3',
    'TLorentzVector': 'vector.LorentzVector',
    'TRotation': 'vector.Rotation',
    'TLorentzRotation': 'vector.LorentzRotation',

    'TMatrixT<float>': ('matrix.Matrix', dict(type='float')),
    'TMatrixT<double>': ('matrix.Matrix', dict(type='double')),
    'TMatrixTSym<float>': ('matrix.SymmetricMatrix', dict(type='float')),
    'TMatrixTSym<double>': ('matrix.SymmetricMatrix', dict(type='double')),

    'RooWorkspace': 'stats.workspace.Workspace',
    'RooStats::ModelConfig': 'stats.modelconfig.ModelConfig',
    'RooArgSet': 'stats.collection.ArgSet',
    'RooArgList': 'stats.collection.ArgList',
    'RooRealVar': 'stats.value.RealVar',
    'RooSimultaneous': 'stats.pdf.Simultaneous',
    'RooAddPdf': 'stats.pdf.AddPdf',
    'RooProdPdf': 'stats.pdf.ProdPdf',
    'RooCatType': 'stats.category.CatType',
    'RooCategory': 'stats.category.Category',
    'RooDataSet': 'stats.dataset.DataSet',
    'RooMinimizer': 'stats.fit.Minimizer',
    'RooFitResult': 'stats.fit.FitResult',

    'RooStats::HistFactory::Data': 'stats.histfactory.Data',
    'RooStats::HistFactory::Sample': 'stats.histfactory.Sample',
    'RooStats::HistFactory::HistoSys': 'stats.histfactory.HistoSys',
    'RooStats::HistFactory::HistoFactor': 'stats.histfactory.HistoFactor',
    'RooStats::HistFactory::OverallSys': 'stats.histfactory.OverallSys',
    'RooStats::HistFactory::NormFactor': 'stats.histfactory.NormFactor',
    'RooStats::HistFactory::ShapeSys': 'stats.histfactory.ShapeSys',
    'RooStats::HistFactory::ShapeFactor': 'stats.histfactory.ShapeFactor',
    'RooStats::HistFactory::Channel': 'stats.histfactory.Channel',
    'RooStats::HistFactory::Measurement': 'stats.histfactory.Measurement',
}

# map rootpy name to location in rootpy (i.e. Axis -> plotting.axis)
INIT_REGISTRY_ROOTPY = {}
for rtype, rptype in INIT_REGISTRY.items():
    if isinstance(rptype, tuple):
        rptype = rptype[0]
    cls_path, _, cls_name = rptype.rpartition('.')
    INIT_REGISTRY_ROOTPY[cls_name] = cls_path

# these dicts are populated as classes are registered at runtime
# ROOT class name -> rootpy class
REGISTRY = {}
# rootpy class name -> rootpy class
REGISTRY_ROOTPY = {}

def asrootpy(thing, **kwargs):

    # is this thing already converted?
    if isinstance(thing, Object):
        return thing

    warn = kwargs.pop('warn', True)
    after_init = kwargs.pop('after_init', False)

    # is this thing a class?
    if isinstance(thing, QROOT.PyRootType):
        if issubclass(thing, Object):
            return thing
        result = lookup(thing)
        if result is None:
            if warn:
                log.warn(
                    "There is no rootpy implementation "
                    "of the class `{0}`".format(thing.__name__))
            return thing
        if after_init:
            # preserve ROOT's __init__
            class asrootpy_cls(result):
                def __new__(self, *args, **kwargs):
                    return asrootpy(thing(*args, **kwargs), warn=warn)
            asrootpy_cls.__name__ = '{0}_asrootpy'.format(thing.__name__)
            return asrootpy_cls
        return result

    thing_cls = thing.__class__
    rootpy_cls = lookup(thing_cls)
    if rootpy_cls is None:
        if warn:
            log.warn(
                "A subclass of `{0}` is not "
                "implemented in rootpy".format(
                    thing_cls.__name__))
        return thing

    # cast
    thing.__class__ = rootpy_cls
    # call the _post_init if one exists
    if hasattr(thing, '_post_init'):
        thing._post_init(**kwargs)

    return thing

def _get_class(path, name):

    rootpy_module = __import__(
        path, globals(), locals(), [name], -1)
    return getattr(rootpy_module, name)

def lookup(cls):

    cls_name = cls.__name__
    return lookup_by_name(cls_name)

def lookup_by_name(cls_name):

    if cls_name in REGISTRY:
        return REGISTRY[cls_name]
    if cls_name not in INIT_REGISTRY:
        return None
    entry = INIT_REGISTRY[cls_name]
    if isinstance(entry, tuple):
        path, dynamic_kwargs = entry
    elif isinstance(entry, basestring):
        path = entry
        dynamic_kwargs = None
    cls_path, _, rootpy_cls_name = path.rpartition('.')

    rootpy_cls = _get_class(cls_path, rootpy_cls_name)

    if dynamic_kwargs is not None:
        rootpy_cls = rootpy_cls.dynamic_cls(**dynamic_kwargs)
    REGISTRY[cls_name] = rootpy_cls
    return rootpy_cls

def lookup_rootpy(rootpy_cls_name):

    rootpy_cls = REGISTRY_ROOTPY.get(rootpy_cls_name, None)
    if rootpy_cls is not None:
        return rootpy_cls
    cls_path = INIT_REGISTRY_ROOTPY.get(rootpy_cls_name, None)
    if cls_path is None:
        return None

    rootpy_cls = _get_class(cls_path, rootpy_cls_name)

    REGISTRY_ROOTPY[rootpy_cls_name] = rootpy_cls
    return rootpy_cls

class register(object):

    def __init__(self, names=None, builtin=False):

        if names is not None:
            if type(names) not in (list, tuple):
                names = [names]
        self.names = names
        self.builtin = builtin

    def __call__(self, cls):

        if issubclass(cls, Object):
            # all rootpy classes which inherit from ROOT classes
            # must place the ROOT base class as the last class
            # in the inheritance list and inherit from Object
            rootbase = cls.__bases__[-1]
            cls_names = [rootbase.__name__]
        else:
            cls_names = [cls.__name__]

        if self.names is not None:
            cls_names += self.names

        for name in cls_names:
            if name in REGISTRY:
                log.warn(
                    "duplicate registration of "
                    "class `{0}`".format(name))
            REGISTRY[name] = cls
        return cls

def create(cls_name, *args, **kwargs):

    cls = getattr(R, cls_name, None)
    if cls is None:
        return None
    obj = cls(*args, **kwargs)
    return asrootpy(obj)
<|endoftext|>


<commit_before># coding: utf-8
from __future__ import print_function, division
import json
import urllib
import os

try:
    import urlparse
    from urllib import urlencode
except:  # For Python 3
    import urllib.parse as urlparse
    from urllib.parse import urlencode

##############
# SEARCH URL #
##############
# http://pkk5.rosreestr.ru/api/features/1
#   ?text=38:36:000021:1106
#   &tolerance=4
#   &limit=11
SEARCH_URL = "http://pkk5.rosreestr.ru/api/features/1"

############################
# URL to get area metainfo #
############################
# http://pkk5.rosreestr.ru/api/features/1/38:36:21:1106
META_URL = "http://pkk5.rosreestr.ru/api/features/1/"

#########################
# URL to get area image #
#########################
# http://pkk5.rosreestr.ru/arcgis/rest/services/Cadastre/CadastreSelected/MapServer/export
#   ?dpi=96
#   &transparent=true
#   &format=png32
#   &layers=show%3A6%2C7
#   &bbox=11612029.005008286%2C6849457.6834302815%2C11612888.921576614%2C6849789.706771941
#   &bboxSR=102100
#   &imageSR=102100
#   &size=1440%2C556
#   &layerDefs=%7B%226%22%3A%22ID%20%3D%20%2738%3A36%3A21%3A1106%27%22%2C%227%22%3A%22ID%20%3D%20%2738%3A36%3A21%3A1106%27%22%7D
#   &f=image
# WHERE:
#    "layerDefs" decode to {"6":"ID = '38:36:21:1106'","7":"ID = '38:36:21:1106'"}
#    "f" may be `json` or `html`
#    set `&format=svg&f=json` to export image in svg
IMAGE_URL = "http://pkk5.rosreestr.ru/arcgis/rest/services/Cadastre/CadastreSelected/MapServer/export"

class Area:
    search_url = SEARCH_URL
    meta_url = META_URL
    image_url = IMAGE_URL
    buffer = 10

    def __init__(self, code, media_path=""):
        self.media_path = media_path
        self.image_url = ""
        self.xy = []
        self.width = 0
        self.height = 0
        self.image_path = ""
        self.extent = {}
        self.image_extent = {}
        self.center = {}
        self.attrs = {}
        if not self.media_path:
            self.media_path = os.path.dirname(os.path.realpath(__file__))
        if not os.path.isdir(self.media_path):
            os.makedirs(self.media_path)
        self.code = code
        self.code_id = ""
        self.file_name = self.code.replace(":", "-")
        search_data = self.search()
        self.download_meta()
        if search_data:
            formats = ["svg", ""]
            for f in formats:
                self.image_url = self.get_image_url(f)
                if self.image_url:
                    image = self.download_image(f)
                    if image:
                        geom = self.get_geometry(f);
                        break

    def get_coord(self):
        return self.xy

    def get_attrs(self):
        return self.attrs

    def to_geojson(self):
        if self.xy:
            features = []
            feature_collection = {
                "type": "FeatureCollection",
                "crs": {"type": "name", "properties": {"name": "EPSG:3857"}},
                "features": features
            }
            for x, y in self.xy:
                feature = {"type": "Feature", "geometry": {"type": "Point", "coordinates": [x, y]}}
                features.append(feature)
            return json.dumps(feature_collection)
        return False

    def get_image_url(self, format):
        if self.code_id and self.extent:
            ex = self.get_buffer_extent_list()
            dx, dy = map(lambda i: int((ex[i[0]] - ex[i[1]]) * 5), [[2, 0], [3, 1]])
            code = self.code_id
            layers = ["6", "7"]
            params = {
                "dpi": 96,
                "transparent": "false",
                "format": "png32",
                "layers": "show:%s" % ",".join(layers),
                "bbox": ",".join(map(str, ex)),
                "bboxSR": 102100,
                "imageSR": 102100,
                "size": "%s,%s" % (dx, dy),
                "layerDefs": {layer: str("ID = '%s'" % code) for layer in layers},
                "f": "json"
            }
            if format:
                params["format"] = format
            url_parts = list(urlparse.urlparse(IMAGE_URL))
            query = dict(urlparse.parse_qsl(url_parts[4]))
            query.update(params)
            url_parts[4] = urlencode(query)
            meta_url = urlparse.urlunparse(url_parts)
            if meta_url:
                response = urllib.urlopen(meta_url)
                data = json.loads(response.read())
                if data["href"]:
                    image_url = data["href"]
                    self.width = data["width"]
                    self.height = data["height"]
                    self.image_extent = data["extent"]
                    # print(image_url)
                    return image_url
        return False

    def download_image(self, format="png"):
        try:
            image_file = urllib.URLopener()
            basedir = self.media_path
            savedir = os.path.join(basedir, "tmp")
            if not os.path.isdir(savedir):
                os.makedirs(savedir)
            file_path = os.path.join(savedir, "%s.%s" % (self.file_name, format))
            image_file.retrieve(self.image_url, file_path)
            self.image_path = file_path
            return image_file
        except Exception:
            print("Nothing found")
        return False

    def get_meta_url(self):
        if self.code_id:
            # url = urlparse.urljoin(self.meta_url, self.code_id)
            url = self.meta_url + str(self.code_id)
            return url
        return ""

    def download_meta(self):
        url = self.get_meta_url()
        if url:
            search_url = url
            response = urllib.urlopen(search_url)
            data = json.loads(response.read())
            if data["feature"] and data["feature"]["attrs"]:
                self.attrs = data["feature"]["attrs"]
                return self.attrs
        return False

    @staticmethod
    def get_extent_list(extent):
        return [extent["xmin"], extent["ymin"], extent["xmax"], extent["ymax"]]

    def get_buffer_extent_list(self):
        ex = self.extent
        buf = self.buffer
        ex = [ex["xmin"] - buf, ex["ymin"] - buf, ex["xmax"] + buf, ex["ymax"] + buf]
        return ex

    def search(self):
        try:
            search_data = self.download_search_result()
            if search_data:
                features = search_data["features"]
                if features and len(features):
                    area = features[0]
                    attrs = area["attrs"]
                    if attrs["cn"] == self.code:
                        self.attrs["address"] = attrs["address"]
                        self.code_id = attrs["id"]
                        self.extent = area["extent"]
                        self.center = area["center"]
                        return search_data
        except Exception:
            pass
        print("Nothing found")
        return False

    def download_search_result(self):
        search_url = SEARCH_URL + "?text=%s&tolerance=4&limit=1" % self.code
        response = urllib.urlopen(search_url)
        data = json.loads(response.read())
        return data

    def get_geometry(self, format):
        if format == "svg":
            self.xy = self.read_svg()
        else:
            image_xy_corner = self.get_image_xy_corner()
            if image_xy_corner:
                self.xy = self.image_corners_to_coord(image_xy_corner)
        return self.xy

    def read_svg(self):
        # from xml.dom import minidom
        # import pysvg.parser
        import svg
        svg_coord = []
        object = svg.parse(self.image_path)
        self.get_svg_points(object, svg_coord)
        self.xy = self.image_corners_to_coord(svg_coord)
        return self.xy
        # svg = pysvg.parser.parse(self.image_path)
        # p = svg.get_width(), svg.get_height()

        # doc = minidom.parse(self.image_path)  # parseString also exists
        # path_strings = [path.getAttribute('d') for path
        #                 in doc.getElementsByTagName('path')]
        # doc.unlink()

    def get_svg_points(self, object, svg_coord):
        if object.__dict__.has_key("items"):
            for i in object.items:
                if i.__dict__.has_key("start"):
                    svg_coord.append([i.start.x, i.start.y])
                else:
                    self.get_svg_points(i, svg_coord)
        else:
            pass

    def get_image_xy_corner(self):
        import numpy as np
        import cv2
        # from matplotlib import pyplot as plt

        image_xy_corners = []
        img = cv2.imread(self.image_path)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # gray = cv2.medianBlur(gray,5)
        try:
            corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 1, useHarrisDetector=True)
            corners = np.int0(corners)
            for i in corners:
                x, y = i.ravel()
                image_xy_corners.append([x, y])
            return image_xy_corners
        except Exception as ex:
            print(ex)
        return image_xy_corners

    def image_corners_to_coord(self, image_xy_corners):
        ex = self.get_extent_list(self.image_extent)
        dx = ((ex[2] - ex[0]) / self.width)
        dy = ((ex[3] - ex[1]) / self.height)
        xy_corners = []
        for im_x, im_y in image_xy_corners:
            x = ex[0] + (im_x * dx)
            y = ex[3] - (im_y * dy)
            xy_corners.append([x, y])
        return xy_corners

    def show_plot(self, image_xy_corner):
        import cv2
        from matplotlib import pyplot as plt
        corners = image_xy_corners
        img = cv2.imread(self.image_path)
        for x, y in corners:
            cv2.circle(img, (x, y), 3, 255, -1)
        plt.imshow(img), plt.show()

def getopts():
    import argparse
    import textwrap

    """
    Get the command line options.
    """
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=textwrap.dedent("""\
        Get geojson with coordinates of area by cadastral number.
        http://pkk5.rosreestr.ru/
        """)
    )
    parser.add_argument('-c', '--code', action='store', type=str, required=True,
                        help='area cadastral number')

    opts = parser.parse_args()

    return opts

if __name__ == "__main__":
    area = Area("38:36:000021:1106")
    # area = Area("38:06:144003:4723")
    # area = Area("38:36:000033:375")
    abspath = os.path.abspath('.')
    geojson = area.to_geojson()
    if geojson:
        filename = '%s.geojson' % area.file_name
        f = open(filename, 'w')
        f.write(geojson)
        f.close()
        print(os.path.join(abspath, filename))

    # abspath = os.path.abspath('.')
    # opt = getopts()
    # if opt.code:
    #     area = Area(opt.code)
    #     geojson = area.to_geojson()
    #     if geojson:
    #         filename = '%s.geojson' % area.file_name
    #         f = open(filename, 'w')
    #         f.write(geojson)
    #         f.close()
    #         print(os.path.join(abspath, filename))
<commit_msg>comment test block<commit_after># coding: utf-8
from __future__ import print_function, division
import json
import urllib
import os

try:
    import urlparse
    from urllib import urlencode
except:  # For Python 3
    import urllib.parse as urlparse
    from urllib.parse import urlencode

##############
# SEARCH URL #
##############
# http://pkk5.rosreestr.ru/api/features/1
#   ?text=38:36:000021:1106
#   &tolerance=4
#   &limit=11
SEARCH_URL = "http://pkk5.rosreestr.ru/api/features/1"

############################
# URL to get area metainfo #
############################
# http://pkk5.rosreestr.ru/api/features/1/38:36:21:1106
META_URL = "http://pkk5.rosreestr.ru/api/features/1/"

#########################
# URL to get area image #
#########################
# http://pkk5.rosreestr.ru/arcgis/rest/services/Cadastre/CadastreSelected/MapServer/export
#   ?dpi=96
#   &transparent=true
#   &format=png32
#   &layers=show%3A6%2C7
#   &bbox=11612029.005008286%2C6849457.6834302815%2C11612888.921576614%2C6849789.706771941
#   &bboxSR=102100
#   &imageSR=102100
#   &size=1440%2C556
#   &layerDefs=%7B%226%22%3A%22ID%20%3D%20%2738%3A36%3A21%3A1106%27%22%2C%227%22%3A%22ID%20%3D%20%2738%3A36%3A21%3A1106%27%22%7D
#   &f=image
# WHERE:
#    "layerDefs" decode to {"6":"ID = '38:36:21:1106'","7":"ID = '38:36:21:1106'"}
#    "f" may be `json` or `html`
#    set `&format=svg&f=json` to export image in svg
IMAGE_URL = "http://pkk5.rosreestr.ru/arcgis/rest/services/Cadastre/CadastreSelected/MapServer/export"

class Area:
    search_url = SEARCH_URL
    meta_url = META_URL
    image_url = IMAGE_URL
    buffer = 10

    def __init__(self, code, media_path=""):
        self.media_path = media_path
        self.image_url = ""
        self.xy = []
        self.width = 0
        self.height = 0
        self.image_path = ""
        self.extent = {}
        self.image_extent = {}
        self.center = {}
        self.attrs = {}
        if not self.media_path:
            self.media_path = os.path.dirname(os.path.realpath(__file__))
        if not os.path.isdir(self.media_path):
            os.makedirs(self.media_path)
        self.code = code
        self.code_id = ""
        self.file_name = self.code.replace(":", "-")
        search_data = self.search()
        self.download_meta()
        if search_data:
            formats = ["svg", ""]
            for f in formats:
                self.image_url = self.get_image_url(f)
                if self.image_url:
                    image = self.download_image(f)
                    if image:
                        geom = self.get_geometry(f);
                        break

    def get_coord(self):
        return self.xy

    def get_attrs(self):
        return self.attrs

    def to_geojson(self):
        if self.xy:
            features = []
            feature_collection = {
                "type": "FeatureCollection",
                "crs": {"type": "name", "properties": {"name": "EPSG:3857"}},
                "features": features
            }
            for x, y in self.xy:
                feature = {"type": "Feature", "geometry": {"type": "Point", "coordinates": [x, y]}}
                features.append(feature)
            return json.dumps(feature_collection)
        return False

    def get_image_url(self, format):
        if self.code_id and self.extent:
            ex = self.get_buffer_extent_list()
            dx, dy = map(lambda i: int((ex[i[0]] - ex[i[1]]) * 5), [[2, 0], [3, 1]])
            code = self.code_id
            layers = ["6", "7"]
            params = {
                "dpi": 96,
                "transparent": "false",
                "format": "png32",
                "layers": "show:%s" % ",".join(layers),
                "bbox": ",".join(map(str, ex)),
                "bboxSR": 102100,
                "imageSR": 102100,
                "size": "%s,%s" % (dx, dy),
                "layerDefs": {layer: str("ID = '%s'" % code) for layer in layers},
                "f": "json"
            }
            if format:
                params["format"] = format
            url_parts = list(urlparse.urlparse(IMAGE_URL))
            query = dict(urlparse.parse_qsl(url_parts[4]))
            query.update(params)
            url_parts[4] = urlencode(query)
            meta_url = urlparse.urlunparse(url_parts)
            if meta_url:
                response = urllib.urlopen(meta_url)
                data = json.loads(response.read())
                if data["href"]:
                    image_url = data["href"]
                    self.width = data["width"]
                    self.height = data["height"]
                    self.image_extent = data["extent"]
                    # print(image_url)
                    return image_url
        return False

    def download_image(self, format="png"):
        try:
            image_file = urllib.URLopener()
            basedir = self.media_path
            savedir = os.path.join(basedir, "tmp")
            if not os.path.isdir(savedir):
                os.makedirs(savedir)
            file_path = os.path.join(savedir, "%s.%s" % (self.file_name, format))
            image_file.retrieve(self.image_url, file_path)
            self.image_path = file_path
            return image_file
        except Exception:
            print("Nothing found")
        return False

    def get_meta_url(self):
        if self.code_id:
            # url = urlparse.urljoin(self.meta_url, self.code_id)
            url = self.meta_url + str(self.code_id)
            return url
        return ""

    def download_meta(self):
        url = self.get_meta_url()
        if url:
            search_url = url
            response = urllib.urlopen(search_url)
            data = json.loads(response.read())
            if data["feature"] and data["feature"]["attrs"]:
                self.attrs = data["feature"]["attrs"]
                return self.attrs
        return False

    @staticmethod
    def get_extent_list(extent):
        return [extent["xmin"], extent["ymin"], extent["xmax"], extent["ymax"]]

    def get_buffer_extent_list(self):
        ex = self.extent
        buf = self.buffer
        ex = [ex["xmin"] - buf, ex["ymin"] - buf, ex["xmax"] + buf, ex["ymax"] + buf]
        return ex

    def search(self):
        try:
            search_data = self.download_search_result()
            if search_data:
                features = search_data["features"]
                if features and len(features):
                    area = features[0]
                    attrs = area["attrs"]
                    if attrs["cn"] == self.code:
                        self.attrs["address"] = attrs["address"]
                        self.code_id = attrs["id"]
                        self.extent = area["extent"]
                        self.center = area["center"]
                        return search_data
        except Exception:
            pass
        print("Nothing found")
        return False

    def download_search_result(self):
        search_url = SEARCH_URL + "?text=%s&tolerance=4&limit=1" % self.code
        response = urllib.urlopen(search_url)
        data = json.loads(response.read())
        return data

    def get_geometry(self, format):
        if format == "svg":
            self.xy = self.read_svg()
        else:
            image_xy_corner = self.get_image_xy_corner()
            if image_xy_corner:
                self.xy = self.image_corners_to_coord(image_xy_corner)
        return self.xy

    def read_svg(self):
        # from xml.dom import minidom
        # import pysvg.parser
        import svg
        svg_coord = []
        object = svg.parse(self.image_path)
        self.get_svg_points(object, svg_coord)
        self.xy = self.image_corners_to_coord(svg_coord)
        return self.xy
        # svg = pysvg.parser.parse(self.image_path)
        # p = svg.get_width(), svg.get_height()

        # doc = minidom.parse(self.image_path)  # parseString also exists
        # path_strings = [path.getAttribute('d') for path
        #                 in doc.getElementsByTagName('path')]
        # doc.unlink()

    def get_svg_points(self, object, svg_coord):
        if object.__dict__.has_key("items"):
            for i in object.items:
                if i.__dict__.has_key("start"):
                    svg_coord.append([i.start.x, i.start.y])
                else:
                    self.get_svg_points(i, svg_coord)
        else:
            pass

    def get_image_xy_corner(self):
        import numpy as np
        import cv2
        # from matplotlib import pyplot as plt

        image_xy_corners = []
        img = cv2.imread(self.image_path)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # gray = cv2.medianBlur(gray,5)
        try:
            corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 1, useHarrisDetector=True)
            corners = np.int0(corners)
            for i in corners:
                x, y = i.ravel()
                image_xy_corners.append([x, y])
            return image_xy_corners
        except Exception as ex:
            print(ex)
        return image_xy_corners

    def image_corners_to_coord(self, image_xy_corners):
        ex = self.get_extent_list(self.image_extent)
        dx = ((ex[2] - ex[0]) / self.width)
        dy = ((ex[3] - ex[1]) / self.height)
        xy_corners = []
        for im_x, im_y in image_xy_corners:
            x = ex[0] + (im_x * dx)
            y = ex[3] - (im_y * dy)
            xy_corners.append([x, y])
        return xy_corners

    def show_plot(self, image_xy_corner):
        import cv2
        from matplotlib import pyplot as plt
        corners = image_xy_corners
        img = cv2.imread(self.image_path)
        for x, y in corners:
            cv2.circle(img, (x, y), 3, 255, -1)
        plt.imshow(img), plt.show()

def getopts():
    import argparse
    import textwrap

    """
    Get the command line options.
    """
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=textwrap.dedent("""\
        Get geojson with coordinates of area by cadastral number.
        http://pkk5.rosreestr.ru/
        """)
    )
    parser.add_argument('-c', '--code', action='store', type=str, required=True,
                        help='area cadastral number')

    opts = parser.parse_args()

    return opts

if __name__ == "__main__":
    # area = Area("38:36:000021:1106")
    # area = Area("38:06:144003:4723")
    # area = Area("38:36:000033:375")

    abspath = os.path.abspath('.')
    opt = getopts()
    if opt.code:
        area = Area(opt.code)
        geojson = area.to_geojson()
        if geojson:
            filename = '%s.geojson' % area.file_name
            f = open(filename, 'w')
            f.write(geojson)
            f.close()
            print(os.path.join(abspath, filename))
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""Create a matplotlib animation from route data."""
from __future__ import print_function, division
import glob
import numpy as np
import os
import sys
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation

from route_plotter import parse_route, stitch_tiles, coords_to_bbox

def main():
  args = parse_args()
  routes = _parse_routes(args.route)
  if not routes:
    print('No valid route data provided.', file=sys.stderr)
    return
  print('Parsed', len(routes), 'valid routes.')

  routes = _filter_routes(routes, loop_gap_threshold=args.max_loop_gap,
                          start_cluster_radius=args.max_start_dist)
  assert bool(routes), '_filter_routes removed all routes'
  print('Filtered to', len(routes), 'routes.')

  if not os.path.exists(args.tile_cache):
    os.makedirs(args.tile_cache)
  bg_img, bg_extent = stitch_tiles(_bounding_box(routes), zoom=args.zoom,
                                   cachedir=args.tile_cache,
                                   flatten=(not args.color_tiles),
                                   margin_scale=args.max_margin)
  print('Assembled %d x %d background map image.' % bg_img.shape[:2])

  fig, ax = _setup_figure(bg_img, bg_extent, scale=args.scale)
  max_duration = max(s[-1] for _,s in routes)
  frame_times = np.linspace(0, max_duration, args.num_frames)
  anim = _animate(fig, ax, routes, frame_times)
  print('Prepared animation loop.')

  if args.save:
    dpi = plt.rcParams.get('figure.dpi', 100.0)
    anim.save(args.save, dpi=dpi, savefig_kwargs=dict(pad_inches=0))
  else:
    plt.show()

def _animate(fig, ax, routes, frame_data):
  lines = ax.plot(np.zeros((0, len(routes))), c='r', alpha=0.75, zorder=1)
  heads = ax.scatter([], [], c='b', zorder=2)
  timer = ax.text(1, 0, '0:00:00', transform=ax.transAxes,
                  bbox=dict(facecolor='white'),
                  verticalalignment='bottom', horizontalalignment='right')
  no_head = np.full(2, np.nan)
  end_pts = np.zeros((len(routes), 2))
  plot_data = []
  for i, line in enumerate(lines):
    r, s = routes[i]
    plot_data.append((np.fliplr(r), s, line))
  artists = lines + [heads, timer]

  def update_frame(num_seconds):
    for i, (xy, time, line) in enumerate(plot_data):
      idx = np.searchsorted(time, num_seconds)
      if idx < xy.shape[0]:
        line.set_data(xy[:idx].T)
        end_pts[i] = xy[idx]
      elif idx == xy.shape[0]:
        line.set_data(xy[:idx].T)
        end_pts[i] = no_head
      else:
        end_pts[i] = no_head
    heads.set_offsets(end_pts)
    # update the clock
    mins, secs = divmod(num_seconds, 60)
    hours, mins = divmod(mins, 60)
    timer.set_text('%d:%02d:%02d' % (hours, mins, secs))
    return artists

  return FuncAnimation(fig, update_frame, frames=frame_data, blit=True,
                       interval=100, repeat=True, repeat_delay=150)

def parse_args():
  cache_dir = os.path.join(os.path.dirname(__file__), 'tiles')
  ap = ArgumentParser(description=__doc__,
                      formatter_class=ArgumentDefaultsHelpFormatter)
  ap.add_argument('--color-tiles', action='store_true',
                  help='Use full-color map tiles.')
  ap.add_argument('--scale', type=float, default=1,
                  help='Figure size scale fraction.')
  ap.add_argument('--zoom', type=int, default=None, help='Map zoom level.')
  ap.add_argument('--tile-cache', type=str, default=cache_dir,
                  help='Directory for storing cached map tiles.')
  ap.add_argument('--save', type=str, help='Save animation to disk.')
  ap.add_argument('--max-margin', type=float, default=0.05,
                  help='Keep up to this fraction of width/height as margin.')
  ap.add_argument('--max-loop-gap', type=float, default=200,
                  help='Maximum gap between start/end points, in meters.')
  ap.add_argument('--max-start-dist', type=float, default=200,
                  help=('Maximum distance from route start to the '
                        'mean starting location, in meters.'))
  ap.add_argument('--num-frames', type=int, default=500,
                  help='Number of frames to animate.')
  ap.add_argument('route', type=str, nargs='+', help='Route file(s) to use.')
  args = ap.parse_args()
  if len(args.route) == 1:
    args.route = glob.glob(args.route[0])
  args.route = map(open, args.route)
  return args

def _setup_figure(bg_img, bg_extent, scale=1.0):
  plt.rc('figure', autolayout=False)  # turn off tight_layout
  dpi = plt.rcParams.get('figure.dpi', 100.0)
  fig = plt.figure(dpi=dpi, frameon=False)

  # scale the figure to fit the bg image
  bg_height, bg_width = bg_img.shape[:2]
  fig.set_size_inches(bg_width / dpi * scale, bg_height / dpi * scale)

  ax = fig.add_axes([0, 0, 1, 1])
  ax.set_axis_off()
  ax.xaxis.set_major_locator(plt.NullLocator())
  ax.yaxis.set_major_locator(plt.NullLocator())
  ax.imshow(bg_img, zorder=0, extent=bg_extent, cmap='Greys_r')
  ax.autoscale(False)
  ax.margins(0, 0)
  return fig, ax

def _parse_routes(file_paths):
  all_coords = []
  for fh in file_paths:
    coords, times = parse_route(fh, return_time=True)
    if coords.ndim == 2 and coords.shape[0] >= 2 and coords.shape[1] == 2:
      seconds = (times - times[0]).astype('timedelta64[s]').astype(int)
      assert coords.shape[0] == seconds.shape[0]
      all_coords.append((coords, seconds))
  return all_coords

def _filter_routes(routes, loop_gap_threshold=200, start_cluster_radius=200):
  """Selects routes that:
    - start and end in roughly the same place (forming a loop)
    - start close to the mean starting location
  """
  starts = np.array([r[0] for r,_ in routes])
  ends = np.array([r[-1] for r,_ in routes])

  # convert lat/lon displacements into approximate distances (meters)
  gap_dists = _greatcircle_distance(starts, ends)
  mask = gap_dists < loop_gap_threshold

  # iterative thresholding to narrow down the center location
  thresh = np.inf
  while thresh > start_cluster_radius:
    start_dist = _greatcircle_distance(starts, starts[mask].mean(axis=0))
    thresh = np.percentile(start_dist[mask], 95)
    mask &= start_dist < thresh
  return [routes[i] for i in np.where(mask)[0]]

def _bounding_box(routes):
  min_coord = np.array([r.min(axis=0) for r,_ in routes]).min(axis=0)
  max_coord = np.array([r.max(axis=0) for r,_ in routes]).max(axis=0)
  return coords_to_bbox([min_coord, max_coord])

def _greatcircle_distance(latlon0, latlon1):
  EARTH_RADIUS = 6371000  # approximate spherical radius
  lat0, lon0 = np.deg2rad(latlon0).T
  lat1, lon1 = np.deg2rad(latlon1).T
  return EARTH_RADIUS * np.arccos(
      np.sin(lat0) * np.sin(lat1) +
      np.cos(lat0) * np.cos(lat1) * np.cos(np.abs(lon0 - lon1)))

if __name__ == '__main__':
  main()
<commit_msg>adding a bunch of command-line options for styling<commit_after>#!/usr/bin/env python
"""Create a matplotlib animation from route data."""
from __future__ import print_function, division
import glob
import numpy as np
import os
import sys
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation

from route_plotter import parse_route, stitch_tiles, coords_to_bbox

def main():
  args = parse_args()
  routes = _parse_routes(args.route)
  if not routes:
    print('No valid route data provided.', file=sys.stderr)
    return
  print('Parsed', len(routes), 'valid routes.')

  routes = _filter_routes(routes, loop_gap_threshold=args.max_loop_gap,
                          start_cluster_radius=args.max_start_dist)
  assert bool(routes), '_filter_routes removed all routes'
  print('Filtered to', len(routes), 'routes.')

  if not os.path.exists(args.tile_cache):
    os.makedirs(args.tile_cache)
  bg_img, bg_extent = stitch_tiles(_bounding_box(routes), zoom=args.zoom,
                                   cachedir=args.tile_cache,
                                   flatten=(not args.color_tiles),
                                   margin_scale=args.max_margin)
  print('Assembled %d x %d background map image.' % bg_img.shape[:2])

  fig, ax = _setup_figure(bg_img, bg_extent, scale=args.scale)
  max_duration = max(s[-1] for _,s in routes)
  frame_times = np.linspace(0, max_duration, args.num_frames)
  anim = _animate(fig, ax, routes, frame_times, lc=args.line_color,
                  lw=args.line_width, hc=args.head_color, hs=args.head_size,
                  line_alpha=args.line_alpha, head_alpha=args.head_alpha)
  print('Prepared animation loop.')

  if args.save:
    dpi = plt.rcParams.get('figure.dpi', 100.0)
    anim.save(args.save, dpi=dpi, savefig_kwargs=dict(pad_inches=0))
  else:
    plt.show()

def _animate(fig, ax, routes, frame_data, lc='r', lw=2, line_alpha=0.75,
             hc='b', hs=20, head_alpha=1):
  n = len(routes)
  lines = ax.plot(np.zeros((0, n)), c=lc, lw=lw, alpha=line_alpha, zorder=1)
  heads = ax.scatter([], [], c=hc, s=hs, alpha=head_alpha, edgecolors='none',
                     zorder=2)
  timer = ax.text(1, 0, '0:00:00', transform=ax.transAxes,
                  bbox=dict(facecolor='white'),
                  verticalalignment='bottom', horizontalalignment='right')
  no_head = np.full(2, np.nan)
  end_pts = np.zeros((n, 2))
  plot_data = []
  for i, line in enumerate(lines):
    r, s = routes[i]
    plot_data.append((np.fliplr(r), s, line))
  artists = lines + [heads, timer]

  def update_frame(num_seconds):
    for i, (xy, time, line) in enumerate(plot_data):
      idx = np.searchsorted(time, num_seconds)
      if idx < xy.shape[0]:
        line.set_data(xy[:idx].T)
        end_pts[i] = xy[idx]
      elif idx == xy.shape[0]:
        line.set_data(xy[:idx].T)
        end_pts[i] = no_head
      else:
        end_pts[i] = no_head
    heads.set_offsets(end_pts)
    # update the clock
    mins, secs = divmod(num_seconds, 60)
    hours, mins = divmod(mins, 60)
    timer.set_text('%d:%02d:%02d' % (hours, mins, secs))
    return artists

  return FuncAnimation(fig, update_frame, frames=frame_data, blit=True,
                       interval=100, repeat=True, repeat_delay=150)

def parse_args():
  cache_dir = os.path.join(os.path.dirname(__file__), 'tiles')
  ap = ArgumentParser(description=__doc__,
                      formatter_class=ArgumentDefaultsHelpFormatter)
  ap.add_argument('--color-tiles', action='store_true',
                  help='Use full-color map tiles.')
  ap.add_argument('--scale', type=float, default=1,
                  help='Figure size scale fraction.')
  ap.add_argument('--zoom', type=int, default=None, help='Map zoom level.')
  ap.add_argument('--tile-cache', type=str, default=cache_dir,
                  help='Directory for storing cached map tiles.')
  ap.add_argument('--save', type=str, help='Save animation to disk.')
  ap.add_argument('--max-margin', type=float, default=0.05,
                  help='Keep up to this fraction of width/height as margin.')
  ap.add_argument('--max-loop-gap', type=float, default=200,
                  help='Maximum gap between start/end points, in meters.')
  ap.add_argument('--max-start-dist', type=float, default=200,
                  help=('Maximum distance from route start to the '
                        'mean starting location, in meters.'))
  ap.add_argument('--num-frames', type=int, default=500,
                  help='Number of frames to animate.')
  ap.add_argument('--line-color', type=str, default='red', help='Line color.')
  ap.add_argument('--line-width', type=float, default=1.5, help='Line width.')
  ap.add_argument('--line-alpha', type=float, default=.75, help='Line opacity.')
  ap.add_argument('--head-color', type=str, default='blue', help='Head color.')
  ap.add_argument('--head-size', type=float, default=36, help='Head size.')
  ap.add_argument('--head-alpha', type=float, default=1, help='Head opacity.')
  ap.add_argument('route', type=str, nargs='+', help='Route file(s) to use.')
  args = ap.parse_args()
  if len(args.route) == 1:
    args.route = glob.glob(args.route[0])
  args.route = map(open, args.route)
  return args

def _setup_figure(bg_img, bg_extent, scale=1.0):
  plt.rc('figure', autolayout=False)  # turn off tight_layout
  dpi = plt.rcParams.get('figure.dpi', 100.0)
  fig = plt.figure(dpi=dpi, frameon=False)

  # scale the figure to fit the bg image
  bg_height, bg_width = bg_img.shape[:2]
  fig.set_size_inches(bg_width / dpi * scale, bg_height / dpi * scale)

  ax = fig.add_axes([0, 0, 1, 1])
  ax.set_axis_off()
  ax.xaxis.set_major_locator(plt.NullLocator())
  ax.yaxis.set_major_locator(plt.NullLocator())
  ax.imshow(bg_img, zorder=0, extent=bg_extent, cmap='Greys_r')
  ax.autoscale(False)
  ax.margins(0, 0)
  return fig, ax

def _parse_routes(file_paths):
  all_coords = []
  for fh in file_paths:
    coords, times = parse_route(fh, return_time=True)
    if coords.ndim == 2 and coords.shape[0] >= 2 and coords.shape[1] == 2:
      seconds = (times - times[0]).astype('timedelta64[s]').astype(int)
      assert coords.shape[0] == seconds.shape[0]
      all_coords.append((coords, seconds))
  return all_coords

def _filter_routes(routes, loop_gap_threshold=200, start_cluster_radius=200):
  """Selects routes that:
    - start and end in roughly the same place (forming a loop)
    - start close to the mean starting location
  """
  starts = np.array([r[0] for r,_ in routes])
  ends = np.array([r[-1] for r,_ in routes])

  # convert lat/lon displacements into approximate distances (meters)
  gap_dists = _greatcircle_distance(starts, ends)
  mask = gap_dists < loop_gap_threshold

  # iterative thresholding to narrow down the center location
  thresh = np.inf
  while thresh > start_cluster_radius:
    start_dist = _greatcircle_distance(starts, starts[mask].mean(axis=0))
    thresh = np.percentile(start_dist[mask], 95)
    mask &= start_dist < thresh
  return [routes[i] for i in np.where(mask)[0]]

def _bounding_box(routes):
  min_coord = np.array([r.min(axis=0) for r,_ in routes]).min(axis=0)
  max_coord = np.array([r.max(axis=0) for r,_ in routes]).max(axis=0)
  return coords_to_bbox([min_coord, max_coord])

def _greatcircle_distance(latlon0, latlon1):
  EARTH_RADIUS = 6371000  # approximate spherical radius
  lat0, lon0 = np.deg2rad(latlon0).T
  lat1, lon1 = np.deg2rad(latlon1).T
  return EARTH_RADIUS * np.arccos(
      np.sin(lat0) * np.sin(lat1) +
      np.cos(lat0) * np.cos(lat1) * np.cos(np.abs(lon0 - lon1)))

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


<commit_before>from routejoin import dijkstra, common

def route_network(G, nodes=[]):
  """
  create paths between all nodes of the network

  returns a list of paths
  e.g.:
  [[18462, 19256, 19052, 19075, 18600, 18631, 18818, 18881, 18872],
   [18872, 18881, 18818, 18631, 18852, 18860]]
  """
  nodes_len = len(nodes)
  routes = []

  # be sure all nodes exists
  for node in nodes:
    if not G.has_key(node):
      raise common.MissingNodeError(node)

  if nodes_len < 2:
    raise common.NotEnoughNodesError()
  else:
    last_node = nodes[0]
    for next_node in nodes[1:]:
      route = dijkstra.shortest_path(G, last_node, next_node)

      # reduce the routing costs for the nodes of
      # this route in the graph, to get the following
      # routes to prefer the already included nodes
      for node in route:
        for neighbor in G[node].iterkeys():
          # reduce the cost by 50%
          G[neighbor][node] = int(G[neighbor][node]/2)
      last_node = next_node

      routes.append(route)
  return routes

<commit_msg>reducing routing consts in both directions between nodes<commit_after>from routejoin import dijkstra, common

def route_network(G, nodes=[]):
  """
  create paths between all nodes of the network

  returns a list of paths
  e.g.:
  [[18462, 19256, 19052, 19075, 18600, 18631, 18818, 18881, 18872],
   [18872, 18881, 18818, 18631, 18852, 18860]]
  """
  nodes_len = len(nodes)
  routes = []

  # be sure all nodes exists
  for node in nodes:
    if not G.has_key(node):
      raise common.MissingNodeError(node)

  if nodes_len < 2:
    raise common.NotEnoughNodesError()
  else:
    last_node = nodes[0]
    for next_node in nodes[1:]:
      route = dijkstra.shortest_path(G, last_node, next_node)

      # reduce the routing costs for the nodes of
      # this route in the graph, to get the following
      # routes to prefer the already included nodes
      for node in route:
        for neighbor in G[node].iterkeys():
          # reduce the cost by 50%
          G[neighbor][node] = int(G[neighbor][node]/2)
          G[node][neighbor] = int(G[node][neighbor]/2)
      last_node = next_node

      routes.append(route)
  return routes

<|endoftext|>