Commit 43d1ecfb authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

Polymorphism for indicators initialization

Cultural surface added in as indicators output
parent c12068b2
#!/usr/bin/python
# -*- coding: utf-8 -*-
import abc
class Indicator:
@abc.abstractmethod
def __init__(self, config, initial_patches, patches_md5sum, targetPAT):
pass
@abc.abstractmethod
def compute_indicator(self, patches):
pass
......@@ -5,7 +5,9 @@ import geopandas as gpd
import os, sys
import yaml
import numpy as np
from proximite import Proximite
from Indicator import Indicator
from overrides import overrides
from proximite import Proximite as Proximity
from resilience_list import Resilience
from productivite import Productivity
from indice_biodiversite_2 import Biodiversity
......@@ -42,18 +44,28 @@ class Scenario:
# Doing the reallocation
self.patches.loc[samples.index.values,'cultgeopat'] = newCult.values
class CulturalIndicator(Indicator):
@overrides
def __init__(self, config, initial_patches=None, patches_md5sum=None, targetPAT=None):
self.cultgeopat = config
@overrides
def compute_indicator(self, patches):
return patches[patches['cultgeopat']==self.cultgeopat]['SURF_PARC'].sum()
class Indicators:
def __init__(self, config, initial_patches, patches_md5sum, targetPAT):
self._proximity = Proximite(None, initial_patches, None, targetPAT)
self._resilience = Resilience(config['resilience'], initial_patches)
self._productivity = Productivity()
self._biodiversity = Biodiversity(config['biodiversity'], initial_patches, patches_md5sum)
self._social = Social(config['social'], initial_patches, patches_md5sum)
self.indicators_names = ['proximity', 'resilience', 'productivity', 'biodiversity', 'social']
self._indicators = {
indicator.lower(): eval(indicator)(config.get(indicator.lower()), initial_patches, patches_md5sum, targetPAT)
for indicator in ['Proximity', 'Resilience', 'Productivity', 'Biodiversity', 'Social']
}
self.indicators_names = list(self._indicators.keys())
for cultgeopat in targetPAT.index.tolist():
self._indicators[cultgeopat] = CulturalIndicator(cultgeopat)
self.indicators_names.append(cultgeopat)
def compute_indicators(self, patches):
return [self.proximity(patches), self.resilience(patches),
self.productivity(patches), self.biodiversity(patches), self.social(patches)]
return [ind.compute_indicator(patches) for ind in self._indicators.values()]
def compute_indicators_pool(self, scenarios):
rows=[]
......@@ -61,21 +73,6 @@ class Indicators:
rows.append(self.compute_indicators(patches))
return pd.DataFrame(rows, columns=self.indicators_names)
def proximity(self, patches):
return self._proximity.compute_indicator(patches)
def resilience(self, patches):
return self._resilience.compute_indicator(patches)
def productivity(self, patches):
return self._productivity.compute_indicator(patches)
def biodiversity(self, patches):
return self._biodiversity.compute_indicator(patches)
def social(self, patches):
return self._social.compute_indicator(patches)
class MCMC:
def __init__(self, mcmc_config_filename):
if not os.path.isfile(mcmc_config_filename):
......
from MCMC import *
import timeit
if __name__ == '__main__':
mcmc = MCMC('MCMC_config.yml')
scenario = Scenario(mcmc.patches.copy())
for ind in ['proximity', 'resilience', 'productivity', 'biodiversity', 'social']:
print('{}: {:.4f} s'.format(ind,
timeit.timeit(lambda: eval('mcmc.indicators._indicators[\''+ind+'\'].compute_indicator(scenario.patches)'), number=10)/10
))
print('\nreallocate: {:.4f} s'.format(timeit.timeit(lambda:
scenario.reallocate(mcmc.rng, mcmc.targetPAT, mcmc.mcmc_config['ratio_patches_to_modify'])
, number=10)/10))
for ind in ['proximity', 'resilience', 'productivity', 'biodiversity', 'social']:
print('{}: {:.4f} s'.format(ind,
timeit.timeit(lambda: eval('mcmc.indicators._indicators[\''+ind+'\'].compute_indicator(scenario.patches)'), number=10)/10
))
'''
proximity: 0.0142 s
resilience: 1.5464 s
productivity: 0.0186 s
biodiversity: 0.0181 s
social: 0.1293 s
reallocate: 0.0453 s
proximity: 0.0174 s
resilience: 1.1516 s
productivity: 0.0122 s
biodiversity: 0.0169 s
social: 0.2007 s
'''
#!/usr/bin/python
# -*- coding: utf-8 -*-
import hashlib
import geopandas as gpd
def load_pat_patches(filename):
'''
Load shapefile containing cultural patches and applying initial filters.
'''
patches = gpd.GeoDataFrame.from_file(filename, encoding='utf-8')
patches = patches[patches['cultgeopat']!='Non Considérée']
patches['init_cult'] = patches['cultgeopat']
return patches
def md5sum(filename):
hash_md5 = hashlib.md5()
with open(filename, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
......@@ -14,7 +14,6 @@ class Proximite(Indicator):
'''
@overrides
def __init__(self, config, initial_patches, patches_md5sum=None, targetPAT=None):
# def __init__(self, config, initial_patches, patches_md5sum, surf_totale_cible) :
Population_PAT = initial_patches.groupby('INSEE_COM')['POPULATION'].first().sum() #498873
popByBdv = initial_patches.groupby('Bdv').apply(lambda x:x.groupby('INSEE_COM')['POPULATION'].first().sum())
# OK but slower
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment