<commit_before>#!/usr/bin/env python
# MosflmHelpers.py
#   Copyright (C) 2006 CCLRC, <NAME>
#
#   This code is distributed under the terms and conditions of the
#   CCP4 Program Suite Licence Agreement as a CCP4 Library.
#   A copy of the CCP4 licence can be obtained by writing to the
#   CCP4 Secretary, Daresbury Laboratory, Warrington WA4 4AD, UK.
#
# 23rd June 2006
# 
# Helper functions which will make working with Mosflm in detail a little
# easier... for instance parsing the rather extensive output file to 
# decide if the integration went well or no.
# 
# FIXME 19/OCT/06 need to be able to parse the integration log to be able to 
#                 decide if it went ok for all images.
# 
# FIXME 19/OCT/06 would also be useful to be able to get an estimate of
#                 the "useful" integration limit (e.g. where the individual
#                 reflections have an I/sigma ~ 1)

import os

def _parse_mosflm_integration_output(integration_output_list):
    '''Parse mosflm output from integration, passed in as a list of
    strings.'''

    length = len(integration_output_list)

    per_image_stats = {0:{}}

    current_image = 0

    pixel_size = 0.0

    for i in range(length):
        record = integration_output_list[i]


        if 'Pixel size of' in record:
            pixel_size = float(record.replace('mm', ' ').split()[3])
        
        if 'Processing Image' in record:
            current_image = int(record.split()[2])

            if not per_image_stats.has_key(current_image):
                per_image_stats[current_image] = {'scale':1.0}

        if 'Integrating Image' in record:
            current_image = int(record.split()[2])

        if 'XCEN    YCEN  XTOFRA   XTOFD' in record:
            data = map(float, integration_output_list[i + 1].split())
            beam = (data[0], data[1])
            distance = data[3]

            per_image_stats[current_image]['beam'] = beam
            per_image_stats[current_image]['distance'] = distance

        if 'Smoothed value for refined mosaic spread' in record:
            mosaic = float(record.split()[-1])
            per_image_stats[current_image]['mosaic'] = mosaic

        if 'Final rms residual:' in record:
            residual = float(record.replace('mm', ' ').split()[3])
            # FIXME to do this need to be able to compute the
            # residual in pixels...
            rmsd = residual / pixel_size
            per_image_stats[current_image]['rmsd_pixel'] = rmsd
            per_image_stats[current_image]['rmsd_phi'] = 0.0

        if 'Real cell parameters' in record:
            cell = map(float, integration_output_list[i + 1].split())
            per_image_stats[current_image]['cell'] = cell

        if 'Spots measured on this image' in record:
            spots = int(record.split()[0])
            # FIXME this is misnamed because it matches a name in the
            # XDS version of this parser.
            per_image_stats[current_image]['strong'] = spots

        if 'are OVERLOADS' in record:
            overloads = int(record.replace(',', ' ').split()[4])
            per_image_stats[current_image]['overloads'] = overloads
            
        if 'Number of bad spots' in record:
            bad = int(record.split()[-1])
            # FIXME also with the name...
            per_image_stats[current_image]['rejected'] = bad
            
    return per_image_stats

def _print_integrate_lp(integrate_lp_stats):
    '''Print the contents of the integrate.lp dictionary.'''

    images = integrate_lp_stats.keys()
    images.sort()

    if images[0] == 0:
        images = images[1:]

    for i in images:
        data = integrate_lp_stats[i]
        print '%4d %5.3f %5d %5d %5d %4.2f %6.2f' % \
              (i, data['scale'], data['strong'],
               data['overloads'], data['rejected'],
               data.get('mosaic', 0.0), data['distance'])

if __name__ == '__main__':
    integrate_lp = os.path.join(os.environ['DPA_ROOT'], 'Wrappers', 'CCP4',
                                'Doc', 'mosflm-reintegration.log')
    stats = _parse_mosflm_integration_output(
        open(integrate_lp, 'r').readlines())
    _print_integrate_lp(stats)
    <commit_msg>Added in the happy-integration function ;o)<commit_after>#!/usr/bin/env python
# MosflmHelpers.py
#   Copyright (C) 2006 CCLRC, <NAME>
#
#   This code is distributed under the terms and conditions of the
#   CCP4 Program Suite Licence Agreement as a CCP4 Library.
#   A copy of the CCP4 licence can be obtained by writing to the
#   CCP4 Secretary, Daresbury Laboratory, Warrington WA4 4AD, UK.
#
# 23rd June 2006
# 
# Helper functions which will make working with Mosflm in detail a little
# easier... for instance parsing the rather extensive output file to 
# decide if the integration went well or no.
# 
# FIXME 19/OCT/06 need to be able to parse the integration log to be able to 
#                 decide if it went ok for all images.
# 
# FIXME 19/OCT/06 would also be useful to be able to get an estimate of
#                 the "useful" integration limit (e.g. where the individual
#                 reflections have an I/sigma ~ 1)

import os

def _parse_mosflm_integration_output(integration_output_list):
    '''Parse mosflm output from integration, passed in as a list of
    strings.'''

    length = len(integration_output_list)

    per_image_stats = {0:{}}

    current_image = 0

    pixel_size = 0.0

    for i in range(length):
        record = integration_output_list[i]


        if 'Pixel size of' in record:
            pixel_size = float(record.replace('mm', ' ').split()[3])
        
        if 'Processing Image' in record:
            current_image = int(record.split()[2])

            if not per_image_stats.has_key(current_image):
                per_image_stats[current_image] = {'scale':1.0}

        if 'Integrating Image' in record:
            current_image = int(record.split()[2])

        if 'XCEN    YCEN  XTOFRA   XTOFD' in record:
            data = map(float, integration_output_list[i + 1].split())
            beam = (data[0], data[1])
            distance = data[3]

            per_image_stats[current_image]['beam'] = beam
            per_image_stats[current_image]['distance'] = distance

        if 'Smoothed value for refined mosaic spread' in record:
            mosaic = float(record.split()[-1])
            per_image_stats[current_image]['mosaic'] = mosaic

        if 'Final rms residual:' in record:
            residual = float(record.replace('mm', ' ').split()[3])
            # FIXME to do this need to be able to compute the
            # residual in pixels...
            rmsd = residual / pixel_size
            per_image_stats[current_image]['rmsd_pixel'] = rmsd
            per_image_stats[current_image]['rmsd_phi'] = 0.0

        if 'Real cell parameters' in record:
            cell = map(float, integration_output_list[i + 1].split())
            per_image_stats[current_image]['cell'] = cell

        if 'Spots measured on this image' in record:
            spots = int(record.split()[0])
            # FIXME this is misnamed because it matches a name in the
            # XDS version of this parser.
            per_image_stats[current_image]['strong'] = spots

        if 'are OVERLOADS' in record:
            overloads = int(record.replace(',', ' ').split()[4])
            per_image_stats[current_image]['overloads'] = overloads
            
        if 'Number of bad spots' in record:
            bad = int(record.split()[-1])
            # FIXME also with the name...
            per_image_stats[current_image]['rejected'] = bad

    per_image_stats.pop(0, None)
            
    return per_image_stats

def _print_integrate_lp(integrate_lp_stats):
    '''Print the contents of the integrate.lp dictionary.'''

    images = integrate_lp_stats.keys()
    images.sort()

    for i in images:
        data = integrate_lp_stats[i]
        print '%4d %5.3f %5d %5d %5d %4.2f %6.2f' % \
              (i, data['scale'], data['strong'],
               data['overloads'], data['rejected'],
               data.get('mosaic', 0.0), data['distance'])

def _happy_integrate_lp(integrate_lp_stats):
    '''Return a string which explains how happy we are with the integration.'''

    images = integrate_lp_stats.keys()
    images.sort()

    results = ''

    for i in images:
        data = integrate_lp_stats[i]

        if data['rmsd_pixel'] > 1.0:
            status = '*'

        else:

            status = '.'

        results += status

    return results

if __name__ == '__main__':
    integrate_lp = os.path.join(os.environ['DPA_ROOT'], 'Wrappers', 'CCP4',
                                'Doc', 'mosflm-reintegration.log')
    stats = _parse_mosflm_integration_output(
        open(integrate_lp, 'r').readlines())
    _print_integrate_lp(stats)
    
    print _happy_integrate_lp(stats)
{'fraction_non_alphanumeric': 0.1064381553726463, 'fraction_numerical': 0.0139812831209832, 'mean_word_length': 2.366223908918406, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2988276', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2613, 'n_words': 786}