Skip to content

Working with Crunch Cubes from scrunch

slobodan-ilic edited this page Oct 11, 2018 · 1 revision

The following snippets in python will demonstrate how to do basic analyses by combining the power of scrunch client + CrunchCube library.

scrunch

scrunch is a Crunch.io client for working with Crunch.io platform from Python. Please refer to its GitHub page, for setting it up.

Connecting to Crunch.io

Once you have scrunch and cr.cube installed (cr.cube should install as scrunch dependency), you can connect to Crunch.io:

import scrunch as sc
sc.connect('[email protected]', '****', site_url='https://alpha.crunch.io/api/')

and access different datasets:

ds_name = 'ECONOMIST - 20171125 Draft'
ds = sc.get_dataset(ds_name)

Once you have a dataset handy, you can create analyses, leveraging the power of crtabs, a method that creates CrunchCube representation of the cube response JSON:

from scrunch.cubes import crtabs

You can create a cube by combining variable names:

variable_names = [
    'Direction of country',
    'Friend or enemy',
]
cube = crtabs(dataset, variable_names)

This returns the CrunchCube representation of the CAT x CAT analysis, that you referenced with variable names. You can then request different analytic measurements from the obtained cube object:

cube.as_array()
cube.proportions(axis=2)

The cube supports basic console formatting for it's basic measure (which is unweighted counts). So when you print it, you get the nice formatted output:

print cube
Clone this wiki locally