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

PopulationDetailed and Population become Population #135

Open
wants to merge 2 commits into
base: develop
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ docker-compose exec api python manage.py importcancer
To access the apis :
* http://localhost:8000/api/hospitals
* http://localhost:8000/api/population
* http://localhost:8000/api/populationdetailed
* http://localhost:8000/api/depression (optional query params: year, agegroup, province, gender)
* http://localhost:8000/api/cancer
* http://localhost:8000/api/hospital-networks
Expand Down
8 changes: 4 additions & 4 deletions backend/api/management/commands/importpopulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def get_data_by_province(province_name):
name = np.repeat(province_name, 8)
year = np.array([2010,2011,2012,2013,2014,2015,2016,2017])
amount = np.array(data)
c = np.column_stack([name,year, amount])
c = np.column_stack([name,year, amount, np.repeat(geo, 8)])
if not os.path.exists(os.path.join(settings.BASE_DIR, 'api', 'source-data', 'population')):
os.makedirs(os.path.join(settings.BASE_DIR, 'api', 'source-data', 'population'))
np.savetxt(os.path.join(settings.BASE_DIR, 'api', 'source-data', 'population', province_name +'.csv'), c, delimiter=',', header="name, year, amount", comments="", fmt='%s')
np.savetxt(os.path.join(settings.BASE_DIR, 'api', 'source-data', 'population', province_name +'.csv'), c, delimiter=',', header="name, year, amount,geo", comments="", fmt='%s')
return c


Expand All @@ -76,14 +76,14 @@ def parse_population_data():
get_data_by_province("Luxembourg"),
get_data_by_province("Namur")
))
np.savetxt(os.path.join(settings.BASE_DIR, 'api', 'source-data', 'population', 'all_province.csv'), a, delimiter=',', header="name, year, amount", comments="", fmt='%s')
np.savetxt(os.path.join(settings.BASE_DIR, 'api', 'source-data', 'population', 'all_province.csv'), a, delimiter=',', header="name, year, amount,geo", comments="", fmt='%s')
csvFile = open(os.path.join(settings.BASE_DIR, 'api', 'source-data', 'population', 'all_province.csv'))
reader = csv.DictReader(csvFile)
for row in reader:
yield row

def transform_population_data(row):
p = Population(name=row['name'], year=row[' year'], amount=row[' amount'])
p = Population(name=row['name'], year=row[' year'], amount=row[' amount'], gender="M-F", code=row['geo'])
yield p

def load_population_data(population):
Expand Down
5 changes: 2 additions & 3 deletions backend/api/management/commands/importpopulationdetailed.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import csv

from bonobo.contrib.django import ETLCommand
from api.models import PopulationDetailed
from api.models import Population
from django.conf import settings


Expand Down Expand Up @@ -43,7 +43,7 @@ def build_params():
yield {'geo': code, 'sex': gender, 'precision': 1, 'unit': 'NR', 'age': 'Y'+str(age), 'rawAge': age, 'time': 2017, 'name': codes_to_names[code]}

def transform_feature_population_data(row):
p = PopulationDetailed(name=row['name'], code=row['geo'], year=row['time'], amount=row['amount'], age=row['rawAge'], gender=row['sex'])
p = Population(name=row['name'], code=row['geo'], year=row['time'], amount=row['amount'], age=row['rawAge'], gender=row['sex'])
yield p

def load_feature_population_data(population):
Expand All @@ -59,4 +59,3 @@ def get_graph(self, **options):
load_feature_population_data
)
return graph

16 changes: 16 additions & 0 deletions backend/api/migrations/0018_auto_20180724_1332.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 2.0.7 on 2018-07-24 13:32

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0017_auto_20180724_1232'),
]

operations = [
migrations.DeleteModel(
name='Population',
),
]
28 changes: 28 additions & 0 deletions backend/api/migrations/0019_auto_20180724_1424.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 2.0.7 on 2018-07-24 14:24

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0018_auto_20180724_1332'),
]

operations = [
migrations.CreateModel(
name='Population',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=600)),
('year', models.IntegerField()),
('amount', models.IntegerField()),
('age', models.IntegerField(null=True)),
('gender', models.CharField(max_length=600, null=True)),
('code', models.CharField(max_length=5, null=True)),
],
),
migrations.DeleteModel(
name='PopulationDetailed',
),
]
5 changes: 0 additions & 5 deletions backend/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ class Population(models.Model):
name = models.CharField(max_length = 600)
year = models.IntegerField()
amount = models.IntegerField()

