8000 Full timestamps by FredrikWartenberg · Pull Request #37 · vikinganalytics/mvg · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Full timestamps #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ENV/
env.bak/
venv.bak/


# Distribution / packaging
build/
develop-eggs/
Expand All @@ -26,6 +27,8 @@ Lib/
Scripts/
doc/
fwdevelop/
tools/


# Test otuput
mca/test_output
Expand Down
42 changes: 34 additions & 8 deletions mvg/analysis_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _add_datetime(self):
datetime.
"""

self._results_df = self._add_datetime_df(self._results_df, "datetime")
self._results_df = self._add_datetime_df(self._results_df, "timestamps")

def _add_datetime_df(self, dframe, timecolumn):
"""
Expand All @@ -125,13 +125,13 @@ def _add_datetime_df(self, dframe, timecolumn):

# EPOCH to datetime considering time zone
dt_col = pd.to_datetime(
dframe["timestamps"], unit=self._t_unit, utc=True
dframe[timecolumn], unit=self._t_unit, utc=True
).dt.tz_convert(self._t_zone)

dframe["datetime"] = dt_col

# Mark timecolumn as available
self.time_column = timecolumn
# self.time_column = timecolumn

return dframe

Expand Down Expand Up @@ -240,14 +240,19 @@ def summary(self):
print(f"from {from_t} to {to_t}")

# Default method
def plot(self, interactive=True): # pylint: disable=unused-argument
def plot(
self, interactive=True, time_format=None
): # pylint: disable=unused-argument
"""Pro forma ancestor function.

Parameters
----------
interactive : bool
True: show plot, False: save plot

time_format: str, optional
strftime format specifier for tick_x_lables. If not given
only dates are shown. To show dates and time use %y%m%d-%H:%M:%S

Returns
-------
Expand Down Expand Up @@ -393,7 +398,7 @@ def summary(self):
print(tabulate(tab, headers="keys", tablefmt="psql"))
return tab

def plot(self, interactive=True):
def plot(self, interactive=True, time_format=None):
"""
Generate a basic plot on RMS.

Expand All @@ -402,6 +407,11 @@ def plot(self, interactive=True):
interactive : bool
True: show plot, False: save plot

time_format: str, optional
strftime format specifier for tick_x_lables. If not given
only dates are shown. To show dates and time use %y%m%d-%H:%M:%S


Returns
-------
plot file name : str
Expand Down Expand Up @@ -480,7 +490,7 @@ def summary(self):

return [tbl, tbl2, self.emerging_df]

def plot(self, interactive=True):
def plot(self, interactive=True, time_format=None):
"""
Generate a basic plot on ModeId.

Expand All @@ -489,14 +499,24 @@ def plot(self, interactive=True):
interactive : bool
True: show plot, False: save plot

time_format: str, optional
strftime format specifier for tick_x_lables. If not given
only dates are shown. To show dates and time use %y%m%d-%H:%M:%S

Returns
-------
plot file name : str
name of plot file (or emtpy string in case of interactive plot)
"""

self.check_status()
plotting.modes_over_time(self.to_df(), self.request_id(), timeunit=self._t_unit)
plotting.modes_over_time(
data=self.to_df(),
request_id=self.request_id(),
timeunit=self._t_unit,
time_format=time_format,
)

return self._render_plot(interactive)


Expand Down Expand Up @@ -579,7 +599,8 @@ def summary(self):
print(tabulate(tbl, headers=["atypical", "N"], tablefmt="psql"))
return [self.typicality, tbl]

def plot(self, interactive=True):
# pylint: disable=too-many-locals
def plot(self, interactive=True, time_format=None):
"""Generate a (not so) basic plot for BlackSheep
Will show per atypical asset changes to and from
atypical modes
Expand All @@ -589,6 +610,11 @@ def plot(self, interactive=True):
interactive : bool
True: show plot, False: save plot

time_format: str, optional
strftime format specifier for tick_x_lables. If not given
only dates are shown. To show dates and time use %y%m%d-%H:%M:%S


Returns
-------
plot file name : str
Expand Down
2 changes: 1 addition & 1 deletion mvg/mvg.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, endpoint: str, token: str):
self.endpoint = endpoint
self.token = token

self.mvg_version = self.parse_version("v0.7.0")
self.mvg_version = self.parse_version("v0.7.1")
self.tested_api_version = self.parse_version("v0.1.12")

# Errors to ignore
Expand Down
11 changes: 10 additions & 1 deletion mvg/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def modes_over_time(
show_uncertain=True,
only_start_end_timeticks=False,
timetick_angle=85,
time_format=None,
):
"""Creates a rectangular timeline of modes.

Expand Down Expand Up @@ -189,6 +190,10 @@ def modes_over_time(
timetick_angle: float, optional
the angle of time tick texts.

time_format: str, optional
strftime format specifier for tick_x_lables. If not given
only dates are shown. To show dates and time use %y%m%d-%H:%M:%S

Returns
----------
image: object of class matplotlib.axes
Expand Down Expand Up @@ -286,7 +291,11 @@ def _plot_row(row_data, is_uncert_data, y_pos=0):

# Modify ticks position and create legend
df_changes = data.iloc[tick_index]
tick_x_labels = df_changes["Date"].apply(lambda x: x.date())
if time_format is None:
tick_x_labels = df_changes["Date"].apply(lambda x: x.date())
else:
tick_x_labels = df_changes["Date"].apply(lambda x: x.strftime(time_format))

axes.set_xticklabels(tick_x_labels, rotation=timetick_angle)
legend_labels = [
patches.Patch(facecolor=colors[i], edgecolor="black", label="No data")
Expand Down
98 changes: 98 additions & 0 deletions tools/analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# This example requires sources to be uploaded to
# the server side

import os
import json
import typer
from typing import Optional
from mvg import MVG
from mvg.analysis_classes import parse_results
import logging
import sys

root_logger = logging.getLogger()
root_logger.setLevel("INFO")
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(name)s - %(message)s")
stream_handler = logging.StreamHandler(stream=sys.stderr)
stream_handler.setFormatter(formatter)
root_logger.addHandler(stream_handler)

# Instantiate a session object with mvg library
# replace token and server with your url/token
ENDPOINT = os.environ["TEST_URL"]
TOKEN = os.environ["TEST_TOKEN"]

app = typer.Typer() # Create typer app

@app.command()
def run(
source_id: str = typer.Argument(
...,
help='Source ID'),
feature: Optional[str] = typer.Argument("ModeId",
help='Feature'),
start: Optional[int] = typer.Argument(None,
help='Start timestamp [epoch]'),
end: Optional[int] = typer.Argument(None,
help='End timestamp [epoch]'),
show: bool = typer.Option(True, help='Show plot rather than saving it'),
pdb: bool = typer.Option(False, help='Enter pdb debugger'),
params: bool = typer.Option(False, help='Use ./params.json file')
):
"""Run an analyis on Source ID"""


ses = MVG(ENDPOINT, TOKEN)

# Fetch parameters if given
if params:
with open("params.json", "r", encoding="utf-8") as json_data:
params = json.load(json_data)
print(f"Params loaded from ./params.json")
print(json.dumps(params, indent=4))
else:
params = {}

req = ses.request_analysis(sid=source_id,
feature=feature,
parameters=params,
start_timestamp=start,
end_timestamp=end)
request_id = req["request_id"]
print(f"Waiting for {request_id}")
ses.wait_for_analyses([request_id])
get_and_display_results(ses, request_id, show, pdb)

@app.command()
def retrieve(
request_id: str = typer.Argument(
...,
help='Request ID'),
show: bool = typer.Option(True, help='Show plot rather than saving it'),
pdb: bool = typer.Option(False, help='Enter pdb debugger')
):
"""Retrieve a previous analyis with request ID"""

ses = MVG(ENDPOINT, TOKEN)
get_and_display_results(ses, request_id, show, pdb)



def get_and_display_results(ses, request_id, show, pdb):

# Get results
res_dict = ses.get_analysis_results(request_id)

# Parse results
res = parse_results(res_dict, "Europe/Stockholm", "s")
if pdb:
breakpoint()

res.summary()
res.plot(show,time_format="%y%m%d-%H:%M:%S")
res.to_df().head()
print("Bye")


if __name__ == '__main__':
app()
0