working on it ...

Filters

Explore Public Snippets

Sort by

Found 94k snippets

    public by eduros93 modified Mar 1, 2019  103  3  2  -1

    4D+ Data Visualization

    plt, pd, sns, faceting, parallel coordinates
    # Parallel coordinates
    # Each data point is a line showing the value of each dim as it goes from
    #   left to right. You can se relation between dimensions, particularly
    #   relations with the "class_column", which is the hue variable
    # Image in comments
    from pandas.plotting import parallel_coordinates
    parallel_coordinates(df, class_column='targetCol', 
                          color=('#FFE888', '#FF9999'))
                          
    ###                      
    
    # 2D faceting of 2D scatterplots (4D)
    # Image in comments
    g = sns.FacetGrid(data, col="colFacet",  row="rowFacet")
    g = g.map(plt.scatter, "xCol", "yCol", edgecolor="w")
    
    ###
    
    # Line plot
    # Computes the mean of the y variable for each of the x values
    #   You can encode a third dim by using a line for each value of the dim
    #   You add a 4th (and 5th if you want) with facets
    # Image in comments
    grid = sns.FacetGrid(df, row='facetRowVar', size=2.2, aspect=1.6)
    grid.map(sns.pointplot, 'xVar', 'yVar', 'lineVar', palette='deep')
    grid.add_legend()
    												

    public by eduros93 modified Mar 1, 2019  55  1  1  0

    3D Data visualization

    plt, pd, sns, faceting, boxplot, pairplot, scatter, scatterplot, density plot
    # Pairplot (image in comments)
    g = sns.pairplot(train[selectedCols], hue='target', palette = 'seismic',
                    size=1.2, diag_kind = 'kde',diag_kws=dict(shade=True),
                    plot_kws=dict(s=10))
    g.set(xticklabels=[])
    
    ###
    
    # Boxplot faceted for 2nd dim and grouped for 3rd dim (image in comments)
    sns.boxplot(x="facetCol", y="boxplotCol", hue="hueCol", data=df)
    
    ###
    
    # 1st dim facet, 2nd and 3rd dims are the scatter plot
    #   the hue dimension is used to give statistical info of the y axis var
    #   We discretize the facet variable to have a finite number of facets
    #   We discretize the y axis variable to then show the quartiles in the plot
    #   Image in comments
    df['discreteFacetVar'] = pd.qcut(df['facetVar'], 
                                    q=quantile_list, labels=quantile_labels)
    df['discreteYaxisVar'] = pd.qcut(df['yaxisVar'], 
                                    q=quantile_list, labels=quantile_labels)
    g = sns.FacetGrid(df, col="discreteFacetVar", 
                      hue='discreteYaxisVar')
    g.map(plt.scatter, "xaxisVar", "yaxisVar", alpha=.7)
    
    ###
    
    # Like scatterplot but showing density. Scatter plot 2.0
    # 3rd dim is hue, we made a manual hue by plotting twice
    #   each time with a different color
    # Image in comments
    plot1 = sns.kdeplot(df1['col1'], df1['col2'],
                      cmap="YlOrBr", shade=True, shade_lowest=False)
    plot2 = sns.kdeplot(df2['col1'], df2['col2'],
                      cmap="Reds", shade=True, shade_lowest=False)
                      
                      
                      
                      
              												

    public by eduros93 modified Mar 1, 2019  48  0  2  0

    2D Data Visualization

    correlation, heatmap, plt, sns, pd, eda
    # 1:
    # Correlation matrix (Pearson correlation)
    def plotCorr(df):
      plt.figure(figsize=(14,12))
      plt.title('Pearson Correlation of Features', y=1.05, size=15)
      sns.heatmap(df.astype(float).corr(),linewidths=0.1,vmax=1.0, 
                  square=True, linecolor='white', annot=True)						
      plt.show()                
      
    ###   
    
    # 2:
    # Focused correlation matrix
    #   Apply some condition on the correlation of the cols over the target col
    #   before plotting it
    corr = df.corr()
    mask = (corr["targetCol"] > 0.4) + (corr["targetCol"] < -0.4)
    selectedCols = corr.loc[mask].index.values
    plotCorr(df[selectedCols])
    
    ###				
    
    # 3:
    # Joint plot (image in comments)
    sns.jointplot(x='col1', y='col2', data=df,
                   kind='reg', space=0, size=5, ratio=4)
                   
    ###
    
    # Grouped bar plot
    # Perform discrete histogram on column x and group by coulmn on hue
    sns.countplot(x="histCol", hue="groupCol", data=df)
    
    ###
    
    # Faceted boxplot
    # As many boxplots of "y" as values has "x"
    sns.boxplot(x="discreteCol", y="continuousCol", data=df)
    						
    ###
    
    # Distplot with 2nd dim as hue (image in comments)
    #   We use FacetGrid to encode the hue beacuse distplot doesn't have it
    #   (last time I checked anyway)
    g = sns.FacetGrid(wines, hue='hueCol')
    g.map(sns.distplot, 'histCol', kde=False, bins=15)

    public by eduros93 modified Mar 1, 2019  33  0  1  0

    Feature engineering with Pandas

    preprocessing, eda, pd
    # Length of str
    train['Name_length'] = train['Name'].apply(len)
    
    # create binary variable
    train['Has_Cabin'] = train["Cabin"].apply(lambda x: 0 if type(x) == float else 1)
    
    # create binary var using filters
    dataset['IsAlone'] = 0
        dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
    
    # fill na's with median or other statistic
    dataset['Fare'] = dataset['Fare'].fillna(train['Fare'].median())
    
    # divide variable into quantiles
    train['CategoricalFare'] = pd.qcut(train['Fare'], 4)
    
    # divide variable into equal-range bins
    train['CategoricalAge'] = pd.cut(train['Age'], 5)
    
    # write a function that receives a value from a series and outputs the value of  a
    # new feature
     dataset['Title'] = dataset['Name'].apply(get_title)
    
    # replace problematic values
    dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
    
    # replace in batch with a mapping
    dataset['Sex'] = dataset['Sex'].map( {'female': 0, 'male': 1} ).astype(int)
    
    # assign a value to a whole filtered selection
    dataset.loc[(dataset['Fare'] > 7.91) & (dataset['Fare'] <= 14.454), 'Fare'] = 1
    
    # batch replacing in a column
    dataset['Title'] = dataset['Title'].replace(weirdTitlesList, 'Rare')
    
    # fill nan's with specific guesses for subgroups
    dataset.loc[ (dataset.Age.isnull()) & (dataset.Sex == i) 
                  & (dataset.Pclass == j),'Age'] = myGuess
    
    

    public by DunyaBernardon modified Dec 31, 2018  101  0  3  -1

    py4E

    Learning to code
    // Enter here the actual content of the snippet.						
    print("hello world")
    hello world
    
    
    
    
                

    public by bmooers modified Feb 22, 2018  758  1  4  0

    roundview.py for PyMOL

    rounds off the viewport settings in PyMOL to 2 decimals places and returns on one line.
    // Enter here the actual content of the snippet.						
    
    from __future__ import division
    from __future__ import print_function
    # -*- coding: utf-8 -*-
    
    """
    version 1.0         26 October 2015
        Posted in github for first time.
    version 1.1         23 November 2015
        Corrected description of the rounding off the matrix elements.
        Corrected hard wrapped text the broke the script. 
        Added example of running program as a horizontal script.
        Made code pep8 compliant (changed use of blank lines, 
            removed whitespaces in defualt arguments assignments, 
            inserted whitespaces after commas in lists, 
            removed whitespaces at the ends of lines).
        Added version number.
    version 1.2         23 May 2016
        Edited copyright notice.
        Corrected typos
        
    version 1.3         23 July 2016
        Added missing parenthesis at end of file.
        
       Copyright Notice
      ================
      
         Copyright (C) 2016  Blaine Mooers
        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation, either version 3 of the License.
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
        See the GNU General Public License for more details:
        http://www.gnu.org/licenses/.
      The source code in this file is copyrighted, but you can
      freely use and copy it as long as you don't change or remove any of
      the copyright notices.
      
      Blaine Mooers, PhD 
      blaine-mooers@ouhsc.edu
      975 NE 10th St, BRC 466
      University of Oklahoma Health Sciences Center, 
      Oklahoma City, OK, USA 73104
     """
    from pymol import stored, cmd
    __author__ = "Blaine Mooers"
    __copyright__ = "Blaine Mooers, University of Oklahoma Health Sciences Center, Oklahoma City, OK, USA 73104"
    __license__ = "GPL-3"
    __version__ = "1.0.2"
    __credits__ = ["William Beasley","Chiedza Kanyumbu"] 
    # people who reported bug fixes, made suggestions, etc. 
    __date__ = "30 May 2016"
    __maintainer__ = "Blaine Mooers"
    __email__ = "blaine-mooers@ouhsc.edu"
    __status__ = "Production" 
    
    
    def roundview(StoredView=0, decimal_places=2, outname="roundedview.txt"):
    
        """
        DESCRIPTION
        Adds the command "roundview" that gets a view (default is 0,
        the current view; you can get a stored view assigned to some
        other digit with the view command) and rounds to two decimal
        places (two digits to the right of the decimal point) the
        viewpoint matrix elements and rewrites the matrix elements
        on a single line with no whitespaces and a semicolon at the
        end. The saved space eases the making of a single line of
        PyMOL commands separated by semicolons. This enables rapid
        and interactive editing of chunks of PyMOL commands. The
        viewpoints are appended to the bottom of a text file in the
        present working directory called "roundedview.txt". The line
        could be easier to copy from this file than from the command
        history window in the external gui. A semicolon with nothing
        to the right of it at the end of a line of grouped commands
        is harmless.
        USAGE
         
        roundview [view, decimal_places, outname] 
        Note that the values in the [] are optional.
        The default values  for the arguments of the function
        are "0,2, roundedview.txt". 
        Simple one-line example with roundview.py script in current working
        directory--check by typing 'pwd' and 'ls *.py' on the command line. PyMOL
        should return 'roundview.py' in the lisf of files in the external (top) gui.
        Next, paste the following command on the external (top) commandline, hit
        return, and wait 5-10 seconds:
        fetch 1lw9, async=0; run roundview.py; roundview 0,1
        The following view setting will be returned without the blackslash.
        set_view (1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0,-155.2,35.1,11.5,9.7,122.3,188.0,-20.0);
        Advanced option:
        Copy roundview.py to the folder ~/.pymol/startup and then
        the command will always be accessible. You may have to 
        create these directories. 
        18 elements of the view matrix (0-17)
        0 - 8 = column-major 3x3 matrix that rotates the model axes
        to camera axes 
        9 - 11 = origin of rotation relative to the camera
        in camera space
        12 - 14 = origin of rotation in model space
        15 = front plane distance from the camera
        16 = rear plane distance from the camera
        17 = orthoscopic flag 
        (not implemented in older versions)
        
        """
        
        #convert the commandline arguments from strings to integers
    
        StoredView = int(StoredView)
        decimal_places = int(decimal_places)
        
        
        #call the get_view function
    
        m = cmd.get_view(StoredView)
    
    
        #Make a list of the elements in the orientation matrix.
    
        myList = [m[0], m[1], m[2], m[3], m[4], m[5], m[6],
            m[7], m[8], m[9], m[10], m[11], m[12], m[13], m[14],
            m[15], m[16], m[17]]
    
    
        #Round off the matrix elements to two decimal places (two fractional places)
        #This rounding approach solved the problem of unwanted
        #whitespaces when I tried using a string format statement
    
        myRoundedList = [ round(elem, decimal_places) for elem in myList]
        
        
        #x is the format of the output. The whitespace is required
        #between the "set_view" and "(".
        
        x = 'set_view ({0},{1},{2},{3},{4},{5},{6},{7},\
    {8},{9},{10},{11},{12},{13},{14},{15},{16},{17});'
    
    
        #print to the external gui.
    
        print x.format(*myRoundedList)
    
    
        #Write to a text file.
    
        myFile = open("roundedview.txt", "a")
        myFile.write(x.format(*myRoundedList) + "\n")
        myFile.close()
        return
    
    
        #The extend command makes roundview into a PyMOL command.
    
    cmd.extend("roundview", roundview)

    public by Tech_JA modified Oct 26, 2017  546  1  4  0

    config-highlight.cfg

    https://gist.github.com/dsosby/1122904
    [Obsidian]
    definition-foreground = #678CB1
    error-foreground = #FF0000
    string-background = #293134
    keyword-foreground = #93C763
    normal-foreground = #E0E2E4
    comment-background = #293134
    hit-foreground = #E0E2E4
    builtin-background = #293134
    stdout-foreground = #678CB1
    cursor-foreground = #E0E2E4
    break-background = #293134
    comment-foreground = #66747B
    hilite-background = #2F393C
    hilite-foreground = #E0E2E4
    definition-background = #293134
    stderr-background = #293134
    hit-background = #000000
    console-foreground = #E0E2E4
    normal-background = #293134
    builtin-foreground = #E0E2E4
    stdout-background = #293134
    console-background = #293134
    stderr-foreground = #FB0000
    keyword-background = #293134
    string-foreground = #EC7600
    break-foreground = #E0E2E4
    error-background = #293134						

    public by alejo8591 modified Oct 10, 2017  357  0  3  0

    Python Selenium get cookie value

    Python Selenium get cookie value: scrape.py
    import time
    from selenium import webdriver
    
    driver = webdriver.Chrome('./chromedriver')
    driver.get('https://ui.lkqd.com/login')
    assert 'LKQD' in driver.title
    
    time.sleep(2)
    username_field = driver.find_element_by_name('username')
    username_field.send_keys('myusername')
    
    password_field = driver.find_element_by_name('password')
    password_field.send_keys('mypassword')
    
    submit_button = driver.find_elements_by_xpath("//*[contains(text(), 'Sign In')]")[0]
    submit_button.click()
    time.sleep(2)
    
    cookies_list = driver.get_cookies()
    cookies_dict = {}
    for cookie in cookies_list:
        cookies_dict[cookie['name']] = cookie['value']
    
    session_id = cookies_dict.get('session')
    print(session_id)
    
    driver.quit()
    
    
    

    public by Nick Moore modified Aug 30, 2017  556  0  3  0

    The block structure for SnakeCoin.

    The block structure for SnakeCoin.: snakecoin-block.py
    import hashlib as hasher
    
    class Block:
      def __init__(self, index, timestamp, data, previous_hash):
        self.index = index
        self.timestamp = timestamp
        self.data = data
        self.previous_hash = previous_hash
        self.hash = self.hash_block()
      
      def hash_block(self):
        sha = hasher.sha256()
        sha.update(str(self.index) + 
                   str(self.timestamp) + 
                   str(self.data) + 
                   str(self.previous_hash))
        return sha.hexdigest()
    
    
    

    public by lmontealegre modified Aug 22, 2017  597  1  4  0

    CONSTANT Definitions for Database server and SQL scripts

    Standard for defining connections and SQL definitions
    #=================================================================
    # Database Server Connection String
    # Database = AGENTS  Server = <IP>
    #=================================================================
    CNN_AGENTS = {'Server':'<IP>', 'User':'<Name>', 'Password':'<Pwd>', 'Database':'<DBName>'}
    
    #=================================================================
    # SQL Script Definition to Get Gran Total Agent counts
    # Database = AGENTS  Server = <IP>
    #=================================================================
    SQL_WR0TOTALCNTS = '''
    Select
      Count(SOL.SOL_KEY) As NoRec,
      Count(Distinct SOL.AGT_SOL_NO) As CntSOL,
      Sum(SOL.AGT_SOL_NOFILE) As SumSOL_NF,
      Sum(SOL.AGT_SOL_DWLERR) As SumSOL_DWLERR,
      Sum(SOL.AGT_SOL_OK) As SumSOL_OK,
      Sum(SOL.AGT_SOL_DWLOK) As SumSOL_DWLOK,
      Min(Cast(SOL.AGT_DTE_POSTDATE As datetime)) As Min_POSTDATE,
      Max(Cast(SOL.AGT_DTE_POSTDATE As datetime)) As Max_POSTDATE,
      DateDiff(day, Min(Cast(SOL.AGT_DTE_POSTDATE As datetime)), Max(Cast(SOL.AGT_DTE_POSTDATE As datetime))) As DateRange,
      Count(Distinct Cast(SOL.AGT_DTE_POSTDATE As datetime)) As ProcDates,
      Sum(Case When Len(IsNull(SOL.AGT_SOL_DWLPATH, '')) > 0 Then 1 Else 0
      End) As DwlOK,
      Sum((Convert(bigint,SOL.AGT_SOL_DWLBYTE)) / (1048576)) As MB,
      Count(Distinct SOL.AGT_DTE_EVT) As Cnt_RAN_EVT
    From
      AGENTS.dbo.DIBBS_AGN_SOL SOL
    '''
    
    #=================================================================
    # SQL Script Definition to Get Gran Total Agent counts
    # Database = AGENTS  Server = <IP> To be designed
    #=================================================================
    SQL_SelectedDates = '''
    Select
      Count(SOL.SOL_KEY) As NoRec,
      Count(Distinct SOL.AGT_SOL_NO) As CntSOL,
      Sum(SOL.AGT_SOL_NOFILE) As SumSOL_NF,
      Sum(SOL.AGT_SOL_DWLERR) As SumSOL_DWLERR,
      Sum(SOL.AGT_SOL_OK) As SumSOL_OK,
      Sum(SOL.AGT_SOL_DWLOK) As SumSOL_DWLOK,
      Min(Cast(SOL.AGT_DTE_POSTDATE As datetime)) As Min_POSTDATE,
      Max(Cast(SOL.AGT_DTE_POSTDATE As datetime)) As Max_POSTDATE,
      DateDiff(day, Min(Cast(SOL.AGT_DTE_POSTDATE As datetime)), Max(Cast(SOL.AGT_DTE_POSTDATE As datetime))) As DateRange,
      Count(Distinct Cast(SOL.AGT_DTE_POSTDATE As datetime)) As ProcDates,
      Sum(Case When Len(IsNull(SOL.AGT_SOL_DWLPATH, '')) > 0 Then 1 Else 0
      End) As DwlOK,
      Sum((Convert(bigint,SOL.AGT_SOL_DWLBYTE)) / (1048576)) As MB,
      Count(Distinct SOL.AGT_DTE_EVT) As Cnt_RAN_EVT
    From
      AGENTS.dbo.DIBBS_AGN_SOL SOL
    '''
    • Public Snippets
    • Channels Snippets