<commit_before>from __future__ import division

import numpy as np

import chainer
from chainer.backends import cuda
import chainer.functions as F

from chainercv.experimental.links.model.fcis.utils.proposal_target_creator \
    import ProposalTargetCreator
from chainercv.links.model.faster_rcnn.faster_rcnn_train_chain \
    import _fast_rcnn_loc_loss
from chainercv.links.model.faster_rcnn.utils.anchor_target_creator \
    import AnchorTargetCreator
from chainercv.utils import mask_to_bbox

class FCISTrainChain(chainer.Chain):

    def __init__(
            self, fcis,
            rpn_sigma=3.0, roi_sigma=1.0,
            n_sample=128,
            pos_ratio=0.25, pos_iou_thresh=0.5,
            neg_iou_thresh_hi=0.5, neg_iou_thresh_lo=0.0,
            mask_size=21, binary_thresh=0.4
    ):

        super(FCISTrainChain, self).__init__()
        with self.init_scope():
            self.fcis = fcis
        self.rpn_sigma = rpn_sigma
        self.roi_sigma = roi_sigma
        self.n_sample = n_sample

        self.loc_normalize_mean = fcis.loc_normalize_mean
        self.loc_normalize_std = fcis.loc_normalize_std

        self.anchor_target_creator = AnchorTargetCreator()
        self.proposal_target_creator = ProposalTargetCreator(
            n_sample=n_sample,
            loc_normalize_mean=self.loc_normalize_mean,
            loc_normalize_std=self.loc_normalize_std,
            pos_ratio=pos_ratio, pos_iou_thresh=pos_iou_thresh,
            neg_iou_thresh_hi=neg_iou_thresh_hi,
            neg_iou_thresh_lo=neg_iou_thresh_lo,
            mask_size=mask_size, binary_thresh=binary_thresh)

    def __call__(self, x, masks, labels, scale=1.0):
        n = masks.shape[0]
        # batch size = 1
        if n != 1:
            raise ValueError('Currently only batch size 1 is supported.')

        _, _, H, W = x.shape
        img_size = (H, W)
        assert img_size == masks.shape[2:]

        rpn_features, roi_features = self.fcis.extractor(x)
        rpn_locs, rpn_scores, rois, roi_indices, anchor = self.fcis.rpn(
            rpn_features, img_size, scale)

        # batch size = 1
        mask = masks[0]
        bbox = mask_to_bbox(mask)
        label = labels[0]
        rpn_score = rpn_scores[0]
        rpn_loc = rpn_locs[0]
        roi = rois

        # Sample RoIs and forward
        sample_roi, gt_roi_loc, gt_roi_mask, gt_roi_label = \
            self.proposal_target_creator(roi, bbox, mask, label)

        sample_roi_index = self.xp.zeros(
            (len(sample_roi),), dtype=np.int32)
        roi_ag_seg_score, roi_ag_loc, roi_cls_score, _, _ = self.fcis.head(
            roi_features, sample_roi, sample_roi_index, img_size, gt_roi_label)

        # RPN losses
        gt_rpn_loc, gt_rpn_label = self.anchor_target_creator(
            bbox, anchor, img_size)

        # CPU -> GPU
        if cuda.get_device_from_array(gt_rpn_loc).id == -1:
            gt_rpn_loc = cuda.to_gpu(gt_rpn_loc)
            gt_rpn_label = cuda.to_gpu(gt_rpn_label)

        rpn_loc_loss = _fast_rcnn_loc_loss(
            rpn_loc, gt_rpn_loc, gt_rpn_label, self.rpn_sigma)
        rpn_cls_loss = F.softmax_cross_entropy(rpn_score, gt_rpn_label)

        # Losses for outputs of the head
        n_rois = roi_ag_loc.shape[0]
        gt_roi_fg_label = (gt_roi_label > 0).astype(np.int)
        roi_loc = roi_ag_loc[self.xp.arange(n_rois), gt_roi_fg_label, :]

        roi_loc_loss = _fast_rcnn_loc_loss(
            roi_loc, gt_roi_loc, gt_roi_label, self.roi_sigma)
        roi_cls_loss = F.softmax_cross_entropy(
            roi_cls_score, gt_roi_label)
        roi_mask_loss = F.softmax_cross_entropy(
            roi_ag_seg_score, gt_roi_mask)

        loss = rpn_loc_loss + rpn_cls_loss \
            + roi_loc_loss + roi_cls_loss + roi_mask_loss
        chainer.reporter.report({
            'rpn_loc_loss': rpn_loc_loss,
            'rpn_cls_loss': rpn_cls_loss,
            'roi_loc_loss': roi_loc_loss,
            'roi_cls_loss': roi_cls_loss,
            'roi_mask_loss': roi_mask_loss,
            'loss': loss,
        }, self)

        return loss
<commit_msg>fix typo in fcis_train_chain<commit_after>from __future__ import division

import numpy as np

import chainer
from chainer.backends import cuda
import chainer.functions as F

from chainercv.experimental.links.model.fcis.utils.proposal_target_creator \
    import ProposalTargetCreator
from chainercv.links.model.faster_rcnn.faster_rcnn_train_chain \
    import _fast_rcnn_loc_loss
from chainercv.links.model.faster_rcnn.utils.anchor_target_creator \
    import AnchorTargetCreator
from chainercv.utils import mask_to_bbox

class FCISTrainChain(chainer.Chain):

    def __init__(
            self, fcis,
            rpn_sigma=3.0, roi_sigma=1.0,
            n_sample=128,
            pos_ratio=0.25, pos_iou_thresh=0.5,
            neg_iou_thresh_hi=0.5, neg_iou_thresh_lo=0.0,
            mask_size=21, binary_thresh=0.4
    ):

        super(FCISTrainChain, self).__init__()
        with self.init_scope():
            self.fcis = fcis
        self.rpn_sigma = rpn_sigma
        self.roi_sigma = roi_sigma
        self.n_sample = n_sample

        self.loc_normalize_mean = fcis.loc_normalize_mean
        self.loc_normalize_std = fcis.loc_normalize_std

        self.anchor_target_creator = AnchorTargetCreator()
        self.proposal_target_creator = ProposalTargetCreator(
            n_sample=n_sample,
            loc_normalize_mean=self.loc_normalize_mean,
            loc_normalize_std=self.loc_normalize_std,
            pos_ratio=pos_ratio, pos_iou_thresh=pos_iou_thresh,
            neg_iou_thresh_hi=neg_iou_thresh_hi,
            neg_iou_thresh_lo=neg_iou_thresh_lo,
            mask_size=mask_size, binary_thresh=binary_thresh)

    def __call__(self, x, masks, labels, scale=1.0):
        n = masks.shape[0]
        # batch size = 1
        if n != 1:
            raise ValueError('Currently only batch size 1 is supported.')

        _, _, H, W = x.shape
        img_size = (H, W)
        assert img_size == masks.shape[2:]

        rpn_features, roi_features = self.fcis.extractor(x)
        rpn_locs, rpn_scores, rois, roi_indices, anchor = self.fcis.rpn(
            rpn_features, img_size, scale)

        # batch size = 1
        mask = masks[0]
        bbox = mask_to_bbox(mask)
        label = labels[0]
        rpn_score = rpn_scores[0]
        rpn_loc = rpn_locs[0]
        roi = rois

        # Sample RoIs and forward
        sample_roi, gt_roi_loc, gt_roi_mask, gt_roi_label = \
            self.proposal_target_creator(roi, bbox, mask, label)

        sample_roi_index = self.xp.zeros(
            (len(sample_roi),), dtype=np.int32)
        roi_ag_seg_score, roi_ag_loc, roi_cls_score, _, _ = self.fcis.head(
            roi_features, sample_roi, sample_roi_index, img_size, gt_roi_label)

        # RPN losses
        gt_rpn_loc, gt_rpn_label = self.anchor_target_creator(
            bbox, anchor, img_size)

        # CPU -> GPU
        if cuda.get_array_module(rpn_loc.array) != np:
            gt_rpn_loc = cuda.to_gpu(gt_rpn_loc)
            gt_rpn_label = cuda.to_gpu(gt_rpn_label)

        rpn_loc_loss = _fast_rcnn_loc_loss(
            rpn_loc, gt_rpn_loc, gt_rpn_label, self.rpn_sigma)
        rpn_cls_loss = F.softmax_cross_entropy(rpn_score, gt_rpn_label)

        # Losses for outputs of the head
        n_rois = roi_ag_loc.shape[0]
        gt_roi_fg_label = (gt_roi_label > 0).astype(np.int)
        roi_loc = roi_ag_loc[self.xp.arange(n_rois), gt_roi_fg_label, :]

        roi_loc_loss = _fast_rcnn_loc_loss(
            roi_loc, gt_roi_loc, gt_roi_label, self.roi_sigma)
        roi_cls_loss = F.softmax_cross_entropy(
            roi_cls_score, gt_roi_label)
        roi_mask_loss = F.softmax_cross_entropy(
            roi_ag_seg_score, gt_roi_mask)

        loss = rpn_loc_loss + rpn_cls_loss \
            + roi_loc_loss + roi_cls_loss + roi_mask_loss
        chainer.reporter.report({
            'rpn_loc_loss': rpn_loc_loss,
            'rpn_cls_loss': rpn_cls_loss,
            'roi_loc_loss': roi_loc_loss,
            'roi_cls_loss': roi_cls_loss,
            'roi_mask_loss': roi_mask_loss,
            'loss': loss,
        }, self)

        return loss
<|endoftext|>


<commit_before>from django.core.management.base import BaseCommand

from protein.models import Protein
from structure.models import Structure
from protein.models import ProteinSegment
from common.alignment import Alignment
from common.models import WebLink
import pprint
from collections import OrderedDict

class Command(BaseCommand):

    def handle(self, *args, **options):
        Homology_model = HomologyModeling('5ht2b_human', 'Agonist')
        multi_alignment = Homology_model.run_pairwise_alignment()
        main_template = Homology_model.select_main_template(multi_alignment)
        main_alignment = Homology_model.run_main_alignment(Homology_model.reference_protein,main_template)
        non_conserved_alignment = Homology_model.run_non_conserved_switcher(main_alignment)

        self.stdout.write(Homology_model.statistics, ending='')

class HomologyModeling(object):
    ''' Class to build homology models for GPCRs.

        @param reference_id: str, protein id \n
        @param role: str, endogenous ligand role of reference \n
        @param query_roles: list, list of endogenous ligand roles to be applied for template search \n
        @param testing: boolean, test program with existing structure, default: False \n
    '''
    def __init__(self, reference_id, role, query_roles='default', testing=False):
        self.reference_id = reference_id
        self.role = role
        self.query_roles = query_roles
        self.testing = testing

    def __repr__(self):
        return "<{}, {}>".format(self.reference_id, self.role)

    def get_receptor_data(self, receptor):
        ''' Get Protein object of receptor.

            @param receptor: str, protein id
        '''
        self.reference_protein = Protein.objects.get(entry_name=self.reference_id)
        self.uniprot_id = self.reference_protein.accession
        self.reference_sequence = self.reference_protein.sequence
        self.statistics = CreateStatistics(self.uniprot_id)
        return self.reference_protein

    def get_structure_data(self, pdb):
        ''' Get Structure object based on pdb code.

            @param pdb: str, pdb code
        '''
        slist = Structure.objects.get(pdb_code_id__index=pdb)
        return slist.resolution

    def get_target_structures_data(self, role):
        ''' Get all target Structure objects based on endogenous ligand role. Returns QuerySet object.

            @param role: str, endogenous ligand role
        '''
        self.role = role

        if self.query_roles == 'default':
            if self.testing == False:
                self.structures_datatable = Structure.objects.filter(endogenous_ligand_id__role_id__role=self.role).order_by('protein_id','-resolution').distinct('protein_id')
            elif self.testing == True:
                self.structures_datatable = Structure.objects.filter(endogenous_ligand_id__role_id__role=self.role).order_by('protein_id','-resolution').distinct('protein_id').exclude(protein_id__entry_name=self.reference_id)
        elif type(self.query_roles) is list:
            for query_role in self.query_roles:
                assert (Structure.objects.filter(endogenous_ligand_id__role_id__role=query_role)), "InputError: query role argument {} is not valid. No such entry in the database.".format(query_role)
            if self.testing == False:
                self.structures_datatable = Structure.objects.filter(endogenous_ligand_id__role_id__role__in=self.query_roles).order_by('protein_id','-resolution').distinct('protein_id')
            elif self.testing == True:
                self.structures_datatable = Structure.objects.filter(endogenous_ligand_id__role_id__role__in=self.query_roles).order_by('protein_id','-resolution').distinct('protein_id').exclude(protein_id__entry_name=self.reference_id)
        return self.structures_datatable

    def get_targets_protein_data(self, structures_data):
        ''' Get all target Protein objects based on Structure objects. Returns a list of Protein objects.

            @param structures_data: QuerySet, query set of Structure objects. Output of get_target_structures_data function.
        '''
        plist = []
        for target in structures_data:
            plist.append(Protein.objects.get(id=target.protein_id))
        return plist

    def run_pairwise_alignment(self, segments='default', reference=True, calculate_similarity=True, targets=None):
        ''' Creates pairwise alignment between reference and target receptor(s).
            Returns Alignment object.

            @param segments: list, list of segments to use, e.g.: ['TM1','IL1','TM2','EL1'] \n
            @param reference: boolean, if True, reference receptor is used as reference, default: True.
            @param calculate_similarity: boolean, if True, call Alignment.calculate_similarity, default: True.
            @param targets: list, list of Protein objects to use as targets. By default it uses all targets with role
            specified when initializing the HomologyModeling() class.
        '''
        if not targets:
            targets = self.get_targets_protein_data(self.get_target_structures_data(self.role))

        # core functions from alignment.py
        a = Alignment()
        if reference==True:
            a.load_reference_protein(self.get_receptor_data(self.reference_id))
        a.load_proteins(targets)
        if segments=='default':
            segments = ProteinSegment.objects.filter(slug__in=['TM1','TM2','TM3','TM4','TM5','TM6','TM7'])
            a.load_segments(segments)
        else:
            segments = ProteinSegment.objects.filter(slug__in=segments)
            a.load_segments(segments)
        a.build_alignment()
        if calculate_similarity==True:
            a.calculate_similarity()
        return a

    def select_main_template(self, alignment):
        ''' Select main template for homology model based on highest sequence similarity. Returns Structure object of main template.

            @param alignment: Alignment, aligment object where the first protein is the reference. Output of pairwise_alignment function.
        '''
        self.similarity_table = OrderedDict()
        self.identity_table = OrderedDict()

        for i in range(1,len(alignment.proteins)):
            self.similarity_table[alignment.proteins[i]] = int(alignment.proteins[i].similarity)
            self.identity_table[alignment.proteins[i]] = int(alignment.proteins[i].identity)

        best = [0,0]
        for key, value in self.similarity_table.items():
            if best[1]<value:
                best = [key,value]
            elif value==0:
                pass
            elif best[1]==value:
                best+=[key,value]

        main_structure = self.structures_datatable.get(protein_id__entry_name=best[0])

        self.main_pdb_id = str(WebLink.objects.get(id=main_structure.pdb_code_id))[-4:]
        self.main_template_sequence = best[0].sequence
        if len(main_structure.preferred_chain)>1:
            self.main_template_preferred_chain = main_structure.preferred_chain[0]
        else:
            self.main_template_preferred_chain = main_structure.preferred_chain

        self.statistics.add_info("main_template", self.main_pdb_id)
        self.statistics.add_info("preferred_chain", self.main_template_preferred_chain)
        return main_structure

    def run_main_alignment(self, reference, main_template):
        ''' Creates an alignment between reference (Protein object) and main_template (Structure object)
            where matching residues are depicted with the one-letter residue code, mismatches with '.',
            gaps with '-', gaps due to shorter sequences with 'x'. returns a AlignedReferenceAndTemplate class.

            @param reference: Protein object, reference receptor
            @param main_template: Structure object, main template
        '''
        main_template_protein = Protein.objects.get(id=main_template.protein_id)
        a = self.run_pairwise_alignment(reference=False, calculate_similarity=False, targets=[reference, main_template_protein])
        ref = a.proteins[0].alignment
        temp = a.proteins[1].alignment
        reference_string = ''
        template_string = ''
        matching_string = ''
        reference_dict = OrderedDict()
        template_dict = OrderedDict()
        segment_count = 0

        for ref_segment, temp_segment in zip(ref,temp):
            segment_count+=1
            for ref_position, temp_position in zip(ref_segment,temp_segment):

                if ref_position[1]!=False and temp_position[1]!=False:
                    if ref_position[0]==temp_position[0]:
                        reference_dict[ref_position[0]]=ref_position[2]
                        template_dict[temp_position[0]]=temp_position[2]
                        reference_string+=ref_position[2]
                        template_string+=temp_position[2]
                        if ref_position[2]==temp_position[2]:
                            matching_string+=ref_position[2]
                        else:
                            matching_string+='.'
                    else:
                        print("Error: Generic numbers don't align")

                elif ref_position[1]!=False and temp_position[1]==False:
                    reference_dict[ref_position[0]]=ref_position[2]
                    reference_string+=ref_position[2]
                    if temp_position[2]=='-':
                        template_dict[temp_position[0]]='-'
                        template_string+='-'
                        matching_string+='-'
                    elif temp_position[2]=='_':
                        template_dict[temp_position[0]]='x'
                        template_string+='x'
                        matching_string+='x'

                elif ref_position[2]=='-' and temp_position[1]!=False:
                    reference_dict[ref_position[0]]='-'
                    template_dict[temp_position[0]]=temp_position[2]
                    reference_string+='-'
                    template_string+=temp_position[2]
                    matching_string+='-'

                elif ref_position[2]=='-' and temp_position[2]=='-':
                    reference_dict[ref_position[0]]='-'
                    template_dict[temp_position[0]]='-'
                    reference_string+='-'
                    template_string+='-'
                    matching_string+='-'
            reference_dict["TM"+str(segment_count)+"_end"]='/'
            template_dict["TM"+str(segment_count)+"_end"]='/'
            reference_string+='/'
            template_string+='/'
            matching_string+='/'

        main_alignment = AlignedReferenceAndTemplate(self.reference_id, self.main_pdb_id, reference_dict, template_dict, matching_string)
        return main_alignment

    def run_non_conserved_switcher(self, alignment_input, switch_bulges=True, switch_constrictions=True):
        '''
        '''
        ref_length = 0
        ref_bulge_list, temp_bulge_list, ref_const_list, temp_const_list = [],[],[],[]

        for ref_res, temp_res, aligned_res in zip(alignment_input.reference_dict, alignment_input.template_dict, alignment_input.aligned_string):
            if ref_res!='-' and ref_res!='/':
                ref_length+=1

            gn = ref_res
            gn_TM = self.gn_num_extract(gn, 'x')[0]
            gn_num = self.gn_num_extract(gn, 'x')[1]

            # bulges and constrictions
            if aligned_res=='-':
                if switch_bulges==True:
                    if alignment_input.reference_dict[ref_res]=='-' and alignment_input.reference_dict[self.gn_indecer(gn,'x',-1)] not in ['-','/'] and alignment_input.reference_dict[self.gn_indecer(gn,'x',+1)] not in ['-','/']: #and alignment_input.reference_dict[self.gn_indecer(gn,'x',-1)]!='/' and alignment_input.reference_dict[self.gn_indecer(gn,'x',+1)]!='/':

                        # bulge in template
                        if len(str(gn_num))==3:
                            temp_bulge_list.append({gn:alignment_input.template_dict[temp_res]})
                            print(gn, gn_TM, gn_num, alignment_input.template_dict[self.gn_indecer(gn, 'x', -1)])
                        # constriction in target
                        else:
                            pass

                    elif alignment_input.template_dict[temp_res]=='-' and alignment_input.template_dict[self.gn_indecer(gn,'x',-1)] not in ['-','/'] and alignment_input.template_dict[self.gn_indecer(gn,'x',+1)] not in ['-','/']: #and alignment_input.reference_dict[self.gn_indecer(gn,'x',-1)]!='/' and alignment_input.reference_dict[self.gn_indecer(gn,'x',+1)]!='/':

                        # bulge in target
                        if len(str(gn_num))==3:
                            ref_bulge_list.append({gn:alignment_input.reference_dict[ref_res]})
                            print(gn, gn_TM, gn_num, alignment_input.template_dict[self.gn_indecer(gn, 'x', -1)])

                        # constriction in template
                        else:
                            pass
        print(temp_bulge_list)
        print(ref_bulge_list)

    def gn_num_extract(self, gn, delimiter):
        try:
            split = gn.split(delimiter)
            return int(split[0]), int(split[1])
        except:
            return '/', '/'

    def gn_indecer(self, gn, delimiter, direction):
        split = self.gn_num_extract(gn, delimiter)
        if split[0]!='/':
            if len(str(split[1]))==2:
                return str(split[0])+delimiter+str(split[1]+direction)
            elif len(str(split[1]))==3:
                if direction<0:
                    direction += 1
                return str(split[0])+delimiter+str(int(str(split[1])[:2])+direction)
        return '/'

class BulgesAndConstrictions(object):
    def __init__(self):
        pass
    def bulge_in_reference():
        pass

class AlignedReferenceAndTemplate(object):
    ''' Representation class for HomologyModeling.run_main_alignment() function.
    '''
    def __init__(self, reference_id, template_id, reference_dict, template_dict, aligned_string):
        self.reference_id = reference_id
        self.template_id = template_id
        self.reference_dict = reference_dict
        self.template_dict = template_dict
        self.aligned_string = aligned_string

    def __repr__(self):
        return "<{}, {}>".format(self.reference_id,self.template_id)

class CreateStatistics(object):
    ''' Statistics dictionary for HomologyModeling.
    '''
    def __init__(self, uniprot):
        self.uniprot = uniprot
        self.info_dict = OrderedDict()

    def __repr__(self):
        return "<{} \n {} \n>".format(self.uniprot, self.info_dict)

    def add_info(self, info_name, info):
        ''' Adds new information to the statistics dictionary.

            @param info_name: str, info name as dictionary key
            @param info: object, any object as value
        '''
        self.info_dict[info_name] = info

        <commit_msg>main template search fix<commit_after>from django.core.management.base import BaseCommand

from protein.models import Protein
from structure.models import Structure
from protein.models import ProteinSegment
from common.alignment import Alignment
from common.models import WebLink
import pprint
from collections import OrderedDict

class Command(BaseCommand):

    def handle(self, *args, **options):
        Homology_model = HomologyModeling('5ht2b_human', 'Agonist')
        multi_alignment = Homology_model.run_pairwise_alignment()
        main_template = Homology_model.select_main_template(multi_alignment)
        main_alignment = Homology_model.run_main_alignment(Homology_model.reference_protein, main_template)

        non_conserved_alignment = Homology_model.run_non_conserved_switcher(main_alignment)

#        self.stdout.write(Homology_model.statistics, ending='')

class HomologyModeling(object):
    ''' Class to build homology models for GPCRs.

        @param reference_id: str, protein id \n
        @param role: str, endogenous ligand role of reference \n
        @param query_roles: list, list of endogenous ligand roles to be applied for template search, default: same as reference \n
        @param testing: boolean, test program with existing structure, default: False \n
    '''
    def __init__(self, reference_id, role, query_roles='default', testing=False):
        self.reference_id = reference_id
        self.role = role
        self.query_roles = query_roles
        self.testing = testing

    def __repr__(self):
        return "<{}, {}>".format(self.reference_id, self.role)

    def get_receptor_data(self, receptor):
        ''' Get Protein object of receptor.

            @param receptor: str, protein id
        '''
        self.reference_protein = Protein.objects.get(entry_name=self.reference_id)
        self.uniprot_id = self.reference_protein.accession
        self.reference_sequence = self.reference_protein.sequence
        self.statistics = CreateStatistics(self.uniprot_id)
        return self.reference_protein

    def get_structure_data(self, pdb):
        ''' Get Structure object based on pdb code.

            @param pdb: str, pdb code
        '''
        slist = Structure.objects.get(pdb_code_id__index=pdb)
        return slist.resolution

    def get_targets_structure_data(self, role, query_roles):
        ''' Get all target Structure objects based on endogenous ligand role. Returns QuerySet object.

            @param role: str, endogenous ligand role
        '''
        self.role = role
        self.query_roles = query_roles

        if self.query_roles == 'default':
            if self.testing == False:
                self.structures_datatable = Structure.objects.filter(endogenous_ligand_id__role_id__role=self.role).order_by('protein_id','-resolution').distinct('protein_id')
            elif self.testing == True:
                self.structures_datatable = Structure.objects.filter(endogenous_ligand_id__role_id__role=self.role).order_by('protein_id','-resolution').distinct('protein_id').exclude(protein_id__entry_name=self.reference_id)
        elif type(self.query_roles) is list:
            for query_role in self.query_roles:
                assert (Structure.objects.filter(endogenous_ligand_id__role_id__role=query_role)), "InputError: query role argument {} is not valid. No such entry in the database.".format(query_role)
            if self.testing == False:
                self.structures_datatable = Structure.objects.filter(endogenous_ligand_id__role_id__role__in=self.query_roles).order_by('protein_id','-resolution').distinct('protein_id')
            elif self.testing == True:
                self.structures_datatable = Structure.objects.filter(endogenous_ligand_id__role_id__role__in=self.query_roles).order_by('protein_id','-resolution').distinct('protein_id').exclude(protein_id__entry_name=self.reference_id)
        return self.structures_datatable

    def get_targets_protein_data(self, structures_data):
        ''' Get all target Protein objects based on Structure objects. Returns a list of Protein objects.

            @param structures_data: QuerySet, query set of Structure objects. Output of get_targets_structure_data function.
        '''
        plist = []
        for target in structures_data:
            plist.append(Protein.objects.get(id=target.protein_id))
        return plist

    def run_pairwise_alignment(self, segments='default_7TM', reference=True, calculate_similarity=True, targets=None):
        ''' Creates pairwise alignment between reference and target receptor(s).
            Returns Alignment object.

            @param segments: list, list of segments to use, e.g.: ['TM1','IL1','TM2','EL1'] \n
            @param reference: boolean, if True, reference receptor is used as reference, default: True.
            @param calculate_similarity: boolean, if True, call Alignment.calculate_similarity, default: True.
            @param targets: list, list of Protein objects to use as targets. By default it uses all targets with role
            specified when initializing the HomologyModeling() class.
        '''
        self.segments = segments
        if not targets:
            targets = self.get_targets_protein_data(self.get_targets_structure_data(self.role, self.query_roles))

        # core functions from alignment.py
        a = Alignment()
        if reference==True:
            a.load_reference_protein(self.get_receptor_data(self.reference_id))
        a.load_proteins(targets)
        if self.segments=='default_7TM':
            self.segments = ProteinSegment.objects.filter(slug__in=['TM1','TM2','TM3','TM4','TM5','TM6','TM7'])
            a.load_segments(self.segments)
        else:
            self.segments = ProteinSegment.objects.filter(slug__in=self.segments)
            a.load_segments(self.segments)
        a.build_alignment()
        if calculate_similarity==True:
            a.calculate_similarity()
        return a

    def select_main_template(self, alignment):
        ''' Select main template for homology model based on highest sequence similarity. Returns Structure object of main template.

            @param alignment: Alignment, aligment object where the first protein is the reference. Output of pairwise_alignment function.
        '''
        self.similarity_table = OrderedDict()
        self.identity_table = OrderedDict()

        for i in range(1,len(alignment.proteins)):
            self.similarity_table[alignment.proteins[i]] = int(alignment.proteins[i].similarity)
            self.identity_table[alignment.proteins[i]] = int(alignment.proteins[i].identity)

        best = [0,0]
        for key, value in self.similarity_table.items():
            if best[1]<value:
                best = [key,value]
            elif value==0:
                pass
            elif best[1]==value:
                best+=[key,value]

        main_structure = self.structures_datatable.get(protein_id__entry_name=best[0])

        self.main_pdb_id = str(WebLink.objects.get(id=main_structure.pdb_code_id))[-4:]
        self.main_template_sequence = best[0].sequence
        if len(main_structure.preferred_chain)>1:
            self.main_template_preferred_chain = main_structure.preferred_chain[0]
        else:
            self.main_template_preferred_chain = main_structure.preferred_chain

        self.statistics.add_info("main_template", self.main_pdb_id)
        self.statistics.add_info("preferred_chain", self.main_template_preferred_chain)
        return main_structure

    def run_main_alignment(self, reference, main_template, segments='default_7TM'):
        ''' Creates an alignment between reference (Protein object) and main_template (Structure object)
            where matching residues are depicted with the one-letter residue code, mismatches with '.',
            gaps with '-', gaps due to shorter sequences with 'x'. returns a AlignedReferenceAndTemplate class.

            @param reference: Protein object, reference receptor
            @param main_template: Structure object, main template
        '''

        main_template_protein = Protein.objects.get(id=main_template.protein_id)
        if segments=='default_7TM':
            a = self.run_pairwise_alignment(reference=False, calculate_similarity=False, targets=[reference, main_template_protein])
        else:
            self.segments = segments
            a = self.run_pairwise_alignment(segments=self.segments, reference=False, calculate_similarity=False, targets=[reference, main_template_protein])
        ref = a.proteins[0].alignment
        temp = a.proteins[1].alignment
        reference_string = ''
        template_string = ''
        matching_string = ''
        reference_dict = OrderedDict()
        template_dict = OrderedDict()
        segment_count = 0

        for ref_segment, temp_segment in zip(ref,temp):
            segment_count+=1
            for ref_position, temp_position in zip(ref_segment,temp_segment):

                if ref_position[1]!=False and temp_position[1]!=False:
                    if ref_position[0]==temp_position[0]:
                        reference_dict[ref_position[0]]=ref_position[2]
                        template_dict[temp_position[0]]=temp_position[2]
                        reference_string+=ref_position[2]
                        template_string+=temp_position[2]
                        if ref_position[2]==temp_position[2]:
                            matching_string+=ref_position[2]
                        else:
                            matching_string+='.'
                    else:
                        print("Error: Generic numbers don't align")

                elif ref_position[1]!=False and temp_position[1]==False:
                    reference_dict[ref_position[0]]=ref_position[2]
                    reference_string+=ref_position[2]
                    if temp_position[2]=='-':
                        template_dict[temp_position[0]]='-'
                        template_string+='-'
                        matching_string+='-'
                    elif temp_position[2]=='_':
                        template_dict[temp_position[0]]='x'
                        template_string+='x'
                        matching_string+='x'

                elif ref_position[2]=='-' and temp_position[1]!=False:
                    reference_dict[ref_position[0]]='-'
                    template_dict[temp_position[0]]=temp_position[2]
                    reference_string+='-'
                    template_string+=temp_position[2]
                    matching_string+='-'

                elif ref_position[2]=='-' and temp_position[2]=='-':
                    reference_dict[ref_position[0]]='-'
                    template_dict[temp_position[0]]='-'
                    reference_string+='-'
                    template_string+='-'
                    matching_string+='-'
            reference_dict["TM"+str(segment_count)+"_end"]='/'
            template_dict["TM"+str(segment_count)+"_end"]='/'
            reference_string+='/'
            template_string+='/'
            matching_string+='/'

        main_alignment = AlignedReferenceAndTemplate(self.reference_id, self.main_pdb_id, reference_dict, template_dict, matching_string)
        return main_alignment

    def run_non_conserved_switcher(self, alignment_input, switch_bulges=True, switch_constrictions=True):
        '''
        '''
        ref_length = 0
        ref_bulge_list, temp_bulge_list, ref_const_list, temp_const_list = [],[],[],[]

        for ref_res, temp_res, aligned_res in zip(alignment_input.reference_dict, alignment_input.template_dict, alignment_input.aligned_string):
            if ref_res!='-' and ref_res!='/':
                ref_length+=1

            gn = ref_res
            gn_TM = self.gn_num_extract(gn, 'x')[0]
            gn_num = self.gn_num_extract(gn, 'x')[1]

            # bulges and constrictions
            if aligned_res=='-':
                if switch_bulges==True:
                    if alignment_input.reference_dict[ref_res]=='-' and alignment_input.reference_dict[self.gn_indecer(gn,'x',-1)] not in ['-','/'] and alignment_input.reference_dict[self.gn_indecer(gn,'x',+1)] not in ['-','/']: #and alignment_input.reference_dict[self.gn_indecer(gn,'x',-1)]!='/' and alignment_input.reference_dict[self.gn_indecer(gn,'x',+1)]!='/':

                        # bulge in template
                        if len(str(gn_num))==3:
                            temp_bulge_list.append({gn:alignment_input.template_dict[temp_res]})
#                            print(gn, gn_TM, gn_num, alignment_input.template_dict[self.gn_indecer(gn, 'x', -1)])
                        # constriction in target
                        else:
                            pass

                    elif alignment_input.template_dict[temp_res]=='-' and alignment_input.template_dict[self.gn_indecer(gn,'x',-1)] not in ['-','/'] and alignment_input.template_dict[self.gn_indecer(gn,'x',+1)] not in ['-','/']: #and alignment_input.reference_dict[self.gn_indecer(gn,'x',-1)]!='/' and alignment_input.reference_dict[self.gn_indecer(gn,'x',+1)]!='/':

                        # bulge in target
                        if len(str(gn_num))==3:
                            ref_bulge_list.append({gn:alignment_input.reference_dict[ref_res]})
#                            print(gn, gn_TM, gn_num, alignment_input.template_dict[self.gn_indecer(gn, 'x', -1)])

                        # constriction in template
                        else:
                            pass
#        print(temp_bulge_list)
#        print(ref_bulge_list)

    def gn_num_extract(self, gn, delimiter):
        try:
            split = gn.split(delimiter)
            return int(split[0]), int(split[1])
        except:
            return '/', '/'

    def gn_indecer(self, gn, delimiter, direction):
        split = self.gn_num_extract(gn, delimiter)
        if split[0]!='/':
            if len(str(split[1]))==2:
                return str(split[0])+delimiter+str(split[1]+direction)
            elif len(str(split[1]))==3:
                if direction<0:
                    direction += 1
                return str(split[0])+delimiter+str(int(str(split[1])[:2])+direction)
        return '/'

class BulgesAndConstrictions(object):
    def __init__(self):
        pass
    def bulge_in_reference():
        pass

class AlignedReferenceAndTemplate(object):
    ''' Representation class for HomologyModeling.run_main_alignment() function.
    '''
    def __init__(self, reference_id, template_id, reference_dict, template_dict, aligned_string):
        self.reference_id = reference_id
        self.template_id = template_id
        self.reference_dict = reference_dict
        self.template_dict = template_dict
        self.aligned_string = aligned_string

    def __repr__(self):
        return "<{}, {}>".format(self.reference_id,self.template_id)

class CreateStatistics(object):
    ''' Statistics dictionary for HomologyModeling.
    '''
    def __init__(self, uniprot):
        self.uniprot = uniprot
        self.info_dict = OrderedDict()

    def __repr__(self):
        return "<{} \n {} \n>".format(self.uniprot, self.info_dict)

    def add_info(self, info_name, info):
        ''' Adds new information to the statistics dictionary.

            @param info_name: str, info name as dictionary key
            @param info: object, any object as value
        '''
        self.info_dict[info_name] = info

        <|endoftext|>


<commit_before>from __future__ import print_function
import math
import numpy as np

class Survey:
    def __init__(self):
        pass

    def __get_species_in_subject__(self,aggregations):
        """
        use Ali's and Margaret's code to determine how many species are a given subject
        and return those X top species
        :return:
        """
        num_species = int(np.median(aggregations["num species"]))
        assert(num_species >= 1)
        # sort the species by the number of votes
        species_by_vote = []

        for species_id in aggregations:
            if species_id not in ["num users","num species"]:
                species_by_vote.append((species_id,aggregations[species_id]["num votes"]))
        sorted_species = sorted(species_by_vote,key = lambda x:x[1],reverse=True)

        return sorted_species[:num_species]

    def __shannon_entropy__(self,probabilities):
        return -sum([p*math.log(p) for p in probabilities])

    def __calc__pielou__(self,aggregations):
        """
        return a row for survey tasks with subject id and pielou index
        :param aggregations:
        :return:
        """
        # for development - a few bad aggregations have crept into the db (for small projects) so skip them
        if max(aggregations["num species"]) == 0:
            return ""

        num_users = aggregations["num users"]
        probabilities = []
        for details in aggregations.values():
            # list => "num species", int => "num users"
            if type(details) in [list,int]:
                continue

            probabilities.append(details["num votes"]/float(num_users))

        num_species = len(probabilities)

        if num_species == 1:
            pielou_index = 0
        else:
            shannon = self.__shannon_entropy__(probabilities)
            pielou_index = shannon/math.log(num_species)

        return pielou_index

    def __species_annotation__(self,aggregation_so_far,annotation):
        """
        for a given user's annotation - go through each species that the user marked and process that annotation
        :param aggregation_so_far: aggregations so far for a single subject
        :param annotation: the next annotation for that subject
        :return:
        """
        for species_ann in annotation:
            species = species_ann["choice"]

            # is the first classification we've encountered for this species?
            if species not in aggregation_so_far:
                aggregation_so_far[species] = {"num votes": 1,"followup":{}}

            else:
                aggregation_so_far[species]["num votes"] += 1

            # go through each of the follow up questions
            for question,answer in species_ann["answers"].items():
                # is this the first time we've seen this particular followup question
                # should probably always happen the first time we see the species (since most, if not
                # all of the time, all of the follow up questions should be required)
                if question not in aggregation_so_far[species]["followup"]:
                    aggregation_so_far[species]["followup"][question] = {}

                # if list - multiple answers are allowed
                if isinstance(answer,list):
                    for ans in answer:
                        # first time we've seen this particular answer?
                        if ans not in aggregation_so_far[species]["followup"]:
                            aggregation_so_far[species]["followup"][question][ans] = 1
                        else:
                            aggregation_so_far[species]["followup"][question][ans] += 1
                else:
                    # first time we've seen this particular answer
                    if answer not in aggregation_so_far[species]["followup"]:
                        aggregation_so_far[species]["followup"][question][answer] = 1
                    else:
                        aggregation_so_far[species]["followup"][question][answer] += 1

        return aggregation_so_far

    def __aggregate__(self,raw_classifications,aggregated_results={}):

        # todo - figure out why this is happening
        if raw_classifications == {}:
            return dict()

        assert len(raw_classifications.keys()) == 1

        task_id = raw_classifications.keys()[0]

        # go through each subject independently
        for subject_id in raw_classifications[task_id]:
            # just to save on some typing
            annotation_dictionary = raw_classifications[task_id][subject_id]
            # some annotations appear to be empty - those should just be skipped
            # as a result, don't calculate "num users" just yet
            subject_results = {"num species":[]}

            for annotations_per_user in annotation_dictionary.values():
                # how many species did this user report?
                num_species = len([a for a in annotations_per_user if a != []])

                # if the user just reported nothing - skip
                if num_species == 0:
                    continue

                subject_results["num species"].append(num_species)
                # assert False

                # go through each individual species annotation from user
                for ann in annotations_per_user:
                    # not sure why we have sometimes getting empty annotations but it happens - if so, skip
                    if ann == []:
                        continue

                    subject_results = self.__species_annotation__(subject_results,ann)

            # save the results and record how many people have seen this particular subject
            # on the off chance that all of the annotations were empty, skip it
            if subject_results["num species"] != []:
                # record how many people saw this image
                subject_results["num users"] = len(annotation_dictionary)
                # what is the aggregate number of species in this image?
                subject_results["num species in image"] = self.__get_species_in_subject__(subject_results)
                subject_results["pielou index"] = self.__calc__pielou__(subject_results)

                aggregated_results[subject_id] = subject_results

        return aggregated_results
<commit_msg>fixed problem with follow up questions<commit_after>from __future__ import print_function
import math
import numpy as np

class Survey:
    def __init__(self):
        pass

    def __get_species_in_subject__(self,aggregations):
        """
        use Ali's and Margaret's code to determine how many species are a given subject
        and return those X top species
        :return:
        """
        num_species = int(np.median(aggregations["num species"]))
        assert(num_species >= 1)
        # sort the species by the number of votes
        species_by_vote = []

        for species_id in aggregations:
            if species_id not in ["num users","num species"]:
                species_by_vote.append((species_id,aggregations[species_id]["num votes"]))
        sorted_species = sorted(species_by_vote,key = lambda x:x[1],reverse=True)

        return sorted_species[:num_species]

    def __shannon_entropy__(self,probabilities):
        return -sum([p*math.log(p) for p in probabilities])

    def __calc__pielou__(self,aggregations):
        """
        return a row for survey tasks with subject id and pielou index
        :param aggregations:
        :return:
        """
        # for development - a few bad aggregations have crept into the db (for small projects) so skip them
        if max(aggregations["num species"]) == 0:
            return ""

        num_users = aggregations["num users"]
        probabilities = []
        for details in aggregations.values():
            # list => "num species", int => "num users"
            if type(details) in [list,int]:
                continue

            probabilities.append(details["num votes"]/float(num_users))

        num_species = len(probabilities)

        if num_species == 1:
            pielou_index = 0
        else:
            shannon = self.__shannon_entropy__(probabilities)
            pielou_index = shannon/math.log(num_species)

        return pielou_index

    def __species_annotation__(self,aggregation_so_far,annotation):
        """
        for a given user's annotation - go through each species that the user marked and process that annotation
        :param aggregation_so_far: aggregations so far for a single subject
        :param annotation: the next annotation for that subject
        :return:
        """
        for species_ann in annotation:
            species = species_ann["choice"]

            # is the first classification we've encountered for this species?
            if species not in aggregation_so_far:
                aggregation_so_far[species] = {"num votes": 1,"followup":{}}

            else:
                aggregation_so_far[species]["num votes"] += 1

            # go through each of the follow up questions
            for question,answer in species_ann["answers"].items():
                # is this the first time we've seen this particular followup question
                # should probably always happen the first time we see the species (since most, if not
                # all of the time, all of the follow up questions should be required)
                if question not in aggregation_so_far[species]["followup"]:
                    aggregation_so_far[species]["followup"][question] = {}

                # if list - multiple answers are allowed
                if isinstance(answer,list):
                    for ans in answer:
                        # first time we've seen this particular answer?
                        if ans not in aggregation_so_far[species]["followup"][question]:
                            aggregation_so_far[species]["followup"][question][ans] = 1
                        else:
                            aggregation_so_far[species]["followup"][question][ans] += 1
                else:
                    # first time we've seen this particular answer
                    if answer not in aggregation_so_far[species]["followup"][question]:
                        aggregation_so_far[species]["followup"][question][answer] = 1
                    else:
                        aggregation_so_far[species]["followup"][question][answer] += 1

        return aggregation_so_far

    def __aggregate__(self,raw_classifications,aggregated_results):

        # todo - figure out why this is happening
        if raw_classifications == {}:
            return dict()

        assert len(raw_classifications.keys()) == 1

        task_id = raw_classifications.keys()[0]

        # go through each subject independently
        for subject_id in raw_classifications[task_id]:
            # just to save on some typing
            annotation_dictionary = raw_classifications[task_id][subject_id]
            # some annotations appear to be empty - those should just be skipped
            # as a result, don't calculate "num users" just yet
            subject_results = {"num species":[]}

            for annotations_per_user in annotation_dictionary.values():
                # how many species did this user report?
                num_species = len([a for a in annotations_per_user if a != []])

                # if the user just reported nothing - skip
                if num_species == 0:
                    continue

                subject_results["num species"].append(num_species)
                # assert False

                # go through each individual species annotation from user
                for ann in annotations_per_user:
                    # not sure why we have sometimes getting empty annotations but it happens - if so, skip
                    if ann == []:
                        continue

                    subject_results = self.__species_annotation__(subject_results,ann)

            # save the results and record how many people have seen this particular subject
            # on the off chance that all of the annotations were empty, skip it
            if subject_results["num species"] != []:
                # record how many people saw this image
                subject_results["num users"] = len(annotation_dictionary)
                # what is the aggregate number of species in this image?
                subject_results["num species in image"] = self.__get_species_in_subject__(subject_results)
                subject_results["pielou index"] = self.__calc__pielou__(subject_results)

                aggregated_results[subject_id] = subject_results

                print(aggregated_results[subject_id])
                print(raw_classifications[task_id][subject_id])
                assert False
        return aggregated_results
<|endoftext|>


<commit_before># curved.py
#
# Copyright 2002 Wichert Akkerman <wichert@wiggy.net>

"""Twisted integration code
"""

__docformat__ = 'epytext en'

from twisted.internet import protocol
from twisted.internet import reactor
from twisted.python import log
import sys
from pyrad import dictionary
from pyrad import host
from pyrad import packet

class PacketError(Exception):
    """Exception class for bogus packets

    PacketError exceptions are only used inside the Server class to
    abort processing of a packet.
    """

class RADIUS(host.Host, protocol.DatagramProtocol):
    def __init__(self, hosts={}, dict=dictionary.Dictionary()):
        host.Host.__init__(self, dict=dict)
        self.hosts = hosts

    def processPacket(self, pkt):
        pass

    def createPacket(self, **kwargs):
        raise NotImplementedError('Attempted to use a pure base class')

    def datagramReceived(self, datagram, source):
        host, port = source
        try:
            pkt = self.CreatePacket(packet=datagram)
        except packet.PacketError as err:
            log.msg('Dropping invalid packet: ' + str(err))
            return

        if host not in self.hosts:
            log.msg('Dropping packet from unknown host ' + host)
            return

        pkt.source = (host, port)
        try:
            self.processPacket(pkt)
        except PacketError as err:
            log.msg('Dropping packet from %s: %s' % (host, str(err)))

class RADIUSAccess(RADIUS):
    def createPacket(self, **kwargs):
        self.CreateAuthPacket(**kwargs)

    def processPacket(self, pkt):
        if pkt.code != packet.AccessRequest:
            raise PacketError(
                    'non-AccessRequest packet on authentication socket')

class RADIUSAccounting(RADIUS):
    def createPacket(self, **kwargs):
        self.CreateAcctPacket(**kwargs)

    def processPacket(self, pkt):
        if pkt.code != packet.AccountingRequest:
            raise PacketError(
                    'non-AccountingRequest packet on authentication socket')

if __name__ == '__main__':
    log.startLogging(sys.stdout, 0)
    reactor.listenUDP(1812, RADIUSAccess())
    reactor.listenUDP(1813, RADIUSAccounting())
    reactor.run()
<commit_msg>remove #33<commit_after># curved.py
#
# Copyright 2002 Wichert Akkerman <wichert@wiggy.net>

"""Twisted integration code
"""

__docformat__ = 'epytext en'

from twisted.internet import protocol
from twisted.internet import reactor
from twisted.python import log
import sys
from pyrad import dictionary
from pyrad import host
from pyrad import packet

class PacketError(Exception):
    """Exception class for bogus packets

    PacketError exceptions are only used inside the Server class to
    abort processing of a packet.
    """

class RADIUS(host.Host, protocol.DatagramProtocol):
    def __init__(self, hosts={}, dict=dictionary.Dictionary()):
        host.Host.__init__(self, dict=dict)
        self.hosts = hosts

    def processPacket(self, pkt):
        pass

    def createPacket(self, **kwargs):
        raise NotImplementedError('Attempted to use a pure base class')

    def datagramReceived(self, datagram, (host, port)):
        try:
            pkt = self.CreatePacket(packet=datagram)
        except packet.PacketError as err:
            log.msg('Dropping invalid packet: ' + str(err))
            return

        if host not in self.hosts:
            log.msg('Dropping packet from unknown host ' + host)
            return

        pkt.source = (host, port)
        try:
            self.processPacket(pkt)
        except PacketError as err:
            log.msg('Dropping packet from %s: %s' % (host, str(err)))

class RADIUSAccess(RADIUS):
    def createPacket(self, **kwargs):
        self.CreateAuthPacket(**kwargs)

    def processPacket(self, pkt):
        if pkt.code != packet.AccessRequest:
            raise PacketError(
                    'non-AccessRequest packet on authentication socket')

class RADIUSAccounting(RADIUS):
    def createPacket(self, **kwargs):
        self.CreateAcctPacket(**kwargs)

    def processPacket(self, pkt):
        if pkt.code != packet.AccountingRequest:
            raise PacketError(
                    'non-AccountingRequest packet on authentication socket')

if __name__ == '__main__':
    log.startLogging(sys.stdout, 0)
    reactor.listenUDP(1812, RADIUSAccess())
    reactor.listenUDP(1813, RADIUSAccounting())
    reactor.run()
<|endoftext|>


<commit_before># config.py

import matplotlib as mp
import numpy as np
import warnings
import os
#import glob
import platform
import shutil
import subprocess
import sys
import time

pyVer = sys.version_info

# Set timing function
if pyVer < (3,3):
    if sys.platform == 'win32':
        # On Windows, the best timer is time.clock
        wallTime = time.clock
    else: #sys.platform.startswith('linux') 'cygwin' 'darwin'
        # On most other platforms the best timer is time.time
        wallTime = time.time
else:
    wallTime = time.perf_counter

#pyrhoPath = os.path.abspath(pyrho.__file__) # http://stackoverflow.com/questions/247770/retrieving-python-module-path
pyrhoPath = os.path.dirname(os.path.abspath(__file__)) # modulePath
#print(pyrhoPath)

pyrhoNEURONpath = os.path.join(pyrhoPath, 'NEURON')
NMODLfiles = ['RhO3.mod', 'RhO4.mod', 'RhO6.mod']
#HOCfiles = glob.glob("*.hoc")
HOCfiles = [h for h in os.listdir(pyrhoNEURONpath) if h.endswith('.hoc')]
#NMODLfilesIncPath = [os.path.join(pyrhoNEURONpath, f) for f in NMODLfiles]
#print(NMODLfilesIncPath)

##### Output level settings #####
global verbose
if 'verbose' not in vars() or 'verbose' not in globals() or verbose is None:
    verbose = 1 # Text notification output level [0,1,2]

def createDir(path):
    if os.path.isdir(path):
        return
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

### Set data and figure directories to defaults
if 'dDir' not in vars() or 'dDir' not in globals() or dDir is None:
    dDir = 'data/'
#if not os.path.isdir(dDir):
    createDir(dDir)

if 'fDir' not in vars() or 'fDir' not in globals() or fDir is None:
    fDir = 'figs/'
#if not os.path.isdir(fDir):
    createDir(fDir)

def setupGUI(path=None):
    if path is None: # Copy image folders to home directory
        path = os.getcwd()
    createDir('gui')
    # ...
    return

def simAvailable(sim):
    sim = sim.lower()
    if sim is 'python': #sim is 'Python' or sim is 'python':
        return True
    elif sim is 'neuron': #sim is 'NEURON' or sim is 'Neuron' or sim is 'neuron':
        return checkNEURON()
    elif sim is 'brian' or sim is 'brian2': #sim is 'Brian' or sim is 'brian' or sim is 'Brian2' or sim is 'brian2':
        return checkBrian()
    else:
        return False

def checkNEURON(test=False):
    try:
        import neuron as nrn
        found = True
        if verbose > 1:
            print('NEURON module found!')
        if test:
            print('Running suite of tests...')
            nrn.test()
    except ImportError:
        found = False
        if verbose > 1:
            print('NEURON module not found!')
    return found

def setupNEURON(path=None):
    # Check for a working NEURON installation...
    #checkNEURON()
    cwd = os.getcwd()
    if not checkNEURON():
        print('Please install NEURON and rerun!')
        return

    ### To load mod files:
    # Add os.environ['NRN_NMODL_PATH'] to environment variables. See $NEURONPATH/nrn/lib/python/neuron/__init__.py
    if path is None:
        try:
            path = os.environ['NRN_NMODL_PATH']
            print("Using path from 'NRN_NMODL_PATH' for hoc & nmodl files: ")
        except KeyError:
            path = cwd
            os.environ['NRN_NMODL_PATH'] = path
            #os.putenv(key, value) # Direct assignment is preferable
            print('Using current directory for hoc & nmodl files: ')
    else:
        print("Using suplied path for hoc & nmodl files: ")
    print(path)

    # mod files should be compiled in the working directory for NEURON to find them
    # Alternatively, change os.environ['NRN_NMODL_PATH'] ?

    #print('NEURON module path: ', path)
    print('Copying mod {} and hoc {} files from {}...'.format(NMODLfiles, HOCfiles, pyrhoNEURONpath))
    if os.path.isdir(path): # Check path
        NMODLfilesIncPath = [os.path.join(pyrhoNEURONpath, f) for f in NMODLfiles]
        for f in NMODLfilesIncPath:
            shutil.copy2(f, path)
        #for f in NMODLfiles:
        #    shutil.copy2(pyrhoNEURONpath, path, f) # >= 3.3
        # for h in os.listdir("pyrhoNEURONpath"): #HOCfiles:
            # if h.endswith(".hoc"):
                # shutil.copy2(pyrhoNEURONpath, path, h)
        HOCfilesIncPath = [os.path.join(pyrhoNEURONpath, f) for f in HOCfiles]
        for f in HOCfilesIncPath:
            shutil.copy2(f, path)
        #for f in HOCfiles:
        #    shutil.copy2(pyrhoNEURONpath, path, f)

    nrnivmodl = shutil.which('nrnivmodl')
    if nrnivmodl is None:
        arch = platform.machine()
        nrnivmodl = os.path.join(path, "nrn/{}/bin/nrnivmodl".format(arch)) # Should this be another variable?
    print('Compiling mod files with ', nrnivmodl)
    try:
        cwd = os.getcwd()
        os.chdir(path)
        retcode = subprocess.call(nrnivmodl) #nrn/x86_64/bin/nrnivmodl
        os.chdir(cwd)
        # Use subprocess.run() for Python >= 3.5
        if retcode < 0:
            print('NMODL compilation was terminated by signal', -retcode, file=sys.stderr)
        else:
            print("NMODL compilation returned", retcode, file=sys.stderr)
    except OSError as e:
        print('NMODL Compilation fialed: ', e, file=sys.stderr)
    return

def checkBrian(test=False):
    try:
        import brian2 as br
        found = True
        if verbose > 1:
            print('Brian module found!')
        if test:
            print('Running suite of tests...')
            br.prefs.codegen.target = 'cython' #'numpy #'auto' #'weave' - Python 2 only
            out = br.codegen.cpp_prefs.get_compiler_and_args()
            print(out)
            br.test()
    except ImportError:
        found = False
        if verbose > 1:
            print('Brian module not found!')

    return found

def setupBrian():
    return

##### Plot settings #####

figDisplay = 'screen' #'paper'

colours = ['b','g','r','c','m','y','k']
styles = ['-', '--', '-.', ':']

latexInstalled = False # Change this!
# http://nipunbatra.github.io/2014/08/latexify/
# http://www.tex.ac.uk/ctan/macros/latex/contrib/pythontex/pythontex_install.py
if latexInstalled:
    mp.rcParams['text.usetex'] = True
#mp.rcParams.update(rcdef) # Set back to defaults

xLabelPos = 0.98

def setFigOutput(figDisplay='screen', width=None):
    golden_ratio  = (1.0 + np.sqrt(5)) / 2.0
    global saveFigFormat
    global addTitles
    global addStimulus
    global eqSize

    # http://matplotlib.org/users/customizing.html
    if figDisplay == 'screen':
        dpi = 300 #mp.rcParams['savefig.dpi'] #300
        #figFormat = 'retina'
        saveFigFormat = 'png'
        if width is None:
            width = 12
        figWidth = width
        figHeight = figWidth / golden_ratio
        tickSize = 14
        labelSize = 16
        legendSize = 12
        titleSize = 'small' #'x-large'
        eqSize = 18
        linewidth = 1.5
        addTitles = True
        addStimulus = True

    elif figDisplay == 'paper':
        #http://www.nature.com/nature/authors/gta/3c_Final_artwork.pdf
        #http://www.plosone.org/static/figureSpecifications
        #http://www.ploscompbiol.org/static/figureSpecifications
        #http://www.frontiersin.org/about/AuthorGuidelines#_FigureandTableGuidelines
        dpi = 300 #1200 # Set DPI 300 - 600
        saveFigFormat = 'png'#'eps'             # Supported formats: eps, pdf, pgf, png, ps, raw, rgba, svg, svgz.
        #figFormat = 'eps'
        if width is None:
            width = 18/2.54 #5#7
        figWidth = width
        figHeight = figWidth / golden_ratio
        tickSize = 8
        labelSize = 10
        legendSize = 10
        titleSize = 'medium'
        eqSize = 10
        linewidth = 2
        addTitles = False
        addStimulus = True

        mp.rcParams['savefig.dpi'] = dpi        # http://nbviewer.ipython.org/gist/minrk/3301035
                                                # http://wiki.scipy.org/Cookbook/Matplotlib/AdjustingImageSize

    else:
        warnings.warn('Warning: Unknown display type selected - using matplotlib defaults!')

    # http://matplotlib.org/users/customizing.html
    mp.rcParams['figure.figsize'] = (figWidth, figHeight) #pylab.rcParams
    mp.rcParams['figure.dpi'] = dpi
    #mp.rcParams['savefig.dpi'] = dpi           # http://nbviewer.ipython.org/gist/minrk/3301035
    mp.rcParams['axes.labelsize'] = labelSize
    mp.rcParams['ytick.labelsize'] = tickSize
    mp.rcParams['xtick.labelsize'] = tickSize
    mp.rcParams['legend.fontsize'] = legendSize
    mp.rcParams['axes.titlesize'] = titleSize
    mp.rcParams['lines.linewidth'] = linewidth
    mp.rcParams['font.size'] = eqSize
    #mp.rcParams['figure.autolayout'] = True

    #return saveFigFormat, addTitles, addStimulus, eqSize

# plotPeakRecovery = False
# plotStateVars = False
# plotKinetics = False

#saveFigFormat, addTitles, addStimulus, eqSize = setFigOutput(display)
setFigOutput(figDisplay)

try:
    __IPYTHON__
    import IPython
    #from IPython import display
except NameError:
    pass
else: # and IPython. See also get_ipython()
    figFormat = 'retina' # 'svg'
    IPython.display.set_matplotlib_formats(figFormat)
    #display.set_matplotlib_formats(figFormat)
    if verbose > 1:
        print("Default display figure format set: "+figFormat)

#IPython.core.magic.Magics.basic.pprint()
#%pprint
#%precision %.6g
#np.set_printoptions(precision=6) # Number of decimal places for floats
# Long    Scientific
# 1e-4    1e-5
# 1e15    1e16

<commit_msg>Made path to nrnivmodl cross-platform and made its return code print to stdout rather than stderr<commit_after># config.py

import matplotlib as mp
import numpy as np
import warnings
import os
#import glob
import platform
import shutil
import subprocess
import sys
import time

pyVer = sys.version_info

# Set timing function
if pyVer < (3,3):
    if sys.platform == 'win32':
        # On Windows, the best timer is time.clock
        wallTime = time.clock
    else: #sys.platform.startswith('linux') 'cygwin' 'darwin'
        # On most other platforms the best timer is time.time
        wallTime = time.time
else:
    wallTime = time.perf_counter

#pyrhoPath = os.path.abspath(pyrho.__file__) # http://stackoverflow.com/questions/247770/retrieving-python-module-path
pyrhoPath = os.path.dirname(os.path.abspath(__file__)) # modulePath
#print(pyrhoPath)

pyrhoNEURONpath = os.path.join(pyrhoPath, 'NEURON')
NMODLfiles = ['RhO3.mod', 'RhO4.mod', 'RhO6.mod']
#HOCfiles = glob.glob("*.hoc")
HOCfiles = [h for h in os.listdir(pyrhoNEURONpath) if h.endswith('.hoc')]
#NMODLfilesIncPath = [os.path.join(pyrhoNEURONpath, f) for f in NMODLfiles]
#print(NMODLfilesIncPath)

##### Output level settings #####
global verbose
if 'verbose' not in vars() or 'verbose' not in globals() or verbose is None:
    verbose = 1 # Text notification output level [0,1,2]

def createDir(path):
    if os.path.isdir(path):
        return
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

### Set data and figure directories to defaults
if 'dDir' not in vars() or 'dDir' not in globals() or dDir is None:
    dDir = 'data/'
#if not os.path.isdir(dDir):
    createDir(dDir)

if 'fDir' not in vars() or 'fDir' not in globals() or fDir is None:
    fDir = 'figs/'
#if not os.path.isdir(fDir):
    createDir(fDir)

def setupGUI(path=None):
    if path is None: # Copy image folders to home directory
        path = os.getcwd()
    createDir('gui')
    # ...
    return

def simAvailable(sim):
    sim = sim.lower()
    if sim is 'python': #sim is 'Python' or sim is 'python':
        return True
    elif sim is 'neuron': #sim is 'NEURON' or sim is 'Neuron' or sim is 'neuron':
        return checkNEURON()
    elif sim is 'brian' or sim is 'brian2': #sim is 'Brian' or sim is 'brian' or sim is 'Brian2' or sim is 'brian2':
        return checkBrian()
    else:
        return False

def checkNEURON(test=False):
    try:
        import neuron as nrn
        found = True
        if verbose > 1:
            print('NEURON module found!')
        if test:
            print('Running suite of tests...')
            nrn.test()
    except ImportError:
        found = False
        if verbose > 1:
            print('NEURON module not found!')
    return found

def setupNEURON(path=None):
    # Check for a working NEURON installation...
    #checkNEURON()
    cwd = os.getcwd()
    if not checkNEURON():
        print('Please install NEURON and rerun!')
        return

    ### To load mod files:
    # Add os.environ['NRN_NMODL_PATH'] to environment variables. See $NEURONPATH/nrn/lib/python/neuron/__init__.py
    if path is None:
        try:
            path = os.environ['NRN_NMODL_PATH']
            print("Using path from 'NRN_NMODL_PATH' for hoc & nmodl files: ")
        except KeyError:
            path = cwd
            os.environ['NRN_NMODL_PATH'] = path
            #os.putenv(key, value) # Direct assignment is preferable
            print('Using current directory for hoc & nmodl files: ')
    else:
        print("Using suplied path for hoc & nmodl files: ")
    print(path)

    # mod files should be compiled in the working directory for NEURON to find them
    # Alternatively, change os.environ['NRN_NMODL_PATH'] ?

    #print('NEURON module path: ', path)
    print('Copying mod {} and hoc {} files from {}...'.format(NMODLfiles, HOCfiles, pyrhoNEURONpath))
    if os.path.isdir(path): # Check path
        NMODLfilesIncPath = [os.path.join(pyrhoNEURONpath, f) for f in NMODLfiles]
        for f in NMODLfilesIncPath:
            shutil.copy2(f, path)
        #for f in NMODLfiles:
        #    shutil.copy2(pyrhoNEURONpath, path, f) # >= 3.3
        # for h in os.listdir("pyrhoNEURONpath"): #HOCfiles:
            # if h.endswith(".hoc"):
                # shutil.copy2(pyrhoNEURONpath, path, h)
        HOCfilesIncPath = [os.path.join(pyrhoNEURONpath, f) for f in HOCfiles]
        for f in HOCfilesIncPath:
            shutil.copy2(f, path)
        #for f in HOCfiles:
        #    shutil.copy2(pyrhoNEURONpath, path, f)

    nrnivmodl = shutil.which('nrnivmodl')
    if nrnivmodl is None:
        arch = platform.machine()
        nrnivmodl = os.path.join(path, "nrn", arch, "bin", "nrnivmodl") # Should this be another variable?
    print('Compiling mod files with ', nrnivmodl)
    try:
        cwd = os.getcwd()
        os.chdir(path)
        retcode = subprocess.call(nrnivmodl) #nrn/x86_64/bin/nrnivmodl
        os.chdir(cwd)
        # Use subprocess.run() for Python >= 3.5
        if retcode < 0:
            print('NMODL compilation was terminated by signal', -retcode, file=sys.stderr)
        else:
            print("NMODL compilation returned", retcode) #, file=sys.stderr)
    except OSError as e:
        print('NMODL Compilation failed: ', e, file=sys.stderr)
    return

def checkBrian(test=False):
    try:
        import brian2 as br
        found = True
        if verbose > 1:
            print('Brian module found!')
        if test:
            print('Running suite of tests...')
            br.prefs.codegen.target = 'cython' #'numpy #'auto' #'weave' - Python 2 only
            out = br.codegen.cpp_prefs.get_compiler_and_args()
            print(out)
            br.test()
    except ImportError:
        found = False
        if verbose > 1:
            print('Brian module not found!')

    return found

def setupBrian():
    return

##### Plot settings #####

figDisplay = 'screen' #'paper'

colours = ['b','g','r','c','m','y','k']
styles = ['-', '--', '-.', ':']

latexInstalled = False # Change this!
# http://nipunbatra.github.io/2014/08/latexify/
# http://www.tex.ac.uk/ctan/macros/latex/contrib/pythontex/pythontex_install.py
if latexInstalled:
    mp.rcParams['text.usetex'] = True
#mp.rcParams.update(rcdef) # Set back to defaults

xLabelPos = 0.98

def setFigOutput(figDisplay='screen', width=None):
    golden_ratio  = (1.0 + np.sqrt(5)) / 2.0
    global saveFigFormat
    global addTitles
    global addStimulus
    global eqSize

    # http://matplotlib.org/users/customizing.html
    if figDisplay == 'screen':
        dpi = 300 #mp.rcParams['savefig.dpi'] #300
        #figFormat = 'retina'
        saveFigFormat = 'png'
        if width is None:
            width = 12
        figWidth = width
        figHeight = figWidth / golden_ratio
        tickSize = 14
        labelSize = 16
        legendSize = 12
        titleSize = 'small' #'x-large'
        eqSize = 18
        linewidth = 1.5
        addTitles = True
        addStimulus = True

    elif figDisplay == 'paper':
        #http://www.nature.com/nature/authors/gta/3c_Final_artwork.pdf
        #http://www.plosone.org/static/figureSpecifications
        #http://www.ploscompbiol.org/static/figureSpecifications
        #http://www.frontiersin.org/about/AuthorGuidelines#_FigureandTableGuidelines
        dpi = 300 #1200 # Set DPI 300 - 600
        saveFigFormat = 'png'#'eps'             # Supported formats: eps, pdf, pgf, png, ps, raw, rgba, svg, svgz.
        #figFormat = 'eps'
        if width is None:
            width = 18/2.54 #5#7
        figWidth = width
        figHeight = figWidth / golden_ratio
        tickSize = 8
        labelSize = 10
        legendSize = 10
        titleSize = 'medium'
        eqSize = 10
        linewidth = 2
        addTitles = False
        addStimulus = True

        mp.rcParams['savefig.dpi'] = dpi        # http://nbviewer.ipython.org/gist/minrk/3301035
                                                # http://wiki.scipy.org/Cookbook/Matplotlib/AdjustingImageSize

    else:
        warnings.warn('Warning: Unknown display type selected - using matplotlib defaults!')

    # http://matplotlib.org/users/customizing.html
    mp.rcParams['figure.figsize'] = (figWidth, figHeight) #pylab.rcParams
    mp.rcParams['figure.dpi'] = dpi
    #mp.rcParams['savefig.dpi'] = dpi           # http://nbviewer.ipython.org/gist/minrk/3301035
    mp.rcParams['axes.labelsize'] = labelSize
    mp.rcParams['ytick.labelsize'] = tickSize
    mp.rcParams['xtick.labelsize'] = tickSize
    mp.rcParams['legend.fontsize'] = legendSize
    mp.rcParams['axes.titlesize'] = titleSize
    mp.rcParams['lines.linewidth'] = linewidth
    mp.rcParams['font.size'] = eqSize
    #mp.rcParams['figure.autolayout'] = True

    #return saveFigFormat, addTitles, addStimulus, eqSize

# plotPeakRecovery = False
# plotStateVars = False
# plotKinetics = False

#saveFigFormat, addTitles, addStimulus, eqSize = setFigOutput(display)
setFigOutput(figDisplay)

try:
    __IPYTHON__
    import IPython
    #from IPython import display
except NameError:
    pass
else: # and IPython. See also get_ipython()
    figFormat = 'retina' # 'svg'
    IPython.display.set_matplotlib_formats(figFormat)
    #display.set_matplotlib_formats(figFormat)
    if verbose > 1:
        print("Default display figure format set: "+figFormat)

#IPython.core.magic.Magics.basic.pprint()
#%pprint
#%precision %.6g
#np.set_printoptions(precision=6) # Number of decimal places for floats
# Long    Scientific
# 1e-4    1e-5
# 1e15    1e16

<|endoftext|>


<commit_before># config.py

import matplotlib as mp
import numpy as np
import warnings
import os
#import glob
import platform
import shutil
import subprocess
import sys
import time

pyVer = sys.version_info

# Set timing function
if pyVer < (3,3):
    if sys.platform == 'win32':
        # On Windows, the best timer is time.clock
        wallTime = time.clock
    else: #sys.platform.startswith('linux') 'cygwin' 'darwin'
        # On most other platforms the best timer is time.time
        wallTime = time.time
