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

# Copyright (c) 2017 shmilee

import logging
import gdpy3.read as gdr
from gdpy3.plot import data1d, history

log0 = logging.getLogger('test')
log1 = logging.getLogger('gdp')

#CasePath = '/home/IFTS_shmilee/201703XX-residual-ZF/n18-T'
#CasePath = '/home/IFTS_shmilee/201703XX-residual-ZF/obo-4T-50dots-0.4'
CasePath = '/home/IFTS_shmilee/20170527-ITG-nonlinear/n14-D/'

if __name__ == '__main__':
    log1.setLevel(10)
    dictobj = gdr.read(CasePath)
    zf = data1d.Data1dFigureV110922(
        dictobj,
        'zonal_flow',
        #'residual_zonal_flow',
    )
    his = history.HistoryFigureV110922(
        dictobj,
        #'ion_density_entropy',
        #'ion_momentum',
        #'ion_energy',
        'ion_particle_momentum_flux',
        #'ion_energy_flux',
        #'field_phi',
        #'field_phi00',
        #'mode3_phi',
    )
    for gf in [
            # zf,
            his]:
        gf.calculate(
            # region_start=120, region_end=400,
            # region_start=20, region_end=120,
            # plot_method='plot_surface',
            plot_method='pcolormesh',
        )
        log0.info('calculation: %s ' % gf.calculation)
        input('Enter to continue, show: ')
        gf.show()
        input('Enter to continue, close: ')
        gf.close()
<commit_msg>test trackparticle orbits<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright (c) 2017 shmilee

import logging
import gdpy3.read as gdr
from gdpy3.plot import data1d, history, trackparticle

log0 = logging.getLogger('test')
log1 = logging.getLogger('gdp')

#CasePath = '/home/IFTS_shmilee/201703XX-residual-ZF/n18-T'
#CasePath = '/home/IFTS_shmilee/201703XX-residual-ZF/obo-4T-50dots-0.4'
CasePath = '/home/IFTS_shmilee/20170527-ITG-nonlinear/n14-D/'

if __name__ == '__main__':
    log1.setLevel(10)
    dictobj = gdr.read(CasePath)
    # zf = data1d.Data1dFigureV110922(
    #    dictobj,
    #    'zonal_flow',
    #    #'residual_zonal_flow',
    #)
    # his = history.HistoryFigureV110922(
    #    dictobj,
    #    #'ion_density_entropy',
    #    #'ion_momentum',
    #    #'ion_energy',
    #    'ion_particle_momentum_flux',
    #    #'ion_energy_flux',
    #    #'field_phi',
    #    #'field_phi00',
    #    #'mode3_phi',
    #)
    track = trackparticle.TrackParticleFigureV110922(
        dictobj,
        'orbit_2d_ion',
        #'orbit_3d_ion',
        #'orbit_2d_electron',
    )
    for gf in [
            # zf,
            # his,
            track]:
        gf.calculate(
            # region_start=120, region_end=400,
            # region_start=20, region_end=120,
            # plot_method='plot_surface',
            # plot_method='pcolormesh',
            # key='random', index=range(5,14),
            key='in-', index=range(1, 10),
        )
        if 'orbit_2d' in gf.name:
            gf.figurestyle = ['gdpy3-notebook',
                              {'figure.figsize': (12.0, 12.0)}]
        log0.info('calculation: %s ' % gf.calculation)
        input('Enter to continue, show: ')
        gf.show()
        input('Enter to continue, close: ')
        gf.close()
<|endoftext|>


<commit_before>from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType

from talks.api_ox.models import Location, Organisation

class EventGroup(models.Model):
    title = models.CharField(max_length=250)
    slug = models.SlugField()
    description = models.TextField()

    def __unicode__(self):
        return self.title

class Speaker(models.Model):
    name = models.CharField(max_length=250)
    slug = models.SlugField()
    bio = models.TextField()

    def __unicode__(self):
        return self.name

class Tag(models.Model):

    slug = models.SlugField()
    name = models.CharField(max_length=250, unique=True)
    description = models.TextField()
    # TODO url?
    # TODO categorisation? e.g. Organisation

    def __unicode__(self):
        return self.name

class TagItem(models.Model):

    tag = models.ForeignKey(Tag)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    item = GenericForeignKey('content_type', 'object_id')   # atm: Event, EventGroup

class Event(models.Model):
    start = models.DateTimeField(null=True, blank=True)
    end = models.DateTimeField(null=True, blank=True)
    title = models.CharField(max_length=250)
    slug = models.SlugField()
    description = models.TextField()
    speakers = models.ManyToManyField(Speaker, null=True, blank=True)
    # TODO audience should it be free text
    # TODO booking information; structure?

    group = models.ForeignKey(EventGroup, null=True, blank=True)
    location = models.ForeignKey(Location, null=True, blank=True)
    # TODO I'm guessing an event can be organised by multiple departments?
    department_organiser = models.ForeignKey(Organisation, null=True, blank=True)

    tags = GenericRelation(TagItem)

    def __unicode__(self):
        return "Event: {title} ({start})".format(title=self.title,
                                          start=self.start.strftime("%Y-%m-%d %H:%M"))

    def get_absolute_url(self):
        from django.core.urlresolvers import reverse
        return reverse('event', args=[str(self.id)])
<commit_msg>Removed inline import<commit_after>from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse

from talks.api_ox.models import Location, Organisation

class EventGroup(models.Model):
    title = models.CharField(max_length=250)
    slug = models.SlugField()
    description = models.TextField()

    def __unicode__(self):
        return self.title

class Speaker(models.Model):
    name = models.CharField(max_length=250)
    slug = models.SlugField()
    bio = models.TextField()

    def __unicode__(self):
        return self.name

class Tag(models.Model):

    slug = models.SlugField()
    name = models.CharField(max_length=250, unique=True)
    description = models.TextField()
    # TODO url?
    # TODO categorisation? e.g. Organisation

    def __unicode__(self):
        return self.name

class TagItem(models.Model):

    tag = models.ForeignKey(Tag)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    item = GenericForeignKey('content_type', 'object_id')   # atm: Event, EventGroup

class Event(models.Model):
    start = models.DateTimeField(null=True, blank=True)
    end = models.DateTimeField(null=True, blank=True)
    title = models.CharField(max_length=250)
    slug = models.SlugField()
    description = models.TextField()
    speakers = models.ManyToManyField(Speaker, null=True, blank=True)
    # TODO audience should it be free text
    # TODO booking information; structure?

    group = models.ForeignKey(EventGroup, null=True, blank=True)
    location = models.ForeignKey(Location, null=True, blank=True)
    # TODO I'm guessing an event can be organised by multiple departments?
    department_organiser = models.ForeignKey(Organisation, null=True, blank=True)

    tags = GenericRelation(TagItem)

    def __unicode__(self):
        return "Event: {title} ({start})".format(title=self.title,
                                          start=self.start.strftime("%Y-%m-%d %H:%M"))

    def get_absolute_url(self):
        return reverse('event', args=[str(self.id)])
<|endoftext|>


<commit_before># Lint as: python2, python3
# Copyright 2019 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Python source file include Iris pipeline functions and necesasry utils.

For a TFX pipeline to successfully run, a preprocessing_fn and a
_build_estimator function needs to be provided.  This file contains both.
"""

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

import absl
import tensorflow as tf
import tensorflow_model_analysis as tfma
from tensorflow_transform.tf_metadata import schema_utils

_FEATURE_KEYS = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
_LABEL_KEY = 'variety'

# Tf.Transform considers these features as "raw"
def _get_raw_feature_spec(schema):
  return schema_utils.schema_as_feature_spec(schema).feature_spec

def _gzip_reader_fn(filenames):
  """Small utility returning a record reader that can read gzip'ed files."""
  return tf.data.TFRecordDataset(filenames, compression_type='GZIP')

def _example_serving_receiver_fn(schema):
  """Build the serving in inputs.

  Args:
    schema: the schema of the input data.

  Returns:
    Tensorflow graph which parses examples, applying tf-transform to them.
  """
  raw_feature_spec = _get_raw_feature_spec(schema)
  raw_feature_spec.pop(_LABEL_KEY)

  raw_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
      raw_feature_spec, default_batch_size=None)
  serving_input_receiver = raw_input_fn()

  return tf.estimator.export.ServingInputReceiver(
      serving_input_receiver.features, serving_input_receiver.receiver_tensors)

def _eval_input_receiver_fn(schema):
  """Build everything needed for the tf-model-analysis to run the model.

  Args:
    schema: the schema of the input data.

  Returns:
    EvalInputReceiver function, which contains:
      - Tensorflow graph which parses raw untransformed features, applies the
        tf-transform preprocessing operators.
      - Set of raw, untransformed features.
      - Label against which predictions will be compared.
  """
  # Notice that the inputs are raw features, not transformed features here.
  raw_feature_spec = _get_raw_feature_spec(schema)

  serialized_tf_example = tf.compat.v1.placeholder(
      dtype=tf.string, shape=[None], name='input_example_tensor')

  # Add a parse_example operator to the tensorflow graph, which will parse
  # raw, untransformed, tf examples.
  features = tf.io.parse_example(
      serialized=serialized_tf_example, features=raw_feature_spec)

  # The key name MUST be 'examples'.
  receiver_tensors = {'examples': serialized_tf_example}

  return tfma.export.EvalInputReceiver(
      features=features,
      receiver_tensors=receiver_tensors,
      labels=features.pop(_LABEL_KEY))

def _input_fn(filenames, schema, batch_size=200):
  """Generates features and labels for training or evaluation.

  Args:
    filenames: [str] list of CSV files to read data from.
    schema: Schema of the input data.
    batch_size: int First dimension size of the Tensors returned by input_fn

  Returns:
    A (features, indices) tuple where features is a dictionary of
      Tensors, and indices is a single Tensor of label indices.
  """

  feature_spec = _get_raw_feature_spec(schema)

  dataset = tf.data.experimental.make_batched_features_dataset(
      filenames, batch_size, feature_spec, reader=_gzip_reader_fn)

  features = tf.compat.v1.data.make_one_shot_iterator(dataset).get_next()

  # We pop the label because we do not want to use it as a feature while we're
  # training.
  return features, features.pop(_LABEL_KEY)

def _keras_model_builder():
  """Creates a DNN Keras model  for classifying iris data.

  Returns:
    A keras Model.
  """

  l = tf.keras.layers
  opt = tf.keras.optimizers
  inputs = [l.Input(shape=(1,), name=f) for f in _FEATURE_KEYS]
  input_layer = l.concatenate(inputs)
  d1 = l.Dense(8, activation='relu')(input_layer)
  output = l.Dense(3, activation='softmax')(d1)
  model = tf.keras.Model(inputs=inputs, outputs=output)
  model.compile(
      loss='sparse_categorical_crossentropy',
      optimizer=opt.Adam(lr=0.001),
      metrics=[
          tf.keras.metrics.BinaryAccuracy(name='accuracy')
      ])
  absl.logging.info(model.summary())
  return model

