Skip to content
Advertisement

python: what are efficient techniques to deal with deeply nested data in a flexible manner?

My question is not about a specific code snippet but more general, so please bear with me:

How should I organize the data I’m analyzing, and which tools should I use to manage it?

I’m using python and numpy to analyse data. Because the python documentation indicates that dictionaries are very optimized in python, and also due to the fact that the data itself is very structured, I stored it in a deeply nested dictionary.

Here is a skeleton of the dictionary: the position in the hierarchy defines the nature of the element, and each new line defines the contents of a key in the precedent level:

JavaScript

Edit: To explain a bit better my data set:

JavaScript

The type of operations I perform is for instance to compute properties of the arrays (listed under Ch1, Ch2), pick up arrays to make a new collection, for instance analyze responses of N01 from region 16 (R16) of a given individual at different time points, etc.

This structure works well for me and is very fast, as promised. I can analyze the full data set pretty quickly (and the dictionary is far too small to fill up my computer’s ram : half a gig).

My problem comes from the cumbersome manner in which I need to program the operations of the dictionary. I often have stretches of code that go like this:

JavaScript

which is ugly, cumbersome, non reusable, and brittle (need to recode it for any variant of the dictionary).

I tried using recursive functions, but apart from the simplest applications, I ran into some very nasty bugs and bizarre behaviors that caused a big waste of time (it does not help that I don’t manage to debug with pdb in ipython when I’m dealing with deeply nested recursive functions). In the end the only recursive function I use regularly is the following:

JavaScript

I know I’m doing this wrong, because my code is long, noodly and non-reusable. I need to either use better techniques to flexibly manipulate the dictionaries, or to put the data in some database format (sqlite?). My problem is that since I’m (badly) self-taught in regards to programming, I lack practical experience and background knowledge to appreciate the options available. I’m ready to learn new tools (SQL, object oriented programming), whatever it takes to get the job done, but I am reluctant to invest my time and efforts into something that will be a dead end for my needs.

So what are your suggestions to tackle this issue, and be able to code my tools in a more brief, flexible and re-usable manner?

Addendum: apart of doing something with a particular sub-dictionary of the data dictionary, here are some examples of operations I implemented for the dataset dic, or a sub dictionary of it:

actually I have some recursive functions that worked well:

JavaScript

for some operations I found no other way than to flatten the dictionary:

JavaScript

Advertisement

Answer

“I stored it in a deeply nested dictionary”

And, as you’ve seen, it doesn’t work out well.

What’s the alternative?

  1. Composite keys and a shallow dictionary. You have an 8-part key: ( individual, imaging session, Region imaged, timestamp of file, properties of file, regions of interest in image, format of data, channel of acquisition ) which maps to an array of values.

    JavaScript

    The issue with this is search.

  2. Proper class structures. Actually, a full-up Class definition may be overkill.

“The type of operations I perform is for instance to compute properties of the arrays (listed under Ch1, Ch2), pick up arrays to make a new collection, for instance analyze responses of N01 from region 16 (R16) of a given individual at different time points, etc.”

Recommendation

First, use a namedtuple for your ultimate object.

JavaScript

Or something like that. Build a simple list of these named tuple objects. You can then simply iterate over them.

Second, use many simple map-reduce operations on this master list of the array objects.

Filtering:

JavaScript

Reducing by Common Key:

JavaScript

This will create a subset in the map that has exactly the items you want.

You can then do indiidual_dict[‘AS091209M02’] and have all of their data. You can do this for any (or all) of the available keys.

JavaScript

This does not copy any data. It’s fast and relatively compact in memory.

Mapping (or transforming) the array:

JavaScript

If the array is itself a list, you’re can update that list without breaking the tuple as a whole. If you need to create a new array from an existing array, you’re creating a new tuple. There’s nothing wrong with this, but it is a new tuple. You wind up with programs like this.

JavaScript

You can build up transformations, reductions, mappings into more elaborate things.

The most important thing is creating only the dictionaries you need from the master list so you don’t do any more filtering than is minimally necessary.

BTW. This can be mapped to a relational database trivially. It will be slower, but you can have multiple concurrent update operations. Except for multiple concurrent updates, a relational database doesn’t offer any features above this.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement