参数过多的类:更好的设计策略?

2024-05-19 16:10:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究神经元模型。我正在设计的一个类是细胞类,它是一个神经元的拓扑描述(几个部分连接在一起)。它有许多参数,但它们都是相关的,例如:

轴突段数、顶端分叉、体细胞长度、体细胞直径、顶端长度、分枝随机性、分枝长度等。。。总共大约有15个参数!

我可以将所有这些设置为一些默认值,但我的类看起来很疯狂,有几行参数。这种事情偶尔也会发生在别人身上,是不是有更好的方法来设计这个,还是我做的对?

更新: 正如你们中的一些人所要求的,我已经附上了这个类的代码,正如你们所看到的,这个类有大量的参数(>;15),但它们都是用来定义单元格拓扑结构的,而且是必需的。问题本质上是他们创造的物理物体非常复杂。我附加了一个这个类生成的对象的图像表示。经验丰富的程序员如何以不同的方式避免定义中的这么多参数?

enter image description here

class LayerV(__Cell):

    def __init__(self,somatic_dendrites=10,oblique_dendrites=10,
                somatic_bifibs=3,apical_bifibs=10,oblique_bifibs=3,
                L_sigma=0.0,apical_branch_prob=1.0,
                somatic_branch_prob=1.0,oblique_branch_prob=1.0,
                soma_L=30,soma_d=25,axon_segs=5,myelin_L=100,
                apical_sec1_L=200,oblique_sec1_L=40,somadend_sec1_L=60,
                ldecf=0.98):

        import random
        import math

        #make main the regions:
        axon=Axon(n_axon_seg=axon_segs)

        soma=Soma(diam=soma_d,length=soma_L)

        main_apical_dendrite=DendriticTree(bifibs=
                apical_bifibs,first_sec_L=apical_sec1_L,
                L_sigma=L_sigma,L_decrease_factor=ldecf,
                first_sec_d=9,branch_prob=apical_branch_prob)

        #make the somatic denrites

        somatic_dends=self.dendrite_list(num_dends=somatic_dendrites,
                       bifibs=somatic_bifibs,first_sec_L=somadend_sec1_L,
                       first_sec_d=1.5,L_sigma=L_sigma,
                       branch_prob=somatic_branch_prob,L_decrease_factor=ldecf)

        #make oblique dendrites:

        oblique_dends=self.dendrite_list(num_dends=oblique_dendrites,
                       bifibs=oblique_bifibs,first_sec_L=oblique_sec1_L,
                       first_sec_d=1.5,L_sigma=L_sigma,
                       branch_prob=oblique_branch_prob,L_decrease_factor=ldecf)

        #connect axon to soma:
        axon_section=axon.get_connecting_section()
        self.soma_body=soma.body
        soma.connect(axon_section,region_end=1)

        #connect apical dendrite to soma:
        apical_dendrite_firstsec=main_apical_dendrite.get_connecting_section()
        soma.connect(apical_dendrite_firstsec,region_end=0)

        #connect oblique dendrites to apical first section:
        for dendrite in oblique_dends:
            apical_location=math.exp(-5*random.random()) #for now connecting randomly but need to do this on some linspace
            apsec=dendrite.get_connecting_section()
            apsec.connect(apical_dendrite_firstsec,apical_location,0)

        #connect dendrites to soma:
        for dend in somatic_dends:
            dendsec=dend.get_connecting_section()
            soma.connect(dendsec,region_end=random.random()) #for now connecting randomly but need to do this on some linspace

        #assign public sections
        self.axon_iseg=axon.iseg
        self.axon_hill=axon.hill
        self.axon_nodes=axon.nodes
        self.axon_myelin=axon.myelin
        self.axon_sections=[axon.hill]+[axon.iseg]+axon.nodes+axon.myelin
        self.soma_sections=[soma.body]
        self.apical_dendrites=main_apical_dendrite.all_sections+self.seclist(oblique_dends)
        self.somatic_dendrites=self.seclist(somatic_dends)
        self.dendrites=self.apical_dendrites+self.somatic_dendrites
        self.all_sections=self.axon_sections+[self.soma_sections]+self.dendrites

Tags: selfbranchconnectsigmafirstsomaprobaxon
3条回答

我要说的是,这种方法没有错——如果你需要15个参数来建模,你需要15个参数。如果没有合适的默认值,则在创建对象时必须传入所有15个参数。否则,您只需设置默认值,然后通过setter或直接更改它。

另一种方法是为某些常见类型的神经元(在您的示例中)创建子类,并为某些值提供良好的默认值,或者从其他参数派生值。

或者您可以将神经元的部分封装在单独的类中,并将这些部分用于您建模的实际神经元。一、 例如,可以编写单独的类来建模突触、轴突、胞体等

您也许可以使用Python“dict”对象? http://docs.python.org/tutorial/datastructures.html#dictionaries

尝试以下方法:

class Neuron(object):

    def __init__(self, **kwargs):
        prop_defaults = {
            "num_axon_segments": 0, 
            "apical_bifibrications": "fancy default",
            ...
        }

        for (prop, default) in prop_defaults.iteritems():
            setattr(self, prop, kwargs.get(prop, default))

然后,您可以创建这样的Neuron

n = Neuron(apical_bifibrications="special value")

相关问题 更多 >