@@ -552,3 +552,113 @@ def delete_flow(flow_id: int) -> bool:
552552 True if the deletion was successful. False otherwise.
553553 """
554554 return openml .utils ._delete_entity ("flow" , flow_id )
555+
556+
557+ def edit_flow (
558+ flow_id : int ,
559+ custom_name : str | None = None ,
560+ tags : list [str ] | None = None ,
561+ language : str | None = None ,
562+ description : str | None = None ,
563+ ) -> int :
564+ """Edits an OpenMLFlow.
565+
566+ In addition to providing the flow id of the flow to edit (through flow_id),
567+ you must specify a value for at least one of the optional function arguments,
568+ i.e. one value for a field to edit.
569+
570+ This function allows editing of non-critical fields only.
571+ Editable fields are: custom_name, tags, language, description.
572+
573+ Editing is allowed only for the owner of the flow.
574+
575+ Parameters
576+ ----------
577+ flow_id : int
578+ ID of the flow.
579+ custom_name : str, optional
580+ Custom name for the flow.
581+ tags : list[str], optional
582+ Tags to associate with the flow.
583+ language : str, optional
584+ Language in which the flow is described.
585+ Starts with 1 upper case letter, rest lower case, e.g. 'English'.
586+ description : str, optional
587+ Human-readable description of the flow.
588+
589+ Returns
590+ -------
591+ flow_id : int
592+ The ID of the edited flow.
593+
594+ Raises
595+ ------
596+ TypeError
597+ If flow_id is not an integer.
598+ ValueError
599+ If no fields are provided for editing.
600+ OpenMLServerException
601+ If the user is not authorized to edit the flow or if the flow doesn't exist.
602+
603+ Examples
604+ --------
605+ >>> import openml
606+ >>> # Edit the custom name of a flow
607+ >>> edited_flow_id = openml.flows.edit_flow(123, custom_name="My Custom Flow Name")
608+ >>>
609+ >>> # Edit multiple fields at once
610+ >>> edited_flow_id = openml.flows.edit_flow(
611+ ... 456,
612+ ... custom_name="Updated Flow",
613+ ... language="English",
614+ ... description="An updated description for this flow",
615+ ... tags=["machine-learning", "classification"]
616+ ... )
617+ """
618+ if not isinstance (flow_id , int ):
619+ raise TypeError (f"`flow_id` must be of type `int`, not { type (flow_id )} ." )
620+
621+ # Check if at least one field is provided for editing
622+ fields_to_edit = [custom_name , tags , language , description ]
623+ if all (field is None for field in fields_to_edit ):
624+ raise ValueError (
625+ "At least one field must be provided for editing. "
626+ "Available fields: custom_name, tags, language, description"
627+ )
628+
629+ # Compose flow edit parameters as XML
630+ form_data = {"flow_id" : flow_id } # type: openml._api_calls.DATA_TYPE
631+ xml = OrderedDict () # type: 'OrderedDict[str, OrderedDict]'
632+ xml ["oml:flow_edit_parameters" ] = OrderedDict ()
633+ xml ["oml:flow_edit_parameters" ]["@xmlns:oml" ] = "http://openml.org/openml"
634+ xml ["oml:flow_edit_parameters" ]["oml:custom_name" ] = custom_name
635+ xml ["oml:flow_edit_parameters" ]["oml:language" ] = language
636+ xml ["oml:flow_edit_parameters" ]["oml:description" ] = description
637+
638+ # Handle tags - convert list to comma-separated string if provided
639+ if tags is not None :
640+ if isinstance (tags , list ):
641+ xml ["oml:flow_edit_parameters" ]["oml:tag" ] = "," .join (tags )
642+ else :
643+ xml ["oml:flow_edit_parameters" ]["oml:tag" ] = str (tags )
644+ else :
645+ xml ["oml:flow_edit_parameters" ]["oml:tag" ] = None
646+
647+ # Remove None values from XML
648+ for key in list (xml ["oml:flow_edit_parameters" ]):
649+ if not xml ["oml:flow_edit_parameters" ][key ]:
650+ del xml ["oml:flow_edit_parameters" ][key ]
651+
652+ file_elements = {
653+ "edit_parameters" : ("description.xml" , xmltodict .unparse (xml )),
654+ } # type: openml._api_calls.FILE_ELEMENTS_TYPE
655+
656+ result_xml = openml ._api_calls ._perform_api_call (
657+ "flow/edit" ,
658+ "post" ,
659+ data = form_data ,
660+ file_elements = file_elements ,
661+ )
662+ result = xmltodict .parse (result_xml )
663+ edited_flow_id = result ["oml:flow_edit" ]["oml:id" ]
664+ return int (edited_flow_id )
0 commit comments