app.py 4.77 KB
Newer Older
Arman Khalatyan's avatar
Arman Khalatyan committed
1 2
import streamlit as st
import pandas as pd
Arman Khalatyan's avatar
Arman Khalatyan committed
3 4 5 6 7 8
import numpy as np
import matplotlib.pyplot as plt
import pyvo as vo
import pandas as pd
import matplotlib as mpl

Arman Khalatyan's avatar
Arman Khalatyan committed
9 10 11
import plotly.express as px
import seaborn as sns
from scipy.stats import norm
Arman Khalatyan's avatar
Arman Khalatyan committed
12 13

@st.cache
Arman Khalatyan's avatar
Arman Khalatyan committed
14
def get_data_sql(Nstars=1000):    
Arman Khalatyan's avatar
Arman Khalatyan committed
15 16 17 18 19 20 21
    # init tap service
    tap_service = vo.dal.TAPService('https://gaia.aip.de/tap')
    #vo.utils.http.session.headers['Authorization'] = 'Your Token '
    
    # manage the jobs yourself
    jobs = []
    
Arman Khalatyan's avatar
Arman Khalatyan committed
22 23
    limit = Nstars
    total = 2*Nstars
Arman Khalatyan's avatar
Arman Khalatyan committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    qstr="select  source_id,xgal,ygal,zgal,rgal,ruwe,mg0,bprp0  from gdr2_contrib.starhorse as s where s.SH_OUTFLAG LIKE '00000' AND s.SH_GAIAFLAG LIKE '000' "
    i=0
    for offset in range(0, total, limit):
        sql = qstr +' LIMIT %s OFFSET %s' % (limit, offset)
        print(sql)
        job =     tap_service.submit_job(sql, language='postgresql', runid='batch'+str(i))
        job.run()
        jobs.append(job)
        i=i+1
    
    i=0
    dfvec=[]
    # collect the results
    for job in jobs:
        print('getting results:',i)
        job.raise_if_error()
        results = job.fetch_result()
        dfvec.append(results.to_table().to_pandas())
        i=i+1
    i=0
    
    print('combining...')
    df=pd.concat(dfvec)
    return df
Arman Khalatyan's avatar
Arman Khalatyan committed
48
    
Arman Khalatyan's avatar
Arman Khalatyan committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

# To make things easier later, we're also importing numpy and pandas for
# working with sample data.


cols=[
    'dist05', 'dist16', 'dist50', 'dist84', 'dist95',
    'av05','av16', 'av50', 'av84', 'av95',
    'teff16', 'teff50', 'teff84',
    'logg16','logg50', 'logg84',
    'met16', 'met50', 'met84',
    'mass16', 'mass50','mass84',
    'ag50', 'abp50', 'arp50',
    'mg0', 'bprp0',
    'xgal', 'ygal',
    'zgal', 'rgal',
    'ruwe'
]

map_cols=[
    #'dist05', 'dist16', 
    'dist50', 
    #'dist84', 'dist95', 
    #'av05','av16', 
    #'av50', 
    #'av84', 'av95', 
   # 'teff16', 
    'teff50', 
   # 'teff84', 
   # 'logg16',
    'logg50',
   # 'logg84', 
   # 'met16', 
    'met50',
   # 'met84', 
   # 'mass16', 
    'mass50',
   # 'mass84', 
  #  'ag50', 
  #  'abp50', 
  #  'arp50', 
    'mg0', 'bprp0',
    'xgal', 'ygal','zgal', 
  #  'rgal', 
   # 'ruwe'
]

#suppress_st_warning=True)

@st.cache
def gendata(Nstars=1000):
    dfsh=pd.read_parquet(
        "~/data/sh_tmp.parq",columns=cols).head(Nstars)#.sample(frac=0.02,random_state=42)
    selection=dfsh.dist50<10
    df = dfsh[selection]#pd.DataFrame(
         #{"x":dfsh.xgal,"y":dfsh.ygal,"y":dfsh.ygal})
    dflen=len(df)
    df=genmap(df)
    return df


def plot_figure_matplotlib(df, xname,yname,colorname):
    plt.scatter(
        df[xname].values, df[yname].values,
        c=df[colorname], s=0.3, cmap='Spectral', alpha=1.0)
    st.pyplot()

def plot_figure_plotly(df,xname,yname):
    #df = gendata()
    fig = px.scatter(df, x=xname, y=yname, title="title")
    st.plotly_chart(fig)
   


## Main rendering part


Arman Khalatyan's avatar
Arman Khalatyan committed
126
st.title('Testing the interactive  maps')
Arman Khalatyan's avatar
Arman Khalatyan committed
127 128

st.write('## selected stars parameters')
Arman Khalatyan's avatar
Arman Khalatyan committed
129
N = st.slider("Pick a number", 1000, 100000, 10000,10000)
Arman Khalatyan's avatar
Arman Khalatyan committed
130 131
df = get_data_sql(N)
map_cols=['xgal','ygal','zgal','rgal','ruwe','mg0','bprp0']
Arman Khalatyan's avatar
Arman Khalatyan committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
Nstars=len(df)
st.markdown(f"N = **{Nstars}** stars selected by condition in **gendata()** function")

st.sidebar.markdown('## selected projection parameters')

st.sidebar.markdown('## Plotting parameters')
xname = st.sidebar.selectbox(
    'Select the X axis',
     map_cols,6)
yname = st.sidebar.selectbox(
    'Select the Y axis',
     map_cols,5)
colorname = st.sidebar.selectbox(
    'Select the Color axis',
     map_cols,3)



if st.checkbox('Show Scatter plot',value=False):
    #plot_figure_plotly(df,xname,yname)
    plot_figure_matplotlib(df, xname,yname,colorname)

if st.checkbox('Show pairwise relationships in a dataset'):
    sns.pairplot(df[[xname,yname,colorname]])#, hue="a");
    st.pyplot()

if st.checkbox('Warning: the KDE plots are very slow'):
    g = sns.PairGrid(df[[xname,yname,colorname]])
    g.map_diag(sns.kdeplot)
    g.map_offdiag(sns.kdeplot, n_levels=8);
    st.pyplot()

Arman Khalatyan's avatar
Arman Khalatyan committed
164 165 166 167
if st.checkbox('Warning: the interactive can be very slow'):
    plot_figure_plotly(df,xname,yname)
    st.pyplot()

Arman Khalatyan's avatar
Arman Khalatyan committed
168 169 170 171 172


import holoviews as hv
from holoviews import opts
from holoviews import dim
Arman Khalatyan's avatar
Arman Khalatyan committed
173
from holoviews.operation.datashader import datashade, shade, dynspread, rasterize
Arman Khalatyan's avatar
Arman Khalatyan committed
174 175 176 177
hv.extension('bokeh')



Arman Khalatyan's avatar
Arman Khalatyan committed
178
if st.checkbox( "interactive holoview plot"):
Arman Khalatyan's avatar
Arman Khalatyan committed
179
    #plot_figure_plotly(df,xname,yname)
Arman Khalatyan's avatar
Arman Khalatyan committed
180 181
    hex_tiles = hv.HexTiles(df[[xname,yname]])
    hex_tiles.opts(opts.HexTiles(width=500, height=400, tools=['hover'], colorbar=True))
Arman Khalatyan's avatar
Arman Khalatyan committed
182 183
    st.bokeh_chart(hv.render(hex_tiles, backend='bokeh'))

Arman Khalatyan's avatar
Arman Khalatyan committed
184 185 186 187 188
if st.checkbox( "interactive scatter plot"):
    #plot_figure_plotly(df,xname,yname)
    points = hv.Points(df[[xname,yname]])
   # hex_tiles.opts(opts.HexTiles(width=500, height=400, tools=['hover'], colorbar=True))
    st.bokeh_chart(hv.render(points, backend='bokeh'))