<jupyter_start><jupyter_text>Set up SageMaker FeatureStore<jupyter_code>region = boto3.Session().region_name

boto_session = boto3.Session(region_name=region)
sagemaker_client = boto_session.client(service_name='sagemaker', region_name=region)
featurestore_runtime = boto_session.client(service_name='sagemaker-featurestore-runtime', region_name=region)

feature_store_session = Session(
    boto_session=boto_session,
    sagemaker_client=sagemaker_client,
    sagemaker_featurestore_runtime_client=featurestore_runtime
)

role = get_execution_role()
s3_client = boto3.client('s3', region_name=region)

default_s3_bucket_name = feature_store_session.default_bucket()
prefix = 'sagemaker-featurestore-demo'<jupyter_output><empty_output><jupyter_text>Get data from S3<jupyter_code># Get data from S3 
bucket_gen = 'nsclc-clinical-genomic-data'
#bucket_gen = <S3-BUCKET-NAME>

# Genomic data 
data_key_gen = 'Genomic-data-119patients.csv'
#data_key_gen = <FILE-NAME.csv>

data_location_gen = 's3://{}/{}'.format(bucket_gen, data_key_gen)
data_gen = pd.read_csv(data_location_gen)<jupyter_output><empty_output><jupyter_text>Ingest data into FeatureStore<jupyter_code>genomic_feature_group_name = 'genomic-feature-group-' + strftime('%d-%H-%M-%S', gmtime())
genomic_feature_group = FeatureGroup(name=genomic_feature_group_name, sagemaker_session=feature_store_session)

current_time_sec = int(round(time.time()))

def cast_object_to_string(data_frame):
    for label in data_frame.columns:
        if data_frame.dtypes[label] == 'object':
            data_frame[label] = data_frame[label].astype("str").astype("string")

# Cast object dtype to string. SageMaker FeatureStore Python SDK will then map the string dtype to String feature type.
cast_object_to_string(data_gen)

# Record identifier and event time feature names
record_identifier_feature_name = "Case_ID"
event_time_feature_name = "EventTime"

# Append EventTime feature
data_gen[event_time_feature_name] = pd.Series([current_time_sec]*len(data_gen), dtype="float64")

# Load feature definitions to the feature group. SageMaker FeatureStore Python SDK will auto-detect the data schema based on input data.
genomic_feature_group.load_feature_definitions(data_frame=data_gen); # output is suppressed


def wait_for_feature_group_creation_complete(feature_group):
    status = feature_group.describe().get("FeatureGroupStatus")
    while status == "Creating":
        print("Waiting for Feature Group Creation")
        time.sleep(5)
        status = feature_group.describe().get("FeatureGroupStatus")
    if status != "Created":
        raise RuntimeError(f"Failed to create feature group {feature_group.name}")
    print(f"FeatureGroup {feature_group.name} successfully created.")

genomic_feature_group.create(
    s3_uri=f"s3://{default_s3_bucket_name}/{prefix}",
    record_identifier_name=record_identifier_feature_name,
    event_time_feature_name=event_time_feature_name,
    role_arn=role,
    enable_online_store=True
)

wait_for_feature_group_creation_complete(feature_group=genomic_feature_group)

genomic_feature_group.ingest(
    data_frame=data_gen, max_workers=3, wait=True
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0995275590551181, 'fraction_numerical': 0.006299212598425197, 'mean_word_length': 7.081424936386768, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '27425', 'license': 'MIT-0', 'n_tokens_mistral': 1062, 'n_tokens_neox': 1042, 'n_words': 173, 'path': 'genomic/preprocess-genomic-data.ipynb', 'repo_name': 'curtlockhart/machine-learning-pipelines-for-multimodal-health-data', 'size': '3175'}