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

Example:

  from google.colab.data_table import DataTable
  from vega_datasets import data
  airports = data.airports()  # DataFrame
  DataTable(airports)  # Displays as interactive table
"""
from __future__ import absolute_import as _
from __future__ import division as _
from __future__ import print_function as _

import json as _json
import traceback as _traceback
import IPython as _IPython
from IPython.utils import traitlets as _traitlets
import pandas as _pd
import six as _six

from google.colab import _interactive_table_helper

__all__ = [
    'DataTable', 'enable_dataframe_formatter', 'disable_dataframe_formatter',
    'load_ipython_extension', 'unload_ipython_extension'
]

_GVIZ_JS = 'https://ssl.gstatic.com/colaboratory/data_table/3f5755b8f541b959/data_table.js'

_DATA_TABLE_HELP_URL = 'https://colab.research.google.com/notebooks/data_table.ipynb'

_JAVASCRIPT_MODULE_MIME_TYPE = 'application/vnd.google.colaboratory.module+javascript'

#  pylint:disable=g-import-not-at-top
#  pylint:disable=g-importing-member
if _six.PY2:
  from cgi import escape as _escape
else:
  from html import escape as _escape
#  pylint:enable=g-importing-member
#  pylint:enable=g-import-not-at-top

def _force_to_latin1(x):
  return 'nonunicode data: %s...' % _escape(x[:100].decode('latin1'))

_DEFAULT_NONUNICODE_FORMATTER = _force_to_latin1
if _six.PY2:
  _DEFAULT_FORMATTERS = {unicode: lambda x: x.encode('utf8')}
else:
  _DEFAULT_FORMATTERS = {str: lambda x: x}

class DataTable(_IPython.display.DisplayObject):
  """An interactive data table display.

  Attributes:
    include_index: (boolean) whether to include the index in a table by default.
    num_rows_per_page: (int) default number of rows per page.
    max_rows: (int) number of rows beyond which the table will be truncated.
    max_columns: (int) number of columns beyond which the table will be
      truncated.
  """
  # Configurable defaults for initialization.
  include_index = True
  num_rows_per_page = 25
  max_rows = 20000
  max_columns = 20

  @classmethod
  def formatter(cls, dataframe, **kwargs):
    # Don't use data table for hierarchical index or columns.
    if isinstance(dataframe.columns, _pd.MultiIndex):
      return None
    if isinstance(dataframe.index, _pd.MultiIndex):
      return None
    # For large dataframes, fall back to pandas rather than truncating.
    if dataframe.shape[0] > cls.max_rows:
      return None
    if dataframe.shape[1] > cls.max_columns:
      return None
    return cls(dataframe, **kwargs)._repr_javascript_module_()  # pylint: disable=protected-access

  def __init__(self,
               dataframe,
               include_index=None,
               num_rows_per_page=None,
               max_rows=None,
               max_columns=None):
    """Constructor.

    Args:
       dataframe: the dataframe source for the table
       include_index: boolean specifying whether index should be included.
         Defaults to DataTable.include_index
       num_rows_per_page: display that many rows per page initially. Defaults to
         DataTable.num_rows_per_page.
       max_rows: if len(data) exceeds this value a warning will be printed and
         the table truncated. Defaults to DataTable.max_rows.
       max_columns: if len(columns) exceeds this value a warning will be printed
         and truncated. Defaults to DataTable.max_columns.
    """

    def _default(value, default):
      return default if value is None else value

    self._dataframe = dataframe
    self._include_index = _default(include_index, self.include_index)
    self._num_rows_per_page = _default(num_rows_per_page,
                                       self.num_rows_per_page)
    self._max_rows = _default(max_rows, self.max_rows)
    self._max_columns = _default(max_columns, self.max_columns)

  def _preprocess_dataframe(self):
    dataframe = self._dataframe.iloc[:self._max_rows, :self._max_columns]

    if self._include_index or dataframe.shape[1] == 0:
      dataframe = dataframe.reset_index()
    if not dataframe.columns.is_unique:
      df_copy = dataframe.copy(deep=False)
      df_copy.columns = range(dataframe.shape[1])
      records = df_copy.to_records(index=False)
      dataframe = records[[str(n) for n in list(records.dtype.names)]]
    return dataframe

  def _repr_mimebundle_(self, include=None, exclude=None):
    mime_bundle = {'text/html': self._repr_html_()}
    try:
      dataframe = self._preprocess_dataframe()
      mime_bundle[_JAVASCRIPT_MODULE_MIME_TYPE] = self._gen_js(dataframe)
    except:  # pylint: disable=bare-except
      # need to catch and print exception since it is user visible
      _traceback.print_exc()
    return mime_bundle

  def _repr_html_(self):
    return self._dataframe._repr_html_()  # pylint: disable=protected-access

  def _repr_javascript_module_(self):
    try:
      return self._gen_js(self._preprocess_dataframe())
    except:  # pylint: disable=bare-except
      # need to catch and print exception since it is user visible
      _traceback.print_exc()

  def _gen_js(self, dataframe):
    """Returns javascript for this table."""
    columns = dataframe.columns
    data = dataframe.values

    data_formatters = {}
    header_formatters = {}
    default_formatter = _interactive_table_helper._find_formatter(  # pylint: disable=protected-access
        _DEFAULT_FORMATTERS)

    for i, _ in enumerate(columns):
      data_formatters[i] = default_formatter
      header_formatters[i] = default_formatter

    formatted_data = _interactive_table_helper._format_data(  # pylint: disable=protected-access
        data, _DEFAULT_NONUNICODE_FORMATTER, data_formatters)
    column_types = formatted_data['column_types']

    columns_and_types = []
    for i, (column_type, column) in enumerate(zip(column_types, columns)):
      columns_and_types.append((column_type, str(header_formatters[i](column))))

    column_options = []
    if self._include_index:
      # Collapse index columns to minimum necessary width. We specify 1px but
      # they will auto-expand as necessary.
      column_options = [{
          'width': '1px',
          'className': 'index_column'
      }] * self._dataframe.index.nlevels

    return """
      import "{gviz_url}";

      window.createDataTable({{
        data: {data},
        columns: {columns},
        columnOptions: {column_options},
        rowsPerPage: {num_rows_per_page},
        helpUrl: "{help_url}",
      }});
    """.format(
        gviz_url=_GVIZ_JS,
        data=formatted_data['data'],
        columns=_json.dumps(columns_and_types),
        column_options=_json.dumps(column_options),
        num_rows_per_page=self._num_rows_per_page,
        help_url=_DATA_TABLE_HELP_URL,
    )

class _JavascriptModuleFormatter(_IPython.core.formatters.BaseFormatter):
  format_type = _traitlets.Unicode(_JAVASCRIPT_MODULE_MIME_TYPE)
  print_method = _traitlets.ObjectName('_repr_javascript_module_')

def _register_jsmodule_mimetype():
  """Register _repr_javascript_module_ with the IPython display mechanism."""
  display_formatter = _IPython.get_ipython().display_formatter
  display_formatter.formatters.setdefault(
      _JAVASCRIPT_MODULE_MIME_TYPE,
      _JavascriptModuleFormatter(parent=display_formatter))

_original_formatters = {}

def enable_dataframe_formatter():
  """Enables DataTable as the default IPython formatter for Pandas DataFrames."""
  key = _JAVASCRIPT_MODULE_MIME_TYPE
  if key not in _original_formatters:
    _register_jsmodule_mimetype()
    formatters = _IPython.get_ipython().display_formatter.formatters
    _original_formatters[key] = formatters[key].for_type_by_name(
        'pandas.core.frame', 'DataFrame', DataTable.formatter)

def disable_dataframe_formatter():
  """Restores the original IPython formatter for Pandas DataFrames."""
  key = _JAVASCRIPT_MODULE_MIME_TYPE
  if key in _original_formatters:
    formatters = _IPython.get_ipython().display_formatter.formatters
    # pop() handles the case of original_formatter = None.
    formatters[key].pop('pandas.core.frame.DataFrame')
    formatters[key].for_type_by_name('pandas.core.frame', 'DataFrame',
                                     _original_formatters.pop(key))

def load_ipython_extension(ipython):  # pylint: disable=unused-argument
  """Enable DataTable output for all Pandas dataframes."""
  enable_dataframe_formatter()

def unload_ipython_extension(ipython):  # pylint: disable=unused-argument
  """Disable DataTable output for all Pandas dataframes."""
  disable_dataframe_formatter()
<commit_msg>Internal update.<commit_after># Copyright 2019 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Interactive table for displaying pandas dataframes.

Example:

  from google.colab.data_table import DataTable
  from vega_datasets import data
  airports = data.airports()  # DataFrame
  DataTable(airports)  # Displays as interactive table
"""
from __future__ import absolute_import as _
from __future__ import division as _
from __future__ import print_function as _

import json as _json
import traceback as _traceback
import IPython as _IPython
from IPython.utils import traitlets as _traitlets
import pandas as _pd
import six as _six

from google.colab import _interactive_table_helper

__all__ = [
    'DataTable', 'enable_dataframe_formatter', 'disable_dataframe_formatter',
    'load_ipython_extension', 'unload_ipython_extension'
]

_GVIZ_JS = 'https://ssl.gstatic.com/colaboratory/data_table/9e554b27bdd509f3/data_table.js'

_DATA_TABLE_HELP_URL = 'https://colab.research.google.com/notebooks/data_table.ipynb'

_JAVASCRIPT_MODULE_MIME_TYPE = 'application/vnd.google.colaboratory.module+javascript'

#  pylint:disable=g-import-not-at-top
#  pylint:disable=g-importing-member
if _six.PY2:
  from cgi import escape as _escape
else:
  from html import escape as _escape
#  pylint:enable=g-importing-member
#  pylint:enable=g-import-not-at-top

def _force_to_latin1(x):
  return 'nonunicode data: %s...' % _escape(x[:100].decode('latin1'))

_DEFAULT_NONUNICODE_FORMATTER = _force_to_latin1
if _six.PY2:
  _DEFAULT_FORMATTERS = {unicode: lambda x: x.encode('utf8')}
else:
  _DEFAULT_FORMATTERS = {str: lambda x: x}

class DataTable(_IPython.display.DisplayObject):
  """An interactive data table display.

  Attributes:
    include_index: (boolean) whether to include the index in a table by default.
    num_rows_per_page: (int) default number of rows per page.
    max_rows: (int) number of rows beyond which the table will be truncated.
    max_columns: (int) number of columns beyond which the table will be
      truncated.
  """
  # Configurable defaults for initialization.
  include_index = True
  num_rows_per_page = 25
  max_rows = 20000
  max_columns = 20

  @classmethod
  def formatter(cls, dataframe, **kwargs):
    # Don't use data table for hierarchical index or columns.
    if isinstance(dataframe.columns, _pd.MultiIndex):
      return None
    if isinstance(dataframe.index, _pd.MultiIndex):
      return None
    # For large dataframes, fall back to pandas rather than truncating.
    if dataframe.shape[0] > cls.max_rows:
      return None
    if dataframe.shape[1] > cls.max_columns:
      return None
    return cls(dataframe, **kwargs)._repr_javascript_module_()  # pylint: disable=protected-access

  def __init__(self,
               dataframe,
               include_index=None,
               num_rows_per_page=None,
               max_rows=None,
               max_columns=None):
    """Constructor.

    Args:
       dataframe: the dataframe source for the table
       include_index: boolean specifying whether index should be included.
         Defaults to DataTable.include_index
       num_rows_per_page: display that many rows per page initially. Defaults to
         DataTable.num_rows_per_page.
       max_rows: if len(data) exceeds this value a warning will be printed and
         the table truncated. Defaults to DataTable.max_rows.
       max_columns: if len(columns) exceeds this value a warning will be printed
         and truncated. Defaults to DataTable.max_columns.
    """

    def _default(value, default):
      return default if value is None else value

    self._dataframe = dataframe
    self._include_index = _default(include_index, self.include_index)
    self._num_rows_per_page = _default(num_rows_per_page,
                                       self.num_rows_per_page)
    self._max_rows = _default(max_rows, self.max_rows)
    self._max_columns = _default(max_columns, self.max_columns)

  def _preprocess_dataframe(self):
    dataframe = self._dataframe.iloc[:self._max_rows, :self._max_columns]

    if self._include_index or dataframe.shape[1] == 0:
      dataframe = dataframe.reset_index()
    if not dataframe.columns.is_unique:
      df_copy = dataframe.copy(deep=False)
      df_copy.columns = range(dataframe.shape[1])
      records = df_copy.to_records(index=False)
      dataframe = records[[str(n) for n in list(records.dtype.names)]]
    return dataframe

  def _repr_mimebundle_(self, include=None, exclude=None):
    mime_bundle = {'text/html': self._repr_html_()}
    try:
      dataframe = self._preprocess_dataframe()
      mime_bundle[_JAVASCRIPT_MODULE_MIME_TYPE] = self._gen_js(dataframe)
    except:  # pylint: disable=bare-except
      # need to catch and print exception since it is user visible
      _traceback.print_exc()
    return mime_bundle

  def _repr_html_(self):
    return self._dataframe._repr_html_()  # pylint: disable=protected-access

  def _repr_javascript_module_(self):
    try:
      return self._gen_js(self._preprocess_dataframe())
    except:  # pylint: disable=bare-except
      # need to catch and print exception since it is user visible
      _traceback.print_exc()

  def _gen_js(self, dataframe):
    """Returns javascript for this table."""
    columns = dataframe.columns
    data = dataframe.values

    data_formatters = {}
    header_formatters = {}
    default_formatter = _interactive_table_helper._find_formatter(  # pylint: disable=protected-access
        _DEFAULT_FORMATTERS)

    for i, _ in enumerate(columns):
      data_formatters[i] = default_formatter
      header_formatters[i] = default_formatter

    formatted_data = _interactive_table_helper._format_data(  # pylint: disable=protected-access
        data, _DEFAULT_NONUNICODE_FORMATTER, data_formatters)
    column_types = formatted_data['column_types']

    columns_and_types = []
    for i, (column_type, column) in enumerate(zip(column_types, columns)):
      columns_and_types.append((column_type, str(header_formatters[i](column))))

    column_options = []
    if self._include_index:
      # Collapse index columns to minimum necessary width. We specify 1px but
      # they will auto-expand as necessary.
      column_options = [{
          'width': '1px',
          'className': 'index_column'
      }] * self._dataframe.index.nlevels

    return """
      import "{gviz_url}";

      window.createDataTable({{
        data: {data},
        columns: {columns},
        columnOptions: {column_options},
        rowsPerPage: {num_rows_per_page},
        helpUrl: "{help_url}",
      }});
    """.format(
        gviz_url=_GVIZ_JS,
        data=formatted_data['data'],
        columns=_json.dumps(columns_and_types),
        column_options=_json.dumps(column_options),
        num_rows_per_page=self._num_rows_per_page,
        help_url=_DATA_TABLE_HELP_URL,
    )

class _JavascriptModuleFormatter(_IPython.core.formatters.BaseFormatter):
  format_type = _traitlets.Unicode(_JAVASCRIPT_MODULE_MIME_TYPE)
  print_method = _traitlets.ObjectName('_repr_javascript_module_')

def _register_jsmodule_mimetype():
  """Register _repr_javascript_module_ with the IPython display mechanism."""
  display_formatter = _IPython.get_ipython().display_formatter
  display_formatter.formatters.setdefault(
      _JAVASCRIPT_MODULE_MIME_TYPE,
      _JavascriptModuleFormatter(parent=display_formatter))

_original_formatters = {}

def enable_dataframe_formatter():
  """Enables DataTable as the default IPython formatter for Pandas DataFrames."""
  key = _JAVASCRIPT_MODULE_MIME_TYPE
  if key not in _original_formatters:
    _register_jsmodule_mimetype()
    formatters = _IPython.get_ipython().display_formatter.formatters
    _original_formatters[key] = formatters[key].for_type_by_name(
        'pandas.core.frame', 'DataFrame', DataTable.formatter)

def disable_dataframe_formatter():
  """Restores the original IPython formatter for Pandas DataFrames."""
  key = _JAVASCRIPT_MODULE_MIME_TYPE
  if key in _original_formatters:
    formatters = _IPython.get_ipython().display_formatter.formatters
    # pop() handles the case of original_formatter = None.
    formatters[key].pop('pandas.core.frame.DataFrame')
    formatters[key].for_type_by_name('pandas.core.frame', 'DataFrame',
                                     _original_formatters.pop(key))

def load_ipython_extension(ipython):  # pylint: disable=unused-argument
  """Enable DataTable output for all Pandas dataframes."""
  enable_dataframe_formatter()

def unload_ipython_extension(ipython):  # pylint: disable=unused-argument
  """Disable DataTable output for all Pandas dataframes."""
  disable_dataframe_formatter()
<|endoftext|>


<commit_before>from RPi import GPIO

from .devices import GPIODeviceError, GPIODevice, GPIOThread

class OutputDeviceError(GPIODeviceError):
    pass

class OutputDevice(GPIODevice):
    def __init__(self, pin=None):
        super(OutputDevice, self).__init__(pin)
        GPIO.setup(pin, GPIO.OUT)

    def on(self):
        GPIO.output(self.pin, True)

    def off(self):
        GPIO.output(self.pin, False)

class LED(OutputDevice):
    def __init__(self, pin=None):
        super(LED, self).__init__(pin)
        self._blink_thread = None

    def blink(self, on_time=1, off_time=1):
        self._stop_blink()
        self._blink_thread = GPIOThread(target=self._blink_led, args=(on_time, off_time))
        self._blink_thread.start()

    def _stop_blink(self):
        if self._blink_thread:
            self._blink_thread.stop()
            self._blink_thread = None

    def _blink_led(self, on_time, off_time):
        while True:
            super(LED, self).on()
            if self._blink_thread.stopping.wait(on_time):
                break
            super(LED, self).off()
            if self._blink_thread.stopping.wait(off_time):
                break

    def on(self):
        self._stop_blink()
        super(LED, self).on()

    def off(self):
        self._stop_blink()
        super(LED, self).off()

class Buzzer(OutputDevice):
    pass

class Motor(OutputDevice):
    pass
<commit_msg>Initial commit of basic Robot class<commit_after>from RPi import GPIO
from time import sleep

from .devices import GPIODeviceError, GPIODevice, GPIOThread

class OutputDeviceError(GPIODeviceError):
    pass

class OutputDevice(GPIODevice):
    def __init__(self, pin=None):
        super(OutputDevice, self).__init__(pin)
        GPIO.setup(pin, GPIO.OUT)

    def on(self):
        GPIO.output(self.pin, True)

    def off(self):
        GPIO.output(self.pin, False)

class LED(OutputDevice):
    def __init__(self, pin=None):
        super(LED, self).__init__(pin)
        self._blink_thread = None

    def blink(self, on_time=1, off_time=1):
        self._stop_blink()
        self._blink_thread = GPIOThread(target=self._blink_led, args=(on_time, off_time))
        self._blink_thread.start()

    def _stop_blink(self):
        if self._blink_thread:
            self._blink_thread.stop()
            self._blink_thread = None

    def _blink_led(self, on_time, off_time):
        while True:
            super(LED, self).on()
            if self._blink_thread.stopping.wait(on_time):
                break
            super(LED, self).off()
            if self._blink_thread.stopping.wait(off_time):
                break

    def on(self):
        self._stop_blink()
        super(LED, self).on()

    def off(self):
        self._stop_blink()
        super(LED, self).off()

class Buzzer(OutputDevice):
    pass

class Motor(OutputDevice):
    pass

class Robot(object):
    def __init__(self, left=None, right=None):
        if not all([left, right]):
            raise GPIODeviceError(
                'left and right pins must be provided'
            )

        self._left = Motor(left)
        self._right = Motor(right)

    def left(self, seconds=None):
        self._left.on()
        if seconds is not None:
            sleep(seconds)
            self._left.off()

    def right(self, seconds=None):
        self._right.on()
        if seconds is not None:
            sleep(seconds)
            self._right.off()

    def forwards(self, seconds=None):
        self.left()
        self.right()
        if seconds is not None:
            sleep(seconds)
            self.stop()

    def stop(self):
        self._left.off()
        self._right.off()
<|endoftext|>


<commit_before>"Implements ArrayConstraint"
from .set import ConstraintSet
from .single_equation import SingleEquationConstraint

class ArrayConstraint(ConstraintSet, SingleEquationConstraint):
    "A ConstraintSet for prettier array-constraint printing"
    def __init__(self, constraints, left, oper, right):
        super(ArrayConstraint, self).__init__(constraints)
        self.left = left
        self.oper = oper
        self.right = right
<commit_msg>add docstring<commit_after>"Implements ArrayConstraint"
from .set import ConstraintSet
from .single_equation import SingleEquationConstraint

class ArrayConstraint(ConstraintSet, SingleEquationConstraint):
    """A ConstraintSet for prettier array-constraint printing.

    Because ArrayConstraint also inherits from ConstraintSet,
    its substitutions is recursive and so left and right are
    only used for string and latex printing. In practice they're
    likely to be either NomialArrays or Varkeys of VectorVariables."""
    def __init__(self, constraints, left, oper, right):
        super(ArrayConstraint, self).__init__(constraints)
        self.left = left
        self.oper = oper
        self.right = right
<|endoftext|>


<commit_before>"""Implement the SignomialProgram class"""
import numpy as np

from time import time
from functools import reduce as functools_reduce
from operator import mul

from .nomials import Posynomial
from .substitution import getsubs
from .geometric_program import GeometricProgram

