Beispiel #1
0
    def setUp(self):
        test_input_data = PDB()
        test_input_data.load(f'{data_folder}/molA.pdb')
        test_input_data.load(f'{data_folder}/molB.pdb')

        test_restraints = Restraint(test_input_data.raw_pdb)
        test_restraints.load([1, 2, 3], 'A')
        test_restraints.load([2, 3, 4], 'B')

        self.Geometry = Geometry(test_input_data, test_restraints)
Beispiel #2
0
class HitManager:
    """Manages hits stored inside a Pandas dataframe"""
    hits = None
    HIT_DTYPES = {
        'sl': np.uint8,
        'layer': np.uint8,
        'wire': np.uint8,
        'time': np.float32
    }
    POS_RES = (0.3, 0.0, 0.0)

    def __init__(self):
        self.G = Geometry(config)
        self.reset()

    def add_hits(self, hits_list):
        """Adds a list of hits with the given parameters"""
        hits = pd.DataFrame(hits_list,
                            columns=['sl', 'layer', 'wire',
                                     'time']).astype(self.HIT_DTYPES)
        self.hits = self.hits.append(hits, ignore_index=True, sort=False)

    def reset(self):
        """Removes all hits from the dataframe"""
        self.hits = pd.DataFrame(
            columns=['sl', 'layer', 'wire', 'time']).astype(self.HIT_DTYPES)

    def calc_pos(self, sls=None):
        """Calculates the left/right hit positions for all the hits in the dataframe"""
        # Filling the hit positions in the local reference frame of the superlayer
        self.G.fill_hits_position(self.hits, reco=True)
        if sls:
            nhits = len(self.hits)
            zeros = np.zeros(nhits, dtype=DTYPE_COOR)
            for col in [
                    'glposx', 'glposy', 'glposz', 'grposx', 'grposy', 'grposz'
            ]:
                self.hits[col] = zeros
            # Calculating the hit positions for each SL in the global reference frame
            for iSL, sl in sls.items():
                sel = self.hits['sl'] == iSL
                # Updating global positions for left hits
                pos_global = sl.coor_to_global(
                    self.hits.loc[sel, ['lposx', 'posy', 'posz']].values)
                self.hits.loc[sel, ['glposx', 'glposy', 'glposz']] = pos_global
                # Updating global positions for right hits
                pos_global = sl.coor_to_global(
                    self.hits.loc[sel, ['rposx', 'posy', 'posz']].values)
                self.hits.loc[sel, ['grposx', 'grposy', 'grposz']] = pos_global
Beispiel #3
0
    def fitness_function(pdb_dic, individual):
        """Calculate the fitness of an individual."""
        # use the chromossome and create the structure!

        # fun fact: if you do not use deepcopy here,
        #  the fitness will depend on the number of processors
        #  since pdb_dic is a shared data structure
        individual_dic = copy.deepcopy(pdb_dic)
        individual_str = ' '.join([f'{j:.2f}' for j in individual])
        c = np.array(individual_dic['B']['coord'])

        translation_center = individual[3:]
        rotation_angles = individual[:3]

        translated_coords = Geometry.translate(c, translation_center)
        rotated_coords = Geometry.rotate(translated_coords, rotation_angles)

        individual_dic['B']['coord'] = list(rotated_coords)

        # use a temporary file to keep the execution simple with
        #  some I/O trade-off
        pdb = NamedTemporaryFile(delete=False, suffix='.pdb')
        for chain in individual_dic:
            coord_l = individual_dic[chain]['coord']
            raw_l = individual_dic[chain]['raw']
            for coord, line in zip(coord_l, raw_l):
                new_x, new_y, new_z = format_coords(coord)
                new_line = (f'{line[:30]} {new_x} {new_y} {new_z} '
                            f'{line[55:]}' + os.linesep)
                pdb.write(str.encode(new_line))
        pdb.close()

        # Calculate fitnesses!
        # ================================#
        # energy = run_dcomplex(pdb.name)
        satisfaction = calc_satisfaction(pdb.name,
                                         individual_dic['A']['restraints'],
                                         individual_dic['B']['restraints'])
        # ================================#

        # unlink the pdb so that it disappears
        os.unlink(pdb.name)

        # return [satisfaction, energy]
        ga_log.debug(f'{individual_str} {satisfaction:.2f} {pdb.name}')
        # this must (?) be a list: github.com/DEAP/deap/issues/256
        return [satisfaction]