else:
    wallTime = time.perf_counter

home = os.path.expanduser('~')
pyrhoPath = os.path.dirname(os.path.abspath(__file__)) # modulePath

pyrhoNEURONpath = os.path.join(pyrhoPath, 'NEURON')
NMODLfiles = ['RhO3c.mod', 'RhO4c.mod', 'RhO6c.mod'] #['RhO3.mod', 'RhO4.mod', 'RhO6.mod']
#HOCfiles = glob.glob("*.hoc")
HOCfiles = [h for h in os.listdir(pyrhoNEURONpath) if h.endswith('.hoc')]
#NMODLfilesIncPath = [os.path.join(pyrhoNEURONpath, f) for f in NMODLfiles]
NEURONinstallScript = 'install_neuron.sh'

##### Output level settings #####
global verbose
if 'verbose' not in vars() or 'verbose' not in globals() or verbose is None:
    verbose = 1 # Text notification output level [0,1,2]

def createDir(path):
    if os.path.isdir(path):
        return
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

### Set data and figure directories to defaults
if 'dDir' not in vars() or 'dDir' not in globals() or dDir is None:
    dDir = 'data/'
    createDir(dDir)

if 'fDir' not in vars() or 'fDir' not in globals() or fDir is None:
    fDir = 'figs/'
    createDir(fDir)

GUIdir = 'gui'
def setupGUI(path=None):
    if path is None: # Copy image folders to home directory
        path = os.path.join(os.getcwd(), GUIdir)

    createDir(path)
    pyrhoGUIpath = os.path.join(pyrhoPath, GUIdir)
    pngFiles = [f for f in os.listdir(pyrhoGUIpath) if f.endswith('.png')]
    for f in pngFiles:
        shutil.copy2(os.path.join(pyrhoGUIpath, f), path)

    return

def simAvailable(sim):
    sim = sim.lower()
    if sim is 'python':
        return True
    elif sim is 'neuron':
        return checkNEURON()
    elif sim is 'brian' or sim is 'brian2':
        return checkBrian()
    else:
        return False

def checkNEURON(test=False):
    try:
        import neuron as nrn
        found = True
        if verbose > 1:
            print('NEURON module found!')
        if test:
            print('Running suite of tests...')
            nrn.test()
    except ImportError:
        found = False
        if verbose > 1:
            print('NEURON module not found!')
    return found

def setupNEURON(path=None, NEURONpath=None):
    """Setup the NEURON simulator to work with PyRhO
        path        := Path to working directory containing hoc and mod files (default=pwd)
        NEURONpath  := Path to NEURON directory containing nrn (and iv)"""

    cwd = os.getcwd()
    if not checkNEURON():   # Check for a working NEURON installation...
        if sys.platform == 'win32':
            print('Please install NEURON and rerun!')
            return
        else:
            try:
                if NEURONpath is None:
                    print("Please specify an installation path for NEURON with 'NEURONpath' and run again")
                    return
                #NEURONscriptPath = os.path.join(home, 'NEURON')
                #NEURONscriptPath = pyrhoNEURONpath
                NEURONscriptIncPath = os.path.join(pyrhoNEURONpath, NEURONinstallScript)
                exitcode = subprocess.call([NEURONscriptIncPath, NEURONpath]) # shell=True # .check_call
            except:
                shutil.copy2(os.path.join(NEURONscriptPath, NEURONinstallScript), cwd)
                print('Unable to install NEURON - please install manually with the script copied to {}.'.format(cwd))

    ### To load mod files:
    # Add os.environ['NRN_NMODL_PATH'] to environment variables. See $NEURONPATH/nrn/lib/python/neuron/__init__.py
    if path is None:
        try:
            path = os.environ['NRN_NMODL_PATH']
            print("Using path from 'NRN_NMODL_PATH' for hoc & nmodl files: ", end='')
        except KeyError:
            path = cwd
            os.environ['NRN_NMODL_PATH'] = path
            #os.putenv(key, value) # Direct assignment is preferable
            print('Using current directory for hoc & nmodl files: ', end='')
    else:
        print("Using suplied path for hoc & nmodl files: ", end='')
    print(path)

    # mod files should be compiled in the working directory for NEURON to find them
    # Alternatively, change os.environ['NRN_NMODL_PATH'] ?

    #print('NEURON module path: ', path)
    print('Copying mod {} and hoc {} files from {}...'.format(NMODLfiles, HOCfiles, pyrhoNEURONpath))
    if os.path.isdir(path): # Check path
        NMODLfilesIncPath = [os.path.join(pyrhoNEURONpath, f) for f in NMODLfiles]
        for f in NMODLfilesIncPath:
            shutil.copy2(f, path)
        #for f in NMODLfiles:
        #    shutil.copy2(pyrhoNEURONpath, path, f) # >= 3.3
        # for h in os.listdir("pyrhoNEURONpath"): #HOCfiles:
            # if h.endswith(".hoc"):
                # shutil.copy2(pyrhoNEURONpath, path, h)
        HOCfilesIncPath = [os.path.join(pyrhoNEURONpath, f) for f in HOCfiles]
        for f in HOCfilesIncPath:
            shutil.copy2(f, path)
        #for f in HOCfiles:
        #    shutil.copy2(pyrhoNEURONpath, path, f)

    nrnivmodl = shutil.which('nrnivmodl')
    if nrnivmodl is None:
        arch = platform.machine()
        if NEURONpath is not None:
            nrnivmodl = os.path.join(NEURONpath, "nrn", arch, "bin", "nrnivmodl")
        else:
            nrnivmodl = os.path.join(path, "nrn", arch, "bin", "nrnivmodl") # Try the hoc/mod path

    print('Compiling mod files with ', nrnivmodl)
    try:
        cwd = os.getcwd()
        os.chdir(path)
        retcode = subprocess.call(nrnivmodl) #nrn/x86_64/bin/nrnivmodl
        os.chdir(cwd)
        # Use subprocess.run() for Python >= 3.5
        if retcode < 0:
            print('NMODL compilation was terminated by signal', -retcode, file=sys.stderr)
        else:
            print("NMODL compilation returned", retcode) #, file=sys.stderr)
    except OSError as e:
        print('NMODL Compilation failed: ', e, file=sys.stderr)
        print('Try setting the NEURON directory as an environment variable or passing it as the NEURONpath argument.')
    return

def checkBrian(test=False):
    try:
        import brian2 as br
        found = True
        if verbose > 1:
            print('Brian module found!')
        if test:
            print('Running suite of tests...')
            br.prefs.codegen.target = 'cython' #'numpy #'auto' #'weave' - Python 2 only
            out = br.codegen.cpp_prefs.get_compiler_and_args()
            print(out)
            br.test()
    except ImportError:
        found = False
        if verbose > 1:
            print('Brian module not found!')

    return found

def setupBrian():
    if not checkBrian():
        import pip
        pip.main(['install', 'brian2']) # pip install brian2
    return

##### Plot settings #####

figDisplay = 'screen' #'paper'

colours = ['b','g','r','c','m','y','k']
styles = ['-', '--', '-.', ':']

latexInstalled = False # Change this!
# http://nipunbatra.github.io/2014/08/latexify/
# http://www.tex.ac.uk/ctan/macros/latex/contrib/pythontex/pythontex_install.py
if latexInstalled:
    mp.rcParams['text.usetex'] = True
#mp.rcParams.update(rcdef) # Set back to defaults

xLabelPos = 0.98

def setFigOutput(figDisplay='screen', width=None):
    golden_ratio  = (1.0 + np.sqrt(5)) / 2.0
    global saveFigFormat
    global addTitles
    global addStimulus
    global eqSize

    # http://matplotlib.org/users/customizing.html
    if figDisplay == 'screen':
        dpi = 300 #mp.rcParams['savefig.dpi'] #300
        #figFormat = 'retina'
        saveFigFormat = 'png'
        if width is None:
            width = 12
        figWidth = width
        figHeight = figWidth / golden_ratio
        tickSize = 14
        labelSize = 16
        legendSize = 12
        titleSize = 'small' #'x-large'
        eqSize = 18
        linewidth = 1.5
        addTitles = True
        addStimulus = True

    elif figDisplay == 'paper':
        #http://www.nature.com/nature/authors/gta/3c_Final_artwork.pdf
        #http://www.plosone.org/static/figureSpecifications
        #http://www.ploscompbiol.org/static/figureSpecifications
        #http://www.frontiersin.org/about/AuthorGuidelines#_FigureandTableGuidelines
        dpi = 300 #1200 # Set DPI 300 - 600
        saveFigFormat = 'png'#'eps'             # Supported formats: eps, pdf, pgf, png, ps, raw, rgba, svg, svgz.
        #figFormat = 'eps'
        if width is None:
            width = 18/2.54 #5#7
        figWidth = width
        figHeight = figWidth / golden_ratio
        tickSize = 8
        labelSize = 10
        legendSize = 10
        titleSize = 'medium'
        eqSize = 10
        linewidth = 2
        addTitles = False
        addStimulus = True

        mp.rcParams['savefig.dpi'] = dpi        # http://nbviewer.ipython.org/gist/minrk/3301035
                                                # http://wiki.scipy.org/Cookbook/Matplotlib/AdjustingImageSize

    else:
        warnings.warn('Warning: Unknown display type selected - using matplotlib defaults!')

    # http://matplotlib.org/users/customizing.html
    mp.rcParams['figure.figsize'] = (figWidth, figHeight) #pylab.rcParams
    mp.rcParams['figure.dpi'] = dpi
    #mp.rcParams['savefig.dpi'] = dpi           # http://nbviewer.ipython.org/gist/minrk/3301035
    mp.rcParams['axes.labelsize'] = labelSize
    mp.rcParams['ytick.labelsize'] = tickSize
    mp.rcParams['xtick.labelsize'] = tickSize
    mp.rcParams['legend.fontsize'] = legendSize
    mp.rcParams['axes.titlesize'] = titleSize
    mp.rcParams['lines.linewidth'] = linewidth
    mp.rcParams['font.size'] = eqSize
    #mp.rcParams['figure.autolayout'] = True

    #return saveFigFormat, addTitles, addStimulus, eqSize

setFigOutput(figDisplay)

fancyPlots = False #True

def setFigStyle(fancyPlots=False): # Merge this with setFigOutput
    if fancyPlots:
        try:
            import seaborn
        except ImportError:
            warnings.warn('Seaborn not found - using default plotting scheme.')
        else: # Try another package?
            pass
        finally: # Always do this last
            pass
    else:
        if 'seaborn' in sys.modules:
            seaborn.reset_orig()
        #setFigOutput(figDisplay)

def resetPlot():
    if 'seaborn' in sys.modules:
        seaborn.reset_orig()
    setFigOutput(figDisplay)

try:
    __IPYTHON__
    import IPython
    #from IPython import display
except NameError:
    pass
else: # and IPython. See also get_ipython()
    figFormat = 'retina' # 'svg'
    IPython.display.set_matplotlib_formats(figFormat)
    #display.set_matplotlib_formats(figFormat)
    if verbose > 1:
        print("Default display figure format set: "+figFormat)

#IPython.core.magic.Magics.basic.pprint()
#%pprint
#%precision %.6g
#np.set_printoptions(precision=6) # Number of decimal places for floats
# Long    Scientific
# 1e-4    1e-5
# 1e15    1e16

<commit_msg>Updated warning for NEURON compilation under Windows<commit_after># config.py

import matplotlib as mp
import numpy as np
import warnings
import os
#import glob
import platform
import shutil
import subprocess
import sys
import time

pyVer = sys.version_info