class PopulationDetailed(models.Model):
name = models.CharField(max_length = 600)
year = models.IntegerField()
amount = models.IntegerField()
age = models.IntegerField(null=True)
gender = models.CharField(max_length = 600, null = True)
code = models.CharField(max_length = 5, null=True)
Expand Down
9 changes: 2 additions & 7 deletions backend/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
from rest_framework import serializers
from api.models import Hospital, HospitalNetwork, Department
from api.models import Bed, Depression, Cancer
from api.models import Population, PopulationDetailed
from api.models import Population

class PopulationSerializer(serializers.ModelSerializer):
class Meta:
model = Population
fields = ('id', 'name', 'year', 'amount')

class PopulationDetailedSerializer(serializers.ModelSerializer):
class Meta:
model = PopulationDetailed
fields = ('id', 'name', 'year', 'amount', 'age', 'gender', 'code')

class CancerSerializer(serializers.ModelSerializer):
class Meta:
model = Cancer
Expand Down
3 changes: 0 additions & 3 deletions backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
url('cancer', views.cancer_data),
url('hospitals/(?P<pk>[0-9]+)/$', views.hospital_detail),
url('hospitals/', views.hospital_list),
url('population/(?P<pk>[0-9]+)/$', views.population_detail),
url('population/', views.population_data),
url('populationdetailed/(?P<pk>[0-9]+)/$', views.populationDetailed_detail),
url('populationdetailed/', views.populationDetailed_data),
url('depression', views.depression_data),
url('hospital-networks/(?P<pk>.*)/beds$', views.beds_per_network),
url('hospital-networks/', views.hospitalNetwork_list),
Expand Down
44 changes: 17 additions & 27 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from api.models import Bed, Department, Hospital, HospitalNetwork, Population, PopulationDetailed, Depression, Cancer
from api.serializers import HospitalSerializer, DepartmentSerializer, HospitalNetworkSerializer, PopulationSerializer, PopulationDetailedSerializer
from api.models import Bed, Department, Hospital, HospitalNetwork, Population, Depression, Cancer
from api.serializers import HospitalSerializer, DepartmentSerializer, HospitalNetworkSerializer, PopulationSerializer
from api.serializers import CancerSerializer, DepressionSerializer, HospitalNetworkSerializer, BedSerializer
from django.db.models import Sum
def isInt(value):
Expand All @@ -29,19 +29,6 @@ def hospital_detail(request, pk):
serializer = HospitalSerializer(hospital)
return JsonResponse(serializer.data)

def population_data(request):
population = Population.objects.all()
serializer = PopulationSerializer(population, many=True)
return JsonResponse(serializer.data, safe=False)

def population_detail(request, pk):
try:
population = Population.objects.get(pk=pk)
except Population.DoesNotExist:
raise Http404("Population not found")
serializer = PopulationSerializer(population)
return JsonResponse(serializer.data)

def population_grouped_per_province(popList):
resp = {}
for pop in popList:
Expand All @@ -51,23 +38,26 @@ def population_grouped_per_province(popList):
resp[pop.code]=[{"age": pop.age, "total":pop.amount}]
return resp

def populationDetailed_data(request):
male_population = PopulationDetailed.objects.filter(gender="M")
female_population = PopulationDetailed.objects.filter(gender="F")
def population_grouped_per_province_total(popList):
resp = {}
for pop in popList:
if pop.code in resp:
resp[pop.code].append({"year": pop.year, "total":pop.amount})
else:
resp[pop.code]=[{"year": pop.year, "total":pop.amount}]
return resp

def population_data(request):
male_population = Population.objects.filter(gender="M")
female_population = Population.objects.filter(gender="F")
total_population = Population.objects.filter(gender="M-F")
r = [
{ "gender": "M", "provinces": population_grouped_per_province(male_population)},
{ "gender": "F", "provinces": population_grouped_per_province(female_population)}
{ "gender": "F", "provinces": population_grouped_per_province(female_population)},
{ "gender": "M-F", "provinces": population_grouped_per_province_total(total_population)}
]
return JsonResponse(r, safe=False)

def populationDetailed_detail(request, pk):
try:
population = PopulationDetailed.objects.get(pk=pk)
except PopulationDetailed.DoesNotExist:
raise Http404("Populationdeatiled not found")
serializer = PopulationDetailedSerializer(population)
return JsonResponse(serializer.data)

def cancer_data(request):
population = Cancer.objects.all()
serializer = CancerSerializer(population, many=True)
Expand Down