Exemple #1
0
    def build_pipelines(self):
        st.write("# Pipelines")

        st.write("This example illustrates how AutoGOAL automatically builds "
                 "a graph of pipelines for different problems settings.")

        from autogoal.kb._data import DATA_TYPES

        types_str = [cls.__name__ for cls in DATA_TYPES]

        st.write("""
            AutoGOAL pipeline discovery is based on a hierarchy of semantic datatypes.
            Each type represents a semantic datum that can be used in a machine learning algorithm,
            from matrices and vectors to sentences, entities and and images.

            The following picture shows all available semantic data types.
            You can click the top right corner to enlarge.
            """)

        st.image("/code/docs/guide/datatypes.png", use_column_width=True)

        from autogoal.contrib import find_classes

        all_classes = {k.__name__: k for k in find_classes()}

        st.write(f"""
            ## Algorithm Library

            AutoGOAL automatically builds pipelines by selecting from a wide range of algorithms
            implemented in `contrib` modules.
            The list of all available algorithms is shown here.

            There are a total of **{len(all_classes)}** algorithms implemented.
            Select one to display some information.
            """)

        class_name = st.selectbox("Select an algorithm", list(all_classes))
        class_type = all_classes[class_name]

        st.write(f"### {class_type.__module__}.{class_name}")

        run_signature = inspect.signature(class_type.run)
        st.write(
            f"**Input type**: {run_signature.parameters['input'].annotation}")
        st.write(f"**Output type**: {run_signature.return_annotation}")

        st.write("#### Parameters")
        params = []
        for name, param in inspect.signature(
                class_type.__init__).parameters.items():
            if name == 'self':
                continue

            params.append(f"* **{name}**: {param.annotation}")
        st.write("\n".join(params))

        st.write("## Pipeline Builder")

        st.write("""
            AutoGOAL can automatically build pipelines given a desired input and output
            value. It uses the annotations of the `run` method of each algorithm to detect
            which algorithms can be connected.

            In the following section, you can select a desired input and output types and 
            explore the pipelines that AutoGOAL discovers.
            In the left sidebar you can fine-tune the input value, e.g., make it a list
            of elements instead of a single element.
            """)

        st.sidebar.markdown("### Configure input and output types")
        list_input = st.sidebar.number_input("Input list (level)", 0, 3, 1)
        list_output = st.sidebar.number_input("Output list (level)", 0, 3, 0)
        tuples = st.sidebar.checkbox("Is supervised (use Tuple in input)",
                                     True)

        input_type = st.selectbox("Select an input type", types_str,
                                  types_str.index('Sentence'))

        output_type = st.selectbox("Select and output type", types_str,
                                   types_str.index('CategoricalVector'))

        input_type = input_type + "()"
        for i in range(list_input):
            input_type = f"List({input_type})"

        output_type = output_type + "()"
        for i in range(list_output):
            input_type = f"List({output_type})"

        if tuples:
            input_type = f"Tuple({input_type}, {output_type})"

        st.write(f"#### Defined input type:  `{input_type}`")
        st.write(f"#### Defined output type: `{output_type}`")

        st.write("""
            The following code uses explicitely AutoGOAL's pipeline discovery engine
            to find all the pipelines that can be constructed from the desired
            input to the desired output.
            """)

        code = textwrap.dedent(f"""
            from autogoal.kb import *
            from autogoal.kb import build_pipelines
            from autogoal.contrib import find_classes

            # explicitly build the graph of pipelines
            space = build_pipelines(
                input={input_type},
                output={output_type},
                registry=find_classes(),
            )
            """)

        st.code(code)

        try:
            space = eval_code(code, "space")
        except Exception as e:
            if "No pipelines can be constructed" in str(e):
                st.error(str(e))
                st.info(
                    "Try changing the input and output type or select **Is supervised** in the left sidebar."
                )
                return

            raise

        st.write("""
            ### The Pipelines Graph
            
            This is the graph that represents all the posible pipelines find by AutoGOAL.
            Each node in this graph is an algorithm from the _Algorithm Library_ that is
            compatible with the input and output types of its neighbors.
            Any path from the top to the bottom of the graph represents a valid pipeline.
            """)

        graph = nx.DiGraph()

        def get_node_repr(node):
            try:
                return get_node_repr(node.inner)
            except:
                return dict(label=str(node).split(".")[-1],
                            module=node.__module__.split("_")[0])

        for node in space.graph.nodes:
            attrs = get_node_repr(node)
            graph.add_node(attrs["label"], **attrs)

        for u, v in space.graph.edges:
            graph.add_edge(
                get_node_repr(u)["label"],
                get_node_repr(v)["label"])

        pos = nx.nx_pydot.pydot_layout(graph, prog="dot", root=space.Start)
        chart = (nxa.draw_networkx(
            graph, pos=pos, node_color="module",
            node_tooltip="label").properties(height=500).interactive())

        st.altair_chart(chart, use_container_width=True)

        st.write("""
            ### Example Pipeline
            
            Here is an example pipeline that has been randomly sampled from the previous graph.
            You can try different samples. Notice how not only the nodes (algorithms) that participate
            in the pipeline are different each time, but also their internal hyperparameters change.
            
            When sampling a pipeline from the graph AutoGOAL samples all the internal
            hyperparameters as defined by the constructor.
            When these hyperparameters have complex values (e.g., an algorithm per-se), AutoGOAL
            recursively samples instances of the internal algorithms, and so on.
            """)

        st.code(space.sample())

        st.button("Sample another pipeline")
Exemple #2
0
r = pdk.Deck(
    scatter_layer, initial_view_state=view,
    map_style="mapbox://styles/mapbox/light-v9",
    views=("MapView", False),
    tooltip=tooltip
)
st.pydeck_chart(r)
df

#default_sounding = str(df.loc[df.index[0], 'sounding_id'])
default_sounding = str(df.iloc[df['delta'].idxmax()]['sounding_id'])
sounding_id = st.text_input("Enter the sounding ID here", default_sounding)
df_detail = pd.read_json(config['swift_storage']['base_url']+'/datasets/oco-2/peaks-detected-details/peak_data-si_'+sounding_id+'.json')

gaussian_param = get_gaussian_param(sounding_id, df)

df_detail['gaussian_out'] = df_detail.apply(
    lambda dataset: gaussian(dataset['distance'], m=gaussian_param['slope'], b=gaussian_param['intercept'], A=gaussian_param['amplitude'], sig=gaussian_param['sigma'])
    , axis=1)

altair_graph = alt.Chart(df_detail).encode(x='distance')

scatter = altair_graph.mark_circle().encode(
        y=alt.Y('xco2', scale=alt.Scale(zero=False)),
        color='xco2', tooltip=['xco2']
    )
line =  altair_graph.mark_line(color='red').encode(
    y='gaussian_out'
)
st.altair_chart(scatter+line, use_container_width=True)
def main():
    st.write("""
    # Streamlit: EKA Goals Modelling App

    Statistical Estimations of Goals Scored in the England Korfball League Since 2014/15 Season"""
             )

    st.sidebar.header('Dataset Filters')
    dataset = st.sidebar.selectbox("Select Dataset", datasets)

    df = pd.read_csv(data_sources[dataset])

    seasons = st.sidebar.multiselect("Select Seasons", df.Season.unique())
    teams = st.sidebar.multiselect("Select Teams",
                                   df['Home Team'].sort_values().unique())
    venue = st.sidebar.multiselect("Select Venue", venues)
    global goals
    goals = filter_scores(df, teams, seasons, venue)['Goals']

    st.sidebar.header('Histogram')

    bin_size = st.sidebar.number_input('Bin Size', 1, 10, 1)
    x_max = goals.max() + 1

    if goals.size == 0:
        st.warning('No Data. Please update dataset filters.')
    else:

        global x
        x = np.arange(0, x_max, bin_size)
        global x_arr
        x_arr = np.arange(0, x_max + bin_size, bin_size)
        global y
        y = np.histogram(goals, bins=x_arr, density=True)[0]

        st.write(f"""
        **Observations: ** {goals.count()}

        **Mean: ** {goals.mean():.2f}
        **Var: ** {goals.var():.2f}
        **Std: ** {goals.std():.2f}
        **Skew: ** {goals.skew():.2f}
        **Kurtosis: ** {goals.kurtosis():.2f}""")

        st.sidebar.header('Distribution Parameters')

        dist_name = st.sidebar.selectbox("Select Distribution", distributions)

        params = add_parameter_ui(dist_name, y)

        st.sidebar.markdown('#### Formula')
        formula = st.sidebar.markdown(get_formula(dist_name))

        dist = get_dist_data(dist_name, params)

        # graph = plot_data(goals, dist, dist_name, params)

        graph = st.altair_chart(plot_altair(y, dist, dist_name, bin_size))

        g_data, dist_data = y, dist[:-1]

        mse = metrics.mean_squared_error(g_data, dist_data)
        rmse = mse**.5
        e_var = metrics.explained_variance_score(g_data, dist_data)
        max_error = metrics.max_error(g_data, dist_data)
        mae = metrics.mean_absolute_error(g_data, dist_data)
        # r2_score = metrics.r2_score(g_data, dist_data)
        sum_square_errors = sse(g_data, dist_data)
        total_sum_square = sst(g_data)

        st.write(f"""
        **MSE: ** {mse * 100 :.5f}
        **RMSE: ** {rmse : .5f}

        **Max Error:** {max_error: .5f}
        **MAE: ** {mae: .5f}
        **SSE: ** {sum_square_errors: .5f}
        **SST: ** {total_sum_square: .5f}

        **Explained Variance: ** {e_var:.3f}""")
