Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
geopat
geopat
Commits
43d1ecfb
Commit
43d1ecfb
authored
Apr 19, 2019
by
Dumoulin Nicolas
Browse files
Polymorphism for indicators initialization
Cultural surface added in as indicators output
parent
c12068b2
Changes
5
Hide whitespace changes
Inline
Side-by-side
scenariosAleatoires/Indicator.py
View file @
43d1ecfb
#!/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
scenariosAleatoires/MCMC.py
View file @
43d1ecfb
...
...
@@ -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
):
...
...
scenariosAleatoires/MCMC_benchmark_indicators.py
0 → 100644
View file @
43d1ecfb
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
(
'
\n
reallocate: {:.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
'''
scenariosAleatoires/patutils.py
0 → 100644
View file @
43d1ecfb
#!/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
()
scenariosAleatoires/proximite.py
View file @
43d1ecfb
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment