import sciris as sc
od = sc.odict(a=['some', 'strings'], b=[1,2,3])
print(od)#0: 'a': ['some', 'strings']
#1: 'b': [1, 2, 3]
Needing a better way of ordering dictionaries was one of the original inspirations for Sciris back in 2014. In those dark days of Python <=3.6, dictionaries were unordered, which meant that dict.keys() could give you anything. (And you still can’t do dict.keys()[0], much less dict[0]). This tutorial describes Sciris’ ordered dict, the odict, its close cousin the objdict, and its pandas-powered pseudorelative, the dataframe.
odictIn basically every situation except one, an odict can be used like a dict. (Since this is a tutorial, see if you can intuit what that one situation is!) For example, creating an odictworks just like creating a regular dict:
#0: 'a': ['some', 'strings']
#1: 'b': [1, 2, 3]
Okay, it doesn’t exactly look like a dict, but it is one:
Keys: ['a', 'b']
Values: [['some', 'strings'], [1, 2, 3]]
Items: [('a', ['some', 'strings']), ('b', [1, 2, 3])]
Looks pretty much the same as a regular dict, except that od.keys() returns a regular list (so, yes, you can do od.keys()[0]). But, you can do things you can’t do with a regular dict, such as:
Item 0 is called a and has value ['some', 'strings']
Item 1 is called b and has value [1, 2, 3]
We can, as you probably guessed, also retrieve items by index as well:
Remember the question about the situation where you wouldn’t use an odict? The answer is if your dict has integer keys, then although you still could use an odict, it’s probably best to use a regular dict. But even float keys are fine to use (if somewhat strange).
You might’ve noticed that the odict has more verbose output than a regular dict. This is because its primary purpose is as a high-level container for storing large(ish) objects.
For example, let’s say we want to store a number of named simulation results. Look at how we’re able to leverage the odict in the loop that creates the plots
import numpy as np
import matplotlib.pyplot as plt
class Sim:
def __init__(self, n=20, n_factors=6):
self.results = sc.odict()
self.n = n
self.n_factors = n_factors
def run(self):
for i in range(self.n_factors):
label = f'y = N^{i+1}'
result = np.random.randn(self.n)**(i+1)
self.results[label] = result
def plot(self):
with sc.options.context(jupyter=True): # Jupyter-optimized plotting
plt.figure()
rows,cols = sc.getrowscols(len(self.results))
for i,label,result in self.results.enumitems(): # odict magic!
plt.subplot(rows, cols, i+1)
plt.scatter(np.arange(self.n), result, c=result, cmap='parula')
plt.title(label)
sc.figlayout() # Trim whitespace from the figure
sim = Sim()
sim.run()
sim.plot()
We can quickly access these results for exploratory data analysis without having to remember and type the labels explicitly:
Sim results are
#0: 'y = N^1':
array([ 1.01216514, -1.22082793, 0.36278103, 2.1954858 , -1.4946486 ,
1.15596067, 1.00634502, -1.17885787, 0.14802834, 1.55746228,
2.22051031, 2.02977198, -0.41751767, 0.62817532, 1.29201681,
-1.142223 , -0.96159448, 0.75942302, 0.00340971, -1.13153331])
#1: 'y = N^2':
array([1.13969778e-01, 1.82711801e-03, 6.03321879e-01, 2.06232880e+00,
2.20680656e-01, 1.19080419e+00, 8.56749454e-01, 1.72038018e+00,
1.71332317e+00, 5.73115584e-02, 8.78160179e-01, 6.51546682e-01,
1.61517890e-02, 5.03785427e-01, 1.38325022e-01, 1.86724121e-01,
2.83099987e-01, 2.91324661e-02, 1.32671651e-01, 2.44072431e+00])
#2: 'y = N^3':
array([-1.13713546e+00, -5.76629705e-04, -3.55073886e-02, -1.25440995e+00,
1.73332689e-03, -2.21497160e+00, -2.01572497e-01, -2.08165538e-02,
8.83635216e+00, -2.05433026e+00, -1.16080150e-01, -9.98761416e-01,
2.86486465e+00, 2.50627717e+00, 2.52620013e-01, 1.98576401e-01,
7.28865221e-03, -4.38194915e-03, -9.80729062e-01, 2.41848742e+00])
#3: 'y = N^4':
array([6.49577360e-02, 1.32197536e+00, 7.08824041e-01, 4.12371073e-02,
5.35520077e+00, 8.59730589e-03, 2.61465804e+00, 7.43671313e-05,
1.57545427e-07, 4.29182837e+01, 9.02197028e-03, 1.97819429e+00,
4.36248364e+00, 3.35482902e+01, 5.37658921e-02, 1.80974336e-01,
1.02528660e+01, 5.82857576e+00, 1.66147907e-01, 1.86653826e-02])
#4: 'y = N^5':
array([ 8.80781287e-03, 3.10063727e+01, 1.07974053e+02, 4.17294661e-09,
-4.93152175e+00, 8.40446990e+01, -3.31472909e+00, -2.22027586e+00,
-2.00317908e+00, 7.25438597e-02, -6.19505901e-03, -6.73031608e-03,
-1.10140443e-03, 3.61749915e-02, 3.25378722e-06, -3.49190249e-05,
1.57373919e+01, 8.95903133e+01, -5.79972433e+01, 1.51642713e-01])
#5: 'y = N^6':
array([4.24883643e-01, 1.47554386e+02, 4.93020907e-01, 8.42882967e-01,
1.02425295e+00, 1.19213081e-02, 4.49910631e+00, 1.58666817e-06,
1.36977361e-02, 1.41976240e+00, 1.62613286e-03, 1.16465604e-06,
3.09843870e-04, 7.86655470e+01, 2.13621697e+00, 2.72652880e-04,
6.87290584e+01, 2.86836736e-01, 1.94269042e-02, 2.44431481e+01])
The first set of results is
[ 1.01216514 -1.22082793 0.36278103 2.1954858 -1.4946486 1.15596067
1.00634502 -1.17885787 0.14802834 1.55746228 2.22051031 2.02977198
-0.41751767 0.62817532 1.29201681 -1.142223 -0.96159448 0.75942302
0.00340971 -1.13153331]
The first set of results has median
0.495 (95% CI: -1.365, 2.209)
This is a have-your-cake-and-eat-it-too situation: the first set of results is correctly labeled (sim.results['y = N^1']), but you can easily access it without having to type all that (sim.results[0]).
objdictWhen you’re just writing throwaway analysis code, it can be a pain to type mydict['key1']['key2'] over and over. (Right-pinky overuse is a real medical issue.) Wouldn’t it be nice if you could just type mydict.key1.key2, but otherwise have everything work exactly like a dict? This is where the objdict comes in: it’s identical to an odict (and hence like a regular dict), except you can use “object syntax” (a.b) instead of “dict syntax” (a['b']). This is especially handy for using f-strings, since you don’t have to worry about nested quotes:
Checking ob[0] = ['some', 'strings']
Checking ob.key1 = ['some', 'strings']
Checking ob["key1"] = ['some', 'strings']
In most cases, you probably want to use objdicts rather than odicts just to have the extra flexibility. Why would you ever use an odict over an objdict? Mostly just because there’s small but nonzero overhead in doing the extra attribute checking: odict is faster (faster than even collections.OrderedDict, though slower than a plain dict). The differences are tiny (literally nanoseconds) so won’t matter unless you’re doing millions of operations. But if you’re reading this, chances are high that you do sometimes need to do millions of dict operations.
The Sciris sc.dataframe() works exactly like pandas pd.DataFrame(), with a couple extra features, mostly to do with creation, indexing, and manipulation.
Any valid pandas dataframe initialization works exactly the same in Sciris. However, Sciris is a bit more flexible about how you can create the dataframe, again optimized for letting you make them quickly with minimal code. For example:
It’s not a huge difference, but the Sciris one is shorter. Sciris also makes it easier to define types on dataframe creation:
x y z
0 a 1.0 True
1 b 2.0 False
2 c 3.0 True
You can also define data types along with the columns:
x y z
0 a 1.0 True
1 b 2.0 False
2 c 3.0 True
The df.disp() command will do its best to show the full dataframe. By default, Sciris dataframes (just like pandas) are shown in abbreviated form:
0 1 2 3 4 5 6 \
0 0.828700 0.048886 0.768274 0.843568 0.030478 0.854842 0.761076
1 0.155621 0.608770 0.624841 0.730224 0.083377 0.648842 0.539396
2 0.096059 0.759670 0.260757 0.343641 0.736119 0.147592 0.774187
3 0.355791 0.388584 0.024696 0.794519 0.142946 0.160582 0.435262
4 0.217018 0.894533 0.812892 0.169658 0.813665 0.456990 0.673954
.. ... ... ... ... ... ... ...
65 0.635292 0.172450 0.950885 0.919725 0.077633 0.320526 0.682889
66 0.644394 0.547738 0.640064 0.816581 0.736159 0.588619 0.427447
67 0.924516 0.072059 0.401013 0.127436 0.110339 0.102318 0.487392
68 0.336104 0.040524 0.837547 0.082373 0.940718 0.897675 0.999384
69 0.476757 0.923131 0.993134 0.765488 0.191435 0.165112 0.532726
7 8 9
0 0.784482 0.655205 0.448030
1 0.748897 0.347327 0.035430
2 0.943415 0.277750 0.877120
3 0.445637 0.499421 0.182285
4 0.715807 0.312566 0.940190
.. ... ... ...
65 0.927933 0.914098 0.472105
66 0.140298 0.193569 0.319331
67 0.571294 0.975276 0.733944
68 0.242543 0.602931 0.568328
69 0.516984 0.434041 0.309105
[70 rows x 10 columns]
But sometimes you just want to see the whole thing. The official way to do it in pandas is with pd.options_context, but this is a lot of effort if you’re just poking around in a script or terminal (which, if you’re printing a dataframe, you probably are). By default, df.disp() shows the whole damn thing:
0 1 2 3 4 5 6 7 8 9
0 0.8287 0.0489 0.7683 0.8436 0.0305 0.8548 0.7611 0.7845 0.6552 0.4480
1 0.1556 0.6088 0.6248 0.7302 0.0834 0.6488 0.5394 0.7489 0.3473 0.0354
2 0.0961 0.7597 0.2608 0.3436 0.7361 0.1476 0.7742 0.9434 0.2778 0.8771
3 0.3558 0.3886 0.0247 0.7945 0.1429 0.1606 0.4353 0.4456 0.4994 0.1823
4 0.2170 0.8945 0.8129 0.1697 0.8137 0.4570 0.6740 0.7158 0.3126 0.9402
5 0.7097 0.6218 0.2316 0.2562 0.9651 0.0132 0.9296 0.5987 0.8221 0.2353
6 0.5245 0.0895 0.2948 0.5057 0.1875 0.9821 0.1707 0.7256 0.8030 0.7894
7 0.4115 0.6715 0.8603 0.2350 0.2552 0.9439 0.8979 0.6060 0.4046 0.2433
8 0.2918 0.9541 0.0687 0.7500 0.2754 0.3608 0.3121 0.0207 0.6600 0.8976
9 0.6783 0.7008 0.6008 0.5879 0.5199 0.4409 0.7930 0.4580 0.4126 0.0168
10 0.2227 0.7654 0.4085 0.6556 0.1976 0.7889 0.7746 0.7360 0.7141 0.4582
11 0.9869 0.5400 0.4103 0.2226 0.7955 0.5153 0.2664 0.3714 0.2770 0.2186
12 0.2353 0.1669 0.5716 0.4126 0.0705 0.2139 0.6916 0.8461 0.0281 0.3143
13 0.9127 0.6664 0.7803 0.9326 0.6729 0.7228 0.3938 0.1897 0.1205 0.7616
14 0.7619 0.4722 0.8172 0.8504 0.9246 0.8178 0.3874 0.1953 0.1961 0.3169
15 0.8866 0.3428 0.0339 0.9896 0.1862 0.2292 0.3602 0.8766 0.9700 0.3087
16 0.5083 0.8357 0.6351 0.2651 0.7161 0.5396 0.1265 0.8298 0.3861 0.2643
17 0.7618 0.7189 0.7040 0.9252 0.3042 0.7183 0.8541 0.5121 0.1331 0.5424
18 0.6096 0.0028 0.9154 0.0928 0.6710 0.1327 0.7958 0.2380 0.2457 0.6359
19 0.9861 0.6678 0.1663 0.4092 0.0945 0.1555 0.1873 0.8528 0.9001 0.9756
20 0.2467 0.9495 0.0767 0.0108 0.5315 0.8197 0.2282 0.5234 0.1204 0.9618
21 0.5041 0.2000 0.5129 0.6163 0.4890 0.5407 0.1781 0.1511 0.3319 0.9306
22 0.6529 0.6192 0.8678 0.8329 0.0342 0.2700 0.7290 0.5906 0.2878 0.6170
23 0.1968 0.9586 0.1234 0.8736 0.1262 0.8248 0.3104 0.6375 0.8629 0.5467
24 0.4721 0.1127 0.1404 0.6832 0.0338 0.7064 0.0246 0.2905 0.9692 0.1530
25 0.5040 0.2106 0.7425 0.6280 0.3769 0.4415 0.8378 0.9526 0.5114 0.3342
26 0.7216 0.6611 0.3830 0.0510 0.4681 0.3551 0.1844 0.0111 0.0132 0.7284
27 0.4232 0.9135 0.6549 0.5887 0.4432 0.3820 0.7625 0.0904 0.9921 0.4535
28 0.4938 0.0594 0.1107 0.2134 0.9438 0.1951 0.7588 0.3044 0.7649 0.1775
29 0.5494 0.8086 0.4051 0.8074 0.0881 0.5834 0.3340 0.5670 0.4056 0.6930
30 0.8982 0.4595 0.9332 0.7280 0.6483 0.2957 0.9879 0.7359 0.7242 0.2712
31 0.8373 0.7857 0.2016 0.3737 0.3552 0.7948 0.3971 0.6565 0.0110 0.6682
32 0.1127 0.3939 0.9211 0.6105 0.2493 0.7565 0.3284 0.2088 0.7615 0.1108
33 0.3646 0.8868 0.7023 0.8222 0.0836 0.8962 0.1885 0.9414 0.9703 0.2774
34 0.5105 0.2884 0.1480 0.3854 0.9225 0.1040 0.0515 0.7469 0.3248 0.6451
35 0.0798 0.2201 0.5783 0.8885 0.6947 0.0058 0.5053 0.0862 0.4673 0.3876
36 0.3105 0.9268 0.6725 0.8405 0.2255 0.2012 0.2875 0.9523 0.9550 0.9452
37 0.7626 0.8486 0.8262 0.1082 0.3442 0.0842 0.8347 0.1330 0.5367 0.7210
38 0.9761 0.0333 0.5839 0.6964 0.8159 0.8438 0.2270 0.6780 0.7267 0.7633
39 0.3762 0.8801 0.3093 0.4613 0.9376 0.2641 0.5088 0.3711 0.7794 0.0990
40 0.3969 0.6636 0.8543 0.3558 0.8490 0.1391 0.2080 0.7993 0.7334 0.9894
41 0.0559 0.1288 0.3865 0.3092 0.5184 0.0840 0.8373 0.0574 0.1086 0.9954
42 0.8816 0.9999 0.1260 0.6582 0.7104 0.7761 0.5192 0.9331 0.6181 0.2494
43 0.6122 0.3268 0.8437 0.9606 0.6914 0.9847 0.4107 0.8181 0.1626 0.7726
44 0.5884 0.8852 0.0486 0.5585 0.3697 0.9258 0.6510 0.2454 0.5702 0.6445
45 0.9898 0.6272 0.4508 0.2368 0.3786 0.6677 0.4226 0.5921 0.3510 0.7702
46 0.9895 0.2839 0.9626 0.9136 0.3996 0.6324 0.7439 0.3073 0.5029 0.0704
47 0.1599 0.0169 0.3956 0.9310 0.0380 0.6093 0.6214 0.3923 0.8262 0.6053
48 0.9073 0.6810 0.3158 0.5280 0.4567 0.5401 0.7929 0.4084 0.7957 0.7628
49 0.3968 0.8873 0.1233 0.7708 0.4375 0.1261 0.3319 0.9312 0.2883 0.1348
50 0.9001 0.4889 0.0717 0.4014 0.4977 0.5438 0.9231 0.9823 0.9884 0.5854
51 0.5547 0.0092 0.9548 0.8992 0.6618 0.7780 0.3333 0.8700 0.0850 0.1200
52 0.0956 0.6856 0.0514 0.0959 0.7089 0.7578 0.9091 0.5580 0.8806 0.3962
53 0.3205 0.6266 0.9781 0.1115 0.0314 0.6552 0.1055 0.5731 0.7569 0.9184
54 0.7348 0.7936 0.2120 0.7424 0.1363 0.6645 0.9525 0.6008 0.5767 0.6909
55 0.9081 0.4786 0.8231 0.2445 0.7595 0.1879 0.7846 0.6853 0.7137 0.3832
56 0.3818 0.2833 0.4068 0.1154 0.3832 0.5573 0.3040 0.1508 0.3007 0.6638
57 0.9493 0.4661 0.1473 0.4228 0.5056 0.9029 0.7939 0.8607 0.6967 0.7629
58 0.9142 0.2392 0.3656 0.2303 0.8695 0.1536 0.3652 0.8691 0.3594 0.7320
59 0.1854 0.4955 0.4559 0.9096 0.6134 0.4737 0.7173 0.3279 0.4061 0.0720
60 0.4597 0.7414 0.4938 0.0123 0.4576 0.3301 0.0719 0.1567 0.8827 0.0411
61 0.0657 0.1268 0.4038 0.3594 0.0828 0.1322 0.1866 0.7609 0.4065 0.2265
62 0.8915 0.0978 0.0771 0.7690 0.0711 0.2854 0.8306 0.4137 0.9009 0.9400
63 0.1494 0.5214 0.8563 0.6342 0.1266 0.8731 0.4700 0.5787 0.9301 0.0712
64 0.2652 0.1913 0.7154 0.0194 0.2959 0.6312 0.2526 0.0013 0.5618 0.0526
65 0.6353 0.1724 0.9509 0.9197 0.0776 0.3205 0.6829 0.9279 0.9141 0.4721
66 0.6444 0.5477 0.6401 0.8166 0.7362 0.5886 0.4274 0.1403 0.1936 0.3193
67 0.9245 0.0721 0.4010 0.1274 0.1103 0.1023 0.4874 0.5713 0.9753 0.7339
68 0.3361 0.0405 0.8375 0.0824 0.9407 0.8977 0.9994 0.2425 0.6029 0.5683
69 0.4768 0.9231 0.9931 0.7655 0.1914 0.1651 0.5327 0.5170 0.4340 0.3091
You can also pass other options if you want to customize it further:
0 1 ... 8 9
0 8.3e-01 4.9e-02 ... 0.7 4.5e-01
1 1.6e-01 6.1e-01 ... 0.3 3.5e-02
2 9.6e-02 7.6e-01 ... 0.3 8.8e-01
3 3.6e-01 3.9e-01 ... 0.5 1.8e-01
4 2.2e-01 8.9e-01 ... 0.3 9.4e-01
.. ... ... ... ... ...
65 6.4e-01 1.7e-01 ... 0.9 4.7e-01
66 6.4e-01 5.5e-01 ... 0.2 3.2e-01
67 9.2e-01 7.2e-02 ... 1.0 7.3e-01
68 3.4e-01 4.1e-02 ... 0.6 5.7e-01
69 4.8e-01 9.2e-01 ... 0.4 3.1e-01
[70 rows x 10 columns]
All the regular pandas methods (df['mycol'], df.mycol, df.loc, df.iloc, etc.) work exactly the same. But Sciris gives additional options for indexing. Specifically, getitem commands (what happens under the hood when you call df[thing]) will first try the standard pandas getitem, but then fall back to iloc if that fails. For example:
——————————————————————— Regular pandas indexing ——————————————————————— 23 ————————————————————————— Pandas-like iloc indexing ————————————————————————— x 2 values 23 valid 0 Name: 1, dtype: int64 ——————————————————————— Automatic iloc indexing ——————————————————————— x 2 values 23 valid 0 Name: 1, dtype: int64
One quirk of pandas dataframes is that almost every operation creates a copy rather than modifies the original dataframe in-place (leading to the infamous SettingWithCopyWarning.) This is extremely helpful, and yet, sometimes you do want to modify a dataframe in place. For example, to append a row:
x y z
0 a 1 1
1 b 2 0
2 c 3 1
3 d 4 0
That was easy! For reference, here’s the pandas equivalent (since append was deprecated):
That’s rather a pain to type, and if you mess up (e.g. type newrow instead of [newrow]), in some cases it won’t even fail, just give you the wrong result! Crikey.
Just like how sc.cat() will take anything vaguely arrayish and turn it into an actual array, sc.dataframe.cat() will do the same thing: