Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort dataframes generated by hypothesis when row tuples have different dtypes

I want to create dataframes where End is larger than Start.

This I do with:

from hypothesis.extra.pandas import columns, data_frames, column
import hypothesis.strategies as st

positions = st.integers(min_value=0, max_value=int(1e7))
strands = st.sampled_from("+ -".split())
data_frames(columns=columns(["Start", "End"], dtype=int),
            rows=st.tuples(positions, positions).map(sorted)).example()

which gives

     Start      End
0   589492  6620613
1  5990807  8083222
2   252458  8368032
3  1575938  5763895
4  4689113  9133040
5  7439297  8646668
6   838051  1886133

However, I want to add a third column, Strand to the data, as generated with the strategy above. Then this stops working:

data_frames(columns=columns(["Start", "End", "Strands"], dtype=int),
            rows=st.tuples(positions, positions, strands).map(sorted)).example()

It gives the error

TypeError: '<' not supported between instances of 'str' and 'int'

This is due to the tuple sorting of both ints and strs. How do I fix this?

I can ask hypothesis to generate a dataframe with pos, pos, strand_int where strand_int is either 0 or 1 and convert this to "-" or "+" in the test, but it feels icky.

like image 811
The Unfun Cat Avatar asked Jun 18 '26 20:06

The Unfun Cat


1 Answers

Best method

better_dfs_min = data_frames(index=range_indexes(min_size=better_df_minsize),
                             columns=[column("Chromosome", chromosomes_small),
                                      column("Start", elements=small_lengths),
                                      column("End", elements=small_lengths),
                                      column("Strand", strands)])


@st.composite()
def dfs_min(draw):
    df = draw(better_dfs_min)
    df.loc[:, "End"] += df.Start
    return df

@given(df=dfs_min())
def test_me(df):
    print(df)
    assert 0

First attempt:

from hypothesis.extra.pandas import columns, data_frames, column
import hypothesis.strategies as st

def mysort(tp):

    key = [-1, tp[1], tp[2], int(1e10)]

    return [x for _, x in sorted(zip(key, tp))]

positions = st.integers(min_value=0, max_value=int(1e7))
strands = st.sampled_from("+ -".split())
chromosomes = st.sampled_from(elements=["chr{}".format(str(e)) for e in list(range(23)) + "X Y M".split()])

data_frames(columns=columns(["Chromosome", "Start", "End", "Strand"], dtype=int), rows=st.tuples(chromosomes, positions, positions, strands).map(mysort)).example()

Result:

  Chromosome    Start      End Strand
0      chr13  5660600  6171569      -
1       chrY  3987154  5435816      +
2      chr11  4659655  4956997      +
3      chr14   239357  8566407      +
4       chr3  3200488  9337489      +
5       chr8   304886  1078020      +

There must be a better way to do it than implement your own sort... My sorting depends on the integers in Start and End being between 0 and int(1e10) - 1 which feels icky.

like image 103
The Unfun Cat Avatar answered Jun 27 '26 17:06

The Unfun Cat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!