-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphyserror.py
1544 lines (1325 loc) · 65.9 KB
/
physerror.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
from numpy.typing import ArrayLike
from matplotlib import pyplot as plt
import scipy.stats as stats
from scipy.integrate import solve_ivp
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import pandas as pd
from dataclasses import dataclass, field
import seaborn as sns
import sys
import inquirer
@dataclass
class Data():
""" An initializer and container dataclass that is initialized and reused
by the user and other classes, as well as their methods. There are many
attributes that calculate common statistical errors, constants, and
general error propagation methods, and two class methods.
Attributes
----------
delta : float
[]
A : float
[]
B : float
[]
x_mean : float
[]
x_best : float
[]
y_mean : float
[]
y_best : float
[]
sigma_y : float
[]
sigma_A : float
[]
sigma_B : float
[]
sigma_x : float
[]
sigma_x_best : float
[]
sigma_y_best : float
[]
sigma_x_mean : float
[]
sigma_y_mean : float
[]
sigma_frac : float
[]
Parameters
----------
user_x_data : ArrayLike = [1,2,3,4,5]
[]
user_y_data : ArrayLike = [1,2,3,4,5]
[]
data_type : str = 'manual'
[]
Note
----
Will update this soon-ish
"""
user_x_data : ArrayLike = field(default_factory = lambda : [1,2,3,4,5])
user_y_data : ArrayLike = field(default_factory = lambda : [1,2,3,4,5])
data_type : str = field(default_factory = lambda : 'manual')
_x_data : np.ndarray = field(init=False)
_y_data : np.ndarray = field(init=False)
_df : pd.DataFrame = field(init=False)
_colname1 : str = field(init=False)
_colname2 : str = field(init=False)
def __post_init__(cls):
cls._x_data, cls._y_data, cls._df, \
cls._colname1, cls._colname2 = cls._initdata(cls.user_x_data, cls.user_y_data)
cls.user_x_data = cls._x_data
cls.user_y_data = cls._y_data
cls._initcalcs()
def _initcalcs(cls):
# Checks that cls._x_data is not an empty array. If it is, it does not calculate
# any of the attributes inside the if statement
if len(cls._x_data) != 0:
N = len(cls._x_data)
cls.delta = N*sum(cls._x_data**2) - (sum(cls._x_data))**2
cls.A = (((sum(cls._x_data**2)*sum(cls._y_data)) - (sum(cls._x_data)*sum(cls._x_data*cls._y_data)))/cls.delta)
cls.B = (N * sum(cls._x_data*cls._y_data) - (sum(cls._x_data)*sum(cls._y_data))) / cls.delta
cls.x_mean = abs(np.mean(cls._x_data))
cls.x_best = sum(cls._x_data)/N
cls.y_mean = abs(np.mean(cls._y_data))
cls.y_best = sum(cls._y_data)/N
cls.sigma_y = np.sqrt((1/(N - 2))*sum((cls._y_data - cls.A - (cls.B * cls._x_data))**2))
cls.sigma_A = cls.sigma_y*np.sqrt(sum(cls._x_data**2)/cls.delta)
cls.sigma_B = cls.sigma_y*np.sqrt(N/cls.delta)
cls.sigma_x = np.sqrt(sum((cls._x_data - cls.x_mean)**2)/(N - 1))
cls.sigma_x_best = np.sqrt((1/(N - 1))*sum((cls._x_data - cls.x_mean)**2))
cls.sigma_y_best = np.sqrt((1/(N - 1))*sum((cls._y_data - cls.y_mean)**2))
cls.sigma_x_mean = cls.sigma_x/np.sqrt(N)
cls.sigma_y_mean = cls.sigma_y/np.sqrt(N)
cls.sigma_frac = 1/np.sqrt(2 * (N - 1))
# Initializes and returns the data that will be reused
def _initdata(cls, xdata = np.arange(5) + 1, ydata = np.arange(5) + 1):
""" Callable but largely useless if done so. Used to read in a csv if
the user so wishes, store data and their user-inputed names, then
returns that data back to the line where it was called.
Parameters
----------
xdata : np.ndarray
The given x data. As it is used, it is the "user_x_data"
passed in at the time of initialization.
ydata : np.ndarray
The given y data. As it is used, it is the "user_y_data"
pass in at the time of initialization.
Returns
-------
np.ndarray
The x_data array created from either the passed-in
user_x_data or the csv file that was read in by the user.
np.ndarray
The y_data array created from either the passed-in
user_y_data or the csv file that was read in by the user.
pd.DataFrame
The pandas DataFrame created from either the passed-in
user_x_data and user_y_data or the csv file that was
read in by the user.
str
The name entered by the user for x_data.
str
The name entered by the user for y_data.
"""
reader = FileReaders()
match cls.data_type:
case 'CSV':
x_data, y_data = reader.csvreader()
case 'Excel':
x_data, y_data = reader.excelreader()
case 'manual':
if type(xdata) == np.ndarray:
x_data = xdata
y_data = ydata
else:
x_data = np.array(xdata)
y_data = np.array(xdata)
case _:
sys.exit("Unknown data type or WIP Section, please restart.")
# Stacks the arrays to be turned into a pandas DataFrame
temparray = np.stack((x_data, y_data))
colname1 = input("Please type the first data set's name: ")
colname2 = input("Please type the second data set's name: ")
# Creates a DataFrame using the transpose of the array stack for data and the
# user input data names for column names
datafile = pd.DataFrame(np.transpose(temparray), columns = [colname1, colname2])
# Renames the df index name to Trial
datafile.index.name = 'Trial'
# Iterates the index count by one for readability
datafile.index += 1
# Deletes temparray as it will not be used again
del temparray
# Prints out the df for the user to see
print(datafile)
# Returns the given variables into the Class' cls-variables
return x_data, y_data, datafile, colname1, colname2
##### Will do docstring documentation later #####
def outlier(cls):
""" A method that creates two empty arrays then searches the
cls.x_data and cls.y_data arrays for values that are outside
the standard 2 * sigma outlier "limit".
Returns
-------
np.ndarray
An array that contains either the outliers that were found
in the user's x_data, or a string stating no outliers were
found.
np.ndarray
An array that contains either the outliers that were found
in the user's y_data, or a string stating no outliers were
found.
"""
### For some reason this is throwing an IndexError when deleting
# New x_data and y_data variables for ease of use
x_data = cls._x_data
y_data = cls._y_data
print(x_data)
print(y_data)
# Immediately exits the program if the x_data or y_data are somehow empty arrays
if np.size(x_data) == 0 or np.size(y_data) == 0:
sys.exit()
# Outlier variables to be used for later
x_outliers = np.zeros(len(x_data))
y_outliers = np.zeros(len(y_data))
# Iterater variables for x_data, x_outliers, y_data, and y_outliers respectively
i = 0
j = 0
k = 0
l = 0
# Loops through the x_data array to check for outliers
for row in x_data:
# Checks if the row value is greater than the mean + 2*sigma
if row > (cls.x_mean + 2 * cls.sigma_x):
# If above is true, inserts the row value into the j cell of x_outliers
x_outliers[j] = int(row)
# Iterates j by one
j += 1
# Deletes the outlier cell from x_data
x_data = np.delete(x_data, i)
i -= 1
# Checks if the row value is less than the mean - 2*sigma
elif row < (cls.x_mean - 2*cls.sigma_x):
# If above is true, inserts the row value into the j cell of x_outliers
x_outliers[j] = int(row)
# Iterates j by one
j += 1
# Deletes the outlier cell from x_data
x_data = np.delete(x_data, i)
i -= 1
# Iterates i by one
i += 1
# Loops through the y_data array to check for outliers
for row in y_data:
# Checks if the row value is greater than the mean + 2*sigma
if row > (cls.y_mean + 2*cls.sigma_y):
# If above is true, inserts the row value into the l cell of y_outliers
y_outliers[l] = int(row)
# Iterates l by one
l += 1
# Deletes the outlier cell from y_data
y_data = np.delete(y_data, k)
k -= 1
# Checks if the row value is less than the mean - 2*sigma
elif row < (cls.y_mean - 2*cls.sigma_y):
# If above is true, inserts the row value into the l cell of y_outliers
y_outliers[l] = int(row)
# Iterates j by one
l += 1
# Deletes the outlier cell from y_data
y_data = np.delete(y_data, k)
k -= 1
# Iterates k by one
k += 1
# Resizes the x_outliers array to the size of j to remove redundant zeroes
x_outliers.resize(j)
# Resizes the y_outliers array to the size of l to remove redundant zeroes
y_outliers.resize(l)
# Checks if there were no outliers in x_data
if np.size(x_outliers) == 0:
# If above is true, reinitializes x_outliers to the given string
x_outliers = 'No outliers in x data'
# Checks if there were no outliers in y_data
if np.size(y_outliers) == 0:
# If above is true, reinitializes y_outliers to the given string
y_outliers = 'No outliers in y data'
return x_outliers, y_outliers
def export(cls):
"""Exports error analysis calculations to either an Excel workbook
or JSON file based on user's choice.
"""
import inquirer
cls_dict = vars(cls)
cls_dict_keys = cls_dict.keys()
cls_keys_list = []
cls_values_spliced_list = []
for var in cls_dict_keys:
if any(var_type in var for var_type in ['data', 'df', 'col']):
pass
else:
cls_keys_list.append(var)
cls_values_spliced_list.append(cls_dict[var])
export_df = pd.DataFrame(np.transpose([cls_values_spliced_list]), index=cls_keys_list, columns=["Error Calculations"])
print(export_df, "\n")
file_type_q = [
inquirer.List(
"export_file",
message="Choose a file type to export to",
choices=["Excel",
"JSON"],
),
]
match inquirer.prompt(file_type_q)["export_file"]:
case "Excel":
file_name = input("Enter a file name (no extension): ")
file_path = file_name + ".xlsx"
export_df.to_excel(file_path)
case "JSON":
file_name = input("Enter a file name (no extension): ")
file_path = file_name + ".json"
export_df.to_json(file_path, orient='columns', indent=4)
print("File exported successfully.\n")
class Graphs:
"""Allows the user to create various graphs from the user_data
pulled from Data.
Attributes
----------
graph_title : str = "Graph"
String used as the title for any graphing function that is run.
Defaults to "Graph".
title_size : int = 11 | float
Numerical value used as the title font size for any graphing
function that is run. Defaults to 11.
x_label : str = "x label"
String used as the x-axis label for any graphing function with
points that is run. Defaults to "x label"
y_label : str = "y label"
String used as the y-axis label for any graphing function with
points that is run. Defaults to "y label".
p_color : str = "cyan" | list[str] | ArrayLike[str]
Either a single string, list, or ArrayLike of strings that is used
to color the points in any graphing function with points that is run.
Defaults to "cyan".
line_color : str = "black"
A string used to color the line in the linreg and resid functions.
Defaults to "black".
errbar_color : str = "red"
A string used to color the error bars in the errbargraph function.
Defaults to "red".
dist_check : str = "Yes" | "No"
A string used to determine if the datahist function will generate
normal distrbution histograms. Only accepts "Yes" and "No", and
defaults to "No".
dataset_check : int = 1 | 2
An integer used to determine how many datasets will be generated
from the datahist function. Only accepts 1 and 2, and defaults to 1.
hist_color : str = "green" | list[str] | ArrayLike[str]
Either a single string, list, or ArrayLike of strings to determine
the color of the the histogram(s) from the datahist function. Defaults
to "green".
dbl_pend_line : str = "lime"
A string used to change the color of specifically the double pendulum
function's line color. Defaults to "lime".
dbl_pend_trace : str = "black"
A string used to change the color of specifically the double pendulum
function's trace line color. Defaults to "black".
"""
def __init__(cls):
cls.graph_title = "Graph"
cls.title_size = 11
cls.x_label = "x label"
cls.y_label = "y label"
cls.p_color = 'red'
cls.line_color = 'black'
cls.errbar_color = 'red'
cls.dist_check = 'No'
cls.dataset_check = 1
cls.hist_color = 'green'
cls.dbl_pend_line = 'lime'
cls.dbl_pend_trace = 'black'
def linreg(cls, user_data : Data):
""" Uses the given x_data and y_data arrays to create a linear
regression plot.
Parameters
----------
user_data : Data
Requires the user to pass in an instance of
Data to make use of the user's data.
"""
# New x_data and y_data for ease of use
x_data = user_data._x_data
y_data = user_data._y_data
# Sets the figure's title to the default (or passed in) graph title
plt.title(cls.graph_title, fontsize = cls.title_size)
# Sets the figure data to x_data and y_data, colored orange
if np.size(cls.p_color) == 1:
plt.plot(x_data, y_data, 'o', color = cls.p_color, linestyle="")
elif np.size(cls.p_color) != 1:
for i in range(np.size(cls.p_color)):
plt.plot(x_data[i], y_data[i], 'o', color = cls.p_color[i])
# Sets the figure's xlabel to the user's entered x_data name
plt.xlabel(cls.x_label, fontsize = 11)
# Sets the figure's xlabel to the user's entered y_data name
plt.ylabel(cls.y_label, fontsize = 11)
# Adds the linear regression line to the plot
plt.plot(x_data, user_data.A + user_data.B * x_data, color = cls.line_color)
# Displays the linear regression plot
plt.show()
def errbargraph(cls, user_data : Data):
""" Uses the x data and y data from Data to create a point-based
with error bars on each point. Error size is the sigma_x value
calculated in Data.
Parameters
----------
user_data : Data
Requires the user to pass in an instance of
Data to make use of the user's data.
"""
# New df for ease of use
# df = user_data._df
x_data = user_data._x_data
y_data = user_data._y_data
plt.title(cls.graph_title, fontsize = cls.title_size)
plt.xlabel(cls.x_label, fontsize=11)
plt.ylabel(cls.y_label, fontsize=11)
# Checks the size of the p_color attribute and plots the graph depending on if it's
# greater than 1.
if np.size(cls.p_color) == 1:
plt.errorbar(x=x_data, y=y_data, yerr=user_data.sigma_x, marker=".",
capsize=3, ecolor=cls.errbar_color, linewidth=1, color=cls.p_color,
linestyle="")
elif np.size(cls.p_color) > 1:
for i in range(np.size(x_data)):
plt.errorbar(x=x_data[i], y=y_data[i], yerr=user_data.sigma_x, marker=".",
capsize=3, ecolor=cls.errbar_color, linewidth=1, color=cls.p_color[i])
plt.show()
def datahist(cls, user_data : Data):
"""Generates a histogram of one or two sets of data pulled
from the Data class using pandas' DataFrame.hist method.
Parameters
----------
user_data : Data
Requires the user to pass in an instance of
Data to make use of the user's data.
"""
# New df for ease of use
datafile = user_data._df
# Initialized to the df's columns array for future use
columns = datafile.columns
stdcheck = cls.dist_check
# Internal function used to determine what type of histogram graph will be created
def stdcheckfunc(ax = None, index = 0):
# Checks if the user entered a yes(-adjecent) input
if stdcheck.lower() == "y" or stdcheck.lower() == "yes":
# Pulls the minimum and maximum for the x-axis from data later down
if index == 0:
xmin = np.min(user_data.user_x_data)
xmax = np.max(user_data.user_x_data)
mean = user_data.x_mean
sigma = user_data.sigma_x
else:
xmin = np.min(user_data.user_y_data)
xmax = np.max(user_data.user_y_data)
mean = user_data.y_mean
sigma = user_data.sigma_y
# Creates an x-axis array of 100 values
x = np.linspace(xmin, xmax, 100)
# Calls the scipy.stats.pdf method with x as the data, user_data x_mean as the mean,
# and user_data sigma_x as the scale
p = stats.norm.pdf(x, mean, sigma)
# Creates a graph with x on the x-axis and p on the y-axis
if check == 1:
plt.plot(x, p, 'k', linewidth = 2)
else:
ax.plot(x, p, 'k', linewidth = 2)
# plt.title(cls.graph_title)
# Passes the if statement if the user entered a no(-adjecent) input
elif stdcheck.lower() == "n" or stdcheck.lower() == "no":
pass
else:
# Runs the given print statement if anything other than yes or no is given
print("Unknown input, assuming 'no'.")
pass
# Calls for and runs the histcheck function
check = cls.dataset_check
# Creates a continuous loop that breaks only if an accepted input is given
while True:
# Checks if the histcheck value is 1
if check == 1:
while True:
# If the above is true, calls for user input with the given printed statement
histnum = input(f"Which dataset would you like to use? {user_data._colname1} or {user_data._colname2}: ")
# Checks if the user input is equivalent to colname1
if histnum == user_data._colname1:
# If the above is true, creates a histogram from the first column of the user_data DataFrame
datafile.hist(bins = len(datafile.index), grid = False, rwidth = .9,
column = columns[0], color = 'green', density = True)
# Runs stdcheckfunc to create a standard distribution graph (if user entered yes earlier)
stdcheckfunc()
# Breaks out of the inner while loop
break
# Checks if the user input is equivalent to colname2
elif histnum == user_data._colname2:
# If the above is true, creates histogram from the second column of the user_data DataFrame
datafile.hist(bins = len(datafile.axes[0]), grid = False, rwidth = .9,
column = columns[1], color = 'green', density = True)
# Runs stdcheckfunc to create a standard distribution graph (if user entered yes earlier)
stdcheckfunc()
# Breaks out of the inner while loop
break
else:
# If any other input is entered, prints the given statement before going back to the start of
# the while loop
print(f"Please enter only {user_data._colname1} or {user_data._colname2}")
# Breaks out of the outer while loop
break
# Checks if the histcheck instance is equal to 2
elif check == 2:
if np.size(cls.hist_color) == 1:
local_hist_color = ['green', 'red']
else:
local_hist_color = cls.hist_color
# Creates a subplot which is 1 graph wide and 2 graphs tall
fig, axes = plt.subplots(nrows = 2, ncols = 1)
fig.suptitle(cls.graph_title, ha='center', va='center')
for i in range(check):
ax = axes[i]
# Attaches the data from the first column of the df to the top plot
datafile.hist(bins = len(datafile.axes[0]), grid = False, rwidth = .9,
column = columns[i], color = local_hist_color[i], ax = ax,
density = True)
if i == 0:
# Runs stdcheckfunc to create a standard distribution graph (if user entered yes earlier)
stdcheckfunc(ax=ax, index = i)
elif i == 1:
stdcheckfunc(ax=ax, index = i)
# Breaks out of the outer while loop
break
# Breaks out of the outer while loop
break
else:
# If any other value is given for the histcheck instance, prints the given statement
print("Invalid value detected. Terminating program.")
exit()
plt.show()
def sctrplot(cls, user_data: Data):
""" Uses the given x_data and y_data to create a scatter plot
via matplot.pyplot's scatter method. Customization options
are available, similar to the original pyplot method.
Parameters
----------
user_data : Data
Requires the user to pass in an instance of
Data to make use of the user's data.
"""
# Local instances of user_data._x_data and user_data._y_data for ease of use
x_data = user_data._x_data
y_data = user_data._y_data
# Sets a new pyplot figure with the 'constrained' layout
plt.figure(num = 1, layout = 'constrained')
# Generates a scatter plot using the given x_data for x and y_data for y
# Optional customization options can be used to change the output graph
if np.size(cls.p_color) == 1:
plt.scatter(x = x_data, y = y_data, marker = "D", c = cls.p_color, edgecolors = 'k')
elif np.size(cls.p_color) != 1:
for i in range(np.size(cls.p_color)):
plt.scatter(x = x_data[i], y = y_data[i], marker = "D", c = cls.p_color[i], edgecolors = 'k')
# Pulls user_data colname1 and colname2 for the x-axis label and
# y-axis label respectively.
plt.xlabel(cls.x_label)
plt.ylabel(cls.y_label)
# Uses the passed in gtitle argument to set the plot's title
plt.title(cls.graph_title)
print("\nDisplaying graph using user's data...")
# Displays the generated plot
plt.show()
def resid(cls, user_data: Data):
""" Uses user_data._df to create a residuals scatter plot
via the seaborn sns.residplot method. The graph's title
can optionally be customized.
Parameters
----------
user_data : Data
Requires the user to pass in an instance of
Data to make use of the user's data.
"""
# Sets a new pyplot figure
plt.figure(num = 1)
# Generates a residual scatter plot with the DataFrame created from
# the user's data. Sets x and y labels to user_data._colname1
# and user_data._colname2 respectively
sns.residplot(data = user_data._df, x = cls.x_label, y = cls.y_label)
# Sets the plot's title to the passed in gtitle argument
plt.title(cls.graph_title)
# Displays the generated plot
plt.show()
def dbl_pend(cls, theta_0 : float, phi_0 : float, theta_dot_0 = 0, phi_dot_0 = 0, anim_type = 0):
"""Generates either a point mass or bar mass double pendulum
animation based on the pass in initial values. Angles are read
as the angle between the bar/string and an imaginary horizontal
line going through the point.
Point mass calculations and animation code were taken from
matplotlib's documentation:
https://matplotlib.org/stable/gallery/animation/double_pendulum.html
Parameters
----------
theta_0 : float
Initial angle of the top bar/string.
phi_0 : float
Initial angle of the bottom bar/string.
theta_dot_0 : int = 0 (optional)
Initial velocity of the top bar/string. Defaults to 0.
phi_dot_0 : int = 0 (optional)
Initial velocity of the bottom bar/string. Defaults to 0.
anim_type : int = 0 | 1 (optional)
Optional variable that determines the type of double
pendulum that will be used. Defaults to 0 for Point Mass,
accepts 1 for Bar Mass.
"""
import matplotlib.animation as animation
gravity = 9.81 # m/s^2
len_1 = float(input('Enter the length of the top pendulum in meters: '))
len_2 = float(input('Enter the length of the bottom pendulum in meters: '))
tot_len = len_1 + len_2
mass_1 = float(input('Enter the mass of the top pendulum in g: '))
mass_2 = float(input('Enter the mass of the bottom pendulum in g: '))
time_lim = float(input('Enter the time limit in seconds: '))
def point_mass(time, state):
dydx = np.zeros_like(state)
dydx[0] = state[1]
ang_delta = state[2] - state[0]
den1 = (mass_1 + mass_2) * len_1 - mass_2 * len_1 * np.cos(ang_delta) * np.cos(ang_delta)
dydx[1] = ((mass_2 * len_1 * state[1] * state[1] * np.sin(ang_delta) * np.cos(ang_delta) + mass_2 * gravity * np.sin(state[2]) * np.cos(ang_delta)
+ mass_2 * len_2 * state[3] * state[3] * np.sin(ang_delta) - (mass_1 + mass_2) * gravity * np.sin(state[0])) / den1)
dydx[2] = state[3]
den2 = (len_2/len_1) * den1
dydx[3] = ((- mass_2 * len_2 * state[3] * state[3] * np.sin(ang_delta) * np.cos(ang_delta) + (mass_1 + mass_2) * gravity * np.sin(state[0]) * np.cos(ang_delta)
- (mass_1 + mass_2) * len_1 * state[1] * state[1] * np.sin(ang_delta) - (mass_1 + mass_2) * gravity * np.sin(state[2])) / den2)
return dydx
def bar_mass(time, state):
dydx = np.zeros_like(state)
dydx[0] = state[1]
ang_delta = state[2] - state[0]
mu = 1 + (mass_1/mass_2)
den1 = len_1*(mu - np.cos(ang_delta)**2)
dydx[1] = (gravity*(np.sin(state[2])*np.cos(ang_delta) - mu*np.sin(state[0])) -
(len_2*(state[3]**2) + len_1*(state[1]**2)*np.cos(ang_delta))*np.sin(ang_delta))/den1
dydx[2] = state[3]
den2 = (len_2/len_1) * den1
dydx[3] = (gravity*mu*(np.sin(state[0])*np.cos(ang_delta) - np.sin(state[2])) -
(mu*len_1*(state[1]**2) + len_2*(state[3]**2)*np.cos(ang_delta))*np.sin(ang_delta))/den2
return dydx
state = np.radians([theta_0, theta_dot_0, phi_0, phi_dot_0])
dt = 0.03
time = np.arange(0, time_lim, dt)
if anim_type == 0:
output = solve_ivp(point_mass, time[[0, -1]], state, t_eval=time).y.T
anim_type = 'point'
elif anim_type == 1:
output = solve_ivp(bar_mass, time[[0, -1]], state, t_eval=time).y.T
anim_type = 'bar'
else:
print('Invalid input, exiting program')
exit()
out_x1 = len_1 * np.sin(output[:, 0])
out_y1 = -len_1 * np.cos(output[:, 0])
out_x2 = len_2 * np.sin(output[:, 2]) + out_x1
out_y2 = -len_2 * np.cos(output[:, 2]) + out_y1
fig = plt.figure(figsize=(5, 4))
ax = fig.add_subplot(autoscale_on=False, xlim=(-tot_len, tot_len), ylim=(-tot_len, tot_len))
ax.set_aspect('equal')
ax.grid()
line, = ax.plot([], [], 'o-', color=cls.dbl_pend_line, lw=2)
trace, = ax.plot([], [], 'k.-', color=cls.dbl_pend_trace, lw=1, ms=2)
time_template = 'time = %.2fs'
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
def animate(i):
thisx = [0, out_x1[i], out_x2[i]]
thisy = [0, out_y1[i], out_y2[i]]
history_x = out_x2[:i]
history_y = out_y2[:i]
line.set_data(thisx, thisy)
trace.set_data(history_x, history_y)
time_text.set_text(time_template % (i*dt))
return line, trace, time_text
plt.title(cls.graph_title, fontsize=cls.title_size)
ani = animation.FuncAnimation(fig, animate, len(output), interval=dt*1000, blit=True)
print(f"Saving gif as {theta_0}{phi_0}{anim_type}anim.gif")
ani.save(f"{str(theta_0).strip('.')}{str(phi_0).strip('.')}{anim_type}anim.gif", writer='imagemagick', fps=20)
plt.show()
class _InquirePrompts:
"""A private class that contains all the methods and functions needed
to create and run the command-line-interface.
"""
def __init__(cls):
cls.graphs_obj = Graphs()
cls._title_prompt = "Title"
cls._title_size_prompt = "Title Size"
cls._x_label_prompt = "x-label"
cls._y_label_prompt = "y-label"
cls._point_colors_prompt = "Point Colors"
cls._line_color_prompt = "Line Color"
cls._errbar_color_prompt = "Error Bar Color"
cls._hist_color_prompt = "Histogram Color(s)"
cls._theta_0 = 122
cls._phi_0 = 122
cls._theta_0_dot = 0
cls._phi_0_dot = 0
cls._pend_type = 0
cls.data_q = [
inquirer.List(
"data",
message="Select a data file type",
choices=["CSV", "Excel"],
),
]
cls.funcs_q = [
inquirer.List(
"function",
message="Select a function to modify and/or run",
choices=["Export Data",
"Data.outlier",
"Graphs.linreg",
"Graphs.errbargraph",
"Graphs.datahist",
"Graphs.sctrplot",
"Graphs.resid",
"Graphs.dbl_pend",
"Change File/Data",
"Exit/Quit"],
),
]
ufunc_msg = "Select a property to change, or 'Run' to run the function. Select 'Back' to return to the function select menu."
cls.linreg_q = [
inquirer.List(
"linreg",
message="Graphs.linreg -- " + ufunc_msg,
choices=[cls._title_prompt,
cls._title_size_prompt,
cls._x_label_prompt,
cls._y_label_prompt,
cls._point_colors_prompt,
cls._line_color_prompt,
"Run", "Back", "Exit/Quit"],
),
]
cls.errbar_q = [
inquirer.List(
"errbar",
message="Graphs.errbargraph -- " + ufunc_msg,
choices=[cls._title_prompt,
cls._title_size_prompt,
cls._x_label_prompt,
cls._y_label_prompt,
cls._point_colors_prompt,
cls._errbar_color_prompt,
"Run", "Back", "Exit/Quit"],
),
]
cls.datahist_q = [
inquirer.List(
"datahist",
message="Graphs.datahist -- " + ufunc_msg,
choices=[cls._title_prompt,
cls._title_size_prompt,
"Normal Distribution",
cls._hist_color_prompt,
"Dataset Count",
"Run", "Back", "Exit/Quit"],
),
]
cls.datahist_count = [
inquirer.List(
"datahist_count",
message="Select a number of data sets to use",
choices=['1',
'2']
),
]
cls.datahist_normal = [
inquirer.List(
"datahist_normal",
message="Select Yes, No, or Cancel",
choices=['Yes',
'No',
'Cancel']
)
]
cls.sctrplot_q = [
inquirer.List(
"sctrplot",
message="Graphs.sctrplot -- " + ufunc_msg,
choices=[cls._title_prompt,
cls._title_size_prompt,
cls._x_label_prompt,
cls._y_label_prompt,
cls._point_colors_prompt,
"Run", "Back", "Exit/Quit"],
),
]
cls.resid_q = [
inquirer.List(
"resid",
message="Graphs.resid -- " + ufunc_msg,
choices=[cls._title_prompt,
cls._title_size_prompt,
cls._x_label_prompt,
cls._y_label_prompt,
cls._point_colors_prompt,
"Run", "Back", "Exit/Quit"],
),
]
cls.dbl_pend_q = [
inquirer.List(
"dblpend",
message="Graphs.dbl_pend -- " + ufunc_msg,
choices=[cls._title_prompt,
cls._title_size_prompt,
"Line Color",
"Trace Color",
"Initial Values",
"Pendulum Type",
"Run", "Back", "Exit/Quit"],
),
]
cls.dbl_pend_init_q = [
inquirer.List(
"dblpend_init",
message="Choose an initial value to change",
choices=["Theta 0",
"Theta 0 Dot",
"Phi 0",
"Phi 0 Dot",
"Back", "Exit/Quit"],
),
]
cls.dbl_pend_type_q = [
inquirer.List(
"dblpend_type",
message="Choose an pendulum type",
choices=["Point Mass",
"Bar Mass"],
),
]
cls.val_change_q = [
inquirer.List(
"val_change",
message="Would you like to edit this property?",
choices=["Yes",
"No",
"Exit/Quit"],
),
]
cls.p_color_q = [
inquirer.List(
"p_color",