Best Python code snippet using lisa_python
linhao_support.py
Source:linhao_support.py  
1import csv2import logging as logger3import os4from _csv import writer as csv_writer5from collections import defaultdict6import numpy as np7import scipy8from matplotlib import pyplot as plt9import imagepipe.raw_functions10import imagepipe.tools.helpers11import imagepipe.wrapped_functions12from imagepipe import core_functions as cf13from imagepipe.core_functions import generator_wrapper14@generator_wrapper(in_dims=(None, 2, 2, 2, 2, 1, 1, None, None, 2, 2), out_dims=(None,))15def linhao_gfp_render(name_pattern, proj_gfp, qual_gfp, cell_labels, average_gfp_pad, average_gfp_list,16                      predicted_average, std_err, upper_outliers, gfp_outliers, proj_mch,17                      save=False, directory_to_save_to='verification'):18    # To Consider: bind all the x/y axes together.19    plt.figure(figsize=(20, 15))20    plt.suptitle(name_pattern)21    main_ax = plt.subplot(241)22    plt.imshow(proj_gfp, interpolation='nearest')23    plt.contour(cell_labels > 0, [0.5], colors='w')24    plt.subplot(242, sharex=main_ax, sharey=main_ax)25    plt.imshow(np.log(proj_gfp + np.min(proj_gfp[proj_gfp > 0])), cmap='hot', interpolation='nearest')26    plt.contour(cell_labels > 0, [0.5], colors='w')27    plt.subplot(243, sharex=main_ax, sharey=main_ax)28    plt.imshow(qual_gfp, cmap='gray', interpolation='nearest')29    plt.contour(cell_labels > 0, [0.5], colors='w')30    ax = plt.subplot(244, sharex=main_ax, sharey=main_ax)31    plt.imshow(cell_labels, cmap=plt.cm.spectral, interpolation='nearest')32    unique = np.sort(np.unique(cell_labels))[1:]33    for i in unique:34        mask = cell_labels == i35        x, y = scipy.ndimage.measurements.center_of_mass(mask)36        ax.text(y-8, x+8, '%s' % i, fontsize=10)37    plt.subplot(245, sharex=main_ax, sharey=main_ax)38    plt.imshow(average_gfp_pad, cmap='hot', interpolation='nearest')39    plt.colorbar()40    plt.contour(cell_labels > 0, [0.5], colors='w')41    plt.subplot(246)42    plt.plot(np.sort(average_gfp_list), 'ko')43    plt.plot(predicted_average, 'r')44    plt.plot(predicted_average + std_err, 'g')45    plt.plot(predicted_average - std_err, 'g')46    ax = plt.subplot(247, sharex=main_ax, sharey=main_ax)47    plt.imshow(gfp_outliers, cmap=plt.cm.spectral, interpolation='nearest')48    unique = np.unique(gfp_outliers)49    for i in unique:50        mask = gfp_outliers == i51        x, y = scipy.ndimage.measurements.center_of_mass(mask)52        ax.text(y-8, x+8, '%s' % i, fontsize=10)53    plt.subplot(248, sharex=main_ax, sharey=main_ax)54    plt.imshow(proj_gfp, interpolation='nearest')55    plt.contour(cell_labels > 0, [0.5], colors='w')56    if not save:57        plt.show()58    else:59        name_puck = directory_to_save_to+'/'+'gfp_base-'+name_pattern+'.png'60        plt.savefig(name_puck)61        plt.close()62@generator_wrapper(in_dims=(None, 2, 2, 2, 2, 2, 2, 2, 2), out_dims=(None,))63def linhao_mch_render(name_pattern, proj_mCh, mitochondria, skeleton,64                      mito_classes, final_classes, cell_labels,65                      radius_mask, support_mask,66                      save=False, directory_to_save_to='verification'):67    # To Consider: bind all the x/y axes together.68    cell_binary = cell_labels > 069    plt.figure(figsize=(20.0, 15.0))70    plt.suptitle(name_pattern)71    plt.subplot(241)72    plt.imshow(proj_mCh, interpolation='nearest')73    plt.contour(cell_binary, [0.5], colors='k')74    plt.subplot(242)75    plt.imshow(np.log(proj_mCh + np.min(proj_mCh[proj_mCh > 0])), cmap='hot', interpolation='nearest')76    plt.contour(cell_binary, [0.5], colors='w')77    plt.subplot(243)78    plt.imshow(mitochondria, interpolation='nearest', cmap='gray')79    plt.subplot(244)80    plt.imshow(skeleton, interpolation='nearest', cmap=plt.cm.spectral)81    plt.contour(mitochondria, [0.5], colors='w')82    plt.subplot(245)83    plt.imshow(radius_mask, cmap='hot', interpolation='nearest')84    plt.contour(cell_binary, [0.5], colors='w')85    plt.subplot(246)86    plt.imshow(support_mask, cmap='hot', interpolation='nearest')87    plt.contour(cell_binary, [0.5], colors='w')88    plt.subplot(247)89    plt.imshow(mito_classes, interpolation='nearest', cmap='coolwarm')90    plt.contour(cell_binary, [0.5], colors='k')91    plt.subplot(248)92    plt.imshow(final_classes, interpolation='nearest', cmap='coolwarm')93    plt.contour(cell_binary, [0.5], colors='k')94    if not save:95        plt.show()96    else:97        name_puck = directory_to_save_to+'/'+'mCh_base-'+name_pattern+'.png'98        plt.savefig(name_puck)99        plt.close()100@generator_wrapper(in_dims=(None, 2, 2, 2, 2, 2, 2), out_dims=(None,))101def linhao_mqvi_render(name_pattern, mito_outlines, cell_labels,102                       projected_gfp, projected_mch,103                       gfp_mqvi, mch_mqvi,104                       save=False, directory_to_save_to='verification'):105    plt.figure(figsize=(26.0, 15.0))106    plt.suptitle(name_pattern)107    main_ax = plt.subplot(221)108    plt.title('mCherry')109    plt.imshow(np.log(projected_mch+np.min(projected_mch[projected_mch > 0])),110               interpolation='nearest', cmap='hot')111    plt.contour(mito_outlines, [0.5], colors='b')112    plt.contour(cell_labels, [0.5], colors='g')113    plt.subplot(222, sharex=main_ax, sharey=main_ax)114    plt.title('GFP')115    plt.imshow(np.log(projected_gfp+np.min(projected_gfp[projected_gfp > 0])),116               interpolation='nearest', cmap='hot')117    plt.contour(mito_outlines, [0.5], colors='b')118    plt.contour(cell_labels, [0.5], colors='g')119    plt.subplot(224, sharex=main_ax, sharey=main_ax)120    plt.title('GFP MQVI')121    plt.imshow(gfp_mqvi, interpolation='nearest', cmap='hot')122    plt.colorbar()123    plt.subplot(223, sharex=main_ax, sharey=main_ax)124    plt.title('mCherry MQVI')125    plt.imshow(mch_mqvi, interpolation='nearest', cmap='hot')126    plt.colorbar()127    if not save:128        plt.show()129    else:130        name_puck = directory_to_save_to+'/'+'mqvi-'+name_pattern+'.png'131        plt.savefig(name_puck)132        plt.close()133@generator_wrapper134def linhao_summarize(primary_namespace, output):135    with open(output, 'ab') as output_file:136        writer = csv_writer(output_file)137        namespace = primary_namespace['name pattern']138        tag_group = primary_namespace['group id']139        secondary_namespace = primary_namespace['per_cell']140        pre_puck = [namespace] + tag_group141        for key, value in secondary_namespace.iteritems():142            if key != '_pad':143                proper_puck = pre_puck+[key, value['gfp_mqvi'], value['mch_mqvi'], value['final_classification']]144                writer.writerow(proper_puck)145    return primary_namespace146@generator_wrapper147def linhao_secondary_summarize(primary_namespace, output):148    with open(output, 'ab') as output_file:149        writer = csv_writer(output_file)150        namespace = primary_namespace['name pattern']151        tag_group = primary_namespace['group id']152        total_cells = len(np.unique(primary_namespace['pre_cell_labels'])) - 1153        analyzed_cells = len(np.unique(primary_namespace['cell_labels'])) - 1154        writer.writerow([namespace] + tag_group + [total_cells, analyzed_cells])155    return primary_namespace156def Linhao_traverse(main_root,157                    matching_rule='c', matching_map=None):158    """159    Traverses the main_root directory, looking for all the '.tif/.TIF' files, performs name matching160    then iterates through the resulting matched dictironary.161    Matching assumption is that except for the matching keys, the names are identical162    :param main_root: folder from which will be traversed in depth163    :param matching_rule: name modification to type mapping. Currently '' for no matching, 'color' for colors164    :param matching_map: {'pattern in the file name': color channel number}165    :return:166    """167    matched_images = defaultdict(lambda: ['']*len(matching_map.keys()))168    tags_dict = defaultdict(lambda: [])169    if matching_rule:170        assert(matching_map is not None)171    for current_location, sub_directories, files in os.walk(main_root):172        if files:173            for img in files:174                if ('.TIF' in img or '.tif' in img) and '_thumb_' not in img and 'w' in img:175                    prefix = imagepipe.raw_functions.split_and_trim(current_location, main_root)176                    img_codename = img.split(' ')[0].split('_')177                    color = matching_map[img_codename[-1]]178                    name_pattern = ' - '.join(prefix + img_codename[:-1])179                    matched_images[name_pattern][color] = os.path.join(current_location, img)180                    time_stamp = prefix[-1]181                    if time_stamp == 'HS':182                        time = 0183                    elif 'HS' in time_stamp:184                        time = -30185                    else:186                        time = time_stamp[3:-3]  # int(time_stamp[3:-3])187                    tags_dict[name_pattern] = []188                    tags_dict[name_pattern].append(time)  # time in the times series189                    _date = prefix[0][:8]190                    tags_dict[name_pattern].append("%s-%s-%s" % (_date[:2], _date[2:4], _date[4:]))191                    tags_dict[name_pattern].append(prefix[-2])192    delset = []193    for name_pattern, (color_set) in matched_images.iteritems():194        # debugger.logger.debug(color_set)195        if any([color == '' for color in color_set]):196            logger.info('in %s, colorset is broken:', name_pattern)197            for color_name in color_set:198                logger.info('\t %s', color_name)199            logger.info('name_pattern will be deleted')200            delset.append(name_pattern)201    for name_pattern in delset:202        del matched_images[name_pattern]203    user_input_about_new_csv_file = raw_input("To continue, enter 2. To start the process from the beginning, enter 1")204    if user_input_about_new_csv_file == '1':205        print "Preparing a new CSV file"206        initial_open = open("matched_images.csv", 'wb')207        # this is the file we need to save unless user provides input saying we can override it208        writer = csv.writer(initial_open, delimiter='\t')209        for key in matched_images:210            writer.writerow([key] + matched_images[key] + [0])211        initial_open.close()212    else:213        print "Continuing where the process last left off"214        file_exists = os.path.isfile("matched_images.tmp")215        if file_exists:216            open_tmp = open('matched_images.tmp', 'r')217            read_preexisting_tmp = csv.reader(open_tmp, delimiter = '\t')218            tmp_list = []219            for row in read_preexisting_tmp:220                tmp_list.append(row)221            open_tmp.close()222            open_csv = open('matched_images.csv', 'r')223            read_preexisting_csv = csv.reader(open_csv, delimiter = '\t')224            csv_list = []225            for row in read_preexisting_csv:226                csv_list.append(row)227            open_csv.close()228            for csv_row in csv_list:229                for tmp_row in tmp_list:230                    if csv_row[0] == tmp_row[0]:231                        csv_row[3] = tmp_row[3]232            open_csv_write = open('matched_images.csv', 'wb')233            override_csv = csv.writer(open_csv_write, delimiter='\t')234            for new_csv_row in csv_list:235                override_csv.writerow(new_csv_row)236    open_updated_csv_to_read = open('matched_images.csv', 'rb')237    csv_reader = csv.reader(open_updated_csv_to_read, delimiter='\t')238    open_tmp_to_write = open("matched_images.tmp", 'wb')239    writer_check_tmp = csv.writer(open_tmp_to_write, delimiter='\t')240    for row in csv_reader:241        name_pattern = row[0]242        color_set = [row[1], row[2]]243        if row[3] == 1:244            writer_check_tmp.writerow(row)245            continue246        channels = []247        for color in color_set:248            channels.append(imagepipe.tools.helpers.tiff_stack_2_np_arr(color))249        print "name pattern", name_pattern250        print 'channels', channels251        yield name_pattern, tags_dict[name_pattern], channels252        row[3] = 1...traversals.py
Source:traversals.py  
1import logging2import os3import csv4from collections import defaultdict5import numpy as np6from imagepipe.raw_functions import split_and_trim7from imagepipe.tools.helpers import tiff_stack_2_np_arr8import sys9logger = logging.getLogger('Default Debug Logger')10logger.setLevel(logging.DEBUG)11fh = logging.FileHandler('debug_log.log')12fh.setLevel(logging.DEBUG)13# create console handler with a higher log level14ch = logging.StreamHandler()15ch.setLevel(logging.DEBUG)16# create formatter and add it to the handlers17formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')18fh.setFormatter(formatter)19ch.setFormatter(formatter)20# add the handlers to the logger21logger.addHandler(fh)22logger.addHandler(ch)23def name_channels(stack_group_generator, channel_names):24    """25    Assigns names to the channel for the future processing and bundles them together26    :param stack_group_generator: generator returning image stack,27    :param channel_names:28    :return:29    """30    for name_pattern, group_ids, channels in stack_group_generator:31        print 'name pattern', name_pattern32        print 'group ids', group_ids33        print 'channels', [chan.shape for chan in channels]34        group_dict = {'name pattern': name_pattern,35                      'group id': group_ids}36        group_dict['channel list'] = channel_names37        for chan_name, chan in zip(channel_names, channels):38            group_dict[chan_name] = chan39        yield group_dict40def color_based_traversal(main_root, coding_separator='.', group_anchor=1):41    """42    Traverses the main root directory pulling the data from the images. different layers are assumed43    to be different colors, images are assumed to be 2D.44    :param main_root:45    :param coding_separator: character used to derived codename of the image46    :param group_anchor: position where the group information is stored47    :return:48    """49    matched_images = []50    for current_location, sub_directories, files in os.walk(main_root):51        if files:52            for img in files:53                if ('.TIF' in img or '.tif' in img) and '_thumb_' not in img:54                    prefix = split_and_trim(current_location, main_root)55                    # print current_location, main_root56                    # print prefix57                    # print "test"58                    # sys.exit()59                    img_codename = img[:-4].split(coding_separator)60                    print img, img_codename61                    name_pattern = ' - '.join(prefix + img_codename)62                    # group_by = img_codename[0].split('rpe')[1].split('dapi')[0].strip()63                    group_by = img_codename[group_anchor]64                    matched_images.append((name_pattern, group_by, os.path.join(current_location, img)))65    for name_pattern, group_by, image_location in matched_images:66        stack = tiff_stack_2_np_arr(image_location)67        # print stack.shape68        # stack = np.rollaxis(stack[0, :, :, :], 2)  # on the data where channels have not been split into z stacks69        channels = np.split(stack, stack.shape[0])70        channels = [chan[0, :, :] for chan in channels]71        # print "channel debug"72        # for channel in channels:73            # print '>', channel.shape, channel.dtype74        yield name_pattern, group_by, channels75def z_stack_based_traversal(main_root, matching_rule='c', matching_map=None):76    """77    Traverses the main_root directory, looking for all the '.tif/.TIF' files, performs name matching78    then iterates through the resulting matched dictironary.79    Matching assumption is that except for the matching keys, the names are identical80    :param main_root: folder from which will be traversed in depth81    :param matching_rule: name modification to type mapping. Currently '' for no matching, 'color' for colors82    :param matching_map: {'pattern in the file name': color channel number}83    :return:84    """85    matched_images = defaultdict(lambda: ['']*len(matching_map.keys()))86    tags_dict = defaultdict(lambda: [])87    if matching_rule:88        assert(matching_map is not None)89    for current_location, sub_directories, files in os.walk(main_root):90        if files:91            for img in files:92                if ('.TIF' in img or '.tif' in img) and '_thumb_' not in img and 'w' in img:93                    prefix = split_and_trim(current_location, main_root)94                    img_codename = img.split(' ')[0].split('_')95                    color = matching_map[img_codename[-1]]96                    name_pattern = ' - '.join(prefix + img_codename[:-1])97                    matched_images[name_pattern][color] = os.path.join(current_location, img)98                    time_stamp = prefix[-1]99                    if time_stamp == 'HS':100                        time = 0101                    elif 'HS' in time_stamp:102                        time = -30103                    else:104                        time = time_stamp[3:-3]  # int(time_stamp[3:-3])105                    tags_dict[name_pattern] = []106                    tags_dict[name_pattern].append(time)  # time in the times series107                    _date = prefix[0][:8]108                    tags_dict[name_pattern].append("%s-%s-%s" % (_date[:2], _date[2:4], _date[4:]))109                    tags_dict[name_pattern].append(prefix[-2])110    delset = []111    for name_pattern, (color_set) in matched_images.iteritems():112        # debugger.logger.debug(color_set)113        if any([color == '' for color in color_set]):114            logger.info('in %s, colorset is broken:', name_pattern)115            for color_name in color_set:116                logger.info('\t %s', color_name)117            logger.info('name_pattern will be deleted')118            delset.append(name_pattern)119    for name_pattern in delset:120        del matched_images[name_pattern]121    user_input_about_new_csv_file = raw_input("To continue, enter 2. To start the process from the beginning, enter 1")122    if user_input_about_new_csv_file == '1':123        print "Preparing a new CSV file"124        initial_open = open("matched_images.csv", 'wb')125        # this is the file we need to save unless user provides input saying we can override it126        writer = csv.writer(initial_open, delimiter='\t')127        for key in matched_images:128            writer.writerow([key] + matched_images[key] + [0])129        initial_open.close()130    else:131        print "Continuing where the process last left off"132        file_exists = os.path.isfile("matched_images.tmp")133        if file_exists:134            open_tmp = open('matched_images.tmp', 'r')135            read_preexisting_tmp = csv.reader(open_tmp, delimiter = '\t')136            tmp_list = []137            for row in read_preexisting_tmp:138                tmp_list.append(row)139            open_tmp.close()140            open_csv = open('matched_images.csv', 'r')141            read_preexisting_csv = csv.reader(open_csv, delimiter = '\t')142            csv_list = []143            for row in read_preexisting_csv:144                csv_list.append(row)145            open_csv.close()146            for csv_row in csv_list:147                for tmp_row in tmp_list:148                    if csv_row[0] == tmp_row[0]:149                        csv_row[3] = tmp_row[3]150            open_csv_write = open('matched_images.csv', 'wb')151            override_csv = csv.writer(open_csv_write, delimiter='\t')152            for new_csv_row in csv_list:153                override_csv.writerow(new_csv_row)154    open_updated_csv_to_read = open('matched_images.csv', 'rb')155    csv_reader = csv.reader(open_updated_csv_to_read, delimiter='\t')156    open_tmp_to_write = open("matched_images.tmp", 'wb')157    writer_check_tmp = csv.writer(open_tmp_to_write, delimiter='\t')158    for row in csv_reader:159        name_pattern = row[0]160        color_set = [row[1], row[2]]161        if row[3] == 1:162            writer_check_tmp.writerow(row)163            continue164        channels = []165        for color in color_set:166            channels.append(tiff_stack_2_np_arr(color))167        print "name pattern", name_pattern168        print 'channels', channels169        yield name_pattern, tags_dict[name_pattern], channels170        row[3] = 1...main.py
Source:main.py  
1import re2import csv3with open("phonebook_raw.csv", 'r', encoding='utf-8') as f:4    rows = csv.reader(f, delimiter=",")5    contacts_list = list(rows)6    new_list = []7def names_moving():8    name_pattern = r'([Ð-Я])'9    name_substitution = r' \1'10    for column in contacts_list[1:]:11        line = column[0] + column[1] + column[2]12        if len((re.sub(name_pattern, name_substitution, line).split())) == 3:13            column[0] = re.sub(name_pattern, name_substitution, line).split()[0]14            column[1] = re.sub(name_pattern, name_substitution, line).split()[1]15            column[2] = re.sub(name_pattern, name_substitution, line).split()[2]16        elif len((re.sub(name_pattern, name_substitution, line).split())) == 2:17            column[0] = re.sub(name_pattern, name_substitution, line).split()[0]18            column[1] = re.sub(name_pattern, name_substitution, line).split()[1]19            column[2] = ''20        elif len((re.sub(name_pattern, name_substitution, line).split())) == 1:21            column[0] = re.sub(name_pattern, name_substitution, line).split()[0]22            column[1] = ''23            column[2] = ''24    return25def phone_number_formatting():26    phone_pattern = re.compile(27        r'(\+7|8)?\s*\(?(\d{3})\)?\s*\D?(\d{3})[-\s+]?(\d{2})-?(\d{2})((\s)?\(?(доб.)?\s?(\d+)\)?)?')28    phone_substitution = r'+7(\2)\3-\4-\5\7\8\9'29    for column in contacts_list:30        column[5] = phone_pattern.sub(phone_substitution, column[5])31    return32def duplicates_combining():33    for column in contacts_list[1:]:34        first_name = column[0]35        last_name = column[1]36        for contact in contacts_list:37            new_first_name = contact[0]38            new_last_name = contact[1]39            if first_name == new_first_name and last_name == new_last_name:40                if column[2] == '':41                    column[2] = contact[2]42                if column[3] == '':43                    column[3] = contact[3]44                if column[4] == '':45                    column[4] = contact[4]46                if column[5] == '':47                    column[5] = contact[5]48                if column[6] == '':49                    column[6] = contact[6]50    for contact in contacts_list:51        if contact not in new_list:52            new_list.append(contact)53    return new_list54if __name__ == '__main__':55    names_moving()56    phone_number_formatting()57    duplicates_combining()58    with open("phonebook.csv", "w", encoding='utf-8') as f:59        datawriter = csv.writer(f, delimiter=',')...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
