-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtour.html
More file actions
631 lines (618 loc) · 59.7 KB
/
tour.html
File metadata and controls
631 lines (618 loc) · 59.7 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
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
<!DOCTYPE html>
<html lang="en" data-content_root="./">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Tour of the cctbx — CCTBX Developer documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=fa44fd50" />
<link rel="stylesheet" type="text/css" href="_static/classic.css?v=def86cc0" />
<script src="_static/documentation_options.js?v=9c77bcdb"></script>
<script src="_static/doctools.js?v=9a2dae69"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="CCTBX development history" href="history.html" />
<link rel="prev" title="Installation overview" href="installation.html" />
</head><body>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="history.html" title="CCTBX development history"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="installation.html" title="Installation overview"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">CCTBX Developer documentation</a> »</li>
<li class="nav-item nav-item-this"><a href="">Tour of the cctbx</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="tour-of-the-cctbx">
<span id="tour"></span><h1><a class="toc-backref" href="#id1" role="doc-backlink">Tour of the cctbx</a><a class="headerlink" href="#tour-of-the-cctbx" title="Link to this heading">¶</a></h1>
<nav class="contents" id="table-of-contents">
<p class="topic-title">Table of Contents</p>
<ul class="simple">
<li><p><a class="reference internal" href="#tour-of-the-cctbx" id="id1">Tour of the cctbx</a></p>
<ul>
<li><p><a class="reference internal" href="#the-beach-in-the-box" id="id2">The beach in the box</a></p></li>
<li><p><a class="reference internal" href="#at-the-very-bottom" id="id3">At the very bottom</a></p></li>
<li><p><a class="reference internal" href="#scitbx-array-family-flex" id="id4"><code class="docutils literal notranslate"><span class="pre">scitbx.array_family.flex</span></code></a></p></li>
<li><p><a class="reference internal" href="#cctbx-array-family-flex" id="id5"><code class="docutils literal notranslate"><span class="pre">cctbx.array_family.flex</span></code></a></p></li>
<li><p><a class="reference internal" href="#a-balancing-act" id="id6">A balancing act</a></p></li>
<li><p><a class="reference internal" href="#hybrid-systems" id="id7">Hybrid systems</a></p></li>
<li><p><a class="reference internal" href="#building-bridges" id="id8">Building bridges</a></p></li>
<li><p><a class="reference internal" href="#thinking-hybrid" id="id9">Thinking hybrid</a></p></li>
</ul>
</li>
</ul>
</nav>
<section id="the-beach-in-the-box">
<h2><a class="toc-backref" href="#id2" role="doc-backlink">The beach in the box</a><a class="headerlink" href="#the-beach-in-the-box" title="Link to this heading">¶</a></h2>
<p>If you go to the beach you will find massive amounts of a material known
to crystallographers as quartz, which in this case is just a fancy word
for sand. As an example here is the cctbx way of playing in the sandbox:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">cctbx</span> <span class="kn">import</span> <span class="n">xray</span>
<span class="kn">from</span> <span class="nn">cctbx</span> <span class="kn">import</span> <span class="n">crystal</span>
<span class="kn">from</span> <span class="nn">cctbx.array_family</span> <span class="kn">import</span> <span class="n">flex</span>
<span class="n">quartz_structure</span> <span class="o">=</span> <span class="n">xray</span><span class="o">.</span><span class="n">structure</span><span class="p">(</span>
<span class="n">special_position_settings</span><span class="o">=</span><span class="n">crystal</span><span class="o">.</span><span class="n">special_position_settings</span><span class="p">(</span>
<span class="n">crystal_symmetry</span><span class="o">=</span><span class="n">crystal</span><span class="o">.</span><span class="n">symmetry</span><span class="p">(</span>
<span class="n">unit_cell</span><span class="o">=</span><span class="p">(</span><span class="mf">5.01</span><span class="p">,</span><span class="mf">5.01</span><span class="p">,</span><span class="mf">5.47</span><span class="p">,</span><span class="mi">90</span><span class="p">,</span><span class="mi">90</span><span class="p">,</span><span class="mi">120</span><span class="p">),</span>
<span class="n">space_group_symbol</span><span class="o">=</span><span class="s2">"P6222"</span><span class="p">)),</span>
<span class="n">scatterers</span><span class="o">=</span><span class="n">flex</span><span class="o">.</span><span class="n">xray_scatterer</span><span class="p">([</span>
<span class="n">xray</span><span class="o">.</span><span class="n">scatterer</span><span class="p">(</span>
<span class="n">label</span><span class="o">=</span><span class="s2">"Si"</span><span class="p">,</span>
<span class="n">site</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="mf">2.</span><span class="p">,</span><span class="mi">1</span><span class="o">/</span><span class="mf">2.</span><span class="p">,</span><span class="mi">1</span><span class="o">/</span><span class="mf">3.</span><span class="p">),</span>
<span class="n">u</span><span class="o">=</span><span class="mf">0.2</span><span class="p">),</span>
<span class="n">xray</span><span class="o">.</span><span class="n">scatterer</span><span class="p">(</span>
<span class="n">label</span><span class="o">=</span><span class="s2">"O"</span><span class="p">,</span>
<span class="n">site</span><span class="o">=</span><span class="p">(</span><span class="mf">0.197</span><span class="p">,</span><span class="o">-</span><span class="mf">0.197</span><span class="p">,</span><span class="mf">0.83333</span><span class="p">),</span>
<span class="n">u</span><span class="o">=</span><span class="mi">0</span><span class="p">)]))</span>
<span class="n">quartz_structure</span><span class="o">.</span><span class="n">show_summary</span><span class="p">()</span><span class="o">.</span><span class="n">show_scatterers</span><span class="p">()</span>
</pre></div>
</div>
<p>Running this script with <a class="reference external" href="http://www.python.org/">Python</a> produces the output:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Number</span> <span class="n">of</span> <span class="n">scatterers</span><span class="p">:</span> <span class="mi">2</span>
<span class="n">It</span> <span class="n">special</span> <span class="n">positions</span><span class="p">:</span> <span class="mi">2</span>
<span class="n">Unit</span> <span class="n">cell</span><span class="p">:</span> <span class="p">(</span><span class="mf">5.01</span><span class="p">,</span> <span class="mf">5.01</span><span class="p">,</span> <span class="mf">5.47</span><span class="p">,</span> <span class="mi">90</span><span class="p">,</span> <span class="mi">90</span><span class="p">,</span> <span class="mi">120</span><span class="p">)</span>
<span class="n">Space</span> <span class="n">group</span><span class="p">:</span> <span class="n">P</span> <span class="mi">62</span> <span class="mi">2</span> <span class="mi">2</span> <span class="p">(</span><span class="n">No</span><span class="o">.</span> <span class="mi">180</span><span class="p">)</span>
<span class="n">Label</span> <span class="n">M</span> <span class="n">Coordinates</span> <span class="n">Occ</span> <span class="n">Uiso</span> <span class="ow">or</span> <span class="n">Ustar</span>
<span class="n">Si</span> <span class="mi">3</span> <span class="mf">0.5000</span> <span class="mf">0.5000</span> <span class="mf">0.3333</span> <span class="mf">1.00</span> <span class="mf">0.2000</span>
<span class="n">O</span> <span class="mi">6</span> <span class="mf">0.1970</span> <span class="o">-</span><span class="mf">0.1970</span> <span class="mf">0.8333</span> <span class="mf">1.00</span> <span class="mf">0.0000</span>
</pre></div>
</div>
<p>Note that the script is pure Python, even though at first sight the
format might appear to be specifically designed for crystallographic
data. Now let’s give the <code class="docutils literal notranslate"><span class="pre">quartz_structure</span></code> a rest break at the
beach:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">libtbx</span> <span class="kn">import</span> <span class="n">easy_pickle</span>
<span class="n">easy_pickle</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="s2">"beach"</span><span class="p">,</span> <span class="n">quartz_structure</span><span class="p">)</span>
</pre></div>
</div>
<p>This creates a file with the name <code class="docutils literal notranslate"><span class="pre">beach</span></code> containing all the
information required for restoring the <code class="docutils literal notranslate"><span class="pre">quartz_structure</span></code> <strong>object</strong>,
which is the technical term for the entire hierarchy of data
referenced by the <code class="docutils literal notranslate"><span class="pre">quartz_structure</span></code> identifier in the Python
script. A very important point to notice is that the <code class="docutils literal notranslate"><span class="pre">easy_pickle</span></code>
module used for storing the <code class="docutils literal notranslate"><span class="pre">quartz_structure</span></code> is not specific to our
object. <code class="docutils literal notranslate"><span class="pre">easy_pickle</span></code> will store and restore (almost) any
user-defined Python object.</p>
<p>Being automatically generated, the <code class="docutils literal notranslate"><span class="pre">beach</span></code> file is not pretty to look
at, but this is not important because we can easily resurrect the
original object to extract any information that we might be interested
in. In a potentially <em>different script</em> on a potentially <em>different
computer</em> with a potentially <em>different operating system</em>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">libtbx</span> <span class="kn">import</span> <span class="n">easy_pickle</span>
<span class="n">quartz_structure</span> <span class="o">=</span> <span class="n">easy_pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s2">"beach"</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that it is not necessary to explicitly import the relevant cctbx
modules in this script. Python’s <code class="docutils literal notranslate"><span class="pre">pickle</span></code> module does it for us
automatically after inspecting the <code class="docutils literal notranslate"><span class="pre">beach</span></code> file.</p>
<p>In practice a “live object” in memory is much more valuable than
information stored in a good-old file format because there are often
many different questions one can ask about particular real-world
objects. For example, we could ask: What are the site symmetries of the
atoms in the <code class="docutils literal notranslate"><span class="pre">quartz_structure</span></code>? Here is how we ask that question in
Python’s language:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">scatterer</span> <span class="ow">in</span> <span class="n">quartz_structure</span><span class="o">.</span><span class="n">scatterers</span><span class="p">():</span>
<span class="nb">print</span> <span class="s2">"</span><span class="si">%s</span><span class="s2">:"</span> <span class="o">%</span> <span class="n">scatterer</span><span class="o">.</span><span class="n">label</span><span class="p">,</span> <span class="s2">"</span><span class="si">%8.4f</span><span class="s2"> </span><span class="si">%8.4f</span><span class="s2"> </span><span class="si">%8.4f</span><span class="s2">"</span> <span class="o">%</span> <span class="n">scatterer</span><span class="o">.</span><span class="n">site</span>
<span class="n">site_symmetry</span> <span class="o">=</span> <span class="n">quartz_structure</span><span class="o">.</span><span class="n">site_symmetry</span><span class="p">(</span><span class="n">scatterer</span><span class="o">.</span><span class="n">site</span><span class="p">)</span>
<span class="nb">print</span> <span class="s2">" point group type:"</span><span class="p">,</span> <span class="n">site_symmetry</span><span class="o">.</span><span class="n">point_group_type</span><span class="p">()</span>
<span class="nb">print</span> <span class="s2">" special position operator:"</span><span class="p">,</span> <span class="n">site_symmetry</span><span class="o">.</span><span class="n">special_op</span><span class="p">()</span>
</pre></div>
</div>
<p>Answer:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Si</span><span class="p">:</span> <span class="mf">0.5000</span> <span class="mf">0.5000</span> <span class="mf">0.3333</span>
<span class="n">point</span> <span class="n">group</span> <span class="nb">type</span><span class="p">:</span> <span class="mi">222</span>
<span class="n">special</span> <span class="n">position</span> <span class="n">operator</span><span class="p">:</span> <span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="o">/</span><span class="mi">3</span>
<span class="n">O</span><span class="p">:</span> <span class="mf">0.1970</span> <span class="o">-</span><span class="mf">0.1970</span> <span class="mf">0.8333</span>
<span class="n">point</span> <span class="n">group</span> <span class="nb">type</span><span class="p">:</span> <span class="mi">2</span>
<span class="n">special</span> <span class="n">position</span> <span class="n">operator</span><span class="p">:</span> <span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">-</span><span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="o">*</span><span class="n">y</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="o">*</span><span class="n">y</span><span class="p">,</span><span class="mi">5</span><span class="o">/</span><span class="mi">6</span>
</pre></div>
</div>
<p>Another question we could ask: What are the structure factors up to
a resolution of d_min=2 Angstrom?</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">f_calc</span> <span class="o">=</span> <span class="n">quartz_structure</span><span class="o">.</span><span class="n">structure_factors</span><span class="p">(</span><span class="n">d_min</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">f_calc</span><span class="p">()</span>
<span class="n">f_calc</span><span class="o">.</span><span class="n">show_summary</span><span class="p">()</span><span class="o">.</span><span class="n">show_array</span><span class="p">()</span>
</pre></div>
</div>
<p>Answer:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Miller</span> <span class="n">array</span> <span class="n">info</span><span class="p">:</span> <span class="kc">None</span>
<span class="n">Type</span> <span class="n">of</span> <span class="n">data</span><span class="p">:</span> <span class="n">complex_double</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">7</span>
<span class="n">Type</span> <span class="n">of</span> <span class="n">sigmas</span><span class="p">:</span> <span class="kc">None</span>
<span class="n">Number</span> <span class="n">of</span> <span class="n">Miller</span> <span class="n">indices</span><span class="p">:</span> <span class="mi">7</span>
<span class="n">Anomalous</span> <span class="n">flag</span><span class="p">:</span> <span class="kc">None</span>
<span class="n">Unit</span> <span class="n">cell</span><span class="p">:</span> <span class="p">(</span><span class="mf">5.01</span><span class="p">,</span> <span class="mf">5.01</span><span class="p">,</span> <span class="mf">5.47</span><span class="p">,</span> <span class="mi">90</span><span class="p">,</span> <span class="mi">90</span><span class="p">,</span> <span class="mi">120</span><span class="p">)</span>
<span class="n">Space</span> <span class="n">group</span><span class="p">:</span> <span class="n">P</span> <span class="mi">62</span> <span class="mi">2</span> <span class="mi">2</span> <span class="p">(</span><span class="n">No</span><span class="o">.</span> <span class="mi">180</span><span class="p">)</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="p">(</span><span class="o">-</span><span class="mf">11.3483432953</span><span class="o">-</span><span class="mf">3.90019504038e-16</span><span class="n">j</span><span class="p">)</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="p">(</span><span class="o">-</span><span class="mf">14.9620947104</span><span class="o">-</span><span class="mf">25.915108226</span><span class="n">j</span><span class="p">)</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="p">(</span><span class="mf">1.46915343413</span><span class="o">-</span><span class="mf">2.54464839202</span><span class="n">j</span><span class="p">)</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="p">(</span><span class="o">-</span><span class="mf">12.8387095938</span><span class="o">+</span><span class="mi">0</span><span class="n">j</span><span class="p">)</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="p">(</span><span class="mf">5.39203951708</span><span class="o">-</span><span class="mf">9.3392864</span><span class="n">j</span><span class="p">)</span>
<span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="p">(</span><span class="o">-</span><span class="mf">1.80942693741</span><span class="o">-</span><span class="mf">2.84059649279e-16</span><span class="n">j</span><span class="p">)</span>
<span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="p">(</span><span class="mf">4.95031293935</span><span class="o">+</span><span class="mf">8.57419352432</span><span class="n">j</span><span class="p">)</span>
</pre></div>
</div>
<p>Now we could turn our attention to the new <code class="docutils literal notranslate"><span class="pre">f_calc</span></code> object and start
asking different questions. For example: What are the d-spacings?</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">f_calc</span><span class="o">.</span><span class="n">d_spacings</span><span class="p">()</span><span class="o">.</span><span class="n">show_array</span><span class="p">()</span>
</pre></div>
</div>
<p>Answer:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="mf">4.33878727296</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="mf">3.39927502294</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="mf">2.31368408207</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="mf">2.505</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="mf">2.27753582331</span>
<span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="mf">2.16939363648</span>
<span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="mf">2.01658808355</span>
</pre></div>
</div>
<p>Sometimes questions alone are not enough. We actually want to do
something. For example select only low-resolution intensities:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">low_resolution_only</span> <span class="o">=</span> <span class="n">f_calc</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">f_calc</span><span class="o">.</span><span class="n">d_spacings</span><span class="p">()</span><span class="o">.</span><span class="n">data</span><span class="p">()</span> <span class="o">></span> <span class="mf">2.5</span><span class="p">)</span>
<span class="n">low_resolution_only</span><span class="o">.</span><span class="n">show_array</span><span class="p">()</span>
</pre></div>
</div>
<p>Answer:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="p">(</span><span class="o">-</span><span class="mf">11.3483432953</span><span class="o">-</span><span class="mf">3.90019504038e-16</span><span class="n">j</span><span class="p">)</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="p">(</span><span class="o">-</span><span class="mf">14.9620947104</span><span class="o">-</span><span class="mf">25.915108226</span><span class="n">j</span><span class="p">)</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="p">(</span><span class="o">-</span><span class="mf">12.8387095938</span><span class="o">+</span><span class="mi">0</span><span class="n">j</span><span class="p">)</span>
</pre></div>
</div>
<p>Of course, the cctbx does not have a canned answer to every question,
even if it is a reasonable question. Fortunately, by virtue of being a
Python based system, the cctbx does lend itself to being extended and
embedded in order to form answers to questions that one might come
across. The cctbx has now reached a degree of completeness where this
can quite often be done without venturing into the deeper and darker
layers, the C++ core that we haven’t so far presented.</p>
</section>
<section id="at-the-very-bottom">
<h2><a class="toc-backref" href="#id3" role="doc-backlink">At the very bottom</a><a class="headerlink" href="#at-the-very-bottom" title="Link to this heading">¶</a></h2>
<p>Python is a great language for just about everything. It is just
unfortunate that we do not currently have machines smart enough to turn
any Python script into efficient machine language (but visit the <a class="reference external" href="http://psyco.sourceforge.net/">PSYCO</a>
web site to witness mankind stretching out its feelers in that
direction). Certainly, future generations will pity us for having to
resort to counting bits and bytes in order to get our work done
(imagine yourself with a set of <a class="reference external" href="http://bca.cryst.bbk.ac.uk/bca/CNews/1998/Dec98/strips.html">Beevers-Lipson strips</a> getting ready
for a structure factor calculation).</p>
<p>Some core components of the cctbx started out as ‘C’ libraries (SgInfo,
AtomInfo). Moving from ‘C’ to C++ including the Standard Template
Library (STL) was a major step away from the bits-and-bytes counting
era. For example, switching to C++ exception handling for dealing with
errors reduced the source code size significantly and resulted in much
improved readability. Equally important, using <code class="docutils literal notranslate"><span class="pre">std::vector</span></code> for
managing dynamically allocated memory was a huge improvement over using
‘C’ style raw memory allocation functions (<code class="docutils literal notranslate"><span class="pre">malloc()</span></code> and <code class="docutils literal notranslate"><span class="pre">free()</span></code>).
However, the idea of using <code class="docutils literal notranslate"><span class="pre">std::vector</span></code> throughout the cctbx wasn’t
very satisfactory: for small arrays such as 3x3 rotation matrices the
dynamic memory allocation overhead can become a rate-limiting factor,
and for large arrays the copy-semantics enforce a coding style that
is difficult to follow. For example, consider a member function of a
space group class that computes an array of multiplicities given an
array of Miller indices. The scalar version of this function would
certainly look like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">int</span> <span class="n">multiplicity</span><span class="p">(</span><span class="n">miller</span><span class="p">::</span><span class="n">index</span><span class="o"><></span> <span class="n">const</span><span class="o">&</span> <span class="n">h</span><span class="p">);</span>
</pre></div>
</div>
<p>Here is the direct translation to a vector version:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">std</span><span class="p">::</span><span class="n">vector</span><span class="o"><</span><span class="nb">int</span><span class="o">></span> <span class="n">multiplicity</span><span class="p">(</span><span class="n">std</span><span class="p">::</span><span class="n">vector</span><span class="o"><</span><span class="n">miller</span><span class="p">::</span><span class="n">index</span><span class="o"><></span> <span class="o">></span> <span class="n">const</span><span class="o">&</span> <span class="n">h</span><span class="p">);</span>
</pre></div>
</div>
<p>However, <code class="docutils literal notranslate"><span class="pre">std::vector</span></code> has deep-copy semantics (the same is true for
<code class="docutils literal notranslate"><span class="pre">std::valarray</span></code>). This results in the following:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>std::vector<int> multiplicity(std::vector<miller::index<> > const& h)
{
std::vector<int> result; // Constructs the array.
result.reserve(h.size()); // Optional, but improves performance.
for(std::size_t i=0; i<h.size();i++) { // Loops over all Miller indices.
result.push_back(multiplicity(h[i])); // Uses the scalar overload
} // to do the actual work.
return result; // Ouch!
}
</pre></div>
</div>
<p>“Ouch” indicates that the <em>entire array</em> is copied when the function
returns! While this might still be acceptable for arrays of Miller
indices which are mostly of moderate size, it becomes a real burden
when dealing with large maps. But returning to the example, in order to
avoid the copying overhead the function above could be coded as:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">void</span> <span class="n">multiplicity</span><span class="p">(</span><span class="n">std</span><span class="p">::</span><span class="n">vector</span><span class="o"><</span><span class="n">miller</span><span class="p">::</span><span class="n">index</span><span class="o"><></span> <span class="o">></span> <span class="n">const</span><span class="o">&</span> <span class="n">h</span><span class="p">,</span>
<span class="n">std</span><span class="p">::</span><span class="n">vector</span><span class="o"><</span><span class="nb">int</span><span class="o">>&</span> <span class="n">result</span><span class="p">);</span>
</pre></div>
</div>
<p>Unfortunately this is not only harder to read, but also more difficult
to use because the result has to be instantiated before the function
is called. This prevents convenient chaining of the type used in the
<code class="docutils literal notranslate"><span class="pre">quartz_structure</span></code> examples above.</p>
<p>Other major problems are the absence of a multi-dimensional array
type in the STL and limited support for algebraic array operations. We
considered using <a class="reference external" href="http://www.oonumerics.org/blitz/">Blitz++</a>, and <a class="reference external" href="http://www.boost.org/libs/multi_array/doc/">boost::multi_array</a>, but these do only
partially meet our specific requirements. For small arrays we actively
used <a class="reference external" href="http://www.boost.org/libs/array/">boost::array</a> for some time, but this was also not entirely
satisfactory due to the lack of convenient constructors which again
prevents chaining. So eventually we started the major effort of
implementing a family of small and large array types that address all
our requirements and are as uniform as possible: the scitbx array family.</p>
</section>
<section id="scitbx-array-family-flex">
<h2><a class="toc-backref" href="#id4" role="doc-backlink"><code class="docutils literal notranslate"><span class="pre">scitbx.array_family.flex</span></code></a><a class="headerlink" href="#scitbx-array-family-flex" title="Link to this heading">¶</a></h2>
<p>The scitbx array family forms the backbone of the cctbx project. Viewed
from the C++ side the family is quite big and diverse, but viewed from
the Python side things are a lot simpler, as usual. All small C++ array
types are transparently mapped to standard Python tuples. This gives
immediate access to the rich and familiar set of standard tools for
manipulating tuples. All large array types are transparently and
uniformly mapped to a group of Python types in the
<code class="docutils literal notranslate"><span class="pre">scitbx.array_family.flex</span></code> module. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">scitbx.array_family</span> <span class="kn">import</span> <span class="n">flex</span>
<span class="n">flex</span><span class="o">.</span><span class="n">double</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span> <span class="c1"># a 1-dimensional array of 30 double-precision values</span>
<span class="n">flex</span><span class="o">.</span><span class="n">int</span><span class="p">(</span><span class="n">flex</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span> <span class="c1"># a 2-dimensional array of 2x3 integer values</span>
</pre></div>
</div>
<p>For numeric element types the <code class="docutils literal notranslate"><span class="pre">flex</span></code> type supports algebraic operations:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">flex</span><span class="o">.</span><span class="n">double</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">flex</span><span class="o">.</span><span class="n">double</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">])</span>
<span class="gp">>>> </span><span class="nb">tuple</span><span class="p">(</span><span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="p">)</span>
<span class="go">(4.0, 4.0, 4.0)</span>
<span class="gp">>>> </span><span class="nb">tuple</span><span class="p">(</span><span class="n">flex</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="p">))</span>
<span class="go">(2.0, 2.0, 2.0)</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">flex</span></code> type also supports a large number of other functions
(<code class="docutils literal notranslate"><span class="pre">abs</span></code>, <code class="docutils literal notranslate"><span class="pre">sin</span></code>, <code class="docutils literal notranslate"><span class="pre">pow</span></code>, etc.), slicing, and as seen in the
<code class="docutils literal notranslate"><span class="pre">quartz_structure</span></code> example above, pickling.</p>
<p>If all this looks similar to the popular <a class="reference external" href="http://numeric.scipy.org/">Numeric</a> module: it is at the
surface. However, there are two very important differences:</p>
<ul class="simple">
<li><p>Under the hood the <code class="docutils literal notranslate"><span class="pre">flex</span></code> types are instantiations of a C++ array type
that resembles familiar STL container types as much as possible.
In contrast Numeric presents itself with a raw ‘C’ API.</p></li>
<li><p>It is straightforward to implement other <code class="docutils literal notranslate"><span class="pre">flex</span></code> types with custom
user-defined element types, even outside the scitbx module. This
would be extremely difficult to do with Numeric, and is virtually
impossible if the user-defined types are implemented in C++.</p></li>
</ul>
</section>
<section id="cctbx-array-family-flex">
<h2><a class="toc-backref" href="#id5" role="doc-backlink"><code class="docutils literal notranslate"><span class="pre">cctbx.array_family.flex</span></code></a><a class="headerlink" href="#cctbx-array-family-flex" title="Link to this heading">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">cctbx.array_family.flex</span></code> inherits all <code class="docutils literal notranslate"><span class="pre">flex</span></code> types from the
<code class="docutils literal notranslate"><span class="pre">scitbx.array_family.flex</span></code> module and adds a few types specific to
the cctbx module, for example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">cctbx.array_family</span> <span class="kn">import</span> <span class="n">flex</span>
<span class="n">flex</span><span class="o">.</span><span class="n">miller_index</span><span class="p">(((</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)))</span>
<span class="n">flex</span><span class="o">.</span><span class="n">hendrickson_lattman</span><span class="p">(((</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">)))</span>
</pre></div>
</div>
<p>Another example is <code class="docutils literal notranslate"><span class="pre">flex.xray_scatterer</span></code> used in the
<code class="docutils literal notranslate"><span class="pre">quartz_structure</span></code> above. The cctbx specific C++ code for
establishing these Python types is fairly minimal (about 470 lines for
exposing 6 types, including full pickle support and all copyright
statements). This approach can therefore be easily adopted for
user-defined types in other modules.</p>
</section>
<section id="a-balancing-act">
<h2><a class="toc-backref" href="#id6" role="doc-backlink">A balancing act</a><a class="headerlink" href="#a-balancing-act" title="Link to this heading">¶</a></h2>
<p>Python’s <strong>convenience of use</strong> is directly related to the way the
Python type system works: all type information is evaluated at runtime.
For example consider this trivial function:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">plus</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
</pre></div>
</div>
<p>It works instantly for many different argument types:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">plus</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c1"># integer values</span>
<span class="go">3</span>
<span class="gp">>>> </span><span class="n">plus</span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="mi">2</span><span class="n">j</span><span class="p">,</span> <span class="mi">2</span><span class="o">+</span><span class="mi">3</span><span class="n">j</span><span class="p">)</span> <span class="c1"># complex values</span>
<span class="go">(3+5j)</span>
<span class="gp">>>> </span><span class="n">plus</span><span class="p">([</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">],</span> <span class="p">[</span><span class="s1">'c'</span><span class="p">,</span> <span class="s1">'d'</span><span class="p">])</span> <span class="c1"># lists</span>
<span class="go">['a', 'b', 'c', 'd']</span>
</pre></div>
</div>
<p>It works because the meaning of <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">+</span> <span class="pre">b</span></code> is determined at runtime
based on the actual types of <code class="docutils literal notranslate"><span class="pre">a</span></code> and <code class="docutils literal notranslate"><span class="pre">b</span></code>.</p>
<p>The <strong>runtime efficiency</strong> of C++ code is directly related to the way
the C++ type system works: type information is usually evaluated at
compile-time (virtual functions are an exception which we will not
consider here). Fortunately C++ has a very powerful mechanism that
helps us avoid explicitly coding polymorphic functions over and over
again:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">template</span> <span class="o"><</span><span class="n">typename</span> <span class="n">T</span><span class="o">></span>
<span class="n">T</span> <span class="n">plus</span><span class="p">(</span><span class="n">T</span> <span class="n">const</span><span class="o">&</span> <span class="n">a</span><span class="p">,</span> <span class="n">T</span> <span class="n">const</span><span class="o">&</span> <span class="n">b</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>This template function is automatically <em>instantiated</em> for a given type
<code class="docutils literal notranslate"><span class="pre">T</span></code> as used:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">int</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nb">int</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
<span class="nb">int</span> <span class="n">c</span> <span class="o">=</span> <span class="n">plus</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">);</span> <span class="o">//</span> <span class="n">implicitly</span> <span class="n">instantiates</span> <span class="n">plus</span> <span class="k">with</span> <span class="n">T</span><span class="o">==</span><span class="nb">int</span>
</pre></div>
</div>
<p>Given a system that is based on both Python and C++ we have the freedom
of choosing the quick-and-easy runtime polymorphism offered by Python,
or the more efficient compile-time polymorphism offered by C++.</p>
<p>An important consideration in deciding which solution is the most
appropriate for a given problem is that a polymorphic Python function
requires very little memory at runtime. In contrast, each new
instantiation of a template function eventually results in a complete
copy of the corresponding machine language instructions tailored for
the specific types involved. This point may seem subtle at first, but
being overly generous with the use of C++ compile-time polymorphism can
lead to very large executable sizes and excessive compile times.</p>
<p>A comparison of the <code class="docutils literal notranslate"><span class="pre">plus</span></code> Python function and its C++ counterpart
shows that the notational overhead of the C++ syntax can easily double
the size of the source code. Therefore a programmer, given the choice,
will naturally lean towards the Python solution until the runtime
penalty due to the dynamic typing is prohibitive for a given
application. However, when putting a dynamically typed system and a
statically typed system together there are situations where it is
important to carefully consider the best balance.</p>
</section>
<section id="hybrid-systems">
<h2><a class="toc-backref" href="#id7" role="doc-backlink">Hybrid systems</a><a class="headerlink" href="#hybrid-systems" title="Link to this heading">¶</a></h2>
<p>Considerations of the type discussed in the previous section directly
lead to the following situation:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">flex</span><span class="o">.</span><span class="n">int</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">flex</span><span class="o">.</span><span class="n">double</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">a</span> <span class="o">*</span> <span class="n">b</span>
<span class="go">TypeError: unsupported operand type(s) for *:</span>
<span class="go">'scitbx_boost.array_family.flex_scitbx_ext.int' and</span>
<span class="go">'scitbx_boost.array_family.flex_scitbx_ext.double'</span>
</pre></div>
</div>
<p>In passing we note that there is a simple solution which will produce
the desired result:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span><span class="o">.</span><span class="n">as_double</span><span class="p">()</span> <span class="o">*</span> <span class="n">b</span>
</pre></div>
</div>
<p>However, for the purpose of this discussion let’s pretend that this
solution does not exist. Of course the first question is: what is
the reason for the apparently stupid limitation?</p>
<p>As mentioned before, the Python <code class="docutils literal notranslate"><span class="pre">flex</span></code> types are implemented as
instantiations of C++ class templates. This ensures that all array
operations are very fast. However, from the discussion in the previous
section it follows that exposing the full class with its many member
functions to Python <strong>for each element type</strong> (<code class="docutils literal notranslate"><span class="pre">int</span></code>, <code class="docutils literal notranslate"><span class="pre">double</span></code>,
<code class="docutils literal notranslate"><span class="pre">miller::index<></span></code>, etc.) creates very sizable object files. If only
<em>homogeneous</em> operators (<code class="docutils literal notranslate"><span class="pre">int</span> <span class="pre">*</span> <span class="pre">int</span></code>, <code class="docutils literal notranslate"><span class="pre">double</span> <span class="pre">*</span> <span class="pre">double</span></code>, etc.) are
used the combined size of the object files scales linearly with the
number of element types involved. However, if the library is expanded
to support <em>heterogeneous</em> operators (<code class="docutils literal notranslate"><span class="pre">int</span> <span class="pre">*</span> <span class="pre">double</span></code>, <code class="docutils literal notranslate"><span class="pre">double</span> <span class="pre">*</span>
<span class="pre">int</span></code>, etc.) the combined object files grow proportional to the square
of the number of array element types involved! With current technology
this is simply prohibitive.</p>
<p>Limitations of the kind discussed here will apply to any hybrid
dynamically/statically typed system. In the broader picture the
limitation shown above is just one typical example. If we want to enjoy
the many benefits of using Python <em>and</em> have a system that produces
results with a reasonable runtime efficiency we have to adopt the
approach of sparsely sampling the space of possible C++ template
instantiations. For this idea to work in practice we need a powerful
and easy to use language-integration tool as discussed in the next
section.</p>
</section>
<section id="building-bridges">
<h2><a class="toc-backref" href="#id8" role="doc-backlink">Building bridges</a><a class="headerlink" href="#building-bridges" title="Link to this heading">¶</a></h2>
<p>The cctbx project has evolved together with the <a class="reference external" href="http://www.boost.org/libs/python/doc/">Boost.Python</a> library.
All Python/C++ bindings in the cctbx project are implemented using this
library. Here is a simplified example of how it works in practice:</p>
<p>This is the C++ class that we want to use from Python:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>//! Parallelepiped that contains an asymmetric unit.
class brick
{
public:
//! Constructor.
/*! Determines the parallelepiped given a space group type.
*/
explicit
brick(space_group_type const& sg_type);
//! Formats the information about the brick as a string.
/*! Example: 0<=x<=1/8; -1/8<=y<=0; 1/8<z<7/8
*/
std::string as_string() const;
//! Tests if a given point is inside the brick.
bool is_inside(tr_vec const& p) const;
};
</pre></div>
</div>
<p>These are the corresponding Boost.Python bindings:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">class_</span><span class="o"><</span><span class="n">brick</span><span class="o">></span><span class="p">(</span><span class="s2">"brick"</span><span class="p">,</span> <span class="n">no_init</span><span class="p">)</span>
<span class="o">.</span><span class="n">def</span><span class="p">(</span><span class="n">init</span><span class="o"><</span><span class="n">space_group_type</span> <span class="n">const</span><span class="o">&></span><span class="p">())</span>
<span class="o">.</span><span class="n">def</span><span class="p">(</span><span class="s2">"__str__"</span><span class="p">,</span> <span class="o">&</span><span class="n">brick</span><span class="p">::</span><span class="n">as_string</span><span class="p">)</span>
<span class="o">.</span><span class="n">def</span><span class="p">(</span><span class="s2">"is_inside"</span><span class="p">,</span> <span class="o">&</span><span class="n">brick</span><span class="p">::</span><span class="n">is_inside</span><span class="p">)</span>
<span class="p">;</span>
</pre></div>
</div>
<p>And here is how the class is used in Python:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">cctbx</span> <span class="kn">import</span> <span class="n">sgtbx</span>
<span class="gp">>>> </span><span class="n">brick</span> <span class="o">=</span> <span class="n">sgtbx</span><span class="o">.</span><span class="n">brick</span><span class="p">(</span><span class="n">sgtbx</span><span class="o">.</span><span class="n">space_group_type</span><span class="p">(</span><span class="s2">"I a -3 d"</span><span class="p">))</span>
<span class="gp">>>> </span><span class="nb">print</span> <span class="n">brick</span>
<span class="go">0<=x<=1/8; -1/8<=y<=0; 1/8<z<7/8</span>
<span class="gp">>>> </span><span class="n">brick</span><span class="o">.</span><span class="n">is_inside</span><span class="p">(</span><span class="n">sgtbx</span><span class="o">.</span><span class="n">tr_vec</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">)))</span>
<span class="go">0</span>
</pre></div>
</div>
<p>Typically it only takes a few minutes to implement the Python bindings
for a new class or function. Since it usually takes orders of
magnitudes longer to implement C++ classes and functions the extra time
spent on the Python bindings is in general negligible.</p>
</section>
<section id="thinking-hybrid">
<h2><a class="toc-backref" href="#id9" role="doc-backlink">Thinking hybrid</a><a class="headerlink" href="#thinking-hybrid" title="Link to this heading">¶</a></h2>
<p>Boost.Python’s ease of use enables us to <em>think hybrid</em> when developing
new algorithms. We can conveniently start with a Python
implementation. The rich set of precompiled tools included in the
scitbx and the cctbx gives us a head start because many operations are
already carried out at C++ speed even though we are only using Python
to assemble the new functionality. If necessary, the working procedure
can be used to discover the rate-limiting sub-algorithms. To maximize
performance these can be reimplemented in C++, together with the Python
bindings needed to tie them back into the existing higher-level
procedure.</p>
<p>To give an example, this approach was used in the development of the
<em>Euclidean model matching algorithm</em> found in the cctbx
(<code class="docutils literal notranslate"><span class="pre">cctbx/euclidean_model_matching.py</span></code>). This algorithm is used to
compare heavy-atom substructures or anomalously scattering
substructures from isomorphous replacement or anomalous diffraction
experiments. The algorithm was first implemented in about 300 lines of
pure Python. We wrote another 200 lines of comprehensive regression
tests for thoroughly debugging the implementation. For a while the pure
Python code actually worked fast enough for us, until we started to
work with a very large substructure with 66 anomalous scatterers. Some
simple optimizations of the Python implementation resulted only in a
modest speedup, but after replacing about 30 lines of Python with a
C++ implementation the algorithm runs about 50 times faster.</p>
<p>Of course there is still more to gain by reimplementing the entire
algorithm in C++. However, one has to keep in mind that developing C++
code is typically much more time-consuming than developing in Python.
For example, the 30 lines of Python mentioned in the previous paragraph
turned into more then 100 lines of C++, not counting the additional 13
lines for the Boost.Python bindings. It is also important to keep in
mind that developing maintainable C++ code requires much more
hard-earned working experience than developing useful Python code. C++
has many pitfalls that one must learn to avoid. In contrast the Python
language is structured in a way that steers even the novice programmer
onto safe routes. In fact, Python was originally conceived as a
language for teaching programming. Amazingly this heritage is still
preserved even though Python has grown to be a very richly featured
language.</p>
<p>Looking back, the cctbx started out mainly as a library of C++ classes
with ‘C’ heritage, and for a while the growth was mainly concentrated
on the C++ parts. However, a very important difference between the 1.0
release and the upcoming 2.0 release is that the Python parts now
constitute a much more significant fraction of the total sources. We
expect this trend to continue, as illustrated qualitatively in this
figure:</p>
<img alt="_images/python_cpp_mix.png" src="_images/python_cpp_mix.png" />
<p>This figure shows the ratio of newly added C++ and Python code over
time as new applications are implemented. We expect this ratio to level
out near 70% Python. From an inside viewpoint the increasing ability to
solve new problems mostly with the easy-to-use Python language rather
than a necessarily more arcane statically typed language is the return
on a major investment, namely our involvement in the development of
Boost.Python. From an outside viewpoint we hope that the ability to
solve some problems entirely using only Python will enable a larger
group of scientist to participate in the rapid development of new
algorithms. It is also important to notice that Python is an ideal
language for integrating diverse existing tools, no matter which
language they are written in. If portability is not an issue this can
be a great solution to some problems. We are convinced that the cctbx
can be very useful as an intelligent mediator between otherwise
incompatible crystallographic applications.</p>
<p><a class="reference internal" href="index.html#introduction"><span class="std std-ref">Back</span></a></p>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<div>
<h3><a href="index.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Tour of the cctbx</a><ul>
<li><a class="reference internal" href="#the-beach-in-the-box">The beach in the box</a></li>
<li><a class="reference internal" href="#at-the-very-bottom">At the very bottom</a></li>
<li><a class="reference internal" href="#scitbx-array-family-flex"><code class="docutils literal notranslate"><span class="pre">scitbx.array_family.flex</span></code></a></li>
<li><a class="reference internal" href="#cctbx-array-family-flex"><code class="docutils literal notranslate"><span class="pre">cctbx.array_family.flex</span></code></a></li>
<li><a class="reference internal" href="#a-balancing-act">A balancing act</a></li>
<li><a class="reference internal" href="#hybrid-systems">Hybrid systems</a></li>
<li><a class="reference internal" href="#building-bridges">Building bridges</a></li>
<li><a class="reference internal" href="#thinking-hybrid">Thinking hybrid</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Previous topic</h4>
<p class="topless"><a href="installation.html"
title="previous chapter">Installation overview</a></p>
</div>
<div>
<h4>Next topic</h4>
<p class="topless"><a href="history.html"
title="next chapter">CCTBX development history</a></p>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/tour.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<search id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="history.html" title="CCTBX development history"
>next</a> |</li>
<li class="right" >
<a href="installation.html" title="Installation overview"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">CCTBX Developer documentation</a> »</li>
<li class="nav-item nav-item-this"><a href="">Tour of the cctbx</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2014, University of California.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.4.7.
</div>
</body>
</html>