class SignomialProgram(object):
    """Prepares a collection of signomials for a SP solve.

    Arguments
    ---------
    cost : Constraint
        Signomial to minimize when solving
    constraints : list of Signomials
        Constraints to maintain when solving (implicitly Signomials <= 1)

    Attributes with side effects
    ----------------------------
    `gps` is set during a solve
    `result` is set at the end of a solve

    Examples
    --------
    >>> gp = gpkit.geometric_program.SignomialProgram(
                        # minimize
                        x,
                        [   # subject to
                            1/x - y/x,  # <= 1, implicitly
                            y/10  # <= 1
                        ])
    >>> gp.solve()
    """

    def __init__(self, cost, constraints):
        if any(cost.cs <= 0):
            raise ValueError("""SignomialPrograms need Posyomial objectives.

    The equivalent of a Signomial objective can be constructed by constraining
    a dummy variable z to be greater than the desired Signomial objective s
    (z >= s) and then minimizing that dummy variable."""")

        self.cost = cost
        self.constraints = constraints
        self.signomials = [cost] + list(constraints)

        self.posynomials, self.negynomials = [self.cost], [None]
        self.negvarkeys = set()
        for sig in self.constraints:
            p_exps, p_cs = [], []
            n_exps, n_cs = [{}], [1]  # add the 1 from the "<= 1" constraint
            for c, exp in zip(sig.cs, sig.exps):
                if c > 0:
                    p_cs.append(c)
                    p_exps.append(exp)
                elif c < 0:
                    n_cs.append(-c)
                    n_exps.append(exp)
                    self.negvarkeys.update(exp.keys())
            posy = Posynomial(p_exps, p_cs) if p_cs != [] else None
            negy = Posynomial(n_exps, n_cs) if n_cs != [1] else None
            self.posynomials.append(posy)
            self.negynomials.append(negy)

        if not self.negvarkeys:
            raise ValueError("SignomialPrograms must contain at least one"
                             " Signomial.")

    def localsolve(self, solver=None, verbosity=1, x0=None, reltol=1e-4,
                   iteration_limit=50, *args, **kwargs):
        """Locally solves a SignomialProgram and returns the solution.

        Arguments
        ---------
        solver : str or function (optional)
            By default uses one of the solvers found during installation.
            If set to "mosek", "mosek_cli", or "cvxopt", uses that solver.
            If set to a function, passes that function cs, A, p_idxs, and k.
        verbosity : int (optional)
            If greater than 0, prints solve time and number of iterations.
            Each GP is created and solved with verbosity one less than this, so
            if greater than 1, prints solver name and time for each GP.
        x0 : dict (optional)
            Initial location to approximate signomials about.
        reltol : float
            Iteration ends when this is greater than the distance between two
            consecutive solve's objective values.
        iteration_limit : int
            Maximum GP iterations allowed.
        *args, **kwargs :
            Passed to solver function.

        Returns
        -------
        result : dict
            A dictionary containing the translated solver result.
        """
        if verbosity > 0:
            print("Beginning signomial solve.")
            self.starttime = time()

        if x0 is None:
            x0 = {}
        else:
            x0 = getsubs({str(vk): vk for vk in self.negvarkeys},
                         self.negvarkeys, x0)
        sp_inits = {vk: vk.descr["sp_init"] for vk in self.negvarkeys
                    if "sp_init" in vk.descr}
        sp_inits.update(x0)
        x0 = sp_inits
        # HACK: initial guess for negative variables
        x0.update({var: 1 for var in self.negvarkeys if var not in x0})

        iterations = 0
        prevcost, cost = None, None
        self.gps = []

        while (iterations < iteration_limit
               and (not (cost and prevcost)
                    or abs(prevcost-cost)/(prevcost + cost) > reltol)
               and (cost > 1e-10 or cost == None)):

            gp = self.step(x0, verbosity)
            self.gps.append(gp)  # NOTE: SIDE EFFECTS

            try:
                result = gp.solve(solver, verbosity=verbosity-1,
                                  *args, **kwargs)
            except (RuntimeWarning, ValueError):
                # TODO: should we add the nearest_feasible gp to the program?
                # TODO: should we count it as an iteration?
                nearest_feasible = gp.feasibility_search(verbosity=verbosity-1)
                result = nearest_feasible.solve(verbosity=verbosity-1)
                result["cost"] = None

            x0 = result["variables"]
            prevcost, cost = cost, result["cost"]
            iterations += 1

        if verbosity > 0:
            print("Solving took %i GP solves" % iterations
                  + " and %.3g seconds." % (time() - self.starttime))

        # parse the result and return nu's of original monomials from
        #  variable sensitivities
        nu = result["sensitivities"]["monomials"]
        sens_vars = {var: sum([gp.exps[i][var]*nu[i] for i in locs])
                     for (var, locs) in gp.varlocs.items()}
        nu_ = []
        for signomial in self.signomials:
            for c, exp in zip(signomial.cs, signomial.exps):
                var_ss = [sens_vars[var]*val for var, val in exp.items()]
                nu_.append(functools_reduce(mul, var_ss, np.sign(c)))
        result["sensitivities"]["monomials"] = np.array(nu_)
        # TODO: SP sensitivities are weird, and potentially incorrect

        self.result = result  # NOTE: SIDE EFFECTS
        return result

    def step(self, x0=None, verbosity=1):
        if x0 is None:
            # HACK: initial guess for negative variables
            x0 = {var: 1 for var in self.negvarkeys}
            sp_inits = {vk: vk.descr["sp_init"] for vk in self.negvarkeys
                        if "sp_init" in vk.descr}
            x0,update(sp_inits)
        posy_approxs = []
        for p, n in zip(self.posynomials, self.negynomials):
            if n is None:
                posy_approx = p
            else:
                posy_approx = p/n.mono_approximation(x0)
            posy_approxs.append(posy_approx)

        gp = GeometricProgram(posy_approxs[0], posy_approxs[1:],
                              verbosity=verbosity-1)
        return gp

    def __repr__(self):
        return "gpkit.%s(\n%s)" % (self.__class__.__name__, str(self))

    def __str__(self):
        """String representation of a SignomialProgram.

        Contains all of its parameters."""
        return "\n".join(["  # minimize",
                          "    %s," % self.cost,
                          "[ # subject to"] +
                         ["    %s <= 1," % constr
                          for constr in self.constraints] +
                         [']'])

    def _latex(self, unused=None):
        """LaTeX representation of a SignomialProgram.

        Contains all of its parameters."""
        return "\n".join(["\\begin{array}[ll]",
                          "\\text{}",
                          "\\text{minimize}",
                          "    & %s \\\\" % self.cost._latex(),
                          "\\text{subject to}"] +
                         ["    & %s \\leq 1\\\\" % constr._latex()
                          for constr in self.constraints] +
                         ["\\end{array}"])
<commit_msg>remove extra quotation mark<commit_after>"""Implement the SignomialProgram class"""
import numpy as np

from time import time
from functools import reduce as functools_reduce
from operator import mul

from .nomials import Posynomial
from .substitution import getsubs
from .geometric_program import GeometricProgram

class SignomialProgram(object):
    """Prepares a collection of signomials for a SP solve.

    Arguments
    ---------
    cost : Constraint
        Signomial to minimize when solving
    constraints : list of Signomials
        Constraints to maintain when solving (implicitly Signomials <= 1)

    Attributes with side effects
    ----------------------------
    `gps` is set during a solve
    `result` is set at the end of a solve

    Examples
    --------
    >>> gp = gpkit.geometric_program.SignomialProgram(
                        # minimize
                        x,
                        [   # subject to
                            1/x - y/x,  # <= 1, implicitly
                            y/10  # <= 1
                        ])
    >>> gp.solve()
    """

    def __init__(self, cost, constraints):
        if any(cost.cs <= 0):
            raise ValueError("""SignomialPrograms need Posyomial objectives.

    The equivalent of a Signomial objective can be constructed by constraining
    a dummy variable z to be greater than the desired Signomial objective s
    (z >= s) and then minimizing that dummy variable.""")

        self.cost = cost
        self.constraints = constraints
        self.signomials = [cost] + list(constraints)

        self.posynomials, self.negynomials = [self.cost], [None]
        self.negvarkeys = set()
        for sig in self.constraints:
            p_exps, p_cs = [], []
            n_exps, n_cs = [{}], [1]  # add the 1 from the "<= 1" constraint
            for c, exp in zip(sig.cs, sig.exps):
                if c > 0:
                    p_cs.append(c)
                    p_exps.append(exp)
                elif c < 0:
                    n_cs.append(-c)
                    n_exps.append(exp)
                    self.negvarkeys.update(exp.keys())
            posy = Posynomial(p_exps, p_cs) if p_cs != [] else None
            negy = Posynomial(n_exps, n_cs) if n_cs != [1] else None
            self.posynomials.append(posy)
            self.negynomials.append(negy)

        if not self.negvarkeys:
            raise ValueError("SignomialPrograms must contain at least one"
                             " Signomial.")

    def localsolve(self, solver=None, verbosity=1, x0=None, reltol=1e-4,
                   iteration_limit=50, *args, **kwargs):
        """Locally solves a SignomialProgram and returns the solution.

        Arguments
        ---------
        solver : str or function (optional)
            By default uses one of the solvers found during installation.
            If set to "mosek", "mosek_cli", or "cvxopt", uses that solver.
            If set to a function, passes that function cs, A, p_idxs, and k.
        verbosity : int (optional)
            If greater than 0, prints solve time and number of iterations.
            Each GP is created and solved with verbosity one less than this, so
            if greater than 1, prints solver name and time for each GP.
        x0 : dict (optional)
            Initial location to approximate signomials about.
        reltol : float
            Iteration ends when this is greater than the distance between two
            consecutive solve's objective values.
        iteration_limit : int
            Maximum GP iterations allowed.
        *args, **kwargs :
            Passed to solver function.

        Returns
        -------
        result : dict
            A dictionary containing the translated solver result.
        """
        if verbosity > 0:
            print("Beginning signomial solve.")
            self.starttime = time()

        if x0 is None:
            x0 = {}
        else:
            x0 = getsubs({str(vk): vk for vk in self.negvarkeys},
                         self.negvarkeys, x0)
        sp_inits = {vk: vk.descr["sp_init"] for vk in self.negvarkeys
                    if "sp_init" in vk.descr}
        sp_inits.update(x0)
        x0 = sp_inits
        # HACK: initial guess for negative variables
        x0.update({var: 1 for var in self.negvarkeys if var not in x0})

        iterations = 0
        prevcost, cost = None, None
        self.gps = []

        while (iterations < iteration_limit
               and (not (cost and prevcost)
                    or abs(prevcost-cost)/(prevcost + cost) > reltol)
               and (cost > 1e-10 or cost == None)):

            gp = self.step(x0, verbosity)
            self.gps.append(gp)  # NOTE: SIDE EFFECTS

            try:
                result = gp.solve(solver, verbosity=verbosity-1,
                                  *args, **kwargs)
            except (RuntimeWarning, ValueError):
                # TODO: should we add the nearest_feasible gp to the program?
                # TODO: should we count it as an iteration?
                nearest_feasible = gp.feasibility_search(verbosity=verbosity-1)
                result = nearest_feasible.solve(verbosity=verbosity-1)
                result["cost"] = None

            x0 = result["variables"]
            prevcost, cost = cost, result["cost"]
            iterations += 1

        if verbosity > 0:
            print("Solving took %i GP solves" % iterations
                  + " and %.3g seconds." % (time() - self.starttime))

        # parse the result and return nu's of original monomials from
        #  variable sensitivities
        nu = result["sensitivities"]["monomials"]
        sens_vars = {var: sum([gp.exps[i][var]*nu[i] for i in locs])
                     for (var, locs) in gp.varlocs.items()}
        nu_ = []
        for signomial in self.signomials:
            for c, exp in zip(signomial.cs, signomial.exps):
                var_ss = [sens_vars[var]*val for var, val in exp.items()]
                nu_.append(functools_reduce(mul, var_ss, np.sign(c)))
        result["sensitivities"]["monomials"] = np.array(nu_)
        # TODO: SP sensitivities are weird, and potentially incorrect

        self.result = result  # NOTE: SIDE EFFECTS
        return result

    def step(self, x0=None, verbosity=1):
        if x0 is None:
            # HACK: initial guess for negative variables
            x0 = {var: 1 for var in self.negvarkeys}
            sp_inits = {vk: vk.descr["sp_init"] for vk in self.negvarkeys
                        if "sp_init" in vk.descr}
            x0,update(sp_inits)
        posy_approxs = []
        for p, n in zip(self.posynomials, self.negynomials):
            if n is None:
                posy_approx = p
            else:
                posy_approx = p/n.mono_approximation(x0)
            posy_approxs.append(posy_approx)

        gp = GeometricProgram(posy_approxs[0], posy_approxs[1:],
                              verbosity=verbosity-1)
        return gp

    def __repr__(self):
        return "gpkit.%s(\n%s)" % (self.__class__.__name__, str(self))

    def __str__(self):
        """String representation of a SignomialProgram.

        Contains all of its parameters."""
        return "\n".join(["  # minimize",
                          "    %s," % self.cost,
                          "[ # subject to"] +
                         ["    %s <= 1," % constr
                          for constr in self.constraints] +
                         [']'])

    def _latex(self, unused=None):
        """LaTeX representation of a SignomialProgram.

        Contains all of its parameters."""
        return "\n".join(["\\begin{array}[ll]",
                          "\\text{}",
                          "\\text{minimize}",
                          "    & %s \\\\" % self.cost._latex(),
                          "\\text{subject to}"] +
                         ["    & %s \\leq 1\\\\" % constr._latex()
                          for constr in self.constraints] +
                         ["\\end{array}"])
<|endoftext|>


<commit_before># This file is meant to be used from the GQIS console
# It is highly specific to the DDPSC project.
from __future__ import print_function
from fractions import Fraction
import os
points = []
coordinates = []
raspberries = []
width = 6
height = 30
# offset = 10
offset = 11

# Initial GPS point that existed from standing in Greenhouse 9C and checking my location.
initial_lat = -10062973.096
initial_long = 4675266.934

# rPI_145 is 2.794 meters (x) and 0.5461 meters (y) from the initial lat/long point
rPI_145_lat = initial_lat + 2.794
rPI_145_long = initial_long + 0.5461
rPI_145 = QgsPoint(rPI_145_lat, rPI_145_long)
# points.append(rPI_145)
# We need to use the X, Y coordinates to convert latitude and longitude.
# init_x = (abs(145-(width*height)-offset) % width) + 1
# init_y = (abs(145-(width*height)-offset) / width) + 1
init_x = (abs(145-offset) % width) + 1
init_y = (abs(145-offset) / width) + 1

# Coordinates that the Raspberry PIs are separated by in an ideal grid.
# Still have to adjust for the first column that is off by a few.
# rPIs are separated by rPI_x in meters
rPI_x = 0.9779
# rPIs are separated by rPI_y in meters
rPI_y = 0.4572
# If they're in column 4 and 5 they have a different offset.
rPI_x_new = 0.9652
rPI_y_new = 0.1905

# Deriving initial rPI coordinates.
# We start at 10.9.0.11 or ShakoorCamera11 and proceed to generate points from there.
rPI_11_lat = rPI_145_lat - (rPI_x * 2)
rPI_11_long = rPI_145_long + (rPI_y * 22)
rPI_11 = QgsPoint(rPI_11_lat, rPI_11_long)
# Points are the QgsPoint coordinates in meters
# points.append(rPI_11)
# Coordinates are the floats that make up the QgsPoint coordinates in meters.
# coordinates.append([rPI_11_lat, rPI_11_long])
# Raspberries is the IP address of the Raspberry PI/inventory_hostname
# raspberries.append("10.9.0.11")
# We need to use the X, Y coordinates to convert latitude and longitude.
# init_x = (abs(11-(width*height)-offset) % width) + 1
# init_y = (abs(11-(width*height)-offset) / width) + 1

# Trackback variables that get updated at the end of each for loop
# updated to the just recently calculated Raspbery Pi
prev_x = init_x
prev_y = init_y
# prev_lat = rPI_11_lat
# prev_long = rPI_11_long
prev_lat = rPI_145_lat
prev_long = rPI_145_long
# Starting from ShakoorCamera12 to ShakoorCamera190
# Remember python ranges go to n-1
for ind in range(11, 191):
    # Formula to calculate x, y coordinates
    # formula = abs(ind-(width*height)-offset)
    formula = abs(ind - offset)
    y = (formula / width) + 1
    x = (formula % width) + 1
    # Adding shift for the first column
    shift_indices = []
    shift_indices = shift_indices + range(11, 191, 6) + range(12, 191, 6)
    rPI_x_mod = rPI_x
    rPI_y_mod = rPI_y
    if ind in shift_indices:
        rPI_x_mod = rPI_x_new
        rPI_y_mod = rPI_y_new
    else:
        rPI_x_mod = rPI_x
        rPI_y_mod = rPI_y
    rPI_lat = prev_lat + (rPI_x_mod * (prev_x - x))
    rPI_long = prev_long + (rPI_y_mod * (prev_y - y))
    # Calculating latitude longitude changes via grid coords
    # if x < prev_x:
    #     rPI_lat = prev_lat + (rPI_x_mod * (prev_x - x))
    # elif x > prev_x:
    #     rPI_lat = prev_lat - (rPI_x_mod * (x - prev_x))
    # else:
    #     rPI_lat = prev_lat
    # # Doing the same for y / longitude
    # if y < prev_y:
    #     rPI_long = prev_long - (rPI_y_mod * (prev_y - y))
    # elif y > prev_y:
    #     rPI_long = prev_long + (rPI_y_mod * (y - prev_y))
    # else:
    #     rPI_long = prev_long
    rPI_point = QgsPoint(rPI_lat, rPI_long)
    rPI_coords = [rPI_lat, rPI_long]
    points.append(rPI_point)
    coordinates.append(rPI_coords)
    raspberries.append("10.9.0."+str(ind))
    # Updating the trackback variables.
    # prev_x = x
    # prev_y = y
    # prev_lat = rPI_lat
    # prev_long = rPI_long

# create a memory layer with all the rPI points
# Most of this code came from: http://gis.stackexchange.com/questions/86812/how-to-draw-polygons-from-the-python-console
layer = QgsVectorLayer('Point', 'points', 'memory')
# add the first point (initial point)
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPoint(initial_lat, initial_long)
pt.setGeometry(QgsGeometry.fromPoint(point1))
pr.addFeatures([pt])
layer.updateExtents()
# For loop to create all of the Raspberry Pi points.
for rPI in points:
    pr = layer.dataProvider()
    pt = QgsFeature()
    pt.setGeometry(QgsGeometry.fromPoint(rPI))
    pr.addFeatures([pt])
    layer.updateExtents()

# add the layer to the canvas
QgsMapLayerRegistry.instance().addMapLayers([layer])

# Converting points from meter coordinates (3857) to GPS / Lat & Long coordinates in WGS 4326
# ESRI:3857 WGS Pseudo-Mercator
sourceCrs = QgsCoordinateReferenceSystem(3857)
# ESRI:4326 WGS (GCS)
destCrs = QgsCoordinateReferenceSystem(4326)
# Setting up transformation vector
xform = QgsCoordinateTransform(sourceCrs, destCrs)

# Only one of these transforms is technically necessary.
# Transforming directly
transformed = []
for rPI in points:
    transformed.append(xform.transform(rPI))

# Transforming indirectly.
transformed_coords = []
for rPI_c in coordinates:
    transformed_coords.append(xform.transform(QgsPoint(rPI_c[0], rPI_c[1])))

# Chaning directory to the place on my laptop that GIS data is stored.
os.chdir("D:\\DDPSC\\Raspberry_Pi\\GIS\\")
filename_1 = "transformed_coords.txt"
filename_2 = "jpeg_exif_coords.txt"
with open(filename_1, 'w') as fn_1:
    with open(filename_2, 'w') as fn_2:
        for index in range(len(transformed_coords)):
            trans = transformed_coords[index]
            lat = trans[0]
            lng = trans[1]
            # Printed in VisualSFM format
            print("{0} {1} {2}".format(raspberries[index], lat, lng), file=fn_1)
            if lat < 1:
                lat = abs(lat)
                lat_ref = "W"
                lat = Fraction(lat).limit_denominator()
            else:
                lat = abs(lat)
                lat_ref = "E"
                lat = Fraction(lat).limit_denominator()
            if lng < 1:
                lng = abs(lng)
                lng_ref = "S"
                lng = Fraction(lng).limit_denominator()
            else:
                lng = abs(lng)
                lng_ref = "N"
                lng = Fraction(lng).limit_denominator()
            # Printed in JPEG Exif format
            print("{IP} {lng_ref}; {lng},0/1,0/1; {lat_ref}; {lat},0/1,0/1; 0; 604/1; 2".format(IP=raspberries[index], lng_ref=lng_ref, lng=lng, lat_ref=lat_ref, lat=lat), file=fn_2)
<commit_msg>Half-flipped coordinates<commit_after># This file is meant to be used from the GQIS console
# It is highly specific to the DDPSC project.
from __future__ import print_function
from fractions import Fraction
import os
points = []
coordinates = []
raspberries = []
width = 6
height = 30
# offset = 10
offset = 11

# Initial GPS point that existed from standing in Greenhouse 9C and checking my location.
initial_lat = -10062973.096
initial_long = 4675266.934

# rPI_145 is 2.794 meters (x) and 0.5461 meters (y) from the initial lat/long point
rPI_145_lat = initial_lat + 2.794
rPI_145_long = initial_long + 0.5461
rPI_145 = QgsPoint(rPI_145_lat, rPI_145_long)
# points.append(rPI_145)
# We need to use the X, Y coordinates to convert latitude and longitude.
# init_x = (abs(145-(width*height)-offset) % width) + 1
# init_y = (abs(145-(width*height)-offset) / width) + 1
init_x = (abs(145-offset) % width) + 1
init_y = (abs(145-offset) / width) + 1

# Coordinates that the Raspberry PIs are separated by in an ideal grid.
# Still have to adjust for the first column that is off by a few.
# rPIs are separated by rPI_x in meters
rPI_x = 0.9779
# rPIs are separated by rPI_y in meters
rPI_y = 0.4572
# If they're in column 4 and 5 they have a different offset.
rPI_x_new = 0.9652
rPI_y_new = 0.1905

# Deriving initial rPI coordinates.
# We start at 10.9.0.11 or ShakoorCamera11 and proceed to generate points from there.
rPI_11_lat = rPI_145_lat - (rPI_x * 2)
rPI_11_long = rPI_145_long + (rPI_y * 22)
rPI_11 = QgsPoint(rPI_11_lat, rPI_11_long)
# Points are the QgsPoint coordinates in meters
# points.append(rPI_11)
# Coordinates are the floats that make up the QgsPoint coordinates in meters.
# coordinates.append([rPI_11_lat, rPI_11_long])
# Raspberries is the IP address of the Raspberry PI/inventory_hostname
# raspberries.append("10.9.0.11")
# We need to use the X, Y coordinates to convert latitude and longitude.
# init_x = (abs(11-(width*height)-offset) % width) + 1
# init_y = (abs(11-(width*height)-offset) / width) + 1

# Trackback variables that get updated at the end of each for loop
# updated to the just recently calculated Raspbery Pi
prev_x = init_x
prev_y = init_y
# prev_lat = rPI_11_lat
# prev_long = rPI_11_long
prev_lat = rPI_145_lat
prev_long = rPI_145_long
# Starting from ShakoorCamera12 to ShakoorCamera190
# Remember python ranges go to n-1
for ind in range(11, 191):
    # Formula to calculate x, y coordinates
    # formula = abs(ind-(width*height)-offset)
    formula = abs(ind - offset)
    y = (formula / width) + 1
    x = (formula % width) + 1
    # Adding shift for the first column
    shift_indices = []
    shift_indices = shift_indices + range(11, 191, 6) + range(12, 191, 6)
    rPI_x_mod = rPI_x
    rPI_y_mod = rPI_y
    if ind in shift_indices:
        rPI_x_mod = rPI_x_new
        rPI_y_mod = rPI_y_new
    else:
        rPI_x_mod = rPI_x
        rPI_y_mod = rPI_y
    # rPI_lat = prev_lat + (rPI_x_mod * (prev_x - x))
    # rPI_long = prev_long + (rPI_y_mod * (prev_y - y))
    rPI_lat = prev_lat + (rPI_x_mod * (prev_x - x))
    rPI_long = prev_long + (rPI_y_mod * (y - prev_y))
    # Calculating latitude longitude changes via grid coords
    # if x < prev_x:
    #     rPI_lat = prev_lat + (rPI_x_mod * (prev_x - x))
    # elif x > prev_x:
    #     rPI_lat = prev_lat - (rPI_x_mod * (x - prev_x))
    # else:
    #     rPI_lat = prev_lat
    # # Doing the same for y / longitude
    # if y < prev_y:
    #     rPI_long = prev_long - (rPI_y_mod * (prev_y - y))
    # elif y > prev_y:
    #     rPI_long = prev_long + (rPI_y_mod * (y - prev_y))
    # else:
    #     rPI_long = prev_long
    rPI_point = QgsPoint(rPI_lat, rPI_long)
    rPI_coords = [rPI_lat, rPI_long]
    points.append(rPI_point)
    coordinates.append(rPI_coords)
    raspberries.append("10.9.0."+str(ind))
    # Updating the trackback variables.
    # prev_x = x
    # prev_y = y
    # prev_lat = rPI_lat
    # prev_long = rPI_long

# create a memory layer with all the rPI points
# Most of this code came from: http://gis.stackexchange.com/questions/86812/how-to-draw-polygons-from-the-python-console
layer = QgsVectorLayer('Point', 'points', 'memory')
# add the first point (initial point)
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPoint(initial_lat, initial_long)
pt.setGeometry(QgsGeometry.fromPoint(point1))
pr.addFeatures([pt])
layer.updateExtents()
# For loop to create all of the Raspberry Pi points.
for rPI in points:
    pr = layer.dataProvider()
    pt = QgsFeature()
    pt.setGeometry(QgsGeometry.fromPoint(rPI))
    pr.addFeatures([pt])
    layer.updateExtents()

# add the layer to the canvas
QgsMapLayerRegistry.instance().addMapLayers([layer])

# Converting points from meter coordinates (3857) to GPS / Lat & Long coordinates in WGS 4326
# ESRI:3857 WGS Pseudo-Mercator
sourceCrs = QgsCoordinateReferenceSystem(3857)
# ESRI:4326 WGS (GCS)
destCrs = QgsCoordinateReferenceSystem(4326)
# Setting up transformation vector
xform = QgsCoordinateTransform(sourceCrs, destCrs)

# Only one of these transforms is technically necessary.
# Transforming directly
transformed = []
for rPI in points:
    transformed.append(xform.transform(rPI))

# Transforming indirectly.
transformed_coords = []
for rPI_c in coordinates:
    transformed_coords.append(xform.transform(QgsPoint(rPI_c[0], rPI_c[1])))

# Chaning directory to the place on my laptop that GIS data is stored.
os.chdir("D:\\DDPSC\\Raspberry_Pi\\GIS\\")
filename_1 = "transformed_coords.txt"
filename_2 = "jpeg_exif_coords.txt"
with open(filename_1, 'w') as fn_1:
    with open(filename_2, 'w') as fn_2:
        for index in range(len(transformed_coords)):
            trans = transformed_coords[index]
            lat = trans[0]
            lng = trans[1]
            # Printed in VisualSFM format
            print("{0} {1} {2}".format(raspberries[index], lat, lng), file=fn_1)
            if lat < 1:
                lat = abs(lat)
                lat_ref = "W"
                lat = Fraction(lat).limit_denominator()
            else:
                lat = abs(lat)
                lat_ref = "E"
                lat = Fraction(lat).limit_denominator()
            if lng < 1:
                lng = abs(lng)
                lng_ref = "S"
                lng = Fraction(lng).limit_denominator()
            else:
                lng = abs(lng)
                lng_ref = "N"
                lng = Fraction(lng).limit_denominator()
            # Printed in JPEG Exif format
            print("{IP} {lng_ref}; {lng},0/1,0/1; {lat_ref}; {lat},0/1,0/1; 0; 604/1; 2".format(IP=raspberries[index], lng_ref=lng_ref, lng=lng, lat_ref=lat_ref, lat=lat), file=fn_2)
<|endoftext|>


<commit_before>#!/usr/bin/python
from __future__ import print_function
import serial, struct

ser = serial.Serial()
ser.port = '/dev/ttyUSB0'
ser.baudrate = 9600

ser.open()
ser.flushInput()

class SDS011():
    def process_frame(self, d):
        r = struct.unpack('<HHxxBBB', d[2:])
        pm25 = r[0] / 10.0
        pm10 = r[1] / 10.0
        checksum = sum(ord(v) for v in d[2:8]) % 256
        return (pm25, pm10, checksum)

    def run(self):
        byte, data = 0, ""
        while byte != "\xaa":
            byte = ser.read(size=1)
        d = ser.read(size=10)
        if d[0] == "\xc0":
            self.process_frame(byte + d)
<commit_msg>further class corrections for SDS011<commit_after>#!/usr/bin/python
from __future__ import print_function
import serial, struct

class SDS011():

    def __init__(self):
        self.ser = serial.Serial()
        self.ser.port = '/dev/ttyUSB0'
        self.ser.baudrate = 9600
        self.ser.open()
        self.ser.flushInput()

    def process_frame(self, d):
        r = struct.unpack('<HHxxBBB', d[2:])
        pm25 = r[0] / 10.0
        pm10 = r[1] / 10.0
        checksum = sum(ord(v) for v in d[2:8]) % 256
        return (pm25, pm10, checksum)

    def run(self):
        byte, data = 0, ""
        while byte != "\xaa":
            byte = self.ser.read(size=1)
        d = self.ser.read(size=10)
        if d[0] == "\xc0":
            self.process_frame(byte + d)
<|endoftext|>


<commit_before>"""
Copyright (C) 2014 Bernd Zeimetz <bernd@bzed.de>
Copyright (C) 2014 Bernd Zeimetz <b.zeimetz@conova.com>

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.
"""

from flask import Flask, request, abort, Response
from IPy import IP
from fnmatch import fnmatch

from .grammar import grammar
import requests
from requests.auth import HTTPBasicAuth
import logging
import os
import json

static_favicon = True
try:
    from wand.image import Image
    from StringIO import StringIO
    static_favicon = False
except ImportError:
    pass

configfile_name = 'graphite_aclproxy.conf'

instance_relative_config = False
instance_path = None
if os.path.exists('/etc/carbon/{0!s}'.format(configfile_name )):
    instance_relative_config = True
    instance_path = '/etc/carbon'

app = Flask(__name__.split('.')[0],
            static_url_path='/static',
            instance_relative_config=instance_relative_config,
            instance_path=instance_path
            )
app.config.from_object('graphite_aclproxy.default_settings')

if os.getenv('GRAPHITE_ACLPROXY_SETTINGS'):
    app.config.from_envvar('GRAPHITE_ACLPROXY_SETTINGS')
else:
    app.config.from_pyfile(configfile_name, silent=False)

logging.basicConfig(level=app.config['LOG_LEVEL'])
LOG = logging.getLogger(app.config['LOG_NAME'])

AUTH = app.config['REQUESTS_GRAPHITE_AUTH']
IP_ACL = app.config['IP_ACL']
DYNAMIC_FAVICON_TARGET = app.config['DYNAMIC_FAVICON_TARGET']

@app.route('/favicon.ico')
def favicon():
    if static_favicon:
        return app.send_static_file('favicon.ico')

    # for dynamic fun:
    # width=64&height=64&from=-2hours&graphOnly=true&target=carbon.agents.*.metricsReceived
    favicon_args = {
            'width': 32,
            'height': 32,
            'from': '-2hours',
            'graphOnly': 'true',
            'target': DYNAMIC_FAVICON_TARGET,
            'format': 'png'
            }
    response, headers = upstream_req('/render/', favicon_args)
    response_file = StringIO()
    for data in response():
        response_file.write(data)
    response_file.seek(0)
    image = Image(file=response_file, format='png')
    image.format = 'ico'
    headers['content-type'] = 'image/x-icon'
    return Response(image.make_blob(), headers=headers)

@app.route('/', defaults={'url': ''})
@app.route('/<path:url>')
def root(url):
    LOG.warn('Unknown url: {0!s}'.format(url))
    abort(404)

@app.route('/render/')
def render_proxy():
    """Proxy the render API.
    """
    if not check_render_ip_acl():
        LOG.warn("FailedACL: '%s', '400', '%s'",
                 request.remote_addr,
                 request.query_string
                 )
        abort(400)

    response, headers = upstream_req('/render/',
                                     request.args.to_dict(flat=False)
                                     )
    return Response(response(), headers=headers, status=200)

@app.route('/metrics/find/')
def metrics_proxy():
    """Proxy the render API.
    """
    response, headers = upstream_req('/metrics/find/',
                                     request.args.to_dict(flat=False)
                                     )
    # reponse() is a generator.
    response_data = filter_metrics_ip_acl(''.join([x for x in response()]))
    return Response(response_data, headers=headers, status=200)

def upstream_req(path, args):
    if AUTH:
        auth = HTTPBasicAuth(AUTH[0], AUTH[1])
    else:
        auth = None

    url = '{0!s}{1!s}'.format(app.config['REQUESTS_GRAPHITE_URL'], path)
    auth = app.config['REQUESTS_GRAPHITE_AUTH']
    headers = {}
    r = requests.get(url,
                     stream=True,
                     params=args,
                     headers=headers,
                     verify=app.config['REQUESTS_SSL_VERIFY'],
                     auth=auth
                     )
    LOG.info("UpstreamRequest: '%s','%s'", r.status_code, args)

    # abort if status_code != 200
    if r.status_code != 200:
        abort(503, 'graphite render api returned an error')

    r_headers = dict(r.headers)
    headers = {
        'Content-Type': r_headers['Content-Type']
    }

    def resp_generator():
        for chunk in r.iter_content(app.config['REQUESTS_CHUNK_SIZE']):
            yield chunk
    return (resp_generator, headers)

def get_allowed_ip_acl_tokens(remote_ip):
    remote_ip = IP(remote_ip)
    allowed_tokens = []
    for network, acl_tokens in IP_ACL.iteritems():
        if remote_ip in IP(network):
            allowed_tokens.extend(acl_tokens)
    return allowed_tokens

def filter_metrics_ip_acl(response):
    filtered_response = []
    remote_ip = request.remote_addr
    allowed_tokens = get_allowed_ip_acl_tokens(remote_ip)
    if not allowed_tokens:
        LOG.warn("No ACLs for %s", remote_ip)
        return []

    try:
        filter_tokens = []
        for allowed_token in allowed_tokens:
            token_parts = allowed_token.split('.')
            for i in range(1, len(token_parts) + 1):
                filter_tokens.append('.'.join(token_parts[0:i]))
        # remove duplicates
        filter_tokens = list(set(filter_tokens))
        response_data = json.loads(response)
        for resp in response_data:
            for filter_token in filter_tokens:
                if fnmatch(resp['id'], filter_token):
                    filtered_response.append(resp)
                    break
        return json.dumps(filtered_response)
    except Exception as err:
        LOG.warn("FailedRequest: %s (%s) - %s",
                 str(err),
                 request.query_string,
                 response)
        abort(400, 'Failed to parse targets')
    return True

def check_render_ip_acl():
    remote_ip = request.remote_addr
    allowed_tokens = get_allowed_ip_acl_tokens(remote_ip)
    if not allowed_tokens:
        LOG.warn("No ACLs for %s", remote_ip)
        return False

    try:
        if 'target' not in request.args:
            raise ValueError('target missing in query')
        for target in request.args.getlist('target'):
            tokens = grammar.parseString(target)
            for token in _evaluateTokens(tokens):
                token_allowed = False
                LOG.debug("evaluated target: %s", token)
                allowed_token = allowed_tokens[0]
                for allowed_token in allowed_tokens:
                    if fnmatch(token, allowed_token):
                        LOG.debug("token %s allowed in [%s]",
                                  token,
                                  allowed_token,
                                  )
                        token_allowed = True
                        break
                if not token_allowed:
                    LOG.warn("token %s not allowed in [%s]",
                             token,
                             allowed_token)
                    return False
    except Exception as err:
        LOG.warn("FailedRequest: %s (%s)", str(err), request.query_string)
        abort(400, 'Failed to parse targets')

    return True

def _evaluateTokens(tokens):
    if tokens.expression:
        for i in _evaluateTokens(tokens.expression):
            yield i

    elif tokens.pathExpression:
        yield tokens.pathExpression

    elif tokens.call:
        for arg in tokens.call.args:
            for i in _evaluateTokens(arg):
                yield i

        for kwarg in tokens.call.kwargs:
            for i in _evaluateTokens(kwarg.args[0]):
                yield i
<commit_msg>move some code out of the general try/except part<commit_after>"""
Copyright (C) 2014 Bernd Zeimetz <bernd@bzed.de>
Copyright (C) 2014 Bernd Zeimetz <b.zeimetz@conova.com>

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.
"""

from flask import Flask, request, abort, Response
from IPy import IP
from fnmatch import fnmatch

from .grammar import grammar
import requests
from requests.auth import HTTPBasicAuth
import logging
import os
import json

static_favicon = True
try:
    from wand.image import Image
    from StringIO import StringIO
    static_favicon = False
except ImportError:
    pass

configfile_name = 'graphite_aclproxy.conf'

instance_relative_config = False
instance_path = None
if os.path.exists('/etc/carbon/{0!s}'.format(configfile_name )):
    instance_relative_config = True
    instance_path = '/etc/carbon'

app = Flask(__name__.split('.')[0],
            static_url_path='/static',
            instance_relative_config=instance_relative_config,
            instance_path=instance_path
            )
app.config.from_object('graphite_aclproxy.default_settings')

if os.getenv('GRAPHITE_ACLPROXY_SETTINGS'):
    app.config.from_envvar('GRAPHITE_ACLPROXY_SETTINGS')
else:
    app.config.from_pyfile(configfile_name, silent=False)

logging.basicConfig(level=app.config['LOG_LEVEL'])
LOG = logging.getLogger(app.config['LOG_NAME'])

AUTH = app.config['REQUESTS_GRAPHITE_AUTH']
IP_ACL = app.config['IP_ACL']
DYNAMIC_FAVICON_TARGET = app.config['DYNAMIC_FAVICON_TARGET']

@app.route('/favicon.ico')
def favicon():
    if static_favicon:
        return app.send_static_file('favicon.ico')

    # for dynamic fun:
    # width=64&height=64&from=-2hours&graphOnly=true&target=carbon.agents.*.metricsReceived
    favicon_args = {
            'width': 32,
            'height': 32,
            'from': '-2hours',
            'graphOnly': 'true',
            'target': DYNAMIC_FAVICON_TARGET,
            'format': 'png'
            }
    response, headers = upstream_req('/render/', favicon_args)
    response_file = StringIO()
    for data in response():
        response_file.write(data)
    response_file.seek(0)
    image = Image(file=response_file, format='png')
    image.format = 'ico'
    headers['content-type'] = 'image/x-icon'
    return Response(image.make_blob(), headers=headers)

@app.route('/', defaults={'url': ''})
@app.route('/<path:url>')
def root(url):
    LOG.warn('Unknown url: {0!s}'.format(url))
    abort(404)

@app.route('/render/')
def render_proxy():
    """Proxy the render API.
    """
    if not check_render_ip_acl():
        LOG.warn("FailedACL: '%s', '400', '%s'",
                 request.remote_addr,
                 request.query_string
                 )
        abort(400)

    response, headers = upstream_req('/render/',
                                     request.args.to_dict(flat=False)
                                     )
    return Response(response(), headers=headers, status=200)

@app.route('/metrics/find/')
def metrics_proxy():
    """Proxy the render API.
    """
    response, headers = upstream_req('/metrics/find/',
                                     request.args.to_dict(flat=False)
                                     )
    # reponse() is a generator.
    response_data = filter_metrics_ip_acl(''.join([x for x in response()]))
    return Response(response_data, headers=headers, status=200)

def upstream_req(path, args):
    if AUTH:
        auth = HTTPBasicAuth(AUTH[0], AUTH[1])
    else:
        auth = None

    url = '{0!s}{1!s}'.format(app.config['REQUESTS_GRAPHITE_URL'], path)
    auth = app.config['REQUESTS_GRAPHITE_AUTH']
    headers = {}
    r = requests.get(url,
                     stream=True,
                     params=args,
                     headers=headers,
                     verify=app.config['REQUESTS_SSL_VERIFY'],
                     auth=auth
                     )
    LOG.info("UpstreamRequest: '%s','%s'", r.status_code, args)

    # abort if status_code != 200
    if r.status_code != 200:
        abort(503, 'graphite render api returned an error')

    r_headers = dict(r.headers)
    headers = {
        'Content-Type': r_headers['Content-Type']
    }

    def resp_generator():
        for chunk in r.iter_content(app.config['REQUESTS_CHUNK_SIZE']):
            yield chunk
    return (resp_generator, headers)

def get_allowed_ip_acl_tokens(remote_ip):
    remote_ip = IP(remote_ip)
    allowed_tokens = []
    for network, acl_tokens in IP_ACL.iteritems():
        if remote_ip in IP(network):
            allowed_tokens.extend(acl_tokens)
    return allowed_tokens

def filter_metrics_ip_acl(response):
    filtered_response = []
    remote_ip = request.remote_addr
    allowed_tokens = get_allowed_ip_acl_tokens(remote_ip)
    if not allowed_tokens:
        LOG.warn("No ACLs for %s", remote_ip)
        return []

    filter_tokens = []
    for allowed_token in allowed_tokens:
        token_parts = allowed_token.split('.')
        for i in range(1, len(token_parts) + 1):
            filter_tokens.append('.'.join(token_parts[0:i]))
    # remove duplicates
    filter_tokens = list(set(filter_tokens))

    try:
        response_data = json.loads(response)
        for resp in response_data:
            for filter_token in filter_tokens:
                if fnmatch(resp['id'], filter_token):
                    filtered_response.append(resp)
                    break
        return json.dumps(filtered_response)
    except Exception as err:
        LOG.warn("FailedRequest: %s (%s) - %s",
                 str(err),
                 request.query_string,
                 response)
        abort(400, 'Failed to parse targets')
    return True

def check_render_ip_acl():
    remote_ip = request.remote_addr
    allowed_tokens = get_allowed_ip_acl_tokens(remote_ip)
    if not allowed_tokens:
        LOG.warn("No ACLs for %s", remote_ip)
        return False

    try:
        if 'target' not in request.args:
            raise ValueError('target missing in query')
        for target in request.args.getlist('target'):
            tokens = grammar.parseString(target)
            for token in _evaluateTokens(tokens):
                token_allowed = False
                LOG.debug("evaluated target: %s", token)
                allowed_token = allowed_tokens[0]
                for allowed_token in allowed_tokens:
                    if fnmatch(token, allowed_token):
                        LOG.debug("token %s allowed in [%s]",
                                  token,
                                  allowed_token,
                                  )
                        token_allowed = True
                        break
                if not token_allowed:
                    LOG.warn("token %s not allowed in [%s]",
                             token,
                             allowed_token)
                    return False
    except Exception as err:
        LOG.warn("FailedRequest: %s (%s)", str(err), request.query_string)
        abort(400, 'Failed to parse targets')

    return True

def _evaluateTokens(tokens):
    if tokens.expression:
        for i in _evaluateTokens(tokens.expression):
            yield i

    elif tokens.pathExpression:
        yield tokens.pathExpression

    elif tokens.call:
        for arg in tokens.call.args:
            for i in _evaluateTokens(arg):
                yield i

        for kwarg in tokens.call.kwargs:
            for i in _evaluateTokens(kwarg.args[0]):
                yield i
<|endoftext|>


<commit_before>from itertools import izip, chain, repeat

from django.core.urlresolvers import reverse
from django.core.cache import cache

from grid.models import Grid

def grouper(n, iterable, padvalue=None):
    "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
    return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)

def grid_headers(request):
    cache_key = 'grid_headers'
    grid_headers = cache.get(cache_key)
    if grid_headers is None:
        grid_headers = list(Grid.objects.filter(header=True))
        grid_headers = grouper(7, grid_headers)
        cache.set(cache_key, grid_headers, 60 * 5)
    return {'grid_headers': grid_headers}
<commit_msg>Removing another cache call<commit_after>from itertools import izip, chain, repeat

from grid.models import Grid

def grouper(n, iterable, padvalue=None):
    "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
    return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)

def grid_headers(request):
    grid_headers = list(Grid.objects.filter(header=True))
    grid_headers = grouper(7, grid_headers)
    return {'grid_headers': grid_headers}
<|endoftext|>