Skip to content
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

run saau #8

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion .github/workflows/python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Preinstall
run: |
pipenv run pip install cython numpy
sudo apt install libopenblas-dev libgdal-dev
sudo apt install libopenblas-dev libgdal-dev libproj-dev
- name: Install dependencies
if: steps.cache-pipenv.outputs.cache-hit != 'true'
run: |
Expand All @@ -52,6 +52,8 @@ jobs:
- name: Import saau
run: |
pipenv run python -c "import saau; print(saau)"
- name: Run saau
run: pipenv run python -m saau
# - name: Archive test report
# uses: actions/upload-artifact@v1
# if: always()
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ docs/_build/

# PyBuilder
target/

stats_data/
.idea
*.sqlite
26 changes: 0 additions & 26 deletions Pipfile

This file was deleted.

803 changes: 0 additions & 803 deletions Pipfile.lock

This file was deleted.

1,764 changes: 1,764 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[tool.poetry]
name = "statistical-atlas-of-au"
version = "0.1.0"
authors = ["Elliana May <[email protected]>"]
license = "MIT"
readme = "README.md"
packages = [{include = "statistical_atlas_of_au"}]
description = 'Statistical Atlas of Australia'

[tool.poetry.dependencies]
python = ">=3.10,<3.12"

requests = "*"
betamax = "*"
cartopy = "*"
matplotlib = "*"
pandas = "~1.5.0"
seaborn = "*"
humanize = "*"
lxml = "*"
dill = "*"
tqdm = "*"
rasterio = "*"
gn-arcrest = "*"
cython = "*"
numpy = "*"
coloredlogs = "*"
pandasdmx = { git = "https://github.com/Mause/pandaSDMX", branch = "range-period", extras = ["cache"] }
fastparquet = "^2023.10.1"

[tool.poetry.group.dev.dependencies]
pdbpp = "^0.10.3"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
16 changes: 14 additions & 2 deletions saau/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import logging
import warnings
import argparse
import coloredlogs
from operator import itemgetter
from concurrent.futures import ThreadPoolExecutor as PoolExecutor
from os.path import join, dirname, exists, expanduser
from bdb import BdbQuit

import pandasdmx

STATS_DATA = 'c:\\stats_data'
CACHE = join(STATS_DATA, 'cache')
OUTPUT = join(STATS_DATA, 'output')
HERE = dirname(__file__)
logging.basicConfig(level=logging.DEBUG)
coloredlogs.install(level='DEBUG')
coloredlogs.install(logger=pandasdmx.logger, level='DEBUG')
sys.path.insert(0, expanduser('~/Dropbox/temp/arcrest'))

from .services import Services
Expand Down Expand Up @@ -52,8 +56,10 @@ def ensure_data(prov):
logging.info('Obtaining data for %s', get_name(prov))
try:
val = prov.obtain_data()
except BdbQuit:
raise
except Exception as e:
logging.exception(e)
logging.exception('failed to obtain data')
return False

if val not in {True, False}:
Expand Down Expand Up @@ -160,6 +166,9 @@ def build_image(prov, rerender_all):
else:
logging.info('Render unsuccessful')

except BdbQuit:
raise

except NotImplementedError:
logging.exception("Can't render %s", get_name(prov))

Expand All @@ -169,6 +178,9 @@ def build_image(prov, rerender_all):
get_name(prov)
)

except Exception:
logging.exception("failed to render image")

del prov # force cleanup


Expand Down
26 changes: 7 additions & 19 deletions saau/sections/age/median.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,13 @@ def has_required_data(self):
def obtain_data(self):
data = get_generic_data(
DATASETID,
and_=[
'FREQUENCY.A',
'REGIONTYPE.SA2',
'MEASURE.MAGE'
],
or_=[
'STATE.0',
'STATE.1',
'STATE.2',
'STATE.3',
'STATE.4',
'STATE.5',
'STATE.6',
'STATE.7',
'STATE.8',
'STATE.9'
]
and_={
'FREQUENCY':'A',
'REGIONTYPE':'SA2',
'MEASURE':'MAGE',
'STATE': ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
}
)
assert data['series']

return self.save_json(FILENAME, data)

Expand All @@ -49,7 +37,7 @@ def region_lookup(self, sa3):
def build_image(self):
colors = get_cmap('Purples')

