Ejemplo n.º 1
0
def write_figure_plotly(
        figure: go.Figure,
        results_path: str,
        file_format=fledge.config.config['plots']['file_format']):
    """Utility function for writing / storing plotly figure to output file. File format can be given with
    `file_format` keyword argument, otherwise the default is obtained from config parameter `plots/file_format`.

    - `results_path` should be given as file name without file extension, because the file extension is appended
      automatically based on given `file_format`.
    - Valid file formats: 'png', 'jpg', 'jpeg', 'webp', 'svg', 'pdf', 'html', 'json'
    """

    if file_format in ['png', 'jpg', 'jpeg', 'webp', 'svg', 'pdf']:
        pio.write_image(
            figure,
            f"{results_path}.{file_format}",
            width=fledge.config.config['plots']['plotly_figure_width'],
            height=fledge.config.config['plots']['plotly_figure_height'])
    elif file_format in ['html']:
        pio.write_html(figure, f"{results_path}.{file_format}")
    elif file_format in ['json']:
        pio.write_json(figure, f"{results_path}.{file_format}")
    else:
        raise ValueError(
            f"Invalid `file_format` for `write_figure_plotly`: {file_format}"
            f" - Valid file formats: 'png', 'jpg', 'jpeg', 'webp', 'svg', 'pdf', 'html', 'json'"
        )
Ejemplo n.º 2
0
def test_write_json_filelike(fig1, pretty, remove_uids):
    # Configure file-like mock
    filemock = MagicMock()

    # write_json to mock file
    pio.write_json(fig1, filemock, pretty=pretty, remove_uids=remove_uids)

    # check write contents
    expected = pio.to_json(fig1, pretty=pretty, remove_uids=remove_uids)
    filemock.write.assert_called_once_with(expected)
Ejemplo n.º 3
0
def test_write_json_filelike(fig1, pretty, remove_uids):
    # Configure file-like mock
    filemock = MagicMock()

    # write_json to mock file
    pio.write_json(fig1, filemock, pretty=pretty, remove_uids=remove_uids)

    # check write contents
    expected = pio.to_json(fig1, pretty=pretty, remove_uids=remove_uids)
    filemock.write.assert_called_once_with(expected)
Ejemplo n.º 4
0
 def save_plotly_fig(self, iter_id, fig, sub_dir):
     if self.fig_save_dir:
         sub_dir = sub_dir[:].replace(" ", "_")
         root = os.path.join(self.fig_save_dir, sub_dir)
         os.makedirs(root, exist_ok=True)
         file_name = "{:09d}".format(iter_id)
         path = os.path.join(root, file_name + ".json")
         pio.write_json(fig, path)
         if self.SAVE_RENDERED_FIGS:
             path = os.path.join(root, file_name)
             pio.write_image(fig, path + ".png")
             pio.write_image(fig, path + ".svg")
Ejemplo n.º 5
0
def output_plot_file(figure, path, output_format, width, height):
    server_config = {
        'plotlyServerURL': 'https://chart-studio.plotly.com',
        'showLink': True
    }
    if 'studio' in output_format:
        pio.write_html(figure,
                       file=path + '.html',
                       config={
                           'editable': True,
                           'displaylogo': False,
                           'plotlyServerURL':
                           'https://chart-studio.plotly.com',
                           'showLink': True,
                           'showSendToCloud': True,
                           'toImageButtonOptions': {
                               'format': 'png',
                               'width': width,
                               'height': height,
                               'scale': 6
                           }
                       })
    elif 'html' in output_format:
        pio.write_html(figure, file=path + '.html', config=server_config)
    elif 'png' in output_format:
        pio.write_image(figure,
                        file=path + '.png',
                        format='png',
                        width=width,
                        height=height,
                        scale=6)
    elif 'jpg' in output_format:
        pio.write_image(figure,
                        file=path + '.jpg',
                        format='jpg',
                        width=width,
                        height=height,
                        scale=6)
    elif 'svg' in output_format:
        pio.write_image(figure,
                        file=path + '.svg',
                        format='svg',
                        width=width,
                        height=height)
    elif 'pdf' in output_format:
        pio.write_image(figure,
                        file=path + '.pdf',
                        format='pdf',
                        width=width,
                        height=height)
    elif 'json' in output_format:
        pio.write_json(figure, file=path)
Ejemplo n.º 6
0
def test_write_json_from_file_string(fig1, pretty, remove_uids):
    with tempfile.TemporaryDirectory() as dir_name:

        # Write json
        path = os.path.join(dir_name, "fig1.json")
        pio.write_json(fig1, path, pretty=pretty, remove_uids=remove_uids)

        # Open as text file
        with open(path, "r") as f:
            result = f.read()

        # Check contents that were written
        expected = pio.to_json(fig1, pretty=pretty, remove_uids=remove_uids)
        assert result == expected
Ejemplo n.º 7
0
def test_write_json_from_file_string(fig1, pretty, remove_uids):
    with tempfile.TemporaryDirectory() as dir_name:

        # Write json
        path = os.path.join(dir_name, 'fig1.json')
        pio.write_json(fig1, path, pretty=pretty, remove_uids=remove_uids)

        # Open as text file
        with open(path, 'r') as f:
            result = f.read()

        # Check contents that were written
        expected = pio.to_json(fig1, pretty=pretty, remove_uids=remove_uids)
        assert result == expected
Ejemplo n.º 8
0
def main():
    # df = pd.read_csv("/home/acc/twitter.csv")
    keyword = str(sys.argv[1])
    thresh = int(sys.argv[2])
    sql = '''select text, lat, lon, full_name, sentiment  from tweets_transform where lower(text) like '%{0}%';'''.format(
        keyword)
    # fetch data from sql
    conn = psycopg2.connect(
        "dbname='twitter' user='******' host='10.0.0.26' port = '5432' password='******'"
    )
    cur = conn.cursor()
    cur.execute(sql)
    df = pd.DataFrame(cur.fetchall())
    df.columns = ['text', 'lat', 'lon', 'full_name', 'sentiment']
    df['sentiment'] = (df['sentiment'] - 5) / 10
    df = df.dropna()
    # transform
    df.lat = df.lat.astype('float')
    df.lon = df.lon.astype('float')
    # aggregate
    DF = df.groupby('full_name').mean()
    DF['n'] = df.groupby('full_name').count()['sentiment'].values
    DF = df.groupby('full_name').mean()
    DF['n'] = df.groupby('full_name').count()['sentiment'].values
    DF = DF.sort_values('n', ascending=False).reset_index()
    # filter out small towns
    DF = DF[DF['n'] > thresh]
    # plot aggregates
    fig = px.scatter_mapbox(DF,
                            lat="lat",
                            lon="lon",
                            hover_data=["full_name"],
                            mapbox_style="carto-positron",
                            color="sentiment",
                            size="n",
                            zoom=3.5,
                            opacity=.5)
    pio.write_json(fig, '/home/acc/output/tweet1.plotly')
    # plot individual data
    fig = px.scatter_mapbox(df,
                            lat="lat",
                            lon="lon",
                            hover_data=["text", "full_name", "sentiment"],
                            mapbox_style="carto-positron",
                            color="sentiment",
                            zoom=3.5,
                            opacity=.5)
    pio.write_json(fig, '/home/acc/output/tweet2.plotly')
def percentage(passed_name, field_name, field_descriptor):
    full_path = folder_path + passed_name + '_perc.json'

    helper = h.Helpers()
    if not path.exists(full_path):
        db = helper.get_census()

        state_name = "Alabama"
        result = _percentage_helper(db, helper, state_name, passed_name)
        df = pd.DataFrame(list(result))
        print("Added", state_name)

        for filename in os.listdir(state_path):
            state_name = filename.split(".")[0]
            if state_name != "Alabama":
                result = _percentage_helper(db, helper, state_name,
                                            passed_name)
                temp_df = pd.DataFrame(list(result))
                df = df.append(temp_df, ignore_index=True)
                print("Added", state_name)

        df = helper.join_fips_data(df)

        df = df[[
            'fips', 'Location', field_name + "_Yes", field_name + "_Not",
            field_name
        ]]

        with open(full_path, 'w') as file:
            df.to_json(file, orient='records')
    else:
        print(passed_name + "Exists")
        with open(full_path, 'r') as file:
            df = pd.read_json(file, orient='records', dtype=False)

    labels = {field_name: field_descriptor}
    color = field_name

    fig = helper.plot(df, color, labels)

    file_path = "../web_json/hispanic/" + passed_name + "_percentages.json"

    if not path.exists(file_path):
        import plotly.io as pio
        pio.write_json(fig, file=file_path)
    else:
        print("Plot already written")
Ejemplo n.º 10
0
def generate_model_plot(df, x, y, preds, out_file=None, title=None):
    plt = px.scatter(
        df,
        x=x,
        y=y,
        color="Playoffs",
        hover_name="Team",
        hover_data=["Year"],
        title=title,
        labels={
            "Made_Playoffs": "Playoffs",
        },
        template="plotly_dark",
    )
    plt_ols = plt.add_traces(
        go.Scatter(x=df[x], y=preds, mode="lines", name="OLS"))
    pio.write_json(plt_ols, file=out_file)
def racial_diversity():
    df = _prepare_dataframe()

    df = _calculate_diversity_index(df)

    df['Text'] = '<br>Breakdowns by Percentages: ' + \
                 '<br>Asian (Hispanic, Not Hispanic): (' + df['Asian_Perc_Yes'].round(2).astype(str) + '%, ' + df[
                     'Asian_Perc_Not'].round(2).astype(str) + '%)' + \
                 '<br>Black (Hispanic, Not Hispanic): (' + df['Black_Perc_Yes'].round(2).astype(str) + '%, ' + df[
                     'Black_Perc_Not'].round(2).astype(str) + '%)' + \
                 '<br>Indian or Alaskan Native (Hispanic, Not Hispanic): (' + df['Indian_Alaska_Perc_Yes'].round(
        2).astype(str) + '%, ' + df['Indian_Alaska_Perc_Not'].round(2).astype(str) + '%)' + \
                 '<br>Other (Hispanic, Not Hispanic): (' + df['Other_Perc_Yes'].round(2).astype(str) + '%, ' + df[
                     'Other_Perc_Not'].round(2).astype(str) + '%)' + \
                 '<br>Native Hawaiian and Other Pacific (Hispanic, Not Hispanic): (' + df['Pacific_Perc_Yes'].round(
        2).astype(str) + '%, ' + df['Pacific_Perc_Not'].round(2).astype(str) + '%)' + \
                 '<br>White (Hispanic, Not Hispanic): (' + df['White_Perc_Yes'].round(2).astype(str) + '%, ' + df[
                     'White_Perc_Not'].round(2).astype(str) + '%)' + \
                 '<br>Two or More (Hispanic, Not Hispanic): (' + df['Two_More_Perc_Yes'].round(2).astype(str) + '%, ' + \
                 df['Two_More_Perc_Not'].round(2).astype(str) + '%)'

    file_path = "../Census_Analysis/Hispanic/Race/diversity_index.json"

    if not path.exists(file_path):
        with open(file_path, 'w') as f:
            df.to_json(f, orient='records')
    else:
        print('File Exists')

    helper = h.Helpers()

    labels = {"Diversity Index": "Diversity Index (See Readme)"}
    color = 'Diversity Index'

    fig = helper.plot(df, color, labels, hover_data=True)

    file_path = "../web_json/diversity/hispanic_racial_diversity.json"

    if not path.exists(file_path):
        import plotly.io as pio
        pio.write_json(fig, file=file_path)
    else:
        print("Plot already written")
Ejemplo n.º 12
0
def percentage_hispanic():
    helper = h.Helpers()

    if not path.exists("../Census_Analysis/hispanic_perc.json"):
        db = helper.get_census()

        state_name = "Alabama"
        result = _percentage_hispanic_helper(db, helper, state_name)
        df = pd.DataFrame(list(result))
        print("Added", state_name)

        for filename in os.listdir("../State_JSONs"):
            state_name = filename.split(".")[0]
            if state_name != "Alabama":
                result = _percentage_hispanic_helper(db, helper, state_name)
                temp_df = pd.DataFrame(list(result))
                df = df.append(temp_df, ignore_index=True)
                print("Added", state_name)

        df = helper.join_fips_data(df)

        df = df[['fips', 'Hispanic_Perc', 'Location']]

        with open("../Census_Analysis/hispanic_perc.json", 'w') as file:
            df.to_json(file, orient='records')
    else:
        with open("../Census_Analysis/hispanic_perc.json", 'r') as file:
            df = pd.read_json(file, orient='records', dtype=False)

    color = 'Hispanic_Perc'
    labels = {"Hispanic_Perc": "Hispanic Percentage"}

    fig = helper.plot(df, color, labels)

    file_path = "../web_json/hispanic_percentages.json"

    if not path.exists(file_path):
        import plotly.io as pio
        pio.write_json(fig, file=file_path)
    else:
        print("Plot already written")
Ejemplo n.º 13
0
def main():
    # df = pd.read_csv("/home/acc/twitter.csv")
    df = pd.read_csv(sys.stdin)
    df.to_csv("/home/acc/output/last.csv")
    df = df.drop(0)
    df.columns = ["full_name", "time", "text", "lat", "lon", "sentiment"]
    df['sentiment'] = (df['sentiment'] - 5) / 10
    df = df.dropna()
    # transform
    df.lat = df.lat.astype('float')
    df.lon = df.lon.astype('float')
    # aggregate
    DF = df.groupby('full_name').mean()
    DF['n'] = df.groupby('full_name').count()['sentiment'].values
    DF = df.groupby('full_name').mean()
    DF['n'] = df.groupby('full_name').count()['sentiment'].values
    DF = DF.sort_values('n', ascending=False).reset_index()
    # filter out small towns
    # DF = DF[DF['n'] > thresh]
    # plot aggregates
    fig = px.scatter_mapbox(DF,
                            lat="lat",
                            lon="lon",
                            hover_data=["full_name"],
                            mapbox_style="carto-positron",
                            color="sentiment",
                            size="n",
                            zoom=3.5,
                            opacity=.5)
    pio.write_json(fig, '/home/acc/output/tweet1.plotly')
    # plot individual data
    fig = px.scatter_mapbox(df,
                            lat="lat",
                            lon="lon",
                            hover_data=["text", "full_name", "sentiment"],
                            mapbox_style="carto-positron",
                            color="sentiment",
                            zoom=3.5,
                            opacity=.5)
    pio.write_json(fig, '/home/acc/output/tweet2.plotly')
Ejemplo n.º 14
0
    def create_profile(self, profile, fig, gffig):

        "Create a profile from inputted data which can be launched from again instead of inputting data and processing."

        logging.info(f"Creating new profile with name: {profile}")

        with open(rf"{self.relativepath}\settings.json", 'r') as settings:
            profiles = json.load(settings)

        if profile in profiles["profiles"].keys():
            logging.error(
                f"Creating profile with name {profile} when it already exists."
            )
            raise ValueError(
                "Profile already exists. Please provide a different name or delete existing profile."
            )

        time = datetime.now()
        # Create summary of profile to be shown in -p
        profiles['profiles'][profile] = {
            "Date created": time.strftime("%d/%m/%Y %H:%M"),
            "Antibiotic count": len(self.antibiotics),
            "Number of isolates": len(self.isolateIDs),
            "Antibiotics": ', '.join(self.antibiotics[:5]) + '...',
            "Isolates": ', '.join(self.isolateIDs[:5]) + '...'
        }

        with open(rf"{self.relativepath}\settings.json", 'w') as settings:
            logging.info("Saving new profile to settings.")
            json.dump(profiles, settings, indent=4)

        pio.write_json(fig, rf"{self.relativepath}\profiles\{profile}.json")
        if self.Genotype:
            pio.write_json(gffig,
                           rf"{self.relativepath}\profiles\{profile}_gf.json")

        logging.info("Profile saved.")
        print("New profile has been saved.")
Ejemplo n.º 15
0
def game_simulator(file, half, start, stop):
    # start perf monitoring so we see how long it takes
    t1_start = perf_counter()
    print("Process started at: ", datetime.now())

    file_plus_path = "data/" + file
    df = pd.read_csv(file_plus_path, error_bad_lines=False)

    df["time"] = df["time"].astype(float)
    df["half"] = df["half"].astype(int)

    # Filter dataframe to include only activity within the time window provided
    df = df[(df["half"] == half) & (df["time"] >= start) & (df["time"] <= stop)]

    # Set the marker size for scatterplot points
    df["size"] = 10

    # Make the ball marker size smaller than the other markers
    df.loc[df["jersey_number"] == 0, "size"] = 3

    # Hide substitutes and ball out of bounds by making their size 0
    df.loc[df["x"] == None, "size"] = 0
    df.loc[df["y"] == None, "size"] = 0
    df.loc[df["x"] == None, "jersey_number"] = ""
    df.loc[df["y"] == None, "jersey_number"] = ""

    # Limit the dataframe to include only columns that we're going to use
    df = df[["half", "time", "x", "y", "team", "size", "jersey_number"]]

    # Replace nan with None id df
    df = df.where(pd.notnull(df), None)

    # Rename teamId column so it looks nicer when displayed on Legend
    df = df.rename(columns={"team": "Team"})

    # Limit time column to two decimals
    df["time"] = df["time"].astype(float)
    df["time"] = df.time.round(decimals=2)

    # else:
    # df = df.sort_values(by=['half', 'time'])

    colour0 = "#009BFF"
    colour1 = "grey"
    colour_ball = "red"

    # Make sure jersey number is an int not a decimal
    df["jersey_number"] = (
        df["jersey_number"].astype("str").replace("\.0", "", regex=True)
    )

    # null out jersey number field for ball or it shows a zero on the marker, which looks stupid
    df["jersey_number"].replace("0", "", inplace=True)

    color_discrete_map = {"Home": colour0, "Away": colour1, "Ball": colour_ball}

    # Plotly Express version
    fig = px.scatter(
        df,
        x="x",
        y="y",
        color="Team",
        hover_name="jersey_number",
        animation_frame="time",
        animation_group="jersey_number",
        range_x=[-0.05, 1.05],
        range_y=[-0.05, 1.05],
        size="size",
        size_max=10,
        opacity=0.8,
        color_discrete_map=color_discrete_map,
        text="jersey_number",
        hover_data={
            "x": False,
            "y": False,
            "time": False,
            "size": False,
            "Team": False,
            "jersey_number": False,
        },
    )

    # Add corner flags to prevent zoom and pitch distortion
    fig.add_scatter(
        x=[0, 0, 1, 1],
        y=[0, 1, 0, 1],
        mode="markers",
        marker=dict(size=1, color="red"),
        name="Flags",
    )

    # Make jersey number really small inside markers
    fig.update_traces(
        textfont_size=7, textfont_color="white", hovertemplate=None, hoverinfo="none"
    )
    fig.update_yaxes(autorange="reversed")

    fig.update_layout(
        xaxis=dict(range=[-0.05, 1.05]),
        yaxis=dict(range=[-0.05, 1.05]),
        coloraxis_showscale=False,
    )

    # Remove side color scale and hide zero and gridlines
    fig.update_layout(
        xaxis=dict(showgrid=False, zeroline=False),
        yaxis=dict(showgrid=False, zeroline=False),
    )

    # Disable axis ticks and labels
    fig.update_xaxes(showticklabels=False, title_text="")
    fig.update_yaxes(showticklabels=False, title_text="")

    image_file = "assets/Pitch.png"
    image_path = os.path.join(os.getcwd(), image_file)

    from PIL import Image

    img = Image.open(image_path)

    fig.add_layout_image(
        dict(
            source=img,
            xref="x",
            yref="y",
            x=0,
            y=0,
            sizex=1,
            sizey=1,
            sizing="stretch",
            opacity=0.7,
            layer="below",
        )
    )

    ############  Playback Speed Setting  ##################
    fig.layout.updatemenus[0].buttons[0].args[1]["frame"]["duration"] = 200
    ###############################################

    fig["layout"]["sliders"][0]["pad"]["t"] = 0
    fig["layout"]["updatemenus"][0]["pad"]["t"] = 0

    pio.templates["custom_dark"] = go.layout.Template()
    pio.templates["custom_dark"]["layout"]["paper_bgcolor"] = "#282828"
    pio.templates["custom_dark"]["layout"]["plot_bgcolor"] = "#282828"

    fig.update_layout(
        template="custom_dark",
        xaxis=dict(showgrid=False, showticklabels=False),
        # plot_bgcolor='#282828',
        # paper_bgcolor='#282828'
    )

    # slider format and adjustments for aesthetic purposes
    fig["layout"]["sliders"][0]["pad"] = dict(r=0, t=0.0,)
    fig["layout"]["sliders"][0]["minorticklen"] = 2
    fig["layout"]["sliders"][0]["ticklen"] = 5
    fig["layout"]["sliders"][0]["tickcolor"] = "grey"
    fig["layout"]["sliders"][0]["font"]["color"] = "grey"
    fig["layout"]["sliders"][0]["bgcolor"] = "grey"
    fig["layout"]["sliders"][0]["bordercolor"] = "grey"
    fig["layout"]["template"]["data"]["scatter"][0]["marker"]["line"][
        "color"
    ] = "red"
    fig["layout"]["template"]["data"]["scatter"][0]["marker"]["opacity"] = 0.9

    fig.update_layout(margin=dict(l=20, r=20, b=20, t=20))

    fig.update_layout(
        legend_orientation="v", transition={"duration": 0, "ordering": "traces first"}
    )

    # Make sure pitch background image shape doesn't get distorted
    fig.update_yaxes(scaleanchor="x", scaleratio=0.65)

    t1_stop = perf_counter()
    print("Process took " + str((t1_stop - t1_start) / 60) + " minutes of time")

    fig.update_layout(legend=dict(yanchor="top", y=0.95, xanchor="left", x=-0.08))
    fig.update_layout(
        updatemenus=[
            dict(
                type="buttons",
                showactive=False,
                y=-0.14,
                x=-0.08,
                xanchor="left",
                yanchor="bottom",
            )
        ]
    )
    fig.update_layout(autosize=True, hovermode="closest")
    # fig.update_layout(showlegend=False)
    fig.update_layout(legend=dict(font=dict(family="Arial", size=10, color="grey")))

    # Hide corner flag trace in the legend
    for trace in fig["data"]:
        if trace["name"] == "Flags":
            trace["showlegend"] = False

    export = input("Do you wish to export the graph to json (y/n)?:")
    if export == "y":
        export_file_name = input(
            "Please enter a name for the json file to be exported (ending with .json): "
        )
        export_file_name = "data/" + export_file_name
        with open(export_file_name, "w") as f:
            # json_data = fig.to_json()
            pio.write_json(fig, f)
            f.close()

    return fig
Ejemplo n.º 16
0
# # Show image

# %%
import plotly.io as pio
from IPython.display import SVG, display, Image
img_bytes = pio.to_image(fig, format="svg")
display(SVG(img_bytes))

# %%
fig.write_image('out.png')

# %% [markdown]
# # Export json for Plotly viewer

# %%
pio.write_json(fig,'demo_plotly.json')

# %% [markdown]
# # SQL access

# %%
# python database api specification v2.0

# %%
import sqlite3
import os

# %%
if os.path.exists('test.db'):
    os.remove('test.db')
Ejemplo n.º 17
0
fig.add_trace(
    go.Scatter(x=cuarentena[10:], y=act_cum[10:], mode='lines',
               name='Activos'))
fig.add_trace(
    go.Scatter(x=cuarentena[10:],
               y=recup_cum[10:],
               mode='lines',
               name='Recuperados'))

fig.update_layout(
    margin=dict(l=20, r=50, b=20, t=30),
    title='Casos Acumulados',
)

# fig.write_html('grafica1.html', auto_open=True)
io.write_json(fig, 'grafica1.json', pretty=True)
fs.json_js(arch_json='grafica1.json', arch_js='grafica1.js', nombre='grafica1')

# Grafica diario nuevos contagiados vs cuarenten
fig2 = go.Figure()
fig2.add_trace(
    go.Scatter(x=cuarentena[10:],
               y=conf_dia[10:],
               mode='lines',
               name='Nuevos casos',
               showlegend=False))
fig2.update_layout(
    margin=dict(l=20, r=20, b=20, t=30),
    title='Nuevos Casos Diarios',
)