<commit_before>
def simulate_density(mass_kg=40, bd_start=1000, n_bd=101, block_start=1,
        n_blocks=8):
    '''Produce a range of body densities given an initial mass and density'''
    import numpy
    import pandas

    # Range of body densities to test
    types = ['weight', 'float']
    bd_range = bd_start+numpy.arange(0, n_bd)
    bodydensities = numpy.tile(numpy.repeat(bd_range, n_blocks), len(types))

    block_range = block_start+numpy.arange(0, n_blocks)
    blocks = numpy.tile(numpy.tile(block_range, n_bd), len(types))

    types = numpy.repeat(types, n_bd*n_blocks)

    columns = ['type', 'dens_kgm3', 'n_blocks', 'rho_mod', 'delta_rho']
    df = pandas.DataFrame(index=range(len(bodydensities)), columns=columns)

    for i in range(len(df)):
        print(i, df.index[i])
        df.loc[df.index[i], 'type'] = types[i]
        df.loc[df.index[i], 'dens_kgm3'] = bodydensities[i]
        df.loc[df.index[i], 'n_blocks'] = blocks[i]
        rho_mod = calc_mod_density(mass_kg, bodydensities[i], blocks[i], t)
        df.loc[df.index[i], 'rho_mod'] = rho_mod
        df.loc[df.index[i], 'delta_ro'] = rho_mod - bodydensities[i]

    return df


def calc_mod_density(mass_kg, dens_kgm3, n_blocks, block_type):
    '''Calculate the modified density with attached blocks

    Args
    ----
    mass_kg: float
        mass of the seal (kg)
    dens_kgm3: float
        mass of the seal (kg/m^3)
    block_type: str
        Type of modification block for experiment (`weight` or `float`)
    n_blocks: int
        number of modifying blocks attached

    Returns
    -------
    total_dens: float
        combined density of seal and attached blocks

    Notes
    -----
    All blocks dimensions were manually measured and then drawn in `FreeCAD`
    and the volumes calculated using the `FCInfo` macro.

    FLOAT:
      Float blocks were made from syntactic, non-compressible foam. They were
      attached with a neoprene pouch but were not compensated by a lead strip.
      The effect of the pouch buoyancy was not measured by considered to be
      negligible.

      Float block dimensions (with and without sailcloth)
        L:150mm, W:40mm, H:30mm (41, 29)
        shaved 45deg on all edges, 5.5 mm from edge

      Volume float           - 0.000174510114214 m³
      Mass float             - 0.034 kg
      Mass float w/sailcloth - 0.042 kg

    WEIGHT AND NEUTRAL:
      Neutral and weight blocks were made from PE plastic with 2 16mm diameter
      holes drilled length-wise to a depth of 144mm. These holes were
      left empty for neutral blocks. For weight blocks, these holes were filled
      with 140mm long 16mm diameter copper pipe that were filled with lead.

      The buoyancy of the neoprene pouch with which they were attached was
      compensated by a thin strip of lead that allowed the pouch to be neutral
      at surface. As the neoprene compresses with depth, this strip should overcompensate
      to a small degree.

      PE block dimensions:
        L:150mm, W:41mm, H:30mm
        rounded on one width edge - 15mm from edge length-wise and height-wise
      Hole dimensions (2 holes):
        L:144mm length, D:16mm
      Weight rod dimensions (one rod per hole):
        L:140mm, D:15mm

      Mass block w/holes   - 0.118 kg
      Mass weight bar      - 0.260 kg
      Mass weight block    - 0.118 kg + 2(0.260 kg) = 0.638 kg
      Volume block w/holes - 0.00012016274768 m³
      Volume weight bar    - 0.00000176714586764 m³
      Volume weight+2bars  - 1.202e-4 + 2(1.767e-6) = 1.237e-4

      4 weight tubes = 2 weights
    '''

    # Modifier block attributes
    block_types = ['weight', 'float', 'neutral']
    mod_vol_m3 = {'weight':1.237e-4, 'float':1.745e-4, 'neutral':1.202e-4}
    mod_mass_kg = {'weight': 0.638,  'float': 0.034, 'neutral':0.118}

    # Calculate combined density
    if block_type in block_types:
        seal_vol_m3 = mass_kg / dens_kgm3
        total_mass = (mass_kg + (n_blocks * mod_mass_kg[block_type]))
        total_vol  = (seal_vol_m3 + (n_blocks * mod_vol_m3[block_type]))
        total_dens = total_mass / total_vol

    # Raise error if incorrect `block_type` passed
    else:
        raise KeyError("`block_type` must be 'weight', 'float', or 'neutral'"

    return total_dens


#def calc_mod_density_kagari(mass_kg, dens_kgm3, n_blocks, block_type):
#    def mod_weight(mass_kg, dens_kgm3, n_weights):
#        total_dens = ((mass_kg*1000 + 168*4 + 260*n_weights) /
#                      (mass_kg*1000 / (dens_kgm3/1000) + 168*4) * 1000)
#        return total_dens
#
#    def mod_float(mass_kg, dens_kgm3, n_weights):
#        total_dens = ((mass_kg*1000 + 168*(4-n_floats) + 35*n_floats) /
#                      (mass_kg*1000 / (dens_kgm3/1000) + 168*4) * 1000)
#        return total_dens
#
#    if block_type == 'weight':
#        total_dens = mod_weight(mass_kg, dens_kgm3, n_blocks)
#    elif block_type == 'float':
#        total_dens = mod_float(mass_kg, dens_kgm3, n_blocks)
#    else:
#        raise ValueError('block_type must be "weight" or "float"')
#
#    return total_dens


def add_bodydensity_to_experiments(path_field, path_isotope):
    import numpy
    import pandas

    import utils_seal_physio

    # Load experiments and convert datetimes to datetime
    field = pandas.read_csv(path_field, comment='#')
    field['date'] = pandas.to_datetime(field['date'])

    # Remove rows without an ID (experiments not to be used)
    field = field[~numpy.isnan(field['id'])]

    # Load isotope analysis and isotopemetric data, skip first 4 rows
    isotope = pandas.read_csv(path_isotope, comment='#')

    # Get percent body compositions, including density - what we want
    perc_comps = utils_seal_physio.lip2dens(isotope['fat_perc'])
    isotope['density_kgm3'] = perc_comps['density']*1000

    # List of columns to add to experiments from isotope-isotope data
    cols = ['mass_kg', 'length_cm', 'girth_cm','water_l', 'water_perc', 'fat_kg',
            'fat_perc', 'protein_kg', 'protein_perc', 'density_kgm3']

    # Create new columns in experiment dataframe
    for col in cols:
        field[col] = numpy.nan
    field['total_dens'] = numpy.nan

    # Add data from isotope-morpho dataframe to experiments dataframe
    for i in range(len(field)):
        idx = int(field['tritium_id'].iloc[i])
        midx = numpy.where(isotope['id'] == idx)[0][0]
        field.loc[i, cols] = isotope.ix[midx,cols]

        # Cacluate total density with modification, buoyant forces
        total_dens = calc_mod_density(field['mass_kg'][i],
                                      field['density_kgm3'][i],
                                      field['n_blocks'][i],
                                      field['block_type'][i])

        field.loc[i, 'total_dens'] = total_dens

    return field, isotope
<commit_msg>fixed import of pyotelem for physio_seal module<commit_after>
def simulate_density(mass_kg=40, bd_start=1000, n_bd=101, block_start=1,
        n_blocks=8):
    '''Produce a range of body densities given an initial mass and density'''
    import numpy
    import pandas

    # Range of body densities to test
    types = ['weight', 'float']
    bd_range = bd_start+numpy.arange(0, n_bd)
    bodydensities = numpy.tile(numpy.repeat(bd_range, n_blocks), len(types))

    block_range = block_start+numpy.arange(0, n_blocks)
    blocks = numpy.tile(numpy.tile(block_range, n_bd), len(types))

    types = numpy.repeat(types, n_bd*n_blocks)

    columns = ['type', 'dens_kgm3', 'n_blocks', 'rho_mod', 'delta_rho']
    df = pandas.DataFrame(index=range(len(bodydensities)), columns=columns)

    for i in range(len(df)):
        print(i, df.index[i])
        df.loc[df.index[i], 'type'] = types[i]
        df.loc[df.index[i], 'dens_kgm3'] = bodydensities[i]
        df.loc[df.index[i], 'n_blocks'] = blocks[i]
        rho_mod = calc_mod_density(mass_kg, bodydensities[i], blocks[i], t)
        df.loc[df.index[i], 'rho_mod'] = rho_mod
        df.loc[df.index[i], 'delta_ro'] = rho_mod - bodydensities[i]

    return df


def calc_mod_density(mass_kg, dens_kgm3, n_blocks, block_type):
    '''Calculate the modified density with attached blocks

    Args
    ----
    mass_kg: float
        mass of the seal (kg)
    dens_kgm3: float
        mass of the seal (kg/m^3)
    block_type: str
        Type of modification block for experiment (`weight` or `float`)
    n_blocks: int
        number of modifying blocks attached

    Returns
    -------
    total_dens: float
        combined density of seal and attached blocks

    Notes
    -----
    All blocks dimensions were manually measured and then drawn in `FreeCAD`
    and the volumes calculated using the `FCInfo` macro.

    FLOAT:
      Float blocks were made from syntactic, non-compressible foam. They were
      attached with a neoprene pouch but were not compensated by a lead strip.
      The effect of the pouch buoyancy was not measured by considered to be
      negligible.

      Float block dimensions (with and without sailcloth)
        L:150mm, W:40mm, H:30mm (41, 29)
        shaved 45deg on all edges, 5.5 mm from edge

      Volume float           - 0.000174510114214 m³
      Mass float             - 0.034 kg
      Mass float w/sailcloth - 0.042 kg

    WEIGHT AND NEUTRAL:
      Neutral and weight blocks were made from PE plastic with 2 16mm diameter
      holes drilled length-wise to a depth of 144mm. These holes were
      left empty for neutral blocks. For weight blocks, these holes were filled
      with 140mm long 16mm diameter copper pipe that were filled with lead.

      The buoyancy of the neoprene pouch with which they were attached was
      compensated by a thin strip of lead that allowed the pouch to be neutral
      at surface. As the neoprene compresses with depth, this strip should overcompensate
      to a small degree.

      PE block dimensions:
        L:150mm, W:41mm, H:30mm
        rounded on one width edge - 15mm from edge length-wise and height-wise
      Hole dimensions (2 holes):
        L:144mm length, D:16mm
      Weight rod dimensions (one rod per hole):
        L:140mm, D:15mm

      Mass block w/holes   - 0.118 kg
      Mass weight bar      - 0.260 kg
      Mass weight block    - 0.118 kg + 2(0.260 kg) = 0.638 kg
      Volume block w/holes - 0.00012016274768 m³
      Volume weight bar    - 0.00000176714586764 m³
      Volume weight+2bars  - 1.202e-4 + 2(1.767e-6) = 1.237e-4

      4 weight tubes = 2 weights
    '''

    # Modifier block attributes
    block_types = ['weight', 'float', 'neutral']
    mod_vol_m3 = {'weight':1.237e-4, 'float':1.745e-4, 'neutral':1.202e-4}
    mod_mass_kg = {'weight': 0.638,  'float': 0.034, 'neutral':0.118}

    # Calculate combined density
    if block_type in block_types:
        seal_vol_m3 = mass_kg / dens_kgm3
        total_mass = (mass_kg + (n_blocks * mod_mass_kg[block_type]))
        total_vol  = (seal_vol_m3 + (n_blocks * mod_vol_m3[block_type]))
        total_dens = total_mass / total_vol

    # Raise error if incorrect `block_type` passed
    else:
        raise KeyError("`block_type` must be 'weight', 'float', or 'neutral'"

    return total_dens


#def calc_mod_density_kagari(mass_kg, dens_kgm3, n_blocks, block_type):
#    def mod_weight(mass_kg, dens_kgm3, n_weights):
#        total_dens = ((mass_kg*1000 + 168*4 + 260*n_weights) /
#                      (mass_kg*1000 / (dens_kgm3/1000) + 168*4) * 1000)
#        return total_dens
#
#    def mod_float(mass_kg, dens_kgm3, n_weights):
#        total_dens = ((mass_kg*1000 + 168*(4-n_floats) + 35*n_floats) /
#                      (mass_kg*1000 / (dens_kgm3/1000) + 168*4) * 1000)
#        return total_dens
#
#    if block_type == 'weight':
#        total_dens = mod_weight(mass_kg, dens_kgm3, n_blocks)
#    elif block_type == 'float':
#        total_dens = mod_float(mass_kg, dens_kgm3, n_blocks)
#    else:
#        raise ValueError('block_type must be "weight" or "float"')
#
#    return total_dens


def add_bodydensity_to_experiments(path_field, path_isotope):
    import numpy
    import pandas

    import pyotelem

    # Load experiments and convert datetimes to datetime
    field = pandas.read_csv(path_field, comment='#')
    field['date'] = pandas.to_datetime(field['date'])

    # Remove rows without an ID (experiments not to be used)
    field = field[~numpy.isnan(field['id'])]

    # Load isotope analysis and isotopemetric data, skip first 4 rows
    isotope = pandas.read_csv(path_isotope, comment='#')

    # Get percent body compositions, including density - what we want
    perc_comps = pyotelem.physio_seal.lip2dens(isotope['fat_perc'])
    isotope['density_kgm3'] = perc_comps['density']*1000

    # List of columns to add to experiments from isotope-isotope data
    cols = ['mass_kg', 'length_cm', 'girth_cm','water_l', 'water_perc', 'fat_kg',
            'fat_perc', 'protein_kg', 'protein_perc', 'density_kgm3']

    # Create new columns in experiment dataframe
    for col in cols:
        field[col] = numpy.nan
    field['total_dens'] = numpy.nan

    # Add data from isotope-morpho dataframe to experiments dataframe
    for i in range(len(field)):
        idx = int(field['tritium_id'].iloc[i])
        midx = numpy.where(isotope['id'] == idx)[0][0]
        field.loc[i, cols] = isotope.ix[midx,cols]

        # Cacluate total density with modification, buoyant forces
        total_dens = calc_mod_density(field['mass_kg'][i],
                                      field['density_kgm3'][i],
                                      field['n_blocks'][i],
                                      field['block_type'][i])

        field.loc[i, 'total_dens'] = total_dens

    return field, isotope

{'fraction_non_alphanumeric': 0.10394448502556611, 'fraction_numerical': 0.038568298027757485, 'mean_word_length': 2.7427556041552763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6492307', 'n_tokens_mistral': 4965, 'n_tokens_neox': 4392, 'n_words': 1290}