@@ -668,6 +668,51 @@ def _render_drug_entry(fields: list, index: int, result: dict) -> None:
668668 result [dose_field ["key" ]] = ddose
669669
670670
671+ def _polyp_output_block (
672+ pi : int , info_dict : dict , intervention : object , hist_dict : dict
673+ ) -> str :
674+ """
675+ Generate the code block for a polyp's information, intervention(s), and histology.
676+ Args:
677+ pi (int): The polyp index (1-based).
678+ info_dict (dict): The polyp information dictionary.
679+ intervention (object): The polyp intervention(s), can be dict or list of dicts.
680+ hist_dict (dict): The polyp histology dictionary.
681+ Returns:
682+ str: The generated code block.
683+ """
684+ code_lines = []
685+ code_lines .append (
686+ f"InvestigationDatasetCompletion(page).fill_polyp_x_information({ pretty_dict (info_dict )} , { pi } )"
687+ )
688+ # Intervention
689+ if isinstance (intervention , list ):
690+ if len (intervention ) > 1 :
691+ code_lines .append (
692+ f"InvestigationDatasetCompletion(page).fill_polyp_x_multiple_interventions({ pretty_list (intervention )} , { pi } )"
693+ )
694+ elif len (intervention ) == 1 and isinstance (intervention [0 ], dict ):
695+ code_lines .append (
696+ f"InvestigationDatasetCompletion(page).fill_polyp_x_intervention({ pretty_dict (intervention [0 ])} , { pi } )"
697+ )
698+ else :
699+ code_lines .append (f"# No intervention for polyp { pi } " )
700+ elif isinstance (intervention , dict ):
701+ code_lines .append (
702+ f"InvestigationDatasetCompletion(page).fill_polyp_x_intervention({ pretty_dict (intervention )} , { pi } )"
703+ )
704+ else :
705+ code_lines .append (f"# No intervention for polyp { pi } " )
706+ # Histology
707+ if not hist_dict :
708+ code_lines .append (f"# No histology for polyp { pi } " )
709+ else :
710+ code_lines .append (
711+ f"InvestigationDatasetCompletion(page).fill_polyp_x_histology({ pretty_dict (hist_dict )} , { pi } )"
712+ )
713+ return "\n " .join (code_lines )
714+
715+
671716def show_polyp_information_and_intervention_and_histology () -> None :
672717 """
673718 Show the Polyp Information, Intervention & Histology section, allowing multiple polyps and interventions.
@@ -682,12 +727,11 @@ def show_polyp_information_and_intervention_and_histology() -> None:
682727 # Collect all fields for import analysis
683728 all_fields = polyp_info_fields + polyp_intervention_fields + polyp_histology_fields
684729 enums = get_enums_used (all_fields )
685- if enums :
686- import_block = (
687- enum_import_string + new_indented_line_string .join (sorted (enums )) + "\n )\n "
688- )
689- else :
690- import_block = ""
730+ import_block = (
731+ enum_import_string + new_indented_line_string .join (sorted (enums )) + "\n )\n "
732+ if enums
733+ else ""
734+ )
691735
692736 num_polyps = st .number_input (
693737 "Number of polyps" , min_value = 0 , max_value = 20 , value = 1 , step = 1
@@ -698,55 +742,22 @@ def show_polyp_information_and_intervention_and_histology() -> None:
698742
699743 for pi in range (1 , num_polyps + 1 ):
700744 st .markdown (f"### Polyp { pi } " )
701- polyp_info = _render_polyp_info (polyp_info_fields , pi )
702- polyp_info_dicts [pi ] = polyp_info
745+ polyp_info_dicts [pi ] = _render_polyp_info (polyp_info_fields , pi )
703746 interventions = _render_interventions (polyp_intervention_fields , pi )
704- # If interventions is a list of length 1, store as dict, else as list
705- if isinstance (interventions , list ) and len (interventions ) == 1 :
706- polyp_interventions_dicts [pi ] = interventions [0 ]
707- else :
708- polyp_interventions_dicts [pi ] = interventions
709- polyp_histology = _render_histology (polyp_histology_fields , pi )
710- polyp_histology_dicts [pi ] = polyp_histology
747+ polyp_interventions_dicts [pi ] = (
748+ interventions [0 ]
749+ if isinstance (interventions , list ) and len (interventions ) == 1
750+ else interventions
751+ )
752+ polyp_histology_dicts [pi ] = _render_histology (polyp_histology_fields , pi )
711753
712754 st .markdown ("#### Output" )
713- output_blocks = []
714755 for pi in range (1 , num_polyps + 1 ):
715756 info_dict = polyp_info_dicts [pi ]
716757 hist_dict = polyp_histology_dicts [pi ]
717758 intervention = polyp_interventions_dicts [pi ]
718- code_lines = []
719- # Information
720- code_lines .append (
721- f"InvestigationDatasetCompletion(page).fill_polyp_x_information({ pretty_dict (info_dict )} , { pi } )"
722- )
723- # Intervention
724- if isinstance (intervention , list ):
725- if len (intervention ) > 1 :
726- code_lines .append (
727- f"InvestigationDatasetCompletion(page).fill_polyp_x_multiple_interventions({ pretty_list (intervention )} , { pi } )"
728- )
729- elif len (intervention ) == 1 and isinstance (intervention [0 ], dict ):
730- code_lines .append (
731- f"InvestigationDatasetCompletion(page).fill_polyp_x_intervention({ pretty_dict (intervention [0 ])} , { pi } )"
732- )
733- else :
734- code_lines .append (f"# No intervention for polyp { pi } " )
735- elif isinstance (intervention , dict ):
736- code_lines .append (
737- f"InvestigationDatasetCompletion(page).fill_polyp_x_intervention({ pretty_dict (intervention )} , { pi } )"
738- )
739- else :
740- code_lines .append (f"# No intervention for polyp { pi } " )
741- # Histology
742- if not hist_dict :
743- code_lines .append (f"# No histology for polyp { pi } " )
744- else :
745- code_lines .append (
746- f"InvestigationDatasetCompletion(page).fill_polyp_x_histology({ pretty_dict (hist_dict )} , { pi } )"
747- )
748- output_blocks .append ("\n " .join (code_lines ))
749- st .code ("\n " .join (code_lines ), language = "python" )
759+ code_block = _polyp_output_block (pi , info_dict , intervention , hist_dict )
760+ st .code (code_block , language = "python" )
750761 if import_block :
751762 st .code (import_block , language = "python" )
752763
0 commit comments