예제 #1
0
파일: figure.py 프로젝트: vlam020/vectorbt
    def show(self, *args, **kwargs) -> None:
        """Show the figure."""
        from vectorbt.settings import plotting

        fig_kwargs = dict(width=self.layout.width, height=self.layout.height)
        show_kwargs = merge_dicts(fig_kwargs, plotting['show'], kwargs)
        _Figure.show(self, *args, **show_kwargs)
def main():
    # Data File Name and file path
    filename = 'us-counties.csv'
    filepath = os.path.join('./data/raw', filename)

    # Import data into DataFrame and rename columns
    df = read_csv(filepath, dtype={'fips': str})
    df.columns = ['date', 'county', 'state', 'zip', 'cases', 'deaths']

    print(df.dtypes)
    print(f'Number of total cases:  {df.cases.sum()}')
    print(f'Number of total deaths:  {df.deaths.sum()}')

    # Extract Year, Month and Date and place in separate columns
    df['year'] = DatetimeIndex(df.date, yearfirst=True).year
    df['month'] = DatetimeIndex(df.date, yearfirst=True).month_name()

    # States and Counties
    states = df.state.unique()
    counties = df.county.unique()

    # Sum of Grouped Dats
    grp_dates = df.groupby(['date', 'state'])['cases', 'deaths'].sum()
    print(grp_dates.sum())

    # Plot Scatter plot of Cases vs Deaths
    renderers.default = 'browser'

    fig = Figure(data=Scatter(x=df.cases, y=df.deaths, mode='markers'))

    fig.show()

    return
    def show_plot(fig: go.Figure) -> None:
        """Save plot to the specified file."""

        # Show only if SHOW_PLOT is True
        if SHOW_PLOT:
            fig.update_layout(template=_plot_layout_template)
            fig.show()
예제 #4
0
def persist_figure(qf_fig: go.Figure, ticker: yf.Ticker):
    """Generate plot from QuantFig and store resulting chart as HTML file.

    Args:
        ticker (yf.Ticker): yfinance ticker symbol, from that qf was derived
        qf (cf.QuantFig): QuantFig object which contains chart data
    """
    # plot figure
    qf_fig.show()

    # store interactive HTML file
    qf_fig.write_html(
        f"""{ticker}_quantFig_{date.today().strftime("%Y_%m_%d")}.html""")
예제 #5
0
def generate(data, to_json=False, mode="freeform"):
    """Generate graph data and show in browser."""
    labels, source, target, value, hovers = [], [], [], [], []
    indexes = {}
    indexes[json.dumps(None)] = 0
    labels.append("")
    # data = find_json(data)
    for idx, (thing, parent, _) in enumerate(walker(data), 1):
        indexes[json.dumps(thing)] = idx
        label = ("{}".format(thing.get("__label", "Unknown")))
        labels.append(label)
        source.append(idx)
        target.append(indexes[json.dumps(parent)])
        value.append(max((thing["__cost"], 0.00001)))  # 0.0 wouldn't plot
        hovers.append(prettify_details(thing))

    fig = Figure(data=[
        Sankey(ids=labels,
               arrangement=mode,
               node=dict(
                   pad=15,
                   thickness=20,
                   line=dict(color="black", width=0.5),
                   label=labels,
                   color=["green"] + ["blue"] * (len(labels) - 1),
               ),
               link=dict(source=source,
                         target=target,
                         value=value,
                         label=hovers,
                         hoverlabel=dict(font=dict(
                             family="Courier New, monospace"))))
    ])

    fig.update_layout(title_text="Execution plan",
                      margin=dict(l=0, r=0, t=0, b=0),
                      font_size=12,
                      font_family="monospace")
    if to_json:
        return json.dumps([fig], cls=utils.PlotlyJSONEncoder)
    else:
        fig.show()
    return None
예제 #6
0
def expect_fig(fig: go.Figure, filename: str, check: bool) -> None:
    """Check for pixel-for-pixel equivalence to stored image."""
    ext = "png"
    if check:
        found = fig.to_image(format=ext)
        try:
            file = open(filename + "." + ext, "rb")
            expected = file.read()
            if expected != found:
                print(f"{filename}: differs from reference image.")
                raise FileNotFoundError(errno.ENOENT,
                                        os.strerror(errno.ENOENT),
                                        filename + ".new." + ext)
            print(f"{filename}: image identical.")
#            fig.show(config=default_config())
        except FileNotFoundError as e:
            file_new = open(e.filename, "wb")
            file_new.write(found)
            print(f"{filename}: creating new reference image.")
            fig.show(config=default_config())
            assert False
    else:
        print(f"{filename}: image not compared.")
예제 #7
0
parser.add_argument('filename', help='npz file')
parser.add_argument('--backend', default='matplotlib', help='matplotlib or plotly')
args = parser.parse_args()

with np.load(args.filename) as npzfile:
    x = npzfile['x']
    y = npzfile['y']
    z = npzfile['z']

if args.backend == 'matplotlib':

    import matplotlib.pyplot as plt
    from matplotlib import cm
    from mpl_toolkits.mplot3d import Axes3D

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    surf = ax.plot_trisurf(x, y, z, cmap=cm.terrain)
    plt.show()

elif args.backend == 'plotly':

    from plotly.graph_objects import Figure
    from plotly.graph_objects import Mesh3d, Surface

    fig = Figure(data=[Mesh3d(x=x, y=y, z=z, opacity=0.5)])
    fig.show()

else:
    print('Unknown backend')
예제 #8
0
파일: main.py 프로젝트: jckdm/thesis
def main():
    # get path to directory
    filepath = input('Path to directory (blank if PWD): ')

    # check if valid path
    if filepath != '':
        if not path.isdir(filepath):
            exit('Invalid path')

    data, lats, lons, flag, good, bad = [], [None, None], [None,
                                                           None], False, 0, 0

    # ask if user wants too show images' metadata (added for my zine)
    meta = input('\nShow metadata? (y/n): ')

    if meta.lower() in ('yes', 'y'):
        metaflag = True
    elif meta.lower() in ('no', 'n'):
        metaflag = False
    else:
        exit('Invalid response')

    # read all files in dir
    for file in glob(filepath + '*'):
        file = file.lower()
        # skip non-image files
        if not file.endswith('.jpg') and not file.endswith(
                '.jpeg') and not file.endswith('.png'):
            continue

        # extract Exif data from image
        exif = {
            TAGS[key]: val
            for key, val in Image.open(file).getexif().items() if key in TAGS
        }

        # extract GPS + datetime
        try:
            loc = exif['GPSInfo']
            dt = exif['DateTimeOriginal']
            good += 1
        # skip if either missing
        except KeyError:
            bad += 1
            continue

        # extract latitude and longitude
        lat = {
            'dir': loc[1],
            'deg': loc[2][0],
            'min': loc[2][1],
            'sec': loc[2][2]
        }
        lon = {
            'dir': loc[3],
            'deg': loc[4][0],
            'min': loc[4][1],
            'sec': loc[4][2]
        }

        # clean and print metadata
        if metaflag:
            cleanLat = str(lat['deg']) + '° ' + str(lat['min']) + '\' ' + str(
                lat['sec']) + '\" ' + str(lat['dir'])
            cleanLon = str(lon['deg']) + '° ' + str(lon['min']) + '\' ' + str(
                lon['sec']) + '\" ' + str(lon['dir'])

            print(
                f'File: {file}   Latitude: {cleanLat}   Longitude: {cleanLon}   Time: {dt}'
            )

        # calculate full coordinate with degree, minute, second
        truLat = float(lat['deg'] + (lat['min'] / 60.0) +
                       (lat['sec'] / 3600.0))
        truLon = float(lon['deg'] + (lon['min'] / 60.0) +
                       (lon['sec'] / 3600.0))

        # calculate mins and maxes
        if flag:
            lons[0], lons[1] = min(lons[0], truLon), max(lons[1], truLon)
            lats[0], lats[1] = min(lats[0], truLat), max(lats[1], truLat)
        # first time just assign values and flip flag
        else:
            lons[0], lons[1] = truLon, truLon
            lats[0], lats[1] = truLat, truLat
            flag = True

        data.append({
            'img': file,
            'lat': lat,
            'lon': lon,
            'datetime': dt,
            'truLat': truLat,
            'truLon': truLon
        })

    # not enough valid images
    if good <= 1:
        exit('Didn\'t find enough valid image files for a visualization.')

    print(
        f'\nExtracted metadata from {good} files. Unable to extract from {bad}.\n'
    )

    # prompt for viz choice
    q = input(
        'Please enter the number corresponding to your visualization of choice:\n1: Unsorted path\n2: Sorted path\n3: Both paths overlaid\n\n#: '
    )

    # validate user input
    while q not in ('1', '2', '3'):
        q = input('#: ')
    q = int(q)

    coords, sortedCoords, unSortedData = 'M ', 'M ', None

    # copy data, add first point
    if q == 1 or q == 3:
        unSortedData = data.copy()
        coords += str(unSortedData[0]['truLat']) + ',' + str(
            unSortedData[0]['truLon']) + ' '
    # sort data, add first point
    if q == 2 or q == 3:
        data.sort(key=lambda x: x['datetime'])
        sortedCoords += str(data[0]['truLat']) + ',' + str(
            data[0]['truLon']) + ' '

    # append rest of points
    for i in range(1, good):
        if q == 1 or q == 3:
            coords += ('L' + str(unSortedData[i]['truLat']) + ',' +
                       str(unSortedData[i]['truLon']) + ' ')
        if q == 2 or q == 3:
            sortedCoords += ('L' + str(data[i]['truLat']) + ',' +
                             str(data[i]['truLon']) + ' ')

    paths = []

    # if using unsorted, append path
    if coords != 'M ':
        paths.append({'type': 'path', 'path': coords, 'line_color': '#3CB371'})
    # if using sorted, append path
    if sortedCoords != 'M ':
        paths.append({
            'type': 'path',
            'path': sortedCoords,
            'line_color': '#6666FF'
        })

    fig = Figure(layout=Layout(plot_bgcolor='RGBA(1,1,1,0)'))
    # draw axes from min to max
    fig.update_xaxes(range=[lats[0], lats[1]], color='#FFFFFF')
    fig.update_yaxes(range=[lons[0], lons[1]], color='#FFFFFF')

    fig.update_layout(shapes=paths)
    fig.show()
예제 #9
0
 def show(self, figure: graph_objects.Figure):
     figure.show()
예제 #10
0
    def shap_explain(self,
                     data,
                     index=None,
                     link=None,
                     show=True,
                     layout_dict=None):
        """Method for plotting a waterfall graph or return corresponding JSON if show=False.

        Args:
            data (:obj:`pd.DataFrame`, :obj:`pd.Series`): Data for shap values calculation.
            index (:obj:`int`, optional): Index of the observation of interest, if data is pd.DataFrame.
            link (:obj:`callable`, optional): A function for transforming shap values into predictions.
            Unnecessary if self.objective is present and it takes values in ['binary', 'poisson', 'gamma'].
            show (:obj:`boolean`, optional): Whether to plot a graph or return a json.
            layout_dict (:obj:`boolean`, optional): Dictionary containing the parameters of plotly figure layout.

        Returns:
            None or dict: Waterfall graph or corresponding JSON.
        """
        def logit(x):
            return true_divide(1, add(1, exp(-x)))

        explainer = TreeExplainer(self.model)
        if isinstance(self.model, (XGBClassifier, XGBRegressor)):
            feature_names = self.model.get_booster().feature_names
        elif isinstance(self.model, (LGBMClassifier, LGBMRegressor)):
            feature_names = self.model.feature_name_
        elif isinstance(self.model, (CatBoostClassifier, CatBoostRegressor)):
            feature_names = self.model.feature_names_
        else:
            raise NotImplementedError(
                f'Error with the backend choice. Supported backends: {self._backends}'
            )

        index = index if (isinstance(
            data, DataFrame)) and (index is not None) else None
        data = DataFrame(data).T[feature_names] if isinstance(
            data, Series) else data[feature_names]
        data = data if index is None else data.loc[[index], :]
        shap_values = explainer.shap_values(data)
        cond_bool = isinstance(shap_values, list) and (len(shap_values) == 2)
        shap_values = shap_values[0] if cond_bool else shap_values
        expected_value = explainer.expected_value[
            0] if cond_bool else explainer.expected_value

        prediction = DataFrame([expected_value] +
                               shap_values.reshape(-1).tolist(),
                               index=['Intercept'] + feature_names,
                               columns=['SHAP Value'])
        prediction['CumSum'] = cumsum(prediction['SHAP Value'])
        prediction['Value'] = append(nan, data.values.reshape(-1))

        if (self.objective is not None) and (link is None):
            link = exp if self.objective in [
                'poisson', 'gamma'
            ] else logit if self.objective == 'binary' else None
        if link is not None:
            prediction['Link'] = link(prediction['CumSum'])
            prediction['Contribution'] = [link(expected_value)] + list(
                diff(prediction['Link']))
        else:
            prediction['Contribution'] = [expected_value] + list(
                diff(prediction['CumSum']))

        fig = Figure(
            Waterfall(
                name=f'Prediction {index}',
                orientation='h',
                measure=['relative'] * len(prediction),
                y=[
                    prediction.index[i] if i == 0 else
                    f'{prediction.index[i]}={data.values.reshape(-1)[i-1]}'
                    for i in range(len(prediction.index))
                ],
                x=prediction['Contribution']))
        fig.update_layout(**(layout_dict if layout_dict is not None else {}))

        if show:
            fig.show()
        else:
            json_ = prediction[['Value', 'SHAP Value',
                                'Contribution']].T.to_dict()
            fig_base64 = b64encode(
                to_image(fig, format='jpeg', engine='kaleido')).decode('ascii')
            json_.update({
                'id': int(data.index.values),
                'predict': prediction['Link'][-1],
                "ShapValuesPlot": fig_base64
            })
            return json_
예제 #11
0
fig = Figure()
fig.update_layout(
    title="Iterated Entanglement Swapping under Noise (dep_err = 0.03)",
    xaxis_title="Iterations",
    xaxis=dict(range=[0, 10]),
    yaxis_title="Fidelity",
)
iter_range = np.arange(11)
for i in range(7):
    fids = iterated_noisy_experiment(0.03, 0.025 * i, 10)
    plot_data = Scatter(
        x=iter_range, y=fids, name="ro_err=" + str(np.round(0.025 * i, 3))
    )
    fig.add_trace(plot_data)
try:
    fig.show(renderer="svg")
except ValueError as e:
    print(e)  # requires plotly-orca

fig = Figure()
fig.update_layout(
    title="Iterated Entanglement Swapping under Noise (ro_err = 0.05)",
    xaxis_title="Iterations",
    xaxis=dict(range=[0, 10]),
    yaxis_title="Fidelity",
)
iter_range = np.arange(11)
for i in range(9):
    fids = iterated_noisy_experiment(0.01 * i, 0.05, 10)
    plot_data = Scatter(
        x=iter_range, y=fids, name="dep_err=" + str(np.round(0.01 * i, 3))
예제 #12
0
class TonextyPlot(object):
    def __init__(self, y, upper, lower, x=None):

        self.fig = Figure()
        self.colors = ['rgb(144, 144, 144)', 'rgb(190, 190, 190)']
        self.config = {'displayModeBar': False}

        self.y = y
        self.upper, self.lower = upper, lower

        if isinstance(x, type(None)):
            self.x = array(list(range(0, len(y))))

    def add_traces(self):

        self.fig.add_trace(
            Scatter(x=self.x,
                    y=self.lower,
                    line_color='rgb(220, 220, 220)',
                    showlegend=False,
                    mode='lines',
                    name='Нижняя граница (95%-й интервал)'))

        self.fig.add_trace(
            Scatter(x=self.x,
                    y=self.y,
                    name='Прогноз',
                    mode='lines',
                    line=dict(color=self.colors[0]),
                    showlegend=True))

        self.fig.add_trace(
            Scatter(x=self.x,
                    y=self.upper,
                    fill='tonexty',
                    fillcolor='rgba(250, 250, 250, 0.4)',
                    line_color='rgb(220, 220, 220)',
                    showlegend=False,
                    mode='lines',
                    name='Верхняя граница (95%-й интервал)'))

        return self

    def update_layout(self):

        title = 'Результат построения 95%-й доверительного интервала прогноза'
        self.fig.update_layout(
            title={
                'text': title,
                'y': 0.98,
                'x': 0.08
            },
            height=320,
            margin={
                't': 50,
                'b': 0
            },
            legend_orientation="h",
            legend=dict(x=-0.005, y=1.125),
        )
        return self

    def to_json(self):

        self.add_traces()
        self.update_layout()

        return self.fig.to_json()

    def plot(self, anaconda=False):

        self.add_traces()

        if not anaconda:
            self.update_layout()
        else:
            margin_bottom = 50
            self.update_layout()
            self.fig.layout.height += margin_bottom
            self.fig.layout.margin = {'t': 50, 'b': margin_bottom}

        return self.fig.show(config=self.config)