# Set timing function
if pyVer < (3,3):
    if sys.platform == 'win32':
        # On Windows, the best timer is time.clock
        wallTime = time.clock
    else: #sys.platform.startswith('linux') 'cygwin' 'darwin'
        # On most other platforms the best timer is time.time
        wallTime = time.time
else:
    wallTime = time.perf_counter

home = os.path.expanduser('~')
pyrhoPath = os.path.dirname(os.path.abspath(__file__)) # modulePath

pyrhoNEURONpath = os.path.join(pyrhoPath, 'NEURON')
NMODLfiles = ['RhO3c.mod', 'RhO4c.mod', 'RhO6c.mod'] #['RhO3.mod', 'RhO4.mod', 'RhO6.mod']
#HOCfiles = glob.glob("*.hoc")
HOCfiles = [h for h in os.listdir(pyrhoNEURONpath) if h.endswith('.hoc')]
#NMODLfilesIncPath = [os.path.join(pyrhoNEURONpath, f) for f in NMODLfiles]
NEURONinstallScript = 'install_neuron.sh'

##### Output level settings #####
global verbose
if 'verbose' not in vars() or 'verbose' not in globals() or verbose is None:
    verbose = 1 # Text notification output level [0,1,2]

def createDir(path):
    if os.path.isdir(path):
        return
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

### Set data and figure directories to defaults
if 'dDir' not in vars() or 'dDir' not in globals() or dDir is None:
    dDir = 'data/'
    createDir(dDir)

if 'fDir' not in vars() or 'fDir' not in globals() or fDir is None:
    fDir = 'figs/'
    createDir(fDir)

GUIdir = 'gui'
def setupGUI(path=None):
    if path is None: # Copy image folders to home directory
        path = os.path.join(os.getcwd(), GUIdir)

    createDir(path)
    pyrhoGUIpath = os.path.join(pyrhoPath, GUIdir)
    pngFiles = [f for f in os.listdir(pyrhoGUIpath) if f.endswith('.png')]
    for f in pngFiles:
        shutil.copy2(os.path.join(pyrhoGUIpath, f), path)

    return

def simAvailable(sim):
    sim = sim.lower()
    if sim is 'python':
        return True
    elif sim is 'neuron':
        return checkNEURON()
    elif sim is 'brian' or sim is 'brian2':
        return checkBrian()
    else:
        return False

def checkNEURON(test=False):
    try:
        import neuron as nrn
        found = True
        if verbose > 1:
            print('NEURON module found!')
        if test:
            print('Running suite of tests...')
            nrn.test()
    except ImportError:
        found = False
        if verbose > 1:
            print('NEURON module not found!')
    return found

def setupNEURON(path=None, NEURONpath=None):
    """Setup the NEURON simulator to work with PyRhO
        path        := Path to PyRhO's working directory containing hoc and mod files (default=pwd)
        NEURONpath  := Path to NEURON installation directory containing nrn (and iv)"""

    cwd = os.getcwd()
    if not checkNEURON():   # Check for a working NEURON installation...
        if sys.platform == 'win32':
            # TODO: Create cmd script for compiling on Windows
            warnings.warn('Compilation on Windows is not yet supported - please install NEURON manually and rerun!')
            return
        else:
            try:
                if NEURONpath is None:
                    print("Please specify an installation path for NEURON with 'NEURONpath' and run again")
                    return
                #NEURONscriptPath = os.path.join(home, 'NEURON')
                #NEURONscriptPath = pyrhoNEURONpath
                NEURONscriptIncPath = os.path.join(pyrhoNEURONpath, NEURONinstallScript)
                exitcode = subprocess.call([NEURONscriptIncPath, NEURONpath]) # shell=True # .check_call
            except:
                shutil.copy2(os.path.join(NEURONscriptPath, NEURONinstallScript), cwd)
                print('Unable to install NEURON - please install manually with the script copied to {}.'.format(cwd))

    ### To load mod files:
    # Add os.environ['NRN_NMODL_PATH'] to environment variables. See $NEURONPATH/nrn/lib/python/neuron/__init__.py
    if path is None:
        try:
            path = os.environ['NRN_NMODL_PATH']
            print("Using path from 'NRN_NMODL_PATH' for hoc & nmodl files: ", end='')
        except KeyError:
            path = cwd
            os.environ['NRN_NMODL_PATH'] = path
            #os.putenv(key, value) # Direct assignment is preferable
            print('Using current directory for hoc & nmodl files: ', end='')
    else:
        print("Using suplied path for hoc & nmodl files: ", end='')
    print(path)

    # mod files should be compiled in the working directory for NEURON to find them
    # Alternatively, change os.environ['NRN_NMODL_PATH'] ?

    #print('NEURON module path: ', path)
    print('Copying mod {} and hoc {} files from {}...'.format(NMODLfiles, HOCfiles, pyrhoNEURONpath))
    if os.path.isdir(path): # Check path
        NMODLfilesIncPath = [os.path.join(pyrhoNEURONpath, f) for f in NMODLfiles]
        for f in NMODLfilesIncPath:
            shutil.copy2(f, path)
        #for f in NMODLfiles:
        #    shutil.copy2(pyrhoNEURONpath, path, f) # >= 3.3
        # for h in os.listdir("pyrhoNEURONpath"): #HOCfiles:
            # if h.endswith(".hoc"):
                # shutil.copy2(pyrhoNEURONpath, path, h)
        HOCfilesIncPath = [os.path.join(pyrhoNEURONpath, f) for f in HOCfiles]
        for f in HOCfilesIncPath:
            shutil.copy2(f, path)
        #for f in HOCfiles:
        #    shutil.copy2(pyrhoNEURONpath, path, f)

    nrnivmodl = shutil.which('nrnivmodl')
    if nrnivmodl is None:
        arch = platform.machine()
        if NEURONpath is not None:
            nrnivmodl = os.path.join(NEURONpath, "nrn", arch, "bin", "nrnivmodl")
        else:
            nrnivmodl = os.path.join(path, "nrn", arch, "bin", "nrnivmodl") # Try the hoc/mod path

    print('Compiling mod files with ', nrnivmodl)
    try:
        cwd = os.getcwd()
        os.chdir(path)
        retcode = subprocess.call(nrnivmodl) #nrn/x86_64/bin/nrnivmodl
        os.chdir(cwd)
        # Use subprocess.run() for Python >= 3.5
        if retcode < 0:
            print('NMODL compilation was terminated by signal', -retcode, file=sys.stderr)
        else:
            print("NMODL compilation returned", retcode) #, file=sys.stderr)
    except OSError as e:
        print('NMODL Compilation failed: ', e, file=sys.stderr)
        print('Try setting the NEURON directory as an environment variable or passing it as the NEURONpath argument.')
    return

def checkBrian(test=False):
    try:
        import brian2 as br
        found = True
        if verbose > 1:
            print('Brian module found!')
        if test:
            print('Running suite of tests...')
            br.prefs.codegen.target = 'cython' #'numpy #'auto' #'weave' - Python 2 only
            out = br.codegen.cpp_prefs.get_compiler_and_args()
            print(out)
            br.test()
    except ImportError:
        found = False
        if verbose > 1:
            print('Brian module not found!')

    return found

def setupBrian():
    if not checkBrian():
        import pip
        pip.main(['install', 'brian2']) # pip install brian2
    return

##### Plot settings #####

figDisplay = 'screen' #'paper'

colours = ['b','g','r','c','m','y','k']
styles = ['-', '--', '-.', ':']

latexInstalled = False # Change this!
# http://nipunbatra.github.io/2014/08/latexify/
# http://www.tex.ac.uk/ctan/macros/latex/contrib/pythontex/pythontex_install.py
if latexInstalled:
    mp.rcParams['text.usetex'] = True
#mp.rcParams.update(rcdef) # Set back to defaults

xLabelPos = 0.98

def setFigOutput(figDisplay='screen', width=None):
    golden_ratio  = (1.0 + np.sqrt(5)) / 2.0
    global saveFigFormat
    global addTitles
    global addStimulus
    global eqSize

    # http://matplotlib.org/users/customizing.html
    if figDisplay == 'screen':
        dpi = 300 #mp.rcParams['savefig.dpi'] #300
        #figFormat = 'retina'
        saveFigFormat = 'png'
        if width is None:
            width = 12
        figWidth = width
        figHeight = figWidth / golden_ratio
        tickSize = 14
        labelSize = 16
        legendSize = 12
        titleSize = 'small' #'x-large'
        eqSize = 18
        linewidth = 1.5
        addTitles = True
        addStimulus = True

    elif figDisplay == 'paper':
        #http://www.nature.com/nature/authors/gta/3c_Final_artwork.pdf
        #http://www.plosone.org/static/figureSpecifications
        #http://www.ploscompbiol.org/static/figureSpecifications
        #http://www.frontiersin.org/about/AuthorGuidelines#_FigureandTableGuidelines
        dpi = 300 #1200 # Set DPI 300 - 600
        saveFigFormat = 'png'#'eps'             # Supported formats: eps, pdf, pgf, png, ps, raw, rgba, svg, svgz.
        #figFormat = 'eps'
        if width is None:
            width = 18/2.54 #5#7
        figWidth = width
        figHeight = figWidth / golden_ratio
        tickSize = 8
        labelSize = 10
        legendSize = 10
        titleSize = 'medium'
        eqSize = 10
        linewidth = 2
        addTitles = False
        addStimulus = True

        mp.rcParams['savefig.dpi'] = dpi        # http://nbviewer.ipython.org/gist/minrk/3301035
                                                # http://wiki.scipy.org/Cookbook/Matplotlib/AdjustingImageSize

    else:
        warnings.warn('Warning: Unknown display type selected - using matplotlib defaults!')

    # http://matplotlib.org/users/customizing.html
    mp.rcParams['figure.figsize'] = (figWidth, figHeight) #pylab.rcParams
    mp.rcParams['figure.dpi'] = dpi
    #mp.rcParams['savefig.dpi'] = dpi           # http://nbviewer.ipython.org/gist/minrk/3301035
    mp.rcParams['axes.labelsize'] = labelSize
    mp.rcParams['ytick.labelsize'] = tickSize
    mp.rcParams['xtick.labelsize'] = tickSize
    mp.rcParams['legend.fontsize'] = legendSize
    mp.rcParams['axes.titlesize'] = titleSize
    mp.rcParams['lines.linewidth'] = linewidth
    mp.rcParams['font.size'] = eqSize
    #mp.rcParams['figure.autolayout'] = True

    #return saveFigFormat, addTitles, addStimulus, eqSize

setFigOutput(figDisplay)

fancyPlots = False #True

def setFigStyle(fancyPlots=False): # Merge this with setFigOutput
    if fancyPlots:
        try:
            import seaborn
        except ImportError:
            warnings.warn('Seaborn not found - using default plotting scheme.')
        else: # Try another package?
            pass
        finally: # Always do this last
            pass
    else:
        if 'seaborn' in sys.modules:
            seaborn.reset_orig()
        #setFigOutput(figDisplay)

def resetPlot():
    if 'seaborn' in sys.modules:
        seaborn.reset_orig()
    setFigOutput(figDisplay)

try:
    __IPYTHON__
    import IPython
    #from IPython import display
except NameError:
    pass
else: # and IPython. See also get_ipython()
    figFormat = 'retina' # 'svg'
    IPython.display.set_matplotlib_formats(figFormat)
    #display.set_matplotlib_formats(figFormat)
    if verbose > 1:
        print("Default display figure format set: "+figFormat)

#IPython.core.magic.Magics.basic.pprint()
#%pprint
#%precision %.6g
#np.set_printoptions(precision=6) # Number of decimal places for floats
# Long    Scientific
# 1e-4    1e-5
# 1e15    1e16

<|endoftext|>