<commit_before>#
# Copyright (c) 2009 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.rpath.com/permanent/licenses/CPL-1.0.
#
# This program is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#

"""
Module for parsing SuSE update info metadata. This was introduced in SLES 11.
Refer to patchxml.py for previous versions.
"""

__all__ = ('UpdateInfoXml', )

from rpath_xmllib import api1 as xmllib

from repomd.packagexml import PackageCompare
from repomd.xmlcommon import XmlFileParser, SlotNode
from repomd.errors import UnknownElementError, UnknownAttributeError

class _Updates(SlotNode):
    """
    Represents updates in an updateinfo.xml.
    """

    __slots__ = ()

    def addChild(self, child):
        """
        Set attributes on child nodes.
        """

        if child.getName() != 'update':
            raise UnknownElement(child)

        child.status = None
        child.emailfrom = None
        child.type = None

        for attr, value in child.iterAttributes():
            if attr == 'status':
                child.status = value
            elif attr == 'from':
                child.emailfrom = value
            elif attr == 'type':
                child.type = value
            elif attr == 'version':
                pass
            else:
                raise UnknownAttributeError(child, attr)

        SlotNode.addChild(self, child)

    def getUpdateInfo(self):
        """
        Return a list of update objects.
        """

        return self.getChildren('update')

class _Update(SlotNode):
    """
    Represents an update entry in updateinfo.xml.
    """

    __slots__ = ('status', 'emailfrom', 'type', 'id', 'title', 'release',
        'issued', 'references', 'description', 'pkglist', 'packages')

    # All attributes are defined in __init__ by iterating over __slots__,
    # this confuses pylint.
    # W0201 - Attribute $foo defined outside __init__
    # pylint: disable-msg=W0201

    def addChild(self, child):
        """
        Parse the children of update.
        """

        n = child.getName()
        if n == 'id':
            self.id = child.finalize()
        elif n == 'title':
            self.title = child.finalize()
        elif n == 'release':
            self.release = child.finalize()
        elif n == 'issued':
            self.issued = child.getAttribute('date')
        elif n == 'references':
            self.references = child.getChildren('reference')
        elif n == 'description':
            self.description = child.finalize()
        elif n == 'pkglist':
            c = child.getChildren('collection')
            assert len(c) == 1
            self.pkglist = c[0].getChildren('package')
        else:
            raise UnknownElementError(child)

class _References(SlotNode):
    """
    Represent a list of references in updateinfo.xml.
    """

    __slots__ = ()

    def addChild(self, child):
        if child.getName() != 'reference':
            raise UnknownElementError(child)

        child.href = None
        child.id = None
        child.title = None
        child.type = None

        for attr, value in child.iterAttributes():
            if attr == 'href':
                child.href = value
            elif attr == 'id':
                child.id = value
            elif attr == 'title':
                child.title = value
            elif attr == 'type':
                child.type = value
            else:
                raise UnknownAttributeError(child, attr)

        SlotNode.addChild(self, child)

class _Reference(SlotNode):
    """
    Prepesent a single reference.
    """

    __slots__ = ('href', 'id', 'title', 'type')

class _Collection(SlotNode):
    """
    Represents a pkglist collection in updateinfo.xml.
    """

    __slots__ = ()

    def addChild(self, child):
        """
        Update child attributes.
        """

        if child.getName() != 'package':
            raise UnknownElement(child)

        child.name = None
        child.arch = None
        child.version = None
        child.release = None

        child.location = ''

        for attr, value in child.iterAttributes():
            if attr == 'name':
                child.name = value
            elif attr == 'arch':
                child.arch = value
            elif attr == 'version':
                child.version = value
            elif attr == 'release':
                child.release = value
            else:
                raise UnknownAttributeError(child, attr)

        SlotNode.addChild(self, child)

class _UpdateInfoPackage(SlotNode, PackageCompare):
    """
    Represnts a package entry in a pkglist of an update in updateinfo.xml.
    """

    __slots__ = ('filename', 'name', 'arch', 'version', 'release',
        'reboot_suggested', 'restart_suggested',  'epoch', 'location',
        'summary', 'relogin_suggested')

    # All attributes are defined in __init__ by iterating over __slots__,
    # this confuses pylint.
    # W0201 - Attribute $foo defined outside __init__
    # pylint: disable-msg=W0201

    def addChild(self, child):
        """
        Parse children of pkglist.collection
        """

        n = child.getName()
        if n == 'filename':
            self.filename = child.finalize()
        elif n == 'reboot_suggested':
            self.reboot_suggested = child.finalize()
        elif n == 'restart_suggested':
            self.restart_suggested = child.finalize()
        elif n == 'relogin_suggested':
            self.relogin_suggested = child.finalize()
        else:
            raise UnknownElementError(child)

class UpdateInfoXml(XmlFileParser):
    """
    Bind all types for parsing updateinfo.xml.
    """

    def _registerTypes(self):
        """
        Setup parser.
        """

        self._databinder.registerType(_Updates, name='updates')
        self._databinder.registerType(_Update, name='update')
        self._databinder.registerType(_References, name='references')
        self._databinder.registerType(_Reference, name='reference')
        self._databinder.registerType(_Collection, name='collection')
        self._databinder.registerType(_UpdateInfoPackage, name='package')

        self._databinder.registerType(xmllib.StringNode, name='id')
        self._databinder.registerType(xmllib.StringNode, name='title')
        self._databinder.registerType(xmllib.StringNode, name='release')
        self._databinder.registerType(xmllib.StringNode, name='description')
        self._databinder.registerType(xmllib.StringNode, name='filename')
<commit_msg>add more required attributes<commit_after>#
# Copyright (c) 2009 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.rpath.com/permanent/licenses/CPL-1.0.
#
# This program is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#

"""
Module for parsing SuSE update info metadata. This was introduced in SLES 11.
Refer to patchxml.py for previous versions.
"""

__all__ = ('UpdateInfoXml', )

from rpath_xmllib import api1 as xmllib

from repomd.packagexml import PackageCompare
from repomd.xmlcommon import XmlFileParser, SlotNode
from repomd.errors import UnknownElementError, UnknownAttributeError

class _Updates(SlotNode):
    """
    Represents updates in an updateinfo.xml.
    """

    __slots__ = ()

    def addChild(self, child):
        """
        Set attributes on child nodes.
        """

        if child.getName() != 'update':
            raise UnknownElement(child)

        child.status = None
        child.emailfrom = None
        child.type = None

        for attr, value in child.iterAttributes():
            if attr == 'status':
                child.status = value
            elif attr == 'from':
                child.emailfrom = value
            elif attr == 'type':
                child.type = value
            elif attr == 'version':
                pass
            else:
                raise UnknownAttributeError(child, attr)

        SlotNode.addChild(self, child)

    def getUpdateInfo(self):
        """
        Return a list of update objects.
        """

        return self.getChildren('update')

class _Update(SlotNode):
    """
    Represents an update entry in updateinfo.xml.
    """

    __slots__ = ('status', 'emailfrom', 'type', 'id', 'title', 'release',
        'issued', 'references', 'description', 'pkglist', 'packages',
        'summary', 'packages')

    # All attributes are defined in __init__ by iterating over __slots__,
    # this confuses pylint.
    # W0201 - Attribute $foo defined outside __init__
    # pylint: disable-msg=W0201

    def addChild(self, child):
        """
        Parse the children of update.
        """

        n = child.getName()
        if n == 'id':
            self.id = child.finalize()
        elif n == 'title':
            self.title = child.finalize()
        elif n == 'release':
            self.release = child.finalize()
        elif n == 'issued':
            self.issued = child.getAttribute('date')
        elif n == 'references':
            self.references = child.getChildren('reference')
        elif n == 'description':
            self.description = child.finalize()
        elif n == 'pkglist':
            c = child.getChildren('collection')
            assert len(c) == 1
            self.pkglist = c[0].getChildren('package')
        else:
            raise UnknownElementError(child)

class _References(SlotNode):
    """
    Represent a list of references in updateinfo.xml.
    """

    __slots__ = ()

    def addChild(self, child):
        if child.getName() != 'reference':
            raise UnknownElementError(child)

        child.href = None
        child.id = None
        child.title = None
        child.type = None

        for attr, value in child.iterAttributes():
            if attr == 'href':
                child.href = value
            elif attr == 'id':
                child.id = value
            elif attr == 'title':
                child.title = value
            elif attr == 'type':
                child.type = value
            else:
                raise UnknownAttributeError(child, attr)

        SlotNode.addChild(self, child)

class _Reference(SlotNode):
    """
    Prepesent a single reference.
    """

    __slots__ = ('href', 'id', 'title', 'type')

class _Collection(SlotNode):
    """
    Represents a pkglist collection in updateinfo.xml.
    """

    __slots__ = ()

    def addChild(self, child):
        """
        Update child attributes.
        """

        if child.getName() != 'package':
            raise UnknownElement(child)

        child.name = None
        child.arch = None
        child.version = None
        child.release = None

        child.location = ''

        for attr, value in child.iterAttributes():
            if attr == 'name':
                child.name = value
            elif attr == 'arch':
                child.arch = value
            elif attr == 'version':
                child.version = value
            elif attr == 'release':
                child.release = value
            else:
                raise UnknownAttributeError(child, attr)

        SlotNode.addChild(self, child)

class _UpdateInfoPackage(SlotNode, PackageCompare):
    """
    Represnts a package entry in a pkglist of an update in updateinfo.xml.
    """

    __slots__ = ('filename', 'name', 'arch', 'version', 'release',
        'reboot_suggested', 'restart_suggested',  'epoch', 'location',
        'summary', 'relogin_suggested')

    # All attributes are defined in __init__ by iterating over __slots__,
    # this confuses pylint.
    # W0201 - Attribute $foo defined outside __init__
    # pylint: disable-msg=W0201

    def addChild(self, child):
        """
        Parse children of pkglist.collection
        """

        n = child.getName()
        if n == 'filename':
            self.filename = child.finalize()
        elif n == 'reboot_suggested':
            self.reboot_suggested = child.finalize()
        elif n == 'restart_suggested':
            self.restart_suggested = child.finalize()
        elif n == 'relogin_suggested':
            self.relogin_suggested = child.finalize()
        else:
            raise UnknownElementError(child)

class UpdateInfoXml(XmlFileParser):
    """
    Bind all types for parsing updateinfo.xml.
    """

    def _registerTypes(self):
        """
        Setup parser.
        """

        self._databinder.registerType(_Updates, name='updates')
        self._databinder.registerType(_Update, name='update')
        self._databinder.registerType(_References, name='references')
        self._databinder.registerType(_Reference, name='reference')
        self._databinder.registerType(_Collection, name='collection')
        self._databinder.registerType(_UpdateInfoPackage, name='package')

        self._databinder.registerType(xmllib.StringNode, name='id')
        self._databinder.registerType(xmllib.StringNode, name='title')
        self._databinder.registerType(xmllib.StringNode, name='release')
        self._databinder.registerType(xmllib.StringNode, name='description')
        self._databinder.registerType(xmllib.StringNode, name='filename')
<|endoftext|>


<commit_before>from itertools import product

def isPalindrome(val, base=10):
    def digit(d):
        return int(val / (base**d)) % base

    n = 1
    tmp = val
    while int(tmp / base) > 0:
        tmp /= base
        n += 1

    return all(digit(i) == digit(n - 1 - i) for i in range(int(n/2)))

p1,p2=0,0
for i, j in product(reversed(range(100, 1000)), repeat=2):
    if isPalindrome(i*j) and p1*p2 < i*j:
        p1,p2=i,j

print(p1*p2, p1, p2)
<commit_msg>Update 004.py<commit_after>from itertools import product

def num_of_digits(val, base):
    " 基数 base のもとでの桁数を求める "
    n = 1
    while int(val / base) > 0:
        val /= base
        n += 1
    return n

def isPalindrome(val, base=10):
    def digit(d):
        ' d 桁目の数字 '
        return int(val / (base**d)) % base

    n = num_of_digits(val, base)

    return all(digit(i) == digit(n - 1 - i) for i in range(int(n/2)))

p1,p2=0,0
for i, j in product(reversed(range(100, 1000)), repeat=2):
    if isPalindrome(i*j) and p1*p2 < i*j:
        p1,p2=i,j

print(p1*p2, '=', p1, 'x', p2)
<|endoftext|>


<commit_before># Copyright 2014 Akretion - Alexis de Lattre <alexis.delattre@akretion.com>
# Copyright 2014 Tecnativa - Pedro M. Baeza
# Copyright 2018 Tecnativa - Carlos Dauden
# Copyright 2021 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
    "name": "Account Payment Partner",
    "version": "14.0.1.3.2",
    "category": "Banking addons",
    "license": "AGPL-3",
    "summary": "Adds payment mode on partners and invoices",
    "author": "Akretion, Tecnativa, Odoo Community Association (OCA)",
    "website": "https://github.com/OCA/bank-payment",
    "depends": ["account_payment_mode"],
    "data": [
        "views/res_partner_view.xml",
        "views/account_move_view.xml",
        "views/account_move_line.xml",
        "views/account_payment_mode.xml",
        "views/report_invoice.xml",
        "reports/account_invoice_report_view.xml",
    ],
    "demo": ["demo/partner_demo.xml"],
    "installable": True,
}
<commit_msg>account_payment_partner 14.0.1.3.3<commit_after># Copyright 2014 Akretion - Alexis de Lattre <alexis.delattre@akretion.com>
# Copyright 2014 Tecnativa - Pedro M. Baeza
# Copyright 2018 Tecnativa - Carlos Dauden
# Copyright 2021 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
    "name": "Account Payment Partner",
    "version": "14.0.1.3.3",
    "category": "Banking addons",
    "license": "AGPL-3",
    "summary": "Adds payment mode on partners and invoices",
    "author": "Akretion, Tecnativa, Odoo Community Association (OCA)",
    "website": "https://github.com/OCA/bank-payment",
    "depends": ["account_payment_mode"],
    "data": [
        "views/res_partner_view.xml",
        "views/account_move_view.xml",
        "views/account_move_line.xml",
        "views/account_payment_mode.xml",
        "views/report_invoice.xml",
        "reports/account_invoice_report_view.xml",
    ],
    "demo": ["demo/partner_demo.xml"],
    "installable": True,
}
<|endoftext|>


<commit_before>from json import dumps
import os

from requests.packages.urllib3.exceptions import MaxRetryError

from app import settings
from app.private_publisher import PrivatePublisher
from app.settings import session
from app.helpers.exceptions import DecryptError, BadMessageError, RetryableError

class ResponseProcessor:

    @staticmethod
    def options():
        rv = {}
        try:
            rv["secret"] = os.getenv("SDX_COLLECT_SECRET").encode("ascii")
        except:
            # No secret in env
            pass
        return rv

    def __init__(self, logger):
        self.logger = logger
        self.tx_id = ""

        self.rrm_publisher = PrivatePublisher(
            logger, settings.RABBIT_URLS, settings.RABBIT_RRM_RECEIPT_QUEUE
        )
        self.ctp_publisher = PrivatePublisher(
            logger, settings.RABBIT_URLS, settings.RABBIT_CTP_RECEIPT_QUEUE
        )

    def process(self, encrypted_survey):
        # decrypt
        decrypted_json = self.decrypt_survey(encrypted_survey)

        metadata = decrypted_json['metadata']
        self.logger = self.logger.bind(user_id=metadata['user_id'], ru_ref=metadata['ru_ref'])

        if 'tx_id' in decrypted_json:
            self.tx_id = decrypted_json['tx_id']
            self.logger = self.logger.bind(tx_id=self.tx_id)

        try:
            self.validate_survey(decrypted_json)
        except BadMessageError:
            # If the validation fails, the message is to be marked "invalid"
            # and then stored. We don't then want to stop processing at this point.
            decrypted_json['invalid'] = True

        self.store_survey(decrypted_json)
        self.send_receipt(decrypted_json)
        return

    def send_receipt(self, decrypted_json):
        receipt_json = {
            'tx_id': decrypted_json['tx_id'],
            'collection': {
                'exercise_sid': decrypted_json['collection']['exercise_sid']
            },
            'metadata': {
                'ru_ref': decrypted_json['metadata']['ru_ref'],
                'user_id': decrypted_json['metadata']['user_id']
            }
        }

        if decrypted_json.get("survey_id") and decrypted_json["survey_id"] == "census":
            queue_ok = self.ctp_publisher.publish_message(dumps(receipt_json), secret=settings.SDX_COLLECT_SECRET)
        else:
            queue_ok = self.rrm_publisher.publish_message(dumps(receipt_json), secret=settings.SDX_COLLECT_SECRET)

        if not queue_ok:
            raise RetryableError()

    def decrypt_survey(self, encrypted_survey):
        self.logger.debug("Decrypting survey")
        response = self.remote_call(settings.SDX_DECRYPT_URL, data=encrypted_survey)
        try:
            self.response_ok(response)
        except BadMessageError:
            # Translate the Bad Message into a Decrypt Error to force quarantine
            raise DecryptError
        return response.json()

    def validate_survey(self, decrypted_json):
        self.logger.debug("Validating survey")
        self.response_ok(self.remote_call(settings.SDX_VALIDATE_URL, json=decrypted_json))

    def store_survey(self, decrypted_json):
        self.logger.debug("Storing survey")
        self.response_ok(self.remote_call(settings.SDX_RESPONSES_URL, json=decrypted_json))

    def remote_call(self, request_url, json=None, data=None, headers=None, verify=True, auth=None):
        try:
            self.logger.info("Calling service", request_url=request_url)
            r = None

            if json:
                r = session.post(request_url, json=json, headers=headers, verify=verify, auth=auth)
            elif data:
                r = session.post(request_url, data=data, headers=headers, verify=verify, auth=auth)
            else:
                r = session.get(request_url, headers=headers, verify=verify, auth=auth)

            return r

        except MaxRetryError:
            self.logger.error("Max retries exceeded (5)", request_url=request_url)

    def response_ok(self, res):
        res_logger = self.logger
        res_logger.bind(request_url=res.url, status_code=res.status_code)

        if res.status_code == 200 or res.status_code == 201:
            res_logger.info("Returned from service", response="ok")
            return

        elif res.status_code == 400:
            res_logger.info("Returned from service", response="client error")
            raise BadMessageError

        else:
            res_logger.error("Returned from service", response="service error")
            raise RetryableError
<commit_msg>Changes status_code to status<commit_after>from json import dumps
import os

from requests.packages.urllib3.exceptions import MaxRetryError

from app import settings
from app.private_publisher import PrivatePublisher
from app.settings import session
from app.helpers.exceptions import DecryptError, BadMessageError, RetryableError

class ResponseProcessor:

    @staticmethod
    def options():
        rv = {}
        try:
            rv["secret"] = os.getenv("SDX_COLLECT_SECRET").encode("ascii")
        except:
            # No secret in env
            pass
        return rv

    def __init__(self, logger):
        self.logger = logger
        self.tx_id = ""

        self.rrm_publisher = PrivatePublisher(
            logger, settings.RABBIT_URLS, settings.RABBIT_RRM_RECEIPT_QUEUE
        )
        self.ctp_publisher = PrivatePublisher(
            logger, settings.RABBIT_URLS, settings.RABBIT_CTP_RECEIPT_QUEUE
        )

    def process(self, encrypted_survey):
        # decrypt
        decrypted_json = self.decrypt_survey(encrypted_survey)

        metadata = decrypted_json['metadata']
        self.logger = self.logger.bind(user_id=metadata['user_id'], ru_ref=metadata['ru_ref'])

        if 'tx_id' in decrypted_json:
            self.tx_id = decrypted_json['tx_id']
            self.logger = self.logger.bind(tx_id=self.tx_id)

        try:
            self.validate_survey(decrypted_json)
        except BadMessageError:
            # If the validation fails, the message is to be marked "invalid"
            # and then stored. We don't then want to stop processing at this point.
            decrypted_json['invalid'] = True

        self.store_survey(decrypted_json)
        self.send_receipt(decrypted_json)
        return

    def send_receipt(self, decrypted_json):
        receipt_json = {
            'tx_id': decrypted_json['tx_id'],
            'collection': {
                'exercise_sid': decrypted_json['collection']['exercise_sid']
            },
            'metadata': {
                'ru_ref': decrypted_json['metadata']['ru_ref'],
                'user_id': decrypted_json['metadata']['user_id']
            }
        }

        if decrypted_json.get("survey_id") and decrypted_json["survey_id"] == "census":
            queue_ok = self.ctp_publisher.publish_message(dumps(receipt_json), secret=settings.SDX_COLLECT_SECRET)
        else:
            queue_ok = self.rrm_publisher.publish_message(dumps(receipt_json), secret=settings.SDX_COLLECT_SECRET)

        if not queue_ok:
            raise RetryableError()

    def decrypt_survey(self, encrypted_survey):
        self.logger.debug("Decrypting survey")
        response = self.remote_call(settings.SDX_DECRYPT_URL, data=encrypted_survey)
        try:
            self.response_ok(response)
        except BadMessageError:
            # Translate the Bad Message into a Decrypt Error to force quarantine
            raise DecryptError
        return response.json()

    def validate_survey(self, decrypted_json):
        self.logger.debug("Validating survey")
        self.response_ok(self.remote_call(settings.SDX_VALIDATE_URL, json=decrypted_json))

    def store_survey(self, decrypted_json):
        self.logger.debug("Storing survey")
        self.response_ok(self.remote_call(settings.SDX_RESPONSES_URL, json=decrypted_json))

    def remote_call(self, request_url, json=None, data=None, headers=None, verify=True, auth=None):
        try:
            self.logger.info("Calling service", request_url=request_url)
            r = None

            if json:
                r = session.post(request_url, json=json, headers=headers, verify=verify, auth=auth)
            elif data:
                r = session.post(request_url, data=data, headers=headers, verify=verify, auth=auth)
            else:
                r = session.get(request_url, headers=headers, verify=verify, auth=auth)

            return r

        except MaxRetryError:
            self.logger.error("Max retries exceeded (5)", request_url=request_url)

    def response_ok(self, res):
        res_logger = self.logger
        res_logger.bind(request_url=res.url, status=res.status_code)

        if res.status_code == 200 or res.status_code == 201:
            res_logger.info("Returned from service", response="ok")
            return

        elif res.status_code == 400:
            res_logger.info("Returned from service", response="client error")
            raise BadMessageError

        else:
            res_logger.error("Returned from service", response="service error")
            raise RetryableError
<|endoftext|>


<commit_before>#!/usr/bin/python2.5
#
# Copyright 2009 the Melange authors.
#
# 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.

"""This module contains the Organization Application Model."""

__authors__ = [
  '"Todd Larsen" <tlarsen@google.com>',
]

from google.appengine.ext import db

from django.utils.translation import ugettext

from soc.models import licenses

import soc.models.document
import soc.models.group_app
import soc.models.user

class OrgApplication(soc.models.group_app.GroupApplication):
  """Specialized questions for the Organization application.

  These questions are in addition to those in the GroupApplication Model.

  Eventually, this will be replaced with a Question/Answer/Quiz/Response
  approach.  At that time, existing OrgApplication entities will be migrated
  (converted) to their new representations in the Datastore.
  """

  prior_participation = db.TextProperty(required=False, verbose_name=ugettext(
      'Has your group participated previously?'
      ' If so, please summarize your involvement and any past successes'
      ' and failures.'))

  prior_application = db.TextProperty(required=False, verbose_name=ugettext(
      'If your group has not previously participated, have you applied in'
      ' the past?  If so, for what sort of participation?'))

  license_name = db.StringProperty(required=True, choices=licenses.LICENSES,
      verbose_name=ugettext('What license does your organization use?'))

  ideas = db.LinkProperty(required=True, verbose_name=ugettext(
      'What is the URL to the ideas list of your organization?'))
  ideas.help_text = ugettext('For instance a link to a Melange public '
      'document or some other URL')

  dev_mailing_list = db.StringProperty(required=False, verbose_name=ugettext(
      'What is the main development mailing list for your group?'))
  dev_mailing_list.help_text = ugettext(
      'Mailing list email address, URL to sign-up page, etc.')

  contrib_template = db.LinkProperty(required=False, verbose_name=ugettext(
      'What is the URL to the application template would you like contributors'
      ' to your organization to use.'))
  contrib_template.help_text = ugettext(
      'This template can be used by contributors, such as students'
      ' and other non-member participants, when they apply to contribute'
      ' to the organization.')
  contrib_template.help_text = ugettext('For instance a link to a Melange '
      'public document or some other URL')

  contrib_disappears = db.TextProperty(required=True, verbose_name=ugettext(
      'What is your plan for dealing with disappearing contributors?'))
  contrib_disappears.help_text = ugettext(
      'Contributors include students and other non-member participants.')

  member_disappears = db.TextProperty(required=True, verbose_name=ugettext(
      'What is your plan for dealing with disappearing members?'))
  member_disappears.help_text = ugettext(
      'Members include mentors, administrators, and the like.')

  encourage_contribs = db.TextProperty(required=True, verbose_name=ugettext(
      'What steps will you take to encourage contributors to interact with'
      ' your community before, during, and after the program?'))
  encourage_contribs.help_text = contrib_disappears.help_text

  continued_contribs = db.TextProperty(required=True, verbose_name=ugettext(
      'What will you do to ensure that your accepted contributors stick'
      ' with the project after the program concludes?'))
  continued_contribs.help_text = contrib_disappears.help_text

  #: field storing whether the User has agreed to the site-wide Terms of Service.
  #: (Not a required field because the Terms of Service might not be present
  #: when the first User profile is created when bootstrapping the site.)
  agreed_to_admin_agreement = db.BooleanProperty(required=False, default=False,
      verbose_name=ugettext('I Agree to the Admin Agreement'))
  agreed_to_admin_agreement.help_text = ugettext(
      'Indicates whether the user agreed to the Admin Agreement.')
<commit_msg>Added a link to the official OSI licenses list<commit_after>#!/usr/bin/python2.5
#
# Copyright 2009 the Melange authors.
#
# 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.

"""This module contains the Organization Application Model."""

__authors__ = [
  '"Todd Larsen" <tlarsen@google.com>',
]

from google.appengine.ext import db

from django.utils.translation import ugettext

from soc.models import licenses

import soc.models.document
import soc.models.group_app
import soc.models.user

class OrgApplication(soc.models.group_app.GroupApplication):
  """Specialized questions for the Organization application.

  These questions are in addition to those in the GroupApplication Model.

  Eventually, this will be replaced with a Question/Answer/Quiz/Response
  approach.  At that time, existing OrgApplication entities will be migrated
  (converted) to their new representations in the Datastore.
  """

  prior_participation = db.TextProperty(required=False, verbose_name=ugettext(
      'Has your group participated previously?'
      ' If so, please summarize your involvement and any past successes'
      ' and failures.'))

  prior_application = db.TextProperty(required=False, verbose_name=ugettext(
      'If your group has not previously participated, have you applied in'
      ' the past?  If so, for what sort of participation?'))

  license_name = db.StringProperty(required=True, choices=licenses.LICENSES,
      verbose_name=ugettext('What license does your organization use?'))
  license_name.example_text=ugettext('See '
      ' the official list.')

  ideas = db.LinkProperty(required=True, verbose_name=ugettext(
      'What is the URL to the ideas list of your organization?'))
  ideas.help_text = ugettext('For instance a link to a Melange public '
      'document or some other URL')

  dev_mailing_list = db.StringProperty(required=False, verbose_name=ugettext(
      'What is the main development mailing list for your group?'))
  dev_mailing_list.help_text = ugettext(
      'Mailing list email address, URL to sign-up page, etc.')

  contrib_template = db.LinkProperty(required=False, verbose_name=ugettext(
      'What is the URL to the application template would you like contributors'
      ' to your organization to use.'))
  contrib_template.help_text = ugettext(
      'This template can be used by contributors, such as students'
      ' and other non-member participants, when they apply to contribute'
      ' to the organization.')
  contrib_template.help_text = ugettext('For instance a link to a Melange '
      'public document or some other URL')

  contrib_disappears = db.TextProperty(required=True, verbose_name=ugettext(
      'What is your plan for dealing with disappearing contributors?'))
  contrib_disappears.help_text = ugettext(
      'Contributors include students and other non-member participants.')

  member_disappears = db.TextProperty(required=True, verbose_name=ugettext(
      'What is your plan for dealing with disappearing members?'))
  member_disappears.help_text = ugettext(
      'Members include mentors, administrators, and the like.')

  encourage_contribs = db.TextProperty(required=True, verbose_name=ugettext(
      'What steps will you take to encourage contributors to interact with'
      ' your community before, during, and after the program?'))
  encourage_contribs.help_text = contrib_disappears.help_text

  continued_contribs = db.TextProperty(required=True, verbose_name=ugettext(
      'What will you do to ensure that your accepted contributors stick'
      ' with the project after the program concludes?'))
  continued_contribs.help_text = contrib_disappears.help_text

  #: field storing whether the User has agreed to the site-wide Terms of Service.
  #: (Not a required field because the Terms of Service might not be present
  #: when the first User profile is created when bootstrapping the site.)
  agreed_to_admin_agreement = db.BooleanProperty(required=False, default=False,
      verbose_name=ugettext('I Agree to the Admin Agreement'))
  agreed_to_admin_agreement.help_text = ugettext(
      'Indicates whether the user agreed to the Admin Agreement.')
<|endoftext|>


<commit_before><commit_msg>Casts vals to string for set and back when get<commit_after><|endoftext|>


<commit_before>from __future__ import annotations as ann

from typing import List, Optional, Union

import numpy as np
from pyquaternion import Quaternion

from paralleldomain.utilities.coordinate_system import INTERNAL_COORDINATE_SYSTEM, CoordinateSystem

class Transformation:
    def __init__(self, quaternion: Quaternion = None, translation: Union[np.ndarray, List] = None):
        self._Rq = quaternion if quaternion is not None else Quaternion(w=1, x=0, y=0, z=0)
        self._t = (
            np.asarray(translation).reshape(
                3,
            )
            if translation is not None
            else np.array([0, 0, 0])
        )

    def __repr__(self):
        rep = f"R: {self.rpy}, t: {self.translation}"
        return rep

    def __matmul__(self, other) -> Union["Transformation", np.ndarray]:
        convert_to_transform = True
        if isinstance(other, Transformation):
            transform = self.transformation_matrix @ other.transformation_matrix
        elif isinstance(other, np.ndarray):
            transform = self.transformation_matrix @ other
            convert_to_transform = other.shape == (4, 4)
        else:
            raise ValueError(f"Invalid value {other}! Has to be a Transformation or 4x4 numpy array!")
        if convert_to_transform:
            return Transformation.from_transformation_matrix(mat=transform)
        return transform

    def __rmatmul__(self, other) -> Union["Transformation", np.ndarray]:
        convert_to_transform = True
        if isinstance(other, Transformation):
            transform = other.transformation_matrix @ self.transformation_matrix
        elif isinstance(other, np.ndarray):
            transform = other @ self.transformation_matrix
            convert_to_transform = other.shape == (4, 4)
        else:
            raise ValueError(f"Invalid value {other}! Has to be a Transformation or 4x4 numpy array!")

        if convert_to_transform:
            return Transformation.from_transformation_matrix(mat=transform)
        return transform

    @property
    def transformation_matrix(self) -> np.ndarray:
        matrix = np.eye(4)
        matrix[:3, :3] = self.rotation
        matrix[:3, 3] = self.translation
        return matrix

    @property
    def rotation(self) -> np.ndarray:
        return self._Rq.rotation_matrix

    @property
    def quaternion(self) -> Quaternion:
        return self._Rq

    @property
    def rotation_quaternion(self) -> np.ndarray:
        return self._Rq.elements

    @property
    def rpy(self) -> List[float]:
        return [
            self._Rq.yaw_pitch_roll[2],
            self._Rq.yaw_pitch_roll[1],
            self._Rq.yaw_pitch_roll[0],
        ]

    @property
    def translation(self) -> np.ndarray:
        return self._t

    @property
    def inverse(self) -> "Transformation":
        q_inv = self.quaternion.inverse
        t_inv = -1 * q_inv.rotate(self.translation)
        T_inv = Transformation(q_inv, t_inv)
        return T_inv

    @classmethod
    def from_transformation_matrix(cls, mat: np.ndarray) -> "Transformation":
        quat = Quaternion(matrix=mat)
        translation = mat[:3, 3]
        return cls(quaternion=quat, translation=translation)

    @classmethod
    def from_euler_angles(
        cls,
        yaw: float,
        pitch: float,
        roll: float,
        translation: Optional[np.ndarray] = None,
        is_degrees: bool = False,
        order: str = "rpy",
        coordinate_system: Optional[Union[str, CoordinateSystem]] = None,
    ) -> "Transformation":
        if translation is None:
            translation = np.array([0.0, 0.0, 0.0])

        if coordinate_system is None:
            coordinate_system = INTERNAL_COORDINATE_SYSTEM
        elif isinstance(coordinate_system, str):
            coordinate_system = CoordinateSystem(axis_directions=coordinate_system)

        quat = coordinate_system.quaternion_from_rpy(
            yaw=yaw, pitch=pitch, roll=roll, is_degrees=is_degrees, order=order
        )
        return cls(quaternion=quat, translation=translation)
<commit_msg>Transformation casts from list to np.ndarray Constructor has default values for no rotation / translation<commit_after>from __future__ import annotations as ann

from typing import List, Optional, Union

import numpy as np
from pyquaternion import Quaternion

from paralleldomain.utilities.coordinate_system import INTERNAL_COORDINATE_SYSTEM, CoordinateSystem

class Transformation:
    def __init__(self, quaternion: Quaternion = None, translation: Union[np.ndarray, List] = None):
        self._Rq = quaternion if quaternion is not None else Quaternion(w=1, x=0, y=0, z=0)
        self._t = np.asarray(translation).reshape(3) if translation is not None else np.array([0, 0, 0])

    def __repr__(self):
        rep = f"R: {self.rpy}, t: {self.translation}"
        return rep

    def __matmul__(self, other) -> Union["Transformation", np.ndarray]:
        convert_to_transform = True
        if isinstance(other, Transformation):
            transform = self.transformation_matrix @ other.transformation_matrix
        elif isinstance(other, np.ndarray):
            transform = self.transformation_matrix @ other
            convert_to_transform = other.shape == (4, 4)
        else:
            raise ValueError(f"Invalid value {other}! Has to be a Transformation or 4x4 numpy array!")
        if convert_to_transform:
            return Transformation.from_transformation_matrix(mat=transform)
        return transform

    def __rmatmul__(self, other) -> Union["Transformation", np.ndarray]:
        convert_to_transform = True
        if isinstance(other, Transformation):
            transform = other.transformation_matrix @ self.transformation_matrix
        elif isinstance(other, np.ndarray):
            transform = other @ self.transformation_matrix
            convert_to_transform = other.shape == (4, 4)
        else:
            raise ValueError(f"Invalid value {other}! Has to be a Transformation or 4x4 numpy array!")

        if convert_to_transform:
            return Transformation.from_transformation_matrix(mat=transform)
        return transform

    @property
    def transformation_matrix(self) -> np.ndarray:
        matrix = np.eye(4)
        matrix[:3, :3] = self.rotation
        matrix[:3, 3] = self.translation
        return matrix

    @property
    def rotation(self) -> np.ndarray:
        return self._Rq.rotation_matrix

    @property
    def quaternion(self) -> Quaternion:
        return self._Rq

    @property
    def rotation_quaternion(self) -> np.ndarray:
        return self._Rq.elements

    @property
    def rpy(self) -> List[float]:
        return [
            self._Rq.yaw_pitch_roll[2],
            self._Rq.yaw_pitch_roll[1],
            self._Rq.yaw_pitch_roll[0],
        ]

    @property
    def translation(self) -> np.ndarray:
        return self._t

    @property
    def inverse(self) -> "Transformation":
        q_inv = self.quaternion.inverse
        t_inv = -1 * q_inv.rotate(self.translation)
        T_inv = Transformation(q_inv, t_inv)
        return T_inv

    @classmethod
    def from_transformation_matrix(cls, mat: np.ndarray) -> "Transformation":
        quat = Quaternion(matrix=mat)
        translation = mat[:3, 3]
        return cls(quaternion=quat, translation=translation)

    @classmethod
    def from_euler_angles(
        cls,
        yaw: float,
        pitch: float,
        roll: float,
        translation: Optional[np.ndarray] = None,
        is_degrees: bool = False,
        order: str = "rpy",
        coordinate_system: Optional[Union[str, CoordinateSystem]] = None,
    ) -> "Transformation":
        if translation is None:
            translation = np.array([0.0, 0.0, 0.0])

        if coordinate_system is None:
            coordinate_system = INTERNAL_COORDINATE_SYSTEM
        elif isinstance(coordinate_system, str):
            coordinate_system = CoordinateSystem(axis_directions=coordinate_system)

        quat = coordinate_system.quaternion_from_rpy(
            yaw=yaw, pitch=pitch, roll=roll, is_degrees=is_degrees, order=order
        )
        return cls(quaternion=quat, translation=translation)
<|endoftext|>


<commit_before>'''
Validates a user entered pattern against CyboxPattern and PCRE grammars.
'''

from __future__ import print_function
import argparse
from antlr4 import InputStream
from antlr4 import CommonTokenStream
from antlr4.error.ErrorListener import ErrorListener
from grammars.CyboxPatternLexer import CyboxPatternLexer
from grammars.CyboxPatternParser import CyboxPatternParser
from grammars.CyboxPatternListener import CyboxPatternListener
from grammars.PCRELexer import PCRELexer
from grammars.PCREParser import PCREParser

class CyboxPatternErrorListener(ErrorListener):
    '''
    Modifies ErrorListener to collect error message and set flag to False when
    invalid pattern is encountered.
    '''
    def __init__(self):
        super(CyboxPatternErrorListener, self).__init__()
        self.err_strings = []

    def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
        self.err_strings.append("FAIL: Error found at line %d:%d. %s" %
                                (line, column, msg))

class PCREErrorListener(ErrorListener):
    '''
    Modifies ErrorListener for PCRE to collect a more appropriate error
    message.
    '''
    def __init__(self):
        super(PCREErrorListener, self).__init__()
        self.err_strings = []

    def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
        self.err_strings.append("FAIL: Regex error found. %s." % msg)

class REGEXValidatorListener(CyboxPatternListener):
    """
    A parser listener which triggers when the parse process encounters regex
    literals.  This listener will validate the regexes and collect error
    messages into a list.
    """
    def __init__(self):
        self.err_strings = []

    def exitRegexLiteral(self, ctx):
        # returns a TerminalNode; we need the actual token string
        regex = str(ctx.RegexLiteral())

        # strip leading/trailing "/"
        in_ = InputStream(regex[1:-1])
        lexer = PCRELexer(in_)
        lexer.removeErrorListeners()
        stream = CommonTokenStream(lexer)
        pcre_parser = PCREParser(stream)
        pcre_parser.buildParseTrees = False
        pcre_parser.removeErrorListeners()
        errListener = PCREErrorListener()
        pcre_parser.addErrorListener(errListener)
        pcre_parser.alternation()

        self.err_strings.extend(err + ' REGEX: ' + regex
                                for err in errListener.err_strings)

def run_validator(pattern):
    '''
    Validates a pattern against the CybOX grammar.  Error messages are
    returned in a list.  The test passed if the returned list is empty.
    '''

    if isinstance(pattern, (str, unicode)):
        pattern = InputStream(pattern)

    parseErrListener = CyboxPatternErrorListener()

    lexer = CyboxPatternLexer(pattern)
    # it always adds a console listener by default... remove it.
    lexer.removeErrorListeners()

    stream = CommonTokenStream(lexer)

    parser = CyboxPatternParser(stream)
    parser.buildParseTrees = False
    # it always adds a console listener by default... remove it.
    parser.removeErrorListeners()
    parser.addErrorListener(parseErrListener)
    regexListener = REGEXValidatorListener()
    parser.addParseListener(regexListener)

    parser.startExpression()

    err_strings = parseErrListener.err_strings + regexListener.err_strings

    return err_strings

def validate(user_input, ret_errs=False, print_errs=False):
    '''
    Wrapper for run_validator function that returns True if the user_input
    contains a valid CybOX pattern or False otherwise. The error messages may
    also be returned or printed based upon the ret_errs and print_errs arg
    values.
    '''

    errs = run_validator(user_input)
    passed = len(errs) == 0

    if print_errs:
        for err in errs:
            print(err)

    if ret_errs:
        return passed, errs

    return passed

def main():
    '''
    Continues to validate patterns until it encounters EOF within a pattern
    file or Ctrl-C is pressed by the user.
    '''
    parser = argparse.ArgumentParser(
        description='Validate CybOX Patterns.')
    parser.add_argument('-f', '--file',
                        help="Specify this arg to read patterns from a file.",
                        type=argparse.FileType("r"))
    args = parser.parse_args()

    pass_count = fail_count = 0

    # I tried using a generator (where each iteration would run raw_input()),
    # but raw_input()'s behavior seems to change when called from within a
    # generator: I only get one line, then the generator completes!  I don't
    # know why behavior changes...
    import functools
    if args.file:
        nextpattern = args.file.readline
    else:
        nextpattern = functools.partial(raw_input, "Enter a pattern to validate: ")

    try:
        while True:
            pattern = nextpattern()
            if not pattern: break
            tests_passed, err_strings = validate(pattern, True)

            if tests_passed:
                print("\nPASS: %s" % pattern)
                pass_count += 1

            else:
                for err in err_strings:
                    print(err, '\n')
                fail_count += 1
    except (EOFError, KeyboardInterrupt):
        pass
    finally:
        if args.file:
            args.file.close()

    print("\nPASSED:", pass_count, " patterns")
    print("FAILED:", fail_count, " patterns")

if __name__ == '__main__':
    main()
<commit_msg>Use new pattern expression rule name<commit_after>'''
Validates a user entered pattern against CyboxPattern and PCRE grammars.
'''

from __future__ import print_function
import argparse
from antlr4 import InputStream
from antlr4 import CommonTokenStream
from antlr4.error.ErrorListener import ErrorListener
from grammars.CyboxPatternLexer import CyboxPatternLexer
from grammars.CyboxPatternParser import CyboxPatternParser
from grammars.CyboxPatternListener import CyboxPatternListener
from grammars.PCRELexer import PCRELexer
from grammars.PCREParser import PCREParser

class CyboxPatternErrorListener(ErrorListener):
    '''
    Modifies ErrorListener to collect error message and set flag to False when
    invalid pattern is encountered.
    '''
    def __init__(self):
        super(CyboxPatternErrorListener, self).__init__()
        self.err_strings = []

    def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
        self.err_strings.append("FAIL: Error found at line %d:%d. %s" %
                                (line, column, msg))

class PCREErrorListener(ErrorListener):
    '''
    Modifies ErrorListener for PCRE to collect a more appropriate error
    message.
    '''
    def __init__(self):
        super(PCREErrorListener, self).__init__()
        self.err_strings = []

    def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
        self.err_strings.append("FAIL: Regex error found. %s." % msg)

