PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted)...

38
PyTables An ondisk binary data container Francesc Alted May 9 th 2012, Aus=n Python meetup

Transcript of PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted)...

Page 1: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

PyTables  

An  on-­‐disk  binary  data  container  

Francesc  Alted  

May  9th  2012,  Aus=n  Python  meetup  

Page 2: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Overview  

•  What  PyTables  is?  

•  Data  structures  in  PyTables  

•  The  one  million  song  dataset  

•  Advanced  capabili=es  in  PyTables  

Page 3: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

What  it  is  •  A  binary  data  container  for  on-­‐disk,  structured  data  

•  Based  on  the  standard  de-­‐facto  HDF5  format  •  Free  soKware  (BSD  license)  •  Dis=nc=ve  capabili=es:  – NumPy  way  to  select  data  – Data  can  be  compressed  using  many  different  compressors  (and  filters)  

– Out-­‐of-­‐core  calcula=ons  –  Powerful  search  in  Table  objects  (including  column  indexing)  

Page 4: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

What  it  is  not  

•  Not  a  rela=onal  database  replacement  

•  Not  a  distributed  database  

•  Not  extremely  secure  or  safe  

•  Not  a  mere  HDF5  wrapper  

Page 5: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Design  goals  •  Allow  to  structure  your  data  in  a  hierarchical  way.  •  Easy  to  use.  It  implements  the  Natural  Naming  scheme  for  allowing  convenient  access  to  the  data.  

•  All  the  cells  in  datasets  can  be  mul1dimensional  en==es.  

•  Most  of  the  I/O  opera=ons  speed  should  be  only  limited  by  the  underlying  I/O  subsystem,  be  it  disk  or  memory.  

•  Enable  the  end  user  to  save  and  deal  with  large  datasets  with  minimum  overhead,  i.e.  each  single  byte  of  data  on  disk  has  to  be  represented  by  one  byte  plus  a  small  frac=on  when  loaded  into  memory.  

Page 6: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

About  HDF5  (Hierarchical  Data  File  version  5)  

•  A  versa1le  data  model  that  can  represent  very  complex  data  objects  and  a  wide  variety  of  metadata.  

•  A  completely  portable  file  format  with  no  limit  on  the  number  or  size  of  data  objects  in  the  collec=on.  

•  Implements  a  high-­‐level  API  with  C,  C++,  Fortran  90,  and  Java  interfaces.  

•  A  rich  set  of  integrated  performance  features  that  allow  for  access  1me  and  storage  space  op1miza1ons.  

•  Free  soKware  (BSD,  MIT  kind  of  license).    

Page 7: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

LEVERAGING  NUMPY  

Page 8: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Easing  disk  access  via  NumPy  paradigm  

•  Retrieving  a  data  set  por=on  – array[1]  – array[2:3,2:100:2,  …,  :10]  – array[[3,10,30,1000]]  – array[array2  >  0]    

•  Out  of  core  opera=ons  –  (array1**3  /  array2)  -­‐    sin(array3)  

You  don’t  need  to  learn  other  paradigms!  

Page 9: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Using  NumPy  as  memory  container  

vs  

Page 10: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

DATA  STRUCTURES  

Page 11: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Data  structures  

•  High  level  of  flexibility  for  structuring  your  data:  –   Datatypes:  scalars  (numerical  &  strings),  records,  enumerated,  =me…  

– Tables  support  mul=dimensional  cells  and  nested  records  

– Mu=dimensional  arrays  – Variable  length  arrays  

Page 12: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

The  Array  object  

•  Easy  to  create:  –  file.createArray(mygroup,  ‘array’,  numpy_arr)  

•  Shape  cannot  change  •  Cannot  be  compressed  

Page 13: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

The  CArray  object  

•  Data  is  stored  in  chunks  •  Each  chunk  can  be  compressed  independently  

•  Shape  cannot  change  

Page 14: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

The  EArray  object  

•  Data  is  stored  in  chunks  •  Can  be  compressed  •  Shape  can  change  (either  enlarged  or  shrunk)  •  Shape  must  be  kept  regular  

 

Page 15: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

The  VLArray  object  

•  Data  is  stored  in  variable  length  rows  •  Can  be  enlarged  or  shrunk  

•  Data  cannot  be  compressed    

Page 16: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

The  Table  object  

