-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_dist_gen.py
161 lines (131 loc) · 4.6 KB
/
new_dist_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""Distribution Generator
Description:
Generates 2-d distributions for Cart.c
Usage:
dist_gen.py [--n=<N> --dist_type=<dist> --dimx=<dx> --dimy=<dy> --pixel=<pixel> --plot=<plot>]
3dxy.py -h | --help
Options:
-h --help Show this screen.
-N <N> --n=<N> Number of Points [default: 50000]
-d <dist> --dist_type=<dist> Distribution Type [default: exp]
-x <dx> --dimx=<dx> Number of Pixels in x dimension
[default: 1024]
-y <dy> --dimy=<dy> Number of Pixels in x dimension
[default: 1024]
-p <pixel> --pixel=<pixel> Pixel Gap for partition [default: 300]
-P <plot> --plot =<plot> Display figure [default: True]
"""
# Michael Arnold
# 10/10/17
import scipy.special as sp
import scipy.stats as stats
import numpy as np
import matplotlib.pyplot as plt
import docopt
import sys,os
sys.setrecursionlimit(10000)
def process_args(args):
global dimx
global dimy
global pixel_gap
'''process all command line arguments and return relevant values.'''
n = int(args['--n'])
dist_type = args['--dist_type']
dimx = int(args['--dimx'])
dimy = int(args['--dimy'])
pixel_gap= int(args['--pixel'])
plot = args['--plot']
print(plot)
return n,dist_type,dimx,dimy,pixel_gap,plot
def hot_firebreaks(max_x,max_y):
""" Create data file for firebreak xy position"""
HOT = np.genfromtxt("optimal_forests/optimal_forest_L64_DL.csv",delimiter=",")
HOT_data= np.zeros([1,2])
for i,x in enumerate(HOT):
for j,y in enumerate(x):
if y==0:
HOT_data = np.append(HOT_data,np.array([[i,j]]),axis=0)
HOT_data=HOT_data[1:,:]
x = HOT_data[:,0]
y = HOT_data[:,1]
x *= 1/64
x = x*(dimx-2*pixel_gap)+pixel_gap
y *=1/64
y = y*(dimy-2*pixel_gap)+pixel_gap
np.savetxt('cart-1.2.2/'+'xy'+str(dimx)+str(dimy)+'hot0.dat',np.c_[x,y],fmt='%10.8f %10.8f')
return
def generate_dist(n,dist_type):
if dist_type == 'exp':
x = stats.expon.rvs(size = n)
x *= 1/np.max(x)
x=x*(dimx-2*pixel_gap)+pixel_gap
y = stats.expon.rvs(size = n)
y *=1/np.max(y)
y = y*(dimy-2*pixel_gap)+pixel_gap
return x,y
## add distribution types
elif dist_type == "gaussian":
sig=10
#x = np.sqrt(2*sig)*sp.erfinv(2*np.random.rand(n)+1)#*(dimx-2*pixel_gap)+pixel_gap
#y = np.sqrt(2*sig)*sp.erfinv(2*np.random.rand(n)+1)#*(dimy-2*pixel_gap)+pixel_gap
x = np.abs(stats.norm.rvs(size = n))
x *= 1/np.max(x)
x=x*(dimx-2*pixel_gap)+pixel_gap
y = np.abs(stats.norm.rvs(size = n))
y *=1/np.max(y)
y = y*(dimy-2*pixel_gap)+pixel_gap
return x,y
elif dist_type == "hot":
sig=10
#x = np.sqrt(2*sig)*sp.erfinv(2*np.random.rand(n)+1)#*(dimx-2*pixel_gap)+pixel_gap
#y = np.sqrt(2*sig)*sp.erfinv(2*np.random.rand(n)+1)#*(dimy-2*pixel_gap)+pixel_gap
x = np.abs(stats.norm.rvs(size = n,scale=64))
max_x=np.max(x)
x *= 1/np.max(x)
x=x*(dimx-2*pixel_gap)+pixel_gap
y = np.abs(stats.norm.rvs(size = n,scale=64))
max_y=np.max(y)
y *=1/np.max(y)
y = y*(dimy-2*pixel_gap)+pixel_gap
hot_firebreaks(max_x,max_y)
return x,y
else:
print("Error: distribution not recognized")
def find_density(x,y,dimx,dimy,pixel_gap):
'''finds spatial density in grid'''
nx,ny = dimx-(2*pixel_gap),dimy-(2*pixel_gap) # change to be number of pixels
H,xedges,yedges = np.histogram2d(x/np.max(x),y/np.max(y),bins=[nx,ny])
ave_density = np.average(H)
return H,xedges,yedges,ave_density
def generate_array(H,xedges,yedges,dimx,dimy,pixel_gap,ave_density):
'''make array of data in correct dimensions to feed to cart.c'''
data = np.ones([dimx,dimy])
data *= ave_density
for i,x in enumerate(H):
for j,y in enumerate(x):
data[i+pixel_gap,j+pixel_gap] = y
return data
def output_data(data,filename):
'''Setup the output file and return for writing.'''
np.savetxt('cart-1.2.2/'+filename,data,fmt='%10.8f')
##### maybe take this from command line
def main():
# Get command line arguments
n,dist_type,dimx,dimy,pixel_gap,plot = process_args(docopt.docopt(__doc__))
print(n,dist_type)
filename = str(dimx)+str(dimy)+dist_type+'.dat'
x,y = generate_dist(n,dist_type)
np.savetxt('cart-1.2.2/'+'xy'+filename,np.c_[x,y],fmt='%10.8f %10.8f')
H,xedges,yedges,ave_density = find_density(x,y,dimx,dimy,pixel_gap)
np.savetxt('cart-1.2.2/'+'H'+filename,np.c_[x,y],fmt='%10.8f %10.8f')
data = generate_array(H,xedges,yedges,dimx,dimy,pixel_gap,ave_density)
output_data(data,filename)
print('Data file created in directory cart-1.2.1/'+filename)
# plot things
if plot == 'True':
plt.plot(x,y,',')
#plt.xlim([0,10])
#plt.ylim([0,10])
plt.show()
if __name__ == '__main__':
main()