age_data = abs_data_to_dataframe(self.load_json(FILENAME))
age_data = abs_data_to_dataframe(self.data_dir_join(FILENAME))
age_data = [
(
self.region_lookup(data_point.REGION),
Expand Down
5 changes: 1 addition & 4 deletions saau/sections/ancestry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ def obtain_data(self):
return self.save_json(self.filename, get_data(self.ancestry_name))

def build_image(self):
data = abs_data_to_dataframe(
self.load_json(self.filename),
['ANCP', 'FREQUENCY']
)
data = abs_data_to_dataframe(self.data_dir_join(self.filename))
data = data[data.pop('Time') == 2011]
del data['REGIONTYPE']

Expand Down
9 changes: 7 additions & 2 deletions saau/sections/image_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ def data_dir_join(self, name: PathOrStr) -> str:
return join(self.data_dir, name)

def save_json(self, name: PathOrStr, data: Any) -> bool:
with open(self.data_dir_join(name), 'w') as fh:
json.dump(data, fh, indent=4)
import pandas as pd

if isinstance(data, (pd.DataFrame, pd.Series)):
data.to_json(self.data_dir_join(name))
else:
with open(self.data_dir_join(name), 'w') as fh:
json.dump(data, fh, indent=4)
return True

def load_json(self, name: PathOrStr) -> Any:
Expand Down
3 changes: 1 addition & 2 deletions saau/sections/population/density.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@


def get_data(data_dir):
with open(join(data_dir, filename)) as fh:
return abs_data_to_dataframe(json.load(fh))
return abs_data_to_dataframe(join(data_dir, filename))


def main(services, data_dir):
Expand Down
2 changes: 1 addition & 1 deletion saau/sections/population/male_vs_female.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def obtain_data(self):
return self.save_json(FILENAME, data)

def load_data(self):
df = abs_data_to_dataframe(self.load_json(FILENAME))
df = abs_data_to_dataframe(self.data_dir_join(FILENAME))

df.SEX_ABS = (
df.SEX_ABS
Expand Down
2 changes: 1 addition & 1 deletion saau/sections/transportation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def obtain_data(self):
assert hasattr(self, 'layers')
return self.save_json(
self.path,
get_paths(self.layers).tolist()
get_paths(self.layers)
)

def has_required_data(self):
Expand Down
31 changes: 13 additions & 18 deletions saau/sections/transportation/data.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
from operator import itemgetter
from itertools import chain
from typing import List
from functools import cache

import cgi
from urllib.parse import parse_qs

from ...utils.py3_hook import with_hook

with with_hook():
from arcrest import Catalog
from arcrest import Catalog
import numpy as np
from tqdm import tqdm


cgi.parse_qs = parse_qs # type: ignore


def get_layers(service):
@cache
def get_layers():
catalog = Catalog('http://services.ga.gov.au/site_7/rest/services')
service = catalog['NM_Transport_Infrastructure']
layers = service.layers
return {
layer.name: layer
Expand All @@ -29,13 +25,10 @@ def mend_extent(extent):


def get_data(requested_layers: List[str]):
catalog = Catalog('http://services.ga.gov.au/site_7/rest/services')
service = catalog['NM_Transport_Infrastructure']
layers = get_layers(service)

layers = get_layers()
return chain.from_iterable(
layers[layer].QueryLayer(Geometry=mend_extent(layers[layer].extent))
for layer in requested_layers
for layer in tqdm(requested_layers, desc='Fetching requested layers')
)


Expand All @@ -48,10 +41,12 @@ def get_paths(request_layers: List[str]) -> np.array:
if hasattr(geometry, 'paths')
)

return np.array([
result = [
tuple(
(part.x, part.y)
for part in path
)
for path in paths
])
]

return result # np.array(result)
13 changes: 7 additions & 6 deletions saau/services/aus_map/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
from ...sections.image_provider import RequiresData
from ...utils.download import get_binary

name = lambda q: q.attributes['NAME_1']
name = lambda q: q.attributes['Name']
DummyRecord = namedtuple('DummyRecord', 'attributes,geometry')
LL = namedtuple('LL', 'lat,lon')

AUS_NW = LL(111.5, -1)
AUS_SE = LL(155, -42)
YELLOWED_PAPER = '#F1E4BB'

"http://www.gadm.org/download"
URL = 'http://biogeo.ucdavis.edu/data/gadm2.8/shp/AUS_adm_shp.zip'
URL = 'https://www.geoboundaries.org/data/1_3_3/zip/shapefile/AUS/AUS_ADM1.shp.zip'
FILENAME = basename(URL)


Expand All @@ -35,15 +34,17 @@ def get_states(services):

shpfile = shape_from_zip(
services.aus_map.data_dir_join(FILENAME),
"AUS_adm1"
'AUS_ADM1',
)

val = list(shpfile.records())

try_simplify = lambda a: a.simplify(0.5) if hasattr(a, 'simplify') else a

val = [
DummyRecord(
rec.attributes,
rec.geometry.simplify(0.5)
try_simplify(rec.geometry)
)
for rec in val
]
Expand Down Expand Up @@ -73,7 +74,7 @@ def get_map(services, show_world=False, zorder=0):
# ax.background_patch.set_visible(False)
# ax.outline_patch.set_visible(False)

ax.background_patch.set_facecolor(YELLOWED_PAPER)
# ax.background_patch.set_facecolor(YELLOWED_PAPER)
ax.patch.set_facecolor(YELLOWED_PAPER)
ax.set_facecolor(YELLOWED_PAPER)

Expand Down
1 change: 0 additions & 1 deletion saau/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import requests
import sys
from os.path import expanduser
sys.path.insert(0, expanduser('~/Dropbox/temp/arcrest'))
import arcrest.compat
from betamax import Betamax

Expand Down
1 change: 1 addition & 0 deletions saau/utils/download/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def get_binary(url, filename):
)

r = requests.get(url, stream=True)
r.raise_for_status()

with open(filename, 'wb') as fh:
copyfileobj(
Expand Down
Loading