Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'diabetes_classifier.json' (OS Error: Read-only file system, errno = 30) #188

Closed
stephentandjiria opened this issue Aug 6, 2021 · 2 comments

Comments

@stephentandjiria
Copy link

Hi, I have been trying to replicate the example here, but I cannot write the json classifier model due to the following error:

Running "flutter pub get" in logistic_regressor...
Launching lib\main.dart on sdk gphone x86 in debug mode...
Running Gradle task 'assembleDebug'...
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...
Debug service listening on ws://127.0.0.1:64760/6bAEB-pabM4=/ws
Syncing files to device sdk gphone x86...
I/flutter ( 7530): accuracy on k fold validation: 0.63
I/flutter ( 7530): 0.76
E/flutter ( 7530): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FileSystemException: Cannot create file, path = 'diabetes_classifier.json' (OS Error: Read-only file system, errno = 30)
E/flutter ( 7530): #0      _File.create.<anonymous closure> (dart:io/file_impl.dart:255:9)
E/flutter ( 7530): #1      _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter ( 7530): #2      _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter ( 7530): <asynchronous suspension>
E/flutter ( 7530): #3      SerializableMixin.saveAsJson (package:ml_algo/src/common/serializable/serializable_mixin.dart:9:18)
E/flutter ( 7530): <asynchronous suspension>
E/flutter ( 7530): #4      _MyHomePageState.trainModel (package:logistic_regressor/main.dart:99:5)
E/flutter ( 7530): <asynchronous suspension>
E/flutter ( 7530): 

I have tried using permission in android/app/src/main/AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.logistic_regressor">
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

and also requested the permission via Permission.manageExternalStorage.request() in my main.dart:

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:ml_algo/ml_algo.dart';
import 'package:ml_dataframe/ml_dataframe.dart';
import 'package:ml_preprocessing/ml_preprocessing.dart';
import 'package:flutter/services.dart' show rootBundle;

import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void trainModel() async {
    final rawCsvContent = await rootBundle.loadString('datasets/pima_indians_diabetes_database.csv');
    final samples = DataFrame.fromRawCsv(rawCsvContent);

    // === Prepare Dataset ===
    final targetColumnName = 'class variable (0 or 1)';

    final splits = splitData(samples, [0.7]);
    final validationData = splits[0];
    final testData = splits[1];

    // === Setup model selection algorithm ===
    final validator = CrossValidator.kFold(validationData, numberOfFolds: 5);

    final createClassifier = (DataFrame samples) =>
        LogisticRegressor(
          samples,
          targetColumnName,
          optimizerType: LinearOptimizerType.gradient,
          iterationsLimit: 90,
          learningRateType: LearningRateType.decreasingAdaptive,
          batchSize: samples.rows.length,
          probabilityThreshold: 0.7,
          collectLearningData: true,
        );

    // === Evaluate model performance ===
    final scores = await validator.evaluate(createClassifier, MetricType.accuracy);
    final accuracy = scores.mean();
    print('accuracy on k fold validation: ${accuracy.toStringAsFixed(2)}');

    final testSplits = splitData(testData, [0.8]);
    final classifier = createClassifier(testSplits[0]);
    final finalScore = classifier.assess(testSplits[1], MetricType.accuracy);

    print(finalScore.toStringAsFixed(2)); // approx. 0.75

    // === Write the model to JSON file ===
    var status = await Permission.manageExternalStorage.status;
    if (status.isDenied) {
      await Permission.manageExternalStorage.request();
    }

    await classifier.saveAsJson('diabetes_classifier.json');
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    trainModel();
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('Plugin example app'),
        ),
        body: new Center(
          child: new Column(children: <Widget>[
            new Text('Running'),
          ]),
        ),
      ),
    );
  }
}

If anyone got insight on what the problem is, I would really appreciate if you could help.
Cheers!

@gyrdym
Copy link
Owner

gyrdym commented Aug 11, 2021

@stephentandjiria Hi! It looks like you have some problems with permissions in your file system, the issue isn't connected to the library

@stephentandjiria
Copy link
Author

@gyrdym I am still unsure on what's going on, but thanks anyway for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants