Skip to content

Commit b928cc0

Browse files
author
Mike
committed
Update documentation around resource and QuickCharts generation
1 parent 4a6dc86 commit b928cc0

4 files changed

Lines changed: 252 additions & 111 deletions

File tree

doc/main.md

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ upload your datasets to HDX.
2727
- [Tags](#tags)
2828
- [Maintainer](#maintainer)
2929
- [Organization](#organization)
30+
- [Resource Generation](#resource-generation)
31+
- [QuickCharts Generation](#quickcharts-generation)
3032
- [Resource Specific Operations](#resource-specific-operations)
33+
- [Showcase Management](#showcase-management)
3134
- [User Management](#user-management)
3235
- [Organization Management](#organization-management)
3336
- [Vocabulary Management](#vocabulary-management)
@@ -48,6 +51,8 @@ The library has detailed API documentation which can be found in the menu at the
4851

4952

5053
## Breaking Changes
54+
From 6.0.0, generate_resource_view is renamed to generate_quickcharts
55+
5156
From 5.9.9, get_location_iso3s returns uppercase codes instead of lowercase
5257

5358
From 5.9.8, get_date_of_dataset has become get_reference_period,
@@ -738,6 +743,132 @@ If you want to set the organization, you do it like this:
738743
ORGANIZATION is either a string id, dictionary or an Organization
739744
object.
740745

746+
### Resource generation
747+
748+
There are a range of helpful functions to generate resources. In the following
749+
examples, RESOURCE DATA takes the form {"name": NAME, "description":
750+
DESCRIPTION} and ENCODING is a file encoding like "utf-8".
751+
752+
A resource can be generated from ROWS which is a list of list, tuple or
753+
dictionary. HEADERS is either a row number (rows start counting at 1), or the
754+
actual headers defined as a list of strings. If not set, all rows will be
755+
treated as containing values:
756+
757+
dataset.generate_resource_from_rows("FOLDER", "FILENAME", ROWS,
758+
RESOURCE DATA, HEADERS, "ENCODING")
759+
760+
A resource for the purpose of driving QuickCharts can be generated by taking
761+
ROWS, a list of dictionaries, and producing a cut down subset from it. HXLTAGS
762+
are added as the second row after the header. The reduction in rows is
763+
performed by only outputting the rows where COLUMN_NAME has a value in
764+
QC_IDENTIFIERS. Optionally the columns that are output can be limited by
765+
specifying them in HEADERS.
766+
767+
dataset.generate_qc_resource_from_rows("FOLDER", "FILENAME", ROWS,
768+
RESOURCE DATA, HXLTAGS, "COLUMN_NAME", QC_IDENTIFIERS, HEADERS, "ENCODING")
769+
770+
Building on these basic resource generation methods, there are more powerful
771+
ones `generate_resource_from_iterator` and `download_and_generate_resource`.
772+
773+
A resource can be generated from a given list or tuple: HEADERS and an ITERATOR
774+
which can return rows in list, tuple or dictionary form. A mapping from headers
775+
to HXL hashtags, HXLTAGS, must be provided along with the FOLDER and FILENAME
776+
where the file will be generated for upload to the filestore. The dataset
777+
reference period can optionally be set by supplying DATECOL for looking up
778+
dates or YEARCOL for looking up years. DATECOl and YEARCOL can be a column name
779+
or the index of a column. Note that any timezone information is ignored and UTC
780+
is assumed.
781+
782+
Alternatively, DATE_FUNCTION can be supplied to handle any dates
783+
in a row. It should accept a row and should return None to ignore the row or a
784+
dictionary which can either be empty if there are no dates in the row or can be
785+
populated with keys startdate and/or enddate which are of type timezone-aware
786+
datetime. The lowest start date and highest end date are used to set the
787+
reference period and are returned in the results dictionary in keys startdate
788+
and enddate.
789+
790+
dataset.generate_resource_from_iterator(HEADERS, ITERATOR, HXLTAGS,
791+
"FOLDER", "FILENAME", RESOURCE_DATA, DATECOL or YEARCOL or DATE_FUNCTION,
792+
QUICKCHARTS, "ENCODING")
793+
794+
If desired, `generate_resource_from_iterator` can generate a separate
795+
QuickCharts resource designed to be used in a time series QuickCharts bite
796+
provided that the input has #indicator+code, #date and #indicator+value+num.
797+
This is achieved by supplying the parameter QUICKCHARTS which activates various
798+
QuickCharts related actions depending upon the keys given in the dictionary.
799+
The returned dictionary will contain the QuickCharts resource in the key
800+
qc_resource. If the keys: hashtag - the HXL hashtag to examine - and values -
801+
the 3 values to look for in that column - are supplied, then a list of booleans
802+
indicating which QuickCharts bites should be enabled will be returned in the
803+
key bites_disabled in the returned dictionary. For the 3 values, if the key:
804+
numeric_hashtag is supplied then if that column for a given value contains no
805+
numbers, then the corresponding bite will be disabled. If the key: cutdown is
806+
given, if it is 1, then a separate cut down list is created containing only
807+
columns with HXL hashtags and rows with desired values (if hashtag and values
808+
are supplied) for the purpose of driving QuickCharts. It is returned in the key
809+
qcrows in the returned dictionary with the matching headers in qcheaders. If
810+
cutdown is 2, then a resource is created using the cut down list. If the key
811+
cutdownhashtags is supplied, then only the provided hashtags are used for
812+
cutting down otherwise the full list of HXL tags is used.
813+
814+
The QuickCharts resource will be of form similar to below:
815+
816+
GHO (CODE),ENDYEAR,Numeric
817+
#indicator+code,#date+year+end,#indicator+value+num
818+
VIOLENCE_HOMICIDERATE,1994,123.4
819+
MDG_0000000001,2015,123.4
820+
821+
`download_and_generate_resource` builds on `generate_resource_from_iterator`.
822+
It uses an DOWNLOADER, an object of class `Download`, `Retrieve` or other class
823+
that implements `BaseDownload` to download from URL. Additional arguments in
824+
**KWARGS are passed to the `get_tabular_rows` method of the DOWNLOADER.
825+
826+
Optionally, headers can be inserted at specific positions. This is achieved
827+
using HEADER_INSERTIONS. If supplied, it is a list of tuples of the form
828+
(position, header) to be inserted. A function, ROW_FUNCTION, is called for each
829+
row. If supplied, it takes as arguments: headers (prior to any insertions) and
830+
row (which will be in dict or list form depending upon the dict_rows argument)
831+
and outputs a modified row.
832+
833+
The rest of the arguments are the same as for
834+
`generate_resource_from_iterator`.
835+
836+
dataset.download_and_generate_resource(DOWNLOADER, "URL", HXLTAGS,
837+
"FOLDER", "FILENAME", RESOURCE_DATA, HEADER_INSERTIONS, ROW_FUNCTION,
838+
DATECOL or YEARCOL or DATE_FUNCTION, QUICKCHARTS, **KWARGS)
839+
840+
### QuickCharts Generation
841+
842+
QuickCharts can be generated for datasets using the call below. RESOURCE is a
843+
a resource id or name, or resource metadata from a Resource object or a
844+
dictionary, or the position of the resource in the dataset. It defaults to the
845+
position 0. PATH points to configuration which if not supplied, defaults to the
846+
internal indicators resource view template. You can disable specific bites by
847+
providing BITES_DISABLED, a list of 3 bools where True indicates a specific bite
848+
is disabled and False indicates leave enabled.
849+
850+
datasets.generate_quickcharts(RESOURCE, "PATH", BITES_DISABLED, INDICATORS,
851+
FIND_REPLACE)
852+
853+
The parameter INDICATORS is only for use with the built-in configuration and is
854+
a list with 3 dictionaries of form:
855+
856+
{"code": "MY_INDICATOR_CODE", "title": "MY_INDICATOR_TITLE",
857+
"unit": "MY_INDICATOR_UNIT"}.
858+
859+
Optionally, the following defaults can be overridden in INDICATORS:
860+
861+
{"code_col": "#indicator+code", "value_col": "#indicator+value+num",
862+
"date_col": "#date+year", "date_format": "%Y", "aggregate_col": "null"}.
863+
864+
The built-in configuration assumes data will be of form similar to below:
865+
866+
GHO (CODE),ENDYEAR,Numeric
867+
#indicator+code,#date+year+end,#indicator+value+num
868+
VIOLENCE_HOMICIDERATE,1994,123.4
869+
MDG_0000000001,2015,123.4
870+
871+
741872
## Resource Specific Operations
742873

743874
You can download a resource using the **download** function eg.
@@ -759,8 +890,9 @@ There is a getter to read the value back:
759890
file_to_upload = resource.get_file_to_upload()
760891

761892
## Showcase Management
893+
762894
The **Showcase** class enables you to manage showcases, creating, deleting and updating
763-
(as for other HDX objects) according to your permissions.
895+
(as for other HDX objects) according to your permissions.
764896

765897
To see the list of datasets a showcase is in, you use the **get\_datasets** function eg.
766898

setup.cfg

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,3 @@ install_requires =
5454
[options.packages.find]
5555
where = src
5656

57-
[bdist_wheel]
58-
universal = 1
59-

0 commit comments

Comments
 (0)