# Densify() operation

**URL:** <https://discuss.hail.is/t/densify-operation/3098>\
**Category:** Hail Query & hailctl\
**Created:** [February 1, 2023, 9:26pm UTC](https://discuss.hail.is/t/densify-operation/3098 "2023-02-01T21:26:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![arm](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@arm](https://discuss.hail.is/u/arm)\
**Post date:** [February 1, 2023, 9:26pm UTC](https://discuss.hail.is/t/densify-operation/3098/1 "2023-02-01T21:26:54Z")

</div>

Hello Hail team,

I am trying to understand the best way to work with my sparse matrix table.

My understanding is that before performing any filters, I should use hl.experimental.densify() to convert from a sparse mt to a dense mt. When I do this, however, I go from ~1100 steps to ~69,000 steps for any count, write, and show operations. Because of this a single count() or write() will take hours to calculate.

What can I do to make this more efficient?

Thanks!

An example script might look like:  
import hail as hl

import argparse

# Arguements

parser = argparse.ArgumentParser()

parser.add\_argument(“-f”, “–full\_run”, action=“store\_true”, help=“Runs on chr22 and chrX only by default. If full\_run is set, it runs on the whole matrix. WARNING: This will be VERY expensive”)

parser.add\_argument(“-w”, “–overwrite”, action=‘store\_true’, help=“If set will overwrite output matrix if it already exists”)

requiredNamed = parser.add\_argument\_group(‘required named arguments’)

requiredNamed.add\_argument(“-i”, “–input\_mt\_path”, required=True)

requiredNamed.add\_argument(“-o”, “–output\_mt\_path”, required=True)

#requiredNamed.add\_argument(“-p”, “–requester\_pays\_project\_id”, help=“Project ID to bill to when accessing requester pays bucket, needed to access hail annotationDB”)

args = parser.parse\_args()

# Store Inputs

input\_mt\_path = args.input\_mt\_path

output\_mt\_path = args.output\_mt\_path

#requester\_pays\_project\_id = args.requester\_pays\_project\_id

# read mt

mt = hl.read\_matrix\_table(input\_mt\_path)

mt = hl.experimental.densify(mt)

# Save mt densified and filtered to CHR22/PPMI

mt.write(output\_mt\_path, overwrite=True)
