Last active
July 14, 2025 13:56
-
-
Save sarthakpati/042a6b566d9097d6db086a6fa0770cdb to your computer and use it in GitHub Desktop.
Write out any generic function interface as a yaml definition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import inspect | |
| import yaml | |
| from typing import Callable, Optional | |
| def export_function_interface_to_yaml(func: Callable, file_name: str) -> None: | |
| """ | |
| Export the interface of a Python function to a YAML file. | |
| Args: | |
| func (Callable): The function whose interface is to be exported. | |
| file_name (str): The name of the YAML file to create. | |
| """ | |
| # Get the function's signature | |
| signature = inspect.signature(func) | |
| func_info = {"function_name": func.__name__, "arguments": []} | |
| # Extract parameter details | |
| for name, param in signature.parameters.items(): | |
| arg_info = { | |
| "name": name, | |
| "type": ( | |
| str(param.annotation) | |
| if param.annotation != inspect.Parameter.empty | |
| else "Any" | |
| ), | |
| "default": ( | |
| param.default if param.default != inspect.Parameter.empty else None | |
| ), | |
| } | |
| func_info["arguments"].append(arg_info) | |
| # Write to YAML file | |
| with open(file_name, "w") as yaml_file: | |
| yaml.dump(func_info, yaml_file, default_flow_style=False) | |
| # Example usage | |
| def example_dcm2nifti( | |
| input_dir_with_read_access: str, output_dir_with_write_access: str | |
| ) -> None: | |
| """ | |
| Convert DICOM files in the input directory to NIfTI format and save them in the output directory. | |
| Args: | |
| input_dir_with_read_access (str): Path to the directory containing DICOM files. | |
| output_dir_with_write_access (str): Path to the directory where NIfTI files will be saved. | |
| """ | |
| pass | |
| def example_registration( | |
| input_nifti: str, | |
| reference_nifti: str, | |
| transform_file: str, | |
| registration_type: Optional[str] = "affine", | |
| interpolation_method: Optional[str] = "linear", | |
| ) -> None: | |
| """ | |
| Perform image registration between two NIfTI files. | |
| Args: | |
| input_nifti (str): Path to the input NIfTI file. | |
| reference_nifti (str): Path to the reference NIfTI file. | |
| transform_file (str): Path to the output transform file. | |
| registration_type (Optional[str]): Type of registration (e.g., "affine", "nonlinear"). Defaults to "affine". | |
| interpolation_method (Optional[str]): Interpolation method to use (e.g., "linear", | |
| """ | |
| pass | |
| export_function_interface_to_yaml(example_dcm2nifti, "interface_dcm2nifti.yaml") | |
| export_function_interface_to_yaml(example_registration, "interface_registration.yaml") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment