# Path: pyem/star.py
# def parse_star(starfile, keep_index=False, augment=True, nrows=sys.maxsize):
#     tables = star_table_offsets(starfile)
#     dfs = {t: parse_star_table(starfile, offset=tables[t][0], nrows=min(tables[t][3], nrows), keep_index=keep_index)
#            for t in tables}
#     if Relion.OPTICDATA in dfs:
#         if Relion.PARTICLEDATA in dfs:
#             data_table = Relion.PARTICLEDATA
#         elif Relion.MICROGRAPHDATA in dfs:
#             data_table = Relion.MICROGRAPHDATA
#         elif Relion.IMAGEDATA in dfs:
#             data_table = Relion.IMAGEDATA
#         else:
#             data_table = None
#         if data_table is not None:
#             df = pd.merge(dfs[Relion.OPTICDATA], dfs[data_table], on=Relion.OPTICSGROUP)
#         else:
#             df = dfs[Relion.OPTICDATA]
#     else:
#         df = dfs[next(iter(dfs))]
#     df = check_defaults(df, inplace=True)
#     if augment:
#         augment_star_ucsf(df, inplace=True)
#     return df
# 
# Path: pyem/star.py
# def write_star(starfile, df, resort_fields=True, resort_records=False, simplify=True, optics=True):
#     if not starfile.endswith(".star"):
#         starfile += ".star"
#     if resort_records:
#         df = sort_records(df, inplace=True)
#     if simplify and len([c for c in df.columns if "ucsf" in c or "eman" in c]) > 0:
#         df = simplify_star_ucsf(df)
# 
#     if optics:
#         if Relion.OPTICSGROUP not in df:
#             df[Relion.OPTICSGROUP] = 1
#         gb = df.groupby(Relion.OPTICSGROUP)
#         df_optics = gb[df.columns.intersection(Relion.OPTICSGROUPTABLE)].first().reset_index(drop=False)
#         df = df.drop(columns=Relion.OPTICSGROUPTABLE, errors="ignore")
#         data_table = Relion.PARTICLEDATA if is_particle_star(df) else Relion.MICROGRAPHDATA
#         dfs = {Relion.OPTICDATA: df_optics, data_table: df}
#         write_star_tables(starfile, dfs, resort_fields=resort_fields)
#     else:
#         write_star_table(starfile, df, table=Relion.IMAGEDATA, resort_fields=resort_fields)


import numpy as np
import sys
    import argparse
from pyem.star import parse_star
from pyem.star import write_star
            print("No CTF resolution field found in input")
            return 1
        df = df.loc[ind]

    if args.max_ctf_fom is not None:
        ind = df["rlnCtfFigureOfMerit"] <= args.max_ctf_fom
        df = df.loc[ind]

    if args.min_ctf_fom is not None:
        ind = df["rlnCtfFigureOfMerit"] >= args.min_ctf_fom
        df = df.loc[ind]
    
    if args.min_particles is not None:
        counts = df["rlnMicrographName"].value_counts()
        subset = df.set_index("rlnMicrographName").loc[counts.index[counts > args.min_particles]]
        df = subset.reset_index()

    if args.subsample is not None:
        if args.subsample < 1:
            args.subsample = np.max(np.round(args.subsample * df.shape[0]), 1)
        if args.bootstrap is not None:
            print("Not implemented yet")
            return 1
            inds = np.random.choice(df.shape[0],
                                    size=(np.int(args.subsample),
                                    df.shape[0]/np.int(args.subsample)),
                                    replace=True)
        else:
            df = df.sample(np.int(args.subsample), random_state=args.seed)


# Path: pyem/star.py
# def parse_star(starfile, keep_index=False, augment=True, nrows=sys.maxsize):
#     tables = star_table_offsets(starfile)
#     dfs = {t: parse_star_table(starfile, offset=tables[t][0], nrows=min(tables[t][3], nrows), keep_index=keep_index)
#            for t in tables}
#     if Relion.OPTICDATA in dfs:
#         if Relion.PARTICLEDATA in dfs:
#             data_table = Relion.PARTICLEDATA
#         elif Relion.MICROGRAPHDATA in dfs:
#             data_table = Relion.MICROGRAPHDATA
#         elif Relion.IMAGEDATA in dfs:
#             data_table = Relion.IMAGEDATA
#         else:
#             data_table = None
#         if data_table is not None:
#             df = pd.merge(dfs[Relion.OPTICDATA], dfs[data_table], on=Relion.OPTICSGROUP)
#         else:
#             df = dfs[Relion.OPTICDATA]
#     else:
#         df = dfs[next(iter(dfs))]
#     df = check_defaults(df, inplace=True)
#     if augment:
#         augment_star_ucsf(df, inplace=True)
#     return df
# 
# Path: pyem/star.py
# def write_star(starfile, df, resort_fields=True, resort_records=False, simplify=True, optics=True):
#     if not starfile.endswith(".star"):
#         starfile += ".star"
#     if resort_records:
#         df = sort_records(df, inplace=True)
#     if simplify and len([c for c in df.columns if "ucsf" in c or "eman" in c]) > 0:
#         df = simplify_star_ucsf(df)
# 
#     if optics:
#         if Relion.OPTICSGROUP not in df:
#             df[Relion.OPTICSGROUP] = 1
#         gb = df.groupby(Relion.OPTICSGROUP)
#         df_optics = gb[df.columns.intersection(Relion.OPTICSGROUPTABLE)].first().reset_index(drop=False)
#         df = df.drop(columns=Relion.OPTICSGROUPTABLE, errors="ignore")
#         data_table = Relion.PARTICLEDATA if is_particle_star(df) else Relion.MICROGRAPHDATA
#         dfs = {Relion.OPTICDATA: df_optics, data_table: df}
#         write_star_tables(starfile, dfs, resort_fields=resort_fields)
#     else:
#         write_star_table(starfile, df, table=Relion.IMAGEDATA, resort_fields=resort_fields)


# Path: subset.py
import numpy as np
import sys
    import argparse
from pyem.star import parse_star
from pyem.star import write_star

            print("No CTF resolution field found in input")
            return 1
        df = df.loc[ind]

    if args.max_ctf_fom is not None:
        ind = df["rlnCtfFigureOfMerit"] <= args.max_ctf_fom
        df = df.loc[ind]

    if args.min_ctf_fom is not None:
        ind = df["rlnCtfFigureOfMerit"] >= args.min_ctf_fom
        df = df.loc[ind]
    
    if args.min_particles is not None:
        counts = df["rlnMicrographName"].value_counts()
        subset = df.set_index("rlnMicrographName").loc[counts.index[counts > args.min_particles]]
        df = subset.reset_index()

    if args.subsample is not None:
        if args.subsample < 1:
            args.subsample = np.max(np.round(args.subsample * df.shape[0]), 1)
        if args.bootstrap is not None:
            print("Not implemented yet")
            return 1
            inds = np.random.choice(df.shape[0],
                                    size=(np.int(args.subsample),
                                    df.shape[0]/np.int(args.subsample)),
                                    replace=True)
        else:
            df = df.sample(np.int(args.subsample), random_state=args.seed)


    write_star(args.output, df)