-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstats.py
More file actions
151 lines (128 loc) · 3.84 KB
/
stats.py
File metadata and controls
151 lines (128 loc) · 3.84 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 3 03:56:25 2019
@author: gabriel
"""
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as sci
M,P,D,A=[[] for i in range(4)]
with open('moondata','r') as f:
next(f)
for line in f:
# print(line)
# print(line.split(','))
l = line.split(',')
if l[0]!='':
M.append(eval(l[0]))
else:
M.append(-1)
P.append(eval(l[1]))
D.append(eval(l[2]))
if len(l)>=4 and l[3]!='\n':
A.append(eval(l[3]))
else:
A.append(-1)
#make numpy arrays
M = np.array(M)
P = np.array(P)
D = np.array(D)
A = np.array(A)
#%%
# histograms
plt.figure(1, figsize = (7, 5))
logbins = np.logspace(np.log10(np.min(P[~(P == -1)])), \
np.log10(np.max(P[~(P == -1)])), 50)
plt.hist(P, bins = logbins,color='#0504aa', \
alpha=0.7, rwidth=0.85)
plt.grid(axis='y', alpha=0.75)
plt.xscale('log')
plt.xlabel('Period (h)', fontsize = 16)
plt.ylabel('Frequency', fontsize = 16)
#%%
# histograms
plt.figure(2, figsize = (7, 5))
logbins = np.logspace(np.log10(np.min(M[~(M == -1)])), \
np.log10(np.max(M[~(M == -1)])), 20)
plt.hist(M, bins = logbins, color='#0504aa', \
alpha=0.7, rwidth=0.85)
plt.xscale('log')
plt.xlabel('Mass (kg)', fontsize = 16)
plt.ylabel('Frequency', fontsize = 16)
#%%
# histograms
plt.figure(3, figsize = (7, 5))
plt.grid(axis='y', alpha=0.75)
logbins = np.logspace(np.log10(np.min(D[~(D == -1)])), \
np.log10(np.max(D[~(D == -1)])), 20)
plt.hist(D, bins = logbins, color='#0504aa', \
alpha=0.7, rwidth=0.85)
plt.xscale('log')
plt.xlabel('Distance (km)', fontsize = 16)
plt.ylabel('Frequency', fontsize = 16)
#%%
# histograms
plt.figure(4, figsize = (7, 5))
plt.grid(axis='y', alpha=0.75)
plt.hist(A[~(A == -1)], bins = 20, color='#0504aa', \
alpha=0.7, rwidth=0.85)
plt.xlabel('Angle of orbit (degree)', fontsize = 16)
plt.ylabel('Frequency', fontsize = 16)
#%% correlatioms
plt.scatter(P, D)
plt.xscale('log')
plt.yscale('log')
plt.ylim([1, 20**10])
#%%
indexes = np.where(~(A == -1) & ~(D == -1))
plt.scatter(A[indexes], D[indexes])
plt.yscale('log')
#%%
indexes = np.where(~(A == -1) & ~(P == -1))
plt.scatter(A[indexes], P[indexes])
plt.yscale('log')
#%%
#make data with APD
indexes = np.where(~(A == -1) & ~(P == -1) & ~(D == -1))
X = np.column_stack((A[indexes], np.log10(P[indexes]), np.log10(D[indexes])))
#normalize X
mean = np.mean(X, axis = 0)
std = np.std(X, axis = 0)
Xnorm = (X - mean) / std
#%%
# plot X in 3D
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(Xnorm[:, 0], Xnorm[:, 1], Xnorm[:, 2], c= 'k')
#%%
# apply gaussian KDE
from scipy.stats import gaussian_kde
kernel = gaussian_kde(Xnorm.T, bw_method = 0.1)
Xnewnorm = kernel.resample(25)
Xnewnorm = Xnewnorm.T
Xnew = Xnewnorm * std + mean
Xnew[:, 1:] = 10 ** (Xnew[:, 1:])
fig = plt.figure(figsize=(16,10))
ax = fig.add_subplot(1, 3, 1)
ax.scatter(X[:, 0], 10 ** X[:, 1], c = 'b')
ax.scatter(Xnew[:, 0], Xnew[:, 1], c = 'r')
ax.set_yscale('log')
ax.set_xlabel('Angle of orbit (degree)', fontsize = 16)
ax.set_ylabel('Period (h)', fontsize = 16)
ax = fig.add_subplot(1, 3, 2)
ax.scatter(X[:, 0], 10 ** X[:, 2], c = 'b')
ax.scatter(Xnew[:, 0], Xnew[:, 2], c = 'r')
ax.set_yscale('log')
ax.set_xlabel('Angle of orbit (degree)', fontsize = 16)
ax.set_ylabel('Distance (km)', fontsize = 16)
ax = fig.add_subplot(1, 3, 3)
ax.scatter(10 ** X[:, 1], 10 ** X[:, 2], c = 'b')
ax.scatter(Xnew[:, 1], Xnew[:, 2], c = 'r')
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xlabel('Period (h)', fontsize = 16)
ax.set_ylabel('Distance (km)', fontsize = 16)
plt.tight_layout()