Exemple #4
0
    total_data, daily_data = read_national_data(url_casos_chile, "original")

    if data_select == "Casos Totales":
        data = total_data
    else:
        data = daily_data

    regiones = list(total_data["region_title"].unique())
    regiones = st.multiselect(
        "Seleccione las regiones que desea incluir en el gráfico:",
        options=regiones,
        default=regiones)

    global_graph = get_global_chart(data, regiones, "infectados", scale,
                                    "Fecha")
    st.altair_chart(global_graph)

    regional_graph = get_regional_chart(data, regiones, "infectados", scale,
                                        "Fecha", "Región")
    st.altair_chart(regional_graph)

    proportion_graph = get_regional_proportion_chart(data, regiones,
                                                     "infectados", scale,
                                                     "Fecha", "Región")
    st.altair_chart(proportion_graph)

    date_select = st.date_input("Selecciona el día que deseas inspeccionar:",
                                value=default_date,
                                key="national_date")

    map_national_graph = generate_regions_map(data, date_select, "infectados",
Exemple #5
0
import altair as alt
import matplotlib.pyplot as plt
import pandas as pd
import plotly.express as px
import seaborn as sns
import streamlit as st

path = "data/titanic.csv"
df = pd.read_csv(path)

chart = alt.Chart(df).mark_bar().encode(alt.X("Age:Q", bin=True), y="count()")
st.altair_chart(chart)

fig, ax = plt.subplots()
df.hist(
    bins=8,
    column="Age",
    grid=False,
    figsize=(8, 8),
    color="#86bf91",
    zorder=2,
    rwidth=0.9,
    ax=ax,
)
st.pyplot(fig)

fig, ax = plt.subplots()
sns.distplot(df["Age"], ax=ax)
st.pyplot(fig)

fig = px.histogram(df, x="Age")
Exemple #6
0
st.subheader('Here is 1 empty map')
st.deck_gl_chart()

# TODO: Implement add_rows on DeckGL
# st.subheader('1 filled map')
# x = st.deck_gl_chart()
# x.add_rows({'lat': 0, 'lon': 0})

st.subheader('Here are 10 errors')
st.write(1)
st.vega_lite_chart({})
st.write(2)
st.vega_lite_chart(data, {})
st.write(3)
st.vega_lite_chart(data)
st.write(4)
st.vega_lite_chart()
st.write(5)
st.altair_chart()
st.write(6)
st.line_chart()
st.write(7)
st.area_chart()
st.write(8)
st.bar_chart()
st.write(9)
st._native_chart()
st.write(10)
st.map()
Exemple #7
0
source = g.melt('year', var_name='category', value_name='y')

line = alt.Chart(source).mark_line().encode(
    x='year:O',

    y='y:Q',
     color='category:N',


    tooltip=['category:N','y:Q', 'year:O']
).properties(
    width=700,
    height=400
).interactive()

st.altair_chart(line, use_container_width=True)

st.markdown("""We can  immediately see an upward trend of temperature, even compared to the moving Average
and from 1950 average the temperature is almost 3 Degrees higher!! """)

st.markdown("""Since this data is at State level, we can drill down to  the change in temperatures
across each state""")

g=df_usa.groupby(['year','State'])['AverageTemperature'].mean()
g = pd.DataFrame(g)
g=g.reset_index()
source = g

b=alt.Chart(source).mark_boxplot().encode(
    x='State:O',
    y='AverageTemperature:Q',
if sidebar == "Français":
    select_box_1 = st.selectbox('', ["Mode Production","Mode irrigation","Culture","Irrigation","Serre",
                                       "Superficie Champ","SAU par culture"])

if sidebar == "English":
    select_box_1 = st.selectbox('', ["Production mode","Irrigation mode","Crop","Irrigation ","Greenhouse",
                                       "Field area","UAA by crop"])


# FR

if select_box_1 == "Culture":
    chart = alt.Chart(df).mark_bar().encode(
    alt.X("count()", bin=False),
    y='Culture',color='Culture').properties(width=700, height=500)
    st.altair_chart(chart)
    
elif select_box_1 == "Mode Production":
    chart = alt.Chart(df).mark_bar().encode(
    alt.X("count()", bin=False),
    y='Mode_Production',color='Mode_Production').properties(width=700, height=200)
    st.altair_chart(chart)

elif select_box_1 == "Mode irrigation":
    chart = alt.Chart(df).mark_bar().encode(
    alt.X("count()", bin=False),
    y='Mode_irrigation',color='Mode_irrigation').properties(width=700, height=250)
    st.altair_chart(chart)

elif select_box_1 == "Irrigation":
    chart = alt.Chart(df).mark_bar().encode(
def write():
    with st.spinner("Cargando Elementos Básicos ..."):
        st.title("Elementos Básicos de Streamlit")

######################ELEMENTOS DE TEXTO########################################
    st.header("Elementos de Texto")
    with st.beta_expander('Elementos de encabezado'):
        st.title("Esto es un titulo")
        st.code("""st.title("Esto es un titulo")""")
        st.header('Esto es un encabezado')
        st.code("""st.header('Esto es un encabezado')""")
        st.subheader('Esto es un subencabezado')
        st.code("""st.subheader('Esto es un subencabezado')""")

    with st.beta_expander("Elementos de markdown"):
        st.markdown('''
		# Esto es un título en markdown 
		## Esto es un encabezado
		### Esto es subencabezado

		:+1: :sunglasses: Y esto es una lista:
		- Item 1
		- Item 2

		''')
        st.code("""st.markdown(	
		'''
		# Esto es un título en markdown 
		## Esto es un encabezado
		### Esto es subencabezado

		:+1: :sunglasses: Y esto es una lista:
		- Item 1
		- Item 2

		''')""")

    with st.beta_expander("Elementos de texto simple"):
        st.text('Tenemos un texto de ancho fijo')
        st.code("""st.text('Tenemos un texto de ancho fijo')""")
        st.text('Podemos además insertar listas de objetos:')
        st.write(['st', 'is <', 3])
        st.code("""st.write(['st', 'is <', 3])""")
        st.write("Y podemos tambien insertar ecuaciones de Látex:")
        st.latex(r''' e^{i\pi} + 1 = 0 ''')
        st.code("""st.latex(r''' e^{i\pi} + 1 = 0 ''')""")

######################ELEMENTOS DE DATA########################################
    st.header("Elementos de Data")
    with st.beta_expander("Elementos para mostrar data "):

        st.write(
            "Podemos mostrar la información contenida en un dataframe de Pandas:"
        )
        df = pd.DataFrame(np.random.randn(50, 20),
                          columns=('col %d' % i for i in range(20)))
        st.dataframe(df.style.highlight_max(axis=0))  #resaltamos los máximos
        st.code(
            """df = pd.DataFrame(np.random.randn(50, 20),columns=('col %d' % i for i in range(20)))
st.dataframe(df.style.highlight_max(axis=0)) #seteamo el ancho y largo, resaltamos los máximos"""
        )

        st.write(
            "También podemos mostrar la misma información a través de una tabla simple:"
        )
        st.table(df.iloc[0:10])
        st.code("""st.table(df.iloc[0:10])""")

        st.write(
            "Y finalmente podemos también mostrar la información contenida en formato JSON:"
        )
        st.json({'foo': 'bar', 'fu': 'ba'})
        st.code("""st.json({'foo':'bar','fu':'ba'})""")

######################ELEMENTOS GRÁFICOS########################################
    st.header("Elementos gráficos")
    with st.beta_expander("Elementos de Ploteo"):
        st.write(
            "Podemos hacer gráficas lineales (¡y se puede interactuar con ellos!):"
        )
        chart_data = pd.DataFrame(np.random.randn(20, 3),
                                  columns=['a', 'b', 'c'])
        st.line_chart(chart_data)
        st.code(
            """chart_data = pd.DataFrame(np.random.randn(20, 3),columns=['a', 'b', 'c'])
st.line_chart(chart_data)""")

        st.write("Podemos hacer gráficos de áreas:")
        chart_data = pd.DataFrame(np.random.randn(20, 3),
                                  columns=['a', 'b', 'c'])
        st.area_chart(chart_data)
        st.code(
            """chart_data = pd.DataFrame(np.random.randn(20, 3),columns=['a', 'b', 'c'])
st.area_chart(chart_data)""")

        st.write(" Podemos hacer gráficos de barras:")
        chart_data = pd.DataFrame(np.random.randn(50, 3),
                                  columns=["a", "b", "c"])
        st.bar_chart(chart_data)
        st.code(
            """chart_data = pd.DataFrame(np.random.randn(50, 3),columns=["a", "b", "c"])
st.bar_chart(chart_data)""")

        st.write("Streamlit además tiene integración con Matplotlib")
        arr = np.random.normal(1, 1, size=100)
        fig, ax = plt.subplots()
        ax.hist(arr, bins=20)
        st.pyplot(fig)
        st.code("""arr = np.random.normal(1, 1, size=100)
fig, ax = plt.subplots()
ax.hist(arr, bins=20)
st.pyplot(fig)""")

        st.write("También integración con Altair:")
        df = pd.DataFrame(np.random.randn(200, 3), columns=['a', 'b', 'c'])
        c = alt.Chart(df).mark_circle().encode(x='a',
                                               y='b',
                                               size='c',
                                               color='c',
                                               tooltip=['a', 'b', 'c'])
        st.altair_chart(c, use_container_width=True)
        st.code(
            """df = pd.DataFrame(np.random.randn(200, 3),columns=['a', 'b', 'c'])
c = alt.Chart(df).mark_circle().encode(	x='a', y='b', size='c', color='c', tooltip=['a', 'b', 'c'])
st.altair_chart(c, use_container_width=True)""")

        st.write(
            "Y finalmente puede integrarse además con mapas de OpenStreetMaps:"
        )
        df = pd.DataFrame(
            np.random.randn(500, 2) / [50, 50] + [-33.46, -70.65],
            columns=['lat', 'lon'])  #[-33.50, -70.55],columns=['lat', 'lon'])
        st.map(df)

        st.write(
            "De la misma forma anterior, Streamlit puede integrarse con bibliotecas como Vega-Lite, Plotly, Bokeh, PyDeck, DEck_GL y Graphviz. ¡Los invitamos a testearlos! "
        )
        st.code("""st.vega_lite_chart(data)
st.plotly_chart(data)
st.bokeh_chart(data)
st.pydeck_chart(data)
st.deck_gl_chart(data)
st.graphviz_chart(data)""")

######################ELEMENTOS MULTIMEDIA########################################
    st.header("Elementos Multimedia")
    with st.beta_expander("Elementos de música, audio y video"):
        from PIL import Image
        st.write(
            "En esta sección podemos cargar imágenes directamente desde nuestro PC y agregarle un comentario: "
        )
        image = Image.open(r'resources/sunrise.jpg')
        st.image(
            image,
            caption='Puesto de Sol en las montañas. Ojalá pudiese estar allí. ',
            use_column_width=True)
        st.code("""image = Image.open('sunrise.jpg')
st.image(image, caption='Puesto de Sol en las montañas. Ojalá pudiese estar allí. ',use_column_width=True)"""
                )

        st.write(
            "Podemos además agregar archivos de audio, como el Valse Opus 64 de Chopin (también conocido como el 'vals del perrito'):"
        )
        audio_file = open(r'resources/Chopin-valse-opus64.ogg', 'rb')
        audio_bytes = audio_file.read()
        st.audio(audio_bytes, format='audio/ogg')
        st.code("""audio_file = open('Chopin-valse-opus64.ogg', 'rb')
audio_bytes = audio_file.read()
st.audio(audio_bytes, format='audio/ogg')""")

        st.write(
            """Podemos por otra parte agregar videos directamente desde nuestro PC:"""
        )
        video_file = open(r'resources/Star - 6962.mp4', 'rb')
        video_bytes = video_file.read()
        st.video(video_bytes)
        st.code("""video_file = open('Star - 6962.mp4', 'rb')
video_bytes = video_file.read()
st.video(video_bytes)""")

        st.write("Y finalmente también podemos agregar videos de Youtube:")
        st.video('https://www.youtube.com/watch?v=NUYvbT6vTPs')

######################WIDGETS INTERACTIVOS########################################
    st.header("Widgets Interactivos")
    with st.beta_expander("Widgets"):
        st.write("Dentro de los widgets interactivos podemos incluir botones:")
        if st.button('Digamos hola', key="say_hello"):
            st.write('Respuesta: ¡Hola quien quiera que seas!')
        else:
            st.write('Respuesta: ¡Adios!')
        st.code("""if st.button('Digamos hola',key="say_hello"):
	st.write('Respuesta: ¡Hola quien quiera que seas!')
else:
	st.write('Respuesta: ¡Adios!')""")

        st.write(
            "Tambien tenemos checkbox para marcar alguna preferancia u opción:"
        )
        agree = st.checkbox('Estoy de acuerdo.')
        if agree:
            st.write('¡Genial! Me acabas de vender tu alma :).')
            st.code("""agree = st.checkbox('Estoy de acuerdo.')
if agree:
	st.write('¡Genial! Me acabas de vender tu alma :).')""")

        st.write("O también radio buttons para marcar entre varias opciones:")
        ingrediente = st.radio("¿Que ingrediente prefieres en la pizza?",
                               ('Tocino', 'Piña', 'Choricillo'))
        if ingrediente == 'Tocino':
            st.write('¡Cuidado con el colesterol!.')
        elif ingrediente == 'Piña':
            st.write(
                '¡¿En serio?! ¿No me digas que también le echas Coca-Cola al vino?'
            )
        else:
            st.write("Buena elección, padawan.")
        st.code(
            """ingrediente = st.radio("¿Que ingrediente prefieres en la pizza?",('Tocino', 'Piña', 'Choricillo'))
if ingrediente == 'Tocino':
	st.write('¡Cuidado con el colesterol!.')
elif ingrediente=='Piña':
	st.write('¡¿En serio?! ¿No me digas que también le echas Coca-Cola al vino?')
else:
	st.write("Buena elección, padawan.")""")

        st.write(
            "Las selectboxes te permiten seleccionar una opción dentro de una lista desplegable:"
        )
        opcion = st.selectbox('¿Cómo quieres ser contactado?',
                              ('Email', 'Teléfono de casa', 'Celular'))
        st.write('Seleccionaste:', opcion)
        st.code(
            """	st.write("Las selectboxes te permiten seleccionar más de una opción:")
opcion = st.selectbox('¿Cómo quieres ser contactado?',('Email', 'Teléfono de casa', 'Celular'))
st.write('Seleccionaste:', opcion)""")

        st.write("Existe la opción de elegir múltiples alternativas:")
        opciones = st.multiselect('¿Cuáles son tus colores favoritos?',
                                  ['Green', 'Yellow', 'Red', 'Blue'],
                                  ['Yellow', 'Red'])
        st.write('You selected:', opciones)
        st.code(
            """	opciones= st.multiselect('¿Cuáles son tus colores favoritos?',['Green', 'Yellow', 'Red', 'Blue'],	['Yellow', 'Red'])
st.write('You selected:', opciones)""")

        st.write(
            "Con el widget 'Slider' puedes elegir un valor,un rango de valores o incluso fechas:"
        )
        x = st.slider("Valor de x:",
                      min_value=1,
                      max_value=100,
                      value=5,
                      step=1,
                      format=None,
                      key=None)
        st.write('Su cuadrado es:', x * x)
        valores = st.slider('Selecciona un rango de valores:', 0.0, 100.0,
                            (25.0, 75.0))
        st.write('Valores:', valores)
        cita = st.slider("Agenda tu cita:", value=(time(11, 00), time(12, 45)))
        st.write("Agendaste tu cita para entre las ",
                 cita[0].strftime("%H:%M"), " y las ",
                 cita[1].strftime("%H:%M"))
        st.code(
            """x= st.slider("Valor de x:", min_value=1, max_value=100, value=5, step=1, format=None, key=None)
st.write('Su cuadrado es:', x * x)
valores = st.slider('Selecciona un rango de valores:',	0.0, 100.0, (25.0, 75.0))
st.write('Valores:', valores)
cita = st.slider("Agenda tu cita:",value=(time(11, 00), time(12, 45)))
st.write("Agendaste tu cita para entre las ", cita[0].strftime("%H:%M")," y las ",cita[1].strftime("%H:%M"))"""
        )

        st.write("Además de lo anterior, podemos también ingresar texto:")
        title_text = st.text_input('Ingresa tu película favorita:',
                                   'Iron Man 3')
        st.write('Tu película favorita es ', title_text)
        st.code(
            """title_text = st.text_input('Ingresa tu película favorita:', 'Iron Man 3')
st.write('Tu película favorita es ', title_text)""")

        st.write("Podemos insertar números:")
        number = st.number_input('Inserta un número:')
        st.write('Tu número es el ', number)
        st.code("""number = st.number_input('Inserta un número:')
st.write('Tu número es el ', number)""")

        st.write(
            "Podemos agregar tambien en un área de texto para, por ejemplo, analizar sentimientos:"
        )
        txt = st.text_area(
            'Texto a analizar',
            '''¡Me encanta lo que hace el candidato del partido azul! ¡Vota por el partido azul!'''
        )
        st.write('Sentimiento: ', 'Favorable')  #run_sentiment_analysis(txt))
        st.code(
            """txt = st.text_area('Texto a analizar', '''¡Me encanta lo que hace el candidato del partido azul! ¡Vota por el partido azul!''')
st.write('Sentimiento: ', 'Favorable' )#run_sentiment_analysis(txt))""")

        st.write("Podemos ingresar fechas u horas:")
        d = st.date_input("¿Cuándo naciste?", date(2019, 7, 6))
        st.write('Naciste el ', d)
        t = st.time_input('Programar alarma para las ', time(8, 45))
        st.write('Alarma programda a las ', t)
        st.code("""d = st.date_input("'¿Cuándo naciste?'",date(2019, 7, 6))
st.write('Naciste el ', d)
t = st.time_input('Programar alarma para las ', time(8, 45))
st.write('Alarma programda a las ', t)""")

        st.write("Finalmente podemos subir un archivo o múltiples archivos:")
        uploaded_file = st.file_uploader("Elige un archivo:")
        if uploaded_file is not None:
            bytes_data = uploaded_file.read()
            st.write(bytes_data)
        uploaded_files = st.file_uploader("Elige tus archivos:",
                                          accept_multiple_files=True)
        for uploaded_file in uploaded_files:
            bytes_data = uploaded_file.read()
            st.write("filename:", uploaded_file.name)
            st.write(bytes_data)
        st.code("""uploaded_file = st.file_uploader("Elige un archivo:")
if uploaded_file is not None:
    bytes_data = uploaded_file.read()
    st.write(bytes_data)
uploaded_files = st.file_uploader("Elige tus archivos:", accept_multiple_files=True)
for uploaded_file in uploaded_files:
    bytes_data = uploaded_file.read()
    st.write("filename:", uploaded_file.name)
    st.write(bytes_data)""")


######################ELEMENTOS DE PROCESO, INFORMACION Y CONTROL DE FLUJO########################################
    st.header("Procesamiento, Información y Control de Flujo")
    with st.beta_expander("Elementos de Procesamiento:"):
        st.write(
            "Podemos crear elementos que se vayan actualizando con el tiempo:")
        if st.button("Aprietame para iniciar el conteo"):
            with st.empty():
                for seconds in range(3):
                    st.write(f"⏳ {seconds} segundos han pasado")
                    tim.sleep(1)
                st.write("✔️ conteo de segundos terminado.")
        st.write("Podemos crear una barra de progreso:", key="msje2")
        if st.button("Aprietame para iniciar la barra de carga"):
            my_bar = st.progress(0)
            for percent_complete in range(100):
                tim.sleep(0.03)
                my_bar.progress(percent_complete + 1)

        st.write("Podemos también poner elementos de espera:")
        if st.button("Esperar"):
            with st.spinner('Esperando 3 segundos...'):
                tim.sleep(3)
            st.success('Listo!')

    with st.beta_expander("Elementos de información:"):
        st.write(
            "Además podemos crear distintos cuadros para comunicarnos con los usuarios:"
        )
        st.info("Esto es un cuadro de información.")
        st.warning('Esto es un cuadro de advertencia.')
        st.success('Esto es un cuadro de éxito.')
        st.error("Esto es un cuadro de error.")
        e = RuntimeError(
            'También podemos levantar Excepciones. Por ejemplo estos es un RuntimeError'
        )
        st.exception(e)

    with st.beta_expander("Elementos de código:"):
        st.write("Podemos además mostrar código directamente:")
        st.code("""st.write('Este código será impreso en pantalla.')""")

    #######################################################################PLACEHOLDERS, HELPS & OPTIONS

    # placeholder = st.empty()
    # # Replace the placeholder with some text:
    # placeholder.text("Hello")
    # # Replace the text with a chart:
    # placeholder.line_chart({"data": [1, 5, 2, 6]})
    # # Replace the chart with several elements:
    # with placeholder.beta_container():
    #     st.write("This is one element")
    #     st.write("This is another")
    # # Clear all those elements:
    # placeholder.empty()

    # st.help(pd.DataFrame)

    #######################################################################MUTATE DATA
    # df1 = pd.DataFrame(
    #    np.random.randn(50, 20),
    #    columns=('col %d' % i for i in range(20)))
    # # my_table = st.table(df1)
    # df2 = pd.DataFrame(
    #    np.random.randn(50, 20),
    #    columns=('col %d' % i for i in range(20)))
    # # my_table.add_rows(df2)
    # # Now the table shown in the Streamlit app contains the data for
    # # df1 followed by the data for df2.

    # my_chart = st.line_chart(df1)
    # my_chart.add_rows(df2)
Exemple #10
0
table = st.empty()
#st.table(df_to_table(df))

st.markdown("# Analyse")
st.markdown("""## Absatz vs. Werbeausgaben:""")
show_vlines = False
if show_deltas:
    show_vlines = True
else:
    show_vlines = False

df = update_df(b0, w1, df)
table.table(df_to_table(df))  # Input data into table "placeholder"

chart = chart_regression(b0, w1, df, show_vlines=show_vlines)
st.altair_chart(chart)
color = "green"
st.success(f'''
Schätzung = **{b0}** + **{w1}** x Werbeaufwand
''')  #,unsafe_allow_html=True)

st.markdown("""## Analyse des Schätzfehlers""")
chart1 = chart_w1_loss(b0, w1, df)
chart2 = chart_b0_loss(b0, w1, df)
chart = alt.hconcat(chart1.properties(width=350), chart2.properties(width=350))
bar = chart_yminushat(df)
chart_total = alt.vconcat(chart, bar.properties(width=750))
st.altair_chart(chart_total)

st.markdown("""## Gradient descent""")
#calc_gradient = st.checkbox("Schätze Parameter via Gradient Descent")
                get_position=["lonstartl", "latstartl"],
                radius=100,
                elevation_scale=4,
                elevation_range=[0, 1100],
                pickable=True,
                extruded=True,
            ),
        ],
    ))

#สร้างกราฟแท่งแบ่งตามนาที
st.subheader("Breakdown by minute between %i:00 and %i:00" % (hour,
                                                              (hour + 1) % 24))
filtered = data[(data[DATE_TIME].dt.hour >= hour)
                & (data[DATE_TIME].dt.hour < (hour + 1))]
hist = np.histogram(filtered[DATE_TIME].dt.minute, bins=60, range=(0, 60))[0]
chart_data = pd.DataFrame({"minute": range(60), "pickups": hist})

st.altair_chart(alt.Chart(chart_data).mark_area(
    interpolate='step-after', ).encode(x=alt.X("minute:Q",
                                               scale=alt.Scale(nice=False)),
                                       y=alt.Y("pickups:Q"),
                                       tooltip=['minute', 'pickups']),
                use_container_width=True)

st.markdown("Thank you for your attention.")
st.markdown("We hope to see you again soon.")

st.sidebar.markdown(
    "Powered by department of Survey Engineering , Chulalongkorn University")
Exemple #12
0
    df_int = pd.merge(df_int,
                      communaute,
                      left_on='Date',
                      right_on='Date',
                      how='outer')

    df_int['Date'] = pd.to_datetime(df_int['Date'], dayfirst=True)
    df_int = df_int.sort_values("Date").ffill().fillna(0)
    df_int.columns = ["Date", "Importes", "Contact", "Communauté"]

    ch0 = alt.Chart(df_int).transform_fold(
        ['Importes', 'Contact', 'Communauté'], ).mark_line(size=5).encode(
            x='Date:T', y='value:Q', color='key:N').properties(height=500,
                                                               width=700)

    st.altair_chart(ch0)

    st.write(
        "Les cas importés, ayant ensuite crée des cas contact, proviennent des pays suivants:"
    )

    ch3 = alt.Chart(df.dropna(subset=['Source/Voyage'])).mark_bar().encode(
        x='Source/Voyage:N', y=alt.Y('count()',
                                     title='Nombre de patients')).properties(
                                         title="Provenance des malades",
                                         height=300,
                                         width=700)

    st.write(ch3)

    # Interactive Map
Exemple #13
0
def main():
    if choice == "Basic EDA":
        #Shape of df
        st.text("Shape of dataset:")
        st.write(df0.shape)
        #Stats and general description
        st.text("General description:")
        st.write(df0.describe(include='all'))
        #Type of data
        st.text("Data types:")
        st.write(df0.dtypes)
        #Sum of nulls
        st.text("Do I have null data?")
        st.write(df0.isnull().sum())

        if st.button('Correlation map'):

            df1 = df0.drop(['VGChartz_Score', 'status', 'Total_Shipped'],
                           axis=1)
            c_plot = sns.heatmap(df1.corr(), annot=True, cmap="YlGnBu")
            st.write(c_plot)
            st.pyplot()

    if choice == "Dashboard":
        #Platforms on demand by year of release
        st.subheader("Videogame ranking filter")
        x1 = st.slider('Year of release', 1970, 2020, (1970, 2020))
        x = st.number_input('Select ranking', min_value=1, max_value=55792)

        if st.button('Filter data'):
            df1 = df0[['Year', 'Name', 'Rank', 'Platform']]
            df1 = df1[df1['Rank'] <= x]
            df1 = df1[df1['Year'] >= x1[0]]
            df1 = df1[df1['Year'] <= x1[1]]
            df1['Year'] = df1['Year'].astype('int64')
            df1 = df1.sort_values(by=['Rank'])
            st.write(df1)
            barplot = sns.barplot(x="Rank",
                                  y="Platform",
                                  hue="Name",
                                  data=df1,
                                  palette="mako")
            barplot.plot(kind='bar')
            st.pyplot()

        #Platform analysis
        st.subheader("Most demanded platforms")
        if st.button('See barchart'):
            img = Image.open("df1p.png")
            st.image(img, width=700, caption="Sum Global_Sales (5 Years)")
            st.write(
                "Most demanded platforms for the last 5 years: XOne, Wii and PS4."
            )

        #Geographic distribution
        st.subheader("Geographical Distribution")
        if st.checkbox('Comparative chart'):
            dfNA = df0[[
                'Year',
                'NA_Sales',
            ]]
            dfNA['label'] = 'NA'
            dfNA = dfNA.rename(columns={'NA_Sales': 'Sales'})

            dfPAL = df0[[
                'Year',
                'PAL_Sales',
            ]]
            dfPAL['label'] = 'PAL'
            dfPAL = dfPAL.rename(columns={'PAL_Sales': 'Sales'})

            dfJP = df0[[
                'Year',
                'JP_Sales',
            ]]
            dfJP['label'] = 'JP'
            dfJP = dfJP.rename(columns={'JP_Sales': 'Sales'})

            dfOT = df0[[
                'Year',
                'Other_Sales',
            ]]
            dfOT['label'] = 'OT'
            dfOT = dfOT.rename(columns={'Other_Sales': 'Sales'})

            dftot = pd.concat([dfNA, dfPAL, dfJP, dfOT])
            dftot = dftot[dftot['Year'] > 1995]
            source = dftot.rename(columns={
                'Year': 'x',
                'label': 'category',
                'Sales': 'y'
            })
            subset_data = source
            genre_input = st.multiselect(
                'Choose category',
                source.groupby('category').count().reset_index()
                ['category'].tolist())
            if len(genre_input) > 0:
                subset_data = source[source['category'].isin(genre_input)]

            totalcases = alt.Chart(subset_data).transform_filter(
                alt.datum.y > 0).mark_line().encode(
                    x=alt.X('x', type='nominal', title='Year of release'),
                    y=alt.Y('sum(y):Q', title='Global_Sales(millions)'),
                    color='category',
                    tooltip='sum(y)',
                ).properties(width=800,
                             height=400).configure_axis(labelFontSize=17,
                                                        titleFontSize=20)
            st.altair_chart(totalcases)

            st.write("JP: Japan, NA: North Ameria, PAL: Europe, OT: Others")
Exemple #14
0
size = st.slider("Size", min_value=1000, max_value=100_000, step=10_000)
dataset = pd.DataFrame({
    "x": np.random.normal(size=size),
    "y": np.random.normal(size=size)
})

mpl_start = time()
mpl_plot = mpl_scatter(dataset, "x", "y")
mpl_finish = time()

st.pyplot(mpl_plot)
mpl_render = time()
st.subheader("Matplotlib")
st.write(f"Create: {mpl_finish - mpl_start:.3f}s")
st.write(f"Render: {mpl_render - mpl_finish:.3f}s")
st.write(f"Total: {mpl_render - mpl_start:.3f}s")

alt_start = time()
alt_plot = altair_scatter(dataset, "x", "y")
alt_finish = time()

st.altair_chart(alt_plot)
alt_render = time()
st.subheader("Altair")
st.write(f"Create: {alt_finish - alt_start:.3f}s")
st.write(f"Render: {alt_render - alt_finish:.3f}s")
st.write(f"Total: {alt_render - alt_start:.3f}s")

speedup = (mpl_render - mpl_start) / (alt_render - alt_start)
st.write(f"MPL / Altair Ratio: {speedup:.1f}x")
def main():
    """ Stock APP """
    ##General Settings
    st.set_page_config(page_title='CLUE - Immobilienatlas', page_icon='logo.jpg')

    ## Hide Hamburger Menu
    hide_menu_style = """
        <style>
        #MainMenu {visibility: hidden;}
        </style>
        """
    st.markdown(hide_menu_style, unsafe_allow_html=True)

    ##Selectbox of city
    city = st.selectbox('Wähle deine Stadt aus', select_list, 0)
    df_select = df[(df['Stadt'] == city)]
    
    try:
        ### Get offers
        city_name = city
        city_Lon = format(df_clt['Lon_City'][(df_clt['Stadt'] == city_name)].drop_duplicates().values[0])
        city_Lat = format(df_clt['Lat_City'][(df_clt['Stadt'] == city_name)].drop_duplicates().values[0])

        offer = requests.get('https://www.realbest.de/immobiliensuche?realEstateType=CONDOMINIUM&scrollToResults=true&cityinput='+city_name+'%2C+Deutschland&longitude='+city_Lon+'&latitude='+city_Lat+'&searchRadius=FIFTEEN&searchParameters=true&cid=352040&afId=4mhZD105376')
        soup_offer = bs(offer.content, 'html.parser')
        content = soup_offer.body('td', {'class': 'details-with-more-columns'})

        ## Scraping PLZ und Stadt
        data_PLZ = soup_offer.body('span', {'class': 'address'})
        content_PLZ = pd.DataFrame(data_PLZ).astype(str)
        df_PLZ = pd.DataFrame(content_PLZ[0].str.split().to_list(), columns=[0,1,2,3]).drop(columns=[0, 1]).rename(columns={3: 'Stadt', 2: 'Postleitzahl'}).dropna().reset_index().drop(columns=['index'])

        ## Scraping Preis
        data_preis = soup_offer.body('strong')
        content_preis = pd.DataFrame(data_preis).astype(str)
        df_preis = pd.DataFrame(content_preis[0].str.split().to_list(), columns=[0,1,2,3]).drop(columns=[1,2,3]).rename(columns={0: 'Preis'})
        df_preis_1 = pd.DataFrame(df_preis['Preis'].str.replace('<strong>', '').apply(pd.to_numeric, errors='coerce').dropna()).reset_index().drop(columns=['index'])
        df_preis_final = df_preis_1[(df_preis_1['Preis'] > 100)].reset_index().drop(columns=['index']) * 1000

        ## Scraping QM
        data_qm = soup_offer.body('span', {'class': 'size'})
        content_qm = pd.DataFrame(data_qm).astype(str)
        df_qm = pd.DataFrame(content_qm[0].str.split().to_list(), columns=[0,1,]).rename(columns={0: 'QM'}).reset_index().drop(columns=[1, 'index']).dropna()

        ## Get Link
        data_link = soup_offer.body('a', {'type': 'submit'})
        content_link = pd.DataFrame(data_link).astype(str)
        df_link = pd.DataFrame(content_link[0].str.split('"').to_list())
        df_links = 'https://www.realbest.de' + df_link[[1,3]][(df_link[1] == 'hidden-print')].reset_index().drop(columns=['index', 1]).rename(columns={3: 'Link'}) + '&afId=4mhZD105376'

        ## Concat
        df_p_qm = pd.concat([df_PLZ, df_preis_final, df_qm, df_links], axis=1)
        df_p_qm['QM'] = df_p_qm['QM'].str.replace(',', '').astype(float) / 100
        df_p_qm['Preis/QM'] = round(df_p_qm['Preis'] / df_p_qm['QM'], 1)

        offer_1 = pd.merge(df_p_qm, df_value_coord[['Stadt', 'Postleitzahl', 'Miete/QM - PLZ']], left_on=['Stadt', 'Postleitzahl'], right_on=['Stadt', 'Postleitzahl']).drop_duplicates()
        offer_1['Erwartete Rendite (%)'] = round((offer_1['Miete/QM - PLZ'] * 12) / offer_1['Preis/QM'] * 100 , 2)
        offer_2 = offer_1.rename(columns={'Miete/QM - PLZ': 'Erwartete Miete/QM'}).drop(columns=['Stadt'])

        offer_3 = offer_2[['Postleitzahl', 'Preis', 'QM', 'Preis/QM', 'Erwartete Miete/QM', 'Erwartete Rendite (%)', 'Link']].reset_index().drop(columns=['index'])
        offer_3['Angebot'] = '<a href="' + offer_3['Link'] + '" target="_blank">Zum Angebot</a>'

        offer_4 = offer_3[['Postleitzahl', 'Preis', 'QM', 'Preis/QM', 'Erwartete Miete/QM', 'Erwartete Rendite (%)', 'Angebot']]
        
        html = offer_4.to_html(escape=False, index=False)
        tr = 1
    except:
        tr = 0

    ###Plotting
    #Scatter per City and zipcode
    st.success(city + ' - Preis und Miete je Qudratmeterklasse (QM-Klasse) und je Postleitzahl')

    scat_city = alt.Chart(df_select).mark_circle(size=60, clip=True, opacity=0.8).encode(
        alt.X('Preis/QM - QMK',
            scale=alt.Scale(domain=(df_select['Preis/QM - QMK'].min(), df_select['Preis/QM - QMK'].max())),
            title='Preis/QM'),
        alt.Y('Miete/QM - QMK',
            scale=alt.Scale(domain=(df_select['Miete/QM - QMK'].min(), df_select['Miete/QM - QMK'].max())),
            title='Miete/QM'),
            tooltip=['Postleitzahl', 'QM-Klasse', 'Miete/QM - QMK', 'Preis/QM - QMK', 'Rendite (%) - QMK'],
            size='Rendite (%) - QMK',
            color=alt.Color('Rendite (%) - QMK', scale=alt.Scale(scheme='redyellowgreen'), legend=alt.Legend(orient="top"))).interactive().properties(
                width=700, ##if HTML div has defined width: 100%, we can use container
                height=450
    )
    df_scatter = pd.DataFrame(df_select[['Postleitzahl', 'QM-Klasse', 'Miete/QM - QMK', 'Preis/QM - QMK', 'Rendite (%) - QMK']]).drop_duplicates()
    df_scatter.sort_values(by=['Rendite (%) - QMK'], inplace=True, ascending=False)
    df_scatter.reset_index(inplace=True)
    df_scatter.drop(columns=['index'], inplace=True)
    
    st.altair_chart(scat_city)


    ## Angebote Tabelle
    if tr == 1:
        st.success('Aktuelle Angebote in - ' + city)
        st.markdown(html, unsafe_allow_html=True)
        st.text('')
    else:
        pass

    ##Table
    st.success('Details zur gewählten Stadt - ' + city)
    
    left_col, right_col =st.beta_columns(2)
    with left_col:
        PLZ = st.text_input("Gib eine Postleitzahl ein, um danach zu filtern", "")
    
    if PLZ == '':
        plz_input = df_scatter
    else:
        plz_input = df_scatter[(df_scatter['Postleitzahl'] == PLZ)]

    with right_col:
        QMK = st.selectbox('Wähle eine QM-Klasse, um danach zu filtern', ['', 'bis 40 QM', '40 bis 60 QM', '60 bis 80 QM', '80 bis 100 QM', '100 bis 120 QM', 'ab 120 QM'])
    
    if QMK == '':
        qmk_input = plz_input.style.set_precision(2).bar(subset=['Miete/QM - QMK', 'Preis/QM - QMK', 'Rendite (%) - QMK'], align='mid', color=['#red', '#0059ff'])
    else:
        qmk_input = plz_input[(plz_input['QM-Klasse'] == QMK)].style.set_precision(2).bar(subset=['Miete/QM - QMK', 'Preis/QM - QMK', 'Rendite (%) - QMK'], align='mid', color=['#d65f5f', '#5fba7d'])

    st.table(qmk_input)
def main(hidemenu=True):

    # hide streamlit menu
    if hidemenu:
        hide_streamlit_style = """
        <style>
        #MainMenu {visibility: hidden;}
        footer {visibility: hidden;}
        </style>

        """
        st.markdown(hide_streamlit_style, unsafe_allow_html=True) 

    st.sidebar.title('Данные о covid-19 в Калининградской области')
    st.sidebar.text('v' + __version__)

    # prepare data for drawing
    p, paginator = sfunc.pagemaker() # paginator
    data = sfunc.dataloader('https://raw.githubusercontent.com/KonstantinKlepikov/covid-kaliningrad/datasets/data/data.csv')
    rosstat = sfunc.dataloader('https://raw.githubusercontent.com/KonstantinKlepikov/covid-kaliningrad/datasets/data/rosstat.csv')
    ds = sfunc.asidedata(data, rosstat) # data for aside menu
    # high, low = sfunc.irDestrib(data)
    _colsPro = sfunc.profession(data)
    _colsReg = sfunc.regDistr(data)

    # aside menu
    st.sidebar.markdown('Обновлено: {}'.format(ds['update']))
    st.sidebar.markdown('всего выявлено: **{0}** ({1}%)'.format(ds['sick'], ds['proc']))
    st.sidebar.markdown('официально умерло: **{}**'.format(ds['dead']))
    st.sidebar.markdown('официальная летальность: **{}%**'.format(ds['let']))
    st.sidebar.markdown('выписано: **{}**'.format(ds['ex']))
    st.sidebar.markdown('Вакцинированация:')
    st.sidebar.markdown('привито (Ф1): **{0}** ({1}%)'.format(ds['pr1'], ds['prproc1']))
    st.sidebar.markdown('привито (Ф2): **{0}** ({1}%)'.format(ds['pr2'], ds['prproc2']))
    st.sidebar.markdown('всего заболело: **{0}** ({1}%)'.format(ds['vacc_cases'], ds['vacc_proc_full']))
    st.sidebar.markdown('от заболевших: {}%'.format(ds['vacc_proc']))
    st.sidebar.markdown('от полностью привитых: {}%'.format(ds['vacc_proc_vac']))
    st.sidebar.markdown('официально умерло: {}'.format(ds['vacc_dead']))
    st.sidebar.markdown('официальная летальность: **{}**%'.format(ds['vacc_let']))
    # st.sidebar.markdown('IR4 >= 1 дней: **{}**'.format(high))
    # st.sidebar.markdown('IR4 < 1 дней: **{}**'.format(low))
    st.sidebar.markdown('Смерти связанные с covid(росстат):')
    st.sidebar.markdown('на {}'.format(ds['rstat_date']))
    st.sidebar.markdown('умерло: {}'.format(ds['rstat_dead']))    
    st.sidebar.markdown('летальность: **{}**%'.format(ds['rstat_let']))
    st.sidebar.markdown('Умерли с ковид/пневмонией:')
    st.sidebar.markdown('на {}'.format(ds['cov_pnew_date']))
    st.sidebar.markdown('умерло: {}'.format(ds['cov_pnew_dead']))
    st.sidebar.markdown('летальность: {}%'.format(ds['cov_pnew_let']))


    # main content
    page = st.radio('Данные', paginator)


    if page == 'intro':
        st.header(p[page])

        st.subheader('Описание проекта')
        st.markdown('Проект работает с открытыми данными, собранными из различных официальных источников. \
            Данные обновляются в конце дня. Предсталеные визуализированные данные не являются точными и не могут \
            отражать истинную картину распространения covid-19 в Калининградской области. Автор проекта агрегирует \
            данные с образовательной целью и не несет ответственности за их достоверность. Весь контент и код \
            проекта предоставляется по [MIT лицензии](https://opensource.org/licenses/mit-license.php).')

        st.subheader('Как это сделано?')
        st.markdown('[Статья о том, как собрано это приложение](https://konstantinklepikov.github.io/2021/01/10/zapuskaem-machine-learning-mvp.html)')
        st.markdown('[Репозиторий проекта](https://github.com/KonstantinKlepikov/covid-kaliningrad)')
        st.markdown('[Данные](https://docs.google.com/spreadsheets/d/1iAgNVDOUa-g22_VcuEAedR2tcfTlUcbFnXV5fMiqCR8/edit#gid=1038226408)')

        st.subheader('Контакты')
        st.markdown('[Мой блог про machine learning](https://konstantinklepikov.github.io/)')
        st.markdown('[Я на github](https://github.com/KonstantinKlepikov)')
        st.markdown('[Телеграм](https://t.me/KlepikovKonstantin)')

        st.markdown('К сожалению медицинские службы региона не смогли предоставить исторические данные. Буду благодарен \
            за любой источник информации, если таковой имеется - пишите в [телеграм](https://t.me/KlepikovKonstantin).')
        st.image('https://raw.githubusercontent.com/KonstantinKlepikov/covid-kaliningrad/main/img/answer.png', use_column_width=True)


    ##########################################
    ############## cases #####################
    ##########################################
    
    elif page == 'cases':
        st.header(p[page])
        st.markdown('До 19.20.2020 данные о симптоматики предоставлялись нерегулярно. После 19.10.2020 нет данных о тяжести течения болезни.')

        ############## cases ##############
        ch = Linear(
            p[page], 
            data[['дата', 'всего', 'ОРВИ', 'пневмония', 'без симптомов', 'тяжелая форма']]
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.selectionchart())

        ############## area cases ##############
        ch = Area(
            p[page], 
            data[['дата', 'ОРВИ', 'пневмония', 'без симптомов']],
            height=400
            )
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.selectionchart())
        
        ############## cumsum cases ##############
        ch = Linear(
            'Количество случаев аккумулировано', 
            data[['дата', 'кумул. случаи']], 
            height=400, 
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())
        
        ############## under control ##############
        ch = Area(
            'Находятся под наблюдением (выдано предписание об изоляции)',
            data[['дата', 'мед.наблюдение']],
            height=400
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.selectionchart())
        
        ############## orvi ##############
        ch = Area(
            '% случаев с ОРВИ к общему числу',
            sfunc.ratio(data[['дата', 'всего', 'ОРВИ']], above='ОРВИ', below='всего'),
            height=300
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.selectionchart())
        
        ############## pnevmonia ##############
        ch = Area(
            '% случаев с пневмонией к общему числу',
            sfunc.ratio(data[['дата', 'всего', 'пневмония']], above='пневмония', below='всего'),
            height=300
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.selectionchart())
        
        ############## no simptoms ##############
        ch = Area(
            '% случаев без симптомов к общему числу',
            sfunc.ratio(data[['дата', 'всего', 'без симптомов']], above='без симптомов', below='всего'),
            height=300
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.selectionchart())

        ############## 30/1000 ##############
        ch = Linear(
            'Количество случаев на 1000 человек за последние 30 дней', 
            data[['дата', '30days_1000']], 
            height=400, 
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())

        ############## invitro cases ##############
        st.subheader('Данные о случаях, выявленных в сети клиник Invitro (IgG)')
        st.markdown('Нет сведений о том, что данные случаи учитываются в статистике Роспотребнадзора. Сведения \
            получены на сайте [invitro.ru](https://invitro.ru/l/invitro_monitor/)')

        ch = Linear(
            'Кейсы в Invitro', 
            data[['дата', 'positive']],
            height=400
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.selectionchart())

        ############## invitro cases cumulative ##############
        ch = Linear(
            'Кейсы в Invitro аккумулировано', 
            data[['дата', 'positivecum']],
            height=400
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())

        ############## vaccinated casses ##############
        ch = Area(
            'Выявлено среди вакцинированных', 
            data[['дата', 'привитых']],
            height=300, 
            grid=False, 
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())
    
    ##########################################
    ############# infection rate #############
    ##########################################

    elif page == 'infection rate':
        st.header(p[page])

        ############## ir4 ##############
        st.markdown('IR4 расчитывается по методике Роспотребнадзора - как отношение количества заболевших за прошедшие \
            4 дня к количеству заболевших за предыдущие прошедшие 4 дня.')
        ch = Linear(
            'Infection Rate 4 days', 
            data[['дата', 'infection rate']], 
            level=1
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.baselinechart())

        ############## ir7 ##############   
        ch = Linear(
            'Infection Rate 7 days', 
            data[['дата', 'IR7']], 
            level=1
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.baselinechart())

        ############## ir difference ##############
        # dfnorm = data[['дата', 'отношение']].copy()
        # ma = dfnorm[['дата', 'отношение']].max()[1]
        # mi = dfnorm[['дата', 'отношение']].min()[1]
        # dfnorm[['отношение']] = (dfnorm[['отношение']] - mi)/(ma-mi)
        # dfnorm.fillna(0, inplace=True)
        # dfnorm['отношение'] = dfnorm['отношение'].apply(lambda x: round(x, 2))
        ch = Linear(
            'Распределение отношения количества дней с положительным ir4 к количеству дней с отрицательным ir4', 
            data[['дата', 'отношение']], 
            level=1
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.baselinechart())

    ##########################################
    ############### deaths ###################
    ##########################################

    elif page == 'deaths':
        st.header(p[page])

        ############## deaths with polynomial ##############
        ch = Area(
            'умерли от ковид', 
            data[['дата', 'умерли от ковид']],
            height=400, 
            interpolate='step', 
            poly=7,
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.polynomialchart())

        ############## death cumsum ##############
        ch = Linear(
            'смертельные случаи нарастающим итогом', 
            data[['дата', 'кумул.умерли']], 
            height=400, 
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())
        
        ############## 30/1000 death ##############
        ch = Linear(
            'Количество смертей на 1000 человек за последние 30 дней', 
            data[['дата', '30days_1000die']], 
            height=400, 
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())

        ############## hospital death data ##############
        st.markdown('Информация об умерших в палатах, отведенных для больных для больных пневмонией/covid предоставлялась \
            мед.службами по запросу [newkaliningrad.ru](https://www.newkaliningrad.ru/)')
        ch = Linear(
            'умерли в палатах для ковид/пневмонии', 
            data[['дата', 'умерли в палатах для ковид/пневмония с 1 апреля']].query("'2020-11-01' <= дата & `умерли в палатах для ковид/пневмония с 1 апреля` > 0"), 
            height=400, 
            point=True, 
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())

        ############## rosstat death ##############
        rosstat_ = rosstat.copy(deep=True)
        rosstat_['Месяц'] = pd.to_datetime(rosstat_['Месяц'], dayfirst=True)
        ch = Area(
            'Данные Росстата о смертности с диагнозом COVID-19', 
            rosstat_, 
            target='Месяц',
            height=400,
            width=800
            )
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.emptychart())
        
        ############## vaccinated dead ##############
        ch = Linear(
            'Умерло среди вакцинированных', 
            data[['дата', 'привитых умерло']],
            height=300, 
            grid=False,
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())

    ##########################################
    ############## capacity ##################
    ##########################################

    elif page == 'capacity':
        st.header(p[page])
        st.markdown('Активные случаи - это заразившиеся минус выздоровевшие и умершие. Ежедневные данные о количестве \
            болеющих и госпитализированных не предоставляются')

        ############## exit ##############
        ch = Linear(
            'Выздоровевшие', 
            data[['дата', 'всего', 'выписали']], 
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.selectionchart())

        ############## cumsum exit ##############
        ch = Linear(
            'Выздоровевшие нарастающим итогом', 
            data[['дата', 'кумул. случаи', 'кумул.выписаны']], 
            height=400, 
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())

        ############## cumsum minus exit ##############
        ch = Linear(
            'Активные случаи нарастающим итогом', 
            data[['дата', 'кумул.активные']], 
            height=400, 
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())

        ############## hospital places ##############
        ch = Point(
            'Развернуто под covid-19', 
            sfunc.slicedData(
                data[['дата', 'доступно под ковид', 'занято под ковид']],
                "'2020-02-01' <= дата"
                ),
            height=600, 
            grid=False, 
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())
        
        ch = Point(
            'Развернуто под covid-19 и пневмонию', 
            sfunc.slicedData(
                data[['дата', 'доступно под ковид и пневмонию', 'занято под ковид и пневмонию']],
                "'2020-02-01' <= дата"
                ),
            height=600, 
            grid=False, 
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())
        
        ch = Point(
            'Находится на кислородной поддержке', 
            sfunc.slicedData(
                data[['дата', 'кисл.поддержка']],
                "'2020-02-01' <= дата"
                ),
            height=300, 
            grid=False, 
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())
        
        ch = Point(
            'Развернуто ИВЛ', 
            sfunc.slicedData(
                data[['дата', 'доступно ИВЛ', 'занято ИВЛ']],
                "'2020-02-01' <= дата"
                ),
            height=600, 
            grid=False, 
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())

    ##########################################
    ############### tests ####################
    ##########################################

    elif page == 'tests':
        st.header(p[page])

        ############## tests ##############
        ch = Linear(
            'Тесты за день', 
            data[['дата', 'кол-во тестов', 'кол-во обследованных']], 
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.selectionchart())
        
        ############## tests cumulative ##############
        ch = Linear(
            'Общее количество тестов аккумулировано', 
            data[['дата', 'кол-во тестов кумул', 'кол-во протестированных']], 
            height=400, 
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())

        ############## tests and cases ##############
        st.markdown('Для наглядности, количество тестов разделено на 10 для приведенных графиков.')
        ch = Linear(
            'Тестирование и распространение болезни', 
            data[['дата', 'ОРВИ', 'пневмония', 'без симптомов', 'кол-во тестов / 10']], 
            height=500
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.selectionchart())

        ############### tests and exit ##############
        ch = Linear(
            'Тестирование и выписка', 
            data[['дата', 'выписали', 'кол-во тестов / 10']], 
            height=500,
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.selectionchart())

        ############## invitro tests ##############
        st.subheader('Данные о тестах, проведенных в сети клиник Invitro (IgG)')
        st.markdown('Нет сведений о том, что данные о тестах invitro учитываются в статистике Роспотребнадзора. \
            Сведения получены на сайте [invitro.ru](https://invitro.ru/l/invitro_monitor/)')

        ch = Linear(
            'Кейсы в Invitro', 
            data[['дата', 'positive', 'negative']]
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.selectionchart())

        ############## invitro tests cumulative ##############
        ch = Linear(
            'Тесты в Invitro аккумулирован', 
            data[['дата', 'totalcum']], 
            height=600
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())

        ############## invitro cases cumulative ##############
        ch = Linear(
            'Тесты в Invitro аккумулировано (на фоне общего числа официально зафиксированных случаев)', 
            data[['дата', 'кумул. случаи', 'positivecum', 'negativecum']], 
            height=600
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())

        ############## invitro cases shape ##############
        ch = Area(
            '% положительных тестов в Invitro',
            sfunc.ratio(data[['дата', 'total', 'positive']], above='positive', below='total')
            )
        ch.legend=None
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.selectionchart())

    ##########################################
    ##############vaccination ################
    ##########################################

    elif page == 'vaccination':
        st.header(p[page])
        
        ############## vaccin income total ##############
        ch = Area(
            'Всего поступило вакцин',
            data[['дата', 'всего поступило']], 
            height=400, 
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())
        
        st.markdown('Графа "поступило кумулятивно" определяет объем вакцины sputnik-v. После 2021-09-01 не публиковались сведения о типе вакцины, поступившей в регион.')
        
        st.markdown('В значение поступившей вакцины и к значениям привитых официальной статистикой отнесены 300 доз \
            экспериментальной вакцины (20% плацебо). Сообщалось, что прививку получили чиновники (губернатор Калининградской области)\
            Кроме того, сообщалось, что по оканчанию эксперимента все, кто получаил плацебо, будут привиты действующим препаратом.\
            Сведений о том, что все участники эксперимента действительно получиили настоящий препарат не имеется.')

        ############## vaccine income ##############
        dfv = data[['дата', 'поступило кумулятивно', 'эпивак кумулятивно', 'ковивак кумул', 'спутник лайт кумул']].query("'2021-09-01' >= дата")
        ch = Area(
            'Поступиление вакцин', 
            dfv,
            height=400
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())
        
        st.markdown('В статистику не включены данные по вакцинации военнослужащих. По сообщению пресс.службы Балт.Флота от 29.10.2021, 98,7% военнослужащих прошли вакцинацию.')
        
        st.markdown('Данный график не содержит сведения о ревакцинации.')

        ############## vaccination outcome ##############
        dfout = sfunc.slicedData(
            data[['дата', 'компонент 1', 'компонент 2']],
            "'2020-08-01' <= дата"
            )
        ch = Point(
            'Использовано вакцин',
            dfout, 
            height=400, 
            )
        ch.draw()
        ch.richchart()
        st.altair_chart(ch.emptychart())

    ##########################################
    ############## regions ###################
    ##########################################

    elif page == 'regions':
        st.header(p[page])

        ############## Kaliningrad and regions ##############
        ch = Area(
            'Калининград и регионы', 
            data[['дата', 'Калининград', 'все кроме Калининграда']], 
            interpolate='step', 
            height=400,
            )
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.selectionchart())
        
        ############## activivty linear ##############
        dfreg = sfunc.nonzeroData(data[['дата', 'Калининград', 'все кроме Калининграда']])
        ch = Linear(
            '', 
            dfreg, 
            interpolate='monotone',
            height=400,
            )
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.selectionchart())

        ############## All regions ##############
        ch = Area(
            'Распределение случаев по региону', 
            data[_colsReg], 
            interpolate='step', 
            height=600,
            )
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.selectionchart())

    ##########################################
    ############ regions detail ##############
    ##########################################

    elif page == 'regions detail':

        ############## regions by city ##############
        st.header('Распределение по регионам (подробнее)')
        multichart = sfunc.precision('Калининград', data[['дата', 'Калининград']])
        for i in _colsReg:
            if i != 'дата' and i != 'Калининград':
                multichart = multichart & sfunc.precision(i, data[['дата', i]])
        st.altair_chart(
            multichart
            )

    ##########################################
    ##################### demographics #######
    ##########################################

    elif page == 'demographics':
        st.header(p[page])

        ############## activivty ##############
        ch = Area(
            'Распределение случаев по статусу', 
            data[['дата', 'воспитанники/учащиеся', 'работающие', 'служащие', 'неработающие и самозанятые', 'пенсионеры']], 
            interpolate='step', 
            height=400,
            )
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.selectionchart())
        
        ############## activivty linear ##############
        dfact = sfunc.nonzeroData(data[['дата', 'воспитанники/учащиеся', 'работающие', 'служащие', 'неработающие и самозанятые', 'пенсионеры']])
        ch = Linear(
            '', 
            dfact,
            interpolate='monotone',
            height=300,
            )
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.selectionchart())

        ############## profession diagram ##############
        ch = Area(
            'Распределение случаев по роду деятельности', 
            data[_colsPro], 
            interpolate='step', 
            height=600,
            )
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.selectionchart())

        ############## sex ##############
        ch = Area(
            'Распределение случаев по полу', 
            data[['дата', 'мужчины', 'женщины']], 
            interpolate='step', 
            height=400,
            )
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.selectionchart())
        
        ############## sex point ##############
        dfsex = sfunc.nonzeroData(data[['дата', 'мужчины', 'женщины']])
        ch = Point(
            '', 
            dfsex, 
            height=200,
            )
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.selectionchart())

        ############## age destribution ##############
        _colsAge = sfunc.ageDestr(data)
        ch = Area(
            'Распределение случаев по возрасту', 
            data[_colsAge], 
            interpolate='step', 
            height=400,
            )
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.selectionchart())
        
        ############## age destribution linear ##############
        dfage = sfunc.nonzeroData(data[_colsAge])
        ch = Linear(
            '', 
            dfage,
            interpolate='monotone', 
            height=300,
            )
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.selectionchart())

        ############## source ##############
        ch = Area(
            'Распределение по источнику заражения', 
            data[['дата', 'завозные', 'контактные', 'не установлены']], 
            interpolate='step', 
            height=400,
            )
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.selectionchart())
        
        ############## not indexed source of infection ##############
        ch = Area(
            '% случаев с неустановленным источником заражения',
            sfunc.ratio(data[['дата', 'всего', 'не установлены']], above='не установлены', below='всего'),
            height=300
            )
        ch.legend=None
        ch.draw()
        ch.leanchart()
        st.altair_chart(ch.selectionchart())

    ##########################################
    ############# demographics detail ########
    ##########################################
    
    elif page == 'demographics detail':

        # profession destribution by profession
        st.header('Распределение по деятельности (подробнее)')
        multichart = sfunc.precision('>пенсионеры', data[['дата', '>пенсионеры']])
        for i in _colsPro:
            if i != 'дата' and i != '>пенсионеры':
                multichart = multichart & sfunc.precision(i, data[['дата', i]])
        st.altair_chart(
            multichart
            )
Exemple #17
0
data = load_data()

boxw = data.copy()
boxw = boxw.rename(columns={'hail_size': 'Hail Size'})

line_data = data.copy()
line_data = line_data.groupby(
    line_data['year'], as_index=False)['hail_size'].agg({'NReports': 'count'})

st.subheader("Number of hail reports by year")

st.altair_chart(
    alt.Chart(line_data).mark_line().encode(
        x=alt.X("year:O", title="Year", scale=alt.Scale(nice=True)),
        y=alt.Y("NReports:Q", title="Number of reports"),
        tooltip=["year", "NReports"],
    ),
    use_container_width=True,
)

year = st.slider("Year to look at",
                 min_value=1979,
                 max_value=2019,
                 value=2019,
                 step=1)

data = data[data[DATE_TIME].dt.year == year]

st.subheader("Geocoded data between {} and {}".format(year, (year + 1)))

midpoint = (np.nanmean(data["lat"]), np.nanmean(data["lon"]))
Exemple #18
0
def page_ta(today_date=date.today() - timedelta(days=1)):
    """Technical analysis page."""
    ta_type = {
        "Bollinger": {
            "price":
            ["ta_volatility_bbm", "ta_volatility_bbh", "ta_volatility_bbl"],
            "ind": ["ta_volatility_bbhi", "ta_volatility_bbli"],
        },
        "SMA": {
            "price": ["ta_trend_sma_fast", "ta_trend_sma_slow"],
        },
        "RSI": {
            "price": ["ta_trend_sma_10", "ta_trend_sma_25"],
            "ind": ["ta_momentum_rsi"],
        },
        "MACD": {
            "price": [],
            "ind":
            ["ta_trend_macd", "ta_trend_macd_signal", "ta_trend_macd_diff"],
        },
        "Momentum": {
            "price": [],
            "ind": ["ta_momentum_tsi", "ta_momentum_rsi", "ta_momentum_stoch"],
        },
    }

    select_eq = st.selectbox("Select equity", list(EQ_DICT.keys()))
    dates = pd.date_range(today_date - timedelta(days=800), today_date)
    df = load_ohlcv_data(EQ_DICT[select_eq], dates)

    col0, col1 = st.beta_columns(2)
    select_days = col0.selectbox("Lookback period", ["1M", "2M", "3M", "6M"],
                                 2)
    select_days = str2days[select_days]
    select_ta = col1.selectbox("Add TA", ["Bollinger", "SMA", "RSI"])

    source = df.iloc[-select_days:].reset_index()
    st.altair_chart(chart_candlestick(source,
                                      cols=ta_type[select_ta]["price"]),
                    use_container_width=True)

    col2, col3 = st.beta_columns(2)
    select_days2 = col2.selectbox("Select period", ["6M", "9M", "1Y", "2Y"], 2)
    select_days2 = str2days[select_days2]
    select_ta2 = col3.selectbox(
        "Select TA", ["Bollinger", "SMA", "RSI", "MACD", "Momentum"])
    st.line_chart(df[["close"] +
                     ta_type[select_ta2]["price"]].iloc[-select_days2:])
    if ta_type[select_ta2].get("ind") is not None:
        st.line_chart(df[ta_type[select_ta2]["ind"]].iloc[-select_days2:])

    # Prepare target: X Periods Return
    select_periods = st.slider("Select periods", 7, 28, 14)
    df["y"] = pct_change(df["close"], select_periods) * 100

    st.subheader(f"{select_periods}-day Returns")
    df1 = df[["y"]].copy().dropna()
    st.area_chart(df1["y"])

    st.subheader("Target Histogram")
    hist_values, hist_indexes = np.histogram(df1["y"],
                                             bins=np.arange(-10, 10, 0.5))
    st.bar_chart(pd.DataFrame(data=hist_values, index=hist_indexes[0:-1]))
    st.write(
        "Target value min: `{0:.2f}%`; max: `{1:.2f}%`; mean: `{2:.2f}%`; std: `{3:.2f}`"
        .format(np.min(df1["y"]), np.max(df1["y"]), np.mean(df1["y"]),
                np.std(df1["y"])))

    # Univariate Analysis
    st.subheader("Correlation coefficient ta features and target column")
    x_cols = [
        col for col in df.columns if col != "y" and col.startswith("ta_")
    ]
    df2 = df[x_cols + ["y"]].copy().dropna()
    values = [np.corrcoef(df2[col], df2["y"])[0, 1] for col in x_cols]
    st.bar_chart(data=pd.DataFrame(data=values, index=x_cols))
Exemple #19
0
if start_hour > end_hour:
    st.error("start hour need to be less than or equal to end hour")

hist = np.histogram(data[DATE_TIME].dt.hour, bins=25, range=(0, 25))[0]

chart_data = pd.DataFrame({"hour": range(25), "count": hist})
chart_data['start'] = start_hour
chart_data['end'] = end_hour
chart1 = alt.Chart(chart_data).mark_area(interpolate='step-after',)\
    .encode(x=alt.X("hour:O", scale=alt.Scale(nice=False)),y=alt.Y("count:Q"),tooltip=['hour', 'count'])\
    .configure_mark(opacity=0.5,color='red')
# chart2 = alt.Chart(chart_data).mark_rule().encode(x='start')
# c = alt.Chart(chart1 + chart2)
# chart2 = alt.Chart().mark_rule().encode(x='end')
st.altair_chart(chart1, use_container_width=True)


@st.cache()
def load_hour(data, start_hour, end_hour):
    return data[(data[DATE_TIME].dt.hour >= start_hour)
                & (data[DATE_TIME].dt.hour <= end_hour)]


data = load_hour(data, start_hour, end_hour)
data = data.copy()

st.markdown("### Current period and location has " + str(data.shape[0]) +
            " records\n")

