Skip to content

Commit

Permalink
Reorganize sample_data folder to accomodate multiple use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
annehaley committed Aug 2, 2024
1 parent bfd69c2 commit cfa32a8
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 145 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ minio/*
rabbitmq/*
.vscode/
staticfiles/
sample_data/*/*
sample_data/downloads/*

# osmnx data cache folder
cache
Expand Down
17 changes: 10 additions & 7 deletions sample_data/ingest_sample_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
from uvdat.core.models import Chart, Context, Dataset, FileItem


USE_CASE_FOLDER = Path('sample_data/use_cases')


def ingest_file(file_info, index=0, dataset=None, chart=None):
file_path = file_info.get('path')
file_name = file_info.get('name', file_path.split('/')[-1])
file_url = file_info.get('url')
file_metadata = file_info.get('metadata', {})

file_location = Path('sample_data', file_path)
file_location = Path('sample_data/downloads', file_path)
file_type = file_path.split('.')[-1]
if not file_location.exists():
print(f'\t Downloading data file {file_name}.')
Expand Down Expand Up @@ -46,9 +49,9 @@ def ingest_file(file_info, index=0, dataset=None, chart=None):
new_file_item.file.save(file_path, ContentFile(f.read()))


def ingest_contexts():
def ingest_contexts(use_case):
print('Creating Context objects...')
with open('sample_data/contexts.json') as contexts_json:
with open(str(USE_CASE_FOLDER / use_case / 'contexts.json')) as contexts_json:
data = json.load(contexts_json)
for context in data:
print('\t- ', context['name'])
Expand All @@ -66,9 +69,9 @@ def ingest_contexts():
context_for_setting.datasets.set(Dataset.objects.filter(name__in=context['datasets']))


def ingest_charts():
def ingest_charts(use_case):
print('Creating Chart objects...')
with open('sample_data/charts.json') as charts_json:
with open(str(USE_CASE_FOLDER / use_case / 'charts.json')) as charts_json:
data = json.load(charts_json)
for chart in data:
print('\t- ', chart['name'])
Expand Down Expand Up @@ -100,9 +103,9 @@ def ingest_charts():
)


def ingest_datasets(include_large=False, dataset_indexes=None):
def ingest_datasets(use_case, include_large=False, dataset_indexes=None):
print('Creating Dataset objects...')
with open('sample_data/datasets.json') as datasets_json:
with open(str(USE_CASE_FOLDER / use_case / 'datasets.json')) as datasets_json:
data = json.load(datasets_json)
for index, dataset in enumerate(data):
if dataset_indexes is None or index in dataset_indexes:
Expand Down
112 changes: 0 additions & 112 deletions sample_data/ingest_sample_data_output.txt

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,5 @@
"datasets": [
"DC Metro"
]
},
{
"name": "Boston-Washington Transportation",
"default_map_center": [
40.5,
-74.5
],
"default_map_zoom": 8,
"datasets": [
"MBTA Rapid Transit",
"MBTA Commuter Rail",
"Massachusetts Elevation Data",
"Boston Hurricane Surge Inundation Zones",
"Bsoton FEMA National Flood Hazard",
"Boston Neighborhoods",
"Boston Census 2020 Block Groups",
"Boston Zip Codes",
"Boston Sea Level Rises",
"Boston 10-Year Flood Events",
"Boston 100-Year Flood Events",
"DC Metro"
]
}
]
File renamed without changes.
13 changes: 10 additions & 3 deletions uvdat/core/management/commands/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class Command(BaseCommand):
requires_migrations_checks = True

def add_arguments(self, parser):
parser.add_argument(
'use_case',
choices=['boston_floods', 'new_york_energy'],
help='Sample data collection to load',
)
parser.add_argument(
'--include_large',
action='store_true',
Expand All @@ -15,15 +20,17 @@ def add_arguments(self, parser):
parser.add_argument('--dataset_indexes', nargs='*', type=int)

def handle(self, *args, **kwargs):
print('Populating server with sample data...')
use_case = kwargs['use_case']
include_large = kwargs['include_large']
dataset_indexes = kwargs['dataset_indexes']
if dataset_indexes is None or len(dataset_indexes) == 0:
dataset_indexes = None

print(f'Populating server with sample data for use case {use_case}...')
ingest_datasets(
use_case,
include_large=include_large,
dataset_indexes=dataset_indexes,
)
ingest_contexts()
ingest_charts()
ingest_contexts(use_case)
ingest_charts(use_case)
1 change: 1 addition & 0 deletions uvdat/core/tests/test_populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_populate():

call_command(
'populate',
'boston_floods',
include_large=True,
dataset_indexes=dataset_indexes,
)
Expand Down

0 comments on commit cfa32a8

Please sign in to comment.