polo.utils package¶
Submodules¶
polo.utils.dialog_utils module¶
-
polo.utils.dialog_utils.
make_message_box
(message, parent=None, icon=1, buttons=1024, connected_function=None)[source]¶ General helper function to create popup message box dialogs to convey situational information to the user.
- Parameters
message (str) – The message to display to the user.
parent (QDialog, optional) – Parent dialog, defaults to None
icon (int, optional) – QMessageBox icon to display along with the message, defaults to QtWidgets.QMessageBox.Information
buttons (set, optional) – Buttons to include in the message box, defaults to QtWidgets.QMessageBox.Ok
connected_function (func, optional) – Function to connect to buttonClicked event, defaults to None
- Returns
The message box.
- Return type
QMessageBox
polo.utils.exceptions module¶
-
exception
polo.utils.exceptions.
EmptyDirectoryError
[source]¶ Bases:
Exception
Raised when attempting to load images from an empty directory
-
exception
polo.utils.exceptions.
EmptyRunNameError
[source]¶ Bases:
Exception
Raised when reading in an HWI directory but it does not contain number of images corresponding to number of wells.
-
exception
polo.utils.exceptions.
ForbiddenImageTypeError
[source]¶ Bases:
Exception
Raised when user attempts to load in an image that is not in the allowed image types.
-
exception
polo.utils.exceptions.
IncompletePlateError
[source]¶ Bases:
Exception
Raised when reading in an HWI directory but it does not contain number of images corresponding to number of wells.
-
exception
polo.utils.exceptions.
InvalidCocktailFile
[source]¶ Bases:
Exception
Raised when user attempts to load in a file containing well cocktail information that does not confrom to existing formating standards.
polo.utils.ftp_utils module¶
polo.utils.io_utils module¶
-
class
polo.utils.io_utils.
BarTender
(cocktail_dir, cocktail_meta)[source]¶ Bases:
object
Class for organizing and accessing
Menu
data.- Parameters
cocktail_dir (str or Path) – Directory containing cocktail menu csv filepaths
cocktail_meta (Path or str) – Path to cocktail metadata file which describes the contents of each cocktail menu csv file
Cocktail metadata file should be a csv file with the following headers ordered from top to bottom. Each header name is followed by a description.
File Name: Name of cocktail menu file Dates Used: Range of dates the cocktail menu was used (m/d/y-m/d/y) Plate Number Screen Type: 'm' for membrane screens, 's' for soluble screens
Adds
Menu
objects to thepolo.utils.io_utils.BarTender.menus
attribute by reading the cocktail csv files included in theCOCKTAIL_DATA_PATH
directory.
-
static
date_range_parser
(date_range_string)[source]¶ Utility function for converting the date ranges in the cocktail metadata csv file to datetime objects using the
datetime_converter()
method.Date ranges should have the format
'start date - end date' If the date range is for the most recent cocktail menu then there will not be an end date and the format will be 'start date - '
- Parameters
date_range_string (str) – string to pull dates out of
- Returns
tuple of datetime objects, start date and end date
- Return type
tuple
-
static
datetime_converter
(date_string)[source]¶ General utility function for converting strings to datetime objects. Attempts to convert the string by trying a couple of datetime formats that are common in cocktail menu files and other locations in the HWI file universe Polo runs across.
- Parameters
date_string (str) – string to convert to datetime
- Returns
datetime object
- Return type
datetime
Uses the basename of a
Menu
file path to return aMenu
object. Useful for retrieving menus based on the text of comboBoxes since when menus are displayed to the user only the basename is used.
Get a
Menu
instance who’s usage dates include the date passed through the date argument and matches the screen type passed through the type_ argument.Screen types can either be ‘s’ for ‘soluble’ screens or ‘m’ for membrane screens.
- Parameters
date (datetime) – Date to search menus with
type (str) – Type of screen to return (soluble or membrane)
- Returns
menu matching the given date and type
- Return type
Returns a
Menu
instance by its file path, which is used as the key for accessing the menus attribute normally.- Parameters
path (str) – file path of a menu csv file
- Returns
Menu instance that is mapped to given path
- Return type
Returns all
Menu
instances of a given screen type.‘s’ for soluble screens and ‘m’ for membrane screens. No other characters should be passed to type_.
- Parameters
type (str (max length 1)) – Key for type of screen to return
- Returns
list of menus of that screen type
- Return type
list
-
class
polo.utils.io_utils.
CocktailMenuReader
(menu_file_path, delim=',')[source]¶ Bases:
object
CocktailMenuReader instances should be used to read a csv file containing a collection of cocktail screens. The csv file should contain cocktail related formulations and assign each cocktail to a specific well in the screening plate.
cocktail_menu = 'path/to/my/csv' my_reader = CocktailMenuReader(cocktail_menu) csv_cocktails = my_reader.read_menu_file() # csv_cocktails now holds list of Cocktail objects read from # cocktail_menu
- Parameters
menu_file (str or Path) – Path to cocktail menu file to read. Should be csv formated
delim (str, optional) – Seperator for menu_file; really should not need to be changed, defaults to ‘,’
-
cocktail_map
= {0: 'well_assignment', 1: 'number', 2: 'commercial_code', 8: 'pH'}¶
-
formula_pos
= 4¶
Read the contents of cocktail menu csv file. The
Menu
file path is read from theMenu.menu_file_path
attribute. The first two lines of all the cocktail menu files included in Polo are header lines and so the reader will skip the first two lines before actually reading in any data. Each row is converted to aCocktail
object and then added to a dictionary based on theCocktail
instance’s well assignment.- Returns
Dictionary of Cocktail instances. Key value is the Cocktail’s well assignment in the screening plate (base 1).
- Return type
dict
-
classmethod
set_cocktail_map
(map)[source]¶ Classmethod to edit the
cocktail_map
. Thecocktail_map
describes where theCocktail
level information is stored in a given cocktail row in the csv file. It is a dictionary that maps specific indices in a row toCocktail
attributes.The default cocktail_map dictionary is below.
>>> cocktail_map = { 0: 'well_assignment', 1: 'number', 8: 'pH', 2: 'commercial_code' }
This tells instances of CocktailMenuReader to look at index 0 of a row for the well_assignment attribute of the Cocktail class, index 1 for the number attribute of the Cocktail class, etc.
- Parameters
map (dict) – Dictionary mapping csv row indices to Cocktail object attributes
-
classmethod
set_formula_pos
(pos)[source]¶ Classmethod to change the
CocktailMenuReader.formula_pos
attribute. Theformula_pos
describes the location (base 0) of the chemical formula in a row of a cocktail menu file csv. For some reason, HWI cocktail menu files will only have one chemical formula per row (cocktail) no matter the number of reagents that composite that cocktail. This is why its location is represented using an int instead of a dict.Generally,
CocktailMenuReader.formula_pos
should not be changed without a very good reason as the position of the chemical formula is consistent across all HWI cocktail menu files.- Parameters
pos (int) – Index where chemical formula can be found
-
class
polo.utils.io_utils.
HtmlWriter
(run, **kwargs)[source]¶ Bases:
polo.utils.io_utils.RunSerializer
-
static
make_template
(template_path)[source]¶ Given a path to an html file to serve as a jinja2 template, read the file and create a new template object.
- Parameters
template_path – Path to the jinja2 template file.
-
write_complete_run
(output_path, encode_images=True)[source]¶ Create an HTML report from a
Run
orHWIRun
instance.- Parameters
output_path (str or Path) – Path to write html file to.
encode_images (bool, optional) – Write images as base64 directly to the html file, defaults to True. Greatly increases the file size but means that report will still contain images even if the originals are deleted or removed.
- Returns
Path to html report if write succeeds, Exception otherwise.
- Return type
str or Exception
-
write_grid_screen
(output_path, plate_list, well_number, x_reagent, y_reagent, well_volume, run_name=None)[source]¶ Write the contents of optimization grid screen to an html file.
- Parameters
output_path (str) – Path to html file
plate_list (list) – list containing grid screen data
well_number (int or str) – well number of hit screen is created from
x_reagent (Reagent) – reagent varied in x direction
y_reagent (Reagent) – Reagent varied in y direction
well_volume (int or str) – Volume of well used in screen
run_name (str, optional) – name of run, defaults to None
-
static
-
class
polo.utils.io_utils.
JsonWriter
(run, output_path)[source]¶ Bases:
polo.utils.io_utils.RunSerializer
Small class that can be used to serialize a run to a json formated file.
- Parameters
-
static
json_encoder
(obj)[source]¶ General purpose json encoder for encoding python objects. Very similar to the encoder function
json_encoder()
except does not include class and module information in the returned dictionary. If the object cannot be converted to a dictionary it is returned as a string.- Parameters
obj (obj) – Object to convert to dictionary
- Returns
dict or str
- Return type
dict or str
-
class
polo.utils.io_utils.
Menu
(path, start_date, end_date, type_, cocktails={})[source]¶ Bases:
object
-
property
cocktails
¶
-
property
-
class
polo.utils.io_utils.
MsoReader
(mso_path)[source]¶ Bases:
object
The MsoReader class is used to parse the content of mso formated files and apply the image classifications stored in these files to runs in Polo.
-
classify_images_from_mso_file
(images)[source]¶ Applies the image classifications stored in an mso file to a collection of
Image
objects. Allows for some degree of compatability with MarcoscopeJ and for users who have stored their image classifications in the mso format. Additionally, human classification backups are saved as mso files when Polo is closed as they take up much less space than xtal files.- Parameters
images (list) – List of images to apply classifications to
- Returns
List of images with mso classifications applied, or Exception if this fails
- Return type
list or Exception
-
static
read_mso_classification
(mso_classification)[source]¶ Helper method to read and convert image classifications in a mso file to MARCO image classifications. The exact conversion scheme is layed out in the
REV_MSO_DICT
constant. Additionally, MarcoscopeJ will allow images to have multiple classifications by assigning multiple image codes seperated by “-“. If this is the case Polo takes the classification corresponding to the mso code with the highest value.- Parameters
mso_classification (str) – Mso image classification code
- Returns
MARCO classification if code can be decoded, None otherwise
- Return type
str or None
-
-
class
polo.utils.io_utils.
MsoWriter
(run, output_path)[source]¶ Bases:
polo.utils.io_utils.RunSerializer
-
property
first_line
¶ Create the first line of the mso file.
- Returns
List to write as the first line of the mso file.
- Return type
list
-
get_cocktail_csv_data
()[source]¶ Reads and returns the cocktail csv data assigned to the
cocktail_menu
attribute of the MsoWriter’srun
attribute.- Returns
List of lists containing cocktail csv data.
- Return type
list
-
mso_version
= 'msoversion2'¶
-
static
row_formater
(cocktail_row)[source]¶ Format a cocktail row as read from a cocktail csv file to an mso file row. Main change is appending empty strings to the cocktail row so list ends up always having a length of 17. This is important because the image classification code always occurs at the 18th item in an mso file row.
- Parameters
cocktail_row (list) – Cocktail row as read from cocktail csv file.
- Returns
Cocktail row reformated for mso writing.
- Return type
list
-
write_mso_file
(use_marco_classifications=False)[source]¶ Writes an mso formated file for use with MarcoScopeJ based on the images and classifications of the
Run
instance referenced by the MsoWriter’srun
attribute.- Parameters
use_marco_classifications (bool, optional) – Include the MARCO classification in the mso file instead of human classifications, defaults to False
- Returns
True if file is written successfully, False otherwise.
- Return type
bool
-
property
-
class
polo.utils.io_utils.
PptxWriter
(output_path)[source]¶ Bases:
object
Use for creating pptx presentation slides from
Run
orHWIRun
instances.- Parameters
output_path (str or Path) – Path to write pptx file to.
included_attributes (dict, optional) – [description], defaults to {}
image_types (set or list, optional) – Images included in the presentation must have a classification in this set, defaults to None
human (bool, optional) – Use human classification as the image classification, defaults to False
marco (bool, optional) – Use the MARCO classification as the image classification, defaults to False
favorite (bool, optional) – Only include images marked as favorite, defaults to False
-
_add_image_to_slide
(image, slide, left, top, height)[source]¶ Private method for adding images to a slide. If the
Image
does not have a file written on the local machine as can be the case with saved runs who’s image data only exists in their xtal files this method will write a temporary image file to the PoloTEMP_DIR
which then should be deleted after the presentaton file is written.- Parameters
image (Image) – Image to add to the slide
slide (slide) – Slide to add the image to
left (float) – Left cordinate location of the image in inches
top (float) – Top cordinate location of the image in inches
height (float) – Height of the image in inches
- Returns
[]
- Return type
[type]
-
_add_multi_image_slide
(slide, images, labeler)[source]¶ Private method for adding a slide that will have multiple images.
- Parameters
slide (slide) – Slide to add the images to
images (list) – Images to add to the slide
labeler (func) – Function to use to label the individual images
- Returns
slide with images added
- Return type
slide
-
_add_multi_spectrum_slide
(images, well_number)[source]¶ Create a slide to show a all spectrums a well has been imaged in.
- Parameters
images (list) – Images to include on the slide
well_number (int) – Well number to use in the slide title
- Returns
New slide
- Return type
slide
-
_add_new_slide
(template=5)[source]¶ Add a new slide to the presentation referenced by the
_presentation
attribute.- Parameters
template (int, optional) – Slide template integer identifier, defaults to 5. See the python-pptx package for more details on template integers
- Returns
Presentation with the new slide added
- Return type
Presentation
-
_add_single_image_slide
(image, title, metadata=None, img_scaler=0.65)[source]¶ General helper method for adding a slide with a single image to a presentation.
- Parameters
image (Image) – Image to add to the slide
title (str) – Title to use for the slide
metadata (str, optional) – Additional information to write to the slide, defaults to None
img_scaler (float, optional) – Scaler to apply to size of the image, defaults to 0.65 ,should be between 0 and 1. 1 is full sized image.
- Returns
The new slide with Image added
- Return type
slide
-
_add_text_to_slide
(slide, text, left, top, width, height, rotation=0, font_size=14)[source]¶ Helper method to add text to a slide
- Parameters
slide (slide) – Slide to add text to
text (str) – Text to add to the slide
left (float) – Left cordinate location of the text in inches
top (float) – Top cordinate location of the text in inches
width (float) – Width of the text in inches
height (float) – Height of the text in inches
rotation (int, optional) – Rotation to apply to the text in degrees, defaults to 0
font_size (int, optional) – Font size of text, defaults to 14
- Returns
Slide with text added
- Return type
slide
-
_add_timeline_slide
(images, well_number)[source]¶ Create a timeline (time resolved) slide that show the progression of a sample in a particular well.
- Parameters
images (list) – List of images to include in the slide
well_number (int) – Well number to use in the title of the slide
- Returns
New slide
- Return type
slide
-
_delete_temp_images
()[source]¶ Delete an temporary images used to create the pptx presentation.
- Returns
True, if images are removed successfully, Exception otherwise.
- Return type
bool or Exception
-
add_cocktail_slide
(well, cocktail)[source]¶ Add slide with details on
Cocktail
information.- Parameters
well (int) – Well number to use in slide title
cocktail (Cocktail) – Cocktail to write as a slide
-
add_table_to_slide
(slide, data, left, top)[source]¶ General helper method for adding a table to a slide.
- Parameters
slide (slide) – Slide to add the table to
data (list) – List of lists that has the data to write to the table
left (float) – Left offset in inches to place to table
top (float) – Top cordinate for placing the table
- Returns
Slide with table added
- Return type
slide
-
make_presentation
(run, images, title, subtitle=None, cocktail_data=True, all_specs=False, all_dates=False)[source]¶ Create a pptx presentation file from a screening run instance. This should be the only public method in the class. All other methods are internal to actually creating the presentation file.
- Parameters
run (Run) – Run to create the presentation from
title (str) – Title for the presentation
subtitle (str, optional) – Subtitle for the presentation, defaults to None
cocktail_data (bool, optional) – If True include cocktail data on slide, defaults to True
all_specs (bool, optional) – If True include all spectrum image slide, defaults to False
all_dates (bool, optional) – If True include all dates slide, defaults to False
- Returns
True if presentation was created successfully, Exception otherwise
- Return type
bool or Exception
-
slide_title_formater
= 'Well Number {}'¶
-
class
polo.utils.io_utils.
RunCsvWriter
(run, output_path=None, **kwargs)[source]¶ Bases:
polo.utils.io_utils.RunSerializer
-
property
fieldnames
¶ Get the current fieldnames based on the data stored in the
run
attribute. Currently is somewhat expensive to call since it requires parsing all records inrun
in order to determine all the fieldnames that should be included in order to definitely avoid keyerrors later down the line.- Returns
List of fieldnames (headers) for the csv data
- Return type
list
-
get_csv_data
()[source]¶ Convert the
run
attribute to csv style data. Returns a tuple of headers and a list of dictionaries with each dictionary representing one row of csv data.- Returns
Tuple, list of headers and list of dicts
- Return type
tuple
-
classmethod
image_to_row
(image)[source]¶ Given an
Image
object, convert it into a list that could be easily written to a csv file.- Parameters
image (Image) –
Image
object to convert to list- Returns
List
- Return type
list
-
property
output_path
¶ Get the hidden attribute _output_path.
- Returns
Output path
- Return type
str
-
write_csv
()[source]¶ Write the
Run
object referenced by therun
attribute as a csv file to the location specified by theoutput_path
attribute.- Returns
True, if csv file content was written successfully, return error thrown otherwise.
- Return type
Bool or Exception
-
property
-
class
polo.utils.io_utils.
RunDeserializer
(xtal_path)[source]¶ Bases:
object
-
static
clean_base64_string
(string, out_fmt=<class 'bytes'>)[source]¶ Image instances may contain byte strings that store their actual crystallization image encoded as base64. Previously, these byte strings were written directly into the json file as strings causing the b’ byte string identifier to be written along with the actual base64 data. This method removes those artifacts if they are present and returns a clean byte string with only the actual base64 data.
- Parameters
string (str) – a string to interrogate
- Returns
byte string with non-data artifacts removed
- Return type
bytes
-
static
dict_to_obj
(d)[source]¶ Opposite of the obj_to_dict method in XtalWriter class, this method takes a dictionary instance that has been previously serialized and attempts to convert it back into an object instance. Used as the object_hook argument when calling json.loads to read xtal files.
- Parameters
d (dict) – dictionary to convert back to object
- Returns
an object
- Return type
object
-
xtal_header_reader
(xtal_file_io)[source]¶ Reads the header section of an open xtal file. Should always be called before reading the json content of an xtal file. Note than xtal files must always have a line of equal signs before the json content even if there is no header content otherwise this method will read one line into the json content causing the json reader to throw an error.
- Parameters
xtal_file_io (TextIoWrapper) – xtal file currently being read
- Returns
xtal header contents
- Return type
list
-
static
-
class
polo.utils.io_utils.
RunImporter
(target_path)[source]¶ Bases:
object
-
_target_is_valid
()[source]¶ Private method to check if a the file or directory referenced by
target_path
is valid for import.- Returns
True if could be imported, False otherwise
- Return type
bool
-
allowed_filetypes
= {'.rar', '.xtal'}¶
-
static
crack_open_a_rar_one
(rar_path)[source]¶ Unrar a directory of images
- Parameters
rar_path (str or Path) – Path to rar archive file
- Returns
Unrar result
- Return type
str, exception or int
-
create_import_thread
()[source]¶ Creates a
polo.threads.thread.QuickThread
with function based on the type of file that is to be imported.- Returns
QuickThread
- Return type
-
-
class
polo.utils.io_utils.
RunLinker
[source]¶ Bases:
object
Class to hold methods relating to linking runs either by date or by spectrum.
-
static
link_runs_by_spectrum
(runs)[source]¶ Link a collection of
HWIRun
instances together by spectrum. All non-visibleHWIRun
instances are linked together in a monodirectional circular linked list. Each visibleHWIRun
instance will then point to the same non-visible run through theiralt_spectrum
attribute as a way to access the non-visible linked list.- Parameters
runs (list) – List of runs to link together
- Returns
List of runs linked by spectrum
- Return type
list
-
static
link_visualizer
(self, run)[source]¶ Create a text representation of the linked list structure between runs for debugging.
- Parameters
run (Run) – Run to build representation from
- Returns
Text representation of the linked list
- Return type
str
-
static
the_big_link
(runs)[source]¶ Wrapper method to do all the linking required for a collection of runs. First calls
unlink_runs_completely()
to separate any existing links so things do not get tangled. Then callslink_runs_by_date()
andlink_runs_by_spectrum()
.- Parameters
runs (list) – List of runs to link
- Returns
List of runs with links made
- Return type
list
-
static
-
class
polo.utils.io_utils.
RunSerializer
(run=None)[source]¶ Bases:
object
-
classmethod
make_thread
(job_function, **kwargs)[source]¶ Creates a new
QuickThread
object. The job function is the function the thread will execute and and arguments that the job function requires should be passed has keyword arguments. These are stored as a dictionary in the new thread object until the thread is activated and they are passed as arguments.
-
classmethod
-
class
polo.utils.io_utils.
SceneExporter
(graphics_scene=None, file_path=None)[source]¶ Bases:
object
-
static
write_image
(scene, file_path)[source]¶ Write the contents of a
QGraphicsScene
to a png file.- Parameters
scene (QGraphicsScene) –
QGraphicsScene
to convert to image file.file_path (str) – Path to save image to.
- Returns
File path to saved image if successful, Exception otherwise.
- Return type
str or Exception
-
static
-
class
polo.utils.io_utils.
XtalToImages
(run, output_dir)[source]¶ Bases:
object
One of the features of Polo is the ability to save images, classifications and cocktail data all as one (xtal) file. This also means that users may be loading in their run data from xtal files but want some way to recover the images that are encoded (as base64) within the delicious onion like layers of the JSON formated xtal file. This class acomplishes this goal by intializing with a run that has base64 encoded image data and writes that data as jpegs to a user specified directory.
Currently under construction as of 12-14-2020
-
class
polo.utils.io_utils.
XtalWriter
(run, main_window, **kwargs)[source]¶ Bases:
polo.utils.io_utils.RunSerializer
-
static
clean_run_for_save
(run)[source]¶ Remove circular references from the run passed through the run argument to avoid issues when writing to json files.
-
file_ext
= '.xtal'¶
-
header_flag
= '<>'¶
-
header_line
= '{}{}:{}\n'¶
-
static
json_encoder
(obj)[source]¶ Use instead of the default json encoder when writing an xtal file. If the encoded object is from a module within Polo will include a module and class identifier so it can be more easily deserialized when loaded back into the program.
- Param
obj: An object to serialize to json.
- Returns
A dictionary or string version of the passed object
-
run_to_dict
()[source]¶ Create a json string from the run stored in the run attribute.
- Returns
Run instance serialized to json
- Return type
str
-
write_xtal_file
(output_path=None)[source]¶ Method to serialize run object to xtal file format.
- Parameters
output_path (str) – Xtal file path
- Returns
path to xtal file
- Return type
str
-
write_xtal_file_on_thread
(output_path)[source]¶ Wrapper method around
write_xtal_file()
that executes on aQuickThread
instance to prevent freezing the GUI when saving large xtal files- Parameters
output_path (str) – Path to xtal file
-
property
xtal_header
¶ Creates the header for an xtal file when called. Header lines are indicated as such by the string in the header_line constant, which should be ‘<>’. The last line of the header will be a row of equal signs and then the actual json content begins on the next line.
-
static
-
polo.utils.io_utils.
if_dir_not_exists_make
(parent_dir, child_dir=None)[source]¶ If only parent_dir is given attempts to make that directory. If parent and child are given tries to make a directory child_dir within parent dir.
polo.utils.math_utils module¶
polo.utils.unrar_utils module¶
-
polo.utils.unrar_utils.
test_for_working_unrar
(unrar_exe='unrar')[source]¶ Tests if a working unrar installation exists on the machine.
- Parameters
unrar_exe (Path or str, optional) – Path to unrar executable file, defaults to UNRAR_EXE
- Returns
True if working installation exists, False otherwise
- Return type
bool
-
polo.utils.unrar_utils.
unrar_archive
(rar_path, target_dir=None)[source]¶ De-compress a rar archive and return the path to the uncompressed archive if it exists. All unrar functions including this one are dependent of their being a working unrar installation. Unrar is included for both Windows and Mac operating systems but not for Linux.
- Parameters
rar_path (Path or str) – Path to rar archive file
target_dir (Path or str, optional) – Location to place the unrared file, defaults to None
- Returns
Path if unrar is successful, error code if unrar fails or Exception if
exception is raised in the unrar process. :rtype: Path, str or Exception