Esempio n. 1
0
def main():

    # Clear out the old directory:
    if os.path.exists(html_output_dir):
        shutil.rmtree(html_output_dir)
    LocMgr.ensure_dir_exists(html_output_dir)

    root_html = Join(html_output_dir, "index.html")

    data = []
    for xmlfile in NeuroMLDataLibrary.get_channelMLV1FilesWithSingleChannel():


        #class NeuroMLDataLibrary(object):
#
#            def get_channelMLV1Files(self):


        #if xmlfile != "/home/michael/hw_to_come/morphforge/src/test_data/NeuroML/V1/example_simulations/GranCellLayer_NeuroML/Golgi_NaF_CML.xml":
        #        continue


        #if xmlfile != "/home/michael/hw_to_come/morphforge/src/test_data/NeuroML/V1/example_simulations/MainenEtAl_PyramidalCell_NeuroML/K_ChannelML.xml":
        #        continue
        #if xmlfile != "/home/michael/hw_to_come/morphforge/src/test_data/NeuroML/V1/example_simulations/CA1PyramidalCell_NeuroML/kdr.xml":
        #    continue

        # Compare:
        data.append(compareNeuroMLChl(xmlfile))

        # Re-update the html:
        with open(root_html, "w") as f:
            f.write(Template(root_html_tmpl, {'data': data}).respond())
def main():

    # Clear out the old directory:
    if os.path.exists(html_output_dir):
        shutil.rmtree(html_output_dir)
    LocMgr.ensure_dir_exists(html_output_dir)

    root_html = Join(html_output_dir, "index.html")

    data = []
    for xmlfile in NeuroMLDataLibrary.get_channelMLV1FilesWithSingleChannel():

        # Compare:
        data.append(compareNeuroMLChl(xmlfile))

        # Re-update the html:
        with open(root_html, "w") as f:
            f.write(Template(root_html_tmpl, {'data': data}).respond())

        #break
    print 'Done'
Esempio n. 3
0
    def _write_to_file(self, bundlefilename=None):
        bundleloc = LocMgr.get_simulation_tmp_dir()
        bundlesuffix = '.bundle'

        if bundlefilename is None:
            bundle_dir = bundleloc + '/' + self.get_sim_md5sum()[0:2] + '/'
            bundle_dir = LocMgr.ensure_dir_exists(bundle_dir)
            bundle_fname = self.get_sim_md5sum() + bundlesuffix
            bundlefilename = os.path.join(bundle_dir, bundle_fname)

        FileIO.write_to_file(txt=cPickle.dumps(self), filename=bundlefilename)
        # print 'bundlefilename', bundlefilename
        return bundlefilename
Esempio n. 4
0
    def prepare_sim_bundle(cls, sim):

        simstring = cPickle.dumps(sim)
        simmd5sum = StrUtils.get_hash_md5(simstring)

        simloc = LocMgr.get_simulation_tmp_dir() + simmd5sum[0:2]
        simloc = LocMgr.ensure_dir_exists(simloc)
        simfilename = Join(simloc, simmd5sum + cls.simsuffix)

        FileIO.write_to_file(txt=simstring, filename=simfilename)

        bundle = SimMetaDataBundle(sim)
        return bundle
Esempio n. 5
0
    def prepare_sim_bundle(cls, sim):

        simstring = cPickle.dumps(sim)
        simmd5sum = StrUtils.get_hash_md5(simstring)

        simloc = LocMgr.get_simulation_tmp_dir() + simmd5sum[0:2]
        simloc = LocMgr.ensure_dir_exists(simloc)
        simfilename = Join(simloc, simmd5sum + cls.simsuffix)

        FileIO.write_to_file(txt=simstring, filename=simfilename)

        bundle = SimMetaDataBundle(sim)
        return bundle
Esempio n. 6
0
    def _write_to_file(self, bundlefilename=None):
        bundleloc = LocMgr.get_simulation_tmp_dir()
        bundlesuffix = '.bundle'

        if bundlefilename is None:
            bundle_dir = bundleloc + '/' + self.get_sim_md5sum()[0:2] + '/'
            bundle_dir = LocMgr.ensure_dir_exists(bundle_dir)
            bundle_fname = self.get_sim_md5sum() + bundlesuffix
            bundlefilename = os.path.join(bundle_dir, bundle_fname)

        FileIO.write_to_file(txt=cPickle.dumps(self),
                             filename=bundlefilename)
        # print 'bundlefilename', bundlefilename
        return bundlefilename
