<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 MPContribs
Walkthrough
start with a materials detail page on MP with user contributions
navigate to https
Step1:  Contribute data on Refractive Index
We'll prepare refractive index data for contribution to https
Step2:  Explore and extract refractive index data
https
Step3:  Prepare a single contribution for testing
Note that a contribution to a specific MP material contains 4 optional components
Step4:  Retrieve and update project info
Let's add the URL for the DB and also set unique_identifiers to False. This flag indicates that this project can contain multiple contributions (rows in the landing page's overview table) for the same MP material (mp-id). We also want to include descriptions of the data columns in the project's other field.
Step5:  Try searching for refractive in MPContribs browse page now
Initialize data columns
Step6:  Submit test contribution
Simply provide your list of contributions as argument to the client's submit_contributions function to prepare and upload them to MPContribs. Duplicate checking will happen automatically if unique_identifers is set to True for the project (the default). If successful, the client will return the number of contributions submitted.
Step7:  Your first contribution should now show up in your project on https
Step8:  Publish contributions
Step9:  Retrieve and query contributions
Step10:  Grab the table ID and retrieve it as Pandas DataFrame. Show a graph.
Step11:  Finally, let's build up a more complicated query to reduce the list of contributions to the ones we might be interested in.
<ASSISTANT_TASK:>
Python Code:
name = "your-project-name"
apikey = "your-api-key"  # profile.materialsproject.org
Explanation: MPContribs
Walkthrough
start with a materials detail page on MP with user contributions
navigate to https://mpcontribs.org and explore
apply for project on https://workshop-contribs.materialsproject.org/contribute (wait for approval by admins)
End of explanation
from mpcontribs.client import Client
from mp_api.matproj import MPRester
from refractivesqlite import dboperations as DB
from pandas import DataFrame
Explanation: Contribute data on Refractive Index
We'll prepare refractive index data for contribution to https://workshop-contribs.materialsproject.org using the mpcontribs-client.
End of explanation
db = DB.Database("refractive.db")
#db.create_database_from_url()
db.search_pages("Au", exact=True)
materials = db.search_custom(
    'select * from pages where book="Au" and hasrefractive=1 and hasextinction=1'
)
Explanation: Explore and extract refractive index data
https://github.com/HugoGuillen/refractiveindex.info-sqlite/blob/master/Tutorial.ipynb
End of explanation
page_id = materials[0][0]
mat = db.get_material(page_id)
mpr = MPRester(apikey)
def get_contrib(mat):
    info = mat.get_page_info()
    formula = info["book"]
    mpid = mpr.get_materials_ids(formula)[0]
    rmin, rmax = info['rangeMin']*1000, info['rangeMax']*1000
    mid = (rmin + rmax) / 2
    n = mat.get_refractiveindex(mid)
    k = mat.get_extinctioncoefficient(mid)
    
    x = "wavelength λ [μm]"
    refrac = DataFrame(mat.get_complete_refractive(), columns=[x, "n"])
    refrac.set_index(x, inplace=True)
    extinct = DataFrame(mat.get_complete_extinction(), columns=[x, "k"])
    extinct.set_index(x, inplace=True)
    df = refrac.join(extinct["k"])
    df.attrs["title"] = f"Complex refractive index (n+ik) for {formula}"
    df.attrs["labels"] = {
        "value": "n, k",  # y-axis label
        "variable": "Re/Im" # legend name (= df.columns.name)
    }
    df.plot(**df.attrs)#.show()
    df.attrs["name"] = "n,k(λ)"
    return {
        "project": name,
        "identifier": str(mpid),
        "data": {
            "n": float(n),
            "k": float(k),
            "range.min": f"{rmin} nm",
            "range.mid": f"{mid} nm",
            "range.max": f"{rmax} nm",
            "points": info["points"],
            "page": info["page"]
        },
        "tables": [df]
    }
