[27c943]: / pathflowai / __pycache__ / utils.cpython-36.pyc

Download this file

520 lines (464 with data), 26.3 kB

3

v└A]pŃ@sŠdZddlZddlmZddlmZddlmZddl	Z	ddl
mZddlj
ZddljZddlZddlZddljZddlmZmZde_ddlZddljZddl
mZddlZddlZddlZddlZddlZdd	lm Z dd
l!m"Z"ddl#Z#ddlZddl$j%Z&ddl'Z'ddl(Z(ddl(m)Z)ddl*Z*ddl+Z+ddl,Z-ddl,m.Z/d
däZ0ddddäZ1deddäZ2ddäZ3ddäZ4dfddäZ5ddäZ6gfdd äZ7ddgfd!d"äZ8d#d$äZ9ddgd%d&fd'd(äZ:d)d*äZ;gd+fd,d-äZ<d.d/äZ=dgd1d2äZ>d+gd0d3ddd4d5d6ddfd7d8äZ?d+gd0d3d9ddd4d5d6dfd:d;äZ@d+gd0d3d<fd=d>äZAd?d@äZBd9ejCâd6d3dgdhd5df	dBdCäZDdDdEäZEgfdFdGäZFdHdIäZGdidJdKäZHdLdMäZIdNdOäZJdjdPdQäZKdRdSäZLdkdTdUäZMdldVdWäZNdmdXdYäZOdnd\d]äZPd^d_äZQd`daäZRdbdcäZSdS)ozú
utils.py
=======================
General utilities that still need to be broken up into preprocessing, machine learning input preparation, and output submodules.
ÚN)┌
BeautifulSoup)┌Point)┌Polygon)┌join)┌Image┌	ImageDrawg _áB)┌Dataset)┌train_test_split)┌deepzoom)┌SafeDataLoadercCs*tj|â}tjdj|â|dŹ}|jâ|S)zÍLoad pandas dataframe from SQL, accessing particular patch size within SQL.

	Parameters
	----------
	sql_file:str
		SQL db.
	patch_size:int
		Patch size.

	Returns
	-------
	dataframe
		Patch level information.

	zselect * from "{}";)┌con)┌sqlite3┌connect┌pd┌read_sql┌format┌close)┌sql_file┌
patch_size┌conn┌dfęr˙A/Users/joshualevy/Documents/GitHub/PathFlowAI/pathflowai/utils.py┌load_sql_df*s
r┌replacecCs0tj|â}|jdâjt|â||dŹ|jâdS)z˝Write dataframe containing patch level information to SQL db.

	Parameters
	----------
	df:dataframe
		Dataframe containing patch information.
	sql_file:str
		SQL database.
	patch_size:int
		Size of patches.
	mode:str
		Replace or append.

	┌index)r┌	if_existsN)r
r┌	set_index┌to_sql┌strr)rrr┌moderrrr┌df2sql?s
r!ÚŔTFc
sťtj|â}tj|||ddŹëtłjâdëłjł\}ëççfddä}|ddâ}|jëtj	|ddŹët
jçççççfdd	ät|łsćdndâDâłd
Ź}	|	S)aôConvert SVS, TIF or TIFF to dask array.

	Parameters
	----------
	svs_file:str
		Image file.
	tile_size:int
		Size of chunk to be read in.
	overlap:int
		Do not modify, overlap between neighboring tiles.
	remove_last:bool
		Remove last tile because it has a custom size.
	allow_unknown_chunksizes: bool
		Allow different chunk sizes, more flexible, but slowdown.

	Returns
	-------
	dask.array
		Dask Array.

	>>> arr=svs2dask_array(svs_file, tile_size=1000, overlap=0, remove_last=True, allow_unknown_chunksizes=False)
	>>> arr2=arr.compute()
	>>> arr3=to_pil(cv2.resize(arr2, dsize=(1440,700), interpolation=cv2.INTER_CUBIC))
	>>> arr3.save(test_image_name)T)┌	tile_size┌overlapZlimit_boundsÚcstjłjł||fââjdâS)Nr%rÚ)r%rr&)┌np┌array┌get_tile┌	transpose)┌i┌j)┌gen┌	max_levelrr┌<lambda>ssz svs2dask_array.<locals>.<lambda>r)Zpurecs>g|]6ëtjçççfddätłłs&dndâDâłddŹĹqS)cs"g|]}tjłł|âłtjâĹqSr)┌daZfrom_delayedr'┌uint8)┌.0r,)┌
