Commit 3e3a395c authored by Pablo Iturrieta Rebolledo's avatar Pablo Iturrieta Rebolledo
Browse files

Added spatial entropy funcs

parent 51608a9f
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData" autoUpload="Always" serverName="pciturri@rs1:22" remoteFilesAllowedToDisappearOnAutoupload="false">
<serverData>
<paths name="pciturri@rs1:22">
<serverdata>
<mappings>
<mapping deploy="/tmp/pycharm_project_983" local="$PROJECT_DIR$" />
</mappings>
</serverdata>
</paths>
</serverData>
<option name="myAutoUpload" value="ALWAYS" />
</component>
</project>
\ No newline at end of file
......@@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="Remote Python 2.7.17 (sftp://pciturri@rs1:22/usr/bin/python)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (venv)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Remote Python 2.7.17 (sftp://pciturri@rs1:22/usr/bin/python)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
import numpy
import resource
usage = resource.getrusage(resource.RLIMIT_NICE).ru_maxrss
print(usage)
\ No newline at end of file
import numpy as np
from rpy2 import robjects
from rpy2.robjects import FloatVector
import rpy2.robjects.packages as rpackages
import matplotlib.ticker as tick
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from csep.core import poisson_evaluations as poisson
from csep.utils import plots
import numpy as np
import copy
import scipy.stats
import itertools
import pickle
import os
from codes import get_catalogs, get_models, paths
import matplotlib.pyplot as plt
def classify_forecast(model):
bins = np.logspace(-8,-1,8)
array = model.spatial_counts()
discretized_array = np.digitize(array, bins)
return discretized_array
def get_entropy_measures(model):
discretized_array = classify_forecast(model)
ndarray = model.region.get_cartesian(discretized_array)
plt.close('all')
plt.imshow(ndarray)
plt.show()
spat = rpackages.importr("SpatEntropy")
Array = robjects.FloatVector(ndarray.ravel())
Mat = robjects.r['matrix'](Array, nrow=ndarray.shape[1])
#
# Results = spat.oneill(Mat)
# rel_oneil = Results[-1][0]
# oneill = Results[-2][0]
# print("O'neill's Entropy")
# print('Entropy: %.3f, Relative Entropy: %.3f\n' % (oneill, rel_oneil))
#
# Results = spat.contagion(Mat)
# contagion = Results[-1][0]
# print("Li and Reynolds' index")
# print('Contagion: %.3f\n' % contagion)
#
# Results = spat.leibovici(Mat, 1, 3)
# rel_leibovici = Results[-1][0]
# leibovici = Results[-2][0]
# print("Leibovici's Entropy")
# print('Entropy: %.3f, Relative Entropy: %.3f\n' % (leibovici, rel_leibovici))
#
# Results = spat.parredw(Mat)
# parresol = Results[-1][0]
# print("Parresol and Edwards' entropy. ")
# print('Entropy: %.3f\n' % parresol)
print("Altieri's Entropy")
class_intersects = robjects.FloatVector([2])
Results = spat.altieri(Mat,1,class_intersects)
classes = np.array(Results[0])
print("Classes", classes)
spi = np.array(Results[1])
spi_rel = np.array(Results[2])
res = np.array(Results[3])
res_rel = np.array(Results[4])
print("Class 1: Spatial Mutual Information: %.3f, Residual Entropy: %.3f" % (spi[0], res[0]))
print("Class 2: Spatial Mutual Information: %.3f, Residual Entropy: %.3f\n" % (spi[1], res[1]))
# print("Class 3: Spatial Mutua l Information: %.3f, Residual Entropy: %.3f\n" % (spi[2], res[2]))
return Results
if __name__ == '__main__':
models = {10: get_models.ten_years()}
model = models[10][1]
a = get_entropy_measures(model)
# narray = np.random.randint(0, 7, 128)
# print(a)
##to plot
# data.binary=matrix(as.numeric(data=="a"), nrow(data))
# plot(as.im(data.binary, W=batty.entropy$areas.tess$window), main="",
# col=gray(seq(1,0,l=length(unique(c(data.binary))))), ribbon=FALSE)
# plot(batty.entropy$areas.tess, add=TRUE, border=2)
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