<commit_before>import six
from adminsortable2.admin import SortableInlineAdminMixin
from django.contrib import admin
from django.db.models import Count
from django.utils.safestring import mark_safe
from icekit.admin_tools.polymorphic import \
    ChildModelPluginPolymorphicParentModelAdmin
import models
from icekit.content_collections.admin import TitleSlugAdmin
from icekit.plugins.base import BaseChildModelPlugin, PluginMount
from icekit.admin import ICEkitContentsAdmin
from icekit.templatetags.icekit_tags import grammatical_join
from icekit.admin_tools.mixins import ThumbnailAdminMixin, FluentLayoutsMixin
from icekit.admin_tools.utils import admin_link, admin_url
from polymorphic.admin import PolymorphicChildModelAdmin, PolymorphicChildModelFilter

from glamkit_collections.models import Country

class WorkCreatorsInlineForCreators(admin.TabularInline, ThumbnailAdminMixin):
    model = models.WorkCreator
    raw_id_fields = ('work',)
    extra = 1
    exclude = ('order',) # doing this prevents sorting from being edited in this context.
    readonly_fields = (
        'link',
    )

    def link(self, inst):
        thumb_html = self.thumbnail(inst.work)
        if thumb_html:
            return '{1}'.format(
                admin_url(inst.work),
                thumb_html,
            )
        else:
            return admin_link(inst.work)
    link.allow_tags = True

    def get_queryset(self, request):
        return super(WorkCreatorsInlineForCreators, self) \
            .get_queryset(request) \
            .filter(work__publishing_is_draft=True,
                    creator__publishing_is_draft=True)

class WorkCreatorsInlineForWorks(SortableInlineAdminMixin, WorkCreatorsInlineForCreators):
    exclude = None # re-enable sorting
    raw_id_fields = ('creator',)

    def link(self, inst):
        # NB skip ...ForCreators.thumbnail()
        thumb_html = self.thumbnail(inst.creator)
        if thumb_html:
            return '{1}'.format(
                admin_url(inst.creator),
                thumb_html,
            )
        else:
            return admin_link(inst.creator)
    link.allow_tags = True

    def get_queryset(self, request):
        return super(WorkCreatorsInlineForWorks, self) \
            .get_queryset(request) \
            .filter(work__publishing_is_draft=True,
                    creator__publishing_is_draft=True)

class WorkOriginsInline(SortableInlineAdminMixin, admin.TabularInline):
    model = models.WorkOrigin
    raw_id_fields = ('geographic_location', )
    extra = 0

class WorkImageInline(
    # Some super-weirdness means that this inline doesn't appear on
    # docker-cloud staging if SortableInlineAdminMixin is enabled.
    # Giving up for now. TODO: reinstate, or choose a different sorting ui lib
    # SortableInlineAdminMixin,
    admin.TabularInline, ThumbnailAdminMixin
):
    model = models.WorkImage
    raw_id_fields = ('image', 'work')
    extra = 1
    readonly_fields = (
        'thumbnail',
    )

    def thumbnail(self, inst):
        return '{1}{2}'.format(
            admin_url(inst.image),
            super(WorkImageInline, self).thumbnail(inst),
            inst.caption,
        )
    thumbnail.allow_tags = True

    def get_thumbnail_source(self, inst):
        return inst.image.image

class CreatorChildAdmin(
    PolymorphicChildModelAdmin,
    ICEkitContentsAdmin,
    FluentLayoutsMixin,
):
    base_model = models.CreatorBase
    save_on_top = True
    raw_id_fields = ['portrait', ]
    exclude = ('layout', 'alt_slug',)
    prepopulated_fields = {"slug": ("name_display",)}
    inlines = [WorkCreatorsInlineForCreators] + \
        ICEkitContentsAdmin.inlines

    readonly_fields = (
        'birth_date_earliest',
        'birth_date_latest',
        'birth_date_sort_ascending',
        'birth_date_sort_descending',
        'birth_date_edtf',
        'death_date_earliest',
        'death_date_latest',
        'death_date_sort_ascending',
        'death_date_sort_descending',
        'death_date_edtf',
    )

    NAME_FIELDSET =  ('Name', {
        'fields': (
            'name_display',
            'slug',
            'name_sort',
            'admin_notes',
        ),
    })

    DATE_FIELDSETS = (
        ("Dates", {
            'fields': (
                ('birth_date_display',
                'death_date_display',),
            ),
        }),
        ("Advanced date controls", {
            'classes': ('collapse',),
            'fields': (
                ('birth_date_earliest',
                'birth_date_latest',),
                ('birth_date_sort_ascending',
                'birth_date_sort_descending',),
                'birth_date_edtf',

                ('death_date_earliest',
                'death_date_latest',),
                ('death_date_sort_ascending',
                'death_date_sort_descending',),
                'death_date_edtf',
            ),
        }),
    )

    LINKS_FIELDSET = ('Links', {
        'fields': (
            'website',
            'wikipedia_link',
        ),
    })

    fieldsets = (
        NAME_FIELDSET,
    ) + DATE_FIELDSETS + (
        LINKS_FIELDSET,
        ("Details", {
            'fields': (
                'portrait',
                'list_image',
                'boosted_search_terms',
                'admin_notes',
            ),
        }),
    )

class WorkChildAdmin(
    PolymorphicChildModelAdmin,
    ICEkitContentsAdmin,
    FluentLayoutsMixin,
    TitleSlugAdmin,
):
    base_model = models.WorkBase
    save_on_top = True
    exclude = ('layout', 'alt_slug',)
    prepopulated_fields = {"slug": ("accession_number", "title",)}
    readonly_fields = (
        "date_edtf",
        'date_earliest',
        'date_latest',
        'date_sort_ascending',
        'date_sort_descending',
    )

    inlines = [WorkOriginsInline, WorkCreatorsInlineForWorks, WorkImageInline] + \
        ICEkitContentsAdmin.inlines

    DATE_FIELDSETS = (
        ("Date", {
            'fields': (
                'date_display',
            ),
        }),
        ("Advanced date controls", {
            'classes': ('collapse',),
            'fields': (
                ('date_earliest',
                 'date_latest',),
                ('date_sort_ascending',
                 'date_sort_descending',),
                'date_edtf',
            ),
        }),

    )
    LINKS_FIELDSET = ('Links', {
        'fields': (
            'website',
            'wikipedia_link',
        ),
    })

    fieldsets = (
        (None, {
            'fields': (
                'title',
                'subtitle',
                'slug',
                'oneliner',
            ),
        }),
    ) + DATE_FIELDSETS + (
        ("Details", {
            'fields': (
                'credit_line',
                'accession_number',
                'department',
                'list_image',
                'boosted_search_terms',
                'admin_notes',
            ),
        }),
        LINKS_FIELDSET,
    )

class WorkChildModelPlugin(six.with_metaclass(
    PluginMount, BaseChildModelPlugin)):
    """
    Mount point for ``WorkBase`` child model plugins.
    """
    model_admin = WorkChildAdmin

class CreatorChildModelPlugin(six.with_metaclass(
    PluginMount, BaseChildModelPlugin)):
    """
    Mount point for ``CreatorBase`` child model plugins.
    """
    model_admin = CreatorChildAdmin

class CreatorBaseAdmin(
    ChildModelPluginPolymorphicParentModelAdmin,
    ICEkitContentsAdmin,
    FluentLayoutsMixin,
    ThumbnailAdminMixin,
):
    base_model = models.CreatorBase
    child_model_plugin_class = CreatorChildModelPlugin
    child_model_admin = CreatorChildAdmin

    search_fields = (
        "name_display",
        "name_sort",
        "id",
        "admin_notes",
    )
    list_display = ('thumbnail',) + ICEkitContentsAdmin.list_display + (
        'works_count',
    )
    list_display_links = list_display[:2]
    list_filter = ICEkitContentsAdmin.list_filter + (
        PolymorphicChildModelFilter,
        'workcreator__role',
    )

    def get_queryset(self, request):
        return super(CreatorBaseAdmin, self).get_queryset(request)\
            .annotate(works_count=Count('works'))

    def works_count(self, inst):
        return inst.works_count
    works_count.admin_order_field = 'works_count'

class CountryFilter(admin.SimpleListFilter):
    title = 'Country'
    parameter_name = 'country'

    def lookups(self, request, model_admin):
        return ((c.id, c) for c in Country.objects.filter(
            id__in=model_admin.get_queryset(request).values_list(
                'origin_locations__country_id', flat=True
            ).distinct()
        ))

    def queryset(self, request, queryset):
        if self.value():
            return queryset.filter(origin_locations__geographic_location__country_id=self.value())
        else:
            return queryset

class WorkBaseAdmin(
    ChildModelPluginPolymorphicParentModelAdmin,
    ICEkitContentsAdmin,
    FluentLayoutsMixin,
    ThumbnailAdminMixin,
):
    base_model = models.WorkBase
    child_model_plugin_class = WorkChildModelPlugin
    child_model_admin = WorkChildAdmin

    list_display = ('thumbnail',) + ICEkitContentsAdmin.list_display + (
        'child_type_name',
        'creators_admin_links',
        'country_flags',
    )
    list_display_links = list_display[:2]
    search_fields = (
        'title',
        'slug',
        'id',
        'admin_notes',
        'accession_number',
        'credit_line',
    )
    list_filter = ICEkitContentsAdmin.list_filter + (
        PolymorphicChildModelFilter,
        'department',
        CountryFilter,
    )

    def get_queryset(self, request):
        return super(WorkBaseAdmin, self).get_queryset(request).prefetch_related('origin_locations__country')

    def country_flags(self, inst):
        result = []
        for loc in inst.origin_locations.all():
            f = loc.flag()
            if f:
                result.append(f)
        return mark_safe(" ".join(result))
    country_flags.short_description = 'Countries'
    country_flags.admin_order_field = 'origin_locations'

    def creators_admin_links(self, inst):
        r = []
        od = inst.workcreator_set.filter(
            work__publishing_is_draft=True
        ).creators_grouped_by_role()
        for role, creators in od:
            if role:
                line = "%s: " % role
            else:
                line = "Creator: "
            line += grammatical_join([admin_link(x) for x in creators])
            r.append(line)
        return "; ".join(r)
    creators_admin_links.short_description = "Creators"
    creators_admin_links.allow_tags = True

admin.site.register(models.CreatorBase, CreatorBaseAdmin)
admin.site.register(models.WorkBase, WorkBaseAdmin)
admin.site.register(models.Role, TitleSlugAdmin)
admin.site.register(models.WorkImageType, TitleSlugAdmin)
<commit_msg>Admin tweaks<commit_after>import six
from adminsortable2.admin import SortableInlineAdminMixin
from django.contrib import admin
from django.db.models import Count
from django.utils.safestring import mark_safe
from icekit.admin_tools.polymorphic import \
    ChildModelPluginPolymorphicParentModelAdmin
import models
from icekit.content_collections.admin import TitleSlugAdmin
from icekit.plugins.base import BaseChildModelPlugin, PluginMount
from icekit.admin import ICEkitContentsAdmin
from icekit.templatetags.icekit_tags import grammatical_join
from icekit.admin_tools.mixins import ThumbnailAdminMixin, FluentLayoutsMixin
from icekit.admin_tools.utils import admin_link, admin_url
from polymorphic.admin import PolymorphicChildModelAdmin, PolymorphicChildModelFilter

from glamkit_collections.models import Country

class WorkCreatorsInlineForCreators(admin.TabularInline, ThumbnailAdminMixin):
    model = models.WorkCreator
    raw_id_fields = ('work',)
    extra = 1
    exclude = ('order',) # doing this prevents sorting from being edited in this context.
    readonly_fields = (
        'link',
    )

    def link(self, inst):
        thumb_html = self.thumbnail(inst.work)
        if thumb_html:
            return '{1}'.format(
                admin_url(inst.work),
                thumb_html,
            )
        else:
            return admin_link(inst.work)
    link.allow_tags = True

    def get_queryset(self, request):
        return super(WorkCreatorsInlineForCreators, self) \
            .get_queryset(request) \
            .filter(work__publishing_is_draft=True,
                    creator__publishing_is_draft=True)

class WorkCreatorsInlineForWorks(SortableInlineAdminMixin, WorkCreatorsInlineForCreators):
    exclude = None # re-enable sorting
    raw_id_fields = ('creator',)

    def link(self, inst):
        # NB skip ...ForCreators.thumbnail()
        thumb_html = self.thumbnail(inst.creator)
        if thumb_html:
            return '{1}'.format(
                admin_url(inst.creator),
                thumb_html,
            )
        else:
            return admin_link(inst.creator)
    link.allow_tags = True

    def get_queryset(self, request):
        return super(WorkCreatorsInlineForWorks, self) \
            .get_queryset(request) \
            .filter(work__publishing_is_draft=True,
                    creator__publishing_is_draft=True)

class WorkOriginsInline(SortableInlineAdminMixin, admin.TabularInline):
    model = models.WorkOrigin
    raw_id_fields = ('geographic_location', )
    extra = 0

class WorkImageInline(
    # Some super-weirdness means that this inline doesn't appear on
    # docker-cloud staging if SortableInlineAdminMixin is enabled.
    # Giving up for now. TODO: reinstate, or choose a different sorting ui lib
    # SortableInlineAdminMixin,
    admin.TabularInline, ThumbnailAdminMixin
):
    model = models.WorkImage
    raw_id_fields = ('image', 'work')
    extra = 1
    readonly_fields = (
        'thumbnail',
    )

    def thumbnail(self, inst):
        return '{1}{2}'.format(
            admin_url(inst.image),
            super(WorkImageInline, self).thumbnail(inst),
            inst.caption,
        )
    thumbnail.allow_tags = True

    def get_thumbnail_source(self, inst):
        return inst.image.image

class CreatorChildAdmin(
    PolymorphicChildModelAdmin,
    ICEkitContentsAdmin,
    FluentLayoutsMixin,
):
    base_model = models.CreatorBase
    save_on_top = True
    raw_id_fields = ('portrait', )
    exclude = ('layout', 'alt_slug',)
    prepopulated_fields = {"slug": ("name_display",)}
    inlines = [WorkCreatorsInlineForCreators] + \
        ICEkitContentsAdmin.inlines

    readonly_fields = (
        'birth_date_earliest',
        'birth_date_latest',
        'birth_date_sort_ascending',
        'birth_date_sort_descending',
        'birth_date_edtf',
        'death_date_earliest',
        'death_date_latest',
        'death_date_sort_ascending',
        'death_date_sort_descending',
        'death_date_edtf',
    )

    NAME_FIELDSET =  ('Name', {
        'fields': (
            'name_display',
            'slug',
            'name_sort',
        ),
    })

    DATE_FIELDSETS = (
        ("Dates", {
            'fields': (
                ('birth_date_display',
                'death_date_display',),
            ),
        }),
        ("Advanced date controls", {
            'classes': ('collapse',),
            'fields': (
                ('birth_date_earliest',
                'birth_date_latest',),
                ('birth_date_sort_ascending',
                'birth_date_sort_descending',),
                'birth_date_edtf',

                ('death_date_earliest',
                'death_date_latest',),
                ('death_date_sort_ascending',
                'death_date_sort_descending',),
                'death_date_edtf',
            ),
        }),
    )

    LINKS_FIELDSET = ('Links', {
        'fields': (
            'website',
            'wikipedia_link',
        ),
    })

    fieldsets = (
        NAME_FIELDSET,
    ) + DATE_FIELDSETS + (
        LINKS_FIELDSET,
        ("Details", {
            'fields': (
                'portrait',
                'list_image',
                'boosted_search_terms',
                'admin_notes',
            ),
        }),
    )

class WorkChildAdmin(
    PolymorphicChildModelAdmin,
    ICEkitContentsAdmin,
    FluentLayoutsMixin,
    TitleSlugAdmin,
):
    base_model = models.WorkBase
    save_on_top = True
    exclude = ('layout', 'alt_slug',)
    prepopulated_fields = {"slug": ("accession_number", "title",)}
    readonly_fields = (
        "date_edtf",
        'date_earliest',
        'date_latest',
        'date_sort_ascending',
        'date_sort_descending',
    )

    inlines = [WorkOriginsInline, WorkCreatorsInlineForWorks, WorkImageInline] + \
        ICEkitContentsAdmin.inlines

    DATE_FIELDSETS = (
        ("Date", {
            'fields': (
                'date_display',
            ),
        }),
        ("Advanced date controls", {
            'classes': ('collapse',),
            'fields': (
                ('date_earliest',
                 'date_latest',),
                ('date_sort_ascending',
                 'date_sort_descending',),
                'date_edtf',
            ),
        }),

    )
    LINKS_FIELDSET = ('Links', {
        'fields': (
            'website',
            'wikipedia_link',
        ),
    })

    fieldsets = (
        (None, {
            'fields': (
                'title',
                'subtitle',
                'slug',
                'oneliner',
            ),
        }),
    ) + DATE_FIELDSETS + (
        ("Details", {
            'fields': (
                'credit_line',
                'accession_number',
                'department',
                'list_image',
                'boosted_search_terms',
                'admin_notes',
            ),
        }),
        LINKS_FIELDSET,
    )

class WorkChildModelPlugin(six.with_metaclass(
    PluginMount, BaseChildModelPlugin)):
    """
    Mount point for ``WorkBase`` child model plugins.
    """
    model_admin = WorkChildAdmin

class CreatorChildModelPlugin(six.with_metaclass(
    PluginMount, BaseChildModelPlugin)):
    """
    Mount point for ``CreatorBase`` child model plugins.
    """
    model_admin = CreatorChildAdmin

class CreatorBaseAdmin(
    ChildModelPluginPolymorphicParentModelAdmin,
    ICEkitContentsAdmin,
    FluentLayoutsMixin,
    ThumbnailAdminMixin,
):
    base_model = models.CreatorBase
    child_model_plugin_class = CreatorChildModelPlugin
    child_model_admin = CreatorChildAdmin

    search_fields = (
        "name_display",
        "name_sort",
        "id",
        "admin_notes",
    )
    list_display = ('thumbnail',) + ICEkitContentsAdmin.list_display + (
        'works_count',
    )
    list_display_links = list_display[:2]
    list_filter = ICEkitContentsAdmin.list_filter + (
        PolymorphicChildModelFilter,
        'workcreator__role',
    )

    def get_queryset(self, request):
        return super(CreatorBaseAdmin, self).get_queryset(request)\
            .annotate(works_count=Count('works'))

    def works_count(self, inst):
        return inst.works_count
    works_count.admin_order_field = 'works_count'

class CountryFilter(admin.SimpleListFilter):
    title = 'Country'
    parameter_name = 'country'

    def lookups(self, request, model_admin):
        return ((c.id, c) for c in Country.objects.filter(
            id__in=model_admin.get_queryset(request).values_list(
                'origin_locations__country_id', flat=True
            ).distinct()
        ))

    def queryset(self, request, queryset):
        if self.value():
            return queryset.filter(origin_locations__geographic_location__country_id=self.value())
        else:
            return queryset

class WorkBaseAdmin(
    ChildModelPluginPolymorphicParentModelAdmin,
    ICEkitContentsAdmin,
    FluentLayoutsMixin,
    ThumbnailAdminMixin,
):
    base_model = models.WorkBase
    child_model_plugin_class = WorkChildModelPlugin
    child_model_admin = WorkChildAdmin

    list_display = ('thumbnail',) + ICEkitContentsAdmin.list_display + (
        'child_type_name',
        'creators_admin_links',
        'country_flags',
    )
    list_display_links = list_display[:2]
    search_fields = (
        'title',
        'slug',
        'id',
        'admin_notes',
        'accession_number',
        'credit_line',
    )
    list_filter = ICEkitContentsAdmin.list_filter + (
        PolymorphicChildModelFilter,
        'department',
        CountryFilter,
    )

    def get_queryset(self, request):
        return super(WorkBaseAdmin, self).get_queryset(request).prefetch_related('origin_locations__country')

    def country_flags(self, inst):
        result = []
        for loc in inst.origin_locations.all():
            f = loc.flag()
            if f:
                result.append(f)
        return mark_safe(" ".join(result))
    country_flags.short_description = 'Countries'
    country_flags.admin_order_field = 'origin_locations'

    def creators_admin_links(self, inst):
        r = []
        od = inst.workcreator_set.filter(
            work__publishing_is_draft=True
        ).creators_grouped_by_role()
        for role, creators in od:
            if role:
                line = "%s: " % role
            else:
                line = "Creator: "
            line += grammatical_join([admin_link(x) for x in creators])
            r.append(line)
        return "; ".join(r)
    creators_admin_links.short_description = "Creators"
    creators_admin_links.allow_tags = True

admin.site.register(models.CreatorBase, CreatorBaseAdmin)
admin.site.register(models.WorkBase, WorkBaseAdmin)
admin.site.register(models.Role, TitleSlugAdmin)
admin.site.register(models.WorkImageType, TitleSlugAdmin)
<|endoftext|>


<commit_before>from django.db import models

# Create your models here.
<commit_msg>made users model<commit_after>from django.db import models

class VumiGoUser(models.Model):
    msisdn = models.CharField(max_length=30,
                              blank=False,
                              verbose_name="MSISDN (Mobile Number)")
    sex = models.CharField(max_length=6)
    age = models.CharField(max_length=30)
    community = models.CharField(max_length=30)

    def __unicode__(self):
        return self.msisdn

    class Meta:
        verbose_name = "User"<|endoftext|>


<commit_before># coding: utf-8

import sys
import ctypes
import warnings
import hashlib
import mkl
import numpy as np
import scipy.sparse as sp
from scipy.sparse import SparseEfficiencyWarning

class PyPardisoSolver:
    """
    Python interface to the Intel MKL PARDISO library for solving large sparse linear systems of equations Ax=b.

    Pardiso documentation: https://software.intel.com/en-us/node/470282

    --- Basic usage ---
    matrix type: real (float64) and nonsymetric
    methods: solve, factorize

    - use the "solve(A,b)" method to solve Ax=b for x, where A is a sparse CSR matrix and b is a vector or matrix
    - use the "factorize(A)" method first, if you intend to solve the system more than once for different right-hand
      sides, the factorization will be reused automatically afterwards

    --- Advanced usage ---
    methods: get_iparm, get_iparms, set_iparm, set_matrix_type, set_phase

    - additional options can be accessed by setting the iparms (see Pardiso documentation for description)
    - other matrix types can be chosen with the "set_matrix_type" method. complex matrix types are currently not
      supported
    - the solving phases can be set with the "set_phase" method
    - The out-of-core (OOC) solver either fails or crashes my computer, be careful with iparm[60]

    --- Statistical info ---
    methods: set_statistical_info_on, set_statistical_info_off

    - the Pardiso solver writes statistical info to the C stdout if desired
    - if you use pypardiso from within a jupyter notebook you can turn the statistical info on and capture the output
      real-time by wrapping your call to "solve" with wurlitzer.sys_pipes() (https://github.com/minrk/wurlitzer,
      https://pypi.python.org/pypi/wurlitzer/)
    - wurlitzer dosen't work on windows, info appears in notebook server console window if used from jupyter notebook

    --- Number of threads ---
    methods: get_max_threads, set_num_threads
    - you can control the number of threads by using the "set_num_threads" method

    """

    def __init__(self, mtype=11, phase=13, size_limit_storage=5e7):

        if sys.platform == 'darwin':
            libmkl_core = ctypes.CDLL('libmkl_core.dylib')
        elif sys.platform == 'win32':
            libmkl_core = ctypes.CDLL('mkl_core.dll')
        else:
            libmkl_core = ctypes.CDLL('libmkl_core.so')

        if ctypes.sizeof(ctypes.c_void_p) == 8:
            self._mkl_pardiso = libmkl_core.mkl_pds_lp64_pardiso
        else:
            self._mkl_pardiso = libmkl_core.mkl_pds_pardiso

        self.pt = np.zeros(64, dtype=np.int32)
        self.iparm = np.zeros(64, dtype=np.int32)
        self.perm = np.zeros(0, dtype=np.int32)

        self.mtype = mtype
        self.phase = phase
        self.msglvl = False

        self.factorized_A = sp.csr_matrix((0,0))
        self.size_limit_storage = size_limit_storage

    def factorize(self, A):
        """
        Factorize the matrix A, the factorization will automatically be used if the same matrix A is passed to the
        solve method. This will drastically increase the speed of solve, if solve is called more than once for the
        same matrix A

        --- Parameters ---
        A: sparse square CSR matrix (scipy.sparse.csr.csr_matrix)
           other matrix types will be converted to CSR
        """

        A = self._check_A(A)

        if A.nnz > self.size_limit_storage:
            self.factorized_A = self._hash_csr_matrix(A)
        else:
            self.factorized_A = A.copy()

        self.set_phase(12)
        b = np.zeros((A.shape[0],1))
        self._call_pardiso(A, b)

    def solve(self, A, b):
        """
        solve Ax=b for x

        --- Parameters ---
        A: sparse square CSR matrix (scipy.sparse.csr.csr_matrix)
           other matrix types will be converted to CSR
        b: numpy ndarray
           right-hand side(s), b.shape[0] needs to be the same as A.shape[0]

        --- Returns ---
        x: numpy ndarray
           solution of the system of linear equations, same shape as b
        """

        A = self._check_A(A)
        b = self._check_b(A, b)

        if self._is_already_factorized(A):
            self.set_phase(33)
        else:
            self.set_phase(13)

        x = self._call_pardiso(A, b)

        # it is possible to call the solver with empty columns, but computationally expensive to check this
        # beforehand, therefore only the results is checked for infinite elements.
        if not np.isfinite(x).all():
            warnings.warn('The result contains infinite elements. Make sure that matrix A contains no empty columns.',
                          PyPardisoWarning)

        return x

    def _is_already_factorized(self, A):
        if type(self.factorized_A) == str:
            return self._hash_csr_matrix(A) == self.factorized_A
        else:
            return self._csr_matrix_equal(A, self.factorized_A)

    def _csr_matrix_equal(self, a1, a2):
        return all((np.array_equal(a1.indptr, a2.indptr),
                    np.array_equal(a1.indices, a2.indices),
                    np.array_equal(a1.data, a2.data)))

    def _hash_csr_matrix(self, matrix):
        return (hashlib.sha1(matrix.indices).hexdigest() +
                hashlib.sha1(matrix.indptr).hexdigest() +
                hashlib.sha1(matrix.data).hexdigest())

    def _check_A(self, A):
        if A.shape[0] != A.shape[1]:
            raise ValueError('Matrix A needs to be square, but has shape: {}'.format(A.shape))

        if not sp.isspmatrix_csr(A):
            warnings.warn('pypardiso requires matrix A to be in CSR format for maximum efficiency',
                          SparseEfficiencyWarning)
            A = sp.csr_matrix(A)

        # scipy allows unsorted csr-indices, which lead to completely wrong pardiso results
        if not A.has_sorted_indices:
            A.sort_indices()

        # scipy allows csr matrices with empty rows. a square matrix with an empty row is singular. calling
        # pardiso with a matrix A that contains empty rows leads to a segfault
        if not np.diff(A.indptr).all():
            raise ValueError('Matrix A is singular, because it contains empty row(s)')

        if A.dtype != np.float64:
            if A.dtype in [np.float16, np.float32, np.int16, np.int32, np.int64]:
                warnings.warn("Matrix A's data type was converted from {} to float64".format(str(A.dtype)),
                              PyPardisoWarning)
                A = A.astype(np.float64)
            else:
                raise TypeError('Dtype {} for matrix A is not supported'.format(str(A.dtype)))

        return A

    def _check_b(self, A, b):
        if sp.isspmatrix(b):
            warnings.warn('PyPardiso requires the right-hand side b to be a dense array for maximum efficiency',
                          SparseEfficiencyWarning)
            b = b.todense()

        # pardiso expects fortran (column-major) order if b is a matrix
        if b.ndim == 2:
            b = np.asfortranarray(b)

        if b.shape[0] != A.shape[0]:
            raise ValueError("Dimension mismatch: Matrix A {} and array b {}".format(A.shape, b.shape))

        if b.dtype != np.float64:
            if b.dtype in [np.float16, np.float32, np.int16, np.int32, np.int64]:
                warnings.warn("Array b's data type was converted from {} to float64".format(str(b.dtype)),
                              PyPardisoWarning)
                b = b.astype(np.float64)
            else:
                raise TypeError('Dtype {} for array b is not supported'.format(str(b.dtype)))

        return b

    def _call_pardiso(self, A, b):
        A_data = A.data
        A_ia = A.indptr + 1
        A_ja = A.indices + 1
        x = np.zeros_like(b)
        pardiso_error = ctypes.c_int32(0)

        c_int32_p = ctypes.POINTER(ctypes.c_int32)
        c_float64_p = ctypes.POINTER(ctypes.c_double)

        self._mkl_pardiso(self.pt.ctypes.data_as(c_int32_p), # pt
                          ctypes.byref(ctypes.c_int32(1)), # maxfct
                          ctypes.byref(ctypes.c_int32(1)), # mnum
                          ctypes.byref(ctypes.c_int32(self.mtype)), # mtype -> 11 for real-nonsymetric
                          ctypes.byref(ctypes.c_int32(self.phase)), # phase -> 13
                          ctypes.byref(ctypes.c_int32(A.shape[0])), #N -> number of equations/size of matrix
                          A_data.ctypes.data_as(c_float64_p), # A -> non-zero entries in matrix
                          A_ia.ctypes.data_as(c_int32_p), # ia -> csr-indptr
                          A_ja.ctypes.data_as(c_int32_p), # ja -> csr-indices
                          self.perm.ctypes.data_as(c_int32_p), # perm -> empty
                          ctypes.byref(ctypes.c_int32(1 if b.ndim == 1 else b.shape[1])), # nrhs
                          self.iparm.ctypes.data_as(c_int32_p), # iparm-array
                          ctypes.byref(ctypes.c_int32(self.msglvl)), # msg-level -> 1: statistical info is printed
                          b.ctypes.data_as(c_float64_p), # b -> right-hand side vector/matrix
                          x.ctypes.data_as(c_float64_p), # x -> output
                          ctypes.byref(pardiso_error)) # pardiso error

        if pardiso_error.value != 0:
            raise PyPardisoError(pardiso_error.value)
        else:
            return np.ascontiguousarray(x) # change memory-layout back from fortran to c order

        #elif x.shape[1] == 1:
        #    return x.ravel() # return 1-dim array like scipy
        ### -> move this to the scipy aliases to have the strange behaviour there for compatibility

    def get_iparms(self):
        """Returns a dictionary of iparms"""
        return dict(enumerate(self.iparm, 1))

    def get_iparm(self, i):
        """Returns the i-th iparm (1-based indexing)"""
        return self.iparm[i-1]

    def set_iparm(self, i, value):
        """set the i-th iparm to 'value' (1-based indexing)"""
        if not i in {1,2,4,5,6,8,10,11,12,13,18,19,21,24,25,27,28,31,34,35,36,37,56,60}:
            warnings.warn('{} is no input iparm. See the Pardiso documentation.'.format(value), PyPardisoWarning)
        self.iparm[i-1] = value

    def set_matrix_type(self, mtype):
        """Set the matrix type (see Pardiso documentation)"""
        self.mtype = mtype

    def get_max_threads(self):
        """Returns the maximum number of threads the solver will use"""
        return mkl.get_max_threads()

    def set_num_threads(self, num_threads):
        """Set the number of threads the solver should use (only a hint, not guaranteed that
        the solver uses this amount)"""
        mkl.set_num_threads(num_threads)

    def set_statistical_info_on(self):
        """Display statistical info (appears in notebook server console window if pypardiso is
        used from jupyter notebook, use wurlitzer to redirect info to the notebook)"""
        self.msglvl = 1

    def set_statistical_info_off(self):
        """Turns statistical info off"""
        self.msglvl = 0

    def set_phase(self, phase):
        """Set the phase(s) for the solver. See the Pardiso documentation for details."""
        self.phase = phase

    def remove_stored_factorization(self):
        """removes the stored factorization, this will free the memory in python, but the factorization in pardiso
        is still accessible with a direct call to self._call_pardiso(A,b) with phase=33"""
        self.factorized_A = sp.csr_matrix((0,0))

class PyPardisoWarning(UserWarning):
    pass

class PyPardisoError(Exception):

    def __init__(self, value):
        self.value = value

    def __str__(self):
        return ('The Pardiso solver failed with error code {}. '
                'See Pardiso documentation for details.'.format(self.value))

        <commit_msg>maybe the pardiso_error was the culprit all along<commit_after># coding: utf-8

import sys
import ctypes
import warnings
import hashlib
import mkl
import numpy as np
import scipy.sparse as sp
from scipy.sparse import SparseEfficiencyWarning

class PyPardisoSolver:
    """
    Python interface to the Intel MKL PARDISO library for solving large sparse linear systems of equations Ax=b.

    Pardiso documentation: https://software.intel.com/en-us/node/470282

    --- Basic usage ---
    matrix type: real (float64) and nonsymetric
    methods: solve, factorize

    - use the "solve(A,b)" method to solve Ax=b for x, where A is a sparse CSR matrix and b is a vector or matrix
    - use the "factorize(A)" method first, if you intend to solve the system more than once for different right-hand
      sides, the factorization will be reused automatically afterwards

    --- Advanced usage ---
    methods: get_iparm, get_iparms, set_iparm, set_matrix_type, set_phase

    - additional options can be accessed by setting the iparms (see Pardiso documentation for description)
    - other matrix types can be chosen with the "set_matrix_type" method. complex matrix types are currently not
      supported
    - the solving phases can be set with the "set_phase" method
    - The out-of-core (OOC) solver either fails or crashes my computer, be careful with iparm[60]

    --- Statistical info ---
    methods: set_statistical_info_on, set_statistical_info_off

    - the Pardiso solver writes statistical info to the C stdout if desired
    - if you use pypardiso from within a jupyter notebook you can turn the statistical info on and capture the output
      real-time by wrapping your call to "solve" with wurlitzer.sys_pipes() (https://github.com/minrk/wurlitzer,
      https://pypi.python.org/pypi/wurlitzer/)
    - wurlitzer dosen't work on windows, info appears in notebook server console window if used from jupyter notebook

    --- Number of threads ---
    methods: get_max_threads, set_num_threads
    - you can control the number of threads by using the "set_num_threads" method

    """

    def __init__(self, mtype=11, phase=13, size_limit_storage=5e7):

        if sys.platform == 'darwin':
            libmkl_core = ctypes.CDLL('libmkl_core.dylib')
        elif sys.platform == 'win32':
            libmkl_core = ctypes.CDLL('mkl_core.dll')
        else:
            libmkl_core = ctypes.CDLL('libmkl_core.so')

        if ctypes.sizeof(ctypes.c_void_p) == 8:
            self._mkl_pardiso = libmkl_core.mkl_pds_lp64_pardiso
        else:
            self._mkl_pardiso = libmkl_core.mkl_pds_pardiso

        self.pt = np.zeros(64, dtype=np.int32)
        self.iparm = np.zeros(64, dtype=np.int32)
        self.perm = np.zeros(0, dtype=np.int32)

        self.mtype = mtype
        self.phase = phase
        self.msglvl = False

        self.factorized_A = sp.csr_matrix((0,0))
        self.size_limit_storage = size_limit_storage

    def factorize(self, A):
        """
        Factorize the matrix A, the factorization will automatically be used if the same matrix A is passed to the
        solve method. This will drastically increase the speed of solve, if solve is called more than once for the
        same matrix A

        --- Parameters ---
        A: sparse square CSR matrix (scipy.sparse.csr.csr_matrix)
           other matrix types will be converted to CSR
        """

        A = self._check_A(A)

        if A.nnz > self.size_limit_storage:
            self.factorized_A = self._hash_csr_matrix(A)
        else:
            self.factorized_A = A.copy()

        self.set_phase(12)
        b = np.zeros((A.shape[0],1))
        self._call_pardiso(A, b)

    def solve(self, A, b):
        """
        solve Ax=b for x

        --- Parameters ---
        A: sparse square CSR matrix (scipy.sparse.csr.csr_matrix)
           other matrix types will be converted to CSR
        b: numpy ndarray
           right-hand side(s), b.shape[0] needs to be the same as A.shape[0]

        --- Returns ---
        x: numpy ndarray
           solution of the system of linear equations, same shape as b
        """

        A = self._check_A(A)
        b = self._check_b(A, b)

        if self._is_already_factorized(A):
            self.set_phase(33)
        else:
            self.set_phase(13)

        x = self._call_pardiso(A, b)

        # it is possible to call the solver with empty columns, but computationally expensive to check this
        # beforehand, therefore only the results is checked for infinite elements.
        if not np.isfinite(x).all():
            warnings.warn('The result contains infinite elements. Make sure that matrix A contains no empty columns.',
                          PyPardisoWarning)

        return x

    def _is_already_factorized(self, A):
        if type(self.factorized_A) == str:
            return self._hash_csr_matrix(A) == self.factorized_A
        else:
            return self._csr_matrix_equal(A, self.factorized_A)

    def _csr_matrix_equal(self, a1, a2):
        return all((np.array_equal(a1.indptr, a2.indptr),
                    np.array_equal(a1.indices, a2.indices),
                    np.array_equal(a1.data, a2.data)))

    def _hash_csr_matrix(self, matrix):
        return (hashlib.sha1(matrix.indices).hexdigest() +
                hashlib.sha1(matrix.indptr).hexdigest() +
                hashlib.sha1(matrix.data).hexdigest())

    def _check_A(self, A):
        if A.shape[0] != A.shape[1]:
            raise ValueError('Matrix A needs to be square, but has shape: {}'.format(A.shape))

        if not sp.isspmatrix_csr(A):
            warnings.warn('pypardiso requires matrix A to be in CSR format for maximum efficiency',
                          SparseEfficiencyWarning)
            A = sp.csr_matrix(A)

        # scipy allows unsorted csr-indices, which lead to completely wrong pardiso results
        if not A.has_sorted_indices:
            A.sort_indices()

        # scipy allows csr matrices with empty rows. a square matrix with an empty row is singular. calling
        # pardiso with a matrix A that contains empty rows leads to a segfault
        if not np.diff(A.indptr).all():
            raise ValueError('Matrix A is singular, because it contains empty row(s)')

        if A.dtype != np.float64:
            if A.dtype in [np.float16, np.float32, np.int16, np.int32, np.int64]:
                warnings.warn("Matrix A's data type was converted from {} to float64".format(str(A.dtype)),
                              PyPardisoWarning)
                A = A.astype(np.float64)
            else:
                raise TypeError('Dtype {} for matrix A is not supported'.format(str(A.dtype)))

        return A

    def _check_b(self, A, b):
        if sp.isspmatrix(b):
            warnings.warn('PyPardiso requires the right-hand side b to be a dense array for maximum efficiency',
                          SparseEfficiencyWarning)
            b = b.todense()

        # pardiso expects fortran (column-major) order if b is a matrix
        if b.ndim == 2:
            b = np.asfortranarray(b)

        if b.shape[0] != A.shape[0]:
            raise ValueError("Dimension mismatch: Matrix A {} and array b {}".format(A.shape, b.shape))

        if b.dtype != np.float64:
            if b.dtype in [np.float16, np.float32, np.int16, np.int32, np.int64]:
                warnings.warn("Array b's data type was converted from {} to float64".format(str(b.dtype)),
                              PyPardisoWarning)
                b = b.astype(np.float64)
            else:
                raise TypeError('Dtype {} for array b is not supported'.format(str(b.dtype)))

        return b

    def _call_pardiso(self, A, b):
        A_data = A.data
        A_ia = A.indptr + 1
        A_ja = A.indices + 1
        x = np.zeros_like(b)
        #pardiso_error = ctypes.c_int32(0)
        pardiso_error = 0

        c_int32_p = ctypes.POINTER(ctypes.c_int32)
        c_float64_p = ctypes.POINTER(ctypes.c_double)

        self._mkl_pardiso(self.pt.ctypes.data_as(c_int32_p), # pt
                          ctypes.byref(ctypes.c_int32(1)), # maxfct
                          ctypes.byref(ctypes.c_int32(1)), # mnum
                          ctypes.byref(ctypes.c_int32(self.mtype)), # mtype -> 11 for real-nonsymetric
                          ctypes.byref(ctypes.c_int32(self.phase)), # phase -> 13
                          ctypes.byref(ctypes.c_int32(A.shape[0])), #N -> number of equations/size of matrix
                          A_data.ctypes.data_as(c_float64_p), # A -> non-zero entries in matrix
                          A_ia.ctypes.data_as(c_int32_p), # ia -> csr-indptr
                          A_ja.ctypes.data_as(c_int32_p), # ja -> csr-indices
                          self.perm.ctypes.data_as(c_int32_p), # perm -> empty
                          ctypes.byref(ctypes.c_int32(1 if b.ndim == 1 else b.shape[1])), # nrhs
                          self.iparm.ctypes.data_as(c_int32_p), # iparm-array
                          ctypes.byref(ctypes.c_int32(self.msglvl)), # msg-level -> 1: statistical info is printed
                          b.ctypes.data_as(c_float64_p), # b -> right-hand side vector/matrix
                          x.ctypes.data_as(c_float64_p), # x -> output
                          ctypes.byref(ctypes.c_int32(pardiso_error))) # pardiso error

        if pardiso_error != 0:
            raise PyPardisoError(pardiso_error)
        else:
            return np.ascontiguousarray(x) # change memory-layout back from fortran to c order

        #elif x.shape[1] == 1:
        #    return x.ravel() # return 1-dim array like scipy
        ### -> move this to the scipy aliases to have the strange behaviour there for compatibility

    def get_iparms(self):
        """Returns a dictionary of iparms"""
        return dict(enumerate(self.iparm, 1))

    def get_iparm(self, i):
        """Returns the i-th iparm (1-based indexing)"""
        return self.iparm[i-1]

    def set_iparm(self, i, value):
        """set the i-th iparm to 'value' (1-based indexing)"""
        if not i in {1,2,4,5,6,8,10,11,12,13,18,19,21,24,25,27,28,31,34,35,36,37,56,60}:
            warnings.warn('{} is no input iparm. See the Pardiso documentation.'.format(value), PyPardisoWarning)
        self.iparm[i-1] = value

    def set_matrix_type(self, mtype):
        """Set the matrix type (see Pardiso documentation)"""
        self.mtype = mtype

    def get_max_threads(self):
        """Returns the maximum number of threads the solver will use"""
        return mkl.get_max_threads()

    def set_num_threads(self, num_threads):
        """Set the number of threads the solver should use (only a hint, not guaranteed that
        the solver uses this amount)"""
        mkl.set_num_threads(num_threads)

    def set_statistical_info_on(self):
        """Display statistical info (appears in notebook server console window if pypardiso is
        used from jupyter notebook, use wurlitzer to redirect info to the notebook)"""
        self.msglvl = 1

    def set_statistical_info_off(self):
        """Turns statistical info off"""
        self.msglvl = 0

    def set_phase(self, phase):
        """Set the phase(s) for the solver. See the Pardiso documentation for details."""
        self.phase = phase

    def remove_stored_factorization(self):
        """removes the stored factorization, this will free the memory in python, but the factorization in pardiso
        is still accessible with a direct call to self._call_pardiso(A,b) with phase=33"""
        self.factorized_A = sp.csr_matrix((0,0))

class PyPardisoWarning(UserWarning):
    pass

class PyPardisoError(Exception):

    def __init__(self, value):
        self.value = value

    def __str__(self):
        return ('The Pardiso solver failed with error code {}. '
                'See Pardiso documentation for details.'.format(self.value))

        <|endoftext|>


<commit_before>__version__ = '3.10.8'
<commit_msg>bump minor version<commit_after>__version__ = '3.11.0'
<|endoftext|>


<commit_before>#!/usr/bin/python
#
# Copyright (c) 2012 Mikkel Schubert <MSchubert@snm.ku.dk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#
# Converts a GTF file to a set of BED6 files, one for each
# feature in the GTF file (CDS, Exon, ...). Also generates a list
# of introns, and UTRs based on sequences in the GTF.
#
from __future__ import with_statement
from __future__ import print_function

import sys
from argparse import ArgumentParser

import pysam

from pypeline.common.utilities import set_in, get_in
import pypeline.common.text as text

###############################################################################
###############################################################################
# Functions used for GTF parsing and filtering

def filter_gtf_record(gtf):
    return gtf.feature not in ("exon", "CDS")

def update_gtf_table(table, gtf, scaffolds, contig_prefix):
    # Workaround for bug in Pysam, which mis-parses individual properties
    # (e.g. exon_number) if these are not quoted. This does not apply to
    # asDict, which uses a different parsing implementation (v0.7.8).
    properties = gtf.asDict()

    keys = (gtf.source,
            properties["gene_id"],
            properties["transcript_id"],
            int(properties["exon_number"]),
            gtf.feature)

    record = {"contig": contig_prefix + gtf.contig,
              "start": gtf.start,
              # In pysam, 'end' equals the past-the-end position
              "end": gtf.end - 1,
              "strand": gtf.strand,
              "feature": gtf.feature,
              "transcript": properties["transcript_id"]}

    if record["contig"] in scaffolds:
        contig = scaffolds[record["contig"]]
        record["contig"] = contig["chrom"]
        record["start"] += int(contig["chromStart"])
        record["end"] += int(contig["chromStart"])

    assert not get_in(table, keys), keys
    set_in(table, keys, record)

def read_gtf(lines, scaffolds, contig_prefix):
    table = {}  # gene_id -> transcript_id -> exon_number -> feature -> [items]
    for gtf in text.parse_lines(lines, pysam.asGTF()):
        if not filter_gtf_record(gtf):
            update_gtf_table(table, gtf, scaffolds, contig_prefix)

    return table

def read_scaffolds(filename):
    scaffolds = {}
    with open(filename) as handle:
        header = handle.readline().strip("#\n\r").split("\t")
        for line in handle:
            row = dict(zip(header, line.rstrip("\r\n").split("\t")))
            scaffolds[row["contig"]] = row
    return scaffolds

###############################################################################
###############################################################################

def get_introns(exons):
    lst = [exons[n]["exon"] for n in sorted(exons)]
    if lst[0]["strand"] == "-":
        # Exon numbers follow the read direction, and the coordinates are
        # therefore descending for regions on the negative strand. Below we
        # assume that the coordinates are ascending, so reorder the list.
        lst = lst[::-1]

    introns = []
    for (record_a, record_b) in zip(lst, lst[1:]):
        if record_a["end"] == record_b["start"] - 1:
            # Intron has been lost?
            continue

        record = dict(record_a)
        record.update(feature="intron",
                      start=record_a["end"] + 1,
                      end=record_b["start"] - 1)
        assert record["start"] <= record["end"], lst

        introns.append(record)

    return introns

def split_exon(exon, cds):
    """Takes an exon and a CDS, and returns a map of regions for each
       feature (UTR5/3, CDS) that may be inferred from the arguments.
       Note that the CDS is simply returned as is, to simplify
       downstream handling of these features."""
    results = [cds]

    if exon["start"] < cds["start"]:
        utr = dict(exon)
        utr.update(end=cds["start"] - 1,
                   feature=(exon["strand"] == "+" and "UTR5" or "UTR3"))
        results.append(utr)

    if exon["end"] > cds["end"]:
        utr = dict(exon)
        utr.update(start=cds["end"] + 1,
                   feature=(exon["strand"] == "+" and "UTR3" or "UTR5"))
        results.append(utr)

    return results

def split_exons(exons, func):
    # By looping over the list sorted by exon-number, we can easily
    # determine whether or not we are dealing with a 5' or 3' UTR.
    seen_cds = False
    for (_, exon) in sorted(exons.iteritems()):
        if "CDS" in exon:
            seen_cds = True
            cds, exon = exon["CDS"], exon["exon"]

            func(split_exon(exon, cds))
        else:
            utr = dict(exon["exon"])
            utr.update(feature=(seen_cds and "UTR3" or "UTR5"))

            func([utr])

def select_transcripts(options, transcripts, protein_coding):
    """Returns the largest transcript, preferring well formed
    transcripts (len(CDS) % 3 == 0) if the gene is protein coding."""
    if options.keep_all_transcripts and options.keep_malformed_proteins:
        return transcripts.itervalues()

    selection = []
    for transcript in transcripts.itervalues():
        well_formed = True
        exon_len = cds_len = 0
        for records in transcript.itervalues():
            exon_record = records["exon"]
            exon_len += exon_record["end"] - exon_record["start"] + 1

            if protein_coding and ("CDS" in records):
                cds_record = records["CDS"]
                cds_len += cds_record["end"] - cds_record["start"] + 1

        if protein_coding:
            well_formed = (cds_len and (cds_len % 3 == 0))

        if well_formed or options.keep_malformed_proteins:
            selection.append(((well_formed, exon_len), transcript))

    if options.keep_all_transcripts or not selection:
        return [item[-1] for item in selection]

    return [max(selection, key=lambda item: item[0])[-1]]

def _do_build_feature_table(options, table, features, protein_coding):
    def add_records(records):
        for record in records:
            features[record["feature"]].append(record)

    retained = read = 0
    for transcripts in table.itervalues():
        read += len(transcripts)
        for exons in select_transcripts(options, transcripts, protein_coding):
            retained += 1
            add_records(get_introns(exons))
            yield (exons, add_records)

    print("\t- Processed %i transcripts, filtered %i (%.1f%%) ..."
          % (read, read - retained, (100.0 * (read - retained)) / read))

def build_coding_seqs_table(options, table):
    """Takes a table generated from a GTF file, and constructs a table for each
       feature, inferring introns and UTRs from the exons and CDSs of the input
       table."""
    print("Building table of features for coding sequences ...")
    features = {"UTR5": [],
                "UTR3": [],
                "CDS": [],
                "intron": []}

    feature_table = _do_build_feature_table(options, table, features, True)
    for (exons, add_records) in feature_table:
        split_exons(exons, add_records)
    return features

def build_noncoding_seqs_table(options, table):
    print("Building table of features for non-coding sequences ...")
    features = {"exon": [],
                "intron": []}

    feature_table = _do_build_feature_table(options, table, features, False)
    for (exons, add_records) in feature_table:
        add_records(record["exon"] for record in exons.itervalues())
    return features

def write_bed(table, target):
    if not table:
        return

    def sort_key(record):
        return (record["contig"], record["start"], record["end"])

    with open(target, "w") as out:
        for record in sorted(table, key=sort_key):
            out.write("%s\t%i\t%i\t%s\t%i\t%s\n" %
                      # As described on http://genome.ucsc.edu/FAQ/FAQformat
                      (record["contig"],      # chrom
                       record["start"],       # chromStart
                       record["end"] + 1,     # chromEnd, past-the-end
                       record["transcript"],  # name
                       0,                     # score
                       record["strand"]))     # strand

###############################################################################
###############################################################################

def parse_arguments(argv):
    parser = ArgumentParser()
    parser.add_argument('infile', metavar="INPUT.gtf",
                        help="Input file in GTF format.")
    parser.add_argument('output_prefix', metavar="OUTPUT_PREFIX",
                        help="Prefix of output files.")
    parser.add_argument('scaffolds', metavar="SCAFFOLDS", nargs="?",
                        help="Mapping of scaffolds to contig positions; e.g. "
                             "mapping individual Un* scaffolds onto chrUn.")
    parser.add_argument("--keep-all-transcripts",
                        action="store_true", default=False,
                        help="Include all transcripts in the output BED "
                             "files, not just the longest transcript of each "
                             "gene [default: off]")
    parser.add_argument("--keep-malformed-proteins",
                        action="store_true", default=False,
                        help="Include transcripts of protein-coding in the "
                             "output, even if the the length of the CDS is "
                             "not divisible by 3 [default: off]")
    parser.add_argument('--contig-prefix', default="",
                        help="Add prefix to contig names (e.g. 'chr') "
                             "[default: no prefix].")

    return parser.parse_args(argv)

def main(argv):
    args = parse_arguments(argv)

    scaffolds = {}
    if args.scaffolds:
        print("Reading scaffolds information from %r" % (args.scaffolds,))
        scaffolds = read_scaffolds(args.scaffolds)

    with open(args.infile, "r") as gtf_file:
        print("Reading GTF from %r" % (args.infile,))
        src_table = read_gtf(gtf_file, scaffolds, args.contig_prefix)

    for (source, table) in src_table.iteritems():
        print("Writing tables for '%s' ..." % source)

        if source.startswith("protein"):
            features = build_coding_seqs_table(args, table)
        else:
            features = build_noncoding_seqs_table(args, table)

        for feature in features:
            fpath = "%s.%s.%s.bed" % (args.output_prefix, source, feature)

            print("\tWriting %ss to '%s' ..." % (feature, fpath, ))
            write_bed(features[feature], fpath)

    return 0

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
<commit_msg>Group GTF records by biotype, not source.<commit_after>#!/usr/bin/python
#
# Copyright (c) 2012 Mikkel Schubert <MSchubert@snm.ku.dk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#
# Converts a GTF file to a set of BED6 files, one for each
# feature in the GTF file (CDS, Exon, ...). Also generates a list
# of introns, and UTRs based on sequences in the GTF.
#
from __future__ import with_statement
from __future__ import print_function

import sys
from argparse import ArgumentParser

import pysam

from pypeline.common.utilities import set_in, get_in
import pypeline.common.text as text

###############################################################################
###############################################################################
# Functions used for GTF parsing and filtering

def filter_gtf_record(gtf):
    return gtf.feature not in ("exon", "CDS")

def update_gtf_table(table, gtf, scaffolds, contig_prefix):
    # Workaround for bug in Pysam, which mis-parses individual properties
    # (e.g. exon_number) if these are not quoted. This does not apply to
    # asDict, which uses a different parsing implementation (v0.7.8).
    properties = gtf.asDict()

    keys = (properties["gene_biotype"],
            properties["gene_id"],
            properties["transcript_id"],
            int(properties["exon_number"]),
            gtf.feature)

    record = {"contig": contig_prefix + gtf.contig,
              "start": gtf.start,
              # In pysam, 'end' equals the past-the-end position
              "end": gtf.end - 1,
              "strand": gtf.strand,
              "feature": gtf.feature,
              "transcript": properties["transcript_id"]}

    if record["contig"] in scaffolds:
        contig = scaffolds[record["contig"]]
        record["contig"] = contig["chrom"]
        record["start"] += int(contig["chromStart"])
        record["end"] += int(contig["chromStart"])

    assert not get_in(table, keys), keys
    set_in(table, keys, record)

def read_gtf(lines, scaffolds, contig_prefix):
    table = {}  # gene_id -> transcript_id -> exon_number -> feature -> [items]
    for gtf in text.parse_lines(lines, pysam.asGTF()):
        if not filter_gtf_record(gtf):
            update_gtf_table(table, gtf, scaffolds, contig_prefix)

    return table

def read_scaffolds(filename):
    scaffolds = {}
    with open(filename) as handle:
        header = handle.readline().strip("#\n\r").split("\t")
        for line in handle:
            row = dict(zip(header, line.rstrip("\r\n").split("\t")))
            scaffolds[row["contig"]] = row
    return scaffolds

###############################################################################
###############################################################################

def get_introns(exons):
    lst = [exons[n]["exon"] for n in sorted(exons)]
    if lst[0]["strand"] == "-":
        # Exon numbers follow the read direction, and the coordinates are
        # therefore descending for regions on the negative strand. Below we
        # assume that the coordinates are ascending, so reorder the list.
        lst = lst[::-1]

    introns = []
    for (record_a, record_b) in zip(lst, lst[1:]):
        if record_a["end"] == record_b["start"] - 1:
            # Intron has been lost?
            continue

        record = dict(record_a)
        record.update(feature="intron",
                      start=record_a["end"] + 1,
                      end=record_b["start"] - 1)
        assert record["start"] <= record["end"], lst

        introns.append(record)

    return introns

def split_exon(exon, cds):
    """Takes an exon and a CDS, and returns a map of regions for each
       feature (UTR5/3, CDS) that may be inferred from the arguments.
       Note that the CDS is simply returned as is, to simplify
       downstream handling of these features."""
    results = [cds]

    if exon["start"] < cds["start"]:
        utr = dict(exon)
        utr.update(end=cds["start"] - 1,
                   feature=(exon["strand"] == "+" and "UTR5" or "UTR3"))
        results.append(utr)

    if exon["end"] > cds["end"]:
        utr = dict(exon)
        utr.update(start=cds["end"] + 1,
                   feature=(exon["strand"] == "+" and "UTR3" or "UTR5"))
        results.append(utr)

    return results

def split_exons(exons, func):
    # By looping over the list sorted by exon-number, we can easily
    # determine whether or not we are dealing with a 5' or 3' UTR.
    seen_cds = False
    for (_, exon) in sorted(exons.iteritems()):
        if "CDS" in exon:
            seen_cds = True
            cds, exon = exon["CDS"], exon["exon"]

            func(split_exon(exon, cds))
        else:
            utr = dict(exon["exon"])
            utr.update(feature=(seen_cds and "UTR3" or "UTR5"))

            func([utr])

def select_transcripts(options, transcripts, protein_coding):
    """Returns the largest transcript, preferring well formed
    transcripts (len(CDS) % 3 == 0) if the gene is protein coding."""
    if options.keep_all_transcripts and options.keep_malformed_proteins:
        return transcripts.itervalues()

    selection = []
    for transcript in transcripts.itervalues():
        well_formed = True
        exon_len = cds_len = 0
        for records in transcript.itervalues():
            exon_record = records["exon"]
            exon_len += exon_record["end"] - exon_record["start"] + 1

            if protein_coding and ("CDS" in records):
                cds_record = records["CDS"]
                cds_len += cds_record["end"] - cds_record["start"] + 1

        if protein_coding:
            well_formed = (cds_len and (cds_len % 3 == 0))

        if well_formed or options.keep_malformed_proteins:
            selection.append(((well_formed, exon_len), transcript))

    if options.keep_all_transcripts or not selection:
        return [item[-1] for item in selection]

    return [max(selection, key=lambda item: item[0])[-1]]

def _do_build_feature_table(options, table, features, protein_coding):
    def add_records(records):
        for record in records:
            features[record["feature"]].append(record)

    retained = read = 0
    for transcripts in table.itervalues():
        read += len(transcripts)
        for exons in select_transcripts(options, transcripts, protein_coding):
            retained += 1
            add_records(get_introns(exons))
            yield (exons, add_records)

    print("\t- Processed %i transcripts, filtered %i (%.1f%%) ..."
          % (read, read - retained, (100.0 * (read - retained)) / read))

def build_coding_seqs_table(options, table):
    """Takes a table generated from a GTF file, and constructs a table for each
       feature, inferring introns and UTRs from the exons and CDSs of the input
       table."""
    print("Building table of features for coding sequences ...")
    features = {"UTR5": [],
                "UTR3": [],
                "CDS": [],
                "intron": []}

    feature_table = _do_build_feature_table(options, table, features, True)
    for (exons, add_records) in feature_table:
        split_exons(exons, add_records)
    return features

def build_noncoding_seqs_table(options, table):
    print("Building table of features for non-coding sequences ...")
    features = {"exon": [],
                "intron": []}

    feature_table = _do_build_feature_table(options, table, features, False)
    for (exons, add_records) in feature_table:
        add_records(record["exon"] for record in exons.itervalues())
    return features

def write_bed(table, target):
    if not table:
        return

    def sort_key(record):
        return (record["contig"], record["start"], record["end"])

    with open(target, "w") as out:
        for record in sorted(table, key=sort_key):
            out.write("%s\t%i\t%i\t%s\t%i\t%s\n" %
                      # As described on http://genome.ucsc.edu/FAQ/FAQformat
                      (record["contig"],      # chrom
                       record["start"],       # chromStart
                       record["end"] + 1,     # chromEnd, past-the-end
                       record["transcript"],  # name
                       0,                     # score
                       record["strand"]))     # strand

###############################################################################
###############################################################################

def parse_arguments(argv):
    parser = ArgumentParser()
    parser.add_argument('infile', metavar="INPUT.gtf",
                        help="Input file in GTF format.")
    parser.add_argument('output_prefix', metavar="OUTPUT_PREFIX",
                        help="Prefix of output files.")
    parser.add_argument('scaffolds', metavar="SCAFFOLDS", nargs="?",
                        help="Mapping of scaffolds to contig positions; e.g. "
                             "mapping individual Un* scaffolds onto chrUn.")
    parser.add_argument("--keep-all-transcripts",
                        action="store_true", default=False,
                        help="Include all transcripts in the output BED "
                             "files, not just the longest transcript of each "
                             "gene [default: off]")
    parser.add_argument("--keep-malformed-proteins",
                        action="store_true", default=False,
                        help="Include transcripts of protein-coding in the "
                             "output, even if the the length of the CDS is "
                             "not divisible by 3 [default: off]")
    parser.add_argument('--contig-prefix', default="",
                        help="Add prefix to contig names (e.g. 'chr') "
                             "[default: no prefix].")

    return parser.parse_args(argv)

def main(argv):
    args = parse_arguments(argv)

    scaffolds = {}
    if args.scaffolds:
        print("Reading scaffolds information from %r" % (args.scaffolds,))
        scaffolds = read_scaffolds(args.scaffolds)

    with open(args.infile, "r") as gtf_file:
        print("Reading GTF from %r" % (args.infile,))
        src_table = read_gtf(gtf_file, scaffolds, args.contig_prefix)

    for (source, table) in src_table.iteritems():
        print("Writing tables for '%s' ..." % source)

        if source.startswith("protein"):
            features = build_coding_seqs_table(args, table)
        else:
            features = build_noncoding_seqs_table(args, table)

        for feature in features:
            fpath = "%s.%s.%s.bed" % (args.output_prefix, source, feature)

            print("\tWriting %ss to '%s' ..." % (feature, fpath, ))
            write_bed(features[feature], fpath)

    return 0

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
<|endoftext|>


<commit_before>#!/usr/bin/env python
import sys,os
import struct
from xml.dom import minidom

def print_help_exit():
  print "usage: cascade.py <cv_haar_cascade.xml> <num stages>"
  sys.exit(1)

if len(sys.argv)!= 3:
    print_help_exit()

n_stages = int(sys.argv[2])
#parse xml file
xmldoc = minidom.parse(sys.argv[1])

trees = xmldoc.getElementsByTagName('trees')
max_stages = len(trees)
if n_stages > max_stages:
    raise Exception("The max number of stages is: %d"%(max_stages))

# read stages
stages = [len(t.childNodes)/2 for t in trees][0:n_stages]
stage_threshold = xmldoc.getElementsByTagName('stage_threshold')[0:n_stages]

# total number of features
n_features = sum(stages)
# read features threshold
threshold = xmldoc.getElementsByTagName('threshold')[0:n_features]
#theres one of each per feature
alpha1 = xmldoc.getElementsByTagName('left_val')[0:n_features]
alpha2 = xmldoc.getElementsByTagName('right_val')[0:n_features]

#read rectangles
feature = xmldoc.getElementsByTagName('rects')[0:n_features]

#read cascade size
size = (map(int, xmldoc.getElementsByTagName('size')[0].childNodes[0].nodeValue.split()))
fout = open(os.path.basename(sys.argv[1]).split('.')[0]+".cascade", "w")

n_rectangles = 0
for f in feature:
    rects = f.getElementsByTagName('_')
    n_rectangles = n_rectangles + len(rects)

#print some cascade info
print "size:%dx%d"%(size[0], size[1])
print "stages:%d"%len(stages)
print "features:%d"%n_features
print "rectangles:%d"%n_rectangles

# write detection window size
fout.write(struct.pack('i', size[0]))
fout.write(struct.pack('i', size[1]))

# write num stages
fout.write(struct.pack('i', len(stages)))

# write num feat in stages
for s in stages:
    fout.write(struct.pack('B', s)) # uint8_t

# write stages thresholds
for t in stage_threshold:
    fout.write(struct.pack('h', int(float(t.childNodes[0].nodeValue)*256))) #int16_t

# write features threshold 1 per feature
for t in threshold:
    fout.write(struct.pack('h', int(float(t.childNodes[0].nodeValue)*4096))) #int16_t

# write alpha1 1 per feature
for a in alpha1:
    fout.write(struct.pack('h', int(float(a.childNodes[0].nodeValue)*256))) #int16_t

# write alpha2 1 per feature
for a in alpha2:
    fout.write(struct.pack('h', int(float(a.childNodes[0].nodeValue)*256))) #int16_t

# write num_rects per feature
for f in feature:
    rects = f.getElementsByTagName('_')
    fout.write(struct.pack('B', len(rects))) # uint8_t

# write rects weights 1 per rectangle
for f in feature:
    rects = f.getElementsByTagName('_')
    for r in rects:
        l = map(int, r.childNodes[0].nodeValue[:-1].split())
        fout.write(struct.pack('b', l[4])) #int8_t NOTE: multiply by 4096

# write rects
for f in feature:
    rects = f.getElementsByTagName('_')
    for r in rects:
        l = map(int, r.childNodes[0].nodeValue[:-1].split())
        fout.write(struct.pack('BBBB',l[0], l[1], l[2], l[3])) #uint8_t
<commit_msg>Cleanup the cascade generator<commit_after>#!/usr/bin/env python2.7
import sys,os
import struct
import argparse
from xml.dom import minidom

def cascade_info(path):
    #parse xml file
    xmldoc = minidom.parse(path)

    trees = xmldoc.getElementsByTagName('trees')
    n_stages = len(trees)

    # read stages
    stages = [len(t.childNodes)/2 for t in trees][0:n_stages]
    stage_threshold = xmldoc.getElementsByTagName('stage_threshold')[0:n_stages]

    # total number of features
    n_features = sum(stages)
    # read features threshold
    threshold = xmldoc.getElementsByTagName('threshold')[0:n_features]
    #theres one of each per feature
    alpha1 = xmldoc.getElementsByTagName('left_val')[0:n_features]
    alpha2 = xmldoc.getElementsByTagName('right_val')[0:n_features]

    #read rectangles
    feature = xmldoc.getElementsByTagName('rects')[0:n_features]

    #read cascade size
    size = (map(int, xmldoc.getElementsByTagName('size')[0].childNodes[0].nodeValue.split()))
    fout = open(os.path.basename(path).split('.')[0]+".cascade", "w")

    n_rectangles = 0
    for f in feature:
        rects = f.getElementsByTagName('_')
        n_rectangles = n_rectangles + len(rects)

    #print some cascade info
    print("size:%dx%d"%(size[0], size[1]))
    print("stages:%d"%len(stages))
    print("features:%d"%n_features)
    print("rectangles:%d"%n_rectangles)

def cascade_binary(path, n_stages, name):
    #parse xml file
    xmldoc = minidom.parse(path)

    trees = xmldoc.getElementsByTagName('trees')
    max_stages = len(trees)
    if n_stages > max_stages:
        raise Exception("The max number of stages is: %d"%(max_stages))

    if n_stages == 0:
        n_stages = max_stages

    # read stages
    stages = [len(t.childNodes)/2 for t in trees][0:n_stages]
    stage_threshold = xmldoc.getElementsByTagName('stage_threshold')[0:n_stages]

    # total number of features
    n_features = sum(stages)

    # read features threshold
    threshold = xmldoc.getElementsByTagName('threshold')[0:n_features]

    # theres one of each per feature
    alpha1 = xmldoc.getElementsByTagName('left_val')[0:n_features]
    alpha2 = xmldoc.getElementsByTagName('right_val')[0:n_features]

    # read rectangles
    feature = xmldoc.getElementsByTagName('rects')[0:n_features]

    # read cascade size
    size = (map(int, xmldoc.getElementsByTagName('size')[0].childNodes[0].nodeValue.split()))

    # open output file with the specified name or xml file name
    if not name:
        name = os.path.basename(path).split('.')[0]
    fout = open(name+".cascade", "w")

    n_rectangles = 0
    for f in feature:
        rects = f.getElementsByTagName('_')
        n_rectangles = n_rectangles + len(rects)

    # write detection window size
    fout.write(struct.pack('i', size[0]))
    fout.write(struct.pack('i', size[1]))

    # write num stages
    fout.write(struct.pack('i', len(stages)))

    # write num feat in stages
    for s in stages:
        fout.write(struct.pack('B', s)) # uint8_t

    # write stages thresholds
    for t in stage_threshold:
        fout.write(struct.pack('h', int(float(t.childNodes[0].nodeValue)*256))) #int16_t

    # write features threshold 1 per feature
    for t in threshold:
        fout.write(struct.pack('h', int(float(t.childNodes[0].nodeValue)*4096))) #int16_t

    # write alpha1 1 per feature
    for a in alpha1:
        fout.write(struct.pack('h', int(float(a.childNodes[0].nodeValue)*256))) #int16_t

    # write alpha2 1 per feature
    for a in alpha2:
        fout.write(struct.pack('h', int(float(a.childNodes[0].nodeValue)*256))) #int16_t

    # write num_rects per feature
    for f in feature:
        rects = f.getElementsByTagName('_')
        fout.write(struct.pack('B', len(rects))) # uint8_t

    # write rects weights 1 per rectangle
    for f in feature:
        rects = f.getElementsByTagName('_')
        for r in rects:
            l = map(int, r.childNodes[0].nodeValue[:-1].split())
            fout.write(struct.pack('b', l[4])) #int8_t NOTE: multiply by 4096

    # write rects
    for f in feature:
        rects = f.getElementsByTagName('_')
        for r in rects:
            l = map(int, r.childNodes[0].nodeValue[:-1].split())
            fout.write(struct.pack('BBBB',l[0], l[1], l[2], l[3])) #uint8_t

    # print cascade info
    print("size:%dx%d"%(size[0], size[1]))
    print("stages:%d"%len(stages))
    print("features:%d"%n_features)
    print("rectangles:%d"%n_rectangles)
    print("binary cascade generated")

def main():
    # CMD args parser
    parser = argparse.ArgumentParser(description='haar cascade generator')
    parser.add_argument("-i", "--info",     action = "store_true",  help = "print cascade info and exit")
    parser.add_argument("-n", "--name",     action = "store",       help = "set cascade name", default = "")
    parser.add_argument("-s", "--stages",   action = "store",       help = "set the maximum number of stages", type = int, default=0)
    parser.add_argument("file", action = "store", help = "OpenCV xml cascade file path")

    # Parse CMD args
    args = parser.parse_args()

    if args.info:
        # print cascade info and exit
        cascade_info(args.file)
        return

    # generate a binary cascade from the xml cascade
    cascade_binary(args.file, args.stages, args.name)

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


<commit_before>#!/usr/bin/env python3
import RPi.GPIO as GPIO ## Import GPIO library
import time ## Import 'time' library. Allows us to use 'sleep'

GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
BLUELED = 33
REDLED = 35
GREENLED = 37
GPIO.setup(BLUELED, GPIO.OUT) ## Setup GPIO Pin to OUT
GPIO.setup(REDLED, GPIO.OUT) ## Setup GPIO Pin to OUT
GPIO.setup(GREENLED, GPIO.OUT) ## Setup GPIO Pin to OUT

##Define a function named Blink()
def Blink(pin, numTimes,speed):
    for i in range(0,numTimes):## Run loop numTimes
        print "Iteration " + str(i+1)## Print current loop
        GPIO.output(7,True)## Switch on pin 7
        time.sleep(speed)## Wait
        GPIO.output(7,False)## Switch off pin 7
        time.sleep(speed)## Wait
    print "Done" ## When loop is complete, print "Done"
    GPIO.cleanup()

## Ask user for total number of blinks and length of each blink
iterations = raw_input("Enter total number of times to blink: ")
speed = raw_input("Enter length of each blink(seconds): ")

## Start Blink() function.
## Convert user input from strings to numeric data types and pass to Blink() as parameters
Blink(int(BLUELED), int(iterations),float(speed))
<commit_msg>fixed typo in pin assignment<commit_after>#!/usr/bin/env python3
import RPi.GPIO as GPIO ## Import GPIO library
import time ## Import 'time' library. Allows us to use 'sleep'

GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
BLUELED = 33
REDLED = 35
GREENLED = 37
GPIO.setup(BLUELED, GPIO.OUT) ## Setup GPIO Pin to OUT
GPIO.setup(REDLED, GPIO.OUT) ## Setup GPIO Pin to OUT
GPIO.setup(GREENLED, GPIO.OUT) ## Setup GPIO Pin to OUT

##Define a function named Blink()
def Blink(pin, numTimes,speed):
    for i in range(0,numTimes):## Run loop numTimes
        print "Iteration " + str(i+1)## Print current loop
        GPIO.output(pin,True)## Switch on pin 7
        time.sleep(speed)## Wait
        GPIO.output(pin,False)## Switch off pin 7
        time.sleep(speed)## Wait
    print "Done" ## When loop is complete, print "Done"
    GPIO.cleanup()

## Ask user for total number of blinks and length of each blink
iterations = raw_input("Enter total number of times to blink: ")
speed = raw_input("Enter length of each blink(seconds): ")
led = raw_input("Enter color of led to blink (red|green|blue): ")

if (led == "red"):
    driveled = REDLED
else if (led == "green"):
    driveled = GREENLED
else:
    driveled = BLUELED

## Start Blink() function.
## Convert user input from strings to numeric data types and pass to Blink() as parameters
Blink(int(driveled), int(iterations),float(speed))
<|endoftext|>


<commit_before>from six import string_types
from pypif.obj.common.method import Method
from pypif.obj.common.rcl import Rcl
from pypif.obj.common.value import Value

class Property(Value, Rcl):
    """
    Class to store information about a property and conditions under which it exists.
    """

    def __init__(self, name=None, scalars=None, vectors=None, matrices=None, files=None, units=None, conditions=None,
                 method=None, methods=None, data_type=None, references=None, contacts=None, licenses=None, tags=None, **kwargs):
        """
        Constructor.

        :param name: String with the name of the property.
        :param scalars: One or more dictionaries, strings, numbers, or :class:`.Scalar` objects.
        :param vectors: One or more lists of dictionaries, strings, numbers, or :class:`.Scalar` objects,
                each representing a vector.
        :param matrices: One of more lists of lists of dictionaries, strings, numbers, or :class:`.Scalar` objects,
                each representing a matrix with rows as the innermost lists.
        :param files: One of more dictionaries, strings, or :class:`.FileReference` objects.
        :param units: String with the units of the property.
        :param conditions: List of dictionaries or :class:`.Value` objects with the conditions at which the
                property exists.
        :param method: Dictionary or :class:`.Method` object describing the method used to get the property value.
        :param methods: List of dictionary or :class:`.Method` object describing the method used to get the property value.
        :param data_type: String containing "EXPERIMENTAL", "COMPUTATIONAL", "FIT", or "MACHINE_LEARNING" to set the
                broad category of data.
        :param references: List of dictionaries or :class:`.Reference` objects where information about the
                property is published.
        :param contacts: List of dictionaries, strings, or :class:`.Person` objects with people to contact for
                information about the property.
        :param licenses: List of dictionaries, strings, or :class:`.License` objects with licensing information
                for the property.
        :param tags: List of strings or numbers that are tags for this object.
        :param kwargs: Dictionary of fields that are not supported.
        """
        # The order of the constructors is important here. The second constructor could overwrite values set during
        # the first if there is overlap.
        Value.__init__(self, name=name, scalars=scalars, vectors=vectors, matrices=matrices, files=files,
                       units=units, tags=tags, **kwargs)
        self.references = references
        self.contacts = contacts
        self.licenses = licenses
        self._conditions = None
        self.conditions = conditions
        self._method = None
        self.method = method
        self._methods = None
        self.methods = methods
        self._data_type = None
        self.data_type = data_type

    @property
    def conditions(self):
        return self._conditions

    @conditions.setter
    def conditions(self, conditions):
        self._validate_list_type('conditions', conditions, dict, Value)
        self._conditions = self._get_object(Value, conditions)

    @conditions.deleter
    def conditions(self):
        self._conditions = None

    @property
    def method(self):
        return self._method

    @method.setter
    def method(self, method):
        self._validate_type('method', method, dict, Method)
        self._method = self._get_object(Method, method)

    @method.deleter
    def method(self):
        self._method = None

    @property
    def methods(self):
        return self._methods

    @methods.setter
    def methods(self, methods):
        self._validate_list_type('method', methods, dict, Method)
        self._methods = self._get_object(Method, methods)

    @methods.deleter
    def methods(self):
        self._methods = None

    @property
    def data_type(self):
        return self._data_type

    @data_type.setter
    def data_type(self, data_type):
        self._validate_type('data_type', data_type, string_types)
        self._data_type = data_type

    @data_type.deleter
    def data_type(self):
        self._data_type = None
<commit_msg>Deprecating method<commit_after>from six import string_types
from pypif.obj.common.method import Method
from pypif.obj.common.rcl import Rcl
from pypif.obj.common.value import Value
from warnings import warn

class Property(Value, Rcl):
    """
    Class to store information about a property and conditions under which it exists.
    """

    def __init__(self, name=None, scalars=None, vectors=None, matrices=None, files=None, units=None, conditions=None,
                 methods=None, data_type=None, references=None, contacts=None, licenses=None, tags=None, **kwargs):
        """
        Constructor.

        :param name: String with the name of the property.
        :param scalars: One or more dictionaries, strings, numbers, or :class:`.Scalar` objects.
        :param vectors: One or more lists of dictionaries, strings, numbers, or :class:`.Scalar` objects,
                each representing a vector.
        :param matrices: One of more lists of lists of dictionaries, strings, numbers, or :class:`.Scalar` objects,
                each representing a matrix with rows as the innermost lists.
        :param files: One of more dictionaries, strings, or :class:`.FileReference` objects.
        :param units: String with the units of the property.
        :param conditions: List of dictionaries or :class:`.Value` objects with the conditions at which the
                property exists.
        :param method: Dictionary or :class:`.Method` object describing the method used to get the property value.
        :param methods: List of dictionary or :class:`.Method` object describing the method used to get the property value.
        :param data_type: String containing "EXPERIMENTAL", "COMPUTATIONAL", "FIT", or "MACHINE_LEARNING" to set the
                broad category of data.
        :param references: List of dictionaries or :class:`.Reference` objects where information about the
                property is published.
        :param contacts: List of dictionaries, strings, or :class:`.Person` objects with people to contact for
                information about the property.
        :param licenses: List of dictionaries, strings, or :class:`.License` objects with licensing information
                for the property.
        :param tags: List of strings or numbers that are tags for this object.
        :param kwargs: Dictionary of fields that are not supported.
        """
        # The order of the constructors is important here. The second constructor could overwrite values set during
        # the first if there is overlap.
        Value.__init__(self, name=name, scalars=scalars, vectors=vectors, matrices=matrices, files=files,
                       units=units, tags=tags, **kwargs)
        self.references = references
        self.contacts = contacts
        self.licenses = licenses
        self._conditions = None
        self.conditions = conditions
        self._methods = None
        self.methods = methods
        if 'method' in kwargs:
            self._methods = method
        self._data_type = None
        self.data_type = data_type

    @property
    def conditions(self):
        return self._conditions

    @conditions.setter
    def conditions(self, conditions):
        self._validate_list_type('conditions', conditions, dict, Value)
        self._conditions = self._get_object(Value, conditions)

    @conditions.deleter
    def conditions(self):
        self._conditions = None

    @property
    def method(self):
        warn("method has been deprecated in favor of methods")
        return self._methods

    @method.setter
    def method(self, method):
        warn("method has been deprecated in favor of methods")
        self._validate_type('method', method, dict, Method)
        self._method = self._get_object(Method, method)

    @method.deleter
    def method(self):
        warn("method has been deprecated in favor of methods")
        self._method = None

    @property
    def methods(self):
        return self._methods

    @methods.setter
    def methods(self, methods):
        self._validate_list_type('methods', methods, dict, Method)
        self._methods = self._get_object(Method, methods)

    @methods.deleter
    def methods(self):
        self._methods = None

    @property
    def data_type(self):
        return self._data_type

    @data_type.setter
    def data_type(self, data_type):
        self._validate_type('data_type', data_type, string_types)
        self._data_type = data_type

    @data_type.deleter
    def data_type(self):
        self._data_type = None
<|endoftext|>