Beispiel #4
0
    def _recreate(input_structure_dic, individual, pdb_name):
        """Use the chromossome information and create the structure."""
        input_dic = copy.deepcopy(input_structure_dic)
        c = np.array(input_dic['B']['coord'])

        translation_center = individual[3:]
        rotation_angles = individual[:3]

        translated_coords = Geometry.translate(c, translation_center)
        rotated_coords = Geometry.rotate(translated_coords, rotation_angles)

        input_dic['B']['coord'] = list(rotated_coords)

        with open(pdb_name, 'w') as fh:
            for chain in input_dic:
                coord_l = input_dic[chain]['coord']
                raw_l = input_dic[chain]['raw']
                for coord, line in zip(coord_l, raw_l):
                    new_x, new_y, new_z = format_coords(coord)
                    new_line = (f'{line[:30]} {new_x} {new_y} {new_z}'
                                f' {line[55:]}' + os.linesep)
                    fh.write(new_line)
        fh.close()
Beispiel #5
0
def main():

    from time import time

    from modules.geometry import Geometry
    from modules.leaf import Leaf

    steps = 1000
    noise = 0
    stp = 0.25
    killzone = stp * 2.
    geom_fn = 'geom'
    leaf_fn = 'leaf'

    simple_map_limit = 10

    source_count_terminate = 1
    normal_compare = True
    normal_limit = 0.5

    L = Leaf(stp, geometry=Geometry(geom_fn), noise=noise, killzone=killzone)

    t1 = time()

    for i in range(steps):
        try:
            L.grow(simple_map_limit=simple_map_limit,
                   normal_compare=normal_compare,
                   normal_limit=normal_limit)
            L.print_info()

            if len(L.geometry.sources) < source_count_terminate:
                break
        except KeyboardInterrupt:
            print('KeyboardInterrupt')
            break

    t2 = time()

    print('\n\ntime:', t2 - t1)

    print('\n\nwriting leaf ...')
    L.to_file(leaf_fn)
    print('done.')

    return
Beispiel #6
0
def read_data(input_files, runnum):
    """
    Reading data from CSV file into a Pandas dataframe, applying selection and sorting
    """
    # Reading each file and merging into 1 dataframe
    hits = []
    for index, file in enumerate(input_files):
        skipLines = 0
        # Skipping the line with column names for the new trigger format (because trigger lines have 1 extra value breaking the CSV reader)
        if args.trigger_v1:
            skipLines = max(skipLines, 1)
        # Skipping the old buffer content of the boards that is dumped at the beginning of the run
        if 'data_000000' in file:
            skipLines = range(1, 131072)
        column_names = [
            'HEAD', 'FPGA', 'TDC_CHANNEL', 'ORBIT_CNT', 'BX_COUNTER',
            'TDC_MEAS', 'TRG_QUALITY'
        ]
        df = pd.read_csv(file,
                         nrows=args.number,
                         skiprows=skipLines,
                         engine='c',
                         names=column_names)
        # Converting to memory-optimised data types
        for name in ['HEAD', 'FPGA', 'TDC_CHANNEL']:
            df[name] = df[name].astype(np.uint8)
        for name in ['TRG_QUALITY']:
            df[name] = df[name].fillna(-1).astype(np.uint8)
        for name in ['BX_COUNTER', 'TDC_MEAS']:
            df[name] = df[name].astype(np.uint16)
        for name in ['ORBIT_CNT']:
            df[name] = df[name].astype(np.uint32)
        # Removing possible incomplete rows e.g. last line of last file
        df.dropna(inplace=True)
        hits.append(df)
    allhits = pd.concat(hits, ignore_index=True, copy=False)
    df_events = None
    print('### Read {0:d} hits from {1:d} input files'.format(
        allhits.shape[0], len(hits)))
    # Calculating gometry related columns
    G = Geometry(CONFIGURATION)
    G.fill_hits_geometry(allhits)
    ### # Increase output of all channels with id below 130 by 1 ns --> NOT NEEDED
    ### allhits.loc[allhits['TDC_CHANNEL'] <= 130, 'TDC_MEAS'] = allhits['TDC_MEAS']+1
    # Calculate absolute time in ns of each hit
    allhits['TIME_ABS'] = (
        allhits['ORBIT_CNT'].astype(np.float64) * DURATION['orbit'] +
        allhits['BX_COUNTER'].astype(np.float64) * DURATION['bx'] +
        allhits['TDC_MEAS'].astype(np.float64) * DURATION['tdc']).astype(
            np.float64)
    # Adding columns to be calculated
    nHits = allhits.shape[0]
    allhits['TIME0'] = np.zeros(nHits, dtype=np.float64)
    allhits['EVENT_NR'] = np.ones(nHits, dtype=np.uint32) * -1
    # Calculating additional info about the hits
    conditions = [
        (allhits['TDC_CHANNEL'] % 4 == 1),
        (allhits['TDC_CHANNEL'] % 4 == 2),
        (allhits['TDC_CHANNEL'] % 4 == 3),
        (allhits['TDC_CHANNEL'] % 4 == 0),
    ]
    chanshift_x = [
        0,
        -1,
        0,
        -1,
    ]
    pos_z = [
        ZCELL * 3.5,
        ZCELL * 1.5,
        ZCELL * 2.5,
        ZCELL * 0.5,
    ]
    posshift_x = [
        0,
        0,
        0.5,
        0.5,
    ]
    # Adding columns
    allhits['X_POSSHIFT'] = np.select(conditions, posshift_x,
                                      default=0).astype(np.float16)
    allhits['Z_POS'] = np.select(conditions, pos_z,
                                 default=0).astype(np.float16)

    # Correcting absolute time by per-chamber latency
    for sl in range(4):
        sel = allhits['SL'] == sl
        allhits.loc[sel,
                    'TIME_ABS'] = allhits.loc[sel,
                                              'TIME_ABS'] + TIME_OFFSET_SL[sl]

    # Detecting events based on trigger signals
    if args.event:
        df_events = calc_event_numbers(allhits, runnum, args.trigger_v1)
    else:
        # Grouping hits separated by large time gaps together
        allhits.sort_values('TIME_ABS', inplace=True)
        grp = allhits['TIME_ABS'].diff().fillna(0)
        grp[grp <= 1.1 * TDRIFT] = 0
        grp[grp > 0] = 1
        grp = grp.cumsum().astype(np.int32)
        allhits['EVENT_NR'] = grp
        events = allhits.groupby('EVENT_NR')
        nHits = events.size()
        nHits_unique = events['TDC_CHANNEL'].nunique()
        nSL = events['SL'].nunique()
        # Selecting only events with manageable numbers of hits
        events = nHits.index[(nSL >= args.chambers)
                             & (nHits_unique >= (MEANTIMER_CLUSTER_SIZE * 3)) &
                             (nHits <= args.max_hits)]
        # Marking events that don't pass the basic selection
        sel = allhits['EVENT_NR'].isin(events)
        allhits.loc[~sel, 'EVENT_NR'] = -1
        df_events = pd.DataFrame(data={'EVENT_NR': events})
        df_events['TIME0'] = -1
        df_events['TRG_BITS'] = -1
        df_events['TIMEDIFF_TRG_20'] = -1e9
        df_events['TIMEDIFF_TRG_21'] = -1e9
        df_events.set_index('EVENT_NR', inplace=True)
    # Removing hits with no event number
    allhits.drop(allhits.index[allhits['EVENT_NR'] == -1], inplace=True)
    # Calculating event times
    df_events['TIME0_BEFORE'] = df_events['TIME0'].diff().fillna(0)
    df_events['TIME0_AFTER'] = df_events['TIME0'].diff(-1).fillna(0)

    # Removing hits with irrelevant tdc channels
    sel = allhits['TDC_CHANNEL_NORM'] <= NCHANNELS
    for ch in CHANNELS_VIRTUAL:
        sel = sel | ((allhits['FPGA'] == ch[0]) &
                     (allhits['TDC_CHANNEL'] == ch[1]))
    allhits.drop(allhits.index[~sel], inplace=True)
    # Removing events that don't pass acceptance cuts
    if args.accepted:
        select_accepted_events(allhits, df_events)

    nHits = allhits.shape[0]
    # Adding extra columns to be filled in the analyse method
    # allhits['TIMENS'] = np.zeros(nHits, dtype=np.float16)
    # allhits['X_POS_LEFT'] = np.zeros(nHits, dtype=np.float32)
    # allhits['X_POS_RIGHT'] = np.zeros(nHits, dtype=np.float32)

    # Adding the time and position information for the hits
    allhits['TIMENS'] = allhits['TIME_ABS'] - allhits['TIME0']
    G.fill_hits_position(allhits)

    #############################################
    ### DATA HANDLING

    if VERBOSE:
        print('')
        print('dataframe size                   :', len(allhits))
        print('')

    if VERBOSE:
        print('dataframe size (no trigger hits) :', len(allhits))
        print('')
        print('min values in dataframe')
        print(allhits[[
            'TDC_CHANNEL', 'TDC_CHANNEL_NORM', 'TDC_MEAS', 'BX_COUNTER',
            'ORBIT_CNT'
        ]].min())
        print('')
        print('max values in dataframe')
        print(allhits[[
            'TDC_CHANNEL', 'TDC_CHANNEL_NORM', 'TDC_MEAS', 'BX_COUNTER',
            'ORBIT_CNT'
        ]].max())
        print('')

    return allhits, df_events
Beispiel #7
0
    # 1. Load structure
    ga_log.info('Loading Structures module')
    input_molecules = PDB()
    input_molecules.load(run_params['mol_a'])
    input_molecules.load(run_params['mol_b'])

    # 2. Load restraints
    ga_log.info('Loading Restraints module')
    restraints = Restraint(input_molecules.raw_pdb)
    restraints.load(run_params['restraints_a'], 'A')
    restraints.load(run_params['restraints_b'], 'B')

    # 3. Position
    ga_log.info('Loading Geometry module')
    geo = Geometry(input_molecules, restraints)
    geo.calc_initial_position()
    initial_complex = geo.apply_transformation()

    # 4. Run GA
    ga_log.info('Loading Genetic Algorithm module')
    ga = GeneticAlgorithm(initial_complex, run_params, ga_params)
    ga.setup()
    results = ga.run()

    # 5. Scoring
    ga_log.info('Loading Scoring module')
    scoring = Scoring(results, run_params)
    results = scoring.score()

    # 6. Analysis
Beispiel #8
0
class TestGeometry(unittest.TestCase):
    def setUp(self):
        test_input_data = PDB()
        test_input_data.load(f'{data_folder}/molA.pdb')
        test_input_data.load(f'{data_folder}/molB.pdb')

        test_restraints = Restraint(test_input_data.raw_pdb)
        test_restraints.load([1, 2, 3], 'A')
        test_restraints.load([2, 3, 4], 'B')

        self.Geometry = Geometry(test_input_data, test_restraints)

    def test_calc_initial_position(self):
        self.Geometry.calc_initial_position()
        expected_ligand_coord = np.array(
            [[4.41488596, 6.18960229, 2.7898511],
             [5.38935079, 4.86187319, 6.26942201],
             [3.13271966, 7.23145808, 8.22975065],
             [-0.11367046, 5.48686873, 9.2551331],
             [-3.37010188, 7.31580583, 9.73693127]])
        expected_receptor_coord = np.array([[-5.0052, 0.9464, 0.611],
                                            [-1.9152, 2.3144, 2.455],
                                            [0.3288, -0.6806, 1.717],
                                            [2.6488, 0.0484, -1.236],
                                            [3.9428, -2.6286, -3.547]])

        observed_ligand_coord = self.Geometry.ligand_coord
        observed_receptor_coord = self.Geometry.receptor_coord

        self.assertTrue(
            np.allclose(observed_ligand_coord, expected_ligand_coord))
        self.assertTrue(
            np.allclose(observed_receptor_coord, expected_receptor_coord))

    def test_apply_transformation(self):
        observed_tidy_complex = self.Geometry.apply_transformation()
        observed_tidy_complex = observed_tidy_complex.split(os.linesep)

        with open(f'{data_folder}/tidy_transformed.pdb', 'r') as test_tidy_fh:
            expected_tidy_complex = ''.join(test_tidy_fh.readlines())
        test_tidy_fh.close()

        expected_tidy_complex = expected_tidy_complex.split(os.linesep)
        self.assertEqual(len(observed_tidy_complex),
                         len(expected_tidy_complex))
        for observed_line, expected_line in zip(observed_tidy_complex,
                                                expected_tidy_complex):
            self.assertEqual(observed_line, expected_line)

    def test_translate(self):
        coords = np.array([[34.082, -15.413, 35.895],
                           [34.912, -15.074, 34.747]])
        point = [-1, +2, -3]
        observed_trans_coords = self.Geometry.translate(coords, point)
        expected_trans_coords = np.array([[33.082, -13.413, 32.895],
                                          [33.912, -13.074, 31.747]])
        self.assertTrue(
            np.allclose(observed_trans_coords, expected_trans_coords))

    def test_rotate(self):
        coords = np.array([[34.082, -15.413, 35.895],
                           [34.912, -15.074, 34.747]])
        rotation = [327, 57, 12]
        observed_rot_coords = self.Geometry.rotate(coords, rotation)
        expected_rot_coords = np.array(
            [[34.42920652, -15.11616826, 36.03487809],
             [34.56479348, -15.37083174, 34.60712191]])

        self.assertTrue(np.allclose(observed_rot_coords, expected_rot_coords))
Beispiel #9
0
 def __init__(self):
     self.G = Geometry(config)
     self.reset()
Beispiel #10
0
events[['X_GLOB', 'Y_GLOB', 'Z_GLOB']] = [np.nan, np.nan, np.nan]

# Add number of tappino (c)
#mapconverter.addTappinoNum(events)

missinghits = pd.DataFrame(columns=['ORBIT', 'BX', 'CHAMBER', 'LAYER', 'WIRE', 'X', 'Y', 'Z'])
segments = pd.DataFrame(columns=['VIEW', 'ORBIT', 'CHAMBER', 'NHITS', 'M', 'SIGMAM', 'Q', 'SIGMAQ', 'CHI2', 'HIT_INDEX', 'T0', 'T_REFIT', 'T_SCINTAND', 'T_SCINTPMT'])

# Reconstruction
from modules.geometry.sl import SL
from modules.geometry.segment import Segment
from modules.geometry import Geometry, COOR_ID
from modules.analysis import config as CONFIGURATION

# Initialize geometry
G = Geometry(CONFIGURATION)
SLs = {}
for iSL in config.SL_SHIFT.keys():
    SLs[iSL] = SL(iSL, config.SL_SHIFT[iSL], config.SL_ROTATION[iSL])

# Defining which SLs should be plotted (and fitted) in which global view
GLOBAL_VIEW_SLs = {}
for view in ['xz', 'yz']: GLOBAL_VIEW_SLs[view] = [SLs[x] for x in config.SL_VIEW[view]]
#    'xz': [SLs[0], SLs[2]],
#    'yz': [SLs[1], SLs[3]]

if args.verbose >= 1: print("[", datetime.now() - itime, "]", "Reconstructing segments")

# Reset counters
nEvSl, iEvSl = len(events.groupby(['ORBIT', 'CHAMBER'])), 0