Skip to content

Commit

Permalink
outputs and mv legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hodge committed Apr 6, 2020
1 parent 2a35d0d commit 6a9fb84
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
File renamed without changes.
50 changes: 45 additions & 5 deletions getgraphs.py → legacy/getgraphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,22 @@ def clear_buffer(num, path_buffer, save_subplot):
state = "horizontal"
return num, path_buffer, state

def add_fill(attributes):
for attribute in attributes:
attribute['stroke-width'] = f"{attribute.get('stroke-width')}px"
attribute['style'] = f"{attribute['stroke-width']};{attribute['stroke']}"
if attribute['stroke'] == '#4285f4':
del attribute['transform']
del attribute['stroke-linecap']
del attribute['stroke-miterlimit']
del attribute['stroke-linejoin']
return attributes

def save_subplot(path_buffer, num):
"""Take all the paths in the buffer and save them to a new file"""
print(f"Saving sublot {num}")
paths_to_save, attributes_to_save = tuple(zip(*path_buffer))
attributes_to_save = add_fill(attributes_to_save)
svgpathtools.wsvg(
paths_to_save,
attributes=attributes_to_save,
Expand All @@ -87,9 +99,31 @@ def save_subplot(path_buffer, num):
path_buffer = []

state = "horizontal"

num = 1
order_num = 1
count = 0
y_place_old = 0
y_place_new = 0

for path_type_name, path, attribute in relevant_elements:


# if count % 6 == 0:
# if num == 1:
# order_num = 1
# y_place_old = float(str(path.start).split('(')[-1].split('+')[0])
#
#
# elif num < 7:
# y_place_new = float(str(path.start).split('(')[-1].split('+')[0])
# if y_place_new > y_place_old:
# order_num = order_num + 1
# y_place_old = y_place_new
#
# elif num > 6:


if expected_trend_path(path_type_name, path_buffer):
num, path_buffer, state = clear_buffer(num, path_buffer, save_subplot)

Expand Down Expand Up @@ -125,27 +159,33 @@ def _prep_output_folder(input_file, output_folder, overwrite_name):
def _extract_graph_components(attributes, paths):
"""Only keep lines of the svg related to the plots"""
relevant_elements = []

for path, attribute in zip(paths, attributes):

if path._end is None:
print("This does happen")
continue

style = attribute["style"]
try:
fill = attribute["fill"]
stroke = attribute["stroke"]
stroke_width = attribute["stroke-width"]
except KeyError:
continue

if style is None:
if fill is None:
continue

if "stroke:#dadce0" in style and "stroke-width:1.19px" in style:
if "#dadce0" in stroke and ".5" in stroke_width:
relevant_elements.append(("horizontal", path, attribute))
continue

# Check for a blue path, or blue filled object
if "stroke:#4285f4" in style: # or ("fill:#4285f4" in style and :
if "#4285f4" in stroke: # or ("fill:#4285f4" in style and :
relevant_elements.append(("trend", path, attribute))
continue

if "fill:#4285f4" in style and isinstance(
if "#4285f4" in fill and isinstance(
path[0], svgpathtools.path.CubicBezier
):
relevant_elements.append(("trend_point", path, attribute))
Expand Down
6 changes: 3 additions & 3 deletions mobius.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ def _download(blobs, svg):
if len(blobs):
for blob in blobs:
extension = "svg" if svg else "pdf"
with open(f"{get_country(blob, svg)}.{extension}", "wb+") as fileobj:
with open(f"{extension}s/{get_country(blob, svg)}.{extension}", "wb+") as fileobj:
client.download_blob_to_file(blob, fileobj)

print("Download complete")
print(f"Download {country_code} {extension} complete. Saved to /{extension}s")
else:
print("Could not find any files for that code")
print(f"Could not find a {extension} file for code {country_code}")

if svg:
regex = f"\d{{4}}-\d{{2}}-\d{{2}}_{country_code}_.+"
Expand Down
11 changes: 9 additions & 2 deletions mobius/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,24 @@ def csv_process(paths, name, date_lookup, output_folder, plots=True, save=None):
result_df["graph_num"] = name

if save:
os.mkdir(f"{output_folder}/csv") if not os.path.exists(f"{output_folder}/csv") else False
result_df.to_csv(
os.path.join(output_folder, f"{name}.csv"),
os.path.join(output_folder, f"csv/{name}.csv"),
sep=",",
index=False,
float_format="%.3f",
)

if plots:
os.mkdir(f"{output_folder}/plot") if not os.path.exists(f"{output_folder}/plot") else False
plt.plot(result_df.date, result_df.value)
plt.ylim(-80, 80)
plt.savefig(os.path.join(output_folder, f"{name}.png"))
plt.xticks(rotation=90)
plt.xlabel('Date')
plt.ylabel('Mobility change %')
plt.axhline(y=0.5, color='k', linestyle='--', linewidth=0.5)
plt.tight_layout()
plt.savefig(os.path.join(output_folder, f"plot/{name}.png"))

plt.clf()

Expand Down
3 changes: 2 additions & 1 deletion mobius/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def clear_buffer(num, path_buffer):
path_buffer.append((path, attribute))

if save:
os.mkdir(f"{output_folder}/svg") if not os.path.exists(f"{output_folder}/svg") else False
# TODO: see if this can be in clear buffer.
# Don't forget the last graph in the buffer
save_subplot(path_buffer, output_folder, num)
Expand All @@ -86,7 +87,7 @@ def save_subplot(path_buffer, output_folder, num):
logging.info(f"Saving sublot {num}")
paths_to_save, attributes_to_save = tuple(zip(*path_buffer))
svgpathtools.wsvg(
paths_to_save, filename=os.path.join(output_folder, f"{num}.svg"),
paths_to_save, filename=os.path.join(output_folder, f"svg/{num}.svg"),
)


Expand Down

0 comments on commit 6a9fb84

Please sign in to comment.