contrib = get_contrib(mat)
Explanation: Prepare a single contribution for testing
Note that a contribution to a specific MP material contains 4 optional components:
simple (potentially nested) "key-value" data
tables as Pandas DataFrame objects (think spreadsheets and csv files)
structures as Pymatgen Structure objects (think CIF, POSCAR, ...)
attachments (think gzipped text files, PNG/JPG..)
Example for a single contribution dictionary:
{
    "project": "sandbox",
    "identifier": "mp-4",
    "data": {
        "a": "3 eV",
        "b": {"c": "hello", "d": 3},
        "d.e.f": "nest via dot-notation"
    },
    "structures": [&lt;pymatgen Structure&gt;, ...],
    "tables": [&lt;pandas DataFrame&gt;, ...],
    "attachments": [&lt;pathlib.Path&gt;, &lt;mpcontribs.client.Attachment&gt;, ...]
}
End of explanation
client = Client(
    host="workshop-contribs-api.materialsproject.org",
    apikey=apikey
)
db.check_url_version()
update = {
    'unique_identifiers': False,
    'references': [
        {'label': 'website', 'url': 'https://refractiveindex.info'},
        {'label': 'source', 'url': "https://refractiveindex.info/download/database/rii-database-2019-02-11.zip"}
    ],
    "other": { # describe the root fields here to automatically include tooltips on MP
        "n": "real part of complex refractive index",
        "k": "imaginary part of complex refractive index",
        "range": "wavelength range for n,k in nm",
        "points": "number of λ points in range",
        "page": "reference to data source/publication"
    }
}
# could also update authors, title, long_title, description
client.projects.update_entry(pk=name, project=update).result()
Explanation: Retrieve and update project info
Let's add the URL for the DB and also set unique_identifiers to False. This flag indicates that this project can contain multiple contributions (rows in the landing page's overview table) for the same MP material (mp-id). We also want to include descriptions of the data columns in the project's other field.
End of explanation
client.init_columns(name, {
    "n": "", # dimensionless
    "k": "",
    "range.min": "nm",
    "range.mid": "nm",
    "range.max": "nm",
    "points": "",
    "page": None # text    
})
Explanation: Try searching for refractive in MPContribs browse page now
Initialize data columns
End of explanation
client.submit_contributions([contrib])
Explanation: Submit test contribution
Simply provide your list of contributions as argument to the client's submit_contributions function to prepare and upload them to MPContribs. Duplicate checking will happen automatically if unique_identifers is set to True for the project (the default). If successful, the client will return the number of contributions submitted.
End of explanation
contributions = []
for material in materials:
    page_id = material[0]
    mat = db.get_material(page_id)
    contrib = get_contrib(mat)
    contributions.append(contrib)
contributions[10]
client.delete_contributions(name)
client.submit_contributions(contributions, ignore_dupes=True)
Explanation: Your first contribution should now show up in your project on https://workshop-contribs.materialsproject.org :)
Prepare and upload all contributions
End of explanation
client.make_public(name)
Explanation: Publish contributions
End of explanation
all_ids = client.get_all_ids(
    {"project": name},
    include=["tables"],
    data_id_fields={name: "page"},
    fmt="map"
)
Explanation: Retrieve and query contributions
End of explanation
tid = all_ids[name]["mp-81"]["Johnson"]["tables"]["n,k(λ)"]["id"]
client.get_table(tid).display()
Explanation: Grab the table ID and retrieve it as Pandas DataFrame. Show a graph.
End of explanation
query = {
    "project": name,
    "formula__contains": "Au",
    #"identifier__in": []
    "data__n__value__lt": 200,
    "data__k__value__gte": 7,
    "_sort": "-data__range__mid__value",
    "_fields": [
        "id", "identifier", "formula",
        "data.range.mid.value",
        "data.n.value",
        "data.k.value"
    ]
}
print(client.get_totals(query))
client.contributions.get_entries(**query).result()
Explanation: Finally, let's build up a more complicated query to reduce the list of contributions to the ones we might be interested in.
End of explanation
<END_TASK>