Esempio n. 7
0
    def _trace_from_string(cls, srcstr):
        parser = ply.yacc.yacc(tabmodule='tracestring_parsetab',
                                outputdir=LocMgr.ensure_dir_exists('/tmp/parsetabs/'),
                                debug=SettingsMgr.get_ply_yacc_debug_flag())

        (unit, trace_prototypes) = parser.parse(srcstr, lexer=l)

        # Copy accross the start values:
        start_value = 0
        for prototype in trace_prototypes:
            prototype.start_value = start_value
            piece = prototype.toTracePiece()
            start_value = piece.get_end_value()

        # Convert to pieces
        pieces = [trace_prototype.toTracePiece() for trace_prototype in trace_prototypes]
        trace = TracePiecewise(pieces=pieces, comment='Src: %s' % srcstr)
        trace = trace * (1.0 * unit)
        return trace
Esempio n. 8
0
    def _trace_from_string(cls, srcstr):
        parser = ply.yacc.yacc(
            tabmodule='tracestring_parsetab',
            outputdir=LocMgr.ensure_dir_exists('/tmp/parsetabs/'),
            debug=SettingsMgr.get_ply_yacc_debug_flag())

        (unit, trace_prototypes) = parser.parse(srcstr, lexer=l)

        # Copy accross the start values:
        start_value = 0
        for prototype in trace_prototypes:
            prototype.start_value = start_value
            piece = prototype.toTracePiece()
            start_value = piece.get_end_value()

        # Convert to pieces
        pieces = [
            trace_prototype.toTracePiece()
            for trace_prototype in trace_prototypes
        ]
        trace = TracePiecewise(pieces=pieces, comment='Src: %s' % srcstr)
        trace = trace * (1.0 * unit)
        return trace
Esempio n. 9
0
def compareNeuroMLChl(xmlFile):
    model, chl_type = os.path.splitext(xmlFile)[0].split("/")[-2:]
    print model, chl_type

    op_dir = LocMgr.ensure_dir_exists(Join(html_output_dir, model, chl_type))
    op_html = Join(op_dir, "index.html")
    c = ComparisonResult(xmlfile=xmlFile, op_file = op_html, same_chl=True, exception=None)

    try:

        # Make the NeuroUnits channel:
        chl_neuro = NeuroML_Via_NeuroUnits_ChannelNEURON(xml_filename=xmlFile,  )
        c.chl_neurounits = chl_neuro


        op_pdf_file = Join(op_dir, 'Op1.pdf')
        #WriteToPDF(eqnset = chl_neuro.eqnset, filename = op_pdf_file)
        c.chl_neurounits_pdf = op_pdf_file


        # Make the NeuroML channel:
        xsl_file = "/home/michael/srcs/neuroml/CommandLineUtils/ChannelMLConverter/ChannelML_v1.8.1_NEURONmod.xsl"
        chl_xsl = NeuroML_Via_XSL_ChannelNEURON(xml_filename=xmlFile, xsl_filename=xsl_file,  )
        c.chl_xsl = chl_xsl
        c.chl_xsl_hoc = []


        chl_neuro_res = simulate_chl_all(chl_neuro)
        chl_xsl_res = simulate_chl_all(chl_xsl)
        c.chl_neurounit_hoc = []


        for i, (rN, rX) in enumerate(zip(chl_neuro_res, chl_xsl_res)):

            c.chl_neurounit_hoc.append(rN.hocfilename )
            c.chl_xsl_hoc.append(rX.hocfilename )

            tN = rN.get_trace("CurrentClamp").convert_to_fixed(dt=unit("1.01:ms"))
            tX = rX.get_trace("CurrentClamp").convert_to_fixed(dt=unit("1.01:ms"))

            # Compare current traces:
            tN._data[np.fabs(tN.time_pts_ms - 0) <0.05] *=0
            tX._data[np.fabs(tX.time_pts_ms - 0) <0.05] *=0
            tN._data[np.fabs(tN.time_pts_ms - 200) <0.05] *=0
            tX._data[np.fabs(tX.time_pts_ms - 200) <0.05] *=0
            tN._data[np.fabs(tN.time_pts_ms - 700) <0.05] *=0
            tX._data[np.fabs(tX.time_pts_ms - 700) <0.05] *=0
            print "TR1"
            f = QuantitiesFigure()
            ax1 = f.add_subplot(4, 1, 1)
            ax2 = f.add_subplot(4, 1, 2)
            ax3 = f.add_subplot(4, 1, 3)
            ax4 = f.add_subplot(4, 1, 4)
            ax1.plotTrace(tN, color='b')
            ax1.plotTrace(tX, color='g', linewidth=20, alpha=0.2)
            ax2.plotTrace(tN.window((200, 250)*pq.ms), color='b')
            ax2.plotTrace(tX.window((200, 250)*pq.ms), color='g', linewidth=20, alpha=0.2)

            num = (tN-tX)
            denom = (tN+tX)
            diff = num/denom
            ax3.plotTrace(diff, color='r')

            ax4.plotTrace(rN.get_trace('SomaVoltage'), color='m')
            ax4.plotTrace(rX.get_trace('SomaVoltage'), color='m', linewidth=20, alpha=0.2)

            if num.max()[1] > unit("0.1:pA"):
                c.same_chl = False

            out_im = Join(op_dir, "out_im%03d" % i)
            pylab.savefig(out_im+".png")
            pylab.savefig(out_im+".pdf")
            c.output_image_files.append(out_im)
            pylab.close()

        c.finished_ok=True



    except NeuroUnitsImportNeuroMLNotImplementedException, e:
        print 'Exception caught:', e

        s = StringIO.StringIO()
        traceback.print_exc(file=s)
        c.exception_long=s.getvalue()
        c.exception="%s (%s)"%(str(e), str(type(e)))
        c.same_chl = False
        c.finished_ok=False
