Example #1
0
def _plot_HiC(matrix_data, vmax, colors=['white', 'red']):
    #vmax=thr
    red_list = list()
    green_list = list()
    blue_list = list()
    #colors=['white','red']
    #colors=['darkblue','green','gold','darkred']
    for color in colors:
        col = Color(color).rgb
        red_list.append(col[0])
        green_list.append(col[1])
        blue_list.append(col[2])
    c = Colormap()
    d = {'blue': blue_list, 'green': green_list, 'red': red_list}
    mycmap = c.cmap(d)
    fig, ax = plt.subplots(figsize=(8, 8))
    #new_data=np.triu(matrix_data)
    #new_data=np.transpose(new_data[:,::-1])
    #mask = np.zeros_like(new_data)
    #mask[np.tril_indices_from(mask,-1)] = True
    #mask=np.transpose(mask[:,::-1])
    #with sns.axes_style("white"):
    #sns.heatmap(new_data,xticklabels=100,yticklabels=100,mask=mask,cmap=mycmap,cbar=False)
    #ax.set_facecolor('w')
    #fig.patch.set_facecolor('w')
    ax.set_facecolor('w')
    ax.grid(b=None)
    sns.heatmap(matrix_data.T,
                vmax=vmax,
                xticklabels=100,
                yticklabels=100,
                cmap=mycmap,
                cbar=False)
Example #2
0
    def get_html(self):

        # We have 3 colors but sometimes you may have only one or 2.
        # This may be an issue with canvasXpress. It seems essential
        # to sort the color column so that names are sorted alphabetically
        # and to include colors that are present in the sale order
        """try:
            self.data.sort_values(by='color', inplace=True)
        except:
            self.data.sort("color", inplace=True)
        """
        # Jinja related
        from easydev import get_package_location
        env = Environment()
        env.loader = jinja2.FileSystemLoader(
            get_package_location("gdsctools") + "/gdsctools/data/templates/")
        template = env.get_template("scatter.html")

        # We need to cireate 20 different colors
        from colormap import Colormap
        c = Colormap()
        cmap = c.cmap_linear("red", "blue", "yellow", N=20)

        colors = self.data[self._colname_color]
        colors = ["red" for x in self.data[self._colname_color]]

        jinja = {}
        jinja["colors"] = ["rgba(205,0,0,0.5)"]
        jinja["Group"] = colors
        jinja['xlabel'] = '"%s"' % self.xlabel
        jinja['ylabel'] = '"%s"' % self.ylabel

        selection = [self._colname_x, self._colname_y]

        text = []
        for index in zip(self.data.index):
            text.append("<pre>%s</pre>" %
                        self.data.ix[index][selection].to_string())
        jinja['vars'] = text

        #self.data.markersize /= (self.data.markersize.max()/3.)
        self.data['markersize'] = 20
        selection = [self._colname_x, self._colname_y, self._colname_size]
        #First value is Y, second is X, following will be used in the
        try:  # introduced in pandas > 0.16.2
            jinja['data'] = self.data[selection].round(3).values.tolist()
        except:  #for py3.3 on travis
            jinja['data'] = np.around(self.data[selection]).values.tolist()

        jinja[
            'title'] = '"Regression coefficient vs Bayes factor for all drugs"'

        jinja['minX'] = self.minX
        jinja['minY'] = self.minY
        jinja['maxX'] = self.maxX
        jinja['maxY'] = self.maxY

        self.html = template.render(jinja)
        return self.html
Example #3
0
 def get_colors(self, sequencial):
     c = Colormap()
     if sequencial:
         mycmap = c.cmap('YlGnBu')
     else:
         mycmap = c.cmap('tab10')
     rgba = mycmap(np.linspace(0, 1, 256)) * 255
     long = len(rgba)
     return rgba, long
Example #4
0
    def get_html(self):

        # We have 3 colors but sometimes you may have only one or 2.
        # This may be an issue with canvasXpress. It seems essential
        # to sort the color column so that names are sorted alphabetically
        # and to include colors that are present in the sale order
        """try:
            self.data.sort_values(by='color', inplace=True)
        except:
            self.data.sort("color", inplace=True)
        """
        # Jinja related
        from easydev import get_package_location
        env = Environment()
        env.loader = jinja2.FileSystemLoader(
                        get_package_location("gdsctools")
                        + "/gdsctools/data/templates/")
        template = env.get_template("scatter.html")

        # We need to cireate 20 different colors
        from colormap import Colormap
        c = Colormap()
        cmap = c.cmap_linear("red", "blue", "yellow", N=20)

        colors = self.data[self._colname_color]
        colors = ["red" for x in self.data[self._colname_color]]

        jinja = {}
        jinja["colors"] = ["rgba(205,0,0,0.5)"]
        jinja["Group"] = colors
        jinja['xlabel'] = '"%s"' % self.xlabel
        jinja['ylabel'] = '"%s"' % self.ylabel

        text = []
        for index in zip(self.data.index):
            text.append("<pre>%s</pre>" % self.data.ix[index].to_string())
        jinja['vars'] = text

        #self.data.markersize /= (self.data.markersize.max()/3.)
        self.data['markersize'] = 20
        selection = [self._colname_x, self._colname_y, self._colname_size]
        #First value is Y, second is X, following will be used in the
        try: # introduced in pandas > 0.16.2
            jinja['data'] = self.data[selection].round(3).values.tolist()
        except: #for py3.3 on travis
            jinja['data'] = np.around(self.data[selection]).values.tolist()

        jinja['title'] = '"Regression coefficient/ttest/bayes factor for all drugs"' 

        jinja['minX'] = 0
        jinja['minY'] = 0
        jinja['maxX'] = 1
        jinja['maxY'] = self.data[self._colname_y].max() * 1.1

        self.html = template.render(jinja)
        return self.html
Example #5
0
def cmap_builder(name, name2=None, name3=None):
    """return a colormap object compatible with matplotlib

    If only parameter **name** is provided, it should be a known matplotlib 
    colormap name (e.g., jet). If **name2** is provided, then a new colormap 
    is created going from the color **name** to the color **name2** with a
    linear scale. Finally, if **name3** is provided, a linear scaled colormap
    is built from color **name** to color **name3** with the intermediate color
    being the **name2**

    Matplotlib colormap map names


    """
    c = Colormap()
    # an R colormap
    if name and name2 and name3:
        return c.cmap_linear(name, name2, name3)
    elif name and name2:
        return c.cmap_bicolor(name, name2)
    elif name == 'heat':
        return c.get_cmap_heat()
    elif name == 'heat_r':
        return c.get_cmap_heat_r()
    # matplotlic colormaps
    elif name in c.colormaps:
        return c.cmap(name)
    # some custom diverging colormaps with black in the middle.
    elif name in c.diverging_black:
        return c.cmap(name)
    else:
        #valid = c.colormaps + c.diverging_black
        txt = "name provided {0} is not recognised. ".format(name)
        txt += "\n valid name can be found in colormap.colormap_names"
        raise ValueError(txt)
Example #6
0
    def getColormapViews(self):
        """
        Return a list of the colormap views defined by the layout.
        If there is more than one colormap view in the current
        application layout, each one can be accessed via its own element
        in the list. For example, if there are 3 colormap views in the
        GUI, the following sequence of commands can be used to load a
        different colormap in each one: 

            c = v.getColormapViews()
            c[0].setColormap('coolwarm')
            c[1].setColormap('spring')
            c[2].setColormap('cubehelix')

        Returns
        -------
        list
            A list of Colormap objects.
        """
        commandStr = "getColormapViews"
        colormapViewsList = self.con.cmdTagList(commandStr)
        colormapViews = []
        if (colormapViewsList[0] != ""):
            for cmv in colormapViewsList:
                colormapView = Colormap(cmv, self.con)
                colormapViews.append(colormapView)
        return colormapViews
Example #7
0
def _show_chic_clusterresult3(Results,
                              matrix_data,
                              colors=['white', 'green', 'blue', 'red']):
    if np.max(matrix_data.shape) < 3000:
        thr = np.percentile(matrix_data, 99.5)
    else:
        thr = np.percentile(matrix_data, 99.9)
    matrix_data[matrix_data > thr] = thr
    print(thr)
    red_list = list()
    green_list = list()
    blue_list = list()
    #['darkblue','seagreen','yellow','gold','coral','hotpink','red']
    for color in colors:
        col = Color(color).rgb
        red_list.append(col[0])
        green_list.append(col[1])
        blue_list.append(col[2])
    c = Colormap()
    d = {'blue': blue_list, 'green': green_list, 'red': red_list}
    mycmap = c.cmap(d)
    #plt.subplots(figsize=(8,8))
    #sns.heatmap(np.transpose(matrix_data[:,::-1]),cmap=mycmap)
    #sns.heatmap(np.transpose(matrix_data[:,::-1]),xticklabels=100,yticklabels=500,cmap=mycmap)
    plt.subplots(figsize=(8, 8))

    #sns.heatmap(np.transpose(matrix_data[:,::-1]),cmap=mycmap)

    #sns.heatmap(np.transpose(matrix_data[:,::-1]),xticklabels=100,yticklabels=500,cmap=mycmap,cbar=False)
    sns.heatmap(matrix_data.T,
                xticklabels=100,
                yticklabels=1000,
                cmap=mycmap,
                cbar=False)

    for i in Results.index:
        upper = Results.ix[i, 'upper']
        bottom = Results.ix[i, 'bottom']
        left = Results.ix[i, 'left']
        right = Results.ix[i, 'right']
        x_loc = [upper, upper, bottom, bottom, upper]
        y_loc = [left, right, right, left, left]
        plt.plot(x_loc, y_loc, '-', color='k', lw=2.5)
    plt.grid(b=None)
    plt.show()
Example #8
0
def test_soften():
    c = Colormap(10)
    c.flat(0, 10, (0, 0, 0))
    c.flat(3, 6, (255, 128, 64))

    c.soften()

    assert np.array_equal(
        c.cmap, [[0, 0, 0], [0, 0, 0], [63, 32, 16], [191, 96, 48],
                 [255, 128, 64], [191, 96, 48], [63, 32, 16], [0, 0, 0],
                 [0, 0, 0], [0, 0, 0]]), "Array didn't soften properly"
Example #9
0
 def get_colormap(self, choice):
     imcategory, imtype = choice
     if imcategory == 'Common':
         if imtype in self.fileclass.channels[
                 'ir'] or imtype in self.fileclass.channels['wv']:
             return 'gray_r'
         return 'gray'
     cmap = Colormap.get_colormap(imtype)
     if cmap == 0:
         print(imtype.upper() + '色阶存在问题。将使用灰度色阶。')
         return 'gray_r'
     return cmap
Example #10
0
def test_rotate():
    c = Colormap(130)
    c.flat(0, 130, (0, 0, 0))
    c.flat(0, 1, (1, 1, 1))

    v1 = sum(c.cmap[0])
    c.rotate(1)

    assert np.array_equal(c.cmap[0], [0, 0, 0]), \
        "Array 0th element didn't shift out (%s)" % str(c.cmap[0])
    assert np.array_equal(c.cmap[1], [1, 1, 1]), \
        "Array 1st element didn't shift in (%s)" % str(c.cmap[1])

    v2 = sum(c.cmap[0])
    c.rotate(-1)
    v3 = sum(c.cmap[0])

    # when we rotate out and rotate back, there should be net zero change to
    # the value of the first element. But the interim first element shoud be
    # different
    assert v1 == v3, "Array didn't rotate back (0)"
    assert v2 != v3, "Array didn't rotate back (1)"
Example #11
0
def test_rotate():
    c = Colormap(130)
    c.flat(0, 130, (0, 0, 0))
    c.flat(0, 1, (1, 1, 1))

    v1 = sum(c.cmap[0])
    c.rotate(1)

    assert np.array_equal(c.cmap[0], [0, 0, 0]), \
        "Array 0th element didn't shift out (%s)" % str(c.cmap[0])
    assert np.array_equal(c.cmap[1], [1, 1, 1]), \
        "Array 1st element didn't shift in (%s)" % str(c.cmap[1])

    v2 = sum(c.cmap[0])
    c.rotate(-1)
    v3 = sum(c.cmap[0])

    # when we rotate out and rotate back, there should be net zero change to
    # the value of the first element. But the interim first element shoud be
    # different
    assert v1 == v3, "Array didn't rotate back (0)"
    assert v2 != v3, "Array didn't rotate back (1)"
Example #12
0
    def getLinkedColormaps(self):
        """
        Get the colormaps that are linked to this image view.

        Returns
        -------
        list
            A list of Colormap objects.
        """
        resultList = self.con.cmdTagList("getLinkedColormaps",
                                         imageView=self.getId())
        linkedColormapViews = []
        if (resultList[0] != ""):
            for colormap in resultList:
                linkedColormapView = Colormap(colormap, self.con)
                linkedColormapViews.append(linkedColormapView)
        return linkedColormapViews
Example #13
0
def diff_reflect():
    diff_reflect_cdict = {
        'red':
        ((0.000, 0.000, 0.000), (0.333, 1.000, 1.000), (0.417, 0.000, 0.000),
         (0.500, 0.000, 0.000), (0.583, 1.000, 1.000), (0.750, 1.000, 1.000),
         (0.833, 1.000, 1.000), (1.000, 1.000, 1.000)),
        'green':
        ((0.000, 0.000, 0.000), (0.333, 1.000, 1.000), (0.417, 0.000, 0.000),
         (0.500, 1.000, 1.000), (0.583, 1.000, 1.000), (0.750, 0.000, 0.000),
         (0.833, 0.000, 0.000), (1.000, 1.000, 1.000)),
        'blue':
        ((0.000, 0.000, 0.000), (0.333, 1.000, 1.000), (0.417, 1.000, 1.000),
         (0.500, 1.000, 1.000), (0.583, 0.000, 0.000), (0.750, 0.000, 0.000),
         (0.833, 1.000, 1.000), (1.000, 1.000, 1.000))
    }
    diff_reflect_coltbl = Colormap(
        'DIFF_REFLECT_COLTBL',
        diff_reflect_cdict)  #You will need to import this
    return diff_reflect_coltbl
Example #14
0
 def get_options(self):
     wv_colormaps, ir_colormaps = Colormap.get_colormap_list()
     for imtype in self.channel_flag.keys():
         if self.channel_flag[imtype]:
             self.common_imoptions['Common'].append(imtype)
     for imtype in self.fileclass.composite.keys():
         composite_flag = [
             self.channel_flag[channel_require]
             for channel_require in self.fileclass.composite[imtype]
         ]
         if all(composite_flag):
             self.common_imoptions['Composite'].append(imtype)
     for wv in self.fileclass.channels['wv']:
         if self.channel_flag[wv]:
             self.spec_imoptions['WV强化']['channels'].append(wv)
             self.spec_imoptions['WV强化']['colormaps'] = wv_colormaps
     for ir in self.fileclass.channels['ir']:
         if self.channel_flag[ir]:
             self.spec_imoptions['IR强化']['channels'].append(ir)
             self.spec_imoptions['IR强化']['colormaps'] = ir_colormaps
Example #15
0
def test_soften():
    c = Colormap(10)
    c.flat(0, 10, (0, 0, 0))
    c.flat(3, 6, (255, 128, 64))

    c.soften()

    assert np.array_equal(c.cmap, [
                          [0, 0, 0],
                          [0, 0, 0],
                          [63, 32, 16],
                          [191, 96, 48],
                          [255, 128, 64],
                          [191, 96, 48],
                          [63, 32, 16],
                          [0, 0, 0],
                          [0, 0, 0],
                          [0, 0, 0]
                          ]), "Array didn't soften properly"
