Skip to content

For issue 1575: Eliminating Manual Class Registration in Unitxt, replaced by Import Paths #1713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
74 changes: 22 additions & 52 deletions docs/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import PythonLexer
from unitxt.artifact import Artifact
from unitxt.artifact import (
get_class_or_function_from_artifact_type,
get_module_class,
)
from unitxt.text_utils import print_dict_as_python
from unitxt.utils import load_json

Expand Down Expand Up @@ -50,8 +53,8 @@ def imports_to_syntax_highlighted_html(subtypes: List[str])-> str:
return ""
module_to_class_names = defaultdict(list)
for subtype in subtypes:
subtype_class = Artifact._class_register.get(subtype)
module_to_class_names[subtype_class.__module__].append(subtype_class.__name__)
(module, class_name) = get_module_class(subtype)
module_to_class_names[module].append(class_name)

imports_txt = ""
for modu in sorted(module_to_class_names.keys()):
Expand Down Expand Up @@ -101,31 +104,6 @@ def custom_walk(top):
yield entry


def all_subtypes_of_artifact(artifact):
if (
artifact is None
or isinstance(artifact, str)
or isinstance(artifact, bool)
or isinstance(artifact, int)
or isinstance(artifact, float)
):
return []
if isinstance(artifact, list):
to_return = []
for art in artifact:
to_return.extend(all_subtypes_of_artifact(art))
return to_return
# artifact is a dict
to_return = []
for key, value in artifact.items():
if isinstance(value, str):
if key == "__type__":
to_return.append(value)
else:
to_return.extend(all_subtypes_of_artifact(value))
return to_return


def get_all_type_elements(nested_dict):
type_elements = set()

Expand All @@ -148,19 +126,18 @@ def recursive_search(d):

@lru_cache(maxsize=None)
def artifact_type_to_link(artifact_type):
artifact_class = Artifact._class_register.get(artifact_type)
type_class_name = artifact_class.__name__
artifact_class_id = f"{artifact_class.__module__}.{type_class_name}"
return f'<a class="reference internal" href="../{artifact_class.__module__}.html#{artifact_class_id}" title="{artifact_class_id}"><code class="xref py py-class docutils literal notranslate"><span class="pre">{type_class_name}</span></code></a>'
artifact_module, artifact_class_name = get_module_class(artifact_type)
return f'<a class="reference internal" href="../{artifact_module}.html#{artifact_module}.{artifact_class_name}" title="{artifact_module}.{artifact_class_name}"><code class="xref py py-class docutils literal notranslate"><span class="pre">{artifact_class_name}</span></code></a>'


# flake8: noqa: C901


def make_content(artifact, label, all_labels):
artifact_type = artifact["__type__"]
artifact_class = Artifact._class_register.get(artifact_type)
type_class_name = artifact_class.__name__
catalog_id = label.replace("catalog.", "")
artifact_type = artifact["__type__"] #qualified class name
artifact_class = get_class_or_function_from_artifact_type(artifact_type)

catalog_id = label.replace("catalog.", "")
result = ""

if "__description__" in artifact and artifact["__description__"] is not None:
Expand Down Expand Up @@ -203,23 +180,16 @@ def make_content(artifact, label, all_labels):
)

for type_name in type_elements:
# source = f'<span class="nt">__type__</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">{type_name}</span>'
source = f'<span class="n">__type__{type_name}</span><span class="p">'
target = artifact_type_to_link(type_name)
html_for_dict = html_for_dict.replace(
source,
f'<span class="n" STYLE="font-size:108%">{target}</span><span class="p">'
# '<span class="nt">&quot;type&quot;</span><span class="p">:</span><span class="w"> </span>'
# + target,
)

pattern = r'(<span class="nt">)&quot;(.*?)&quot;(</span>)'
artifact_module, artifact_class_name = get_module_class(type_name)
pattern = re.compile(f'<span class="n">__type__(.*?)<span class="n">{artifact_class_name}</span>')
repl = '<span class="n" STYLE="font-size:108%">'+artifact_type_to_link(type_name)+"</span>"
html_for_dict = pattern.sub(repl, html_for_dict)

# pattern = r'(<span class="nt">)&quot;(.*?)&quot;(</span>)'
# Replacement function
html_for_dict = re.sub(pattern, r"\1\2\3", html_for_dict)
# html_for_dict = re.sub(pattern, r"\1\2\3", html_for_dict)

subtypes = all_subtypes_of_artifact(artifact)
subtypes = list(set(subtypes))
subtypes = type_elements
subtypes.remove(artifact_type) # this was already documented
html_for_imports = imports_to_syntax_highlighted_html(subtypes)

Expand All @@ -235,13 +205,13 @@ def make_content(artifact, label, all_labels):
result += " " + html_for_element + "\n"

if artifact_class.__doc__:
explanation_str = f"Explanation about `{type_class_name}`"
explanation_str = f"Explanation about `{artifact_class.__name__}`"
result += f"\n{explanation_str}\n"
result += "+" * len(explanation_str) + "\n\n"
result += artifact_class.__doc__ + "\n"

for subtype in subtypes:
subtype_class = Artifact._class_register.get(subtype)
subtype_class = get_class_or_function_from_artifact_type(subtype)
subtype_class_name = subtype_class.__name__
if subtype_class.__doc__:
explanation_str = f"Explanation about `{subtype_class_name}`"
Expand Down
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def autodoc_skip_member(app, what, name, obj, would_skip, options):
class_name = obj.__qualname__.split(".")[0]
if (
class_name
and Artifact.is_registered_class_name(class_name)
and class_name != name
):
return True
Expand Down
5 changes: 4 additions & 1 deletion prepare/metrics/custom_f1.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,7 @@ class NERWithoutClassReporting(NER):
global_target=global_target,
)

add_to_catalog(metric, "metrics.ner", overwrite=True)
if __name__ == "__main__" or __name__ == "custom_f1":
# because a class is defined in this module, need to not add_to_catalog just for importing that module in order to retrieve the defined class
# and need to prepare for case when this module is run directly from python (__main__) or, for example, from test_preparation (custom_f1)
add_to_catalog(metric, "metrics.ner", overwrite=True)
Loading