dask_get_tiler+┌sample_tile_shaperr˙
<listcomp>wsz-svs2dask_array.<locals>.<listcomp>.<listcomp>rr%)┌allow_unknown_chunksizes┌axis)r0┌concatenate┌range)r2)r6r3┌	n_tiles_y┌remove_lastr4)r+rr5wsz"svs2dask_array.<locals>.<listcomp>)r6)
┌	openslideZ
open_slider
ZDeepZoomGenerator┌lenZlevel_dimensionsZlevel_tiles┌shape┌dask┌delayedr0r8r9)
┌svs_filer#r$r;r6┌imgZ	n_tiles_xr)Zsample_tile┌arrr)r6r3r-r.r:r;r4r┌svs2dask_arrayVs

4rDcCs,t|dj|ââ}t|â}tj||jââ|S)z┘Convert SVS, TIF, TIFF to NPY.

	Parameters
	----------
	input_dir:str
		Output file dir.
	basename:str
		Basename of output file
	svs_file:str
		SVS, TIF, TIFF file input.

	Returns
	-------
	str
		NPY output file.
	z{}.npy)rrrDr'┌save┌compute)┌	input_dir┌basenamerAZnpy_out_filerCrrr┌img2npy_zsrIcCs"tj|â}tjtj|âdâ|jfS)zčLoad SVS, TIF, TIFF

	Parameters
	----------
	svs_file:type
		Description of parameter `svs_file`.

	Returns
	-------
	type
		Description of returned object.
	r%r)r%r)r┌openr'r*r(┌size)rA┌imrrr┌
load_imageÉs
rMcCsÜ|ddůddůdf|ddůddůdf|ddůddůdf}}}d|d|d|}d|}drľ|rľ|jâ}tj|djâ|djâgâj}|S)	a<Create a gray scale intensity mask. This will be changed soon to support other thresholding QC methods.

	Parameters
	----------
	arr:dask.array
		Dask array containing image information.
	img_size:int
		Deprecated.
	sparse:bool
		Deprecated

	Returns
	-------
	dask.array
		Intensity, grayscale array over image.

	Nrr%r&g┼Ć1w-!Ë?gbX9┤╚Ô?g╔vżč/Ż?gÓo@)┌nonzeror'r(rF┌T)rC┌img_size┌sparse┌r┌b┌g┌gray┌maskrrr┌create_purple_maskásF rWcCstj|t|âfddŹS)zÂOptional add intensity mask to the dask array.

	Parameters
	----------
	arr:dask.array
		Image data.

	Returns
	-------
	array
		Image data with intensity added as forth channel.

	r)r7)r'r8rW)rCrrr┌add_purple_maskŻsrXcs$çfddä|Dâëçfddä|DâS)a2Convert annotation xml to shapely objects and store in dictionary.

	Parameters
	----------
	xml_file:str
		XML file containing annotations.
	img_size:int
		Deprecated.
	annotations:list
		Annotations to look for in xml export.

	Returns
	-------
	dict
		Dictionary with annotation-shapely object pairs.

	csi|]}tł|ddŹ|ôqS)F)┌annotation_name┌
return_coords)┌parse_coord_return_boxes)r2┌
annotation)┌xml_filerr˙
<dictcomp>▀sz3create_sparse_annotation_arrays.<locals>.<dictcomp>csi|]}ł||ôqSrr)r2r\)┌interior_points_dictrrr^Ósr)r]rP┌annotationsr)r_r]r┌create_sparse_annotation_arrays═sracCsl|jdârt|ânt|dddŹ}|jddů}i}|dk	rN|jt|||dŹâ|dk	rd|jd|iâ||fS)	a~Load SVS-like image (including NPY), segmentation/classification annotations, generate dask array and dictionary of annotations.

	Parameters
	----------
	svs_file:str
		Image file
	xml_file:str
		Annotation file.
	npy_mask:array
		Numpy segmentation mask.
	annotations:list
		List of annotations in xml.

	Returns
	-------
	array
		Dask array of image.
	dict
		Annotation masks.

	z.npyiŔr)r#r$Nr&)r`r`)┌endswith┌npy2darDr>┌updatera)rAr]┌npy_maskr`rCrP┌masksrrr┌load_process_imageÔs rgcCs*|jdâj|ddŹtj|t|dââdS)z Saves dask array image, dictionary of annotations to zarr and pickle respectively.

	Parameters
	----------
	arr:array
		Image.
	masks:dict
		Dictionary of annotation shapes.
	out_zarr:str
		Zarr output file for image.
	out_pkl:str
		Pickle output file.
	r1T)┌	overwrite┌wbN)┌astypeZto_zarr┌pickle┌dumprJ)rCrf┌out_zarr┌out_pklrrr┌save_datasetsrozoutput_zarr.zarrz
output.pklcCs$t||||â\}}t||||âdS)aÇRun preprocessing pipeline. Store image into zarr format, segmentations maintain as npy, and xml annotations as pickle.

	Parameters
	----------
	svs_file:str
		Input image file.
	xml_file:str
		Input annotation file.
	npy_mask:str
		NPY segmentation mask.
	annotations:list
		List of annotations.
	out_zarr:str
		Output zarr for image.
	out_pkl:str
		Output pickle for annotations.
	N)rgro)rAr]rer`rmrnrCrfrrr┌run_preprocessing_pipelinesrpcCsśddlm}ddlm}tj|â}t|â}|dk}|d
dk|ddk@|ddk@dk}	||	tj||fâd	Ź}
d||
dk|dk@<tj	||j
ââ|S)
axFixes segmentation masks to reduce coarse annotations over empty regions.

	Parameters
	----------
	mask_file:str
		NPY segmentation mask.
	dask_img_array_file:str
		Dask image file.
	out_npy:str
		Output numpy file.
	n_neighbors:int
		Number nearest neighbors for dilation and erosion of mask from background to not background.

	Returns
	-------
	str
		Output numpy file.

	r)┌binary_opening)┌Clientg.gi@r%r&)┌	structure).r).r%).r&)Zdask_image.ndmorphrq┌dask.distributedrrr0┌	from_zarrrc┌onesr'rErF)Z	mask_fileZdask_img_array_fileZout_npy┌n_neighborsrqrrZdask_img_arrayrVZis_tissue_maskZis_tissue_mask_img┌openingrrr┌adjust_mask3s
(ryz./cCsrtj|ddŹ|jdâdjdâd}t||â\}}tjt|dj|ââ|âtj	|t
t|dj|ââd	âd
d
ŹdS)z¨Store images into npy format and store annotations into pickle dictionary.

	Parameters
	----------
	svs_file:str
		Image file.
	xml_file:str
		Annotations file.
	annotations:list
		List of annotations in image.
	output_dir:str
		Output directory.
	T)┌exist_ok˙/r%┌.rz{}.npyz{}.pklri)┌protocolNÚ    r~)┌os┌makedirs┌splitrgr'rErrrkrlrJ)rAr]r`┌
output_dirrHrCrfrrr┌process_svsVs
râcCstj|âtjt|dââfS)z╬Load ZARR image and annotations pickle.

	Parameters
	----------
	in_zarr:str
		Input image.
	in_pkl:str
		Input annotations.

	Returns
	-------
	dask.array
		Image array.
	dict
		Annotations dictionary.

	┌rb)r0rurk┌loadrJ)Zin_zarrZin_pklrrr┌load_datasetlsrćšÓ?cCs2t||â||||ů|||ůf|kjâ|kS)z,Deprecated, computes whether patch is valid.)┌print┌mean)┌xs┌ysr┌purple_mask┌intensity_threshold┌	thresholdrrr┌is_valid_patchÇs
rĆÚÓgY@g┌c"s4ddlëddl}ddlm}
ddlj}ddl}ddlm}ddlm	}ddl
m}d}tł|ł|ł|ł|||	|
|dŹ}ÉyPt
t|d	jłâât|d
jłâââ\}ëdłkr╩dët|	s┬t|d
jłâân|	â}ndët|â}t|jdâ}t|jdâ}t|łłâ}t|łłâ}x`łD]X}y$ł|Ér6|ł|âgngł|<Wn,ł|Ér`|ł|âgngł|<YnXÉqWtjçççççfddä|t|dât|dââDâdddddgłÉs╝łntddätłâDââdŹë|
Ér˘łjddůddůfëg}xNłddgjjâD]8\ë	ë
|j|ł	ł	łůł
ł
łůf|kjâ|kâÉqWtj|j|Äâ}t dâłj!|ë|
ÉsĂg}łÉr˛dłj!ddůdf<xŽłddgjjâD]L\ë	ë
ł	ł}ł
ł} |j|j"|ł	|ůł
| ůfdłdgłdŹdâÉqáWnBx@łddgjjâD]*\ë	ë
|jçççç	ç
fddäłDââÉqWtjłj|Äâ}t dâłÉrd|tj#łłâ}|łjddůddůf<tłjddůddůfâëtj$ççfdd äâtj%łjdââłj!ddůdf<Wnft&k
Ér.}!zHt |!â|d!d7<|d!|kÉrt&d"âént d#ât'f|ÄWYdd}!~!XnXłS)$aFinal step of preprocessing pipeline. Break up image into patches, include if not background and of a certain intensity, find area of each annotation type in patch, spatial information, image ID and dump data to SQL table.

	Parameters
	----------
	basename:str
		Patient ID.
	input_dir:str
		Input directory.
	annotations:list
		List of annotations to record, these can be different tissue types, must correspond with XML labels.
	threshold:float
		Value between 0 and 1 that indicates the minimum amount of patch that musn't be background for inclusion.
	patch_size:int
		Patch size of patches; this will become one of the tables.
	generate_finetune_segmentation:bool
		Deprecated.
	target_class:int
		Number of segmentation classes desired, from 0th class to target_class-1 will be annotated in SQL.
	intensity_threshold:float
		Value between 0 and 255 that represents minimum intensity to not include as background. Will be modified with new transforms.
	target_threshold:float
		Deprecated.
	adj_mask:str
		Adjusted mask if performed binary opening operations in previous preprocessing step.
	basic_preprocess:bool
		Do not store patch level information.
	tries:int
		Number of tries in case there is a Dask timeout, run again.

	Returns
	-------
	dataframe
		Patch information.

	rN)┌	dataframe)┌unary_union)┌MultiPolygon)┌productÚ)rHrGr`rÄr┌generate_finetune_segmentation┌target_classrŹ┌target_threshold┌adj_mask┌basic_preprocess┌triesz{}.zarrz{}_mask.pklr`Tz{}_mask.npyFr%cs<g|]4\}}ł|ł|łłdgdgłr,łntłâĹqS)ZNAg)r=)r2r+r,)r`rHr┌segmentationrśrrr5Ňsz-extract_patch_information.<locals>.<listcomp>┌ID┌x┌yrr\cSsg|]}t|âĹqSr)r)r2r+rrrr5Ňs)┌columnszValid Patches Complete┌segment)r9Zbinscs&g|]}łjtâłłgłł|âĹqSr)r@┌is_coords_in_box)r2r\)r?rfrrŐrőrrr5ŰszArea Info CompleteÚcsłłj|ddůfjjâS)NrĄ)┌iloc┌values┌argmax)r+)┌annot┌
patch_inforrr/÷sz+extract_patch_information.<locals>.<lambda>rťz&Exceeded past maximum number of tries.zRestarting preprocessing again.)(r?┌timerĺ┌
dask.arrayr(┌multiprocessingZshapely.opsrô┌shapely.geometryrö┌	itertoolsrĽ┌dictrćrrrcrW┌floatr>┌intr┌	DataFramer9┌listrąrŽ┌tolist┌appendrër'rFrł┌loc┌	histogramZfloat16┌	vectorize┌arange┌	Exception┌extract_patch_information)"rHrGr`rÄrrŚrśrŹrÖrÜrŤrťr¬┌ddr0rČrôrörĽZ	max_triesZkargsrCZsegmentation_maskrîZx_maxZy_maxZx_stepsZy_stepsr\Z
valid_patchesZ	area_infoZxfZyf┌er)rĘr`rHr?rfręrrŁrśrŐrőrr╗ćsx'
 	&
$,d4
:&6
r╗z
patch_info.dbc
CsHt|||||||||	|
|dŹ}tj|â}
|jt|â|
ddŹ|
jâdS)a▄Short summary.

	Parameters
	----------
	basename:str
		Patient ID.
	input_dir:str
		Input directory.
	annotations:list
		List of annotations to record, these can be different tissue types, must correspond with XML labels.
	threshold:float
		Value between 0 and 1 that indicates the minimum amount of patch that musn't be background for inclusion.
	patch_size:int
		Patch size of patches; this will become one of the tables.
	out_db:str
		Output SQL database.
	generate_finetune_segmentation:bool
		Deprecated.
	target_class:int
		Number of segmentation classes desired, from 0th class to target_class-1 will be annotated in SQL.
	intensity_threshold:float
		Value between 0 and 255 that represents minimum intensity to not include as background. Will be modified with new transforms.
	target_threshold:float
		Deprecated.
	adj_mask:str
		Adjusted mask if performed binary opening operations in previous preprocessing step.
	basic_preprocess:bool
		Do not store patch level information.
	)rŚrśrŹrÖrÜrŤrÁ)rrN)r╗r
rrrr)rHrGr`rÄr┌out_dbrŚrśrŹrÖrÜrŤręrrrr┌generate_patch_pipelines
r┐zpatch_info.pklcs4tjççççfddä|DââjddŹ}|j|âdS)zDeprecated.csg|]}t|łłłłâĹqSr)r╗)r2rH)r`rGrrÄrrr5*sz'save_all_patch_info.<locals>.<listcomp>T)┌dropN)r┌concatZreset_index┌	to_pickle)Z	basenamesrGr`rÄrZ
output_pklrr)r`rGrrÄr┌save_all_patch_info(s&r├c	CsĘtjj|ârtj|â}nîtj|â}tjdj|â|dŹ}|j	â|dj
â}tj|dgdŹ}t|â\}}t|â\}}d|d<d|d<d|d<tj
|||gâ}|j|â|S)	aOCreate dataframe that splits slides into training validation and test.

	Parameters
	----------
	train_val_test_pkl:str
		Pickle for training validation and test slides.
	input_info_db:str
		Patch information SQL database.
	patch_size:int
		Patch size looking to access.

	Returns
	-------
	dataframe
		Train test validation splits.

	zselect * from "{}";)rr×)ríZtrain┌set┌val┌test)r┌path┌existsrZread_pickler
rrrr┌uniquer▓r	r┴r┬)	Ztrain_val_test_pkl┌
input_info_dbrZIDsrrZ	IDs_trainZIDs_testZIDs_valrrr┌create_train_val_test0s

r╦r%c	s└tj|â}	tjdj|â|	dŹë|	jâłjâëłjtj	łd|j
âë|Ésö|rłłdjâjâ}
t
|
âdkrćtłjddůddůfâ}
n║t|â}
t|âtdâkrĘ|g}n
tj|â}|j|âłtj	łd|âëx|
D]}d	ł|<qÍWxZ|j
D]P}
|Érłd|
kł|@d	kn
łd|
k}|jârţ|j|
|
fjłj||
f<qţWtjçfd
däâtjłjdââłd
<d
tłâkÉr╝|d	kÉr╝łjłd
|kën(d	łd<|dkÉr╝łjłt|â|këłS)a7Modify the patch information to get ready for deep learning, incorporate whole slide labels if needed.

	Parameters
	----------
	input_info_db:str
		SQL DB file.
	slide_labels:dataframe
		Dataframe with whole slide labels.
	pos_annotation_class:str
		Tissue/annotation label to label with whole slide image label, if not supplied, any slide's patches receive the whole slide label.
	patch_size:int
		Patch size.
	segmentation:bool
		Segmentation?
	other_annotations:list
		Other annotations to access from patch information.
	target_segmentation_class:int
		Segmentation class to threshold.
	target_threshold:float
		Include patch if patch has target area greater than this.
	classify_annotations:bool
		Classifying annotations for pretraining, or final model?

	Returns
	-------
	dataframe
		Modified patch information.

	zselect * from "{}";)rr×r\r%NrĄrĹgcsłj|łj|dS)Nr\)rą)r+)rrrr/ősz#modify_patch_info.<locals>.<lambda>r┌area┌target)r
rrrrrZdrop_duplicatesrÂr'┌isinrr╔r┤r=r│rą┌type┌copy┌deepcopy┌extend┌sumrŽrŞr╣r>r)r╩Zslide_labelsZpos_annotation_classrrŁZother_annotations┌target_segmentation_classrÖZclassify_annotationsr┌targetsZincluded_annotationsr═ZslideZ
slide_boolr)rr┌modify_patch_infoSs:



*&
rÍcCstjtj|ddŹâS)zÉNumpy to dask array.

	Parameters
	----------
	npy_file:str
		Input npy file.

	Returns
	-------
	dask.array
		Converted numpy array to dask.

	zr+)Z	mmap_mode)r0Z
from_arrayr'rů)Znpy_filerrrrcösrccCsBi}x8|D]0}yt||ddŹ||<Wq
g||<Yq
Xq
W|S)zDeprecated.F)rZ)r[)r]rPr`Zinterior_point_dictr\rrr┌grab_interior_pointsĄs
rÎcCsDtjd|dâ}x"|D]}tj|âj|dddŹqWtj|âjâ}|S)zDeprecated.┌Lrr%)┌outline┌fill)r┌newrZDraw┌polygonr'r(rN)rPZpolygonsrBr▄rVrrr┌boxes2interior«s

rŢcCsŐg}tt|âdâ}xr|jdâD]d}|d|krxR|jdâD]D}ddä|jdâDâ}|rb|j|âq:|jttj|âjtjâââq:WqW|S)a Get list of shapely objects for each annotation in the XML object.

	Parameters
	----------
	xml_file:str
		Annotation file.
	annotation_name:str
		Name of xml annotation.
	return_coords:bool
		Just return list of coords over shapes.

	Returns
	-------
	list
		List of shapely objects.

	┌htmlr\Zpartofgroup┌coordinatescSsg|]}|d|dfĹqS)rčrár)r2┌
coordinaterrrr5Đsz,parse_coord_return_boxes.<locals>.<listcomp>rÓ)	rrJZfindAllrÁrr'r(rjr░)r]rYrZ┌boxesZxml_datar\r▀┌coordsrrrr[Ěs$r[cCsZt|ârRttjddgddgddgddggâ||â}|j|dâjt|jâ}nd}|S)zŔGet area of annotation in patch.

	Parameters
	----------
	coords:array
		X,Y coordinates of patch.
	patch_size:int
		Patch size.
	boxes:list
		Shapely objects for annotations.

	Returns
	-------
	float
		Area of annotation type.

	rr%g)r=rr'r(┌intersectionr╠r░)rÔrrß┌pointsr╠rrrrúěs
.rúcsççfddäłDâS)z■Find if image intersects with annotations.

	Parameters
	----------
	image_coord_dict:dict
		Dictionary of patches.
	boxes:list
		Shapely annotation shapes.

	Returns
	-------
	dict
		Dictionary of whether image intersects with any of the annotations.

	cs*i|]"ëtttççfddäłâââłôqS)cs|jłłâS)N)Z
intersects)rč)┌image┌image_coord_dictrrr/sz.is_image_in_boxes.<locals>.<dictcomp>.<lambda>)┌anyr│┌map)r2)rßrŠ)rňrr^sz%is_image_in_boxes.<locals>.<dictcomp>r)rŠrßr)rßrŠr┌is_image_in_boxes˝srÚcsçfddä|DâS)┌
Deprecatedcsi|]}t|łâ|ôqSr)┌image2coords)r2rň)┌output_pointrrr^sz%images2coord_dict.<locals>.<dictcomp>r)┌imagesrýr)rýr┌images2coord_dictsrţcCstjt|dââS)rŕz*.jpg)┌globr)┌	image_dirrrr┌
dir2imagessr˝cCs&t||â}t|â}t|â}t||dŹS)rŕ)rŠrß)r[r˝rţrÚ)r­r]r\rßrÝZ
coord_dictrrr┌return_image_in_boxes_dicts
r˛cCsPtj|jdâdjdâdjdâddůâjtâjâ\}}}}t|||||dŹS)	zDeprecated.r{r%r|r┌_N)┌nx┌ny┌xi┌yirýr~)r'r(rürjr▒r┤┌return_image_coord)Z
image_filerýr˘r§r¸r÷rrrrŰs>rŰcst|||âëçfddäłDâS)rŕcsg|]}ł|r|ĹqSrr)r2rB)┌image_in_boxes_dictrrr5sz!retain_images.<locals>.<listcomp>)r˛)r­r]r\r)r¨r┌
retain_imagessr˙Ú
Úcs×|
r@tjłłgâtj||||d||||	dgâStj||||||gâëtjçççfddäddg|dg||	gd|	ggDââ}t|âSdS)rŕr&cs,g|]$}tjłłgâłtj|âjâĹqSr)r'r(r┤)r2Z	new_point)┌static_point┌xc┌ycrrr5"sz&return_image_coord.<locals>.<listcomp>rN)r'r(r)r˘r§ZxlZylr÷r¸r■r ZdimxZdimyrýrńr)rřr■r rr°s
<4r°cCst|âdkrdj|ÄS|S)z(Fixes illegitimate basename, deprecated.rŘz{}0{})r=r)rHrrr┌fix_name%s
rcCsöxÄtjt|dââD]z}|jdâd}|d|jdâů||jdâdů}}t|âdkrt|dj||f×Äâ}t||âtjdj||âd	d
ŹqWdS)zFixes basenames, deprecated.┌*r{r%Nr|rŘz{}0{}{}zmv {} {}T)┌shellr~)	r´rrü┌rfindr=rrł┌
subprocess┌call)Zfile_dir┌filenamerH┌suffixZnew_filenamerrr┌	fix_names+s&
rcCsŞtj|jddůâ}xnt|jdâD]\}|j|}|d}|d}|d}|d}	||df}
|
j}|
||||	ů|||	ůf<q$Wtj|d|jd	âůd
dŹtj||j	tj
ââdS)
aConvert segmentation predictions from model to numpy masks.

	Parameters
	----------
	y_pred:list
		List of patch segmentation masks
	patch_info:dataframe
		Patch information from DB.
	segmentation_map:array
		Existing segmentation mask.
	npy_output:str
		Output npy file.
	r&Nrr×rčrár.r{T)rzÚ■   )r'┌zerosr>r9rąrrÇrrErjr1)Zy_predręZsegmentation_mapZ
npy_outputr+Zpatch_info_ir×rŐrőrZ
predictionZ
pred_shaperrr┌segmentation_predictions2npy8s
 r)r)r"rTF)NT)rçr~)rĹF)F)rĹ)F)rĹ)rrrűrűrrrŘrŘrÉrÉF)T┌__doc__┌numpyr'Zbs4rrşrZshapely.geometry.polygonrr´┌os.pathrZplotly.graph_objsZ
graph_objs┌goZplotly.offlineZoffline┌py┌pandasrZscipy.sparserQZsps┌PILrr┌MAX_IMAGE_PIXELSrrr
ZtorchZtorch.utils.datarZsklearn.model_selectionr	Z	pysnooperrźr(r0r?r<r
rkrđZ
nonechucks┌ncrZ
DataLoaderrr!rDrIrMrWrXrargrorpryrârćrĆr╗r┐r├r╦r▓rÍrcrÎrŢr[rúrÚrţr˝r˛rŰr˙r°rrrrrrr┌<module>sé






$
%#
 } %# A
	
!