Example #16
0
def cmap_builder(name, name2=None, name3=None):
    """return a colormap object compatible with matplotlib

    If only parameter **name** is provided, it should be a known matplotlib 
    colormap name (e.g., jet). If **name2** is provided, then a new colormap 
    is created going from the color **name** to the color **name2** with a
    linear scale. Finally, if **name3** is provided, a linear scaled colormap
    is built from color **name** to color **name3** with the intermediate color
    being the **name2**

    Matplotlib colormap map names

    """
    c = Colormap()
    # an R colormap
    if name and name2 and name3:
        return c.cmap_linear(name, name2, name3)
    elif name and name2:
        return c.cmap_bicolor(name, name2)
    elif name == 'heat':
        return c.get_cmap_heat()
    elif name == 'heat_r':
        return c.get_cmap_heat_r()
    # matplotlic colormaps
    elif name in c.colormaps:
        return c.cmap(name)
    # some custom diverging colormaps with black in the middle.
    elif name in c.diverging_black:
        return c.cmap(name)
    elif name.count("_") == 2:
        name1, name2, name3 = name.split("_")
        return c.cmap_linear(name1, name2, name3)
    else:
        #valid = c.colormaps + c.diverging_black
        txt = "name provided {0} is not recognised. ".format(name)
        txt += "\n valid name can be found in colormap.colormap_names"
        raise ValueError(txt)
Example #17
0
def alignmultiple(niifiles,
                  subjectid,
                  outputprefix,
                  mcmask='brainmask',
                  skips=(5, 5, 5),
                  rots=(0, 0, 0)):
    '''
    alignmultiple(niifiles, subjectid, outputprefix, mcmask,skips,rots)

    <niifiles> is a list of NIFTI filenames
    <subjectid> is like 'C0001'
    <outputprefix> is like 'T1average'
    <mcmask>: can be
        'brainmask', a str, indicates that we use a skullstripped brain mask returned by
            afni utility and then use this mask to align multiple files
        [mn, sd], a list, with the mn and sd outputs of defineellipse3d.m.

    <skips> is number of slices to skip in each of the 3 dimensions.
        Default: (5, 5, 5).
    <rots> is a 3-vector with number of CCW rotations to apply for each slicing.
        Default: (0, 0, 0).

    The purpose of this function is to align and average all of the NIFTI files
    and write out a new NIFTI file.

    We pause for the user to manually define an binary 3D ellipse on the
    first NIFTI in order to restrict the alignment procedure to those voxels.
    Consider, for example, restricting the ellipse to cortex and maybe a little bit
    of surrounding space.

    We loop over NIFTIs after the first one to align each with the first using
    a rigid-body transformation and a correlation metric. Slices are extracted
    using cubic interpolation to derive volumes that match the first.

    Our final volume is the mean of all of the volumes (first volume plus the
    resliced versions of the remaining volumes).

    We write out a NIFTI file to <anatomicals>/<outputprefix>.nii.gz using the first NIFTI as a template.

    Inspections of results are written to a directory <ppresults>/<outputprefix>figures.

    history:
    2016/09/02 - change where files are written; fix the gzipping
    2016/07/12 - switch to _untouch_
    2016/06/10 - force non-finite values in resliced volumes to be 0 (flirt fails otherwise)
    2016/05/29 - added visualization of residuals

    #========================
    below is by RZ:
    #========================
    Note:
    We differ from XXX

    To do:
    - implement the ellipse, and alignment
    - implement the brain extraction and alignment using afni or fsl

    history:
    20180714 accept the pathlib object for niftitiles
    20180620 RZ created this file based on cvnalignmultiple.m
    '''

    from RZutilpy.cvnpy import cvnpath
    from RZutilpy.system import unix_wrapper
    from RZutilpy.imageprocess import makeimagestack3dfiles
    from RZutilpy.mri import savenifti
    import nibabel as nib
    from numpy import isnan, isfinite, percentile, abs, all, stack
    from os.path import dirname, join
    from colormap import Colormap
    from pathlib import Path

    # calc
    dir0 = cvnpath('anatomicals') / subjectid
    pp0 = cvnpath('ppresults') / subjectid

    # match files
    if all([isinstance(p, Path) for p in niifiles]):
        niifiles = [str(p) for p in niifiles]
    niifiles = matchfiles(
        niifiles)  # after matchfiles niifiles become pathlib objects

    # load the first volume
    vol1 = nib.load(str(niifiles[0]))
    vol1data = vol1.get_data().astype('f4')  # 32 bit floating point
    vol1data[isnan(vol1data)] = 0

    # ===============================================
    # use afni to align all volumnes
    # ===============================================
    # here is different from KK' cvnalignmultiple.m, we use afni linear align utility
    # to align multiple volumes

    # let's create a brainmask using afni utility
    datadir = niifiles.parent
    brainmaskfile = datadir / 'T1alignbrainmask.nii.gz'

    # skullstrip to create the mask
    result = unix_wrapper('3dSkullStrip -input {} -prefix {} -mask_vol'.format(
        str(niifiles[0]), str(brainmaskfile)))
    # on stone 3DSkullStrip cannot specify the output directory... afni bug??
    # we manually move the mask file
    result = unix_wrapper('mv T1alignbrainmask.nii.gz %s/'.format(
        str(datadir)))

    # read the mask vol file
    brainmask = nib.load(str(brainmaskfile)).get_data()

    # inspect first volume and brainmask
    makeimagestack3dfiles(vol1data,
                          join(str(pp0), '{}figures'.format(outputprefix),
                               'vol{:03d}'.format(0)),
                          skips,
                          rots,
                          wantnorm=1,
                          addborder=1)
    makeimagestack3dfiles(brainmask,
                          join(str(pp0), '%sfigures'.format(outputprefix),
                               'brainmask'),
                          skips,
                          rots,
                          wantnorm=1,
                          addborder=1)
    # ===============================================

    # # manually define ellipse on the first volume for use in the auto alignment
    # if isempty(mcmask)
    #   [f,mn,sd] = defineellipse3d(vol1data)
    #   mcmask = {mn sd}
    #   fprintf('mcmask = %s\n',cell2str(mcmask))
    # else
    #   mn = mcmask{1}
    #   sd = mcmask{2}

    # loop over volumes
    outputfiles = [p[:-7] + '_aligned.nii.gz' for p in niifiles]

    vols = []  # note that this list does not contain the 1st volume
    for p in range(1, len(niifiles)):
        # issue the cmd as list. This is the input for unix_wrapper
        cmd = ['3dAllineate',\
        '-base', niifiles[0], '-source', niifiles[p], '-prefix', outputfiles[p],\
        '-weight', brainmaskfile, '-1Dparam_save', niifiles[p][:-7],\
        '-interp', 'cubic', '-cost', 'crM', '-final', 'wsinc5']
        # do it
        unix_wrapper(cmd)

        ## start the alignment
        #alignvolumedata(vol2data,vol2size,vol1data,vol1size)  # NOTICE THAT FIRST VOLUME IS THE TARGET!

        # auto-align (rigid-body, correlation)
        #alignvolumedata_auto(mn,sd,[1 1 1 1 1 1 0 0 0 0 0 0],[4 4 4])
        #alignvolumedata_auto(mn,sd,[1 1 1 1 1 1 0 0 0 0 0 0],[2 2 2])
        #alignvolumedata_auto(mn,sd,[1 1 1 1 1 1 0 0 0 0 0 0],[1 1 1])

        # get the transformation
        #tr = alignvolumedata_exporttransformation

        # get slices from vol2 to match vol1
        #matchvol = extractslices(vol2data,vol2size,vol1data,vol1size,tr)

        # read the aligned volume
        matchvol = nib.load(outputfiles[p]).get_data()
        assert all(matchvol.shape == vol1data.shape
                   ), 'This # %d volume have a different size!' % p
        # REALLY IMPORTANT: ENSURE FINITE (e.g. flirt blows up otherwise)
        matchvol[~isfinite(matchvol)] = 0
        # inspect it
        makeimagestack3dfiles(matchvol,
                              join(pp0, '%sfigures' % outputprefix,
                                   'vol%03d' % p),
                              skips,
                              rots,
                              wantnorm=1,
                              addborder=1)
        # record it into the list
        vols.append(matchvol)

    # average all vols
    meanvol = sum(vols) / len(vols)
    # inspect it
    makeimagestack3dfiles(meanvol,
                          join(pp0, '%sfigures' % outputprefix, 'volavg'),
                          skips,
                          rots,
                          wantnorm=1,
                          addborder=1)

    # finally, write out residual images
    # prepare colormap
    if len(vols) != 1:
        mx = percentile(stack([abs(vol - meanvol) for vol in vols], axis=-1),
                        99)
        [makeimagestack3dfiles(vol-meanvol, join(pp0, '%sfigures' % outputprefix, 'resid%03d' % p),\
          skips,rots, cmap=Colormap().cmap_linear('blue','black','red'), wantnorm=(-mx, mx), \
          addborder=1) for p, vol in enumerate(vols)]

    # save the mean vol NIFTI file as output
    savenifti(meanvol, join(dir0, '%s.nii.gz' % outputprefix), niftiobj=vol1)
Example #18
0
samplenum = "None"
imnum = 1
vidnum = 1
exposure = float(10000.0)  # value in microseconds
avg = 0
t0 = time.time()
t, avg1, avg2, avg3, oldt, oldavg1, oldavg2, oldavg3, oldert, olderavg1, olderavg2, olderavg3 = [], [], [], [], [], [], [], [], [], [], [], []
x1, y1, x2, y2 = 0, 0, 640, 480
a1, b1, a2, b2 = 0, 0, 640, 480
c1, d1, c2, d2 = 0, 0, 640, 480
i = 1
red, green, blue = True, False, False
path = 'C:/Users/Palmstrom Lab/Desktop/FRHEED/'
filename = 'default'
#### Define colormap ####
cmp = Colormap()
FRHEEDcmap = cmp.cmap_linear('black', 'green', 'white')
cm.register_cmap(name='RHEEDgreen', cmap=FRHEEDcmap)
cmap = cm.gray
#cmp.test_colormap(FRHEEDcmap)
#### Loading UI file from qt designer ####
form_class = uic.loadUiType("FRHEED backup.ui")[0]
#### Define "shortcut" for queue ####
q = queue.Queue()
#### Define video recording codec ####
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
#### Set default appearance of plots ####
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 0.0)
#### Connect to RHEED camera ####
system = PySpin.System.GetInstance()
def main(cli_D=2.03,
         new_gx=0e-8,
         anion_flux=False,
         default_xz=-0.85,
         jkccup=1e-12,
         nrcomps=2,
         dz=1e-7,
         textra=100,
         say='',
         stretch=False):
    """
    cli_D # um2/s
    :return: sim, gui: it is useful to return these objects for access after simulation
    """
    print("main")
    sim = Simulator().get_instance()
    gui = sim.gui()
    dt = 0.001  # s

    length = 10e-5

    comp = Compartment("reference",
                       z=-0.85,
                       cli=0.0052,
                       ki=0.0123,
                       nai=0.014,
                       length=length,
                       radius=default_radius_short,
                       stretch_w=stretch)

    # copies left
    compl = comp.copy("dendrite left")

    # copies right
    compr = []
    compr.append(comp.copy("dendrite right " + str(1)))
    for i in range(nrcomps):
        compr.append(comp.copy("dendrite right " + str(i + 2)))

    # find steady-state values of ions
    sim.run(stop=100,
            dt=0.001,
            plot_update_interval=50,
            data_collect_interval=5,
            block_after=False)

    # set diffusion value
    cli_D *= 1e-7  # cm2 to dm2 (D in dm2/s)
    ki_D = 1.96
    ki_D *= 1e-7  # cm2 to dm2 (D in dm2/s)
    nai_D = 1.33
    nai_D *= 1e-7
    diffusion_object = []

    # connect with Diffusion
    diffusion_object.append(
        Diffusion(compl, comp, ions={
            'cli': cli_D,
            'ki': ki_D,
            'nai': nai_D
        }))
    diffusion_object.append(
        Diffusion(comp,
                  compr[0],
                  ions={
                      'cli': cli_D,
                      'ki': ki_D,
                      'nai': nai_D
                  }))
    for i in range(nrcomps):
        diffusion_object.append(
            Diffusion(compr[i],
                      compr[i + 1],
                      ions={
                          'cli': cli_D,
                          'ki': ki_D,
                          'nai': nai_D
                      }))

    # heatmap incorporating compartment heights
    sc = 1e7
    htplot = Colormap("cmap", 0, compr)
    totalht, initvals = htplot.heatmap(compl,
                                       comp,
                                       compr,
                                       sc,
                                       0,
                                       all=1,
                                       init_vals=None)
    htplot.heatmap(compl, comp, compr, sc, totalht, all=1, init_vals=initvals)

    voltage_reversal_graph_comp = gui.add_graph() \
        .add_ion_conc(comp, "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(comp, "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(comp, line_style='k', y_units_scale=1000, y_plot_units='mV')

    voltage_reversal_graph_compr = gui.add_graph() \
        .add_ion_conc(compr[-1], "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(compr[-1], "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(compr[-1], line_style='k', y_units_scale=1000, y_plot_units='mV')

    voltage_reversal_graph_compl = gui.add_graph() \
        .add_ion_conc(compl, "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(compl, "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(compl, line_style='k', y_units_scale=1000, y_plot_units='mV')

    voltage_reversal_graph_compr1 = gui.add_graph() \
        .add_ion_conc(compr[0], "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(compr[0], "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(compr[0], line_style='k', y_units_scale=1000, y_plot_units='mV')

    # run simulation with diffusion
    sim.run(continuefor=10,
            dt=dt,
            plot_update_interval=5,
            data_collect_interval=1)
    print(datetime.datetime.now())
    print_concentrations(
        [comp, compl, compr[-1]],
        title="Ion concentrations given diffusion between compartments")

    htplot.heatmap(compl, comp, compr, sc, totalht, all=1, init_vals=initvals)

    # (optionally) change anion conductance
    prev_comp_gx = comp.gx
    comp.gx = new_gx
    comp.dz = dz

    voltage_reversal_graph_comp.save(say + 'reference.eps')

    if dz != 0:
        z_graph = gui.add_graph() \
            .add_ion_conc(comp, "z", line_style='m')

    if comp.gx > 0:
        x_graph = gui.add_graph() \
            .add_ion_conc(comp, "absox", line_style='m') \
            .add_ion_conc(compl, "absox", line_style=':m') \
            .add_ion_conc(compr[0], "absox", line_style='m--')

    # (optionally) change anion flux
    if anion_flux:
        comp.xz = default_xz
        comp.xmz = (comp.z * comp.xi - comp.xz * comp.xi_temp) / comp.xm
        print('Anion flux with fixed anions having net charge', comp.xmz,
              'while a proportion of', (1 - comp.ratio),
              'of all impermeants are temporarily mobile anions of charge',
              comp.xz)

    z_graph = gui.add_graph() \
        .add_ion_conc(comp, "z", line_style='m')

    # (optionally) change kcc2
    prev_comp_pkcc2 = comp.pkcc2
    if jkccup is not None:
        comp.jkccup = jkccup
        g_graph = gui.add_graph() \
            .add_ion_conc(comp, "pkcc2", line_style='k')

    vol_graph = gui.add_graph() \
        .add_ion_conc(comp, "w", line_style='b') \
        .add_ion_conc(compl, "w", line_style=':b') \
        .add_ion_conc(compr[0], "w", line_style='b--')

    sim.run(continuefor=textra,
            dt=dt * 0.001,
            plot_update_interval=textra / 32,
            data_collect_interval=textra / 32)
    print(datetime.datetime.now())
    print_concentrations(
        [comp, compl, compr[-1]],
        title="Ion concentrations during event from the dendritic compartment")
    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_halfway_df.eps', say + 'all_halfway_ecl.eps',
                       say + 'all_halfway_vm.eps'
                   ])

    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 16)
    print(datetime.datetime.now())
    print_concentrations(
        [comp, compl, compr[-1]],
        title=
        "Ion concentrations immediately after event from the dendritic compartment"
    )
    # heatmap incorporating compartment heights
    htplot.heatmap(compl, comp, compr, sc, totalht, all=1, init_vals=initvals)

    comp.gx = prev_comp_gx
    comp.jkccup = 0
    comp.dz = 0

    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 1,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at almost steady state")

    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 1,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at steady state")

    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 2,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at steady state")

    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 2,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at steady state")

    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    sim.run(continuefor=textra * 2,
            dt=dt * 0.001,
            plot_update_interval=textra / 2,
            data_collect_interval=textra / 4)
    print(datetime.datetime.now())
    print_concentrations([comp, compl, compr[-1]],
                         title="Ion concentrations at steady state")

    # heatmap incorporating compartment heights
    htplot.heatmap(compl,
                   comp,
                   compr,
                   sc,
                   totalht,
                   all=1,
                   init_vals=initvals,
                   title=[
                       say + 'all_end_df.eps', say + 'all_end_ecl.eps',
                       say + 'all_end_vm.eps'
                   ])
    voltage_reversal_graph_comp.save(say + 'reference.eps')

    return sim, gui
def grow(nr=3, textra=10):
    print("growing via anions")
    sim = Simulator().get_instance()
    gui = sim.gui()
    dt = 0.001  # s

    comp = []
    comp.append(
        Compartment("initial growth cone",
                    z=-0.85,
                    cli=0.00433925284075134,
                    ki=0.1109567493822927,
                    nai=0.0255226350779378,
                    length=5e-5,
                    radius=default_radius_short))

    # steady state
    sim.run(stop=100,
            dt=0.001,
            plot_update_interval=500,
            data_collect_interval=5,
            block_after=False)

    # set diffusion value
    cli_D = 2.03
    cli_D *= 1e-7  # cm2 to dm2 (D in dm2/s)
    ki_D = 1.96
    ki_D *= 1e-7  # cm2 to dm2 (D in dm2/s)
    nai_D = 1.33
    nai_D *= 1e-7

    #another compartment
    comp.append(comp[0].copy("compartment 1"))
    comp[1].L = 10e-5
    comp[1].w = np.pi * comp[1].r**2 * comp[1].L
    diffusion_object = [
        Diffusion(comp[0],
                  comp[1],
                  ions={
                      'cli': cli_D,
                      'ki': ki_D,
                      'nai': nai_D
                  })
    ]

    # heatmap incorporating compartment heights
    sc = 1e5
    htplot = Colormap("dendrite", comp[0].w + comp[1].w, comp)
    totalht, init_vals = htplot.smallheatmap(comp,
                                             sc,
                                             int(htplot.totalh * sc),
                                             all=0,
                                             init_val=None)

    # plot
    voltage_reversal_graph_comp = gui.add_graph() \
        .add_ion_conc(comp[0], "ecl", line_style='g', y_units_scale=1000, y_plot_units='mV') \
        .add_ion_conc(comp[0], "ek", line_style='b', y_units_scale=1000, y_plot_units='mV') \
        .add_voltage(comp[0], line_style='k', y_units_scale=1000, y_plot_units='mV')

    volume_graph = gui.add_graph()
    volume_graph.add_var(volume_graph.time,
                         "time",
                         htplot,
                         "comp0w",
                         line_style='k')
    volume_graph.add_var(volume_graph.time,
                         "time",
                         htplot,
                         "totalh",
                         line_style='b')

    sim.run(continuefor=1,
            dt=dt * 0.001,
            plot_update_interval=0.5,
            data_collect_interval=textra / 16)

    # growth
    for i in range(nr):
        htplot.smallheatmap(comp,
                            sc,
                            totalht,
                            all=0,
                            init_val=init_vals,
                            name='graphs/grow_done' + str(i) + '.eps')
        comp[0].gx = 1

        # stop at certain length
        while comp[0].L < 15e-5:
            print("Fluxing compartment's length: " + str(comp[0].L))
            sim.run(continuefor=0.5,
                    dt=dt * 0.001,
                    plot_update_interval=0.25,
                    data_collect_interval=textra / 16)
            if 9.9e-5 < comp[0].L < 10.3e-5:
                htplot.smallheatmap(comp,
                                    sc,
                                    totalht,
                                    all=0,
                                    init_val=init_vals,
                                    name='graphs/grow_interim' + str(i) +
                                    '.eps')
        comp[0].gx = 0
        print_concentrations(comp, str(i))

        # split compartments
        comp.insert(0, comp[0].copy("compartment " + str(i)))
        comp[1].L -= 5e-5
        comp[0].L = 5e-5
        comp[0].w = np.pi * comp[0].r**2 * comp[0].L
        comp[1].w = np.pi * comp[1].r**2 * comp[1].L

        print_concentrations(comp, str(i))

        # update total height
        htplot.comp = comp

        # update diffusion
        diffusion_object.append(
            Diffusion(comp[0],
                      comp[1],
                      ions={
                          'cli': cli_D,
                          'ki': ki_D,
                          'nai': nai_D
                      }))

        for a in comp:
            print(a.name)

        for j in diffusion_object:
            print(j.name)

        sim.run(continuefor=10,
                dt=dt * 0.001,
                plot_update_interval=5,
                data_collect_interval=textra / 16)

    htplot.smallheatmap(comp,
                        sc,
                        totalht,
                        all=1,
                        init_val=init_vals,
                        name='graphs/grow_end.eps')
    sim.run(continuefor=4,
            dt=dt * 0.001,
            plot_update_interval=2,
            data_collect_interval=0.5)
    htplot.smallheatmap(comp,
                        sc,
                        totalht,
                        all=1,
                        init_val=init_vals,
                        name='graphs/grow_end.eps')

    return sim, gui
Example #21
0
def make_u_plot(u_fname,
                make_single,
                make_with_metric,
                label_p,
                u_zonal=None,
                u_zonal_NH=None,
                Method=None,
                Method_NH=None,
                lat_elem_SH=None,
                lat_elem_NH=None,
                STJ_lat=None,
                method_choice=None,
                time=None,
                file_out_syntax=None):

    open_data = False
    if open_data:
        #open jet data from file
        filename = '{0}/STJ_data_{1}.nc'.format(data_out_dir, file_out_syntax)
        assert os.path.isfile(
            filename
        ), 'File ' + filename + ' does not exist. Need jet latitude for plotting.'
        var_jet = openNetCDF4_get_data(filename)
        jet_NH = var_jet['STJ_lat'][:, 0]
        jet_SH = var_jet['STJ_lat'][:, 1]
    else:
        #use data just calculated
        jet_NH = STJ_lat[0]
        jet_SH = STJ_lat[1]

    #open u wind data - on pressure levels
    assert os.path.isfile(u_fname), 'File ' + u_fname + ' does not exist.'
    var = openNetCDF4_get_data(u_fname)

    # check that input pressure is in pascal
    if var[label_p].max() < 90000.0:
        var[label_p] = var[label_p] * 100.0

    lev250 = FindClosestElem(25000, var[label_p], 0)[0]

    if 'var131' in var:
        uwnd = var['var131'][time, lev250, :, :]
    else:
        uwnd = var['u'][time, lev250, :, :]

    if make_single:
        fname_out = '{}/uwind_{}_with_wind_{}.eps'.format(
            plot_dir, t_elem[t], file_out_syntax)

        fig = plt.figure(figsize=(10, 5))

        # wind plot
        ax1 = plt.subplot2grid((10, 20), (0, 0), colspan=16, rowspan=9)
        # zonal mean
        ax2 = plt.subplot2grid((10, 20), (0, 16), colspan=5, rowspan=9)
        # colour bar
        ax_cb = plt.subplot2grid((10, 20), (9, 0), colspan=20)

        save_plot = True
        plot_u(plt,
               ax1,
               ax2,
               ax_cb,
               jet_NH,
               jet_SH,
               uwnd,
               var,
               t,
               t_elem,
               fname_out,
               save_plot,
               make_single=make_single)

    if make_with_metric:

        fname_out = '{}/validation_uwind_{}_{}.eps'.format(
            plot_dir, time, file_out_syntax)

        fig = plt.figure(figsize=(10, 12))
        #SH
        ax1 = plt.subplot2grid(
            (37, 26), (0, 0), rowspan=22,
            colspan=12)  #half across/gap/half accross. 18 deep
        #NH
        ax2 = plt.subplot2grid((37, 26), (0, 12), rowspan=22, colspan=12)
        # wind plot
        ax3 = plt.subplot2grid((37, 26), (22, 0), rowspan=13, colspan=20)
        # zonal mean
        ax4 = plt.subplot2grid((37, 26), (22, 20), rowspan=13, colspan=5)
        # colour bar
        ax_cb = plt.subplot2grid((37, 26), (35, 0), rowspan=2, colspan=37)

        bounds = np.arange(-50, 60, 5.0)
        cm = Colormap()
        cmap = cm.cmap_linear('#0033ff', '#FFFFFF', '#990000')  #blue/red
        #cmap = pylab.cm.get_cmap('RdBu_r')

        PlottingObject = Plotting(Method, 'cby')
        plot_cbar = False

        #u_zonal = MeanOverDim(data=uwnd, dim=1)

        PlottingObject.poly_2PV_line('SH',
                                     u_zonal,
                                     lat_elem_SH,
                                     time,
                                     fig,
                                     ax1,
                                     plot_cbar,
                                     ax_cb,
                                     bounds,
                                     cmap,
                                     plot_type='subplot_all',
                                     pause=False,
                                     click=False,
                                     save_plot=False)
        # NH
        PlottingObject2 = Plotting(Method_NH, method_choice)
        PlottingObject2.poly_2PV_line('NH',
                                      u_zonal_NH,
                                      lat_elem_NH,
                                      time,
                                      fig,
                                      ax2,
                                      plot_cbar,
                                      None,
                                      bounds,
                                      cmap,
                                      plot_type='subplot_all',
                                      pause=False,
                                      click=False,
                                      save_plot=False)

        save_plot = True
        plot_u(plt,
               ax3,
               ax4,
               ax_cb,
               jet_NH,
               jet_SH,
               uwnd,
               var,
               bounds,
               cmap,
               time,
               fname_out,
               save_plot,
               make_single=False)