hour_text = st.empty()
Exemple #20
0
def page_charts(today_date=date.today() - timedelta(days=1)):
    st.subheader("Shiller charts")
    df0 = load_ie_data()
    c1 = altair.generate_chart("line", df0[["Real_Price",
                                            "10xReal_Earnings"]]).properties(
                                                title="Index Plot",
                                                height=200,
                                                width=260,
                                            )
    c2 = altair.generate_chart("line", df0[["CAPE", "10xLong_IR"]]).properties(
        title="PE (CAPE) Plot",
        height=200,
        width=260,
    )
    st.altair_chart(alt.concat(c1, c2, columns=2), use_container_width=True)

    st.subheader("Stock charts")
    start_date = get_start_date(today_date, options=("3Y", "2Y", "1Y"))
    dates = pd.date_range(today_date - timedelta(days=365 * 2), today_date)

    # MSCI
    symbols = ["URTH", "EEM", "SPY", "ES3.SI"]
    colnames = ["MSCI World", "MSCI EM", "S&P500", "ES3"]
    df1 = load_data(dates, symbols, "SPY")
    df1.columns = colnames
    rebased_df1 = rebase(df1[df1.index >= start_date])
    chart1 = altair.generate_chart("line", rebased_df1).properties(
        title="MSCI",
        height=200,
        width=260,
    )

    # VIX
    symbols = ["^VIX"]
    colnames = ["VIX"]
    df2 = load_data(dates, symbols)[symbols]
    df2.columns = colnames
    chart2 = altair.generate_chart("line",
                                   df2[df2.index >= start_date]).properties(
                                       title="VIX",
                                       height=200,
                                       width=260,
                                   )

    st.altair_chart(alt.concat(chart1, chart2, columns=2),
                    use_container_width=True)

    # etfs
    symbols = ["IWDA", "EIMI"]
    colnames = ["World", "EM"]
    df3a = load_data(dates, symbols)
    df3a.columns = colnames
    rebased_df3a = rebase(df3a[df3a.index >= start_date])
    chart3a = altair.generate_chart("line", rebased_df3a).properties(
        title="ETF",
        height=200,
        width=260,
    )
    symbols = ["O87.SI", "ES3.SI", "CLR.SI"]
    colnames = ["GLD", "ES3", "Lion-Phillip"]
    df3b = load_data(dates, symbols)
    df3b.columns = colnames
    rebased_df3b = rebase(df3b[df3b.index >= start_date])
    chart3b = altair.generate_chart("line", rebased_df3b).properties(
        title="ETF SGX",
        height=200,
        width=260,
    )
    st.altair_chart(alt.concat(chart3a, chart3b, columns=2),
                    use_container_width=True)

    # industrial
    symbols = [
        "ES3.SI", "O5RU.SI", "A17U.SI", "J91U.SI", "BUOU.SI", "ME8U.SI",
        "M44U.SI"
    ]
    colnames = ["ES3", "AA", "Ascendas", "ESR", "FLCT", "MIT", "MLT"]
    df4 = load_data(dates, symbols)
    df4.columns = colnames
    rebased_df4 = rebase(df4[df4.index >= start_date])
    chart4a = altair.generate_chart(
        "line",
        rebased_df4[["ES3", "Ascendas", "FLCT", "MIT", "MLT"]],
    ).properties(
        title="Industrial 1",
        height=200,
        width=260,
    )
    chart4b = altair.generate_chart(
        "line",
        rebased_df4[["ES3", "AA", "ESR"]],
    ).properties(
        title="Industrial 2",
        height=200,
        width=260,
    )
    st.altair_chart(alt.concat(chart4a, chart4b, columns=2),
                    use_container_width=True)

    # retail
    symbols = ["ES3.SI", "C38U.SI", "J69U.SI", "N2IU.SI"]
    colnames = ["ES3", "CICT", "FCT", "MCT"]
    df5 = load_data(dates, symbols)
    df5.columns = colnames
    rebased_df5 = rebase(df5[df5.index >= start_date])
    chart5 = altair.generate_chart("line", rebased_df5).properties(
        title="Retail & Commercial",
        height=200,
        width=250,
    )

    # banks
    symbols = ["ES3.SI", "D05.SI", "O39.SI", "U11.SI"]
    colnames = ["ES3", "DBS", "OCBC", "UOB"]
    df6 = load_data(dates, symbols)
    df6.columns = colnames
    rebased_df6 = rebase(df6[df6.index >= start_date])
    chart6 = altair.generate_chart("line", rebased_df6).properties(
        title="Banks",
        height=200,
        width=250,
    )
    st.altair_chart(alt.concat(chart5, chart6, columns=2),
                    use_container_width=True)
Exemple #21
0
        if combinations[i,j]==1:
            probability=probability*option_prob_win['Probwin'].iloc[j]
            winning=winning+option_poss_win['Moneywon'].iloc[j]
        else:
            probability=probability*(1-option_prob_win['Probwin'].iloc[j])
            winning=winning-option_amount['Moneybet'].iloc[j]
            
    prob_dist=prob_dist.append({'Winning':winning,'Probability':probability}, ignore_index=True)
    
prob_dist=prob_dist.sort_values(by='Winning',ascending=True)
#prob_dist


if prob_dist.shape[0]>1:   
    d=alt.Chart(prob_dist).mark_bar().encode(
        x='Winning',
        y='Probability'
    )
    
    st.altair_chart(d)
    
    expecval=0
    
    for i in range(prob_dist.shape[0]):
        expecval=expecval+prob_dist.iloc[i,0]*prob_dist.iloc[i,1]
    
    'The expected value of your bets is '+str(round(expecval,2))+' dollars.'

    
#option_poss_win
#option_prob_win
Exemple #22
0
def content():
    st.title("Modeling COVID-19 using SIR")

    dd = load_proxy().assign(Location=lambda r: r.Country + ', ' + r.Province)

    st.sidebar.subheader("Region Selector")
    country = st.sidebar.radio("Top 10 Countries by Peak Active Cases",
                               dd.groupby('Country').Active.max().reset_index().sort_values('Active',
                                                                                            ascending=False).head(8)[
                                   'Country'].unique(), 6)

    ss = dd.query(f"Country == '{country}'")

    if len(ss) > 1:
        province = st.sidebar.selectbox("Provinces",
                                        ss.Province.unique())

        d = ss.query(f"Province == '{province}' ").copy()
    else:
        d = ss.copy()

    d_SIR = d[['Day', 'Active', 'Deaths', 'Recovered']].copy().reset_index(drop=True)

    min_N = int(d.Active.max())
    max_N = int(d.Active.max() * 1000)
    init_N = int(min_N * 2)

    N = st.sidebar.slider("Population Size", min_N, max_N, init_N, 10_000)

    if province:
        st.header(f"John Hopkins Data for {province}, {country}")
    else:
        st.header(f"John Hopkins Data for {country}")

    st.dataframe(d_SIR)

    st.header("Converting data to SIR Model")

    with st.echo():
        d_SIR['S'] = N - d_SIR.Active - d_SIR.Deaths - d_SIR.Recovered
        d_SIR['I'] = d_SIR.Active
        d_SIR['R'] = d_SIR.Deaths + d_SIR.Recovered

    st.markdown(f"Population has taken to be **{N}**. Use the slider to change it.")

    st.dataframe(d_SIR.drop(['Active', 'Deaths', 'Recovered'], axis=1))

    # ch = alt.Chart(d).transform_fold(['Active', 'Deaths', 'Recovered'], as_=['type', 'count']).mark_point(size=30,
    #                                                                                                       opacity=0.6).encode(
    #     x='Day:T',
    #     y='count:Q',
    #     tooltip=['Day', 'count:Q', 'type:N'],
    #     color='type:N').interactive()
    #
    # st.altair_chart(ch, use_container_width=True)

    ch = alt.Chart(d_SIR).transform_fold(['I', 'R'], as_=['status', 'count']).mark_point(size=30,
                                                                                         opacity=0.6).encode(
        x='Day:T',
        y='count:Q',
        tooltip=['Day', 'count:Q', 'status:N'],
        color='status:N').interactive()

    st.altair_chart(ch, use_container_width=True)

    st.header("Curve Fitting")
    curve_fitting()

    st.header("SIR Model")
    S0, I0, R0 = list(d_SIR.S)[0], list(d_SIR.I)[0], list(d_SIR.R)[0]

    ode(S0, I0, R0)

    def sir_ode(SIR, t, beta, gamma, N):
        S, I, R = SIR

        dS = -beta * ((S * I) / N)
        dI = beta * ((S * I) / N) - gamma * I
        dR = gamma * I

        dydt = [dS, dI, dR]

        return dydt

    def opti(t, _beta, _gamma):
        y0 = [S0, I0, R0]

        sol = odeint(sir_ode, y0, t, args=(_beta, _gamma, S0 + I0 + R0))

        return np.hstack((sol[:, 1], sol[:, 2]))
        #return sol[:, 1]

    # st.dataframe(d_SIR.head())

    # popt, pcov = curve_fit(opti, t, np.hstack((d_SIR.I.to_numpy(), d_SIR.R.to_numpy())), bounds=(0, [np.inf, 1]))

    st.subheader(r"Optimize $\beta$, $\gamma$")
    st.markdown(r"""
    $\beta$, $\gamma$ parameters can be optimized based on data using by combining `curve_fit` and `odeint`.
    """)
    t = np.arange(len(d_SIR))
    #popt, pcov = curve_fit(opti, t, d_SIR.I.to_numpy(), bounds=(0, [np.inf, 1]))
    popt, pcov = curve_fit(opti, t, np.hstack((d_SIR.I.to_numpy(), d_SIR.R.to_numpy())), bounds=(0, [np.inf, 1]))

    beta, gamma = popt[0], popt[1]

    st.markdown(r">%s %s optimal paramaters found to be $\beta$ = %.2f and $\gamma$ = %.2f " % (province, country, beta, gamma))

    ch = alt.Chart(pd.DataFrame(dict(t=t, I=d_SIR.I))).mark_point(size=30, opacity=0.6,
                                                                  color="#512b58").encode(
        x='t:Q',
        y='I:Q').interactive()

    sol = odeint(sir_ode, [S0, I0, R0], t, args=(beta, gamma, N))

    chP = alt.Chart(pd.DataFrame(dict(t=t, I=sol[:, 1]))).mark_line(color="#3b6978").encode(
        x='t:Q',
        y='I:Q').interactive()

    st.altair_chart((ch + chP), use_container_width=True)

    ch = alt.Chart(pd.DataFrame(dict(t=t, R=d_SIR.R))).mark_point(size=30, opacity=0.6,
                                                                  color="#512b58").encode(
        x='t:Q',
        y='R:Q').interactive()

    chP = alt.Chart(pd.DataFrame(dict(t=t, R=sol[:, 2]))).mark_line(color="#3b6978").encode(
        x='t:Q',
        y='R:Q').interactive()

    st.altair_chart((ch + chP), use_container_width=True)
     hist_values = grade['Grade Stanine (GS) VQ'].value_counts(
     ).to_frame()
     hist_values.columns = ['Count']
     for i in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
         if i in hist_values.index:
             continue
         else:
             hist_values.loc[i] = [0]
     hist_values.reset_index(inplace=True)
     hist_values = hist_values.rename(
         columns={'index': 'Performance Group'})
     hist_values['Performance Group'] = hist_values[
         'Performance Group'].astype('Int64').astype(str)
     test = alt.Chart(hist_values).mark_bar().encode(
         x='Performance Group', y='Count', tooltip=['Count'])
     st.altair_chart(test, use_container_width=True)
 else:
     st.subheader(str('Grade Stanine (GS) VQN') + ' Performance Group')
     hist_values = grade['Grade Stanine (GS) VQN'].value_counts(
     ).to_frame()
     hist_values.columns = ['Count']
     for i in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
         if i in hist_values.index:
             continue
         else:
             hist_values.loc[i] = [0]
     hist_values.reset_index(inplace=True)
     hist_values = hist_values.rename(
         columns={'index': 'Performance Group'})
     hist_values['Performance Group'] = hist_values[
         'Performance Group'].astype('Int64').astype(str)
Exemple #24
0
        data = datasets.load_breast_cancer()
        st.write("""dataset breast""")
    else:
        data = datasets.load_wine()
        st.write("""dataset wine""")
    x = data.data
    y = data.target
    x1 = pd.DataFrame(x, columns=data.feature_names)
    x2 = pd.DataFrame(y)
    return x, y, x1, x2


x, y, x1, x2 = select_dataset(dataset)
#x1=frame(dataset)
st.write(""" ## SHAPE OF DATA IS""", x.shape)
st.write(""" ## No of classes  """, len(np.unique(y)))
box = st.selectbox(""" display data """, options=["inputdata", "targetdata"])
if box == "inputdata":
    st.write(""" ## Input Data""", x1.head())
elif box == "targetdata":
    st.write(""" ## target Data""", x2.head())
#st.write("""# heat map """,sn.heatmap(x1.corr(),annot=True))
#box1=st.selectbox("""## PLOT""",options=["inputdata","targetdata"])
st.bar_chart(x1)
st.area_chart(x1)
st.altair_chart(x1)

import bs4
Href = "https://app.powerbi.com/reportEmbed?reportId=55021219-4186-4a3b-b904-4df92fd29bb0&autoAuth=true&ctid=bf93bb5e-ecf0-4e3d-be0e-79b5cc527a48&config=eyJjbHVzdGVyVXJsIjoiaHR0cHM6Ly93YWJpLWluZGlhLWNlbnRyYWwtYS1wcmltYXJ5LXJlZGlyZWN0LmFuYWx5c2lzLndpbmRvd3MubmV0LyJ9"
st.write(Href)
Exemple #25
0
def main():
    # st.title("StreamBible")
    stc.html(HTML_BANNER)
    menu = ["Home", "MultiVerse", "About"]

    df = load_bible("data/KJV_Bible.csv")

    choice = st.sidebar.selectbox("Menu", menu)

    if choice == "Home":
        st.subheader("Single Verse Search")
        # st.dataframe(df)

        book_list = df['book'].unique().tolist()
        book_name = st.sidebar.selectbox("Books", book_list)
        chapter = st.sidebar.number_input("Chapter", 1)
        verse = st.sidebar.number_input("Verse", 1)
        bible_df = df[df['book'] == book_name]
        # st.dataframe(bible_df)

        # Layout
        c1, c2 = st.beta_columns([2, 1])
        # Single Verse Layout
        with c1:
            try:
                selected_passage = bible_df[(bible_df["chapter"] == chapter)
                                            & (bible_df["verse"] == verse)]
                passage_details = "{0} Chapter::{1} Verse::{2}".format(
                    book_name, chapter, verse)
                st.info(passage_details)
                passage = "{}".format(selected_passage["text"].values[0])
                st.write(passage)
            except Exception as e:
                st.warning(e.args)
            except:
                st.warning("Book out of Range")

        with c2:
            st.success("Verse of the Day")
            chapter_list = range(10)
            verse_list = range(20)
            ch_choice = random.choice(chapter_list)
            vs_choice = random.choice(verse_list)
            random_book_name = random.choice(book_list)

            st.write("Book:{},Ch:{},Vs:{}".format(random_book_name, ch_choice,
                                                  vs_choice))
            rand_bible_df = df[df["book"] == random_book_name]

            try:
                randomly_selected_passage = rand_bible_df[
                    (rand_bible_df["chapter"] == ch_choice)
                    & (rand_bible_df["verse"] == vs_choice)]
                mytext = randomly_selected_passage["text"].values[0]
            except:
                mytext = rand_bible_df[(rand_bible_df["chapter"] == 1) & (
                    rand_bible_df["verse"] == 1)]["text"].values[0]

            # st.write(mytext)

            stc.html(HTML_RANDOM_TEMPLATE.format(mytext), height=300)

        # Search Topic/Term
        search_term = st.text_input("Term/Topic")
        with st.beta_expander("View Results"):
            retrieved_df = df[df["text"].str.contains(search_term)]
            st.dataframe(retrieved_df[["book", "chapter", "verse", "text"]])

    if choice == "MultiVerse":
        st.subheader("MultiVerse Retrieval")
        book_list = df["book"].unique().tolist()
        book_name = st.sidebar.selectbox("Book", book_list)
        chapter = st.sidebar.number_input("Chapter", 1)
        bible_df = df[df["book"] == book_name]
        all_verse = bible_df["verse"].unique().tolist()
        verse = st.sidebar.multiselect("Verse", all_verse, default=1)
        selected_passage = bible_df.iloc[verse]
        st.dataframe(selected_passage)
        passage_details = "{} Chapter::{} Verse::{}".format(
            book_name, chapter, verse)
        st.info(passage_details)

        # Layout
        col1, col2 = st.beta_columns(2)
        # Join all text as a sentence
        docx = " ".join(selected_passage["text"].unique().tolist())

        with col1:
            st.info("Details")
            for row in selected_passage.iterrows():
                st.write(row["text"])

        with col2:
            st.success("StudyMode")
            with st.beta_expander("Visualize Entities"):
                # st.write(docx)
                render_entities(docx)

            with st.beta_expander("Visualize Pos Tags"):
                tagged_docx = get_tags(docx)
                processed_tags = mytag_visualizer(tagged_docx)
                # st.write(processed_tags)  # Raw
                stc.html(processed_tags, height=1000, scrolling=True)

            with st.beta_expander("Keywords"):
                processed_docx = nfx.remove_stopwords(docx)
                keywords_tokens = get_most_common_tokens(processed_docx, 5)
                st.write(keywords_tokens)

        with st.beta_expander("Verse Curve"):
            plot_mendelhall_curve(docx)

        with st.beta_expander("Word Freq Plot"):
            plot_word_freq_with_altair(docx)

        with st.beta_expander("Pos Tags Plot"):
            tagged_docx = get_tags(docx)
            tagged_df = pd.DataFrame(tagged_docx, columns=["Tokens", "Tags"])
            # st.dataframe(tagged_df)
            df_tag_count = tagged_df["Tags"].value_counts().to_frame("counts")
            df_tag_count["tag_type"] = df_tag_count.index
            # st.dataframe(df_tag_count)

            c = alt.Chart(df_tag_count).mark_bar().encode(x="tag_type",
                                                          y="counts")
            st.altair_chart(c, use_container_width=True)

    else:
        st.subheader("About")
        st.text("Build with Streamlit")
        st.text("Example from Jesse E.Agbe(JCharis)")
        st.success(cat)
cas = alt.Chart(covid[covid["Country"] == cty],
                title="Scatter Chart",
                width=500,
                height=400).mark_circle(color='green').encode(
                    x="Date",
                    y="New cases",
                    size="New deaths",
                    color="New recovered",
                    tooltip=[
                        "Date", "Country", "New cases", "New deaths",
                        "New recovered"
                    ]).interactive()

if daily == 'Daily New Cases':
    if typ == "Line Chart":
        st.altair_chart(ca.mark_line(color='firebrick'))
    else:
        st.altair_chart(ca.mark_circle(color='firebrick'))
elif daily == 'Daily New Recoveries':
    if typ == "Line Chart":
        st.altair_chart(re.mark_line(color='green'))
    else:
        st.altair_chart(re.mark_circle(color='green'))
elif daily == 'Daily New Deaths':
    if typ == "Line Chart":
        st.altair_chart(de.mark_line(color='purple'))
    else:
        st.altair_chart(de.mark_circle(color='purple'))

"Visualizing Daily New Cases, recoveries and deaths in a Single Chart"
"In Scatter Chart, Circle represent daily new cases, size of the circle shows the daily deaths and the color variation shows the daily recoveries"
Exemple #27
0
import streamlit as st
import os

st.write("hello")

with st.echo(code_location='below'):
    total_points = st.slider("Number of points in spiral", 1, 5000, 2000)
    num_turns = st.slider("Number of turns in spiral", 1, 100, 9)

    Point = namedtuple('Point', 'x y')
    data = []

    points_per_turn = total_points / num_turns

    for curr_point_num in range(total_points):
        curr_turn, i = divmod(curr_point_num, points_per_turn)
        angle = (curr_turn + 1) * 2 * math.pi * i / points_per_turn
        radius = curr_point_num / total_points
        x = radius * math.cos(angle)
        y = radius * math.sin(angle)
        data.append(Point(x, y))

    st.altair_chart(alt.Chart(pd.DataFrame(data), height=500, width=500)
        .mark_circle(color='#0068c9', opacity=0.5)
        .encode(x='x:Q', y='y:Q'))

b = st.checkbox("click  4 logs")
if b:
    print("hello")
def drawTop15(years, default=True, new=True):
    st.write("")
    st.write(
        "Evolution of the price per m² of the 15 largest cities in France between "
        + years[0] + " and " + years[-1] +
        " (Without Strasbourg which is in Bas-Rhin).")

    city_df_dict = {}
    top15_df = []

    if not new:
        top15_df = st.session_state['top15_df']
    elif default:
        df = pd.read_csv('./data/top15melt.csv')
        top15_df = df.loc[df['date'].str.contains('|'.join(years))]
    else:
        df = pd.read_csv('./data/top15.csv')
        for city in df['Commune'].unique():
            data = []
            for year in years:
                for i in range(1, 13):
                    date = str(year) + '-' + str(i).zfill(2)
                    ordinalDate = pd.to_datetime(date).toordinal()
                    mean = round(
                        df.loc[(df['date'] == date) & (df['Commune'] == city),
                               'pricePerM²'].mean(), 2)
                    data.append([date, ordinalDate, city, mean])

            # Create the pandas DataFrame
            new_df = pd.DataFrame(data,
                                  columns=[
                                      'date', 'ordinalDate', 'city',
                                      'Average Monthly Price'
                                  ])
            new_df.dropna(subset=['Average Monthly Price'], inplace=True)
            new_df['EWMA-12 Average Monthly Price'] = new_df[
                'Average Monthly Price'].ewm(span=12).mean()
            city_df_dict[str(city)] = new_df

        new_df = []

        for city, city_df in city_df_dict.items():
            for year in years:
                for i in range(1, 13):
                    date = str(year) + '-' + str(i).zfill(2)
                    ordinalDate = pd.to_datetime(date).toordinal()
                    date = pd.to_datetime(date)
                    a, b, r, p_value, std_err = linregress(
                        city_df['ordinalDate'],
                        city_df['EWMA-12 Average Monthly Price'])
                    linear = a * ordinalDate + b
                    new_df.append([date, city, linear])

        top15_df = pd.DataFrame(new_df, columns=['date', 'city', 'price'])

        st.session_state['top15_df'] = top15_df

    sortCity = top15_df.loc[top15_df['date'] == years[0] + '-01-01',
                            ['price', 'city']]
    sortCity.sort_values(by=['price'], inplace=True, ascending=False)
    sortCity = sortCity['city'].to_numpy()

    selection = alt.selection_multi(fields=['city'], bind='legend')

    base = alt.Chart(top15_df,
                     title="City TOP 15 (" + years[0] + "-" + years[-1] +
                     ")").encode(x=alt.X('date:T', axis=alt.Axis(title=None)),
                                 y=alt.Y('price:Q',
                                         scale=alt.Scale(zero=False),
                                         axis=alt.Axis(title='price per m²')),
                                 color=alt.Color('city',
                                                 scale=alt.Scale(
                                                     scheme='yellowgreenblue',
                                                     reverse=True),
                                                 sort=sortCity,
                                                 legend=alt.Legend(title="")),
                                 tooltip=['city'])

    points = base.mark_circle().encode(opacity=alt.value(0))

    lines = base.mark_line(interpolate='basis').encode(
        size=alt.value(3),
        opacity=alt.condition(selection, alt.value(1), alt.value(0.2)),
    ).add_selection(selection).properties(height=500)

    c = alt.layer(lines, points)

    st.altair_chart(c, use_container_width=True)
Exemple #29
0
# make line chart 2
chart2_y_label = 'Proportion of Variance'
df2.rename(columns={'Proportion': chart2_y_label}, inplace=True)
lines = alt.Chart(df2).mark_line().encode(
    x='Step',
    y=chart2_y_label,
    color='Quantity',
    tooltip=['Step', chart2_y_label, 'Quantity'])
rule = alt.Chart(pd.DataFrame([{
    'current_step': current_step
}])).mark_rule(color='black').encode(x=alt.X('current_step', title='Step'))
line_chart2 = lines + rule

# make heat chart
heat_chart = alt.Chart(df3[df3['s'] == current_step]).mark_rect().encode(
    alt.X('x:O', axis=None), alt.Y('y:O', axis=None), color='z:Q').properties(
        width=get_width_height_pixels(scenario_id)[0],
        height=get_width_height_pixels(scenario_id)[1],
    )

# show charts
st.header('Co-occurrence Data')
st.altair_chart(heat_chart)
col1, col2 = st.beta_columns(2)
with col1:
    st.header('Information Decomposition')
    st.altair_chart(line_chart1, use_container_width=True)
with col2:
    st.header('Singular Value Decomposition')
    st.altair_chart(line_chart2, use_container_width=True)
Exemple #30
0
def spell(spell_inputs):

    mana = spell_inputs
    st.markdown("## Create the graph you would like to animate")
    time_col = st.selectbox("Select time column", mana.columns)
    curr_time = st.selectbox("Select time", mana[time_col].unique())

    x_col = st.selectbox("Select x axis for line chart", mana.columns)
    xcol_string = x_col + ":O"
    if st.checkbox("Show as continuous?", key="time_line_x_is_cont"):
        xcol_string = x_col + ":Q"
    y_col = st.selectbox("Select y axis for line chart", mana.columns)
    ycol_string = alt.Y(y_col)
    if st.checkbox("Show as sorted?", key="time_line_sort_y"):
        ycol_string = alt.Y(y_col, sort="-x")
    z_col = st.selectbox("Select z axis for line chart", mana.columns)

    time_mana = mana.loc[mana[time_col] == curr_time]

    chart = (alt.Chart(time_mana).mark_line(point=True).encode(
        y=ycol_string,
        x=xcol_string,
        color=z_col,
        tooltip=list(time_mana.columns),
    ).properties(title="Line graph of " + x_col + "," + y_col + " at " +
                 str(curr_time)).configure_title(fontSize=20, ).configure_axis(
                     labelFontSize=20, titleFontSize=20).configure_legend(
                         labelFontSize=20,
                         titleFontSize=20)).properties(height=700)

    st.altair_chart(chart, use_container_width=True)

    # basicaly the animate button should make n graphs and show them and have a time.sleep in between

    st.markdown("## Animate the graph above using " + time_col +
                " as the time.")

    time_interval = st.number_input("Time interval", 0.0, None, value=1.0)

    if st.button("Animate Graph"):

        # declare an empty obj here then update it in loop
        time_chart = st.empty()
        sorted_vals = mana[time_col].unique()
        sorted_vals.sort()
        for times in sorted_vals:

            curr_time_mana = mana.loc[mana[time_col] <= times]
            st.write(times)
            curr_chart = (alt.Chart(curr_time_mana).mark_line(
                point=True).encode(
                    y=ycol_string,
                    x=xcol_string,
                    color=z_col,
                    tooltip=list(time_mana.columns),
                ).properties(
                    title="Line graph of " + x_col + "," + y_col + " at " +
                    str(times)).configure_title(fontSize=20, ).configure_axis(
                        labelFontSize=20, titleFontSize=20).configure_legend(
                            labelFontSize=20,
                            titleFontSize=20)).properties(height=700)
            time_chart.altair_chart(curr_chart, use_container_width=True)
            # sleep
            time.sleep(time_interval)

    return None, mana