# TFX will call this function
def trainer_fn(hparams, schema):
  """Build the estimator using the high level API.

  Args:
    hparams: Holds hyperparameters used to train the model as name/value pairs.
    schema: Holds the schema of the training examples.

  Returns:
    A dict of the following:
      - estimator: The estimator that will be used for training and eval.
      - train_spec: Spec for training.
      - eval_spec: Spec for eval.
      - eval_input_receiver_fn: Input function for eval.
  """

  train_batch_size = 40
  eval_batch_size = 40

  train_input_fn = lambda: _input_fn(  # pylint: disable=g-long-lambda
      hparams.train_files,
      schema,
      batch_size=train_batch_size)

  eval_input_fn = lambda: _input_fn(  # pylint: disable=g-long-lambda
      hparams.eval_files,
      schema,
      batch_size=eval_batch_size)

  train_spec = tf.estimator.TrainSpec(  # pylint: disable=g-long-lambda
      train_input_fn,
      max_steps=hparams.train_steps)

  serving_receiver_fn = lambda: _example_serving_receiver_fn(schema)  # pylint: disable=g-long-lambda

  exporter = tf.estimator.FinalExporter('iris', serving_receiver_fn)
  eval_spec = tf.estimator.EvalSpec(
      eval_input_fn,
      steps=hparams.eval_steps,
      exporters=[exporter],
      name='iris-eval')

  run_config = tf.estimator.RunConfig(
      save_checkpoints_steps=999, keep_checkpoint_max=1)

  run_config = run_config.replace(model_dir=hparams.serving_model_dir)

  estimator = tf.keras.estimator.model_to_estimator(
      keras_model=_keras_model_builder(), config=run_config)

  # Create an input receiver for TFMA processing
  receiver_fn = lambda: _eval_input_receiver_fn(schema)  # pylint: disable=g-long-lambda

  return {
      'estimator': estimator,
      'train_spec': train_spec,
      'eval_spec': eval_spec,
      'eval_input_receiver_fn': receiver_fn
  }
<commit_msg>Remove redundant code and updating comments accordingly.<commit_after># Lint as: python2, python3
# Copyright 2019 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Python source file include Iris pipeline functions and necesasry utils.

For a TFX pipeline to successfully run, a preprocessing_fn and a
_build_estimator function needs to be provided.  This file contains both.
"""

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

import absl
import tensorflow as tf
import tensorflow_model_analysis as tfma
from tensorflow_transform.tf_metadata import schema_utils

_FEATURE_KEYS = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
_LABEL_KEY = 'variety'

# Tf.Transform considers these features as "raw"
def _get_raw_feature_spec(schema):
  return schema_utils.schema_as_feature_spec(schema).feature_spec

def _gzip_reader_fn(filenames):
  """Small utility returning a record reader that can read gzip'ed files."""
  return tf.data.TFRecordDataset(filenames, compression_type='GZIP')

def _example_serving_receiver_fn(schema):
  """Build the serving inputs.

  Args:
    schema: the schema of the input data.

  Returns:
    serving_input_resiver_fn for serving this model, since no transformation is
    required in this case it does not include a tf-transform graph.
  """
  raw_feature_spec = _get_raw_feature_spec(schema)
  raw_feature_spec.pop(_LABEL_KEY)

  raw_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
      raw_feature_spec, default_batch_size=None)
  return raw_input_fn()

def _eval_input_receiver_fn(schema):
  """Build the evalution inputs for the tf-model-analysis to run the model.

  Args:
    schema: the schema of the input data.

  Returns:
    EvalInputReceiver function, which contains:
      - Features (dict of Tensors) to be passed to the model.
      - Raw features as sereliazlied tf.Examples.
      - Labels
  """
  # Notice that the inputs are raw features, not transformed features here.
  raw_feature_spec = _get_raw_feature_spec(schema)

  serialized_tf_example = tf.compat.v1.placeholder(
      dtype=tf.string, shape=[None], name='input_example_tensor')

  # Add a parse_example operator to the tensorflow graph, which will parse
  # raw, untransformed, tf examples.
  features = tf.io.parse_example(
      serialized=serialized_tf_example, features=raw_feature_spec)

  # The key name MUST be 'examples'.
  receiver_tensors = {'examples': serialized_tf_example}

  return tfma.export.EvalInputReceiver(
      features=features,
      receiver_tensors=receiver_tensors,
      labels=features.pop(_LABEL_KEY))

def _input_fn(filenames, schema, batch_size=200):
  """Input function for training and evaluation.

  Args:
    filenames: [str] list of CSV files to read data from.
    schema: Schema of the input data.
    batch_size: int First dimension size of the Tensors returned by input_fn

  Returns:
    A (features, indices) tuple where features is a dictionary of
      Tensors, and indices is a single Tensor of label indices.
  """

  feature_spec = _get_raw_feature_spec(schema)

  dataset = tf.data.experimental.make_batched_features_dataset(
      filenames, batch_size, feature_spec, reader=_gzip_reader_fn)

  features = tf.compat.v1.data.make_one_shot_iterator(dataset).get_next()

  # We pop the label because we do not want to use it as a feature while we're
  # training.
  return features, features.pop(_LABEL_KEY)

def _keras_model_builder():
  """Creates a DNN Keras model  for classifying iris data.

  Returns:
    A keras Model.
  """

  l = tf.keras.layers
  opt = tf.keras.optimizers
  inputs = [l.Input(shape=(1,), name=f) for f in _FEATURE_KEYS]
  input_layer = l.concatenate(inputs)
  d1 = l.Dense(8, activation='relu')(input_layer)
  output = l.Dense(3, activation='softmax')(d1)
  model = tf.keras.Model(inputs=inputs, outputs=output)
  model.compile(
      loss='sparse_categorical_crossentropy',
      optimizer=opt.Adam(lr=0.001),
      metrics=[tf.keras.metrics.BinaryAccuracy(name='accuracy')])
  absl.logging.info(model.summary())
  return model

# TFX will call this function
def trainer_fn(hparams, schema):
  """Build the estimator using the high level API.

  Args:
    hparams: Holds hyperparameters used to train the model as name/value pairs.
    schema: Holds the schema of the training examples.

  Returns:
    A dict of the following:
      - estimator: The estimator that will be used for training and eval.
      - train_spec: Spec for training.
      - eval_spec: Spec for eval.
      - eval_input_receiver_fn: Input function for eval.
  """

  train_batch_size = 40
  eval_batch_size = 40

  train_input_fn = lambda: _input_fn(  # pylint: disable=g-long-lambda
      hparams.train_files,
      schema,
      batch_size=train_batch_size)

  eval_input_fn = lambda: _input_fn(  # pylint: disable=g-long-lambda
      hparams.eval_files,
      schema,
      batch_size=eval_batch_size)

  train_spec = tf.estimator.TrainSpec(  # pylint: disable=g-long-lambda
      train_input_fn,
      max_steps=hparams.train_steps)

  serving_receiver_fn = lambda: _example_serving_receiver_fn(schema)  # pylint: disable=g-long-lambda

  exporter = tf.estimator.FinalExporter('iris', serving_receiver_fn)
  eval_spec = tf.estimator.EvalSpec(
      eval_input_fn,
      steps=hparams.eval_steps,
      exporters=[exporter],
      name='iris-eval')

  run_config = tf.estimator.RunConfig(
      save_checkpoints_steps=999, keep_checkpoint_max=1)

  run_config = run_config.replace(model_dir=hparams.serving_model_dir)

  estimator = tf.keras.estimator.model_to_estimator(
      keras_model=_keras_model_builder(), config=run_config)

  # Create an input receiver for TFMA processing
  receiver_fn = lambda: _eval_input_receiver_fn(schema)  # pylint: disable=g-long-lambda

  return {
      'estimator': estimator,
      'train_spec': train_spec,
      'eval_spec': eval_spec,
      'eval_input_receiver_fn': receiver_fn
  }
<|endoftext|>


<commit_before>from django.contrib import admin
from doormat.models import DoorMat

class DoorMatAdmin(admin.ModelAdmin):
    list_display = ('domain', 'path', 'is_enabled', 'last_published')
    list_display_links = ('domain', 'path')
    list_filter = ('is_enabled', 'domain')
    readonly_fields = ('last_published',)
    search_fields = ('domain', 'path', 'content')
admin.site.register(DoorMat, DoorMatAdmin)<commit_msg>make is_enabled list editable<commit_after>from django.contrib import admin
from doormat.models import DoorMat

class DoorMatAdmin(admin.ModelAdmin):
    list_display = ('domain', 'path', 'is_enabled', 'last_published')
    list_display_links = ('domain', 'path')
    list_editable = ('is_enabled',)
    list_filter = ('is_enabled', 'domain')
    readonly_fields = ('last_published',)
    search_fields = ('domain', 'path', 'content')
admin.site.register(DoorMat, DoorMatAdmin)<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django.conf.urls.defaults import url, patterns
from django.contrib import admin
from django.core.urlresolvers import reverse
from thecut.googleanalytics import views
from thecut.googleanalytics.forms import GDataProfileForm, ProfileForm
from thecut.googleanalytics.models import Profile

class ProfileAdmin(admin.ModelAdmin):
    #form = ProfileForm
    list_display = ['__unicode__', 'web_property_id', 'profile_id', 'is_enabled', 'linked']

    def linked(self, obj):
        if obj.use_gdata():
            label = 'Linked (unlink)'
        else:
            label = 'Unlinked (link)'
        return label %(obj.pk)
    linked.allow_tags = True
    linked.short_description = 'Analytics API'

    def get_form(self, request, obj=None, **kwargs):
        if obj and obj.use_gdata():
            self.form = GDataProfileForm
        else:
            self.form = ProfileForm
        return super(ProfileAdmin, self).get_form(request, obj=None, **kwargs)

    def get_urls(self):
        urlpatterns = patterns('thecut.googleanalytics.views',
            url(r'^oauth2/request/(?P<pk>\d+)$',
                views.OAuth2RequestTokenView.as_view(),
                name='oauth2_request_token'),
            url(r'^oauth2/callback$',
                views.OAuth2CallbackView.as_view(),
                name='oauth2_callback'),
            url(r'^oauth2/revoke/(?P<pk>\d+)$',
                views.OAuth2RevokeTokenView.as_view(admin=self),
                name='oauth2_revoke_token'),
        )
        urlpatterns += super(ProfileAdmin, self).get_urls()
        return urlpatterns

admin.site.register(Profile, ProfileAdmin)

<commit_msg>Show 'disabled' if settings.USE_GDATA is false.<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django.conf.urls.defaults import url, patterns
from django.contrib import admin
from django.core.urlresolvers import reverse
from thecut.googleanalytics import settings, views
from thecut.googleanalytics.forms import GDataProfileForm, ProfileForm
from thecut.googleanalytics.models import Profile

class ProfileAdmin(admin.ModelAdmin):
    list_display = ['__unicode__', 'web_property_id', 'profile_id',
        'is_enabled', 'linked']

    def linked(self, obj):
        if settings.USE_GDATA:
            if obj.use_gdata():
                label = 'Linked (unlink)'
            else:
                label = 'Unlinked (link)'
            return label %(obj.pk)
        else:
            return 'Disabled'
    linked.allow_tags = True
    linked.short_description = 'Analytics API'

    def get_form(self, request, obj=None, **kwargs):
        if obj and obj.use_gdata():
            self.form = GDataProfileForm
        else:
            self.form = ProfileForm
        return super(ProfileAdmin, self).get_form(request, obj=None, **kwargs)

    def get_urls(self):
        urlpatterns = patterns('thecut.googleanalytics.views',
            url(r'^oauth2/request/(?P<pk>\d+)$',
                views.OAuth2RequestTokenView.as_view(),
                name='oauth2_request_token'),
            url(r'^oauth2/callback$',
                views.OAuth2CallbackView.as_view(),
                name='oauth2_callback'),
            url(r'^oauth2/revoke/(?P<pk>\d+)$',
                views.OAuth2RevokeTokenView.as_view(admin=self),
                name='oauth2_revoke_token'),
        )
        urlpatterns += super(ProfileAdmin, self).get_urls()
        return urlpatterns

admin.site.register(Profile, ProfileAdmin)

<|endoftext|>


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

# import funkcí z jiného adresáře
import sys
import os.path
import copy

path_to_script = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(path_to_script, "../extern/pyseg_base/src/"))
#sys.path.append(os.path.join(path_to_script, "../extern/py3DSeedEditor/"))
#sys.path.append(os.path.join(path_to_script, "../src/"))
import unittest

from PyQt4.QtGui import QFileDialog, QApplication, QMainWindow

import numpy as np
import itk
import SimpleITK as sitk

import pycut

#
import dcmreaddata as dcmr
import seed_editor_qt

print sys.argv
class PycutTest(unittest.TestCase):
    interactivetTest = False
    #interactivetTest = True

    def generate_data(self, shp=[128,128,128] ):
        """ Generating random data with cubic object inside"""

        data = np.zeros(shp)
        data [30:35,40:47,20:80] = 1
        data [28:34, 40:90, 50:60] = 1
        data [20:65, 40:45, 40:48] = 1

# inserting box

        #x_noisy = x + np.random.normal(0, 0.6, size=x.shape)
        return data

    def test_segmentation_with_boundary_penalties(self):
        data = generate_data()
        dataskel_itk = sitk.GetImageFromArray(data)

        itk.BinaryThinningImageFilter
        import pdb; pdb.set_trace()

:
        pixelType = itk.UC
        imageType = itk.Image[pixelType, 3]
        readerType = itk.BinaryThinningImageFilter[imageType]
        writerType = itk.ImageFileWriter[imageType]
        reader = readerType.New()
        writer = writerType.New()
        reader.SetFileName( argv[1] )
        writer.SetFileName( argv[2] )
        writer.SetInput( reader.GetOutput() )
        writer.Update()

    @unittest.skipIf(not interactivetTest, 'interactiveTest')
    def test_se(self):
        pass

if __name__ == "__main__":
    unittest.main()
<commit_msg>thinning itk, not working, only 2D algorithm<commit_after>#! /usr/bin/python
# -*- coding: utf-8 -*-

# import funkcí z jiného adresáře
import sys
import os.path
import copy

path_to_script = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(path_to_script, "../extern/pyseg_base/src/"))
#sys.path.append(os.path.join(path_to_script, "../extern/py3DSeedEditor/"))
#sys.path.append(os.path.join(path_to_script, "../src/"))
import unittest

from PyQt4.QtGui import QFileDialog, QApplication, QMainWindow

import numpy as np
import itk
import SimpleITK as sitk

import pycut

#
import dcmreaddata as dcmr
import seed_editor_qt

print sys.argv
class PycutTest(unittest.TestCase):
    interactivetTest = False
    #interactivetTest = True

    def generate_data(self, shp=[100,101,102] ):
        """ Generating random data with cubic object inside"""

        data = np.zeros(shp)
        data [30:35,40:47,20:80] = 1
        data [28:34, 40:90, 50:60] = 1
        data [20:65, 40:45, 40:48] = 1

# inserting box

        #x_noisy = x + np.random.normal(0, 0.6, size=x.shape)
        return data

    def test_simple_itk(self):
        import SimpleITK as sitk
        data = self.generate_data()
        data_itk = sitk.GetImageFromArray(data)
        output = sitk.BinaryThinning(data_itk)

    def test_itk_skeleton_3d(self):
        """
        This is not working. ITK has 2D thinning algorithm only. :-(
        """
        data = self.generate_data()

        data = data.astype(np.int16)

        #data_itk = sitk.GetImageFromArray(data)
        #shape = data_itk.GetSize()
        shape = data.shape#[::-1]

        imT = itk.Image.SS3
        im = imT.New(Regions=shape)
        im.Allocate()

        import pdb; pdb.set_trace()
        for i in range(0,shape[0]):
            for j in range(0,shape[1]):
                for k in range(0,shape[2]):
                    pixel = int(data[i,j,k])
                    #print type(pixel)
                    im.SetPixel([i,j,k], pixel)

        #castFilter = itk.CastImageFilter[itk.Image[itk.F,3], itk.Image[itk.SS,3]]

# skeletonization

        pixelType = itk.SS
        imageType = itk.Image[pixelType, 3]
        thinningFilterType = itk.BinaryThinningImageFilter[imageType, imageType]
        thinningFilter = thinningFilterType.New()
        thinningFilter.New()
        thinningFilter.SetInput(im)
        thinningFilter.Update()
        skelim = thinningFilter.GetOutput()

        import pdb; pdb.set_trace()

        dataout = np.zeros(shape, dtype=np.int8)
        for i in range(0,shape[0]):
            for j in range(0,shape[1]):
                for k in range(0,shape[2]):
                    pixel = skelim.GetPixel([i,j,k])
                    dataout[i,j,k] = pixel

        import pdb; pdb.set_trace()
        data_itk = sitk.GetImageFromArray(dataout)

        import pdb; pdb.set_trace()
        writerType = itk.ImageFileWriter[imageType, imageType]

        reader = readerType.New()
        writer = writerType.New()
        reader.SetFileName( argv[1] )
        writer.SetFileName( argv[2] )
        writer.SetInput( reader.GetOutput() )
        writer.Update()

    @unittest.skipIf(not interactivetTest, 'interactiveTest')
    def test_se(self):
        pass

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


<commit_before>import numpy as np
import nibabel as nib
from numpy.testing import (assert_equal, run_module_suite,
                           assert_array_almost_equal)
from dipy.data import get_data
from dipy.tracking.streamline import Streamlines
from dipy.align.streamlinear import whole_brain_slr, slr_with_qbx
from dipy.tracking.distances import bundles_distances_mam
from dipy.align.streamlinear import transform_streamlines
from dipy.align.streamlinear import compose_matrix44, decompose_matrix44

def test_whole_brain_slr():
    streams, hdr = nib.trackvis.read(get_data('fornix'))
    fornix = [s[0] for s in streams]

    f = Streamlines(fornix)
    f1 = f.copy()
    f2 = f.copy()

    # check translation
    f2._data += np.array([50, 0, 0])
    old_f2 = f2.copy()

    moved, transform, qb_centroids1, qb_centroids2 = whole_brain_slr(
            f1, f2, verbose=True, rm_small_clusters=1, greater_than=0,
            less_than=np.inf, qbx_thr=[40, 30, 20, 15], progressive=False)

    print("transform = ", transform)
    # we can check the quality of registration by comparing the matrices
    # MAM streamline distances before and after SLR
    D12 = bundles_distances_mam(f1, old_f2)
    D1M = bundles_distances_mam(f1, f2)

    d12_minsum = np.sum(np.min(D12, axis=0))
    d1m_minsum = np.sum(np.min(D1M, axis=0))

    print("distances= ", d12_minsum, " ", d1m_minsum)

    assert_equal(d1m_minsum < d12_minsum, True)

    # assert_array_almost_equal(transform[:3, 3], [-50, -0, -0], 3)

    # check rotation
    '''
    mat = compose_matrix44([0, 0, 0, 15, 0, 0])

    f3 = f.copy()
    old_f3 = f3.copy()
    f3 = transform_streamlines(f3, mat)

    moved, transform, qb_centroids1, qb_centroids2 = slr_with_qbx(
            f1, old_f3, verbose=False, rm_small_clusters=0, greater_than=0,
            less_than=np.inf, qbx_thr=[40, 30, 20, 15], progressive=True)

    # we can also check the quality by looking at the decomposed transform

    # assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2)

    moved, transform, qb_centroids1, qb_centroids2 = slr_with_qbx(
            f1, f3, verbose=False, rm_small_clusters=0, select_random=400,
            greater_than=0,
            less_than=np.inf, qbx_thr=[40, 30, 20, 15], progressive=True)

    # we can also check the quality by looking at the decomposed transform

    # assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2)
'''

if __name__ == '__main__':
    run_module_suite()
<commit_msg>Updated test_whole_brain_slr needs work<commit_after>import numpy as np
import nibabel as nib
from numpy.testing import (assert_equal, run_module_suite,
                           assert_array_almost_equal)
from dipy.data import get_data
from dipy.tracking.streamline import Streamlines
from dipy.align.streamlinear import whole_brain_slr, slr_with_qbx
from dipy.tracking.distances import bundles_distances_mam
from dipy.align.streamlinear import transform_streamlines
from dipy.align.streamlinear import compose_matrix44, decompose_matrix44

def test_whole_brain_slr():
    streams, hdr = nib.trackvis.read(get_data('fornix'))
    fornix = [s[0] for s in streams]

    f = Streamlines(fornix)
    f1 = f.copy()
    f2 = f.copy()

    # check translation
    f2._data += np.array([50, 0, 0])
    # old_f2 = f2.copy()

#    from dipy.viz import actor, window
#
#    ren = window.Renderer()
#    ren.add(actor.line(f1, colors=(1, 0, 0)))
#    ren.add(actor.line(f2, colors=(0, 1, 0)))
#    window.show(ren)

    moved, transform, qb_centroids1, qb_centroids2 = whole_brain_slr(
            f1, f2, x0='affine', verbose=True, rm_small_clusters=1,
            greater_than=0, less_than=np.inf,
            qbx_thr=[40, 30, 20, 15, 5, 1], progressive=False)

#    ren = window.Renderer()
#    ren.add(actor.line(f1, colors=(1, 0, 0)))
#    ren.add(actor.line(moved, colors=(0, 1, 0)))
#    #ren.add(actor.line(moved, colors=(0, 0, 1)))
#    window.show(ren)

    print("transform = ", transform)
    # we can check the quality of registration by comparing the matrices
    # MAM streamline distances before and after SLR
    D12 = bundles_distances_mam(f1, f2)
    D1M = bundles_distances_mam(f1, moved)

    d12_minsum = np.sum(np.min(D12, axis=0))
    d1m_minsum = np.sum(np.min(D1M, axis=0))

    print("distances= ", d12_minsum, " ", d1m_minsum)

    assert_equal(d1m_minsum < d12_minsum, True)

    assert_array_almost_equal(transform[:3, 3], [-50, -0, -0], 3)

    # check rotation

    mat = compose_matrix44([0, 0, 0, 15, 0, 0])

    f3 = f.copy()
    old_f3 = f3.copy()
    f3 = transform_streamlines(f3, mat)

    moved, transform, qb_centroids1, qb_centroids2 = slr_with_qbx(
            f1, old_f3, verbose=False, rm_small_clusters=0, greater_than=0,
            less_than=np.inf, qbx_thr=[40, 30, 20, 15, 5, 1],
            progressive=True)

    # we can also check the quality by looking at the decomposed transform

    assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2)

    moved, transform, qb_centroids1, qb_centroids2 = slr_with_qbx(
            f1, f3, verbose=False, rm_small_clusters=0, select_random=400,
            greater_than=0,
            less_than=np.inf, qbx_thr=[40, 30, 20, 15, 5, 1],
            progressive=True)

    # we can also check the quality by looking at the decomposed transform

    assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2)

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


<commit_before>"""
Usage - python pyentropy.py -h
Output - A CSV file of the format (without newlines):
         <valid-after>,
         <entropy for all nodes>,
         <max entropy for all nodes>,
         <entropy for exit nodes>,
         <max entropy for exit nodes>,
         <entropy for guard nodes>,
         <max entropy for guard nodes>,
         <entropy for countries>,
         <max entropy for countries>,
         <entropy for AS>,
         <max entropy for AS>
rsync -arz --delete metrics.torproject.org::metrics-recent/relay-descriptors/consensuses in
"""

import sys
import math
import os
import pygeoip
import StringIO
import stem.descriptor

from optparse import OptionParser
from binascii import b2a_hex, a2b_base64, a2b_hex
from stem.descriptor.server_descriptor import RelayDescriptor, BridgeDescriptor

class Router:
    def __init__(self):
        self.bandwidth = None
        self.advertised_bw = None
        self.country = None
        self.as_no = None
        self.is_exit = None
        self.is_guard = None

    def add_router_info(self, values):
           hex_digest = b2a_hex(a2b_base64(values[2]+"="))
           self.advertised_bw = self.get_advertised_bw(hex_digest)
           ip = values[5]
           self.country = gi_db.country_code_by_addr(ip)
           self.as_no = self.get_as_details(ip)

    def add_weights(self, values):
           self.bandwidth = int(values[0].split('=')[1])

    def add_flags(self, values):
           if "Exit" in values and not "BadExit" in values:
               self.is_exit = True
           if "Guard" in values:
               self.is_guard = True

    def get_as_details(self, ip):
        try:
            value = as_db.org_by_addr(str(ip)).split()
            return value[0]
        except:
            return None

    def get_advertised_bw(self, hex_digest):
        try:
            with open(options.server_desc+hex_digest) as f:
                data = f.read()

            desc_iter = stem.descriptor.server_descriptor.parse_file(StringIO.StringIO(data))
            desc_entries = list(desc_iter)
            desc = desc_entries[0]
            return min(desc.average_bandwidth, desc.burst_bandwidth, desc.observed_bandwidth)
        except:
            return 0

def parse_bw_weights(values):
    data = {}
    try:
        for value in values:
            key, value = value.split("=")
            data[key] = float(value) / 10000
        return data
    except:
        return None

def run(file_name):
    routers = []
    router = None
    Wed, Wee, Wgd, Wgg = 1, 1, 1, 1
    # parse consensus
    with open(file_name, 'r') as f:
        for line in f.readlines():
            key = line.split()[0]
            values = line.split()[1:]
            if key =='r':
                router = Router()
                routers.append(router)
                router.add_router_info(values)
            elif key == 's':
                router.add_flags(values)
            elif key == 'w':
                router.add_weights(values)
            elif key == 'valid-after':
                valid_after = ' '.join(values)
            elif key == 'bandwidth-weights':
                data = parse_bw_weights(values)
                try:
                    Wed = data['Wed']
                    Wee = data['Wee']
                    Wgd = data['Wgd']
                    Wgg = data['Wgg']
                except:
                    pass

    if len(routers) <= 0:
        return

    total_bw, total_exit_bw, total_guard_bw = 0, 0, 0
    guards_no, exits_no = 0, 0
    bw_countries, bw_as = {}, {}
    for router in routers:
        total_bw += router.bandwidth
        if router.is_guard and router.is_exit:
            total_guard_bw += Wgd*router.bandwidth
            total_exit_bw += Wed*router.bandwidth
            guards_no += 1
            exits_no += 1
        elif router.is_guard:
            total_guard_bw += Wgg*router.bandwidth
            guards_no += 1
        elif router.is_exit:
            total_exit_bw += Wee*router.bandwidth
            exits_no += 1
        if bw_countries.has_key(router.country):
            bw_countries[router.country] += router.bandwidth
        else:
            bw_countries[router.country] = router.bandwidth
        if router.as_no:
            if bw_as.has_key(router.as_no):
                bw_as[router.as_no] += router.bandwidth
            else:
                bw_as[router.as_no] = router.bandwidth

    entropy, entropy_exit, entropy_guard, entropy_country, entropy_as = 0.0, 0.0, 0.0, 0.0, 0.0
    for router in routers:
        p = float(router.bandwidth) / float(total_bw)
        if p != 0:
            entropy += -(p * math.log(p, 2))
        if router.is_guard and router.is_exit:
            p = float(Wgd*router.bandwidth) / float(total_guard_bw)
            if p != 0:
                entropy_guard += -(p * math.log(p, 2))
            p = float(Wed*router.bandwidth) / float(total_exit_bw)
            if p != 0:
                entropy_exit += -(p * math.log(p, 2))
        elif router.is_guard:
            p = float(Wgg*router.bandwidth) / float(total_guard_bw)
            if p != 0:
                entropy_guard += -(p * math.log(p, 2))
        elif router.is_exit:
            p = float(Wee*router.bandwidth) / float(total_exit_bw)
            if p != 0:
                entropy_exit += -(p * math.log(p, 2))

    for country in bw_countries.iterkeys():
        p = float(bw_countries[country]) / float(total_bw)
        if p != 0:
            entropy_country += -(p * math.log(p, 2))

    for as_no in bw_as.iterkeys():
        p = float(bw_as[as_no]) / float(total_bw)
        if p !=0:
            entropy_as += -(p * math.log(p, 2))

    # Entropy of uniform distribution of 'n' possible values: log(n)
    max_entropy = math.log(len(routers), 2)
    max_entropy_guard = math.log(guards_no, 2)
    max_entropy_exit = math.log(exits_no, 2)
    max_entropy_country = math.log(len(bw_countries), 2)
    max_entropy_as = math.log(len(bw_as), 2)

    return ",".join([valid_after,
                     str(entropy),
                     str(max_entropy),
                     str(entropy_exit),
                     str(max_entropy_exit),
                     str(entropy_guard),
                     str(max_entropy_guard),
                     str(entropy_country),
                     str(max_entropy_country),
                     str(entropy_as),
                     str(max_entropy_as)])

def parse_args():
    usage = "Usage - python pyentropy.py [options]"
    parser = OptionParser(usage)

    parser.add_option("-g", "--geoip", dest="gi_db", default="GeoIP.dat",
                      help="Input GeoIP database")
    parser.add_option("-a", "--as", dest="as_db", default="GeoIPASNum.dat",
                      help="Input AS GeoIP database")
    parser.add_option("-s", "--server_desc", dest="server_desc",
                      default="data/relay-descriptors/server-descriptors/", help="Server descriptors directory")
    parser.add_option("-o", "--output", dest="output", default="entropy.csv",
                      help="Output filename")
    parser.add_option("-c", "--consensus", dest="consensus", default="in/consensus",
                      help="Input consensus dir")

    (options, args) = parser.parse_args()

    return options

if __name__ == "__main__":
    options = parse_args()
    gi_db = pygeoip.GeoIP(options.gi_db)
    as_db = pygeoip.GeoIP(options.as_db)

    with open(options.output, 'w') as f:
        for file_name in os.listdir(options.consensus):
            string = run(os.path.join(options.consensus, file_name))
            if string:
                f.write("%s\n" % (string))
<commit_msg>Treat unresolved AS numbers correctly (#6232).<commit_after>"""
Usage - python pyentropy.py -h
Output - A CSV file of the format (without newlines):
         <valid-after>,
         <entropy for all nodes>,
         <max entropy for all nodes>,
         <entropy for exit nodes>,
         <max entropy for exit nodes>,
         <entropy for guard nodes>,
         <max entropy for guard nodes>,
         <entropy for countries>,
         <max entropy for countries>,
         <entropy for AS>,
         <max entropy for AS>
rsync -arz --delete metrics.torproject.org::metrics-recent/relay-descriptors/consensuses in
"""

import sys
import math
import os
import pygeoip
import StringIO
import stem.descriptor

from optparse import OptionParser
from binascii import b2a_hex, a2b_base64, a2b_hex
from stem.descriptor.server_descriptor import RelayDescriptor, BridgeDescriptor

class Router:
    def __init__(self):
        self.bandwidth = None
        self.advertised_bw = None
        self.country = None
        self.as_no = None
        self.is_exit = None
        self.is_guard = None

    def add_router_info(self, values):
           hex_digest = b2a_hex(a2b_base64(values[2]+"="))
           self.advertised_bw = self.get_advertised_bw(hex_digest)
           ip = values[5]
           self.country = gi_db.country_code_by_addr(ip)
           self.as_no = self.get_as_details(ip)

    def add_weights(self, values):
           self.bandwidth = int(values[0].split('=')[1])

    def add_flags(self, values):
           if "Exit" in values and not "BadExit" in values:
               self.is_exit = True
           if "Guard" in values:
               self.is_guard = True

    def get_as_details(self, ip):
        try:
            value = as_db.org_by_addr(str(ip)).split()
            return value[0]
        except:
            return ""

    def get_advertised_bw(self, hex_digest):
        try:
            with open(options.server_desc+hex_digest) as f:
                data = f.read()

            desc_iter = stem.descriptor.server_descriptor.parse_file(StringIO.StringIO(data))
            desc_entries = list(desc_iter)
            desc = desc_entries[0]
            return min(desc.average_bandwidth, desc.burst_bandwidth, desc.observed_bandwidth)
        except:
            return 0

def parse_bw_weights(values):
    data = {}
    try:
        for value in values:
            key, value = value.split("=")
            data[key] = float(value) / 10000
        return data
    except:
        return None

def run(file_name):
    routers = []
    router = None
    Wed, Wee, Wgd, Wgg = 1, 1, 1, 1
    # parse consensus
    with open(file_name, 'r') as f:
        for line in f.readlines():
            key = line.split()[0]
            values = line.split()[1:]
            if key =='r':
                router = Router()
                routers.append(router)
                router.add_router_info(values)
            elif key == 's':
                router.add_flags(values)
            elif key == 'w':
                router.add_weights(values)
            elif key == 'valid-after':
                valid_after = ' '.join(values)
            elif key == 'bandwidth-weights':
                data = parse_bw_weights(values)
                try:
                    Wed = data['Wed']
                    Wee = data['Wee']
                    Wgd = data['Wgd']
                    Wgg = data['Wgg']
                except:
                    pass

    if len(routers) <= 0:
        return

    total_bw, total_exit_bw, total_guard_bw = 0, 0, 0
    guards_no, exits_no = 0, 0
    bw_countries, bw_as = {}, {}
    for router in routers:
        total_bw += router.bandwidth
        if router.is_guard and router.is_exit:
            total_guard_bw += Wgd*router.bandwidth
            total_exit_bw += Wed*router.bandwidth
            guards_no += 1
            exits_no += 1
        elif router.is_guard:
            total_guard_bw += Wgg*router.bandwidth
            guards_no += 1
        elif router.is_exit:
            total_exit_bw += Wee*router.bandwidth
            exits_no += 1
        if bw_countries.has_key(router.country):
            bw_countries[router.country] += router.bandwidth
        else:
            bw_countries[router.country] = router.bandwidth
        if bw_as.has_key(router.as_no):
            bw_as[router.as_no] += router.bandwidth
        else:
            bw_as[router.as_no] = router.bandwidth

    entropy, entropy_exit, entropy_guard, entropy_country, entropy_as = 0.0, 0.0, 0.0, 0.0, 0.0
    for router in routers:
        p = float(router.bandwidth) / float(total_bw)
        if p != 0:
            entropy += -(p * math.log(p, 2))
        if router.is_guard and router.is_exit:
            p = float(Wgd*router.bandwidth) / float(total_guard_bw)
            if p != 0:
                entropy_guard += -(p * math.log(p, 2))
            p = float(Wed*router.bandwidth) / float(total_exit_bw)
            if p != 0:
                entropy_exit += -(p * math.log(p, 2))
        elif router.is_guard:
            p = float(Wgg*router.bandwidth) / float(total_guard_bw)
            if p != 0:
                entropy_guard += -(p * math.log(p, 2))
        elif router.is_exit:
            p = float(Wee*router.bandwidth) / float(total_exit_bw)
            if p != 0:
                entropy_exit += -(p * math.log(p, 2))

    for country in bw_countries.iterkeys():
        p = float(bw_countries[country]) / float(total_bw)
        if p != 0:
            entropy_country += -(p * math.log(p, 2))

    for as_no in bw_as.iterkeys():
        p = float(bw_as[as_no]) / float(total_bw)
        if p !=0:
            entropy_as += -(p * math.log(p, 2))

    # Entropy of uniform distribution of 'n' possible values: log(n)
    max_entropy = math.log(len(routers), 2)
    max_entropy_guard = math.log(guards_no, 2)
    max_entropy_exit = math.log(exits_no, 2)
    max_entropy_country = math.log(len(bw_countries), 2)
    max_entropy_as = math.log(len(bw_as), 2)

    return ",".join([valid_after,
                     str(entropy),
                     str(max_entropy),
                     str(entropy_exit),
                     str(max_entropy_exit),
                     str(entropy_guard),
                     str(max_entropy_guard),
                     str(entropy_country),
                     str(max_entropy_country),
                     str(entropy_as),
                     str(max_entropy_as)])

def parse_args():
    usage = "Usage - python pyentropy.py [options]"
    parser = OptionParser(usage)

    parser.add_option("-g", "--geoip", dest="gi_db", default="GeoIP.dat",
                      help="Input GeoIP database")
    parser.add_option("-a", "--as", dest="as_db", default="GeoIPASNum.dat",
                      help="Input AS GeoIP database")
    parser.add_option("-s", "--server_desc", dest="server_desc",
                      default="data/relay-descriptors/server-descriptors/", help="Server descriptors directory")
    parser.add_option("-o", "--output", dest="output", default="entropy.csv",
                      help="Output filename")
    parser.add_option("-c", "--consensus", dest="consensus", default="in/consensus",
                      help="Input consensus dir")

    (options, args) = parser.parse_args()

    return options

if __name__ == "__main__":
    options = parse_args()
    gi_db = pygeoip.GeoIP(options.gi_db)
    as_db = pygeoip.GeoIP(options.as_db)

    with open(options.output, 'w') as f:
        for file_name in os.listdir(options.consensus):
            string = run(os.path.join(options.consensus, file_name))
            if string:
                f.write("%s\n" % (string))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django import forms
from thecut.googleanalytics.models import Profile

class ProfileAdminForm(forms.ModelForm):

    class Meta(object):
        model = Profile
        fields = ['site', 'web_property_id', 'display_features', 'is_enabled']

    def __init__(self, *args, **kwargs):
        super(ProfileAdminForm, self).__init__(*args, **kwargs)

        if 'web_property_id' in self.fields:
            self.fields['web_property_id'].widget.attrs.update(
                {'placeholder': 'UA-xxxxxxxx-x'})

class GoogleAPIProfileAdminForm(ProfileAdminForm):

    class Meta(ProfileAdminForm.Meta):
        fields = ['site', 'profile_id', 'display_features', 'is_enabled']

    def __init__(self, *args, **kwargs):
        super(GoogleAPIProfileAdminForm, self).__init__(*args, **kwargs)
        self.profiles = {}

        api_response = self.get_profiles_list()

        for profile in api_response.get('items', []):
            self.profiles.update({profile['id']: profile})

        self.fields['profile_id'].label = 'Profile'
        self.fields['profile_id'].help_text = 'Google Analytics profiles ' \
            'for {0}'.format(api_response['username'])
        self.fields['profile_id'].required = True
        self.fields['profile_id'].widget = forms.Select(
            choices=self.get_profile_choices())

    def get_profiles_list(self):
        service = self.instance.get_analytics_google_api_client()
        request = service.management().profiles().list(
            webPropertyId='~all', accountId='~all',
            fields='items(id,name,webPropertyId),username')
        return request.execute()

    def get_profile_choices(self):
        return [(profile['id'], profile['name']) for profile in
                self.profiles.values()]

    def save(self, *args, **kwargs):
        profile = self.profiles[self.cleaned_data['profile_id']]
        self.instance.web_property_id = profile['webPropertyId']
        return super(GoogleAPIProfileAdminForm, self).save(*args, **kwargs)
<commit_msg>Updated GoogleAPIProfileAdminForm to use new account summaries api endpoint. Refs #2273.<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django import forms
from django.utils.datastructures import SortedDict
from thecut.googleanalytics.models import Profile

class ProfileAdminForm(forms.ModelForm):

    class Meta(object):
        model = Profile
        fields = ['site', 'web_property_id', 'display_features', 'is_enabled']

    def __init__(self, *args, **kwargs):
        super(ProfileAdminForm, self).__init__(*args, **kwargs)

        if 'web_property_id' in self.fields:
            self.fields['web_property_id'].widget.attrs.update(
                {'placeholder': 'UA-xxxxxxxx-x'})

class GoogleAPIProfileAdminForm(ProfileAdminForm):

    class Meta(ProfileAdminForm.Meta):
        fields = ['site', 'profile_id', 'display_features', 'is_enabled']

    def __init__(self, *args, **kwargs):
        super(GoogleAPIProfileAdminForm, self).__init__(*args, **kwargs)
        self.fields['profile_id'].label = 'View (profile)'
        analytics_username = self.get_account_summaries()['username']
        self.fields['profile_id'].help_text = 'Google Analytics views ' \
            '(profiles) for {0}'.format(analytics_username)
        self.fields['profile_id'].required = True
        self.fields['profile_id'].widget = forms.Select(
            choices=self.get_profile_choices())

    def get_profiles_list(self):
        service = self.instance.get_analytics_google_api_client()
        request = service.management().profiles().list(
            webPropertyId='~all', accountId='~all',
            fields='items(id,name,webPropertyId),username')
        return request.execute()

    def get_account_summaries(self):
        # TODO: Pagination?
        if not hasattr(self, '_account_summaries'):
            service = self.instance.get_analytics_google_api_client()
            request = service.management().accountSummaries().list()
            self._account_summaries = request.execute()
        return self._account_summaries

    def get_profiles(self):
        if not hasattr(self, '_profiles'):
            self._profiles = SortedDict()
            for account in self.get_account_summaries()['items']:
                for web_property in account['webProperties']:
                    for profile in web_property['profiles']:
                        self._profiles.update({
                            profile['id']: {'profile': profile,
                                            'web_property': web_property,
                                            'account': account}})
        return self._profiles

    def get_profile_choices(self):
        choices = []
        for profile_id, data in self.get_profiles().items():
            label = ' | '.join([data['account']['name'],
                                data['web_property']['name'],
                                data['profile']['name']])
            choices += [(profile_id, label)]
        return choices

    def save(self, *args, **kwargs):
        profile = self.get_profiles()[self.cleaned_data['profile_id']]
        self.instance.web_property_id = profile['web_property']['id']
        return super(GoogleAPIProfileAdminForm, self).save(*args, **kwargs)
<|endoftext|>


<commit_before>import numpy as np
import nibabel as nib
from numpy.testing import (assert_equal, run_module_suite,
                           assert_array_almost_equal)
from dipy.data import get_data
from dipy.tracking.streamline import Streamlines
from dipy.align.streamlinear import whole_brain_slr, slr_with_qbx
from dipy.tracking.distances import bundles_distances_mam
from dipy.align.streamlinear import transform_streamlines
from dipy.align.streamlinear import compose_matrix44, decompose_matrix44

def test_whole_brain_slr():
    streams, hdr = nib.trackvis.read(get_data('fornix'))
    fornix = [s[0] for s in streams]

    f = Streamlines(fornix)
    f1 = f.copy()
    f2 = f.copy()

    # check translation
    f2._data += np.array([50, 0, 0])
    # old_f2 = f2.copy()

#    from dipy.viz import actor, window
#
#    ren = window.Renderer()
#    ren.add(actor.line(f1, colors=(1, 0, 0)))
#    ren.add(actor.line(f2, colors=(0, 1, 0)))
#    window.show(ren)

    moved, transform, qb_centroids1, qb_centroids2 = whole_brain_slr(
            f1, f2, x0='affine', verbose=True, rm_small_clusters=1,
            greater_than=0, less_than=np.inf,
            qbx_thr=[40, 30, 20, 15, 5, 1], progressive=False)

#    ren = window.Renderer()
#    ren.add(actor.line(f1, colors=(1, 0, 0)))
#    ren.add(actor.line(moved, colors=(0, 1, 0)))
#    #ren.add(actor.line(moved, colors=(0, 0, 1)))
#    window.show(ren)

    print("transform = ", transform)
    # we can check the quality of registration by comparing the matrices
    # MAM streamline distances before and after SLR
    D12 = bundles_distances_mam(f1, f2)
    D1M = bundles_distances_mam(f1, moved)

    d12_minsum = np.sum(np.min(D12, axis=0))
    d1m_minsum = np.sum(np.min(D1M, axis=0))

    print("distances= ", d12_minsum, " ", d1m_minsum)

    assert_equal(d1m_minsum < d12_minsum, True)

    assert_array_almost_equal(transform[:3, 3], [-50, -0, -0], 3)

    # check rotation

    mat = compose_matrix44([0, 0, 0, 15, 0, 0])

    f3 = f.copy()
    old_f3 = f3.copy()
    f3 = transform_streamlines(f3, mat)

    moved, transform, qb_centroids1, qb_centroids2 = slr_with_qbx(
            f1, old_f3, verbose=False, rm_small_clusters=0, greater_than=0,
            less_than=np.inf, qbx_thr=[40, 30, 20, 15, 5, 1],
            progressive=True)

    # we can also check the quality by looking at the decomposed transform

    assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2)

    moved, transform, qb_centroids1, qb_centroids2 = slr_with_qbx(
            f1, f3, verbose=False, rm_small_clusters=0, select_random=400,
            greater_than=0,
            less_than=np.inf, qbx_thr=[40, 30, 20, 15, 5, 1],
            progressive=True)

    # we can also check the quality by looking at the decomposed transform

    assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2)

if __name__ == '__main__':
    run_module_suite()
<commit_msg>Updated SLR test<commit_after>import numpy as np
import nibabel as nib
from numpy.testing import (assert_equal, run_module_suite,
                           assert_array_almost_equal)
from dipy.data import get_data
from dipy.tracking.streamline import Streamlines
from dipy.align.streamlinear import whole_brain_slr, slr_with_qbx
from dipy.tracking.distances import bundles_distances_mam
from dipy.align.streamlinear import transform_streamlines
from dipy.align.streamlinear import compose_matrix44, decompose_matrix44

def test_whole_brain_slr():
    streams, hdr = nib.trackvis.read(get_data('fornix'))
    fornix = [s[0] for s in streams]

    f = Streamlines(fornix)
    f1 = f.copy()
    f2 = f.copy()

    # check translation
    f2._data += np.array([50, 0, 0])
    # old_f2 = f2.copy()

#    from dipy.viz import actor, window
#
#    ren = window.Renderer()
#    ren.add(actor.line(f1, colors=(1, 0, 0)))
#    ren.add(actor.line(f2, colors=(0, 1, 0)))
#    window.show(ren)

    moved, transform, qb_centroids1, qb_centroids2 = whole_brain_slr(
            f1, f2, x0='affine', verbose=True, rm_small_clusters=1,
            greater_than=0, less_than=np.inf,
            qbx_thr=[5, 2], progressive=False)

#    ren = window.Renderer()
#    ren.add(actor.line(f1, colors=(1, 0, 0)))
#    ren.add(actor.line(moved, colors=(0, 1, 0)))
#    #ren.add(actor.line(moved, colors=(0, 0, 1)))
#    window.show(ren)

    #print("transform = ", transform)
    # we can check the quality of registration by comparing the matrices
    # MAM streamline distances before and after SLR
    D12 = bundles_distances_mam(f1, f2)
    D1M = bundles_distances_mam(f1, moved)

    d12_minsum = np.sum(np.min(D12, axis=0))
    d1m_minsum = np.sum(np.min(D1M, axis=0))

    print("distances= ", d12_minsum, " ", d1m_minsum)

    assert_equal(d1m_minsum < d12_minsum, True)

    assert_array_almost_equal(transform[:3, 3], [-50, -0, -0], 3)

    # check rotation

    mat = compose_matrix44([0, 0, 0, 15, 0, 0])

    f3 = f.copy()
    f3 = transform_streamlines(f3, mat)

    moved, transform, qb_centroids1, qb_centroids2 = slr_with_qbx(
            f1, f3, verbose=False, rm_small_clusters=0, greater_than=0,
            less_than=np.inf, qbx_thr=[5, 2],
            progressive=True)

    # we can also check the quality by looking at the decomposed transform

    assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2)

    moved, transform, qb_centroids1, qb_centroids2 = slr_with_qbx(
            f1, f3, verbose=False, rm_small_clusters=0, select_random=400,
            greater_than=0,
            less_than=np.inf, qbx_thr=[5, 2],
            progressive=True)

    # we can also check the quality by looking at the decomposed transform

    assert_array_almost_equal(decompose_matrix44(transform)[3], -15, 2)

if __name__ == '__main__':
    # run_module_suite()
    test_whole_brain_slr()
<|endoftext|>


<commit_before># Download the Python helper library from twilio.com/docs/python/install
import json
from urllib.request import urlopen

from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
api_key_sid = = 'SKXXXX'
api_key_secret = 'your_api_key_secret'
client = Client(api_key_sid, api_key_secret)

composition_sid = 'CXXXX'
uri = 'https://video.twilio.com/v1/Compositions/{}/Media?Ttl=6000'.format(composition_sid)
response = client.request('GET', uri)
media_location = json.loads(response.text).get('redirect_to')

# For example, download the media to a local file
local_file = open('myFile.mp4', 'w')
local_file.write(urlopen(media_location).read())
<commit_msg>(fixed downloading file)<commit_after># Download the Python helper library from twilio.com/docs/python/install
import json
from urllib.request import urlopen

from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
api_key_sid = = 'SKXXXX'
api_key_secret = 'your_api_key_secret'
client = Client(api_key_sid, api_key_secret)

composition_sid = 'CXXXX'
uri = 'https://video.twilio.com/v1/Compositions/{}/Media?Ttl=6000'.format(composition_sid)
response = client.request('GET', uri)
media_location = json.loads(response.text).get('redirect_to')

# For example, get the data and download the media to a local file
data = urlopen(media_location).read()
local_file = 'myFile.mp4'
with open (local_file, 'wb') as f:
    f.write(data)

<|endoftext|>


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

# pylama:ignore=C901

import asyncio
import urwid
from numpy.linalg import inv
from numpy import dot, array, insert
from opentrons import robot, instruments
from opentrons.robot import robot_configs
from opentrons.util.calibration_functions import probe_instrument
from opentrons.cli.solve import solve

# TODO (andy - 2017/12/05) Run deck calibration (XYZ) seperately from Probe
#     After calibrating the Z axis and 3 points on deck, quit and restart
#     this CLI tool before running tip-probe ('p')
#     Without restarting, pipette collides with probe

# Distance increments for jog
steps = [0.1, 0.25, 0.5, 1, 5, 10, 20, 40, 80]
# Index of selected step
step_index = 2

SAFE_HEIGHT = 130

left = 'Z'
right = 'A'
current_pipette = right

status_text = urwid.Text('')
current_position = (0, 0, 0)

# 200uL tip. Used during calibration process
# The actual calibration represents end of a pipette
# without tip on
TIP_LENGTH = 51.7

# Reference point being calibrated
point_number = -1

# (0, 0) is in bottom-left corner
# Expected reference points
expected = [
    (108.75, 92.8),       # dimple 4
    (373.75, 92.8),      # dimple 6
    (241.25, 273.80)    # dimple 11
]

# Actuals get overridden when ENTER is pressed
actual = [(0, 0)] * 3

# Accessible through 1,2,3 ... keyboard keys to test
# calibration by moving to known locations
test_points = [(x, y, TIP_LENGTH) for x, y in expected] + \
    [
        (132.50, 90.50, TIP_LENGTH),        # Slot 5
        (332.13, 47.41, TIP_LENGTH),        # Corner of 3
        (190.65, 227.57, TIP_LENGTH),       # Corner of 8
        (330.14, 222.78, TIP_LENGTH),       # Corner of 9
    ]

T = robot.config.gantry_calibration
XY = None

# Add fixed Z offset which is known so we don't have to calibrate for height
# during calibration process
def add_z(XY):
    return insert(
        insert(XY, 2, [0, 0, 0], axis=1),
        2,
        [0, 0, 1, T[2][3]],
        axis=0)

def current_step():
    """
    Given current step index return step value in mm
    """
    return steps[step_index]

def position():
    """
    Read position from driver into a tuple and map 3-rd value
    to the axis of a pipette currently used
    """
    try:
        p = robot._driver.position
        res = (p['X'], p['Y'], p[current_pipette.upper()])
    except KeyError:
        # for some reason we are sometimes getting
        # key error in dict returned from driver
        pass
    else:
        return res

def status(text):
    """
    Refresh status in a text box
    """

    points = '\n'.join([
        # Highlight point being calibrated
        ('* ' if point_number == point else '') + "{0} {1}"
        # Display actual and expected coordinates
        .format(coord[0], coord[1])
        for point, coord in enumerate(zip(actual, expected))
    ])

    text = '\n'.join([
        points,
        'Smoothie: {0}'.format(current_position),
        'World: {0}'.format(tuple(dot(inv(T), list(current_position) + [1])[:-1])),  # NOQA
        'Step: {0}'.format(current_step()),
        'Current stage: ' + current_pipette,
        'Message: ' + text
    ])

    status_text.set_text(text)

def jog(axis, direction, step):
    global current_position
    robot._driver.move({axis: robot._driver.position[axis] + direction * step})
    current_position = position()
    status('Jog: ' + repr([axis, str(direction), str(step)]))

key_mappings = {
    'q': lambda: jog(current_pipette, +1, current_step()),
    'a': lambda: jog(current_pipette, -1, current_step()),
    'up': lambda: jog('Y', +1, current_step()),
    'down': lambda: jog('Y', -1, current_step()),
    'left': lambda: jog('X', -1, current_step()),
    'right': lambda: jog('X', +1, current_step()),
}

def key_pressed(key):
    global current_position, current_pipette, step_index, \
        point_number, XY, T

    if key == 'z':
        current_pipette = left if current_pipette == right else right
        status('current pipette axis: ' + repr(current_pipette))
    elif key.isnumeric():
        validate(int(key) - 1)
    # less travel
    elif key == '-':
        if step_index > 0:
            step_index -= 1
        status('step: ' + repr(current_step()))
    # more travel
    elif key == '=':
        if step_index < len(steps) - 1:
            step_index += 1
        status('step: ' + repr(current_step()))
    # skip calibration point
    elif key == 's':
        if (point_number < len(actual) - 1):
            point_number += 1
        status('skipped #{0}'.format(point_number))
    # run tip probe
    elif key == 'p':

        robot.reset()

        pipette = instruments.Pipette(mount='right', channels=1)
        probe_center = tuple(probe_instrument(
            pipette, robot, tip_length=TIP_LENGTH))
        robot.config = robot.config._replace(
            probe_center=probe_center
        )
        status('Tip probe')
    # save calibration point and move to next
    elif key == 'enter':
        if point_number >= len(actual):
            return

        if point_number < 0:
            actual_z = position()[-1]
            expected_z = T[2][3] + TIP_LENGTH
            T[2][3] += actual_z - expected_z
            point_number += 1
            status('saved Z-Offset: {0}'.format(T[2][3]))
            return

        actual[point_number] = position()[:-1]
        point_number += 1
        status('saved #{0}: {1}'.format(point_number, actual[point_number-1]))

        # On last point update gantry calibration
        if point_number == len(actual):
            XY = solve(expected, actual)
            T = add_z(XY)
            robot.config = robot.config._replace(
                    gantry_calibration=list(map(lambda i: list(i), T)),
                )
            status(str(robot.config))

    # move to previous calibration point
    elif key == 'backspace':
        if point_number > 0:
            point_number -= 1
        status('')
    # home
    elif key == '\\':
        robot.home()
        current_position = position()
        status('Homed')
    # calculate transformation matrix
    elif key == 'esc':
        raise urwid.ExitMainLoop
    elif key == ' ':
        try:
            diff = robot_configs.save(robot.config)
            status('Saved')
        except Exception as e:
            status(repr(e))
        else:
            status(str(diff))
    else:
        try:
            key_mappings[key]()
        except KeyError:
            status('invalid key: ' + repr(key))

# move to test points based on current matrix value
def validate(index):
    v = array(list(test_points[index]) + [1])
    x, y, z, _ = dot(inv(T), list(position()) + [1])

    if z < SAFE_HEIGHT:
        _, _, z, _ = dot(T, [x, y, SAFE_HEIGHT, 1])
        robot._driver.move({current_pipette: z})

    x, y, z, _ = dot(T, v)
    robot._driver.move({'X': x, 'Y': y})
    robot._driver.move({current_pipette: z})

def main():
    robot.connect()
    tip = urwid.Text(u"X/Y/Z: left,right/up,down/q,a; Pipette (swap): z; Steps: -/=; Test points: 1, 2, 3")   # NOQA
    pile = urwid.Pile([tip, status_text])
    root = urwid.Filler(pile, 'top')
    event_loop = urwid.main_loop.AsyncioEventLoop(
        loop=asyncio.get_event_loop()
    )
    ui_loop = urwid.MainLoop(
        root,
        handle_mouse=False,
        unhandled_input=key_pressed,
        event_loop=event_loop)
    status('Hello!')
    ui_loop.run()

    print('Robot config: \n', robot.config)

if __name__ == "__main__":
    main()
<commit_msg>erase config.json at beginning of deck calibration<commit_after>#!/usr/bin/env python

# pylama:ignore=C901

# erase previous calibration file before opentrons.robot instantiates
import os
from opentrons.util import environment
config_file_path = environment.get_path('OT_CONFIG_FILE')
if os.path.exists(config_file_path):
    os.remove(config_file_path)

import asyncio                                                          # NOQA
import urwid                                                            # NOQA
from numpy.linalg import inv                                            # NOQA
from numpy import dot, array, insert                                    # NOQA
from opentrons import robot, instruments                                # NOQA
from opentrons.robot import robot_configs                               # NOQA
from opentrons.util.calibration_functions import probe_instrument       # NOQA
from opentrons.cli.solve import solve                                   # NOQA

# Distance increments for jog
steps = [0.1, 0.25, 0.5, 1, 5, 10, 20, 40, 80]
# Index of selected step
step_index = 2

SAFE_HEIGHT = 130

left = 'Z'
right = 'A'
current_pipette = right

status_text = urwid.Text('')
current_position = (0, 0, 0)

# 200uL tip. Used during calibration process
# The actual calibration represents end of a pipette
# without tip on
TIP_LENGTH = 51.7

# Reference point being calibrated
point_number = -1

# (0, 0) is in bottom-left corner
# Expected reference points
expected = [
    (108.75, 92.8),       # dimple 4
    (373.75, 92.8),      # dimple 6
    (241.25, 273.80)    # dimple 11
]

# Actuals get overridden when ENTER is pressed
actual = [(0, 0)] * 3

# Accessible through 1,2,3 ... keyboard keys to test
# calibration by moving to known locations
test_points = [(x, y, TIP_LENGTH) for x, y in expected] + \
    [
        (132.50, 90.50, TIP_LENGTH),        # Slot 5
        (332.13, 47.41, TIP_LENGTH),        # Corner of 3
        (190.65, 227.57, TIP_LENGTH),       # Corner of 8
        (330.14, 222.78, TIP_LENGTH),       # Corner of 9
    ]

T = robot.config.gantry_calibration
XY = None

# Add fixed Z offset which is known so we don't have to calibrate for height
# during calibration process
def add_z(XY):
    return insert(
        insert(XY, 2, [0, 0, 0], axis=1),
        2,
        [0, 0, 1, T[2][3]],
        axis=0)

def current_step():
    """
    Given current step index return step value in mm
    """
    return steps[step_index]

def position():
    """
    Read position from driver into a tuple and map 3-rd value
    to the axis of a pipette currently used
    """
    try:
        p = robot._driver.position
        res = (p['X'], p['Y'], p[current_pipette.upper()])
    except KeyError:
        # for some reason we are sometimes getting
        # key error in dict returned from driver
        pass
    else:
        return res

def status(text):
    """
    Refresh status in a text box
    """

    points = '\n'.join([
        # Highlight point being calibrated
        ('* ' if point_number == point else '') + "{0} {1}"
        # Display actual and expected coordinates
        .format(coord[0], coord[1])
        for point, coord in enumerate(zip(actual, expected))
    ])

    text = '\n'.join([
        points,
        'Smoothie: {0}'.format(current_position),
        'World: {0}'.format(tuple(dot(inv(T), list(current_position) + [1])[:-1])),  # NOQA
        'Step: {0}'.format(current_step()),
        'Current stage: ' + current_pipette,
        'Message: ' + text
    ])

    status_text.set_text(text)

def jog(axis, direction, step):
    global current_position
    robot._driver.move({axis: robot._driver.position[axis] + direction * step})
    current_position = position()
    status('Jog: ' + repr([axis, str(direction), str(step)]))

key_mappings = {
    'q': lambda: jog(current_pipette, +1, current_step()),
    'a': lambda: jog(current_pipette, -1, current_step()),
    'up': lambda: jog('Y', +1, current_step()),
    'down': lambda: jog('Y', -1, current_step()),
    'left': lambda: jog('X', -1, current_step()),
    'right': lambda: jog('X', +1, current_step()),
}

def key_pressed(key):
    global current_position, current_pipette, step_index, \
        point_number, XY, T

    if key == 'z':
        current_pipette = left if current_pipette == right else right
        status('current pipette axis: ' + repr(current_pipette))
    elif key.isnumeric():
        validate(int(key) - 1)
    # less travel
    elif key == '-':
        if step_index > 0:
            step_index -= 1
        status('step: ' + repr(current_step()))
    # more travel
    elif key == '=':
        if step_index < len(steps) - 1:
            step_index += 1
        status('step: ' + repr(current_step()))
    # skip calibration point
    elif key == 's':
        if (point_number < len(actual) - 1):
            point_number += 1
        status('skipped #{0}'.format(point_number))
    # run tip probe
    elif key == 'p':

        robot.reset()

        pipette = instruments.Pipette(mount='right', channels=1)
        probe_center = tuple(probe_instrument(
            pipette, robot, tip_length=TIP_LENGTH))
        robot.config = robot.config._replace(
            probe_center=probe_center
        )
        status('Tip probe')
    # save calibration point and move to next
    elif key == 'enter':
        if point_number >= len(actual):
            return

        if point_number < 0:
            actual_z = position()[-1]
            expected_z = T[2][3] + TIP_LENGTH
            T[2][3] += actual_z - expected_z
            point_number += 1
            status('saved Z-Offset: {0}'.format(T[2][3]))
            return

        actual[point_number] = position()[:-1]
        point_number += 1
        status('saved #{0}: {1}'.format(point_number, actual[point_number-1]))

        # On last point update gantry calibration
        if point_number == len(actual):
            XY = solve(expected, actual)
            T = add_z(XY)
            robot.config = robot.config._replace(
                    gantry_calibration=list(map(lambda i: list(i), T)),
                )
            status(str(robot.config))

    # move to previous calibration point
    elif key == 'backspace':
        if point_number > 0:
            point_number -= 1
        status('')
    # home
    elif key == '\\':
        robot.home()
        current_position = position()
        status('Homed')
    # calculate transformation matrix
    elif key == 'esc':
        raise urwid.ExitMainLoop
    elif key == ' ':
        try:
            diff = robot_configs.save(robot.config)
            status('Saved')
        except Exception as e:
            status(repr(e))
        else:
            status(str(diff))
    else:
        try:
            key_mappings[key]()
        except KeyError:
            status('invalid key: ' + repr(key))

# move to test points based on current matrix value
def validate(index):
    v = array(list(test_points[index]) + [1])
    x, y, z, _ = dot(inv(T), list(position()) + [1])

    if z < SAFE_HEIGHT:
        _, _, z, _ = dot(T, [x, y, SAFE_HEIGHT, 1])
        robot._driver.move({current_pipette: z})

    x, y, z, _ = dot(T, v)
    robot._driver.move({'X': x, 'Y': y})
    robot._driver.move({current_pipette: z})

def main():
    robot.connect()
    tip = urwid.Text(u"X/Y/Z: left,right/up,down/q,a; Pipette (swap): z; Steps: -/=; Test points: 1, 2, 3")   # NOQA
    pile = urwid.Pile([tip, status_text])
    root = urwid.Filler(pile, 'top')
    event_loop = urwid.main_loop.AsyncioEventLoop(
        loop=asyncio.get_event_loop()
    )
    ui_loop = urwid.MainLoop(
        root,
        handle_mouse=False,
        unhandled_input=key_pressed,
        event_loop=event_loop)
    status('Hello!')
    ui_loop.run()

    print('Robot config: \n', robot.config)

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


<commit_before>import unittest
from opaflib.lexer import tokens, LexerException, get_lexer, lexify

class ParserTest(unittest.TestCase):
    def setUp(self):
        self.my_lexer = get_lexer()
    def tearDown(self):
        pass

    def testLexer(self):
        self.assertEqual("[LexToken(NUMBER,'1',1,0)]", str(lexify('1')) )

if __name__ == '__main__':
    unittest.main()

<commit_msg>Tests for the lexer<commit_after>import unittest
from opaflib.lexer import PDFLexer
import ply.lex as lex

class ParserTest(unittest.TestCase):
    def setUp(self):
        self.my_lexer = PDFLexer()
        self.my_lexer.build(debug=False,errorlog=lex.NullLogger())

    def tearDown(self):
        self.my_lexer = None

    def lex(self, data):
        return str(self.my_lexer.lexify(data))

    def testLexer(self):
        tst = [
        (r"[LexToken(LEFT_SQUARE_BRACKET,'[',1,0)]", "["),
        (r"[LexToken(RIGHT_SQUARE_BRACKET,']',1,0)]", "]"),
        (r"[LexToken(DOUBLE_GREATER_THAN_SIGN,'>>',1,0)]", ">>"),
        (r"[LexToken(DOUBLE_LESS_THAN_SIGN,'<<',1,0)]", "<<"),
        (r"[LexToken(HEXSTRING,'',1,0)]","<>"),
        (r"[LexToken(HEXSTRING,'\x01\x02\x03\x04',1,0)]","<01020304>"),
        (r"[LexToken(STRING,'!',1,2)]","(!)"),
        (r"[LexToken(NUMBER,'123.2',1,0)]", '123.2'),
        (r"[LexToken(NAME,'name0',1,0)]", '/name0')]
        for token, string  in tst:
            self.assertEqual(token, self.lex(string))

if __name__ == '__main__':
    unittest.main()

<|endoftext|>


<commit_before>'''
This file contains the manual creation of a list of movie and Tv shows and
episodes objects. These objects are passed as arguments to the open_page()
method call from the fresh_tomatoes.py file
'''

import fresh_tomatoes
from media import Movie, Episode, TvShow

# Movie creation
toy_story = Movie('Toy Story',
                  'A wonderful story of toys', 81,
                  'http://upload.wikimedia.org/wikipedia/en/1/13/\
                  Toy_Story.jpg',
                  'https://www.youtube.com/watch?v=KYz2wyBy3kc')

avatar = Movie('Avatar',
               'A marine on an alien planet', 178,
               'http://upload.wikimedia.org/wikipedia/en/b/b0/\
               Avatar-Teaser-Poster.jpg',
               'https://www.youtube.com/watch?v=5PSNL1qE6VY')

school_of_rock = Movie('School of Rock', 'Using rock music to learn', 109,
                       'http://upload.wikimedia.org/wikipedia/en/1/11/\
                       School_of_Rock_Poster.jpg',
                       'https://www.youtube.com/watch?v=3PsUJFEBC74')

ratatouille = Movie('Ratatouille',
                    'A rat that is a chef in Paris', 115,
                    'http://upload.wikimedia.org/wikipedia/en/5/50/\
                    RatatouillePoster.jpg',
                    'https://www.youtube.com/watch?=v=c3sBBRxDAqk')

midnight_in_paris = Movie('Midnight in Paris',
                          'Going back in time to meet authors', 100,
                          'http://upload.wikimedia.org/wikipedia/en/9/9f/\
                          Midnight_in_Paris_Poster.jpg',
                          'https://www.youtube.com/watch?v=BYRWfS2s2v4')

hunger_games = Movie('Hunger Games',
                     'A real reality show', 123,
                     'http://upload.wikimedia.org/wikipedia/en/4/42/\
                     HungerGamesPoster.jpg',
                     'https://www.youtube.com/watch?v=PbA63a7H0bo')

# List of movies
movies = [toy_story, avatar, school_of_rock, ratatouille,
          midnight_in_paris, hunger_games]

# Episodes creation (Title, Storyline, duration, season, episode)
breaking_bad_e1_s1 = Episode('Pilot',
                             "Walter White, a 50-year old chemistry teacher, \
                             secretly begins making crystallized \
                             methamphetamine to support his family after \
                             learning that he has terminal lung cancer. He \
                             teams up with a former student, Jesse Pinkman, \
                             who is a meth dealer. Jesse tries to sell the \
                             meth they made, but the dealers snatch him and \
                             make him show them their lab, which is in an old \
                             RV. Walt knows the dealers intend to kill him, \
                             so he poisons them while pretending to share his \
                             recipe.", 58, 1, 1)

breaking_bad_e2_s1 = Episode('Cats in the Bag',
                             "Walt and Jesse try to dispose of the two bodies \
                             in the RV, which becomes increasingly \
                             complicated when one of them, Krazy-8, wakes up. \
                             They eventually imprison him in Jesse's basement.\
                             Meanwhile, Skyler grows suspicious of Walter's \
                             recent behavior and discovers that their baby is \
                             a girl. Jesse disposes of the body of the other \
                             dealer, Emilio Koyama, using hydrofluoric acid \
                             as per Walt's instructions, but ignores Walt's \
                             warning to use a plastic bin and instead \
                             destroys his bathtub in the process.", 48, 1, 2)

# TVShow creation (Title, Storyline, poster, trailer, station, episodes list)
breaking_bad = TvShow('Breaking bad', 'The secret lifestyle of a chemistry \
                      teacher',
                      'http://upload.wikimedia.org/wikipedia/commons/4/48/\
                      Breaking_Bad_logo.png',
                      'AMC',
                      [breaking_bad_e1_s1, breaking_bad_e2_s1])

modern_family = TvShow('Modern Family', 'A comedy that follows the lives of a \
                       peculiar family',
                       'http://upload.wikimedia.org/wikipedia/en/5/53/\
                       Modern_Family_Promo_Season_1.jpg',
                       'ABC', [])

# List of tv shows
tvshows = [breaking_bad, modern_family]

fresh_tomatoes.open_page(movies, tvshows)
<commit_msg>fixed img urls<commit_after>'''
This file contains the manual creation of a list of movie and Tv shows and
episodes objects. These objects are passed as arguments to the open_page()
method call from the fresh_tomatoes.py file
'''

import fresh_tomatoes
from media import Movie, Episode, TvShow

# Movie creation
toy_story = Movie('Toy Story',
                  'A wonderful story of toys', 81,
                  'http://upload.wikimedia.org/wikipedia/en/1/13/' +
                  'Toy_Story.jpg',
                  'https://www.youtube.com/watch?v=KYz2wyBy3kc')

avatar = Movie('Avatar',
               'A marine on an alien planet', 178,
               'http://upload.wikimedia.org/wikipedia/en/b/b0/' +
               'Avatar-Teaser-Poster.jpg',
               'https://www.youtube.com/watch?v=5PSNL1qE6VY')

school_of_rock = Movie('School of Rock', 'Using rock music to learn', 109,
                       'http://upload.wikimedia.org/wikipedia/en/1/11/' +
                       'School_of_Rock_Poster.jpg',
                       'https://www.youtube.com/watch?v=3PsUJFEBC74')

ratatouille = Movie('Ratatouille',
                    'A rat that is a chef in Paris', 115,
                    'http://upload.wikimedia.org/wikipedia/en/5/50/' +
                    'RatatouillePoster.jpg',
                    'https://www.youtube.com/watch?=v=c3sBBRxDAqk')

midnight_in_paris = Movie('Midnight in Paris',
                          'Going back in time to meet authors', 100,
                          'http://upload.wikimedia.org/wikipedia/en/9/9f/' +
                          'Midnight_in_Paris_Poster.jpg',
                          'https://www.youtube.com/watch?v=BYRWfS2s2v4')

hunger_games = Movie('Hunger Games',
                     'A real reality show', 123,
                     'http://upload.wikimedia.org/wikipedia/en/4/42/' +
                     'HungerGamesPoster.jpg',
                     'https://www.youtube.com/watch?v=PbA63a7H0bo')

# List of movies
movies = [toy_story, avatar, school_of_rock, ratatouille,
          midnight_in_paris, hunger_games]

# Episodes creation (Title, Storyline, duration, season, episode)
breaking_bad_e1_s1 = Episode('Pilot',
                             "Walter White, a 50-year old chemistry teacher, \
                             secretly begins making crystallized \
                             methamphetamine to support his family after \
                             learning that he has terminal lung cancer. He \
                             teams up with a former student, Jesse Pinkman, \
                             who is a meth dealer. Jesse tries to sell the \
                             meth they made, but the dealers snatch him and \
                             make him show them their lab, which is in an old \
                             RV. Walt knows the dealers intend to kill him, \
                             so he poisons them while pretending to share his \
                             recipe.", 58, 1, 1)

breaking_bad_e2_s1 = Episode('Cats in the Bag',
                             "Walt and Jesse try to dispose of the two bodies \
                             in the RV, which becomes increasingly \
                             complicated when one of them, Krazy-8, wakes up. \
                             They eventually imprison him in Jesse's basement.\
                             Meanwhile, Skyler grows suspicious of Walter's \
                             recent behavior and discovers that their baby is \
                             a girl. Jesse disposes of the body of the other \
                             dealer, Emilio Koyama, using hydrofluoric acid \
                             as per Walt's instructions, but ignores Walt's \
                             warning to use a plastic bin and instead \
                             destroys his bathtub in the process.", 48, 1, 2)

# TVShow creation (Title, Storyline, poster, trailer, station, episodes list)
breaking_bad = TvShow('Breaking bad', 'The secret lifestyle of a chemistry \
                      teacher',
                      'http://upload.wikimedia.org/wikipedia/commons/4/48/' +
                      'Breaking_Bad_logo.png',
                      'AMC',
                      [breaking_bad_e1_s1, breaking_bad_e2_s1])

modern_family = TvShow('Modern Family', 'A comedy that follows the lives of a \
                       peculiar family',
                       'http://upload.wikimedia.org/wikipedia/en/5/53/' +
                       'Modern_Family_Promo_Season_1.jpg',
                       'ABC', [])

# List of tv shows
tvshows = [breaking_bad, modern_family]

fresh_tomatoes.open_page(movies, tvshows)
<|endoftext|>