Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Implementation Plan: Documenting Media properties in the catalog (#1904)
Olga Bulat67cbf85d266c
diff --git a/documentation/projects/proposals/document_all_media_properties/20230426-implementation_plan_documenting_media_properties.md b/documentation/projects/proposals/document_all_media_properties/20230426-implementation_plan_documenting_media_properties.mdnew file mode 100644index 000000000..6cc8e03b5--- /dev/null+++ b/documentation/projects/proposals/document_all_media_properties/20230426-implementation_plan_documenting_media_properties.md@@ -0,0 +1,258 @@+# 2023-04-26 Implementation Plan: Document all media properties in the catalog++<!-- See the implementation plan guide for more information: https://github.com/WordPress/openverse/tree/19791f51c063d0979112f4b9f4eeace04c8cf5ff/docs/projects#implementation-plans-status-in-rfc -->+<!-- This template is exhaustive and may include sections which aren't relevant to your project. Feel free to remove any sections which would not be useful to have. -->++## Reviewers++<!-- Choose two people at your discretion who make sense to review this based on their existing expertise. Check in to make sure folks aren't currently reviewing more than one other proposal or RFC. -->++- [x] @AetherUnbound+- [x] @stacimc++## Project links++<!-- Enumerate any references to other documents/pages, including milestones and other plans -->++- [Project Thread](https://github.com/WordPress/openverse/issues/412)+- [Project Proposal](https://docs.openverse.org/projects/proposals/document_all_media_properties/20230307-project_proposal.html#description)++## Overview++<!-- A brief one or two sentence overview of the implementation being described. -->++A proposal to create a documentation page with description of the media+properties collected by the Catalog, and keep it updated using CI.++## Background++The catalog collects and transforms data from many different sources. The kind+of data collected, transformations, and the expected data shape have changed+over time. This plan will outline the creation of the documentation page about+the media properties in the catalog.++### User stories and user questions++- As a maintainer/contributor, I need to know the list of available properties+ for each media type in the catalog database so that I can better understand+ how different media types are represented within the catalog, can identify any+ gaps or inconsistencies in the properties of media types.+- As a contributor, I need to know what is the expected data type for the+ specific property so that I can correctly save the data in the catalog, or use+ the data in the API.+- As a contributor, I need to know how the data property is transformed and+ validated so that I can ensure that I am collecting the correct data, or can+ identify any potential improvements.+- As a contributor writing a provider API script, I need to know what+ information to save for each data property to collect from providers so that+ the script collects data that is complete, valid for attribution and improves+ search quality.+- As a contributor writing a provider script, I need to know which format to+ convert a media property to so that I can ensure that the data is valid and+ can be saved in the database.+- As a maintainer, I need to see if the discrepancy between the type of data+ property the API returns and what is described in the table (e.g., is `null`+ for a non-nullable property) is described in the documents so that I can open+ a new issue if necessary.+- As an API user, I need to know what exactly this media property is and what+ data points from the providers are used for it (e.g., does the "creator" refer+ to the person who created the object or took the picture?) so that I can use+ the data in my application.+- As a provider of openly-licensed media, I want to know what media properties+ Openverse collects so that I can change my API responses.++The information on data types, transformations, and validations will be+presented as a table and will be extracted from the code. Other information+about the data selection properties and data discrepancies will be written in a+separate markdown file as it is easier to read, write, and lint.++## Output++The result of this project will be a markdown page with a table of media+properties and a long-form description of each property.++The page will be published on+https://docs.openverse.org/meta/media_properties.html with the title of "Media+Properties".++The source will be located at `documentation/meta/media_properties.md`.++See sample at+[media_properties_documentation.md](samples/openverse_media_properties.md).++This is markdown file generated by the `just` script and contains the preamble,+the tables with the data properties, and the long-form description of the+properties from `media_properties.md`.++This file is used in the CI to compare the newly-generated file to the existing+one. If there are differences, the CI will fail and the maintainers will need to+update the documentation page.++## Prior art++The catalog has a DAG documentation generator that extracts the docstrings from+the DAGs and creates a page with the documentation. We can use the same approach+to document media properties. The DAG doc generator runs the code inside docker+to extract DAG information because it requires the project dependencies (i.e.+`airflow`) in order to parse the DAG. In this project, we can simplify and use+the `ast` module to parse static code and extract the docstrings to make the+checks faster. This will, however, mean that some of the settings will not be+picked up and will have to be updated during the generation. One example of this+is the `nullable` field in the `Column` class: it falls back to the value of+`not nullable` during the object initiation.++## Inputs++This section describes where the data for the documentation will be coming from.++### Existing Files++#### SQL DDL files++Located at `docker/upstream_db/`: `0003_openledger_image_schema.sql`,+`0004_openledger_image_view.sql`, `0006_openledger_audio_schema.sql`,+`0007_openledger_audio_view.sql`.++These files define the database schema for the catalog. This will be parsed by+the python script and converted into a Markdown table with the data properties.+In the beginning, we will use the materialized views (`image_view` and+`audio_view`) to get the list of properties and some properties that are not+available in the main database (`popularity`). After+[Popularity calculation optimizations (Matview refresh)](https://github.com/WordPress/openverse/issues/433)+is implemented, and we drop the materialized views, only the main tables+(`image` and `audio`) will be used.++A Python method will extract the information from the SQL files into+`media_properties` dictionary with the name of the media property and the+following values: SQL datatype, and SQL constraints. More data will be added to+this dictionary in the further steps. It will also be possible to add API+information into this dictionary in the API portion of this project.++```python+media_properties = {+ "title": {+ "sql_properties": {+ "type": "character varying",+ "sql_constraints": "(5000)",+ },+ }+}+```++#### `columns.py`++Located in `catalog/dags/common/storage/columns.py`.++This file contains the `Column` class that defines the validation and+transformation rules for the media properties. This file will be parsed by the+python script and the information will be added to the "Python properties"+column in the table with the data properties.++The `Column` class and its child classes describe the validations we use to+write the data collected by provider scripts. The `add_item` method has+docstrings with short descriptions of what each property is. The information+from these items will be added into the `media_properties` dictionary from+step 1.++```{code-block} python+media_properties = {+ "title": {+ "sql_properties": { ... # SQL properties },+ "python_properties": {+ "column": "[StringColumn](https://github.com/WordPress/openverse/blob/b4adc87c4e3cd7c9bdc879affda17fa21791c9ad/catalog/dags/common/storage/columns.py#L361-L401)",+ "required": False,+ "nullable": True,+ "truncate": True,+ "size": 5000,+ },+ }+}+```++#### `media.py`, `image.py` and `audio.py`++Located in `catalog/dags/common/storage/`.++These files contain the `MediaStore`, `ImageStore` and `AudioStore` classes that+validate and transform the data. They are not easy to parse, so they will only+be used for the CI checks. When a PR adds changes to these files, the CI will+show a warning that the documentation needs to be updated.++### New files++This project will create the following documents that will be used to generate+the final documentation page posted on+https://docs.openverse.org/meta/media_properties.html.++### `media_properties.md`++Located at `catalog/utilities/media_docs_gen/media_properties.md`.++See sample at [media_properties.md](samples/sample_media_properties.md).++This is a markdown file with the description of the media properties. This file+is manually written by the maintainers and contains the detailed information+about the shape and kind of data we expect to have for the property, how to+select it from the provider, and any inconsistencies between the data we have in+the database and the data we expect to have. This file is parsed in step 3 and+the information is added to the `media_properties` dictionary. This dictionary+will be used to write the final page on the documentation site.++#### Information to include in `media_properties.md` for each property++- Media types: Image, Audio or both+- Short description+- Names used in the provider scripts, if different from the database name+- Shape of the object (if it is not a simple type)+- Selection criteria+- Normalization and validation performed in the `MediaStore` class and the+ relevant `Column`+- Inconsistencies in the database data. See sample for `identifier` and `tags`+ in [`media_properties.md`](samples/sample_media_properties.md).++### `preamble.md` and `postamble.md`++Located at `catalog/utilities/media_docs_gen/preamble.md` and+`catalog/utilities/media_docs_gen/postamble.md`.++See sample at [preamble.md](samples/sample_preamble.md) and+[postamble](samples/sample_postamble.md). This is a markdown file with the+preamble and the postamble (i.e. the general notes that are applicable to more+than one media property) for the documentation page.++### `generate_media_properties_documentation.py`++Located at+`catalog/utilities/media_docs_gen/generate_media_properties_documentation.py`.++This is a Python script that parses the SQL DDL files, Python files, and the+`media_properties.md` file to generate the `media_properties_documentation.md`+file. This script is run by the `just` script.++## Outlined Steps (parallelizable workstreams)++1. Create a Python script that parses the DDL files, Python files and+ `media_properties.md` file to generate the+ `media_properties_documentation.md` file.+2. Create a `just` script that runs the Python script from step 1 and posts a+ note about how to update the documentation if there are differences.+3. Create markdown files: `general.md` and `media_properties.md` with the+ information about the media properties. This will require going through the+ issues and data in the database to collect the information about any+ inconsistencies.+4. Create checks for changes in `MediaStore` files that will post a note about+ how to update the documentation if there are differences (in precommit).+5. Add the CI workflows to `openverse` repository CI workflows.++## Dependencies++We can use the Python standard library as possible for this project.++## Alternative (or additional) Approaches++Another option for documenting the media properties is to pass a dataclass+instead of the named parameters to the `add_item` method of the `ImageStore` and+`AudioStore`. This would make the documentation closer to code for easier+updating, and have another benefit of allowing us to check for required+properties sooner. However, this would require a lot of refactoring and if+decided on, would need to be a separate project.diff --git a/documentation/projects/proposals/document_all_media_properties/index.md b/documentation/projects/proposals/document_all_media_properties/index.mdindex 6288e0a5f..4e50eaf63 100644--- a/documentation/projects/proposals/document_all_media_properties/index.md+++ b/documentation/projects/proposals/document_all_media_properties/index.md@@ -2,7 +2,8 @@```{toctree}:titlesonly:+:maxdepth: 2:glob:-*+Samples <samples/index.md>```diff --git a/documentation/projects/proposals/document_all_media_properties/samples/index.md b/documentation/projects/proposals/document_all_media_properties/samples/index.mdnew file mode 100644index 000000000..bd6ad3586--- /dev/null+++ b/documentation/projects/proposals/document_all_media_properties/samples/index.md@@ -0,0 +1,8 @@+# Samples++```{toctree}+:titlesonly:+:maxdepth: 1+:glob:+*+```diff --git a/documentation/projects/proposals/document_all_media_properties/samples/openverse_media_properties.md b/documentation/projects/proposals/document_all_media_properties/samples/openverse_media_properties.mdnew file mode 100644index 000000000..b9c1a7918--- /dev/null+++ b/documentation/projects/proposals/document_all_media_properties/samples/openverse_media_properties.md@@ -0,0 +1,140 @@+# Openverse Media Properties++_This document is auto-generated from the source code in+`utilities/media_props_gen/media_props_generation.py`. _++This is a list of the media properties, with the descriptions of corresponding+database columns and Python objects that are used to store and retrieve media+data. The order of the properties corresponds to their order in the `image_view`+materialized view.++## `image_view` properties++| DB Field | DB Nullable | DB Type | Python Column | Description |+| --------------------------- | ----------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------- |+| [`identifier`](#identifier) | True | uuid | [UUIDColumn](https://github.com/WordPress/openverse-catalog/blob/main/openverse_catalog/dags/common/storage/columns.py#L438-L455) (nullable=True, required=False, upsert_strategy=newest_non_null)) | UUID generated by Postgres |+| [`title`](#title) | True | string | [StringColumn](https://github.com/WordPress/openverse-catalog/blob/main/openverse_catalog/dags/common/storage/columns.py#L438-L455) (nullable=True, required=False, upsert_strategy=newest_non_null)) | Media title provided by the creator |+| [`width`](#width) | True | integer | [IntegerColumn](https://github.com/WordPress/openverse-catalog/blob/main/openverse_catalog/dags/common/storage/columns.py#L438-L455) (nullable=True, required=False, upsert_strategy=newest_non_null)) | Image width in pixel |+| [`tags`](#tags) | True | jsonb | [JSONColumn](https://github.com/WordPress/openverse-catalog/blob/main/openverse_catalog/dags/common/storage/columns.py#L326-L392) (nullable=True, required=False, upsert_strategy=merge_jsonb_arrays)) | A list of tag objects |++## Media Properties++### `identifier`++**Media type**: `image`, `audio`++#### Description++UUID generated by Postgres, the primary key of the `image`/`audio` tables.++Openverse `identifier` is generated during the ingestion process when the image+is inserted into the `image` table for the first time.++### Title++**Media type**: `image`, `audio`++The title of the work provided by the creator. If blank, uses "This work" for+the attribution (This work by creator is licensed with CC BY).++#### Shape of the data and Selection criteria++We select the default title returned by the provider. It can be blank. Blank+values (whether None or empty string "") are saved as empty string in the+database (TODO: check if this is true).++#### Existing data problems++Some media items had incorrectly encoded titles [^1 - Link to a description of+Unicode encoding problem in the "postamble"]. This is compensated for in the+Frontend (link to the code that fixes title encoding). This problem has been+fixed for the items that have been reingested after some time in 2020, but might+still persist for items that were not updated since then. Link to issues for+fixing the encoding in the catalog/api/frontend. Some Wikimedia titles have a+shape of "FILE:xxx.svg". The provider script removes them now, but this is still+a problem for items that were ingested earlier. The "FILE" and ".extension" are+removed in the frontend (link to the code). Link to the issue to fix it in the+API.++### `width`++**Media type**: `image`++#### Description++Image width in pixels. The width is used to calculate the aspect ratio of the+image on the frontend to prevent content shifting.++#### Shape of the data collected by provider scripts++If the provider API returns the width, it is collected as an integer. If the+provider API does not return the width, it can be requested using `head` request+to the direct image URL, however, this is not always accurate.++What happens for images where we don't use the largest image?++### `tags`++**Media type**: `image`, `audio`++**Name in provider scripts and `add_item`**: `raw_tags`++#### Description++The list of short descriptive labels or keywords for the media item.++#### Shape of the data collected by provider scripts++Provider scripts collect tags as a list of strings.++Provider API scripts collect the following items as tags: `finnish_museums`+(`subjects`), `flickr` (tags, discards tags that are longer than max_length and+sorts them for consistency), `inaturalist`(taxons),+`metropolitan_museum`(department, medium, culture, objectName, artist name,+classification, object date, credit line, period, tags), `nappy` (tags), `nypl`+(tags are inside meta_data), `rawpixel` (popular_keywords excluding the license+names - should remove this to rely on MediaStore?), `smithsonian`+(indexedStructured), `stocksnap` (keywords), `wordpress` (photo_tag).++#TODO: Fix `nypl` tags collection++#TODO: Remove `rawpixel` tag filtering++#### Data transformation in `MediaStore`++The `MediaStore` converts each tag into a dictionary with `name` and `provider`+properties. For example, ["tag1"] becomes+`{ "name": "tag1", "provider": "provider1"}`.++#### Data transformation in `ingestion_server`/`api`++Some redundant tags (such as `uploaded_by` or the tags with licenses) are+filtered out in the `MediaStore`. Some images also have AI-generated tags from+`clarifai` and `Recognition`. These tags contain the label of what is in the+image, with the accuracy of detection+(`{ "name": "cat", "accuracy": 0.95, "provider": "clarifai"}`). It was+previously determined that tags with an accuracy score below 0.90 are often+unreliable, so those tags are discarded during the+[data cleanup process](https://github.com/WordPress/openverse/blob/a281dc472fbca281bfaa623ebe5ec0b0feab31ee/ingestion_server/ingestion_server/cleanup.py#L114)+in the `ingestion_server`.++#### Data inconsistencies in the database++Some tags collected in the beginning had incorrectly escaped Unicode characters.+When the items were updated during data refresh, both the correct and incorrect+tags were saved because they were different. This is mitigated in+[Nuxt](https://github.com/WordPress/openverse/blob/a281dc472fbca281bfaa623ebe5ec0b0feab31ee/frontend/src/utils/decode-media-data.ts#L95).++## Common issues++### Incorrectly escaped Unicode characters++In: `title`, `tags`, `creator` Sometimes, Unicode characters were encoded+incorrectly. Symbols like `\u<xxxx>` were saved as either `\\u<xxxx>` or+`u<xxxx>`. Thus, instead of showing the non-ascii symbols, they are shown as+gibberish in the UI. This is addressed in the Nuxt front end. When data refresh+is run, the incorrect values can be replaced if the `upsert_strategy` for the+column is `newest_non_null`. However, for the strategies that update the value+instead of replacing it, this results in duplication of the values. Thus, some+items have duplicated tags: the correctly-encoded tags cannot replace the+incorrectly-encoded tags because they are different.diff --git a/documentation/projects/proposals/document_all_media_properties/samples/sample_media_properties.md b/documentation/projects/proposals/document_all_media_properties/samples/sample_media_properties.mdnew file mode 100644index 000000000..84a094989--- /dev/null+++ b/documentation/projects/proposals/document_all_media_properties/samples/sample_media_properties.md@@ -0,0 +1,84 @@+# sample_media_properties.md++## Title++The title of the work. If blank, uses "This work" for the attribution sentence+(This work by creator is licensed with CC BY). Shape of the data and Selection+criteria++We select the default title returned by the provider. It can be blank. Blank+values (whether None or empty string "") are saved as empty string in the+database (TODO: check if this is true). Existing data problems++Some media items had incorrectly encoded titles [^1 - Link to a description of+Unicode encoding problem in the "postamble"]. This is compensated for in the+Frontend (link to the code that fixes title encoding). This problem has been+fixed for the items that have been reingested after some time in 2020, but might+still persist for items that were not updated since then. Link to issues for+fixing the encoding in the catalog/api/frontend. Some Wikimedia titles have a+shape of "FILE:xxx.svg". The provider script removes them now, but this is still+a problem for items that were ingested earlier. The "FILE" and ".extension" are+removed in the frontend (link to the code). Link to the issue to fix it in the+API.++## `identifier`++Used for `image` and `audio` Openverse `identifier` is generated during the+ingestion process when the image is inserted into the `image` table for the+first time.++## `tags`++### Media Type++Used for `image` and `audio`++### Name in provider scripts and `add_item`++`raw_tags`++### Description++The list of short descriptive labels or keywords for the media item.++### Shape of the data collected by provider scripts++Provider scripts collect tags as a list of strings.++Provider API scripts collect the following items as tags: `finnish_museums`+(`subjects`), `flickr` (tags, discards tags that are longer than max_length and+sorts them for consistency), `inaturalist`(taxons),+`metropolitan_museum`(department, medium, culture, objectName, artist name,+classification, object date, credit line, period, tags), `nappy` (tags), `nypl`+(tags are inside meta_data), `rawpixel` (popular_keywords excluding the license+names - should remove this to rely on MediaStore?), `smithsonian`+(indexedStructured), `stocksnap` (keywords), `wordpress` (photo_tag).++#TODO: Fix `nypl` tags collection++#TODO: Remove `rawpixel` tag filtering++### Data transformation in `MediaStore`++The `MediaStore` converts each tag into a dictionary with `name` and `provider`+properties. For example, ["tag1"] becomes+`{ "name": "tag1", "provider": "provider1"}`.++### Data transformation in `ingestion_server`/`api`++Some redundant tags (such as `uploaded_by` or the tags with licenses) are+filtered out in the `MediaStore`. Some images also have AI-generated tags from+`clarifai` and `Recognition`. These tags contain the label of what is in the+image, with the accuracy of detection+(`{ "name": "cat", "accuracy": 0.95, "provider": "clarifai"}`). It was+previously determined that tags with an accuracy score below 0.90 are often+unreliable, so those tags are discarded during the+[data cleanup process](https://github.com/WordPress/openverse/blob/a281dc472fbca281bfaa623ebe5ec0b0feab31ee/ingestion_server/ingestion_server/cleanup.py#L114)+in the `ingestion_server`.++### Data inconsistencies in the database++Some tags collected in the beginning had incorrectly escaped Unicode characters.+When the items were updated during data refresh, both the correct and incorrect+tags were saved because they were different. This is mitigated in+[Nuxt](https://github.com/WordPress/openverse/blob/a281dc472fbca281bfaa623ebe5ec0b0feab31ee/frontend/src/utils/decode-media-data.ts#L95).diff --git a/documentation/projects/proposals/document_all_media_properties/samples/sample_postamble.md b/documentation/projects/proposals/document_all_media_properties/samples/sample_postamble.mdnew file mode 100644index 000000000..36c3baf49--- /dev/null+++ b/documentation/projects/proposals/document_all_media_properties/samples/sample_postamble.md@@ -0,0 +1,17 @@+# sample_postamble.md++## Additional Notes++### Common issues++#### Incorrectly escaped Unicode characters++In: `title`, `tags`, `creator` Sometimes, Unicode characters were encoded+incorrectly. Symbols like `\u<xxxx>` were saved as either `\\u<xxxx>` or+`u<xxxx>`. Thus, instead of showing the non-ascii symbols, they are shown as+gibberish in the UI. This is addressed in the Nuxt front end. When data refresh+is run, the incorrect values can be replaced if the `upsert_strategy` for the+column is `newest_non_null`. However, for the strategies that update the value+instead of replacing it, this results in duplication of the values. Thus, some+items have duplicated tags: the correctly-encoded tags cannot replace the+incorrectly-encoded tags because they are different.diff --git a/documentation/projects/proposals/document_all_media_properties/samples/sample_preamble.md b/documentation/projects/proposals/document_all_media_properties/samples/sample_preamble.mdnew file mode 100644index 000000000..913eb73b3--- /dev/null+++ b/documentation/projects/proposals/document_all_media_properties/samples/sample_preamble.md@@ -0,0 +1,11 @@+# sample_preamble.md++## Openverse Media Properties++_This document is auto-generated from the source code in+`utilities/media_props_gen/media_props_generation.py`. _++This is a list of the media properties, with the descriptions of corresponding+database columns and Python objects that are used to store and retrieve media+data. The order of the properties corresponds to their order in the `image_view`+materialized view.