Skip to main content

code repository for TA2 Knowledge Graph (MinMod) construction & deployment

Project description

Overview

Code for TA2 Knowledge Graph and other related services such as its API, CDR integration, data browser, etc.

Repository Structure

Installation

Setup the workspace

Setup the workspace by cloning ta2-minmod-data and this repository ta2-minmod-kg inside your WORKDIR

git clone --depth 1 https://github.com/DARPA-CRITICALMAAS/ta2-minmod-data
git clone --depth 1 https://github.com/DARPA-CRITICALMAAS/ta2-minmod-kg
mkdir kgdata

The directory will look like this

<WORKDIR>
  ├── data                      # for storing databases
  ├── ta2-minmod-data           # ta2-minmod-data repository
  └── ta2-minmod-kg             # code to setup TA2 KG

Setup the environment variables

The following commands will use these environment variables:

  1. USER_ID & GROUP_ID: the current user id, this is to make sure the docker containers will create files with the same owner as the current user. You can set the USER_ID and GROUP_ID automatically using this command:

    export USER_ID=$(id -u)
    export GROUP_ID=$(id -g)
    
  2. CERT_DIR: a directory containing SSL certificate (fullchain.pem and privkey.pem) for your server (see more at Generating an SSL certificate).

To make it easy to set these environment variables, you can create a copied file named .myenv from env.template and update the values accordingly. Then you can run the following command to set the environment variables:

. ./ta2-minmod-kg/.myenv

Generating an SSL certificate

You can use Let's Encrypt to create a free SSL certificate (fullchain.pem and privkey.pem) for your server. However, for the purpose of testing locally, you can generate your own using the following command

openssl req -x509 -newkey rsa:4096 -keyout privkey.pem -out fullchain.pem -sha256 -days 3650 -nodes -subj "/C=XX/ST=StateName/L=CityName/O=CompanyName/OU=CompanySectionName/CN=CommonNameOrHostname"

Setup dependencies

1. Installing required services using Docker:

List of services: knowledge graph, our API, nginx

cd ta2-minmod-kg
docker network create minmod
docker compose build
cd ..

2. Installing python library

Requiring poetry: pip install poetry

cd ta2-minmod-kg
python -m venv .venv
poetry install --only main
cd ..

Usage

drawing

The figure above denotes how different components work to produce mineral site data and grade tonnage models. The rounded rectangle are systems, blue rectangle are processes, and the parallelogram is the input source.

1. Building TA2 knowledge graph

source ta2-minmod-kg/.venv/bin/activate
python -m statickg ta2-minmod-kg/etl.yml ./kgdata ta2-minmod-data --overwrite-config --refresh 20

Note that this process will continue running to monitor for new changes. Hence, it will not terminate unless a terminating signal is received explicitly.

2. Starting other services

docker compose -f ./ta2-minmod-kg/docker-compose.yml up nginx api

If you also want to start our dashboard, run docker compose up nginx api dashboard instead. Note that currently, URLs for TA2 services are hardcoded in the dashboard, so it will not query our local services.

Once it starts, you can view our API docs in https://localhost/api/v1/docs

3. Upload data to CDR

To upload data to CDR, you need to obtain a token first. Then, run the following command:

export CDR_AUTH_TOKEN=<your cdr token>
export MINMOD_API=https://localhost/api/v1
export MINMOD_SYSTEM=test
python -m minmodkg.integrations.cdr

The two environment variables MINMOD_API and MINMOD_SYSTEM are used for testing purposes. For production, you can simply ignore these two variables and our code will use the default values MINMOD_API=https://minmod.isi.edu/api/v1 and MINMOD_SYSTEM=minmod.

4. Download data from CDR

The CDR endpoint for getting TA2 output is https://api.cdr.land/docs#/Minerals/list_dedup_site_commodity_v1_minerals_dedup_site_searchcommodityget.

There are two required parameters:

  • commodity: name of the commodity (capitalize, case-sensitive) e.g., Lithium. Here is list of commodities
  • system: minmod

By default, the CDR should return all sites matched the provided two parameters. To further filter the data for sites with deposit type classification and sites with grade/tonnage data, we can apply the following additional filters:

  • Site with deposit type classification: with_location = true, and with_deposit_types_only = true

    CURL Command:

    curl -X 'GET' \
    'https://api.cdr.land/v1/minerals/dedup-site/search/Lithium?with_location=true&with_deposit_types_only=true&system=minmod&top_n=1&limit=-1' \
    -H 'Authorization: Bearer <your token>'
    
  • Site with grade/tonnage data: with_contained_metals = true

    CURL Command:

    curl -X 'GET' \
    'https://api.cdr.land/v1/minerals/dedup-site/search/Lithium?with_contained_metals=true&system=minmod&top_n=1&limit=-1' \
    -H 'Authorization: Bearer <your token>'
    

5. Browsing the data locally

You can browse the data locally by replacing the hostname from minmod.isi.edu to localhost. For example, https://minmod.isi.edu/resource/kg by https://localhost/resource/kg

6. Querying data

If you know SPARQL, you can query the data by sending your query to https://<hostname>/sparql. We have a helper function to make it easier if you know Python in here

Also, you can download mineral site data and grade/tonnage model directly from our API. Please see our API docs in https://localhost/api/v1/docs for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

minmodkg-1.4.0.tar.gz (50.2 kB view details)

Uploaded Source

Built Distribution

minmodkg-1.4.0-py3-none-any.whl (60.5 kB view details)

Uploaded Python 3

File details

Details for the file minmodkg-1.4.0.tar.gz.

File metadata

  • Download URL: minmodkg-1.4.0.tar.gz
  • Upload date:
  • Size: 50.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.4 Darwin/23.6.0

File hashes

Hashes for minmodkg-1.4.0.tar.gz
Algorithm Hash digest
SHA256 98155bd55084313fbdf11dca11131d558ef8b1f30fbafda3835f29939c1d84f6
MD5 ca9c9d5970c195bb535a3ff1c948304d
BLAKE2b-256 40b1f7e3f2a817791aea13802c57c465def635ac007cf933ee632f335c9ecb12

See more details on using hashes here.

File details

Details for the file minmodkg-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: minmodkg-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 60.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.4 Darwin/23.6.0

File hashes

Hashes for minmodkg-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f2fd98616d65531eb155a4b0130151f96cd21d5c58f9df0040cbdf69183e4c6
MD5 78ab46b73d3a207638577129d062a4cc
BLAKE2b-256 b2042a865f09cb481790194caed9d05d2b2334a355d023e0004d5ab69465f1b1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page