diff --git a/README.md b/README.md index c5215cc..7bbda7d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/backend/api/management/commands/importpopulation.py b/backend/api/management/commands/importpopulation.py index bf48865..2cad106 100644 --- a/backend/api/management/commands/importpopulation.py +++ b/backend/api/management/commands/importpopulation.py @@ -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 @@ -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): diff --git a/backend/api/management/commands/importpopulationdetailed.py b/backend/api/management/commands/importpopulationdetailed.py index 9f4bd90..23e6c0a 100644 --- a/backend/api/management/commands/importpopulationdetailed.py +++ b/backend/api/management/commands/importpopulationdetailed.py @@ -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 @@ -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): @@ -59,4 +59,3 @@ def get_graph(self, **options): load_feature_population_data ) return graph - diff --git a/backend/api/migrations/0018_auto_20180724_1332.py b/backend/api/migrations/0018_auto_20180724_1332.py new file mode 100644 index 0000000..969d228 --- /dev/null +++ b/backend/api/migrations/0018_auto_20180724_1332.py @@ -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', + ), + ] diff --git a/backend/api/migrations/0019_auto_20180724_1424.py b/backend/api/migrations/0019_auto_20180724_1424.py new file mode 100644 index 0000000..63a42aa --- /dev/null +++ b/backend/api/migrations/0019_auto_20180724_1424.py @@ -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', + ), + ] diff --git a/backend/api/models.py b/backend/api/models.py index c880da5..4e5738f 100644 --- a/backend/api/models.py +++ b/backend/api/models.py @@ -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) diff --git a/backend/api/serializers.py b/backend/api/serializers.py index 1a34b16..c11bb2e 100644 --- a/backend/api/serializers.py +++ b/backend/api/serializers.py @@ -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 diff --git a/backend/api/urls.py b/backend/api/urls.py index 9e6e3e6..9d72410 100644 --- a/backend/api/urls.py +++ b/backend/api/urls.py @@ -7,10 +7,7 @@ url('cancer', views.cancer_data), url('hospitals/(?P[0-9]+)/$', views.hospital_detail), url('hospitals/', views.hospital_list), - url('population/(?P[0-9]+)/$', views.population_detail), url('population/', views.population_data), - url('populationdetailed/(?P[0-9]+)/$', views.populationDetailed_detail), - url('populationdetailed/', views.populationDetailed_data), url('depression', views.depression_data), url('hospital-networks/(?P.*)/beds$', views.beds_per_network), url('hospital-networks/', views.hospitalNetwork_list), diff --git a/backend/api/views.py b/backend/api/views.py index 2634dc0..d1decc1 100644 --- a/backend/api/views.py +++ b/backend/api/views.py @@ -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): @@ -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: @@ -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)