Esempio n. 10
0
    def buildsectionsurface(cls, s):
        import gts
        from morphforge.core import LocMgr
        from os.path import join as Join
        print 'Building Mesh'

        working_dir = LocMgr.ensure_dir_exists('/tmp/mf/mesh/')
        fTemp1 = Join(working_dir, 'pts.txt')
        fTemp2 = Join(working_dir, 'pts.off')
        fTemp3 = Join(working_dir, 'pts.stl')
        fTemp2b = Join(working_dir, 'pts_postSub.off')
        fTemp4 = Join(working_dir, 'pts.gts')

        nstep = 5
        print 'Building Spheres'
        distal_offset = np.array((0.05, 0.05, 0.05))
        ptsP = GeomTools.produce_sphere(centre=s.get_proximal_npa3(),
                radius=s.p_r, n_steps=nstep)
        ptsD = GeomTools.produce_sphere(centre=s.get_distal_npa3()
                + distal_offset, radius=s.d_r, n_steps=nstep)

        print 'Removing Close Points'
        pts = cls.only_pts_at_min_dist(ptsP + ptsD, min_dist=0.01)

        print 'Writing:', fTemp2
        with open(fTemp1, 'w') as f:
            f.write('3 %d\n' % len(pts))
            np.savetxt(f, np.array(pts))

        if os.path.exists(fTemp2):
            os.unlink(fTemp2)
        os.system('qhull T1 QJ o < %s > %s' % (fTemp1, fTemp2))

        # Don't do the subdivision, just copy the files:
        os.system('cp %s %s' % (fTemp2, fTemp2b))
        # fTemp2 = fTemp2b

        f = open(fTemp2b).read().split()
        (nVertex, nFace, nEdge) = [int(i) for i in f[1:4]]
        assert nVertex > 5
        vertices = np.array([float(t) for t in f[4:4 + nVertex
                            * 3]]).reshape(nVertex, 3)

        triangles = np.array([int(t) for t in f[4 + nVertex * 3:]])
        triangles = triangles.reshape((nFace, 4))
        triangles = triangles[:, (1, 2, 3)]

        print 'Writing STL'
        with open(fTemp3, 'w') as fSTL:
            fSTL.write('solid name\n')
            for i in range(triangles.shape[0]):
                (a, b, c) = triangles[i, :]

                fSTL.write('facet normal 0 0 0\n')
                fSTL.write('outer loop \n')
                fSTL.write('vertex %f %f %f\n' % (vertices[a, 0], vertices[a, 1], vertices[a, 2]))
                fSTL.write('vertex %f %f %f\n' % (vertices[b, 0], vertices[b, 1], vertices[b, 2]))
                fSTL.write('vertex %f %f %f\n' % (vertices[c, 0], vertices[c, 1], vertices[c, 2]))
                fSTL.write('endloop \n')
                fSTL.write('endfacet\n')

            fSTL.write('solid end')

        print 'Running stl2gts...'
        if os.path.exists(fTemp4):
            os.unlink(fTemp4)

        os.system('stl2gts < %s > %s' % (fTemp3, fTemp4))

        assert os.path.exists(fTemp4)

        import gts
        f = open(fTemp4)
        s = gts.Surface()
        s = gts.read(f)

        s.cleanup()
        assert s.is_closed()
        assert s.is_orientable()

        # s.tessellate()
        return s
Esempio n. 11
0
    def buildsectionsurface(cls, s):
        import gts
        from morphforge.core import LocMgr
        from os.path import join as Join
        print 'Building Mesh'

        working_dir = LocMgr.ensure_dir_exists('/tmp/mf/mesh/')
        fTemp1 = Join(working_dir, 'pts.txt')
        fTemp2 = Join(working_dir, 'pts.off')
        fTemp3 = Join(working_dir, 'pts.stl')
        fTemp2b = Join(working_dir, 'pts_postSub.off')
        fTemp4 = Join(working_dir, 'pts.gts')

        nstep = 5
        print 'Building Spheres'
        distal_offset = np.array((0.05, 0.05, 0.05))
        ptsP = GeomTools.produce_sphere(centre=s.get_proximal_npa3(),
                                        radius=s.p_r,
                                        n_steps=nstep)
        ptsD = GeomTools.produce_sphere(centre=s.get_distal_npa3() +
                                        distal_offset,
                                        radius=s.d_r,
                                        n_steps=nstep)

        print 'Removing Close Points'
        pts = cls.only_pts_at_min_dist(ptsP + ptsD, min_dist=0.01)

        print 'Writing:', fTemp2
        with open(fTemp1, 'w') as f:
            f.write('3 %d\n' % len(pts))
            np.savetxt(f, np.array(pts))

        if os.path.exists(fTemp2):
            os.unlink(fTemp2)
        os.system('qhull T1 QJ o < %s > %s' % (fTemp1, fTemp2))

        # Don't do the subdivision, just copy the files:
        os.system('cp %s %s' % (fTemp2, fTemp2b))
        # fTemp2 = fTemp2b

        f = open(fTemp2b).read().split()
        (nVertex, nFace, nEdge) = [int(i) for i in f[1:4]]
        assert nVertex > 5
        vertices = np.array([float(t) for t in f[4:4 + nVertex * 3]
                             ]).reshape(nVertex, 3)

        triangles = np.array([int(t) for t in f[4 + nVertex * 3:]])
        triangles = triangles.reshape((nFace, 4))
        triangles = triangles[:, (1, 2, 3)]

        print 'Writing STL'
        with open(fTemp3, 'w') as fSTL:
            fSTL.write('solid name\n')
            for i in range(triangles.shape[0]):
                (a, b, c) = triangles[i, :]

                fSTL.write('facet normal 0 0 0\n')
                fSTL.write('outer loop \n')
                fSTL.write('vertex %f %f %f\n' %
                           (vertices[a, 0], vertices[a, 1], vertices[a, 2]))
                fSTL.write('vertex %f %f %f\n' %
                           (vertices[b, 0], vertices[b, 1], vertices[b, 2]))
                fSTL.write('vertex %f %f %f\n' %
                           (vertices[c, 0], vertices[c, 1], vertices[c, 2]))
                fSTL.write('endloop \n')
                fSTL.write('endfacet\n')

            fSTL.write('solid end')

        print 'Running stl2gts...'
        if os.path.exists(fTemp4):
            os.unlink(fTemp4)

        os.system('stl2gts < %s > %s' % (fTemp3, fTemp4))

        assert os.path.exists(fTemp4)

        import gts
        f = open(fTemp4)
        s = gts.Surface()
        s = gts.read(f)

        s.cleanup()
        assert s.is_closed()
        assert s.is_orientable()

        # s.tessellate()
        return s