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
72de1a7f
Commit
72de1a7f
authored
Apr 29, 2019
by
Dumoulin Nicolas
Browse files
input files copyed in an output directory
parent
b3032ff7
Changes
2
Hide whitespace changes
Inline
Side-by-side
scenariosAleatoires/MCMC.py
View file @
72de1a7f
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import
pandas
as
pd
import
pandas
as
pd
import
geopandas
as
gpd
import
geopandas
as
gpd
import
os
,
sys
import
os
,
sys
,
time
,
shutil
import
yaml
import
yaml
import
numpy
as
np
import
numpy
as
np
from
Indicator
import
Indicator
from
Indicator
import
Indicator
...
@@ -13,7 +13,7 @@ from productivite import Productivity
...
@@ -13,7 +13,7 @@ from productivite import Productivity
from
indice_biodiversite_2
import
Biodiversity
from
indice_biodiversite_2
import
Biodiversity
from
social
import
Social
from
social
import
Social
from
tqdm
import
tqdm
from
tqdm
import
tqdm
from
patutils
import
md5sum
from
patutils
import
md5sum
,
load_pat_patches
class
Scenario
:
class
Scenario
:
def
__init__
(
self
,
patches
):
def
__init__
(
self
,
patches
):
...
@@ -23,8 +23,7 @@ class Scenario:
...
@@ -23,8 +23,7 @@ class Scenario:
'''
'''
Return an instance of class Patches by loading a shapefile of initial patches.
Return an instance of class Patches by loading a shapefile of initial patches.
'''
'''
scenario
=
Scenario
(
gpd
.
GeoDataFrame
.
from_file
(
shpfilename
,
encoding
=
'utf-8'
))
scenario
=
Scenario
(
load_pat_patches
(
shpfilename
))
scenario
.
patches
[
'init_cult'
]
=
scenario
.
patches
[
'cultgeopat'
]
return
scenario
return
scenario
def
reallocate
(
self
,
rng
,
targetPAT
,
ratioNbPatches
):
def
reallocate
(
self
,
rng
,
targetPAT
,
ratioNbPatches
):
...
@@ -80,15 +79,28 @@ class MCMC:
...
@@ -80,15 +79,28 @@ class MCMC:
print
(
'Please copy the template file "MCMC_config.sample.yml" and adjust to your settings and run again this program'
)
print
(
'Please copy the template file "MCMC_config.sample.yml" and adjust to your settings and run again this program'
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
self
.
mcmc_config
=
yaml
.
load
(
open
(
mcmc_config_filename
,
'r'
))
self
.
mcmc_config
=
yaml
.
load
(
open
(
mcmc_config_filename
,
'r'
))
self
.
outputdir
=
self
.
mcmc_config
[
'output_dir'
]
+
'/'
+
time
.
strftime
(
'%Y%m%d-%H%M%S'
)
+
'/'
if
not
os
.
path
.
exists
(
self
.
outputdir
):
os
.
makedirs
(
self
.
outputdir
)
print
(
'All data will be written in {}'
.
format
(
self
.
outputdir
))
else
:
print
(
'Output directory already exists! ({})'
.
format
(
self
.
outputdir
))
sys
.
exit
(
1
)
shutil
.
copy
(
mcmc_config_filename
,
self
.
outputdir
)
for
f
in
[
self
.
mcmc_config
[
'patches'
],
self
.
mcmc_config
[
'target'
],
self
.
mcmc_config
[
'indicators_config'
][
'resilience'
],
self
.
mcmc_config
[
'indicators_config'
][
'biodiversity'
][
'matrixfilename'
],
self
.
mcmc_config
[
'indicators_config'
][
'social'
][
'cost_matrix_filename'
],
self
.
mcmc_config
[
'indicators_config'
][
'social'
][
'patches_costs_filename'
]
]:
shutil
.
copy
(
f
,
self
.
outputdir
)
if
'rng_seed'
in
self
.
mcmc_config
:
if
'rng_seed'
in
self
.
mcmc_config
:
self
.
rng
=
np
.
random
.
RandomState
(
self
.
mcmc_config
[
'rng_seed'
])
self
.
rng
=
np
.
random
.
RandomState
(
self
.
mcmc_config
[
'rng_seed'
])
else
:
else
:
self
.
rng
=
np
.
random
.
RandomState
(
42
)
self
.
rng
=
np
.
random
.
RandomState
(
42
)
print
(
'MCMC initialized with default seed'
)
# self.rng.get_state()
print
(
'MCMC initialized with default seed'
)
# self.rng.get_state()
self
.
patches_md5sum
=
md5sum
(
self
.
mcmc_config
[
'patches'
])
self
.
patches_md5sum
=
md5sum
(
self
.
mcmc_config
[
'patches'
])
self
.
patches
=
gpd
.
GeoDataFrame
.
from_file
(
self
.
mcmc_config
[
'patches'
],
encoding
=
'utf-8'
)
self
.
patches
=
load_pat_patches
(
self
.
mcmc_config
[
'patches'
])
self
.
patches
=
self
.
patches
[
self
.
patches
[
'cultgeopat'
]
!=
'Non Considérée'
]
self
.
patches
[
'init_cult'
]
=
self
.
patches
[
'cultgeopat'
]
self
.
target
=
pd
.
read_csv
(
self
.
mcmc_config
[
'target'
],
sep
=
';'
,
index_col
=
0
)
self
.
target
=
pd
.
read_csv
(
self
.
mcmc_config
[
'target'
],
sep
=
';'
,
index_col
=
0
)
targetRatio
=
(
self
.
target
[
'2050'
]
-
self
.
target
[
'2016'
])
/
self
.
target
[
'2016'
]
targetRatio
=
(
self
.
target
[
'2050'
]
-
self
.
target
[
'2016'
])
/
self
.
target
[
'2016'
]
self
.
targetPAT
=
self
.
patches
.
groupby
(
'cultgeopat'
)[
'SURF_PARC'
].
sum
()
*
(
1
+
targetRatio
)
self
.
targetPAT
=
self
.
patches
.
groupby
(
'cultgeopat'
)[
'SURF_PARC'
].
sum
()
*
(
1
+
targetRatio
)
...
@@ -102,7 +114,7 @@ class MCMC:
...
@@ -102,7 +114,7 @@ class MCMC:
scenario
.
reallocate
(
self
.
rng
,
self
.
targetPAT
,
self
.
mcmc_config
[
'ratio_patches_to_modify'
])
# 3.8 ms
scenario
.
reallocate
(
self
.
rng
,
self
.
targetPAT
,
self
.
mcmc_config
[
'ratio_patches_to_modify'
])
# 3.8 ms
scores
.
append
(
self
.
indicators
.
compute_indicators
(
scenario
.
patches
))
scores
.
append
(
self
.
indicators
.
compute_indicators
(
scenario
.
patches
))
scores
=
pd
.
DataFrame
(
scores
,
columns
=
self
.
indicators
.
indicators_names
)
scores
=
pd
.
DataFrame
(
scores
,
columns
=
self
.
indicators
.
indicators_names
)
scores
.
to_csv
(
'../
output
/
mcmc
2
.csv'
)
scores
.
to_csv
(
self
.
output
dir
+
'
mcmc.csv'
)
# TODO
# TODO
# Storing variation of indicators
# Storing variation of indicators
init_var
=
scores
.
std
()
init_var
=
scores
.
std
()
...
@@ -110,13 +122,9 @@ class MCMC:
...
@@ -110,13 +122,9 @@ class MCMC:
# sequential optimization loop
# sequential optimization loop
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
# scenario = Scenario.load_shp('../output/PAT_patches/PAT_patches.shp')
# target = pd.read_csv('../resources/targetPAT.csv', sep=';',index_col=0)
# targetRatio = (target['2050']-target['2016'])/target['2016']
# targetPAT = scenario.patches.groupby('cultgeopat')['SURF_PARC'].sum()*(1+targetRatio)
# rng = np.random.RandomState()
# scenario.reallocate(rng, targetPAT, 50)
mcmc
=
MCMC
(
'MCMC_config.yml'
)
mcmc
=
MCMC
(
'MCMC_config.yml'
)
# scenario = Scenario(mcmc.patches.copy())
# scenario.reallocate(mcmc.rng, mcmc.targetPAT, mcmc.mcmc_config['ratio_patches_to_modify'])
mcmc
.
run
()
mcmc
.
run
()
# print(mcmc.indicators.biodiversity(mcmc.patches))
# print(mcmc.indicators.biodiversity(mcmc.patches))
# print(mcmc.indicators.proximity(mcmc.patches))
# print(mcmc.indicators.proximity(mcmc.patches))
scenariosAleatoires/MCMC_config.yml
View file @
72de1a7f
patches
:
../output/PAT_patches/PAT_patches.shp
patches
:
../output/PAT_patches/PAT_patches.shp
target
:
../resources/targetPAT.csv
target
:
../resources/targetPAT.csv
output_dir
:
../output/MCMC/
initial_nb_particles
:
10
initial_nb_particles
:
10
ratio_patches_to_modify
:
0.05
ratio_patches_to_modify
:
0.05
indicators_config
:
indicators_config
:
...
...
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