Skip to main content

A simple package that allows creating a pickles dataset and fast random access

Project description

RA Pickles

In many applications, elements from a large dataset, that cannot be stored in memory, need to be sampled (with or without replacement). This simple package simplifies this process by allowing storage of large datasets by splitting them over multiple files in a directory. Once that's done, any dataset element can be queried. Further, most python objects as dataset elements can be used as pickles are used at the backend.

As mentioned before, a dataset will be represented by a directory that contains two sets of files, meta and data. The file identifiers are randomly generated and therefore, two datasets can be added together to create a larger dataset without additional work. This also allows multiple applications to add elements to the dataset in parallel. An example is multiple simulations running in parallel.

Installation

pip3 install ra-pickles

Example

First import all the packages

from ra_pickles import RandomAccessPicklesReader, RandomAccessPicklesWriter
import shutil
import os
import numpy as np

So to create a dataset in test_folder, use the following code. 100 here specifies number of dataset samples in each file. If there are more than 100 files, multiple files will be created. Heuristically, this number can be chosen such that resulting data files are approximately 1 GB, although this wouldn't affect performance much as a complete file is not fully loaded into memory. On the other hand, a too small number can also affect performance as some file systems create issues if there are too many files.

fold = 'test_folder'
writer = RandomAccessPicklesWriter(100, fold)

Now entries to the dataset can be added as follows:

for i in range(30):
    # d is the dataset element which can be any python object
    d = np.ones(4) * i
    
    # and you add it to the dataset
    writer.add(d)
writer.close()

Here, 30 numpy arrays have been added.

To sample elements in the dataset, use the following code:

reader = RandomAccessPicklesReader(fold)
print("Total", reader.get_total())

print("Trying to retreive")
for i in range(30):
    print("Retrieving",i)
    a = reader.get_element(i)
    print(i, a[0])

The index retrieval index can be randomly generated --[0, reader.get_total() ) to sample random elements.

Multiple elements can also be retrieved in parallel for fast access. To do so, first, retrieval threads must be started (and closed at the end):

reader.start_parallel_retrieval_threads(n_theads=5)
data = reader.get_multi_in_parallel([1,2,3])
reader.close_parallel_retrieval_threads()
reader.close()

Good luck!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ra_pickles-0.9.1.5.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ra_pickles-0.9.1.5-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file ra_pickles-0.9.1.5.tar.gz.

File metadata

  • Download URL: ra_pickles-0.9.1.5.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.12

File hashes

Hashes for ra_pickles-0.9.1.5.tar.gz
Algorithm Hash digest
SHA256 84da9e72a239563a2fd4032a06451ae0fb61fb4ee64595dade4c9aadf23ea3a8
MD5 49a0f9f77eea79c93b863b7cdd12644e
BLAKE2b-256 fa06b6f41732b3dab4faa6b6ac56a9c6cb3c31626c268cb9312449da0542c9e0

See more details on using hashes here.

File details

Details for the file ra_pickles-0.9.1.5-py3-none-any.whl.

File metadata

  • Download URL: ra_pickles-0.9.1.5-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.12

File hashes

Hashes for ra_pickles-0.9.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 13caa5df9d94f87fc699bbfd741f834517ad3efdb274c639d6d5f132d9b25ba6
MD5 7466284caf51b79a11ae71742145817d
BLAKE2b-256 09265bb538cc0887a05a17555b9e84e49beeb42a8c568ce574535f2b17390ec2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page