pysilcam/process.py
def extract_particles(imc, timestamp, settings, nnmodel, class_labels, region_properties):
    '''extracts the particles to build stats and export particle rois to HDF5 files writted to disc in the location of settings.ExportParticles.outputpath

    Args:
        imc                         : background-corrected image
        timestamp                   : timestamp of image collection
        settings                    : PySilCam settings
        nnmodel                     : loaded tensorflow model from silcam_classify
        class_labels                : lables of particle classes in tensorflow model
        region_properties           : region properties object returned from regionprops (measure.regionprops(iml, cache=False))

    Returns:
        stats                       : (list of particle statistics for every particle, according to Partstats class)

    @todo clean up all the unnesessary conditional statements in this
    '''
    filenames = ['not_exported'] * len(region_properties)

    # pre-allocation
    predictions = np.zeros((len(region_properties),
         len(class_labels)),
         dtype='float64')
    predictions *= np.nan

    # obtain the original image filename from the timestamp
    filename = timestamp.strftime('D%Y%m%dT%H%M%S.%f')

    if settings.ExportParticles.export_images:
        # Make the HDF5 file
        hdf_filename = os.path.join(settings.ExportParticles.outputpath, filename + ".h5")
        HDF5File = h5py.File(hdf_filename, "w")
        # metadata
        meta = HDF5File.create_group('Meta')
        meta.attrs['Modified'] = str(pd.datetime.now())
        settings_dict = {s: dict(settings.config.items(s)) for s in settings.config.sections()}
        meta.attrs['Settings'] = str(settings_dict)
        meta.attrs['Timestamp'] = str(timestamp)
        meta.attrs['Raw image name'] = filename
        #@todo include more useful information in this meta data, e.g. possibly raw image location and background stack file list.

    # define the geometrical properties to be calculated from regionprops
    propnames = ['major_axis_length', 'minor_axis_length',
                 'equivalent_diameter', 'solidity']

    # pre-allocate some things
    data = np.zeros((len(region_properties), len(propnames)), dtype=np.float64)
    bboxes = np.zeros((len(region_properties), 4), dtype=np.float64)
    nb_extractable_part = 0

    for i, el in enumerate(region_properties):
        data[i, :] = [getattr(el, p) for p in propnames]
        bboxes[i, :] = el.bbox

        # if operating in realtime mode, assume we only care about oil and gas and skip export of overly-derformed particles
        if settings.Process.real_time_stats & (((data[i, 1]/data[i, 0])<0.3) | (data[i, 3]<0.95)):
            continue
        # Find particles that match export criteria
        if ((data[i, 0] > settings.ExportParticles.min_length) & #major_axis_length in pixels
            (data[i, 1] > 2)): # minor length in pixels

            nb_extractable_part += 1
            # extract the region of interest from the corrected colour image
            roi = extract_roi(imc,bboxes[i, :].astype(int))

            # add the roi to the HDF5 file
            filenames[int(i)] = filename + '-PN' + str(i)
            if settings.ExportParticles.export_images:
                dset = HDF5File.create_dataset('PN' + str(i), data = roi)
                #@todo also include particle stats here too.

            # run a prediction on what type of particle this might be
            prediction = sccl.predict(roi, nnmodel)
            predictions[int(i),:] = prediction[0]

    if settings.ExportParticles.export_images:
        # close the HDF5 file
        HDF5File.close()

    # build the column names for the outputed DataFrame
    column_names = np.hstack(([propnames, 'minr', 'minc', 'maxr', 'maxc']))

    # merge regionprops statistics with a seperate bounding box columns
    cat_data = np.hstack((data, bboxes))

    # put particle statistics into a DataFrame
    stats = pd.DataFrame(columns=column_names, data=cat_data)

    logger.info('EXTRACTING {0} IMAGES from {1}'.format(nb_extractable_part, len(stats['major_axis_length'])))

    # add classification predictions to the particle statistics data
    for n,c in enumerate(class_labels):
       stats['probability_' + c] = predictions[:,n]

    # add the filenames of the HDF5 file and particle number tag to the
    # particle statistics data
    stats['export name'] = filenames

    return stats
extracts the particles to build stats and export particle rois to HDF5 files writted to disc in the location of settings.ExportParticles.outputpath

    Args:
        imc                         : background-corrected image
        timestamp                   : timestamp of image collection
        settings                    : PySilCam settings
        nnmodel                     : loaded tensorflow model from silcam_classify
        class_labels                : lables of particle classes in tensorflow model
        region_properties           : region properties object returned from regionprops (measure.regionprops(iml, cache=False))

    Returns:
        stats                       : (list of particle statistics for every particle, according to Partstats class)

    @todo clean up all the unnesessary conditional statements in this
    
extracts the particles to build stats and export particle rois to HDF5 files writted to disc in the location of settings.ExportParticles.outputpath
extracts the particles to build stats and export particle rois to HDF5 files writted to disc in the location of settings . ExportParticles . outputpath
def extract_particles(imc, timestamp, settings, nnmodel, class_labels, region_properties):
    filenames = ['not_exported'] * len(region_properties)
    predictions = np.zeros((len(region_properties),
         len(class_labels)),
         dtype='float64')
    predictions *= np.nan
    filename = timestamp.strftime('D%Y%m%dT%H%M%S.%f')
    if settings.ExportParticles.export_images:
        hdf_filename = os.path.join(settings.ExportParticles.outputpath, filename + ".h5")
        HDF5File = h5py.File(hdf_filename, "w")
        meta = HDF5File.create_group('Meta')
        meta.attrs['Modified'] = str(pd.datetime.now())
        settings_dict = {s: dict(settings.config.items(s)) for s in settings.config.sections()}
        meta.attrs['Settings'] = str(settings_dict)
        meta.attrs['Timestamp'] = str(timestamp)
        meta.attrs['Raw image name'] = filename
    propnames = ['major_axis_length', 'minor_axis_length',
                 'equivalent_diameter', 'solidity']
    data = np.zeros((len(region_properties), len(propnames)), dtype=np.float64)
    bboxes = np.zeros((len(region_properties), 4), dtype=np.float64)
    nb_extractable_part = 0
    for i, el in enumerate(region_properties):
        data[i, :] = [getattr(el, p) for p in propnames]
        bboxes[i, :] = el.bbox
        if settings.Process.real_time_stats & (((data[i, 1]/data[i, 0])<0.3) | (data[i, 3]<0.95)):
            continue
        if ((data[i, 0] > settings.ExportParticles.min_length) & 
            (data[i, 1] > 2)): 
            nb_extractable_part += 1
            roi = extract_roi(imc,bboxes[i, :].astype(int))
            filenames[int(i)] = filename + '-PN' + str(i)
            if settings.ExportParticles.export_images:
                dset = HDF5File.create_dataset('PN' + str(i), data = roi)
            prediction = sccl.predict(roi, nnmodel)
            predictions[int(i),:] = prediction[0]
    if settings.ExportParticles.export_images:
        HDF5File.close()
    column_names = np.hstack(([propnames, 'minr', 'minc', 'maxr', 'maxc']))
    cat_data = np.hstack((data, bboxes))
    stats = pd.DataFrame(columns=column_names, data=cat_data)
    logger.info('EXTRACTING {0} IMAGES from {1}'.format(nb_extractable_part, len(stats['major_axis_length'])))
    for n,c in enumerate(class_labels):
       stats['probability_' + c] = predictions[:,n]
    stats['export name'] = filenames
    return stats
def extract_particles ( imc , timestamp , settings , nnmodel , class_labels , region_properties ) : filenames = [ 'not_exported' ] * len ( region_properties ) predictions = np . zeros ( ( len ( region_properties ) , len ( class_labels ) ) , dtype = 'float64' ) predictions *= np . nan filename = timestamp . strftime ( 'D%Y%m%dT%H%M%S.%f' ) if settings . ExportParticles . export_images : hdf_filename = os . path . join ( settings . ExportParticles . outputpath , filename + ".h5" ) HDF5File = h5py . File ( hdf_filename , "w" ) meta = HDF5File . create_group ( 'Meta' ) meta . attrs [ 'Modified' ] = str ( pd . datetime . now ( ) ) settings_dict = { s : dict ( settings . config . items ( s ) ) for s in settings . config . sections ( ) } meta . attrs [ 'Settings' ] = str ( settings_dict ) meta . attrs [ 'Timestamp' ] = str ( timestamp ) meta . attrs [ 'Raw image name' ] = filename propnames = [ 'major_axis_length' , 'minor_axis_length' , 'equivalent_diameter' , 'solidity' ] data = np . zeros ( ( len ( region_properties ) , len ( propnames ) ) , dtype = np . float64 ) bboxes = np . zeros ( ( len ( region_properties ) , 4 ) , dtype = np . float64 ) nb_extractable_part = 0 for i , el in enumerate ( region_properties ) : data [ i , : ] = [ getattr ( el , p ) for p in propnames ] bboxes [ i , : ] = el . bbox if settings . Process . real_time_stats & ( ( ( data [ i , 1 ] / data [ i , 0 ] ) < 0.3 ) | ( data [ i , 3 ] < 0.95 ) ) : continue if ( ( data [ i , 0 ] > settings . ExportParticles . min_length ) & ( data [ i , 1 ] > 2 ) ) : nb_extractable_part += 1 roi = extract_roi ( imc , bboxes [ i , : ] . astype ( int ) ) filenames [ int ( i ) ] = filename + '-PN' + str ( i ) if settings . ExportParticles . export_images : dset = HDF5File . create_dataset ( 'PN' + str ( i ) , data = roi ) prediction = sccl . predict ( roi , nnmodel ) predictions [ int ( i ) , : ] = prediction [ 0 ] if settings . ExportParticles . export_images : HDF5File . close ( ) column_names = np . hstack ( ( [ propnames , 'minr' , 'minc' , 'maxr' , 'maxc' ] ) ) cat_data = np . hstack ( ( data , bboxes ) ) stats = pd . DataFrame ( columns = column_names , data = cat_data ) logger . info ( 'EXTRACTING {0} IMAGES from {1}' . format ( nb_extractable_part , len ( stats [ 'major_axis_length' ] ) ) ) for n , c in enumerate ( class_labels ) : stats [ 'probability_' + c ] = predictions [ : , n ] stats [ 'export name' ] = filenames return stats
extracts the particles to build stats and export particle rois to HDF5 files writted to disc in the location of settings.ExportParticles.outputpath
extracts the particles to build stats and export particle rois to HDF5 files writted to disc in the location of settings . ExportParticles . outputpath
'''extracts the particles to build stats and export particle rois to HDF5 files writted to disc in the location of settings.ExportParticles.outputpath

    Args:
        imc                         : background-corrected image
        timestamp                   : timestamp of image collection
        settings                    : PySilCam settings
        nnmodel                     : loaded tensorflow model from silcam_classify
        class_labels                : lables of particle classes in tensorflow model
        region_properties           : region properties object returned from regionprops (measure.regionprops(iml, cache=False))

    Returns:
        stats                       : (list of particle statistics for every particle, according to Partstats class)

    @todo clean up all the unnesessary conditional statements in this
    ''' # pre-allocation # obtain the original image filename from the timestamp # Make the HDF5 file # metadata #@todo include more useful information in this meta data, e.g. possibly raw image location and background stack file list. # define the geometrical properties to be calculated from regionprops # pre-allocate some things # if operating in realtime mode, assume we only care about oil and gas and skip export of overly-derformed particles # Find particles that match export criteria #major_axis_length in pixels # minor length in pixels # extract the region of interest from the corrected colour image # add the roi to the HDF5 file #@todo also include particle stats here too. # run a prediction on what type of particle this might be # close the HDF5 file # build the column names for the outputed DataFrame # merge regionprops statistics with a seperate bounding box columns # put particle statistics into a DataFrame # add classification predictions to the particle statistics data # add the filenames of the HDF5 file and particle number tag to the # particle statistics data
stats                       : (list of particle statistics for every particle, according to Partstats class)
stats : ( list of particle statistics for every particle according to Partstats class )
imc
timestamp
settings
nnmodel
class_labels
region_properties
imc                         
None
timestamp                   
timestamp of image collection
timestamp of image collection
settings                    
None
nnmodel                     
loaded tensorflow model from silcam_classify
loaded tensorflow model from silcam_classify
class_labels                
lables of particle classes in tensorflow model
lables of particle classes in tensorflow model
region_properties           
region properties object returned from regionprops (measure.regionprops(iml, cache=False))
region properties object returned from regionprops ( measure . regionprops ( iml cache = False ))