class REGEXValidatorListener(CyboxPatternListener):
    """
    A parser listener which triggers when the parse process encounters regex
    literals.  This listener will validate the regexes and collect error
    messages into a list.
    """
    def __init__(self):
        self.err_strings = []

    def exitRegexLiteral(self, ctx):
        # returns a TerminalNode; we need the actual token string
        regex = str(ctx.RegexLiteral())

        # strip leading/trailing "/"
        in_ = InputStream(regex[1:-1])
        lexer = PCRELexer(in_)
        lexer.removeErrorListeners()
        stream = CommonTokenStream(lexer)
        pcre_parser = PCREParser(stream)
        pcre_parser.buildParseTrees = False
        pcre_parser.removeErrorListeners()
        errListener = PCREErrorListener()
        pcre_parser.addErrorListener(errListener)
        pcre_parser.alternation()

        self.err_strings.extend(err + ' REGEX: ' + regex
                                for err in errListener.err_strings)

def run_validator(pattern):
    '''
    Validates a pattern against the CybOX grammar.  Error messages are
    returned in a list.  The test passed if the returned list is empty.
    '''

    if isinstance(pattern, (str, unicode)):
        pattern = InputStream(pattern)

    parseErrListener = CyboxPatternErrorListener()

    lexer = CyboxPatternLexer(pattern)
    # it always adds a console listener by default... remove it.
    lexer.removeErrorListeners()

    stream = CommonTokenStream(lexer)

    parser = CyboxPatternParser(stream)
    parser.buildParseTrees = False
    # it always adds a console listener by default... remove it.
    parser.removeErrorListeners()
    parser.addErrorListener(parseErrListener)
    regexListener = REGEXValidatorListener()
    parser.addParseListener(regexListener)

    parser.pattern()

    err_strings = parseErrListener.err_strings + regexListener.err_strings

    return err_strings

def validate(user_input, ret_errs=False, print_errs=False):
    '''
    Wrapper for run_validator function that returns True if the user_input
    contains a valid CybOX pattern or False otherwise. The error messages may
    also be returned or printed based upon the ret_errs and print_errs arg
    values.
    '''

    errs = run_validator(user_input)
    passed = len(errs) == 0

    if print_errs:
        for err in errs:
            print(err)

    if ret_errs:
        return passed, errs

    return passed

def main():
    '''
    Continues to validate patterns until it encounters EOF within a pattern
    file or Ctrl-C is pressed by the user.
    '''
    parser = argparse.ArgumentParser(
        description='Validate CybOX Patterns.')
    parser.add_argument('-f', '--file',
                        help="Specify this arg to read patterns from a file.",
                        type=argparse.FileType("r"))
    args = parser.parse_args()

    pass_count = fail_count = 0

    # I tried using a generator (where each iteration would run raw_input()),
    # but raw_input()'s behavior seems to change when called from within a
    # generator: I only get one line, then the generator completes!  I don't
    # know why behavior changes...
    import functools
    if args.file:
        nextpattern = args.file.readline
    else:
        nextpattern = functools.partial(raw_input, "Enter a pattern to validate: ")

    try:
        while True:
            pattern = nextpattern()
            if not pattern: break
            tests_passed, err_strings = validate(pattern, True)

            if tests_passed:
                print("\nPASS: %s" % pattern)
                pass_count += 1

            else:
                for err in err_strings:
                    print(err, '\n')
                fail_count += 1
    except (EOFError, KeyboardInterrupt):
        pass
    finally:
        if args.file:
            args.file.close()

    print("\nPASSED:", pass_count, " patterns")
    print("FAILED:", fail_count, " patterns")

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


<commit_before>import pandas as pd
import glob
import os.path as op

import nibabel as nib
import dipy.core.gradients as dpg
from dipy.segment.mask import median_otsu

def do_preprocessing():
    raise NotImplementedError

class AFQ(object):
    """
    Requires the following file structure:

    study_folder/
                sub-01
                sub-02/sess-test
                sub-02/sess-retest/anat/T1w.nii.gz
                sub-02/sess-retest/dwi/dwi.nii.gz
                sub-02/sess-retest/dwi/dwi.bvals
                sub-02/sess-retest/dwi/dwi.bvecs

    All subjects'/sessions' dwi_prefix needs to be the same!

    That is, you *can't* have:

        sub-01/sess-test/dwi/dwi.nii.gz
        sub-02/sess-retest/dwi/dwi_foo.nii.gz

    Even though these are different subjects/sessions/etc.

    Instead you can (and must!) have:

        sub-01/sess-test/dwi/mydata_foo.nii.gz
        sub-02/sess-retest/dwi/mydata_foo.nii.gz

    Where any part of this string (e.g. "foo") can report on things like the
    number of directions, the preprocessing that happened, etc.

    """
    def __init__(self, raw_path=None, preproc_path=None,
                 sub_prefix="sub", dwi_folder="dwi",
                 dwi_file="*dwi*", anat_folder="anat",
                 anat_file="*T1w*"):
        self.raw_path = raw_path
        self.preproc_path = preproc_path
        if self.preproc_path is None:
            if self.raw_path is None:
                e_s = "must provide either preproc_path or raw_path (or both)"
                raise ValueError(e_s)
            # This creates the preproc_path such that:
            self.preproc_path = do_preprocessing(self.raw_path)
        # This is the place in which each subject's full data lives
        self.subject_dirs = glob.glob(op.join(preproc_path,
                                              '%s*' % sub_prefix))
        self.subjects = [op.split(p)[-1] for p in self.subject_dirs]
        sub_list = []
        sess_list = []
        dwi_file_list = []
        bvec_file_list = []
        bval_file_list = []
        anat_file_list = []
        for subject, sub_dir in zip(self.subjects, self.subject_dirs):
            sessions = glob.glob(op.join(sub_dir, '*'))
            for sess in sessions:
                dwi_file_list.append(glob.glob(op.join(sub_dir,
                                                       ('%s/%s/%s.nii.gz' %
                                                        (sess, dwi_folder,
                                                         dwi_file))))[0])

                bvec_file_list.append(glob.glob(op.join(sub_dir,
                                                        ('%s/%s/%s.bvec*' %
                                                         (sess, dwi_folder,
                                                          dwi_file))))[0])

                bval_file_list.append(glob.glob(op.join(sub_dir,
                                                        ('%s/%s/%s.bval*' %
                                                         (sess, dwi_folder,
                                                          dwi_file))))[0])

                anat_file_list.append(glob.glob(op.join(sub_dir,
                                                        ('%s/%s/%s.nii.gz' %
                                                         (sess,
                                                          anat_folder,
                                                          anat_file))))[0])
                sub_list.append(subject)
                sess_list.append(sess)

        self.data_frame = pd.DataFrame(dict(subjects=sub_list,
                                            dwi_file=dwi_file_list,
                                            bvec_file=bvec_file_list,
                                            bval_file=bval_file_list,
                                            anat_file=anat_file_list,
                                            sess=sess_list))
        self.init_gtab()
        self.init_affine()

    def init_affine(self):
        affine_list = []
        for fdwi in self.data_frame['dwi_file']:
            affine_list.append(nib.load(fdwi).get_affine())
        self.data_frame['dwi_affine'] = affine_list

    def init_gtab(self):
        gtab_list = []
        for fbval, fbvec in zip(self.data_frame['bval_file'],
                                self.data_frame['bvec_file']):
            gtab_list.append(dpg.gradient_table(fbval, fbvec))
        self.data_frame['gtab'] = gtab_list

    def brain_extraction(self, median_radius=4, numpass=4, autocrop=False,
                         vol_idx=None, dilate=None, force_recompute=False):

        if 'brain_mask' in self.data_frame.columns:
            if not force_recompute:
                return

        self.data_frame['brain_mask'] = np.nan
        if 'brain_mask_file' in self.data_frame.columns:
            if not force_recompute:
                for be_fname in self.data_frame['brain_mask_file']:
                    self.data_frame['brain_mask'] =\
                                nib.load(be_fname).get_data()
                    return
        self.data_frame['brain_mask_file'] = np.nan

        for gtab, fdwi in zip(self.data_frame['gtab'],
                              self.data_frame['dwi_file']):
            split_fdwi = op.split(fdwi)
            be_fname = op.join(split_fdwi[0], split_fdwi[1].split('.')[0],
                               '_brain_mask.nii.gz')
            self.data_frame['brain_mask_file'][
                    self.data_frame['dwi_file'] == fdwi] = be_fname

            if not op.exists(be_fname):
                _, brain_mask = median_otsu(mean_b0, median_radius, numpass,
                                            autocrop, dilate=dilate)
                nib.save(nib.Nifti1Image(brain_mask.astype(int), img.affine),
                         be_fname)

            else:
                self.data_frame['brain_mask'] =\
                 nib.load(be_fname).get_data()

    def fit_tensors():
        raise NotImplementedError
<commit_msg>PEP8: Deflake the code. Next we'll probably need to shore up testing here.<commit_after>import pandas as pd
import glob
import os.path as op

import numpy as np

import nibabel as nib
import dipy.core.gradients as dpg
from dipy.segment.mask import median_otsu

def do_preprocessing():
    raise NotImplementedError

class AFQ(object):
    """
    Requires the following file structure:

    study_folder/
                sub-01
                sub-02/sess-test
                sub-02/sess-retest/anat/T1w.nii.gz
                sub-02/sess-retest/dwi/dwi.nii.gz
                sub-02/sess-retest/dwi/dwi.bvals
                sub-02/sess-retest/dwi/dwi.bvecs

    All subjects'/sessions' dwi_prefix needs to be the same!

    That is, you *can't* have:

        sub-01/sess-test/dwi/dwi.nii.gz
        sub-02/sess-retest/dwi/dwi_foo.nii.gz

    Even though these are different subjects/sessions/etc.

    Instead you can (and must!) have:

        sub-01/sess-test/dwi/mydata_foo.nii.gz
        sub-02/sess-retest/dwi/mydata_foo.nii.gz

    Where any part of this string (e.g. "foo") can report on things like the
    number of directions, the preprocessing that happened, etc.

    """
    def __init__(self, raw_path=None, preproc_path=None,
                 sub_prefix="sub", dwi_folder="dwi",
                 dwi_file="*dwi*", anat_folder="anat",
                 anat_file="*T1w*"):
        self.raw_path = raw_path
        self.preproc_path = preproc_path
        if self.preproc_path is None:
            if self.raw_path is None:
                e_s = "must provide either preproc_path or raw_path (or both)"
                raise ValueError(e_s)
            # This creates the preproc_path such that:
            self.preproc_path = do_preprocessing(self.raw_path)
        # This is the place in which each subject's full data lives
        self.subject_dirs = glob.glob(op.join(preproc_path,
                                              '%s*' % sub_prefix))
        self.subjects = [op.split(p)[-1] for p in self.subject_dirs]
        sub_list = []
        sess_list = []
        dwi_file_list = []
        bvec_file_list = []
        bval_file_list = []
        anat_file_list = []
        for subject, sub_dir in zip(self.subjects, self.subject_dirs):
            sessions = glob.glob(op.join(sub_dir, '*'))
            for sess in sessions:
                dwi_file_list.append(glob.glob(op.join(sub_dir,
                                                       ('%s/%s/%s.nii.gz' %
                                                        (sess, dwi_folder,
                                                         dwi_file))))[0])

                bvec_file_list.append(glob.glob(op.join(sub_dir,
                                                        ('%s/%s/%s.bvec*' %
                                                         (sess, dwi_folder,
                                                          dwi_file))))[0])

                bval_file_list.append(glob.glob(op.join(sub_dir,
                                                        ('%s/%s/%s.bval*' %
                                                         (sess, dwi_folder,
                                                          dwi_file))))[0])

                anat_file_list.append(glob.glob(op.join(sub_dir,
                                                        ('%s/%s/%s.nii.gz' %
                                                         (sess,
                                                          anat_folder,
                                                          anat_file))))[0])
                sub_list.append(subject)
                sess_list.append(sess)

        self.data_frame = pd.DataFrame(dict(subjects=sub_list,
                                            dwi_file=dwi_file_list,
                                            bvec_file=bvec_file_list,
                                            bval_file=bval_file_list,
                                            anat_file=anat_file_list,
                                            sess=sess_list))
        self.init_gtab()
        self.init_affine()

    def init_affine(self):
        affine_list = []
        for fdwi in self.data_frame['dwi_file']:
            affine_list.append(nib.load(fdwi).get_affine())
        self.data_frame['dwi_affine'] = affine_list

    def init_gtab(self):
        gtab_list = []
        for fbval, fbvec in zip(self.data_frame['bval_file'],
                                self.data_frame['bvec_file']):
            gtab_list.append(dpg.gradient_table(fbval, fbvec))
        self.data_frame['gtab'] = gtab_list

    def brain_extraction(self, median_radius=4, numpass=4, autocrop=False,
                         vol_idx=None, dilate=None, force_recompute=False):

        if 'brain_mask' in self.data_frame.columns:
            if not force_recompute:
                return

        self.data_frame['brain_mask'] = np.nan
        if 'brain_mask_file' in self.data_frame.columns:
            if not force_recompute:
                for be_fname in self.data_frame['brain_mask_file']:
                    self.data_frame['brain_mask'] =\
                                nib.load(be_fname).get_data()
                    return
        self.data_frame['brain_mask_file'] = np.nan

        for gtab, fdwi in zip(self.data_frame['gtab'],
                              self.data_frame['dwi_file']):
            split_fdwi = op.split(fdwi)
            be_fname = op.join(split_fdwi[0], split_fdwi[1].split('.')[0],
                               '_brain_mask.nii.gz')
            self.data_frame['brain_mask_file'][
                    self.data_frame['dwi_file'] == fdwi] = be_fname

            if not op.exists(be_fname):
                img = nib.load(fdwi)
                data = img.get_data()
                mean_b0 = np.mean(data[..., ~gtab.b0s_mask], -1)
                _, brain_mask = median_otsu(mean_b0, median_radius, numpass,
                                            autocrop, dilate=dilate)
                nib.save(nib.Nifti1Image(brain_mask.astype(int),
                                         img.affine),
                         be_fname)

            else:
                self.data_frame['brain_mask'] =\
                 nib.load(be_fname).get_data()

    def fit_tensors():
        raise NotImplementedError
<|endoftext|>


<commit_before>import tempfile
import os
import os.path as op
import numpy as np
import IPython.display as display
import imageio as io
import itertools

import nibabel as nib
from dipy.viz import window, actor, ui, regtools

from palettable.tableau import Tableau_20

import AFQ.utils.volume as auv
import AFQ.registration as reg

def _inline_interact(scene, inline, interact):
    """
    Helper function to reuse across viz functions
    """
    if interact:
        window.show(scene)

    if inline:
        tdir = tempfile.gettempdir()
        fname = op.join(tdir, "fig.png")
        window.snapshot(scene, fname=fname, size=(1200, 1200))
        display.display_png(display.Image(fname))

    return scene

def visualize_bundles(trk, affine_or_mapping=None, bundle=None, scene=None,
                      color=None, inline=False, interact=False,
                      face_forward=False):
    """
    Visualize bundles in 3D using VTK
    """
    if isinstance(trk, str):
        trk = nib.streamlines.load(trk)
        tg = trk.tractogram
    else:
        # Assume these are streamlines (as list or Streamlines object):
        tg = nib.streamlines.Tractogram(trk)

    if affine_or_mapping is not None:
        tg = tg.apply_affine(np.linalg.inv(affine_or_mapping))

    streamlines = tg.streamlines

    if scene is None:
        scene = window.Scene()

    # There are no bundles in here:
    if list(tg.data_per_streamline.keys()) == []:
        streamlines = list(streamlines)
        sl_actor = actor.line(streamlines, [c for i, c in zip(range(len(streamlines)), regtools.distinguishable_colormap())])
        scene.add(sl_actor)
        sl_actor.GetProperty().SetRenderLinesAsTubes(1)
        sl_actor.GetProperty().SetLineWidth(6)
    if bundle is None:
        for b in np.unique(tg.data_per_streamline['bundle']):
            idx = np.where(tg.data_per_streamline['bundle'] == b)[0]
            this_sl = list(streamlines[idx])
            if color is not None:
                sl_actor = actor.line(this_sl, color)
                sl_actor.GetProperty().SetRenderLinesAsTubes(1)
                sl_actor.GetProperty().SetLineWidth(6)
            else:
                sl_actor = actor.line(this_sl,
                                      Tableau_20.colors[np.mod(20, int(b))])
                sl_actor.GetProperty().SetRenderLinesAsTubes(1)
                sl_actor.GetProperty().SetLineWidth(6)

            scene.add(sl_actor)
    else:
        idx = np.where(tg.data_per_streamline['bundle'] == bundle)[0]
        this_sl = list(streamlines[idx])
        if color is not None:
            sl_actor = actor.line(this_sl, color)
            sl_actor.GetProperty().SetRenderLinesAsTubes(1)
            sl_actor.GetProperty().SetLineWidth(6)

        else:
            sl_actor = actor.line(
                this_sl,
                Tableau_20.colors[np.mod(20, int(bundle))])
            sl_actor.GetProperty().SetRenderLinesAsTubes(1)
            sl_actor.GetProperty().SetLineWidth(6)
        scene.add(sl_actor)

    if face_forward:
        scene.elevation(90)
        scene.set_camera(view_up=(0.0, 0.0, 1.0))
        scene.reset_camera()

    return _inline_interact(scene, inline, interact)

def create_gif(scene, file_name):
    tdir = tempfile.gettempdir()
    window.record(scene, az_ang=3.6, n_frames=100,
                  path_numbering=True, out_path=tdir + '/tgif',
                  size=(1200, 1200))

    angles = []
    for i in range(100):
        if i < 10:
            angle_fname = f"tgif00000{i}.png"
        else:
            angle_fname = f"tgif0000{i}.png"
        angles.append(io.imread(os.path.join(tdir, angle_fname)))

    io.mimsave(file_name, angles)

def visualize_roi(roi, affine_or_mapping=None, static_img=None,
                  roi_affine=None, static_affine=None, reg_template=None,
                  scene=None, color=None, inline=True, interact=False):
    """
    Render a region of interest into a VTK viz as a volume
    """
    if not isinstance(roi, np.ndarray):
        if isinstance(roi, str):
            roi = nib.load(roi).get_fdata()
        else:
            roi = roi.get_fdata()

    if affine_or_mapping is not None:
        if isinstance(affine_or_mapping, np.ndarray):
            # This is an affine:
            if (static_img is None or roi_affine is None
                    or static_affine is None):
                raise ValueError("If using an affine to transform an ROI, "
                                 "need to also specify all of the following",
                                 "inputs: `static_img`, `roi_affine`, ",
                                 "`static_affine`")
            roi = reg.resample(roi, static_img, roi_affine, static_affine)
        else:
            # Assume it is  a mapping:
            if (isinstance(affine_or_mapping, str)
                    or isinstance(affine_or_mapping, nib.Nifti1Image)):
                if reg_template is None or static_img is None:
                    raise ValueError(
                        "If using a mapping to transform an ROI, need to ",
                        "also specify all of the following inputs: ",
                        "`reg_template`, `static_img`")
                affine_or_mapping = reg.read_mapping(affine_or_mapping,
                                                     static_img,
                                                     reg_template)

            roi = auv.patch_up_roi(affine_or_mapping.transform_inverse(
                                   roi,
                                   interpolation='nearest')).astype(bool)

    if scene is None:
        scene = window.Scene()

    roi_actor = actor.contour_from_roi(roi, color=color)
    scene.add(roi_actor)

    if inline:
        tdir = tempfile.gettempdir()
        fname = op.join(tdir, "fig.png")
        window.snapshot(scene, fname=fname)
        display.display_png(display.Image(fname))

    return _inline_interact(scene, inline, interact)

def visualize_volume(volume, x=None, y=None, z=None, scene=None, inline=True,
                     interact=False):
    """
    Visualize a volume
    """
    if scene is None:
        scene = window.Scene()

    shape = volume.shape
    image_actor_z = actor.slicer(volume)
    slicer_opacity = 0.6
    image_actor_z.opacity(slicer_opacity)

    image_actor_x = image_actor_z.copy()
    x_midpoint = int(np.round(shape[0] / 2))
    image_actor_x.display_extent(x_midpoint,
                                 x_midpoint, 0,
                                 shape[1] - 1,
                                 0,
                                 shape[2] - 1)

    image_actor_y = image_actor_z.copy()
    y_midpoint = int(np.round(shape[1] / 2))
    image_actor_y.display_extent(0,
                                 shape[0] - 1,
                                 y_midpoint,
                                 y_midpoint,
                                 0,
                                 shape[2] - 1)

    scene.add(image_actor_z)
    scene.add(image_actor_x)
    scene.add(image_actor_y)

    show_m = window.ShowManager(scene, size=(1200, 900))
    show_m.initialize()

    line_slider_z = ui.LineSlider2D(min_value=0,
                                    max_value=shape[2] - 1,
                                    initial_value=shape[2] / 2,
                                    text_template="{value:.0f}",
                                    length=140)

    line_slider_x = ui.LineSlider2D(min_value=0,
                                    max_value=shape[0] - 1,
                                    initial_value=shape[0] / 2,
                                    text_template="{value:.0f}",
                                    length=140)

    line_slider_y = ui.LineSlider2D(min_value=0,
                                    max_value=shape[1] - 1,
                                    initial_value=shape[1] / 2,
                                    text_template="{value:.0f}",
                                    length=140)

    opacity_slider = ui.LineSlider2D(min_value=0.0,
                                     max_value=1.0,
                                     initial_value=slicer_opacity,
                                     length=140)

    def change_slice_z(slider):
        z = int(np.round(slider.value))
        image_actor_z.display_extent(0, shape[0] - 1, 0, shape[1] - 1, z, z)

    def change_slice_x(slider):
        x = int(np.round(slider.value))
        image_actor_x.display_extent(x, x, 0, shape[1] - 1, 0, shape[2] - 1)

    def change_slice_y(slider):
        y = int(np.round(slider.value))
        image_actor_y.display_extent(0, shape[0] - 1, y, y, 0, shape[2] - 1)

    def change_opacity(slider):
        slicer_opacity = slider.value
        image_actor_z.opacity(slicer_opacity)
        image_actor_x.opacity(slicer_opacity)
        image_actor_y.opacity(slicer_opacity)

    line_slider_z.on_change = change_slice_z
    line_slider_x.on_change = change_slice_x
    line_slider_y.on_change = change_slice_y
    opacity_slider.on_change = change_opacity

    def build_label(text):
        label = ui.TextBlock2D()
        label.message = text
        label.font_size = 18
        label.font_family = 'Arial'
        label.justification = 'left'
        label.bold = False
        label.italic = False
        label.shadow = False
        label.background = (0, 0, 0)
        label.color = (1, 1, 1)

        return label

    line_slider_label_z = build_label(text="Z Slice")
    line_slider_label_x = build_label(text="X Slice")
    line_slider_label_y = build_label(text="Y Slice")
    opacity_slider_label = build_label(text="Opacity")

    panel = ui.Panel2D(size=(300, 200),
                       color=(1, 1, 1),
                       opacity=0.1,
                       align="right")
    panel.center = (1030, 120)

    panel.add_element(line_slider_label_x, (0.1, 0.75))
    panel.add_element(line_slider_x, (0.38, 0.75))
    panel.add_element(line_slider_label_y, (0.1, 0.55))
    panel.add_element(line_slider_y, (0.38, 0.55))
    panel.add_element(line_slider_label_z, (0.1, 0.35))
    panel.add_element(line_slider_z, (0.38, 0.35))
    panel.add_element(opacity_slider_label, (0.1, 0.15))
    panel.add_element(opacity_slider, (0.38, 0.15))

    show_m.scene.add(panel)

    global size
    size = scene.GetSize()

    def win_callback(obj, event):
        global size
        if size != obj.GetSize():
            size_old = size
            size = obj.GetSize()
            size_change = [size[0] - size_old[0], 0]
            panel.re_align(size_change)

    show_m.initialize()

    scene.zoom(1.5)
    scene.reset_clipping_range()

    if interact:
        show_m.add_window_callback(win_callback)
        show_m.render()
        show_m.start()

    return _inline_interact(scene, inline, interact)
<commit_msg>parameterized create gif; added if interact condition to sliders in volume<commit_after>import tempfile
import os
import os.path as op
import numpy as np
import IPython.display as display
import imageio as io
import itertools

import nibabel as nib
from dipy.viz import window, actor, ui, regtools

from palettable.tableau import Tableau_20

import AFQ.utils.volume as auv
import AFQ.registration as reg

def _inline_interact(scene, inline, interact):
    """
    Helper function to reuse across viz functions
    """
    if interact:
        window.show(scene)

    if inline:
        tdir = tempfile.gettempdir()
        fname = op.join(tdir, "fig.png")
        window.snapshot(scene, fname=fname, size=(1200, 1200))
        display.display_png(display.Image(fname))

    return scene

def visualize_bundles(trk, affine_or_mapping=None, bundle=None, scene=None,
                      color=None, inline=False, interact=False,
                      face_forward=False):
    """
    Visualize bundles in 3D using VTK
    """
    if isinstance(trk, str):
        trk = nib.streamlines.load(trk)
        tg = trk.tractogram
    else:
        # Assume these are streamlines (as list or Streamlines object):
        tg = nib.streamlines.Tractogram(trk)

    if affine_or_mapping is not None:
        tg = tg.apply_affine(np.linalg.inv(affine_or_mapping))

    streamlines = tg.streamlines

    if scene is None:
        scene = window.Scene()

    # There are no bundles in here:
    if list(tg.data_per_streamline.keys()) == []:
        streamlines = list(streamlines)
        sl_actor = actor.line(streamlines, [c for i, c in zip(range(len(streamlines)), regtools.distinguishable_colormap())])
        scene.add(sl_actor)
        sl_actor.GetProperty().SetRenderLinesAsTubes(1)
        sl_actor.GetProperty().SetLineWidth(6)
    if bundle is None:
        for b in np.unique(tg.data_per_streamline['bundle']):
            idx = np.where(tg.data_per_streamline['bundle'] == b)[0]
            this_sl = list(streamlines[idx])
            if color is not None:
                sl_actor = actor.line(this_sl, color)
                sl_actor.GetProperty().SetRenderLinesAsTubes(1)
                sl_actor.GetProperty().SetLineWidth(6)
            else:
                sl_actor = actor.line(this_sl,
                                      Tableau_20.colors[np.mod(20, int(b))])
                sl_actor.GetProperty().SetRenderLinesAsTubes(1)
                sl_actor.GetProperty().SetLineWidth(6)

            scene.add(sl_actor)
    else:
        idx = np.where(tg.data_per_streamline['bundle'] == bundle)[0]
        this_sl = list(streamlines[idx])
        if color is not None:
            sl_actor = actor.line(this_sl, color)
            sl_actor.GetProperty().SetRenderLinesAsTubes(1)
            sl_actor.GetProperty().SetLineWidth(6)

        else:
            sl_actor = actor.line(
                this_sl,
                Tableau_20.colors[np.mod(20, int(bundle))])
            sl_actor.GetProperty().SetRenderLinesAsTubes(1)
            sl_actor.GetProperty().SetLineWidth(6)
        scene.add(sl_actor)

    if face_forward:
        scene.elevation(90)
        scene.set_camera(view_up=(0.0, 0.0, 1.0))
        scene.reset_camera()

    return _inline_interact(scene, inline, interact)

def create_gif(scene, file_name, n_frames=60, size=(600, 600)):
    tdir = tempfile.gettempdir()
    window.record(scene, az_ang=360.0/n_frames, n_frames=n_frames,
                  path_numbering=True, out_path=tdir + '/tgif',
                  size=size)

    angles = []
    for i in range(n_frames):
        if i < 10:
            angle_fname = f"tgif00000{i}.png"
        elif i < 100:
            angle_fname = f"tgif0000{i}.png"
        else:
            angle_fname = f"tgif000{i}.png"
        angles.append(io.imread(os.path.join(tdir, angle_fname)))

    io.mimsave(file_name, angles)

def visualize_roi(roi, affine_or_mapping=None, static_img=None,
                  roi_affine=None, static_affine=None, reg_template=None,
                  scene=None, color=None, inline=True, interact=False):
    """
    Render a region of interest into a VTK viz as a volume
    """
    if not isinstance(roi, np.ndarray):
        if isinstance(roi, str):
            roi = nib.load(roi).get_fdata()
        else:
            roi = roi.get_fdata()

    if affine_or_mapping is not None:
        if isinstance(affine_or_mapping, np.ndarray):
            # This is an affine:
            if (static_img is None or roi_affine is None
                    or static_affine is None):
                raise ValueError("If using an affine to transform an ROI, "
                                 "need to also specify all of the following",
                                 "inputs: `static_img`, `roi_affine`, ",
                                 "`static_affine`")
            roi = reg.resample(roi, static_img, roi_affine, static_affine)
        else:
            # Assume it is  a mapping:
            if (isinstance(affine_or_mapping, str)
                    or isinstance(affine_or_mapping, nib.Nifti1Image)):
                if reg_template is None or static_img is None:
                    raise ValueError(
                        "If using a mapping to transform an ROI, need to ",
                        "also specify all of the following inputs: ",
                        "`reg_template`, `static_img`")
                affine_or_mapping = reg.read_mapping(affine_or_mapping,
                                                     static_img,
                                                     reg_template)

            roi = auv.patch_up_roi(affine_or_mapping.transform_inverse(
                                   roi,
                                   interpolation='nearest')).astype(bool)

    if scene is None:
        scene = window.Scene()

    roi_actor = actor.contour_from_roi(roi, color=color)
    scene.add(roi_actor)

    if inline:
        tdir = tempfile.gettempdir()
        fname = op.join(tdir, "fig.png")
        window.snapshot(scene, fname=fname)
        display.display_png(display.Image(fname))

    return _inline_interact(scene, inline, interact)

def visualize_volume(volume, x=None, y=None, z=None, scene=None, inline=True,
                     interact=False):
    """
    Visualize a volume
    """
    if scene is None:
        scene = window.Scene()

    shape = volume.shape
    image_actor_z = actor.slicer(volume)
    slicer_opacity = 0.6
    image_actor_z.opacity(slicer_opacity)

    image_actor_x = image_actor_z.copy()
    x_midpoint = int(np.round(shape[0] / 2))
    image_actor_x.display_extent(x_midpoint,
                                 x_midpoint, 0,
                                 shape[1] - 1,
                                 0,
                                 shape[2] - 1)

    image_actor_y = image_actor_z.copy()
    y_midpoint = int(np.round(shape[1] / 2))
    image_actor_y.display_extent(0,
                                 shape[0] - 1,
                                 y_midpoint,
                                 y_midpoint,
                                 0,
                                 shape[2] - 1)

    scene.add(image_actor_z)
    scene.add(image_actor_x)
    scene.add(image_actor_y)

    show_m = window.ShowManager(scene, size=(1200, 900))
    show_m.initialize()

    if interact:
        line_slider_z = ui.LineSlider2D(min_value=0,
                                        max_value=shape[2] - 1,
                                        initial_value=shape[2] / 2,
                                        text_template="{value:.0f}",
                                        length=140)

        line_slider_x = ui.LineSlider2D(min_value=0,
                                        max_value=shape[0] - 1,
                                        initial_value=shape[0] / 2,
                                        text_template="{value:.0f}",
                                        length=140)

        line_slider_y = ui.LineSlider2D(min_value=0,
                                        max_value=shape[1] - 1,
                                        initial_value=shape[1] / 2,
                                        text_template="{value:.0f}",
                                        length=140)

        opacity_slider = ui.LineSlider2D(min_value=0.0,
                                        max_value=1.0,
                                        initial_value=slicer_opacity,
                                        length=140)

        def change_slice_z(slider):
            z = int(np.round(slider.value))
            image_actor_z.display_extent(0, shape[0] - 1, 0, shape[1] - 1, z, z)

        def change_slice_x(slider):
            x = int(np.round(slider.value))
            image_actor_x.display_extent(x, x, 0, shape[1] - 1, 0, shape[2] - 1)

        def change_slice_y(slider):
            y = int(np.round(slider.value))
            image_actor_y.display_extent(0, shape[0] - 1, y, y, 0, shape[2] - 1)

        def change_opacity(slider):
            slicer_opacity = slider.value
            image_actor_z.opacity(slicer_opacity)
            image_actor_x.opacity(slicer_opacity)
            image_actor_y.opacity(slicer_opacity)

        line_slider_z.on_change = change_slice_z
        line_slider_x.on_change = change_slice_x
        line_slider_y.on_change = change_slice_y
        opacity_slider.on_change = change_opacity

        def build_label(text):
            label = ui.TextBlock2D()
            label.message = text
            label.font_size = 18
            label.font_family = 'Arial'
            label.justification = 'left'
            label.bold = False
            label.italic = False
            label.shadow = False
            label.background = (0, 0, 0)
            label.color = (1, 1, 1)

            return label

        line_slider_label_z = build_label(text="Z Slice")
        line_slider_label_x = build_label(text="X Slice")
        line_slider_label_y = build_label(text="Y Slice")
        opacity_slider_label = build_label(text="Opacity")

        panel = ui.Panel2D(size=(300, 200),
                        color=(1, 1, 1),
                        opacity=0.1,
                        align="right")
        panel.center = (1030, 120)

        panel.add_element(line_slider_label_x, (0.1, 0.75))
        panel.add_element(line_slider_x, (0.38, 0.75))
        panel.add_element(line_slider_label_y, (0.1, 0.55))
        panel.add_element(line_slider_y, (0.38, 0.55))
        panel.add_element(line_slider_label_z, (0.1, 0.35))
        panel.add_element(line_slider_z, (0.38, 0.35))
        panel.add_element(opacity_slider_label, (0.1, 0.15))
        panel.add_element(opacity_slider, (0.38, 0.15))

        show_m.scene.add(panel)

        global size
        size = scene.GetSize()

        def win_callback(obj, event):
            global size
            if size != obj.GetSize():
                size_old = size
                size = obj.GetSize()
                size_change = [size[0] - size_old[0], 0]
                panel.re_align(size_change)

    show_m.initialize()

    scene.zoom(1.5)
    scene.reset_clipping_range()

    if interact:
        show_m.add_window_callback(win_callback)
        show_m.render()
        show_m.start()

    return _inline_interact(scene, inline, interact)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# filename: main.py
"""
一些账户类
"""

sql_query_phone_model = """
select m.MobileNumber,
m.MobileType,
m.MobileArea,
m.AreaCode
from Mobile m
where TRUE
and m.MobileNumber='%(phone)s';
"""
str_query_phone_model = u"""\
手机号字段: %s
手机号类型: %s
手机号归属地: %s
归属地区号: %s
"""
import MySQLdb as mdb
import sys

class MysqlQuery(object):
    def __init__(self, host='localhost', user='wechat', passwd='', dbname='mengbao'):
        try:
            self.con = mdb.connect(host, user, passwd, dbname)
            self.cur = self.con.cursor()
        except mdb.Error, e:
            print "Error %d: %s" % (e.args[0], e.args[1])
            sys.exit(1)

    def __del__(self):
        self._close()

    def __exit__(self):
        self._close()

    def _close(self):
        if self.con:
            self.con.close()

    def query_phone(self, phone):
        phone = self._get_phone_head(phone)
        sql = sql_query_phone_model % {'phone': phone}
        self.cur.execute(sql)
        que = self.cur.fetchone()
        return str_query_phone_model % que

    def query_english(self):
        pass

    def _test(self):
        self.cur.execute("SELECT VERSION()")
        ver = self.cur.fetchone()
        return "Database version : %s " % ver

    @staticmethod
    def _get_phone_head(phone):
        return phone[:7]

if __name__ == '__main__':
    my = MysqlQuery()
    print my._test()
    print my.query_phone('15311447009')
    print my.query_phone('13513208899')
    del my
<commit_msg>update mysql<commit_after># -*- coding: utf-8 -*-
# filename: main.py
"""
一些账户类
"""
import sys
reload(sys)
sys.setdefaultencoding('utf8')  # 编译环境utf8

sql_query_phone_model = """
select m.MobileNumber,
m.MobileType,
m.MobileArea,
m.AreaCode
from Mobile m
where TRUE
and m.MobileNumber='%(phone)s';
"""
str_query_phone_model = u"""\
手机号字段: %s
手机号类型: %s
手机号归属地: %s
归属地区号: %s
"""
import MySQLdb as mdb
import sys

class MysqlQuery(object):
    def __init__(self, host='localhost', user='wechat', passwd='', dbname='mengbao'):
        try:
            self.con = mdb.connect(host, user, passwd, dbname)
            self.cur = self.con.cursor()
        except mdb.Error, e:
            print "Error %d: %s" % (e.args[0], e.args[1])
            sys.exit(1)

    def __del__(self):
        self._close()

    def __exit__(self):
        self._close()

    def _close(self):
        if self.con:
            self.con.close()

    def query_phone(self, phone):
        phone = self._get_phone_head(phone)
        sql = sql_query_phone_model % {'phone': phone}
        self.cur.execute(sql)
        que = self.cur.fetchone()
        return str_query_phone_model % que

    def query_english(self):
        pass

    def _test(self):
        self.cur.execute("SELECT VERSION()")
        ver = self.cur.fetchone()
        return "Database version : %s " % ver

    @staticmethod
    def _get_phone_head(phone):
        return phone[:7]

if __name__ == '__main__':
    my = MysqlQuery()
    print my._test()
    print my.query_phone('15311447009')
    print my.query_phone('13513208899')
    del my
<|endoftext|>


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

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

import nose
from nose.tools import *
from sknano.testing import generate_atoms

def test_NN_parameters():
    atoms = generate_atoms(generator_class='SWNTGenerator', n=3, m=3, nz=10)
    atoms.assign_unique_ids()
    atoms.center_CM()
    atoms.kNN = 6
    atoms.NNrc = 9.0
    new_atoms = atoms.filtered((atoms.z >= -5) & (atoms.z <= 5))
    assert_equal(atoms.kNN, 6)
    assert_equal(atoms.NNrc, 9.0)
    assert_equal(atoms.kNN, new_atoms.kNN)
    assert_equal(atoms.NNrc, new_atoms.NNrc)

def test_filtered_ids():
    atoms = generate_atoms(generator_class='SWNTGenerator', n=5, m=0, nz=5)
    atoms.update_attrs()
    filtered = atoms.filtered_ids(list(range(5, 26)))
    atoms.filter_ids(list(range(5, 26)))
    assert_equal(filtered, atoms)
    assert_true(atoms.Natoms, 20)

def test_filtered():
    atoms = generate_atoms(generator_class='SWNTGenerator', n=5, m=0, nz=5)
    atoms.update_attrs()
    filtered1 = atoms.filtered(atoms.coordination_numbers > 1)
    filtered2 = atoms.filtered("coordination_numbers > 1")
    atoms.filter(atoms.coordination_numbers > 1)
    assert_equal(filtered1, atoms)
    assert_equal(filtered1, filtered2)

def test1():
    atoms = generate_atoms(generator_class='SWNTGenerator', n=3, m=3, nz=10)
    atoms.assign_unique_ids()
    atoms.kNN = 6
    atoms.NNrc = 9.0
    for atom in atoms:
        assert_equals(atom.CN, 0)
        atom.CN = 3
        assert_equals(atom.CN, 3)

    atoms.update_attrs()
    atoms = atoms.filtered((atoms.z >= -5) & (atoms.z <= 5))
    print('Natoms: {}'.format(atoms.Natoms))
    for atom in atoms:
        assert_equals(atom.CN, atoms.kNN)

def test2():
    atoms = generate_atoms(generator_class='SWNTGenerator', n=3, m=3, nz=10)
    atoms.assign_unique_ids()
    atoms.kNN = 3
    atoms.NNrc = 2.0
    atoms.update_attrs()
    print(atoms.ids)
    for atom in atoms:
        print('atom: {}, bond.lengths: {}'.format(atom.id, atom.bonds.lengths))

def test3():

    atoms = generate_atoms(elements='periodic_table')
    atoms.kNN = 3
    atoms_cp = atoms.copy()
    assert_equal(atoms.kNN, atoms_cp.kNN)

if __name__ == '__main__':
    nose.runmodule()
<commit_msg>updating kdtree_atoms unit tests<commit_after>#! /usr/bin/env python

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

import nose
from nose.tools import *

import numpy as np

from sknano.structures import compute_Natoms
from sknano.testing import generate_atoms

def test_NN_parameters():
    atoms = generate_atoms(generator_class='SWNTGenerator', n=3, m=3, nz=10)
    atoms.assign_unique_ids()
    atoms.center_CM()
    atoms.kNN = 6
    atoms.NNrc = 9.0
    new_atoms = atoms.filtered((atoms.z >= -5) & (atoms.z <= 5))
    assert_equal(atoms.kNN, 6)
    assert_equal(atoms.NNrc, 9.0)
    assert_equal(atoms.kNN, new_atoms.kNN)
    assert_equal(atoms.NNrc, new_atoms.NNrc)

def test_filtered_ids():
    atoms = generate_atoms(generator_class='SWNTGenerator', n=5, m=0, nz=5)
    atoms.update_attrs()
    filtered = atoms.filtered_ids(list(range(5, 26)))
    atoms.filter_ids(list(range(5, 26)))
    assert_equal(filtered, atoms)
    assert_true(atoms.Natoms, 20)

def test_filtered():
    atoms = generate_atoms(generator_class='SWNTGenerator', n=5, m=0, nz=5)
    atoms.update_attrs()
    filtered1 = atoms.filtered(atoms.coordination_numbers > 1)
    filtered2 = atoms.filtered("coordination_numbers > 1")
    atoms.filter(atoms.coordination_numbers > 1)
    assert_equal(filtered1, atoms)
    assert_equal(filtered1, filtered2)

def test1():
    atoms = generate_atoms(generator_class='SWNTGenerator', n=3, m=3, nz=10)
    atoms.assign_unique_ids()
    atoms.kNN = 6
    atoms.NNrc = 9.0
    for atom in atoms:
        assert_equals(atom.CN, 0)
        atom.CN = 3
        assert_equals(atom.CN, 3)

    atoms.update_attrs()
    atoms = atoms.filtered((atoms.z >= -5) & (atoms.z <= 5))
    print('Natoms: {}'.format(atoms.Natoms))
    for atom in atoms:
        assert_equals(atom.CN, atoms.kNN)

def test2():
    atoms = generate_atoms(generator_class='SWNTGenerator', n=3, m=3, nz=10)
    atoms.assign_unique_ids()
    atoms.kNN = 3
    atoms.NNrc = 2.0
    atoms.update_attrs()
    print(atoms.ids)
    for atom in atoms:
        print('atom: {}, bond.lengths: {}'.format(atom.id, atom.bonds.lengths))

def test3():
    atoms = generate_atoms(elements='periodic_table')
    atoms.kNN = 3
    atoms_cp = atoms.copy()
    assert_equal(atoms.kNN, atoms_cp.kNN)

def test4():
    atoms = generate_atoms(generator_class='SWNTGenerator', n=3, m=0, nz=5)
    assert_equal(compute_Natoms((3, 0), nz=5), atoms.Natoms)
    assert_equal(atoms.Natoms, atoms.ids[-1])

def test5():
    atoms = generate_atoms(generator_class='SWNTGenerator', n=3, m=0, nz=5)
    assert_true(np.allclose(atoms.coords, atoms.atom_tree.data))

    # atoms.kNN = 3
    # atoms.NNrc = 2.0
    # atoms.update_attrs()
    # print(atoms.ids)

if __name__ == '__main__':
    nose.runmodule()
<|endoftext|>