# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Matrix Visualization Notebook
#
# This notebook is for getting the data we need from HCMST2017 for the matrix visualization, which answers the question:
#
# How does frequency of sex influence quality of relationship?
#

# The original HMCST2017 file comes as a Stata file, so I exported it from Stata as a CSV
import csv

data = []
with open('hcmst2017.csv', newline='', encoding='mac_roman') as csvfile:
    reader = csv.DictReader(csvfile)
    i = 0
    for row in reader:
        if i == 0:
            keys = list(row.keys())
            output = ''
            for key in keys:
                output += key + '\n'
            print(output)
        i += 1
        data.append(row)

print(data[2000]["DOV_Branch"])

sex_frequency_types = set()
for datum in data:
    some_set.add(datum["w6_sex_frequency"])

quality_types = set()
for datum in data:
    quality_types.add(datum["w6_relationship_quality"])
print(quality_types)

frequency_counts = dict()

for person in data:
    key = person["w6_sex_frequency"]
    
    if key not in frequency_counts:
        frequency_counts[key] = 0
    
    frequency_counts[key] += 1
    

matrix_data = {}

frequency_counts = dict()
for person in data:
    key1 = person["w6_sex_frequency"]
    key2 = person["w6_relationship_quality"]
    
    if key1 not in frequency_counts:
        frequency_counts[key1] = {}
    
    if key2 not in frequency_counts[key1]:
        frequency_counts[key1][key2] = 0
        
    frequency_counts[key1][key2] += 1
    

frequency_counts

f = frequency_counts
export_data = []
for key in f:
    if key == '':
        continue
    if 'very poor' not in f[key]:
        f[key]['very poor'] = 0
        
    export_data.append(
        { "frequency": key, "ratings": [
        f[key]['very poor'],
        f[key]['poor'],
        f[key]['fair'],
        f[key]['good'],
        f[key]['excellent']
    ]})

export_data

import json

export_json = json.dumps(export_data)

export_json



StarcoderdataJupyterScriptsDedupFiltered