•  Data  is  stored  in  chunks  •  Can  be  compressed  •  Can  be  enlarged  or  shrunk  •  Fields  cannot  be  of  variable  length  

 

Col1  (int32)  

Col2  (string  10)  

Col3  (bool)  

Col4  (complex64)  

Col5  (float32)  

Page 17: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Anributes:    Metadata  about  data  

table1   Date:  Jul  24  2006  

Observa=ons:  555  

CF:  [0.1,  0.3,  0.6]  

Page 18: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

 Dataset  hierarchy  

root  

group1  

table1   table2  

group2  

array  

Page 19: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

INTERACTIVE  SESSION  

Page 20: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

The  1  million  song  dataset  

•  The  Million  Song  Dataset  is  a  freely-­‐available  collec=on  of  audio  features  and  metadata  for  a  million  contemporary  popular  music  tracks  

•  300  GB  !  •  Created  using  PyTables  

hnp://labrosa.ee.columbia.edu/millionsong/  

Page 21: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

PyTables  dis=nc=ve  features  

•  Supports  a  range  of  compressors:  zlib,  bzip2,  lzo  and  blosc  

•  Can  do  out-­‐of-­‐core  opera=ons  

•  Powerful  search  capabili=es  for  Table  objects,  including  column  indexing  

Page 22: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

COMPRESSION  CAPABILITIES  

Page 23: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Why  compression?  

•  Lets  you  store  more  data  using  the  same  space  

•  Uses  more  CPU,  but  CPU  =me  is  cheap  compared  with  disk  access  

•  Different  compressors  for  different  uses:  bzip2,  zlib,  lzo,  blosc  

 

Page 24: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Why  Blosc?  

Page 25: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

OS  memory  buffers    

Page 26: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Memory  access  vs  CPU  cycle  =me  

Page 27: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Laptop  computer  back  in  2005  

Page 28: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

State  of  the  art  computer  in  2012  (single  node)  

Page 29: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

OUT-­‐OF-­‐CORE  OPERATIONS  

Page 30: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Opera=ng  with  disk-­‐based  arrays  

•  tables.Expr  is  an  op=mized  evaluator  for  expressions  of  disk-­‐based  arrays.    

•  It  is  a  combina=on  of  the  Numexpr  advanced  compu=ng  capabili=es  with  the  high  I/O  performance  of  PyTables.    

•  Similarly  to  Numexpr,  disk-­‐temporaries  are  avoided,  and  mul=-­‐threaded  opera=on  is  preserved.  

Page 31: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Avoiding  temporaries  with  Numexpr  

Tables.Expr  follows  the  same  approach,    but  with  disk  and  memory  instead  

Page 32: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Tables.Expr  in  ac=on  

•  Evalua=ng  .25*x**3  +  .75*x**2  -­‐  1.5*x  -­‐  2     import tables as tb f = tb.openFile(h5fname, "a") x = f.root.x # get the x input r = f.createCArray(f.root, "r", atom=x.atom, shape=x.shape) ex = tb.Expr(’.25*x**3 + .75*x**2 - 1.5*x - 2’) ex.setOutput(r) # output will got to the CArray on disk ex.eval() # evaluate! f.close()

Page 33: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Example  of  out-­‐of-­‐core  opera=on  

Page 34: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

ADVANCED  QUERY  CAPABILITIES  

Page 35: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Different  query  modes  

Regular  query: •  [ r[‘c1’] for r in table

if r[‘c2’] > 2.1 and r[‘c3’] == True)) ]

In-­‐kernel  query: •  [ r[‘c1’] for r in table.where(‘(c2>2.1)&(c3==True)’) ]

Indexed  query: •  table.cols.c2.createIndex() •  table.cols.c3.createIndex() •  [ r[‘c1’] for r in table.where(‘(c2>2.1)&(c3==True)’) ]

Page 36: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Regular  and  in-­‐kernel  queries  

Page 37: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Customizable  indexes  

Page 38: PyTables) · 2020. 1. 21. · PyTables) An)on-disk)binary)datacontainer) Francesc)Alted) May)9th)2012,)Aus=n)Python)meetup)

Indexed  query  performance  The Starving CPU ProblemHigh Performance Libraries

Why Should You Use Them?In-Core High Performance LibrariesOut-of-Core High Performance Libraries

PyTables Pro Query Performance

Francesc Alted Large Data Analysis with Python